Skip to contentSkip to conversation

efi: payload: split out payload Kconfig symbols

PATCH Applied [email protected] 3 participants 13 messages Oct 7, 2025 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/10 efi: payload: split out payload Kconfig symbols

From: Ahmad Fatoum <[email protected]>

We are already keeping different directories for payload and common EFI support. In preparation for adding more payload-specific config options, move the existing ones into their own Kconfig file.

---
 efi/Kconfig         | 20 +-------------------
 efi/payload/Kconfig | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 19 deletions(-)
 create mode 100644 efi/payload/Kconfig
efi/Kconfig +1 -19
index 84f670fd23d3..35850b20ef50 100644
@@ -2,21 +2,7 @@
 
 menu "EFI (Extensible Firmware Interface) Support"
 
-config HAVE_EFI_PAYLOAD
-	bool
-
-config EFI_PAYLOAD
-	bool "Build as EFI payload" if COMPILE_TEST
-	depends on HAVE_EFI_PAYLOAD
-	select PBL_FULLY_PIC if ARM64
-	select EFI
-	select EFI_GUID
-	select EFI_DEVICEPATH
-	select PRINTF_UUID
-	select PRINTF_WCHAR
-	select BLOCK
-	select PARTITION_DISK
-	select HW_HAS_PCI
+source "efi/payload/Kconfig"
 
 config EFI
 	bool
@@ -26,10 +12,6 @@ config HAVE_EFI_STUB
 
 if EFI
 
-config EFI_STUB
-	def_bool EFI_PAYLOAD
-	depends on HAVE_EFI_STUB
-
 config EFI_GUID
 	bool
 	help
efi/payload/Kconfig new file +27 -0
new file mode 100644
index 000000000000..9263d59de94a
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config HAVE_EFI_PAYLOAD
+	bool
+
+config EFI_PAYLOAD
+	bool "Build as EFI payload" if COMPILE_TEST
+	depends on HAVE_EFI_PAYLOAD
+	select PBL_FULLY_PIC if ARM64
+	select EFI
+	select EFI_GUID
+	select EFI_DEVICEPATH
+	select PRINTF_UUID
+	select PRINTF_WCHAR
+	select BLOCK
+	select PARTITION_DISK
+	select HW_HAS_PCI
+
+config HAVE_EFI_STUB
+	bool
+
+if EFI_PAYLOAD
+
+config EFI_STUB
+	def_bool HAVE_EFI_STUB
+
+endif
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 2/10 tlsf: move kasan_poison_shadow into tlsf_add_pool

From: Ahmad Fatoum <[email protected]>

Unallocated memory is poisoned by default to catch heap overflows. Its control data can also be poisoned by default as the code takes care to unpoison it on access.

Move the poisoning thus tlsf_add_pool(), so users need not worry about it.

---
 common/tlsf.c        | 7 +++++--
 common/tlsf_malloc.c | 2 --
 2 files changed, 5 insertions(+), 4 deletions(-)
common/tlsf.c +5 -2
index 8666b94ea387..4ee53a9b2f20 100644
@@ -814,6 +814,7 @@ size_t tlsf_alloc_overhead(void)
 
 pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
 {
+	control_t* control = tlsf_cast(control_t*, tlsf);
 	block_header_t* block;
 	block_header_t* next;
 
@@ -844,7 +845,7 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
 	block_set_size(block, pool_bytes);
 	block_set_free(block);
 	block_set_prev_used(block);
-	block_insert(tlsf_cast(control_t*, tlsf), block);
+	block_insert(control, block);
 
 	/* Split the block to create a zero-size sentinel block. */
 	next = block_link_next(block);
@@ -852,6 +853,9 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
 	block_set_used(next);
 	block_set_prev_free(next);
 
+	kasan_poison_shadow(mem, bytes, KASAN_TAG_INVALID);
+	kasan_poison_shadow(control, sizeof(control), KASAN_TAG_INVALID);
+
 	return mem;
 }
 
@@ -927,7 +931,6 @@ tlsf_t tlsf_create_with_pool(void* mem, size_t bytes)
 {
 	tlsf_t tlsf = tlsf_create(mem);
 	tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size());
-	kasan_poison_shadow(mem, bytes, KASAN_TAG_INVALID);
 	return tlsf;
 }
 
common/tlsf_malloc.c +0 -2
index 6e9d48af26bb..74089fe7f390 100644
@@ -115,8 +115,6 @@ void *malloc_add_pool(void *mem, size_t bytes)
 	if (!new_pool)
 		return NULL;
 
-	kasan_poison_shadow(mem, bytes, KASAN_TAG_INVALID);
-
 	new_pool_entry = malloc(sizeof(*new_pool_entry));
 	if (!new_pool_entry)
 		return NULL;
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 3/10 tlsf: support on-demand requesting memory from external store

In reply to [email protected]

From: Chali Anis <[email protected]>

barebox running under an operating system (or nearly one like UEFI) does not have control over all the memory and should not hog too much memory upfront as that can interfere with other applications, e.g. EFI drivers or the EFI firmware itself.

To accommodate this use case, allow TLSF to dynamically request more memory from an external store via tlsf_register_store().

---
 common/tlsf.c  | 29 ++++++++++++++++++++++++++++-
 include/tlsf.h |  1 +
 2 files changed, 29 insertions(+), 1 deletion(-)
common/tlsf.c +28 -1
index 4ee53a9b2f20..636b57b1c642 100644
@@ -159,6 +159,9 @@ typedef struct control_t
 	/* Empty lists point at this block to indicate they are free. */
 	block_header_t block_null;
 
+	void (*request_store)(tlsf_t, size_t);
+
+
 	/* Bitmaps for free lists. */
 	unsigned int fl_bitmap;
 	unsigned int sl_bitmap[FL_INDEX_COUNT];
@@ -568,7 +571,7 @@ static block_header_t* block_trim_free_leading(control_t* control, block_header_
 	return remaining_block;
 }
 
