Skip to contentSkip to conversation

Add commands to load BLS and UKI files

PATCH Superseded Alec Brown via Grub-devel 3 participants 12 messages Apr 12, 2025 – Apr 21, 2025 Kbuild

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add commands to load BLS and UKI files

v3:
- Added --enable-fallback option to check the default directory if the --path option isn't able to find entries.
- Added the function blsuki_set_find_entry_info() to help set the path and device of BLS and UKI entries.
- Removed grub_strchr uses in uki_read_osrel().
- Converted the static variable "cmd_type" into a parameter and added enums.
- Fixed improper handling of the output of grub_make_system_path_relative_to_its_root().

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                  |   55 ++++++++
 grub-core/Makefile.core.def     |   14 ++
 grub-core/commands/blsuki.c     | 1519 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 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, 2071 insertions(+), 7 deletions(-)

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

#1 lore raw

PATCH v3 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                 |   29 +
 grub-core/Makefile.core.def    |   10 +
 grub-core/commands/blsuki.c    | 1057 ++++++++++++++++++++++++++++++++
 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, 1558 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 +29 -0
index d9b26fa36..ea7a13953 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,34 @@ Print a block list (@pxref{Block list syntax}) for @var{file}.
 @end deffn
 
 
+@node blscfg
+@subsection blscfg
+
+@deffn Command blscfg [@option{--path} dir] [@option{--enable-fallback}] [@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}. If no BLS entries are found, the
+@option{--enable-fallback} option can be used to check for entries in the default
+directory.
+
+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 +1057 -0

1,057 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

Re: [PATCH v3 1/3] blsuki: Add blscfg command to parse Boot Loader Specification snippets

Missing From:...

Quoted 3 lines · Alec Brown

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.

Please add links to the specification here.

Quoted 37 lines
---
 Makefile.util.def              |   16 +
 docs/grub.texi                 |   29 +
 grub-core/Makefile.core.def    |   10 +
 grub-core/commands/blsuki.c    | 1057 ++++++++++++++++++++++++++++++++
 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, 1558 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 +6 -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;

Makefile.util.def program = { · line 1391

Quoted 10 lines
Makefile.util.def +10 -0
+  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)';
+};
+

Please move tests to separate patch.

Quoted 16 lines
Makefile.util.def +0 -0
 program = {
   name = grub-menulst2cfg;
   mansection = 1;
docs/grub.texi +1 -0
index d9b26fa36..ea7a13953 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

docs/grub.texi Print a block list (@pxref{Block list syntax}) for @var{file}. · line 6610

Quoted 7 lines
docs/grub.texi +4 -0
@@ -6603,6 +6604,34 @@ Print a block list (@pxref{Block list syntax}) for @var{file}.
 @end deffn
 
 
+@node blscfg
+@subsection blscfg
+
+@deffn Command blscfg [@option{--path} dir] [@option{--enable-fallback}] [@option{--show-default}] [@option{--show-non-default}] [@option{--entry} file]

The long options have short counterparts. Why do not you list them here?

Quoted 12 lines
docs/grub.texi +12 -0
+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}. If no BLS entries are found, the
+@option{--enable-fallback} option can be used to check for entries in the default
+directory.
+
+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

docs/grub.texi Print a block list (@pxref{Block list syntax}) for @var{file}. · line 6632

Quoted 10 lines
docs/grub.texi +10 -0
+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

I would add links to the specification here and give some examples of BLS files. Additionally, I think it is worth mentioning how this integrates with current grub.cfg and how both methods of loading system images are related.

Quoted 8 lines
docs/grub.texi +0 -0
 @node boot
 @subsection boot
 
grub-core/Makefile.core.def +0 -0
index f70e02e69..f3b776c0a 100644

