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