-static block_header_t* block_locate_free(control_t* control, size_t size)
+static block_header_t* __block_locate_free(control_t* control, size_t size)
 {
 	int fl = 0, sl = 0;
 	block_header_t* block = 0;
@@ -598,6 +601,23 @@ static block_header_t* block_locate_free(control_t* control, size_t size)
 	return block;
 }
 
+static block_header_t* block_locate_free(control_t* control, size_t size)
+{
+	block_header_t *block;
+
+	block = __block_locate_free(control, size);
+	if (block)
+		return block;
+
+	if (!control->request_store || !size)
+		return NULL;
+
+	control->request_store(tlsf_cast(tlsf_t, control),
+			       size + tlsf_pool_overhead());
+
+	return __block_locate_free(control, size);
+}
+
 static void* block_prepare_used(control_t* control, block_header_t* block,
 				size_t size, size_t used)
 {
@@ -934,6 +954,13 @@ tlsf_t tlsf_create_with_pool(void* mem, size_t bytes)
 	return tlsf;
 }
 
+void tlsf_register_store(tlsf_t tlsf, void (*cb)(tlsf_t tlsf, size_t bytes))
+{
+	control_t* control = tlsf_cast(control_t*, tlsf);
+
+	control->request_store = cb;
+}
+
 void tlsf_destroy(tlsf_t tlsf)
 {
 	/* Nothing to do. */
include/tlsf.h +1 -0
index e0dcdac1e7ab..ea86b0600c49 100644
@@ -63,6 +63,7 @@ tlsf_t tlsf_create(void* mem);
 tlsf_t tlsf_create_with_pool(void* mem, size_t bytes);
 void tlsf_destroy(tlsf_t tlsf);
 pool_t tlsf_get_pool(tlsf_t tlsf);
+void tlsf_register_store(tlsf_t tlsf, void (*cb)(tlsf_t tlsf, size_t bytes));
 
 /* Add/remove memory pools. */
 pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v2 03/10] tlsf: support on-demand requesting memory from external store

Quoted 22 lines · On Mon, Oct 06, 2025 at 08:59:48PM -0400, [email protected] wrote:

From: Chali Anis <[email protected]>

barebox running under an operating system (or nearly one like UEFI) does not have control over all the memory and should not hog too much memory upfront as that can interfere with other applications, e.g. EFI drivers or the EFI firmware itself.

To accommodate this use case, allow TLSF to dynamically request more memory from an external store via tlsf_register_store().

---
 common/tlsf.c  | 29 ++++++++++++++++++++++++++++-
 include/tlsf.h |  1 +
 2 files changed, 29 insertions(+), 1 deletion(-)
common/tlsf.c +0 -0
index 4ee53a9b2f20..636b57b1c642 100644

common/tlsf.c typedef struct control_t · line 164

Quoted 6 lines
common/tlsf.c +3 -0
@@ -159,6 +159,9 @@ typedef struct control_t
 	/* Empty lists point at this block to indicate they are free. */
 	block_header_t block_null;
 
+	void (*request_store)(tlsf_t, size_t);
+
+

This breaks compilation on 32bit platforms.

The reason is we have this directly under the definition of control_t:

tlsf_static_assert(sizeof(control_t) % ALIGN_SIZE == 0);

I fixed it up as follows.

It looks odd to me that we have to manually align the struct size each time we add a field to this struct. Isn't there something better?

Sascha

---------------------------8<------------------------------

Quoted 1 line

From 11fcc69bbc41954d5e28a3b082dc39171bd6cdc0 Mon Sep 17 00:00:00 2001

From: Sascha Hauer <[email protected]>
Date: Tue, 7 Oct 2025 15:39:38 +0200
Subject: [PATCH] fixup! tlsf: support on-demand requesting memory from
external store

---
 common/tlsf.c | 2 --
 1 file changed, 2 deletions(-)
common/tlsf.c +0 -2
index 636b57b1c6..c4ba8259b2 100644
@@ -161,11 +161,9 @@ typedef struct control_t
 
 	void (*request_store)(tlsf_t, size_t);
 
-
 	/* Bitmaps for free lists. */
 	unsigned int fl_bitmap;
 	unsigned int sl_bitmap[FL_INDEX_COUNT];
-	u32 : BYTES_TO_BITS(ALIGN_SIZE - sizeof(size_t));
 
 	/* Head of free lists. */
 	block_header_t* blocks[FL_INDEX_COUNT][SL_INDEX_COUNT];
Signature · 8 lines
2.47.3


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#4 lore raw

Re: [PATCH v2 03/10] tlsf: support on-demand requesting memory from external store

Quoted 32 lines · Sascha Hauer
Quoted 22 lines · On Mon, Oct 06, 2025 at 08:59:48PM -0400, [email protected] wrote:

From: Chali Anis <[email protected]>

barebox running under an operating system (or nearly one like UEFI) does not have control over all the memory and should not hog too much memory upfront as that can interfere with other applications, e.g. EFI drivers or the EFI firmware itself.

To accommodate this use case, allow TLSF to dynamically request more memory from an external store via tlsf_register_store().

---
 common/tlsf.c  | 29 ++++++++++++++++++++++++++++-
 include/tlsf.h |  1 +
 2 files changed, 29 insertions(+), 1 deletion(-)
common/tlsf.c +0 -0
index 4ee53a9b2f20..636b57b1c642 100644

common/tlsf.c typedef struct control_t · line 164

Quoted 6 lines
common/tlsf.c +3 -0
@@ -159,6 +159,9 @@ typedef struct control_t
 	/* Empty lists point at this block to indicate they are free. */
 	block_header_t block_null;
 
+	void (*request_store)(tlsf_t, size_t);
+
+

This breaks compilation on 32bit platforms.

Quoted 5 lines

The reason is we have this directly under the definition of control_t:

tlsf_static_assert(sizeof(control_t) % ALIGN_SIZE == 0);

I fixed it up as follows.

Fixup looks good, thanks.

Quoted 2 lines

It looks odd to me that we have to manually align the struct size each time we add a field to this struct. Isn't there something better?

Maybe we could hack something together with alignment or a union, but in the end it would not be applicable to block_header_t, which is intentionally overlayed in a misaligned manner as prev_phys_block is part of the previous allocation...

The asserts at least work :-)

Cheers,
Ahmad

Quoted 30 lines

Sascha

---------------------------8<------------------------------ From 11fcc69bbc41954d5e28a3b082dc39171bd6cdc0 Mon Sep 17 00:00:00 2001
From: Sascha Hauer <[email protected]>
Date: Tue, 7 Oct 2025 15:39:38 +0200
Subject: [PATCH] fixup! tlsf: support on-demand requesting memory from
external store

---
 common/tlsf.c | 2 --
 1 file changed, 2 deletions(-)
common/tlsf.c +0 -2
index 636b57b1c6..c4ba8259b2 100644
@@ -161,11 +161,9 @@ typedef struct control_t
 
 	void (*request_store)(tlsf_t, size_t);
 
-
 	/* Bitmaps for free lists. */
 	unsigned int fl_bitmap;
 	unsigned int sl_bitmap[FL_INDEX_COUNT];
-	u32 : BYTES_TO_BITS(ALIGN_SIZE - sizeof(size_t));
 
 	/* Head of free lists. */
 	block_header_t* blocks[FL_INDEX_COUNT][SL_INDEX_COUNT];
Signature · 4 lines
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |
#5 lore raw

PATCH v2 4/10 efi: payload: add support for EFI initrd media protocol

In reply to [email protected]

From: Chali Anis <[email protected]>

Implement the EFI initrd media protocol, which allows registering an initramfs data to be loaded by the linux after vi EFI Initrd Media protocol.

---
 efi/guid.c                |   4 ++
 efi/payload/Makefile      |   1 +
 efi/payload/efi-initrd.c  | 104 ++++++++++++++++++++++++++++++++++++++
 include/efi.h             |   4 ++
 include/efi/efi-payload.h |   3 ++
 5 files changed, 116 insertions(+)
 create mode 100644 efi/payload/efi-initrd.c
efi/guid.c +4 -0
index ef230a2b2403..46440c9f3328 100644
@@ -44,6 +44,7 @@ const efi_guid_t efi_load_file_protocol_guid = EFI_LOAD_FILE_PROTOCOL_GUID;
 const efi_guid_t efi_load_file2_protocol_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
 const efi_guid_t efi_device_path_utilities_protocol_guid =
 	EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;
+const efi_guid_t efi_linux_initrd_media_guid = EFI_LINUX_INITRD_MEDIA_GUID;
 
 #define EFI_GUID_STRING(guid, short, long) do {	\
 	if (!efi_guidcmp(guid, *g))		\
@@ -128,5 +129,8 @@ const char *efi_guid_string(efi_guid_t *g)
 	EFI_GUID_STRING(EFI_ISCSIDXE_INF_GUID, "IScsiDxe.inf", "EFI IScsiDxe.inf File GUID");
 	EFI_GUID_STRING(EFI_VLANCONFIGDXE_INF_GUID, "VlanConfigDxe.inf", "EFI VlanConfigDxe.inf File GUID");
 
+	/* Ramdisk */
+	EFI_GUID_STRING(EFI_LINUX_INITRD_MEDIA_GUID, "Initrd Media", "EFI Linux Initrd Media GUID");
+
 	return "unknown";
 }
efi/payload/Makefile +1 -0
index a4a0332a8288..1250535302d3 100644
@@ -2,6 +2,7 @@
 
 obj-y += init.o
 obj-y += image.o
+obj-y += efi-initrd.o
 obj-$(CONFIG_OFTREE) += fdt.o
 bbenv-y += env-efi
 obj-$(CONFIG_CMD_IOMEM) += iomem.o
efi/payload/efi-initrd.c new file +104 -0

105 lines not on this page · raw message

include/efi.h +4 -0
index 40b69fdb0188..bd339ea097c3 100644
@@ -558,6 +558,9 @@ extern struct efi_runtime_services *RT;
 #define EFI_DRIVER_BINDING_PROTOCOL_GUID \
     EFI_GUID(0x18a031ab, 0xb443, 0x4d1a, 0xa5, 0xc0, 0x0c, 0x09, 0x26, 0x1e, 0x9f, 0x71)
 
