From ac3ed23638cbc4b10059f9678283d08b4a082136 Mon Sep 17 00:00:00 2001
From: Luca Boccassi <bluca@debian.org>
Date: Tue, 27 Feb 2018 19:23:41 +0000
Subject: UEFI: add minimal grub.cfg to fat32 partition

On some UEFI implementations, like the AMI found in the Supermicro
X10SDV-TP8F development board, the fat32 partition will be loaded
first and so Grub will set it the root, and then drop to the console
as it cannot find any config on it.
Add a minimal grub.cfg that allows Grub to find the main config on
the ISO 9660 partition and load it.
Closes: #892406
---
 scripts/build/binary_grub-efi | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/scripts/build/binary_grub-efi b/scripts/build/binary_grub-efi
index bad4dc82c..aea42a693 100755
--- a/scripts/build/binary_grub-efi
+++ b/scripts/build/binary_grub-efi
@@ -133,6 +133,21 @@ case "${LB_ARCHITECTURES}" in
 esac
 
 
+# On some platforms the EFI grub image will be loaded, so grub's root
+# variable will be set to the EFI partition. This means that grub will
+# look in that partition for a grub.cfg file, and even if it finds it
+# it will not be able to find the vmlinuz and initrd.
+# Drop a minimal grub.cfg in the EFI partition that sets the root and prefix
+# to whatever partition holds the /live/vmlinuz image, and load the grub
+# config from that same partition.
+# This is what the Ubuntu livecd already does.
+mkdir -p ${_CHROOT_DIR}/grub-efi-temp-cfg
+cat >${_CHROOT_DIR}/grub-efi-temp-cfg/grub.cfg <<EOF
+search --set=root --file /live/vmlinuz
+set prefix=(\\\$root)/boot/grub
+configfile (\\\$root)/boot/grub/grub.cfg
+EOF
+
 # The code below is adapted from tools/boot/jessie/boot-x86
 # in debian-cd
 
@@ -143,10 +158,14 @@ esac
 # the case of a multi-arch amd64/i386 image
 
 size=0
-for file in ${_CHROOT_DIR}/grub-efi-temp/efi/boot/boot*.efi; do
+for file in ${_CHROOT_DIR}/grub-efi-temp/efi/boot/boot*.efi \
+		${_CHROOT_DIR}/grub-efi-temp-cfg/grub.cfg; do
 	size=\$((\$size + \$(stat -c %s "\$file")))
 done
 
+# directories: efi efi/boot boot boot/grub
+size=\$((\$size + 4096 * 4))
+
 blocks=\$(((\$size / 1024 + 55) / 32 * 32 ))
 
 rm -f ${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img
@@ -155,6 +174,11 @@ mmd -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ::efi
 mmd -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ::efi/boot
 mcopy -o -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ${_CHROOT_DIR}/grub-efi-temp/efi/boot/boot*.efi \
 	"::efi/boot"
+
+mmd -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ::boot
+mmd -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ::boot/grub
+mcopy -o -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ${_CHROOT_DIR}/grub-efi-temp-cfg/grub.cfg \
+	"::boot/grub"
 END
 
 case "${LB_BUILD_WITH_CHROOT}" in
@@ -186,6 +210,7 @@ cp -r chroot/grub-efi-temp/* binary/
 rm -rf chroot/grub-efi-temp-x86_64-efi
 rm -rf chroot/grub-efi-temp-i386-efi
 rm -rf chroot/grub-efi-temp-arm64-efi
+rm -rf chroot/grub-efi-temp-cfg
 rm -rf chroot/grub-efi-temp
 
 # We rely on: binary_loopback_cfg to generate grub.cfg and other configuration files
-- 
cgit v1.2.3