binman: Support locating an FDT map
authorSimon Glass <sjg@chromium.org>
Mon, 8 Jul 2019 20:25:44 +0000 (14:25 -0600)
committerSimon Glass <sjg@chromium.org>
Wed, 24 Jul 2019 19:54:08 +0000 (12:54 -0700)
Add support for locating an image's Fdt map which is used to determine
the contents and structure of the image.

Signed-off-by: Simon Glass <sjg@chromium.org>
tools/binman/etype/fdtmap.py
tools/binman/ftest.py
tools/binman/test/128_decode_image.dts [new file with mode: 0644]

index bfd7962be3ab893781d7a1b4c6d55ffa7d1b040e..ddb9738e5cb85415359d00f1b42bed7faf350309 100644 (file)
@@ -15,7 +15,26 @@ from fdt import Fdt
 import state
 import tools
 
-FDTMAP_MAGIC = b'_FDTMAP_'
+FDTMAP_MAGIC   = b'_FDTMAP_'
+FDTMAP_HDR_LEN = 16
+
+def LocateFdtmap(data):
+    """Search an image for an fdt map
+
+    Args:
+        data: Data to search
+
+    Returns:
+        Position of fdt map in data, or None if not found. Note that the
+            position returned is of the FDT header, i.e. before the FDT data
+    """
+    hdr_pos = data.find(FDTMAP_MAGIC)
+    size = len(data)
+    if hdr_pos != -1:
+        hdr = data[hdr_pos:hdr_pos + FDTMAP_HDR_LEN]
+        if len(hdr) == FDTMAP_HDR_LEN:
+            return hdr_pos
+    return None
 
 class Entry_fdtmap(Entry):
     """An entry which contains an FDT map
@@ -24,7 +43,9 @@ class Entry_fdtmap(Entry):
         None
 
     An FDT map is just a header followed by an FDT containing a list of all the
-    entries in the image.
+    entries in the image. The root node corresponds to the image node in the
+    original FDT, and an image-name property indicates the image name in that
+    original tree.
 
     The header is the string _FDTMAP_ followed by 8 unused bytes.
 
index de459b2b3b6322ff4188b6e3f6b75e1da9b5336f..d800ba1e9d8fd47221f35a30284421a9803d4735 100644 (file)
@@ -24,6 +24,7 @@ import command
 import control
 import elf
 import fdt
+from etype import fdtmap
 import fdt_util
 import fmap_util
 import test_util
@@ -2267,6 +2268,20 @@ class TestFunctional(unittest.TestCase):
         self.assertEqual(len(data), 0x100 + section_size)
         self.assertEqual(section_size, 0x400 + dtb_size)
 
+    def testFindFdtmap(self):
+        """Test locating an FDT map in an image"""
+        self._CheckLz4()
+        data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
+        image = control.images['image']
+        entries = image.GetEntries()
+        entry = entries['fdtmap']
+        self.assertEqual(entry.image_pos, fdtmap.LocateFdtmap(data))
+
+    def testFindFdtmapMissing(self):
+        """Test failing to locate an FDP map"""
+        data = self._DoReadFile('005_simple.dts')
+        self.assertEqual(None, fdtmap.LocateFdtmap(data))
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/tools/binman/test/128_decode_image.dts b/tools/binman/test/128_decode_image.dts
new file mode 100644 (file)
index 0000000..449fccc
--- /dev/null
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/dts-v1/;
+
+/ {
+       #address-cells = <1>;
+       #size-cells = <1>;
+
+       binman {
+               size = <0xc00>;
+               u-boot {
+               };
+               section {
+                       align = <0x100>;
+                       cbfs {
+                               size = <0x400>;
+                               u-boot {
+                                       cbfs-type = "raw";
+                               };
+                               u-boot-dtb {
+                                       cbfs-type = "raw";
+                                       cbfs-compress = "lzma";
+                                       cbfs-offset = <0x80>;
+                               };
+                       };
+                       u-boot-dtb {
+                               compress = "lz4";
+                       };
+               };
+               fdtmap {
+               };
+               image-header {
+                       location = "end";
+               };
+       };
+};