+#define EFI_LINUX_INITRD_MEDIA_GUID \
+	EFI_GUID(0x5568e427, 0x68fc, 0x4f3d,  0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68)
+
 struct efi_driver_binding_protocol {
 	efi_status_t (EFIAPI * supported)(
 			struct efi_driver_binding_protocol *this,
@@ -603,6 +606,7 @@ extern const efi_guid_t efi_guid_event_group_reset_system;
 extern const efi_guid_t efi_load_file_protocol_guid;
 extern const efi_guid_t efi_load_file2_protocol_guid;
 extern const efi_guid_t efi_device_path_utilities_protocol_guid;
+extern const efi_guid_t efi_linux_initrd_media_guid;
 
 struct efi_config_table {
 	efi_guid_t guid;
include/efi/efi-payload.h +3 -0
index fe45864dd8a7..d8e66a187a87 100644
@@ -34,4 +34,7 @@ int efi_set_variable_usec(char *name, efi_guid_t *vendor, uint64_t usec);
 void *efi_earlymem_alloc(const struct efi_system_table *sys_table,
 			 size_t *memsize, enum efi_memory_type mem_type);
 
+int efi_initrd_register(void *initrd, size_t initrd_size);
+void efi_initrd_unregister(void);
+
 #endif
Signature · 1 line
2.34.1
#6 lore raw

PATCH v2 5/10 efi: payload: earlymem: allocate only the barebox needs in term of memory

In reply to [email protected]

From: Chali Anis <[email protected]>

With TLSF now able to invoke a callback to request additional memory on demand, limit early allocations to what barebox strictly requires and have everything handled via the request callback. This ensures more deterministic behavior and reduces the risk of OOM conditions.

---
 common/tlsf_malloc.c       | 18 ++++++++++++++++++
 efi/payload/Kconfig        |  1 +
 efi/payload/early-mem.c    | 19 ++++---------------
 efi/payload/entry-multi.c  |  5 ++---
 efi/payload/entry-single.c |  5 ++---
 efi/payload/init.c         | 15 +++++++++++++++
 include/efi/efi-payload.h  |  2 +-
 include/malloc.h           |  1 +
 8 files changed, 44 insertions(+), 22 deletions(-)
common/tlsf_malloc.c +18 -0
index 74089fe7f390..d3b665a2fc40 100644
@@ -11,11 +11,14 @@
 #include <stdio.h>
 #include <module.h>
 #include <tlsf.h>
+#include <linux/sizes.h>
+#include <linux/log2.h>
 
 #include <linux/kasan.h>
 #include <linux/list.h>
 
 tlsf_t tlsf_mem_pool;
+static void (*malloc_request_store)(size_t bytes);
 
 struct pool_entry {
 	pool_t pool;
@@ -124,3 +127,18 @@ void *malloc_add_pool(void *mem, size_t bytes)
 
 	return (void *)new_pool;
 }
+
+static void tlsf_request_store(tlsf_t tlsf, size_t bytes)
+{
+	size_t size;
+
+	size = __roundup_pow_of_two(bytes + sizeof(struct pool_entry));
+
+	malloc_request_store(max_t(size_t, SZ_8M, size));
+}
+
+void malloc_register_store(void (*cb)(size_t bytes))
+{
+	malloc_request_store = cb;
+	tlsf_register_store(tlsf_mem_pool, tlsf_request_store);
+}
efi/payload/Kconfig +1 -0
index 9263d59de94a..a403acc81a93 100644
@@ -15,6 +15,7 @@ config EFI_PAYLOAD
 	select BLOCK
 	select PARTITION_DISK
 	select HW_HAS_PCI
+	select MALLOC_TLSF
 
 config HAVE_EFI_STUB
 	bool
efi/payload/early-mem.c +4 -15
index 0f79a8c30694..c5e94b33a756 100644
@@ -6,28 +6,17 @@
 #include <efi/efi-payload.h>
 
 void *efi_earlymem_alloc(const struct efi_system_table *sys_table,
-			 size_t *memsize, enum efi_memory_type mem_type)
+			 size_t memsize, enum efi_memory_type mem_type)
 {
 	struct efi_boot_services *bs = sys_table->boottime;
-	enum efi_allocate_type alloc_type = EFI_ALLOCATE_ANY_PAGES;
 	efi_physical_addr_t mem;
 	efi_status_t efiret;
 
-	if (IS_ENABLED(CONFIG_X86)) {
-		/* Try to stay clear of memory mapped devices */
-		alloc_type = EFI_ALLOCATE_MAX_ADDRESS;
-		mem = SZ_1G - 1;
-	}
-
-	for (*memsize = SZ_256M; *memsize >= SZ_8M; *memsize /= 2) {
-		efiret  = bs->allocate_pages(alloc_type, mem_type,
-					     *memsize / EFI_PAGE_SIZE, &mem);
-		if (!EFI_ERROR(efiret) || efiret != EFI_OUT_OF_RESOURCES)
-			break;
-	}
+	efiret  = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES, mem_type,
+					     memsize / EFI_PAGE_SIZE, &mem);
 	if (EFI_ERROR(efiret))
 		panic("failed to allocate %zu byte memory pool: 0x%lx\n",
-		      *memsize, efiret);
+		      memsize, efiret);
 
 	return efi_phys_to_virt(mem);
 }
efi/payload/entry-multi.c +2 -3
index d5d54cdf70a1..82f3dfffe870 100644
@@ -23,7 +23,6 @@ static void efi_putc(void *ctx, int ch)
 
 void __efistub_efi_pe_entry(void *image, struct efi_system_table *sys_table)
 {
-	size_t memsize;
 	void *mem;
 	static struct barebox_efi_data efidata;
 
@@ -37,7 +36,7 @@ void __efistub_efi_pe_entry(void *image, struct efi_system_table *sys_table)
 
 	handoff_data_add(HANDOFF_DATA_EFI, &efidata, sizeof(efidata));
 
-	mem = efi_earlymem_alloc(sys_table, &memsize, EFI_LOADER_CODE);
+	mem = efi_earlymem_alloc(sys_table, SZ_16M, EFI_BOOT_SERVICES_CODE);
 
-	barebox_pbl_entry((uintptr_t)mem, memsize, NULL);
+	barebox_pbl_entry((uintptr_t)mem, SZ_16M, NULL);
 }
efi/payload/entry-single.c +2 -3
index 8600bd845c49..f481d0942ba5 100644
@@ -17,7 +17,6 @@
 void efi_main(efi_handle_t image, struct efi_system_table *sys_table)
 {
 	efi_status_t efiret;
-	size_t memsize;
 	void *mem;
 
 #ifdef DEBUG
@@ -37,9 +36,9 @@ void efi_main(efi_handle_t image, struct efi_system_table *sys_table)
 		BS->handle_protocol(efi_loaded_image->device_handle,
 				&efi_device_path_protocol_guid, (void **)&efi_device_path);
 
-	mem = efi_earlymem_alloc(sys_table, &memsize, EFI_LOADER_DATA);
+	mem = efi_earlymem_alloc(sys_table, SZ_16M, EFI_BOOT_SERVICES_DATA);
 
-	mem_malloc_init(mem, mem + memsize - 1);
+	mem_malloc_init(mem, mem + SZ_16M - 1);
 
 	start_barebox();
 }
efi/payload/init.c +15 -0
index 239c5ce9ec18..5b827c57ed1f 100644
@@ -270,11 +270,26 @@ static int efi_init(void)
 }
 device_efi_initcall(efi_init);
 
