[PATCH v2 14/28] binman: Refactor _BuildSectionData()

Simon Glass sjg at chromium.org
Tue Oct 27 00:40:12 CET 2020


At present this function does the padding needed around an entry. It is
easier to understand what is going on if we have a function that returns
the contents of an entry, with padding included.

Refactor the code accordingly, adding a new GetPaddedData() method.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

(no changes since v1)

 tools/binman/etype/section.py | 59 +++++++++++++++++++++++++++++------
 tools/binman/image.py         |  2 +-
 2 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/tools/binman/etype/section.py b/tools/binman/etype/section.py
index d05adf00274..f80432914f2 100644
--- a/tools/binman/etype/section.py
+++ b/tools/binman/etype/section.py
@@ -16,6 +16,7 @@ from binman.entry import Entry
 from dtoc import fdt_util
 from patman import tools
 from patman import tout
+from patman.tools import ToHexSize
 
 
 class Entry_section(Entry):
@@ -144,6 +145,36 @@ class Entry_section(Entry):
     def ObtainContents(self):
         return self.GetEntryContents()
 
+    def GetPaddedDataForEntry(self, entry):
+        """Get the data for an entry including any padding
+
+        Gets the entry data and uses the section pad-byte value to add padding
+        before and after as defined by the pad-before and pad-after properties.
+        This does not consider alignment.
+
+        Args:
+            entry: Entry to check
+
+        Returns:
+            Contents of the entry along with any pad bytes before and
+            after it (bytes)
+        """
+        data = b''
+        # Handle padding before the entry
+        if entry.pad_before:
+            data += tools.GetBytes(self._pad_byte, entry.pad_before)
+
+        # Add in the actual entry data
+        data += entry.GetData()
+
+        # Handle padding after the entry
+        if entry.pad_after:
+            data += tools.GetBytes(self._pad_byte, entry.pad_after)
+
+        self.Detail('GetPaddedDataForEntry: size %s' % ToHexSize(self.data))
+
+        return data
+
     def _BuildSectionData(self):
         """Build the contents of a section
 
@@ -158,23 +189,15 @@ class Entry_section(Entry):
         section_data = b''
 
         for entry in self._entries.values():
-            data = entry.GetData()
+            data = self.GetPaddedDataForEntry(entry)
             # Handle empty space before the entry
             pad = (entry.offset or 0) - self._skip_at_start - len(section_data)
             if pad > 0:
                 section_data += tools.GetBytes(self._pad_byte, pad)
 
-            # Handle padding before the entry
-            if entry.pad_before:
-                section_data += tools.GetBytes(self._pad_byte, entry.pad_before)
-
             # Add in the actual entry data
             section_data += data
 
-            # Handle padding after the entry
-            if entry.pad_after:
-                section_data += tools.GetBytes(self._pad_byte, entry.pad_after)
-
         if self.size:
             section_data += tools.GetBytes(self._pad_byte,
                                            self.size - len(section_data))
@@ -182,6 +205,24 @@ class Entry_section(Entry):
                     (len(self._entries), len(section_data)))
         return self.CompressData(section_data)
 
+    def GetPaddedData(self):
+        """Get the data for a section including any padding
+
+        Gets the section data and uses the parent section's pad-byte value to
+        add padding before and after as defined by the pad-before and pad-after
+        properties. If this is a top-level section (i.e. an image), this is the
+        same as GetData(), since padding is not supported.
+
+        This does not consider alignment.
+
+        Returns:
+            Contents of the section along with any pad bytes before and
+            after it (bytes)
+        """
+        if self.section:
+            return super().GetPaddedData()
+        return self.GetData()
+
     def GetData(self):
         return self._BuildSectionData()
 
diff --git a/tools/binman/image.py b/tools/binman/image.py
index a8772c3763b..d65ab887b80 100644
--- a/tools/binman/image.py
+++ b/tools/binman/image.py
@@ -146,7 +146,7 @@ class Image(section.Entry_section):
         fname = tools.GetOutputFilename(self._filename)
         tout.Info("Writing image to '%s'" % fname)
         with open(fname, 'wb') as fd:
-            data = self.GetData()
+            data = self.GetPaddedData()
             fd.write(data)
         tout.Info("Wrote %#x bytes" % len(data))
 
-- 
2.29.0.rc2.309.g374f81d7ae-goog



More information about the U-Boot mailing list