Skip to contentSkip to conversation

Add basic Boot Loader Interface support

PATCH Superseded Oliver Steffen 3 participants 10 messages Jan 16, 2023 – Mar 2, 2023

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

cover Add basic Boot Loader Interface support

This is a step towards supporting unified kernel images (UKI) in Grub.

Add a new module named boot_loader_interface, which provides a command with the same name. It implements a small but quite useful part of the Boot Loader Interface [0]. This interface uses EFI variables for communication between the boot loader and the operating system.

This module sets two EFI variables under the vendor GUID 4a67b082-0a4c-41cf-b6c7-440b29bb8c4f:

- LoaderInfo: contains GRUB + <version number>.
This allows the running operating system to identify the boot loader used during boot.

- LoaderDevicePartUUID: contains the partition UUID of the EFI System Partition (ESP). This is used by systemd-gpt-auto-genereator [1] to find the root partitions (and others too), via partition type IDs [2]. This is especially useful for UKIs, where the kernel command line is fixed and usually does not contain any information about the root partition.

This module is only available on EFI platforms.

[0] https://systemd.io/BOOT_LOADER_INTERFACE/
[1] https://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html
[2] https://uapi-group.org/specifications/specs/discoverable_partitions_specification/

Oliver Steffen (2):
efi: add grub_variable_with_attributes
Add a module for the Boot Loader Interface

 grub-core/Makefile.core.def                |   6 +
 grub-core/commands/boot_loader_interface.c | 217 +++++++++++++++++++++
 grub-core/kern/efi/efi.c                   |  19 +-
 include/grub/efi/efi.h                     |   6 +
 4 files changed, 242 insertions(+), 6 deletions(-)
 create mode 100644 grub-core/commands/boot_loader_interface.c
Signature · 1 line
2.39.0
#1 lore raw

PATCH 1/2 efi: add grub_variable_with_attributes

Add a function to the EFI module that allows setting EFI variables with specific attributes.

This is useful for marking variables as volatile, for example.

---
 grub-core/kern/efi/efi.c | 19 +++++++++++++------
 include/grub/efi/efi.h   |  6 ++++++
 2 files changed, 19 insertions(+), 6 deletions(-)
grub-core/kern/efi/efi.c +13 -6
index cf49d6357..96fa96c15 100644
@@ -201,8 +201,8 @@ grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size,
 }
 
 grub_err_t
-grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
-		      void *data, grub_size_t datasize)
+grub_efi_set_variable_with_attributes(const char *var, const grub_efi_guid_t *guid,
+		      void *data, grub_size_t datasize, grub_uint32_t attributes)
 {
   grub_efi_status_t status;
   grub_efi_runtime_services_t *r;
@@ -219,10 +219,7 @@ grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
 
   r = grub_efi_system_table->runtime_services;
 
-  status = efi_call_5 (r->set_variable, var16, guid,
-		       (GRUB_EFI_VARIABLE_NON_VOLATILE
-			| GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
-			| GRUB_EFI_VARIABLE_RUNTIME_ACCESS),
+  status = efi_call_5 (r->set_variable, var16, guid, attributes,
 		       datasize, data);
   grub_free (var16);
   if (status == GRUB_EFI_SUCCESS)
@@ -231,6 +228,16 @@ grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
   return grub_error (GRUB_ERR_IO, "could not set EFI variable `%s'", var);
 }
 
+grub_err_t
+grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
+		      void *data, grub_size_t datasize)
+{
+  return grub_efi_set_variable_with_attributes (var, guid, data, datasize, 
+			GRUB_EFI_VARIABLE_NON_VOLATILE
+			| GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
+			| GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
+}
+
 grub_efi_status_t
 grub_efi_get_variable_with_attributes (const char *var,
 				       const grub_efi_guid_t *guid,
include/grub/efi/efi.h +6 -0
index e61272de5..d7bf0765b 100644
@@ -86,6 +86,12 @@ grub_efi_status_t EXPORT_FUNC (grub_efi_get_variable) (const char *variable,
 						       grub_size_t *datasize_out,
 						       void **data_out);
 grub_err_t
+EXPORT_FUNC (grub_efi_set_variable_with_attributes) (const char *var,
+				     const grub_efi_guid_t *guid,
+				     void *data,
+				     grub_size_t datasize,
+             grub_uint32_t attribute);
+grub_err_t
 EXPORT_FUNC (grub_efi_set_variable) (const char *var,
 				     const grub_efi_guid_t *guid,
 				     void *data,
Signature · 1 line
2.39.0
#2 lore raw

Re: [PATCH v1 1/2] efi: add grub_variable_with_attributes

Quoted 16 lines · Oliver Steffen

Add a function to the EFI module that allows setting EFI variables with specific attributes.

This is useful for marking variables as volatile, for example.

---
 grub-core/kern/efi/efi.c | 19 +++++++++++++------
 include/grub/efi/efi.h   |  6 ++++++
 2 files changed, 19 insertions(+), 6 deletions(-)
grub-core/kern/efi/efi.c +0 -0
index cf49d6357..96fa96c15 100644

grub-core/kern/efi/efi.c grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size, · line 204

Quoted 6 lines
grub-core/kern/efi/efi.c +1 -2
@@ -201,8 +201,8 @@ grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size,
 }
 
 grub_err_t
-grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
-		      void *data, grub_size_t datasize)
+grub_efi_set_variable_with_attributes(const char *var, const grub_efi_guid_t *guid,

Please add missing space before "(".

grub-core/kern/efi/efi.c grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size, · line 205

Quoted 1 line
grub-core/kern/efi/efi.c +1 -0
+		      void *data, grub_size_t datasize, grub_uint32_t attributes)

s/grub_uint32_t attributes/grub_efi_uint32_t attributes/?

Quoted 16 lines
grub-core/kern/efi/efi.c +1 -4
 {
   grub_efi_status_t status;
   grub_efi_runtime_services_t *r;
@@ -219,10 +219,7 @@ grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
 
   r = grub_efi_system_table->runtime_services;
 
-  status = efi_call_5 (r->set_variable, var16, guid,
-		       (GRUB_EFI_VARIABLE_NON_VOLATILE
-			| GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
-			| GRUB_EFI_VARIABLE_RUNTIME_ACCESS),
+  status = efi_call_5 (r->set_variable, var16, guid, attributes,
 		       datasize, data);
   grub_free (var16);
   if (status == GRUB_EFI_SUCCESS)

grub-core/kern/efi/efi.c grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid, · line 232

Quoted 5 lines
grub-core/kern/efi/efi.c +2 -0
@@ -231,6 +228,16 @@ grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
   return grub_error (GRUB_ERR_IO, "could not set EFI variable `%s'", var);
 }
 
+grub_err_t
+grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,

Again, missing space before "("...

Quoted 18 lines
grub-core/kern/efi/efi.c +8 -0
+		      void *data, grub_size_t datasize)
+{
+  return grub_efi_set_variable_with_attributes (var, guid, data, datasize,
+			GRUB_EFI_VARIABLE_NON_VOLATILE
+			| GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
+			| GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
+}
+
 grub_efi_status_t
 grub_efi_get_variable_with_attributes (const char *var,
 				       const grub_efi_guid_t *guid,
include/grub/efi/efi.h +0 -0
index e61272de5..d7bf0765b 100644
@@ -86,6 +86,12 @@ grub_efi_status_t EXPORT_FUNC (grub_efi_get_variable) (const char *variable,
 						       grub_size_t *datasize_out,
 						       void **data_out);

include/grub/efi/efi.h grub_efi_status_t EXPORT_FUNC (grub_efi_get_variable) (const char *variable, · line 97

Quoted 10 lines
include/grub/efi/efi.h +6 -0
 grub_err_t
+EXPORT_FUNC (grub_efi_set_variable_with_attributes) (const char *var,
+				     const grub_efi_guid_t *guid,
+				     void *data,
+				     grub_size_t datasize,
+             grub_uint32_t attribute);
+grub_err_t
 EXPORT_FUNC (grub_efi_set_variable) (const char *var,
 				     const grub_efi_guid_t *guid,
 				     void *data,

Otherwise patch LGTM...

Daniel

#3 lore raw

PATCH 2/2 Add a module for the Boot Loader Interface

In reply to Oliver Steffen

Add a new module named boot_loader_interface, which provides a command with the same name. It implements a small but quite useful part of the Boot Loader Interface [0]. This interface uses EFI variables for communication between the boot loader and the operating system.

This module sets two EFI variables under the vendor GUID 4a67b082-0a4c-41cf-b6c7-440b29bb8c4f:

- LoaderInfo: contains GRUB + <version number>.
This allows the running operating system to identify the boot loader used during boot.

- LoaderDevicePartUUID: contains the partition UUID of the EFI System Partition (ESP). This is used by systemd-gpt-auto-generator [1] to find the root partitions (and others too), via partition type IDs [2].

This module is only available on EFI platforms.

[0] https://systemd.io/BOOT_LOADER_INTERFACE/
[1] https://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html
[2] https://uapi-group.org/specifications/specs/discoverable_partitions_specification/

---
 grub-core/Makefile.core.def                |   6 +
 grub-core/commands/boot_loader_interface.c | 217 +++++++++++++++++++++
 2 files changed, 223 insertions(+)
 create mode 100644 grub-core/commands/boot_loader_interface.c
grub-core/Makefile.core.def +6 -0
index ba967aac8..23455fb71 100644
@@ -2547,3 +2547,9 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = boot_loader_interface;
+  efi = commands/boot_loader_interface.c;
+  enable = efi;
+};
grub-core/commands/boot_loader_interface.c new file +217 -0

217 lines not on this page · raw message

Signature · 1 line
2.39.0
#4 lore raw

Re: [PATCH v1 2/2] Add a module for the Boot Loader Interface

Quoted 1 line · Oliver Steffen

Add a new module named boot_loader_interface, which provides a command

I would prefer something shorter than boot_loader_interface. bli?

Quoted 34 lines

with the same name. It implements a small but quite useful part of the Boot Loader Interface [0]. This interface uses EFI variables for communication between the boot loader and the operating system.

This module sets two EFI variables under the vendor GUID 4a67b082-0a4c-41cf-b6c7-440b29bb8c4f:

- LoaderInfo: contains GRUB + <version number>.
This allows the running operating system to identify the boot loader used during boot.

- LoaderDevicePartUUID: contains the partition UUID of the EFI System Partition (ESP). This is used by systemd-gpt-auto-generator [1] to find the root partitions (and others too), via partition type IDs [2].

This module is only available on EFI platforms.

[0] https://systemd.io/BOOT_LOADER_INTERFACE/
[1] https://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html
[2] https://uapi-group.org/specifications/specs/discoverable_partitions_specification/

---
 grub-core/Makefile.core.def                |   6 +
 grub-core/commands/boot_loader_interface.c | 217 +++++++++++++++++++++
 2 files changed, 223 insertions(+)
 create mode 100644 grub-core/commands/boot_loader_interface.c
grub-core/Makefile.core.def +0 -0
index ba967aac8..23455fb71 100644

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

Quoted 6 lines
grub-core/Makefile.core.def +3 -0
@@ -2547,3 +2547,9 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = boot_loader_interface;

s/boot_loader_interface/bli/?

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

Quoted 1 line
grub-core/Makefile.core.def +1 -0
+  efi = commands/boot_loader_interface.c;

Ditto and below if needed...

grub-core/commands/boot_loader_interface.c line 1

Quoted 9 lines
grub-core/Makefile.core.def +2 -0
+  enable = efi;
+};
grub-core/commands/boot_loader_interface.c new file +1 -0
new file mode 100644
index 000000000..ccd7fa3d9
@@ -0,0 +1,217 @@
+/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/

Could you move this to the end of the file? Good example you can find in the Xen project [1].

grub-core/commands/boot_loader_interface.c line 3

Quoted 2 lines
grub-core/commands/boot_loader_interface.c new file +2 -0
+/* boot_loader_interface.c - implementation of the boot loader interface
+ */

Please use correct formatting for comments [2] and move this one behind the license below.

Quoted 6 lines
grub-core/commands/boot_loader_interface.c new file +6 -0
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *
+ *  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

grub-core/commands/boot_loader_interface.c line 21

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+ *  (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/>.
+ */

Please take license from the grub-core/kern/efi/sb.c file and update year in it. You can also merge module description with the license like it is done in the grub-core/kern/efi/sb.c.

Quoted 8 lines
grub-core/commands/boot_loader_interface.c new file +8 -0
+#include <grub/charset.h>
+#include <grub/efi/api.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/efi.h>
+#include <grub/err.h>
+#include <grub/extcmd.h>
+#include <grub/gpt_partition.h>
+#include <grub/misc.h>

grub-core/commands/boot_loader_interface.c line 40

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+#include <grub/mm.h>
+#include <grub/partition.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#define MODNAME "boot_loader_interface"
+
+static const grub_efi_guid_t boot_loader_interface_vendor_guid =
+  { 0x4a67b082, 0x0a4c, 0x41cf,
+    {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f} };

Please define this GUID as constant in the include/grub/efi/api.h (you will find good examples how it should be done there). I expect it can be useful for others too.

grub-core/commands/boot_loader_interface.c line 48

Quoted 7 lines
grub-core/commands/boot_loader_interface.c new file +7 -0
+static char *
+machine_get_bootdevice (void)
+{
+  grub_efi_loaded_image_t *image;
+
+  image = grub_efi_get_loaded_image (grub_efi_image_handle);
+  if (!image)

Please compare pointers explicitly with NULL, e.g. "if (image == NULL)", here and below...

Quoted 12 lines
grub-core/commands/boot_loader_interface.c new file +12 -0
+    return NULL;
+
+  return grub_efidisk_get_device_name (image->device_handle);
+}
+
+static grub_err_t
+get_part_uuid (grub_device_t dev, char **part_uuid)
+{
+  grub_err_t status = GRUB_ERR_NONE;
+  grub_disk_t disk;
+  struct grub_gpt_partentry entry;
+  grub_gpt_part_guid_t *guid;

grub-core/commands/boot_loader_interface.c line 70

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+
+  if (!dev || !dev->disk || !dev->disk->partition)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid device"));
+
+  disk = grub_disk_open (dev->disk->name);
+  if (!disk)
+    {
+      status = grub_errno;
+      grub_dprintf (MODNAME, "Error opening disk\n");
+      return grub_errno;

s/return grub_errno/return status/?

grub-core/commands/boot_loader_interface.c line 76

Quoted 6 lines
grub-core/commands/boot_loader_interface.c new file +6 -0
+    }
+
+  if (grub_strcmp (dev->disk->partition->partmap->name, "gpt") != 0)
+    {
+      status = grub_error (GRUB_ERR_BAD_PART_TABLE,
+                           N_("This is not a GPT partition table"));

All errors in the GRUB start with lower case, i.e. s/This is/this is/.

grub-core/commands/boot_loader_interface.c line 73

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      goto finish;

s/finish/fail/ and below please...

grub-core/commands/boot_loader_interface.c line 81

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+    }
+
+  if (grub_disk_read (disk, dev->disk->partition->offset,
+                      dev->disk->partition->index, sizeof (entry), &entry))

..., , &entry) != GRUB_ERR_NONE)

grub-core/commands/boot_loader_interface.c line 94

Quoted 13 lines
grub-core/commands/boot_loader_interface.c new file +13 -0
+    {
+      status = grub_errno;
+      grub_dprintf (MODNAME, "%s: Read error\n", dev->disk->name);
+      goto finish;
+    }
+
+  guid = &entry.guid;
+  *part_uuid = grub_xasprintf (
+      "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+      grub_le_to_cpu32 (guid->data1), grub_le_to_cpu16 (guid->data2),
+      grub_le_to_cpu16 (guid->data3), guid->data4[0], guid->data4[1],
+      guid->data4[2], guid->data4[3], guid->data4[4], guid->data4[5],
+      guid->data4[6], guid->data4[7]);

I think this is generic thing and can be converted into a function. Similar code is in the grub-core/commands/probe.c and util/grub-probe.c. It seems to me it is at least easy to make one function for the GRUB core code. Please do that. Of course in separate patch.

grub-core/commands/boot_loader_interface.c line 98

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+  if (!*part_uuid)
+    {
+      status = grub_errno;
+    }

Please drop redundant braces.

grub-core/commands/boot_loader_interface.c line 100

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+finish:

Labels should be prefixed with a space...

grub-core/commands/boot_loader_interface.c line 111

Quoted 11 lines
grub-core/commands/boot_loader_interface.c new file +11 -0
+  grub_disk_close (disk);
+
+  return status;
+}
+
+static grub_err_t
+set_efi_str_variable (const char *name, const grub_efi_guid_t *guid,
+                      const char *value)
+{
+  grub_size_t len;
+  grub_size_t len16;

grub_size_t len, len16;

grub-core/commands/boot_loader_interface.c line 116

Quoted 5 lines
grub-core/commands/boot_loader_interface.c new file +5 -0
+  grub_efi_char16_t *value_16;
+  grub_err_t status;
+
+  len = grub_strlen (value);
+  len16 = len * GRUB_MAX_UTF16_PER_UTF8;

What will happen when len == 0?

Is there any chance for overflow here? If yes please use overflow aware operators from include/grub/safemath.h here.

grub-core/commands/boot_loader_interface.c line 123

Quoted 6 lines
grub-core/commands/boot_loader_interface.c new file +6 -0
+  value_16 = grub_calloc (len16 + 1, sizeof (value_16[0]));
+  if (!value_16)
+    return grub_errno;
+
+  len16
+      = grub_utf8_to_utf16 (value_16, len16, (grub_uint8_t *)value, len, NULL);

Please do not break lines in that way. It is difficult to read.

Here you can just do

  len16 = grub_utf8_to_utf16 (value_16, len16, (grub_uint8_t *)value, len, NULL);

grub-core/commands/boot_loader_interface.c line 127

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+  value_16[len16] = 0;
+
+  status = grub_efi_set_variable_with_attributes (
+      name, guid, (void *)value_16, (len16 + 1) * sizeof (value_16[0]),

s/(void *)value_16/(void *) value_16/

Casts require a space... Please fix this kinds of problems everywhere.

grub-core/commands/boot_loader_interface.c line 129

Quoted 2 lines
grub-core/commands/boot_loader_interface.c new file +2 -0
+      GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS | GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
+  if (status != GRUB_ERR_NONE)

Yeah, I like this explicit comparison...

grub-core/commands/boot_loader_interface.c line 133

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+    {
+      grub_dprintf (MODNAME, "Error setting EFI variable %s: %d\n", name,
+                    status);
+    }

Please do not add braces if they are not needed...

grub-core/commands/boot_loader_interface.c line 143

Quoted 9 lines
grub-core/commands/boot_loader_interface.c new file +9 -0
+  grub_free (value_16);
+
+  return status;
+}
+
+static grub_err_t
+set_loader_info (void)
+{
+  grub_err_t status;

You do not need the status variable here. Just return immediately status from the set_efi_str_variable() call.

grub-core/commands/boot_loader_interface.c line 145

Quoted 2 lines
grub-core/commands/boot_loader_interface.c new file +2 -0
+  status = set_efi_str_variable (
+      "LoaderInfo", &boot_loader_interface_vendor_guid, PACKAGE_STRING);
  return set_efi_str_variable ("LoaderInfo", &boot_loader_interface_vendor_guid, PACKAGE_STRING);

I am OK with lines (a bit) longer than 80 chars when it increases readability.

Quoted 14 lines
grub-core/commands/boot_loader_interface.c new file +14 -0
+  return status;
+}
+
+static grub_err_t
+set_loader_device_part_uuid (void)
+{
+  grub_err_t status = GRUB_ERR_NONE;
+  char *device_name = NULL;
+  grub_device_t device;
+  char *part_uuid = NULL;
+
+  device_name = machine_get_bootdevice ();
+  if (!device_name)
+    {

grub-core/commands/boot_loader_interface.c line 169

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+      return grub_error (GRUB_ERR_BAD_DEVICE,
+                         N_("Unable to find boot device"));
+    }
+
+  device = grub_device_open (device_name);
+  if (!device)
+    {
+      status = grub_errno;
+      grub_dprintf (MODNAME, "Error opening device: %s", device_name);
+      goto err;

s/err/fail/ and below please...

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+    }
+
+  status = get_part_uuid (device, &part_uuid);
+

grub-core/commands/boot_loader_interface.c line 183

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+  grub_device_close (device);
+
+  if (status == GRUB_ERR_NONE)
+    {
+      status = set_efi_str_variable ("LoaderDevicePartUUID",
+                                     &boot_loader_interface_vendor_guid,
+                                     part_uuid);
+    }
+
+err:

Missing space before label.

Quoted 21 lines
grub-core/commands/boot_loader_interface.c new file +21 -0
+  grub_free (part_uuid);
+  grub_free (device_name);
+  return status;
+}
+
+static grub_err_t
+grub_cmd_boot_loader_interface (grub_extcmd_context_t ctxt __attribute__ ((unused)),
+                                int argc __attribute__ ((unused)),
+                                char **args __attribute__ ((unused)))
+{
+  grub_err_t status;
+
+  status = set_loader_info ();
+  if (status != GRUB_ERR_NONE)
+    return status;
+
+  status = set_loader_device_part_uuid ();
+  if (status != GRUB_ERR_NONE)
+    return status;
+
+  return GRUB_ERR_NONE;

grub-core/commands/boot_loader_interface.c line 214

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+}
+
+static grub_extcmd_t cmd;
+
+GRUB_MOD_INIT (boot_loader_interface)
+{
+  grub_dprintf (MODNAME, "%s got here\n", __func__);
+  cmd = grub_register_extcmd (
+      "boot_loader_interface", grub_cmd_boot_loader_interface, 0, NULL,
+      N_("Set EFI variables according to Boot Loader Interface spec."), NULL);

This kind of formatting does not parse well...

cmd = grub_register_extcmd ("boot_loader_interface", grub_cmd_boot_loader_interface, 0, NULL,

                              N_("Set EFI variables according to Boot Loader Interface spec."), NULL);

Please fix similar problem everywhere.

grub-core/commands/boot_loader_interface.c line 217

Quoted 3 lines
grub-core/commands/boot_loader_interface.c new file +3 -0
+}
+
+GRUB_MOD_FINI (boot_loader_interface) { grub_unregister_extcmd (cmd); }

GRUB_MOD_FINI (boot_loader_interface)
{
grub_unregister_extcmd (cmd);
}

Daniel

[1] https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/arch/x86/spec_ctrl.c;h=e80e2a5ed1a9b61bce564f83baf95492ff17198d;hb=HEAD
[2] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments

#5 lore raw

Re: [PATCH v1 2/2] Add a module for the Boot Loader Interface

Thank you for the comments, Daniel.

Quoting Daniel Kiper (2023-02-15 19:27:03)

Quoted 4 lines · Oliver Steffen
Quoted 1 line

Add a new module named boot_loader_interface, which provides a command

I would prefer something shorter than boot_loader_interface. bli?

Then bli it is.

Quoted 48 lines
Quoted 35 lines

with the same name. It implements a small but quite useful part of the Boot Loader Interface [0]. This interface uses EFI variables for communication between the boot loader and the operating system.

This module sets two EFI variables under the vendor GUID 4a67b082-0a4c-41cf-b6c7-440b29bb8c4f:

- LoaderInfo: contains GRUB + <version number>.
This allows the running operating system to identify the boot loader used during boot.

- LoaderDevicePartUUID: contains the partition UUID of the EFI System Partition (ESP). This is used by systemd-gpt-auto-generator [1] to find the root partitions (and others too), via partition type IDs [2].

This module is only available on EFI platforms.

[0] https://systemd.io/BOOT_LOADER_INTERFACE/
[1] https://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html
[2] https://uapi-group.org/specifications/specs/discoverable_partitions_specification/

---
 grub-core/Makefile.core.def                |   6 +
 grub-core/commands/boot_loader_interface.c | 217 +++++++++++++++++++++
 2 files changed, 223 insertions(+)
 create mode 100644 grub-core/commands/boot_loader_interface.c
grub-core/Makefile.core.def +0 -0
index ba967aac8..23455fb71 100644

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

Quoted 6 lines
grub-core/Makefile.core.def +3 -0
@@ -2547,3 +2547,9 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = boot_loader_interface;

s/boot_loader_interface/bli/?

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

Quoted 1 line
grub-core/Makefile.core.def +1 -0
+  efi = commands/boot_loader_interface.c;

Ditto and below if needed...

Quoted 12 lines

grub-core/commands/boot_loader_interface.c line 1

Quoted 9 lines
grub-core/Makefile.core.def +2 -0
+  enable = efi;
+};
grub-core/commands/boot_loader_interface.c new file +1 -0
new file mode 100644
index 000000000..ccd7fa3d9
@@ -0,0 +1,217 @@
+/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/

Could you move this to the end of the file? Good example you can find in the Xen project [1].

I personally do not care about this, it was just part of the file I used as a template. Is this wanted, or can we just drop it?

Quoted 121 lines

grub-core/commands/boot_loader_interface.c line 3

Quoted 3 lines
grub-core/commands/boot_loader_interface.c new file +2 -0
+/* boot_loader_interface.c - implementation of the boot loader interface
+ */

Please use correct formatting for comments [2] and move this one behind the license below.

Quoted 6 lines
grub-core/commands/boot_loader_interface.c new file +6 -0
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *
+ *  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

grub-core/commands/boot_loader_interface.c line 21

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+ *  (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/>.
+ */

Please take license from the grub-core/kern/efi/sb.c file and update year in it. You can also merge module description with the license like it is done in the grub-core/kern/efi/sb.c.

Quoted 8 lines
grub-core/commands/boot_loader_interface.c new file +8 -0
+#include <grub/charset.h>
+#include <grub/efi/api.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/efi.h>
+#include <grub/err.h>
+#include <grub/extcmd.h>
+#include <grub/gpt_partition.h>
+#include <grub/misc.h>

grub-core/commands/boot_loader_interface.c line 40

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+#include <grub/mm.h>
+#include <grub/partition.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#define MODNAME "boot_loader_interface"
+
+static const grub_efi_guid_t boot_loader_interface_vendor_guid =
+  { 0x4a67b082, 0x0a4c, 0x41cf,
+    {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f} };

Please define this GUID as constant in the include/grub/efi/api.h (you will find good examples how it should be done there). I expect it can be useful for others too.

grub-core/commands/boot_loader_interface.c line 48

Quoted 7 lines
grub-core/commands/boot_loader_interface.c new file +7 -0
+static char *
+machine_get_bootdevice (void)
+{
+  grub_efi_loaded_image_t *image;
+
+  image = grub_efi_get_loaded_image (grub_efi_image_handle);
+  if (!image)

Please compare pointers explicitly with NULL, e.g. "if (image == NULL)", here and below...

Quoted 12 lines
grub-core/commands/boot_loader_interface.c new file +12 -0
+    return NULL;
+
+  return grub_efidisk_get_device_name (image->device_handle);
+}
+
+static grub_err_t
+get_part_uuid (grub_device_t dev, char **part_uuid)
+{
+  grub_err_t status = GRUB_ERR_NONE;
+  grub_disk_t disk;
+  struct grub_gpt_partentry entry;
+  grub_gpt_part_guid_t *guid;

grub-core/commands/boot_loader_interface.c line 70

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+
+  if (!dev || !dev->disk || !dev->disk->partition)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid device"));
+
+  disk = grub_disk_open (dev->disk->name);
+  if (!disk)
+    {
+      status = grub_errno;
+      grub_dprintf (MODNAME, "Error opening disk\n");
+      return grub_errno;

s/return grub_errno/return status/?

grub-core/commands/boot_loader_interface.c line 76

Quoted 6 lines
grub-core/commands/boot_loader_interface.c new file +6 -0
+    }
+
+  if (grub_strcmp (dev->disk->partition->partmap->name, "gpt") != 0)
+    {
+      status = grub_error (GRUB_ERR_BAD_PART_TABLE,
+                           N_("This is not a GPT partition table"));

All errors in the GRUB start with lower case, i.e. s/This is/this is/.

grub-core/commands/boot_loader_interface.c line 73

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      goto finish;

s/finish/fail/ and below please...

grub-core/commands/boot_loader_interface.c line 81

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+    }
+
+  if (grub_disk_read (disk, dev->disk->partition->offset,
+                      dev->disk->partition->index, sizeof (entry), &entry))

..., , &entry) != GRUB_ERR_NONE)

grub-core/commands/boot_loader_interface.c line 94

Quoted 13 lines
grub-core/commands/boot_loader_interface.c new file +13 -0
+    {
+      status = grub_errno;
+      grub_dprintf (MODNAME, "%s: Read error\n", dev->disk->name);
+      goto finish;
+    }
+
+  guid = &entry.guid;
+  *part_uuid = grub_xasprintf (
+      "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+      grub_le_to_cpu32 (guid->data1), grub_le_to_cpu16 (guid->data2),
+      grub_le_to_cpu16 (guid->data3), guid->data4[0], guid->data4[1],
+      guid->data4[2], guid->data4[3], guid->data4[4], guid->data4[5],
+      guid->data4[6], guid->data4[7]);
Quoted 4 lines

I think this is generic thing and can be converted into a function. Similar code is in the grub-core/commands/probe.c and util/grub-probe.c. It seems to me it is at least easy to make one function for the GRUB core code. Please do that. Of course in separate patch.

There are multiple representations of a GUID:
- grub_gpt_part_guid (gpt_partition.h)
- grub_efi_guid (efi/api.h)
- grub_efi_packed_guid (efi/api.h)

I would add a function for the first kind to gpt_partition.h and partmap/gpt.c that prints into a string buffer (length checked).

It would be nice to add a format specifier for GUIDs to the printf implementation,
but that only makes much sense (I think) if the GUID repesentations could be unified into one first, in a central place.

Quoted 31 lines

grub-core/commands/boot_loader_interface.c line 98

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+  if (!*part_uuid)
+    {
+      status = grub_errno;
+    }

Please drop redundant braces.

grub-core/commands/boot_loader_interface.c line 100

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+finish:

Labels should be prefixed with a space...

grub-core/commands/boot_loader_interface.c line 111

Quoted 11 lines
grub-core/commands/boot_loader_interface.c new file +11 -0
+  grub_disk_close (disk);
+
+  return status;
+}
+
+static grub_err_t
+set_efi_str_variable (const char *name, const grub_efi_guid_t *guid,
+                      const char *value)
+{
+  grub_size_t len;
+  grub_size_t len16;

grub_size_t len, len16;

grub-core/commands/boot_loader_interface.c line 116

Quoted 5 lines
grub-core/commands/boot_loader_interface.c new file +5 -0
+  grub_efi_char16_t *value_16;
+  grub_err_t status;
+
+  len = grub_strlen (value);
+  len16 = len * GRUB_MAX_UTF16_PER_UTF8;
Quoted 4 lines

What will happen when len == 0?

Is there any chance for overflow here? If yes please use overflow aware operators from include/grub/safemath.h here.

Fixing this. Same problem also in grub_efi_set_variable_with_attributes. This is were I found this code. Fill fix there too.

Quoted 147 lines

grub-core/commands/boot_loader_interface.c line 123

Quoted 7 lines
grub-core/commands/boot_loader_interface.c new file +6 -0
+  value_16 = grub_calloc (len16 + 1, sizeof (value_16[0]));
+  if (!value_16)
+    return grub_errno;
+
+  len16
+      = grub_utf8_to_utf16 (value_16, len16, (grub_uint8_t *)value, len, NULL);

Please do not break lines in that way. It is difficult to read.

Here you can just do

  len16 = grub_utf8_to_utf16 (value_16, len16, (grub_uint8_t *)value, len, NULL);

grub-core/commands/boot_loader_interface.c line 127

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+  value_16[len16] = 0;
+
+  status = grub_efi_set_variable_with_attributes (
+      name, guid, (void *)value_16, (len16 + 1) * sizeof (value_16[0]),

s/(void *)value_16/(void *) value_16/

Casts require a space... Please fix this kinds of problems everywhere.

grub-core/commands/boot_loader_interface.c line 129

Quoted 2 lines
grub-core/commands/boot_loader_interface.c new file +2 -0
+      GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS | GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
+  if (status != GRUB_ERR_NONE)

Yeah, I like this explicit comparison...

grub-core/commands/boot_loader_interface.c line 133

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+    {
+      grub_dprintf (MODNAME, "Error setting EFI variable %s: %d\n", name,
+                    status);
+    }

Please do not add braces if they are not needed...

grub-core/commands/boot_loader_interface.c line 143

Quoted 9 lines
grub-core/commands/boot_loader_interface.c new file +9 -0
+  grub_free (value_16);
+
+  return status;
+}
+
+static grub_err_t
+set_loader_info (void)
+{
+  grub_err_t status;

You do not need the status variable here. Just return immediately status from the set_efi_str_variable() call.

grub-core/commands/boot_loader_interface.c line 145

Quoted 2 lines
grub-core/commands/boot_loader_interface.c new file +2 -0
+  status = set_efi_str_variable (
+      "LoaderInfo", &boot_loader_interface_vendor_guid, PACKAGE_STRING);
  return set_efi_str_variable ("LoaderInfo", &boot_loader_interface_vendor_guid, PACKAGE_STRING);

I am OK with lines (a bit) longer than 80 chars when it increases readability.

Quoted 14 lines
grub-core/commands/boot_loader_interface.c new file +14 -0
+  return status;
+}
+
+static grub_err_t
+set_loader_device_part_uuid (void)
+{
+  grub_err_t status = GRUB_ERR_NONE;
+  char *device_name = NULL;
+  grub_device_t device;
+  char *part_uuid = NULL;
+
+  device_name = machine_get_bootdevice ();
+  if (!device_name)
+    {

grub-core/commands/boot_loader_interface.c line 169

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+      return grub_error (GRUB_ERR_BAD_DEVICE,
+                         N_("Unable to find boot device"));
+    }
+
+  device = grub_device_open (device_name);
+  if (!device)
+    {
+      status = grub_errno;
+      grub_dprintf (MODNAME, "Error opening device: %s", device_name);
+      goto err;

s/err/fail/ and below please...

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+    }
+
+  status = get_part_uuid (device, &part_uuid);
+

grub-core/commands/boot_loader_interface.c line 183

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+  grub_device_close (device);
+
+  if (status == GRUB_ERR_NONE)
+    {
+      status = set_efi_str_variable ("LoaderDevicePartUUID",
+                                     &boot_loader_interface_vendor_guid,
+                                     part_uuid);
+    }
+
+err:

Missing space before label.

Quoted 21 lines
grub-core/commands/boot_loader_interface.c new file +21 -0
+  grub_free (part_uuid);
+  grub_free (device_name);
+  return status;
+}
+
+static grub_err_t
+grub_cmd_boot_loader_interface (grub_extcmd_context_t ctxt __attribute__ ((unused)),
+                                int argc __attribute__ ((unused)),
+                                char **args __attribute__ ((unused)))
+{
+  grub_err_t status;
+
+  status = set_loader_info ();
+  if (status != GRUB_ERR_NONE)
+    return status;
+
+  status = set_loader_device_part_uuid ();
+  if (status != GRUB_ERR_NONE)
+    return status;
+
+  return GRUB_ERR_NONE;

grub-core/commands/boot_loader_interface.c line 214

Quoted 10 lines
grub-core/commands/boot_loader_interface.c new file +10 -0
+}
+
+static grub_extcmd_t cmd;
+
+GRUB_MOD_INIT (boot_loader_interface)
+{
+  grub_dprintf (MODNAME, "%s got here\n", __func__);
+  cmd = grub_register_extcmd (
+      "boot_loader_interface", grub_cmd_boot_loader_interface, 0, NULL,
+      N_("Set EFI variables according to Boot Loader Interface spec."), NULL);

This kind of formatting does not parse well...

cmd = grub_register_extcmd ("boot_loader_interface", grub_cmd_boot_loader_interface, 0, NULL,

                              N_("Set EFI variables according to Boot Loader Interface spec."), NULL);

Please fix similar problem everywhere.

grub-core/commands/boot_loader_interface.c line 217

Quoted 3 lines
grub-core/commands/boot_loader_interface.c new file +3 -0
+}
+
+GRUB_MOD_FINI (boot_loader_interface) { grub_unregister_extcmd (cmd); }

GRUB_MOD_FINI (boot_loader_interface)
{
grub_unregister_extcmd (cmd);
}

Quoted 5 lines

Daniel

[1] https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/arch/x86/spec_ctrl.c;h=e80e2a5ed1a9b61bce564f83baf95492ff17198d;hb=HEAD
[2] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments

Thanks for the review. I will send out v2 shortly.

-Oliver

#6 lore raw

Re: [PATCH v1 2/2] Add a module for the Boot Loader Interface

Quoted 9 lines · Oliver Steffen

Thank you for the comments, Daniel.

Quoting Daniel Kiper (2023-02-15 19:27:03)

Quoted 4 lines · Oliver Steffen
Quoted 1 line

Add a new module named boot_loader_interface, which provides a command

I would prefer something shorter than boot_loader_interface. bli?

Then bli it is.

Cool! Thanks!

Quoted 60 lines
Quoted 47 lines
Quoted 34 lines

with the same name. It implements a small but quite useful part of the Boot Loader Interface [0]. This interface uses EFI variables for communication between the boot loader and the operating system.

This module sets two EFI variables under the vendor GUID 4a67b082-0a4c-41cf-b6c7-440b29bb8c4f:

- LoaderInfo: contains GRUB + <version number>.
This allows the running operating system to identify the boot loader used during boot.

- LoaderDevicePartUUID: contains the partition UUID of the EFI System Partition (ESP). This is used by systemd-gpt-auto-generator [1] to find the root partitions (and others too), via partition type IDs [2].

This module is only available on EFI platforms.

[0] https://systemd.io/BOOT_LOADER_INTERFACE/
[1] https://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html
[2] https://uapi-group.org/specifications/specs/discoverable_partitions_specification/

---
 grub-core/Makefile.core.def                |   6 +
 grub-core/commands/boot_loader_interface.c | 217 +++++++++++++++++++++
 2 files changed, 223 insertions(+)
 create mode 100644 grub-core/commands/boot_loader_interface.c
grub-core/Makefile.core.def +0 -0
index ba967aac8..23455fb71 100644

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

Quoted 6 lines
grub-core/Makefile.core.def +3 -0
@@ -2547,3 +2547,9 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = boot_loader_interface;

s/boot_loader_interface/bli/?

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

Quoted 1 line
grub-core/Makefile.core.def +1 -0
+  efi = commands/boot_loader_interface.c;

Ditto and below if needed...

Quoted 12 lines

grub-core/commands/boot_loader_interface.c line 1

Quoted 9 lines
grub-core/Makefile.core.def +2 -0
+  enable = efi;
+};
grub-core/commands/boot_loader_interface.c new file +1 -0
new file mode 100644
index 000000000..ccd7fa3d9
@@ -0,0 +1,217 @@
+/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/

Could you move this to the end of the file? Good example you can find in the Xen project [1].

Quoted 2 lines

I personally do not care about this, it was just part of the file I used as a template. Is this wanted, or can we just drop it?

If you do not need it please drop it.

[...]

Quoted 18 lines
Quoted 8 lines

grub-core/commands/boot_loader_interface.c line 94

Quoted 7 lines
grub-core/commands/boot_loader_interface.c new file +7 -0
+  guid = &entry.guid;
+  *part_uuid = grub_xasprintf (
+      "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+      grub_le_to_cpu32 (guid->data1), grub_le_to_cpu16 (guid->data2),
+      grub_le_to_cpu16 (guid->data3), guid->data4[0], guid->data4[1],
+      guid->data4[2], guid->data4[3], guid->data4[4], guid->data4[5],
+      guid->data4[6], guid->data4[7]);
Quoted 4 lines

I think this is generic thing and can be converted into a function. Similar code is in the grub-core/commands/probe.c and util/grub-probe.c. It seems to me it is at least easy to make one function for the GRUB core code. Please do that. Of course in separate patch.

There are multiple representations of a GUID:
- grub_gpt_part_guid (gpt_partition.h)
- grub_efi_guid (efi/api.h)
- grub_efi_packed_guid (efi/api.h)

Quoted 7 lines

I would add a function for the first kind to gpt_partition.h and partmap/gpt.c that prints into a string buffer (length checked).

It would be nice to add a format specifier for GUIDs to the printf implementation,
but that only makes much sense (I think) if the GUID repesentations could be unified into one first, in a central place.

Could you do the unification and introduce format specifier for GUIDs?

Quoted 31 lines
Quoted 31 lines

grub-core/commands/boot_loader_interface.c line 98

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+  if (!*part_uuid)
+    {
+      status = grub_errno;
+    }

Please drop redundant braces.

grub-core/commands/boot_loader_interface.c line 100

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+finish:

Labels should be prefixed with a space...

grub-core/commands/boot_loader_interface.c line 111

Quoted 11 lines
grub-core/commands/boot_loader_interface.c new file +11 -0
+  grub_disk_close (disk);
+
+  return status;
+}
+
+static grub_err_t
+set_efi_str_variable (const char *name, const grub_efi_guid_t *guid,
+                      const char *value)
+{
+  grub_size_t len;
+  grub_size_t len16;

grub_size_t len, len16;

Quoted 5 lines
grub-core/commands/boot_loader_interface.c new file +5 -0
+  grub_efi_char16_t *value_16;
+  grub_err_t status;
+
+  len = grub_strlen (value);
+  len16 = len * GRUB_MAX_UTF16_PER_UTF8;
Quoted 7 lines
Quoted 4 lines

What will happen when len == 0?

Is there any chance for overflow here? If yes please use overflow aware operators from include/grub/safemath.h here.

Fixing this. Same problem also in grub_efi_set_variable_with_attributes. This is were I found this code. Fill fix there too.

Cool! Thanks a lot!

Quoted 1 line

Thanks for the review. I will send out v2 shortly.

Great!

Thanks,

Daniel

#7 lore raw

Re: [PATCH v1 2/2] Add a module for the Boot Loader Interface

Quoted 115 lines · Daniel Kiper
Quoted 5 lines · Oliver Steffen

Thank you for the comments, Daniel.

Quoting Daniel Kiper (2023-02-15 19:27:03)

Quoted 2 lines · Oliver Steffen
Quoted 1 line

Add a new module named boot_loader_interface, which provides a

command

Quoted 4 lines
Quoted 2 lines

I would prefer something shorter than boot_loader_interface. bli?

Then bli it is.

Cool! Thanks!

Quoted 1 line
Quoted 1 line
Quoted 1 line

with the same name. It implements a small but quite useful part of

the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Boot Loader Interface [0]. This interface uses EFI variables for communication between the boot loader and the operating system.

This module sets two EFI variables under the vendor GUID 4a67b082-0a4c-41cf-b6c7-440b29bb8c4f:

- LoaderInfo: contains GRUB + <version number>.
This allows the running operating system to identify the boot

loader

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

used during boot.

- LoaderDevicePartUUID: contains the partition UUID of the EFI System Partition (ESP). This is used by systemd-gpt-auto-generator [1] to find the root partitions (and

others

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

too), via partition type IDs [2].

This module is only available on EFI platforms.

[0] https://systemd.io/BOOT_LOADER_INTERFACE/
[1]

https://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html

Quoted 1 line
Quoted 1 line
Quoted 1 line

[2]

https://uapi-group.org/specifications/specs/discoverable_partitions_specification/

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
---
 grub-core/Makefile.core.def                |   6 +
 grub-core/commands/boot_loader_interface.c | 217

+++++++++++++++++++++

Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
 2 files changed, 223 insertions(+)
 create mode 100644 grub-core/commands/boot_loader_interface.c
(quoted diff) +0 -0

b/grub-core/Makefile.core.def

Quoted 13 lines
Quoted 13 lines

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

Quoted 10 lines

index ba967aac8..23455fb71 100644

grub-core/Makefile.core.def +3 -0
@@ -2547,3 +2547,9 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = boot_loader_interface;

s/boot_loader_interface/bli/?

Quoted 7 lines
Quoted 7 lines

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

Quoted 1 line
grub-core/Makefile.core.def +1 -0
+  efi = commands/boot_loader_interface.c;

Ditto and below if needed...

Review

Quoted 3 lines
grub-core/Makefile.core.def +2 -0
+  enable = efi;
+};
(quoted diff) +0 -0

