Skip to contentSkip to conversation

binman: Make FIT image subentries respect offset, alignment and padding

PATCH New Alper Nebi Yasak 2 participants 10 messages Aug 31, 2020 – Sep 1, 2020

lore
Share & tools
lore
#0 lore raw

v2 cover binman: Make FIT image subentries respect offset, alignment and padding

I've been automating the process in doc/README.chromium-chainload and while experimenting with whether a "kernel" image with u-boot-spl and u-boot would work, noticed I couldn't align/offset/pad the two parts.

E.g. in something like the following, binman doesn't add the necessary padding to place the "u-boot" to the correct offset within the "kernel-1" data:

	fit {
		description = "example";

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

				u-boot-spl {
				};
				u-boot {
					offset = <CONFIG_SPL_PAD_TO>;
				};
			};
		};
	};

Not sure if that'll ever be really necessary besides my experiment, but it doesn't seem like skipping the padding was a deliberate choice, so here are some fixes I wrote for that.

Changes in v2:
- Add test to check that sections ignore hash*, signature* nodes
- Move section padding test to the end of file
- Renumber tests to accommodate for the first patch's new test
- Use 'section' instead of 'image' for FIT subimage sections
- Also rename 'Members:' comment for the renamed _fit_content variable
- Clarify comments around FIT subimage section/content processing
- Don't check for section.ObtainContents() returning False (never does)

v1: https://patchwork.ozlabs.org/project/uboot/list/?series=197693

Alper Nebi Yasak (3):
binman: Ignore hash*, signature* nodes in sections
binman: Respect pad-before property of section subentries
binman: Build FIT image subentries with the section etype

 tools/binman/etype/fit.py                     | 41 +++++++------
 tools/binman/etype/section.py                 |  4 +-
 tools/binman/ftest.py                         | 37 ++++++++++++
 .../165_section_ignore_hash_signature.dts     | 40 +++++++++++++
 tools/binman/test/166_pad_in_sections.dts     | 26 +++++++++
 .../test/167_fit_image_subentry_alignment.dts | 57 +++++++++++++++++++
 6 files changed, 186 insertions(+), 19 deletions(-)
 create mode 100644 tools/binman/test/165_section_ignore_hash_signature.dts
 create mode 100644 tools/binman/test/166_pad_in_sections.dts
 create mode 100644 tools/binman/test/167_fit_image_subentry_alignment.dts
Signature · 1 line
2.28.0
#1 lore raw

PATCH v2 1/3 binman: Ignore hash*, signature* nodes in sections

Switch to str.startswith for matching like the FIT etype does since the current version doesn't ignore 'hash-1', 'hash-2', etc.

---

Changes in v2:
- Add test to check that sections ignore hash*, signature* nodes

 tools/binman/etype/section.py                 |  2 +-
 tools/binman/ftest.py                         |  6 +++
 .../165_section_ignore_hash_signature.dts     | 40 +++++++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)
 create mode 100644 tools/binman/test/165_section_ignore_hash_signature.dts
tools/binman/etype/section.py +1 -1
index 73c5553c81..c5166a5b57 100644
@@ -83,7 +83,7 @@ class Entry_section(Entry):
 
     def _ReadEntries(self):
         for node in self._node.subnodes:
-            if node.name == 'hash':
+            if node.name.startswith('hash') or node.name.startswith('signature'):
                 continue
             entry = Entry.Create(self, node)
             entry.ReadNode()
tools/binman/ftest.py +6 -0
index 5f650b5f94..ab88ee96ab 100644
@@ -3477,5 +3477,11 @@ class TestFunctional(unittest.TestCase):
         fnode = dtb.GetNode('/images/kernel')
         self.assertNotIn('data', fnode.props)
 
+    def testSectionIgnoreHashSignature(self):
+        """Test that sections ignore hash, signature nodes for its data"""
+        data = self._DoReadFile('165_section_ignore_hash_signature.dts')
+        expected = (U_BOOT_DATA + U_BOOT_DATA)
+        self.assertEqual(expected, data)
+
 if __name__ == "__main__":
     unittest.main()
tools/binman/test/165_section_ignore_hash_signature.dts new file +40 -0
new file mode 100644
index 0000000000..8adbe25512
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/dts-v1/;
+
+/ {
+	#address-cells = <1>;
+	#size-cells = <1>;
+
+	binman {
+		section at 0 {
+			u-boot {
+			};
+			hash {
+				algo = "sha256";
+			};
+			signature {
+				algo = "sha256,rsa2048";
+				key-name-hint = "dev";
+			};
+		};
+		section at 1 {
+			u-boot {
+			};
+			hash-1 {
+				algo = "sha1";
+			};
+			hash-2 {
+				algo = "sha256";
+			};
+			signature-1 {
+				algo = "sha1,rsa2048";
+				key-name-hint = "dev";
+			};
+			signature-2 {
+				algo = "sha256,rsa2048";
+				key-name-hint = "dev";
+			};
+		};
+	};
+};
Signature · 1 line
2.28.0
#2 lore raw

PATCH v2 1/3 binman: Ignore hash*, signature* nodes in sections

Quoted 7 lines · Alper Nebi Yasak

Switch to str.startswith for matching like the FIT etype does since the current version doesn't ignore 'hash-1', 'hash-2', etc.

---

Quoted 8 lines

Changes in v2:
- Add test to check that sections ignore hash*, signature* nodes

 tools/binman/etype/section.py                 |  2 +-
 tools/binman/ftest.py                         |  6 +++
 .../165_section_ignore_hash_signature.dts     | 40 +++++++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)
 create mode 100644 tools/binman/test/165_section_ignore_hash_signature.dts
#3 lore raw

PATCH v2 1/3 binman: Ignore hash*, signature* nodes in sections

Quoted 7 lines · Alper Nebi Yasak

Switch to str.startswith for matching like the FIT etype does since the current version doesn't ignore 'hash-1', 'hash-2', etc.

---

Quoted 8 lines

Changes in v2:
- Add test to check that sections ignore hash*, signature* nodes

 tools/binman/etype/section.py                 |  2 +-
 tools/binman/ftest.py                         |  6 +++
 .../165_section_ignore_hash_signature.dts     | 40 +++++++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)
 create mode 100644 tools/binman/test/165_section_ignore_hash_signature.dts

Applied to u-boot-dm, thanks!

#4 lore raw

PATCH v2 2/3 binman: Respect pad-before property of section subentries

In reply to Alper Nebi Yasak

Other relevant properties (pad-after, offset, size, align, align-size, align-end) already work since Pack() sets correct ranges for subentries' data (.offset, .size variables), but some padding here is necessary to align the data within this range to match the pad-before property.

---

Changes in v2:
- Move section padding test to the end of file
- Renumber test to accommodate for the first patch's new test

 tools/binman/etype/section.py             |  2 +-
 tools/binman/ftest.py                     |  8 +++++++
 tools/binman/test/166_pad_in_sections.dts | 26 +++++++++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 tools/binman/test/166_pad_in_sections.dts
tools/binman/etype/section.py +1 -1
index c5166a5b57..72600b1ef3 100644
@@ -152,7 +152,7 @@ class Entry_section(Entry):
         for entry in self._entries.values():
             data = entry.GetData()
             base = self.pad_before + (entry.offset or 0) - self._skip_at_start
-            pad = base - len(section_data)
+            pad = base - len(section_data) + (entry.pad_before or 0)
             if pad > 0:
                 section_data += tools.GetBytes(self._pad_byte, pad)
             section_data += data
tools/binman/ftest.py +8 -0
index ab88ee96ab..53da709d51 100644
@@ -3483,5 +3483,13 @@ class TestFunctional(unittest.TestCase):
         expected = (U_BOOT_DATA + U_BOOT_DATA)
         self.assertEqual(expected, data)
 
+    def testPadInSections(self):
+        """Test pad-before, pad-after for entries in sections"""
+        data = self._DoReadFile('166_pad_in_sections.dts')
+        expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
+                    U_BOOT_DATA + tools.GetBytes(ord('!'), 6) +
+                    U_BOOT_DATA)
+        self.assertEqual(expected, data)
+
 if __name__ == "__main__":
     unittest.main()
tools/binman/test/166_pad_in_sections.dts new file +26 -0
new file mode 100644
index 0000000000..f2b327ff9f
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/dts-v1/;
+
+/ {
+	#address-cells = <1>;
+	#size-cells = <1>;
+
+	binman {
+		pad-byte = <0x26>;
+		section {
+			pad-byte = <0x21>;
+
+			before {
+				type = "u-boot";
+			};
+			u-boot {
+				pad-before = <12>;
+				pad-after = <6>;
+			};
+			after {
+				type = "u-boot";
+			};
+		};
+	};
+};
Signature · 1 line
2.28.0
#5 lore raw

PATCH v2 2/3 binman: Respect pad-before property of section subentries

Quoted 13 lines · Alper Nebi Yasak

Other relevant properties (pad-after, offset, size, align, align-size, align-end) already work since Pack() sets correct ranges for subentries' data (.offset, .size variables), but some padding here is necessary to align the data within this range to match the pad-before property.

---

Changes in v2:
- Move section padding test to the end of file
- Renumber test to accommodate for the first patch's new test

Quoted 5 lines
 tools/binman/etype/section.py             |  2 +-
 tools/binman/ftest.py                     |  8 +++++++
 tools/binman/test/166_pad_in_sections.dts | 26 +++++++++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 tools/binman/test/166_pad_in_sections.dts
#6 lore raw

PATCH v2 2/3 binman: Respect pad-before property of section subentries

Quoted 13 lines · Alper Nebi Yasak

Other relevant properties (pad-after, offset, size, align, align-size, align-end) already work since Pack() sets correct ranges for subentries' data (.offset, .size variables), but some padding here is necessary to align the data within this range to match the pad-before property.

---

Changes in v2:
- Move section padding test to the end of file
- Renumber test to accommodate for the first patch's new test

Quoted 5 lines
 tools/binman/etype/section.py             |  2 +-
 tools/binman/ftest.py                     |  8 +++++++
 tools/binman/test/166_pad_in_sections.dts | 26 +++++++++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 tools/binman/test/166_pad_in_sections.dts

Applied to u-boot-dm, thanks!

#7 lore raw

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

In reply to Alper Nebi Yasak

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>;
				};
			};
		};
	}

---

Changes in v2:
- Renumber test to accommodate for the first patch's new test
- Use 'section' instead of 'image' for FIT subimage sections
- Also rename 'Members:' comment for the renamed _fit_content variable
- Clarify comments around FIT subimage section/content processing
- Don't check for section.ObtainContents() returning False (never does)

 tools/binman/etype/fit.py                     | 41 +++++++------
 tools/binman/ftest.py                         | 23 ++++++++
 .../test/167_fit_image_subentry_alignment.dts | 57 +++++++++++++++++++
 3 files changed, 104 insertions(+), 17 deletions(-)
 create mode 100644 tools/binman/test/167_fit_image_subentry_alignment.dts
tools/binman/etype/fit.py +24 -17
index 75712f4409..acba53aa92 100644
@@ -55,14 +55,14 @@ class Entry_fit(Entry):
         """
         Members:
             _fit: FIT file being built
-            _fit_content: dict:
+            _fit_sections: dict:
                 key: relative path to entry Node (from the base of the FIT)
-                value: List of Entry objects comprising the contents of this
+                value: Entry_section object comprising the contents of this
                     node
         """
         super().__init__(section, etype, node)
         self._fit = None
-        self._fit_content = defaultdict(list)
+        self._fit_sections = {}
         self._fit_props = {}
 
     def ReadNode(self):
@@ -91,15 +91,23 @@ class Entry_fit(Entry):
 
             rel_path = node.path[len(base_node.path):]
             has_images = depth == 2 and rel_path.startswith('/images/')
+            if has_images:
+                # This node is a FIT subimage node (e.g. "/images/kernel")
+                # containing content nodes. We collect the subimage nodes and
+                # section entries for them here to merge the content subnodes
+                # together and put the merged contents in the subimage node's
+                # 'data' property later.
+                entry = Entry.Create(self.section, node, etype='section')
+                entry.ReadNode()
+                self._fit_sections[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)
+                    # This subnode is a content node not meant to appear in
+                    # the FIT (e.g. "/images/kernel/u-boot"), so don't call
+                    # fsw.add_node() or _AddNode() for it.
+                    pass
                 else:
                     with fsw.add_node(subnode.name):
                         _AddNode(base_node, depth + 1, subnode)
@@ -123,9 +131,8 @@ class Entry_fit(Entry):
         This adds the 'data' properties to the input ITB (Image-tree Binary)
         then runs mkimage to process it.
         """
+        # self._BuildInput() either returns bytes or raises an exception.
         data = self._BuildInput(self._fdt)
-        if data == False:
-            return False
         uniq = self.GetUniqueName()
         input_fname = tools.GetOutputFilename('%s.itb' % uniq)
         output_fname = tools.GetOutputFilename('%s.fit' % uniq)
@@ -150,13 +157,13 @@ class Entry_fit(Entry):
         Returns:
             New fdt contents (bytes)
         """
-        for path, entries in self._fit_content.items():
+        for path, section in self._fit_sections.items():
             node = fdt.GetNode(path)
-            data = b''
-            for entry in entries:
-                if not entry.ObtainContents():
-                    return False
-                data += entry.GetData()
+            # Entry_section.ObtainContents() either returns True or
+            # raises an exception.
+            section.ObtainContents()
+            section.Pack(0)
+            data = section.GetData()
             node.AddData('data', data)
 
         fdt.Sync(auto_resize=True)
tools/binman/ftest.py +23 -0
index 53da709d51..760a2dd44f 100644
@@ -3489,6 +3489,29 @@ class TestFunctional(unittest.TestCase):
         expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
                     U_BOOT_DATA + tools.GetBytes(ord('!'), 6) +
                     U_BOOT_DATA)
+
+    def testFitImageSubentryAlignment(self):
+        """Test relative alignability of FIT image subentries"""
+        entry_args = {
+            'test-id': TEXT_DATA,
+        }
+        data, _, _, _ = self._DoReadFileDtb('167_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__":
tools/binman/test/167_fit_image_subentry_alignment.dts new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.28.0
#8 lore raw

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

Quoted 43 lines · Alper Nebi Yasak

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>;
                                };
                        };
                };
        }

---

Changes in v2:
- Renumber test to accommodate for the first patch's new test
- Use 'section' instead of 'image' for FIT subimage sections
- Also rename 'Members:' comment for the renamed _fit_content variable
- Clarify comments around FIT subimage section/content processing
- Don't check for section.ObtainContents() returning False (never does)

Quoted 5 lines
 tools/binman/etype/fit.py                     | 41 +++++++------
 tools/binman/ftest.py                         | 23 ++++++++
 .../test/167_fit_image_subentry_alignment.dts | 57 +++++++++++++++++++
 3 files changed, 104 insertions(+), 17 deletions(-)
 create mode 100644 tools/binman/test/167_fit_image_subentry_alignment.dts
#9 lore raw

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

Quoted 43 lines · Alper Nebi Yasak

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>;
                                };
                        };
                };
        }

---

Changes in v2:
- Renumber test to accommodate for the first patch's new test
- Use 'section' instead of 'image' for FIT subimage sections
- Also rename 'Members:' comment for the renamed _fit_content variable
- Clarify comments around FIT subimage section/content processing
- Don't check for section.ObtainContents() returning False (never does)

Quoted 5 lines
 tools/binman/etype/fit.py                     | 41 +++++++------
 tools/binman/ftest.py                         | 23 ++++++++
 .../test/167_fit_image_subentry_alignment.dts | 57 +++++++++++++++++++
 3 files changed, 104 insertions(+), 17 deletions(-)
 create mode 100644 tools/binman/test/167_fit_image_subentry_alignment.dts

Applied to u-boot-dm, thanks!