[Concept,03/13] scripts: ubuntu: Honour --kernel in grub-cfg parser

Message ID 20260507221507.505998-4-sjg@u-boot.org
State New
Headers
Series bootstd: bls: Scan every partition; Ubuntu autoinstall via BLS |

Commit Message

Simon Glass May 7, 2026, 10:14 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

parse_grub_cmdline() hard-codes 'casper/vmlinuz' in its regex, so a user
passing -k with a non-default kernel path silently falls through to the
'could not find a casper linux entry' error even when grub.cfg does
carry a matching entry under the requested path.

Take the kernel path as a parameter, re.escape() it before splicing into
the pattern, and quote it in the failure message so the user sees what
was searched for.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 scripts/ubuntu-iso-to-uboot.py | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
  

Patch

diff --git a/scripts/ubuntu-iso-to-uboot.py b/scripts/ubuntu-iso-to-uboot.py
index f6326b7eceb..22c6928091b 100755
--- a/scripts/ubuntu-iso-to-uboot.py
+++ b/scripts/ubuntu-iso-to-uboot.py
@@ -104,10 +104,10 @@  def check_tools() -> None:
         )
 
 
-def parse_grub_cmdline(iso: Path) -> str:
+def parse_grub_cmdline(iso: Path, kernel: str) -> str:
     """Return the kernel cmdline from the ISO's default grub entry.
 
-    Parses the first `linux /casper/vmlinuz ...` line in /boot/grub/grub.cfg
+    Parses the first `linux <kernel> ...` line in /boot/grub/grub.cfg
     and strips the kernel path, so the caller can pass the remaining tokens
     to the kernel (e.g. '--- quiet splash' on Ubuntu 24.04.1).
     """
@@ -119,12 +119,17 @@  def parse_grub_cmdline(iso: Path) -> str:
         )
         cfg = dst.read_text(errors='replace')
 
+    # The kernel path comes from the user (or its default), so escape it
+    # before splicing it into the regex.
+    kernel_re = re.escape(kernel.lstrip('/'))
     m = re.search(
-        r'^\s*linux\s+\S*casper/vmlinuz\S*\s*(.*)$',
+        rf'^\s*linux\s+\S*{kernel_re}\S*\s*(.*)$',
         cfg, re.MULTILINE,
     )
     if not m:
-        tout.fatal('could not find a casper linux entry in /boot/grub/grub.cfg')
+        tout.fatal(
+            f'could not find a {kernel} linux entry in /boot/grub/grub.cfg'
+        )
     # Collapse any run of whitespace to a single space
     return ' '.join(m.group(1).split())
 
@@ -249,7 +254,7 @@  def main() -> None:
     title = args.title or f'U-Boot BLS boot ({vol_id})'
     cmdline = args.cmdline
     if cmdline is None:
-        cmdline = parse_grub_cmdline(args.iso)
+        cmdline = parse_grub_cmdline(args.iso, args.kernel)
     tout.notice(f'   Volume label: {vol_id}')
     tout.notice(f'   ESP GUID:     {esp_guid}')
     tout.notice(f'   Cmdline:      {cmdline}')