ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/bin/bash
set -e
 
localpath=$(cd $(dirname $0) && pwd)
build_system=lichee
[ -d $localpath/../../../device/config/chips ] && build_system=longan
[ -d $localpath/../../tools/pack/chips ] && build_system=lichee
 
echo "build_system: $build_system"
 
#Setup common variables
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-
if [ -n "`echo ${LICHEE_CHIP} | grep "sun5[0-9]i"`" ] && \
        [ "x${LICHEE_ARCH}" = "xarm64" ]; then
    export ARCH=arm64
    export CROSS_COMPILE=aarch64-linux-gnu-
fi
 
if [ -n "${LICHEE_TOOLCHAIN_PATH}" \
        -a -d "${LICHEE_TOOLCHAIN_PATH}" ]; then
    GCC=$(find ${LICHEE_TOOLCHAIN_PATH} -perm /a+x -a -regex '.*-gcc');
    export CROSS_COMPILE="${GCC%-*}-";
elif [ -n "${LICHEE_CROSS_COMPILER}" ]; then
    export CROSS_COMPILE="${LICHEE_CROSS_COMPILER}-"
fi
 
export AS=${CROSS_COMPILE}as
export LD=${CROSS_COMPILE}ld
export CC=${CROSS_COMPILE}gcc
export AR=${CROSS_COMPILE}ar
export NM=${CROSS_COMPILE}nm
export STRIP=${CROSS_COMPILE}strip
export OBJCOPY=${CROSS_COMPILE}objcopy
export OBJDUMP=${CROSS_COMPILE}objdump
export LOCALVERSION=""
export MKBOOTIMG=${LICHEE_TOOLS_DIR}/pack/pctools/linux/android/mkbootimg
if [ "x$CCACHE_DIR" != "x" ];then
    CCACHE_Y="ccache "
fi
MAKE="make CROSS_COMPILE="${CCACHE_Y}${CROSS_COMPILE}""
 
if [ -n "$ANDROID_CLANG_PATH" ]; then
    export PATH=$ANDROID_CLANG_PATH:$PATH
    MAKE="make CC=clang HOSTCC=clang NM=llvm-nm OBJCOPY=llvm-objcopy"
    ARCH_PREFIX=arm
    [ "x$ARCH" == "xarm64" ] && ARCH_PREFIX=aarch64
    if [ -n "$ANDROID_TOOLCHAIN_PATH" ]; then
        export CROSS_COMPILE=$ANDROID_TOOLCHAIN_PATH/$ARCH_PREFIX-linux-androidkernel-
        export CLANG_TRIPLE=$ARCH_PREFIX-linux-gnu-
    fi
fi
 
KERNEL_VERSION=`${MAKE} -s kernelversion -C ./`
LICHEE_KDIR=`pwd`
LICHEE_MOD_DIR=${LICHEE_KDIR}/output/lib/modules/${KERNEL_VERSION}
export LICHEE_KDIR
 
update_kern_ver()
{
    if [ -r include/generated/utsrelease.h ]; then
        KERNEL_VERSION=`cat include/generated/utsrelease.h |awk -F\" '{print $2}'`
    fi
    LICHEE_MOD_DIR=${LICHEE_KDIR}/output/lib/modules/${KERNEL_VERSION}
}
 
