1f9e807
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
1f9e807
From: Daniel Axtens <dja@axtens.net>
1f9e807
Date: Mon, 19 Jul 2021 14:35:55 +1000
1f9e807
Subject: [PATCH] powerpc: adjust setting of prefix for signed binary case
1f9e807
1f9e807
On RHEL-signed powerpc grub, we sign a grub with -p /grub2 and expect
1f9e807
that there's a boot partition.
1f9e807
1f9e807
Unfortunately grub_set_prefix_and_root tries to convert this to
1f9e807
($fwdevice)/grub2. This ends up being (ieee1275/disk)/grub2 and that
1f9e807
falls apart pretty quickly - there's no file-system on ieee1275/disk,
1f9e807
and it makes the search routine try things like
1f9e807
(ieee1275/disk,msdos2)(ieee1275/disk)/grub2 which also doesn't work.
1f9e807
1f9e807
Detect if we would be about to create (ieee1275/disk)/path and don't:
1f9e807
preserve a prefix of /path instead and hope the search later finds us.
1f9e807
1f9e807
Related: rhbz#1899864
1f9e807
1f9e807
Signed-off-by: Daniel Axtens <dja@axtens.net>
e602a06
[rharwood@redhat.com: squash in fixup commit]
e602a06
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
1f9e807
---
e602a06
 grub-core/kern/main.c | 49 ++++++++++++++++++++++++++++++++++++++++++++-----
e602a06
 1 file changed, 44 insertions(+), 5 deletions(-)
1f9e807
1f9e807
diff --git a/grub-core/kern/main.c b/grub-core/kern/main.c
e622855
index b573be6650..3fc3401472 100644
1f9e807
--- a/grub-core/kern/main.c
1f9e807
+++ b/grub-core/kern/main.c
e602a06
@@ -216,13 +216,52 @@ grub_set_prefix_and_root (void)
1f9e807
   if (device)
1f9e807
     {
1f9e807
       char *prefix_set;
1f9e807
-    
1f9e807
-      prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
1f9e807
-      if (prefix_set)
1f9e807
+
1f9e807
+#ifdef __powerpc__
1f9e807
+      /* We have to be careful here on powerpc-ieee1275 + signed grub. We
1f9e807
+	 will have signed something with a prefix that doesn't have a device
1f9e807
+	 because we cannot know in advance what partition we're on.
1f9e807
+
1f9e807
+	 We will have had !device earlier, so we will have set device=fwdevice
1f9e807
+	 However, we want to make sure we do not end up setting prefix to be
1f9e807
+	 ($fwdevice)/path, because we will then end up trying to boot or search
1f9e807
+	 based on a prefix of (ieee1275/disk)/path, which will not work because
1f9e807
+	 it's missing a partition.
1f9e807
+
1f9e807
+	 Also:
1f9e807
+	  - You can end up with a device with an FS directly on it, without
1f9e807
+	    a partition, e.g. ieee1275/cdrom.
1f9e807
+
1f9e807
+	  - powerpc-ieee1275 + grub-install sets e.g. prefix=(,gpt2)/path,
1f9e807
+	    which will have now been extended to device=$fwdisk,partition
1f9e807
+	    and path=/path
1f9e807
+
e602a06
+	  - PowerVM will give us device names like
e602a06
+	    ieee1275//vdevice/v-scsi@3000006c/disk@8100000000000000
e602a06
+	    and we don't want to try to encode some sort of truth table about
e602a06
+	    what sorts of paths represent disks with partition tables and those
e602a06
+	    without partition tables.
e602a06
+
e602a06
+	 So we act unless there is a comma in the device, which would indicate
e602a06
+	 a partition has already been specified.
e602a06
+
e602a06
+	 (If we only have a path, the code in normal to discover config files
e602a06
+	 will try both without partitions and then with any partitions so we
e602a06
+	 will cover both CDs and HDs.)
1f9e807
+       */
e602a06
+      if (grub_strchr (device, ',') == NULL)
1f9e807
+        grub_env_set ("prefix", path);
1f9e807
+      else
1f9e807
+#endif
1f9e807
 	{
1f9e807
-	  grub_env_set ("prefix", prefix_set);
1f9e807
-	  grub_free (prefix_set);
1f9e807
+	  prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
1f9e807
+	  if (prefix_set)
1f9e807
+	  {
1f9e807
+	    grub_env_set ("prefix", prefix_set);
1f9e807
+	    grub_free (prefix_set);
1f9e807
+	  }
1f9e807
 	}
1f9e807
+
1f9e807
       grub_env_set ("root", device);
1f9e807
     }
1f9e807