from binman.entry import Entry
from binman.etype.blob import Entry_blob
+from dtoc import fdt_util
+import struct
# This is imported if needed
state = None
This is a blob containing a device tree. The contents of the blob are
obtained from the list of available device-tree files, managed by the
'state' module.
+
+ Additional attributes:
+ prepend: Header used (e.g. 'length')
"""
def __init__(self, section, etype, node):
# Put this here to allow entry-docs and help to work without libfdt
from binman import state
super().__init__(section, etype, node)
+ self.prepend = None
+
+ def ReadNode(self):
+ super().ReadNode()
+ self.prepend = fdt_util.GetString(self._node, 'prepend')
+ if self.prepend and self.prepend not in ['length']:
+ self.Raise("Invalid prepend in '%s': '%s'" %
+ (self._node.name, self.prepend))
def ObtainContents(self):
"""Get the device-tree from the list held by the 'state' module"""
# will still return the old contents
state.UpdateFdtContents(self.GetFdtEtype(), data)
return ok
+
+ def CompressData(self, indata):
+ data = super().CompressData(indata)
+ if self.prepend == 'length':
+ hdr = struct.pack('<I', len(data))
+ data = hdr + data
+ return data
+
+ def DecompressData(self, indata):
+ if self.prepend == 'length':
+ data_len = struct.unpack('<I', indata[:4])[0]
+ indata = indata[4:4 + data_len]
+ data = super().DecompressData(indata)
+ return data
expect = U_BOOT_SPL_DATA + U_BOOT_DATA
self.assertEqual(expect, data[:len(expect)])
+ def testCompressDtbPrependInvalid(self):
+ """Test that invalid header is detected"""
+ with self.assertRaises(ValueError) as e:
+ self._DoReadFileDtb('235_compress_dtb_prepend_invalid.dts')
+ self.assertIn("Node '/binman/u-boot-dtb': Invalid prepend in "
+ "'u-boot-dtb': 'invalid'", str(e.exception))
+
+ def testCompressDtbPrependLength(self):
+ """Test that compress with length header works as expected"""
+ data = self._DoReadFileRealDtb('236_compress_dtb_prepend_length.dts')
+ image = control.images['image']
+ entries = image.GetEntries()
+ self.assertIn('u-boot-dtb', entries)
+ u_boot_dtb = entries['u-boot-dtb']
+ self.assertIn('fdtmap', entries)
+ fdtmap = entries['fdtmap']
+
+ image_fname = tools.get_output_filename('image.bin')
+ orig = control.ReadEntry(image_fname, 'u-boot-dtb')
+ dtb = fdt.Fdt.FromData(orig)
+ dtb.Scan()
+ props = self._GetPropTree(dtb, ['size', 'uncomp-size'])
+ expected = {
+ 'u-boot:size': len(U_BOOT_DATA),
+ 'u-boot-dtb:uncomp-size': len(orig),
+ 'u-boot-dtb:size': u_boot_dtb.size,
+ 'fdtmap:size': fdtmap.size,
+ 'size': len(data),
+ }
+ self.assertEqual(expected, props)
+
+ # Check implementation
+ self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)])
+ rest = data[len(U_BOOT_DATA):]
+ comp_data_len = struct.unpack('<I', rest[:4])[0]
+ comp_data = rest[4:4 + comp_data_len]
+ orig2 = self._decompress(comp_data)
+ self.assertEqual(orig, orig2)
+
if __name__ == "__main__":
unittest.main()