do_init_to_dts()
{
   local DTC_SRC_PATH=${LICHEE_KERN_DIR}/arch/${ARCH}/boot/dts
   if [ "x${ARCH}" == "xarm64" ]; then
       DTC_SRC_PATH=${DTC_SRC_PATH}/sunxi
   fi
   local DTC_DEP_BOARD1=${DTC_SRC_PATH}/.board.dtb.d.dtc.tmp
   local DTC_SRC_BOARD1=${DTC_SRC_PATH}/.board.dtb.dts.tmp
   local DTC_DEP_BOARD=${DTC_SRC_PATH}/.${LICHEE_CHIP}-${LICHEE_BOARD}.dtb.d.dtc.tmp
   local DTC_SRC_BOARD=${DTC_SRC_PATH}/.${LICHEE_CHIP}-${LICHEE_BOARD}.dtb.dts.tmp
   local DTC_DEP_BUSSINESS=${DTC_SRC_PATH}/.${LICHEE_CHIP}-${LICHEE_BUSSINESS}.dtb.d.dtc.tmp
   local DTC_SRC_BUSSINESS=${DTC_SRC_PATH}/.${LICHEE_CHIP}-${LICHEE_BUSSINESS}.dtb.dts.tmp
   local DTC_DEP_COMMON=${DTC_SRC_PATH}/.${LICHEE_CHIP}-soc.dtb.d.dtc.tmp
   local DTC_SRC_COMMON=${DTC_SRC_PATH}/.${LICHEE_CHIP}-soc.dtb.dts.tmp
 
   local DTC_COMPILER=${LICHEE_KERN_DIR}/scripts/dtc/dtc
 
   local DTC_INI_FILE_BASE=${LICHEE_BOARD_CONFIG_DIR}/sys_config.fex
   local DTC_INI_FILE=output/sys_config_fix.fex
 
   cp $DTC_INI_FILE_BASE $DTC_INI_FILE
   sed -i "s/\(\[dram\)_para\(\]\)/\1\2/g" $DTC_INI_FILE
   sed -i "s/\(\[nand[0-9]\)_para\(\]\)/\1\2/g" $DTC_INI_FILE
 
   if [ ! -f $DTC_COMPILER ]; then
       echo "Script_to_dts: Can not find dtc compiler.\n"
       exit 1
   fi
 
   if [ -f $DTC_DEP_BOARD1 ]; then
       printf "Script_to_dts: use board special dts file board.dts\n"
       DTC_DEP_FILE=${DTC_DEP_BOARD1}
       DTC_SRC_FILE=${DTC_SRC_BOARD1}
   elif [ -f $DTC_DEP_BOARD ]; then
       printf "Script_to_dts: use board special dts file ${LICHEE_CHIP}-${LICHEE_BOARD}.dts\n"
       DTC_DEP_FILE=${DTC_DEP_BOARD}
       DTC_SRC_FILE=${DTC_SRC_BOARD}
   elif [ -f $DTC_DEP_BUSSINESS ]; then
       printf "Script_to_dts: use bussiness special dts file ${LICHEE_CHIP}-${LICHEE_BUSSINESS}.dts\n"
       DTC_DEP_FILE=${DTC_DEP_BUSSINESS}
       DTC_SRC_FILE=${DTC_SRC_BUSSINESS}
   else
       printf "Script_to_dts: use common dts file ${LICHEE_CHIP}-soc.dts\n"
       DTC_DEP_FILE=${DTC_DEP_COMMON}
       DTC_SRC_FILE=${DTC_SRC_COMMON}
   fi
 
   local DTC_FLAGS="-W no-unit_address_vs_reg"
 
   if [ -d ${LICHEE_CHIP_CONFIG_DIR}/dtbo ];  then
       echo "sunxi_dtb create"
       $DTC_COMPILER ${DTC_FLAGS} -@ -O dtb -o ${LICHEE_KERN_DIR}/output/sunxi.dtb    \
           -b 0            \
           -i $DTC_SRC_PATH    \
           -F $DTC_INI_FILE    \
           -d $DTC_DEP_FILE $DTC_SRC_FILE
   else
       echo "sunxi_dtb create"
       $DTC_COMPILER -p 2048 ${DTC_FLAGS} -O dtb -o ${LICHEE_KERN_DIR}/output/sunxi.dtb    \
           -b 0            \
           -i $DTC_SRC_PATH    \
           -F $DTC_INI_FILE    \
           -d $DTC_DEP_FILE $DTC_SRC_FILE
   fi
 
   if [ $? -ne 0 ]; then
       echo "Conver script to dts failed"
       exit 1
   fi
 
   #restore the orignal dtsi
   if [ "x${LICHEE_LINUX_DEV}" = "xdragonboard" \
       -o "x${LICHEE_LINUX_DEV}" = "xdragonmat" ]; then
       local DTS_PATH=${LICHEE_KERN_DIR}/arch/${ARCH}/boot/dts
       [ "x${ARCH}" = "xarm64" ] && DTS_PATH=${LICHEE_KERN_DIR}/arch/${ARCH}/boot/dts/sunxi
       if [ -f ${DTS_PATH}/${LICHEE_CHIP}_bak.dtsi ];then
           rm -f ${DTS_PATH}/${LICHEE_CHIP}.dtsi
           mv  ${DTS_PATH}/${LICHEE_CHIP}_bak.dtsi  ${DTS_PATH}/${LICHEE_CHIP}.dtsi
       fi
   fi
 
   printf "Conver script to dts ok.\n"
 
   # It'is used for debug dtb
   $DTC_COMPILER ${DTC_FLAGS} -I dtb -O dts -o ${LICHEE_KERN_DIR}/output/.sunxi.dts ${LICHEE_KERN_DIR}/output/sunxi.dtb
 
   return
}
 