b/grub-core/commands/boot_loader_interface.c

Quoted 8 lines
Quoted 8 lines

grub-core/commands/boot_loader_interface.c line 1

Quoted 6 lines

new file mode 100644
index 000000000..ccd7fa3d9

grub-core/commands/boot_loader_interface.c new file +1 -0
@@ -0,0 +1,217 @@
+/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/

Could you move this to the end of the file? Good example you can find

in

Quoted 4 lines
Quoted 1 line

the Xen project [1].

I personally do not care about this, it was just part of the file I used as a template. Is this wanted, or can we just drop it?

If you do not need it please drop it.

[...]

Quoted 4 lines
Quoted 4 lines

grub-core/commands/boot_loader_interface.c

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+  guid = &entry.guid;
+  *part_uuid = grub_xasprintf (
+      "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+      grub_le_to_cpu32 (guid->data1), grub_le_to_cpu16

(guid->data2),

Quoted 1 line
Quoted 1 line

grub-core/commands/boot_loader_interface.c line 6

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      grub_le_to_cpu16 (guid->data3), guid->data4[0],

guid->data4[1],

Quoted 1 line
Quoted 1 line

grub-core/commands/boot_loader_interface.c line 7

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      guid->data4[2], guid->data4[3], guid->data4[4],

guid->data4[5],

Quoted 4 lines
Quoted 4 lines

grub-core/commands/boot_loader_interface.c line 94

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      guid->data4[6], guid->data4[7]);

I think this is generic thing and can be converted into a function. Similar code is in the grub-core/commands/probe.c and

util/grub-probe.c.

Quoted 11 lines
Quoted 2 lines

It seems to me it is at least easy to make one function for the GRUB core code. Please do that. Of course in separate patch.

There are multiple representations of a GUID:
- grub_gpt_part_guid (gpt_partition.h)
- grub_efi_guid (efi/api.h)
- grub_efi_packed_guid (efi/api.h)

I would add a function for the first kind to gpt_partition.h and partmap/gpt.c that prints into a string buffer (length checked).

Quoted 7 lines
Quoted 4 lines

It would be nice to add a format specifier for GUIDs to the printf implementation,
but that only makes much sense (I think) if the GUID repesentations could be unified into one first, in a central place.

Could you do the unification and introduce format specifier for GUIDs?

Assume there is a common GUID struct.
Then we have the following options (AFAIK):
- add a format specifier for it, lets say '%g' or '%y'. This causes problems with the -Wformat checks. We could disable it, but that's a bad idea.
- use an additional qualifier for %p, for example %p{GUID}. This way gcc will not complain (%p takes void *). But it is ugly if one wants to print a '{' after a GUID.
- Do not use a custom format specifier at all; just provide a "to-string" function. To print a guid one needs a temporary buffer (less convenient, but still better than repeating the raw printf statement for GUID everywhere, like it is now).