grub-core/Makefile.core.def module = { · line 851

Quoted 7 lines
grub-core/Makefile.core.def +4 -0
@@ -845,6 +845,16 @@ module = {
   common = commands/blocklist.c;
 };
 
+module = {
+  name = blsuki;
+  common = commands/blsuki.c;
+  common = lib/vercmp.c;

Probably this should be a part of the kernel.

grub-core/Makefile.core.def module = { · line 852

Quoted 1 line
grub-core/Makefile.core.def +1 -0
+  enable = powerpc_ieee1275;

??? Really? PowerPC? IEEE 1275? I think something is off here...

grub-core/Makefile.core.def module = { · line 854

Quoted 2 lines
grub-core/Makefile.core.def +2 -0
+  enable = efi;
+  enable = i386_pc;

PC?

grub-core/Makefile.core.def module = { · line 855

Quoted 1 line
grub-core/Makefile.core.def +1 -0
+  enable = emu;

Again, emu?

I think we should have EFI here only.

Quoted 260 lines
grub-core/Makefile.core.def +2 -0
+};
+
 module = {
   name = boot;
   common = commands/boot.c;
grub-core/commands/blsuki.c new file +249 -0
new file mode 100644
index 000000000..0fb4f870a
@@ -0,0 +1,1057 @@
+/*
+ *  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/>.
+ */
+
+#include <grub/list.h>
+#include <grub/types.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/extcmd.h>
+#include <grub/i18n.h>
+#include <grub/fs.h>
+#include <grub/env.h>
+#include <grub/file.h>
+#include <grub/normal.h>
+#include <grub/safemath.h>
+#include <grub/lib/envblk.h>
+#include <grub/lib/vercmp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#define GRUB_BLS_CONFIG_PATH "/loader/entries/"
+
+static const struct grub_arg_option bls_opt[] =
+  {
+    {"path", 'p', 0, "Specify path to find BLS entries.", N_("DIR"), ARG_TYPE_PATHNAME},
+    {"enable-fallback", 'f', 0, "Fallback to the default BLS path if --path fails to find BLS entries.", 0, ARG_TYPE_NONE},
+    {"show-default", 'd', 0, "Allow the default BLS entry to be added to the GRUB menu.", 0, ARG_TYPE_NONE},
+    {"show-non-default", 'n', 0, "Allow the non-default BLS entries to be added to the GRUB menu.", 0, ARG_TYPE_NONE},
+    {"entry", 'e', 0, "Allow specific BLS entries to be added to the GRUB menu.", N_("FILE"), ARG_TYPE_FILE},
+    {0, 0, 0, 0, 0, 0}
+  };
+
+struct keyval
+{
+  const char *key;
+  char *val;
+};
+
+static grub_blsuki_entry_t *entries = NULL;
+
+#define FOR_BLSUKI_ENTRIES(var) FOR_LIST_ELEMENTS (var, entries)
+
+static grub_err_t
+blsuki_add_keyval (grub_blsuki_entry_t *entry, char *key, char *val)
+{
+  char *k, *v;
+  struct keyval **kvs, *kv;
+  grub_size_t size;
+  int new_n = entry->nkeyvals + 1;
+
+  if (entry->keyvals_size == 0)
+    {
+      size = sizeof (struct keyval *);
+      kvs = grub_malloc (size);
+      if (kvs == NULL)
+	return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't allocate space for BLS key values");
+
+      entry->keyvals = kvs;
+      entry->keyvals_size = size;
+    }
+  else if (entry->keyvals_size < new_n * sizeof (struct keyval *))
+    {
+      size = entry->keyvals_size * 2;
+      kvs = grub_realloc (entry->keyvals, size);
+      if (kvs == NULL)
+	return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't reallocate space for BLS key values");
+
+      entry->keyvals = kvs;
+      entry->keyvals_size = size;
+    }
+
+  kv = grub_malloc (sizeof (struct keyval));
+  if (kv == NULL)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't find space for new BLS key value");
+
+  k = grub_strdup (key);
+  if (k == NULL)
+    {
+      grub_free (kv);
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't find space for new BLS key value");
+    }
+
+  v = grub_strdup (val);
+  if (v == NULL)
+    {
+      grub_free (k);
+      grub_free (kv);
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't find space for new BLS key value");
+    }
+
+  kv->key = k;
+  kv->val = v;
+
+  entry->keyvals[entry->nkeyvals] = kv;
+  entry->nkeyvals = new_n;
+
+  return GRUB_ERR_NONE;
+}
+
+/*
+ * Find the value of the key named by keyname.  If there are allowed to be
+ * more than one, pass a pointer to an int set to -1 the first time, and pass
+ * the same pointer through each time after, and it'll return them in sorted
+ * order as defined in the BLS fragment file.
+ */
+static char *
+blsuki_get_val (grub_blsuki_entry_t *entry, const char *keyname, int *last)
+{
+  int idx, start = 0;
+  struct keyval *kv = NULL;
+  char *ret = NULL;
+
+  if (last != NULL)
+    start = *last + 1;
+
+  for (idx = start; idx < entry->nkeyvals; idx++)
+    {
+      kv = entry->keyvals[idx];
+
+      if (grub_strcmp (keyname, kv->key) == 0)
+	{
+	  ret = kv->val;
+	  break;
+	}
+    }
+
+  if (last != NULL)
+    {
+      if (idx == entry->nkeyvals)
+	*last = -1;
+      else
+	*last = idx;
+    }
+
+  return ret;
+}
+
+static grub_err_t
+blsuki_add_entry (grub_blsuki_entry_t *entry)
+{
+  grub_blsuki_entry_t *e, *last = NULL;
+  grub_err_t err;
+  int rc;
+
+  if (entries == NULL)
+    {
+      grub_dprintf ("blsuki", "Add entry with id \"%s\"\n", entry->filename);
+      entries = entry;
+      return GRUB_ERR_NONE;
+    }
+
+  FOR_BLSUKI_ENTRIES (e)
+    {
+      err = grub_split_vercmp (entry->filename, e->filename, true, &rc);
+      if (err != GRUB_ERR_NONE)
+	return err;
+
+      if (rc == GRUB_VERCMP_SAME)
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "duplicate file");
+
+      if (rc == GRUB_VERCMP_NEWER)
+	{
+	  grub_dprintf ("blsuki", "Add entry with id \"%s\"\n", entry->filename);
+	  grub_list_push (GRUB_AS_LIST_P (&e), GRUB_AS_LIST (entry));
+	  if (entry->next == entries)
+	    {
+	      entries = entry;
+	      entry->prev = NULL;
+	    }
+	  else
+	    last->next = entry;
+	  return GRUB_ERR_NONE;
+	}
+      last = e;
+    }
+
+  if (last != NULL)
+    {
+      grub_dprintf ("blsuki", "Add entry with id \"%s\"\n", entry->filename);
+      last->next = entry;
+      entry->prev = &last;
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry)
+{
+  grub_err_t err = GRUB_ERR_NONE;
+
+  for (;;)
+    {
+      char *buf;
+      char *separator;
+
+      buf = grub_file_getline (f);
+      if (buf == NULL)
+	break;
+
+      while (buf != NULL && buf[0] != '\0' && (buf[0] == ' ' || buf[0] == '\t'))
+	buf++;
+      if (buf[0] == '#')
+	{
+	  grub_free (buf);
+	  continue;
+	}
+
+      separator = grub_strchr (buf, ' ');
+
+      if (separator == NULL)
+	separator = grub_strchr (buf, '\t');
+
+      if (separator == NULL || separator[1] == '\0')
+	{
+	  grub_free (buf);
+	  break;
+	}
+
+      separator[0] = '\0';
+
+      do
+	{
+	  separator++;
+	}
+      while (*separator == ' ' || *separator == '\t');
+
+      err = blsuki_add_keyval (entry, buf, separator);
+      grub_free (buf);
+      if (err != GRUB_ERR_NONE)
+	break;
+    }
+

grub-core/commands/blsuki.c line 259

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+  return err;
+}
+
+struct read_entry_info
+{
+  const char *devid;
+  const char *dirname;
+  grub_file_t file;
+};
+

What is a difference between bls_read_entry() and blsuki_read_entry()? I think every function, except really basic ones, should have shorter or longer descriptions what they do.

grub-core/commands/blsuki.c line 265

Quoted 6 lines
grub-core/commands/blsuki.c new file +6 -0
+static int
+blsuki_read_entry (const char *filename,
+		   const struct grub_dirhook_info *dirhook_info __attribute__ ((__unused__)),
+		   void *data)
+{
+  grub_size_t m = 0, n, ext_len = 5;

What is n? What is m?

Quoted 7 lines
grub-core/commands/blsuki.c new file +7 -0
+  grub_err_t err;
+  char *p = NULL;
+  grub_file_t f = NULL;
+  grub_blsuki_entry_t *entry;
+  struct read_entry_info *info = (struct read_entry_info *) data;
+
+  grub_dprintf ("blsuki", "filename: \"%s\"\n", filename);

grub-core/commands/blsuki.c line 282

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+
+  n = grub_strlen (filename);
+
+  if (info->file != NULL)
+    {
+      f = info->file;
+    }
+  else
+    {
+      if (filename[0] == '.')

It seems to me you are skipping all files starting with '.' in name. Is it intentional?

grub-core/commands/blsuki.c line 285

Quoted 3 lines
grub-core/commands/blsuki.c new file +3 -0
+	return 0;
+
+      if (n <= 5)

Why 5? I think it begs for comment.

grub-core/commands/blsuki.c line 288

Quoted 3 lines
grub-core/commands/blsuki.c new file +3 -0
+	return 0;
+
+      if (grub_strcmp (filename + n - ext_len, ".conf") != 0)

Are you sure it does not underflow? What will happen if the file name is shorter than ".conf"?

Quoted 23 lines
grub-core/commands/blsuki.c new file +23 -0
+	return 0;
+
+      p = grub_xasprintf ("(%s)%s/%s", info->devid, info->dirname, filename);
+
+      f = grub_file_open (p, GRUB_FILE_TYPE_CONFIG);
+      grub_free (p);
+      if (f == NULL)
+	goto finish;
+    }
+
+  entry = grub_zalloc (sizeof (*entry));
+  if (entry == NULL)
+    goto finish;
+
+  if (info->file != NULL)
+    {
+      char *slash;
+
+      slash = grub_strrchr (filename, '/');
+      if (slash == NULL)
+	slash = grub_strrchr (filename, '\\');
+
+      if (slash != NULL)

grub-core/commands/blsuki.c line 321

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+	{
+	  while (*slash == '/' || *slash == '\\')
+	    slash++;
+
+	  m = slash - filename;
+	}
+      else
+	m = 0;
+    }
+  n -= m;

When I see this I think the functions require more comments in the code...

Quoted 15 lines
grub-core/commands/blsuki.c new file +15 -0
+  entry->filename = grub_strndup (filename + m, n);
+  if (entry->filename == NULL)
+    {
+      grub_free (entry);
+      goto finish;
+    }
+
+  err = bls_read_entry (f, entry);
+
+  if (err == GRUB_ERR_NONE)
+    blsuki_add_entry (entry);
+  else
+    grub_free (entry);
+
+ finish:

grub-core/commands/blsuki.c line 347

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+  if (f != NULL)
+    grub_file_close (f);
+
+  return 0;
+}
+
+static char **
+blsuki_make_list (grub_blsuki_entry_t *entry, const char *key, int *num)
+{
+  int last = -1;

Should not you define -1 as a constant?

grub-core/commands/blsuki.c line 355

Quoted 8 lines
grub-core/commands/blsuki.c new file +8 -0
+  char *val;
+  int nlist = 0;
+  char **list;
+
+  list = grub_malloc (sizeof (char *));
+  if (list == NULL)
+    return NULL;
+  list[0] = NULL;

If you use grub_zalloc() you can drop this NULL assignment.

Quoted 5 lines
grub-core/commands/blsuki.c new file +5 -0
+  while (1)
+    {
+      char **new;
+
+      val = blsuki_get_val (entry, key, &last);

grub-core/commands/blsuki.c line 371

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+      if (val == NULL)
+	break;
+
+      new = grub_realloc (list, (nlist + 2) * sizeof (char *));
+      if (new == NULL)
+	break;
+
+      list = new;
+      list[nlist++] = val;
+      list[nlist] = NULL;

Ditto.

Quoted 45 lines
grub-core/commands/blsuki.c new file +45 -0
+  }
+
+  if (nlist == 0)
+    {
+      grub_free (list);
+      return NULL;
+    }
+
+  if (num != NULL)
+    *num = nlist;
+
+  return list;
+}
+
+static char *
+blsuki_field_append (bool is_env_var, char *buffer, const char *start, const char *end)
+{
+  char *tmp;
+  const char *field;
+  int term = (is_env_var == true) ? 2 : 1;
+  grub_size_t size = 0;
+
+  tmp = grub_strndup (start, end - start + 1);
+  if (tmp == NULL)
+    return NULL;
+
+  field = tmp;
+
+  if (is_env_var == true)
+    {
+      field = grub_env_get (tmp);
+      if (field == NULL)
+	return buffer;
+    }
+
+  if (grub_add (grub_strlen (field), term, &size))
+    return NULL;
+
+  if (buffer == NULL)
+    buffer = grub_zalloc (size);
+  else
+    {
+      if (grub_add (size, grub_strlen (buffer), &size))
+	return NULL;
+      buffer = grub_realloc (buffer, size);

grub-core/commands/blsuki.c line 426

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+    }
+
+  if (buffer == NULL)
+    return NULL;
+
+  tmp = buffer + grub_strlen (buffer);
+  tmp = grub_stpcpy (tmp, field);
+
+  if (is_env_var == true)
+      tmp = grub_stpcpy (tmp, " ");

Wrong indention.

Quoted 53 lines
grub-core/commands/blsuki.c new file +53 -0
+  return buffer;
+}
+
+static char *
+blsuki_expand_val (const char *value)
+{
+  char *buffer = NULL;
+  const char *start = value;
+  const char *end = value;
+  bool is_env_var = false;
+
+  if (value == NULL)
+    return NULL;
+
+  while (*value != '\0')
+    {
+      if (*value == '$')
+	{
+	  if (start != end)
+	    {
+	      buffer = blsuki_field_append (is_env_var, buffer, start, end);
+	      if (buffer == NULL)
+		return NULL;
+	    }
+
+	  is_env_var = true;
+	  start = value + 1;
+	}
+      else if (is_env_var == true)
+	{
+	  if (grub_isalnum (*value) == 0 && *value != '_')
+	    {
+	      buffer = blsuki_field_append (is_env_var, buffer, start, end);
+	      is_env_var = false;
+	      start = value;
+	      if (*start == ' ')
+		start++;
+	    }
+	}
+
+      end = value;
+      value++;
+    }
+
+  if (start != end)
+    {
+      buffer = blsuki_field_append (is_env_var, buffer, start, end);
+      if (buffer == NULL)
+	return NULL;
+    }
+
+  return buffer;
+}

grub-core/commands/blsuki.c line 490

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+
+static char **
+blsuki_early_initrd_list (const char *initrd)
+{
+  int nlist = 0;
+  char **list = NULL;
+  char *separator;
+  char *tmp;
+
+  while ((separator = grub_strchr (initrd, ' ')))

What about TABs as separators? Are they allowed? I think they are...

Quoted 51 lines
grub-core/commands/blsuki.c new file +51 -0
+    {
+      list = grub_realloc (list, (nlist + 2) * sizeof (char *));
+      if (list == NULL)
+	return NULL;
+
+      tmp = grub_strndup (initrd, separator - initrd);
+      if (tmp == NULL)
+	{
+	  grub_free (list);
+	  return NULL;
+	}
+      list[nlist++] = tmp;
+      list[nlist] = NULL;
+      initrd = separator + 1;
+    }
+
+  list = grub_realloc (list, (nlist + 2) * sizeof (char *));
+  if (list == NULL)
+    return NULL;
+
+  tmp = grub_strndup (initrd, grub_strlen (initrd));
+  if (tmp == NULL)
+    {
+      grub_free (list);
+      return NULL;
+    }
+  list[nlist++] = tmp;
+  list[nlist] = NULL;
+
+  return list;
+}
+
+static void
+bls_create_entry (grub_blsuki_entry_t *entry)
+{
+  int argc = 0;
+  const char **argv = NULL;
+  char *title = NULL;
+  char *clinux = NULL;
+  char *linux_path = NULL;
+  grub_size_t linux_size;
+  char *options = NULL;
+  char **initrds = NULL;
+  char *initrd = NULL;
+  grub_size_t initrd_size;
+  const char *early_initrd = NULL;
+  char **early_initrds = NULL;
+  char *initrd_prefix = NULL;
+  char *prefix = NULL;
+  char *devicetree = NULL;
+  char *dt = NULL;

grub-core/commands/blsuki.c line 551

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+  grub_size_t dt_size;
+  char *id = entry->filename;
+  char *dotconf = id;
+  char *hotkey = NULL;
+  char *users = NULL;
+  char **classes = NULL;
+  char **args = NULL;
+  char *src = NULL;
+  char *tmp;
+  const char *sdval = NULL;

Do we need all these variables?

Quoted 23 lines
grub-core/commands/blsuki.c new file +23 -0
+  int i;
+  grub_size_t size = 0;
+  bool add_dt_prefix = false;
+  bool savedefault;
+
+  linux_path = blsuki_get_val (entry, "linux", NULL);
+  if (linux_path == NULL)
+    {
+      grub_dprintf ("blsuki", "Skipping file %s with no 'linux' key.\n", entry->filename);
+      goto finish;
+    }
+
+  if (grub_add (sizeof ("linux "), grub_strlen (linux_path), &linux_size))
+    {
+      grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected calculating linux buffer size");
+      goto finish;
+    }
+  clinux = grub_malloc (linux_size);
+  if (clinux == NULL)
+    goto finish;
+  tmp = clinux;
+  tmp = grub_stpcpy (tmp, "linux");
+  tmp = grub_stpcpy (tmp, " ");

grub-core/commands/blsuki.c line 584

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+  tmp = grub_stpcpy (tmp, linux_path);
+
+  /* Strip the ".conf" off the end before we make it our "id" field. */
+  do
+    {
+      dotconf = grub_strstr (dotconf, ".conf");
+    }
+  while (dotconf != NULL && dotconf[5] != '\0');
+  if (dotconf != NULL)
+    dotconf[0] = '\0';

This code looks strange. Why do not you compare 5 last characters of the string with ".conf" and cut it when it matches?

Quoted 41 lines
grub-core/commands/blsuki.c new file +41 -0
+  title = blsuki_get_val (entry, "title", NULL);
+  options = blsuki_expand_val (blsuki_get_val (entry, "options", NULL));
+
+  if (options == NULL)
+    options = blsuki_expand_val (grub_env_get ("default_kernelopts"));
+
+  initrds = blsuki_make_list (entry, "initrd", NULL);
+
+  devicetree = blsuki_expand_val (blsuki_get_val (entry, "devicetree", NULL));
+
+  if (devicetree == NULL)
+    {
+      devicetree = blsuki_expand_val (grub_env_get ("devicetree"));
+      add_dt_prefix = true;
+    }
+
+  hotkey = blsuki_get_val (entry, "grub_hotkey", NULL);
+  users = blsuki_expand_val (blsuki_get_val (entry, "grub_users", NULL));
+  classes = blsuki_make_list (entry, "grub_class", NULL);
+  args = blsuki_make_list (entry, "grub_arg", &argc);
+
+  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)
+    goto finish;
+  argv[0] = title ? title : linux_path;
+  for (i = 1; i < argc; i++)
+    argv[i] = args[i-1];
+  argv[argc] = NULL;
+
+  early_initrd = grub_env_get ("early_initrd");
+
+  grub_dprintf ("blsuki", "adding menu entry for \"%s\" with id \"%s\"\n",
+		title, id);
+  if (early_initrd != NULL)
+    {

grub-core/commands/blsuki.c line 636

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+      early_initrds = blsuki_early_initrd_list (early_initrd);
+      if (early_initrds == NULL)
+	{
+	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("failed to create early initrd list"));
+	  goto finish;
+	}
+
+      if (initrds != NULL && initrds[0] != NULL)
+	{
+	  initrd_prefix = grub_strrchr (initrds[0], '/');

grub_strrchr() may return NULL...

grub-core/commands/blsuki.c line 641

Quoted 5 lines
grub-core/commands/blsuki.c new file +5 -0
+	  initrd_prefix = grub_strndup (initrds[0], initrd_prefix - initrds[0] + 1);
+	}
+      else
+	{
+	  initrd_prefix = grub_strrchr (linux_path, '/');

Ditto...

Quoted 19 lines
grub-core/commands/blsuki.c new file +19 -0
+	  initrd_prefix = grub_strndup (linux_path, initrd_prefix - linux_path + 1);
+	}
+
+      if (initrd_prefix == NULL)
+	{
+	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("failed to allocate initrd prefix buffer"));
+	  goto finish;
+	}
+    }
+
+  if (early_initrds != NULL || initrds != NULL)
+    {
+      initrd_size = sizeof ("initrd");
+
+      for (i = 0; early_initrds != NULL && early_initrds[i] != NULL; i++)
+	{
+	  if (grub_add (initrd_size, sizeof (" "), &initrd_size) ||
+	      grub_add (initrd_size, grub_strlen (initrd_prefix), &initrd_size) ||
+	      grub_add (initrd_size, grub_strlen (early_initrds[i]), &initrd_size) ||

grub-core/commands/blsuki.c line 670

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+	      grub_add (initrd_size, 1, &initrd_size))
+	    {
+	      grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected calculating initrd buffer size");
+	      goto finish;
+	    }
+	}
+
+      for (i = 0; initrds != NULL && initrds[i] != NULL; i++)
+	{
+	  if (grub_add (initrd_size, sizeof (" "), &initrd_size) ||

You know you add 2 here...

Quoted 81 lines
grub-core/commands/blsuki.c new file +81 -0
+	      grub_add (initrd_size, grub_strlen (initrds[i]), &initrd_size) ||
+	      grub_add (initrd_size, 1, &initrd_size))
+	    {
+	      grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected calculating initrd buffer size");
+	      goto finish;
+	    }
+	}
+
+      if (grub_add (initrd_size, 1, &initrd_size))
+	{
+	  grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected calculating initrd buffer size");
+	  goto finish;
+	}
+
+      initrd = grub_malloc (initrd_size);
+      if (initrd == NULL)
+	goto finish;
+
+      tmp = grub_stpcpy (initrd, "initrd");
+      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 = grub_stpcpy (tmp, initrd_prefix);
+	  tmp = grub_stpcpy (tmp, early_initrds[i]);
+	  grub_free (early_initrds[i]);
+	}
+
+      for (i = 0; initrds != NULL && initrds[i] != NULL; i++)
+	{
+	  grub_dprintf ("blsuki", "adding initrd %s\n", initrds[i]);
+	  tmp = grub_stpcpy (tmp, " ");
+	  tmp = grub_stpcpy (tmp, initrds[i]);
+	}
+      tmp = grub_stpcpy (tmp, "\n");
+    }
+
+  if (devicetree != NULL)
+    {
+      if (add_dt_prefix == true)
+	{
+	  prefix = grub_strrchr (linux_path, '/');
+	  prefix = grub_strndup (linux_path, prefix - linux_path + 1);
+	  if (prefix == NULL)
+	    {
+	      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("failed to allocate prefix buffer"));
+	      goto finish;
+	    }
+	}
+
+      if (grub_add (sizeof ("devicetree "), 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");
+	  goto finish;
+	}
+
+      if (add_dt_prefix == true)
+	{
+	  if (grub_add (dt_size, grub_strlen (prefix), &dt_size))
+	    {
+	      grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected calculating device tree buffer size");
+	      goto finish;
+	    }
+	}
+
+      dt = grub_malloc (dt_size);
+      if (dt == NULL)
+	goto finish;
+      tmp = dt;
+      tmp = grub_stpcpy (dt, "devicetree");
+      tmp = grub_stpcpy (tmp, " ");
+      if (add_dt_prefix == true)
+	tmp = grub_stpcpy (tmp, prefix);
+      tmp = grub_stpcpy (tmp, devicetree);
+      tmp = grub_stpcpy (tmp, "\n");
+
+      grub_free (prefix);
+    }
+
+  grub_dprintf ("blsuki", "devicetree %s for id:\"%s\"\n", dt, id);

grub-core/commands/blsuki.c line 761

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+
+  sdval = grub_env_get ("save_default");
+  savedefault = ((NULL != sdval) && (grub_strcmp (sdval, "true") == 0));
+  src = grub_xasprintf ("%s%s%s%s\n"
+			"%s%s",
+			savedefault ? "savedefault\n" : "",
+			clinux, options ? " " : "", options ? options : "",
+			initrd ? initrd : "", dt ? dt : "");
+
+  grub_normal_add_menu_entry (argc, argv, classes, id, users, hotkey, NULL, src, 0, entry);

I think I would split this function to smaller parts for readability...

Quoted 29 lines
grub-core/commands/blsuki.c new file +29 -0
+ finish:
+  grub_free (clinux);
+  grub_free (dt);
+  grub_free (initrd);
+  grub_free (initrd_prefix);
+  grub_free (early_initrds);
+  grub_free (devicetree);
+  grub_free (initrds);
+  grub_free (options);
+  grub_free (classes);
+  grub_free (args);
+  grub_free (argv);
+  grub_free (src);
+}
+
+struct find_entry_info
+{
+  const char *dirname;
+  const char *devid;
+  grub_device_t dev;
+  grub_fs_t fs;
+};
+
+static grub_err_t
+blsuki_set_find_entry_info (struct find_entry_info *info, const char *dirname, const char *devid)
+{
+  grub_device_t dev;
+  grub_fs_t fs;
+

grub-core/commands/blsuki.c line 801

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+  if (info == NULL)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "info parameter is not set");
+
+  if (devid == NULL)
+    {
+#ifdef GRUB_MACHINE_EMU
+      devid = "host";
+#else
+      devid = grub_env_get ("root");
+#endif

Is the EMU implementation fully functional? If yes I would move it to separate patch to not confuse a reader. If not I would drop it...

Quoted 34 lines
grub-core/commands/blsuki.c new file +34 -0
+      if (devid == NULL)
+	return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "root");
+    }
+
+  /* Check that we aren't closing and opening the same device. */
+  if (info->dev != NULL && grub_strcmp (info->devid, devid) != 0)
+    {
+      grub_device_close (info->dev);
+      info->dev = NULL;
+    }
+  /* If we are using the same device, then we can skip this step and only set the directory. */
+  if (info->dev == NULL)
+    {
+      grub_dprintf ("blsuki", "opening %s\n", devid);
+      dev = grub_device_open (devid);
+      if (dev == NULL)
+	return grub_errno;
+
+      grub_dprintf ("blsuki", "probing fs\n");
+      fs = grub_fs_probe (dev);
+      if (fs == NULL)
+	{
+	  grub_device_close (dev);
+	  return grub_errno;
+	}
+
+      info->devid = devid;
+      info->dev = dev;
+      info->fs = fs;
+    }
+
+  info->dirname = dirname;
+
+  return GRUB_ERR_NONE;

