grub.cfg: bruteforce linux/initrd if no grub.cfg

this is probably not safe, and neither is the current
fall back code which is being removed from master.

i already finished it before testing, but after realising
the above, so i'll just stash it in this branch for now.

Signed-off-by: Leah Rowe <leah@libreboot.org>
btrfsvols
Leah Rowe 2023-12-18 06:44:09 +00:00
parent 2d6e5ca4c4
commit fbb8f8b1d0
1 changed files with 98 additions and 20 deletions

View File

@ -90,6 +90,11 @@ function search_isolinux {
done
echo # Insert newline
}
function try_without_config {
}
menuentry 'Load Operating System (incl. fully encrypted disks) [o]' --hotkey='o' {
if [ "${grub_scan_disk}" != "ata" ]; then
@ -146,31 +151,104 @@ menuentry 'Load Operating System (incl. fully encrypted disks) [o]' --hotkey='o
try_user_config "${vol}"
done
if [ "${grub_scan_disk}" != "ata" ]; then
# Last resort, if all else fails
set root=ahci0,1
for p in / /boot/; do
if [ -f "${p}vmlinuz" ]; then
linux ${p}vmlinuz root=/dev/sda1 rw
if [ -f "${p}initrd.img" ]; then
initrd ${p}initrd.img
# bruteforce as a last result. figure out how to boot
# linux without a grub configuration file. (crypto and lvm unsupported)
set vmlinuz=""
set initrd=""
set devtype=""
set devnum=""
set devletter=""
set partnum=""
for t in ata ahci; do
set vmlinuz=""
set initrd=""
if [ "${t}" = "ata" ] && [ "${grub_scan_disk}" = "ahci" ]; then
continue
elif [ "${t}" = "ahci" ] && [ "${grub_scan_disk}" = "ata" ]; then
continue
fi
for dev in 0 1 2 3 4 5 6 7 8; do
for part in 1 2 3 4 5 6 7 8 9; do
set root="${t}${devnum},${partnum}"
set devnum="${dev}"
set partnum="${part}"
for k in /vmlinuz* @/vmlinuz*; do
if [ ! -f "${k}" ] || [ ! -d /boot ]; then
continue
fi
set vmlinuz="${k}"
# do not break. if furthe rentries are
# available, due to alphabetic sorting,
# the next entry is likely a newer
# kernel version
done
for k in /boot/vmlinuz* @/boot/vmlinuz*; do
if [ "${vmlinuz}" != "" ]; then
break
fi
set vmlinuz="${k}"
done
if [ "${vmlinuz}" = "" ]; then
continue
fi
for i in /initrd* /initramfs* @/initrd* @/initramfs*; do
if [ ! -f "${i}" ] || [ ! -d "/boot" ]; then
continue
done
set initrd="${i}"
# ditto. do not break yet
done
for i in /boot/initrd* /boot/initramfs* @/boot/initrd* @/boot/initramfs*; do
if [ "${initrd}" != "" ]; then
break
fi
set initrd="${i}"
done
if [ "${vmlinuz}" != "" ] && [ "${initrd}" != "" ]; then
set devtype="${t}"
break
fi
set vmlinuz=""
set initrd=""
done
if [ "${vmlinuz}" != "" ] && [ "${initrd}" != "" ]; then
break
fi
done
fi
if [ "${vmlinuz}" != "" ] && [ "${initrd}" != "" ]; then
break
fi
done
if [ "${grub_scan_disk}" != "ahci" ]; then
# Last resort (for setups that use IDE instead of SATA)
set root=ata0,1
for p in / /boot/; do
if [ -f "${p}vmlinuz" ]; then
linux ${p}vmlinuz root=/dev/sda1 rw
if [ -f "${p}initrd.img" ]; then
initrd ${p}initrd.img
fi
fi
done
if [ "${vmlinuz}" = "" ] || [ "${initrd}" = "" ]; then
return
fi
if [ "${devnum}" = "0" ]; then
set devletter="a"
elif [ "${devnum}" = "1" ]; then
set devletter="b"
elif [ "${devnum}" = "2" ]; then
set devletter="c"
elif [ "${devnum}" = "3" ]; then
set devletter="d"
elif [ "${devnum}" = "4" ]; then
set devletter="e"
elif [ "${devnum}" = "5" ]; then
set devletter="f"
elif [ "${devnum}" = "6" ]; then
set devletter="g"
elif [ "${devnum}" = "7" ]; then
set devletter="h"
elif [ "${devnum}" = "8" ]; then
set devletter="i"
fi
set btrfsvol=""
if [ "${vmlinuz#@}" != "${vmlinuz}" ]; then
set btrfsvol="rootflags=subvol=@"
fi
set root="${devtype}${devnum},${partnum}"
linux ${vmlinuz} root=/dev/sd${devletter}${partnum} ${btrfsvol} rw
initrd ${initrd}
true # Prevent pager requiring to accept each line instead of whole screen
}