1
0
mirror of https://xff.cz/git/u-boot/ synced 2025-09-08 20:22:10 +02:00

binman: Refactor _BuildSectionData()

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@chromium.org>
This commit is contained in:
Simon Glass
2020-10-26 17:40:12 -06:00
parent 17ea9f35e7
commit 4a655c9bd7
2 changed files with 51 additions and 10 deletions

View File

@@ -16,6 +16,7 @@ from binman.entry import Entry
from dtoc import fdt_util from dtoc import fdt_util
from patman import tools from patman import tools
from patman import tout from patman import tout
from patman.tools import ToHexSize
class Entry_section(Entry): class Entry_section(Entry):
@@ -144,6 +145,36 @@ class Entry_section(Entry):
def ObtainContents(self): def ObtainContents(self):
return self.GetEntryContents() 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): def _BuildSectionData(self):
"""Build the contents of a section """Build the contents of a section
@@ -158,23 +189,15 @@ class Entry_section(Entry):
section_data = b'' section_data = b''
for entry in self._entries.values(): for entry in self._entries.values():
data = entry.GetData() data = self.GetPaddedDataForEntry(entry)
# Handle empty space before the entry # Handle empty space before the entry
pad = (entry.offset or 0) - self._skip_at_start - len(section_data) pad = (entry.offset or 0) - self._skip_at_start - len(section_data)
if pad > 0: if pad > 0:
section_data += tools.GetBytes(self._pad_byte, pad) 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 # Add in the actual entry data
section_data += 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: if self.size:
section_data += tools.GetBytes(self._pad_byte, section_data += tools.GetBytes(self._pad_byte,
self.size - len(section_data)) self.size - len(section_data))
@@ -182,6 +205,24 @@ class Entry_section(Entry):
(len(self._entries), len(section_data))) (len(self._entries), len(section_data)))
return self.CompressData(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): def GetData(self):
return self._BuildSectionData() return self._BuildSectionData()

View File

@@ -146,7 +146,7 @@ class Image(section.Entry_section):
fname = tools.GetOutputFilename(self._filename) fname = tools.GetOutputFilename(self._filename)
tout.Info("Writing image to '%s'" % fname) tout.Info("Writing image to '%s'" % fname)
with open(fname, 'wb') as fd: with open(fname, 'wb') as fd:
data = self.GetData() data = self.GetPaddedData()
fd.write(data) fd.write(data)
tout.Info("Wrote %#x bytes" % len(data)) tout.Info("Wrote %#x bytes" % len(data))