mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Johannes Schneider <johannes.schneider@leica-geosystems.com>
To: barebox@lists.infradead.org
Cc: Marco Felsch <m.felsch@pengutronix.de>,
	Johannes Schneider <johannes.schneider@leica-geosystems.com>
Subject: [PATCH v1 06/14] efi: loader: bootm: load only the PE image, not the whole file
Date: Sun,  4 Oct 2026 01:19:39 +0000	[thread overview]
Message-ID: <20261004011958.3255011-7-johannes.schneider@leica-geosystems.com> (raw)
In-Reply-To: <20261004011958.3255011-1-johannes.schneider@leica-geosystems.com>

With global.bootm.image pointing at a raw partition, bootm loads all
of it, and hands the trailing data to efiloader_load_image() as part
of the image.

Read the PE headers first and load only as much as efi_pe_image_size()
reports. This saves on boot time, since only the actually needed data
is loaded.

Files whose headers do not parse are loaded in full as before.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johannes Schneider <johannes.schneider@leica-geosystems.com>
---
 efi/loader/bootm.c | 56 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 53 insertions(+), 3 deletions(-)

diff --git a/efi/loader/bootm.c b/efi/loader/bootm.c
index 425e61fd70..18539565fd 100644
--- a/efi/loader/bootm.c
+++ b/efi/loader/bootm.c
@@ -35,6 +35,8 @@
 #include <efi/error.h>
 #include <efi/initrd.h>
 #include <efi/devicepath.h>
+#include <efi/loader/pe.h>
+#include <loadable.h>
 
 /**
  * copy_fdt() - Copy the device tree to a new location available to EFI
@@ -193,6 +195,48 @@ static efi_status_t efi_install_initrd(struct image_data *data,
 	return EFI_SUCCESS;
 }
 
+/* The image may sit in a much larger partition: load only the image */
+static const struct resource *efi_load_os(struct image_data *data,
+					  resource_size_t start,
+					  resource_size_t end)
+{
+	size_t len = SZ_64K, size = 0;
+	struct resource *res;
+	ssize_t now;
+	void *hdr;
+
+	if (data->os_res || !data->os || loadable_count(data->os) > 1)
+		return bootm_load_os(data, start, end);
+
+	hdr = xmalloc(len);
+	now = loadable_extract_into_buf(data->os, hdr, len, 0,
+					LOADABLE_EXTRACT_PARTIAL);
+	if (now > 0)
+		size = efi_pe_image_size(hdr, now);
+	free(hdr);
+
+	if (!size)
+		return bootm_load_os(data, start, end);
+	if (size > end - start + 1)
+		return ERR_PTR(-ENOSPC);
+
+	res = request_sdram_region("kernel", start, size, MEMTYPE_LOADER_CODE,
+				   MEMATTRS_RWX);
+	if (!res)
+		return ERR_PTR(-EBUSY);
+
+	now = loadable_extract_into_buf(data->os, (void *)res->start, size, 0,
+					LOADABLE_EXTRACT_PARTIAL);
+	if (now != size) {
+		release_sdram_region(res);
+		return ERR_PTR(now < 0 ? now : -EIO);
+	}
+
+	data->os_res = res;
+
+	return res;
+}
+
 static int efi_loader_bootm(struct image_data *data)
 {
 	const struct resource *os_res;
@@ -208,13 +252,20 @@ static int efi_loader_bootm(struct image_data *data)
 	int ret = 0;
 	void *fdt;
 	int flags = 0;
+	size_t size;
 
 	memory_bank_first_find_space(&start, &end);
 
-	os_res = bootm_load_os(data, start, end);
+	os_res = efi_load_os(data, start, end);
 	if (IS_ERR(os_res))
 		return PTR_ERR(os_res);
 
+	size = efi_pe_file_size((void *)os_res->start, resource_size(os_res));
+	if (!size) {
+		pr_err("%s is not a PE image\n", data->os_file);
+		return -EINVAL;
+	}
+
 	if (filetype_is_linux_efi_image(data->kernel_type)) {
 		const char *options;
 
@@ -262,8 +313,7 @@ static int efi_loader_bootm(struct image_data *data)
 		flags |= EFI_DRYRUN;
 
 	efiret = efiloader_load_image(false, efi_root, file_path,
-				(void *)os_res->start,
-				resource_size(os_res), &handle);
+				(void *)os_res->start, size, &handle);
 	if (efiret != EFI_SUCCESS) {
 		pr_err("Loading image failed\n");
 		goto out;
-- 
2.43.0




  parent reply	other threads:[~2026-10-04  1:21 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-04  1:19 [PATCH v1 00/14] efi: loader: boot Authenticode-signed UKIs Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 01/14] mfd: hgs-efi: do not claim the name of the EFI loader's device Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 02/14] efi: loader: file: report EFI_UNSUPPORTED for volumes without a filesystem Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 03/14] efi: loader: bootm: free the devicetree after installing it Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 04/14] efi: loader: provide EFI_DT_FIXUP_PROTOCOL Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 05/14] efi: loader: pe: add helpers for the image size and a named section Johannes Schneider
2026-10-04  1:19 ` Johannes Schneider [this message]
2026-10-04  1:19 ` [PATCH v1 07/14] efi: loader: bootm: pass the kernel command line to UKIs Johannes Schneider
2026-10-05 17:18   ` Ahmad Fatoum
2026-10-04  1:19 ` [PATCH v1 08/14] efi: loader: verify Authenticode signatures against built-in keys Johannes Schneider
2026-10-05  5:33   ` Ahmad Fatoum
2026-10-05  5:45     ` SCHNEIDER Johannes
2026-10-09  0:07       ` SCHNEIDER Johannes
2026-10-04  1:19 ` [PATCH v1 09/14] efi: loader: authenticode: add a fuzz test Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 10/14] efi: loader: authenticate LoadImage() images when signing is forced Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 11/14] efi: loader: file: expose no filesystem when signed images are forced Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 12/14] bootm: efi: boot signed EFI images " Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 13/14] efi: loader: bootm: install a devicetree for matching UKI devicetrees Johannes Schneider
2026-10-04  1:19 ` [PATCH v1 14/14] efi: loader: bootm: apply overlays carried by a UKI Johannes Schneider

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261004011958.3255011-7-johannes.schneider@leica-geosystems.com \
    --to=johannes.schneider@leica-geosystems.com \
    --cc=barebox@lists.infradead.org \
    --cc=m.felsch@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox