mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: chalianis1@gmail.com
To: s.hauer@pengutronix.de, a.fatoum@barebox.org
Cc: barebox@lists.infradead.org, Chali Anis <chalianis1@gmail.com>
Subject: [PATCH v3 10/11] efi: payload: bootm: add x86 efi stub boot support.
Date: Tue, 16 Sep 2025 00:01:54 -0400	[thread overview]
Message-ID: <20250916040155.814159-10-chalianis1@gmail.com> (raw)
In-Reply-To: <20250916040155.814159-1-chalianis1@gmail.com>

From: Chali Anis <chalianis1@gmail.com>

implement the efi stub boot on x86, this code is tested on qemu,
and dell latitude 7490. the EFI_ALLOCATE_MAX_PAGES cause an xfuncs
issue on real hardware so I transformed it to allocate any pages.

Signed-off-by: Chali Anis <chalianis1@gmail.com>
---
 efi/payload/bootm.c | 143 +++++++++++++-------------------------------
 1 file changed, 40 insertions(+), 103 deletions(-)

diff --git a/efi/payload/bootm.c b/efi/payload/bootm.c
index ce225ab949c9..3d2eaf915f4c 100644
--- a/efi/payload/bootm.c
+++ b/efi/payload/bootm.c
@@ -133,101 +133,6 @@ static int efi_load_file_image(const char *file,
 	return ret;
 }
 
-typedef void(*handover_fn)(void *image, struct efi_system_table *table,
-		struct x86_setup_header *header);
-
-static inline void linux_efi_handover(efi_handle_t handle,
-		struct x86_setup_header *header)
-{
-	handover_fn handover;
-	uintptr_t addr;
-
-	addr = header->code32_start + header->handover_offset;
-	if (IS_ENABLED(CONFIG_X86_64))
-		addr += 512;
-
-	handover = efi_phys_to_virt(addr);
-	handover(handle, efi_sys_table, header);
-}
-
-static int do_bootm_efi(struct image_data *data)
-{
-	void *tmp;
-	void *initrd = NULL;
-	size_t size;
-	efi_handle_t handle;
-	int ret;
-	const char *options;
-	struct efi_loaded_image *loaded_image;
-	struct x86_setup_header *image_header, *boot_header;
-
-	ret = efi_load_file_image(data->os_file, &loaded_image, &handle);
-	if (ret)
-		return ret;
-
-	image_header = (struct x86_setup_header *)loaded_image->image_base;
-
-	if (image_header->boot_flag != 0xAA55 ||
-	    image_header->header != 0x53726448 ||
-	    image_header->version < 0x20b ||
-	    !image_header->relocatable_kernel) {
-		pr_err("Not a valid kernel image!\n");
-		BS->unload_image(handle);
-		return -EINVAL;
-	}
-
-	boot_header = xmalloc(0x4000);
-	memset(boot_header, 0, 0x4000);
-	memcpy(boot_header, image_header, sizeof(*image_header));
-
-	/* Refer to Linux kernel commit a27e292b8a54
-	 * ("Documentation/x86/boot: Reserve type_of_loader=13 for barebox")
-	 */
-	boot_header->type_of_loader = 0x13;
-
-	if (data->initrd_file) {
-		tmp = read_file(data->initrd_file, &size);
-		initrd = xmemalign(PAGE_SIZE, PAGE_ALIGN(size));
-		memcpy(initrd, tmp, size);
-		memset(initrd + size, 0, PAGE_ALIGN(size) - size);
-		free(tmp);
-		boot_header->ramdisk_image = efi_virt_to_phys(initrd);
-		boot_header->ramdisk_size = PAGE_ALIGN(size);
-	}
-
-	options = linux_bootargs_get();
-	if (options) {
-		boot_header->cmd_line_ptr = efi_virt_to_phys(options);
-		boot_header->cmdline_size = strlen(options);
-	}
-
-	boot_header->code32_start = efi_virt_to_phys(loaded_image->image_base +
-			(image_header->setup_sects+1) * 512);
-
-	if (bootm_verbose(data)) {
-		printf("\nStarting kernel at 0x%p", loaded_image->image_base);
-		if (data->initrd_file)
-			printf(", initrd at 0x%08x",
-			       boot_header->ramdisk_image);
-		printf("...\n");
-	}
-
-	if (data->dryrun) {
-		BS->unload_image(handle);
-		free(boot_header);
-		free(initrd);
-		return 0;
-	}
-
-	efi_set_variable_usec("LoaderTimeExecUSec", &efi_systemd_vendor_guid,
-			      ktime_to_us(ktime_get()));
-
-	shutdown_barebox();
-	linux_efi_handover(handle, boot_header);
-
-	return 0;
-}
-
 static bool ramdisk_is_fit(struct image_data *data)
 {
 	struct stat st;
@@ -368,9 +273,9 @@ static int efi_load_ramdisk(struct efi_image_data *e)
 	}
 
 	vmem = efi_allocate_pages(&mem, initrd_size,
-				 EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA);
+				 EFI_ALLOCATE_ANY_PAGES, EFI_LOADER_DATA);
 	if (!vmem) {
-		pr_err("Failed to allocate pages for initrd data\n");
+		pr_err("Failed to allocate pages for initrd data: %s\n", strerror(errno));
 		ret = -ENOMEM;
 		goto free_pool;
 	}