show_help()
{
    printf "
    Build script for Lichee platform
 
    Invalid Options:
 
    help         - show this help
    kernel       - build kernel
    modules      - build kernel module in modules dir
    clean        - clean kernel and modules
 
    "
}
 
build_nand()
{
    NAND_ROOT=${LICHEE_KDIR}/modules/nand
 
    ${MAKE} -C modules/nand LICHEE_MOD_DIR=${LICHEE_MOD_DIR} \
        LICHEE_KDIR=${LICHEE_KDIR} \
        CONFIG_CHIP_ID=${CONFIG_CHIP_ID} install
 
}
 
gpu_message()
{
    echo -ne "\033[34;1m"
    echo "[GPU]: $1"
    echo -ne "\033[0m"
}
 
build_gpu()
{
    GPU_TYPE=`fgrep CONFIG_SUNXI_GPU_TYPE ${LICHEE_KDIR}/.config 2>/dev/null | cut -d \" -f 2`
    if [ "X$GPU_TYPE" = "XNone" -o "X$GPU_TYPE" = "X" ]; then
        gpu_message "No GPU type is configured in ${LICHEE_KDIR}/.config."
        return
    fi
 
    gpu_message "Building $GPU_TYPE device driver..."
 
    if [ "X${LICHEE_PLATFORM}" = "Xandroid" -o "X${LICHEE_PLATFORM}" = "Xsecureandroid" ] ; then
        TMP_OUT=$OUT
        TMP_TOP=$TOP
        unset OUT
        unset TOP
    fi
 
    ${MAKE} -C modules/gpu LICHEE_MOD_DIR=${LICHEE_MOD_DIR} LICHEE_KDIR=${LICHEE_KDIR}
 
    if [ "X${LICHEE_PLATFORM}" = "Xandroid" -o "X${LICHEE_PLATFORM}" = "Xsecureandroid" ] ; then
        export OUT=$TMP_OUT
        export TOP=$TMP_TOP
    fi
 
    gpu_message "$GPU_TYPE device driver has been built."
}
 
clean_gpu()
{
    GPU_TYPE=`fgrep CONFIG_SUNXI_GPU_TYPE ${LICHEE_KDIR}/.config 2>/dev/null | cut -d \" -f 2`
    if [ "X$GPU_TYPE" = "XNone" -o "X$GPU_TYPE" = "X" ]; then
        gpu_message "No GPU type is configured in .config."
        return
    fi
 
    gpu_message "Cleaning $GPU_TYPE device driver..."
    ${MAKE} -C modules/gpu LICHEE_MOD_DIR=${LICHEE_MOD_DIR} LICHEE_KDIR=${LICHEE_KDIR} clean
    gpu_message "$GPU_TYPE device driver has been cleaned."
}
 
