guowenxue
2024-09-26 2b179f80693db10cb5a93ee649917ae6988feaf2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/bin/bash
 
# Build Cortex-M FreeRTOS SDK or not
#BUILD_MCORE=yes
 
# this project absolute path
PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
 
# top project absolute path
TOP_PATH=$(realpath $PRJ_PATH/..)
 
# binaries build prefix install path
PRFX_PATH=$PRJ_PATH/install
 
# binaries finally install path if needed
#INST_PATH=/tftp
 
# download taballs path
TARBALL_PATH=$PRJ_PATH/tarballs
 
# config file path
CONF_FILE=$TOP_PATH/config.json
 
# shell script will exit once get command error
set -e
 
#+-------------------------+
#| Shell script functions  |
#+-------------------------+
 
function pr_error() {
    echo -e "\033[40;31m $1 \033[0m"
}
 
function pr_warn() {
    echo -e "\033[40;33m $1 \033[0m"
}
 
function pr_info() {
    echo -e "\033[40;32m $1 \033[0m"
}
 
# decompress a packet to destination path
function do_unpack()
{
    tarball=$1
    dstpath=`pwd`
 
    if [[ $# == 2 ]] ; then
        dstpath=$2
    fi
 
    pr_info "decompress $tarball => $dstpath"
 
    mkdir -p $dstpath
    case $tarball in
        *.tar.gz)
            tar -xzf $tarball -C $dstpath
            ;;
 
        *.tar.bz2)
            tar -xjf $tarball -C $dstpath
            ;;
 
        *.tar.xz)
            tar -xJf $tarball -C $dstpath
            ;;
 
        *.tar.zst)
            tar -I zstd -xf $tarball -C $dstpath
            ;;
 
        *.tar)
            tar -xf $tarball -C $dstpath
            ;;
 
        *.zip)
            unzip -qo $tarball -d $dstpath
            ;;
 
        *)
            pr_error "decompress Unsupport packet: $tarball"
            return 1;
            ;;
    esac
}
 
# select firmware version by BSP version
function export_fmver()
{
    if [[ $BSP_VER =~ 6.1.36 ]] ;  then
 
        export FMW_IMX=firmware-imx-8.21
 
    elif [[ $BSP_VER =~ 5.15.71 ]] ;  then
 
        export FMW_IMX=firmware-imx-8.18
 
    fi
 
    export FMWS="$FMW_IMX"
    export FMW_URL=https://www.nxp.com/lgfiles/NMG/MAD/YOCTO/
    export FMW_PATH=$PRJ_PATH/firmware/
}
 
# parser configure file and export environment variable
function export_env()
{
    export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
    export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
    export BSP_URL=`jq -r ".bsp.giturl" $CONF_FILE`
    export CROSS_COMPILE=`jq -r ".bsp.cortexAtool" $CONF_FILE`
    export MCORE_COMPILE=`jq -r ".bsp.cortexMtool" $CONF_FILE`
 
    export BRANCH=$BSP_VER
    export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
    export ARCH=arm
 
    # i.MX6ULL only need uboot-imx
    if [[ $BOARD =~ mx6ull ]] ; then
        export SRCS="uboot-imx"
        return ;
    fi
 
    export SRCS="uboot-imx imx-atf imx-mkimage"
    export_fmver
 
    if [[ $BOARD =~ mx8mp ]] ; then
 
        ATF_PLATFORM=imx8mp
        IMX_BOOT_SOC_TARGET=iMX8MP
        IMXBOOT_TARGETS=flash_evk
        IMXBOOT_DTB=imx8mp-evk.dtb
        MKIMG_BIN_PATH=$PRJ_PATH/imx-mkimage/iMX8M/
 
    elif [[ $BOARD =~ mx8mm ]] ; then
 
        ATF_PLATFORM=imx8mm
        IMX_BOOT_SOC_TARGET=iMX8MM
        IMXBOOT_TARGETS=flash_ddr4_evk
        IMXBOOT_DTB=imx8mm-ddr4-evk.dtb
        MKIMG_BIN_PATH=$PRJ_PATH/imx-mkimage/iMX8M/
        IMXBOOT_ARGS="REV=A2"
 
    fi
}
 
