binman: Update CBFS implementation
cover binman: Update CBFS implementation
There isn't really a solid spec for CBFS and it changes from time to time. This series updates binman to deal with four changes in the file format, with the earliest being in October 2020.
No changes are made to the CBFS-filesystem implementation. This has fallen behind, but can still be used for simple files.
This series also fixes a coverage bug, where tests for missing tools affect other tests, thus causing coverage to fall below 100%. This has showed up with the bootgen tool.
Simon Glass (6):
binman: Reset missing bintools after testing
binman: Don't add compression attribute for uncompressed files
binman: Ensure attributes always come last in the metadata
binman: Replace FILENAME_ALIGN 16 with ATTRIBUTE_ALIGN 4
binman: Rename TYPE_STAGE to TYPE_LEGACY_STAGE
binman: Move stage header into a CBFS attribute
cmd/cbfs.c | 2 +- include/cbfs.h | 2 +- tools/binman/cbfs_util.py | 125 +++++++++++++++++++-------------- tools/binman/cbfs_util_test.py | 23 +++--- tools/binman/control.py | 2 + tools/binman/ftest.py | 10 +-- 6 files changed, 93 insertions(+), 71 deletions(-)
Signature · 1 line
2.42.0.655.g421f12c284-goog
PATCH 1/6 binman: Reset missing bintools after testing
For tests which fake bintools being missing, we need to reset the list afterwards, to ensure that future tests do not also see the bintools as missing.
Reset the list when processing is complete.
---
tools/binman/control.py | 2 ++ 1 file changed, 2 insertions(+)
tools/binman/control.py +2 -0
@@ -858,6 +858,8 @@ def Binman(args): data = state.GetFdtForEtype('u-boot-dtb').GetContents() elf.UpdateFile(*elf_params, data) + bintool.Bintool.set_missing_list(None) + # This can only be True if -M is provided, since otherwise binman # would have raised an error already if invalid:
Signature · 1 line
2.42.0.655.g421f12c284-goog
Re: [PATCH 1/6] binman: Reset missing bintools after testing
Hi Simon
Quoted 17 lines · Simon Glass
For tests which fake bintools being missing, we need to reset the list afterwards, to ensure that future tests do not also see the bintools as missing.
Reset the list when processing is complete.
Signed-off-by Simon Glass <[email protected]>---
tools/binman/control.py | 2 ++ 1 file changed, 2 insertions(+)tools/binman/control.py +0
-0
tools/binman/control.py def Binman(args): · line 865
Quoted 8 lines
tools/binman/control.py +2
-0@@ -858,6 +858,8 @@ def Binman(args): data = state.GetFdtForEtype('u-boot-dtb').GetContents() elf.UpdateFile(*elf_params, data) + bintool.Bintool.set_missing_list(None) + # This can only be True if -M is provided, since otherwise binman # would have raised an error already if invalid:
Signature · 2 lines
Thanking You Neha Malcom Francis
Re: [PATCH 1/6] binman: Reset missing bintools after testing
Hi Simon
Quoted 4 lines · Simon Glass
For tests which fake bintools being missing, we need to reset the list afterwards, to ensure that future tests do not also see the bintools as missing.
Quoted 8 lines
Reset the list when processing is complete.
Signed-off-by Simon Glass <[email protected]>---
tools/binman/control.py | 2 ++ 1 file changed, 2 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 2/6 binman: Don't add compression attribute for uncompressed files
In reply to Simon Glasscbfsutil changed to skip adding a compression attribute if there is no compression. Adjust the binman implementation to do the same.
This mirrors commit 105cdf5625 in coreboot.
---
tools/binman/cbfs_util.py | 13 ++++++++----- tools/binman/cbfs_util_test.py | 8 ++++---- tools/binman/ftest.py | 6 +++--- 3 files changed, 15 insertions(+), 12 deletions(-)
tools/binman/cbfs_util.py +8 -5
@@ -333,7 +333,8 @@ class CbfsFile(object): if self.ftype == TYPE_STAGE: pass elif self.ftype == TYPE_RAW: - hdr_len += ATTR_COMPRESSION_LEN + if self.compress: + hdr_len += ATTR_COMPRESSION_LEN elif self.ftype == TYPE_EMPTY: pass else:
@@ -369,9 +370,11 @@ class CbfsFile(object): data = self.comp_bintool.compress(orig_data) self.memlen = len(orig_data) self.data_len = len(data) - attr = struct.pack(ATTR_COMPRESSION_FORMAT, - FILE_ATTR_TAG_COMPRESSION, ATTR_COMPRESSION_LEN, - self.compress, self.memlen) + if self.compress: + attr = struct.pack(ATTR_COMPRESSION_FORMAT, + FILE_ATTR_TAG_COMPRESSION, + ATTR_COMPRESSION_LEN, self.compress, + self.memlen) elif self.ftype == TYPE_EMPTY: data = tools.get_bytes(self.erase_byte, self.size) else:
@@ -405,7 +408,7 @@ class CbfsFile(object): if expected_len != actual_len: # pragma: no cover # Test coverage of this is not available since this should never # happen. It probably indicates that get_header_len() is broken. - raise ValueError("Internal error: CBFS file '%s': Expected headers of %#x bytes, got %#d" % + raise ValueError("Internal error: CBFS file '%s': Expected headers of %#x bytes, got %#x" % (self.name, expected_len, actual_len)) return hdr + name + attr + pad + content + data, hdr_len
tools/binman/cbfs_util_test.py +4 -4
@@ -96,7 +96,7 @@ class TestCbfs(unittest.TestCase): self.assertEqual(arch, cbfs.arch) return cbfs - def _check_uboot(self, cbfs, ftype=cbfs_util.TYPE_RAW, offset=0x38, + def _check_uboot(self, cbfs, ftype=cbfs_util.TYPE_RAW, offset=0x28, data=U_BOOT_DATA, cbfs_offset=None): """Check that the U-Boot file is as expected
@@ -122,7 +122,7 @@ class TestCbfs(unittest.TestCase): self.assertEqual(len(data), cfile.memlen) return cfile - def _check_dtb(self, cbfs, offset=0x38, data=U_BOOT_DTB_DATA, + def _check_dtb(self, cbfs, offset=0x28, data=U_BOOT_DTB_DATA, cbfs_offset=None): """Check that the U-Boot dtb file is as expected
@@ -598,8 +598,8 @@ class TestCbfs(unittest.TestCase): data = cbw.get_data() cbfs = cbfs_util.CbfsReader(data) - self.assertEqual(0x38, cbfs.files['u-boot'].cbfs_offset) - self.assertEqual(0x78, cbfs.files['u-boot-dtb'].cbfs_offset) + self.assertEqual(0x28, cbfs.files['u-boot'].cbfs_offset) + self.assertEqual(0x68, cbfs.files['u-boot-dtb'].cbfs_offset) if __name__ == '__main__':
tools/binman/ftest.py +3 -3
@@ -2670,9 +2670,9 @@ class TestFunctional(unittest.TestCase): 'cbfs/u-boot:offset': 0x38, 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA), 'cbfs/u-boot:image-pos': 0x38, - 'cbfs/u-boot-dtb:offset': 0xb8, + 'cbfs/u-boot-dtb:offset': 0xa8, 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA), - 'cbfs/u-boot-dtb:image-pos': 0xb8, + 'cbfs/u-boot-dtb:image-pos': 0xa8, }, props) def testCbfsBadType(self):
@@ -2854,7 +2854,7 @@ class TestFunctional(unittest.TestCase): ' u-boot 0 4 u-boot 0', ' section 100 %x section 100' % section_size, ' cbfs 100 400 cbfs 0', -' u-boot 138 4 u-boot 38', +' u-boot 128 4 u-boot 28', ' u-boot-dtb 180 105 u-boot-dtb 80 3c9', ' u-boot-dtb 500 %x u-boot-dtb 400 3c9' % fdt_size, ' fdtmap %x 3bd fdtmap %x' %
Signature · 1 line
2.42.0.655.g421f12c284-goog
Re: [PATCH 2/6] binman: Don't add compression attribute for uncompressed files
Hi Simon
Quoted 101 lines · Simon Glass
cbfsutil changed to skip adding a compression attribute if there is no compression. Adjust the binman implementation to do the same.
This mirrors commit
105cdf5625in coreboot.Signed-off-by Simon Glass <[email protected]>---
tools/binman/cbfs_util.py | 13 ++++++++----- tools/binman/cbfs_util_test.py | 8 ++++---- tools/binman/ftest.py | 6 +++--- 3 files changed, 15 insertions(+), 12 deletions(-)tools/binman/cbfs_util.py +8
-5@@ -333,7 +333,8 @@ class CbfsFile(object): if self.ftype == TYPE_STAGE: pass elif self.ftype == TYPE_RAW: - hdr_len += ATTR_COMPRESSION_LEN + if self.compress: + hdr_len += ATTR_COMPRESSION_LEN elif self.ftype == TYPE_EMPTY: pass else:@@ -369,9 +370,11 @@ class CbfsFile(object): data = self.comp_bintool.compress(orig_data) self.memlen = len(orig_data) self.data_len = len(data) - attr = struct.pack(ATTR_COMPRESSION_FORMAT, - FILE_ATTR_TAG_COMPRESSION, ATTR_COMPRESSION_LEN, - self.compress, self.memlen) + if self.compress: + attr = struct.pack(ATTR_COMPRESSION_FORMAT, + FILE_ATTR_TAG_COMPRESSION, + ATTR_COMPRESSION_LEN, self.compress, + self.memlen) elif self.ftype == TYPE_EMPTY: data = tools.get_bytes(self.erase_byte, self.size) else:@@ -405,7 +408,7 @@ class CbfsFile(object): if expected_len != actual_len: # pragma: no cover # Test coverage of this is not available since this should never # happen. It probably indicates that get_header_len() is broken. - raise ValueError("Internal error: CBFS file '%s': Expected headers of %#x bytes, got %#d" % + raise ValueError("Internal error: CBFS file '%s': Expected headers of %#x bytes, got %#x" % (self.name, expected_len, actual_len)) return hdr + name + attr + pad + content + data, hdr_lentools/binman/cbfs_util_test.py +4
-4@@ -96,7 +96,7 @@ class TestCbfs(unittest.TestCase): self.assertEqual(arch, cbfs.arch) return cbfs - def _check_uboot(self, cbfs, ftype=cbfs_util.TYPE_RAW, offset=0x38, + def _check_uboot(self, cbfs, ftype=cbfs_util.TYPE_RAW, offset=0x28, data=U_BOOT_DATA, cbfs_offset=None): """Check that the U-Boot file is as expected@@ -122,7 +122,7 @@ class TestCbfs(unittest.TestCase): self.assertEqual(len(data), cfile.memlen) return cfile - def _check_dtb(self, cbfs, offset=0x38, data=U_BOOT_DTB_DATA, + def _check_dtb(self, cbfs, offset=0x28, data=U_BOOT_DTB_DATA, cbfs_offset=None): """Check that the U-Boot dtb file is as expected@@ -598,8 +598,8 @@ class TestCbfs(unittest.TestCase): data = cbw.get_data() cbfs = cbfs_util.CbfsReader(data) - self.assertEqual(0x38, cbfs.files['u-boot'].cbfs_offset) - self.assertEqual(0x78, cbfs.files['u-boot-dtb'].cbfs_offset) + self.assertEqual(0x28, cbfs.files['u-boot'].cbfs_offset) + self.assertEqual(0x68, cbfs.files['u-boot-dtb'].cbfs_offset) if __name__ == '__main__':tools/binman/ftest.py +2
-2@@ -2670,9 +2670,9 @@ class TestFunctional(unittest.TestCase): 'cbfs/u-boot:offset': 0x38, 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA), 'cbfs/u-boot:image-pos': 0x38, - 'cbfs/u-boot-dtb:offset': 0xb8, + 'cbfs/u-boot-dtb:offset': 0xa8, 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA), - 'cbfs/u-boot-dtb:image-pos': 0xb8, + 'cbfs/u-boot-dtb:image-pos': 0xa8, }, props) def testCbfsBadType(self):
tools/binman/ftest.py class TestFunctional(unittest.TestCase): · line 2860
Quoted 8 lines
tools/binman/ftest.py +1
-1@@ -2854,7 +2854,7 @@ class TestFunctional(unittest.TestCase): ' u-boot 0 4 u-boot 0', ' section 100 %x section 100' % section_size, ' cbfs 100 400 cbfs 0', -' u-boot 138 4 u-boot 38', +' u-boot 128 4 u-boot 28', ' u-boot-dtb 180 105 u-boot-dtb 80 3c9', ' u-boot-dtb 500 %x u-boot-dtb 400 3c9' % fdt_size, ' fdtmap %x 3bd fdtmap %x' %
Signature · 2 lines
Thanking You Neha Malcom Francis
Re: [PATCH 2/6] binman: Don't add compression attribute for uncompressed files
Hi Simon
Quoted 5 lines · Simon Glass
cbfsutil changed to skip adding a compression attribute if there is no compression. Adjust the binman implementation to do the same.
This mirrors commit
105cdf5625in coreboot.
Quoted 8 lines
Signed-off-by Simon Glass <[email protected]>---
tools/binman/cbfs_util.py | 13 ++++++++----- tools/binman/cbfs_util_test.py | 8 ++++---- tools/binman/ftest.py | 6 +++--- 3 files changed, 15 insertions(+), 12 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 3/6 binman: Ensure attributes always come last in the metadata
In reply to Simon Glasscbfsutil changed to write zero bytes instead of 0xff when a small padding must be added. Adjust the binman implementation to do the same.
Drop the code which looks for an unused attribute tag, since it is not used. A future patch moves the attributes to the end of the header in any case, so no data will follow the attributes.
This mirrors commit f0cc7adb2f in coreboot.
---
tools/binman/cbfs_util.py | 46 ++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 22 deletions(-)
tools/binman/cbfs_util.py +24 -22
@@ -54,10 +54,6 @@ ATTR_COMPRESSION_FORMAT = '>IIII' ATTR_COMPRESSION_LEN = 0x10 # Attribute tags -# Depending on how the header was initialised, it may be backed with 0x00 or -# 0xff. Support both. -FILE_ATTR_TAG_UNUSED = 0 -FILE_ATTR_TAG_UNUSED2 = 0xffffffff FILE_ATTR_TAG_COMPRESSION = 0x42435a4c FILE_ATTR_TAG_HASH = 0x68736148 FILE_ATTR_TAG_POSITION = 0x42435350 # PSCB
@@ -394,6 +390,8 @@ class CbfsFile(object): raise ValueError("Internal error: CBFS file '%s': Requested offset %#x but current output position is %#x" % (self.name, self.cbfs_offset, offset)) pad = tools.get_bytes(pad_byte, pad_len) + if attr_pos: + attr_pos += pad_len hdr_len += pad_len # This is the offset of the start of the file's data,
@@ -410,7 +408,7 @@ class CbfsFile(object): # happen. It probably indicates that get_header_len() is broken. raise ValueError("Internal error: CBFS file '%s': Expected headers of %#x bytes, got %#x" % (self.name, expected_len, actual_len)) - return hdr + name + attr + pad + content + data, hdr_len + return hdr + name + pad + attr + content + data, hdr_len class CbfsWriter(object):
@@ -456,6 +454,9 @@ class CbfsWriter(object): self._arch = arch self._bootblock_size = 0 self._erase_byte = 0xff + + # Small padding to align a file uses 0 + self._small_pad_byte = 0 self._align = ENTRY_ALIGN self._add_fileheader = False if self._arch == ARCHITECTURE_X86:
@@ -477,7 +478,7 @@ class CbfsWriter(object): self._bootblock_size, self._align) self._hdr_at_start = True - def _skip_to(self, fd, offset): + def _skip_to(self, fd, offset, pad_byte): """Write out pad bytes until a given offset Args:
@@ -487,16 +488,16 @@ class CbfsWriter(object): if fd.tell() > offset: raise ValueError('No space for data before offset %#x (current offset %#x)' % (offset, fd.tell())) - fd.write(tools.get_bytes(self._erase_byte, offset - fd.tell())) + fd.write(tools.get_bytes(pad_byte, offset - fd.tell())) - def _pad_to(self, fd, offset): + def _pad_to(self, fd, offset, pad_byte): """Write out pad bytes and/or an empty file until a given offset Args: fd: File objext to write to offset: Offset to write to """ - self._align_to(fd, self._align) + self._align_to(fd, self._align, pad_byte) upto = fd.tell() if upto > offset: raise ValueError('No space for data before pad offset %#x (current offset %#x)' %
@@ -505,9 +506,9 @@ class CbfsWriter(object): if todo: cbf = CbfsFile.empty(todo, self._erase_byte) fd.write(cbf.get_data_and_offset()[0]) - self._skip_to(fd, offset) + self._skip_to(fd, offset, pad_byte) - def _align_to(self, fd, align): + def _align_to(self, fd, align, pad_byte): """Write out pad bytes until a given alignment is reached This only aligns if the resulting output would not reach the end of the
@@ -521,7 +522,7 @@ class CbfsWriter(object): """ offset = align_int(fd.tell(), align) if offset < self._size: - self._skip_to(fd, offset) + self._skip_to(fd, offset, pad_byte) def add_file_stage(self, name, data, cbfs_offset=None): """Add a new stage file to the CBFS
@@ -571,7 +572,7 @@ class CbfsWriter(object): raise ValueError('No space for header at offset %#x (current offset %#x)' % (self._header_offset, fd.tell())) if not add_fileheader: - self._pad_to(fd, self._header_offset) + self._pad_to(fd, self._header_offset, self._erase_byte) hdr = struct.pack(HEADER_FORMAT, HEADER_MAGIC, HEADER_VERSION2, self._size, self._bootblock_size, self._align, self._contents_offset, self._arch, 0xffffffff)
@@ -583,7 +584,7 @@ class CbfsWriter(object): fd.write(name) self._header_offset = fd.tell() fd.write(hdr) - self._align_to(fd, self._align) + self._align_to(fd, self._align, self._erase_byte) else: fd.write(hdr)
@@ -600,24 +601,26 @@ class CbfsWriter(object): # THe header can go at the start in some cases if self._hdr_at_start: self._write_header(fd, add_fileheader=self._add_fileheader) - self._skip_to(fd, self._contents_offset) + self._skip_to(fd, self._contents_offset, self._erase_byte) # Write out each file for cbf in self._files.values(): # Place the file at its requested place, if any offset = cbf.calc_start_offset() if offset is not None: - self._pad_to(fd, align_int_down(offset, self._align)) + self._pad_to(fd, align_int_down(offset, self._align), + self._erase_byte) pos = fd.tell() - data, data_offset = cbf.get_data_and_offset(pos, self._erase_byte) + data, data_offset = cbf.get_data_and_offset(pos, + self._small_pad_byte) fd.write(data) - self._align_to(fd, self._align) + self._align_to(fd, self._align, self._erase_byte) cbf.calced_cbfs_offset = pos + data_offset if not self._hdr_at_start: self._write_header(fd, add_fileheader=self._add_fileheader) # Pad to the end and write a pointer to the CBFS master header - self._pad_to(fd, self._base_address or self._size - 4) + self._pad_to(fd, self._base_address or self._size - 4, self._erase_byte) rel_offset = self._header_offset - self._size fd.write(struct.pack('<I', rel_offset & 0xffffffff))
@@ -810,8 +813,6 @@ class CbfsReader(object): # We don't currently use this information atag, alen, compress, _decomp_size = struct.unpack( ATTR_COMPRESSION_FORMAT, data) - elif atag == FILE_ATTR_TAG_UNUSED2: - break else: print('Unknown attribute tag %x' % atag) attr_size -= len(data)
@@ -846,7 +847,8 @@ class CbfsReader(object): def _read_string(cls, fd): """Read a string from a file - This reads a string and aligns the data to the next alignment boundary + This reads a string and aligns the data to the next alignment boundary. + The string must be nul-terminated Args: fd: File to read from
Signature · 1 line
2.42.0.655.g421f12c284-goog
Re: [PATCH 3/6] binman: Ensure attributes always come last in the metadata
cbfsutil changed to write zero bytes instead of 0xff when a small padding must be added. Adjust the binman implementation to do the same.
Drop the code which looks for an unused attribute tag, since it is not used. A future patch moves the attributes to the end of the header in any case, so no data will follow the attributes.
This mirrors commit f0cc7adb2f in coreboot.
---
tools/binman/cbfs_util.py | 46 ++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 22 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 4/6 binman: Replace FILENAME_ALIGN 16 with ATTRIBUTE_ALIGN 4
In reply to Simon Glasscbfsutil changed to 4-byte alignment for filenames instead of 16. Adjust the binman implementation to do the same.
This mirrors commit 5779ca718c in coreboot.
---
tools/binman/cbfs_util.py | 10 +++++----- tools/binman/cbfs_util_test.py | 19 ++++++++++--------- tools/binman/ftest.py | 10 +++++----- 3 files changed, 20 insertions(+), 19 deletions(-)
tools/binman/cbfs_util.py +5 -5
@@ -42,7 +42,7 @@ HEADER_VERSION2 = 0x31313132 FILE_HEADER_FORMAT = b'>8sIIII' FILE_HEADER_LEN = 0x18 FILE_MAGIC = b'LARCHIVE' -FILENAME_ALIGN = 16 # Filename lengths are aligned to this +ATTRIBUTE_ALIGN = 4 # All attribute sizes must be divisible by this # A stage header containing information about 'stage' files # Yes this is correct: this header is in litte-endian format
@@ -186,7 +186,7 @@ def _pack_string(instr): String with required padding (at least one 0x00 byte) at the end """ val = tools.to_bytes(instr) - pad_len = align_int(len(val) + 1, FILENAME_ALIGN) + pad_len = align_int(len(val) + 1, ATTRIBUTE_ALIGN) return val + tools.get_bytes(0, pad_len - len(val))
@@ -300,7 +300,7 @@ class CbfsFile(object): CbfsFile object containing the file information """ cfile = CbfsFile('', TYPE_EMPTY, b'', None) - cfile.size = space_to_use - FILE_HEADER_LEN - FILENAME_ALIGN + cfile.size = space_to_use - FILE_HEADER_LEN - ATTRIBUTE_ALIGN cfile.erase_byte = erase_byte return cfile
@@ -859,8 +859,8 @@ class CbfsReader(object): """ val = b'' while True: - data = fd.read(FILENAME_ALIGN) - if len(data) < FILENAME_ALIGN: + data = fd.read(ATTRIBUTE_ALIGN) + if len(data) < ATTRIBUTE_ALIGN: return None pos = data.find(b'\0') if pos == -1:
tools/binman/cbfs_util_test.py +10 -9
@@ -96,7 +96,7 @@ class TestCbfs(unittest.TestCase): self.assertEqual(arch, cbfs.arch) return cbfs - def _check_uboot(self, cbfs, ftype=cbfs_util.TYPE_RAW, offset=0x28, + def _check_uboot(self, cbfs, ftype=cbfs_util.TYPE_RAW, offset=0x20, data=U_BOOT_DATA, cbfs_offset=None): """Check that the U-Boot file is as expected
@@ -122,7 +122,7 @@ class TestCbfs(unittest.TestCase): self.assertEqual(len(data), cfile.memlen) return cfile - def _check_dtb(self, cbfs, offset=0x28, data=U_BOOT_DTB_DATA, + def _check_dtb(self, cbfs, offset=0x24, data=U_BOOT_DTB_DATA, cbfs_offset=None): """Check that the U-Boot dtb file is as expected
@@ -437,8 +437,9 @@ class TestCbfs(unittest.TestCase): pos = fd.tell() # Create a new CBFS with only the first 4 bytes of the compression tag, - # then try to read the file - tag_pos = pos + cbfs_util.FILE_HEADER_LEN + cbfs_util.FILENAME_ALIGN + # then try to read the file. Note that the tag gets pushed out 4 bytes + tag_pos = (4 + pos + cbfs_util.FILE_HEADER_LEN + + cbfs_util.ATTRIBUTE_ALIGN) newdata = data[:tag_pos + 4] with test_util.capture_sys_output() as (stdout, _stderr): with io.BytesIO(newdata) as fd:
@@ -489,7 +490,7 @@ class TestCbfs(unittest.TestCase): load = 0xfef20000 entry = load + 2 - cfile = self._check_uboot(cbfs, cbfs_util.TYPE_STAGE, offset=0x28, + cfile = self._check_uboot(cbfs, cbfs_util.TYPE_STAGE, offset=0x20, data=U_BOOT_DATA + U_BOOT_DTB_DATA) self.assertEqual(entry, cfile.entry)
@@ -520,7 +521,7 @@ class TestCbfs(unittest.TestCase): self.assertIn('u-boot', cbfs.files) cfile = cbfs.files['u-boot'] self.assertEqual(cfile.name, 'u-boot') - self.assertEqual(cfile.offset, 56) + self.assertEqual(cfile.offset, 0x30) self.assertEqual(cfile.data, COMPRESS_DATA) self.assertEqual(cfile.ftype, cbfs_util.TYPE_RAW) self.assertEqual(cfile.compress, cbfs_util.COMPRESS_LZ4)
@@ -529,7 +530,7 @@ class TestCbfs(unittest.TestCase): self.assertIn('u-boot-dtb', cbfs.files) cfile = cbfs.files['u-boot-dtb'] self.assertEqual(cfile.name, 'u-boot-dtb') - self.assertEqual(cfile.offset, 56) + self.assertEqual(cfile.offset, 0x34) self.assertEqual(cfile.data, COMPRESS_DATA) self.assertEqual(cfile.ftype, cbfs_util.TYPE_RAW) self.assertEqual(cfile.compress, cbfs_util.COMPRESS_LZMA)
@@ -598,8 +599,8 @@ class TestCbfs(unittest.TestCase): data = cbw.get_data() cbfs = cbfs_util.CbfsReader(data) - self.assertEqual(0x28, cbfs.files['u-boot'].cbfs_offset) - self.assertEqual(0x68, cbfs.files['u-boot-dtb'].cbfs_offset) + self.assertEqual(0x20, cbfs.files['u-boot'].cbfs_offset) + self.assertEqual(0x64, cbfs.files['u-boot-dtb'].cbfs_offset) if __name__ == '__main__':
tools/binman/ftest.py +5 -5
@@ -2667,12 +2667,12 @@ class TestFunctional(unittest.TestCase): 'cbfs:offset': 0, 'cbfs:size': len(data), 'cbfs:image-pos': 0, - 'cbfs/u-boot:offset': 0x38, + 'cbfs/u-boot:offset': 0x30, 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA), - 'cbfs/u-boot:image-pos': 0x38, - 'cbfs/u-boot-dtb:offset': 0xa8, + 'cbfs/u-boot:image-pos': 0x30, + 'cbfs/u-boot-dtb:offset': 0xa4, 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA), - 'cbfs/u-boot-dtb:image-pos': 0xa8, + 'cbfs/u-boot-dtb:image-pos': 0xa4, }, props) def testCbfsBadType(self):
@@ -2854,7 +2854,7 @@ class TestFunctional(unittest.TestCase): ' u-boot 0 4 u-boot 0', ' section 100 %x section 100' % section_size, ' cbfs 100 400 cbfs 0', -' u-boot 128 4 u-boot 28', +' u-boot 120 4 u-boot 20', ' u-boot-dtb 180 105 u-boot-dtb 80 3c9', ' u-boot-dtb 500 %x u-boot-dtb 400 3c9' % fdt_size, ' fdtmap %x 3bd fdtmap %x' %
Signature · 1 line
2.42.0.655.g421f12c284-goog
Re: [PATCH 4/6] binman: Replace FILENAME_ALIGN 16 with ATTRIBUTE_ALIGN 4
cbfsutil changed to 4-byte alignment for filenames instead of 16. Adjust the binman implementation to do the same.
This mirrors commit 5779ca718c in coreboot.
---
tools/binman/cbfs_util.py | 10 +++++----- tools/binman/cbfs_util_test.py | 19 ++++++++++--------- tools/binman/ftest.py | 10 +++++----- 3 files changed, 20 insertions(+), 19 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 5/6 binman: Rename TYPE_STAGE to TYPE_LEGACY_STAGE
In reply to Simon GlassIn preparation for changing how stages are stored, rename the existing stage tag.
---
cmd/cbfs.c | 2 +- include/cbfs.h | 2 +- tools/binman/cbfs_util.py | 12 ++++++------ tools/binman/cbfs_util_test.py | 3 ++- 4 files changed, 10 insertions(+), 9 deletions(-)
cmd/cbfs.c +1 -1
@@ -118,7 +118,7 @@ static int do_cbfs_ls(struct cmd_tbl *cmdtp, int flag, int argc,case CBFS_TYPE_CBFSHEADER : type_name ="cbfs header" ;break ; -case CBFS_TYPE_STAGE : +case CBFS_TYPE_LEGACY_STAGE : type_name ="stage" ;break ;case CBFS_TYPE_PAYLOAD :
include/cbfs.h +1 -1
@@ -22,7 +22,7 @@ enum cbfs_result {enum cbfs_filetype {CBFS_TYPE_BOOTBLOCK =0x01 ,CBFS_TYPE_CBFSHEADER =0x02 , -CBFS_TYPE_STAGE =0x10 , +CBFS_TYPE_LEGACY_STAGE =0x10 ,CBFS_TYPE_PAYLOAD =0x20 ,CBFS_TYPE_SELF =CBFS_TYPE_PAYLOAD ,
tools/binman/cbfs_util.py +6 -6
@@ -96,7 +96,7 @@ ARCH_NAMES = { # File types. Only supported ones are included here TYPE_CBFSHEADER = 0x02 # Master header, HEADER_FORMAT -TYPE_STAGE = 0x10 # Stage, holding an executable, see STAGE_FORMAT +TYPE_LEGACY_STAGE = 0x10 # Stage, holding an executable TYPE_RAW = 0x50 # Raw file, possibly compressed TYPE_EMPTY = 0xffffffff # Empty data
@@ -265,7 +265,7 @@ class CbfsFile(object): Returns: CbfsFile object containing the file information """ - cfile = CbfsFile(name, TYPE_STAGE, data, cbfs_offset) + cfile = CbfsFile(name, TYPE_LEGACY_STAGE, data, cbfs_offset) cfile.base_address = base_address return cfile
@@ -326,7 +326,7 @@ class CbfsFile(object): """ name = _pack_string(self.name) hdr_len = len(name) + FILE_HEADER_LEN - if self.ftype == TYPE_STAGE: + if self.ftype == TYPE_LEGACY_STAGE: pass elif self.ftype == TYPE_RAW: if self.compress:
@@ -354,7 +354,7 @@ class CbfsFile(object): attr = b'' pad = b'' data = self.data - if self.ftype == TYPE_STAGE: + if self.ftype == TYPE_LEGACY_STAGE: elf_data = elf.DecodeElf(data, self.base_address) content = struct.pack(STAGE_FORMAT, self.compress, elf_data.entry, elf_data.load,
@@ -639,7 +639,7 @@ class CbfsReader(object): files: Ordered list of CbfsFile objects align: Alignment to use for files, typically ENTRT_ALIGN stage_base_address: Base address to use when mapping ELF files into the - CBFS for TYPE_STAGE files. If this is larger than the code address + CBFS for TYPE_LEGACY_STAGE files. If this is larger than the code address of the ELF file, then data at the start of the ELF file will not appear in the CBFS. Currently there are no tests for behaviour as documentation is sparse
@@ -750,7 +750,7 @@ class CbfsReader(object): fd.seek(cbfs_offset, io.SEEK_SET) if ftype == TYPE_CBFSHEADER: self._read_header(fd) - elif ftype == TYPE_STAGE: + elif ftype == TYPE_LEGACY_STAGE: data = fd.read(STAGE_LEN) cfile = CbfsFile.stage(self.stage_base_address, name, b'', cbfs_offset)
tools/binman/cbfs_util_test.py +2 -1
@@ -490,7 +490,8 @@ class TestCbfs(unittest.TestCase): load = 0xfef20000 entry = load + 2 - cfile = self._check_uboot(cbfs, cbfs_util.TYPE_STAGE, offset=0x20, + cfile = self._check_uboot(cbfs, cbfs_util.TYPE_LEGACY_STAGE, + offset=0x20, data=U_BOOT_DATA + U_BOOT_DTB_DATA) self.assertEqual(entry, cfile.entry)
Signature · 1 line
2.42.0.655.g421f12c284-goog
Re: [PATCH 5/6] binman: Rename TYPE_STAGE to TYPE_LEGACY_STAGE
In preparation for changing how stages are stored, rename the existing stage tag.
---
cmd/cbfs.c | 2 +- include/cbfs.h | 2 +- tools/binman/cbfs_util.py | 12 ++++++------ tools/binman/cbfs_util_test.py | 3 ++- 4 files changed, 10 insertions(+), 9 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 6/6 binman: Move stage header into a CBFS attribute
In reply to Simon Glasscbfsutil completely changed the way that stages are formatted in CBFS. Adjust the binman implementation to do the same.
This mirrors commit 81dc20e744 in coreboot.
---
tools/binman/cbfs_util.py | 64 +++++++++++++++++++++------------- tools/binman/cbfs_util_test.py | 7 ++-- 2 files changed, 42 insertions(+), 29 deletions(-)
tools/binman/cbfs_util.py +39 -25
@@ -44,10 +44,10 @@ FILE_HEADER_LEN = 0x18 FILE_MAGIC = b'LARCHIVE' ATTRIBUTE_ALIGN = 4 # All attribute sizes must be divisible by this -# A stage header containing information about 'stage' files +# A stage-header attribute containing information about 'stage' files # Yes this is correct: this header is in litte-endian format -STAGE_FORMAT = '<IQQII' -STAGE_LEN = 0x1c +ATTR_STAGE_FORMAT = '>IIQII' +ATTR_STAGE_LEN = 0x18 # An attribute describring the compression used in a file ATTR_COMPRESSION_FORMAT = '>IIII'
@@ -59,6 +59,7 @@ FILE_ATTR_TAG_HASH = 0x68736148 FILE_ATTR_TAG_POSITION = 0x42435350 # PSCB FILE_ATTR_TAG_ALIGNMENT = 0x42434c41 # ALCB FILE_ATTR_TAG_PADDING = 0x47444150 # PDNG +FILE_ATTR_TAG_STAGEHEADER = 0x53746748 # StgH # This is 'the size of bootblock reserved in firmware image (cbfs.txt)' # Not much more info is available, but we set it to 4, due to this comment in
@@ -97,6 +98,7 @@ ARCH_NAMES = { # File types. Only supported ones are included here TYPE_CBFSHEADER = 0x02 # Master header, HEADER_FORMAT TYPE_LEGACY_STAGE = 0x10 # Stage, holding an executable +TYPE_STAGE = 0x11 # New-type stage with ATTR_STAGE_FORMAT TYPE_RAW = 0x50 # Raw file, possibly compressed TYPE_EMPTY = 0xffffffff # Empty data
@@ -265,7 +267,7 @@ class CbfsFile(object): Returns: CbfsFile object containing the file information """ - cfile = CbfsFile(name, TYPE_LEGACY_STAGE, data, cbfs_offset) + cfile = CbfsFile(name, TYPE_STAGE, data, cbfs_offset) cfile.base_address = base_address return cfile
@@ -326,8 +328,8 @@ class CbfsFile(object): """ name = _pack_string(self.name) hdr_len = len(name) + FILE_HEADER_LEN - if self.ftype == TYPE_LEGACY_STAGE: - pass + if self.ftype == TYPE_STAGE: + hdr_len += ATTR_STAGE_LEN elif self.ftype == TYPE_RAW: if self.compress: hdr_len += ATTR_COMPRESSION_LEN
@@ -354,11 +356,11 @@ class CbfsFile(object): attr = b'' pad = b'' data = self.data - if self.ftype == TYPE_LEGACY_STAGE: + if self.ftype == TYPE_STAGE: elf_data = elf.DecodeElf(data, self.base_address) - content = struct.pack(STAGE_FORMAT, self.compress, - elf_data.entry, elf_data.load, - len(elf_data.data), elf_data.memsize) + attr = struct.pack(ATTR_STAGE_FORMAT, FILE_ATTR_TAG_STAGEHEADER, + ATTR_STAGE_LEN, elf_data.load, + elf_data.entry - elf_data.load, elf_data.memsize) data = elf_data.data elif self.ftype == TYPE_RAW: orig_data = data
@@ -639,7 +641,7 @@ class CbfsReader(object): files: Ordered list of CbfsFile objects align: Alignment to use for files, typically ENTRT_ALIGN stage_base_address: Base address to use when mapping ELF files into the - CBFS for TYPE_LEGACY_STAGE files. If this is larger than the code address + CBFS for TYPE_STAGE files. If this is larger than the code address of the ELF file, then data at the start of the ELF file will not appear in the CBFS. Currently there are no tests for behaviour as documentation is sparse
@@ -740,26 +742,28 @@ class CbfsReader(object): print('name', name) # If there are attribute headers present, read those - compress = self._read_attr(fd, file_pos, attr, offset) - if compress is None: + attrs = self._read_attr(fd, file_pos, attr, offset) + if attrs is None: return False # Create the correct CbfsFile object depending on the type cfile = None cbfs_offset = file_pos + offset fd.seek(cbfs_offset, io.SEEK_SET) + if DEBUG: + print(f'ftype {ftype:x}') if ftype == TYPE_CBFSHEADER: self._read_header(fd) - elif ftype == TYPE_LEGACY_STAGE: - data = fd.read(STAGE_LEN) + elif ftype == TYPE_STAGE: cfile = CbfsFile.stage(self.stage_base_address, name, b'', cbfs_offset) - (cfile.compress, cfile.entry, cfile.load, cfile.data_len, - cfile.memlen) = struct.unpack(STAGE_FORMAT, data) - cfile.data = fd.read(cfile.data_len) + cfile.load, entry_offset, cfile.memlen = attrs + cfile.entry = cfile.load + entry_offset + cfile.data = fd.read(cfile.memlen) + cfile.data_len = cfile.memlen elif ftype == TYPE_RAW: data = fd.read(size) - cfile = CbfsFile.raw(name, data, cbfs_offset, compress) + cfile = CbfsFile.raw(name, data, cbfs_offset, attrs) cfile.decompress() if DEBUG: print('data', data)
@@ -783,8 +787,8 @@ class CbfsReader(object): """Read attributes from the file CBFS files can have attributes which are things that cannot fit into the - header. The only attributes currently supported are compression and the - unused tag. + header. The only attributes currently supported are compression, stage + header and the unused tag Args: fd: File to read from
@@ -794,11 +798,16 @@ class CbfsReader(object): attributes) Returns: - Compression to use for the file (COMPRESS_...) + Either: + Compression to use for the file (COMPRESS_...) + tuple containing stage info: + load address + entry offset + memory size """ - compress = COMPRESS_NONE + attrs = None if not attr: - return compress + return COMPRESS_NONE attr_size = offset - attr fd.seek(file_pos + attr, io.SEEK_SET) while attr_size:
@@ -813,10 +822,15 @@ class CbfsReader(object): # We don't currently use this information atag, alen, compress, _decomp_size = struct.unpack( ATTR_COMPRESSION_FORMAT, data) + attrs = compress + elif atag == FILE_ATTR_TAG_STAGEHEADER: + atag, alen, load, entry_offset, memsize = struct.unpack( + ATTR_STAGE_FORMAT, data) + attrs = (load, entry_offset, memsize) else: print('Unknown attribute tag %x' % atag) attr_size -= len(data) - return compress + return attrs def _read_header(self, fd): """Read the master header
tools/binman/cbfs_util_test.py +3 -4
@@ -391,7 +391,7 @@ class TestCbfs(unittest.TestCase): cbfs_util.DEBUG = True with test_util.capture_sys_output() as (stdout, _stderr): cbfs_util.CbfsReader(data) - self.assertEqual('name u-boot\ndata %s\n' % U_BOOT_DATA, + self.assertEqual('name u-boot\nftype 50\ndata %s\n' % U_BOOT_DATA, stdout.getvalue()) finally: cbfs_util.DEBUG = False
@@ -475,7 +475,7 @@ class TestCbfs(unittest.TestCase): self._compare_expected_cbfs(data, cbfs_fname) def test_cbfs_stage(self): - """Tests handling of a Coreboot Filesystem (CBFS)""" + """Tests handling of a CBFS stage""" if not elf.ELF_TOOLS: self.skipTest('Python elftools not available') elf_fname = os.path.join(self._indir, 'cbfs-stage.elf')
@@ -490,8 +490,7 @@ class TestCbfs(unittest.TestCase): load = 0xfef20000 entry = load + 2 - cfile = self._check_uboot(cbfs, cbfs_util.TYPE_LEGACY_STAGE, - offset=0x20, + cfile = self._check_uboot(cbfs, cbfs_util.TYPE_STAGE, offset=0x38, data=U_BOOT_DATA + U_BOOT_DTB_DATA) self.assertEqual(entry, cfile.entry)
Signature · 1 line
2.42.0.655.g421f12c284-goog
Re: [PATCH 6/6] binman: Move stage header into a CBFS attribute
cbfsutil completely changed the way that stages are formatted in CBFS. Adjust the binman implementation to do the same.
This mirrors commit 81dc20e744 in coreboot.
---
tools/binman/cbfs_util.py | 64 +++++++++++++++++++++------------- tools/binman/cbfs_util_test.py | 7 ++-- 2 files changed, 42 insertions(+), 29 deletions(-)
Applied to u-boot-dm, thanks!