Skip to contentSkip to conversation

image: fit: Apply overlays using aligned writable FDT copies

PATCH Superseded James Hilliard 3 participants 7 messages Feb 11 – Feb 12

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 image: fit: Apply overlays using aligned writable FDT copies

libfdt expects FDT/DTO blobs to be 8-byte aligned. When loading the base FDT or overlays from a FIT, the mapped buffer may be unaligned, which can break fdt_open_into() on strict-alignment architectures.

boot_get_fdt_fit() relocates the base FDT with boot_relocate_fdt() before applying overlays. That uses the bootm memory map and can overlap with the FIT buffer when the FIT is loaded into RAM, corrupting data needed to load the kernel and ramdisk.

Allocate writable, 8-byte aligned copies of the base FDT and overlays with memalign() and fdt_open_into(). Grow the base buffer as needed, apply overlays to it and pack the final tree. Free each temporary overlay copy after application and check fdt_pack() errors.

Fixes 8fbcc0e0e839 ("boot: Assure FDT is always 8-byte aligned")
Fixes 881f0b77dc8c ("image: apply FDTOs on FDT image node")
Cc Jamie Gibbons <[email protected]>

---
Changes v1 -> v2:
- also fix alignment issues
Changes v2 -> v3:
- do not print on -ENOMEM errors
- let boot_get_fdt_fit_into_buffer() reuse buffers and handle rounding
Changes v3 -> v4:
- rebase on master

---
 boot/image-fit.c | 163 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 124 insertions(+), 39 deletions(-)
boot/image-fit.c +124 -39
index 3ed69b5f7bc..c70b7707c00 100644
@@ -2368,6 +2368,75 @@ 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(const void *src, ulong srclen,
+					ulong extra, ulong min_dstlen,
+					void **fdtdstbuf, ulong *fdtdstlenp)
+{
+	const void *fdtsrcbuf;
+	void *tmp = NULL;
+	void *dstbuf, *newdstbuf;
+	ulong dstlen, newdstlen;
+	int err;
+
+	/* Make sure the source FDT/DTO is 8-byte aligned for libfdt. */
+	fdtsrcbuf = src;
+	if (!IS_ALIGNED((uintptr_t)src, 8)) {
+		tmp = memalign(8, srclen);
+		if (!tmp)
+			return -ENOMEM;
+
+		memcpy(tmp, src, srclen);
+		fdtsrcbuf = tmp;
+	}
+
+	newdstlen = ALIGN(fdt_totalsize(fdtsrcbuf) + extra, SZ_4K);
+	min_dstlen = ALIGN(min_dstlen, SZ_4K);
+	if (newdstlen < min_dstlen)
+		newdstlen = min_dstlen;
+
+	dstbuf = *fdtdstbuf;
+	dstlen = dstbuf ? *fdtdstlenp : 0;
+
+	/*
+	 * If the caller already provided a large enough writable buffer,
+	 * and we're not moving the FDT, nothing to do.
+	 */
+	if (dstbuf && dstlen >= newdstlen && dstbuf == fdtsrcbuf) {
+		free(tmp);
+		return 0;
+	}
+
+	/* Try to reuse existing destination buffer if it is large enough. */
+	if (dstbuf && dstlen >= newdstlen) {
+		err = fdt_open_into(fdtsrcbuf, dstbuf, dstlen);
+		free(tmp);
+		if (err < 0)
+			return err;
+		return 0;
+	}
+
+	newdstbuf = memalign(8, newdstlen);
+	if (!newdstbuf) {
+		free(tmp);
+		return -ENOMEM;
+	}
+
+	err = fdt_open_into(fdtsrcbuf, newdstbuf, newdstlen);
+	free(tmp);
+	if (err < 0) {
+		free(newdstbuf);
+		return err;
+	}
+
+	free(dstbuf);
+	*fdtdstbuf = newdstbuf;
+	*fdtdstlenp = newdstlen;
+
+	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,18 +2449,12 @@ 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, ovcopylen, need;
 	const char *uconfig;
 	const char *uname;
-	/*
-	 * of_flat_tree is storing the void * returned by map_sysmem, then its
-	 * address is passed to boot_relocate_fdt which expects a char ** and it
-	 * is then cast into a ulong. Setting its type to void * would require
-	 * to cast its address to char ** when passing it to boot_relocate_fdt.
-	 * Instead, let's be lazy and use void *.
-	 */
-	char *of_flat_tree;
-	void *base, *ov, *ovcopy = NULL;
+	void *ovcopy = NULL;
+	void *base_buf = NULL;
+	ulong base_buf_size = 0;
 	int i, err, noffset, ov_noffset;
 #endif
 
@@ -2434,18 +2497,25 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr,
 	/* we need to apply overlays */
 
 #ifdef CONFIG_OF_LIBFDT_OVERLAY
-	/* Relocate FDT so resizing does not overwrite other data in FIT. */
-	of_flat_tree = map_sysmem(load, len);
-	len = ALIGN(fdt_totalsize(load), SZ_4K);
-	err = boot_relocate_fdt(&of_flat_tree, &len);
-	if (err) {
-		printf("Required FDT relocation for applying DTOs failed: %d\n",
-		       err);
-		fdt_noffset = -EBADF;
+	/*
+	 * Make a writable copy of the base FDT for applying overlays.
+	 *
+	 * Do not use boot_relocate_fdt() here: it allocates from the bootm map and
+	 * may overlap with the FIT buffer (still needed to load the kernel /
+	 * ramdisk) when the FIT is loaded into RAM.
+	 */
+	err = boot_get_fdt_fit_into_buffer(map_sysmem(load, len), len,
+					   CONFIG_SYS_FDT_PAD, 0, &base_buf,
+					   &base_buf_size);
+	if (err < 0) {
+		if (err != -ENOMEM)
+			printf("Required FDT copy for applying DTOs failed: %s\n",
+			       fdt_strerror(err));
+		fdt_noffset = err;
 		goto out;
 	}
 
-	load = (ulong)of_flat_tree;
+	len = fdt_off_dt_strings(base_buf) + fdt_size_dt_strings(base_buf);
 
 	/* apply extra configs in FIT first, followed by args */
 	for (i = 1; ; i++) {
@@ -2489,48 +2559,61 @@ 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(map_sysmem(ovload, ovlen),
+						   ovlen, 0, 0, &ovcopy,
+						   &ovcopylen);
 		if (err < 0) {
-			printf("failed on fdt_open_into for DTO: %s\n",
-			       fdt_strerror(err));
+			if (err != -ENOMEM)
+				printf("failed on fdt_open_into for DTO: %s\n",
+				       fdt_strerror(err));
 			fdt_noffset = err;
 			goto out;
 		}
 
-		base = map_sysmem(load, len + ovlen);
-		err = fdt_open_into(base, base, len + ovlen);
+		/*
+		 * Ensure the base FDT buffer is open and has enough room for
+		 * the overlay. Grow it on demand.
+		 */
+		need = len + ovcopylen + CONFIG_SYS_FDT_PAD;
+		err = boot_get_fdt_fit_into_buffer(base_buf, base_buf_size, 0,
+						   need, &base_buf,
+						   &base_buf_size);
 		if (err < 0) {
-			printf("failed on fdt_open_into: %s\n",
-			       fdt_strerror(err));
+			if (err != -ENOMEM)
+				printf("failed to expand FDT for DTO application: %s\n",
+				       fdt_strerror(err));
 			fdt_noffset = err;
 			goto out;
 		}
 
 		/* the verbose method prints out messages on error */
-		err = fdt_overlay_apply_verbose(base, ovcopy);
+		err = fdt_overlay_apply_verbose(base_buf, ovcopy);
 		if (err < 0) {
 			fdt_noffset = err;
 			goto out;
 		}
-		fdt_pack(base);
-		len = fdt_totalsize(base);
+		len = fdt_off_dt_strings(base_buf) + fdt_size_dt_strings(base_buf);
+
+		free(ovcopy);
+		ovcopy = NULL;
 	}
+
+	err = fdt_pack(base_buf);
+	if (err < 0) {
+		fdt_noffset = err;
+		goto out;
+	}
+	len = fdt_totalsize(base_buf);
 #else
 	printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
 	fdt_noffset = -EBADF;
 #endif
 
 out:
+#ifdef CONFIG_OF_LIBFDT_OVERLAY
+	if (fdt_noffset >= 0 && base_buf)
+		load = map_to_sysmem(base_buf);
+#endif
 	if (datap)
 		*datap = load;
 	if (lenp)
@@ -2541,6 +2624,8 @@ out:
 		*fit_uname_configp = fit_uname_config;
 
 #ifdef CONFIG_OF_LIBFDT_OVERLAY
+	if (fdt_noffset < 0)
+		free(base_buf);
 	free(ovcopy);
 #endif
 	free(fit_uname_config_copy);
Signature · 1 line
2.43.0
Tested-by on v4 1/1
#1 lore raw

Hi James, Marek, Tom,

Quoted 17 lines · James Hilliard

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

libfdt expects FDT/DTO blobs to be 8-byte aligned. When loading the base FDT or overlays from a FIT, the mapped buffer may be unaligned, which can break fdt_open_into() on strict-alignment architectures.

boot_get_fdt_fit() relocates the base FDT with boot_relocate_fdt() before applying overlays. That uses the bootm memory map and can overlap with the FIT buffer when the FIT is loaded into RAM, corrupting data needed to load the kernel and ramdisk.

Allocate writable, 8-byte aligned copies of the base FDT and overlays with memalign() and fdt_open_into(). Grow the base buffer as needed, apply overlays to it and pack the final tree. Free each temporary overlay copy after application and check fdt_pack() errors.

Quoted 4 lines
Fixes 8fbcc0e0e839 ("boot: Assure FDT is always 8-byte aligned")
Fixes 881f0b77dc8c ("image: apply FDTOs on FDT image node")
Cc Jamie Gibbons <[email protected]>

I can confirm that this patch fixes the remaining overlay application boot errors I was seeing - specifically fdt_open_into overlay alignment error. Thank you all.

Tested with Microchip's PolarFire SoC Icicle kit ES.

Quoted 280 lines

---
Changes v1 -> v2:
  - also fix alignment issues
Changes v2 -> v3:
  - do not print on -ENOMEM errors
  - let boot_get_fdt_fit_into_buffer() reuse buffers and handle rounding
Changes v3 -> v4:
  - rebase on master
---
 boot/image-fit.c | 163 +++++++++++++++++++++++++++++++++++---------- --

 1 file changed, 124 insertions(+), 39 deletions(-)
boot/image-fit.c +4 -0
index 3ed69b5f7bc..c70b7707c00 100644
@@ -2368,6 +2368,75 @@ 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(const void *src, ulong
 srclen,
+                                       ulong extra, ulong
 min_dstlen,
+                                       void **fdtdstbuf, ulong

*fdtdstlenp)

boot/image-fit.c +8 -0
+{
+       const void *fdtsrcbuf;
+       void *tmp = NULL;
+       void *dstbuf, *newdstbuf;
+       ulong dstlen, newdstlen;
+       int err;
+
+       /* Make sure the source FDT/DTO is 8-byte aligned for libfdt.

*/
+       fdtsrcbuf = src;
+       if (!IS_ALIGNED((uintptr_t)src, 8)) {
+               tmp = memalign(8, srclen);
+               if (!tmp)
+                       return -ENOMEM;

boot/image-fit.c +15 -0
+
+               memcpy(tmp, src, srclen);
+               fdtsrcbuf = tmp;
+       }
+
+       newdstlen = ALIGN(fdt_totalsize(fdtsrcbuf) + extra, SZ_4K);
+       min_dstlen = ALIGN(min_dstlen, SZ_4K);
+       if (newdstlen < min_dstlen)
+               newdstlen = min_dstlen;
+
+       dstbuf = *fdtdstbuf;
+       dstlen = dstbuf ? *fdtdstlenp : 0;
+
+       /*
+        * If the caller already provided a large enough writable

buffer,
+        * and we're not moving the FDT, nothing to do. +        */
+       if (dstbuf && dstlen >= newdstlen && dstbuf == fdtsrcbuf) { +               free(tmp);
+               return 0;
+       }
+
+       /* Try to reuse existing destination buffer if it is large enough. */
+       if (dstbuf && dstlen >= newdstlen) {
+               err = fdt_open_into(fdtsrcbuf, dstbuf, dstlen); +               free(tmp);
+               if (err < 0)
+                       return err;
+               return 0;
+       }

boot/image-fit.c +22 -0
+
+       newdstbuf = memalign(8, newdstlen);
+       if (!newdstbuf) {
+               free(tmp);
+               return -ENOMEM;
+       }
+
+       err = fdt_open_into(fdtsrcbuf, newdstbuf, newdstlen);
+       free(tmp);
+       if (err < 0) {
+               free(newdstbuf);
+               return err;
+       }
+
+       free(dstbuf);
+       *fdtdstbuf = newdstbuf;
+       *fdtdstlenp = newdstlen;
+
+       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 -3
@@ -2380,18 +2449,12 @@ 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, ovcopylen, need;
         const char *uconfig;
         const char *uname;
-       /*
-        * of_flat_tree is storing the void * returned by map_sysmem,

then its
-        * address is passed to boot_relocate_fdt which expects a char ** and it
-        * is then cast into a ulong. Setting its type to void * would require
-        * to cast its address to char ** when passing it to boot_relocate_fdt.
-        * Instead, let's be lazy and use void *.
-        */
-       char *of_flat_tree;
-       void *base, *ov, *ovcopy = NULL;
+       void *ovcopy = NULL;
+       void *base_buf = NULL;
+       ulong base_buf_size = 0;
        int i, err, noffset, ov_noffset;
 #endif

boot/image-fit.c +0 -1
@@ -2434,18 +2497,25 @@ int boot_get_fdt_fit(struct bootm_headers
 *images, ulong addr,
         /* we need to apply overlays */
 
  #ifdef CONFIG_OF_LIBFDT_OVERLAY
-       /* Relocate FDT so resizing does not overwrite other data in

FIT. */
-       of_flat_tree = map_sysmem(load, len);
-       len = ALIGN(fdt_totalsize(load), SZ_4K);
-       err = boot_relocate_fdt(&of_flat_tree, &len);
-       if (err) {
-               printf("Required FDT relocation for applying DTOs
failed: %d\n",
-                      err);
-               fdt_noffset = -EBADF;
+       /*
+        * Make a writable copy of the base FDT for applying overlays.
+        *
+        * Do not use boot_relocate_fdt() here: it allocates from the bootm map and
+        * may overlap with the FIT buffer (still needed to load the kernel /
+        * ramdisk) when the FIT is loaded into RAM.
+        */
+       err = boot_get_fdt_fit_into_buffer(map_sysmem(load, len), len,
+                                          CONFIG_SYS_FDT_PAD, 0, &base_buf,
+                                          &base_buf_size); +       if (err < 0) {
+               if (err != -ENOMEM)
+                       printf("Required FDT copy for applying DTOs
failed: %s\n",
+                              fdt_strerror(err));
+               fdt_noffset = err;
                goto out;
        }

-       load = (ulong)of_flat_tree;
+       len = fdt_off_dt_strings(base_buf) +
fdt_size_dt_strings(base_buf);

        /* apply extra configs in FIT first, followed by args */ for (i = 1; ; i++) {

boot/image-fit.c +8 -15
@@ -2489,48 +2559,61 @@ 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(map_sysmem(ovload,
 ovlen),
+                                                  ovlen, 0, 0,
 &ovcopy,
+                                                  &ovcopylen);
                 if (err < 0) {
-                       printf("failed on fdt_open_into for DTO:
 %s\n",
-                              fdt_strerror(err));
+                       if (err != -ENOMEM)
+                               printf("failed on fdt_open_into for
 DTO: %s\n",
+                                      fdt_strerror(err));
                         fdt_noffset = err;
                         goto out;
                 }
 
-               base = map_sysmem(load, len + ovlen);
-               err = fdt_open_into(base, base, len + ovlen);
+               /*
+                * Ensure the base FDT buffer is open and has enough

room for
+                * the overlay. Grow it on demand.
+                */
+               need = len + ovcopylen + CONFIG_SYS_FDT_PAD; +               err = boot_get_fdt_fit_into_buffer(base_buf, base_buf_size, 0,
+                                                  need, &base_buf, +                                                  &base_buf_size); if (err < 0) {
-                       printf("failed on fdt_open_into: %s\n", -                              fdt_strerror(err));
+                       if (err != -ENOMEM)
+                               printf("failed to expand FDT for DTO
application: %s\n",
+                                      fdt_strerror(err)); fdt_noffset = err;
                        goto out;
                }

                /* the verbose method prints out messages on error */ -               err = fdt_overlay_apply_verbose(base, ovcopy); +               err = fdt_overlay_apply_verbose(base_buf, ovcopy); if (err < 0) {
                        fdt_noffset = err;
                        goto out;
                }
-               fdt_pack(base);
-               len = fdt_totalsize(base);
+               len = fdt_off_dt_strings(base_buf) + fdt_size_dt_strings(base_buf);
+
+               free(ovcopy);
+               ovcopy = NULL;
        }
+
+       err = fdt_pack(base_buf);
+       if (err < 0) {
+               fdt_noffset = err;
+               goto out;
+       }
+       len = fdt_totalsize(base_buf);
 #else
        printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
        fdt_noffset = -EBADF;
 #endif

 out:

boot/image-fit.c +4 -0
+#ifdef CONFIG_OF_LIBFDT_OVERLAY
+       if (fdt_noffset >= 0 && base_buf)
+               load = map_to_sysmem(base_buf);
+#endif

        if (datap)
                *datap = load;
        if (lenp)

boot/image-fit.c +2 -1
@@ -2541,6 +2624,8 @@ out:
                 *fit_uname_configp = fit_uname_config;
 
  #ifdef CONFIG_OF_LIBFDT_OVERLAY
+       if (fdt_noffset < 0)
+               free(base_buf);
         free(ovcopy);
  #endif
         free(fit_uname_config_copy);
--

2.43.0

#2 lore raw
In reply to James Hilliard
Quoted 24 lines · James Hilliard
boot/image-fit.c +24 -0
+static int boot_get_fdt_fit_into_buffer(const void *src, ulong srclen,
+					ulong extra, ulong min_dstlen,
+					void **fdtdstbuf, ulong *fdtdstlenp)
+{
+	const void *fdtsrcbuf;
+	void *tmp = NULL;
+	void *dstbuf, *newdstbuf;
+	ulong dstlen, newdstlen;
+	int err;
+
+	/* Make sure the source FDT/DTO is 8-byte aligned for libfdt. */
+	fdtsrcbuf = src;
+	if (!IS_ALIGNED((uintptr_t)src, 8)) {
+		tmp = memalign(8, srclen);
+		if (!tmp)
+			return -ENOMEM;
+
+		memcpy(tmp, src, srclen);
+		fdtsrcbuf = tmp;
+	}
+
+	newdstlen = ALIGN(fdt_totalsize(fdtsrcbuf) + extra, SZ_4K);
+	min_dstlen = ALIGN(min_dstlen, SZ_4K);
+	if (newdstlen < min_dstlen)

boot/image-fit.c int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, · line 2405

Quoted 10 lines
boot/image-fit.c +10 -0
+		newdstlen = min_dstlen;
+
+	dstbuf = *fdtdstbuf;
+	dstlen = dstbuf ? *fdtdstlenp : 0;
+
+	/*
+	 * If the caller already provided a large enough writable buffer,
+	 * and we're not moving the FDT, nothing to do.
+	 */
+	if (dstbuf && dstlen >= newdstlen && dstbuf == fdtsrcbuf) {

Can $dstbuf ever be NULL ?

Review

Quoted 2 lines
(quoted diff) +2 -0
+		free(tmp);
+		return 0;

You could try something like this here, to reduce the duplicate free(tmp) in the code:

"
err = 0;
goto exit;

...

exit:
free(tmp);
return err;
"

Quoted 2 lines
(quoted diff) +2 -0
+	}
+

[...]

boot/image-fit.c int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, · line 2518

Quoted 2 lines
boot/image-fit.c +1 -1
-	load = (ulong)of_flat_tree;
+	len = fdt_off_dt_strings(base_buf) + fdt_size_dt_strings(base_buf);

How does this new length calculation work, can you please clarify that ?

I don't think this will work for fitImages with external data (generated using mkimage -E) , but I might be wrong.

#3 lore raw
Quoted 57 lines · Marek Vasut
Quoted 24 lines · James Hilliard
boot/image-fit.c +24 -0
+static int boot_get_fdt_fit_into_buffer(const void *src, ulong srclen,
+                                     ulong extra, ulong min_dstlen,
+                                     void **fdtdstbuf, ulong *fdtdstlenp)
+{
+     const void *fdtsrcbuf;
+     void *tmp = NULL;
+     void *dstbuf, *newdstbuf;
+     ulong dstlen, newdstlen;
+     int err;
+
+     /* Make sure the source FDT/DTO is 8-byte aligned for libfdt. */
+     fdtsrcbuf = src;
+     if (!IS_ALIGNED((uintptr_t)src, 8)) {
+             tmp = memalign(8, srclen);
+             if (!tmp)
+                     return -ENOMEM;
+
+             memcpy(tmp, src, srclen);
+             fdtsrcbuf = tmp;
+     }
+
+     newdstlen = ALIGN(fdt_totalsize(fdtsrcbuf) + extra, SZ_4K);
+     min_dstlen = ALIGN(min_dstlen, SZ_4K);
+     if (newdstlen < min_dstlen)

boot/image-fit.c int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, · line 2405

Quoted 10 lines
boot/image-fit.c +10 -0
+             newdstlen = min_dstlen;
+
+     dstbuf = *fdtdstbuf;
+     dstlen = dstbuf ? *fdtdstlenp : 0;
+
+     /*
+      * If the caller already provided a large enough writable buffer,
+      * and we're not moving the FDT, nothing to do.
+      */
+     if (dstbuf && dstlen >= newdstlen && dstbuf == fdtsrcbuf) {

Can $dstbuf ever be NULL ?

Review

Quoted 2 lines
(quoted diff) +2 -0
+             free(tmp);
+             return 0;

You could try something like this here, to reduce the duplicate free(tmp) in the code:

"
err = 0;
goto exit;

...

exit:

   free(tmp);
   return err;

"

Quoted 8 lines
Quoted 2 lines
(quoted diff) +2 -0
+     }
+

[...]

boot/image-fit.c int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, · line 2518

Quoted 2 lines
boot/image-fit.c +1 -1
-     load = (ulong)of_flat_tree;
+     len = fdt_off_dt_strings(base_buf) + fdt_size_dt_strings(base_buf);

How does this new length calculation work, can you please clarify that ?

From my understanding, that len is tracking the current packed DTB data length (up to the end of the strings block), not the allocated buffer size. We recompute it after each overlay so need = len + ovcopylen CONFIG_SYS_FDT_PAD grows from real used bytes instead of accumulated slack. This is roughly the same idea as libfdt’s internal fdt_data_size_(), and the final returned size is still taken after fdt_pack() with fdt_totalsize().

Quoted 2 lines

I don't think this will work for fitImages with external data (generated using mkimage -E) , but I might be wrong.

From my understanding, this should still work with mkimage -E, external data only changes where FIT payload bytes are stored, and fit_image_load()/fit_image_get_data() already resolves that before overlay application. At that point we’re operating on a normal copied DTB buffer, and the length math is based on DTB-internal layout, not FIT external-data placement.

#4 lore raw
Quoted 67 lines · James Hilliard
Quoted 57 lines · Marek Vasut
Quoted 24 lines · James Hilliard
boot/image-fit.c +24 -0
+static int boot_get_fdt_fit_into_buffer(const void *src, ulong srclen,
+                                     ulong extra, ulong min_dstlen,
+                                     void **fdtdstbuf, ulong *fdtdstlenp)
+{
+     const void *fdtsrcbuf;
+     void *tmp = NULL;
+     void *dstbuf, *newdstbuf;
+     ulong dstlen, newdstlen;
+     int err;
+
+     /* Make sure the source FDT/DTO is 8-byte aligned for libfdt. */
+     fdtsrcbuf = src;
+     if (!IS_ALIGNED((uintptr_t)src, 8)) {
+             tmp = memalign(8, srclen);
+             if (!tmp)
+                     return -ENOMEM;
+
+             memcpy(tmp, src, srclen);
+             fdtsrcbuf = tmp;
+     }
+
+     newdstlen = ALIGN(fdt_totalsize(fdtsrcbuf) + extra, SZ_4K);
+     min_dstlen = ALIGN(min_dstlen, SZ_4K);
+     if (newdstlen < min_dstlen)

boot/image-fit.c int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, · line 2405

Quoted 10 lines
boot/image-fit.c +10 -0
+             newdstlen = min_dstlen;
+
+     dstbuf = *fdtdstbuf;
+     dstlen = dstbuf ? *fdtdstlenp : 0;
+
+     /*
+      * If the caller already provided a large enough writable buffer,
+      * and we're not moving the FDT, nothing to do.
+      */
+     if (dstbuf && dstlen >= newdstlen && dstbuf == fdtsrcbuf) {

Can $dstbuf ever be NULL ?

Review

Quoted 2 lines
(quoted diff) +2 -0
+             free(tmp);
+             return 0;

You could try something like this here, to reduce the duplicate free(tmp) in the code:

"
err = 0;
goto exit;

...

exit:

    free(tmp);
    return err;

"

Quoted 8 lines
Quoted 2 lines
(quoted diff) +2 -0
+     }
+

[...]

boot/image-fit.c int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, · line 2518

Quoted 2 lines
boot/image-fit.c +1 -1
-     load = (ulong)of_flat_tree;
+     len = fdt_off_dt_strings(base_buf) + fdt_size_dt_strings(base_buf);

How does this new length calculation work, can you please clarify that ?

Quoted 7 lines

From my understanding, that len is tracking the current packed DTB data
length (up to the end of the strings block), not the allocated buffer size. We recompute it after each overlay so need = len + ovcopylen CONFIG_SYS_FDT_PAD grows from real used bytes instead of accumulated slack. This is roughly the same idea as libfdt’s internal fdt_data_size_(), and the final returned size is still taken after fdt_pack() with fdt_totalsize().

Ah I see, thank you for the clarification, this helped. It would be nice to include that as code comment too.

But don't you have to account for the FDT headers too, so wouldn't fdt_totalsize() be more appropriate here ?

Quoted 9 lines
Quoted 2 lines

I don't think this will work for fitImages with external data (generated using mkimage -E) , but I might be wrong.

From my understanding, this should still work with mkimage -E, external
data only changes where FIT payload bytes are stored, and fit_image_load()/fit_image_get_data() already resolves that before overlay application. At that point we’re operating on a normal copied DTB buffer, and the length math is based on DTB-internal layout, not FIT external-data placement.

Clear, you're right.

#5 lore raw
Quoted 77 lines · Marek Vasut
Quoted 67 lines · James Hilliard
Quoted 57 lines · Marek Vasut
Quoted 24 lines · James Hilliard
boot/image-fit.c +24 -0
+static int boot_get_fdt_fit_into_buffer(const void *src, ulong srclen,
+                                     ulong extra, ulong min_dstlen,
+                                     void **fdtdstbuf, ulong *fdtdstlenp)
+{
+     const void *fdtsrcbuf;
+     void *tmp = NULL;
+     void *dstbuf, *newdstbuf;
+     ulong dstlen, newdstlen;
+     int err;
+
+     /* Make sure the source FDT/DTO is 8-byte aligned for libfdt. */
+     fdtsrcbuf = src;
+     if (!IS_ALIGNED((uintptr_t)src, 8)) {
+             tmp = memalign(8, srclen);
+             if (!tmp)
+                     return -ENOMEM;
+
+             memcpy(tmp, src, srclen);
+             fdtsrcbuf = tmp;
+     }
+
+     newdstlen = ALIGN(fdt_totalsize(fdtsrcbuf) + extra, SZ_4K);
+     min_dstlen = ALIGN(min_dstlen, SZ_4K);
+     if (newdstlen < min_dstlen)

boot/image-fit.c int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, · line 2405

Quoted 10 lines
boot/image-fit.c +10 -0
+             newdstlen = min_dstlen;
+
+     dstbuf = *fdtdstbuf;
+     dstlen = dstbuf ? *fdtdstlenp : 0;
+
+     /*
+      * If the caller already provided a large enough writable buffer,
+      * and we're not moving the FDT, nothing to do.
+      */
+     if (dstbuf && dstlen >= newdstlen && dstbuf == fdtsrcbuf) {

Can $dstbuf ever be NULL ?

Review

Quoted 2 lines
(quoted diff) +2 -0
+             free(tmp);
+             return 0;

You could try something like this here, to reduce the duplicate free(tmp) in the code:

"
err = 0;
goto exit;

...

exit:

    free(tmp);
    return err;

"

Quoted 8 lines
Quoted 2 lines
(quoted diff) +2 -0
+     }
+

[...]

boot/image-fit.c int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, · line 2518

Quoted 2 lines
boot/image-fit.c +1 -1
-     load = (ulong)of_flat_tree;
+     len = fdt_off_dt_strings(base_buf) + fdt_size_dt_strings(base_buf);

How does this new length calculation work, can you please clarify that ?

Quoted 7 lines

From my understanding, that len is tracking the current packed DTB data
length (up to the end of the strings block), not the allocated buffer size. We recompute it after each overlay so need = len + ovcopylen CONFIG_SYS_FDT_PAD grows from real used bytes instead of accumulated slack. This is roughly the same idea as libfdt’s internal fdt_data_size_(), and the final returned size is still taken after fdt_pack() with fdt_totalsize().

Quoted 2 lines

Ah I see, thank you for the clarification, this helped. It would be nice to include that as code comment too.

I'll add a comment.

Quoted 3 lines

But don't you have to account for the FDT headers too, so wouldn't fdt_totalsize() be more appropriate here ?

From my understanding, fdt_off_dt_strings() + fdt_size_dt_strings() is the packed DTB data size we need for growth calculations, and it already covers data from the blob start (so headers are implicitly accounted for via the offset). fdt_totalsize() includes spare/free space, so using it there would overestimate need and cause unnecessary buffer growth.

Quoted 12 lines
Quoted 10 lines
Quoted 3 lines

I don't think this will work for fitImages with external data (generated using mkimage -E) , but I might be wrong.

From my understanding, this should still work with mkimage -E, external
data only changes where FIT payload bytes are stored, and fit_image_load()/fit_image_get_data() already resolves that before overlay application. At that point we’re operating on a normal copied DTB buffer, and the length math is based on DTB-internal layout, not FIT external-data placement.

Clear, you're right.

Reviewed-by
#6 lore raw
Quoted 84 lines · James Hilliard
Quoted 77 lines · Marek Vasut
Quoted 67 lines · James Hilliard
Quoted 57 lines · Marek Vasut
Quoted 24 lines · James Hilliard
boot/image-fit.c +24 -0
+static int boot_get_fdt_fit_into_buffer(const void *src, ulong srclen,
+                                     ulong extra, ulong min_dstlen,
+                                     void **fdtdstbuf, ulong *fdtdstlenp)
+{
+     const void *fdtsrcbuf;
+     void *tmp = NULL;
+     void *dstbuf, *newdstbuf;
+     ulong dstlen, newdstlen;
+     int err;
+
+     /* Make sure the source FDT/DTO is 8-byte aligned for libfdt. */
+     fdtsrcbuf = src;
+     if (!IS_ALIGNED((uintptr_t)src, 8)) {
+             tmp = memalign(8, srclen);
+             if (!tmp)
+                     return -ENOMEM;
+
+             memcpy(tmp, src, srclen);
+             fdtsrcbuf = tmp;
+     }
+
+     newdstlen = ALIGN(fdt_totalsize(fdtsrcbuf) + extra, SZ_4K);
+     min_dstlen = ALIGN(min_dstlen, SZ_4K);
+     if (newdstlen < min_dstlen)

boot/image-fit.c int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, · line 2405

Quoted 10 lines
boot/image-fit.c +10 -0
+             newdstlen = min_dstlen;
+
+     dstbuf = *fdtdstbuf;
+     dstlen = dstbuf ? *fdtdstlenp : 0;
+
+     /*
+      * If the caller already provided a large enough writable buffer,
+      * and we're not moving the FDT, nothing to do.
+      */
+     if (dstbuf && dstlen >= newdstlen && dstbuf == fdtsrcbuf) {

Can $dstbuf ever be NULL ?

Review

Quoted 2 lines
(quoted diff) +2 -0
+             free(tmp);
+             return 0;

You could try something like this here, to reduce the duplicate free(tmp) in the code:

"
err = 0;
goto exit;

...

exit:

     free(tmp);
     return err;

"

Quoted 8 lines
Quoted 2 lines
(quoted diff) +2 -0
+     }
+

[...]

boot/image-fit.c int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, · line 2518

Quoted 2 lines
boot/image-fit.c +1 -1
-     load = (ulong)of_flat_tree;
+     len = fdt_off_dt_strings(base_buf) + fdt_size_dt_strings(base_buf);

How does this new length calculation work, can you please clarify that ?

Quoted 7 lines

From my understanding, that len is tracking the current packed DTB data
length (up to the end of the strings block), not the allocated buffer size. We recompute it after each overlay so need = len + ovcopylen CONFIG_SYS_FDT_PAD grows from real used bytes instead of accumulated slack. This is roughly the same idea as libfdt’s internal fdt_data_size_(), and the final returned size is still taken after fdt_pack() with fdt_totalsize().

Quoted 2 lines

Ah I see, thank you for the clarification, this helped. It would be nice to include that as code comment too.

I'll add a comment.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

But don't you have to account for the FDT headers too, so wouldn't fdt_totalsize() be more appropriate here ?

From my understanding, fdt_off_dt_strings() + fdt_size_dt_strings() is the
packed DTB data size we need for growth calculations, and it already covers data from the blob start (so headers are implicitly accounted for via the offset). fdt_totalsize() includes spare/free space, so using it there would overestimate need and cause unnecessary buffer growth.

Thank you for spelling that out. Please also add that as a code comment, this really helps understand the code.

With that in place, for V6, add my:

Thank you for your hard work.