Build Atmel ARM9 board Board Support Packets
guowenxue
2024-08-30 5f28ee54121e6e12f3eac26c5c21fd7737863163
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
#!/bin/bash
 
# 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
}
 
# 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.crosstool" $CONF_FILE`
 
    export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
    export ARCH=arm
 
    ROOTFS_SRC=rootfs
    ROOTFS_IMG=rootfs-${BOARD}.ubi
 
    export SRCS="rootfs"
}
 
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"
        mkdir -p $TARBALL_PATH
 
        # Download source code packet
        if [ ! -s $TARBALL_PATH/$src.tar.xz ] ; then
            wget $BSP_URL/at91/bsp/$BSP_VER/$src.tar.xz -P $TARBALL_PATH
        fi
 
        # decompress source code packet
        do_unpack $TARBALL_PATH/$src.tar.xz $ROOTFS_SRC
    done
}
 
function build_ubifs()
{
    ubinize_cfg=ubinize.cfg
    ubimg_tmp=ubi.img
 
    # Rootfs partition size
    partition_size=100  # MiB
 
    # K9F2G08: 1 block=64 pages,  1 page = 2048
    BLOCK_PAGES=64
    PAGE_SIZE=2048
 
    ROOTFS_IMG=${ROOTFS_IMG}-p`expr $PAGE_SIZE \/ 1024`k
 
    # 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
    fakeroot 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 do_modify()
{
    cd $PRJ_PATH
 
    pr_info "start modify rootfs ..."
 
    cp patches/profile $ROOTFS_SRC/etc/profile
    cp patches/S50sshd $ROOTFS_SRC/etc/init.d/S50sshd
}
 
function do_build()
{
    cd $PRJ_PATH
    mkdir -p $PRFX_PATH
 
    pr_info "start build rootfs image..."
    build_ubifs
 
    mv $ROOTFS_IMG $PRFX_PATH
}
 
function do_install()
{
    cd $PRJ_PATH
 
    echo ""
    pr_info "install all images to '$PRFX_PATH'"
    cp $TOP_PATH/bootloader/install/* $PRFX_PATH
    cp $TOP_PATH/kernel/install/* $PRFX_PATH
 
    ls $PRFX_PATH && echo ""
 
    if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then
        pr_info "install rootfs to '$INST_PATH'"
        cp $PRFX_PATH/* $INST_PATH
    fi
}
 
function do_clean()
{
    for d in $SRCS
    do
        rm -rf $PRJ_PATH/$d
    done
 
    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 rootfs"
    do_clean
    exit;
fi
 
pr_warn "start build rootfs for ${BOARD}"
 
do_fetch
 
do_modify
 
do_build
 
do_install