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