From a9b048ff234d247378a7f73c37982204f1a83c79 Mon Sep 17 00:00:00 2001
From: Klemens Nanni <klemens@posteo.de>
Date: Sun, 23 Aug 2015 11:34:20 +0100
Subject: grub.cfg: Be more intelligent: automatically decrypt, etc

---
 docs/tasks.html                              |   3 +
 resources/grub/config/menuentries/common.cfg | 127 ++++++++++++++++++++-------
 2 files changed, 97 insertions(+), 33 deletions(-)

diff --git a/docs/tasks.html b/docs/tasks.html
index e3588709..6bb35ab4 100644
--- a/docs/tasks.html
+++ b/docs/tasks.html
@@ -461,6 +461,9 @@
 				- see
 				<a href="http://blogs.coreboot.org/blog/2015/05/28/progress-gsoc-week-1/">http://blogs.coreboot.org/blog/2015/05/28/progress-gsoc-week-1/</a> (H8S EC, applies to thinkpads)
 			</p>
+			<p>
+				<a href="https://github.com/lynxis/h8s-ec">https://github.com/lynxis/h8s-ec</a>
+			</p>
 			
 			<p><a href="#pagetop">Back to top of page.</a></p>
 	</div>
diff --git a/resources/grub/config/menuentries/common.cfg b/resources/grub/config/menuentries/common.cfg
index b153484b..45f2bd79 100644
--- a/resources/grub/config/menuentries/common.cfg
+++ b/resources/grub/config/menuentries/common.cfg
@@ -2,49 +2,100 @@ menuentry 'Load Operating System' {
 	insmod ahci
 	insmod part_msdos
 	insmod part_gpt
-	for x in (ahci0,1) (ahci0,2) (ahci0,3) (ahci0,4); do
-		for path in "/grub/libreboot_grub.cfg" "/boot/grub/libreboot_grub.cfg"; do
-			if [ -f "${x}${path}" ] ; then
-				set root=${x}
-				configfile ${path}
+	# Iterate through all possible disks and partitions
+	for i in 0 1; do
+		# Look for fully encrypted disks, prompt for passphrase if successful
+		cryptomount "(ahci${i})"
+		# Check for filesystems replacing MBR/GPT at all, e.g. BTRFS or ZFS
+		x="(crypto${i})"
+		set root=${x}
+		for p in "grub/libreboot_" "boot/grub/libreboot_" "grub/" "boot/grub/"; do
+			if [ -f "${x}/${p}grub.cfg" ] ; then
+				configfile /${p}grub.cfg
 			fi
 		done
+		for j in 0 1 2 3 4 5 6 7 8 9; do
+		# Check for normal MBR/GPT disks
+		# GPT allows more than 4 partitions, /boot on /dev/sda7 is highly unlikely but still possible
+			# /boot can still be encrypted
+			for k in "crypto" "ahci"; do
+				x="(${k}${i},${j})"
+				set root=${x}
+				for p in "grub/libreboot_" "boot/grub/libreboot_" "grub/" "boot/grub/"; do
+					if [ -f "${x}/${p}grub.cfg" ] ; then
+						configfile /${p}grub.cfg
+					fi
+				done
+
+			done
+		done
 	done
 
+# Last resort, otherwise go to GRUB shell
 	set root='ahci0,1'
-	for path in "" "/boot"; do
-		if [ -f "${path}/vmlinuz" ]; then
-			linux ${path}/vmlinuz root=/dev/sda1 rw
-			if [ -f "${path}/initrd.img" ]; then
-				initrd ${path}/initrd.img
+	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
 }