Pick your poison... :-)
Or does anybody have a better idea?
Thanks,
Oliver

#8 lore raw

Re: [PATCH v1 2/2] Add a module for the Boot Loader Interface

Quoted 125 lines · Oliver Steffen
Quoted 115 lines · Daniel Kiper
Quoted 5 lines · Oliver Steffen

Thank you for the comments, Daniel.

Quoting Daniel Kiper (2023-02-15 19:27:03)

Quoted 2 lines · Oliver Steffen
Quoted 1 line

Add a new module named boot_loader_interface, which provides a

command

Quoted 4 lines
Quoted 2 lines

I would prefer something shorter than boot_loader_interface. bli?

Then bli it is.

Cool! Thanks!

Quoted 1 line
Quoted 1 line
Quoted 1 line

with the same name. It implements a small but quite useful part of

the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Boot Loader Interface [0]. This interface uses EFI variables for communication between the boot loader and the operating system.

This module sets two EFI variables under the vendor GUID 4a67b082-0a4c-41cf-b6c7-440b29bb8c4f:

- LoaderInfo: contains GRUB + <version number>.
This allows the running operating system to identify the boot

loader

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

used during boot.

- LoaderDevicePartUUID: contains the partition UUID of the EFI System Partition (ESP). This is used by systemd-gpt-auto-generator [1] to find the root partitions (and

others

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

too), via partition type IDs [2].

This module is only available on EFI platforms.

[0] https://systemd.io/BOOT_LOADER_INTERFACE/
[1]

https://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html

Quoted 1 line
Quoted 1 line
Quoted 1 line

[2]

https://uapi-group.org/specifications/specs/discoverable_partitions_specification/

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
---
 grub-core/Makefile.core.def                |   6 +
 grub-core/commands/boot_loader_interface.c | 217

+++++++++++++++++++++

Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
 2 files changed, 223 insertions(+)
 create mode 100644 grub-core/commands/boot_loader_interface.c
(quoted diff) +0 -0

b/grub-core/Makefile.core.def

Quoted 13 lines
Quoted 13 lines

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

Quoted 10 lines

index ba967aac8..23455fb71 100644

grub-core/Makefile.core.def +3 -0
@@ -2547,3 +2547,9 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = boot_loader_interface;

s/boot_loader_interface/bli/?

Quoted 7 lines
Quoted 7 lines

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

Quoted 1 line
grub-core/Makefile.core.def +1 -0
+  efi = commands/boot_loader_interface.c;

Ditto and below if needed...

Review

Quoted 3 lines
grub-core/Makefile.core.def +2 -0
+  enable = efi;
+};
(quoted diff) +0 -0

b/grub-core/commands/boot_loader_interface.c

Quoted 8 lines
Quoted 8 lines

grub-core/commands/boot_loader_interface.c line 1

Quoted 6 lines

new file mode 100644
index 000000000..ccd7fa3d9

grub-core/commands/boot_loader_interface.c new file +1 -0
@@ -0,0 +1,217 @@
+/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/

Could you move this to the end of the file? Good example you can find

in

Quoted 4 lines
Quoted 1 line

the Xen project [1].

I personally do not care about this, it was just part of the file I used as a template. Is this wanted, or can we just drop it?

If you do not need it please drop it.

[...]

Quoted 4 lines
Quoted 4 lines

grub-core/commands/boot_loader_interface.c

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+  guid = &entry.guid;
+  *part_uuid = grub_xasprintf (
+      "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+      grub_le_to_cpu32 (guid->data1), grub_le_to_cpu16

(guid->data2),

Quoted 1 line
Quoted 1 line

grub-core/commands/boot_loader_interface.c line 6

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      grub_le_to_cpu16 (guid->data3), guid->data4[0],

guid->data4[1],

Quoted 1 line
Quoted 1 line

grub-core/commands/boot_loader_interface.c line 7

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      guid->data4[2], guid->data4[3], guid->data4[4],

guid->data4[5],

Quoted 4 lines
Quoted 4 lines

grub-core/commands/boot_loader_interface.c line 94

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      guid->data4[6], guid->data4[7]);

I think this is generic thing and can be converted into a function. Similar code is in the grub-core/commands/probe.c and

util/grub-probe.c.

Quoted 11 lines
Quoted 2 lines

It seems to me it is at least easy to make one function for the GRUB core code. Please do that. Of course in separate patch.

There are multiple representations of a GUID:
- grub_gpt_part_guid (gpt_partition.h)
- grub_efi_guid (efi/api.h)
- grub_efi_packed_guid (efi/api.h)

I would add a function for the first kind to gpt_partition.h and partmap/gpt.c that prints into a string buffer (length checked).

Quoted 7 lines
Quoted 4 lines

It would be nice to add a format specifier for GUIDs to the printf implementation,
but that only makes much sense (I think) if the GUID repesentations could be unified into one first, in a central place.

Could you do the unification and introduce format specifier for GUIDs?

Quoted 13 lines

Assume there is a common GUID struct.
Then we have the following options (AFAIK):
- add a format specifier for it, lets say '%g' or '%y'. This causes problems with the -Wformat checks. We could disable it, but that's a bad idea.
- use an additional qualifier for %p, for example %p{GUID}. This way gcc will not complain (%p takes void *). But it is ugly if one wants to print a '{' after a GUID.
- Do not use a custom format specifier at all; just provide a "to-string" function. To print a guid one needs a temporary buffer (less convenient, but still better than repeating the raw printf statement for GUID everywhere, like it is now).

I think something like a suffix for %p is a good idea. Like Linux kernel does:
https://www.kernel.org/doc/Documentation/core-api/printk-formats.rst
This avoids the warnings about the format string. I am proposing %pG for GUIDs.

I am almost done implementing this, incl. the GUID unification, and also with the
comments from v2.

- Oliver

#9 lore raw

Re: [PATCH v1 2/2] Add a module for the Boot Loader Interface

In reply to Oliver Steffen
Quoted 147 lines · On Tue, 28 Feb 2023 11:31:41 +0100 Oliver Steffen <[email protected]> wrote:
Quoted 121 lines · Daniel Kiper
Quoted 5 lines · Oliver Steffen

Thank you for the comments, Daniel.

Quoting Daniel Kiper (2023-02-15 19:27:03)

Quoted 2 lines · Oliver Steffen
Quoted 1 line

Add a new module named boot_loader_interface, which provides a

command

Quoted 5 lines
Quoted 3 lines

I would prefer something shorter than boot_loader_interface. bli?

Then bli it is.

Cool! Thanks!

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

with the same name. It implements a small but quite useful part of

the

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Boot Loader Interface [0]. This interface uses EFI variables for communication between the boot loader and the operating system.

This module sets two EFI variables under the vendor GUID 4a67b082-0a4c-41cf-b6c7-440b29bb8c4f:

- LoaderInfo: contains GRUB + <version number>. This allows the running operating system to identify the
boot

loader

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

used during boot.

- LoaderDevicePartUUID: contains the partition UUID of the EFI System Partition (ESP). This is used by systemd-gpt-auto-generator [1] to find the root partitions
(and

others

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

too), via partition type IDs [2].

This module is only available on EFI platforms.

[0] https://systemd.io/BOOT_LOADER_INTERFACE/
[1]

https://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html

Quoted 1 line
Quoted 1 line
Quoted 1 line

[2]

https://uapi-group.org/specifications/specs/discoverable_partitions_specification/

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
---
 grub-core/Makefile.core.def                |   6 +
 grub-core/commands/boot_loader_interface.c | 217

+++++++++++++++++++++

Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
 2 files changed, 223 insertions(+)
 create mode 100644 grub-core/commands/boot_loader_interface.c
(quoted diff) +0 -0

b/grub-core/Makefile.core.def

Quoted 13 lines
Quoted 13 lines

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

Quoted 10 lines

index ba967aac8..23455fb71 100644

grub-core/Makefile.core.def +3 -0
@@ -2547,3 +2547,9 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = boot_loader_interface;

s/boot_loader_interface/bli/?

Quoted 7 lines
Quoted 7 lines

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

Quoted 1 line
grub-core/Makefile.core.def +1 -0
+  efi = commands/boot_loader_interface.c;

Ditto and below if needed...

Review

Quoted 3 lines
grub-core/Makefile.core.def +2 -0
+  enable = efi;
+};
(quoted diff) +0 -0

b/grub-core/commands/boot_loader_interface.c

Quoted 9 lines
Quoted 9 lines

grub-core/commands/boot_loader_interface.c line 1

Quoted 6 lines

new file mode 100644
index 000000000..ccd7fa3d9

grub-core/commands/boot_loader_interface.c new file +1 -0
@@ -0,0 +1,217 @@
+/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/

Could you move this to the end of the file? Good example you can find

in

Quoted 4 lines
Quoted 1 line

the Xen project [1].

I personally do not care about this, it was just part of the file I used as a template. Is this wanted, or can we just drop it?

If you do not need it please drop it.

[...]

Quoted 4 lines
Quoted 4 lines

grub-core/commands/boot_loader_interface.c

Quoted 4 lines
grub-core/commands/boot_loader_interface.c new file +4 -0
+  guid = &entry.guid;
+  *part_uuid = grub_xasprintf (
+      "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+      grub_le_to_cpu32 (guid->data1), grub_le_to_cpu16

(guid->data2),

Quoted 1 line
Quoted 1 line

grub-core/commands/boot_loader_interface.c line 6

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      grub_le_to_cpu16 (guid->data3), guid->data4[0],

guid->data4[1],

Quoted 1 line
Quoted 1 line

grub-core/commands/boot_loader_interface.c line 7

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      guid->data4[2], guid->data4[3], guid->data4[4],

guid->data4[5],

Quoted 4 lines
Quoted 4 lines

grub-core/commands/boot_loader_interface.c line 94

Quoted 1 line
grub-core/commands/boot_loader_interface.c new file +1 -0
+      guid->data4[6], guid->data4[7]);

I think this is generic thing and can be converted into a function. Similar code is in the grub-core/commands/probe.c and

util/grub-probe.c.

Quoted 11 lines
Quoted 2 lines

It seems to me it is at least easy to make one function for the GRUB core code. Please do that. Of course in separate patch.

There are multiple representations of a GUID:
- grub_gpt_part_guid (gpt_partition.h)
- grub_efi_guid (efi/api.h)
- grub_efi_packed_guid (efi/api.h)

I would add a function for the first kind to gpt_partition.h and partmap/gpt.c that prints into a string buffer (length checked).

Quoted 9 lines
Quoted 5 lines

It would be nice to add a format specifier for GUIDs to the printf implementation,
but that only makes much sense (I think) if the GUID repesentations could be unified into one first, in a central place.

Could you do the unification and introduce format specifier for GUIDs?

Assume there is a common GUID struct.
Then we have the following options (AFAIK):
- add a format specifier for it, lets say '%g' or '%y'. This causes problems with the -Wformat checks. We could disable it, but that's a bad idea.
- use an additional qualifier for %p, for example %p{GUID}. This way gcc will not complain (%p takes void *). But it is ugly if one wants to print a '{' after a GUID.
- Do not use a custom format specifier at all; just provide a "to-string" function. To print a guid one needs a temporary buffer (less convenient, but still better than repeating the raw printf statement for GUID everywhere, like it is now).

Quoted 2 lines

Pick your poison... :-)
Or does anybody have a better idea?

What about doing similarly to what the kernel[1] does and have a %pG? This is similar to your second option without the brackets and accompanying issue. One issue with these both is that the argument must be cast to a void*, which might be a little confusing to the reader.

Here's the GCC issue[2] on adding extra format specifiers for the printf checking. Its 10 years old and apparently no progress, so doesn't seem like we'll get any help there.

Glenn

[1] https://www.kernel.org/doc/Documentation/printk-formats.txt
[2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47781