-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbuildimg.sh
More file actions
executable file
·276 lines (237 loc) · 9.56 KB
/
buildimg.sh
File metadata and controls
executable file
·276 lines (237 loc) · 9.56 KB
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
#!/bin/bash
#sudo apt-get install qemu-user-static debootstrap binfmt-support
#r2: dev: 0 part: 1/2
#r64: dev: 1 part: 4/5 (maybe needs fix for root in uboot, boot is checked by checkgpt)
#r2pro: dev: 1 part: 2/3
#r3: dev: 0 part: 5/6
board=$1
distro=$2 #bookworm|noble
kernel="6.18"
device="sdmmc"
source config.sh
if [[ ! "$distro" =~ bookworm|noble|trixie ]];
then
echo "invalid distribution '$distro'";
exit;
fi
if [[ -n "$3" ]] && [[ "$3" =~ ^[1-9]\.[0-9]+$ ]];then kernel=$3;fi
if [[ -n "$4" ]] && [[ "$4" =~ ^(sdmmc|emmc)$ ]];then device=$4;fi
PACKAGE_Error=0
PACKAGES=$(dpkg -l | awk '{print $2}')
NEEDED_PKGS="python3 python3-requests parted qemu-user-static debootstrap binfmt-support"
echo "needed: $NEEDED_PKGS"
for package in $NEEDED_PKGS; do
#TESTPKG=$(dpkg -l |grep "\s${package}")
TESTPKG=$(echo "$PACKAGES" |grep "^${package}")
if [[ -z "${TESTPKG}" ]];then echo "please install ${package}";PACKAGE_Error=1;fi
done
if [ ${PACKAGE_Error} == 1 ]; then exit 1; fi
LDEV=`sudo losetup -f`
function cleanup() {
sudo umount mnt/BPI-ROOT/proc
sudo umount mnt/BPI-BOOT
sudo umount mnt/BPI-ROOT
sudo losetup -d $1
}
trap ctrl_c INT
function ctrl_c() {
echo "** Trapped CTRL-C ($LDEV)"
cleanup $LDEV
exit 1
}
echo "create image for ${board} (${arch}) ${distro} ${kernel} ${device}"
python3 downloadfiles.py ${board} ${kernel} ${device}
ls -lh sourcefiles_${board}.conf
if [[ $? -ne 0 ]];then echo "sourcefiles_$board.conf file missing"; exit 1; fi
. sourcefiles_${board}.conf
echo "image-file: "$imgfile
echo "kernel-file: "$kernelfile
if [[ ! -e ${distro}_${arch}.tar.gz ]];then
./buildchroot.sh ${arch} ${distro}
else
echo "packed rootfs already exists"
fi
ls -lh ${imgfile}
if [[ $? -ne 0 ]];then echo "bootloader file missing"; exit 1; fi
ls -lh ${distro}_${arch}.tar.gz
if [[ $? -ne 0 ]];then echo "rootfs file missing"; exit 1; fi
if [[ -z "${kernelfile}" ]];then
kernel="nokernel"
else
kernel=$(echo ${kernelfile}|sed -e 's/^.*_\(.*\).tar.gz/\1/')
fi
newimgfile=${variant:-$board}_${distro}_${kernel}_${device}.img.gz
cp $imgfile $newimgfile
echo "unpack imgfile ($newimgfile)..."
gunzip $newimgfile
echo "setting up imgfile to loopdev..."
sudo losetup ${LDEV} ${newimgfile%.*} 1> /dev/null
if [[ $? -ne 0 ]];then echo "losetup ${LDEV} failed (${newimgfile%.*})"; exit 1; fi
echo "mounting loopdev..."
sudo partprobe ${LDEV}
if [[ $? -ne 0 ]];then echo "partprobe failed"; exit 1; fi
#replacing bl2 (e.g. R4 8GB)
if [[ -n "$replacebl2" ]] && [[ -n "$bl2file" ]] && [[ -e "$bl2file" ]];then
echo "replace BL2 with $bl2file";
if [[ "$board" =~ ^bpi-r4 ]];then
sudo dd if=$bl2file of=${LDEV}p1
else
echo "BL2 replacement for $board not supported"
fi
fi
mkdir -p mnt/BPI-{B,R}OOT
sudo mount ${LDEV}p${mmcbootpart} mnt/BPI-BOOT
if [[ $? -ne 0 ]];then echo "mounting BPI-BOOT failed"; exit 1; fi
sudo mkdir -p mnt/BPI-BOOT/${ubootconfigdir}
sudo touch mnt/BPI-BOOT/${ubootconfigdir}/${ubootconfig}
sudo mount ${LDEV}p${mmcrootpart} mnt/BPI-ROOT
if [[ $? -ne 0 ]];then echo "mounting BPI-ROOT failed"; exit 1; fi
echo "unpack rootfs to bpi-root loopdev..."
sudo tar -xzf ${distro}_${arch}.tar.gz -C mnt/BPI-ROOT
if [[ -n "${kernelfile}" ]];then
ls -lh ${kernelfile}
if [[ $? -ne 0 ]];then echo "kernel file missing"; exit 1; fi
echo "unpack kernel to bpi-boot loopdev..."
sudo tar -xzf $kernelfile --strip-components=1 -C mnt/BPI-BOOT BPI-BOOT
ls -lR mnt/BPI-BOOT
echo "unpack kernel-modules to bpi-root loopdev..."
sudo tar -xzf $kernelfile --strip-components=2 -C mnt/BPI-ROOT/lib/. BPI-ROOT/lib/
else
echo "kernelfile is empty so it will be missing in resulting image..."
fi
if [[ "$board" == "bpi-r2pro" ]];then
conffile=mnt/BPI-BOOT/extlinux/extlinux.conf
#mkdir -p $(dirname ${conffile})
imgname="Image.gz"
dtbname="bpi-r2pro.dtb"
ls -la $(dirname ${conffile})
echo -e "menu title Select the boot mode\n#timeout 1/10s\nTIMEOUT 50\nDEFAULT linux" | sudo tee $conffile
echo -e "LABEL linux\n linux $imgname\n fdt $dtbname\n"\
" append earlycon=uart8250,mmio32,0xfe660000 " \
"console=ttyS2,1500000n8 root=/dev/mmcblk${mmcdev}p${mmcrootpart} rootwait rw " \
"earlyprintk" | sudo tee -a $conffile
cat ${conffile}
fi
echo "configure rootfs for ${board}..."
targetdir="mnt/BPI-ROOT"
sudo mount -o bind /proc $targetdir/proc
sudo chroot $targetdir tee "/etc/fstab" > /dev/null <<EOF
# <file system> <dir> <type> <options> <dump> <pass>
/dev/mmcblk${mmcdev}p${mmcbootpart} /boot vfat errors=remount-ro 0 1
/dev/mmcblk${mmcdev}p${mmcrootpart} / ext4 defaults 0 0
EOF
echo $board | sudo tee $targetdir/etc/hostname
sudo chroot $targetdir bash -c "apt update; DEBIAN_FRONTEND=noninteractive apt upgrade -y; apt clean"
if [[ ${board} != "bpi-r2pro" ]];then
sudo chroot $targetdir bash -c "apt install --no-install-recommends -y hostapd iw xz-utils"
fi
sudo chroot $targetdir bash -c "apt update; apt install --no-install-recommends -y nftables ${additional_pkgs}"
sudo cp -r conf/generic/* ${targetdir}/
if [[ -e conf/$board ]];then
sudo rsync -av --partial --progress --exclude={'bin','lib','sbin'} conf/${board}/. ${targetdir}/
#fix for copy dir over symlink (rejected by cp)
for d in bin lib sbin;do
if [[ -d conf/${board}/$d ]];then
sudo cp -r conf/${board}/$d/* ${targetdir}/$d/
fi
done
fi
sudo chroot $targetdir bash -c "systemctl enable systemd-networkd"
function download_firmware()
{
targetdir=$1
shift 1 # Removes $1 from the parameter list
fwdir=${targetdir}/lib/firmware/
for f in $@;do
src="https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/plain/$f";
echo "$src => $fwdir/$f"
sudo curl -L --silent --create-dirs --output $fwdir/$f $src
ret=$?
if [[ $ret -ne 0 ]];then return $ret;fi
done
return 0
}
#wifi related commands
if [[ ${board} != "bpi-r2pro" ]];then
#fix for ConditionFileNotEmpty in existing hostapd service (can also point to ${DAEMON_CONF})
if [[ ${board} == "bpi-r2" ]];then
sudo mv $targetdir/etc/hostapd/hostapd_{wlan,ap}0.conf
sudo sed -i 's/^#\(interface=ap0\)/\1/' $targetdir/etc/hostapd/hostapd_ap0.conf
cat $targetdir/etc/hostapd/hostapd_ap0.conf
sudo chroot $targetdir bash -c "ln -fs hostapd_ap0.conf /etc/hostapd/hostapd.conf"
#unpack wmt-tools
sudo tar -xzf $kernelfile --strip-components=1 -C $targetdir BPI-ROOT/{etc,usr,system}
#disable ip setting and dnsmasq (done via systemd-config)
sudo sed -i "/hostapd started...set IP/,/service dnsmasq restart/d" $targetdir/usr/sbin/wifi.sh
#add wifi.sh to rc.local (autostart)
sudo sed -i '/^exit/s/^/\/usr\/sbin\/wifi.sh &\n/' $targetdir/etc/rc.local
else
for a in hostapd iperf wpa_supplicant iproute2;
do
varname="replace${a}"
if [[ -n "${!varname}" ]];then
varname2="${a}file"
#tar -tzf ${!varname2} #currently only show content
echo "unpack $a to bpi-root loopdev..."
if [[ "$a" =~ hostap|wpa_supplicant ]];then
sudo tar -xzf ${!varname2} -C mnt/BPI-ROOT/usr/local/sbin/
else
sudo tar -xzf ${!varname2} -C mnt/BPI-ROOT/usr/local/
fi
fi
done
ls -lh mnt/BPI-ROOT/usr/local/{,s}bin/
ls -lh mnt/BPI-ROOT/etc/hostapd/
fi
#copy firmware
if [[ ! -d firmware ]];
then
./getfirmware.sh
fi
echo "copy firmware files"
sudo cp -r firmware/* ${targetdir}/lib/firmware/
regulatory_repo=https://git.kernel.org/pub/scm/linux/kernel/git/wens/wireless-regdb.git
curl $regulatory_repo/plain/regulatory.db -o regulatory.db-git
curl $regulatory_repo/plain/regulatory.db.p7s -o regulatory.db.p7s-git
sudo cp -r regulatory.* ${targetdir}/lib/firmware/
sudo chroot $targetdir bash -c "update-alternatives --install /lib/firmware/regulatory.db regulatory.db /lib/firmware/regulatory.db-git 200 --slave /lib/firmware/regulatory.db.p7s regulatory.db.p7s /lib/firmware/regulatory.db.p7s-git"
sudo chroot $targetdir bash -c "update-alternatives --set regulatory.db /lib/firmware/regulatory.db-git"
if [[ ${board} == "bpi-r64" ]];then
echo "mt7615e" | sudo tee -a ${targetdir}/etc/modules
fi
if [[ ${board} == "bpi-r4" ]];then
#remove lan0 from lanbr0
sudo sed 's/lan0 //' ${targetdir}/etc/systemd/network/21-lanbr-bind.network
#copy actual firmware files to image
download_firmware $targetdir mediatek/mt7996/{mt7996_dsp,mt7996_eeprom_233,mt7996_rom_patch_233,mt7996_wa_233,mt7996_wm_233}.bin
download_firmware $targetdir mediatek/mt7988/i2p5ge-phy-pmb.bin
download_firmware $targetdir aeonsemi/as21x1x_fw.bin
sudo ls -lRh $fwdir
#changes for 2.5g phy and R4Pro/R4Lite variant
if [[ "$variant" == "bpi-r4-2g5" ]];then pfx=""; else pfx="# "; fi
echo "${pfx}is2g5=1" | sudo tee -a mnt/BPI-BOOT/${ubootconfigdir}/${ubootconfig}
if [[ "$variant" == "bpi-r4pro" ]];then pfx=""; else pfx="# "; fi
echo "${pfx}isr4pro=1" | sudo tee -a mnt/BPI-BOOT/${ubootconfigdir}/${ubootconfig}
if [[ "$variant" == "bpi-r4lite" ]];then pfx=""; else pfx="# "; fi
echo "${pfx}isr4lite=1" | sudo tee -a mnt/BPI-BOOT/${ubootconfigdir}/${ubootconfig}
echo "# mtk-2p5ge" | sudo tee -a ${targetdir}/etc/modules
#linux changes
if [[ "$variant" == "bpi-r4pro" ]];then
sudo rm $targetdir/etc/systemd/network/1*-wan.*
fi
fi
fi
#install userspecified packages
if [[ -n "$userpackages" ]]; then
echo "installing user specified packages: $userpackages"
sudo chroot $targetdir bash -c "DEBIAN_FRONTEND=noninteractive apt install -y $userpackages"
fi
#install/start resolved after all is done (resolving is broken in chroot after that)
sudo chroot $targetdir bash -c "apt install -y systemd-resolved;systemctl enable systemd-resolved"
cleanup ${LDEV}
echo "packing ${newimgfile}"
gzip ${newimgfile%.*}
md5sum ${newimgfile} > ${newimgfile}.md5
echo "install it this way:"
echo "gunzip -c ${newimgfile} | sudo dd bs=1M status=progress conv=notrunc,fsync of=/dev/sdX"
rm $imgfile