guowenxue
2023-12-12 849fbd3ab201e2e4694d302787fed48fb2172898
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 SRCS="imx-atf uboot-imx imx-mkimage"
61     export BRANCH=$BSP_VER
62     export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
63     export ARCH=arm
64
65     export_fmver
66     export SRCS="imx-atf uboot-imx imx-mkimage"
67
68     if [[ $BOARD =~ mx8mp ]] ; then
69
70         ATF_PLATFORM=imx8mp
71         IMX_BOOT_SOC_TARGET=iMX8MP
72         IMXBOOT_TARGETS=flash_ddr4_evk
73         IMXBOOT_DTB=imx8mp-ddr4-evk.dtb
74         MKIMG_BIN_PATH=$PRJ_PATH/imx-mkimage/iMX8M/
75
76     elif [[ $BOARD =~ mx8mm ]] ; then
77
78         ATF_PLATFORM=imx8mm
79         IMX_BOOT_SOC_TARGET=iMX8MM
80         IMXBOOT_TARGETS=flash_ddr4_evk
81         IMXBOOT_DTB=imx8mm-ddr4-evk.dtb
82         MKIMG_BIN_PATH=$PRJ_PATH/imx-mkimage/iMX8M/
83
84     fi
85 }
86
87 function do_fetch()
88 {
89     cd $PRJ_PATH
90
91     for src in $SRCS
92     do
93         if [ -d $src ] ; then
94             pr_info "$src source code fetched already"
95             continue
96         fi
97
98         pr_info "start fetch $src source code"
99         git clone $GIT_URL/$src.git -b $BRANCH --depth=1
100
101         # do patch if patch file exist
102         PATCH_FILE=$PRJ_PATH/patches/$BOARD/$src-$BSP_VER.patch
103         if [ -s $PATCH_FILE ] ; then
104             pr_warn "do patch for $src now..."
105             cd $src
106             patch -p1 < $PATCH_FILE
107             cd -
108         fi
109
110     done
111
112
113     mkdir -p $FMW_PATH && cd $FMW_PATH
114
115     for fmw in $FMWS
116     do
117         if [ -d $fmw ] ; then
118             pr_info "Firmware $fmw fetch already"
119             continue
120         fi
121         pr_info "start fetch $fmw firmware"
122         wget $FMW_URL/$fmw.bin
123
124         bash $fmw.bin --auto-accept > /dev/null 2>&1
125     done
126
127     rm -f *.bin
128 }
129
130 function build_atf()
131 {
132     SRC=imx-atf
133
134     pr_warn "start build $SRC"
135     cd $PRJ_PATH/${SRC}
136
137     make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} PLAT=$ATF_PLATFORM bl31
138
139     set -x
140     cp build/$ATF_PLATFORM/release/bl31.bin $MKIMG_BIN_PATH
141     set +x
142 }
143
144 # Cortex-M SDK download from https://mcuxpresso.nxp.com/ by manual
145 function build_cortexM()
146 {
147     SRC=mcore-sdk
148     DEMO_PATH=boards/$MCORE_BOARD/multicore_examples/rpmsg_lite_str_echo_rtos/armgcc
149     DEMO_BIN=release/rpmsg_lite_str_echo_rtos.bin
150     export ARMGCC_DIR=$(echo $MCORE_COMPILE | sed 's\/bin/.*\\')
151
152     if [ ! -d $PRJ_PATH/$SRC ] ; then
153         if [ "$MCORE_BUILD" == "yes" ] ; then
154             pr_error "INFO: Please download $BOARD SDK from https://mcuxpresso.nxp.com"
155             pr_error "      by manual and decompress it to folder '$SRC'"
156             exit;
157         else
158             pr_warn "Skip build Cortex-M core SDK source code '$SRC'"
159             return ;
160         fi
161     fi
162
163     pr_warn "start build $SRC"
164
165     cd $PRJ_PATH/${SRC}
166     cd $DEMO_PATH
167
168     #bash clean.sh
169     if [ ! -s $DEMO_BIN ] ; then
170         bash build_release.sh
171     fi
172
173     set -x
174     cp $DEMO_BIN $MKIMG_BIN_PATH/$MCORE_IMAGE
175     set +x
176 }
177
178 function build_uboot()
179 {
180     SRC=uboot-imx
181
182     pr_warn "start build $SRC"
183     cd $PRJ_PATH/${SRC}
184
185     if [ ! -f .config ] ; then
186         make ARCH=arm ${BOARD}_defconfig
187     fi
188
189     make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm
190
191     set -x
192     cp u-boot.bin $MKIMG_BIN_PATH
193     cp u-boot-nodtb.bin $MKIMG_BIN_PATH
194     cp spl/u-boot-spl.bin $MKIMG_BIN_PATH
195     cp arch/arm/dts/${BOARD}.dtb $MKIMG_BIN_PATH/$IMXBOOT_DTB
196     cp tools/mkimage $MKIMG_BIN_PATH/mkimage_uboot
197     set +x
198 }
199
200 # The diagram below illustrate a signed iMX8 flash.bin image layout:
201 #   reference: uboot-imx/doc/imx/habv4/guides/mx8m_secure_boot.txt
202 #
203 #                     +-----------------------------+
204 #                     |                             |
205 #                     |     *Signed HDMI/DP FW      |
206 #                     |                             |
207 #                     +-----------------------------+
208 #                     |           Padding           |
209 #             ------- +-----------------------------+ --------
210 #                 ^   |          IVT - SPL          |   ^
211 #          Signed |   +-----------------------------+   |
212 #           Data  |   |        u-boot-spl.bin       |   |
213 #                 |   |              +              |   |  SPL
214 #                 v   |           DDR FW            |   | Image
215 #             ------- +-----------------------------+   |
216 #                     |      CSF - SPL + DDR FW     |   v
217 #                     +-----------------------------+ --------
218 #                     |           Padding           |
219 #             ------- +-----------------------------+ --------
220 #          Signed ^   |          FDT - FIT          |   ^
221 #           Data  |   +-----------------------------+   |
222 #                 v   |          IVT - FIT          |   |
223 #             ------- +-----------------------------+   |
224 #                     |          CSF - FIT          |   |
225 #             ------- +-----------------------------+   |  FIT
226 #                 ^   |       u-boot-nodtb.bin      |   | Image
227 #                 |   +-----------------------------+   |
228 #          Signed |   |       OP-TEE (Optional)     |   |
229 #           Data  |   +-----------------------------+   |
230 #                 |   |        bl31.bin (ATF)       |   |
231 #                 |   +-----------------------------+   |
232 #                 v   |          u-boot.dtb         |   v
233 #             ------- +-----------------------------+ --------
234 #
235 #
236 # Reference: <<IMX_LINUX_USERS_GUIDE.pdf>> 4.5.13 How to build imx-boot image by using imx-mkimage
237
238 function build_imxboot()
239 {
240     SRC=imx-mkimage
241
242     pr_warn "start build $SRC"
243     cd $PRJ_PATH/${SRC}
244
245     # iMX8MP, iMX8MP
246     if [[ $BOARD =~ mx8m ]] ; then
247         pr_info "Copy DDR4 firmware to $MKIMG_BIN_PATH"
248
249         cp $FMW_PATH/firmware-imx-*/firmware/hdmi/cadence/signed_hdmi_imx8m.bin $MKIMG_BIN_PATH
250         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/ddr4_imem_1d*.bin $MKIMG_BIN_PATH
251         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/ddr4_dmem_1d*.bin $MKIMG_BIN_PATH
252         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/ddr4_imem_2d*.bin $MKIMG_BIN_PATH
253         cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/ddr4_dmem_2d*.bin $MKIMG_BIN_PATH
254     fi
255
256     make SOC=$IMX_BOOT_SOC_TARGET REV=A2 $IMXBOOT_TARGETS
257
258     cp $MKIMG_BIN_PATH/flash.bin u-boot-${BOARD}.imx
259     chmod a+x u-boot-${BOARD}.imx
260
261     cp u-boot-${BOARD}.imx $PRFX_PATH
262 }
263
264 function do_build()
265 {
266     cd $PRJ_PATH
267     mkdir -p $PRFX_PATH
268
269     build_atf
270     #build_cortexM
271     build_uboot
272     build_imxboot
273 }
274
275 function do_install()
276 {
277     cd $PRJ_PATH
278
279     echo ""
280     pr_info "bootloader installed to '$PRFX_PATH'"
281     ls $PRFX_PATH && echo ""
282
283     if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then
284         pr_info "install bootloader to '$INST_PATH'"
285         cp $PRFX_PATH/u-boot-${BOARD}.imx $INST_PATH
286     fi
287 }
288
289 function do_clean()
290 {
291     for d in $SRCS
292     do
293         rm -rf $PRJ_PATH/$d
294     done
295
296     rm -rf $PRJ_PATH/firmware
297     rm -rf $PRFX_PATH
298 }
299
300 #+-------------------------+
301 #| Shell script body entry |
302 #+-------------------------+
303
304 cd $PRJ_PATH
305
306 export_env
307
308 if [[ $# == 1 && $1 == -c ]] ;then
309     pr_warn "start clean bootloader"
310     do_clean
311     exit;
312 fi
313
314 pr_warn "start build bootloader for ${BOARD}"
315
316 do_fetch
317
318 do_build
319
320 do_install