[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v6 8/9] Add a module for the Boot Loader Interface
From: |
Oliver Steffen |
Subject: |
[PATCH v6 8/9] Add a module for the Boot Loader Interface |
Date: |
Fri, 14 Apr 2023 16:04:01 +0200 |
Add a new module named bli. 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.
When loaded, 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 available on EFI platforms only. bli relies on the
part_gpt module which has to be loaded beforehand to make the GPT
partitions discoverable.
[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/
Signed-off-by: Oliver Steffen <osteffen@redhat.com>
---
grub-core/Makefile.core.def | 6 ++
grub-core/commands/bli.c | 122 ++++++++++++++++++++++++++++++++++++
include/grub/efi/api.h | 5 ++
3 files changed, 133 insertions(+)
create mode 100644 grub-core/commands/bli.c
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 801df7c21..b77375857 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -2561,3 +2561,9 @@ module = {
common = commands/memtools.c;
condition = COND_MM_DEBUG;
};
+
+module = {
+ name = bli;
+ efi = commands/bli.c;
+ enable = efi;
+};
diff --git a/grub-core/commands/bli.c b/grub-core/commands/bli.c
new file mode 100644
index 000000000..1ce2caa33
--- /dev/null
+++ b/grub-core/commands/bli.c
@@ -0,0 +1,122 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * 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/>.
+ *
+ * Implementation of the Boot Loader Interface.
+ */
+
+#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>
+#include <grub/mm.h>
+#include <grub/partition.h>
+#include <grub/types.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#define MODNAME "bli"
+
+static const grub_guid_t bli_vendor_guid =
GRUB_EFI_VENDOR_BOOT_LOADER_INTERFACE_GUID;
+
+static grub_err_t
+get_part_uuid (const char *device_name, char **part_uuid)
+{
+ grub_device_t device;
+ grub_err_t status = GRUB_ERR_NONE;
+ grub_disk_t disk = NULL;
+ struct grub_gpt_partentry entry;
+
+ device = grub_device_open (device_name);
+ if (device == NULL)
+ {
+ status = grub_error (grub_errno, N_("cannot opening device: %s"),
device_name);
+ goto fail;
+ }
+
+ disk = grub_disk_open (device->disk->name);
+ if (disk == NULL)
+ {
+ status = grub_error (grub_errno, N_("cannot opening disk: %s"),
device_name);
+ return status;
+ }
+
+ if (grub_strcmp (device->disk->partition->partmap->name, "gpt") != 0)
+ {
+ status = grub_error (GRUB_ERR_BAD_PART_TABLE,
+ N_("this is not a GPT partition table: %s"),
device_name);
+ goto fail;
+ }
+
+ if (grub_disk_read (disk, device->disk->partition->offset,
+ device->disk->partition->index, sizeof (entry), &entry)
!= GRUB_ERR_NONE)
+ {
+ status = grub_error (grub_errno, N_("read error: %s"), device_name);
+ goto fail;
+ }
+
+ *part_uuid = grub_xasprintf ("%pG", &entry.guid);
+ if (*part_uuid == NULL)
+ status = grub_errno;
+
+ fail:
+ grub_disk_close (disk);
+ grub_device_close (device);
+
+ return status;
+}
+
+static grub_err_t
+set_loader_device_part_uuid (void)
+{
+ grub_efi_loaded_image_t *image;
+ char *device_name;
+ grub_err_t status = GRUB_ERR_NONE;
+ char *part_uuid = NULL;
+
+ image = grub_efi_get_loaded_image (grub_efi_image_handle);
+ if (image == NULL)
+ return grub_error (GRUB_ERR_BAD_DEVICE, N_("unable to find boot device"));
+
+ device_name = grub_efidisk_get_device_name (image->device_handle);
+ if (device_name == NULL)
+ return grub_error (GRUB_ERR_BAD_DEVICE, N_("unable to find boot device"));
+
+ status = get_part_uuid (device_name, &part_uuid);
+
+ if (status == GRUB_ERR_NONE)
+ status = grub_efi_set_variable_to_string ("LoaderDevicePartUUID",
&bli_vendor_guid, part_uuid,
+
GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
+ |
GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
+ else
+ grub_error (status, N_("unable to determine partition UUID of boot
device"));
+
+ grub_free (part_uuid);
+ grub_free (device_name);
+ return status;
+}
+
+GRUB_MOD_INIT (bli)
+{
+ grub_efi_set_variable_to_string ("LoaderInfo", &bli_vendor_guid,
PACKAGE_STRING,
+ GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
+ | GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
+ set_loader_device_part_uuid ();
+}
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
index 78cc81959..696c273d3 100644
--- a/include/grub/efi/api.h
+++ b/include/grub/efi/api.h
@@ -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;
--
2.40.0
- [PATCH v6 0/9] Add basic Boot Loader Interface support, Oliver Steffen, 2023/04/14
- [PATCH v6 1/9] efi: Add grub_efi_set_variable_with_attributes(), Oliver Steffen, 2023/04/14
- [PATCH v6 2/9] Unify GUID types, Oliver Steffen, 2023/04/14
- [PATCH v6 5/9] types.h: Add GRUB_SSIZE_MAX, Oliver Steffen, 2023/04/14
- [PATCH v6 3/9] kern/misc: Add a format specifier GUIDs, Oliver Steffen, 2023/04/14
- [PATCH v6 4/9] grub-core: Make use of guid printf format specifier, Oliver Steffen, 2023/04/14
- [PATCH v6 9/9] util/grub.d: Activate bli module on EFI, Oliver Steffen, 2023/04/14
- [PATCH v6 7/9] efi: Add grub_efi_set_variable_to_string(), Oliver Steffen, 2023/04/14
- [PATCH v6 8/9] Add a module for the Boot Loader Interface,
Oliver Steffen <=
- [PATCH v6 6/9] kern/misc, kern/efi: Extract UTF-8 to UTF-16 code, Oliver Steffen, 2023/04/14