forked from eugene-tarassov/vivado-risc-v
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk-sd-image
executable file
·149 lines (122 loc) · 3.22 KB
/
mk-sd-image
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
#!/bin/bash
set -e
cd -P `dirname $0`
SD_SIZE=1500
SD_BOOT_SIZE=64
SD_SWAP_SIZE=
SD_IMG=debian-riscv64/debian-riscv64.sd.img
RSYNC_BOOT=
while getopts r:s:b:p:i: name ; do
case $name in
r)
RSYNC_BOOT=$OPTARG
;;
s)
SD_SIZE=$OPTARG
;;
b)
SD_BOOT_SIZE=$OPTARG
;;
p)
SD_SWAP_SIZE=$OPTARG
;;
i)
SD_IMG=$OPTARG
;;
esac
done
if [ ! -d linux-stable/kernel ] ; then
make update-submodules
fi
KERNEL_VER=$(cd linux-stable && git describe --exact-match --abbrev=0)
# --- Retrive Debian disk image ---
make debian-riscv64/initrd debian-riscv64/rootfs.tar.gz
# --- Build BBL and Linux ---
if [ ! -f workspace/boot.elf ] ; then
make bootloader
fi
if [ ! -f linux-stable/arch/riscv/boot/Image ] ; then
make linux
fi
# --- build SD card image ---
mount -l | grep `pwd`/ | while IFS=' ' read -ra LINE ; do
sudo umount ${LINE[0]}
done
losetup -a | grep `pwd`/ | while IFS=':' read -ra LINE ; do
sudo losetup -d ${LINE[0]}
done
losetup -a | grep `pwd`/ | while IFS=':' read -ra LINE ; do
echo "Cannot detach ${LINE[*]}"
exit 1
done
rm -f $SD_IMG
dd if=/dev/zero of=$SD_IMG bs=1M count=$SD_SIZE
sudo losetup -f $SD_IMG
SD_LOOP=$(
losetup -a | grep `pwd`/ | while IFS=':' read -ra LINE ; do
echo ${LINE[0]}
done
)
echo "SD image device: ${SD_LOOP}"
sudo sfdisk --no-tell-kernel ${SD_LOOP} <<-__EOF__
1M,${SD_BOOT_SIZE}M,0xE,*
,,,-
__EOF__
sudo partprobe ${SD_LOOP}
UUID=68d82fa1-1bb5-435f-a5e3-862176586eec
sudo mkfs.vfat -F 16 -n BOOT ${SD_LOOP}p1
sudo mkfs.ext4 -E nodiscard -L rootfs -U $UUID ${SD_LOOP}p2
cat >debian-riscv64/extlinux.conf <<EOF
menu title RISC-V Boot Options.
timeout 50
default Debain $KERNEL_VER
label Debain $KERNEL_VER
kernel /extlinux/image-$KERNEL_VER
initrd /extlinux/initrd-$KERNEL_VER.img
append ro root=UUID=$UUID earlycon initramfs.runsize=24M locale.LANG=en_US.UTF-8
EOF
mkdir -p debian-riscv64/boot
mkdir -p debian-riscv64/rootfs
sudo mount ${SD_LOOP}p1 debian-riscv64/boot
sudo mount ${SD_LOOP}p2 debian-riscv64/rootfs
pushd debian-riscv64/rootfs
if [ -z "$SD_SWAP_SIZE" ] ; then
sudo tar xzf ../rootfs.tar.gz
else
sudo tar --exclude=swapfile -xzf ../rootfs.tar.gz
sudo fallocate -l ${SD_SWAP_SIZE}M swapfile
sudo chmod 600 swapfile
sudo mkswap swapfile
fi
popd
pushd debian-riscv64/boot
sudo mkdir extlinux
sudo cp ../extlinux.conf extlinux
sudo cp ../initrd extlinux/initrd-$KERNEL_VER.img
sudo cp ../../linux-stable/arch/riscv/boot/Image extlinux/image-$KERNEL_VER
sudo cp ../../workspace/boot.elf boot.elf
popd
sudo chown root:root debian-riscv64/rootfs
sudo chmod 755 debian-riscv64/rootfs
echo
echo "Boot partition:"
df debian-riscv64/boot
ls -l debian-riscv64/boot
echo
echo "Root partition:"
df debian-riscv64/rootfs
ls -l debian-riscv64/rootfs
echo
if [ ! -z "$RSYNC_BOOT" ] ; then
rsync -r --delete debian-riscv64/boot/ $RSYNC_BOOT
fi
# According to docs, don't need to run sync before umount.
# umount will complete all pending writes before it actually unmounts the filesystem.
# In reality, without sync, VFAT filesystem sometimes gets corrupted after umount.
# Must be a Linux bug.
sync
sudo umount ${SD_LOOP}p1
sudo umount ${SD_LOOP}p2
sudo fsck -f -p -T ${SD_LOOP}p1 || true
sudo fsck -f -p -T ${SD_LOOP}p2
sudo losetup -d ${SD_LOOP}