function do_fetch()
{
    cd $PRJ_PATH
 
    for src in $SRCS
    do
        if [ -d $src ] ; then
            pr_info "$src source code fetched already"
            continue
        fi
 
        pr_info "start fetch $src source code"
 
        if [[ $BSP_URL =~ github.com ]] ; then
            git clone $BSP_URL/$src.git -b $BRANCH --depth=1
        else
            mkdir -p $TARBALL_PATH
 
            # Download source code packet
            if [ ! -s $TARBALL_PATH/$src.tar.xz ] ; then
                wget $BSP_URL/imx/bsp/$BSP_VER/$src.tar.xz -P $TARBALL_PATH
            fi
 
            # decompress source code packet
            do_unpack $TARBALL_PATH/$src.tar.xz
        fi
 
        # do patch if patch file exist
        patch_file=$PRJ_PATH/patches/$BOARD/$src-$BSP_VER.patch
        if [ -s $patch_file ] ; then
            pr_warn "do patch for $src now..."
            cd $src
            patch -p1 < $patch_file
            cd -
        fi
    done
 
    # i.MX6ULL only need uboot-imx
    if [[ $BOARD =~ mx6ull ]] ; then
        return ;
    fi
 
    # Download CortexM FreeRTOS SDK if needed
    if [ "$BUILD_MCORE" == "yes" ] ; then
 
        src=cortexm-sdk
 
        cd $PRJ_PATH
 
        if [ -d $src ] ; then
            pr_info "$src source code fetched already"
        else
            pr_info "start fetch $src source code"
 
            if [[ $BSP_URL =~ github.com ]] ; then
                pr_error "INFO: Please download $BOARD CortexM SDK from https://mcuxpresso.nxp.com by manual."
            else
                pack=cortexm-sdk-${BOARD}
 
                # Download source code packet
                if [ ! -s $TARBALL_PATH/$pack.tar.gz ] ; then
                    wget $BSP_URL/bsp/$BSP_VER/$pack.tar.gz -P $TARBALL_PATH
                fi
 
                # decompress source code packet
                do_unpack $TARBALL_PATH/$pack.tar.gz $PRJ_PATH/$src
            fi
        fi
    fi
 
    # Download firmware from NXP official URL
    mkdir -p $FMW_PATH && cd $FMW_PATH
 
    for fmw in $FMWS
    do
        if [ -d $fmw ] ; then
            pr_info "Firmware $fmw fetch already"
            continue
        fi
        pr_info "start fetch $fmw firmware"
        wget $FMW_URL/$fmw.bin
 
        bash $fmw.bin --auto-accept > /dev/null 2>&1
    done
 
    rm -f *.bin
}
 
function build_atf()
{
    SRC=imx-atf
 
    pr_warn "start build $SRC"
    cd $PRJ_PATH/${SRC}
 
    make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} PLAT=$ATF_PLATFORM bl31
 
    set -x
    cp build/$ATF_PLATFORM/release/bl31.bin $MKIMG_BIN_PATH
    set +x
}
 
# Cortex-M SDK download from https://mcuxpresso.nxp.com/ by manual
function build_cortexM()
{
    SRC=cortexm-sdk
 
    if [ "$BUILD_MCORE" != "yes" ] ; then
        pr_warn "Skip build Cortex-M core SDK source code '$SRC'"
        return 0;
    fi
 
    if [[ $BOARD =~ mx8mp ]] ; then
        EVK=evkmimx8mp
    fi
    DEMO_PATH=boards/$EVK/multicore_examples/rpmsg_lite_str_echo_rtos/armgcc
    DEMO_BIN=release/rpmsg_lite_str_echo_rtos.bin
    MCORE_IMAGE=cortexM-${BOARD}.bin
 
    if [ ! -d $PRJ_PATH/$SRC ] ; then
        pr_error "INFO: Please download $BOARD SDK from https://mcuxpresso.nxp.com by manual and"
        pr_error "      decompress it to folder '$PRJ_PATH/$SRC'"
        exit;
    fi
 
    pr_warn "start build $SRC"
 
    cd $PRJ_PATH/${SRC}/$DEMO_PATH
    export ARMGCC_DIR=$(echo $MCORE_COMPILE | sed 's\/bin/.*\\')
 
    #bash clean.sh
    if [ ! -s $DEMO_BIN ] ; then
        bash build_release.sh
    fi
 
    set -x
    cp $DEMO_BIN $PRFX_PATH/$MCORE_IMAGE
    set +x
}
 
function build_uboot()
{
    SRC=uboot-imx
    patch_file=$PRJ_PATH/patches/$BOARD/$SRC-$BSP_VER.patch
    defconfig=${BOARD}_defconfig
 
    pr_warn "start build $SRC"
    cd $PRJ_PATH/${SRC}
 
    # do patch if not patched
    if [ ! -s configs/$defconfig -a -s $patch_file ] ; then
        pr_warn "do patch for $SRC now..."
        patch -p1 < $patch_file
    fi
 
    if [ ! -f .config ] ; then
        make ARCH=arm ${BOARD}_defconfig
    fi
    make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm
 
    # i.MX6ULL only need uboot-imx
    if [[ $BOARD =~ mx6ull ]] ; then
        cp u-boot-dtb.imx u-boot-${BOARD}.imx
        chmod a+x u-boot-${BOARD}.imx
        set -x
        cp u-boot-${BOARD}.imx $PRFX_PATH
        set +x
        return 0;
    fi
 
    set -x
    cp u-boot.bin $MKIMG_BIN_PATH
    cp u-boot-nodtb.bin $MKIMG_BIN_PATH
    cp spl/u-boot-spl.bin $MKIMG_BIN_PATH
    cp arch/arm/dts/${BOARD}.dtb $MKIMG_BIN_PATH/$IMXBOOT_DTB
    cp tools/mkimage $MKIMG_BIN_PATH/mkimage_uboot
    set +x
}
 
