SAMA5D4 Xplained Ultra Board BSP
guowenxue
2019-08-12 a68e8c60fd9fa2ab9dc91312c6ba0d75302e92d7
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
#!/bin/bash
# Description: This shell script used to build linux system SDK. You can 
#              use command line argument to specify build target, or select 
#              it by hand if no argument given by command line. More detail
#              information can refer to the help message.
#
#      Author: guowenxue <guowenxue@gmail.com>
#     Version: 1.0.0  
 
 
PROJ_PATH=`pwd`
 
CROSSTOOL=/opt/buildroot/cortex-a5/bin/arm-linux-
 
BOARD=sama5d4
PACK_PATH=${PROJ_PATH}/tarballs
PATCH_PATH=${PROJ_PATH}/patches
PATCH_SUFIX=${BOARD}.patch
 
 
 
ROOTFS_SRC=rootfs
ROOTFS_IMG=rootfs-sama5d4.ubi
 
TFTP_PATH=/tftp
IMGS_PATH=images
 
target=
 
set -e
 
function show_help()
{
    printf "Usage: $1 [system/bootloader/kernel/rootfs/clean]\n\n"
 
    echo "system    : build all source code "
    echo "bootloader: build bootstrap and u-boot "
    echo "kernel    : build linux kernel "
    echo "rootfs    : build ubifs rootfs images"
    printf "clean     : clean the source code\n\n"
    printf "distclean : remove all the source code\n\n"
 
    exit 0;
}
 
function show_banner()
{
    echo "+---------------------------------------------+" 
    printf "$1\n"
    echo "+---------------------------------------------+"
}
 
function select_target()
{
    targets=("" "system" "bootloader" "kernel" "rootfs" "clean" "distclean")
    echo "Please select a build target:"
    echo "1 <system>    , build all source code "
    echo "2 <bootloader>, build bootstrap and u-boot "
    echo "3 <kernel>    , build linux kernel "
    echo "4 <rootfs>    , build ubifs rootfs image"
    echo "5 <clean>     , clean the source code"
    echo "6 <distclean> , remove all the  source code"
    echo ""
 
    stty erase '^H'
    read -p "Enter your choise [1-6] : " choice
 
    if [ $choice -gt 6 ] ; then
        echo "ERROR: Invalid input choice, exit now..."
        exit 1;
    fi
 
    target=${targets[$choice]}
}
 
 
 
# Usage: start_build [SRCDIR: linux-at91/at91bootstrap/u-boot-at91 ]
 
function start_build()
{
    SRC_DIR=$1
 
    if [ ! -d ${SRC_DIR} ] ; then
        show_banner "|       start decmpress $SRC_DIR packet       |"
        tar -xJf ${PACK_PATH}/${SRC_DIR}.tar.xz 
        cd ${SRC_DIR}
 
        if [ -f ${PATCH_PATH}/${SRC_DIR}-${PATCH_SUFIX} ] ; then 
            show_banner "|            patch for ${SRC_DIR}             |"
 
            # update cross compiler
            sed -i -e "s|^+CROSSTOOL=.*|+CROSSTOOL=${CROSSTOOL}|g" ${PATCH_PATH}/${SRC_DIR}-${PATCH_SUFIX}
 
            patch -p1 < ${PATCH_PATH}/${SRC_DIR}-${PATCH_SUFIX}
        fi
    else
        cd ${SRC_DIR}
    fi
 
    show_banner "|           start $SRC_DIR build task...        |"
    chmod a+x build.sh
    bash build.sh
    show_banner "|            $SRC_DIR build task over!          |"
 
    cd -
}
 