+static void efi_request_mem(size_t bytes)
+{
+	efi_status_t efiret;
+	efi_physical_addr_t mem;
+	size_t pages;
+
+	pages = DIV_ROUND_UP(bytes, EFI_PAGE_SIZE);
+	efiret = BS->allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_LOADER_DATA,
+				    pages, &mem);
+	if (!EFI_ERROR(efiret))
+		malloc_add_pool(efi_phys_to_virt(mem), pages * EFI_PAGE_SIZE);
+}
+
 static int efi_core_init(void)
 {
 	struct device *dev;
 	int ret;
 
+	malloc_register_store(efi_request_mem);
+
 	dev = device_alloc("efi-cs", DEVICE_ID_SINGLE);
 	ret = platform_device_register(dev);
 	if (ret)
include/efi/efi-payload.h +1 -1
index d8e66a187a87..337471117d5a 100644
@@ -32,7 +32,7 @@ int efi_set_variable(char *name, efi_guid_t *vendor, uint32_t attributes,
 int efi_set_variable_usec(char *name, efi_guid_t *vendor, uint64_t usec);
 
 void *efi_earlymem_alloc(const struct efi_system_table *sys_table,
-			 size_t *memsize, enum efi_memory_type mem_type);
+			 size_t memsize, enum efi_memory_type mem_type);
 
 int efi_initrd_register(void *initrd, size_t initrd_size);
 void efi_initrd_unregister(void);
include/malloc.h +1 -0
index 35551250324e..69ff23b4a058 100644
@@ -23,6 +23,7 @@
 
 #ifdef CONFIG_MALLOC_TLSF
 void *malloc_add_pool(void *mem, size_t bytes);
+void malloc_register_store(void (*cb)(size_t bytes));
 #endif
 
 #if IN_PROPER
Signature · 1 line
2.34.1
#7 lore raw

PATCH v2 6/10 efi: payload: split image handling from legacy handover boot and add linux x86 image filetype

In reply to [email protected]

From: Chali Anis <[email protected]>

The handover boot support is Linux-specific and can be disabled with CONFIG_EFI_HANDOVER_PROTOCOL. In preparation for adding a compliant boot method, move the handover-specific parts into their own file. Introduce a new filetype for x86 Linux kernel images by checking for the "HdrS" magic (0x53726448) at offset 0x202 in the setup header.

---
 common/filetype.c          |   3 +
 efi/payload/Kconfig        |   5 +
 efi/payload/Makefile       |   1 +
 efi/payload/handover.c     | 154 +++++++++++++++++++++++++++++
 efi/payload/image.c        | 192 +++++--------------------------------
 efi/payload/image.h        |  14 +++
 efi/payload/setup_header.h |  63 ++++++++++++
 include/filetype.h         |   6 ++
 8 files changed, 269 insertions(+), 169 deletions(-)
 create mode 100644 efi/payload/handover.c
 create mode 100644 efi/payload/image.h
 create mode 100644 efi/payload/setup_header.h
common/filetype.c +3 -0
index eb397a175cb6..72f84442cae9 100644
@@ -87,6 +87,7 @@ static const struct filetype_str filetype_str[] = {
 	[filetype_zstd_compressed] = { "ZSTD compressed", "zstd" },
 	[filetype_rockchip_rkss_image] = { "Rockchip signed boot image",
 					   "rk-image" },
+	[filetype_x86_linux_image] = { "x86 Linux image", "x86-linux" },
 };
 
 static const char *file_type_to_nr_string(enum filetype f)
@@ -435,6 +436,8 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
 		return is_dos_exe(buf8) ? filetype_riscv_efi_linux_image : filetype_riscv_linux_image;
 	if (is_riscv_linux_bootimage(buf) && !memcmp(&buf[12], "barebox", 8))
 		return filetype_riscv_barebox_image;
+	if (bufsize > 0x206 && is_x86_linux_bootimage(buf))
+		return filetype_x86_linux_image;
 
 	if (le32_to_cpu(buf[5]) == 0x504d5453)
 		return filetype_mxs_bootstream;
efi/payload/Kconfig +5 -0
index a403acc81a93..d0a46704a6b9 100644
@@ -25,4 +25,9 @@ if EFI_PAYLOAD
 config EFI_STUB
 	def_bool HAVE_EFI_STUB
 
+config EFI_HANDOVER_PROTOCOL
+	bool "EFI Handover protocol"
+	default y
+	depends on X86
+
 endif
efi/payload/Makefile +1 -0
index 1250535302d3..d8b577bf3e23 100644
@@ -2,6 +2,7 @@
 
 obj-y += init.o
 obj-y += image.o
+obj-$(CONFIG_EFI_HANDOVER_PROTOCOL) += handover.o
 obj-y += efi-initrd.o
 obj-$(CONFIG_OFTREE) += fdt.o
 bbenv-y += env-efi
efi/payload/handover.c new file +154 -0

154 lines not on this page · raw message

efi/payload/image.c +23 -169

266 lines not on this page · raw message

efi/payload/image.h new file +14 -0
new file mode 100644
index 000000000000..673b21db839a
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __EFI_PAYLOAD_IMAGE_H__
+#define __EFI_PAYLOAD_IMAGE_H__
+
+#include <efi/types.h>
+
+int efi_load_image(const char *file, struct efi_loaded_image **loaded_image,
+		   efi_handle_t *h);
+
+int efi_execute_image(efi_handle_t handle,
+		      struct efi_loaded_image *loaded_image,
+		      enum filetype filetype);
+
+#endif
efi/payload/setup_header.h new file +63 -0

63 lines not on this page · raw message

include/filetype.h +6 -0
index 6f4a103df304..ff4d54ee3c45 100644
@@ -67,6 +67,7 @@ enum filetype {
 	filetype_nxp_fspi_image,
 	filetype_zstd_compressed,
 	filetype_rockchip_rkss_image,
+	filetype_x86_linux_image,
 	filetype_max,
 };
 
@@ -150,4 +151,9 @@ static inline bool is_riscv_linux_bootimage(const void *header)
 	return le32_to_cpup(header + 56) == 0x05435352;
 }
 
