Skip to contentSkip to conversation

boot/image-fit.c: Resort to using malloc for USE_HOSTCC

PATCH In review Tom Rini 2 participants 2 messages Dec 2, 2025

lore
Share & tools
lore

Tom Rini

author
#0 lore raw

PATCH v2 1/1 boot/image-fit.c: Resort to using malloc for USE_HOSTCC

With the changes in commit 8fbcc0e0e839 ("boot: Assure FDT is always at 8-byte aligned address") to call memalign(...) we now always call memalign(...) rather than malloc(...) when allocating a buffer that may contain a device tree. However, memalign(...) is not portable among all of the host OSes we support. Specifically on macOS it isn't seemingly possible to get memalign exactly. To keep alignment we would need to rework to use posix_memalign(...) for USE_HOSTCC and memalign(...) for U-Boot itself. This would get rather tricky. However, given the limited use case in question, it makes more sense to fall back to malloc(...) for the USE_HOSTCC case.

Fixes 8fbcc0e0e839 ("boot: Assure FDT is always at 8-byte aligned address")

---
Cc: Marek Vasut <[email protected]>

My biggest take-away from this is that I need to move up the plans I had about asking if anyone even uses the host tools on Windows / macOS from post-v2026.01 to instead be later today.

This change is a work-around at best. Ideally, we can drop Windows / macOS host build support and revert this commit at that time.

Changes in v2:
- Rework things entirely, and elaborate on the commit message.

---
 boot/image-fit.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
boot/image-fit.c +14 -1
index cccaa48f6839..3323ca1020d1 100644
@@ -23,6 +23,7 @@
 #include <log.h>
 #include <mapmem.h>
 #include <asm/io.h>
+#include <malloc.h>
 #include <memalign.h>
 #include <asm/global_data.h>
 #ifdef CONFIG_DM_HASH
@@ -35,7 +36,6 @@ DECLARE_GLOBAL_DATA_PTR;
 #include <bootm.h>
 #include <image.h>
 #include <bootstage.h>
-#include <malloc.h>
 #include <upl.h>
 #include <u-boot/crc.h>
 
@@ -2279,7 +2279,18 @@ int fit_image_load(struct bootm_headers *images, ulong addr,
 
 		log_debug("decompressing image\n");
 		if (load == data) {
+			/*
+			 * This could be a compressed device tree, and device
+			 * trees require 8 byte alignment for use. However, in
+			 * userspace we aren't using the tree, and getting at
+			 * a portable memalign is extremely difficult. Continue
+			 * to malloc in the host case.
+			 */
+#ifdef USE_HOSTCC
+			loadbuf = malloc(max_decomp_len);
+#else
 			loadbuf = memalign(8, max_decomp_len);
+#endif
 			load = map_to_sysmem(loadbuf);
 		} else {
 			loadbuf = map_sysmem(load, max_decomp_len);
@@ -2291,11 +2302,13 @@ int fit_image_load(struct bootm_headers *images, ulong addr,
 			return -ENOEXEC;
 		}
 		len = load_end - load;
+#ifndef USE_HOSTCC
 	} else if (load_op != FIT_LOAD_IGNORED && image_type == IH_TYPE_FLATDT &&
 		   ((uintptr_t)buf & 7)) {
 		loadbuf = memalign(8, len);
 		load = map_to_sysmem(loadbuf);
 		memcpy(loadbuf, buf, len);
+#endif
 	} else if (load != data) {
 		log_debug("copying\n");
 		loadbuf = map_sysmem(load, len);
Signature · 1 line
2.43.0
#1 lore raw

Hi Tom,

Quoted 21 lines · Tom Rini

With the changes in commit 8fbcc0e0e839 ("boot: Assure FDT is always at 8-byte aligned address") to call memalign(...) we now always call memalign(...) rather than malloc(...) when allocating a buffer that may contain a device tree. However, memalign(...) is not portable among all of the host OSes we support. Specifically on macOS it isn't seemingly possible to get memalign exactly. To keep alignment we would need to rework to use posix_memalign(...) for USE_HOSTCC and memalign(...) for U-Boot itself. This would get rather tricky. However, given the limited use case in question, it makes more sense to fall back to malloc(...) for the USE_HOSTCC case.

Fixes 8fbcc0e0e839 ("boot: Assure FDT is always at 8-byte aligned address")

---
Cc: Marek Vasut <[email protected]>

My biggest take-away from this is that I need to move up the plans I had about asking if anyone even uses the host tools on Windows / macOS from post-v2026.01 to instead be later today.

Quoted 9 lines

This change is a work-around at best. Ideally, we can drop Windows / macOS host build support and revert this commit at that time.

Changes in v2:
- Rework things entirely, and elaborate on the commit message.

---
 boot/image-fit.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

That patch was not a great solution. There is no indication in the commit message that it is doing an allocation:

commit 8fbcc0e0e839a8e25f636c76e59311033d3817b5

Author: Marek Vasut <[email protected]>
Date:   Thu Nov 13 12:54:51 2025 +0100

boot: Assure FDT is always at 8-byte aligned address

The fitImage may contain FDT at 4-byte aligned address, because alignment of DT tags is 4 bytes. However, libfdt and also Linux expects DT to be at 8-byte aligned address. Make sure that the DTs embedded in fitImages are always used from 8-byte aligned addresses. In case the DT is decompressed, make sure the target buffer is 8-byte aligned. In case the DT is only loaded, make sure the target buffer is 8-byte aligned too.

Regards,
Simon