function build_ubifs()
{
    ubinize_cfg=ubinize.cfg 
    ubimg_tmp=ubi.img
 
 
    # Rootfs partition size
    partition_size=200  # MiB 
 
    # MT29F4G08: 1 block=64 pages,  1 page = 4096B
    BLOCK_PAGES=64
    PAGE_SIZE=4096
 
    # Logic Erase Block Size: UBI requires 2 minimum I/O units out of each Physical Erase Block (PEB) for overhead:
    #   1 for maintaining erase count information, and 1 for maintaining the Volume ID information. 
 
    PEB_SIZE=`expr $PAGE_SIZE \* $BLOCK_PAGES`
    LEB_SIZE=`expr $PEB_SIZE - 2 \* $PAGE_SIZE `
 
    #UBI reserves 4 blocks space for management and bad PEB handling operations
    # 2 PEBs are used to store the UBI volume table
    # 1 PEB is reserved for wear-leveling purposes;
    # 1 PEB is reserved for the atomic LEB change operation;
    # a% of PEBs is reserved for handling bad EBs. The default for NAND is 1%
 
    PEB_CNT=`expr $partition_size \* 1024 \* 1024 / ${PEB_SIZE}`
    LEB_CNT=`expr $PEB_CNT - 4 - $PEB_CNT \/ 100 `
 
    #echo "Parition size ${partition_size}MiB and LEB=$LEB_CNT"
    set -x 
    printf "\nWARNNING: generete rootfs image need root privilege, please input sudo passwd!\n\n" 
    sudo mkfs.ubifs -F -d ${ROOTFS_SRC} -m ${PAGE_SIZE} -e ${LEB_SIZE} -c $LEB_CNT -o ${ubimg_tmp}
    set +x
 
 
    # vol_size smaller than the actual size of the partition to leave room for Ubifs internal data. 
    # With "vol_type=dynamic", Ubifs will end up using the whole space automatically
    vol_size=`expr ${partition_size} \- 10`
 
    printf "[ubifs] \nmode=ubi \nimage=${ubimg_tmp} \nvol_id=0 \nvol_size=${vol_size}MiB\n" > $ubinize_cfg 
    printf "vol_type=dynamic \nvol_name=rootfs \nvol_flags=autoresize\n" >> $ubinize_cfg
    #cat $ubinize_cfg
 
    set -x 
    ubinize -o ${ROOTFS_IMG} -m ${PAGE_SIZE} -p ${PEB_SIZE} $ubinize_cfg
    set +x
 
    rm -f $ubimg_tmp $ubinize_cfg
    chmod a+x ${ROOTFS_IMG}
}
 
function build_rootfs()
{
    if [ ! -d ${ROOTFS_SRC} ] ; then 
        printf "\nWARNNING: decompress rootfs need root privilege, please input sudo passwd!\n\n" 
        sudo tar -xJf ${PACK_PATH}/${ROOTFS_SRC}.tar.xz
    fi
 
    show_banner "|           start build rootfs image...        |"
    build_ubifs
    show_banner "|           rootfs image build over!          |"
 
    if [ -d ${IMGS_PATH} ] ; then
        cp -f ${ROOTFS_IMG} ${IMGS_PATH}
    fi
 
    if [ -d ${TFTP_PATH} ] ; then
        cp -f ${ROOTFS_IMG} ${TFTP_PATH}
    fi
 
    rm -f ${ROOTFS_IMG}
    ls ${IMGS_PATH}
}
 
 
function do_clean()
{
    SRCS="linux-at91 at91bootstrap u-boot-at91"
 
    show_banner "|         clean project source code           |"
    
    for dir in $SRCS
    do
        if [ -d $dir ] ; then
            cd $dir
            bash build.sh clean
            cd -
        fi
    done
}
 
function do_distclean()
    if [ `id -u` != 0 ] ; then 
        printf "\nERROR: distclean need root privilege, please use sudo!\n\n" 
        exit ; 
    fi 
 
    printf "\nWARNNING: Start remove all the source code and images\n\n"
    rm -rf linux-at91 at91bootstrap u-boot-at91 rootfs ${IMGS_PATH}
}
 
function do_build()
{
    if   [ "$target" = "system" ] ; then
        start_build at91bootstrap
        start_build u-boot-at91
        start_build linux-at91
        build_rootfs
 
    elif [ "$target" = "bootloader" ] ; then
        start_build at91bootstrap
        start_build u-boot-at91
 
    elif [ "$target" = "kernel" ] ; then
        start_build linux-at91
 
    elif [ "$target" = "rootfs" ] ; then
        build_rootfs
 
    elif [ "$target" = "clean" ] ; then
        do_clean
 
    elif [ "$target" = "distclean" ] ; then
        do_distclean
 
    else
        show_help
    fi
}
 
mkdir -p ${IMGS_PATH}
 
# check input arguments for build target
if [ $# -ge 1 ] ; then
    target=$1
else 
    select_target
fi
show_banner "|        choose build target [$target]         |"
 
do_build