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 |
8e6109
|
13 |
#INST_PATH=/tftp |
G |
14 |
|
2334ec
|
15 |
# compress system image or not |
G |
16 |
COMPRESS=yes |
|
17 |
|
8e6109
|
18 |
# download taballs path |
G |
19 |
TARBALL_PATH=$PRJ_PATH/tarballs |
849fbd
|
20 |
|
G |
21 |
# config file path |
|
22 |
CONF_FILE=$TOP_PATH/config.json |
|
23 |
|
|
24 |
# shell script will exit once get command error |
|
25 |
set -e |
|
26 |
set -u |
|
27 |
|
|
28 |
#+-------------------------+ |
|
29 |
#| Shell script functions | |
|
30 |
#+-------------------------+ |
|
31 |
|
|
32 |
function pr_error() { |
|
33 |
echo -e "\033[40;31m $1 \033[0m" |
|
34 |
} |
|
35 |
|
|
36 |
function pr_warn() { |
|
37 |
echo -e "\033[40;33m $1 \033[0m" |
|
38 |
} |
|
39 |
|
|
40 |
function pr_info() { |
|
41 |
echo -e "\033[40;32m $1 \033[0m" |
|
42 |
} |
|
43 |
|
|
44 |
# decompress a packet to destination path |
|
45 |
function do_unpack() |
|
46 |
{ |
|
47 |
tarball=$1 |
|
48 |
dstpath=`pwd` |
|
49 |
|
|
50 |
if [[ $# == 2 ]] ; then |
|
51 |
dstpath=$2 |
|
52 |
fi |
|
53 |
|
|
54 |
pr_info "decompress $tarball => $dstpath" |
|
55 |
|
|
56 |
mkdir -p $dstpath |
|
57 |
case $tarball in |
|
58 |
*.tar.gz) |
|
59 |
tar -xzf $tarball -C $dstpath |
|
60 |
;; |
|
61 |
|
|
62 |
*.tar.bz2) |
|
63 |
tar -xjf $tarball -C $dstpath |
|
64 |
;; |
|
65 |
|
|
66 |
*.tar.xz) |
|
67 |
tar -xJf $tarball -C $dstpath |
|
68 |
;; |
|
69 |
|
|
70 |
*.tar.zst) |
|
71 |
tar -I zstd -xf $tarball -C $dstpath |
|
72 |
;; |
|
73 |
|
|
74 |
*.tar) |
|
75 |
tar -xf $tarball -C $dstpath |
|
76 |
;; |
|
77 |
|
|
78 |
*.zip) |
|
79 |
unzip -qo $tarball -d $dstpath |
|
80 |
;; |
|
81 |
|
|
82 |
*) |
|
83 |
pr_error "decompress Unsupport packet: $tarball" |
|
84 |
return 1; |
|
85 |
;; |
|
86 |
esac |
|
87 |
} |
|
88 |
|
|
89 |
# parser configure file and export environment variable |
|
90 |
function export_env() |
|
91 |
{ |
|
92 |
export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'` |
|
93 |
export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'` |
8e6109
|
94 |
export BSP_URL=`jq -r ".bsp.giturl" $CONF_FILE` |
G |
95 |
export DIS_TYPE=`jq -r ".system.distro" $CONF_FILE | tr 'A-Z' 'a-z'` |
849fbd
|
96 |
export DIS_VER=`jq -r ".system.version" $CONF_FILE | tr 'A-Z' 'a-z'` |
G |
97 |
export IMAGE_SIZE=`jq -r ".system.imgsize" $CONF_FILE | tr 'A-Z' 'a-z'` |
|
98 |
export BOOT_SIZE=`jq -r ".system.bootsize" $CONF_FILE | tr 'A-Z' 'a-z'` |
|
99 |
|
|
100 |
export LOOP_DEV=`losetup -f | cut -d/ -f3` |
|
101 |
export MNT_POINT=$PRJ_PATH/mnt |
|
102 |
|
|
103 |
export ROOTFS=rootfs-${DIS_VER} |
|
104 |
export UBOOT_BINPATH=$TOP_PATH/bootloader/install/ |
|
105 |
export KERNEL_BINPATH=$TOP_PATH/kernel/install/ |
|
106 |
export ROOTFS_YCTPATH=$TOP_PATH/yocto/install/ |
2334ec
|
107 |
export IMAGE_NAME=${DIS_TYPE}-${DIS_VER}-${BSP_VER}.img |
849fbd
|
108 |
|
G |
109 |
if [[ $BOARD =~ mx8ulp ]] || [[ $BOARD =~ mx8mq ]] || [[ $BOARD =~ mx8mm ]] ; then |
|
110 |
export UBOOT_OFFSET_SECTOR=66 |
|
111 |
elif [[ $BOARD =~ mx8mn ]] || [[ $BOARD =~ mx8mp ]] || [[ $BOARD =~ mx93 ]] ; then |
|
112 |
export UBOOT_OFFSET_SECTOR=64 |
|
113 |
elif [[ $BOARD =~ mx6 ]] || [[ $BOARD =~ mx7 ]] ; then |
|
114 |
export UBOOT_OFFSET_SECTOR=2 |
|
115 |
fi |
|
116 |
} |
|
117 |
|
|
118 |
function do_fetch() |
|
119 |
{ |
|
120 |
cd $PRJ_PATH |
|
121 |
|
8e6109
|
122 |
if [ -d $ROOTFS/bin ] ; then |
G |
123 |
pr_info "$ROOTFS fetched already" |
|
124 |
return ; |
|
125 |
fi |
849fbd
|
126 |
|
8e6109
|
127 |
if [[ $DIS_TYPE == yocto ]] ; then |
G |
128 |
TAR_TYPE=tar.zst |
|
129 |
else |
|
130 |
TAR_TYPE=tar.xz |
|
131 |
fi |
849fbd
|
132 |
|
8e6109
|
133 |
# Decompress the rootfs form Yocto build install path |
G |
134 |
if [[ $DIS_TYPE == yocto ]] ; then |
849fbd
|
135 |
|
2334ec
|
136 |
for tarball in $ROOTFS_YCTPATH/*.${TAR_TYPE} |
849fbd
|
137 |
do |
G |
138 |
if [ -s $tarball ] ; then |
|
139 |
pr_warn "Decompress $ROOTFS from yocto install path" |
|
140 |
mkdir -p $ROOTFS |
|
141 |
do_unpack $tarball $ROOTFS |
8e6109
|
142 |
return ; |
849fbd
|
143 |
fi |
G |
144 |
done |
8e6109
|
145 |
fi |
849fbd
|
146 |
|
8e6109
|
147 |
tarball=rootfs-${DIS_TYPE}-${DIS_VER}.${TAR_TYPE} |
849fbd
|
148 |
|
8e6109
|
149 |
if [[ $BSP_URL =~ github.com ]] ; then |
G |
150 |
|
|
151 |
pr_error "INFO: Please download $tarball and decompress it to folder '$PRJ_PATH/$ROOTFS'" |
|
152 |
|
|
153 |
else |
|
154 |
|
|
155 |
pr_info "INFO: download $tarball form $BSP_URL" |
|
156 |
|
|
157 |
mkdir -p $TARBALL_PATH |
|
158 |
|
|
159 |
# Download source code packet |
|
160 |
if [ ! -s $TARBALL_PATH/$tarball ] ; then |
|
161 |
wget $BSP_URL/${BOARD}/rootfs/$tarball -P $TARBALL_PATH |
|
162 |
fi |
|
163 |
|
|
164 |
# decompress source code packet |
|
165 |
do_unpack $TARBALL_PATH/$tarball $ROOTFS |
|
166 |
fi |
|
167 |
|
|
168 |
if [ ! -d $ROOTFS/bin ] ; then |
849fbd
|
169 |
pr_error "Fetch rootfs $ROOTFS failed" |
G |
170 |
exit ; |
|
171 |
fi |
|
172 |
} |
|
173 |
|
|
174 |
# System image layout map: |
|
175 |
# +-------------------+--------------------+----------------------+ |
|
176 |
# | Raw Part(10MB) | FAT32 Part2(100MB) | EXT4 Part3(All left) | |
|
177 |
# +-------------------+--------------------+----------------------+ |
|
178 |
# | U-boot on #64 | Kernel and DTB | Root file system | |
|
179 |
# +-------------------+--------------------+----------------------+ |
|
180 |
|
|
181 |
function build_image() |
|
182 |
{ |
|
183 |
# Uboot size set be 10MB and deployed in 64th sector on eMMC/TFCard |
|
184 |
UBOOT_SIZE=10 |
|
185 |
|
|
186 |
mkdir -p $MNT_POINT |
|
187 |
|
|
188 |
pr_info "start generate empty system image" |
|
189 |
dd if=/dev/zero of=${IMAGE_NAME} bs=1024k count=${IMAGE_SIZE} conv=sync |
|
190 |
chmod a+x ${IMAGE_NAME} |
|
191 |
|
|
192 |
pr_info "start partition system image" |
|
193 |
fat_start=$UBOOT_SIZE |
|
194 |
fat_end=`expr $UBOOT_SIZE + $BOOT_SIZE` |
|
195 |
parted ${IMAGE_NAME} mklabel msdos |
|
196 |
parted ${IMAGE_NAME} mkpart primary fat32 ${fat_start}M ${fat_end}M |
|
197 |
parted ${IMAGE_NAME} mkpart primary ext4 ${fat_end}M 100% |
|
198 |
sync |
|
199 |
|
|
200 |
pr_info "losetup system image on $LOOP_DEV" |
|
201 |
losetup /dev/${LOOP_DEV} ${IMAGE_NAME} |
|
202 |
kpartx -av /dev/${LOOP_DEV} |
|
203 |
|
|
204 |
pr_info "start format system image" |
|
205 |
mkfs.vfat /dev/mapper/${LOOP_DEV}p1 |
|
206 |
mkfs.ext4 /dev/mapper/${LOOP_DEV}p2 |
|
207 |
sync |
|
208 |
|
|
209 |
pr_info "start install u-boot image" |
|
210 |
dd if=$UBOOT_BINPATH/u-boot-${BOARD}.imx of=${IMAGE_NAME} bs=512 seek=$UBOOT_OFFSET_SECTOR conv=notrunc,sync |
|
211 |
|
|
212 |
pr_info "start install linux kernel images" |
|
213 |
mount -t vfat /dev/mapper/${LOOP_DEV}p1 ${MNT_POINT} |
|
214 |
cp -rf $KERNEL_BINPATH/Image ${MNT_POINT}/ |
|
215 |
cp -rf $KERNEL_BINPATH/${BOARD}.dtb ${MNT_POINT}/ |
|
216 |
|
|
217 |
sync && umount ${MNT_POINT} |
|
218 |
|
|
219 |
pr_info "update drivers in root filesystem" |
|
220 |
rm -rf $ROOTFS/lib/modules/ |
|
221 |
mkdir -p $ROOTFS/lib/modules/ |
|
222 |
cp -rf $KERNEL_BINPATH/lib/modules/[0-9]*\.[0-9]*\.[0-9]* $ROOTFS/lib/modules/ |
|
223 |
|
|
224 |
pr_info "start install root filesystem" |
|
225 |
mount -t ext4 /dev/mapper/${LOOP_DEV}p2 ${MNT_POINT} |
|
226 |
cp -af $ROOTFS/* ${MNT_POINT} |
|
227 |
sync && umount ${MNT_POINT} |
|
228 |
|
2334ec
|
229 |
pr_warn "Build system image $IMAGE_NAME done" |
849fbd
|
230 |
} |
G |
231 |
|
|
232 |
function exit_handler() |
|
233 |
{ |
|
234 |
pr_warn "Shell script exit now, do some clean work\n" |
|
235 |
set +e |
|
236 |
|
|
237 |
if mountpoint ${MNT_POINT} > /dev/null 2>&1 ; then |
|
238 |
pr_info "umount ${MNT_POINT}" |
|
239 |
umount ${MNT_POINT} > /dev/null 2>&1 |
|
240 |
fi |
|
241 |
|
|
242 |
rm -rf ${MNT_POINT} |
|
243 |
|
|
244 |
if [ -e /dev/mapper/${LOOP_DEV}p1 ] ; then |
|
245 |
pr_info "kpartx -dv /dev/${LOOP_DEV}" |
|
246 |
kpartx -dv /dev/${LOOP_DEV} |
|
247 |
fi |
|
248 |
|
|
249 |
losetup -a | grep "${LOOP_DEV}" > /dev/null 2>&1 |
|
250 |
if [ $? == 0 ] ; then |
|
251 |
pr_info "losetup -d /dev/${LOOP_DEV}" |
|
252 |
losetup -d /dev/${LOOP_DEV} |
|
253 |
fi |
|
254 |
} |
|
255 |
|
|
256 |
function do_build() |
|
257 |
{ |
|
258 |
cd $PRJ_PATH |
|
259 |
|
|
260 |
build_image |
|
261 |
} |
|
262 |
|
|
263 |
function do_install() |
|
264 |
{ |
|
265 |
cd $PRJ_PATH |
|
266 |
|
2334ec
|
267 |
if [[ `echo $COMPRESS | tr 'A-Z' 'a-z'` == "yes" ]] ; then |
G |
268 |
pr_info "Start bzip2 compress $IMAGE_NAME" |
|
269 |
rm -f $IMAGE_NAME.bz2 |
|
270 |
bzip2 $IMAGE_NAME |
|
271 |
fi |
|
272 |
|
849fbd
|
273 |
mkdir -p $PRFX_PATH |
2334ec
|
274 |
mv $IMAGE_NAME* $PRFX_PATH |
849fbd
|
275 |
cp $UBOOT_BINPATH/u-boot-${BOARD}.imx $PRFX_PATH |
G |
276 |
|
|
277 |
if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then |
|
278 |
pr_info "install bootloader and system image to '$INST_PATH'" |
|
279 |
cp $PRFX_PATH/u-boot-${BOARD}.imx $INST_PATH |
2334ec
|
280 |
cp $PRFX_PATH/$IMAGE_NAME* $INST_PATH |
849fbd
|
281 |
fi |
G |
282 |
} |
|
283 |
|
|
284 |
function do_clean() |
|
285 |
{ |
|
286 |
for d in rootfs-* |
|
287 |
do |
|
288 |
rm -rf $PRJ_PATH/$d |
|
289 |
done |
|
290 |
|
8e6109
|
291 |
rm -rf $PRJ_PATH/tarballs |
849fbd
|
292 |
rm -rf $PRFX_PATH |
G |
293 |
rm -f *.img |
|
294 |
} |
|
295 |
|
|
296 |
#+-------------------------+ |
|
297 |
#| Shell script body entry | |
|
298 |
#+-------------------------+ |
|
299 |
|
|
300 |
if [ `id -u` != 0 ] ; then |
|
301 |
pr_error "ERROR: This shell script must run as root" |
|
302 |
exit; |
|
303 |
fi |
|
304 |
|
|
305 |
cd $PRJ_PATH |
|
306 |
|
|
307 |
export_env |
|
308 |
|
|
309 |
if [[ $# == 1 && $1 == -c ]] ;then |
|
310 |
pr_warn "start clean system image" |
|
311 |
do_clean |
|
312 |
exit; |
|
313 |
fi |
|
314 |
|
2334ec
|
315 |
pr_warn "Build system image $IMAGE_NAME" |
849fbd
|
316 |
|
G |
317 |
trap 'exit_handler' EXIT |
|
318 |
|
|
319 |
do_fetch |
|
320 |
|
|
321 |
do_build |
|
322 |
|
|
323 |
do_install |
|
324 |
|