mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v1 42/54] bootm: add global.bootm.efi toggle
Date: Thu, 18 Dec 2025 11:38:02 +0100	[thread overview]
Message-ID: <20251218111242.1527495-43-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251218111242.1527495-1-a.fatoum@pengutronix.de>

EFI-stubbed kernel images can be booted via EFI or completely via DT.
To allow barebox to support both in the same build, introduce a
global.bootm.efi variable that controls this when loader support is
enabled. The default will be to use EFI if support in both barebox and
kernel is available, but there is an option to disable use of EFI and to
enforce it.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/lib64/armlinux.c | 14 ++++---------
 arch/riscv/lib/bootm.c    | 14 ++++---------
 common/bootm.c            | 43 +++++++++++++++++++++++++++++++++++++++
 common/filetype.c         | 16 +++++++++++++++
 efi/payload/bootm.c       |  1 +
 include/bootm.h           | 12 +++++++++++
 include/filetype.h        |  1 +
 7 files changed, 81 insertions(+), 20 deletions(-)

diff --git a/arch/arm/lib64/armlinux.c b/arch/arm/lib64/armlinux.c
index 7803d5671719..e40b61a26b07 100644
--- a/arch/arm/lib64/armlinux.c
+++ b/arch/arm/lib64/armlinux.c
@@ -33,15 +33,10 @@ static int do_bootm_linux(struct image_data *data)
 }
 
 static struct image_handler aarch64_linux_handler = {
-        .name = "ARM aarch64 Linux image",
-        .bootm = do_bootm_linux,
-        .filetype = filetype_arm64_linux_image,
-};
-
-static struct image_handler aarch64_linux_efi_handler = {
-        .name = "ARM aarch64 Linux/EFI image",
-        .bootm = do_bootm_linux,
-        .filetype = filetype_arm64_efi_linux_image,
+	.name = "ARM aarch64 Linux image",
+	.bootm = do_bootm_linux,
+	.check_image = bootm_efi_check_image,
+	.filetype = filetype_arm64_linux_image,
 };
 
 static int do_bootm_barebox(struct image_data *data)
@@ -87,7 +82,6 @@ static int aarch64_register_image_handler(void)
 	if (efi_is_payload())
 		return 0;
 
-	register_image_handler(&aarch64_linux_efi_handler);
 	register_image_handler(&aarch64_linux_handler);
 	register_image_handler(&aarch64_barebox_handler);
 
