Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1qNxEr-0005G7-Ce for mharc-grub-devel@gnu.org; Mon, 24 Jul 2023 11:12:45 -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 1qNxEq-0005Fy-Hr for grub-devel@gnu.org; Mon, 24 Jul 2023 11:12:44 -0400 Received: from smtp-relay-internal-1.canonical.com ([185.125.188.123]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qNxEn-00071E-9c for grub-devel@gnu.org; Mon, 24 Jul 2023 11:12:43 -0400 Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 361073F17B for ; Mon, 24 Jul 2023 15:12:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1690211558; bh=O7tFCQ0CGlT3q1bzMjYo7kyd4dnVNf7ZEpA8886iF/A=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=tXBMbzyHDsSagHaKilh84W65TMNhi0vETdD+kbwR0bPDOTsOsl1VcuLTK0cm8L+J7 BOcpByhkbZCjdaR3LTNo+B/LNIT3vXy4W8zm1q4lHUBRESnyg4Zilz7OEPwwa1cOdd k2rFc4mIIcIo6MVlYqxtWouJflFxbMte89PTrSf1DxlZkTdqyu4mfi6HljN94rofKp xfn7W4OrZI7qbpCqCJ4bhj4UBA9ID+6NG1q04CW/S31h4nwoA+7ilEh+TxNG6tAQvO IdMyYRN4EXSETZxbL5nlBEvLle1kXgOc/l9zpwQyuooxEuO7kxlhIcrkmnFllxkRM1 RKOvoR5RgHtcQ== Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-987e47d2e81so394217066b.1 for ; Mon, 24 Jul 2023 08:12:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690211556; x=1690816356; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=O7tFCQ0CGlT3q1bzMjYo7kyd4dnVNf7ZEpA8886iF/A=; b=edCBs8KghOClv12EJSqlmYnrBBJEA/gwOqhydCJuT2gYsHbrkfvXa8kogcKHEvOY9G ZCH/pZp0na06SvxI+Ro/H7tfMZpkzPycKbOF1oe4g8YQNF3vaX3w8O6yTu7eMSeTzkCJ y+JYIPNwGZi4IhrryoDCbdGLfnEJCFZCyopMs2Rf+8Ux9cGicUl12PrJ+5P/kK2CBkW+ rA7+PN7XS6Cu1JPrbKS/A88l1Yxe22j6Q5yR/WAe+YtR2ELYv08aaeQ0cj7zOqfmHz67 oWePnUy9o1yGE6rC7tv40eS66NwFnCSVPvK/YGqJMLAiAhaZ6o4+yWfep4W2Pg4kEPkT dpoA== X-Gm-Message-State: ABy/qLbIzeiyQh6fcrp/ofk2h3+gQUsuczOKKVgHsFoKLMvZxeMZ7XIk Bh11vtvgZuINiHcTGuUWcnwPmqkSDAENrnf+uNtMVB9a+F1Rtcg4pDmrK7mP6bZs1OWovoRjQyp QRPl8SxXtLkJS3RavmVIo1+PPgeLLkZqa06PX X-Received: by 2002:a17:907:78c5:b0:973:daa0:2f6 with SMTP id kv5-20020a17090778c500b00973daa002f6mr10797514ejc.3.1690211556472; Mon, 24 Jul 2023 08:12:36 -0700 (PDT) X-Google-Smtp-Source: APBJJlGwrP+eCTVxH9gPqxJFGkw9d+uiYX5pDyvvnNyTg5CkxsupGxFAkHCWyzfWJEvX92tvPMO4GQ== X-Received: by 2002:a17:907:78c5:b0:973:daa0:2f6 with SMTP id kv5-20020a17090778c500b00973daa002f6mr10797496ejc.3.1690211556197; Mon, 24 Jul 2023 08:12:36 -0700 (PDT) Received: from jak-t14-g3.. ([2a02:908:2812:7d20:9b3e:59b2:370:6aa]) by smtp.gmail.com with ESMTPSA id l17-20020a1709061c5100b009888aa1da11sm6806724ejg.188.2023.07.24.08.12.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 08:12:35 -0700 (PDT) From: Julian Andres Klode To: grub-devel@gnu.org Cc: Julian Andres Klode Subject: [RFC PATCH] efi: Provide wrappers for load_image, start_image, unload_image Date: Mon, 24 Jul 2023 17:12:34 +0200 Message-Id: <20230724151234.4137071-1-julian.klode@canonical.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=185.125.188.123; envelope-from=julian.klode@canonical.com; helo=smtp-relay-internal-1.canonical.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: Mon, 24 Jul 2023 15:12:44 -0000 These can be used to register a different implementation later, for example, when shim provides a protocol with those functions. --- grub-core/kern/efi/efi.c | 56 ++++++++++++++++++++++++++++++ grub-core/loader/efi/chainloader.c | 13 +++---- grub-core/loader/efi/linux.c | 10 +++--- include/grub/efi/efi.h | 37 ++++++++++++++++++++ 4 files changed, 103 insertions(+), 13 deletions(-) diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c index a2afd8de9..cdc6f3605 100644 --- a/grub-core/kern/efi/efi.c +++ b/grub-core/kern/efi/efi.c @@ -1031,3 +1031,59 @@ grub_efi_compare_device_paths (const grub_efi_device_path_t *dp1, return 0; } + +static const grub_efi_loader_t *override_loader = NULL; +grub_err_t +grub_efi_register_loader (const grub_efi_loader_t *loader) +{ + if (override_loader != NULL) + return grub_error (GRUB_ERR_BUG, "trying to register different loader"); + override_loader = loader; + return GRUB_ERR_NONE; +} + +grub_err_t +grub_efi_unregister_loader (const grub_efi_loader_t *loader) +{ + if (loader != override_loader) + return grub_error (GRUB_ERR_BUG, "trying to unregister different loader"); + + override_loader = NULL; + return GRUB_ERR_NONE; +} + +grub_efi_status_t +grub_efi_load_image (grub_efi_boolean_t boot_policy, + grub_efi_handle_t parent_image_handle, + grub_efi_device_path_t *file_path, void *source_buffer, + grub_efi_uintn_t source_size, + grub_efi_handle_t *image_handle) +{ + if (override_loader != NULL) + return override_loader->load_image (boot_policy, parent_image_handle, + file_path, source_buffer, source_size, + image_handle); + return grub_efi_system_table->boot_services->load_image ( + boot_policy, parent_image_handle, file_path, source_buffer, source_size, + image_handle); +} + +grub_efi_status_t +grub_efi_start_image (grub_efi_handle_t image_handle, + grub_efi_uintn_t *exit_data_size, + grub_efi_char16_t **exit_data) +{ + if (override_loader != NULL) + return override_loader->start_image (image_handle, exit_data_size, + exit_data); + return grub_efi_system_table->boot_services->start_image ( + image_handle, exit_data_size, exit_data); +} + +grub_efi_status_t +grub_efi_unload_image (grub_efi_handle_t image_handle) +{ + if (override_loader != NULL) + return override_loader->unload_image (image_handle); + return grub_efi_system_table->boot_services->unload_image (image_handle); +} diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c index 1de98f783..eb833b678 100644 --- a/grub-core/loader/efi/chainloader.c +++ b/grub-core/loader/efi/chainloader.c @@ -50,14 +50,12 @@ grub_chainloader_unload (void *context) { grub_efi_handle_t image_handle = (grub_efi_handle_t) context; grub_efi_loaded_image_t *loaded_image; - grub_efi_boot_services_t *b; loaded_image = grub_efi_get_loaded_image (image_handle); if (loaded_image != NULL) grub_free (loaded_image->load_options); - b = grub_efi_system_table->boot_services; - b->unload_image (image_handle); + grub_efi_unload_image (image_handle); grub_dl_unref (my_mod); return GRUB_ERR_NONE; @@ -73,7 +71,7 @@ grub_chainloader_boot (void *context) grub_efi_char16_t *exit_data = NULL; b = grub_efi_system_table->boot_services; - status = b->start_image (image_handle, &exit_data_size, &exit_data); + status = grub_efi_start_image (image_handle, &exit_data_size, &exit_data); if (status != GRUB_EFI_SUCCESS) { if (exit_data) @@ -343,9 +341,8 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)), } #endif - status = b->load_image (0, grub_efi_image_handle, file_path, - boot_image, size, - &image_handle); + status = grub_efi_load_image (0, grub_efi_image_handle, file_path, + boot_image, size, &image_handle); if (status != GRUB_EFI_SUCCESS) { if (status == GRUB_EFI_OUT_OF_RESOURCES) @@ -422,7 +419,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)), b->free_pages (address, pages); if (image_handle != NULL) - b->unload_image (image_handle); + grub_efi_unload_image (image_handle); grub_dl_unref (my_mod); diff --git a/grub-core/loader/efi/linux.c b/grub-core/loader/efi/linux.c index 43c4e2d3d..0e575b4eb 100644 --- a/grub-core/loader/efi/linux.c +++ b/grub-core/loader/efi/linux.c @@ -204,9 +204,9 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) mempath[1].header.length = sizeof (grub_efi_device_path_t); b = grub_efi_system_table->boot_services; - status = b->load_image (0, grub_efi_image_handle, - (grub_efi_device_path_t *) mempath, - (void *) addr, size, &image_handle); + status = grub_efi_load_image (0, grub_efi_image_handle, + (grub_efi_device_path_t *)mempath, + (void *)addr, size, &image_handle); if (status != GRUB_EFI_SUCCESS) return grub_error (GRUB_ERR_BAD_OS, "cannot load image"); @@ -231,14 +231,14 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) (grub_uint8_t *) args, len, NULL); grub_dprintf ("linux", "starting image %p\n", image_handle); - status = b->start_image (image_handle, 0, NULL); + status = grub_efi_start_image (image_handle, 0, NULL); /* When successful, not reached */ grub_error (GRUB_ERR_BAD_OS, "start_image() returned 0x%" PRIxGRUB_EFI_UINTN_T, status); grub_efi_free_pages ((grub_addr_t) loaded_image->load_options, GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size)); unload: - b->unload_image (image_handle); + grub_efi_unload_image (image_handle); return grub_errno; } diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index 572f7135f..a6aab7c13 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -129,6 +129,43 @@ grub_err_t grub_arch_efi_linux_load_image_header(grub_file_t file, grub_err_t grub_arch_efi_linux_boot_image(grub_addr_t addr, grub_size_t size, char *args); +grub_efi_status_t +EXPORT_FUNC (grub_efi_load_image) (grub_efi_boolean_t boot_policy, + grub_efi_handle_t parent_image_handle, + grub_efi_device_path_t *file_path, + void *source_buffer, grub_efi_uintn_t source_size, + grub_efi_handle_t *image_handle); + +grub_efi_status_t +EXPORT_FUNC (grub_efi_start_image) (grub_efi_handle_t image_handle, + grub_efi_uintn_t *exit_data_size, + grub_efi_char16_t **exit_data); + +grub_efi_status_t +EXPORT_FUNC (grub_efi_unload_image) (grub_efi_handle_t image_handle); + +typedef struct grub_efi_loader +{ + grub_efi_status_t (*load_image) (grub_efi_boolean_t boot_policy, + grub_efi_handle_t parent_image_handle, + grub_efi_device_path_t *file_path, + void *source_buffer, + grub_efi_uintn_t source_size, + grub_efi_handle_t *image_handle); + + grub_efi_status_t (*start_image) (grub_efi_handle_t image_handle, + grub_efi_uintn_t *exit_data_size, + grub_efi_char16_t **exit_data); + + grub_efi_status_t (*unload_image) (grub_efi_handle_t image_handle); +} grub_efi_loader_t; + +grub_err_t +EXPORT_FUNC (grub_efi_register_loader) (const grub_efi_loader_t *loader); + +grub_err_t +EXPORT_FUNC (grub_efi_unregister_loader) (const grub_efi_loader_t *loader); + grub_addr_t grub_efi_section_addr (const char *section); void grub_efi_mm_init (void); -- 2.40.1