[PATCH 3/3] binman: Build FIT image subentries with the section etype

Alper Nebi Yasak alpernebiyasak at gmail.com
Tue Aug 25 19:59:51 CEST 2020


When reading subentries of each image, the FIT entry type directly
concatenates their contents without padding them according to their
offset, size, align, align-size, align-end, pad-before, pad-after
properties.

This patch makes sure these properties are respected by offloading this
image-data building to the section etype, where each subnode of the
"images" node is processed as a section. Alignments and offsets are
respective to the beginning of each image. For example, the following
fragment can end up having "u-boot-spl" start at 0x88 within the final
FIT binary, while "u-boot" would then end up starting at e.g. 0x20088.

	fit {
		description = "example";

		images {
			kernel-1 {
				description = "U-Boot with SPL";
				type = "kernel";
				arch = "arm64";
				os = "linux";
				compression = "none";

				u-boot-spl {
				};
				u-boot {
					align = <0x10000>;
				};
			};
		};
	}

Signed-off-by: Alper Nebi Yasak <alpernebiyasak at gmail.com>
---

 tools/binman/etype/fit.py                     | 22 +++----
 tools/binman/ftest.py                         | 24 ++++++++
 .../test/166_fit_image_subentry_alignment.dts | 57 +++++++++++++++++++
 3 files changed, 93 insertions(+), 10 deletions(-)
 create mode 100644 tools/binman/test/166_fit_image_subentry_alignment.dts

diff --git a/tools/binman/etype/fit.py b/tools/binman/etype/fit.py
index 75712f4409..f136a2c254 100644
--- a/tools/binman/etype/fit.py
+++ b/tools/binman/etype/fit.py
@@ -62,7 +62,7 @@ class Entry_fit(Entry):
         """
         super().__init__(section, etype, node)
         self._fit = None
-        self._fit_content = defaultdict(list)
+        self._fit_images = {}
         self._fit_props = {}
 
     def ReadNode(self):
@@ -91,15 +91,18 @@ class Entry_fit(Entry):
 
             rel_path = node.path[len(base_node.path):]
             has_images = depth == 2 and rel_path.startswith('/images/')
+            if has_images:
+                entry = Entry.Create(self.section, node, etype='section')
+                entry.ReadNode()
+                self._fit_images[rel_path] = entry
+
             for subnode in node.subnodes:
                 if has_images and not (subnode.name.startswith('hash') or
                                        subnode.name.startswith('signature')):
                     # This is a content node. We collect all of these together
                     # and put them in the 'data' property. They do not appear
                     # in the FIT.
-                    entry = Entry.Create(self.section, subnode)
-                    entry.ReadNode()
-                    self._fit_content[rel_path].append(entry)
+                    pass
                 else:
                     with fsw.add_node(subnode.name):
                         _AddNode(base_node, depth + 1, subnode)
@@ -150,13 +153,12 @@ class Entry_fit(Entry):
         Returns:
             New fdt contents (bytes)
         """
-        for path, entries in self._fit_content.items():
+        for path, image in self._fit_images.items():
             node = fdt.GetNode(path)
-            data = b''
-            for entry in entries:
-                if not entry.ObtainContents():
-                    return False
-                data += entry.GetData()
+            if not image.ObtainContents():
+                return False
+            image.Pack(0)
+            data = image.GetData()
             node.AddData('data', data)
 
         fdt.Sync(auto_resize=True)
diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index 8edf85c89f..ed573d8991 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -3485,5 +3485,29 @@ class TestFunctional(unittest.TestCase):
         fnode = dtb.GetNode('/images/kernel')
         self.assertNotIn('data', fnode.props)
 
+    def testFitImageSubentryAlignment(self):
+        """Test relative alignability of FIT image subentries"""
+        entry_args = {
+            'test-id': TEXT_DATA,
+        }
+        data, _, _, _ = self._DoReadFileDtb('166_fit_image_subentry_alignment.dts',
+                                            entry_args=entry_args)
+        dtb = fdt.Fdt.FromData(data)
+        dtb.Scan()
+
+        node = dtb.GetNode('/images/kernel')
+        data = dtb.GetProps(node)["data"].bytes
+        align_pad = 0x10 - (len(U_BOOT_SPL_DATA) % 0x10)
+        expected = (tools.GetBytes(0, 0x20) + U_BOOT_SPL_DATA +
+                    tools.GetBytes(0, align_pad) + U_BOOT_DATA)
+        self.assertEqual(expected, data)
+
+        node = dtb.GetNode('/images/fdt-1')
+        data = dtb.GetProps(node)["data"].bytes
+        expected = (U_BOOT_SPL_DTB_DATA + tools.GetBytes(0, 20) +
+                    tools.ToBytes(TEXT_DATA) + tools.GetBytes(0, 30) +
+                    U_BOOT_DTB_DATA)
+        self.assertEqual(expected, data)
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/tools/binman/test/166_fit_image_subentry_alignment.dts b/tools/binman/test/166_fit_image_subentry_alignment.dts
new file mode 100644
index 0000000000..360cac5266
--- /dev/null
+++ b/tools/binman/test/166_fit_image_subentry_alignment.dts
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/dts-v1/;
+
+/ {
+	#address-cells = <1>;
+	#size-cells = <1>;
+
+	binman {
+		fit {
+			description = "test-desc";
+			#address-cells = <1>;
+
+			images {
+				kernel {
+					description = "Offset-Align Test";
+					type = "kernel";
+					arch = "arm64";
+					os = "linux";
+					compression = "none";
+					load = <00000000>;
+					entry = <00000000>;
+					u-boot-spl {
+						offset = <0x20>;
+					};
+					u-boot {
+						align = <0x10>;
+					};
+				};
+				fdt-1 {
+					description = "Pad-Before-After Test";
+					type = "flat_dt";
+					arch = "arm64";
+					compression = "none";
+					u-boot-spl-dtb {
+					};
+					text {
+						text-label = "test-id";
+						pad-before = <20>;
+						pad-after = <30>;
+					};
+					u-boot-dtb {
+					};
+				};
+			};
+
+			configurations {
+				default = "conf-1";
+				conf-1 {
+					description = "Kernel with FDT blob";
+					kernel = "kernel";
+					fdt = "fdt-1";
+				};
+			};
+		};
+	};
+};
-- 
2.28.0



More information about the U-Boot mailing list