diff --git a/arch/riscv/lib/bootm.c b/arch/riscv/lib/bootm.c
index 08791458f824..2a47579b41ef 100644
--- a/arch/riscv/lib/bootm.c
+++ b/arch/riscv/lib/bootm.c
@@ -31,15 +31,10 @@ static int do_bootm_linux(struct image_data *data)
 }
 
 static struct image_handler riscv_linux_handler = {
-        .name = "RISC-V Linux image",
-        .bootm = do_bootm_linux,
-        .filetype = filetype_riscv_linux_image,
-};
-
-static struct image_handler riscv_linux_efi_handler = {
-        .name = "RISC-V Linux/EFI image",
-        .bootm = do_bootm_linux,
-        .filetype = filetype_riscv_efi_linux_image,
+	.name = "RISC-V Linux image",
+	.bootm = do_bootm_linux,
+	.check_image = bootm_efi_check_image,
+	.filetype = filetype_riscv_linux_image,
 };
 
 static struct image_handler riscv_barebox_handler = {
@@ -51,7 +46,6 @@ static struct image_handler riscv_barebox_handler = {
 static int riscv_register_image_handler(void)
 {
 	register_image_handler(&riscv_linux_handler);
-	register_image_handler(&riscv_linux_efi_handler);
 	register_image_handler(&riscv_barebox_handler);
 
 	return 0;
diff --git a/common/bootm.c b/common/bootm.c
index 620eb19ff1d3..4e4d940d9198 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -6,6 +6,7 @@
 #include <bootm-overrides.h>
 #include <fs.h>
 #include <fcntl.h>
+#include <efi/mode.h>
 #include <malloc.h>
 #include <memory.h>
 #include <block.h>
@@ -73,6 +74,7 @@ static int bootm_earlycon;
 static int bootm_provide_machine_id;
 static int bootm_provide_hostname;
 static int bootm_verbosity;
+static int bootm_efi_mode = BOOTM_EFI_AVAILABLE;
 
 void bootm_data_init_defaults(struct bootm_data *data)
 {
@@ -95,6 +97,7 @@ void bootm_data_init_defaults(struct bootm_data *data)
 	data->provide_hostname = bootm_provide_hostname;
 	data->verbose = bootm_verbosity;
 	data->dryrun = bootm_dryrun;
+	data->efi_boot = bootm_efi_mode;
 }
 
 void bootm_data_restore_defaults(const struct bootm_data *data)
@@ -115,6 +118,7 @@ void bootm_data_restore_defaults(const struct bootm_data *data)
 	bootm_provide_hostname = data->provide_hostname;
 	bootm_verbosity = data->verbose;
 	bootm_dryrun = data->dryrun;
+	bootm_efi_mode = data->efi_boot;
 }
 
 static enum bootm_verify bootm_verify_mode = BOOTM_VERIFY_AVAILABLE;
@@ -807,6 +811,7 @@ int bootm_boot(struct bootm_data *bootm_data)
 	data->initrd_address = bootm_data->initrd_address;
 	data->os_address = bootm_data->os_address;
 	data->os_entry = bootm_data->os_entry;
+	data->efi_boot = bootm_data->efi_boot;
 
 	ret = read_file_2(data->os_file, &size, &data->os_header, PAGE_SIZE);
 	if (ret < 0 && ret != -EFBIG) {
@@ -1052,6 +1057,30 @@ struct bootm_overrides bootm_set_overrides(const struct bootm_overrides override
 }
 #endif
 
+bool bootm_efi_check_image(struct image_handler *handler,
+			   struct image_data *data,
+			   enum filetype detected_filetype)
+{
+	/* This is our default: Use EFI when support is compiled in,
+	 * and fallback to normal boot otherwise.
+	 */
+	if (data->efi_boot == BOOTM_EFI_AVAILABLE) {
+		if (IS_ENABLED(CONFIG_EFI_LOADER))
+			data->efi_boot = BOOTM_EFI_REQUIRED;
+		else
+			data->efi_boot = BOOTM_EFI_DISABLED;
+	}
+
+	/* If EFI is disabled, we assume EFI-stubbed images are
+	 * just normal non-EFI stubbed ones
+	 */
+	if (data->efi_boot == BOOTM_EFI_DISABLED)
+		return handler->filetype == filetype_no_efistub(detected_filetype);
+
+	/* If EFI is required, we enforce handlers to match exactly */
+	return detected_filetype == handler->filetype;
+}
+
 static int do_bootm_compressed(struct image_data *img_data)
 {
 	struct bootm_data bootm_data = {
@@ -1140,6 +1169,12 @@ static struct image_handler zstd_bootm_handler = {
 
 int linux_rootwait_secs = 10;
 
+static const char * const bootm_efi_loader_mode_names[] = {
+	[BOOTM_EFI_DISABLED] = "disabled",
+	[BOOTM_EFI_AVAILABLE] = "available",
+	[BOOTM_EFI_REQUIRED] = "required",
+};
+
 static int bootm_init(void)
 {
 	globalvar_add_simple("bootm.image", NULL);
@@ -1174,6 +1209,11 @@ static int bootm_init(void)
 		globalvar_add_simple_int("linux.rootwait",
 					 &linux_rootwait_secs, "%d");
 
+	if (IS_ENABLED(CONFIG_EFI_LOADER) && !efi_is_payload())
+		globalvar_add_simple_enum("bootm.efi", &bootm_efi_mode,
+					  bootm_efi_loader_mode_names,
+					  ARRAY_SIZE(bootm_efi_loader_mode_names));
+
 	if (IS_ENABLED(CONFIG_BZLIB))
 		register_image_handler(&bzip2_bootm_handler);
 	if (IS_ENABLED(CONFIG_ZLIB))
@@ -1200,6 +1240,9 @@ BAREBOX_MAGICVAR(global.bootm.oftree, "bootm default oftree");
 BAREBOX_MAGICVAR(global.bootm.tee, "bootm default tee image");
 BAREBOX_MAGICVAR(global.bootm.dryrun, "bootm default dryrun level");
 BAREBOX_MAGICVAR(global.bootm.verify, "bootm default verify level");
+#ifdef CONFIG_EFI_LOADER
+BAREBOX_MAGICVAR(global.bootm.efi, "efiloader: Behavior for EFI-stubbable boot images: EFI \"disabled\", EFI if \"available\" (default), EFI is \"required\"");
+#endif
 BAREBOX_MAGICVAR(global.bootm.verbose, "bootm default verbosity level (0=quiet)");
 BAREBOX_MAGICVAR(global.bootm.earlycon, "Add earlycon option to Kernel for early log output");
 BAREBOX_MAGICVAR(global.bootm.appendroot, "Add root= option to Kernel to mount rootfs from the device the Kernel comes from (default, device can be overridden via global.bootm.root_dev)");
diff --git a/common/filetype.c b/common/filetype.c
index b82667c30a4b..9b348377f222 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -595,3 +595,19 @@ bool filetype_is_linux_efi_image(enum filetype ft)
 		return false;
 	}
 }
+
+enum filetype filetype_no_efistub(enum filetype ft)
+{
+	switch (ft) {
+	case filetype_arm_efi_zimage:
+		return filetype_arm_zimage;
+	case filetype_arm64_efi_linux_image:
+		return filetype_arm64_linux_image;
+	case filetype_riscv_efi_linux_image:
+		return filetype_riscv_linux_image;
+	case filetype_x86_efi_linux_image:
+		return filetype_x86_linux_image;
+	default:
+		return ft;
+	}
+}
diff --git a/efi/payload/bootm.c b/efi/payload/bootm.c
index 87bf1de709b4..ea5b1265aeaa 100644
--- a/efi/payload/bootm.c
+++ b/efi/payload/bootm.c
@@ -316,6 +316,7 @@ struct image_handler efi_x86_linux_handle_tr = {
 static struct image_handler efi_arm64_handle_tr = {
 	.name = "EFI ARM64 Linux kernel",
 	.bootm = do_bootm_efi_stub,
+	.check_image = bootm_efi_check_image,
 	.filetype = filetype_arm64_efi_linux_image,
 };
 
diff --git a/include/bootm.h b/include/bootm.h
index b08e20c50da7..e56a999f0b11 100644
--- a/include/bootm.h
+++ b/include/bootm.h
@@ -13,6 +13,12 @@ enum bootm_verify {
 	BOOTM_VERIFY_AVAILABLE,
 };
 
+enum bootm_efi_mode {
+	BOOTM_EFI_DISABLED,
+	BOOTM_EFI_AVAILABLE,
+	BOOTM_EFI_REQUIRED,
+};
+
 struct bootm_data {
 	const char *os_file;
 	const char *initrd_file;
@@ -40,6 +46,7 @@ struct bootm_data {
 	 * of global.hostname to Kernel.
 	 */
 	bool provide_hostname;
+	enum bootm_efi_mode efi_boot;
 	unsigned long initrd_address;
 	unsigned long os_address;
 	unsigned long os_entry;
@@ -109,6 +116,7 @@ struct image_data {
 	int verbose;
 	int force;
 	int dryrun;
+	enum bootm_efi_mode efi_boot;
 };
 
 struct image_handler {
@@ -165,4 +173,8 @@ void bootm_force_signed_images(void);
 
 void *booti_load_image(struct image_data *data, phys_addr_t *oftree);
 
+bool bootm_efi_check_image(struct image_handler *handler,
+			   struct image_data *data,
+			   enum filetype detected_filetype);
+
 #endif /* __BOOTM_H */
diff --git a/include/filetype.h b/include/filetype.h
index aed02fcc2566..0e19e46c513d 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -91,6 +91,7 @@ enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec)
 int is_fat_boot_sector(const void *_buf);
 bool filetype_is_barebox_image(enum filetype ft);
 bool filetype_is_linux_efi_image(enum filetype ft);
+enum filetype filetype_no_efistub(enum filetype);
 
 static inline bool file_is_compressed_file(enum filetype ft)
 {
-- 
2.47.3




  parent reply	other threads:[~2025-12-18 11:40 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-18 10:37 [PATCH v1 00/54] efi: implement EFI loader support in barebox Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 01/54] efi: payload: initrd: fix type mismatch on 32-bit Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 02/54] efi: loader: switch over event/memory key type to efi_uintn_t Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 03/54] lib: vsprintf: print human-readable EFI GUIDs with %pUs Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 04/54] fs: fat: don't duplicate dentries when resolving differently cased paths Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 05/54] efi: loader: add memory accounting Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 06/54] efi: loader: add pool allocator Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 07/54] efi: types: add EFI_RUNTIME_SECTION Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 08/54] resource: assign memory banks a default type and attr Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 09/54] ARM: lds: add EFI runtime service section Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 10/54] ARM: move needed assembly routines into EFI runtime section Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 11/54] crypto: crc32: implement position independent CRC32 Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 12/54] efi: loader: add support for tracing calls back into UEFI Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 13/54] efi: loader: add table utility functions Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 14/54] lib: add charset helpers Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 15/54] efi: loader: add object handling API Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 16/54] efi: loader: add devicepath support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 17/54] efi: loader: add debug support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 18/54] efi: loader: add boot services support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 19/54] efi: loader: add support for runtime services before ExitBootServices Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 20/54] efi: loader: setup root node Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 21/54] efi: loader: add watchdog support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 22/54] efi: loader: move PE implementation out of common code Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 23/54] efi: loader: protocol: add file protocol support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 24/54] efi: loader: protocol: add Block IO support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 25/54] efi: loader: protocol: implement efi_file_from_path Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 26/54] efi: loader: boot: implement LoadImage BootService Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 27/54] efi: loader: add EFI load option handling Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 28/54] efi: loader: protocol: add graphical output protocol support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 29/54] efi: loader: protocol: add console support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 30/54] efi: loader: protocol: add HII support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 31/54] efi: loader: protocol: add unicode collation support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 32/54] efi: loader: protocol: add random number generator protocol Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 33/54] efi: loader: protocol: add device_path_utilities Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 34/54] efi: loader: support formatting only first device path node to text Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 35/54] efi: loader: protocol: add efi_device_path_to_text support Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 36/54] restart: allow drivers to register runtime restart handler Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 37/54] poweroff: allow drivers to register runtime poweroff handler Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 38/54] ARM: psci: client: register runtime service " Ahmad Fatoum
2025-12-18 10:37 ` [PATCH v1 39/54] ARM: psci: client: register runtime service restart handler Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 40/54] hardening: disable some features when EFI runtime support is enabled Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 41/54] filetype: add new filetype for efi-stubbed ARM zImages Ahmad Fatoum
2025-12-18 10:38 ` Ahmad Fatoum [this message]
2025-12-18 10:38 ` [PATCH v1 43/54] efi: loader: add ESP boot entry provider Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 44/54] efi: loader: add rudimentary EFI boot manager Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 45/54] efi: loader: implement bootm handler Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 46/54] efi: runtime: add EFI variable support Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 47/54] efi: loader: populate OsIndicationsSupported/PlatformLang variables Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 48/54] ARM: don't disable MMU when EFI booting Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 49/54] efi: runtime: add runtime service support after ExitBootServices Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 50/54] efi: runtime: add relocation check Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 51/54] efi: loader: CONFIG_EFI_RT_VOLATILE_STORE Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 52/54] efi: loader: support ExitBootServices Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 53/54] efi: loader: pass along SMBIOS table Ahmad Fatoum
2025-12-18 10:38 ` [PATCH v1 54/54] ARM: configs: add multi_v7/8_efiloader_defconfig Ahmad Fatoum

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=20251218111242.1527495-43-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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