Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ading2210
GitHub Repository: ading2210/shimboot
Path: blob/main/patch_rootfs.sh
402 views
1
#!/bin/bash
2
3
#patch the target rootfs to add any needed drivers
4
5
. ./common.sh
6
. ./image_utils.sh
7
8
print_help() {
9
echo "Usage: ./patch_rootfs.sh shim_path reco_path rootfs_dir"
10
}
11
12
assert_root
13
assert_deps "git gunzip depmod"
14
assert_args "$3"
15
16
copy_modules() {
17
local shim_rootfs=$(realpath -m $1)
18
local reco_rootfs=$(realpath -m $2)
19
local target_rootfs=$(realpath -m $3)
20
21
rm -rf "${target_rootfs}/lib/modules"
22
cp -r "${shim_rootfs}/lib/modules" "${target_rootfs}/lib/modules"
23
24
mkdir -p "${target_rootfs}/lib/firmware"
25
cp -r --remove-destination "${shim_rootfs}/lib/firmware/"* "${target_rootfs}/lib/firmware/"
26
cp -r --remove-destination "${reco_rootfs}/lib/firmware/"* "${target_rootfs}/lib/firmware/"
27
28
mkdir -p "${target_rootfs}/lib/modprobe.d/"
29
mkdir -p "${target_rootfs}/etc/modprobe.d/"
30
cp -r "${reco_rootfs}/lib/modprobe.d/"* "${target_rootfs}/lib/modprobe.d/"
31
cp -r "${reco_rootfs}/etc/modprobe.d/"* "${target_rootfs}/etc/modprobe.d/"
32
33
#decompress kernel modules if necessary - debian won't recognize these otherwise
34
local compressed_files="$(find "${target_rootfs}/lib/modules" -name '*.gz')"
35
if [ "$compressed_files" ]; then
36
echo "$compressed_files" | xargs gunzip
37
for kernel_dir in "$target_rootfs/lib/modules/"*; do
38
local version="$(basename "$kernel_dir")"
39
depmod -b "$target_rootfs" "$version"
40
done
41
fi
42
}
43
44
copy_firmware() {
45
local firmware_path="/tmp/chromium-firmware"
46
local target_rootfs=$(realpath -m $1)
47
48
if [ ! -e "$firmware_path" ]; then
49
download_firmware $firmware_path
50
fi
51
52
cp -r --remove-destination "${firmware_path}/"* "${target_rootfs}/lib/firmware/"
53
}
54
55
download_firmware() {
56
local firmware_url="https://chromium.googlesource.com/chromiumos/third_party/linux-firmware"
57
local firmware_path=$(realpath -m $1)
58
59
git clone --branch master --depth=1 "${firmware_url}" $firmware_path
60
}
61
62
shim_path=$(realpath -m $1)
63
reco_path=$(realpath -m $2)
64
target_rootfs=$(realpath -m $3)
65
shim_rootfs="/tmp/shim_rootfs"
66
reco_rootfs="/tmp/reco_rootfs"
67
68
echo "mounting shim"
69
shim_loop=$(create_loop "${shim_path}")
70
safe_mount "${shim_loop}p3" $shim_rootfs ro
71
72
echo "mounting recovery image"
73
reco_loop=$(create_loop "${reco_path}")
74
safe_mount "${reco_loop}p3" $reco_rootfs ro
75
76
echo "copying modules to rootfs"
77
copy_modules $shim_rootfs $reco_rootfs $target_rootfs
78
79
echo "downloading misc firmware"
80
copy_firmware $target_rootfs
81
82
echo "unmounting and cleaning up"
83
umount $shim_rootfs
84
umount $reco_rootfs
85
losetup -d $shim_loop
86
losetup -d $reco_loop
87
88
echo "done"
89