shimboot/patch_rootfs.sh

90 lines
2.5 KiB
Bash
Raw Normal View History

2023-10-07 00:28:40 -04:00
#!/bin/bash
#patch the target rootfs to add any needed drivers
. ./common.sh
2023-12-22 22:15:24 -05:00
. ./image_utils.sh
2023-10-20 08:04:19 -04:00
print_help() {
echo "Usage: ./patch_rootfs.sh shim_path reco_path rootfs_dir"
}
assert_root
2024-04-29 13:27:08 -04:00
assert_deps "git gunzip depmod"
assert_args "$3"
2023-10-20 08:04:19 -04:00
2023-10-20 06:38:45 -04:00
copy_modules() {
2024-01-25 16:19:25 -05:00
local shim_rootfs=$(realpath -m $1)
local reco_rootfs=$(realpath -m $2)
local target_rootfs=$(realpath -m $3)
2023-10-07 00:28:40 -04:00
2023-10-21 05:06:52 -04:00
rm -rf "${target_rootfs}/lib/modules"
cp -r "${shim_rootfs}/lib/modules" "${target_rootfs}/lib/modules"
mkdir -p "${target_rootfs}/lib/firmware"
2023-10-20 08:04:19 -04:00
cp -r --remove-destination "${shim_rootfs}/lib/firmware/"* "${target_rootfs}/lib/firmware/"
2023-10-20 08:39:43 -04:00
cp -r --remove-destination "${reco_rootfs}/lib/firmware/"* "${target_rootfs}/lib/firmware/"
2023-10-21 02:59:53 -04:00
mkdir -p "${target_rootfs}/lib/modprobe.d/"
mkdir -p "${target_rootfs}/etc/modprobe.d/"
2023-10-20 06:38:45 -04:00
cp -r "${reco_rootfs}/lib/modprobe.d/"* "${target_rootfs}/lib/modprobe.d/"
cp -r "${reco_rootfs}/etc/modprobe.d/"* "${target_rootfs}/etc/modprobe.d/"
#decompress kernel modules if necessary - debian won't recognize these otherwise
local compressed_files="$(find "${target_rootfs}/lib/modules" -name '*.gz')"
if [ "$compressed_files" ]; then
2024-04-29 13:27:08 -04:00
echo "$compressed_files" | xargs gunzip
for kernel_dir in "$target_rootfs/lib/modules/"*; do
local version="$(basename "$kernel_dir")"
depmod -b "$target_rootfs" "$version"
done
fi
2023-10-07 00:28:40 -04:00
}
2023-10-20 08:04:19 -04:00
copy_firmware() {
local firmware_path="/tmp/chromium-firmware"
2024-01-25 16:19:25 -05:00
local target_rootfs=$(realpath -m $1)
2023-10-20 08:04:19 -04:00
if [ ! -e "$firmware_path" ]; then
download_firmware $firmware_path
fi
2023-10-20 22:30:02 -04:00
cp -r --remove-destination "${firmware_path}/"* "${target_rootfs}/lib/firmware/"
2023-10-20 08:04:19 -04:00
}
2023-10-20 06:38:45 -04:00
download_firmware() {
local firmware_url="https://chromium.googlesource.com/chromiumos/third_party/linux-firmware"
2024-01-25 16:19:25 -05:00
local firmware_path=$(realpath -m $1)
2023-10-20 06:38:45 -04:00
git clone --branch master --depth=1 "${firmware_url}" $firmware_path
2023-10-20 08:04:19 -04:00
}
2024-01-25 16:19:25 -05:00
shim_path=$(realpath -m $1)
reco_path=$(realpath -m $2)
target_rootfs=$(realpath -m $3)
2023-10-20 08:04:19 -04:00
shim_rootfs="/tmp/shim_rootfs"
reco_rootfs="/tmp/reco_rootfs"
echo "mounting shim"
shim_loop=$(create_loop "${shim_path}")
make_mountable "${shim_loop}p3"
safe_mount "${shim_loop}p3" $shim_rootfs
echo "mounting recovery image"
reco_loop=$(create_loop "${reco_path}")
make_mountable "${reco_loop}p3"
safe_mount "${reco_loop}p3" $reco_rootfs
echo "copying modules to rootfs"
copy_modules $shim_rootfs $reco_rootfs $target_rootfs
echo "downloading misc firmware"
copy_firmware $target_rootfs
echo "unmounting and cleaning up"
umount $shim_rootfs
umount $reco_rootfs
losetup -d $shim_loop
losetup -d $reco_loop
echo "done"