forked from QQxiaoming/quard_star_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·433 lines (407 loc) · 15.3 KB
/
build.sh
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
#!/bin/bash
set -e
PERMISSION_TOOL=""
UNAMEOUT="$(uname -s)"
case "${UNAMEOUT}" in
Linux*)
HOST_USER_NAME="$(whoami)"
case "${HOST_USER_NAME}" in
root)
PERMISSION_TOOL=""
;;
*)
PERMISSION_TOOL="pkexec"
;;
esac
PROCESSORS=$(< /proc/cpuinfo grep "processor" | wc -l)
;;
Darwin*)
PROCESSORS=$(sysctl -n machdep.cpu.thread_count)
;;
*)
PROCESSORS=4
;;
esac
SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
if [ -z $TOOLCHAIN_ROOT_PATH ]; then
TOOLCHAIN_ROOT_PATH=/opt
fi
GLIB_ELF_CROSS_COMPILE_DIR=$TOOLCHAIN_ROOT_PATH/gcc-riscv64-unknown-linux-gnu
GLIB_ELF_CROSS_PREFIX=$GLIB_ELF_CROSS_COMPILE_DIR/bin/riscv64-unknown-linux-gnu
GLIB_ELF_CROSS_PREFIX_SYSROOT_DIR=$GLIB_ELF_CROSS_COMPILE_DIR/sysroot
NEWLIB_ELF_CROSS_COMPILE_DIR=$TOOLCHAIN_ROOT_PATH/gcc-riscv64-unknown-elf
NEWLIB_ELF_CROSS_PREFIX=$NEWLIB_ELF_CROSS_COMPILE_DIR/bin/riscv64-unknown-elf
BUILD_TARGET=$1
BUILD_ROOTFS_OPT=$2
if [ ! -d "$SHELL_FOLDER/output" ]; then
mkdir $SHELL_FOLDER/output
fi
build_qemu()
{
echo "------------------------------ 编译qemu ------------------------------"
cd $SHELL_FOLDER/qemu-8.0.0
if [ ! -d "$SHELL_FOLDER/output/qemu" ]; then
./configure --prefix=$SHELL_FOLDER/output/qemu --target-list=riscv64-softmmu --enable-gtk --enable-virtfs --disable-gio --enable-plugins --audio-drv-list=pa,alsa,sdl,oss
fi
make -j$PROCESSORS
make install
}
build_qemu_w64()
{
echo "---------------------------- 编译qemu_w64 ----------------------------"
cd $SHELL_FOLDER/qemu-8.0.0
if [ ! -d "$SHELL_FOLDER/output/qemu_w64" ]; then
./configure --prefix=$SHELL_FOLDER/output/qemu_w64 --cross-prefix=x86_64-w64-mingw32- --target-list=riscv64-softmmu --enable-gtk --disable-gio
fi
make -j$PROCESSORS
make install
}
build_qemu_macos()
{
echo "---------------------------- 编译qemu_macos ----------------------------"
cd $SHELL_FOLDER/qemu-8.0.0
if [ ! -d "$SHELL_FOLDER/output/qemu_macos" ]; then
./configure --prefix=$SHELL_FOLDER/output/qemu_macos --target-list=riscv64-softmmu --enable-virtfs
fi
make -j$PROCESSORS
make install
}
build_mask_rom()
{
echo "---------------------------- 编译mask_rom ----------------------------"
if [ ! -d "$SHELL_FOLDER/output/mask_rom" ]; then
mkdir $SHELL_FOLDER/output/mask_rom
fi
cd $SHELL_FOLDER/mask_rom
make CROSS_COMPILE=$NEWLIB_ELF_CROSS_PREFIX- clean
make CROSS_COMPILE=$NEWLIB_ELF_CROSS_PREFIX- -j$PROCESSORS
cp ./build/mask_rom.* $SHELL_FOLDER/output/mask_rom/
}
build_lowlevelboot()
{
echo "-------------------------- 编译lowlevelboot --------------------------"
if [ ! -d "$SHELL_FOLDER/output/lowlevelboot" ]; then
mkdir $SHELL_FOLDER/output/lowlevelboot
fi
cd $SHELL_FOLDER/lowlevelboot
make CROSS_COMPILE=$NEWLIB_ELF_CROSS_PREFIX- clean
make CROSS_COMPILE=$NEWLIB_ELF_CROSS_PREFIX- -j$PROCESSORS
cp ./build/lowlevelboot.* $SHELL_FOLDER/output/lowlevelboot/
}
build_opensbi()
{
echo "---------------------------- 编译opensbi -----------------------------"
if [ ! -d "$SHELL_FOLDER/output/opensbi" ]; then
mkdir $SHELL_FOLDER/output/opensbi
fi
cd $SHELL_FOLDER/opensbi-1.2
make CROSS_COMPILE=$GLIB_ELF_CROSS_PREFIX- PLATFORM=quard_star
cp -r $SHELL_FOLDER/opensbi-1.2/build/platform/quard_star/firmware/fw_jump.bin $SHELL_FOLDER/output/opensbi/fw_jump.bin
cp -r $SHELL_FOLDER/opensbi-1.2/build/platform/quard_star/firmware/fw_jump.elf $SHELL_FOLDER/output/opensbi/fw_jump.elf
$GLIB_ELF_CROSS_PREFIX-objdump --source --demangle --disassemble --reloc --wide $SHELL_FOLDER/output/opensbi/fw_jump.elf > $SHELL_FOLDER/output/opensbi/fw_jump.lst
}
build_sbi_dtb()
{
echo "---------------------------- 生成sbi.dtb -----------------------------"
cd $SHELL_FOLDER/dts
cpp -nostdinc -I include -undef -x assembler-with-cpp quard_star_sbi.dts > quard_star_sbi.dtb.dts.tmp
dtc -i $SHELL_FOLDER/dts -I dts -O dtb -o $SHELL_FOLDER/output/opensbi/quard_star_sbi.dtb quard_star_sbi.dtb.dts.tmp
}
build_trusted_domain()
{
echo "------------------------- 编译trusted_domain -------------------------"
if [ ! -d "$SHELL_FOLDER/output/trusted_domain" ]; then
mkdir $SHELL_FOLDER/output/trusted_domain
fi
cd $SHELL_FOLDER/trusted_domain
make CROSS_COMPILE=$NEWLIB_ELF_CROSS_PREFIX- clean
make CROSS_COMPILE=$NEWLIB_ELF_CROSS_PREFIX- -j$PROCESSORS
cp ./build/trusted_fw.* $SHELL_FOLDER/output/trusted_domain/
}
build_uboot_dtb()
{
echo "--------------------------- 生成uboot.dtb ----------------------------"
if [ ! -d "$SHELL_FOLDER/output/uboot" ]; then
mkdir $SHELL_FOLDER/output/uboot
fi
cd $SHELL_FOLDER/dts
cpp -nostdinc -I include -undef -x assembler-with-cpp quard_star_uboot.dts > quard_star_uboot.dtb.dts.tmp
dtc -I dts -O dtb -o $SHELL_FOLDER/output/uboot/quard_star_uboot.dtb quard_star_uboot.dtb.dts.tmp
cpp -nostdinc -I include -undef -x assembler-with-cpp quard_star_uboot_kgdb.dts > quard_star_uboot_kgdb.dtb.dts.tmp
dtc -I dts -O dtb -o $SHELL_FOLDER/output/uboot/quard_star_uboot_kgdb.dtb quard_star_uboot_kgdb.dtb.dts.tmp
if [ -f "$SHELL_FOLDER/u-boot-2023.04/tools/mkimage" ]; then
$SHELL_FOLDER/u-boot-2023.04/tools/mkimage -A riscv -O linux -T script -C none -a 0 -e 0 -n "Distro Boot Script" -d $SHELL_FOLDER/dts/quard_star_uboot.cmd $SHELL_FOLDER/output/uboot/boot.scr
fi
}
build_uboot()
{
echo "----------------------------- 编译uboot ------------------------------"
if [ ! -d "$SHELL_FOLDER/output/uboot" ]; then
mkdir $SHELL_FOLDER/output/uboot
fi
cd $SHELL_FOLDER/u-boot-2023.04
make CROSS_COMPILE=$GLIB_ELF_CROSS_PREFIX- qemu-quard-star_defconfig
make CROSS_COMPILE=$GLIB_ELF_CROSS_PREFIX- -j$PROCESSORS DEVICE_TREE=../../../../output/uboot/quard_star_uboot
cp $SHELL_FOLDER/u-boot-2023.04/u-boot $SHELL_FOLDER/output/uboot/u-boot.elf
cp $SHELL_FOLDER/u-boot-2023.04/u-boot.map $SHELL_FOLDER/output/uboot/u-boot.map
cp $SHELL_FOLDER/u-boot-2023.04/u-boot.bin $SHELL_FOLDER/output/uboot/u-boot.bin
$GLIB_ELF_CROSS_PREFIX-objdump --source --demangle --disassemble --reloc --wide $SHELL_FOLDER/output/uboot/u-boot.elf > $SHELL_FOLDER/output/uboot/u-boot.lst
cd $SHELL_FOLDER/dts
$SHELL_FOLDER/u-boot-2023.04/tools/mkimage -A riscv -O linux -T script -C none -a 0 -e 0 -n "Distro Boot Script" -d $SHELL_FOLDER/dts/quard_star_uboot.cmd $SHELL_FOLDER/output/uboot/boot.scr
}
build_firmware()
{
echo "--------------------------- 合成firmware固件 ---------------------------"
if [ ! -f "$SHELL_FOLDER/output/lowlevelboot/lowlevelboot.bin" ]; then
echo "not found lowlevelboot.bin, please ./build.sh lowlevelboot"
exit 255
fi
if [ ! -f "$SHELL_FOLDER/output/opensbi/quard_star_sbi.dtb" ]; then
echo "not found quard_star_sbi.dtb, please ./build.sh sbi_dtb"
exit 255
fi
if [ ! -f "$SHELL_FOLDER/output/uboot/quard_star_uboot.dtb" ]; then
echo "not found quard_star_uboot.dtb, please ./build.sh uboot_dtb"
exit 255
fi
if [ ! -f "$SHELL_FOLDER/output/opensbi/fw_jump.bin" ]; then
echo "not found fw_jump.bin, please ./build.sh opensbi"
exit 255
fi
if [ ! -f "$SHELL_FOLDER/output/trusted_domain/trusted_fw.bin" ]; then
echo "not found trusted_fw.bin, please ./build.sh trusted_domain"
exit 255
fi
if [ ! -f "$SHELL_FOLDER/output/uboot/u-boot.bin" ]; then
echo "not found u-boot.bin, please ./build.sh uboot"
exit 255
fi
if [ ! -d "$SHELL_FOLDER/output/fw" ]; then
mkdir $SHELL_FOLDER/output/fw
fi
cd $SHELL_FOLDER/output/fw
rm -rf fw.bin
dd of=fw.bin bs=1k count=12k if=/dev/zero
dd of=fw.bin bs=1k conv=notrunc seek=0 if=$SHELL_FOLDER/output/lowlevelboot/lowlevelboot.bin
dd of=fw.bin bs=1k conv=notrunc seek=512 if=$SHELL_FOLDER/output/opensbi/quard_star_sbi.dtb
dd of=fw.bin bs=1k conv=notrunc seek=1K if=$SHELL_FOLDER/output/uboot/quard_star_uboot.dtb
dd of=fw.bin bs=1k conv=notrunc seek=2K if=$SHELL_FOLDER/output/opensbi/fw_jump.bin
dd of=fw.bin bs=1k conv=notrunc seek=4K if=$SHELL_FOLDER/output/trusted_domain/trusted_fw.bin
dd of=fw.bin bs=1k conv=notrunc seek=8K if=$SHELL_FOLDER/output/uboot/u-boot.bin
rm -rf pflash.img
dd bs=1k count=32k if=/dev/zero | tr '\000' '\377' > pflash.img
dd of=pflash.img bs=1k conv=notrunc seek=0 if=fw.bin
if [ ! -f "$SHELL_FOLDER/output/fw/norflash.img" ]; then
dd bs=1k count=32k if=/dev/zero | tr '\000' '\377' > norflash.img
rm -rf jff2s_dir jffs2.bin
mkdir jff2s_dir
mkfs.jffs2 -lqn -e64 -s320 -p0x1400000 -r jff2s_dir -o jffs2.bin
dd of=norflash.img bs=1k conv=notrunc seek=12K if=jffs2.bin
rm -rf jff2s_dir jffs2.bin
fi
dd of=norflash.img bs=1k conv=notrunc seek=0 if=fw.bin
if [ ! -f "$SHELL_FOLDER/output/fw/sd.img" ]; then
dd bs=1k count=32k if=/dev/zero | tr '\000' '\377' > sd.img
fi
dd of=sd.img bs=1k conv=notrunc seek=0 if=fw.bin
if [ ! -f "$SHELL_FOLDER/output/fw/usb.img" ]; then
dd bs=1k count=32k if=/dev/zero | tr '\000' '\377' > usb.img
fi
if [ ! -f "$SHELL_FOLDER/output/fw/nandflash.img" ]; then
# 256 + 8 = 264
dd bs=1k count=264k if=/dev/zero | tr '\000' '\377' > nandflash.img
fi
}
build_kernel()
{
echo "-------------------------- 编译linux kernel --------------------------"
if [ ! -d "$SHELL_FOLDER/output/linux_kernel" ]; then
mkdir $SHELL_FOLDER/output/linux_kernel
fi
cd $SHELL_FOLDER/linux-6.1.11
make ARCH=riscv CROSS_COMPILE=$GLIB_ELF_CROSS_PREFIX- quard_star_defconfig
make ARCH=riscv CROSS_COMPILE=$GLIB_ELF_CROSS_PREFIX- -j$PROCESSORS
#make ARCH=riscv CROSS_COMPILE=$GLIB_ELF_CROSS_PREFIX- tools/perf -j$PROCESSORS
cp $SHELL_FOLDER/linux-6.1.11/arch/riscv/boot/Image $SHELL_FOLDER/output/linux_kernel/Image
}
build_busybox()
{
echo "---------------------------- 编译busybox -----------------------------"
if [ ! -d "$SHELL_FOLDER/output/busybox" ]; then
mkdir $SHELL_FOLDER/output/busybox
fi
cd $SHELL_FOLDER/busybox-1.33.2
make ARCH=riscv CROSS_COMPILE=$GLIB_ELF_CROSS_PREFIX- quard_star_defconfig
make ARCH=riscv CROSS_COMPILE=$GLIB_ELF_CROSS_PREFIX- -j$PROCESSORS
make ARCH=riscv CROSS_COMPILE=$GLIB_ELF_CROSS_PREFIX- install
}
build_rootfs()
{
echo "----------------------------- 合成文件系统映像 -----------------------------"
if [ ! -f "$SHELL_FOLDER/output/linux_kernel/Image" ]; then
echo "not found Image, please ./build.sh kernel"
exit 255
fi
if [ ! -f "$SHELL_FOLDER/output/uboot/quard_star_uboot.dtb" ]; then
echo "not found quard_star_uboot.dtb, please ./build.sh uboot_dtb"
exit 255
fi
if [ ! -d "$SHELL_FOLDER/output/busybox" ]; then
echo "not found busybox, please ./build.sh busybox"
exit 255
fi
MAKE_ROOTFS_DIR=$SHELL_FOLDER/output/rootfs
TARGET_ROOTFS_DIR=$MAKE_ROOTFS_DIR/rootfs
TARGET_BOOTFS_DIR=$MAKE_ROOTFS_DIR/bootfs
if [ ! -d "$MAKE_ROOTFS_DIR" ]; then
BUILD_ROOTFS_OPT="all"
fi
if [ ! -f "$MAKE_ROOTFS_DIR/rootfs.img" ]; then
BUILD_ROOTFS_OPT="all"
fi
case "$BUILD_ROOTFS_OPT" in
all)
if [ ! -d "$MAKE_ROOTFS_DIR" ]; then
mkdir $MAKE_ROOTFS_DIR
fi
if [ ! -d "$TARGET_ROOTFS_DIR" ]; then
mkdir $TARGET_ROOTFS_DIR
fi
if [ ! -d "$TARGET_BOOTFS_DIR" ]; then
mkdir $TARGET_BOOTFS_DIR
fi
cd $MAKE_ROOTFS_DIR
if [ ! -f "$MAKE_ROOTFS_DIR/rootfs.img" ]; then
dd if=/dev/zero of=rootfs.img bs=1M count=4096
$PERMISSION_TOOL $SHELL_FOLDER/build_rootfs/generate_rootfs.sh $MAKE_ROOTFS_DIR/rootfs.img $SHELL_FOLDER/build_rootfs/sfdisk
fi
cp $SHELL_FOLDER/output/linux_kernel/Image $TARGET_BOOTFS_DIR/Image
cp $SHELL_FOLDER/output/uboot/quard_star_uboot.dtb $TARGET_BOOTFS_DIR/quard_star.dtb
$SHELL_FOLDER/u-boot-2023.04/tools/mkimage -A riscv -O linux -T script -C none -a 0 -e 0 -n "Distro Boot Script" -d $SHELL_FOLDER/dts/quard_star_uboot.cmd $TARGET_BOOTFS_DIR/boot.scr
cp -r $SHELL_FOLDER/output/busybox/* $TARGET_ROOTFS_DIR/
cp -r $SHELL_FOLDER/target_root_script/* $TARGET_ROOTFS_DIR/
if [ ! -d "$TARGET_ROOTFS_DIR/proc" ]; then
mkdir $TARGET_ROOTFS_DIR/proc
fi
if [ ! -d "$TARGET_ROOTFS_DIR/sys" ]; then
mkdir $TARGET_ROOTFS_DIR/sys
fi
if [ ! -d "$TARGET_ROOTFS_DIR/dev" ]; then
mkdir $TARGET_ROOTFS_DIR/dev
fi
if [ ! -d "$TARGET_ROOTFS_DIR/tmp" ]; then
mkdir $TARGET_ROOTFS_DIR/tmp
fi
if [ ! -d "$TARGET_ROOTFS_DIR/mnt" ]; then
mkdir $TARGET_ROOTFS_DIR/mnt
fi
if [ ! -d "$TARGET_ROOTFS_DIR/var" ]; then
mkdir $TARGET_ROOTFS_DIR/var
mkdir $TARGET_ROOTFS_DIR/var/log
mkdir $TARGET_ROOTFS_DIR/var/run
fi
if [ ! -d "$TARGET_ROOTFS_DIR/lib" ]; then
mkdir $TARGET_ROOTFS_DIR/lib
cd $TARGET_ROOTFS_DIR
ln -s ./lib ./lib64
cd $MAKE_ROOTFS_DIR
fi
cp -r $GLIB_ELF_CROSS_PREFIX_SYSROOT_DIR/lib/* $TARGET_ROOTFS_DIR/lib/
cp -r $GLIB_ELF_CROSS_PREFIX_SYSROOT_DIR/usr/bin/* $TARGET_ROOTFS_DIR/usr/bin/
$SHELL_FOLDER/build_rootfs/clean_gitkeep.sh $TARGET_BOOTFS_DIR
$SHELL_FOLDER/build_rootfs/clean_gitkeep.sh $TARGET_ROOTFS_DIR
$PERMISSION_TOOL $SHELL_FOLDER/build_rootfs/build_fs.sh $MAKE_ROOTFS_DIR
;;
bootfs)
if [ ! -d "$TARGET_BOOTFS_DIR" ]; then
mkdir $TARGET_ROOTFS_DIR
fi
cp $SHELL_FOLDER/output/linux_kernel/Image $TARGET_BOOTFS_DIR/Image
cp $SHELL_FOLDER/output/uboot/quard_star_uboot.dtb $TARGET_BOOTFS_DIR/quard_star.dtb
cp $SHELL_FOLDER/output/uboot/boot.scr $TARGET_BOOTFS_DIR/boot.scr
$SHELL_FOLDER/build_rootfs/clean_gitkeep.sh $TARGET_BOOTFS_DIR
$PERMISSION_TOOL $SHELL_FOLDER/build_rootfs/build_fs_only_bootfs.sh $MAKE_ROOTFS_DIR
;;
*)
echo "skip build rootfs.img!"
;;
esac
}
build_all()
{
build_qemu
build_mask_rom
build_lowlevelboot
build_opensbi
build_sbi_dtb
build_trusted_domain
build_uboot_dtb
build_uboot
build_firmware
build_kernel
build_busybox
build_rootfs
}
echo_usage()
{
TARGET="qemu|mask_rom|lowlevelboot|opensbi|sbi_dtb|trusted_domain|uboot|uboot_dtb|firmware|kernel|busybox|rootfs|all"
ROOTFS_OPT="all|bootfs"
USAGE="usage $0 [$TARGET] [$ROOTFS_OPT]"
echo $USAGE
}
case "$BUILD_TARGET" in
--help)
echo_usage
exit 0
;;
qemu)
build_qemu
;;
qemu_w64)
build_qemu_w64
;;
qemu_macos)
build_qemu_macos
;;
mask_rom)
build_mask_rom
;;
lowlevelboot)
build_lowlevelboot
;;
opensbi)
build_opensbi
;;
sbi_dtb)
build_sbi_dtb
;;
trusted_domain)
build_trusted_domain
;;
uboot)
build_uboot
;;
uboot_dtb)
build_uboot_dtb
;;
firmware)
build_firmware
;;
kernel)
build_kernel
;;
busybox)
build_busybox
;;
rootfs)
build_rootfs
;;
all)
build_all
;;
*)
echo_usage
exit 255
;;
esac
echo "----------------------------------- 完成 -----------------------------------"