build_check()
{
    if [ "x$SUNXI_CHECK" = "x1" ];then
        SUNXI_SPARSE_CHECK=1
        SUNXI_SMATCH_CHECK=1
        SUNXI_STACK_CHECK=1
    fi
 
    if [ "x$SUNXI_SPARSE_CHECK" = "x1" ] && [ -f ../tools/codecheck/sparse/sparse ];then
        echo "\n\033[0;31;1mBuilding Round for sparse check...\033[0m\n\n"
        CHECK="../tools/codecheck/sparse/sparse"
        ${MAKE} CHECK=${CHECK} ARCH=${ARCH} -j${LICHEE_JLEVEL} \
                C=2 all modules 2>&1|tee output/build_sparse.txt
        cat output/build_sparse.txt|egrep -w '(warn|error|warning)' >output/warn_sparse.txt
    fi
 
    if [ "x$SUNXI_SMATCH_CHECK" = "x1" ]&& [ -f ../tools/codecheck/smatch/smatch ];then
        echo "\n\033[0;31;1mBuilding Round for smatch check...\033[0m\n\n"
        CHECK="../tools/codecheck/smatch/smatch --full-path --no-data -p=kkernel"
        ${MAKE} CHECK=${CHECK} ARCH=${ARCH} -j${LICHEE_JLEVEL} \
                C=2 all modules 2>&1|tee output/build_smatch.txt
        cat output/build_smatch.txt|egrep -w '(warn|error|warning)' >output/warn_smatch.txt
    fi
 
    if [ "x$SUNXI_STACK_CHECK" = "x1" ];then
        ${MAKE} ARCH=${ARCH} -j${LICHEE_JLEVEL} checkstack 2>&1 \
                |tee output/warn_stack.txt
    fi
}
 
build_dts()
{
    if [ ! -f .config ] ; then
        printf "\n\033[0;31;1mUsing default config ${LICHEE_KERN_DEFCONF_ABSOLUTE} ...\033[0m\n\n"
        ${MAKE} ARCH=${ARCH} defconfig KBUILD_DEFCONFIG=${LICHEE_KERN_DEFCONF_RELATIVE}
    fi
 
    echo "---build dts for ${LICHEE_CHIP} ${LICHEE_BOARD}-----"
    if [ "x${ARCH}" = "xarm" ];then
        dts_path="arch/arm/boot/dts"
    else
        dts_path="arch/arm64/boot/dts/sunxi"
        prefix="sunxi/"
    fi
    dtb_file=board.dtb
    dts_file=board.dts
    if [ ! -f $dts_path/$dts_file ];then
        dtb_file="${LICHEE_CHIP}-${LICHEE_BOARD}.dtb"
        dts_file="${LICHEE_CHIP}-${LICHEE_BOARD}.dts"
    fi
    if [ ! -f $dts_path/$dts_file ];then
        dts_file="${LICHEE_CHIP}-soc.dts"
        dtb_file="${LICHEE_CHIP}-soc.dtb"
    fi
    ${MAKE} ARCH=${ARCH} ${prefix}${dtb_file}
}
 
