Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pc3PG-0007Wm-N8 for mharc-grub-devel@gnu.org; Tue, 14 Mar 2023 08:05:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pc3PE-0007VE-OL for grub-devel@gnu.org; Tue, 14 Mar 2023 08:05:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pc3PC-00089O-CZ for grub-devel@gnu.org; Tue, 14 Mar 2023 08:05:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678795525; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ksutd9oHppH5M0gB2cx5SGugvIwPf2j0l2jFKeTQ3QM=; b=T14gXJscPqRYtnalqzHZzWJGXJaOqr62ku8liRfA/k+TC4+QsnGF+nP27l/vuD5TdXBTMj juYN/ZIMxholQd5BJzSWOb4LRT+x1oOw6GdaOQ0L7pOJkn2Pp0kblQ+u6xHa8UDYVN/xRK m2YdMVYJYsnPxHVWgzTcOHRQ/S1v0AI= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-304-92M7qxw4PMG9d1sB_JwwCw-1; Tue, 14 Mar 2023 08:05:24 -0400 X-MC-Unique: 92M7qxw4PMG9d1sB_JwwCw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4647B3C218AC; Tue, 14 Mar 2023 12:05:24 +0000 (UTC) Received: from osteffen-laptop.redhat.com (unknown [10.39.193.107]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4748447507A; Tue, 14 Mar 2023 12:05:23 +0000 (UTC) From: Oliver Steffen To: grub-devel@gnu.org Cc: Daniel Kiper , Gerd Hoffmann , Javier Martinez Canillas , Glenn Washburn , Oliver Steffen Subject: [PATCH v4 7/8] Add a module for the Boot Loader Interface Date: Tue, 14 Mar 2023 13:04:57 +0100 Message-Id: <20230314120458.117123-8-osteffen@redhat.com> In-Reply-To: <20230314120458.117123-1-osteffen@redhat.com> References: <20230314120458.117123-1-osteffen@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Received-SPF: pass client-ip=170.10.133.124; envelope-from=osteffen@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Mar 2023 12:05:29 -0000 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 + . 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/ Signed-off-by: Oliver Steffen --- 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 diff --git a/docs/grub.texi b/docs/grub.texi index 659885388..e63cad8d9 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -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 diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 71093a100..cdfa2d101 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -2548,3 +2548,9 @@ module = { common = commands/i386/wrmsr.c; enable = x86; }; + +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..3ce0161a8 --- /dev/null +++ b/grub-core/commands/bli.c @@ -0,0 +1,166 @@ +/* + * 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 . + * + * Implementation of the Boot Loader Interface. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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_("error opening device: %s"), device_name); + goto fail; + } + + disk = grub_disk_open (device->disk->name); + if (disk == NULL) + { + status = grub_error (grub_errno, N_("error 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_efi_str_variable (const char *name, const grub_guid_t *guid, + const char *value) +{ + grub_efi_char16_t *value_16; + grub_ssize_t len16; + grub_err_t status; + + len16 = grub_utf8_to_utf16_alloc (value, &value_16, NULL); + + if (len16 < 0) + return grub_errno; + + status = grub_efi_set_variable_with_attributes (name, guid, + (void *) value_16, (len16 + 1) * sizeof (value_16[0]), + GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS | GRUB_EFI_VARIABLE_RUNTIME_ACCESS); + + grub_free (value_16); + + 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 = set_efi_str_variable ("LoaderDevicePartUUID", &bli_vendor_guid, part_uuid); + else + grub_error (status, N_("unable to determine partition UUID of boot device")); + + grub_free (part_uuid); + grub_free (device_name); + return status; +} + +static grub_err_t +grub_cmd_bli (grub_extcmd_context_t ctxt __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) +{ + grub_err_t status; + + status = set_efi_str_variable ("LoaderInfo", &bli_vendor_guid, PACKAGE_STRING); + if (status != GRUB_ERR_NONE) + return status; + + status = set_loader_device_part_uuid (); + if (status != GRUB_ERR_NONE) + return status; + + return GRUB_ERR_NONE; +} + +static grub_extcmd_t cmd; + +GRUB_MOD_INIT (bli) +{ + cmd = grub_register_extcmd ("bli", grub_cmd_bli, 0, NULL, + N_("Set EFI variables according to Boot Loader Interface spec."), NULL); +} + +GRUB_MOD_FINI (bli) +{ + grub_unregister_extcmd (cmd); +} + diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h index a6bd74b96..d5b4f8c8e 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.39.2