# The diagram below illustrate a signed iMX8 flash.bin image layout:
#   reference: uboot-imx/doc/imx/habv4/guides/mx8m_secure_boot.txt
#
#                     +-----------------------------+
#                     |                             |
#                     |     *Signed HDMI/DP FW      |
#                     |                             |
#                     +-----------------------------+
#                     |           Padding           |
#             ------- +-----------------------------+ --------
#                 ^   |          IVT - SPL          |   ^
#          Signed |   +-----------------------------+   |
#           Data  |   |        u-boot-spl.bin       |   |
#                 |   |              +              |   |  SPL
#                 v   |           DDR FW            |   | Image
#             ------- +-----------------------------+   |
#                     |      CSF - SPL + DDR FW     |   v
#                     +-----------------------------+ --------
#                     |           Padding           |
#             ------- +-----------------------------+ --------
#          Signed ^   |          FDT - FIT          |   ^
#           Data  |   +-----------------------------+   |
#                 v   |          IVT - FIT          |   |
#             ------- +-----------------------------+   |
#                     |          CSF - FIT          |   |
#             ------- +-----------------------------+   |  FIT
#                 ^   |       u-boot-nodtb.bin      |   | Image
#                 |   +-----------------------------+   |
#          Signed |   |       OP-TEE (Optional)     |   |
#           Data  |   +-----------------------------+   |
#                 |   |        bl31.bin (ATF)       |   |
#                 |   +-----------------------------+   |
#                 v   |          u-boot.dtb         |   v
#             ------- +-----------------------------+ --------
#
#
# Reference: <<IMX_LINUX_USERS_GUIDE.pdf>> 4.5.13 How to build imx-boot image by using imx-mkimage
 
function build_imxboot()
{
    SRC=imx-mkimage
 
    pr_warn "start build $SRC"
    cd $PRJ_PATH/${SRC}
 
    # iMX8MP, iMX8MP
    if [[ $BOARD =~ mx8m ]] ; then
        pr_info "Copy DDR4 firmware to $MKIMG_BIN_PATH"
 
        cp $FMW_PATH/firmware-imx-*/firmware/hdmi/cadence/signed_hdmi_imx8m.bin $MKIMG_BIN_PATH
 
        # IGKBoard-IMX8MP Board
        cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_imem_202006.bin $MKIMG_BIN_PATH
        cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_dmem_202006.bin $MKIMG_BIN_PATH
 
        # STCRBoard-IMX8MM Board
        cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/ddr4_imem_[1-2]d*.bin $MKIMG_BIN_PATH
        cp $FMW_PATH/firmware-imx-*/firmware/ddr/synopsys/ddr4_dmem_[1-2]d*.bin $MKIMG_BIN_PATH
    fi
 
    make SOC=$IMX_BOOT_SOC_TARGET $IMXBOOT_ARGS $IMXBOOT_TARGETS
 
    cp $MKIMG_BIN_PATH/flash.bin u-boot-${BOARD}.imx
    chmod a+x u-boot-${BOARD}.imx
 
    cp u-boot-${BOARD}.imx $PRFX_PATH
}
 
function do_build()
{
    cd $PRJ_PATH
    mkdir -p $PRFX_PATH
 
    build_uboot
 
    # i.MX6ULL only need uboot-imx
    if [[ ${BOARD} =~ mx6ull ]] ; then
        return ;
    fi
 
    build_atf
    build_imxboot
    build_cortexM
}
 
function do_install()
{
    cd $PRJ_PATH
 
    echo ""
    pr_info "bootloader installed to '$PRFX_PATH'"
    ls $PRFX_PATH && echo ""
 
    if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then
        pr_info "install bootloader to '$INST_PATH'"
        cp $PRFX_PATH/* $INST_PATH
        #sz $PRFX_PATH/u-boot-${BOARD}.imx
    fi
}
 
function do_clean()
{
    for d in $SRCS
    do
        rm -rf $PRJ_PATH/$d
    done
 
    rm -rf $PRJ_PATH/firmware
    rm -rf $PRJ_PATH/cortexm-sdk
    rm -rf $PRJ_PATH/tarballs
    rm -rf $PRFX_PATH
}
 
#+-------------------------+
#| Shell script body entry |
#+-------------------------+
 
cd $PRJ_PATH
 
export_env
 
if [[ $# == 1 && $1 == -c ]] ;then
    pr_warn "start clean bootloader"
    do_clean
    exit;
fi
 
pr_warn "start build bootloader for ${BOARD}"
 
do_fetch
 
do_build
 
do_install