+static inline bool is_x86_linux_bootimage(const void *header)
+{
+	return le32_to_cpup(header + 0x202) == 0x53726448;
+}
+
 #endif /* __FILE_TYPE_H */
Signature · 1 line
2.34.1
#8 lore raw

PATCH v2 7/10 efi: payload: bootm: add support for efi stub boot

In reply to [email protected]

From: Chali Anis <[email protected]>

Introduce support for booting Linux kernels with an EFI stub from the barebox EFI payload. This includes:

- Loading the kernel image via EFI LoadImage/StartImage
- Installing the FDT into the EFI configuration table unless EFI_FDT_FORCE is enabled
- Providing the initrd either through the Initrd Media GUID or the initrd media protocol
- Registering image handlers for EFI applications, x86/ARM64 EFI Linux kernels, and a fallback handler for non-EFI x86 Linux images
- Adding Kconfig options to control FDT forcing and initrd installation

This enables barebox EFI payloads to directly boot Linux kernels with EFI stubs in a way compatible with the Linux EFI boot flow.

---
 efi/payload/Kconfig  |   6 +-
 efi/payload/Makefile |   1 +
 efi/payload/bootm.c  | 219 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 225 insertions(+), 1 deletion(-)
 create mode 100644 efi/payload/bootm.c
efi/payload/Kconfig +5 -1
index d0a46704a6b9..e3a6a72d6a1c 100644
@@ -27,7 +27,11 @@ config EFI_STUB
 
 config EFI_HANDOVER_PROTOCOL
 	bool "EFI Handover protocol"
-	default y
+	depends on !EFI_PAYLOAD_BOOTM
 	depends on X86
 
+config EFI_PAYLOAD_BOOTM
+	bool "EFI bootm protocol"
+	default !X86
+
 endif
efi/payload/Makefile +1 -0
index d8b577bf3e23..083728c53cb4 100644
@@ -3,6 +3,7 @@
 obj-y += init.o
 obj-y += image.o
 obj-$(CONFIG_EFI_HANDOVER_PROTOCOL) += handover.o
+obj-$(CONFIG_EFI_PAYLOAD_BOOTM) += bootm.o
 obj-y += efi-initrd.o
 obj-$(CONFIG_OFTREE) += fdt.o
 bbenv-y += env-efi
efi/payload/bootm.c new file +219 -0

219 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw

PATCH v2 8/10 efi: payload: bootm: add support for fit image

In reply to [email protected]

From: Ahmad Fatoum <[email protected]>

Extend the EFI payload boot flow to support loading kernel, initrd, and devicetree directly from a FIT image. When a FIT is present, the loader extracts the relevant subimages ("kernel", "ramdisk", and "fdt") also support falling back to external files when possible.

---
 efi/payload/Kconfig |   1 +
 efi/payload/bootm.c | 125 ++++++++++++++++++++++++++++++++++++--------
 2 files changed, 104 insertions(+), 22 deletions(-)
efi/payload/Kconfig +1 -0
index e3a6a72d6a1c..4fb866210a69 100644
@@ -32,6 +32,7 @@ config EFI_HANDOVER_PROTOCOL
 
 config EFI_PAYLOAD_BOOTM
 	bool "EFI bootm protocol"
+	select BOOTM_FITIMAGE
 	default !X86
 
 endif
efi/payload/bootm.c +103 -22

147 lines not on this page · raw message

Signature · 1 line
2.34.1
#10 lore raw

PATCH v2 9/10 efi: payload: make selectable without COMPILE_TEST

In reply to [email protected]

From: Ahmad Fatoum <[email protected]>

Now that the barebox EFI payload has support for a non-x86 boot method, let's drop the COMPILE_TEST dependency.

---
 efi/payload/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
efi/payload/Kconfig +1 -1
index 4fb866210a69..3b4a16e1e80c 100644
@@ -4,7 +4,7 @@ config HAVE_EFI_PAYLOAD
 	bool
 
 config EFI_PAYLOAD
-	bool "Build as EFI payload" if COMPILE_TEST
+	bool "Build as EFI payload"
 	depends on HAVE_EFI_PAYLOAD
 	select PBL_FULLY_PIC if ARM64
 	select EFI
Signature · 1 line
2.34.1
#11 lore raw

PATCH v2 10/10 ARM: efi: add a generic defconfig for v8 efi payload

In reply to [email protected]

From: Chali Anis <[email protected]>

The multi_v8_efi_defconfig enables a lot of drivers that are not needed for an EFI payload. Therefore add a minimal EFI-only config.

---
 arch/arm/configs/efi_v8_defconfig | 212 ++++++++++++++++++++++++++++++
 1 file changed, 212 insertions(+)
 create mode 100644 arch/arm/configs/efi_v8_defconfig