grub-core/commands/blsuki.c line 845

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+}
+
+/* info: the filesystem object the file is on. */
+static void
+blsuki_find_entry (struct find_entry_info *info, bool enable_fallback)
+{
+  struct read_entry_info read_entry_info;
+  grub_fs_t dir_fs = NULL;
+  grub_device_t dir_dev = NULL;
+  int fallback = 0;

s/int/bool/

grub-core/commands/blsuki.c line 846

Quoted 1 line
grub-core/commands/blsuki.c new file +1 -0
+  int r = 0;

You can drop this assignment.

grub-core/commands/blsuki.c line 848

Quoted 1 line
grub-core/commands/blsuki.c new file +1 -0
+ read_fallback:

No, please use for/while for the loop here...

Quoted 42 lines
grub-core/commands/blsuki.c new file +42 -0
+  read_entry_info.file = NULL;
+  read_entry_info.dirname = info->dirname;
+
+  grub_dprintf ("blsuki", "scanning dir: %s\n", info->dirname);
+  dir_dev = info->dev;
+  dir_fs = info->fs;
+  read_entry_info.devid = info->devid;
+
+  r = dir_fs->fs_dir (dir_dev, read_entry_info.dirname, blsuki_read_entry,
+		      &read_entry_info);
+  if (r != 0)
+    {
+      grub_dprintf ("blsuki", "blsuki_read_entry returned error\n");
+      grub_errno = GRUB_ERR_NONE;
+    }
+
+  /*
+   * 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.
+   */
+  if (entries == NULL && fallback == 0 && enable_fallback == true)
+    {
+      blsuki_set_find_entry_info (info, GRUB_BLS_CONFIG_PATH, NULL);
+      grub_dprintf ("blsuki", "Entries weren't found in %s, fallback to %s\n",
+		    read_entry_info.dirname, info->dirname);
+      fallback = 1;
+      goto read_fallback;
+    }
+}
+
+static grub_err_t
+blsuki_load_entries (char *path, bool enable_fallback)
+{
+  grub_size_t len;
+  static grub_err_t r;
+  const char *devid = NULL;
+  char *dir = NULL;
+  struct find_entry_info info = {
+      .dev = NULL,
+      .fs = NULL,
+      .dirname = NULL,

grub-core/commands/blsuki.c line 900

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+  };
+  struct read_entry_info rei = {
+      .devid = NULL,
+      .dirname = NULL,
+  };
+
+  if (path != NULL)
+    {
+      len = grub_strlen (path);
+      if (grub_strcmp (path + len - 5, ".conf") == 0)

What if len < 5?

Quoted 48 lines
grub-core/commands/blsuki.c new file +48 -0
+	{
+	  rei.file = grub_file_open (path, GRUB_FILE_TYPE_CONFIG);
+	  if (rei.file == NULL)
+	    return grub_errno;
+
+	  /* blsuki_read_entry() closes the file. */
+	  return blsuki_read_entry (path, NULL, &rei);
+	}
+      else if (path[0] == '(')
+	{
+	  devid = path + 1;
+
+	  dir = grub_strchr (path, ')');
+	  if (dir == NULL)
+	    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid file name `%s'"), path);
+
+	  *dir = '\0';
+
+	  /* Check if there is more than the devid in the path. */
+	  if (dir + 1 < path + len)
+	    dir = dir + 1;
+	}
+      else if (path[0] == '/')
+	dir = path;
+    }
+
+  if (dir == NULL)
+    dir = (char *) GRUB_BLS_CONFIG_PATH;
+
+  r = blsuki_set_find_entry_info (&info, dir, devid);
+  if (r == GRUB_ERR_NONE)
+    blsuki_find_entry (&info, enable_fallback);
+
+  if (info.dev != NULL)
+    grub_device_close (info.dev);
+
+  return r;
+}
+
+static bool
+blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int idx)
+{
+  const char *title;
+  int def_idx;
+
+  if (def_entry == NULL)
+    return false;
+

grub-core/commands/blsuki.c line 958

Quoted 10 lines
grub-core/commands/blsuki.c new file +10 -0
+  if (grub_strcmp (def_entry, entry->filename) == 0)
+    return true;
+
+  title = blsuki_get_val (entry, "title", NULL);
+
+  if (title != NULL && grub_strcmp (def_entry, title) == 0)
+    return true;
+
+  def_idx = (int) grub_strtol (def_entry, NULL, 0);
+  if (grub_errno != GRUB_ERR_NONE)

This check is unreliable. Please take a look at commit ac8a37dda (net/http:
Allow use of non-standard TCP/IP ports) how it should be done properly.

grub-core/lib/vercmp.c

Quoted 166 lines
grub-core/commands/blsuki.c new file +99 -0
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return false;
+    }
+
+  if (def_idx == idx)
+    return true;
+
+  return false;
+}
+
+static grub_err_t
+blsuki_create_entries (bool show_default, bool show_non_default, char *entry_id)
+{
+  const char *def_entry = NULL;
+  grub_blsuki_entry_t *entry = NULL;
+  int idx = 0;
+
+  def_entry = grub_env_get ("default");
+
+  FOR_BLSUKI_ENTRIES(entry)
+    {
+      if (entry->visible == 1)
+	{
+	  idx++;
+	  continue;
+	}
+      if ((show_default == true && blsuki_is_default_entry (def_entry, entry, idx) == true) ||
+	  (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);
+	  entry->visible = 1;
+	}
+      idx++;
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_blscfg (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)),
+		 char **args __attribute__ ((unused)))
+{
+  grub_err_t err;
+  struct grub_arg_list *state = ctxt->state;
+  char *path = NULL;
+  char *entry_id = NULL;
+  bool enable_fallback = false;
+  bool show_default = false;
+  bool show_non_default = false;
+  bool all = true;
+
+  if (state[0].set)
+    path = state[0].arg;
+  if (state[1].set)
+    enable_fallback = true;
+  if (state[2].set)
+    {
+      show_default = true;
+      all = false;
+    }
+  if (state[3].set)
+    {
+      show_non_default = true;
+      all = false;
+    }
+  if (state[4].set)
+    {
+      entry_id = state[4].arg;
+      all = false;
+    }
+  if (all == true)
+    {
+      show_default = true;
+      show_non_default = true;
+    }
+
+  err = blsuki_load_entries (path, enable_fallback);
+  if (err != GRUB_ERR_NONE)
+    return err;
+
+  return blsuki_create_entries (show_default, show_non_default, entry_id);
+}
+
+static grub_extcmd_t bls_cmd;
+
+GRUB_MOD_INIT(blsuki)
+{
+  bls_cmd = grub_register_extcmd ("blscfg", grub_cmd_blscfg, 0,
+				  N_("[-p|--path] [-f|--enable-fallback] DIR [-d|--show-default] [-n|--show-non-default] [-e|--entry] FILE"),
+				  N_("Import Boot Loader Specification snippets."),
+				  bls_opt);
+}
+
+GRUB_MOD_FINI(blsuki)
+{
+  grub_unregister_extcmd (bls_cmd);
+}
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 +0 -0
new file mode 100644
index 000000000..726e2321b

I would move this code to separate patch. Even if it will be unused initially.

Quoted 23 lines
grub-core/lib/vercmp.c new file +22 -0
@@ -0,0 +1,317 @@
+/*
+ *  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/>.
+ */
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/err.h>
+#include <grub/lib/vercmp.h>
+

grub-core/lib/vercmp.c line 32

Quoted 10 lines
grub-core/lib/vercmp.c new file +10 -0
+#define GOTO_RETURN(x) ({ *ret = (x); goto finish; })
+
+/*
+ * compare alpha and numeric segments of two versions
+ * return 1: a is newer than b
+ *        0: a and b are the same version
+ *       -1: a is older than b
+ */
+grub_err_t
+grub_vercmp (const char *a, const char *b, int *ret)

Where is this algorithm come from? I suppose you copied it from somewhere. If yes please mention source here. If not I think you should copy the code from somewhere to not invent the wheel.

grub-core/lib/vercmp.c line 45

Quoted 13 lines
grub-core/lib/vercmp.c new file +13 -0
+{
+  char oldch1, oldch2;
+  char *abuf, *bbuf;
+  char *str1, *str2;
+  char *one, *two;
+  int rc;
+  bool isnum;
+
+  if (ret == NULL)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("return parameter is not set"));
+  *ret = 0;
+
+  grub_dprintf ("blscfg", "%s comparing %s and %s\n", __func__, a, b);

This is generic thing not BLS one... And it seems to me this message is not very helpful...

Daniel

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

#3 lore raw

Re: [PATCH v3 1/3] blsuki: Add blscfg command to parse Boot Loader Specification snippets

Quoted 11 lines

grub-core/Makefile.core.def module = { · line 851

Quoted 4 lines
grub-core/Makefile.core.def +4 -0
+module = {
+  name = blsuki;
+  common = commands/blsuki.c;
+  common = lib/vercmp.c;

Probably this should be a part of the kernel.

Quoted 1 line

+ enable = powerpc_ieee1275;

??? Really? PowerPC? IEEE 1275? I think something is off here...

BLS is specified for ieee1275 explicitly. And the spec actually works for all platforms. We should website or on all the platforms. As for UKI, it's compatible with EFI through chainloader and potentially compatible with others through image parsing.

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

#4 lore raw

Re: [PATCH v3 1/3] blsuki: Add blscfg command to parse Boot Loader Specification snippets

Quoted 11 lines · Vladimir 'phcoder' Serbinenko
     > +module = {
     > +  name = blsuki;
     > +  common = commands/blsuki.c;
     > +  common = lib/vercmp.c;

     Probably this should be a part of the kernel.

     > +  enable = powerpc_ieee1275;

     ??? Really? PowerPC? IEEE 1275? I think something is off here...
Quoted 4 lines

BLS is specified for ieee1275 explicitly. And the spec actually works for all platforms. We should website or on all the platforms. As for UKI, it's compatible with EFI through chainloader and potentially compatible with others through image parsing.

Ugh... Sorry, I messed it up with UKI. Right, BLS is more generic.

If we are talking about UKI, I think we should focus on making it work on UEFI first. When it is merged we can consider making code more generic.

Daniel

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

#5 lore raw

PATCH v3 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     | 92 ++++++++++++++++++++++++++++++---
 grub-core/osdep/linux/getroot.c |  8 +++
 grub-core/osdep/unix/getroot.c  | 10 ++++
 include/grub/emu/misc.h         |  2 +-
 5 files changed, 107 insertions(+), 9 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 +84 -8

191 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
#6 lore raw

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

Quoted 57 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     | 92 ++++++++++++++++++++++++++++++---
 grub-core/osdep/linux/getroot.c |  8 +++
 grub-core/osdep/unix/getroot.c  | 10 ++++
 include/grub/emu/misc.h         |  2 +-
 5 files changed, 107 insertions(+), 9 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 +12 -0
index 0fb4f870a..7b7b3e0e4 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/"
@@ -56,6 +63,40 @@ static grub_blsuki_entry_t *entries = NULL;
 
 #define FOR_BLSUKI_ENTRIES(var) FOR_LIST_ELEMENTS (var, entries)
 
+#ifdef GRUB_MACHINE_EMU
+/*
+ * Cache probing in blsuki_update_boot_device().
+ */
+static int separate_boot = -1;

grub-core/commands/blsuki.c static grub_blsuki_entry_t *entries = NULL; · line 80

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+#endif
+
+/*
+ * 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 = NULL;

You do not need this initialization...

Quoted 105 lines
grub-core/commands/blsuki.c +30 -5
+  if (separate_boot != -1)
+    goto probed;
+
+  separate_boot = 0;
+
+  ret = grub_make_system_path_relative_to_its_root (GRUB_BOOT_DEVICE);
+
+  if (ret != NULL && ret[0] == '\0')
+    separate_boot = 1;
+
+ probed:
+  if (!separate_boot)
+    return 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)
 {
@@ -561,7 +602,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
       goto finish;
     }
 
-  if (grub_add (sizeof ("linux "), grub_strlen (linux_path), &linux_size))
+  if (grub_add (sizeof ("linux " GRUB_BOOT_DEVICE), grub_strlen (linux_path), &linux_size))
     {
       grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected calculating linux buffer size");
       goto finish;
@@ -572,6 +613,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
   tmp = clinux;
   tmp = grub_stpcpy (tmp, "linux");
   tmp = grub_stpcpy (tmp, " ");
+  tmp = blsuki_update_boot_device (tmp);
   tmp = grub_stpcpy (tmp, linux_path);
 
   /* Strip the ".conf" off the end before we make it our "id" field. */
@@ -655,7 +697,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))
@@ -667,7 +709,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))
 	    {
@@ -691,6 +733,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
 	{
 	  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]);
@@ -700,6 +743,7 @@ bls_create_entry (grub_blsuki_entry_t *entry)
 	{
 	  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");
@@ -718,7 +762,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");
@@ -740,6 +784,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);
@@ -836,10 +881,13 @@ blsuki_set_find_entry_info (struct find_entry_info *info, const char *dirname, c
 }
 
 /* info: the filesystem object the file is on. */
-static void
+static grub_err_t
 blsuki_find_entry (struct find_entry_info *info, bool enable_fallback)
 {
   struct read_entry_info read_entry_info;
+  char *default_dir = NULL;
+  char *tmp;
+  grub_size_t default_size;
   grub_fs_t dir_fs = NULL;
   grub_device_t dir_dev = NULL;
   int fallback = 0;

grub-core/commands/blsuki.c blsuki_find_entry (struct find_entry_info *info, bool enable_fallback) · line 920

Quoted 5 lines
grub-core/commands/blsuki.c +1 -1
@@ -869,12 +917,25 @@ blsuki_find_entry (struct find_entry_info *info, bool enable_fallback)
    */
   if (entries == NULL && fallback == 0 && enable_fallback == true)
     {
-      blsuki_set_find_entry_info (info, GRUB_BLS_CONFIG_PATH, NULL);
+      if (grub_add (grub_strlen (GRUB_BOOT_DEVICE), grub_strlen (GRUB_BLS_CONFIG_PATH), &default_size))

You do not need overflow checks here...

Quoted 32 lines
grub-core/commands/blsuki.c +16 -0
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected calculating default directory buffer size");
+
+      default_dir = grub_malloc (default_size);
+      if (default_dir == NULL)
+	return grub_errno;
+
+      tmp = blsuki_update_boot_device (default_dir);
+      tmp = grub_stpcpy (tmp, GRUB_BLS_CONFIG_PATH);
+
+      blsuki_set_find_entry_info (info, default_dir, NULL);
       grub_dprintf ("blsuki", "Entries weren't found in %s, fallback to %s\n",
 		    read_entry_info.dirname, info->dirname);
       fallback = 1;
       goto read_fallback;
     }
+
+  grub_free (default_dir);
+  return GRUB_ERR_NONE;
 }
 
 static grub_err_t
@@ -884,6 +945,9 @@ blsuki_load_entries (char *path, bool enable_fallback)
   static grub_err_t r;
   const char *devid = NULL;
   char *dir = NULL;
+  char *default_dir = NULL;
+  char *tmp;
+  grub_size_t dir_size;
   struct find_entry_info info = {
       .dev = NULL,
       .fs = NULL,

grub-core/commands/blsuki.c blsuki_load_entries (char *path, bool enable_fallback) · line 993

Quoted 6 lines
grub-core/commands/blsuki.c +2 -1
@@ -925,15 +989,27 @@ blsuki_load_entries (char *path, bool enable_fallback)
     }
 
   if (dir == NULL)
-    dir = (char *) GRUB_BLS_CONFIG_PATH;
+    {
+      if (grub_add (grub_strlen (GRUB_BOOT_DEVICE), grub_strlen (GRUB_BLS_CONFIG_PATH), &dir_size))

Ditto...

Daniel

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

#7 lore raw

PATCH v3 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 | 438 +++++++++++++++++++++++++++++++++---
 include/grub/menu.h         |   2 +
 3 files changed, 440 insertions(+), 26 deletions(-)
docs/grub.texi +26 -0
index ea7a13953..df206dad3 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
@@ -8166,6 +8167,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 +412 -26

635 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
#8 lore raw

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

Quoted 3 lines · Alec Brown

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.

Link to the spec please...

Quoted 20 lines
---
 docs/grub.texi              |  26 +++
 grub-core/commands/blsuki.c | 438 +++++++++++++++++++++++++++++++++---
 include/grub/menu.h         |   2 +
 3 files changed, 440 insertions(+), 26 deletions(-)
docs/grub.texi +1 -0
index ea7a13953..df206dad3 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

docs/grub.texi Unset the environment variable @var{envvar}. · line 8173

Quoted 7 lines
docs/grub.texi +4 -0
@@ -8166,6 +8167,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]

Missing short options.

Quoted 11 lines
docs/grub.texi +11 -0
+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.

docs/grub.texi Unset the environment variable @var{envvar}. · line 8194

Quoted 10 lines
docs/grub.texi +10 -0
+
+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
+

I would add a link to the spec here too. And probably it would be good to show some example entries generated by GRUB.

Quoted 21 lines
docs/grub.texi +0 -0
 @ignore
 @node vbeinfo
 @subsection vbeinfo
grub-core/commands/blsuki.c +6 -0
index 7b7b3e0e4..77f59b530 100644
@@ -32,6 +32,12 @@
 #include <grub/lib/envblk.h>
 #include <grub/lib/vercmp.h>
 
+#ifdef GRUB_MACHINE_EFI
+#include <grub/efi/efi.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/pe32.h>
+#endif
+
 #ifdef GRUB_MACHINE_EMU
 #include <grub/emu/misc.h>
 #define GRUB_BOOT_DEVICE "/boot"

grub-core/commands/blsuki.c line 56

Quoted 9 lines
grub-core/commands/blsuki.c +6 -0
@@ -42,6 +48,13 @@
 GRUB_MOD_LICENSE ("GPLv3+");
 
 #define GRUB_BLS_CONFIG_PATH "/loader/entries/"
+#define GRUB_UKI_CONFIG_PATH "/EFI/Linux"
+
+enum
+  {
+    BLSUKI_BLS_CMD	= 1,
+    BLSUKI_UKI_CMD	= 2,

I think you can drop assignments here.

Quoted 115 lines
grub-core/commands/blsuki.c +101 -0
+  };
 
 static const struct grub_arg_option bls_opt[] =
   {
@@ -53,6 +66,18 @@ 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},
+    {"enable-fallback", 'f', 0, "Fallback to the default BLS path if --path fails to find UKI entries.", 0, ARG_TYPE_NONE},
+    {"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;
@@ -291,10 +316,211 @@ 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_BAD_FILE_TYPE, 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_BAD_FILE_TYPE, "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;

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

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+		}
+
+	      val = grub_zalloc (section->raw_data_size);
+	      if (val == NULL)
+		{
+		  err = grub_errno;
+		  goto fail;
+		}
+
+	      grub_file_seek (f, section->raw_data_offset);

You blindly assume grub_file_seek() not fail. This is not true...

Quoted 16 lines
grub-core/commands/blsuki.c +16 -0
+	      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-core/commands/blsuki.c bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) · line 442

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+      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;

Probably you can drop these three lines above.

Quoted 8 lines
grub-core/commands/blsuki.c +8 -0
+ fail:
+  grub_free (dos);
+  grub_free (pe);
+  grub_free (section);
+  grub_free (val);
+  grub_free (key);
+  return err;
+}

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

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+#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:

May I ask you to use for/while here?

Quoted 20 lines
grub-core/commands/blsuki.c +20 -0
+  line = content + *pos;
+  if (*line == '\0')
+    return NULL;
+
+  linelen = 0;
+  while (line[linelen] != '\0' && line[linelen] != '\n' && line[linelen] != '\r')
+    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 */

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

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+  while (linelen > 0 && (*line == ' ' || *line == '\t'))
+    {
+      line++;
+      linelen--;
+    }
+
+  /* Remove trailing whitespace */
+  while (linelen > 0 && (line[linelen - 1] == ' ' || line[linelen - 1] == '\t'))
+    linelen--;
+  line[linelen] = '\0';

Did you try how this code works with empty lines or whole TABs and spaces?

Quoted 67 lines
grub-core/commands/blsuki.c +42 -1
+  if (*line == '#')
+    goto skip;
+
+  /* Split key/value */
+  value = line;
+  while (*value != '\0' && *value != '=')
+    value++;
+  if (*value == '\0')
+    goto skip;
+  *value = '\0';
+  value++;
+  while (*value != '\0' && *value == '=')
+    value++;
+
+  /* Remove quotes from value */
+  if (*value == '\"' && line[linelen - 1] == '\"')
+    {
+      value++;
+      line[linelen - 1] = '\0';
+    }
+
+  *key_ret = line;
+  *val_ret = value;
+  return line;
+}
+
 struct read_entry_info
 {
   const char *devid;
   const char *dirname;
+  int cmd_type;
   grub_file_t file;
 };
 
@@ -304,9 +530,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;
 
@@ -314,6 +542,18 @@ blsuki_read_entry (const char *filename,
 
   n = grub_strlen (filename);
 
+  if (info->cmd_type == BLSUKI_BLS_CMD)
+    {
+      ext = ".conf";
+      file_type = GRUB_FILE_TYPE_CONFIG;
+    }
+  else if (info->cmd_type == BLSUKI_UKI_CMD)
+    {
+      ext = ".efi";
+      file_type = GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE;
+    }
+  ext_len = grub_strlen (ext);
+
   if (info->file != NULL)
     {
       f = info->file;

grub-core/commands/blsuki.c blsuki_read_entry (const char *filename, · line 569

Quoted 9 lines
grub-core/commands/blsuki.c +2 -2
@@ -323,15 +563,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)

What about file names shorter than ".conf"?

Quoted 51 lines
grub-core/commands/blsuki.c +31 -3
 	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;
@@ -368,7 +607,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 (info->cmd_type == BLSUKI_BLS_CMD)
+    err = bls_read_entry (f, entry);
+#ifdef GRUB_MACHINE_EFI
+  else if (info->cmd_type == BLSUKI_UKI_CMD)
+    err = uki_read_entry (f, entry);
+#endif
 
   if (err == GRUB_ERR_NONE)
     blsuki_add_entry (entry);
@@ -820,6 +1078,71 @@ 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;

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

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+  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);

Is .cmdline required by UKI spec?

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

Quoted 6 lines
grub-core/commands/blsuki.c +6 -0
+      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);

Same with .osrel?

Quoted 4 lines
grub-core/commands/blsuki.c +4 -0
+      goto finish;
+    }
+
+  line = osrel;

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

Quoted 10 lines
grub-core/commands/blsuki.c +10 -0
+  while ((line = uki_read_osrel (osrel, &pos, &key, &value)))
+    {
+      if (grub_strcmp ("PRETTY_NAME", key) == 0)
+	{
+	  title = value;
+	  break;
+	}
+    }
+
+  argc += 1;

++argc;

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

Quoted 1 line
grub-core/commands/blsuki.c +1 -0
+  if (grub_mul (argc + 1, sizeof (char *), &size))

Is it correct to add 1 twice? If yes I think this piece of code begs for comment.

Quoted 82 lines
grub-core/commands/blsuki.c +41 -4
+    {
+      grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected creating argv list");
+      goto finish;
+    }
+  argv = grub_malloc (size);
+  if (argv == NULL)
+    goto finish;
+  argv[0] = title;
+  argv[argc] = NULL;
+
+  src = grub_xasprintf ("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;
@@ -829,7 +1152,7 @@ struct find_entry_info
 };
 
 static grub_err_t
-blsuki_set_find_entry_info (struct find_entry_info *info, const char *dirname, const char *devid)
+blsuki_set_find_entry_info (struct find_entry_info *info, const char *dirname, const char *devid, int cmd_type)
 {
   grub_device_t dev;
   grub_fs_t fs;
@@ -839,10 +1162,21 @@ blsuki_set_find_entry_info (struct find_entry_info *info, const char *dirname, c
 
   if (devid == NULL)
     {
+      if (cmd_type == BLSUKI_BLS_CMD)
+	{
 #ifdef GRUB_MACHINE_EMU
-      devid = "host";
+	  devid = "host";
 #else
-      devid = grub_env_get ("root");
+	  devid = grub_env_get ("root");
+#endif
+	}
+#ifdef GRUB_MACHINE_EFI
+      else if (cmd_type == BLSUKI_UKI_CMD)
+	{
+	  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");
@@ -882,10 +1216,11 @@ blsuki_set_find_entry_info (struct find_entry_info *info, const char *dirname, c
 
 /* info: the filesystem object the file is on. */
 static grub_err_t
-blsuki_find_entry (struct find_entry_info *info, bool enable_fallback)
+blsuki_find_entry (struct find_entry_info *info, bool enable_fallback, int cmd_type)
 {
   struct read_entry_info read_entry_info;
   char *default_dir = NULL;
+  const char *cmd_dir = NULL;
   char *tmp;
   grub_size_t default_size;
   grub_fs_t dir_fs = NULL;
@@ -901,6 +1236,7 @@ blsuki_find_entry (struct find_entry_info *info, bool enable_fallback)
   dir_dev = info->dev;
   dir_fs = info->fs;
   read_entry_info.devid = info->devid;
+  read_entry_info.cmd_type = cmd_type;
 
   r = dir_fs->fs_dir (dir_dev, read_entry_info.dirname, blsuki_read_entry,
 		      &read_entry_info);

grub-core/commands/blsuki.c blsuki_find_entry (struct find_entry_info *info, bool enable_fallback) · line 1253

Quoted 6 lines
grub-core/commands/blsuki.c +2 -1
@@ -913,11 +1249,17 @@ blsuki_find_entry (struct find_entry_info *info, bool enable_fallback)
   /*
    * 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".

Where? On ESP? Which one if there is more than one?

Quoted 53 lines
grub-core/commands/blsuki.c +13 -4
    */
   if (entries == NULL && fallback == 0 && enable_fallback == true)
     {
-      if (grub_add (grub_strlen (GRUB_BOOT_DEVICE), grub_strlen (GRUB_BLS_CONFIG_PATH), &default_size))
+      if (cmd_type == BLSUKI_BLS_CMD)
+	cmd_dir = GRUB_BLS_CONFIG_PATH;
+      else if (cmd_type == BLSUKI_UKI_CMD)
+	cmd_dir = GRUB_UKI_CONFIG_PATH;
+
+      if (grub_add (grub_strlen (GRUB_BOOT_DEVICE), grub_strlen (cmd_dir), &default_size))
 	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected calculating default directory buffer size");
 
       default_dir = grub_malloc (default_size);
@@ -925,9 +1267,9 @@ blsuki_find_entry (struct find_entry_info *info, bool enable_fallback)
 	return grub_errno;
 
       tmp = blsuki_update_boot_device (default_dir);
-      tmp = grub_stpcpy (tmp, GRUB_BLS_CONFIG_PATH);
+      tmp = grub_stpcpy (tmp, cmd_dir);
 
-      blsuki_set_find_entry_info (info, default_dir, NULL);
+      blsuki_set_find_entry_info (info, default_dir, NULL, cmd_type);
       grub_dprintf ("blsuki", "Entries weren't found in %s, fallback to %s\n",
 		    read_entry_info.dirname, info->dirname);
       fallback = 1;
@@ -939,15 +1281,18 @@ blsuki_find_entry (struct find_entry_info *info, bool enable_fallback)
 }
 
 static grub_err_t
-blsuki_load_entries (char *path, bool enable_fallback)
+blsuki_load_entries (char *path, bool enable_fallback, int cmd_type)
 {
   grub_size_t len;
+  grub_size_t ext_len;
   static grub_err_t r;
   const char *devid = NULL;
   char *dir = NULL;
   char *default_dir = NULL;
   char *tmp;
+  const char *cmd_dir = NULL;
   grub_size_t dir_size;
+  const char *ext = NULL;
   struct find_entry_info info = {
       .dev = NULL,
       .fs = NULL,
@@ -956,12 +1301,19 @@ blsuki_load_entries (char *path, bool enable_fallback)
   struct read_entry_info rei = {
       .devid = NULL,
       .dirname = NULL,
+      .cmd_type = cmd_type,
   };
 
   if (path != NULL)

grub-core/commands/blsuki.c blsuki_load_entries (char *path, bool enable_fallback) · line 1316

Quoted 10 lines
grub-core/commands/blsuki.c +7 -1
     {
+      if (cmd_type == BLSUKI_BLS_CMD)
+	ext = ".conf";
+      else if (cmd_type == BLSUKI_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)

This math worries me a bit. Is it safe? Same with earlier ones...

Daniel

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

#9 lore raw
In reply to Alec Brown via Grub-devel
Quoted 17 lines · Alec Brown via Grub-devel

v3:
- Added --enable-fallback option to check the default directory if the --path option isn't able to find entries.
- Added the function blsuki_set_find_entry_info() to help set the path and device of BLS and UKI entries.
- Removed grub_strchr uses in uki_read_osrel().
- Converted the static variable "cmd_type" into a parameter and added enums.
- Fixed improper handling of the output of grub_make_system_path_relative_to_its_root().

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

Quoted 9 lines

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

I noticed in your patch set that you haven't done the required "From:" overrides to maintain authorship when these are applied to git. Could you fix that up? Also with patches with multiple secondary authors or if you've done substantial cleanup, could you please add the appropriate "Co-authored-by:" stanzas?

Signature · 6 lines
真実はいつも一つ!/ Always, there's only one truth!

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#10 lore raw
Quoted 25 lines · Neal Gompa
Quoted 23 lines · Alec Brown via Grub-devel

v3:
- Added --enable-fallback option to check the default directory if the --path option isn't able to find entries.
- Added the function blsuki_set_find_entry_info() to help set the path and device of BLS and UKI entries.
- Removed grub_strchr uses in uki_read_osrel().
- Converted the static variable "cmd_type" into a parameter and added enums.
- Fixed improper handling of the output of grub_make_system_path_relative_to_its_root().

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

Quoted 6 lines
Quoted 2 lines

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

I noticed in your patch set that you haven't done the required "From:" overrides to maintain authorship when these are applied to git. Could you fix that up? Also with patches with multiple secondary authors or

Yeah, the "From:" thing has to be fixed.

Quoted 2 lines

if you've done substantial cleanup, could you please add the appropriate "Co-authored-by:" stanzas?

We use SOB only now.

Thanks for the comments!

Daniel

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

#11 lore raw
Quoted 34 lines · Daniel Kiper
Quoted 25 lines · Neal Gompa
Quoted 23 lines · Alec Brown via Grub-devel

v3:
- Added --enable-fallback option to check the default directory if the --path option isn't able to find entries.
- Added the function blsuki_set_find_entry_info() to help set the path and device of BLS and UKI entries.
- Removed grub_strchr uses in uki_read_osrel().
- Converted the static variable "cmd_type" into a parameter and added enums.
- Fixed improper handling of the output of grub_make_system_path_relative_to_its_root().

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

Quoted 6 lines
Quoted 2 lines

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

I noticed in your patch set that you haven't done the required "From:" overrides to maintain authorship when these are applied to git. Could you fix that up? Also with patches with multiple secondary authors or

Quoted 7 lines

Yeah, the "From:" thing has to be fixed.

Quoted 2 lines

if you've done substantial cleanup, could you please add the appropriate "Co-authored-by:" stanzas?

We use SOB only now.

Why? That makes it difficult to specifically query for patches made by multiple people. Many tools and Git forges process that tag *specifically* to be able to give credit to multiple authors.

"Signed-off-by" says absolutely nothing about authorship.

--
真実はいつも一つ!/ Always, there's only one truth!

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