guowenxue
2024-01-25 9987b6f2e72e867f093623d03272638c178bf016
commit | author | age
849fbd 1 #!/bin/bash
G 2
9987b6 3 # Build Cortex-M FreeRTOS SDK or not
G 4 #BUILD_MCORE=yes
5
849fbd 6 # this project absolute path
G 7 PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
8
9 # top project absolute path
10 TOP_PATH=$(realpath $PRJ_PATH/..)
11
12 # binaries build prefix install path
13 PRFX_PATH=$PRJ_PATH/install
14
15 # binaries finally install path if needed
16 INST_PATH=/tftp
17
18 # config file path
19 CONF_FILE=$TOP_PATH/config.json
20
21 # shell script will exit once get command error
22 set -e
23
24 #+-------------------------+
25 #| Shell script functions  |
26 #+-------------------------+
27
28 function pr_error() {
29     echo -e "\033[40;31m $1 \033[0m"
30 }
31
32 function pr_warn() {
33     echo -e "\033[40;33m $1 \033[0m"
34 }
35
36 function pr_info() {
37     echo -e "\033[40;32m $1 \033[0m"
38 }
39
40 # select firmware version by BSP version
41 function export_fmver()
42 {
43     if [[ $BSP_VER =~ 6.1.36 ]] ;  then
44
45         export FMW_IMX=firmware-imx-8.21
46
47     fi
48
49     export FMWS="$FMW_IMX"
50     export FMW_URL=https://www.nxp.com/lgfiles/NMG/MAD/YOCTO/
51     export FMW_PATH=$PRJ_PATH/firmware/
52 }
53
54 # parser configure file and export environment variable
55 function export_env()
56 {
57     export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
58     export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
59     export GIT_URL=`jq -r ".bsp.giturl" $CONF_FILE | tr 'A-Z' 'a-z'`
9987b6 60     export CROSS_COMPILE=`jq -r ".bsp.cortexAtool" $CONF_FILE`
G 61     export MCORE_COMPILE=`jq -r ".bsp.cortexMtool" $CONF_FILE`
849fbd 62
G 63     export BRANCH=$BSP_VER
64     export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
65     export ARCH=arm
66
d3809d 67     # i.MX6ULL only need uboot-imx
G 68     if [[ $BOARD =~ mx6ull ]] ; then
69         export SRCS="uboot-imx"
70         return ;
71     fi
72
73     export SRCS="uboot-imx imx-atf imx-mkimage"
849fbd 74     export_fmver
G 75
76     if [[ $BOARD =~ mx8mp ]] ; then
77
78         ATF_PLATFORM=imx8mp
79         IMX_BOOT_SOC_TARGET=iMX8MP
608938 80         IMXBOOT_TARGETS=flash_evk
G 81         IMXBOOT_DTB=imx8mp-evk.dtb
849fbd 82         MKIMG_BIN_PATH=$PRJ_PATH/imx-mkimage/iMX8M/
G 83
84     elif [[ $BOARD =~ mx8mm ]] ; then
85
86         ATF_PLATFORM=imx8mm
87         IMX_BOOT_SOC_TARGET=iMX8MM
88         IMXBOOT_TARGETS=flash_ddr4_evk
89         IMXBOOT_DTB=imx8mm-ddr4-evk.dtb
90         MKIMG_BIN_PATH=$PRJ_PATH/imx-mkimage/iMX8M/
91
92     fi
93 }
94
95 function do_fetch()
96 {
97     cd $PRJ_PATH
98
99     for src in $SRCS
100     do
101         if [ -d $src ] ; then
102             pr_info "$src source code fetched already"
103             continue
104         fi
105
106         pr_info "start fetch $src source code"
107         git clone $GIT_URL/$src.git -b $BRANCH --depth=1
108
109         # do patch if patch file exist
33a463 110         patch_file=$PRJ_PATH/patches/$BOARD/$src-$BSP_VER.patch
G 111         if [ -s $patch_file ] ; then
849fbd 112             pr_warn "do patch for $src now..."
G 113             cd $src
33a463 114             patch -p1 < $patch_file
849fbd 115             cd -
G 116         fi
117     done
118
d3809d 119     # i.MX6ULL only need uboot-imx
G 120     if [[ $BOARD =~ mx6ull ]] ; then
121         return ;
122     fi
849fbd 123
G 124     mkdir -p $FMW_PATH && cd $FMW_PATH
125
126     for fmw in $FMWS
127     do
128         if [ -d $fmw ] ; then
129             pr_info "Firmware $fmw fetch already"
130             continue
131         fi
132         pr_info "start fetch $fmw firmware"
133         wget $FMW_URL/$fmw.bin
134
135         bash $fmw.bin --auto-accept > /dev/null 2>&1
136     done
137
138     rm -f *.bin
139 }
140
141 function build_atf()
142 {
143     SRC=imx-atf
144
145     pr_warn "start build $SRC"
146     cd $PRJ_PATH/${SRC}
147
148     make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} PLAT=$ATF_PLATFORM bl31
149
150     set -x
151     cp build/$ATF_PLATFORM/release/bl31.bin $MKIMG_BIN_PATH
152     set +x
153 }
154
155 # Cortex-M SDK download from https://mcuxpresso.nxp.com/ by manual
156 function build_cortexM()
157 {
158     SRC=mcore-sdk
9987b6 159
G 160     if [ "$BUILD_MCORE" != "yes" ] ; then
161         pr_warn "Skip build Cortex-M core SDK source code '$SRC'"
162         return 0;
163     fi
164
165     if [[ $BOARD =~ mx8mp ]] ; then
166         EVK=evkmimx8mp
167     fi
168     DEMO_PATH=boards/$EVK/multicore_examples/rpmsg_lite_str_echo_rtos/armgcc
849fbd 169     DEMO_BIN=release/rpmsg_lite_str_echo_rtos.bin
9987b6 170     MCORE_IMAGE=cortexM-${BOARD}.bin
849fbd 171
G 172     if [ ! -d $PRJ_PATH/$SRC ] ; then
9987b6 173         pr_error "INFO: Please download $BOARD SDK from https://mcuxpresso.nxp.com by manual and"
G 174         pr_error "      decompress it to folder '$PRJ_PATH/$SRC'"
175         exit;
849fbd 176     fi
G 177
178     pr_warn "start build $SRC"
179
9987b6 180     cd $PRJ_PATH/${SRC}/$DEMO_PATH
G 181     export ARMGCC_DIR=$(echo $MCORE_COMPILE | sed 's\/bin/.*\\')
849fbd 182
G 183     #bash clean.sh
184     if [ ! -s $DEMO_BIN ] ; then
185         bash build_release.sh
186     fi
187
188     set -x
9987b6 189     cp $DEMO_BIN $PRFX_PATH/$MCORE_IMAGE
849fbd 190     set +x
G 191 }
192
193 function build_uboot()
194 {
195     SRC=uboot-imx
33a463 196     patch_file=$PRJ_PATH/patches/$BOARD/$SRC-$BSP_VER.patch
G 197     defconfig=${BOARD}_defconfig
849fbd 198
G 199     pr_warn "start build $SRC"
200     cd $PRJ_PATH/${SRC}
201
33a463 202     # do patch if not patched
d451d0 203     if [ ! -s configs/$defconfig -a -s $patch_file ] ; then
33a463 204         pr_warn "do patch for $SRC now..."
G 205         patch -p1 < $patch_file
206     fi
207
849fbd 208     if [ ! -f .config ] ; then
G 209         make ARCH=arm ${BOARD}_defconfig
210     fi
211     make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm
212
d3809d 213     # i.MX6ULL only need uboot-imx
G 214     if [[ $BOARD =~ mx6ull ]] ; then
215         cp u-boot-dtb.imx u-boot-${BOARD}.imx
216         chmod a+x u-boot-${BOARD}.imx
217         set -x
218         cp u-boot-${BOARD}.imx $PRFX_PATH
219         set +x
9987b6 220         return 0;
d3809d 221     fi
G 222
849fbd 223     set -x
G 224     cp u-boot.bin $MKIMG_BIN_PATH
225     cp u-boot-nodtb.bin $MKIMG_BIN_PATH
226     cp spl/u-boot-spl.bin $MKIMG_BIN_PATH
227     cp arch/arm/dts/${BOARD}.dtb $MKIMG_BIN_PATH/$IMXBOOT_DTB
228     cp tools/mkimage $MKIMG_BIN_PATH/mkimage_uboot
229     set +x
230 }
231
232 # The diagram below illustrate a signed iMX8 flash.bin image layout:
233 #   reference: uboot-imx/doc/imx/habv4/guides/mx8m_secure_boot.txt
234 #
235 #                     +-----------------------------+
236 #                     |                             |
237 #                     |     *Signed HDMI/DP FW      |
238 #                     |                             |
239 #                     +-----------------------------+
240 #                     |           Padding           |
241 #             ------- +-----------------------------+ --------
242 #                 ^   |          IVT - SPL          |   ^
243 #          Signed |   +-----------------------------+   |
244 #           Data  |   |        u-boot-spl.bin       |   |
245 #                 |   |              +              |   |  SPL
246 #                 v   |           DDR FW            |   | Image
247 #             ------- +-----------------------------+   |
248 #                     |      CSF - SPL + DDR FW     |   v
249 #                     +-----------------------------+ --------
250 #                     |           Padding           |
251 #             ------- +-----------------------------+ --------
252 #          Signed ^   |          FDT - FIT          |   ^
253 #           Data  |   +-----------------------------+   |
254 #                 v   |          IVT - FIT          |   |
255 #             ------- +-----------------------------+   |
256 #                     |          CSF - FIT          |   |
257 #             ------- +-----------------------------+   |  FIT
258 #                 ^   |       u-boot-nodtb.bin      |   | Image
259 #                 |   +-----------------------------+   |
260 #          Signed |   |       OP-TEE (Optional)     |   |
261 #           Data  |   +-----------------------------+   |
262 #                 |   |        bl31.bin (ATF)       |   |
263 #                 |   +-----------------------------+   |
264 #                 v   |          u-boot.dtb         |   v
265 #             ------- +-----------------------------+ --------
266 #
267 #
268 # Reference: <<IMX_LINUX_USERS_GUIDE.pdf>> 4.5.13 How to build imx-boot image by using imx-mkimage
269
270 function build_imxboot()
271 {
272     SRC=imx-mkimage
273
274     pr_warn "start build $SRC"
275     cd $PRJ_PATH/${SRC}
276
277     # iMX8MP, iMX8MP
278     if [[ $BOARD =~ mx8m ]] ; then
279         pr_info "Copy DDR4 firmware to $MKIMG_BIN_PATH"
280
281         cp $FMW_PATH/firmware-imx-*/firmware/hdmi/cadence/signed_hdmi_imx8m.bin $MKIMG_BIN_PATH
608938 282         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_imem_202006.bin $MKIMG_BIN_PATH
G 283         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_dmem_202006.bin $MKIMG_BIN_PATH
849fbd 284     fi
G 285
608938 286     make SOC=$IMX_BOOT_SOC_TARGET $IMXBOOT_TARGETS
849fbd 287
G 288     cp $MKIMG_BIN_PATH/flash.bin u-boot-${BOARD}.imx
289     chmod a+x u-boot-${BOARD}.imx
290
291     cp u-boot-${BOARD}.imx $PRFX_PATH
292 }
293
294 function do_build()
295 {
296     cd $PRJ_PATH
297     mkdir -p $PRFX_PATH
298
299     build_uboot
d3809d 300
G 301     # i.MX6ULL only need uboot-imx
302     if [[ ${BOARD} =~ mx6ull ]] ; then
303         return ;
304     fi
305
306     build_atf
9987b6 307     build_cortexM
849fbd 308     build_imxboot
G 309 }
310
311 function do_install()
312 {
313     cd $PRJ_PATH
314
315     echo ""
316     pr_info "bootloader installed to '$PRFX_PATH'"
317     ls $PRFX_PATH && echo ""
318
319     if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then
320         pr_info "install bootloader to '$INST_PATH'"
9987b6 321         cp $PRFX_PATH/* $INST_PATH
33a463 322         #sz $PRFX_PATH/u-boot-${BOARD}.imx
849fbd 323     fi
G 324 }
325
326 function do_clean()
327 {
328     for d in $SRCS
329     do
330         rm -rf $PRJ_PATH/$d
331     done
332
333     rm -rf $PRJ_PATH/firmware
334     rm -rf $PRFX_PATH
335 }
336
337 #+-------------------------+
338 #| Shell script body entry |
339 #+-------------------------+
340
341 cd $PRJ_PATH
342
343 export_env
344
345 if [[ $# == 1 && $1 == -c ]] ;then
346     pr_warn "start clean bootloader"
347     do_clean
348     exit;
349 fi
350
351 pr_warn "start build bootloader for ${BOARD}"
352
353 do_fetch
354
355 do_build
356
357 do_install