d15d46b
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d15d46b
From: Daniel Axtens <dja@axtens.net>
d15d46b
Date: Thu, 24 Mar 2022 14:34:32 +1100
d15d46b
Subject: [PATCH] powerpc: prefix detection: support device names with commas
d15d46b
d15d46b
Frustratingly, the device name itself can contain an embedded comma:
d15d46b
e.g /pci@800000020000015/pci1014,034A@0/sas/disk@5000c50098a0ee8b
d15d46b
d15d46b
So my previous approach was wrong: we cannot rely upon the presence
d15d46b
of a comma to say that a partition has been specified!
d15d46b
d15d46b
It turns out for prefixes like (,gpt2)/grub2 we really want to make
d15d46b
up a full (device,partition)/patch prefix, because root discovery code
d15d46b
in 10_linux will reset the root variable and use search to fill it again.
d15d46b
If you have run grub-install, you probably don't have search built in,
d15d46b
and if you don't have prefix containing (device,partition), grub will
d15d46b
construct ($root)$prefix/powerpc-ieee1275/search.mod - but because $root
d15d46b
has just been changed, this will no longer work, and the boot will fail!
d15d46b
d15d46b
Retain the gist of the logic, but instead of looking for a comma, look for
d15d46b
a leading '('. This matches the earlier code better anyway.
d15d46b
d15d46b
There's certainly a better fix to be had. But any time you chose to build
d15d46b
with a bare prefix like '/grub2', you're almost certainly going to build in
d15d46b
search anyway, so this will do.
d15d46b
d15d46b
Signed-off-by: Daniel Axtens <dja@axtens.net>
d15d46b
---
d15d46b
 grub-core/kern/main.c | 27 +++++++++++++++++++++------
d15d46b
 1 file changed, 21 insertions(+), 6 deletions(-)
d15d46b
d15d46b
diff --git a/grub-core/kern/main.c b/grub-core/kern/main.c
d15d46b
index 993b8a8598..e94a2f78fb 100644
d15d46b
--- a/grub-core/kern/main.c
d15d46b
+++ b/grub-core/kern/main.c
d15d46b
@@ -242,14 +242,29 @@ grub_set_prefix_and_root (void)
d15d46b
 	    what sorts of paths represent disks with partition tables and those
d15d46b
 	    without partition tables.
d15d46b
 
d15d46b
-	 So we act unless there is a comma in the device, which would indicate
d15d46b
-	 a partition has already been specified.
d15d46b
+          - Frustratingly, the device name itself can contain an embedded comma:
d15d46b
+            /pci@800000020000015/pci1014,034A@0/sas/disk@5000c50098a0ee8b
d15d46b
+            So we cannot even rely upon the presence of a comma to say that a
d15d46b
+            partition has been specified!
d15d46b
 
d15d46b
-	 (If we only have a path, the code in normal to discover config files
d15d46b
-	 will try both without partitions and then with any partitions so we
d15d46b
-	 will cover both CDs and HDs.)
d15d46b
+         If we only have a path in $prefix, the code in normal to discover
d15d46b
+	 config files will try all disks, both without partitions and then with
d15d46b
+	 any partitions so we will cover both CDs and HDs.
d15d46b
+
d15d46b
+         However, it doesn't then set the prefix to be something like
d15d46b
+         (discovered partition)/path, and so it is fragile against runtime
d15d46b
+         changes to $root. For example some of the stuff done in 10_linux to
d15d46b
+         reload $root sets root differently and then uses search to find it
d15d46b
+         again. If the search module is not built in, when we change root, grub
d15d46b
+         will look in (new root)/path/powerpc-ieee1275, that won't work, and we
d15d46b
+         will not be able to load the search module and the boot will fail.
d15d46b
+
d15d46b
+         This is particularly likely to hit us in the grub-install
d15d46b
+         (,msdos2)/grub2 case, so we act unless the supplied prefix starts with
d15d46b
+         '(', which would likely indicate a partition has already been
d15d46b
+         specified.
d15d46b
        */
d15d46b
-      if (grub_strchr (device, ',') == NULL)
d15d46b
+      if (prefix && prefix[0] != '(')
d15d46b
         grub_env_set ("prefix", path);
d15d46b
       else
d15d46b
 #endif