build_kernel()
{
    echo "Building kernel"
 
    cd ${LICHEE_KDIR}
    rm -rf output/
    echo "${LICHEE_MOD_DIR}"
    mkdir -p ${LICHEE_MOD_DIR}
 
    # uImage is arm architecture specific target
    local arch_target=""
    if [ "${ARCH}" = "arm" ]; then
        arch_target="uImage dtbs"
    else
        arch_target="all"
    fi
 
    #exchange sdc0 and sdc2 for dragonBoard card boot
    local check_cardboot=false
    local SYS_CONFIG_FILE=""
    if [ "x$build_system" == "xlichee" ]; then
        [ "x${LICHEE_PLATFORM}" == "xdragonboard" -o "x${LICHEE_PLATFORM}" == "xdragonmat" ] && check_cardboot=true
        SYS_CONFIG_FILE=$localpath/../../tools/pack/chips/${LICHEE_CHIP}/configs/${LICHEE_BOARD}/sys_config.fex
    else
        [ "x${LICHEE_PLATFORM}" == "xlinux" ] && [ "x${LICHEE_LINUX_DEV}" == "xdragonboard" -o "x${LICHEE_LINUX_DEV}" == "xdragonmat" ] && check_cardboot=true
        SYS_CONFIG_FILE=${LICHEE_BOARD_CONFIG_DIR}/sys_config.fex
    fi
 
    if [ "x$check_cardboot" == "xtrue" ]; then
        local DTS_PATH=./arch/${ARCH}/boot/dts
        [ "x$ARCH" == "xarm64" ] && DTS_PATH=$DTS_PATH/sunxi
 
        if [ -f ${DTS_PATH}/${LICHEE_CHIP}_bak.dtsi ];then
            mv ${DTS_PATH}/${LICHEE_CHIP}_bak.dtsi ${DTS_PATH}/${LICHEE_CHIP}.dtsi
        fi
 
        # if find dragonboard_test=1 in sys_config.fex ,then will exchange sdc0 and sdc2
        local card_boot=$(awk -F= '/^dragonboard_test[[:space:]]*=/{gsub(/[[:blank:]]*/,"",$2);print $2}' $SYS_CONFIG_FILE)
        if [ "x$card_boot" == "x1" ]; then
            echo "exchange sdc0 and sdc2 for dragonboard card boot"
            ./scripts/exchange-sdc0-sdc2-for-dragonboard.sh ${DTS_PATH}/${LICHEE_CHIP}.dtsi
        fi
    fi
 
    if [ ! -f .config ] ; then
        printf "\n\033[0;31;1mUsing default config ${LICHEE_KERN_DEFCONF_ABSOLUTE} ...\033[0m\n\n"
        ${MAKE} ARCH=${ARCH} defconfig KBUILD_DEFCONFIG=${LICHEE_KERN_DEFCONF_RELATIVE}
    fi
 
    if [ "x$PACK_TINY_ANDROID" = "xtrue" ]; then
        ARCH=${ARCH} scripts/kconfig/merge_config.sh .config \
            linaro/configs/sunxi-tinyandroid.conf
    fi
 
    if [ "x$PACK_BSPTEST" != "x" -o "x$BUILD_SATA" != "x" -o "x$LICHEE_LINUX_DEV" = "xsata" ]; then
        if [ -f linaro/configs/sunxi-common.conf ];then
            ARCH=${ARCH} scripts/kconfig/merge_config.sh .config \
                linaro/configs/sunxi-common.conf
        fi
        if [ -f linaro/configs/sunxi-sata.conf ];then
            ARCH=${ARCH} scripts/kconfig/merge_config.sh .config \
                linaro/configs/sunxi-sata.conf
        fi
        if [ -f linaro/configs/sunxi-sata-${LICHEE_CHIP}.conf ];then
            ARCH=${ARCH} scripts/kconfig/merge_config.sh .config \
                linaro/configs/sunxi-sata-${LICHEE_CHIP}.conf
        fi
        if [ -f linaro/configs/sunxi-sata-${ARCH}.conf ];then
            ARCH=${ARCH} scripts/kconfig/merge_config.sh .config \
                linaro/configs/sunxi-sata-${ARCH}.conf
        fi
    fi
 
    ${MAKE} ARCH=${ARCH} -j${LICHEE_JLEVEL} ${arch_target} modules
    build_check
    update_kern_ver
    do_init_to_dts
    #The Image is origin binary from vmlinux.
    if [ -f arch/${ARCH}/boot/Image ]; then
        cp -vf arch/${ARCH}/boot/Image output/bImage
    fi
 
    if [ -f arch/${ARCH}/boot/zImage ] || [ -f arch/${ARCH}/boot/uImage ]; then
        cp -vf arch/${ARCH}/boot/[zu]Image output/
    fi
 
    if [ -f arch/${ARCH}/boot/Image.gz ]; then
        cp -vf arch/${ARCH}/boot/Image.gz output/
    fi
 
    echo "Copy rootfs.cpio.gz for ${ARCH}"
    if [ "${ARCH}" = "arm" ]; then
        cp -f rootfs_32bit.cpio.gz output/rootfs.cpio.gz
    else
        cp -f rootfs.cpio.gz output/rootfs.cpio.gz
    fi
 
    cp .config output/
 
    tar -jcf output/vmlinux.tar.bz2 vmlinux
    for file in $(find drivers sound crypto block fs security net -name "*.ko"); do
        cp $file ${LICHEE_MOD_DIR}
    done
    cp -f Module.symvers ${LICHEE_MOD_DIR}
 
}
 
