guowenxue
2024-05-31 682e1c34704ad66e5bc18029afda4e045ae5bc6c
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/
143
144     fi
145 }
146
147 function do_fetch()
148 {
149     cd $PRJ_PATH
150
151     for src in $SRCS
152     do
153         if [ -d $src ] ; then
154             pr_info "$src source code fetched already"
155             continue
156         fi
157
158         pr_info "start fetch $src source code"
8e6109 159
G 160         if [[ $BSP_URL =~ github.com ]] ; then
161             git clone $BSP_URL/$src.git -b $BRANCH --depth=1
162         else
163             mkdir -p $TARBALL_PATH
164
165             # Download source code packet
166             if [ ! -s $TARBALL_PATH/$src.tar.xz ] ; then
86063e 167                 wget $BSP_URL/imx/bsp/$BSP_VER/$src.tar.xz -P $TARBALL_PATH
8e6109 168             fi
G 169
170             # decompress source code packet
171             do_unpack $TARBALL_PATH/$src.tar.xz
172         fi
849fbd 173
G 174         # do patch if patch file exist
33a463 175         patch_file=$PRJ_PATH/patches/$BOARD/$src-$BSP_VER.patch
G 176         if [ -s $patch_file ] ; then
849fbd 177             pr_warn "do patch for $src now..."
G 178             cd $src
33a463 179             patch -p1 < $patch_file
849fbd 180             cd -
G 181         fi
182     done
183
d3809d 184     # i.MX6ULL only need uboot-imx
G 185     if [[ $BOARD =~ mx6ull ]] ; then
186         return ;
187     fi
849fbd 188
8e6109 189     # Download CortexM FreeRTOS SDK if needed
G 190     if [ "$BUILD_MCORE" == "yes" ] ; then
191
192         src=cortexm-sdk
193
194         cd $PRJ_PATH
195
196         if [ -d $src ] ; then
197             pr_info "$src source code fetched already"
198         else
199             pr_info "start fetch $src source code"
200
201             if [[ $BSP_URL =~ github.com ]] ; then
202                 pr_error "INFO: Please download $BOARD CortexM SDK from https://mcuxpresso.nxp.com by manual."
203             else
204                 pack=cortexm-sdk-${BOARD}
205
206                 # Download source code packet
207                 if [ ! -s $TARBALL_PATH/$pack.tar.gz ] ; then
208                     wget $BSP_URL/bsp/$BSP_VER/$pack.tar.gz -P $TARBALL_PATH
209                 fi
210
211                 # decompress source code packet
212                 do_unpack $TARBALL_PATH/$pack.tar.gz $PRJ_PATH/$src
213             fi
214         fi
215     fi
216
217     # Download firmware from NXP official URL
849fbd 218     mkdir -p $FMW_PATH && cd $FMW_PATH
G 219
220     for fmw in $FMWS
221     do
222         if [ -d $fmw ] ; then
223             pr_info "Firmware $fmw fetch already"
224             continue
225         fi
226         pr_info "start fetch $fmw firmware"
227         wget $FMW_URL/$fmw.bin
228
229         bash $fmw.bin --auto-accept > /dev/null 2>&1
230     done
231
232     rm -f *.bin
233 }
234
235 function build_atf()
236 {
237     SRC=imx-atf
238
239     pr_warn "start build $SRC"
240     cd $PRJ_PATH/${SRC}
241
242     make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} PLAT=$ATF_PLATFORM bl31
243
244     set -x
245     cp build/$ATF_PLATFORM/release/bl31.bin $MKIMG_BIN_PATH
246     set +x
247 }
248
249 # Cortex-M SDK download from https://mcuxpresso.nxp.com/ by manual
250 function build_cortexM()
251 {
8e6109 252     SRC=cortexm-sdk
9987b6 253
G 254     if [ "$BUILD_MCORE" != "yes" ] ; then
255         pr_warn "Skip build Cortex-M core SDK source code '$SRC'"
256         return 0;
257     fi
258
259     if [[ $BOARD =~ mx8mp ]] ; then
260         EVK=evkmimx8mp
261     fi
262     DEMO_PATH=boards/$EVK/multicore_examples/rpmsg_lite_str_echo_rtos/armgcc
849fbd 263     DEMO_BIN=release/rpmsg_lite_str_echo_rtos.bin
9987b6 264     MCORE_IMAGE=cortexM-${BOARD}.bin
849fbd 265
G 266     if [ ! -d $PRJ_PATH/$SRC ] ; then
9987b6 267         pr_error "INFO: Please download $BOARD SDK from https://mcuxpresso.nxp.com by manual and"
G 268         pr_error "      decompress it to folder '$PRJ_PATH/$SRC'"
269         exit;
849fbd 270     fi
G 271
272     pr_warn "start build $SRC"
273
9987b6 274     cd $PRJ_PATH/${SRC}/$DEMO_PATH
G 275     export ARMGCC_DIR=$(echo $MCORE_COMPILE | sed 's\/bin/.*\\')
849fbd 276
G 277     #bash clean.sh
278     if [ ! -s $DEMO_BIN ] ; then
279         bash build_release.sh
280     fi
281
282     set -x
9987b6 283     cp $DEMO_BIN $PRFX_PATH/$MCORE_IMAGE
849fbd 284     set +x
G 285 }
286
287 function build_uboot()
288 {
289     SRC=uboot-imx
33a463 290     patch_file=$PRJ_PATH/patches/$BOARD/$SRC-$BSP_VER.patch
G 291     defconfig=${BOARD}_defconfig
849fbd 292
G 293     pr_warn "start build $SRC"
294     cd $PRJ_PATH/${SRC}
295
33a463 296     # do patch if not patched
d451d0 297     if [ ! -s configs/$defconfig -a -s $patch_file ] ; then
33a463 298         pr_warn "do patch for $SRC now..."
G 299         patch -p1 < $patch_file
300     fi
301
849fbd 302     if [ ! -f .config ] ; then
G 303         make ARCH=arm ${BOARD}_defconfig
304     fi
305     make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm
306
d3809d 307     # i.MX6ULL only need uboot-imx
G 308     if [[ $BOARD =~ mx6ull ]] ; then
309         cp u-boot-dtb.imx u-boot-${BOARD}.imx
310         chmod a+x u-boot-${BOARD}.imx
311         set -x
312         cp u-boot-${BOARD}.imx $PRFX_PATH
313         set +x
9987b6 314         return 0;
d3809d 315     fi
G 316
849fbd 317     set -x
G 318     cp u-boot.bin $MKIMG_BIN_PATH
319     cp u-boot-nodtb.bin $MKIMG_BIN_PATH
320     cp spl/u-boot-spl.bin $MKIMG_BIN_PATH
321     cp arch/arm/dts/${BOARD}.dtb $MKIMG_BIN_PATH/$IMXBOOT_DTB
322     cp tools/mkimage $MKIMG_BIN_PATH/mkimage_uboot
323     set +x
324 }
325
326 # The diagram below illustrate a signed iMX8 flash.bin image layout:
327 #   reference: uboot-imx/doc/imx/habv4/guides/mx8m_secure_boot.txt
328 #
329 #                     +-----------------------------+
330 #                     |                             |
331 #                     |     *Signed HDMI/DP FW      |
332 #                     |                             |
333 #                     +-----------------------------+
334 #                     |           Padding           |
335 #             ------- +-----------------------------+ --------
336 #                 ^   |          IVT - SPL          |   ^
337 #          Signed |   +-----------------------------+   |
338 #           Data  |   |        u-boot-spl.bin       |   |
339 #                 |   |              +              |   |  SPL
340 #                 v   |           DDR FW            |   | Image
341 #             ------- +-----------------------------+   |
342 #                     |      CSF - SPL + DDR FW     |   v
343 #                     +-----------------------------+ --------
344 #                     |           Padding           |
345 #             ------- +-----------------------------+ --------
346 #          Signed ^   |          FDT - FIT          |   ^
347 #           Data  |   +-----------------------------+   |
348 #                 v   |          IVT - FIT          |   |
349 #             ------- +-----------------------------+   |
350 #                     |          CSF - FIT          |   |
351 #             ------- +-----------------------------+   |  FIT
352 #                 ^   |       u-boot-nodtb.bin      |   | Image
353 #                 |   +-----------------------------+   |
354 #          Signed |   |       OP-TEE (Optional)     |   |
355 #           Data  |   +-----------------------------+   |
356 #                 |   |        bl31.bin (ATF)       |   |
357 #                 |   +-----------------------------+   |
358 #                 v   |          u-boot.dtb         |   v
359 #             ------- +-----------------------------+ --------
360 #
361 #
362 # Reference: <<IMX_LINUX_USERS_GUIDE.pdf>> 4.5.13 How to build imx-boot image by using imx-mkimage
363
364 function build_imxboot()
365 {
366     SRC=imx-mkimage
367
368     pr_warn "start build $SRC"
369     cd $PRJ_PATH/${SRC}
370
371     # iMX8MP, iMX8MP
372     if [[ $BOARD =~ mx8m ]] ; then
373         pr_info "Copy DDR4 firmware to $MKIMG_BIN_PATH"
374
375         cp $FMW_PATH/firmware-imx-*/firmware/hdmi/cadence/signed_hdmi_imx8m.bin $MKIMG_BIN_PATH
608938 376         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_imem_202006.bin $MKIMG_BIN_PATH
G 377         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_dmem_202006.bin $MKIMG_BIN_PATH
849fbd 378     fi
G 379
608938 380     make SOC=$IMX_BOOT_SOC_TARGET $IMXBOOT_TARGETS
849fbd 381
G 382     cp $MKIMG_BIN_PATH/flash.bin u-boot-${BOARD}.imx
383     chmod a+x u-boot-${BOARD}.imx
384
385     cp u-boot-${BOARD}.imx $PRFX_PATH
386 }
387
388 function do_build()
389 {
390     cd $PRJ_PATH
391     mkdir -p $PRFX_PATH
392
393     build_uboot
d3809d 394
G 395     # i.MX6ULL only need uboot-imx
396     if [[ ${BOARD} =~ mx6ull ]] ; then
397         return ;
398     fi
399
400     build_atf
849fbd 401     build_imxboot
8e6109 402     build_cortexM
849fbd 403 }
G 404
405 function do_install()
406 {
407     cd $PRJ_PATH
408
409     echo ""
410     pr_info "bootloader installed to '$PRFX_PATH'"
411     ls $PRFX_PATH && echo ""
412
413     if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then
414         pr_info "install bootloader to '$INST_PATH'"
9987b6 415         cp $PRFX_PATH/* $INST_PATH
33a463 416         #sz $PRFX_PATH/u-boot-${BOARD}.imx
849fbd 417     fi
G 418 }
419
420 function do_clean()
421 {
422     for d in $SRCS
423     do
424         rm -rf $PRJ_PATH/$d
425     done
426
427     rm -rf $PRJ_PATH/firmware
8e6109 428     rm -rf $PRJ_PATH/cortexm-sdk
G 429     rm -rf $PRJ_PATH/tarballs
849fbd 430     rm -rf $PRFX_PATH
G 431 }
432
433 #+-------------------------+
434 #| Shell script body entry |
435 #+-------------------------+
436
437 cd $PRJ_PATH
438
439 export_env
440
441 if [[ $# == 1 && $1 == -c ]] ;then
442     pr_warn "start clean bootloader"
443     do_clean
444     exit;
445 fi
446
447 pr_warn "start build bootloader for ${BOARD}"
448
449 do_fetch
450
451 do_build
452
453 do_install