@@ -498,6 +403,9 @@ static int efi_load_fdt(struct efi_image_data *e)
 
 static void efi_unload_fdt(struct efi_image_data *e)
 {
+	if (IS_ENABLED(CONFIG_EFI_FDT_FORCE))
+		return;
+	
 	BS->install_configuration_table(&efi_fdt_guid, NULL);
 
 	efi_free_pages(efi_phys_to_virt(e->oftree_res.base),
@@ -539,12 +447,33 @@ static int do_bootm_efi_stub(struct image_data *data)
 	return ret;
 }
 
-static struct image_handler efi_handle_tr = {
+static int efi_app_execute(struct image_data *data)
+{
+	int ret;
+	struct efi_image_data e = { .data = data };
+	enum filetype type;
+
+	ret = efi_load_file_image(data->os_file, &e.loaded_image, &e.handle);
+	if (ret)
+		return ret;
+
+	type = file_detect_type(e.loaded_image->image_base, PAGE_SIZE);
+	
+	return efi_execute_image(e.handle, e.loaded_image, type);
+}
+
+static struct image_handler efi_app_handle_tr = {
 	.name = "EFI Application",
-	.bootm = do_bootm_efi,
+	.bootm = efi_app_execute,
 	.filetype = filetype_exe,
 };
 
+static struct image_handler efi_x86_linux_handle_tr = {
+	.name = "EFI X86 Linux kernel",
+	.bootm = do_bootm_efi_stub,
+	.filetype = filetype_x86_efi_linux_image,
+};
+
 static struct image_handler efi_arm64_handle_tr = {
 	.name = "EFI ARM64 Linux kernel",
 	.bootm = do_bootm_efi_stub,
@@ -594,14 +523,22 @@ static struct binfmt_hook binfmt_arm64_efi_hook = {
 	.hook = efi_execute,
 };
 
+static struct binfmt_hook binfmt_x86_efi_hook = {
+	.type = filetype_x86_efi_linux_image,
+	.hook = efi_execute,
+};
+
 static int efi_register_image_handler(void)
 {
-	register_image_handler(&efi_handle_tr);
+	register_image_handler(&efi_app_handle_tr);
 	binfmt_register(&binfmt_efi_hook);
-
-	if (IS_ENABLED(CONFIG_X86))
+	
+	if (IS_ENABLED(CONFIG_X86)) {
 		register_image_handler(&non_efi_handle_linux_x86);
-
+		register_image_handler(&efi_x86_linux_handle_tr);
+		binfmt_register(&binfmt_x86_efi_hook);
+	}
+	
 	if (IS_ENABLED(CONFIG_ARM64)) {
 		register_image_handler(&efi_arm64_handle_tr);
 		binfmt_register(&binfmt_arm64_efi_hook);
-- 
2.34.1




  parent reply	other threads:[~2025-09-16  4:03 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-16  4:01 [PATCH v3 01/11] ARM: cpu: allow selecting CPU_V7/CPU_V8 directly chalianis1
2025-09-16  4:01 ` [PATCH v3 02/11] efi: payload: split image handling from legacy handover boot support chalianis1
2025-09-16  4:01 ` [PATCH v3 03/11] efi: payload: add support for efi stub boot chalianis1
2025-09-16  4:01 ` [PATCH v3 04/11] efi: payload: add support for fit image chalianis1
2025-09-16  4:01 ` [PATCH v3 05/11] efi: payload: make selectable without COMPILE_TEST chalianis1
2025-09-16  4:01 ` [PATCH v3 06/11] arm: efi: add a generic defconfig for v8 efi payload, chalianis1
2025-09-16  4:01 ` [PATCH v3 07/11] efi: payload: initrd: implement efi initrd media protocol chalianis1
2025-09-16  4:01 ` [PATCH v3 08/11] common: filetype: add x86 linux filetype chalianis1
2025-09-16  4:01 ` [PATCH v3 09/11] efi: payload: early-mem: helps to correctly boot x86 linux chalianis1
2025-09-16  4:01 ` chalianis1 [this message]
2025-09-16  4:01 ` [PATCH v3 11/11] lib: fdt: remove FDT_PADDING chalianis1

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=20250916040155.814159-10-chalianis1@gmail.com \
    --to=chalianis1@gmail.com \
    --cc=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@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