Skip to contentSkip to conversation

Add commands to load BLS and UKI files

PATCH Superseded Alec Brown via Grub-devel 2 participants 14 messages Mar 25, 2025 – Apr 22, 2025 Kbuild

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add commands to load BLS and UKI files

v2:
- Improved function prefixes to indicate internal blsuki functions.
- Added enums to indicate output from vercmp functions.
- Separated bls and uki create_entry functions to improve readability.
- Updated uki code to only store keyvals of necessary PE sections.
- Fixed a bug where an extra '/' was added to the start of kernel and initrd paths.
- Removed extra commands added to grub menu output for bls and uki.

This patch set is introducing BootLoaderSpec support to upstream GRUB from Fedora GRUB. I've also added a uki command to load Unified Kernel Images since it shares similar code to loading BLS config files.

Alec Brown

Alec Brown (1):
blsuki: Add uki command to load Unified Kernel Image entries

Peter Jones (1):
blsuki: Add blscfg command to parse Boot Loader Specification snippets

Robbie Harwood (1):
blsuki: Check for mounted /boot in emu

 Makefile.util.def               |   16 +++
 docs/grub.texi                  |   53 ++++++++
 grub-core/Makefile.core.def     |   14 ++
 grub-core/commands/blsuki.c     | 1453 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 grub-core/commands/legacycfg.c  |    4 +-
 grub-core/commands/menuentry.c  |    8 +-
 grub-core/lib/vercmp.c          |  317 +++++++++++++++++++++++++++++++++++++++++++
 grub-core/normal/main.c         |    6 +
 grub-core/osdep/linux/getroot.c |    8 ++
 grub-core/osdep/unix/getroot.c  |   10 ++
 include/grub/emu/misc.h         |    2 +-
 include/grub/lib/vercmp.h       |   35 +++++
 include/grub/menu.h             |   17 +++
 include/grub/normal.h           |    2 +-
 tests/vercmp_unit_test.c        |   65 +++++++++
 15 files changed, 2003 insertions(+), 7 deletions(-)

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#1 lore raw

PATCH v2 1/3 blsuki: Add blscfg command to parse Boot Loader Specification snippets

The BootLoaderSpec (BLS) defines a scheme where different bootloaders can share a format for boot items and a configuration directory that accepts these common configurations as drop-in files.

---
 Makefile.util.def              |   16 +
 docs/grub.texi                 |   27 +
 grub-core/Makefile.core.def    |   10 +
 grub-core/commands/blsuki.c    | 1028 ++++++++++++++++++++++++++++++++
 grub-core/commands/legacycfg.c |    4 +-
 grub-core/commands/menuentry.c |    8 +-
 grub-core/lib/vercmp.c         |  317 ++++++++++
 grub-core/normal/main.c        |    6 +
 include/grub/lib/vercmp.h      |   35 ++
 include/grub/menu.h            |   15 +
 include/grub/normal.h          |    2 +-
 tests/vercmp_unit_test.c       |   65 ++
 12 files changed, 1527 insertions(+), 6 deletions(-)
 create mode 100644 grub-core/commands/blsuki.c
 create mode 100644 grub-core/lib/vercmp.c
 create mode 100644 include/grub/lib/vercmp.h
 create mode 100644 tests/vercmp_unit_test.c
Makefile.util.def +16 -0
index 038253b37..a911f2e0a 100644
@@ -1373,6 +1373,22 @@ program = {
   ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
 };
 
+program = {
+  testcase = native;
+  name = vercmp_unit_test;
+  common = tests/vercmp_unit_test.c;
+  common = tests/lib/unit_test.c;
+  common = grub-core/kern/list.c;
+  common = grub-core/kern/misc.c;
+  common = grub-core/tests/lib/test.c;
+  common = grub-core/lib/vercmp.c;
+  ldadd = libgrubmods.a;
+  ldadd = libgrubgcry.a;
+  ldadd = libgrubkern.a;
+  ldadd = grub-core/lib/gnulib/libgnu.a;
+  ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
+};
+
 program = {
   name = grub-menulst2cfg;
   mansection = 1;
docs/grub.texi +27 -0
index d9b26fa36..19b0cc024 100644
@@ -6417,6 +6417,7 @@ you forget a command, you can run the command @command{help}
 * background_image::            Load background image for active terminal
 * badram::                      Filter out bad regions of RAM
 * blocklist::                   Print a block list
+* blscfg::                      Load Boot Loader Specification menu entries
 * boot::                        Start up your operating system
 * cat::                         Show the contents of a file
 * clear::                       Clear the screen
@@ -6603,6 +6604,32 @@ Print a block list (@pxref{Block list syntax}) for @var{file}.
 @end deffn
 
 
+@node blscfg
+@subsection blscfg
+
+@deffn Command blscfg [@option{--path} dir] [@option{--show-default}] [@option{--show-non-default}] [@option{--entry} file]
+Load Boot Loader Specification entries into the GRUB menu.
+
+The @option{--path} option overrides the default path to the directory containing
+the BLS entries. If this option isn't used, the default location is
+/loader/entries in @code{$BOOT}.
+
+The @option{--show-default} option allows the default boot entry to be added to the
+GRUB menu from the BLS entries.
+
+The @option{--show-non-default} option allows non-default boot entries to be added to
+the GRUB menu from the BLS entries.
+
+The @option{--entry} option allows specific boot entries to be added to the GRUB menu
+from the BLS entries.
+
+The @option{--entry}, @option{--show-default}, and @option{--show-non-default} options
+are used to filter which BLS entries are added to the GRUB menu. If none are
+used, all entries in the default location or the location specified by @option{--path}
+will be added to the GRUB menu.
+@end deffn
+
+
 @node boot
 @subsection boot
 
grub-core/Makefile.core.def +10 -0
index f70e02e69..f3b776c0a 100644
@@ -845,6 +845,16 @@ module = {
   common = commands/blocklist.c;
 };
 
+module = {
+  name = blsuki;
+  common = commands/blsuki.c;
+  common = lib/vercmp.c;
+  enable = powerpc_ieee1275;
+  enable = efi;
+  enable = i386_pc;
+  enable = emu;
+};
+
 module = {
   name = boot;
   common = commands/boot.c;
grub-core/commands/blsuki.c new file +1028 -0

1,028 lines not on this page · raw message

grub-core/commands/legacycfg.c +2 -2
index 3bf9fe2e4..f3c86dc7f 100644
@@ -143,7 +143,7 @@ legacy_file (const char *filename)
 	    args[0] = oldname;
 	    grub_normal_add_menu_entry (1, args, NULL, NULL, "legacy",
 					NULL, NULL,
-					entrysrc, 0);
+					entrysrc, 0, NULL);
 	    grub_free (args);
 	    entrysrc[0] = 0;
 	    grub_free (oldname);
@@ -204,7 +204,7 @@ legacy_file (const char *filename)
 	}
       args[0] = entryname;
       grub_normal_add_menu_entry (1, args, NULL, NULL, NULL,
-				  NULL, NULL, entrysrc, 0);
+				  NULL, NULL, entrysrc, 0, NULL);
       grub_free (args);
     }
 
grub-core/commands/menuentry.c +5 -3
index 720e6d8ea..09749c415 100644
@@ -78,7 +78,7 @@ grub_normal_add_menu_entry (int argc, const char **args,
 			    char **classes, const char *id,
 			    const char *users, const char *hotkey,
 			    const char *prefix, const char *sourcecode,
-			    int submenu)
+			    int submenu, grub_blsuki_entry_t *blsuki)
 {
   int menu_hotkey = 0;
   char **menu_args = NULL;
@@ -188,6 +188,7 @@ grub_normal_add_menu_entry (int argc, const char **args,
   (*last)->args = menu_args;
   (*last)->sourcecode = menu_sourcecode;
   (*last)->submenu = submenu;
+  (*last)->blsuki = blsuki;
 
   menu->size++;
   return GRUB_ERR_NONE;
@@ -286,7 +287,8 @@ grub_cmd_menuentry (grub_extcmd_context_t ctxt, int argc, char **args)
 				       users,
 				       ctxt->state[2].arg, 0,
 				       ctxt->state[3].arg,
-				       ctxt->extcmd->cmd->name[0] == 's');
+				       ctxt->extcmd->cmd->name[0] == 's',
+				       NULL);
 
   src = args[argc - 1];
   args[argc - 1] = NULL;
@@ -303,7 +305,7 @@ grub_cmd_menuentry (grub_extcmd_context_t ctxt, int argc, char **args)
 				  ctxt->state[0].args, ctxt->state[4].arg,
 				  users,
 				  ctxt->state[2].arg, prefix, src + 1,
-				  ctxt->extcmd->cmd->name[0] == 's');
+				  ctxt->extcmd->cmd->name[0] == 's', NULL);
 
   src[len - 1] = ch;
   args[argc - 1] = src;
grub-core/lib/vercmp.c new file +317 -0

317 lines not on this page · raw message

grub-core/normal/main.c +6 -0
index 04d058f55..2d493f21e 100644
@@ -21,6 +21,7 @@
 #include <grub/net.h>
 #include <grub/normal.h>
 #include <grub/dl.h>
+#include <grub/menu.h>
 #include <grub/misc.h>
 #include <grub/file.h>
 #include <grub/mm.h>
@@ -67,6 +68,11 @@ grub_normal_free_menu (grub_menu_t menu)
 	  grub_free (entry->args);
 	}
 
+      if (entry->blsuki)
+	{
+	  entry->blsuki->visible = 0;
+	}
+
       grub_free ((void *) entry->id);
       grub_free ((void *) entry->users);
       grub_free ((void *) entry->title);
include/grub/lib/vercmp.h new file +35 -0
new file mode 100644
index 000000000..e588ef530
@@ -0,0 +1,35 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2025  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef GRUB_VERCMP_H
+#define GRUB_VERCMP_H	1
+
+enum
+  {
+    GRUB_VERCMP_OLDER	= -1,
+    GRUB_VERCMP_SAME	=  0,
+    GRUB_VERCMP_NEWER	=  1,
+  };
+
+grub_err_t grub_vercmp (const char *a, const char *b, int *ret);
+
+void grub_split_package_string (char *package_string, char **name,
+				char **version, char **release);
+
+grub_err_t grub_split_vercmp (const char *nvr0, const char *nvr1, bool has_name, int *ret);
+
+#endif /* ! GRUB_VERCMP_H */
include/grub/menu.h +15 -0
index ee2b5e910..c25a0d16d 100644
@@ -20,6 +20,18 @@
 #ifndef GRUB_MENU_HEADER
 #define GRUB_MENU_HEADER 1
 
+struct grub_blsuki_entry
+{
+  struct grub_blsuki_entry *next;
+  struct grub_blsuki_entry **prev;
+  struct keyval **keyvals;
+  grub_size_t keyvals_size;
+  int nkeyvals;
+  char *filename;
+  int visible;
+};
+typedef struct grub_blsuki_entry grub_blsuki_entry_t;
+
 struct grub_menu_entry_class
 {
   char *name;
@@ -60,6 +72,9 @@ struct grub_menu_entry
 
   /* The next element.  */
   struct grub_menu_entry *next;
+
+  /* BLS used to populate the entry */
+  grub_blsuki_entry_t *blsuki;
 };
 typedef struct grub_menu_entry *grub_menu_entry_t;
 
include/grub/normal.h +1 -1
index 218cbabcc..d0150e3c2 100644
@@ -145,7 +145,7 @@ grub_normal_add_menu_entry (int argc, const char **args, char **classes,
 			    const char *id,
 			    const char *users, const char *hotkey,
 			    const char *prefix, const char *sourcecode,
-			    int submenu);
+			    int submenu, grub_blsuki_entry_t *blsuki);
 
 grub_err_t
 grub_normal_set_password (const char *user, const char *password);
tests/vercmp_unit_test.c new file +65 -0

65 lines not on this page · raw message

Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

PATCH v2 2/3 blsuki: Check for mounted /boot in emu

In reply to Alec Brown via Grub-devel

Irritatingly, BLS defines paths relatives to the mountpoint of the filesystem which contains its snippets, not / or any other fixed location. So grub2-emu needs to know whether /boot is a separate filesystem from / and conditionally prepend a path.

---
 grub-core/Makefile.core.def     |  4 +++
 grub-core/commands/blsuki.c     | 54 ++++++++++++++++++++++++++++-----
 grub-core/osdep/linux/getroot.c |  8 +++++
 grub-core/osdep/unix/getroot.c  | 10 ++++++
 include/grub/emu/misc.h         |  2 +-
 5 files changed, 70 insertions(+), 8 deletions(-)
grub-core/Makefile.core.def +4 -0
index f3b776c0a..9a0e7bc55 100644
@@ -367,6 +367,10 @@ kernel = {
   emu = kern/emu/cache_s.S;
   emu = kern/emu/hostdisk.c;
   emu = osdep/unix/hostdisk.c;
+  emu = osdep/relpath.c;
+  emu = osdep/getroot.c;
+  emu = osdep/unix/getroot.c;
+  emu = osdep/devmapper/getroot.c;
   emu = osdep/exec.c;
   extra_dist = osdep/unix/exec.c;
   emu = osdep/devmapper/hostdisk.c;
grub-core/commands/blsuki.c +47 -7

112 lines not on this page · raw message

grub-core/osdep/linux/getroot.c +8 -0
index 527d4f0c5..2fc212276 100644
@@ -131,6 +131,7 @@ struct mountinfo_entry
   char fstype[ESCAPED_PATH_MAX + 1], device[ESCAPED_PATH_MAX + 1];
 };
 
+#ifdef GRUB_UTIL
 static char **
 grub_util_raid_getmembers (const char *name, int bootable)
 {
@@ -191,6 +192,7 @@ grub_util_raid_getmembers (const char *name, int bootable)
 
   return devicelist;
 }
+#endif
 
 /* Statting something on a btrfs filesystem always returns a virtual device
    major/minor pair rather than the real underlying device, because btrfs
@@ -579,6 +581,7 @@ out:
   return ret;
 }
 
+#ifdef GRUB_UTIL
 static char *
 get_mdadm_uuid (const char *os_dev)
 {
@@ -636,6 +639,7 @@ out:
 
   return name;
 }
+#endif
 
 static int
 grub_util_is_imsm (const char *os_dev)
@@ -968,6 +972,7 @@ grub_util_part_to_disk (const char *os_dev, struct stat *st,
   return path;
 }
 
+#ifdef GRUB_UTIL
 static char *
 grub_util_get_raid_grub_dev (const char *os_dev)
 {
@@ -1070,6 +1075,7 @@ grub_util_get_raid_grub_dev (const char *os_dev)
   }
   return grub_dev;
 }
+#endif
 
 enum grub_dev_abstraction_types
 grub_util_get_dev_abstraction_os (const char *os_dev)
@@ -1086,6 +1092,7 @@ grub_util_get_dev_abstraction_os (const char *os_dev)
   return GRUB_DEV_ABSTRACTION_NONE;
 }
 
+#ifdef GRUB_UTIL
 int
 grub_util_pull_device_os (const char *os_dev,
 			  enum grub_dev_abstraction_types ab)
@@ -1142,6 +1149,7 @@ grub_util_get_grub_dev_os (const char *os_dev)
 
   return grub_dev;
 }
+#endif
 
 char *
 grub_make_system_path_relative_to_its_root_os (const char *path)
grub-core/osdep/unix/getroot.c +10 -0
index c7aa202ab..1380638d3 100644
@@ -17,6 +17,14 @@
  */
 
 #include <config.h>
+#undef _GL_INLINE_HEADER_BEGIN
+#undef _GL_INLINE_HEADER_END
+#undef _GL_GNUC_PREREQ
+#undef _GL_ATTRIBUTE_COLD
+#undef _GL_ATTRIBUTE_CONST
+#undef _GL_ATTRIBUTE_DEALLOC
+#undef _GL_ATTRIBUTE_FALLTHROUGH
+#undef _GL_ATTRIBUTE_MALLOC
 #include <config-util.h>
 
 #include <sys/stat.h>
@@ -566,6 +574,7 @@ grub_guess_root_devices (const char *dir_in)
 
 #endif
 
+#ifdef GRUB_UTIL
 void
 grub_util_pull_lvm_by_command (const char *os_dev)
 {
@@ -662,6 +671,7 @@ out:
   free (buf);
   free (vgid);
 }
+#endif
 
 /* ZFS has similar problems to those of btrfs (see above).  */
 void
include/grub/emu/misc.h +1 -1
index fefbec499..542f4c18d 100644
@@ -39,7 +39,7 @@ void grub_fini_all (void);
 void grub_find_zpool_from_dir (const char *dir,
 			       char **poolname, char **poolfs);
 
-char *grub_make_system_path_relative_to_its_root (const char *path)
+char * EXPORT_FUNC(grub_make_system_path_relative_to_its_root) (const char *path)
  WARN_UNUSED_RESULT;
 int
 grub_util_device_is_mapped (const char *dev);
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

Re: [PATCH v2 2/3] blsuki: Check for mounted /boot in emu

Quoted 54 lines · Alec Brown

Irritatingly, BLS defines paths relatives to the mountpoint of the filesystem which contains its snippets, not / or any other fixed location. So grub2-emu needs to know whether /boot is a separate filesystem from / and conditionally prepend a path.

---
 grub-core/Makefile.core.def     |  4 +++
 grub-core/commands/blsuki.c     | 54 ++++++++++++++++++++++++++++-----
 grub-core/osdep/linux/getroot.c |  8 +++++
 grub-core/osdep/unix/getroot.c  | 10 ++++++
 include/grub/emu/misc.h         |  2 +-
 5 files changed, 70 insertions(+), 8 deletions(-)
grub-core/Makefile.core.def +4 -0
index f3b776c0a..9a0e7bc55 100644
@@ -367,6 +367,10 @@ kernel = {
   emu = kern/emu/cache_s.S;
   emu = kern/emu/hostdisk.c;
   emu = osdep/unix/hostdisk.c;
+  emu = osdep/relpath.c;
+  emu = osdep/getroot.c;
+  emu = osdep/unix/getroot.c;
+  emu = osdep/devmapper/getroot.c;
   emu = osdep/exec.c;
   extra_dist = osdep/unix/exec.c;
   emu = osdep/devmapper/hostdisk.c;
grub-core/commands/blsuki.c +9 -0
index 0f77fb568..12a9a1ed1 100644
@@ -32,6 +32,13 @@
 #include <grub/lib/envblk.h>
 #include <grub/lib/vercmp.h>
 
+#ifdef GRUB_MACHINE_EMU
+#include <grub/emu/misc.h>
+#define GRUB_BOOT_DEVICE "/boot"
+#else
+#define GRUB_BOOT_DEVICE ""
+#endif
+
 GRUB_MOD_LICENSE ("GPLv3+");
 
 #define GRUB_BLS_CONFIG_PATH "/loader/entries/"
@@ -53,8 +60,40 @@ struct keyval
 
 static grub_blsuki_entry_t *entries = NULL;
 
+/*
+ * Cache probing in blsuki_update_boot_device(). Used for linux entry
Quoted 4 lines

also.
+ */
+static int separate_boot = -1;

On non-emu it's an unused static. It might trigger a warning. I'd prefer this to be inside ifdef.

+

Quoted 11 lines

#define FOR_BLSUKI_ENTRIES(var) FOR_LIST_ELEMENTS (var, entries)

grub-core/commands/blsuki.c +8 -0
+/*
+ * BLS appears to make paths relative to the filesystem that snippets are
+ * on, not /.  Attempt to cope.
+ */
+static char *blsuki_update_boot_device (char *tmp)
+{
+#ifdef GRUB_MACHINE_EMU
+  char *ret;

grub-core/commands/blsuki.c struct keyval · line 87

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+
+  if (separate_boot != -1)
+    goto probed;
+
+  separate_boot = 0;
+
+  ret = grub_make_system_path_relative_to_its_root (GRUB_BOOT_DEVICE);
+
+  if (ret != NULL)
+    separate_boot = 1;

Are you sure that != NULL is the right check? It looks like it should be strcmp with "/". How do you get a NULL here? Shouldn't happen if the path exists.

Quoted 208 lines
grub-core/commands/blsuki.c +10 -1
+
+ probed:
+  if (!separate_boot)
+    return grub_stpcpy (tmp, " ");
+#endif
+
+  return grub_stpcpy (tmp, " " GRUB_BOOT_DEVICE);
+}
+
 static grub_err_t
 blsuki_add_keyval (grub_blsuki_entry_t *entry, char *key, char *val)
 {
@@ -645,7 +684,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
 
       for (i = 0; early_initrds != NULL && early_initrds[i] != NULL; i++)
        {
-         if (grub_add (initrd_size, sizeof (" "), &initrd_size) ||
+         if (grub_add (initrd_size, sizeof (" " GRUB_BOOT_DEVICE),

&initrd_size) ||
grub_add (initrd_size, grub_strlen (initrd_prefix),
&initrd_size) ||
grub_add (initrd_size, grub_strlen (early_initrds[i]),
&initrd_size) ||
grub_add (initrd_size, 1, &initrd_size))

grub-core/commands/blsuki.c +1 -1
@@ -657,7 +696,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
 
       for (i = 0; initrds != NULL && initrds[i] != NULL; i++)
        {
-         if (grub_add (initrd_size, sizeof (" "), &initrd_size) ||
+         if (grub_add (initrd_size, sizeof (" " GRUB_BOOT_DEVICE),

&initrd_size) ||
grub_add (initrd_size, grub_strlen (initrds[i]),
&initrd_size) ||
grub_add (initrd_size, 1, &initrd_size))
{

grub-core/commands/blsuki.c +1 -1
@@ -683,7 +722,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
       for (i = 0; early_initrds != NULL && early_initrds[i] != NULL; i++)
        {
          grub_dprintf ("blsuki", "adding early initrd %s\n",
 early_initrds[i]);
-         tmp = grub_stpcpy (tmp, " ");
+         tmp = blsuki_update_boot_device (tmp);
          tmp = grub_stpcpy (tmp, initrd_prefix);
          tmp = grub_stpcpy (tmp, early_initrds[i]);
          grub_free (early_initrds[i]);
grub-core/commands/blsuki.c +1 -2
@@ -692,7 +731,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
       for (i = 0; initrds != NULL && initrds[i] != NULL; i++)
        {
          grub_dprintf ("blsuki", "adding initrd %s\n", initrds[i]);
-         tmp = grub_stpcpy (tmp, " ");
+         tmp = blsuki_update_boot_device (tmp);
          tmp = grub_stpcpy (tmp, initrds[i]);
        }
       tmp = grub_stpcpy (tmp, "\n");
@@ -711,7 +750,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
            }
        }
 
-      if (grub_add (sizeof ("devicetree "), grub_strlen (devicetree),

&dt_size) ||
+ if (grub_add (sizeof ("devicetree " GRUB_BOOT_DEVICE), grub_strlen
(devicetree), &dt_size) ||

          grub_add (dt_size, 1, &dt_size))
        {
          grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected

calculating device tree buffer size");

grub-core/commands/blsuki.c +3 -2
@@ -735,7 +774,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
        }
       tmp = dt;
       tmp = grub_stpcpy (dt, "devicetree");
-      tmp = grub_stpcpy (tmp, " ");
+      tmp = blsuki_update_boot_device (tmp);
       if (add_dt_prefix == true)
        tmp = grub_stpcpy (tmp, prefix);
       tmp = grub_stpcpy (tmp, devicetree);
@@ -748,9 +787,10 @@ bls_create_entry (grub_blsuki_entry_t *entry)
 
   sdval = grub_env_get ("save_default");
   savedefault = ((NULL != sdval) && (grub_strcmp (sdval, "true") == 0));
-  src = grub_xasprintf ("%slinux %s%s%s\n"
+  src = grub_xasprintf ("%slinux %s%s%s%s\n"
                        "%s%s",
                        savedefault ? "savedefault\n" : "",
+                       separate_boot ? GRUB_BOOT_DEVICE : "",
                        clinux, options ? " " : "", options ? options : "",
                        initrd ? initrd : "", dt ? dt : "");
 
grub-core/osdep/linux/getroot.c +2 -0
index 527d4f0c5..2fc212276 100644
@@ -131,6 +131,7 @@ struct mountinfo_entry
   char fstype[ESCAPED_PATH_MAX + 1], device[ESCAPED_PATH_MAX + 1];
 };
 
+#ifdef GRUB_UTIL
 static char **
 grub_util_raid_getmembers (const char *name, int bootable)
 {
@@ -191,6 +192,7 @@ grub_util_raid_getmembers (const char *name, int
 bootable)
 
   return devicelist;
 }
+#endif
 
 /* Statting something on a btrfs filesystem always returns a virtual

device
major/minor pair rather than the real underlying device, because btrfs

grub-core/osdep/linux/getroot.c +3 -0
@@ -579,6 +581,7 @@ out:
   return ret;
 }
 
+#ifdef GRUB_UTIL
 static char *
 get_mdadm_uuid (const char *os_dev)
 {
@@ -636,6 +639,7 @@ out:
 
   return name;
 }
+#endif
 
 static int
 grub_util_is_imsm (const char *os_dev)
@@ -968,6 +972,7 @@ grub_util_part_to_disk (const char *os_dev, struct
 stat *st,
   return path;
 }
 
+#ifdef GRUB_UTIL
 static char *
 grub_util_get_raid_grub_dev (const char *os_dev)

{

grub-core/osdep/linux/getroot.c +3 -0
@@ -1070,6 +1075,7 @@ grub_util_get_raid_grub_dev (const char *os_dev)
   }
   return grub_dev;
 }
+#endif
 
 enum grub_dev_abstraction_types
 grub_util_get_dev_abstraction_os (const char *os_dev)
@@ -1086,6 +1092,7 @@ grub_util_get_dev_abstraction_os (const char *os_dev)
   return GRUB_DEV_ABSTRACTION_NONE;
 }
 
+#ifdef GRUB_UTIL
 int
 grub_util_pull_device_os (const char *os_dev,
                          enum grub_dev_abstraction_types ab)
@@ -1142,6 +1149,7 @@ grub_util_get_grub_dev_os (const char *os_dev)
 
   return grub_dev;
 }
+#endif
 
 char *
 grub_make_system_path_relative_to_its_root_os (const char *path)
grub-core/osdep/unix/getroot.c +10 -0
index c7aa202ab..1380638d3 100644
@@ -17,6 +17,14 @@
  */
 
 #include <config.h>
+#undef _GL_INLINE_HEADER_BEGIN
+#undef _GL_INLINE_HEADER_END
+#undef _GL_GNUC_PREREQ
+#undef _GL_ATTRIBUTE_COLD
+#undef _GL_ATTRIBUTE_CONST
+#undef _GL_ATTRIBUTE_DEALLOC
+#undef _GL_ATTRIBUTE_FALLTHROUGH
+#undef _GL_ATTRIBUTE_MALLOC
 #include <config-util.h>
 
 #include <sys/stat.h>
@@ -566,6 +574,7 @@ grub_guess_root_devices (const char *dir_in)
 
 #endif
 
+#ifdef GRUB_UTIL
 void
 grub_util_pull_lvm_by_command (const char *os_dev)
 {
@@ -662,6 +671,7 @@ out:
   free (buf);
   free (vgid);
 }
+#endif
 
 /* ZFS has similar problems to those of btrfs (see above).  */
 void
include/grub/emu/misc.h +1 -1
index fefbec499..542f4c18d 100644
@@ -39,7 +39,7 @@ void grub_fini_all (void);
 void grub_find_zpool_from_dir (const char *dir,
                               char **poolname, char **poolfs);
 
-char *grub_make_system_path_relative_to_its_root (const char *path)
+char * EXPORT_FUNC(grub_make_system_path_relative_to_its_root) (const
 char *path)
  WARN_UNUSED_RESULT;
 int
Quoted 5 lines

grub_util_device_is_mapped (const char *dev);
--
2.27.0

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw

RE: [PATCH v2 2/3] blsuki: Check for mounted /boot in emu

Quoted 54 lines · Vladimir 'phcoder' Serbinenko
Quoted 52 lines · Alec Brown

Irritatingly, BLS defines paths relatives to the mountpoint of the filesystem which contains its snippets, not / or any other fixed location. So grub2-emu needs to know whether /boot is a separate filesystem from / and conditionally prepend a path.

---
 grub-core/Makefile.core.def     |  4 +++
 grub-core/commands/blsuki.c     | 54 ++++++++++++++++++++++++++++-----
 grub-core/osdep/linux/getroot.c |  8 +++++
 grub-core/osdep/unix/getroot.c  | 10 ++++++
 include/grub/emu/misc.h         |  2 +-
 5 files changed, 70 insertions(+), 8 deletions(-)
grub-core/Makefile.core.def +4 -0
index f3b776c0a..9a0e7bc55 100644
@@ -367,6 +367,10 @@ kernel = {
   emu = kern/emu/cache_s.S;
   emu = kern/emu/hostdisk.c;
   emu = osdep/unix/hostdisk.c;
+  emu = osdep/relpath.c;
+  emu = osdep/getroot.c;
+  emu = osdep/unix/getroot.c;
+  emu = osdep/devmapper/getroot.c;
   emu = osdep/exec.c;
   extra_dist = osdep/unix/exec.c;
   emu = osdep/devmapper/hostdisk.c;
grub-core/commands/blsuki.c +7 -0
index 0f77fb568..12a9a1ed1 100644
@@ -32,6 +32,13 @@
 #include <grub/lib/envblk.h>
 #include <grub/lib/vercmp.h>
 
+#ifdef GRUB_MACHINE_EMU
+#include <grub/emu/misc.h>
+#define GRUB_BOOT_DEVICE "/boot"
+#else
+#define GRUB_BOOT_DEVICE ""
+#endif
+
 GRUB_MOD_LICENSE ("GPLv3+");
 
 #define GRUB_BLS_CONFIG_PATH "/loader/entries/"
@@ -53,8 +60,40 @@ struct keyval
 
 static grub_blsuki_entry_t *entries = NULL;
 
Quoted 9 lines
Quoted 6 lines
grub-core/commands/blsuki.c +2 -0
+/*
+ * Cache probing in blsuki_update_boot_device(). Used for linux entry

also.
+ */
+static int separate_boot = -1;

On non-emu it's an unused static. It might trigger a warning. I'd prefer this to be inside ifdef.

There's one instance where this value gets used to check if we add the GRUB_BOOT_DEVICE to the start of the Linux kernel path, but this could be changed to use the blsuki_update_boot_device() instead.

Quoted 18 lines
Quoted 18 lines
grub-core/commands/blsuki.c +15 -0
+
 #define FOR_BLSUKI_ENTRIES(var) FOR_LIST_ELEMENTS (var, entries)
 
+/*
+ * BLS appears to make paths relative to the filesystem that snippets are
+ * on, not /.  Attempt to cope.
+ */
+static char *blsuki_update_boot_device (char *tmp)
+{
+#ifdef GRUB_MACHINE_EMU
+  char *ret;
+
+  if (separate_boot != -1)
+    goto probed;
+
+  separate_boot = 0;
+
Quoted 8 lines

grub-core/commands/blsuki.c struct keyval · line 87

Quoted 5 lines
grub-core/commands/blsuki.c +4 -0
+  ret = grub_make_system_path_relative_to_its_root (GRUB_BOOT_DEVICE);
+
+  if (ret != NULL)
+    separate_boot = 1;

Are you sure that != NULL is the right check? It looks like it should be strcmp with "/". How do you get a NULL here? Shouldn't happen if the path exists.

Ah you are correct. I think I confused this with the output of grub_make_system_path_relative_to_its_root_os(). Upon review of the function, if the path given is the mount point, it will return an empty string since it won't return a trailing "/". I'll fix my code to reflect this.

Quoted 10 lines

grub-core/commands/blsuki.c struct keyval · line 96

Quoted 10 lines
grub-core/commands/blsuki.c +9 -0
+
+ probed:
+  if (!separate_boot)
+    return grub_stpcpy (tmp, " ");
+#endif
+
+  return grub_stpcpy (tmp, " " GRUB_BOOT_DEVICE);
+}
+

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw

PATCH v2 3/3 blsuki: Add uki command to load Unified Kernel Image entries

In reply to Alec Brown via Grub-devel

A Unified Kernel Image is a single UEFI PE file that combines a UEFI boot stub, a Linux kernel image, an initrd, and further resources. The uki command will locate where the uki file is and create a GRUB menu entry to load it.

---
 docs/grub.texi              |  26 +++
 grub-core/commands/blsuki.c | 415 ++++++++++++++++++++++++++++++++++--
 include/grub/menu.h         |   2 +
 3 files changed, 428 insertions(+), 15 deletions(-)
docs/grub.texi +26 -0
index 19b0cc024..9317b4130 100644
@@ -6491,6 +6491,7 @@ you forget a command, you can run the command @command{help}
 * tpm2_key_protector_clear::    Clear the TPM2 key protector
 * true::                        Do nothing, successfully
 * trust::                       Add public key to list of trusted keys
+* uki::                         Load Unified Kernel Image menu entries
 * unset::                       Unset an environment variable
 @comment * vbeinfo::                     List available video modes
 * verify_detached::             Verify detached digital signature
@@ -8164,6 +8165,31 @@ Unset the environment variable @var{envvar}.
 @end deffn
 
 
+@node uki
+@subsection uki
+
+@deffn Command uki [@option{--path} dir] [@option{--show-default}] [@option{--show-non-default}] [@option{--entry} file]
+Load Unified Kernel Image entries into the GRUB menu.
+
+The @option{--path} option overrides the default path to the directory containing
+the UKI entries. If this option isn't used, the default location is
+/EFI/Linux in the EFI system partition.
+
+The @option{--show-default} option allows the default boot entry to be added to the
+GRUB menu from the UKI entries.
+
+The @option{--show-non-default} option allows non-default boot entries to be added to
+the GRUB menu from the UKI entries.
+
+The @option{--entry} option allows specific boot entries to be added to the GRUB menu
+from the UKI entries.
+
+The @option{--entry}, @option{--show-default}, and @option{--show-non-default} options
+are used to filter which UKI entries are added to the GRUB menu. If none are
+used, all entries in the default location or the location specified by @option{--path}
+will be added to the GRUB menu.
+@end deffn
+
 @ignore
 @node vbeinfo
 @subsection vbeinfo
grub-core/commands/blsuki.c +400 -15

550 lines not on this page · raw message

include/grub/menu.h +2 -0
index c25a0d16d..907373625 100644
@@ -28,6 +28,8 @@ struct grub_blsuki_entry
   grub_size_t keyvals_size;
   int nkeyvals;
   char *filename;
+  char *dirname;
+  char *devid;
   int visible;
 };
 typedef struct grub_blsuki_entry grub_blsuki_entry_t;
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#6 lore raw

Re: [PATCH v2 3/3] blsuki: Add uki command to load Unified Kernel Image entries

grub-core/commands/blsuki.c line 47

Quoted 10 lines
grub-core/commands/blsuki.c +6 -0
+#ifdef GRUB_MACHINE_EFI
+#include <grub/efi/efi.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/pe32.h>
+#endif
+

Can UKI work without EFI? I think of scenario of putting e.g. EFI disk into coreboot or BIOS machine.

grub-core/commands/blsuki.c line 56

Quoted 10 lines

GRUB_MOD_LICENSE ("GPLv3+");

#define GRUB_BLS_CONFIG_PATH "/loader/entries/"

grub-core/commands/blsuki.c +6 -0
+#define GRUB_UKI_CONFIG_PATH "/EFI/Linux"
+
+#define GRUB_BLS_CMD 1
+#define GRUB_UKI_CMD 2
+
+static int cmd_type = 0;

Can we make this into an enum?

Quoted 8 lines
grub-core/commands/blsuki.c +3 -0
+  if (pe->optional_header.magic != GRUB_PE32_NATIVE_MAGIC)
+    {
+      err = grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "non-native image

not supported");

Maybe it's a bad kernel and not not implemented yet? Later indicates that sick a config is valid, just not supported yet. Is it so?

Quoted 7 lines
grub-core/commands/blsuki.c +3 -0
+
+static char *
+uki_read_osrel (char *content, grub_off_t *pos, char **key_ret, char

**val_ret)

grub-core/commands/blsuki.c +2 -0
+{
+  char *line;

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 462

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+  char *value;
+  grub_size_t linelen;
+
+ skip:
+  line = content + *pos;
+  if (*line == '\0')
+    return NULL;
+
+  linelen = 0;
+  while (line[linelen] != '\0' && !grub_strchr ("\n\r", line[linelen]))

While I recognize the elegance of strchr, we don't use this trick in the code and it makes it more difficult to read. Can you use 2 comparisons instead?

Quoted 6 lines
grub-core/commands/blsuki.c +5 -0
+    linelen++;
+
+  /* Move pos to the next line */
+  *pos += linelen;
+  if (content[*pos] != '\0')

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 477

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+    (*pos)++;
+
+  /* Skip empty line */
+  if (linelen == 0)
+    goto skip;
+
+  line[linelen] = '\0';
+
+  /* Remove leading white space */
+  while (grub_strchr (" \t", *line))

Ditto

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 485

Quoted 9 lines
grub-core/commands/blsuki.c +8 -0
+    {
+      line++;
+      linelen--;
+    }
+
+  /* Remove trailing whitespace */
+  while (linelen > 0 && grub_strchr ("=", line[linelen - 1]))
+    linelen--;

Comment doesn't match what really happens here. Also strchr with single character string makes no sense.

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 494

Quoted 10 lines
grub-core/commands/blsuki.c +9 -0
+  line[linelen] = '\0';
+
+  if (*line == '#')
+    goto skip;
+
+  /* Split key/value */
+  value = line;
+  while (*value != '\0' && !grub_strchr ("=", *value))
+    value++;

Ditto

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 500

Quoted 7 lines
grub-core/commands/blsuki.c +6 -0
+  if (*value == '\0')
+    goto skip;
+  *value = '\0';
+  value++;
+  while (*value != '\0' && grub_strchr ("=", *value))
+    value++;

Ditto

Quoted 6 lines
grub-core/commands/blsuki.c +3 -0
+  if (grub_mul (argc + 1, sizeof (char *), &size))
+    {
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected creating

argv list"));

Not worth translating. Generally the code which is only to guard against malicious input is not worth translating

+ goto finish;

grub-core/commands/blsuki.c bls_create_entry (grub_blsuki_entry_t *entry) · line 1116

Quoted 6 lines
grub-core/commands/blsuki.c +5 -0
+    }
+  argv = grub_malloc (size);
+  if (argv == NULL)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, "failed to allocate argv list");

Grub_malloc already sets err no.

grub-core/commands/blsuki.c bls_create_entry (grub_blsuki_entry_t *entry) · line 1122

Quoted 7 lines
grub-core/commands/blsuki.c +6 -0
+      goto finish;
+    }
+  argv[0] = title;
+  argv[argc] = NULL;
+
+  src = grub_xasprintf ("insmod chain\n"

Why do you need insmod? Didn't automatic insmod work?

+ "chainloader (%s)%s/%s%s%s\n",

Quoted 4 lines
(quoted diff) +2 -0
+                       entry->devid, entry->dirname,
+                       entry->filename, options ? " " : "", options ?

options : "");

Can we have a Linux variant for non-EFI?

grub-core/commands/blsuki.c blsuki_find_entry (struct find_entry_info *info) · line 1187

Quoted 12 lines

/*
* If we aren't able to find BLS entries in the directory given by
info->dirname,
* we can fallback to the default location "/boot/loader/entries/" and
see if we

grub-core/commands/blsuki.c +2 -1
-   * can find the files there.
+   * can find the files there. If we can't find UKI entries, fallback to
+   * "/boot/efi/EFI/Linux".
    */

What's the purpose of fallback? It's not what user/script has requested. It needs to be at very least disableable

grub-core/commands/blsuki.c blsuki_load_entries (char *path) · line 1276

Quoted 11 lines
grub-core/commands/blsuki.c +8 -0
+       }
+      else if (cmd_type == GRUB_UKI_CMD)
+       {
+#ifdef GRUB_MACHINE_EFI
+         grub_efi_loaded_image_t *image;
+         image = grub_efi_get_loaded_image (grub_efi_image_handle);
+         devid = grub_efidisk_get_device_name (image->device_handle);
+#endif

This uses grub image location. What about a scenario when booted from external drive and I want to boot into install on primary disk?

grub-core/commands/blsuki.c grub_cmd_blscfg (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)), · line 1417

Quoted 9 lines
(quoted diff) +2 -0
+static grub_err_t
+grub_cmd_blscfg (grub_extcmd_context_t ctxt, int argc __attribute__

((unused)),

grub-core/commands/blsuki.c +4 -0
+                char **args __attribute__ ((unused)))
+{
+  cmd_type = GRUB_BLS_CMD;
+  return blsuki_cmd (ctxt);

Do we really need a static variable? Maybe a parameter is more appropriate?

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#7 lore raw

RE: [PATCH v2 3/3] blsuki: Add uki command to load Unified Kernel Image entries

Quoted 12 lines · Vladimir 'phcoder' Serbinenko

grub-core/commands/blsuki.c line 47

Quoted 10 lines
grub-core/commands/blsuki.c +6 -0
+#ifdef GRUB_MACHINE_EFI
+#include <grub/efi/efi.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/pe32.h>
+#endif
+

Can UKI work without EFI? I think of scenario of putting e.g. EFI disk into coreboot or BIOS machine.

No UKI only works EFI systems.

Quoted 12 lines

grub-core/commands/blsuki.c line 56

Quoted 11 lines

GRUB_MOD_LICENSE ("GPLv3+");

#define GRUB_BLS_CONFIG_PATH "/loader/entries/"

grub-core/commands/blsuki.c +6 -0
+#define GRUB_UKI_CONFIG_PATH "/EFI/Linux"
+
+#define GRUB_BLS_CMD 1
+#define GRUB_UKI_CMD 2
+
+static int cmd_type = 0;

Can we make this into an enum?

Sure thing!

Quoted 11 lines
Quoted 9 lines
grub-core/commands/blsuki.c +3 -0
+  if (pe->optional_header.magic != GRUB_PE32_NATIVE_MAGIC)
+    {
+      err = grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "non-native image

not supported");

Maybe it's a bad kernel and not not implemented yet? Later indicates that sick a config is valid, just not supported yet. Is it so?

Ah. I copied this error from a comparison that checks the same magic number in grub-core/loader/efi/linux.c. I'll adjust this error statement.

Quoted 13 lines
Quoted 13 lines
grub-core/commands/blsuki.c +3 -0
+
+static char *
+uki_read_osrel (char *content, grub_off_t *pos, char **key_ret, char

**val_ret)

grub-core/commands/blsuki.c +8 -0
+{
+  char *line;
+  char *value;
+  grub_size_t linelen;
+
+ skip:
+  line = content + *pos;
+  if (*line == '\0')
Quoted 8 lines

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 462

Quoted 5 lines
grub-core/commands/blsuki.c +4 -0
+    return NULL;
+
+  linelen = 0;
+  while (line[linelen] != '\0' && !grub_strchr ("\n\r", line[linelen]))

While I recognize the elegance of strchr, we don't use this trick in the code and it makes it more difficult to read. Can you use 2 comparisons instead?

Sure I'll do that instead.

Quoted 70 lines
Quoted 7 lines
grub-core/commands/blsuki.c +5 -0
+    linelen++;
+
+  /* Move pos to the next line */
+  *pos += linelen;
+  if (content[*pos] != '\0')

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 477

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+    (*pos)++;
+
+  /* Skip empty line */
+  if (linelen == 0)
+    goto skip;
+
+  line[linelen] = '\0';
+
+  /* Remove leading white space */
+  while (grub_strchr (" \t", *line))

Ditto

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 485

Quoted 9 lines
grub-core/commands/blsuki.c +8 -0
+    {
+      line++;
+      linelen--;
+    }
+
+  /* Remove trailing whitespace */
+  while (linelen > 0 && grub_strchr ("=", line[linelen - 1]))
+    linelen--;

Comment doesn't match what really happens here. Also strchr with single character string makes no sense.

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 494

Quoted 10 lines
grub-core/commands/blsuki.c +9 -0
+  line[linelen] = '\0';
+
+  if (*line == '#')
+    goto skip;
+
+  /* Split key/value */
+  value = line;
+  while (*value != '\0' && !grub_strchr ("=", *value))
+    value++;

Ditto

grub-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 500

Quoted 7 lines
grub-core/commands/blsuki.c +6 -0
+  if (*value == '\0')
+    goto skip;
+  *value = '\0';
+  value++;
+  while (*value != '\0' && grub_strchr ("=", *value))
+    value++;

Ditto

Quoted 6 lines
grub-core/commands/blsuki.c +3 -0
+  if (grub_mul (argc + 1, sizeof (char *), &size))
+    {
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected creating

argv list"));

Not worth translating. Generally the code which is only to guard against malicious input is not worth translating

grub-core/commands/blsuki.c bls_create_entry (grub_blsuki_entry_t *entry) · line 1116

Quoted 7 lines
grub-core/commands/blsuki.c +6 -0
+      goto finish;
+    }
+  argv = grub_malloc (size);
+  if (argv == NULL)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, "failed to allocate argv list");

Grub_malloc already sets err no.

Quoted 8 lines

grub-core/commands/blsuki.c bls_create_entry (grub_blsuki_entry_t *entry) · line 1122

Quoted 7 lines
grub-core/commands/blsuki.c +6 -0
+      goto finish;
+    }
+  argv[0] = title;
+  argv[argc] = NULL;
+
+  src = grub_xasprintf ("insmod chain\n"

Why do you need insmod? Didn't automatic insmod work?

I didn't realize there was an automatic insmod. I just tested this without the insmod call and you are correct that I don't need this.

Quoted 20 lines
Quoted 6 lines
grub-core/commands/blsuki.c +3 -0
+                       "chainloader (%s)%s/%s%s%s\n",
+                       entry->devid, entry->dirname,
+                       entry->filename, options ? " " : "", options ?

options : "");

Can we have a Linux variant for non-EFI?

grub-core/commands/blsuki.c blsuki_find_entry (struct find_entry_info *info) · line 1187

Quoted 12 lines

/*
* If we aren't able to find BLS entries in the directory given by
info->dirname,
* we can fallback to the default location "/boot/loader/entries/" and
see if we

grub-core/commands/blsuki.c +2 -1
-   * can find the files there.
+   * can find the files there. If we can't find UKI entries, fallback to
+   * "/boot/efi/EFI/Linux".
    */
Quoted 2 lines

What's the purpose of fallback? It's not what user/script has requested. It needs to be at very least disableable

The fallback code was from some of the old blscfg code I was working with and I added the UKI default directory. I'll add an option in case the user wants this behavior.

Quoted 14 lines

grub-core/commands/blsuki.c blsuki_load_entries (char *path) · line 1276

Quoted 12 lines
grub-core/commands/blsuki.c +8 -0
+       }
+      else if (cmd_type == GRUB_UKI_CMD)
+       {
+#ifdef GRUB_MACHINE_EFI
+         grub_efi_loaded_image_t *image;
+         image = grub_efi_get_loaded_image (grub_efi_image_handle);
+         devid = grub_efidisk_get_device_name (image->device_handle);
+#endif

This uses grub image location. What about a scenario when booted from external drive and I want to boot into install on primary disk?

Maybe I might not understand this scenario very well, but would it be better to load the UKI directory using the "--path" option? This bit of code is trying to locate the EFI system partition of the default directory. I'm not entirely sure the best way to find the default directory if it were to be on a different drive.

Quoted 11 lines

grub-core/commands/blsuki.c grub_cmd_blscfg (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)), · line 1417

Quoted 10 lines
(quoted diff) +2 -0
+static grub_err_t
+grub_cmd_blscfg (grub_extcmd_context_t ctxt, int argc __attribute__

((unused)),

grub-core/commands/blsuki.c +4 -0
+                char **args __attribute__ ((unused)))
+{
+  cmd_type = GRUB_BLS_CMD;
+  return blsuki_cmd (ctxt);

Do we really need a static variable? Maybe a parameter is more appropriate?

Sure I can use a parameter if that would be better.
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#8 lore raw

Re: [PATCH v2 3/3] blsuki: Add uki command to load Unified Kernel Image entries

Quoted 13 lines · Alec Brown
Quoted 11 lines · Vladimir 'phcoder' Serbinenko

grub-core/commands/blsuki.c line 47

Quoted 10 lines
grub-core/commands/blsuki.c +6 -0
+#ifdef GRUB_MACHINE_EFI
+#include <grub/efi/efi.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/pe32.h>
+#endif
+

Can UKI work without EFI? I think of scenario of putting e.g. EFI disk

Quoted 5 lines

into

Quoted 1 line

coreboot or BIOS machine.

No UKI only works EFI systems.

Can GRUB parse image and get kernel and initrd out of it and load like Linux? What prevents add doing so. This is not a problem for this patch per se but I want to understand

Quoted 27 lines
Quoted 3 lines
Quoted 2 lines

What's the purpose of fallback? It's not what user/script has requested.

It

Quoted 1 line

needs to be at very least disableable

The fallback code was from some of the old blscfg code I was working with and
I added the UKI default directory. I'll add an option in case the user wants
this behavior.

Quoted 14 lines

grub-core/commands/blsuki.c blsuki_load_entries (char *path) · line 1276

Quoted 12 lines
grub-core/commands/blsuki.c +8 -0
+       }
+      else if (cmd_type == GRUB_UKI_CMD)
+       {
+#ifdef GRUB_MACHINE_EFI
+         grub_efi_loaded_image_t *image;
+         image = grub_efi_get_loaded_image (grub_efi_image_handle);
+         devid = grub_efidisk_get_device_name (image->device_handle);
+#endif

This uses grub image location. What about a scenario when booted from external drive and I want to boot into install on primary disk?

Quoted 8 lines

Maybe I might not understand this scenario very well, but would it be better
to load the UKI directory using the "--path" option? This bit of code is trying to locate the EFI system partition of the default directory. I'm not entirely sure the best way to find the default directory if it were to be on
a different drive.

Does --path disable default path scanning?

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#9 lore raw

RE: [PATCH v2 3/3] blsuki: Add uki command to load Unified Kernel Image entries

Quoted 16 lines · Vladimir 'phcoder' Serbinenko
Quoted 14 lines · Alec Brown
Quoted 11 lines · Vladimir 'phcoder' Serbinenko

grub-core/commands/blsuki.c line 47

Quoted 10 lines
grub-core/commands/blsuki.c +6 -0
+#ifdef GRUB_MACHINE_EFI
+#include <grub/efi/efi.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/pe32.h>
+#endif
+

Can UKI work without EFI? I think of scenario of putting e.g. EFI disk

into

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

coreboot or BIOS machine.

No UKI only works EFI systems.

Can GRUB parse image and get kernel and initrd out of it and load like Linux? What prevents add doing so. This is not a problem for this patch per se but I want to understand

If we aren't utilizing the EFI stub in the UKI, we could parse the kernel and initrd from the section data and load it from there. In that case, I suppose it would be possible to load it for a coreboot or BIOS machine.

Quoted 36 lines
Quoted 28 lines
Quoted 4 lines
Quoted 3 lines

What's the purpose of fallback? It's not what user/script has requested.

It

Quoted 1 line

needs to be at very least disableable

The fallback code was from some of the old blscfg code I was working with and
I added the UKI default directory. I'll add an option in case the user wants
this behavior.

Quoted 14 lines

grub-core/commands/blsuki.c blsuki_load_entries (char *path) · line 1276

Quoted 12 lines
grub-core/commands/blsuki.c +8 -0
+       }
+      else if (cmd_type == GRUB_UKI_CMD)
+       {
+#ifdef GRUB_MACHINE_EFI
+         grub_efi_loaded_image_t *image;
+         image = grub_efi_get_loaded_image (grub_efi_image_handle);
+         devid = grub_efidisk_get_device_name (image->device_handle);
+#endif

This uses grub image location. What about a scenario when booted from external drive and I want to boot into install on primary disk?

Quoted 8 lines

Maybe I might not understand this scenario very well, but would it be better
to load the UKI directory using the "--path" option? This bit of code is trying to locate the EFI system partition of the default directory. I'm not entirely sure the best way to find the default directory if it were to be on
a different drive.

Quoted 1 line

Does --path disable default path scanning?

In this version, it does not. But in the newest version, I've added an option to enable or disable a fallback that would scan for the default path.
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#10 lore raw

Re: [PATCH v2 3/3] blsuki: Add uki command to load Unified Kernel Image entries

Quoted 26 lines · Alec Brown
Quoted 1 line · Vladimir 'phcoder' Serbinenko

Le jeu. 27 mars 2025, 23:44, Alec Brown <[email protected]> a

écrit :

Quoted 14 lines
Quoted 14 lines
Quoted 11 lines · Vladimir 'phcoder' Serbinenko

grub-core/commands/blsuki.c line 47

Quoted 10 lines
grub-core/commands/blsuki.c +6 -0
+#ifdef GRUB_MACHINE_EFI
+#include <grub/efi/efi.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/pe32.h>
+#endif
+

Can UKI work without EFI? I think of scenario of putting e.g. EFI disk

Quoted 8 lines
Quoted 5 lines

into

Quoted 1 line

coreboot or BIOS machine.

No UKI only works EFI systems.

Can GRUB parse image and get kernel and initrd out of it and load like Linux? What prevents add doing so. This is not a problem for this patch

Quoted 9 lines

per

Quoted 1 line

se but I want to understand

If we aren't utilizing the EFI stub in the UKI, we could parse the kernel and
initrd from the section data and load it from there. In that case, I suppose
it would be possible to load it for a coreboot or BIOS machine.

I propose then to have some kind of "uki" command which would map to EFI chainloader for now and later we can add implementation for non-EFI.

Quoted 39 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

What's the purpose of fallback? It's not what user/script has

requested.

Quoted 4 lines
Quoted 4 lines

It

Quoted 1 line

needs to be at very least disableable

The fallback code was from some of the old blscfg code I was working

with

Quoted 8 lines
Quoted 8 lines

and
I added the UKI default directory. I'll add an option in case the user wants
this behavior.

Quoted 3 lines
Quoted 3 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

grub-core/commands/blsuki.c blsuki_load_entries (char *path)

Quoted 7 lines
grub-core/commands/blsuki.c +7 -0
+       }
+      else if (cmd_type == GRUB_UKI_CMD)
+       {
+#ifdef GRUB_MACHINE_EFI
+         grub_efi_loaded_image_t *image;
+         image = grub_efi_get_loaded_image (grub_efi_image_handle);
+         devid = grub_efidisk_get_device_name

(image->device_handle);

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

+#endif

This uses grub image location. What about a scenario when booted from external drive and I want to boot into install on primary disk?

Maybe I might not understand this scenario very well, but would it be better
to load the UKI directory using the "--path" option? This bit of code is trying to locate the EFI system partition of the default directory. I'm

not

Quoted 1 line
Quoted 1 line

entirely sure the best way to find the default directory if it were to

be

Quoted 9 lines
Quoted 4 lines
Quoted 3 lines

on
a different drive.

Does --path disable default path scanning?

In this version, it does not. But in the newest version, I've added an option
to enable or disable a fallback that would scan for the default path.

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#11 lore raw

Re: [PATCH v2 3/3] blsuki: Add uki command to load Unified Kernel Image entries

Hi,

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

Can GRUB parse image and get kernel and initrd out of it and load like Linux? What prevents add doing so. This is not a problem for this patch per se but I want to understand

If we aren't utilizing the EFI stub in the UKI, we could parse the kernel and initrd from the section data and load it from there. In that case, I suppose it would be possible to load it for a coreboot or BIOS machine.

Well, yes and no. A UKI is more than just a packaging format ...

The systemd-stub can for example load extensions, which uses EFI protocols for filesystem access. That comes with challenges even in EFI mode because grub does not register EFI_SIMPLE_FILESYSTEM_PROTOCOL instances for the non-ESP filesystems it is able to read, so that can work only for UKIs loaded from the ESP.

Also the initrd (systemd-gpt-auto-generator to be exact) will look for the LoaderDevicePartUUID EFI variable to figure which disk it has been booted from when searching for the root filesystem, which obviously is not going to work on BIOS/coreboot. There are ways to hack around that limitation, for example by implementing the root filesystem search in grub + patch the kernel command line.

We even had a prototype for the latter, to allow hybrid BIOS/UEFI cloud images using UKIs, but at the end of the day dropped the ball on this and went for separate UKI images for a number of reasons.

Quoted 3 lines

I propose then to have some kind of "uki" command which would map to EFI chainloader for now and later we can add implementation for non-EFI.

Aliasing 'uki' to 'chainloader' is probably a good idea, even of only for better usability. It's not obvious that 'chainloader' should be used to load UKIs.

When it comes to implement 'uki' for non-EFI systems I have my doubts that this adds much value due to UKIs being designed for EFI systems as outlined above.

my two cents,
Gerd

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#12 lore raw

Re: [PATCH v2 3/3] blsuki: Add uki command to load Unified Kernel Image entries

In reply to Vladimir 'phcoder' Serbinenko
Quoted 19 lines · Vladimir 'phcoder' Serbinenko
Quoted 15 lines · Alec Brown
Quoted 12 lines · Vladimir 'phcoder' Serbinenko

grub-core/commands/blsuki.c line 47

Quoted 10 lines
grub-core/commands/blsuki.c +6 -0
+#ifdef GRUB_MACHINE_EFI
+#include <grub/efi/efi.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/pe32.h>
+#endif
+

Can UKI work without EFI? I think of scenario of putting e.g. EFI disk into coreboot or BIOS machine.

Quoted 4 lines
Quoted 1 line

No UKI only works EFI systems.

Can GRUB parse image and get kernel and initrd out of it and load like Linux? What prevents add doing so. This is not a problem for this patch per se but I want to understand

I hacked that together for my own tests a while back and was able to boot a UKI using the contained linux, initrd, cmdline in BIOS mode.

Quoted 31 lines
Quoted 25 lines
Quoted 5 lines
Quoted 3 lines

What's the purpose of fallback? It's not what user/script has requested. It needs to be at very least disableable

The fallback code was from some of the old blscfg code I was working with and I added the UKI default directory. I'll add an option in case the user wants this behavior.

Quoted 14 lines

grub-core/commands/blsuki.c blsuki_load_entries (char *path) · line 1276

Quoted 12 lines
grub-core/commands/blsuki.c +8 -0
+       }
+      else if (cmd_type == GRUB_UKI_CMD)
+       {
+#ifdef GRUB_MACHINE_EFI
+         grub_efi_loaded_image_t *image;
+         image = grub_efi_get_loaded_image (grub_efi_image_handle);
+         devid = grub_efidisk_get_device_name (image->device_handle);
+#endif

This uses grub image location. What about a scenario when booted from external drive and I want to boot into install on primary disk?

Quoted 5 lines

Maybe I might not understand this scenario very well, but would it be better to load the UKI directory using the "--path" option? This bit of code is trying to locate the EFI system partition of the default directory. I'm not entirely sure the best way to find the default directory if it were to be on a different drive.

Quoted 5 lines

Does --path disable default path scanning?
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#13 lore raw

Re: [PATCH v2 3/3] blsuki: Add uki command to load Unified Kernel Image entries

In reply to Alec Brown via Grub-devel
Quoted 6 lines · Alec Brown via Grub-devel

A Unified Kernel Image is a single UEFI PE file that combines a UEFI boot stub, a Linux kernel image, an initrd, and further resources. The uki command will locate where the uki file is and create a GRUB menu entry to load it.

Nice to see someone picking that back up. I had proposed something similar a while ago,
although over at the rhboot grub, because it already had blscfg support back then.
https://github.com/rhboot/grub2/pull/119

Oliver

Quoted 638 lines
---
 docs/grub.texi              |  26 +++
 grub-core/commands/blsuki.c | 415 ++++++++++++++++++++++++++++++++++--
 include/grub/menu.h         |   2 +
 3 files changed, 428 insertions(+), 15 deletions(-)
docs/grub.texi +26 -0
index 19b0cc024..9317b4130 100644
@@ -6491,6 +6491,7 @@ you forget a command, you can run the command @command{help}
 * tpm2_key_protector_clear::    Clear the TPM2 key protector
 * true::                        Do nothing, successfully
 * trust::                       Add public key to list of trusted keys
+* uki::                         Load Unified Kernel Image menu entries
 * unset::                       Unset an environment variable
 @comment * vbeinfo::                     List available video modes
 * verify_detached::             Verify detached digital signature
@@ -8164,6 +8165,31 @@ Unset the environment variable @var{envvar}.
 @end deffn
 
 
+@node uki
+@subsection uki
+
+@deffn Command uki [@option{--path} dir] [@option{--show-default}] [@option{--show-non-default}] [@option{--entry} file]
+Load Unified Kernel Image entries into the GRUB menu.
+
+The @option{--path} option overrides the default path to the directory containing
+the UKI entries. If this option isn't used, the default location is
+/EFI/Linux in the EFI system partition.
+
+The @option{--show-default} option allows the default boot entry to be added to the
+GRUB menu from the UKI entries.
+
+The @option{--show-non-default} option allows non-default boot entries to be added to
+the GRUB menu from the UKI entries.
+
+The @option{--entry} option allows specific boot entries to be added to the GRUB menu
+from the UKI entries.
+
+The @option{--entry}, @option{--show-default}, and @option{--show-non-default} options
+are used to filter which UKI entries are added to the GRUB menu. If none are
+used, all entries in the default location or the location specified by @option{--path}
+will be added to the GRUB menu.
+@end deffn
+
 @ignore
 @node vbeinfo
 @subsection vbeinfo
grub-core/commands/blsuki.c +400 -15
index 12a9a1ed1..bf284e002 100644
@@ -39,9 +39,21 @@
 #define GRUB_BOOT_DEVICE ""
 #endif
 
+#ifdef GRUB_MACHINE_EFI
+#include <grub/efi/efi.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/pe32.h>
+#endif
+
 GRUB_MOD_LICENSE ("GPLv3+");
 
 #define GRUB_BLS_CONFIG_PATH "/loader/entries/"
+#define GRUB_UKI_CONFIG_PATH "/EFI/Linux"
+
+#define GRUB_BLS_CMD 1
+#define GRUB_UKI_CMD 2
+
+static int cmd_type = 0;
 
 static const struct grub_arg_option bls_opt[] =
   {
@@ -52,6 +64,17 @@ static const struct grub_arg_option bls_opt[] =
     {0, 0, 0, 0, 0, 0}
   };
 
+#ifdef GRUB_MACHINE_EFI
+static const struct grub_arg_option uki_opt[] =
+  {
+    {"path", 'p', 0, N_("Specify path to find UKI entries."), N_("DIR"), ARG_TYPE_PATHNAME},
+    {"show-default", 'd', 0, N_("Allow the default UKI entry to be added to the GRUB menu."), 0, ARG_TYPE_NONE},
+    {"show-non-default", 'n', 0, N_("Allow the non-default UKI entries to be added to the GRUB menu."), 0, ARG_TYPE_NONE},
+    {"entry", 'e', 0, N_("Allow specificUKII entries to be added to the GRUB menu."), N_("FILE"), ARG_TYPE_FILE},
+    {0, 0, 0, 0, 0, 0}
+  };
+#endif
+
 struct keyval
 {
   const char *key;
@@ -288,6 +311,206 @@ bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry)
   return err;
 }
 
+#ifdef GRUB_MACHINE_EFI
+static grub_err_t
+uki_read_entry (grub_file_t f, grub_blsuki_entry_t *entry)
+{
+  struct grub_msdos_image_header *dos = NULL;
+  struct grub_pe_image_header *pe = NULL;
+  grub_off_t section_offset = 0;
+  struct grub_pe32_section_table *section = NULL;
+  struct grub_pe32_coff_header *coff_header = NULL;
+  char *val = NULL;
+  char *key = NULL;
+  const char *target[] = {".cmdline", ".osrel", ".linux", NULL};
+  bool has_linux = false;
+  grub_err_t err = GRUB_ERR_NONE;
+
+  dos = grub_zalloc (sizeof (*dos));
+  if (dos == NULL)
+    return grub_errno;
+  if (grub_file_read (f, dos, sizeof (*dos)) < (grub_ssize_t) sizeof (*dos))
+    {
+      err = grub_error (GRUB_ERR_FILE_READ_ERROR, "failed to read UKI image header");
+      goto fail;
+    }
+  if (dos->msdos_magic != GRUB_PE32_MAGIC)
+    {
+      err = grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, N_("plain image kernel not supported"));
+      goto fail;
+    }
+
+  grub_dprintf ("blsuki", "PE/COFF header @ %08x\n", dos->pe_image_header_offset);
+  pe = grub_zalloc (sizeof (*pe));
+  if (pe == NULL)
+    {
+      err = grub_errno;
+      goto fail;
+    }
+  if (grub_file_seek (f, dos->pe_image_header_offset) == (grub_off_t) -1
+      || grub_file_read (f, pe, sizeof (*pe)) != sizeof (*pe))
+    {
+      err = grub_error (GRUB_ERR_FILE_READ_ERROR, "failed to read COFF image header");
+      goto fail;
+    }
+  if (pe->optional_header.magic != GRUB_PE32_NATIVE_MAGIC)
+    {
+      err = grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "non-native image not supported");
+      goto fail;
+    }
+
+  coff_header = &(pe->coff_header);
+  section_offset = dos->pe_image_header_offset + sizeof (*pe);
+
+  for (int i = 0; i < coff_header->num_sections; i++)
+    {
+      key = NULL;
+      val = NULL;
+      section = grub_zalloc (sizeof (*section));
+      if (section == NULL)
+       {
+         err = grub_errno;
+         goto fail;
+       }
+
+      grub_file_seek (f, section_offset);
+      if (grub_file_read (f, section, sizeof (*section)) != sizeof (*section))
+       {
+         err = grub_error (GRUB_ERR_FILE_READ_ERROR, "failed to read section header");
+         goto fail;
+       }
+
+      key = grub_strndup (section->name, 8);
+      if (key == NULL)
+       {
+         err = grub_errno;
+         goto fail;
+       }
+
+      for (int j = 0; target[j] != NULL; j++)
+       {
+         if (grub_strcmp (key, target[j]) == 0)
+           {
+             /*
+              * We don't need to read the contents of the .linux PE section, but we
+              * should verify that the section exists.
+              */
+             if (grub_strcmp (key, ".linux") == 0)
+               {
+                 has_linux = true;
+                 break;
+               }
+
+             val = grub_zalloc (section->raw_data_size);
+             if (val == NULL)
+               {
+                 err = grub_errno;
+                 goto fail;
+               }
+
+             grub_file_seek (f, section->raw_data_offset);
+             if (grub_file_read (f, val, section->raw_data_size) != (grub_ssize_t) section->raw_data_size)
+               {
+                 err = grub_error (GRUB_ERR_FILE_READ_ERROR, "failed to read section");
+                 goto fail;
+               }
+
+             err = blsuki_add_keyval (entry, key, val);
+             if (err != GRUB_ERR_NONE)
+               goto fail;
+
+             break;
+           }
+       }
+
+      section_offset += sizeof (*section);
+      grub_free (section);
+      grub_free (val);
+      grub_free (key);
+    }
+
+  if (has_linux == false)
+    err = grub_error (GRUB_ERR_NO_KERNEL, "UKI is missing the '.linux' section");
+
+  grub_free (dos);
+  grub_free (pe);
+  return err;
+
+ fail:
+  grub_free (dos);
+  grub_free (pe);
+  grub_free (section);
+  grub_free (val);
+  grub_free (key);
+  return err;
+}
+#endif
+
+static char *
+uki_read_osrel (char *content, grub_off_t *pos, char **key_ret, char **val_ret)
+{
+  char *line;
+  char *value;
+  grub_size_t linelen;
+
+ skip:
+  line = content + *pos;
+  if (*line == '\0')
+    return NULL;
+
+  linelen = 0;
+  while (line[linelen] != '\0' && !grub_strchr ("\n\r", line[linelen]))
+    linelen++;
+
+  /* Move pos to the next line */
+  *pos += linelen;
+  if (content[*pos] != '\0')
+    (*pos)++;
+
+  /* Skip empty line */
+  if (linelen == 0)
+    goto skip;
+
+  line[linelen] = '\0';
+
+  /* Remove leading white space */
+  while (grub_strchr (" \t", *line))
+    {
+      line++;
+      linelen--;
+    }
+
+  /* Remove trailing whitespace */
+  while (linelen > 0 && grub_strchr ("=", line[linelen - 1]))
+    linelen--;
+  line[linelen] = '\0';
+
+  if (*line == '#')
+    goto skip;
+
+  /* Split key/value */
+  value = line;
+  while (*value != '\0' && !grub_strchr ("=", *value))
+    value++;
+  if (*value == '\0')
+    goto skip;
+  *value = '\0';
+  value++;
+  while (*value != '\0' && grub_strchr ("=", *value))
+    value++;
+
+  /* Remove quotes from value */
+  if (value[0] == '\"' && line[linelen - 1] == '\"')
+    {
+      value++;
+      line[linelen - 1] = '\0';
+    }
+
+  *key_ret = line;
+  *val_ret = value;
+  return line;
+}
+
 struct read_entry_info
 {
   const char *devid;
@@ -301,9 +524,11 @@ blsuki_read_entry (const char *filename,
                   void *data)
 {
   grub_size_t m = 0, n, ext_len = 5;
-  grub_err_t err;
+  grub_err_t err = GRUB_ERR_NONE;
   char *p = NULL;
+  const char *ext = NULL;
   grub_file_t f = NULL;
+  enum grub_file_type file_type = 0;
   grub_blsuki_entry_t *entry;
   struct read_entry_info *info = (struct read_entry_info *) data;
 
@@ -311,6 +536,18 @@ blsuki_read_entry (const char *filename,
 
   n = grub_strlen (filename);
 
+  if (cmd_type == GRUB_BLS_CMD)
+    {
+      ext = ".conf";
+      file_type = GRUB_FILE_TYPE_CONFIG;
+    }
+  else if (cmd_type == GRUB_UKI_CMD)
+    {
+      ext = ".efi";
+      file_type = GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE;
+    }
+  ext_len = grub_strlen (ext);
+
   if (info->file != NULL)
     {
       f = info->file;
@@ -320,15 +557,14 @@ blsuki_read_entry (const char *filename,
       if (filename[0] == '.')
        return 0;
 
-      if (n <= 5)
+      if (n <= ext_len)
        return 0;
 
-      if (grub_strcmp (filename + n - ext_len, ".conf") != 0)
+      if (grub_strcmp (filename + n - ext_len, ext) != 0)
        return 0;
 
       p = grub_xasprintf ("(%s)%s/%s", info->devid, info->dirname, filename);
-
-      f = grub_file_open (p, GRUB_FILE_TYPE_CONFIG);
+      f = grub_file_open (p, file_type);
       grub_free (p);
       if (f == NULL)
        goto finish;
@@ -365,7 +601,26 @@ blsuki_read_entry (const char *filename,
       goto finish;
     }
 
-  err = bls_read_entry (f, entry);
+  entry->dirname = grub_strdup (info->dirname);
+  if (entry->dirname == NULL)
+    {
+      grub_free (entry);
+      goto finish;
+    }
+
+  entry->devid = grub_strdup (info->devid);
+  if (entry->devid == NULL)
+    {
+      grub_free (entry);
+      goto finish;
+    }
+
+  if (cmd_type == GRUB_BLS_CMD)
+    err = bls_read_entry (f, entry);
+#ifdef GRUB_MACHINE_EFI
+  else if (cmd_type == GRUB_UKI_CMD)
+    err = uki_read_entry (f, entry);
+#endif
 
   if (err == GRUB_ERR_NONE)
     blsuki_add_entry (entry);
@@ -810,6 +1065,75 @@ bls_create_entry (grub_blsuki_entry_t *entry)
   grub_free (src);
 }
 
+static void
+uki_create_entry (grub_blsuki_entry_t *entry)
+{
+  int argc = 0;
+  const char **argv = NULL;
+  char *id = entry->filename;
+  char *title = NULL;
+  char *options = NULL;
+  char *osrel = NULL;
+  char *line;
+  char *key = NULL;
+  char *value = NULL;
+  char *src = NULL;
+  grub_size_t size = 0;
+  grub_off_t pos = 0;
+
+  options = blsuki_get_val (entry, ".cmdline", NULL);
+  if (options == NULL)
+    {
+      grub_dprintf ("blsuki", "Skipping file %s with no '.cmdline' key.\n", entry->filename);
+      goto finish;
+    }
+  osrel = blsuki_get_val (entry, ".osrel", NULL);
+  if (osrel == NULL)
+    {
+      grub_dprintf ("blsuki", "Skipping file %s with no '.osrel' key.\n", entry->filename);
+      goto finish;
+    }
+
+  line = osrel;
+  while ((line = uki_read_osrel (osrel, &pos, &key, &value)))
+    {
+      if (grub_strcmp ("PRETTY_NAME", key) == 0)
+       {
+         title = value;
+         break;
+       }
+    }
+
+  argc += 1;
+  if (grub_mul (argc + 1, sizeof (char *), &size))
+    {
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected creating argv list"));
+      goto finish;
+    }
+  argv = grub_malloc (size);
+  if (argv == NULL)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, "failed to allocate argv list");
+      goto finish;
+    }
+  argv[0] = title;
+  argv[argc] = NULL;
+
+  src = grub_xasprintf ("insmod chain\n"
+                       "chainloader (%s)%s/%s%s%s\n",
+                       entry->devid, entry->dirname,
+                       entry->filename, options ? " " : "", options ? options : "");
+
+
+  grub_normal_add_menu_entry (argc, argv, NULL, id, NULL, NULL, NULL, src, 0, entry);
+
+ finish:
+  grub_free (argv);
+  grub_free (src);
+  grub_free (options);
+  grub_free (osrel);
+}
+
 struct find_entry_info
 {
   const char *dirname;
@@ -830,7 +1154,12 @@ blsuki_find_entry (struct find_entry_info *info)
   int r = 0;
 
   if (dir == NULL)
-    dir = GRUB_BLS_CONFIG_PATH;
+    {
+      if (cmd_type == GRUB_BLS_CMD)
+       dir = GRUB_BLS_CONFIG_PATH;
+      else if (cmd_type == GRUB_UKI_CMD)
+       dir = GRUB_UKI_CONFIG_PATH;
+    }
 
   read_entry_info.file = NULL;
   read_entry_info.dirname = dir;
@@ -853,11 +1182,17 @@ blsuki_find_entry (struct find_entry_info *info)
   /*
    * If we aren't able to find BLS entries in the directory given by info->dirname,
    * we can fallback to the default location "/boot/loader/entries/" and see if we
-   * can find the files there.
+   * can find the files there. If we can't find UKI entries, fallback to
+   * "/boot/efi/EFI/Linux".
    */
   if (r != 0 && info->dirname == NULL && fallback == 0)
     {
-      read_entry_info.dirname = "/boot" GRUB_BLS_CONFIG_PATH;
+      if (cmd_type == GRUB_BLS_CMD)
+       read_entry_info.dirname = "/boot" GRUB_BLS_CONFIG_PATH;
+      else if (cmd_type == GRUB_UKI_CMD)
+       {
+         read_entry_info.dirname = GRUB_UKI_CONFIG_PATH;
+       }
       grub_dprintf ("blsuki", "Entries weren't found in %s, fallback to %s\n",
                    dir, read_entry_info.dirname);
       fallback = 1;
@@ -869,11 +1204,13 @@ static grub_err_t
 blsuki_load_entries (char *path)
 {
   grub_size_t len;
+  grub_size_t ext_len;
   grub_fs_t fs;
   grub_device_t dev;
   static grub_err_t r;
   const char *devid = NULL;
   char *dir = NULL;
+  const char *ext = NULL;
   struct find_entry_info info = {
       .dev = NULL,
       .fs = NULL,
@@ -886,8 +1223,14 @@ blsuki_load_entries (char *path)
 
   if (path != NULL)
     {
+      if (cmd_type == GRUB_BLS_CMD)
+       ext = ".conf";
+      else if (cmd_type == GRUB_UKI_CMD)
+       ext = ".efi";
+
       len = grub_strlen (path);
-      if (grub_strcmp (path + len - 5, ".conf") == 0)
+      ext_len = grub_strlen (ext);
+      if (grub_strcmp (path + len - ext_len, ext) == 0)
        {
          rei.file = grub_file_open (path, GRUB_FILE_TYPE_CONFIG);
          if (rei.file == NULL)
@@ -916,11 +1259,22 @@ blsuki_load_entries (char *path)
 
   if (devid == NULL)
     {
+      if (cmd_type == GRUB_BLS_CMD)
+       {
 #ifdef GRUB_MACHINE_EMU
-      devid = "host";
+         devid = "host";
 #else
-      devid = grub_env_get ("root");
+         devid = grub_env_get ("root");
 #endif
+       }
+      else if (cmd_type == GRUB_UKI_CMD)
+       {
+#ifdef GRUB_MACHINE_EFI
+         grub_efi_loaded_image_t *image;
+         image = grub_efi_get_loaded_image (grub_efi_image_handle);
+         devid = grub_efidisk_get_device_name (image->device_handle);
+#endif
+       }
       if (devid == NULL)
        return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "root");
     }
@@ -1001,7 +1355,10 @@ blsuki_create_entries (bool show_default, bool show_non_default, char *entry_id)
          (show_non_default == true && blsuki_is_default_entry (def_entry, entry, idx) == false) ||
          (entry_id != NULL && grub_strcmp (entry_id, entry->filename) == 0))
        {
-         bls_create_entry (entry);
+         if (cmd_type == GRUB_BLS_CMD)
+           bls_create_entry (entry);
+         else if (cmd_type == GRUB_UKI_CMD)
+           uki_create_entry (entry);
          entry->visible = 1;
        }
       idx++;
@@ -1011,8 +1368,7 @@ blsuki_create_entries (bool show_default, bool show_non_default, char *entry_id)
 }
 
 static grub_err_t
-grub_cmd_blscfg (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)),
-                char **args __attribute__ ((unused)))
+blsuki_cmd (grub_extcmd_context_t ctxt)
 {
   grub_err_t err;
   struct grub_arg_list *state = ctxt->state;
@@ -1021,6 +1377,7 @@ grub_cmd_blscfg (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)),
   bool show_default = false;
   bool show_non_default = false;
   bool all = true;
+  entries = NULL;
 
   if (state[0].set)
     path = state[0].arg;
@@ -1052,17 +1409,45 @@ grub_cmd_blscfg (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)),
   return blsuki_create_entries (show_default, show_non_default, entry_id);
 }
 
+static grub_err_t
+grub_cmd_blscfg (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)),
+                char **args __attribute__ ((unused)))
+{
+  cmd_type = GRUB_BLS_CMD;
+  return blsuki_cmd (ctxt);
+}
+
 static grub_extcmd_t bls_cmd;
 
+#ifdef GRUB_MACHINE_EFI
+static grub_err_t
+grub_cmd_uki (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)),
+                char **args __attribute__ ((unused)))
+{
+  cmd_type = GRUB_UKI_CMD;
+  return blsuki_cmd (ctxt);
+}
+
+static grub_extcmd_t uki_cmd;
+#endif
+
 GRUB_MOD_INIT(blsuki)
 {
   bls_cmd = grub_register_extcmd ("blscfg", grub_cmd_blscfg, 0,
                                  N_("[-p|--path] DIR [-d|--show-default] [-n|--show-non-default] [-e|--entry] FILE"),
                                  N_("Import Boot Loader Specification snippets."),
                                  bls_opt);
+#ifdef GRUB_MACHINE_EFI
+  uki_cmd = grub_register_extcmd ("uki", grub_cmd_uki, 0,
+                                 N_("[-p|--path] DIR [-d|--show-default] [-n|--show-non-default] [-e|--entry] FILE"),
+                                 N_("Import Unified Kernel Images"), uki_opt);
+#endif
 }
 
 GRUB_MOD_FINI(blsuki)
 {
   grub_unregister_extcmd (bls_cmd);
+#ifdef GRUB_MACHINE_EFI
+  grub_unregister_extcmd (uki_cmd);
+#endif
 }
include/grub/menu.h +2 -1
index c25a0d16d..907373625 100644
@@ -28,6 +28,8 @@ struct grub_blsuki_entry
   grub_size_t keyvals_size;
   int nkeyvals;
   char *filename;
+  char *dirname;
+  char *devid;
   int visible;
 };
 typedef struct grub_blsuki_entry grub_blsuki_entry_t;
--

2.27.0

Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel