Skip to contentSkip to conversation

Add basic Boot Loader Interface support

PATCH Superseded Oliver Steffen 1 participant 9 messages Mar 14, 2023 Kbuild

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 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-generator [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.

This series also unifies the GUID implementations and introduces a printf format specifier for them: %pG.

[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/

v4:
- Place grub_utf8_to_utf16_alloc() in kern/misc.c and use it in kern/efi.c
- bli: rework partition GUID code, improve error reporting
- Add documentation for the bli module
- Address other comments

v3:
- https://mail.gnu.org/archive/html/grub-devel/2023-03/msg00008.html
- Unify GUID implementations for GPT and EFI
- Add printf format specifier for GUIDs, drop guid print function from v2.
- WIP/Please comment: Started extracting utf8->utf16 code. Where should this go?
- Address other comments

v2:
- https://mail.gnu.org/archive/html/grub-devel/2023-02/msg00099.html
- Addressed comments from Daniel
- Added a print function for gpt guids`
- Added integer overflow check in UTF16 conversion
- Added config drop-in file that loads the module on EFI

v1:
- https://mail.gnu.org/archive/html/grub-devel/2023-01/msg00104.html

Oliver Steffen (8):
efi: Add grub_efi_set_variable_with_attributes Unify GUID types
kern/misc: Add a format specifier GUIDs.
grub-core: Make use of guid printf format specifier types.h: Add GRUB_SSIZE_MAX
kern/misc, kern/efi: Extract UTF-8 to UTF-16 code Add a module for the Boot Loader Interface util/grub.d: Activate bli module on EFI

 Makefile.util.def                    |   6 +
 docs/grub.texi                       |  22 ++++
 grub-core/Makefile.core.def          |   6 +
 grub-core/commands/acpi.c            |   4 +-
 grub-core/commands/bli.c             | 166 +++++++++++++++++++++++++++
 grub-core/commands/efi/efifwsetup.c  |   4 +-
 grub-core/commands/efi/loadbios.c    |  14 +--
 grub-core/commands/efi/lsefi.c       |  17 +--
 grub-core/commands/efi/lsefisystab.c |  10 +-
 grub-core/commands/efi/lssal.c       |   4 +-
 grub-core/commands/efi/smbios.c      |  12 +-
 grub-core/commands/efi/tpm.c         |   6 +-
 grub-core/commands/probe.c           |   9 +-
 grub-core/disk/efi/efidisk.c         |   4 +-
 grub-core/efiemu/i386/pc/cfgtables.c |   6 +-
 grub-core/efiemu/main.c              |   4 +-
 grub-core/efiemu/runtime/efiemu.c    |  14 +--
 grub-core/kern/efi/acpi.c            |  12 +-
 grub-core/kern/efi/efi.c             |  86 +++++---------
 grub-core/kern/efi/fdt.c             |   2 +-
 grub-core/kern/efi/init.c            |   2 +-
 grub-core/kern/efi/sb.c              |   4 +-
 grub-core/kern/misc.c                | 118 +++++++++++++++----
 grub-core/loader/arm64/linux.c       |   4 +-
 grub-core/loader/efi/fdt.c           |   2 +-
 grub-core/loader/i386/xnu.c          |  13 +--
 grub-core/loader/ia64/efi/linux.c    |   2 +-
 grub-core/net/drivers/efi/efinet.c   |   4 +-
 grub-core/term/efi/console.c         |   2 +-
 grub-core/term/efi/serial.c          |   2 +-
 grub-core/video/efi_gop.c            |   8 +-
 grub-core/video/efi_uga.c            |   2 +-
 include/grub/efi/api.h               |  69 +++++------
 include/grub/efi/efi.h               |  20 ++--
 include/grub/efiemu/efiemu.h         |  10 +-
 include/grub/efiemu/runtime.h        |   2 +-
 include/grub/misc.h                  |   3 +
 include/grub/types.h                 |  11 ++
 util/grub.d/25_bli.in                |  25 ++++
 39 files changed, 483 insertions(+), 228 deletions(-)
 create mode 100644 grub-core/commands/bli.c
 create mode 100644 util/grub.d/25_bli.in
Signature · 1 line
2.39.2
#1 lore raw

PATCH v4 1/8 efi: Add grub_efi_set_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..03abf5531 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_efi_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..8e9a905a4 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_efi_uint32_t attributes);
+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.2
#2 lore raw

PATCH v4 2/8 Unify GUID types

In reply to Oliver Steffen

There are 3 implementations of a GUID in Grub. Replace them with a common one, placed in types.h.

It uses the "packed" flavor of the GUID structs, the alignment attribute is dropped, since it is not required.

---
 grub-core/commands/acpi.c            |  4 +-
 grub-core/commands/efi/efifwsetup.c  |  4 +-
 grub-core/commands/efi/loadbios.c    | 14 +++---
 grub-core/commands/efi/lsefi.c       |  4 +-
 grub-core/commands/efi/lsefisystab.c |  4 +-
 grub-core/commands/efi/lssal.c       |  4 +-
 grub-core/commands/efi/smbios.c      | 12 +++---
 grub-core/commands/efi/tpm.c         |  6 +--
 grub-core/disk/efi/efidisk.c         |  4 +-
 grub-core/efiemu/i386/pc/cfgtables.c |  6 +--
 grub-core/efiemu/main.c              |  4 +-
 grub-core/efiemu/runtime/efiemu.c    | 14 +++---
 grub-core/kern/efi/acpi.c            | 12 +++---
 grub-core/kern/efi/efi.c             | 22 +++++-----
 grub-core/kern/efi/fdt.c             |  2 +-
 grub-core/kern/efi/init.c            |  2 +-
 grub-core/kern/efi/sb.c              |  4 +-
 grub-core/loader/arm64/linux.c       |  4 +-
 grub-core/loader/efi/fdt.c           |  2 +-
 grub-core/loader/i386/xnu.c          |  4 +-
 grub-core/loader/ia64/efi/linux.c    |  2 +-
 grub-core/net/drivers/efi/efinet.c   |  4 +-
 grub-core/term/efi/console.c         |  2 +-
 grub-core/term/efi/serial.c          |  2 +-
 grub-core/video/efi_gop.c            |  8 ++--
 grub-core/video/efi_uga.c            |  2 +-
 include/grub/efi/api.h               | 64 ++++++++++------------------
 include/grub/efi/efi.h               | 16 +++----
 include/grub/efiemu/efiemu.h         | 10 ++---
 include/grub/efiemu/runtime.h        |  2 +-
 include/grub/types.h                 |  9 ++++
 31 files changed, 122 insertions(+), 131 deletions(-)
grub-core/commands/acpi.c +2 -2
index fda62f4ea..fb2fe8325 100644
@@ -759,8 +759,8 @@ grub_cmd_acpi (struct grub_extcmd_context *ctxt, int argc, char **args)
 
 #ifdef GRUB_MACHINE_EFI
   {
-    struct grub_efi_guid acpi = GRUB_EFI_ACPI_TABLE_GUID;
-    struct grub_efi_guid acpi20 = GRUB_EFI_ACPI_20_TABLE_GUID;
+    grub_guid_t acpi = GRUB_EFI_ACPI_TABLE_GUID;
+    grub_guid_t acpi20 = GRUB_EFI_ACPI_20_TABLE_GUID;
 
     efi_call_2 (grub_efi_system_table->boot_services->install_configuration_table,
       &acpi20, grub_acpi_get_rsdpv2 ());
grub-core/commands/efi/efifwsetup.c +2 -2
index de66c3035..704f9d352 100644
@@ -38,7 +38,7 @@ grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
   grub_efi_uint64_t os_indications = GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
   grub_err_t status;
   grub_size_t oi_size;
-  static grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+  static grub_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
 
   if (argc >= 1 && grub_strcmp(args[0], "--is-supported") == 0)
     return !efifwsetup_is_supported ();
@@ -72,7 +72,7 @@ efifwsetup_is_supported (void)
 {
   grub_efi_uint64_t *os_indications_supported = NULL;
   grub_size_t oi_size = 0;
-  static grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+  static grub_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
   grub_efi_boolean_t ret = 0;
 
   grub_efi_get_variable ("OsIndicationsSupported", &global, &oi_size,
grub-core/commands/efi/loadbios.c +7 -7
index 574e41046..8f6b0ecfc 100644
@@ -27,9 +27,9 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static grub_efi_guid_t acpi_guid = GRUB_EFI_ACPI_TABLE_GUID;
-static grub_efi_guid_t acpi2_guid = GRUB_EFI_ACPI_20_TABLE_GUID;
-static grub_efi_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID;
+static grub_guid_t acpi_guid = GRUB_EFI_ACPI_TABLE_GUID;
+static grub_guid_t acpi2_guid = GRUB_EFI_ACPI_20_TABLE_GUID;
+static grub_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID;
 
 #define EBDA_SEG_ADDR	0x40e
 #define LOW_MEM_ADDR	0x413
@@ -105,15 +105,15 @@ fake_bios_data (int use_rom)
   smbios = 0;
   for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
     {
-      grub_efi_packed_guid_t *guid =
+      grub_guid_t *guid =
 	&grub_efi_system_table->configuration_table[i].vendor_guid;
 
-      if (! grub_memcmp (guid, &acpi2_guid, sizeof (grub_efi_guid_t)))
+      if (! grub_memcmp (guid, &acpi2_guid, sizeof (grub_guid_t)))
 	{
 	  acpi = grub_efi_system_table->configuration_table[i].vendor_table;
 	  grub_dprintf ("efi", "ACPI2: %p\n", acpi);
 	}
-      else if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_efi_guid_t)))
+      else if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_guid_t)))
 	{
 	  void *t;
 
@@ -122,7 +122,7 @@ fake_bios_data (int use_rom)
 	    acpi = t;
 	  grub_dprintf ("efi", "ACPI: %p\n", t);
 	}
-      else if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_efi_guid_t)))
+      else if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_guid_t)))
 	{
 	  smbios = grub_efi_system_table->configuration_table[i].vendor_table;
 	  grub_dprintf ("efi", "SMBIOS: %p\n", smbios);
grub-core/commands/efi/lsefi.c +2 -2
index c304d25cc..ff3e71a91 100644
@@ -31,7 +31,7 @@ GRUB_MOD_LICENSE ("GPLv3+");
 
 struct known_protocol
 {
-  grub_efi_guid_t guid;
+  grub_guid_t guid;
   const char *name;
 } known_protocols[] =
   {
@@ -96,7 +96,7 @@ grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)),
       grub_efi_handle_t handle = handles[i];
       grub_efi_status_t status;
       grub_efi_uintn_t num_protocols;
-      grub_efi_packed_guid_t **protocols;
+      grub_guid_t **protocols;
       grub_efi_device_path_t *dp;
 
       grub_printf ("Handle %p\n", handle);
grub-core/commands/efi/lsefisystab.c +2 -2
index ac3299b7f..eff8c41f3 100644
@@ -29,7 +29,7 @@ GRUB_MOD_LICENSE ("GPLv3+");
 
 struct guid_mapping
 {
-  grub_efi_guid_t guid;
+  grub_guid_t guid;
   const char *name;
 };
 
@@ -104,7 +104,7 @@ grub_cmd_lsefisystab (struct grub_command *cmd __attribute__ ((unused)),
 
       for (j = 0; j < ARRAY_SIZE (guid_mappings); j++)
 	if (grub_memcmp (&guid_mappings[j].guid, &t->vendor_guid,
-			 sizeof (grub_efi_guid_t)) == 0)
+			 sizeof (grub_guid_t)) == 0)
 	  grub_printf ("   %s", guid_mappings[j].name);
 
       grub_printf ("\n");
grub-core/commands/efi/lssal.c +2 -2
index 5084ddd8b..7b683dc62 100644
@@ -139,12 +139,12 @@ grub_cmd_lssal (struct grub_command *cmd __attribute__ ((unused)),
   const grub_efi_system_table_t *st = grub_efi_system_table;
   grub_efi_configuration_table_t *t = st->configuration_table;
   unsigned int i;
-  grub_efi_packed_guid_t guid = GRUB_EFI_SAL_TABLE_GUID;
+  grub_guid_t guid = GRUB_EFI_SAL_TABLE_GUID;
 
   for (i = 0; i < st->num_table_entries; i++)
     {
       if (grub_memcmp (&guid, &t->vendor_guid,
-		       sizeof (grub_efi_packed_guid_t)) == 0)
+		       sizeof (grub_guid_t)) == 0)
 	{
 	  disp_sal (t->vendor_table);
 	  return GRUB_ERR_NONE;
grub-core/commands/efi/smbios.c +6 -6
index 75202d5aa..d77239732 100644
@@ -26,14 +26,14 @@ struct grub_smbios_eps *
 grub_machine_smbios_get_eps (void)
 {
   unsigned i;
-  static grub_efi_packed_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID;
+  static grub_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID;
 
   for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
     {
-      grub_efi_packed_guid_t *guid =
+      grub_guid_t *guid =
 	&grub_efi_system_table->configuration_table[i].vendor_guid;
 
-      if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_efi_packed_guid_t)))
+      if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_guid_t)))
 	return (struct grub_smbios_eps *)
 	  grub_efi_system_table->configuration_table[i].vendor_table;
     }
@@ -45,14 +45,14 @@ struct grub_smbios_eps3 *
 grub_machine_smbios_get_eps3 (void)
 {
   unsigned i;
-  static grub_efi_packed_guid_t smbios3_guid = GRUB_EFI_SMBIOS3_TABLE_GUID;
+  static grub_guid_t smbios3_guid = GRUB_EFI_SMBIOS3_TABLE_GUID;
 
   for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
     {
-      grub_efi_packed_guid_t *guid =
+      grub_guid_t *guid =
 	&grub_efi_system_table->configuration_table[i].vendor_guid;
 
-      if (! grub_memcmp (guid, &smbios3_guid, sizeof (grub_efi_packed_guid_t)))
+      if (! grub_memcmp (guid, &smbios3_guid, sizeof (grub_guid_t)))
 	return (struct grub_smbios_eps3 *)
 	  grub_efi_system_table->configuration_table[i].vendor_table;
     }
grub-core/commands/efi/tpm.c +3 -3
index ae09c1bf8..f3d142452 100644
@@ -30,9 +30,9 @@
 
 typedef TCG_PCR_EVENT grub_tpm_event_t;
 
-static grub_efi_guid_t tpm_guid = EFI_TPM_GUID;
-static grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID;
-static grub_efi_guid_t cc_measurement_guid = GRUB_EFI_CC_MEASUREMENT_PROTOCOL_GUID;
+static grub_guid_t tpm_guid = EFI_TPM_GUID;
+static grub_guid_t tpm2_guid = EFI_TPM2_GUID;
+static grub_guid_t cc_measurement_guid = GRUB_EFI_CC_MEASUREMENT_PROTOCOL_GUID;
 
 static grub_efi_handle_t *grub_tpm_handle;
 static grub_uint8_t grub_tpm_version;
grub-core/disk/efi/efidisk.c +2 -2
index d7540040e..7d89fc470 100644
@@ -37,7 +37,7 @@ struct grub_efidisk_data
 };
 
 /* GUID.  */
-static grub_efi_guid_t block_io_guid = GRUB_EFI_BLOCK_IO_GUID;
+static grub_guid_t block_io_guid = GRUB_EFI_BLOCK_IO_GUID;
 
 static struct grub_efidisk_data *fd_devices;
 static struct grub_efidisk_data *hd_devices;
@@ -319,7 +319,7 @@ name_devices (struct grub_efidisk_data *devices)
 	  == GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE)
 	{
 	  grub_efi_vendor_device_path_t *vendor = (grub_efi_vendor_device_path_t *) dp;
-	  const struct grub_efi_guid apple = GRUB_EFI_VENDOR_APPLE_GUID;
+	  const grub_guid_t apple = GRUB_EFI_VENDOR_APPLE_GUID;
 
 	  if (vendor->header.length == sizeof (*vendor)
 	      && grub_memcmp (&vendor->vendor_guid, &apple,
grub-core/efiemu/i386/pc/cfgtables.c +3 -3
index 1098f0b79..056ec0bc9 100644
@@ -29,9 +29,9 @@ grub_machine_efiemu_init_tables (void)
 {
   void *table;
   grub_err_t err;
-  static grub_efi_guid_t smbios = GRUB_EFI_SMBIOS_TABLE_GUID;
-  static grub_efi_guid_t acpi20 = GRUB_EFI_ACPI_20_TABLE_GUID;
-  static grub_efi_guid_t acpi = GRUB_EFI_ACPI_TABLE_GUID;
+  static grub_guid_t smbios = GRUB_EFI_SMBIOS_TABLE_GUID;
+  static grub_guid_t acpi20 = GRUB_EFI_ACPI_20_TABLE_GUID;
+  static grub_guid_t acpi = GRUB_EFI_ACPI_TABLE_GUID;
 
   err = grub_efiemu_unregister_configuration_table (smbios);
   if (err)
grub-core/efiemu/main.c +2 -2
index a81934725..e7037f4ed 100644
@@ -80,7 +80,7 @@ grub_efiemu_unload (void)
 
 /* Remove previously registered table from the list */
 grub_err_t
-grub_efiemu_unregister_configuration_table (grub_efi_guid_t guid)
+grub_efiemu_unregister_configuration_table (grub_guid_t guid)
 {
   struct grub_efiemu_configuration_table *cur, *prev;
 
@@ -136,7 +136,7 @@ grub_efiemu_register_prepare_hook (grub_err_t (*hook) (void *data),
    or with a hook
 */
 grub_err_t
-grub_efiemu_register_configuration_table (grub_efi_guid_t guid,
+grub_efiemu_register_configuration_table (grub_guid_t guid,
 					  void * (*get_table) (void *data),
 					  void (*unload) (void *data),
 					  void *data)
grub-core/efiemu/runtime/efiemu.c +7 -7
index 5db1f347b..63c09da78 100644
@@ -66,7 +66,7 @@ efiemu_convert_pointer (grub_efi_uintn_t debug_disposition,
 
 grub_efi_status_t
 efiemu_get_variable (grub_efi_char16_t *variable_name,
-		     const grub_efi_guid_t *vendor_guid,
+		     const grub_guid_t *vendor_guid,
 		     grub_efi_uint32_t *attributes,
 		     grub_efi_uintn_t *data_size,
 		     void *data);
@@ -74,11 +74,11 @@ efiemu_get_variable (grub_efi_char16_t *variable_name,
 grub_efi_status_t
 efiemu_get_next_variable_name (grub_efi_uintn_t *variable_name_size,
 			       grub_efi_char16_t *variable_name,
-			       grub_efi_guid_t *vendor_guid);
+			       grub_guid_t *vendor_guid);
 
 grub_efi_status_t
 efiemu_set_variable (grub_efi_char16_t *variable_name,
-		     const grub_efi_guid_t *vendor_guid,
+		     const grub_guid_t *vendor_guid,
 		     grub_efi_uint32_t attributes,
 		     grub_efi_uintn_t data_size,
 		     void *data);
@@ -416,7 +416,7 @@ EFI_FUNC (efiemu_convert_pointer) (grub_efi_uintn_t debug_disposition,
 
 /* Find variable by name and GUID. */
 static struct efi_variable *
-find_variable (const grub_efi_guid_t *vendor_guid,
+find_variable (const grub_guid_t *vendor_guid,
 	       grub_efi_char16_t *variable_name)
 {
   grub_uint8_t *ptr;
@@ -438,7 +438,7 @@ find_variable (const grub_efi_guid_t *vendor_guid,
 
 grub_efi_status_t
 EFI_FUNC (efiemu_get_variable) (grub_efi_char16_t *variable_name,
-				const grub_efi_guid_t *vendor_guid,
+				const grub_guid_t *vendor_guid,
 				grub_efi_uint32_t *attributes,
 				grub_efi_uintn_t *data_size,
 				void *data)
@@ -464,7 +464,7 @@ EFI_FUNC (efiemu_get_variable) (grub_efi_char16_t *variable_name,
 grub_efi_status_t EFI_FUNC
 (efiemu_get_next_variable_name) (grub_efi_uintn_t *variable_name_size,
 				 grub_efi_char16_t *variable_name,
-				 grub_efi_guid_t *vendor_guid)
+				 grub_guid_t *vendor_guid)
 {
   struct efi_variable *efivar;
   LOG ('l');
@@ -503,7 +503,7 @@ grub_efi_status_t EFI_FUNC
 
 grub_efi_status_t
 EFI_FUNC (efiemu_set_variable) (grub_efi_char16_t *variable_name,
-				const grub_efi_guid_t *vendor_guid,
+				const grub_guid_t *vendor_guid,
 				grub_efi_uint32_t attributes,
 				grub_efi_uintn_t data_size,
 				void *data)
grub-core/kern/efi/acpi.c +6 -6
index 74f8cd1a9..461c77c33 100644
@@ -26,14 +26,14 @@ struct grub_acpi_rsdp_v10 *
 grub_machine_acpi_get_rsdpv1 (void)
 {
   unsigned i;
-  static grub_efi_packed_guid_t acpi_guid = GRUB_EFI_ACPI_TABLE_GUID;
+  static grub_guid_t acpi_guid = GRUB_EFI_ACPI_TABLE_GUID;
 
   for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
     {
-      grub_efi_packed_guid_t *guid =
+      grub_guid_t *guid =
 	&grub_efi_system_table->configuration_table[i].vendor_guid;
 
-      if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_efi_packed_guid_t)))
+      if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_guid_t)))
 	return (struct grub_acpi_rsdp_v10 *)
 	  grub_efi_system_table->configuration_table[i].vendor_table;
     }
@@ -44,14 +44,14 @@ struct grub_acpi_rsdp_v20 *
 grub_machine_acpi_get_rsdpv2 (void)
 {
   unsigned i;
-  static grub_efi_packed_guid_t acpi20_guid = GRUB_EFI_ACPI_20_TABLE_GUID;
+  static grub_guid_t acpi20_guid = GRUB_EFI_ACPI_20_TABLE_GUID;
 
   for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
     {
-      grub_efi_packed_guid_t *guid =
+      grub_guid_t *guid =
 	&grub_efi_system_table->configuration_table[i].vendor_guid;
 
-      if (! grub_memcmp (guid, &acpi20_guid, sizeof (grub_efi_packed_guid_t)))
+      if (! grub_memcmp (guid, &acpi20_guid, sizeof (grub_guid_t)))
 	return (struct grub_acpi_rsdp_v20 *)
 	  grub_efi_system_table->configuration_table[i].vendor_table;
     }
grub-core/kern/efi/efi.c +11 -11
index 03abf5531..21540b739 100644
@@ -35,12 +35,12 @@ grub_efi_handle_t grub_efi_image_handle;
 /* The pointer to a system table. Filled in by the startup code.  */
 grub_efi_system_table_t *grub_efi_system_table;
 
-static grub_efi_guid_t console_control_guid = GRUB_EFI_CONSOLE_CONTROL_GUID;
-static grub_efi_guid_t loaded_image_guid = GRUB_EFI_LOADED_IMAGE_GUID;
-static grub_efi_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+static grub_guid_t console_control_guid = GRUB_EFI_CONSOLE_CONTROL_GUID;
+static grub_guid_t loaded_image_guid = GRUB_EFI_LOADED_IMAGE_GUID;
+static grub_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
 
 void *
-grub_efi_locate_protocol (grub_efi_guid_t *protocol, void *registration)
+grub_efi_locate_protocol (grub_guid_t *protocol, void *registration)
 {
   void *interface;
   grub_efi_status_t status;
@@ -58,7 +58,7 @@ grub_efi_locate_protocol (grub_efi_guid_t *protocol, void *registration)
    from the heap.  */
 grub_efi_handle_t *
 grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
-			grub_efi_guid_t *protocol,
+			grub_guid_t *protocol,
 			void *search_key,
 			grub_efi_uintn_t *num_handles)
 {
@@ -97,7 +97,7 @@ grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
 
 void *
 grub_efi_open_protocol (grub_efi_handle_t handle,
-			grub_efi_guid_t *protocol,
+			grub_guid_t *protocol,
 			grub_efi_uint32_t attributes)
 {
   grub_efi_boot_services_t *b;
@@ -118,7 +118,7 @@ grub_efi_open_protocol (grub_efi_handle_t handle,
 }
 
 grub_efi_status_t
-grub_efi_close_protocol (grub_efi_handle_t handle, grub_efi_guid_t *protocol)
+grub_efi_close_protocol (grub_efi_handle_t handle, grub_guid_t *protocol)
 {
   grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
 
@@ -201,7 +201,7 @@ grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size,
 }
 
 grub_err_t
-grub_efi_set_variable_with_attributes (const char *var, const grub_efi_guid_t *guid,
+grub_efi_set_variable_with_attributes (const char *var, const grub_guid_t *guid,
 		      void *data, grub_size_t datasize, grub_efi_uint32_t attributes)
 {
   grub_efi_status_t status;
@@ -229,7 +229,7 @@ grub_efi_set_variable_with_attributes (const char *var, const grub_efi_guid_t *g
 }
 
 grub_err_t
-grub_efi_set_variable (const char *var, const grub_efi_guid_t *guid,
+grub_efi_set_variable (const char *var, const grub_guid_t *guid,
 		      void *data, grub_size_t datasize)
 {
   return grub_efi_set_variable_with_attributes (var, guid, data, datasize, 
@@ -240,7 +240,7 @@ grub_efi_set_variable (const char *var, const grub_efi_guid_t *guid,
 
 grub_efi_status_t
 grub_efi_get_variable_with_attributes (const char *var,
-				       const grub_efi_guid_t *guid,
+				       const grub_guid_t *guid,
 				       grub_size_t *datasize_out,
 				       void **data_out,
 				       grub_efi_uint32_t *attributes)
@@ -295,7 +295,7 @@ grub_efi_get_variable_with_attributes (const char *var,
 }
 
 grub_efi_status_t
-grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid,
+grub_efi_get_variable (const char *var, const grub_guid_t *guid,
 		       grub_size_t *datasize_out, void **data_out)
 {
   return grub_efi_get_variable_with_attributes (var, guid, datasize_out, data_out, NULL);
grub-core/kern/efi/fdt.c +1 -1
index 24f955289..8fcf43f1b 100644
@@ -24,7 +24,7 @@ void *
 grub_efi_get_firmware_fdt (void)
 {
   grub_efi_configuration_table_t *tables;
-  static grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
+  static grub_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
   void *firmware_fdt = NULL;
   unsigned int i;
 
grub-core/kern/efi/init.c +1 -1
index b67bc73a1..6ac017754 100644
@@ -36,7 +36,7 @@ static grub_efi_char16_t stack_chk_fail_msg[] =
   L"* GRUB: ABORTED!!! *\r\n"
   L"* GRUB: REBOOTING IN 5 SECONDS... *\r\n";
 
-static grub_efi_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
+static grub_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
 
 /*
  * Don't put this on grub_efi_init()'s local stack to avoid it
grub-core/kern/efi/sb.c +2 -2
index db42c2539..80cfa0888 100644
@@ -30,7 +30,7 @@
 #include <grub/types.h>
 #include <grub/verify.h>
 
-static grub_efi_guid_t shim_lock_guid = GRUB_EFI_SHIM_LOCK_GUID;
+static grub_guid_t shim_lock_guid = GRUB_EFI_SHIM_LOCK_GUID;
 
 /*
  * Determine whether we're in secure boot mode.
@@ -41,7 +41,7 @@ static grub_efi_guid_t shim_lock_guid = GRUB_EFI_SHIM_LOCK_GUID;
 grub_uint8_t
 grub_efi_get_secureboot (void)
 {
-  static grub_efi_guid_t efi_variable_guid = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+  static grub_guid_t efi_variable_guid = GRUB_EFI_GLOBAL_VARIABLE_GUID;
   grub_efi_status_t status;
   grub_efi_uint32_t attr = 0;
   grub_size_t size = 0;
grub-core/loader/arm64/linux.c +2 -2
index 48ab34a25..b3951b2c2 100644
@@ -52,8 +52,8 @@ static struct grub_linux_initrd_context initrd_ctx = {0, 0, 0};
 static grub_efi_handle_t initrd_lf2_handle = NULL;
 static bool initrd_use_loadfile2 = false;
 
-static grub_efi_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID;
-static grub_efi_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+static grub_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID;
+static grub_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
 
 static initrd_media_device_path_t initrd_lf2_device_path = {
   {
grub-core/loader/efi/fdt.c +1 -1
index 35a3be2e5..439964b9c 100644
@@ -86,7 +86,7 @@ grub_err_t
 grub_fdt_install (void)
 {
   grub_efi_boot_services_t *b;
-  static grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
+  static grub_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
   grub_efi_status_t status;
 
   if (fdt == NULL && loaded_fdt == NULL)
grub-core/loader/i386/xnu.c +2 -2
index 4c88ce5e0..93ba4476d 100644
@@ -48,7 +48,7 @@ grub_uint32_t grub_xnu_entry_point, grub_xnu_arg1, grub_xnu_stack;
 /* Aliases set for some tables. */
 struct tbl_alias
 {
-  grub_efi_guid_t guid;
+  grub_guid_t guid;
   const char *name;
 };
 
@@ -694,7 +694,7 @@ grub_cpu_xnu_fill_devicetree (grub_uint64_t *fsbfreq_out)
     {
       void *ptr;
       struct grub_xnu_devtree_key *curkey;
-      grub_efi_packed_guid_t guid;
+      grub_guid_t guid;
       char guidbuf[64];
 
       /* Retrieve current key. */
grub-core/loader/ia64/efi/linux.c +1 -1
index fb9b961f7..3dd2e8236 100644
@@ -106,7 +106,7 @@ query_fpswa (void)
   grub_efi_boot_services_t *bs;
   grub_efi_status_t status;
   grub_efi_uintn_t size;
-  static const grub_efi_guid_t fpswa_protocol =
+  static const grub_guid_t fpswa_protocol =
     { 0xc41b6531, 0x97b9, 0x11d3,
       {0x9a, 0x29, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d} };
 
grub-core/net/drivers/efi/efinet.c +2 -2
index 5adf5f40f..e1955ce90 100644
@@ -27,8 +27,8 @@
 GRUB_MOD_LICENSE ("GPLv3+");
 
 /* GUID.  */
-static grub_efi_guid_t net_io_guid = GRUB_EFI_SIMPLE_NETWORK_GUID;
-static grub_efi_guid_t pxe_io_guid = GRUB_EFI_PXE_GUID;
+static grub_guid_t net_io_guid = GRUB_EFI_SIMPLE_NETWORK_GUID;
+static grub_guid_t pxe_io_guid = GRUB_EFI_PXE_GUID;
 
 static grub_err_t
 send_card_buffer (struct grub_net_card *dev,
grub-core/term/efi/console.c +1 -1
index 532948a8e..53c33872b 100644
@@ -353,7 +353,7 @@ grub_console_getkeystatus (struct grub_term_input *term)
 static grub_err_t
 grub_efi_console_input_init (struct grub_term_input *term)
 {
-  static grub_efi_guid_t text_input_ex_guid =
+  static grub_guid_t text_input_ex_guid =
     GRUB_EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
 
   if (grub_efi_is_finished)
grub-core/term/efi/serial.c +1 -1
index 4c94723c5..b0fa73634 100644
@@ -31,7 +31,7 @@
 #include <grub/i18n.h>
 
 /* GUID.  */
-static grub_efi_guid_t serial_io_guid = GRUB_EFI_SERIAL_IO_GUID;
+static grub_guid_t serial_io_guid = GRUB_EFI_SERIAL_IO_GUID;
 
 static void
 do_real_config (struct grub_serial_port *port)
grub-core/video/efi_gop.c +4 -4
index 7a5054631..86e21d7df 100644
@@ -32,10 +32,10 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static grub_efi_guid_t graphics_output_guid = GRUB_EFI_GOP_GUID;
-static grub_efi_guid_t active_edid_guid = GRUB_EFI_EDID_ACTIVE_GUID;
-static grub_efi_guid_t discovered_edid_guid = GRUB_EFI_EDID_DISCOVERED_GUID;
-static grub_efi_guid_t efi_var_guid = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+static grub_guid_t graphics_output_guid = GRUB_EFI_GOP_GUID;
+static grub_guid_t active_edid_guid = GRUB_EFI_EDID_ACTIVE_GUID;
+static grub_guid_t discovered_edid_guid = GRUB_EFI_EDID_DISCOVERED_GUID;
+static grub_guid_t efi_var_guid = GRUB_EFI_GLOBAL_VARIABLE_GUID;
 static struct grub_efi_gop *gop;
 static unsigned old_mode;
 static int restore_needed;
grub-core/video/efi_uga.c +1 -1
index e74d6c235..a931eecfb 100644
@@ -32,7 +32,7 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-static grub_efi_guid_t uga_draw_guid = GRUB_EFI_UGA_DRAW_GUID;
+static grub_guid_t uga_draw_guid = GRUB_EFI_UGA_DRAW_GUID;
 static struct grub_efi_uga_draw_protocol *uga;
 static grub_uint64_t uga_fb;
 static grub_uint32_t uga_pitch;
include/grub/efi/api.h +23 -41

191 lines not on this page · raw message

include/grub/efi/efi.h +8 -8
index 8e9a905a4..466ac3867 100644
@@ -25,18 +25,18 @@
 #include <grub/efi/api.h>
 
 /* Functions.  */
-void *EXPORT_FUNC(grub_efi_locate_protocol) (grub_efi_guid_t *protocol,
+void *EXPORT_FUNC(grub_efi_locate_protocol) (grub_guid_t *protocol,
 					     void *registration);
 grub_efi_handle_t *
 EXPORT_FUNC(grub_efi_locate_handle) (grub_efi_locate_search_type_t search_type,
-				     grub_efi_guid_t *protocol,
+				     grub_guid_t *protocol,
 				     void *search_key,
 				     grub_efi_uintn_t *num_handles);
 void *EXPORT_FUNC(grub_efi_open_protocol) (grub_efi_handle_t handle,
-					   grub_efi_guid_t *protocol,
+					   grub_guid_t *protocol,
 					   grub_efi_uint32_t attributes);
 grub_efi_status_t
-EXPORT_FUNC(grub_efi_close_protocol) (grub_efi_handle_t handle, grub_efi_guid_t *protocol);
+EXPORT_FUNC(grub_efi_close_protocol) (grub_efi_handle_t handle, grub_guid_t *protocol);
 int EXPORT_FUNC(grub_efi_set_text_mode) (int on);
 void EXPORT_FUNC(grub_efi_stall) (grub_efi_uintn_t microseconds);
 void *
@@ -77,23 +77,23 @@ grub_err_t EXPORT_FUNC (grub_efi_set_virtual_address_map) (grub_efi_uintn_t memo
 							   grub_efi_uint32_t descriptor_version,
 							   grub_efi_memory_descriptor_t *virtual_map);
 grub_efi_status_t EXPORT_FUNC (grub_efi_get_variable_with_attributes) (const char *variable,
-								       const grub_efi_guid_t *guid,
+								       const grub_guid_t *guid,
 								       grub_size_t *datasize_out,
 								       void **data_out,
 								       grub_efi_uint32_t *attributes);
 grub_efi_status_t EXPORT_FUNC (grub_efi_get_variable) (const char *variable,
-						       const grub_efi_guid_t *guid,
+						       const grub_guid_t *guid,
 						       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,
+				     const grub_guid_t *guid,
 				     void *data,
 				     grub_size_t datasize,
 				     grub_efi_uint32_t attributes);
 grub_err_t
 EXPORT_FUNC (grub_efi_set_variable) (const char *var,
-				     const grub_efi_guid_t *guid,
+				     const grub_guid_t *guid,
 				     void *data,
 				     grub_size_t datasize);
 int
include/grub/efiemu/efiemu.h +5 -5
index 5325e5839..caf0b505f 100644
@@ -176,26 +176,26 @@ grub_err_t grub_efiemu_loadcore_load (void);
 struct grub_efiemu_configuration_table
 {
   struct grub_efiemu_configuration_table *next;
-  grub_efi_guid_t guid;
+  grub_guid_t guid;
   void * (*get_table) (void *data);
   void (*unload) (void *data);
   void *data;
 };
 struct grub_efiemu_configuration_table32
 {
-  grub_efi_packed_guid_t vendor_guid;
+  grub_guid_t vendor_guid;
   grub_efi_uint32_t vendor_table;
 } GRUB_PACKED;
 typedef struct grub_efiemu_configuration_table32 grub_efiemu_configuration_table32_t;
 struct grub_efiemu_configuration_table64
 {
-  grub_efi_packed_guid_t vendor_guid;
+  grub_guid_t vendor_guid;
   grub_efi_uint64_t vendor_table;
 } GRUB_PACKED;
 typedef struct grub_efiemu_configuration_table64 grub_efiemu_configuration_table64_t;
-grub_err_t grub_efiemu_unregister_configuration_table (grub_efi_guid_t guid);
+grub_err_t grub_efiemu_unregister_configuration_table (grub_guid_t guid);
 grub_err_t
-grub_efiemu_register_configuration_table (grub_efi_guid_t guid,
+grub_efiemu_register_configuration_table (grub_guid_t guid,
 					  void * (*get_table) (void *data),
 					  void (*unload) (void *data),
 					  void *data);
include/grub/efiemu/runtime.h +1 -1
index 36d2dedf4..c9ad9fdfa 100644
@@ -29,7 +29,7 @@ struct grub_efiemu_ptv_rel
 
 struct efi_variable
 {
-  grub_efi_packed_guid_t guid;
+  grub_guid_t guid;
   grub_uint32_t namelen;
   grub_uint32_t size;
   grub_efi_uint32_t attributes;
include/grub/types.h +9 -0
index 6d5dc5cda..c56ce9820 100644
@@ -365,4 +365,13 @@ static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val)
 # define grub_absolute_pointer(val) ((void *) (val))
 #endif
 
+struct grub_guid
+{
+  grub_uint32_t data1;
+  grub_uint16_t data2;
+  grub_uint16_t data3;
+  grub_uint8_t data4[8];
+} GRUB_PACKED;
+typedef struct grub_guid grub_guid_t;
+
 #endif /* ! GRUB_TYPES_HEADER */
Signature · 1 line
2.39.2
#3 lore raw

PATCH v4 3/8 kern/misc: Add a format specifier GUIDs.

In reply to Oliver Steffen

Extend the printf format specifier for pointers (%p) to accept a suffix specifier G to print GUIDs: %pG can be used to print grub_guid structs. This does not interfere with the -Wformat checking of gcc. Note that the data type is not checked though (%p accepts void*).

---
 grub-core/kern/misc.c | 86 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 65 insertions(+), 21 deletions(-)
grub-core/kern/misc.c +65 -21
index dfae4f9d7..1a8aff543 100644
@@ -24,6 +24,7 @@
 #include <grub/term.h>
 #include <grub/env.h>
 #include <grub/i18n.h>
+#include <grub/types.h>
 
 union printf_arg
 {
@@ -34,7 +35,8 @@ union printf_arg
     {
       INT, LONG, LONGLONG,
       UNSIGNED_INT = 3, UNSIGNED_LONG, UNSIGNED_LONGLONG,
-      STRING
+      STRING,
+      GUID
     } type;
   long long ll;
 };
@@ -772,6 +774,9 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
       switch (c)
 	{
 	case 'p':
+	  if (*(fmt) == 'G')
+	    ++fmt;
+	  /* Fall through. */
 	case 'x':
 	case 'X':
 	case 'u':
@@ -885,6 +890,10 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
 	    args->ptr[curn].type = UNSIGNED_LONGLONG;
 	  else
 	    args->ptr[curn].type = UNSIGNED_INT;
+	  if (*(fmt) == 'G') {
+	    args->ptr[curn].type = GUID;
+	    ++fmt;
+	  }
 	  break;
 	case 's':
 	  args->ptr[curn].type = STRING;
@@ -931,6 +940,12 @@ parse_printf_args (const char *fmt0, struct printf_args *args, va_list args_in)
 	else
 	  args->ptr[n].ll = va_arg (args_in, unsigned int);
 	break;
+      case GUID:
+	if (sizeof (void *) == sizeof (long long))
+	  args->ptr[n].ll = va_arg (args_in, long long);
+	else
+	  args->ptr[n].ll = va_arg (args_in, unsigned int);
+	break;
       }
 }
 
@@ -943,6 +958,27 @@ write_char (char *str, grub_size_t *count, grub_size_t max_len, unsigned char ch
   (*count)++;
 }
 
+static void
+write_number (char *str, grub_size_t *count, grub_size_t max_len, grub_size_t format1,
+	     char rightfill, char zerofill, char c, long long value)
+{
+  char tmp[32];
+  const char *p = tmp;
+  grub_size_t len;
+  grub_size_t fill;
+
+  len = grub_lltoa (tmp, c, value) - tmp;
+  fill = len < format1 ? format1 - len : 0;
+  if (! rightfill)
+    while (fill--)
+      write_char (str, count, max_len, zerofill);
+  while (*p)
+    write_char (str, count, max_len, *p++);
+  if (rightfill)
+    while (fill--)
+      write_char (str, count, max_len, zerofill);
+}
+
 static int
 grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
 		     struct printf_args *args)
@@ -1025,31 +1061,39 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
       switch (c)
 	{
 	case 'p':
-	  write_char (str, &count, max_len, '0');
-	  write_char (str, &count, max_len, 'x');
-	  c = 'x';
+	  if (*(fmt) == 'G')
+	    {
+	      ++fmt;
+	      grub_guid_t *guid = (grub_guid_t *) curarg;
+	      write_number (str, &count, max_len, 8, 0, '0', 'x', grub_le_to_cpu32 (guid->data1));
+	      write_char (str, &count, max_len, '-');
+	      write_number (str, &count, max_len, 4, 0, '0', 'x', grub_le_to_cpu16 (guid->data2));
+	      write_char (str, &count, max_len, '-');
+	      write_number (str, &count, max_len, 4, 0, '0', 'x', grub_le_to_cpu16 (guid->data3));
+	      write_char (str, &count, max_len, '-');
+	      write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[0]);
+	      write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[1]);
+	      write_char (str, &count, max_len, '-');
+	      write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[2]);
+	      write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[3]);
+	      write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[4]);
+	      write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[5]);
+	      write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[6]);
+	      write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[7]);
+	      break;
+	    }
+	  else
+	    {
+	      write_char (str, &count, max_len, '0');
+	      write_char (str, &count, max_len, 'x');
+	      c = 'x';
+	    }
 	  /* Fall through. */
 	case 'x':
 	case 'X':
 	case 'u':
 	case 'd':
-	  {
-	    char tmp[32];
-	    const char *p = tmp;
-	    grub_size_t len;
-	    grub_size_t fill;
-
-	    len = grub_lltoa (tmp, c, curarg) - tmp;
-	    fill = len < format1 ? format1 - len : 0;
-	    if (! rightfill)
-	      while (fill--)
-		write_char (str, &count, max_len, zerofill);
-	    while (*p)
-	      write_char (str, &count, max_len, *p++);
-	    if (rightfill)
-	      while (fill--)
-		write_char (str, &count, max_len, zerofill);
-	  }
+	  write_number (str, &count, max_len, format1, rightfill, zerofill, c, curarg);
 	  break;
 
 	case 'c':
Signature · 1 line
2.39.2
#4 lore raw

PATCH v4 4/8 grub-core: Make use of guid printf format specifier

In reply to Oliver Steffen

Use the new printf format specifier %pG.

Fixes the text representation of GUIDs in the output of the lsefisystab command.

---
 grub-core/commands/efi/lsefi.c       | 13 +------------
 grub-core/commands/efi/lsefisystab.c |  6 +-----
 grub-core/commands/probe.c           |  9 +--------
 grub-core/kern/efi/efi.c             | 28 ++--------------------------
 grub-core/loader/i386/xnu.c          |  9 ++-------
 5 files changed, 7 insertions(+), 58 deletions(-)
grub-core/commands/efi/lsefi.c +1 -12
index ff3e71a91..f9c18df2b 100644
@@ -123,18 +123,7 @@ grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)),
 	  if (k < ARRAY_SIZE (known_protocols))
 	    grub_printf ("  %s\n", known_protocols[k].name);
 	  else
-	    grub_printf ("  %08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
-			 protocols[j]->data1,
-			 protocols[j]->data2,
-			 protocols[j]->data3,
-			 (unsigned) protocols[j]->data4[0],
-			 (unsigned) protocols[j]->data4[1],
-			 (unsigned) protocols[j]->data4[2],
-			 (unsigned) protocols[j]->data4[3],
-			 (unsigned) protocols[j]->data4[4],
-			 (unsigned) protocols[j]->data4[5],
-			 (unsigned) protocols[j]->data4[6],
-			 (unsigned) protocols[j]->data4[7]);
+	    grub_printf ("  %pG\n", protocols[j]);
 	}
 
     }
grub-core/commands/efi/lsefisystab.c +1 -5
index eff8c41f3..79ccee12a 100644
@@ -96,11 +96,7 @@ grub_cmd_lsefisystab (struct grub_command *cmd __attribute__ ((unused)),
 
       grub_printf ("%p  ", t->vendor_table);
 
-      grub_printf ("%08x-%04x-%04x-",
-		   t->vendor_guid.data1, t->vendor_guid.data2,
-		   t->vendor_guid.data3);
-      for (j = 0; j < 8; j++)
-	grub_printf ("%02x", t->vendor_guid.data4[j]);
+      grub_printf ("%pG", &t->vendor_guid);
 
       for (j = 0; j < ARRAY_SIZE (guid_mappings); j++)
 	if (grub_memcmp (&guid_mappings[j].guid, &t->vendor_guid,
grub-core/commands/probe.c +1 -8
index 9a80ea54f..4eee8f5ac 100644
@@ -130,14 +130,7 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
 		  return grub_errno;
 		}
 	      guid = &entry.guid;
-	      grub_snprintf (val, sizeof(val),
-			     "%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]);
+	      grub_snprintf (val, sizeof(val), "%pG", guid);
 	    }
 	  else if (grub_strcmp(dev->disk->partition->partmap->name, "msdos") == 0)
 	    {
grub-core/kern/efi/efi.c +2 -26
index 21540b739..8df861a59 100644
@@ -544,20 +544,7 @@ static void
 dump_vendor_path (const char *type, grub_efi_vendor_device_path_t *vendor)
 {
   grub_uint32_t vendor_data_len = vendor->header.length - sizeof (*vendor);
-  grub_printf ("/%sVendor(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)[%x: ",
-	       type,
-	       (unsigned) vendor->vendor_guid.data1,
-	       (unsigned) vendor->vendor_guid.data2,
-	       (unsigned) vendor->vendor_guid.data3,
-	       (unsigned) vendor->vendor_guid.data4[0],
-	       (unsigned) vendor->vendor_guid.data4[1],
-	       (unsigned) vendor->vendor_guid.data4[2],
-	       (unsigned) vendor->vendor_guid.data4[3],
-	       (unsigned) vendor->vendor_guid.data4[4],
-	       (unsigned) vendor->vendor_guid.data4[5],
-	       (unsigned) vendor->vendor_guid.data4[6],
-	       (unsigned) vendor->vendor_guid.data4[7],
-	       vendor_data_len);
+  grub_printf ("/%sVendor(%pG)[%x: ", type, &vendor->vendor_guid, vendor_data_len);
   if (vendor->header.length > sizeof (*vendor))
     {
       grub_uint32_t i;
@@ -928,18 +915,7 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
 	      {
 		grub_efi_protocol_device_path_t *proto
 		  = (grub_efi_protocol_device_path_t *) dp;
-		grub_printf ("/Protocol(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)",
-			     (unsigned) proto->guid.data1,
-			     (unsigned) proto->guid.data2,
-			     (unsigned) proto->guid.data3,
-			     (unsigned) proto->guid.data4[0],
-			     (unsigned) proto->guid.data4[1],
-			     (unsigned) proto->guid.data4[2],
-			     (unsigned) proto->guid.data4[3],
-			     (unsigned) proto->guid.data4[4],
-			     (unsigned) proto->guid.data4[5],
-			     (unsigned) proto->guid.data4[6],
-			     (unsigned) proto->guid.data4[7]);
+		grub_printf ("/Protocol(%pG)",&proto->guid);
 	      }
 	      break;
 	    default:
grub-core/loader/i386/xnu.c +2 -7
index 93ba4476d..4e31ce99a 100644
@@ -726,13 +726,8 @@ grub_cpu_xnu_fill_devicetree (grub_uint64_t *fsbfreq_out)
 #endif
 
       /* The name of key for new table. */
-      grub_snprintf (guidbuf, sizeof (guidbuf), "%08x-%04x-%04x-%02x%02x-",
-		     guid.data1, guid.data2, guid.data3, guid.data4[0],
-		     guid.data4[1]);
-      for (j = 2; j < 8; j++)
-	grub_snprintf (guidbuf + grub_strlen (guidbuf),
-		       sizeof (guidbuf) - grub_strlen (guidbuf),
-		       "%02x", guid.data4[j]);
+      grub_snprintf (guidbuf, sizeof (guidbuf), "%pG", &guid);
+
       /* For some reason GUID has to be in uppercase. */
       for (j = 0; guidbuf[j] ; j++)
 	if (guidbuf[j] >= 'a' && guidbuf[j] <= 'f')
Signature · 1 line
2.39.2
#5 lore raw

PATCH v4 5/8 types.h: Add GRUB_SSIZE_MAX

In reply to Oliver Steffen

In the same way as GRUB_SIZE_MAX, add GRUB_SSIZE_MAX.

---
 include/grub/types.h | 2 ++
 1 file changed, 2 insertions(+)
include/grub/types.h +2 -0
index c56ce9820..9f43a5580 100644
@@ -122,6 +122,7 @@ typedef grub_uint64_t	grub_size_t;
 typedef grub_int64_t	grub_ssize_t;
 
 # define GRUB_SIZE_MAX 18446744073709551615UL
+# define GRUB_SSIZE_MAX 9223372036854775807L
 
 # if GRUB_CPU_SIZEOF_LONG == 8
 #  define PRIxGRUB_SIZE	 "lx"
@@ -140,6 +141,7 @@ typedef grub_uint32_t	grub_size_t;
 typedef grub_int32_t	grub_ssize_t;
 
 # define GRUB_SIZE_MAX 4294967295UL
+# define GRUB_SSIZE_MAX 2147483647L
 
 # define PRIxGRUB_SIZE	"x"
 # define PRIxGRUB_ADDR	"x"
Signature · 1 line
2.39.2
#6 lore raw

PATCH v4 6/8 kern/misc, kern/efi: Extract UTF-8 to UTF-16 code

In reply to Oliver Steffen

Create a new function for UTF-8 to UTF-16 conversion called grub_utf8_to_utf16_alloc() in the grub-code/kern/misc.c and replace charset conversion code used in some places in the EFI code. It is modeled after the grub_utf8_to_ucs4_alloc() like functions in charset.h. It can't live in charset.h, because it needs to be reachable from the kern/efi code.

Add a check for integer overflow and remove redundant NUL-termination.

---
 grub-core/kern/efi/efi.c | 21 ++++++---------------
 grub-core/kern/misc.c    | 32 ++++++++++++++++++++++++++++++++
 include/grub/misc.h      |  3 +++
 3 files changed, 41 insertions(+), 15 deletions(-)
grub-core/kern/efi/efi.c +6 -15
index 8df861a59..8fc3edfae 100644
@@ -207,15 +207,11 @@ grub_efi_set_variable_with_attributes (const char *var, const grub_guid_t *guid,
   grub_efi_status_t status;
   grub_efi_runtime_services_t *r;
   grub_efi_char16_t *var16;
-  grub_size_t len, len16;
 
-  len = grub_strlen (var);
-  len16 = len * GRUB_MAX_UTF16_PER_UTF8;
-  var16 = grub_calloc (len16 + 1, sizeof (var16[0]));
-  if (!var16)
+  grub_utf8_to_utf16_alloc (var, &var16, NULL);
+
+  if (var16 == NULL)
     return grub_errno;
-  len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
-  var16[len16] = 0;
 
   r = grub_efi_system_table->runtime_services;
 
@@ -250,18 +246,13 @@ grub_efi_get_variable_with_attributes (const char *var,
   grub_efi_runtime_services_t *r;
   grub_efi_char16_t *var16;
   void *data;
-  grub_size_t len, len16;
 
   *data_out = NULL;
   *datasize_out = 0;
 
-  len = grub_strlen (var);
-  len16 = len * GRUB_MAX_UTF16_PER_UTF8;
-  var16 = grub_calloc (len16 + 1, sizeof (var16[0]));
-  if (!var16)
-    return GRUB_EFI_OUT_OF_RESOURCES;
-  len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
-  var16[len16] = 0;
+  grub_utf8_to_utf16_alloc (var, &var16, NULL);
+  if (var16 == NULL)
+    return grub_errno;
 
   r = grub_efi_system_table->runtime_services;
 
grub-core/kern/misc.c +32 -0
index 1a8aff543..b25a65863 100644
@@ -25,6 +25,7 @@
 #include <grub/env.h>
 #include <grub/i18n.h>
 #include <grub/types.h>
+#include <grub/charset.h>
 
 union printf_arg
 {
@@ -1323,6 +1324,37 @@ grub_fatal (const char *fmt, ...)
   grub_abort ();
 }
 
+grub_ssize_t
+grub_utf8_to_utf16_alloc (const char *str8, grub_uint16_t **utf16_msg, grub_uint16_t **last_position)
+{
+  grub_size_t len;
+  grub_size_t len16;
+
+  len = grub_strlen (str8);
+
+  /* Check for integer overflow */
+  if (len > GRUB_SSIZE_MAX / GRUB_MAX_UTF16_PER_UTF8 - 1)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("string too large"));
+      *utf16_msg = NULL;
+      return -1;
+    }
+
+  len16 = len * GRUB_MAX_UTF16_PER_UTF8;
+
+  *utf16_msg = grub_calloc (len16 + 1, sizeof (*utf16_msg[0]));
+  if (*utf16_msg == NULL)
+    return -1;
+
+  len16 = grub_utf8_to_utf16 (*utf16_msg, len16, (grub_uint8_t *) str8, len, NULL);
+
+  if (last_position)
+    *last_position = *utf16_msg + len16;
+
+  return len16;
+}
+
+
 #if BOOT_TIME_STATS
 
 #include <grub/time.h>
include/grub/misc.h +3 -0
index 6c9d56de9..1b35a167f 100644
@@ -529,4 +529,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
 
 #define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) - __builtin_clzll (n) - 1)
 
+grub_ssize_t
+EXPORT_FUNC(grub_utf8_to_utf16_alloc) (const char *str8, grub_uint16_t **utf16_msg, grub_uint16_t **last_position);
+
 #endif /* ! GRUB_MISC_HEADER */
Signature · 1 line
2.39.2
#7 lore raw

PATCH v4 7/8 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/

---
 docs/grub.texi              |  22 +++++
 grub-core/Makefile.core.def |   6 ++
 grub-core/commands/bli.c    | 166 ++++++++++++++++++++++++++++++++++++
 include/grub/efi/api.h      |   5 ++
 4 files changed, 199 insertions(+)
 create mode 100644 grub-core/commands/bli.c
docs/grub.texi +22 -0
index 659885388..e63cad8d9 100644
@@ -4291,6 +4291,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
+* bli::                         Set Boot Loader Interface EFI variables
 * boot::                        Start up your operating system
 * cat::                         Show the contents of a file
 * clear::                       Clear the screen
@@ -4471,6 +4472,27 @@ Print a block list (@pxref{Block list syntax}) for @var{file}.
 @end deffn
 
 
+@node bli
+@subsection bli
+
+@deffn Command bli
+Initialize the @url{https://systemd.io/BOOT_LOADER_INTERFACE/, Boot Loader Interface}.
+
+This command sets two EFI variables under the vendor GUID
+@code{4a67b082-0a4c-41cf-b6c7-440b29bb8c4f}:
+
+@code{LoaderDevicePartUUID} is set to the Partition UUID of the EFI System Partition used during boot.
+This is needed for automatic partition discovery as described by the 
+@url{https://uapi-group.org/specifications/specs/discoverable_partitions_specification/,
+Discoverable Partitions Specification}.
+
+@code{LoaderInfo} is set to the string @code{Grub} followed by the version number.  This is used by
+user-space tools to identify the boot loader that was used.
+
+This command is only available on UEFI platforms.
+@end deffn
+
+
 @node boot
 @subsection boot
 
grub-core/Makefile.core.def +6 -0
index 71093a100..cdfa2d101 100644
@@ -2548,3 +2548,9 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = bli;
+  efi = commands/bli.c;
+  enable = efi;
+};
grub-core/commands/bli.c new file +166 -0

166 lines not on this page · raw message

include/grub/efi/api.h +5 -0
index a6bd74b96..d5b4f8c8e 100644
@@ -374,6 +374,11 @@
     { 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68 } \
   }
 
+#define GRUB_EFI_VENDOR_BOOT_LOADER_INTERFACE_GUID \
+  { 0x4a67b082, 0x0a4c, 0x41cf, \
+    {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f } \
+  }
+
 struct grub_efi_sal_system_table
 {
   grub_uint32_t signature;
Signature · 1 line
2.39.2
#8 lore raw

PATCH v4 8/8 util/grub.d: Activate bli module on EFI

In reply to Oliver Steffen

Add a new configuration drop-in file that loads the bli module and runs the command if booting on the EFI platform.

---
 Makefile.util.def     |  6 ++++++
 util/grub.d/25_bli.in | 25 +++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100644 util/grub.d/25_bli.in
Makefile.util.def +6 -0
index beaef1168..d90e59b94 100644
@@ -510,6 +510,12 @@ script = {
   condition = COND_HOST_LINUX;
 };
 
+script = {
+  name = '25_bli';
+  common = util/grub.d/25_bli.in;
+  installdir = grubconf;
+};
+
 script = {
   name = '30_os-prober';
   common = util/grub.d/30_os-prober.in;
util/grub.d/25_bli.in new file +25 -0
new file mode 100644
index 000000000..ba0f466f0
@@ -0,0 +1,25 @@
+#!/usr/bin/sh
+set -e
+
+# grub-mkconfig helper script.
+# Copyright (C) 2023  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/>.
+
+cat << EOF
+if [ "\$grub_platform" = "efi" ]; then
+  insmod bli
+  bli
+fi
+EOF
Signature · 1 line
2.39.2