arch/arm/configs/efi_v8_defconfig new file +212 -0
new file mode 100644
index 000000000000..5f946dd51d4a
@@ -0,0 +1,212 @@
+CONFIG_64BIT=y
+CONFIG_ARM_OPTIMZED_STRING_FUNCTIONS=y
+CONFIG_ARM_PSCI_CLIENT=y
+CONFIG_NAME="efi_v8_defconfig"
+CONFIG_MMU=y
+CONFIG_MALLOC_SIZE=0x0
+CONFIG_KALLSYMS=y
+CONFIG_PROMPT="barebox> "
+CONFIG_PROMPT_HUSH_PS2="y"
+CONFIG_HUSH_FANCY_PROMPT=y
+CONFIG_AUTO_COMPLETE=y
+CONFIG_MENU=y
+CONFIG_BOOTM_SHOW_TYPE=y
+CONFIG_BOOTM_VERBOSE=y
+CONFIG_BOOTM_INITRD=y
+CONFIG_BOOTM_AIMAGE=y
+CONFIG_BLSPEC=y
+CONFIG_CONSOLE_ALLOW_COLOR=y
+CONFIG_PBL_CONSOLE=y
+CONFIG_CONSOLE_RATP=y
+CONFIG_PARTITION_DISK_EFI=y
+CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW=y
+CONFIG_STATE=y
+CONFIG_BOOTCHOOSER=y
+CONFIG_RESET_SOURCE=y
+CONFIG_MACHINE_ID=y
+CONFIG_FASTBOOT_SPARSE=y
+CONFIG_EFI_PAYLOAD=y
+CONFIG_CMD_DMESG=y
+CONFIG_LONGHELP=y
+CONFIG_CMD_IOMEM=y
+CONFIG_CMD_IMD=y
+CONFIG_CMD_MEMINFO=y
+CONFIG_CMD_ARM_MMUINFO=y
+CONFIG_CMD_REGULATOR=y
+CONFIG_CMD_BFETCH=y
+CONFIG_CMD_MMC_EXTCSD=y
+CONFIG_CMD_POLLER=y
+CONFIG_CMD_SLICE=y
+CONFIG_CMD_GO=y
+CONFIG_CMD_LOADB=y
+CONFIG_CMD_RESET=y
+CONFIG_CMD_BOOTCHOOSER=y
+CONFIG_CMD_PARTITION=y
+CONFIG_CMD_FINDMNT=y
+CONFIG_CMD_EXPORT=y
+CONFIG_CMD_DEFAULTENV=y
+CONFIG_CMD_LOADENV=y
+CONFIG_CMD_PRINTENV=y
+CONFIG_CMD_MAGICVAR=y
+CONFIG_CMD_MAGICVAR_HELP=y
+CONFIG_CMD_SAVEENV=y
+CONFIG_CMD_FILETYPE=y
+CONFIG_CMD_LN=y
+CONFIG_CMD_MD5SUM=y
+CONFIG_CMD_SHA1SUM=y
+CONFIG_CMD_SHA224SUM=y
+CONFIG_CMD_SHA256SUM=y
+CONFIG_CMD_UNCOMPRESS=y
+CONFIG_CMD_LET=y
+CONFIG_CMD_MSLEEP=y
+CONFIG_CMD_READF=y
+CONFIG_CMD_SLEEP=y
+CONFIG_CMD_DHCP=y
+CONFIG_CMD_MIITOOL=y
+CONFIG_CMD_PING=y
+CONFIG_CMD_TFTP=y
+CONFIG_CMD_ECHO_E=y
+CONFIG_CMD_EDIT=y
+CONFIG_CMD_MENU=y
+CONFIG_CMD_MENU_MANAGEMENT=y
+CONFIG_CMD_MENUTREE=y
+CONFIG_CMD_SPLASH=y
+CONFIG_CMD_FBTEST=y
+CONFIG_CMD_READLINE=y
+CONFIG_CMD_TIMEOUT=y
+CONFIG_CMD_CRC=y
+CONFIG_CMD_CRC_CMP=y
+CONFIG_CMD_MEMTEST=y
+CONFIG_CMD_MM=y
+CONFIG_CMD_CLK=y
+CONFIG_CMD_DETECT=y
+CONFIG_CMD_FLASH=y
+CONFIG_CMD_LED=y
+CONFIG_CMD_POWEROFF=y
+CONFIG_CMD_SMC=y
+CONFIG_CMD_LED_TRIGGER=y
+CONFIG_CMD_USBGADGET=y
+CONFIG_CMD_WD=y
+CONFIG_CMD_LOGIN=y
+CONFIG_CMD_PASSWD=y
+CONFIG_CMD_BAREBOX_UPDATE=y
+CONFIG_CMD_FIRMWARELOAD=y
+CONFIG_CMD_OF_DIFF=y
+CONFIG_CMD_OF_NODE=y
+CONFIG_CMD_OF_PROPERTY=y
+CONFIG_CMD_OF_DISPLAY_TIMINGS=y
+CONFIG_CMD_OF_FIXUP_STATUS=y
+CONFIG_CMD_OF_OVERLAY=y
+CONFIG_CMD_OFTREE=y
+CONFIG_CMD_TIME=y
+CONFIG_NET=y
+CONFIG_NET_NETCONSOLE=y
+CONFIG_NET_SNTP=y
+CONFIG_NET_FASTBOOT=y
+CONFIG_NET_9P=y
+CONFIG_NET_9P_VIRTIO=y
+CONFIG_DEEP_PROBE_DEFAULT=y
+CONFIG_OF_BAREBOX_DRIVERS=y
+CONFIG_OF_BAREBOX_ENV_IN_FS=y
+CONFIG_OF_OVERLAY_LIVE=y
+CONFIG_SERIAL_DEV_BUS=y
+CONFIG_SERIAL_AMBA_PL011=y
+CONFIG_DRIVER_SERIAL_EFI_STDIO=y
+CONFIG_DRIVER_SERIAL_NS16550=y
+CONFIG_DRIVER_SERIAL_CADENCE=y
+CONFIG_VIRTIO_CONSOLE=y
+CONFIG_DRIVER_NET_FSL_ENETC=y
+CONFIG_DRIVER_NET_FSL_FMAN=y
+CONFIG_DRIVER_NET_VIRTIO=y
+CONFIG_DP83867_PHY=y
+CONFIG_MICREL_PHY=y
+CONFIG_REALTEK_PHY=y
+CONFIG_SMSC_PHY=y
+CONFIG_NET_DSA_MV88E6XXX=y
+CONFIG_MDIO_BITBANG=y
+CONFIG_NET_USB=y
+CONFIG_NET_USB_ASIX=y
+CONFIG_USB_NET_AX88179_178A=y
+CONFIG_NET_USB_SMSC95XX=y
+CONFIG_NET_USB_RTL8152=y
+CONFIG_DRIVER_SPI_FSL_QUADSPI=y
+CONFIG_I2C=y
+CONFIG_I2C_EFI=y
+CONFIG_MTD=y
+CONFIG_MTD_CONCAT=y
+CONFIG_MTD_M25P80=y
+CONFIG_DRIVER_CFI=y
+CONFIG_CFI_BUFFER_WRITE=y
+CONFIG_VIRTIO_BLK=y
+CONFIG_USB_HOST=y
+CONFIG_USB_DWC3=y
+CONFIG_USB_DWC3_DUAL_ROLE=y
+CONFIG_USB_EHCI=y
+CONFIG_USB_STORAGE=y
+CONFIG_USB_ONBOARD_DEV=y
+CONFIG_USB_GADGET=y
+CONFIG_USB_GADGET_SERIAL=y
+CONFIG_USB_GADGET_FASTBOOT=y
+CONFIG_USB_GADGET_MASS_STORAGE=y
+CONFIG_VIDEO=y
+CONFIG_FRAMEBUFFER_CONSOLE=y
+CONFIG_DRIVER_VIDEO_EFI_GOP=y
+CONFIG_DRIVER_VIDEO_BOCHS_PCI=y
+CONFIG_SOUND=y
+CONFIG_MCI=y
+CONFIG_MCI_MMC_BOOT_PARTITIONS=y
+CONFIG_MCI_DW=y
+CONFIG_MCI_SUNXI_SMHC=y
+CONFIG_MCI_ROCKCHIP_DWCMSHC=y
+CONFIG_MCI_ARASAN=y
+CONFIG_MCI_AM654=y
+CONFIG_COMMON_CLK=y
+CONFIG_COMMON_CLK_SCMI=y
+CONFIG_CLOCKSOURCE_EFI=y
+CONFIG_RAVE_SP_CORE=y
+CONFIG_MFD_RK808=y
+CONFIG_LED=y
+CONFIG_LED_TRIGGERS=y
+CONFIG_EEPROM_AT25=y
+CONFIG_VIRTIO_INPUT=y
+CONFIG_WATCHDOG=y
+CONFIG_WATCHDOG_POLLER=y
+CONFIG_WATCHDOG_EFI=y
+CONFIG_WATCHDOG_DW=y
+CONFIG_RAVE_SP_WATCHDOG=y
+CONFIG_HWRNG=y
+CONFIG_HW_RANDOM_VIRTIO=y
+CONFIG_HW_RANDOM_EFI=y
+CONFIG_DMADEVICES=y
+CONFIG_PINCTRL_SINGLE=y
+CONFIG_REGULATOR=y
+CONFIG_REGULATOR_ARM_SCMI=y
+CONFIG_REGULATOR_RK808=y
+CONFIG_RESET_IMX7=y
+CONFIG_PCI_ECAM_GENERIC=y
+CONFIG_RTC_CLASS=y
+CONFIG_ARM_SCMI_PROTOCOL=y
+CONFIG_GENERIC_PHY=y
+CONFIG_USB_NOP_XCEIV=y
+CONFIG_VIRTIO_MMIO=y
+CONFIG_VIRTIO_PCI=y
+CONFIG_MAILBOX=y
+CONFIG_FS_CRAMFS=y
+CONFIG_FS_EXT4=y
+CONFIG_FS_TFTP=y
+CONFIG_FS_NFS=y
+CONFIG_9P_FS=y
+CONFIG_9P_FS_WRITE=y
+CONFIG_FS_EFI=y
+CONFIG_FS_EFIVARFS=y
+CONFIG_FS_FAT=y
+CONFIG_FS_FAT_WRITE=y
+CONFIG_FS_BPKFS=y
+CONFIG_FS_UIMAGEFS=y
+CONFIG_FS_PSTORE=y
+CONFIG_FS_PSTORE_CONSOLE=y
+CONFIG_FS_RATP=y
+CONFIG_DIGEST_SHA1_ARM64_CE=y
+CONFIG_DIGEST_SHA256_ARM64_CE=y
+CONFIG_LZO_DECOMPRESS=y
+# CONFIG_MISSING_FIRMWARE_ERROR is not set
Signature · 1 line
2.34.1
Applied
#12 lore raw
In reply to [email protected]
Quoted 5 lines · On Mon, 06 Oct 2025 20:59:46 -0400, [email protected] wrote:

