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