build_modules()
{
 
    echo "Building modules"
 
    if [ ! -f include/generated/utsrelease.h ]; then
        printf "Please build kernel first!\n"
        exit 1
    fi
 
    update_kern_ver
    build_nand
    build_gpu
}
 
regen_rootfs_cpio()
{
    echo "regenerate rootfs cpio"
 
    cd ${LICHEE_KDIR}/output
    if [ -x "../scripts/build_rootfs.sh" ]; then
        ../scripts/build_rootfs.sh e ./rootfs.cpio.gz > /dev/null
    else
        echo "No such file: scripts/build_rootfs.sh"
        exit 1
    fi
 
    mkdir -p ./skel/lib/modules/${KERNEL_VERSION}
 
    if [ -d ${LICHEE_KERN_DIR}/tools/efuse_api ];then
        ${MAKE} -C ${LICHEE_KERN_DIR}/tools/efuse_api INSTALL_PATH=$LICHEE_PLAT_OUT/rootfs_def/bin install
    fi
 
    if [ -e ${LICHEE_MOD_DIR}/nand.ko ]; then
        cp ${LICHEE_MOD_DIR}/nand.ko ./skel/lib/modules/${KERNEL_VERSION}
        if [ $? -ne 0 ]; then
            echo "copy nand module error: $?"
            exit 1
        fi
    fi
 
    ko_file=`find ./skel/lib/modules/$KERNEL_VERSION/ -name *.ko`
    if [ ! -z "$ko_file" ]; then
            ${STRIP} -d ./skel/lib/modules/$KERNEL_VERSION/*.ko
    fi
 
    rm -f rootfs.cpio.gz
    ../scripts/build_rootfs.sh c rootfs.cpio.gz > /dev/null
    rm -rf skel
    cd - > /dev/null
}
 
build_ramfs()
{
    local bss_sz=0;
    local bss_offset=0;
    local bss_size=0;
    local CHIP="";
 
    local BIMAGE="output/bImage";
    local RAMDISK="output/rootfs.cpio.gz";
    local DTB="output/sunxi.dtb";
    local BASE="";
    local RAMDISK_OFFSET="";
    local KERNEL_OFFSET="";
 
    # update rootfs.cpio.gz with new module files
    regen_rootfs_cpio
 
    CHIP=`echo ${LICHEE_CHIP} | sed -e 's/.*\(sun[0-9x]*i\).*/\1/g'`;
 
    if [ "${CHIP}" = "sun9i" ]; then
        BASE="0x20000000";
    else
        BASE="0x40000000";
    fi
 
    if [ "${ARCH}" = "arm" ]; then
        KERNEL_OFFSET="0x8000";
    elif [ "${ARCH}" = "arm64" ]; then
        KERNEL_OFFSET="0x80000";
    fi
 
    if [ -f vmlinux ]; then
        bss_offset=`${CROSS_COMPILE}readelf -S vmlinux | \
            awk  '/\.bss/ {print $5}'`;
        bss_size=`${CROSS_COMPILE}readelf -S vmlinux | \
            awk  '/\.bss/ {print $6}'`;
 
        # If linux-4.9 the bss_size will be in next line
        if [ -z "$bss_size" ]; then
            bss_size=`${CROSS_COMPILE}readelf -S vmlinux | \
                 awk  '/\.bss/ {getline var; print var}' | awk '{print $1}'`;
        fi
 
        bss_sz=$[ $((16#$bss_offset))+$((16#$bss_size))]
    fi
 
    # If the size of bImage larger than 16M, will offset 0x02000000
    if [ ${bss_sz} -gt $((16#1000000)) ]; then
        RAMDISK_OFFSET="0x02000000";
    else
        RAMDISK_OFFSET="0x01000000";
    fi
    DTB_OFFSET="0x04000000";
 
    ${MKBOOTIMG} --kernel ${BIMAGE} \
        --ramdisk ${RAMDISK} \
        --dtb ${DTB} \
        --board ${CHIP}_${ARCH} \
        --base ${BASE} \
        --kernel_offset ${KERNEL_OFFSET} \
        --ramdisk_offset ${RAMDISK_OFFSET} \
        --dtb_offset ${DTB_OFFSET} \
        --header_version 0x2 \
        -o output/boot.img
 
    # If uboot use *bootm* to boot kernel, we should use uImage.
    echo build_ramfs
    echo "Copy boot.img to output directory ..."
    cp output/boot.img ${LICHEE_PLAT_OUT}
    cp output/vmlinux.tar.bz2 ${LICHEE_PLAT_OUT}
 
    if [ -f output/zImage ] || [ -f output/uImage ]; then
        cp output/[zu]Image ${LICHEE_PLAT_OUT}
    fi
 
    if [ ! -f output/arisc ]; then
        echo "arisc" > output/arisc
    fi
    cp output/arisc ${LICHEE_PLAT_OUT}
 
    cp output/.sunxi.dts ${LICHEE_PLAT_OUT}
    cp output/sunxi.dtb ${LICHEE_PLAT_OUT}
}
 
gen_output()
{
    if [ "x${LICHEE_PLATFORM}" = "xandroid" ] ; then
        echo "Copy modules to target ..."
        rm -rf ${LICHEE_PLAT_OUT}/lib
        cp -rf ${LICHEE_KDIR}/output/* ${LICHEE_PLAT_OUT}
        return
    fi
 
    if [ -d ${LICHEE_BR_OUT}/target ] ; then
        echo "Copy modules to target ..."
        local module_dir="${LICHEE_BR_OUT}/target/lib/modules"
        rm -rf ${module_dir}
        mkdir -p ${module_dir}
        cp -rf ${LICHEE_MOD_DIR} ${module_dir}
    fi
 
    echo "$0"
}
 
clean_kernel()
{
    clarg="clean"
    if [ "x$1" == "xdistclean" ]; then
        clarg="distclean"
    fi
    echo "Cleaning kernel, arg: $clarg ..."
    ${MAKE} ARCH=${ARCH} "$clarg"
    rm -rf output/*
    echo
}
 
clean_modules()
{
    echo "Cleaning modules ..."
    clean_gpu
    echo
}
 
#####################################################################
#
#                      Main Runtine
#
#####################################################################
 
case "$1" in
    kernel)
        build_kernel
        ;;
    modules)
        build_modules
        ;;
    clean|distclean)
        clean_modules
        clean_kernel "$1"
        ;;
    dts)
        build_dts
        ;;
    *)
        build_kernel
        build_modules
        build_ramfs
        gen_output
        echo -e "\n\033[0;31;1m${LICHEE_CHIP} compile Kernel successful\033[0m\n\n"
        ;;
esac