We are already keeping different directories for payload and common EFI support. In preparation for adding more payload-specific config options, move the existing ones into their own Kconfig file.

Applied, thanks!

[01/10] efi: payload: split out payload Kconfig symbols
https://git.pengutronix.de/cgit/barebox/commit/?id=4ba4e23652ea (link may not be stable)
[02/10] tlsf: move kasan_poison_shadow into tlsf_add_pool
https://git.pengutronix.de/cgit/barebox/commit/?id=4fd2c32546d6 (link may not be stable)
[03/10] tlsf: support on-demand requesting memory from external store
https://git.pengutronix.de/cgit/barebox/commit/?id=7e43bc6e8f8d (link may not be stable)
[04/10] efi: payload: add support for EFI initrd media protocol
https://git.pengutronix.de/cgit/barebox/commit/?id=9ef8a6e873dc (link may not be stable)
[05/10] efi: payload: earlymem: allocate only the barebox needs in term of memory
https://git.pengutronix.de/cgit/barebox/commit/?id=ca4d3c34699c (link may not be stable)
[06/10] efi: payload: split image handling from legacy handover boot and add linux x86 image filetype

        https://git.pengutronix.de/cgit/barebox/commit/?id=24d09817e712 (link may not be stable)

[07/10] efi: payload: bootm: add support for efi stub boot
https://git.pengutronix.de/cgit/barebox/commit/?id=d1485062fc94 (link may not be stable)
[08/10] efi: payload: bootm: add support for fit image
https://git.pengutronix.de/cgit/barebox/commit/?id=cce4c24a1e85 (link may not be stable)
[09/10] efi: payload: make selectable without COMPILE_TEST
https://git.pengutronix.de/cgit/barebox/commit/?id=063da7a655c1 (link may not be stable)
[10/10] ARM: efi: add a generic defconfig for v8 efi payload
https://git.pengutronix.de/cgit/barebox/commit/?id=487cd4e91a99 (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>