shimboot/bootloader/bin/bootstrap.sh

342 lines
8.7 KiB
Bash
Raw Normal View History

2023-10-02 02:08:31 -04:00
#!/bin/busybox sh
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# To bootstrap the factory installer on rootfs. This file must be executed as
# PID=1 (exec).
# Note that this script uses the busybox shell (not bash, not dash).
#original: https://chromium.googlesource.com/chromiumos/platform/initramfs/+/refs/heads/main/factory_shim/bootstrap.sh
2023-10-06 02:33:27 -04:00
#set -x
2023-10-05 02:43:23 -04:00
set +x
2023-10-02 02:08:31 -04:00
invoke_terminal() {
local tty="$1"
local title="$2"
shift
shift
# Copied from factory_installer/factory_shim_service.sh.
echo "${title}" >>${tty}
setsid sh -c "exec script -afqc '$*' /dev/null <${tty} >>${tty} 2>&1 &"
}
enable_debug_console() {
local tty="$1"
2023-10-28 02:09:20 -04:00
echo -e "debug console enabled on ${tty}"
2023-10-02 02:08:31 -04:00
invoke_terminal "${tty}" "[Bootstrap Debug Console]" "/bin/busybox sh"
}
2023-10-03 20:29:55 -04:00
find_rootfs_partitions() {
local disks=$(fdisk -l | sed -n "s/Disk \(\/dev\/.*\):.*/\1/p")
if [ ! "${disks}" ]; then
return 1
fi
2023-10-03 20:29:55 -04:00
for disk in $disks; do
local partitions=$(fdisk -l $disk | sed -n "s/^[ ]\+\([0-9]\+\).*shimboot_rootfs:\(.*\)$/\1:\2/p")
if [ ! "${partitions}" ]; then
continue
fi
for partition in $partitions; do
echo "${disk}${partition}"
done
2023-10-03 20:29:55 -04:00
done
}
2023-10-02 02:08:31 -04:00
2023-11-02 18:56:49 -04:00
find_chromeos_partitions() {
local roota_partitions="$(cgpt find -l ROOT-A)"
local rootb_partitions="$(cgpt find -l ROOT-B)"
if [ "$roota_partitions" ]; then
for partition in $roota_partitions; do
echo "${partition}:ChromeOS_ROOT-A:CrOS"
done
fi
if [ "$rootb_partitions" ]; then
for partition in $rootb_partitions; do
echo "${partition}:ChromeOS_ROOT-B:CrOS"
done
fi
}
find_all_partitions() {
echo "$(find_chromeos_partitions)"
echo "$(find_rootfs_partitions)"
}
2023-10-04 02:56:57 -04:00
#from original bootstrap.sh
move_mounts() {
2023-10-05 02:43:23 -04:00
local base_mounts="/sys /proc /dev"
local newroot_mnt="$1"
for mnt in $base_mounts; do
2023-10-04 02:56:57 -04:00
# $mnt is a full path (leading '/'), so no '/' joiner
2023-10-05 02:43:23 -04:00
mkdir -p "$newroot_mnt$mnt"
mount -n -o move "$mnt" "$newroot_mnt$mnt"
2023-10-04 02:56:57 -04:00
done
}
2023-10-28 02:09:20 -04:00
print_license() {
cat << EOF
ading2210/shimboot: Boot desktop Linux from a Chrome OS RMA shim.
Copyright (C) 2023 ading2210
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
EOF
}
2023-10-06 02:33:27 -04:00
print_selector() {
local rootfs_partitions="$1"
local i=1
echo "┌──────────────────────┐"
echo "│ Shimboot OS Selector │"
echo "└──────────────────────┘"
if [ "${rootfs_partitions}" ]; then
for rootfs_partition in $rootfs_partitions; do
#i don't know of a better way to split a string in the busybox shell
local part_path=$(echo $rootfs_partition | cut -d ":" -f 1)
local part_name=$(echo $rootfs_partition | cut -d ":" -f 2)
echo "${i}) ${part_name} on ${part_path}"
i=$((i+1))
done
else
echo "no bootable partitions found. please see the shimboot documentation to mark a partition as bootable."
fi
echo "q) reboot"
echo "s) enter a shell"
2023-10-28 02:09:20 -04:00
echo "l) view license"
2023-10-06 02:33:27 -04:00
}
get_selection() {
local rootfs_partitions="$1"
local i=1
read -p "Your selection: " selection
if [ "$selection" = "q" ]; then
echo "rebooting now."
reboot -f
elif [ "$selection" = "s" ]; then
reset
2023-11-09 16:13:59 -05:00
enable_debug_console "$TTY1"
2023-10-06 02:33:27 -04:00
return 0
2023-10-28 02:09:20 -04:00
elif [ "$selection" = "l" ]; then
clear
print_license
echo
read -p "press [enter] to return to the bootloader menu"
return 1
2023-10-06 02:33:27 -04:00
fi
2023-10-04 02:56:57 -04:00
for rootfs_partition in $rootfs_partitions; do
2023-10-05 02:43:23 -04:00
local part_path=$(echo $rootfs_partition | cut -d ":" -f 1)
local part_name=$(echo $rootfs_partition | cut -d ":" -f 2)
2023-11-02 18:56:49 -04:00
local part_flags=$(echo $rootfs_partition | cut -d ":" -f 3)
2023-10-06 02:33:27 -04:00
if [ "$selection" = "$i" ]; then
echo "selected $part_path"
2023-11-02 18:56:49 -04:00
if [ "$part_flags" = "CrOS" ]; then
echo "booting chrome os partition"
2023-11-06 13:54:39 -05:00
print_donor_selector "$rootfs_partitions"
get_donor_selection "$rootfs_partitions" "$part_path"
2023-11-02 18:56:49 -04:00
else
boot_target $part_path
fi
2023-11-06 13:54:39 -05:00
return 1
2023-10-06 02:33:27 -04:00
fi
i=$((i+1))
2023-10-04 02:56:57 -04:00
done
2023-10-03 20:29:55 -04:00
2023-10-06 02:33:27 -04:00
echo "invalid selection"
2023-10-28 02:09:20 -04:00
sleep 1
2023-10-06 02:33:27 -04:00
return 1
}
contains_word() {
local substr="$1"
local str="$2"
for word in $str; do
if [ "$word" = "$substr" ]; then
return 0
fi
done
return 1
}
#might be useful in case we need to disable the tpm
#currently this causes a kernel panic when we try to boot cros
unbind_driver() {
local driver_path="$1"
local sys_files="$(ls $driver_path)"
local excluded_files="bind uevent unbind"
for file in $sys_files; do
if ! contains_word "$file" "$excluded_files"; then
echo "$file" > "${driver_path}/unbind"
fi
done
}
unbind_tpm() {
unbind_driver "/sys/bus/spi/drivers/tpm_tis_spi"
unbind_driver "/sys/bus/pnp/drivers/tpm_tis"
unbind_driver "/sys/bus/platform/drivers/tpm_tis"
}
copy_progress() {
local source="$1"
local destination="$2"
mkdir -p "$destination"
tar -cf - -C "${source}" . | pv -f | tar -xf - -C "${destination}"
}
2023-11-06 13:54:39 -05:00
print_donor_selector() {
local rootfs_partitions="$1"
local i=1;
echo "Choose a partition to copy firmware and modules from:";
for rootfs_partition in $rootfs_partitions; do
local part_path=$(echo $rootfs_partition | cut -d ":" -f 1)
local part_name=$(echo $rootfs_partition | cut -d ":" -f 2)
local part_flags=$(echo $rootfs_partition | cut -d ":" -f 3)
if [ "$part_flags" = "CrOS" ]; then
continue;
fi
echo "${i}) ${part_name} on ${part_path}"
i=$((i+1))
done
}
get_donor_selection() {
local rootfs_partitions="$1"
local target="$2"
local i=1;
read -p "Your selection: " selection
for rootfs_partition in $rootfs_partitions; do
local part_path=$(echo $rootfs_partition | cut -d ":" -f 1)
local part_name=$(echo $rootfs_partition | cut -d ":" -f 2)
local part_flags=$(echo $rootfs_partition | cut -d ":" -f 3)
if [ "$part_flags" = "CrOS" ]; then
continue;
fi
if [ "$selection" = "$i" ]; then
echo "selected $part_path as the donor partition"
boot_chromeos $target $part_path
return 0
fi
i=$((i+1))
done
echo "invalid selection"
sleep 1
return 1
}
2023-10-06 02:33:27 -04:00
boot_target() {
local target="$1"
2023-10-05 02:43:23 -04:00
echo "moving mounts to newroot"
2023-10-04 02:56:57 -04:00
mkdir /newroot
2023-10-06 02:33:27 -04:00
mount $target /newroot
2023-10-05 02:43:23 -04:00
move_mounts /newroot
2023-10-04 02:56:57 -04:00
2023-10-05 02:43:23 -04:00
echo "switching root"
mkdir -p /newroot/bootloader
pivot_root /newroot /newroot/bootloader
2023-11-09 16:13:59 -05:00
exec /sbin/init < "$TTY1" >> "$TTY1" 2>&1
2023-10-06 02:33:27 -04:00
}
2023-11-02 18:56:49 -04:00
boot_chromeos() {
local target="$1"
2023-11-06 13:54:39 -05:00
local donor="$2"
echo "mounting target"
mkdir /newroot
mount -o ro $target /newroot
echo "mounting tmpfs"
mount -t tmpfs -o mode=1777 none /newroot/tmp
mount -t tmpfs -o mode=0555 run /newroot/run
mkdir -p -m 0755 /newroot/run/lock
2023-11-06 13:54:39 -05:00
echo "mounting donor partition"
local donor_mount="/newroot/tmp/donor_mnt"
local donor_files="/newroot/tmp/donor"
mkdir -p $donor_mount
mount -o ro $donor $donor_mount
echo "copying modules and firmware to tmpfs (this may take a while)"
copy_progress $donor_mount/lib/modules $donor_files/lib/modules
copy_progress $donor_mount/lib/firmware $donor_files/lib/firmware
mount -o bind $donor_files/lib/modules /newroot/lib/modules
mount -o bind $donor_files/lib/firmware /newroot/lib/firmware
umount $donor_mount
rm -rf $donor_mount
if [ -e "/newroot/etc/init/tpm-probe.conf" ]; then
echo "hiding the tpm from upstart"
mkdir -p /newroot/tmp/empty
mount -o bind /newroot/tmp/empty /sys/class/tpm
fi
2023-11-06 13:54:39 -05:00
2023-11-07 17:49:31 -05:00
echo "patching chrome os rootfs"
cat /newroot/etc/ui_use_flags.txt | sed "/reven_branding/d" | sed "/os_install_service/d" > /newroot/tmp/ui_use_flags.txt
mount -o bind /newroot/tmp/ui_use_flags.txt /newroot/etc/ui_use_flags.txt
echo "moving mounts"
move_mounts /newroot
echo "switching root"
mkdir -p /newroot/tmp/bootloader
pivot_root /newroot /newroot/tmp/bootloader
2023-11-07 17:49:31 -05:00
echo "starting init"
/sbin/modprobe zram
pkill frecon-lite
2023-11-09 16:13:59 -05:00
exec /sbin/init < "$TTY1" >> "$TTY1" 2>&1
2023-11-02 18:56:49 -04:00
}
2023-10-06 02:33:27 -04:00
main() {
2023-10-28 02:09:20 -04:00
echo "starting the shimboot bootloader"
2023-10-06 02:33:27 -04:00
2023-11-09 16:13:59 -05:00
enable_debug_console "$TTY2"
2023-10-06 02:33:27 -04:00
2023-11-02 18:56:49 -04:00
local valid_partitions="$(find_all_partitions)"
2023-10-06 02:33:27 -04:00
while true; do
clear
2023-11-02 18:56:49 -04:00
print_selector "${valid_partitions}"
if get_selection "${valid_partitions}"; then
2023-10-06 02:33:27 -04:00
break
fi
done
2023-10-02 02:08:31 -04:00
}
2023-10-07 00:28:40 -04:00
trap - EXIT
2023-10-02 02:08:31 -04:00
main "$@"
2023-10-06 02:33:27 -04:00
sleep 1d