It is possible to read an Image, locate its FDT map and then read it into
the binman data structures. This allows full access to the entries that
were written to the image. Add support for this.
Signed-off-by: Simon Glass
---
Changes in v2:
- Deal with travis's old lz4 version by skipping tests as necessary
- Move 129_list_fdtmap.dts into the next commit
- Rename 128_decode_image_no_hdr.dts to 129_decode_image_nohdr.dts
- Update Image for LocateFdtmap() returning the position of the header
- Update test to use _DoReadFileRealDtb() helper
tools/binman/entry.py| 5 +++
tools/binman/ftest.py| 39 ++--
tools/binman/image.py| 38 +++
tools/binman/test/129_decode_image_nohdr.dts | 33 +
4 files changed, 111 insertions(+), 4 deletions(-)
create mode 100644 tools/binman/test/129_decode_image_nohdr.dts
diff --git a/tools/binman/entry.py b/tools/binman/entry.py
index ee63d183532..6c74f2a2175 100644
--- a/tools/binman/entry.py
+++ b/tools/binman/entry.py
@@ -162,6 +162,11 @@ class Entry(object):
self.orig_offset = self.offset
self.orig_size = self.size
+# These should not be set in input files, but are set in an FDT map,
+# which is also read by this code.
+self.image_pos = fdt_util.GetInt(self._node, 'image-pos')
+self.uncomp_size = fdt_util.GetInt(self._node, 'uncomp-size')
+
self.align = fdt_util.GetInt(self._node, 'align')
if tools.NotPowerOfTwo(self.align):
raise ValueError("Node '%s': Alignment %s must be a power of two" %
diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index ce66e3a2f20..f3a8e64ad13 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -30,6 +30,7 @@ import fdt_util
import fmap_util
import test_util
import gzip
+from image import Image
import state
import tools
import tout
@@ -2286,8 +2287,7 @@ class TestFunctional(unittest.TestCase):
def testFindImageHeader(self):
"""Test locating a image header"""
self._CheckLz4()
-data = self._DoReadFileDtb('128_decode_image.dts', use_real_dtb=True,
- update_dtb=True)[0]
+data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
image = control.images['image']
entries = image.GetEntries()
entry = entries['fdtmap']
@@ -2296,8 +2296,7 @@ class TestFunctional(unittest.TestCase):
def testFindImageHeaderStart(self):
"""Test locating a image header located at the start of an image"""
-data = self._DoReadFileDtb('117_fdtmap_hdr_start.dts',
- use_real_dtb=True, update_dtb=True)[0]
+data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts')
image = control.images['image']
entries = image.GetEntries()
entry = entries['fdtmap']
@@ -2309,6 +2308,38 @@ class TestFunctional(unittest.TestCase):
data = self._DoReadFile('005_simple.dts')
self.assertEqual(None, image_header.LocateHeaderOffset(data))
+def testReadImage(self):
+"""Test reading an image and accessing its FDT map"""
+self._CheckLz4()
+data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
+image_fname = tools.GetOutputFilename('image.bin')
+orig_image = control.images['image']
+image = Image.FromFile(image_fname)
+self.assertEqual(orig_image.GetEntries().keys(),
+ image.GetEntries().keys())
+
+orig_entry = orig_image.GetEntries()['fdtmap']
+entry = image.GetEntries()['fdtmap']
+self.assertEquals(orig_entry.offset, entry.offset)
+self.assertEquals(orig_entry.size, entry.size)
+self.assertEquals(orig_entry.image_pos, entry.image_pos)
+
+def testReadImageNoHeader(self):
+"""Test accessing an image's FDT map without an image header"""
+self._CheckLz4()
+data = self._DoReadFileRealDtb('129_decode_image_nohdr.dts')
+image_fname = tools.GetOutputFilename('image.bin')
+image = Image.FromFile(image_fname)
+self.assertTrue(isinstance(image, Image))
+self.assertEqual('image', image._name)
+
+def testReadImageFail(self):
+"""Test failing to read an image image's FDT map"""
+self._DoReadFile('005_simple.dts')
+image_fname = tools.GetOutputFilename('image.bin')
+with self.assertRaises(ValueError) as e:
+image = Image.FromFile(image_fname)
+self.assertIn("Cannot find FDT map in image", str(e.exception))
if __name__ == "__main__":
unittest.main()
diff --git a/tools/binman/image.py b/tools/binman/image.py
index 6f4bd5d37b2..f890350a8d0 100644
--- a/tools/binman/image.py
+++ b/tools/binman/image.py
@@ -12,6 +12,9 @@ from operator import attrgetter
import re
import sys
+from etype import fdtmap
+from etype