shimboot/patch_rootfs.sh

90 lines
2.2 KiB
Bash
Raw Normal View History

2023-10-06 23:28:40 -05:00
#!/bin/bash
#patch the target rootfs to add any needed drivers
set -e
if [ "$DEBUG" ]; then
set -x
fi
2023-10-20 07:04:19 -05:00
. ./build_image.sh
print_help() {
echo "Usage: ./patch_rootfs.sh shim_path reco_path rootfs_dir"
}
if [ "$EUID" -ne 0 ]; then
echo "this needs to be run as root."
exit 1
fi
if [ -z "$3" ]; then
print_help
exit 1
fi
2023-10-20 05:38:45 -05:00
copy_modules() {
2023-10-06 23:28:40 -05:00
local shim_rootfs=$(realpath $1)
2023-10-20 05:38:45 -05:00
local reco_rootfs=$(realpath $2)
local target_rootfs=$(realpath $3)
2023-10-06 23:28:40 -05:00
2023-10-21 04:06:52 -05: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 07:04:19 -05:00
cp -r --remove-destination "${shim_rootfs}/lib/firmware/"* "${target_rootfs}/lib/firmware/"
2023-10-20 07:39:43 -05:00
cp -r --remove-destination "${reco_rootfs}/lib/firmware/"* "${target_rootfs}/lib/firmware/"
2023-10-21 01:59:53 -05:00
mkdir -p "${target_rootfs}/lib/modprobe.d/"
mkdir -p "${target_rootfs}/etc/modprobe.d/"
2023-10-20 05:38:45 -05: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/"
2023-10-06 23:28:40 -05:00
}
2023-10-20 07:04:19 -05:00
copy_firmware() {
local firmware_path="/tmp/chromium-firmware"
local target_rootfs=$(realpath $1)
if [ ! -e "$firmware_path" ]; then
download_firmware $firmware_path
fi
2023-10-20 21:30:02 -05:00
cp -r --remove-destination "${firmware_path}/"* "${target_rootfs}/lib/firmware/"
2023-10-20 07:04:19 -05:00
}
2023-10-20 05:38:45 -05:00
download_firmware() {
local firmware_url="https://chromium.googlesource.com/chromiumos/third_party/linux-firmware"
2023-10-20 07:04:19 -05:00
local firmware_path=$(realpath $1)
2023-10-20 05:38:45 -05:00
git clone --branch master --depth=1 "${firmware_url}" $firmware_path
2023-10-20 07:04:19 -05:00
}
shim_path=$(realpath $1)
reco_path=$(realpath $2)
target_rootfs=$(realpath $3)
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"