-menuentry 'Parse ISOLINUX menu (ahci0)' {
+menuentry 'Parse ISOLINUX menu (SATA)' {
 	insmod ahci
 	insmod part_msdos
 	insmod part_gpt
-	for x in (ahci0,1) (ahci0,2) (ahci0,3) (ahci0,4); do
-		set root=${x}
-		for path in "/isolinux/isolinux.cfg" "/syslinux/syslinux.cfg" "/boot/isolinux/isolinux.cfg" "/boot/syslinux/syslinux.cfg"; do
-			if [ -f "${path}" ]; then
-				syslinux_configfile -i ${path}
+	for i in 0 1; do
+		# Check for filesystems replacing MBR/GPT at all, e.g. BTRFS or ZFS
+		set root="(ahci${i})"
+		for p in "/isolinux" "/syslinux"; do
+			if [ -f "${p}${p}.cfg" ] ; then
+				syslinux_configfile -i ${p}${p}.cfg
+					elif [ -f "/boot${p}${p}.cfg" ] ; then
+				syslinux_configfile -i /boot${p}${p}.cfg
 			fi
 		done
+		# Check for normal MBR/GPT disks
+		# GPT allows more than 4 partitions, /boot on /dev/sda7 is highly unlikely but still possible
+		for j in 0 1 2 3 4 5 6 7 8 9; do
+			set root="(ahci${i},${j})"
+			for p in "/isolinux" "/syslinux"; do
+				if [ -f "${p}${p}.cfg" ] ; then
+					syslinux_configfile -i ${p}${p}.cfg
+				elif [ -f "/boot${p}${p}.cfg" ] ; then
+					syslinux_configfile -i /boot${p}${p}.cfg
+				fi
+			done
+		done
 	done
 }
 menuentry 'Parse ISOLINUX menu (USB)' {
 	insmod usbms
 	insmod part_msdos
 	insmod part_gpt
-	for x in (usb0) (usb0,1) (usb0,2) (usb0,3) (usb0,4); do
-		set root=${x}
-		for path in "/isolinux/isolinux.cfg" "/syslinux/syslinux.cfg" "/boot/isolinux/isolinux.cfg" "/boot/syslinux/syslinux.cfg"; do
-			if [ -f "${path}" ]; then
-				syslinux_configfile -i ${path}
+	for i in 0 1; do
+		# Check for filesystems replacing MBR/GPT at all, e.g. BTRFS or ZFS
+		set root="(usb${i})"
+		for p in "/isolinux" "/syslinux"; do
+			if [ -f "${p}${p}.cfg" ] ; then
+				syslinux_configfile -i ${p}${p}.cfg
+					elif [ -f "/boot${p}${p}.cfg" ] ; then
+				syslinux_configfile -i /boot${p}${p}.cfg
 			fi
 		done
+		# Check for normal MBR/GPT disks
+		# GPT allows more than 4 partitions, /boot on /dev/sda7 is highly unlikely but still possible
+		for j in 0 1 2 3 4 5 6 7 8 9; do
+			set root="(usb${i},${j})"
+			for p in "/isolinux" "/syslinux"; do
+				if [ -f "${p}${p}.cfg" ] ; then
+					syslinux_configfile -i ${p}${p}.cfg
+				elif [ -f "/boot${p}${p}.cfg" ] ; then
+					syslinux_configfile -i /boot${p}${p}.cfg
+				fi
+                        done
+		done
 	done
 }
 menuentry 'Parse ISOLINUX menu (CD/DVD)' {
@@ -53,9 +104,11 @@ menuentry 'Parse ISOLINUX menu (CD/DVD)' {
 	insmod iso9660
 	for x in (ata0) (ahci1); do
 		set root=${x}
-		for path in "/isolinux/isolinux.cfg" "/syslinux/syslinux.cfg" "/boot/isolinux/isolinux.cfg" "/boot/syslinux/syslinux.cfg"; do
-			if [ -f "${path}" ]; then
-				syslinux_configfile -i ${path}
+		for p in "/isolinux" "/syslinux"; do
+			if [ -f "${p}${p}.cfg" ] ; then
+				syslinux_configfile -i ${p}${p}.cfg
+			elif [ -f "/boot${p}${p}.cfg" ] ; then
+				syslinux_configfile -i /boot${p}${p}.cfg
 			fi
 		done
 	done
@@ -69,16 +122,24 @@ menuentry 'Search for GRUB configuration (grub.cfg) outside of CBFS' {
 	insmod usbms
 	insmod part_msdos
 	insmod part_gpt
-	for x in (ahci0,1) (ahci0,2) (ahci0,3) (ahci0,4) (usb0) (usb0,1) (usb0,2) (usb0,3) (usb0,4); do
-		for path in "/grub/grub.cfg" "/boot/grub/grub.cfg" "/grub2/grub.cfg" "/boot/grub2/grub.cfg"; do
-			if [ -f "${x}${path}" ] ; then
-				submenu "Load Config from ${x}" ${x} {
-					root=${2}
-					source ${path}
+	for i in ahci0 ahci1 usb0 usb1; do
+		for j in 1 2 3 4 5 6 7 8 9; do
+			x="(${i},${j})"
+			for p in "grub" "boot/grub" "grub2" "boot/grub2"; do
+				if [ -f "${x}/${p}/grub.cfg ] ; then
+					root=$2
+					submenu "Load Config from ${x}" ${x} {
+						source /${p}/grub.cfg
+					}
 					unset superusers
-				}
-			fi
+				fi
+			done
 		done
 	done
 }
-
+menuentry 'Poweroff' {
+halt
+}
+menuentry 'Reboot' {
+reboot
+}
-- 
cgit v1.2.3-70-g09d2