guowenxue
2024-06-16 b96bc188ee00b4ccfb80e5af4fd2f67df22e88fc
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
8e6109 16 #INST_PATH=/tftp
G 17
18 # download taballs path
19 TARBALL_PATH=$PRJ_PATH/tarballs
849fbd 20
G 21 # config file path
22 CONF_FILE=$TOP_PATH/config.json
23
24 # shell script will exit once get command error
25 set -e
26
27 #+-------------------------+
28 #| Shell script functions  |
29 #+-------------------------+
30
31 function pr_error() {
32     echo -e "\033[40;31m $1 \033[0m"
33 }
34
35 function pr_warn() {
36     echo -e "\033[40;33m $1 \033[0m"
37 }
38
39 function pr_info() {
40     echo -e "\033[40;32m $1 \033[0m"
41 }
42
8e6109 43 # decompress a packet to destination path
G 44 function do_unpack()
45 {
46     tarball=$1
47     dstpath=`pwd`
48
49     if [[ $# == 2 ]] ; then
50         dstpath=$2
51     fi
52
53     pr_info "decompress $tarball => $dstpath"
54
55     mkdir -p $dstpath
56     case $tarball in
57         *.tar.gz)
58             tar -xzf $tarball -C $dstpath
59             ;;
60
61         *.tar.bz2)
62             tar -xjf $tarball -C $dstpath
63             ;;
64
65         *.tar.xz)
66             tar -xJf $tarball -C $dstpath
67             ;;
68
69         *.tar.zst)
70             tar -I zstd -xf $tarball -C $dstpath
71             ;;
72
73         *.tar)
74             tar -xf $tarball -C $dstpath
75             ;;
76
77         *.zip)
78             unzip -qo $tarball -d $dstpath
79             ;;
80
81         *)
82             pr_error "decompress Unsupport packet: $tarball"
83             return 1;
84             ;;
85     esac
86 }
87
849fbd 88 # select firmware version by BSP version
G 89 function export_fmver()
90 {
91     if [[ $BSP_VER =~ 6.1.36 ]] ;  then
92
93         export FMW_IMX=firmware-imx-8.21
94
435024 95     elif [[ $BSP_VER =~ 5.15.71 ]] ;  then
G 96
97         export FMW_IMX=firmware-imx-8.18
98
849fbd 99     fi
G 100
101     export FMWS="$FMW_IMX"
102     export FMW_URL=https://www.nxp.com/lgfiles/NMG/MAD/YOCTO/
103     export FMW_PATH=$PRJ_PATH/firmware/
104 }
105
106 # parser configure file and export environment variable
107 function export_env()
108 {
109     export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
110     export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
8e6109 111     export BSP_URL=`jq -r ".bsp.giturl" $CONF_FILE`
9987b6 112     export CROSS_COMPILE=`jq -r ".bsp.cortexAtool" $CONF_FILE`
G 113     export MCORE_COMPILE=`jq -r ".bsp.cortexMtool" $CONF_FILE`
849fbd 114
G 115     export BRANCH=$BSP_VER
116     export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
117     export ARCH=arm
118
d3809d 119     # i.MX6ULL only need uboot-imx
G 120     if [[ $BOARD =~ mx6ull ]] ; then
121         export SRCS="uboot-imx"
122         return ;
123     fi
124
125     export SRCS="uboot-imx imx-atf imx-mkimage"
849fbd 126     export_fmver
G 127
128     if [[ $BOARD =~ mx8mp ]] ; then
129
130         ATF_PLATFORM=imx8mp
131         IMX_BOOT_SOC_TARGET=iMX8MP
608938 132         IMXBOOT_TARGETS=flash_evk
G 133         IMXBOOT_DTB=imx8mp-evk.dtb
849fbd 134         MKIMG_BIN_PATH=$PRJ_PATH/imx-mkimage/iMX8M/
G 135
136     elif [[ $BOARD =~ mx8mm ]] ; then
137
138         ATF_PLATFORM=imx8mm
139         IMX_BOOT_SOC_TARGET=iMX8MM
140         IMXBOOT_TARGETS=flash_ddr4_evk
141         IMXBOOT_DTB=imx8mm-ddr4-evk.dtb
142         MKIMG_BIN_PATH=$PRJ_PATH/imx-mkimage/iMX8M/
eaa90c 143         IMXBOOT_ARGS="REV=A2"
849fbd 144
G 145     fi
146 }
147
148 function do_fetch()
149 {
150     cd $PRJ_PATH
151
152     for src in $SRCS
153     do
154         if [ -d $src ] ; then
155             pr_info "$src source code fetched already"
156             continue
157         fi
158
159         pr_info "start fetch $src source code"
8e6109 160
G 161         if [[ $BSP_URL =~ github.com ]] ; then
162             git clone $BSP_URL/$src.git -b $BRANCH --depth=1
163         else
164             mkdir -p $TARBALL_PATH
165
166             # Download source code packet
167             if [ ! -s $TARBALL_PATH/$src.tar.xz ] ; then
86063e 168                 wget $BSP_URL/imx/bsp/$BSP_VER/$src.tar.xz -P $TARBALL_PATH
8e6109 169             fi
G 170
171             # decompress source code packet
172             do_unpack $TARBALL_PATH/$src.tar.xz
173         fi
849fbd 174
G 175         # do patch if patch file exist
33a463 176         patch_file=$PRJ_PATH/patches/$BOARD/$src-$BSP_VER.patch
G 177         if [ -s $patch_file ] ; then
849fbd 178             pr_warn "do patch for $src now..."
G 179             cd $src
33a463 180             patch -p1 < $patch_file
849fbd 181             cd -
G 182         fi
183     done
184
d3809d 185     # i.MX6ULL only need uboot-imx
G 186     if [[ $BOARD =~ mx6ull ]] ; then
187         return ;
188     fi
849fbd 189
8e6109 190     # Download CortexM FreeRTOS SDK if needed
G 191     if [ "$BUILD_MCORE" == "yes" ] ; then
192
193         src=cortexm-sdk
194
195         cd $PRJ_PATH
196
197         if [ -d $src ] ; then
198             pr_info "$src source code fetched already"
199         else
200             pr_info "start fetch $src source code"
201
202             if [[ $BSP_URL =~ github.com ]] ; then
203                 pr_error "INFO: Please download $BOARD CortexM SDK from https://mcuxpresso.nxp.com by manual."
204             else
205                 pack=cortexm-sdk-${BOARD}
206
207                 # Download source code packet
208                 if [ ! -s $TARBALL_PATH/$pack.tar.gz ] ; then
209                     wget $BSP_URL/bsp/$BSP_VER/$pack.tar.gz -P $TARBALL_PATH
210                 fi
211
212                 # decompress source code packet
213                 do_unpack $TARBALL_PATH/$pack.tar.gz $PRJ_PATH/$src
214             fi
215         fi
216     fi
217
218     # Download firmware from NXP official URL
849fbd 219     mkdir -p $FMW_PATH && cd $FMW_PATH
G 220
221     for fmw in $FMWS
222     do
223         if [ -d $fmw ] ; then
224             pr_info "Firmware $fmw fetch already"
225             continue
226         fi
227         pr_info "start fetch $fmw firmware"
228         wget $FMW_URL/$fmw.bin
229
230         bash $fmw.bin --auto-accept > /dev/null 2>&1
231     done
232
233     rm -f *.bin
234 }
235
236 function build_atf()
237 {
238     SRC=imx-atf
239
240     pr_warn "start build $SRC"
241     cd $PRJ_PATH/${SRC}
242
243     make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} PLAT=$ATF_PLATFORM bl31
244
245     set -x
246     cp build/$ATF_PLATFORM/release/bl31.bin $MKIMG_BIN_PATH
247     set +x
248 }
249
250 # Cortex-M SDK download from https://mcuxpresso.nxp.com/ by manual
251 function build_cortexM()
252 {
8e6109 253     SRC=cortexm-sdk
9987b6 254
G 255     if [ "$BUILD_MCORE" != "yes" ] ; then
256         pr_warn "Skip build Cortex-M core SDK source code '$SRC'"
257         return 0;
258     fi
259
260     if [[ $BOARD =~ mx8mp ]] ; then
261         EVK=evkmimx8mp
262     fi
263     DEMO_PATH=boards/$EVK/multicore_examples/rpmsg_lite_str_echo_rtos/armgcc
849fbd 264     DEMO_BIN=release/rpmsg_lite_str_echo_rtos.bin
9987b6 265     MCORE_IMAGE=cortexM-${BOARD}.bin
849fbd 266
G 267     if [ ! -d $PRJ_PATH/$SRC ] ; then
9987b6 268         pr_error "INFO: Please download $BOARD SDK from https://mcuxpresso.nxp.com by manual and"
G 269         pr_error "      decompress it to folder '$PRJ_PATH/$SRC'"
270         exit;
849fbd 271     fi
G 272
273     pr_warn "start build $SRC"
274
9987b6 275     cd $PRJ_PATH/${SRC}/$DEMO_PATH
G 276     export ARMGCC_DIR=$(echo $MCORE_COMPILE | sed 's\/bin/.*\\')
849fbd 277
G 278     #bash clean.sh
279     if [ ! -s $DEMO_BIN ] ; then
280         bash build_release.sh
281     fi
282
283     set -x
9987b6 284     cp $DEMO_BIN $PRFX_PATH/$MCORE_IMAGE
849fbd 285     set +x
G 286 }
287
288 function build_uboot()
289 {
290     SRC=uboot-imx
33a463 291     patch_file=$PRJ_PATH/patches/$BOARD/$SRC-$BSP_VER.patch
G 292     defconfig=${BOARD}_defconfig
849fbd 293
G 294     pr_warn "start build $SRC"
295     cd $PRJ_PATH/${SRC}
296
33a463 297     # do patch if not patched
d451d0 298     if [ ! -s configs/$defconfig -a -s $patch_file ] ; then
33a463 299         pr_warn "do patch for $SRC now..."
G 300         patch -p1 < $patch_file
301     fi
302
849fbd 303     if [ ! -f .config ] ; then
G 304         make ARCH=arm ${BOARD}_defconfig
305     fi
306     make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm
307
d3809d 308     # i.MX6ULL only need uboot-imx
G 309     if [[ $BOARD =~ mx6ull ]] ; then
310         cp u-boot-dtb.imx u-boot-${BOARD}.imx
311         chmod a+x u-boot-${BOARD}.imx
312         set -x
313         cp u-boot-${BOARD}.imx $PRFX_PATH
314         set +x
9987b6 315         return 0;
d3809d 316     fi
G 317
849fbd 318     set -x
G 319     cp u-boot.bin $MKIMG_BIN_PATH
320     cp u-boot-nodtb.bin $MKIMG_BIN_PATH
321     cp spl/u-boot-spl.bin $MKIMG_BIN_PATH
322     cp arch/arm/dts/${BOARD}.dtb $MKIMG_BIN_PATH/$IMXBOOT_DTB
323     cp tools/mkimage $MKIMG_BIN_PATH/mkimage_uboot
324     set +x
325 }
326
327 # The diagram below illustrate a signed iMX8 flash.bin image layout:
328 #   reference: uboot-imx/doc/imx/habv4/guides/mx8m_secure_boot.txt
329 #
330 #                     +-----------------------------+
331 #                     |                             |
332 #                     |     *Signed HDMI/DP FW      |
333 #                     |                             |
334 #                     +-----------------------------+
335 #                     |           Padding           |
336 #             ------- +-----------------------------+ --------
337 #                 ^   |          IVT - SPL          |   ^
338 #          Signed |   +-----------------------------+   |
339 #           Data  |   |        u-boot-spl.bin       |   |
340 #                 |   |              +              |   |  SPL
341 #                 v   |           DDR FW            |   | Image
342 #             ------- +-----------------------------+   |
343 #                     |      CSF - SPL + DDR FW     |   v
344 #                     +-----------------------------+ --------
345 #                     |           Padding           |
346 #             ------- +-----------------------------+ --------
347 #          Signed ^   |          FDT - FIT          |   ^
348 #           Data  |   +-----------------------------+   |
349 #                 v   |          IVT - FIT          |   |
350 #             ------- +-----------------------------+   |
351 #                     |          CSF - FIT          |   |
352 #             ------- +-----------------------------+   |  FIT
353 #                 ^   |       u-boot-nodtb.bin      |   | Image
354 #                 |   +-----------------------------+   |
355 #          Signed |   |       OP-TEE (Optional)     |   |
356 #           Data  |   +-----------------------------+   |
357 #                 |   |        bl31.bin (ATF)       |   |
358 #                 |   +-----------------------------+   |
359 #                 v   |          u-boot.dtb         |   v
360 #             ------- +-----------------------------+ --------
361 #
362 #
363 # Reference: <<IMX_LINUX_USERS_GUIDE.pdf>> 4.5.13 How to build imx-boot image by using imx-mkimage
364
365 function build_imxboot()
366 {
367     SRC=imx-mkimage
368
369     pr_warn "start build $SRC"
370     cd $PRJ_PATH/${SRC}
371
372     # iMX8MP, iMX8MP
373     if [[ $BOARD =~ mx8m ]] ; then
374         pr_info "Copy DDR4 firmware to $MKIMG_BIN_PATH"
375
376         cp $FMW_PATH/firmware-imx-*/firmware/hdmi/cadence/signed_hdmi_imx8m.bin $MKIMG_BIN_PATH
eaa90c 377
G 378         # IGKBoard-IMX8MP Board
608938 379         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_imem_202006.bin $MKIMG_BIN_PATH
G 380         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_dmem_202006.bin $MKIMG_BIN_PATH
eaa90c 381
G 382         # STCRBoard-IMX8MM Board
383         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/ddr4_imem_[1-2]d*.bin $MKIMG_BIN_PATH
384         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/ddr4_dmem_[1-2]d*.bin $MKIMG_BIN_PATH
849fbd 385     fi
G 386
eaa90c 387     make SOC=$IMX_BOOT_SOC_TARGET $IMXBOOT_ARGS $IMXBOOT_TARGETS
849fbd 388
G 389     cp $MKIMG_BIN_PATH/flash.bin u-boot-${BOARD}.imx
390     chmod a+x u-boot-${BOARD}.imx
391
392     cp u-boot-${BOARD}.imx $PRFX_PATH
393 }
394
395 function do_build()
396 {
397     cd $PRJ_PATH
398     mkdir -p $PRFX_PATH
399
400     build_uboot
d3809d 401
G 402     # i.MX6ULL only need uboot-imx
403     if [[ ${BOARD} =~ mx6ull ]] ; then
404         return ;
405     fi
406
407     build_atf
849fbd 408     build_imxboot
8e6109 409     build_cortexM
849fbd 410 }
G 411
412 function do_install()
413 {
414     cd $PRJ_PATH
415
416     echo ""
417     pr_info "bootloader installed to '$PRFX_PATH'"
418     ls $PRFX_PATH && echo ""
419
420     if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then
421         pr_info "install bootloader to '$INST_PATH'"
9987b6 422         cp $PRFX_PATH/* $INST_PATH
33a463 423         #sz $PRFX_PATH/u-boot-${BOARD}.imx
849fbd 424     fi
G 425 }
426
427 function do_clean()
428 {
429     for d in $SRCS
430     do
431         rm -rf $PRJ_PATH/$d
432     done
433
434     rm -rf $PRJ_PATH/firmware
8e6109 435     rm -rf $PRJ_PATH/cortexm-sdk
G 436     rm -rf $PRJ_PATH/tarballs
849fbd 437     rm -rf $PRFX_PATH
G 438 }
439
440 #+-------------------------+
441 #| Shell script body entry |
442 #+-------------------------+
443
444 cd $PRJ_PATH
445
446 export_env
447
448 if [[ $# == 1 && $1 == -c ]] ;then
449     pr_warn "start clean bootloader"
450     do_clean
451     exit;
452 fi
453
454 pr_warn "start build bootloader for ${BOARD}"
455
456 do_fetch
457
458 do_build
459
460 do_install