Skip to contentSkip to conversation

boot/image-fit.c: Use aligned_alloc(...) not memalign(...)

PATCH In review Tom Rini 1 participant 2 messages Dec 2, 2025 – Dec 4, 2025

lore
Share & tools
lore

Tom Rini

author
#0 lore raw

PATCH v3 1/1 boot/image-fit.c: Use aligned_alloc(...) not memalign(...)

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. The C11 standard does require that aligned_alloc(...) exist and it takes the same parameters as memalign(...) does. Change this file to call aligned_alloc rather than memalign, and for the non-USE_HOSTCC case define that function back to memalign.

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

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

Changes in v3:
- Per Heinrich's suggestion, make use of aligned_alloc(...) rather than memalign(...) as C11 requires that function to exist.

---
 boot/image-fit.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
boot/image-fit.c +8 -3
index cccaa48f6839..fce3a320eac7 100644
@@ -16,6 +16,9 @@
 #include <linux/libfdt.h>
 #include <u-boot/crc.h>
 #include <linux/kconfig.h>
+
+/* C11 standard function for aligned allocations */
+extern void *aligned_alloc(size_t alignment, size_t size);
 #else
 #include <linux/compiler.h>
 #include <linux/sizes.h>
@@ -23,19 +26,21 @@
 #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
 #include <dm.h>
 #include <u-boot/hash.h>
 #endif
+#define aligned_alloc(a, s)	memalign((a), (s))
+
 DECLARE_GLOBAL_DATA_PTR;
 #endif /* !USE_HOSTCC*/
 
 #include <bootm.h>
 #include <image.h>
 #include <bootstage.h>
-#include <malloc.h>
 #include <upl.h>
 #include <u-boot/crc.h>
 
@@ -2279,7 +2284,7 @@ int fit_image_load(struct bootm_headers *images, ulong addr,
 
 		log_debug("decompressing image\n");
 		if (load == data) {
-			loadbuf = memalign(8, max_decomp_len);
+			loadbuf = aligned_alloc(8, max_decomp_len);
 			load = map_to_sysmem(loadbuf);
 		} else {
 			loadbuf = map_sysmem(load, max_decomp_len);
@@ -2293,7 +2298,7 @@ int fit_image_load(struct bootm_headers *images, ulong addr,
 		len = load_end - load;
 	} else if (load_op != FIT_LOAD_IGNORED && image_type == IH_TYPE_FLATDT &&
 		   ((uintptr_t)buf & 7)) {
-		loadbuf = memalign(8, len);
+		loadbuf = aligned_alloc(8, len);
 		load = map_to_sysmem(loadbuf);
 		memcpy(loadbuf, buf, len);
 	} else if (load != data) {
Signature · 1 line
2.43.0
author
#1 lore raw
Quoted 11 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. The C11 standard does require that aligned_alloc(...) exist and it takes the same parameters as memalign(...) does. Change this file to call aligned_alloc rather than memalign, and for the non-USE_HOSTCC case define that function back to memalign.

[...]

Applied to u-boot/next, thanks!

[1/1] boot/image-fit.c: Use aligned_alloc(...) not memalign(...)

      commit: abd6e0f252ee17b18e98be69d87aaca6a26e8336
Signature · 1 line
Tom