boot: image-fit: Add runtime flag to always relocate overlays
PATCH 1/1 boot: image-fit: Add runtime flag to always relocate overlays
Some platforms require device tree overlays (DTOs) to be 8-byte aligned when applying them with libfdt. Previously, the code did not guarantee this alignment, which could result in alignment errors during overlay application.
Introduce a new runtime parameter, always_relocate_overlay, to boot_get_fdt_fit(). When set, overlays are always copied to an 8-byte aligned buffer before being passed to fdt_open_into().
This resolves issues with FDT overlay application on platforms that require strict alignment.
--- boot/image-fdt.c | 2 +- boot/image-fit.c | 23 +++++++++++++++++------ include/image.h | 5 ++++- 3 files changed, 22 insertions(+), 8 deletions(-)
boot/image-fdt.c +1 -1
@@ -431,7 +431,7 @@ static int select_fdt(struct bootm_headers *images, const char *select, u8 arch, fdt_noffset =boot_get_fdt_fit (images, fdt_addr, &fit_uname_fdt, &fit_uname_config, - arch, &load, &len); + arch, &load, &len, true);if (fdt_noffset <0 )return -ENOENT ;
boot/image-fit.c +17 -6
@@ -2370,7 +2370,8 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch,#ifndef USE_HOSTCC int boot_get_fdt_fit (struct bootm_headers *images,ulong addr,const char **fit_unamep,const char **fit_uname_configp, -int arch,ulong *datap,ulong *lenp) +int arch,ulong *datap,ulong *lenp, +bool always_relocate_overlay) {int fdt_noffset, cfg_noffset, count;const void *fit;
@@ -2492,11 +2493,21 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, ov =map_sysmem (ovload, ovlen); ovcopylen =ALIGN (fdt_totalsize (ov),SZ_4K ); - ovcopy =malloc (ovcopylen); -if (!ovcopy) { -printf ("failed to duplicate DTO before application\n" ); - fdt_noffset = -ENOMEM ; - goto out; +if (always_relocate_overlay) { + ovcopy =aligned_alloc (8 , ovcopylen); +if (!ovcopy) { +printf ("failed to allocate aligned DTO buffer\n" ); + fdt_noffset = -ENOMEM ; + goto out; + } +memcpy (ovcopy, ov, ovlen); + }else { + ovcopy =malloc (ovcopylen); +if (!ovcopy) { +printf ("failed to duplicate DTO before application\n" ); + fdt_noffset = -ENOMEM ; + goto out; + } } err =fdt_open_into (ov, ovcopy, ovcopylen);
include/image.h +4 -1
@@ -707,12 +707,15 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch,* @param arch Expected architecture (IH_ARCH_...) * @param datap Returns address of loaded image * @param lenp Returns length of loaded image +* @param always_relocate_overlay If true, always copy overlays to an 8-byte +* aligned buffer before applying (prevents alignment errors) * * Return: node offset of base image, or -ve error code on error */ int boot_get_fdt_fit (struct bootm_headers *images,ulong addr,const char **fit_unamep,const char **fit_uname_configp, -int arch,ulong *datap,ulong *lenp); +int arch,ulong *datap,ulong *lenp, +bool always_relocate_overlay);/** * fit_image_load() - load an image from a FIT
Signature · 1 line
2.43.0
Quoted 1 line · Jamie Gibbons
Some platforms
All platforms, DTs and DTOs have to be 8 Byte aligned, always, period.
Quoted 24 lines
require device tree overlays (DTOs) to be 8-byte aligned when applying them with libfdt. Previously, the code did not guarantee this alignment, which could result in alignment errors during overlay application.
Introduce a new runtime parameter,always_relocate_overlay, toboot_get_fdt_fit(). When set, overlays are always copied to an 8-byte aligned buffer before being passed tofdt_open_into().
This resolves issues with FDT overlay application on platforms that require strict alignment.Fixes 8fbcc0e0e839 ("boot: Assure FDT is always at 8-byte aligned address")Signed-off-by Jamie Gibbons <[email protected]>--- boot/image-fdt.c | 2 +- boot/image-fit.c | 23 +++++++++++++++++------ include/image.h | 5 ++++- 3 files changed, 22 insertions(+), 8 deletions(-)boot/image-fdt.c +0
-0
boot/image-fdt.c static int select_fdt(struct bootm_headers *images, const char *select, u8 arch, · line 434
Quoted 5 lines
boot/image-fdt.c +1
-1@@ -431,7 +431,7 @@ static int select_fdt(struct bootm_headers *images, const char *select, u8 arch, fdt_noffset =boot_get_fdt_fit (images, fdt_addr, &fit_uname_fdt, &fit_uname_config, - arch, &load, &len); + arch, &load, &len, true);
This flag is always true, do we actually need it ?
Quoted 30 lines
boot/image-fdt.c +0
-0if (fdt_noffset <0 )return -ENOENT ;boot/image-fit.c +7
-6@@ -2370,7 +2370,8 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch,#ifndef USE_HOSTCC int boot_get_fdt_fit (struct bootm_headers *images,ulong addr,const char **fit_unamep,const char **fit_uname_configp, -int arch,ulong *datap,ulong *lenp) +int arch,ulong *datap,ulong *lenp, +bool always_relocate_overlay) {int fdt_noffset, cfg_noffset, count;const void *fit;@@ -2492,11 +2493,21 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, ov =map_sysmem (ovload, ovlen); ovcopylen =ALIGN (fdt_totalsize (ov),SZ_4K ); - ovcopy =malloc (ovcopylen); -if (!ovcopy) { -printf ("failed to duplicate DTO before application\n" ); - fdt_noffset = -ENOMEM ; - goto out; +if (always_relocate_overlay) { + ovcopy =aligned_alloc (8 , ovcopylen); +if (!ovcopy) { +printf ("failed to allocate aligned DTO buffer\n" ); + fdt_noffset = -ENOMEM ;
boot/image-fit.c int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, · line 2510
Quoted 10 lines
boot/image-fit.c +10
-0+ goto out; + } +memcpy (ovcopy, ov, ovlen); + }else { + ovcopy =malloc (ovcopylen); +if (!ovcopy) { +printf ("failed to duplicate DTO before application\n" ); + fdt_noffset = -ENOMEM ; + goto out; + }
Looking at this, if the problem is $ov aligned to 4 Bytes, then $base a bit below could also be aligned to 4 Bytes if there are two or more DTs in the fitImage without external data. Can you please try the attached patch and see if that still works ?
boot/image-fit.c +46 -15
@@ -2368,6 +2368,48 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, }#ifndef USE_HOSTCC +#ifdef CONFIG_OF_LIBFDT_OVERLAY +static int boot_get_fdt_fit_into_buffer (ulong loadaddr,ulong loadlen,void **fdtdstbuf) +{ +void *fdtsrcbuf, *map; +ulong fdtdstlen; +int err; + + map =map_sysmem (loadaddr, loadlen); + +/* Set up source buffer, make sure DT in the buffer is 8-Bytes aligned */ +if (!IS_ALIGNED ((uintptr_t )map,8 )) { + fdtsrcbuf =aligned_alloc (8 , loadlen); +if (!fdtsrcbuf) +return -ENOMEM ; +memcpy (fdtsrcbuf, map, loadlen); + }else { + fdtsrcbuf = map; + } + +/* Set up destination buffer, make sure DT in the buffer is 8-Bytes aligned */ + fdtdstlen =ALIGN (fdt_totalsize (fdtsrcbuf),SZ_4K ); + fdtdstbuf =aligned_alloc (8 , fdtdstlen); +if (!fdtdstbuf) { +printf ("failed to duplicate DTO before application\n" ); +if (!IS_ALIGNED ((uintptr_t )map,8 )) +free (fdtsrcbuf); +return -ENOMEM ; + } + + err =fdt_open_into (fdtsrcbuf, *fdtdstbuf, fdtdstlen); +if (!IS_ALIGNED ((uintptr_t )map,8 )) +free (fdtsrcbuf); +if (err <0 ) { +printf ("failed on fdt_open_into for DTO: %s\n" , +fdt_strerror (err)); +return err; + } + +return 0 ; +}; +#endif +int boot_get_fdt_fit (struct bootm_headers *images,ulong addr,const char **fit_unamep,const char **fit_uname_configp,int arch,ulong *datap,ulong *lenp)
@@ -2380,7 +2422,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr,char *next_config =NULL ;ulong load, len;#ifdef CONFIG_OF_LIBFDT_OVERLAY -ulong ovload, ovlen, ovcopylen; +ulong ovload, ovlen;const char *uconfig;const char *uname;/*
@@ -2391,7 +2433,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr,* Instead, let's be lazy and use void *. */ char *of_flat_tree; -void *base, *ov, *ovcopy =NULL ; +void *base, *ovcopy =NULL ;int i, err, noffset, ov_noffset;#endif
@@ -2489,17 +2531,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, }debug ("%s loaded at 0x%08lx len=0x%08lx\n" , uname, ovload, ovlen); - ov =map_sysmem (ovload, ovlen); - - ovcopylen =ALIGN (fdt_totalsize (ov),SZ_4K ); - ovcopy =malloc (ovcopylen); -if (!ovcopy) { -printf ("failed to duplicate DTO before application\n" ); - fdt_noffset = -ENOMEM ; - goto out; - } - - err =fdt_open_into (ov, ovcopy, ovcopylen); + err =boot_get_fdt_fit_into_buffer (ovload, ovlen, &ovcopy);if (err <0 ) {printf ("failed on fdt_open_into for DTO: %s\n" ,fdt_strerror (err));
@@ -2507,8 +2539,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, goto out; } - base =map_sysmem (load, len + ovlen); - err =fdt_open_into (base, base, len + ovlen); + err =boot_get_fdt_fit_into_buffer (load, len + ovlen, &base);if (err <0 ) {printf ("failed on fdt_open_into: %s\n" ,fdt_strerror (err));
Signature · 1 line
2.51.0
From 0444df2cb881cc4d943e224de795aa499b870a5c Mon Sep 17 00:00:00 2001
From: Jamie Gibbons <[email protected]>
Date: Fri, 6 Feb 2026 15:14:57 +0000
Subject: [PATCH] boot: image-fit: Add runtime flag to always relocate overlays
Some platforms require device tree overlays (DTOs) to be 8-byte aligned when applying them with libfdt. Previously, the code did not guarantee this alignment, which could result in alignment errors during overlay application.
Introduce a new runtime parameter, always_relocate_overlay, to boot_get_fdt_fit(). When set, overlays are always copied to an 8-byte aligned buffer before being passed to fdt_open_into().
This resolves issues with FDT overlay application on platforms that require strict alignment.
--- boot/image-fit.c | 61 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 15 deletions(-)
boot/image-fit.c +46 -15
91 lines not on this page · raw message
Signature · 1 line
2.51.0
Hi Marek,
Quoted 8 lines · Marek Vasut
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 1 line · Jamie Gibbons
Some platforms
All platforms, DTs and DTOs have to be 8 Byte aligned, always, period.
Thanks for your feedback. I have realised I misinterpreted Tom's instruction on my previous patch fix about passing a new flag.
Quoted 214 lines
Quoted 32 lines
require device tree overlays (DTOs) to be 8-byte aligned when applying them with libfdt. Previously, the code did not guarantee
this alignment, which could result in alignment errors during overlay
application.
Introduce a new runtime parameter,always_relocate_overlay, toboot_get_fdt_fit(). When set, overlays are always copied to an 8- byte
aligned buffer before being passed tofdt_open_into(). This resolves issues with FDT overlay application on platforms that require strict alignment.Fixes 8fbcc0e0e839 ("boot: Assure FDT is always at 8-byte alignedaddress")
Signed-off-by Jamie Gibbons <[email protected]>---
boot/image-fdt.c| 2 +-
boot/image-fit.c| 23 +++++++++++++++++------
include/image.h | 5 ++++-3 files changed, 22 insertions(+), 8 deletions(-)boot/image-fdt.c +0
-0@@ -431,7 +431,7 @@ static int select_fdt(struct bootm_headers *images, const char *select, u8 arch,
fdt_noffset=
boot_get_fdt_fit(images,fdt_addr,Quoted 8 lines
&fit_uname_fdt,&fit_uname_config,
- arch, &load, &len);
+ arch, &load, &len, true);This flag is always true, do we actually need it ?
Quoted 42 lines
if (
fdt_noffset< 0) return-ENOENT;boot/image-fit.c +0
-0@@ -2370,7 +2370,8 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, #ifndef USE_HOSTCC int boot_get_fdt_fit(struct bootm_headers *images, ulong addr,const char **fit_unamep, const char **fit_uname_configp,
- int arch, ulong *datap, ulong *lenp) + int arch, ulong *datap, ulong *lenp, + boolalways_relocate_overlay) {
intfdt_noffset,cfg_noffset, count; const void *fit;boot/image-fit.c +12
-5@@ -2492,11 +2493,21 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, ov = map_sysmem(ovload, ovlen); ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K); - ovcopy = malloc(ovcopylen); - if (!ovcopy) { - printf("failed to duplicate DTO before application\n"); - fdt_noffset = -ENOMEM; - goto out; + if (always_relocate_overlay) { + ovcopy = aligned_alloc(8, ovcopylen); + if (!ovcopy) { + printf("failed to allocate aligned DTO buffer\n"); + fdt_noffset = -ENOMEM; + goto out; + } + memcpy(ovcopy, ov, ovlen); + } else { + ovcopy = malloc(ovcopylen); + if (!ovcopy) { + printf("failed to duplicate DTOQuoted 4 lines
before application\n");
+fdt_noffset=-ENOMEM; + goto out;
+ }Looking at this, if the problem is $ov aligned to 4 Bytes, then $base a
bit below could also be aligned to 4 Bytes if there are two or more DTs
in the fitImage without external data. Can you please try the attached
patch and see if that still works ?boot/image-fit.c +10
-0@@ -2368,6 +2368,48 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, } #ifndef USE_HOSTCC +#ifdef CONFIG_OF_LIBFDT_OVERLAY +static int boot_get_fdt_fit_into_buffer (ulong loadaddr,ulong loadlen,void **fdtdstbuf) +{ + void *fdtsrcbuf, *map; + ulong fdtdstlen; + int err; + + map = map_sysmem(loadaddr, loadlen); + + /* Set up source buffer, make sure DT in the buffer is 8-Bytes aligned */
+ if (!IS_ALIGNED((uintptr_t)map, 8)) {
+ fdtsrcbuf =aligned_alloc(8, loadlen);
+ if (!fdtsrcbuf)
+ return-ENOMEM;
+memcpy(fdtsrcbuf, map, loadlen);
+ } else {
+ fdtsrcbuf = map;
+ }
+
+ /* Set up destination buffer, make sure DT in the buffer is 8-Bytes
aligned */
+ fdtdstlen =ALIGN(fdt_totalsize(fdtsrcbuf), SZ_4K); + fdtdstbuf =aligned_alloc(8, fdtdstlen);
+ if (!fdtdstbuf) {
+printf("failed to duplicate DTO before application\n");
+ if (!IS_ALIGNED((uintptr_t)map, 8))
+ free(fdtsrcbuf);
+ return-ENOMEM;
+ }boot/image-fit.c +14
-0+ + err = fdt_open_into(fdtsrcbuf, *fdtdstbuf, fdtdstlen); + if (!IS_ALIGNED((uintptr_t)map, 8)) + free(fdtsrcbuf); + if (err < 0) { + printf("failed on fdt_open_into for DTO: %s\n", + fdt_strerror(err)); + return err; + } + + return 0; +}; +#endif +int boot_get_fdt_fit(struct
bootm_headers*images, ulong addr, const char **fit_unamep, const char **fit_uname_configp,
int arch, ulong *datap, ulong *lenp)boot/image-fit.c +1
-1@@ -2380,7 +2422,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, char *next_config = NULL; ulong load, len; #ifdef CONFIG_OF_LIBFDT_OVERLAY - ulong ovload, ovlen, ovcopylen; + ulong ovload, ovlen; const char *uconfig;const char *uname;
/*boot/image-fit.c +1
-1@@ -2391,7 +2433,7 @@ int boot_get_fdt_fit(struct bootm_headers*images, ulong addr, * Instead, let's be lazy and use void *. */ char *of_flat_tree; - void *base, *ov, *ovcopy = NULL; + void *base, *ovcopy = NULL; int i, err, noffset, ov_noffset;#endif
boot/image-fit.c +1
-11@@ -2489,17 +2531,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, } debug("%s loaded at 0x%08lx len=0x%08lx\n", uname, ovload, ovlen); - ov = map_sysmem(ovload, ovlen); - - ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K); - ovcopy = malloc(ovcopylen); - if (!ovcopy) { - printf("failed to duplicate DTO before application\n"); - fdt_noffset = -ENOMEM; - goto out; - } - - err = fdt_open_into(ov, ovcopy, ovcopylen); + err = boot_get_fdt_fit_into_buffer(ovload, ovlen, &ovcopy);if (err < 0) {
printf("failed onfdt_open_intofor DTO:
%s\n",
fdt_strerror(err));boot/image-fit.c +1
-2@@ -2507,8 +2539,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, goto out; } - base = map_sysmem(load, len + ovlen); - err = fdt_open_into(base, base, len + ovlen); + err = boot_get_fdt_fit_into_buffer(load, len + ovlen, &base);
Quoted 5 lines
if (err < 0) {
printf("failed on fdt_open_into: %s\n", fdt_strerror(err));
--
2.51.0
Thanks for the suggestion. I tested your patch and found that, although overlays are applied to aligned buffers, the CPU OPP overlay does not appear in the final device tree in Linux. With my previous patch above (the alignment fix), the OPP overlay works. It looks like the new buffer management breaks overlay chaining or the final DT pointer isn’t updated correctly, so overlays aren’t present in the running device tree.
Let me know if you have suggestions for tracking the buffer or chaining overlays differently.
Thanks,
Jamie.
Quoted 233 lines · On 2/10/26 1:24 PM, [email protected] wrote:
Hi Marek,
Quoted 8 lines · Marek Vasut
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 1 line · Jamie Gibbons
Some platforms
All platforms, DTs and DTOs have to be 8 Byte aligned, always, period.
Thanks for your feedback. I have realised I misinterpreted Tom's instruction on my previous patch fix about passing a new flag.
Quoted 214 lines
Quoted 37 lines
require device tree overlays (DTOs) to be 8-byte aligned when applying them with libfdt. Previously, the code did not guarantee
this alignment, which could result in alignment errors during overlay
application.
Introduce a new runtime parameter,always_relocate_overlay, toboot_get_fdt_fit(). When set, overlays are always copied to an 8- byte
aligned buffer before being passed tofdt_open_into(). This resolves issues with FDT overlay application on platforms that require strict alignment.Fixes 8fbcc0e0e839 ("boot: Assure FDT is always at 8-byte alignedaddress")
Signed-off-by Jamie Gibbons <[email protected]>--- boot/image-fdt.c | 2 +- boot/image-fit.c | 23 +++++++++++++++++------ include/image.h | 5 ++++- 3 files changed, 22 insertions(+), 8 deletions(-)boot/image-fdt.c +0
-1@@ -431,7 +431,7 @@ static int select_fdt(struct bootm_headers *images, const char *select, u8 arch, fdt_noffset = boot_get_fdt_fit(images, fdt_addr, &fit_uname_fdt, &fit_uname_config, - arch,Quoted 3 lines
&load, &len);
+ arch, &load, &len, true);This flag is always true, do we actually need it ?
Quoted 42 lines
if (
fdt_noffset< 0) return-ENOENT;boot/image-fit.c +2
-1@@ -2370,7 +2370,8 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, #ifndef USE_HOSTCC int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, const char **fit_unamep, const char **fit_uname_configp, - int arch, ulong *datap, ulong *lenp) + int arch, ulong *datap, ulong *lenp, + bool always_relocate_overlay) {int
fdt_noffset,cfg_noffset, count;
const void *fit;boot/image-fit.c +12
-5@@ -2492,11 +2493,21 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, ov = map_sysmem(ovload, ovlen); ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K); - ovcopy = malloc(ovcopylen); - if (!ovcopy) { - printf("failed to duplicate DTO before application\n"); - fdt_noffset = -ENOMEM; - goto out; + if (always_relocate_overlay) { + ovcopy = aligned_alloc(8, ovcopylen); + if (!ovcopy) { + printf("failed to allocate aligned DTO buffer\n"); + fdt_noffset = -ENOMEM; + goto out; + } + memcpy(ovcopy, ov, ovlen); + } else { + ovcopy = malloc(ovcopylen); + if (!ovcopy) { + printf("failed to duplicate DTOQuoted 4 lines
before application\n");
+fdt_noffset=-ENOMEM; + goto out;
+ }Looking at this, if the problem is $ov aligned to 4 Bytes, then $base a
bit below could also be aligned to 4 Bytes if there are two or more DTs
in the fitImage without external data. Can you please try the attached
patch and see if that still works ?boot/image-fit.c +10
-0@@ -2368,6 +2368,48 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, } #ifndef USE_HOSTCC +#ifdef CONFIG_OF_LIBFDT_OVERLAY +static int boot_get_fdt_fit_into_buffer (ulong loadaddr,ulong loadlen,void **fdtdstbuf) +{ + void *fdtsrcbuf, *map; + ulong fdtdstlen; + int err; + + map = map_sysmem(loadaddr, loadlen); + + /* Set up source buffer, make sure DT in the buffer is 8-Bytes aligned */
+ if (!IS_ALIGNED((uintptr_t)map, 8)) {
+ fdtsrcbuf =aligned_alloc(8, loadlen);
+ if (!fdtsrcbuf)
+ return-ENOMEM;
+memcpy(fdtsrcbuf, map, loadlen);
+ } else {
+ fdtsrcbuf = map;
+ }
+
+ /* Set up destination buffer, make sure DT in the buffer is 8-Bytes
aligned */
+ fdtdstlen =ALIGN(fdt_totalsize(fdtsrcbuf), SZ_4K); + fdtdstbuf =aligned_alloc(8, fdtdstlen);
+ if (!fdtdstbuf) {
+printf("failed to duplicate DTO before application\n");
+ if (!IS_ALIGNED((uintptr_t)map, 8))
+ free(fdtsrcbuf);
+ return-ENOMEM;
+ }boot/image-fit.c +14
-0+ + err = fdt_open_into(fdtsrcbuf, *fdtdstbuf, fdtdstlen); + if (!IS_ALIGNED((uintptr_t)map, 8)) + free(fdtsrcbuf); + if (err < 0) { + printf("failed on fdt_open_into for DTO: %s\n", + fdt_strerror(err)); + return err; + } + + return 0; +}; +#endif + int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, const char **fit_unamep, const char**fit_uname_configp,
int arch, ulong *datap, ulong *lenp)boot/image-fit.c +1
-1@@ -2380,7 +2422,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, char *next_config = NULL; ulong load, len; #ifdef CONFIG_OF_LIBFDT_OVERLAY - ulong ovload, ovlen, ovcopylen; + ulong ovload, ovlen; const char *uconfig;const char *uname;
/*boot/image-fit.c +1
-1@@ -2391,7 +2433,7 @@ int boot_get_fdt_fit(struct bootm_headers*images, ulong addr, * Instead, let's be lazy and use void *. */ char *of_flat_tree; - void *base, *ov, *ovcopy = NULL; + void *base, *ovcopy = NULL; int i, err, noffset, ov_noffset;#endif
boot/image-fit.c +1
-11@@ -2489,17 +2531,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, } debug("%s loaded at 0x%08lx len=0x%08lx\n", uname, ovload, ovlen); - ov = map_sysmem(ovload, ovlen); - - ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K); - ovcopy = malloc(ovcopylen); - if (!ovcopy) { - printf("failed to duplicate DTO before application\n"); - fdt_noffset = -ENOMEM; - goto out; - } - - err = fdt_open_into(ov, ovcopy, ovcopylen); + err = boot_get_fdt_fit_into_buffer(ovload, ovlen, &ovcopy);if (err < 0) {
printf("failed onfdt_open_intofor DTO:
%s\n",
fdt_strerror(err));boot/image-fit.c +1
-2@@ -2507,8 +2539,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, goto out; } - base = map_sysmem(load, len + ovlen); - err = fdt_open_into(base, base, len + ovlen); + err = boot_get_fdt_fit_into_buffer(load, len + ovlen, &base);Quoted 5 lines
if (err < 0) {
printf("failed on fdt_open_into: %s\n", fdt_strerror(err));
--
2.51.0Thanks for the suggestion. I tested your patch and found that, although
Quoted 8 lines
overlays are applied to aligned buffers, the CPU OPP overlay does not appear in the final device tree in Linux. With my previous patch above (the alignment fix), the OPP overlay works. It looks like the new buffer management breaks overlay chaining or the final DT pointer isn’t updated correctly, so overlays aren’t present in the running device tree.
Let me know if you have suggestions for tracking the buffer or chaining overlays differently.
Maybe some memcpy is missing there somewhere ? Can you please have a look and debug this a bit ?
Thank you
Quoted 240 lines · Marek Vasut
Quoted 231 lines · On 2/10/26 1:24 PM, [email protected] wrote:
Hi Marek,
Quoted 8 lines · Marek Vasut
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 1 line · Jamie Gibbons
Some platforms
All platforms, DTs and DTOs have to be 8 Byte aligned, always, period.
Thanks for your feedback. I have realised I misinterpreted Tom's instruction on my previous patch fix about passing a new flag.
Quoted 212 lines
Quoted 30 lines
require device tree overlays (DTOs) to be 8-byte aligned when applying them with libfdt. Previously, the code did not guarantee
this alignment, which could result in alignment errors during overlay
application.
Introduce a new runtime parameter,always_relocate_overlay, toboot_get_fdt_fit(). When set, overlays are always copied to an 8- byte
aligned buffer before being passed tofdt_open_into(). This resolves issues with FDT overlay application on platforms that require strict alignment.Fixes 8fbcc0e0e839 ("boot: Assure FDT is always at 8-byte alignedaddress")
Signed-off-by Jamie Gibbons <[email protected]>---
boot/image-fdt.c| 2 +-
boot/image-fit.c| 23 +++++++++++++++++------
include/image.h | 5 ++++-3 files changed, 22 insertions(+), 8 deletions(-)boot/image-fdt.c +0
-0@@ -431,7 +431,7 @@ static int select_fdt(struct bootm_headers *images, const char *select, u8 arch,Quoted 8 lines
fdt_noffset= boot_get_fdt_fit(images,fdt_addr,&fit_uname_fdt,&fit_uname_config,
- arch, &load, &len);
+ arch, &load, &len, true);This flag is always true, do we actually need it ?
Quoted 42 lines
if (
fdt_noffset< 0) return-ENOENT;boot/image-fit.c +0
-0@@ -2370,7 +2370,8 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, #ifndef USE_HOSTCC int boot_get_fdt_fit(struct bootm_headers *images, ulong addr,const char **fit_unamep, const char **fit_uname_configp,
- int arch, ulong *datap, ulong *lenp) + int arch, ulong *datap, ulong *lenp, + boolalways_relocate_overlay) {
intfdt_noffset,cfg_noffset, count; const void *fit;boot/image-fit.c +12
-5@@ -2492,11 +2493,21 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, ov = map_sysmem(ovload, ovlen); ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K); - ovcopy = malloc(ovcopylen); - if (!ovcopy) { - printf("failed to duplicate DTO before application\n"); - fdt_noffset = -ENOMEM; - goto out; + if (always_relocate_overlay) { + ovcopy = aligned_alloc(8, ovcopylen); + if (!ovcopy) { + printf("failed to allocate aligned DTO buffer\n"); + fdt_noffset = -ENOMEM; + goto out; + } + memcpy(ovcopy, ov, ovlen); + } else { + ovcopy = malloc(ovcopylen); + if (!ovcopy) { + printf("failed to duplicate DTOQuoted 4 lines
before application\n");
+fdt_noffset=-ENOMEM; + goto out;
+ }Looking at this, if the problem is $ov aligned to 4 Bytes, then $base a
bit below could also be aligned to 4 Bytes if there are two or more DTs
in the fitImage without external data. Can you please try the attached
patch and see if that still works ?boot/image-fit.c +10
-0@@ -2368,6 +2368,48 @@ int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, } #ifndef USE_HOSTCC +#ifdef CONFIG_OF_LIBFDT_OVERLAY +static int boot_get_fdt_fit_into_buffer (ulong loadaddr,ulong loadlen,void **fdtdstbuf) +{ + void *fdtsrcbuf, *map; + ulong fdtdstlen; + int err; + + map = map_sysmem(loadaddr, loadlen); + + /* Set up source buffer, make sure DT in the buffer is 8-Bytes aligned */
+ if (!IS_ALIGNED((uintptr_t)map, 8)) {
+ fdtsrcbuf =aligned_alloc(8, loadlen);
+ if (!fdtsrcbuf)
+ return-ENOMEM;
+memcpy(fdtsrcbuf, map, loadlen);
+ } else {
+ fdtsrcbuf = map;
+ }
+
+ /* Set up destination buffer, make sure DT in the buffer is 8-Bytes
aligned */
+ fdtdstlen =ALIGN(fdt_totalsize(fdtsrcbuf), SZ_4K); + fdtdstbuf =aligned_alloc(8, fdtdstlen);
+ if (!fdtdstbuf) {
+printf("failed to duplicate DTO before application\n");
+ if (!IS_ALIGNED((uintptr_t)map, 8))
+ free(fdtsrcbuf);
+ return-ENOMEM;
+ }boot/image-fit.c +14
-0+ + err = fdt_open_into(fdtsrcbuf, *fdtdstbuf, fdtdstlen); + if (!IS_ALIGNED((uintptr_t)map, 8)) + free(fdtsrcbuf); + if (err < 0) { + printf("failed on fdt_open_into for DTO: %s\n", + fdt_strerror(err)); + return err; + } + + return 0; +}; +#endif +int boot_get_fdt_fit(struct
bootm_headers*images, ulong addr, const char **fit_unamep, const char **fit_uname_configp,
int arch, ulong *datap, ulong *lenp)boot/image-fit.c +1
-1@@ -2380,7 +2422,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, char *next_config = NULL; ulong load, len; #ifdef CONFIG_OF_LIBFDT_OVERLAY - ulong ovload, ovlen, ovcopylen; + ulong ovload, ovlen; const char *uconfig;const char *uname;
/*boot/image-fit.c +1
-1@@ -2391,7 +2433,7 @@ int boot_get_fdt_fit(struct bootm_headers*images, ulong addr, * Instead, let's be lazy and use void *. */ char *of_flat_tree; - void *base, *ov, *ovcopy = NULL; + void *base, *ovcopy = NULL; int i, err, noffset, ov_noffset;#endif
boot/image-fit.c +1
-11@@ -2489,17 +2531,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, } debug("%s loaded at 0x%08lx len=0x%08lx\n", uname, ovload, ovlen); - ov = map_sysmem(ovload, ovlen); - - ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K); - ovcopy = malloc(ovcopylen); - if (!ovcopy) { - printf("failed to duplicate DTO before application\n"); - fdt_noffset = -ENOMEM; - goto out; - } - - err = fdt_open_into(ov, ovcopy, ovcopylen); + err = boot_get_fdt_fit_into_buffer(ovload, ovlen, &ovcopy);if (err < 0) {
printf("failed onfdt_open_intofor DTO:
%s\n",
fdt_strerror(err));boot/image-fit.c +1
-2@@ -2507,8 +2539,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, goto out; } - base = map_sysmem(load, len + ovlen); - err = fdt_open_into(base, base, len + ovlen); + err = boot_get_fdt_fit_into_buffer(load, len + ovlen, &base);Quoted 5 lines
if (err < 0) {
printf("failed on fdt_open_into: %s\n", fdt_strerror(err));Signature · 1 line
2.51.0Thanks for the suggestion. I tested your patch and found that, although
Quoted 8 lines
overlays are applied to aligned buffers, the CPU OPP overlay does not appear in the final device tree in Linux. With my previous patch above (the alignment fix), the OPP overlay works. It looks like the new buffer management breaks overlay chaining or the final DT pointer isn’t updated correctly, so overlays aren’t present in the running device tree.
Let me know if you have suggestions for tracking the buffer or chaining overlays differently.
Quoted 1 line
Maybe some memcpy is missing there somewhere ? Can you please have a look and debug this a bit ?
Also, can you reproduce this in sandbox ? Can you share such a reproducer ?
Hi Marek, all,
Quoted 273 lines · Marek Vasut
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 269 lines · Marek Vasut
Quoted 268 lines · On 2/10/26 1:24 PM, [email protected] wrote:
Hi Marek,
Quoted 9 lines · Marek Vasut
EXTERNAL EMAIL: Do not click links or open attachments unless you
know the content is safeQuoted 1 line · Jamie Gibbons
Some platforms
All platforms, DTs and DTOs have to be 8 Byte aligned, always, period.
Thanks for your feedback. I have realised I misinterpreted Tom's instruction on my previous patch fix about passing a new flag.
Quoted 232 lines
Quoted 37 lines
require device tree overlays (DTOs) to be 8-byte aligned when applying them with libfdt. Previously, the code did not guarantee
this alignment, which could result in alignment errors during overlay
application.
Introduce a new runtime parameter,always_relocate_overlay, toboot_get_fdt_fit(). When set, overlays are always copied to an 8-
byte
aligned buffer before being passed tofdt_open_into(). This resolves issues with FDT overlay application on platforms that
require strict alignment.Fixes 8fbcc0e0e839 ("boot: Assure FDT is always at 8-bytealigned
address")Signed-off-by Jamie Gibbons <[email protected]>---
boot/image-fdt.c| 2 +-
boot/image-fit.c| 23 +++++++++++++++++------
include/image.h | 5 ++++-3 files changed, 22 insertions(+), 8 deletions(-)boot/image-fdt.c +0
-0@@ -431,7 +431,7 @@ static int select_fdt(struct
bootm_headers
*images, const char *select, u8 arch,
fdt_noffset=
boot_get_fdt_fit(images,fdt_addr,Quoted 8 lines
&fit_uname_fdt,&fit_uname_config,
-
arch,
&load, &len);
+
arch,
&load, &len, true);This flag is always true, do we actually need it ?
Quoted 42 lines
if (
fdt_noffset< 0) return-ENOENT;boot/image-fit.c +0
-0@@ -2370,7 +2370,8 @@ int boot_get_setup_fit(struct
bootm_headers
*images,uint8_tarch,
#ifndefUSE_HOSTCC
int boot_get_fdt_fit(structbootm_headers*images, ulong addr,
const char **fit_unamep, const char **fit_uname_configp,
- int arch, ulong *datap, ulong *lenp) + int arch, ulong *datap, ulong *lenp, + boolalways_relocate_overlay)
{
intfdt_noffset,cfg_noffset, count;
const void *fit;boot/image-fit.c +0
-0@@ -2492,11 +2493,21 @@ int boot_get_fdt_fit(struct
bootm_headers
*images, ulong addr,
ov =map_sysmem(ovload, ovlen);ovcopylen =
ALIGN(fdt_totalsize(ov), SZ_4K); - ovcopy = malloc(ovcopylen);
- if (!ovcopy) {
-printf("failed to duplicate DTO before application\n");
-fdt_noffset=-ENOMEM;
- goto out;
+ if (always_relocate_overlay) {
+ ovcopy =aligned_alloc(8, ovcopylen); + if (!ovcopy) {
+ printf("failed to allocate aligned
DTO buffer\n");
+fdt_noffset=-ENOMEM; + goto out;
+ }
+memcpy(ovcopy, ov, ovlen);Quoted 8 lines
+ } else {
+ ovcopy = malloc(ovcopylen);
+ if (!ovcopy) {
+printf("failed to duplicate DTO before application\n");
+fdt_noffset=-ENOMEM; + goto out;
+ }Looking at this, if the problem is $ov aligned to 4 Bytes, then $base
a
bit below could also be aligned to 4 Bytes if there are two or more
DTs
in the fitImage without external data. Can you please try the attached
patch and see if that still works ?boot/image-fit.c +0
-0@@ -2368,6 +2368,48 @@ int boot_get_setup_fit(struct
bootm_headers
*images,uint8_tarch,
}#ifndef
USE_HOSTCCboot/image-fit.c +2
-0+#ifdef CONFIG_OF_LIBFDT_OVERLAY +static int boot_get_fdt_fit_into_buffer(ulong loadaddr, ulongloadlen,
void **fdtdstbuf)boot/image-fit.c +8
-0+{ + void *fdtsrcbuf, *map; + ulong fdtdstlen; + int err; + + map = map_sysmem(loadaddr, loadlen); + + /* Set up source buffer, make sure DT in the buffer is8-
Bytes aligned */
+ if (!IS_ALIGNED((uintptr_t)map, 8)) {
+ fdtsrcbuf =aligned_alloc(8, loadlen); + if (!fdtsrcbuf)
+ return-ENOMEM;
+memcpy(fdtsrcbuf, map, loadlen);
+ } else {
+ fdtsrcbuf = map;
+ }
+
+ /* Set up destination buffer, make sure DT in the buffer is
8-Bytes
aligned */
+ fdtdstlen =ALIGN(fdt_totalsize(fdtsrcbuf), SZ_4K); + fdtdstbuf =aligned_alloc(8, fdtdstlen);
+ if (!fdtdstbuf) {
+printf("failed to duplicate DTO before application\n");
+ if (!IS_ALIGNED((uintptr_t)map, 8)) + free(fdtsrcbuf);
+ return-ENOMEM;
+ }boot/image-fit.c +14
-0+ + err = fdt_open_into(fdtsrcbuf, *fdtdstbuf, fdtdstlen); + if (!IS_ALIGNED((uintptr_t)map, 8)) + free(fdtsrcbuf); + if (err < 0) { + printf("failed on fdt_open_into for DTO: %s\n", + fdt_strerror(err)); + return err; + } + + return 0; +}; +#endif +int boot_get_fdt_fit(struct
bootm_headers*images, ulong addr,
const char **fit_unamep, const char **fit_uname_configp,
int arch, ulong *datap, ulong *lenp)boot/image-fit.c +1
-1@@ -2380,7 +2422,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, char *next_config = NULL; ulong load, len; #ifdef CONFIG_OF_LIBFDT_OVERLAY - ulong ovload, ovlen, ovcopylen; + ulong ovload, ovlen; const char *uconfig;const char *uname;
/*boot/image-fit.c +1
-1@@ -2391,7 +2433,7 @@ int boot_get_fdt_fit(struct bootm_headers*images, ulong addr, * Instead, let's be lazy and use void *. */ char *of_flat_tree; - void *base, *ov, *ovcopy = NULL; + void *base, *ovcopy = NULL; int i, err, noffset, ov_noffset;#endif
boot/image-fit.c +0
-0@@ -2489,17 +2531,7 @@ int boot_get_fdt_fit(struct
bootm_headers
*images, ulong addr,
}
debug("%s loaded at 0x%08lx len=0x%08lx\n", uname, ovload, ovlen); - ov =map_sysmem(ovload, ovlen);
-
- ovcopylen =ALIGN(fdt_totalsize(ov), SZ_4K); - ovcopy = malloc(ovcopylen);
- if (!ovcopy) {
-printf("failed to duplicate DTO before application\n");
-fdt_noffset=-ENOMEM;
- goto out;
- }
-
- err =fdt_open_into(ov, ovcopy, ovcopylen); + err = boot_get_fdt_fit_into_buffer(ovload, ovlen,
&ovcopy);
if (err < 0) {
printf("failed onfdt_open_intofor DTO:
%s\n",
fdt_strerror(err));boot/image-fit.c +1
-2@@ -2507,8 +2539,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, goto out; } - base = map_sysmem(load, len + ovlen); - err = fdt_open_into(base, base, len + ovlen); + err = boot_get_fdt_fit_into_buffer(load, len + ovlen,Quoted 7 lines
&base);
if (err < 0) {
printf("failed onfdt_open_into:
%s\n",
fdt_strerror(err)); --
2.51.0Thanks for the suggestion. I tested your patch and found that, although
overlays are applied to aligned buffers, the CPU OPP overlay does not
appear in the final device tree in Linux. With my previous patch above
(the alignment fix), the OPP overlay works. It looks like the new buffer management breaks overlay chaining or the final DT pointer isn’t
updated correctly, so overlays aren’t present in the running device
tree.
Let me know if you have suggestions for tracking the buffer or chaining
overlays differently.
Quoted 6 lines
Quoted 3 lines
Maybe some memcpy is missing there somewhere ? Can you please have a
look and debug this a bit ?Also, can you reproduce this in sandbox ? Can you share such a reproducer ?
This patch is no longer needed as the issue this was working to fix has been fixed by James' patch: [PATCH v4 1/1] image: fit: Apply overlays using aligned writable FDT copies. Tested and confirmed.