diff mbox series

[RFC,v2,2/2] mkimage: Align fdt images in FIT to 8 bytes

Message ID 20221106154111.872209-3-bjorn@mork.no
State New
Delegated to: Tom Rini
Headers show
Series Prevent alignment issues with "in place" FIT configurations | expand

Commit Message

Bjørn Mork Nov. 6, 2022, 3:41 p.m. UTC
"In place" configurations are strongly discouraged, but known to be
used.  This can cause hard to debug alignment problems since the
value of the "data" property only is guaranteed to be 4-byte
aligned.

Unconditionally aligning fdt images to 8 byte boundaries will
prevent these problems, at the maximum price of 4 bytes per fdt.

Signed-off-by: Bjørn Mork <bjorn@mork.no>
---
 tools/fit_image.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
diff mbox series

Patch

diff --git a/tools/fit_image.c b/tools/fit_image.c
index 923a9755b709..1e060784895d 100644
--- a/tools/fit_image.c
+++ b/tools/fit_image.c
@@ -24,6 +24,41 @@ 
 
 static struct legacy_img_hdr header;
 
+/**
+ * fit_align_fdt_images() - Align all fdt images in the FIT to 8 bytes
+ *
+ * An fdt blob must be placed at an 8 byte aligned offset if it is to
+ * be used "in place". This will not be verified by U-Boot. Simply align
+ * all IH_TYPE_FLATDT images to prevent problems.
+ */
+static int fit_align_fdt_images(void *fit)
+{
+	int images_noffset,  noffset, ret;
+	uint8_t type;
+	size_t size;
+	const void *ptr;
+
+	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
+	if (images_noffset < 0) {
+		printf("Can't find images parent node '%s' (%s)\n",
+		       FIT_IMAGES_PATH, fdt_strerror(images_noffset));
+		return images_noffset;
+	}
+
+	for (noffset = fdt_first_subnode(fit, images_noffset);
+	     noffset >= 0;
+	     noffset = fdt_next_subnode(fit, noffset)) {
+		fit_image_get_type(fit, noffset, &type);
+		if (type != IH_TYPE_FLATDT)
+			continue;
+
+		ret = fdt_alignprop(fit, noffset, FIT_DATA_PROP, 8);
+		if (ret < 0)
+			return ret;
+	}
+	return 0;
+}
+
 static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
 			     const char *tmpfile)
 {
@@ -81,6 +116,10 @@  static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
 						&params->summary);
 	}
 
+	if (!ret) {
+		ret = fit_align_fdt_images(ptr);
+	}
+
 	if (dest_blob) {
 		munmap(dest_blob, destfd_size);
 		close(destfd);