[U-Boot] [PATCH 31/31] binman: Allow writing a map file when something goes wrong

Simon Glass sjg at chromium.org
Fri Sep 14 10:57:36 UTC 2018


When we get a problem like overlapping regions it is sometimes hard to
figure what what is going on. At present we don't write the map file in
this case. However the file does provide useful information.

Catch any packing errors and write a map file (if enabled with -m) to aid
debugging.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 tools/binman/control.py | 12 +++++++++---
 tools/binman/entry.py   | 11 +++++++++--
 tools/binman/ftest.py   | 24 ++++++++++++++++++++++--
 tools/binman/image.py   |  7 ++++++-
 4 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/tools/binman/control.py b/tools/binman/control.py
index caa194c8990..3446e2e79c5 100644
--- a/tools/binman/control.py
+++ b/tools/binman/control.py
@@ -163,9 +163,15 @@ def Binman(options, args):
                 # completed and written, but that does not seem important.
                 image.GetEntryContents()
                 image.GetEntryOffsets()
-                image.PackEntries()
-                image.CheckSize()
-                image.CheckEntries()
+                try:
+                    image.PackEntries()
+                    image.CheckSize()
+                    image.CheckEntries()
+                except Exception as e:
+                    if options.map:
+                        fname = image.WriteMap()
+                        print "Wrote map file '%s' to show errors"  % fname
+                    raise
                 image.SetImagePos()
                 if options.update_fdt:
                     image.SetCalculatedProperties()
diff --git a/tools/binman/entry.py b/tools/binman/entry.py
index 01be291b709..648cfd241f1 100644
--- a/tools/binman/entry.py
+++ b/tools/binman/entry.py
@@ -390,10 +390,17 @@ class Entry(object):
         """
         pass
 
+    @staticmethod
+    def GetStr(value):
+        if value is None:
+            return '<none>  '
+        return '%08x' % value
+
     @staticmethod
     def WriteMapLine(fd, indent, name, offset, size, image_pos):
-        print('%08x  %s%08x  %08x  %s' % (image_pos, ' ' * indent, offset,
-                                          size, name), file=fd)
+        print('%s  %s%s  %s  %s' % (Entry.GetStr(image_pos), ' ' * indent,
+                                    Entry.GetStr(offset), Entry.GetStr(size),
+                                    name), file=fd)
 
     def WriteMap(self, fd, indent):
         """Write a map of the entry to a .map file
diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index 0fe16d90661..a32e423baf5 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -1608,8 +1608,9 @@ class TestFunctional(unittest.TestCase):
 
     def testExpandSizeBad(self):
         """Test an expanding entry which fails to provide contents"""
-        with self.assertRaises(ValueError) as e:
-            self._DoReadFileDtb('89_expand_size_bad.dts', map=True)
+        with test_util.capture_sys_output() as (stdout, stderr):
+            with self.assertRaises(ValueError) as e:
+                self._DoReadFileDtb('89_expand_size_bad.dts', map=True)
         self.assertIn("Node '/binman/_testing': Cannot obtain contents when "
                       'expanding entry', str(e.exception))
 
@@ -1725,6 +1726,25 @@ class TestFunctional(unittest.TestCase):
             TestFunctional._MakeInputFile('-boot', fd.read())
         data = self._DoReadFile('97_elf_strip.dts')
 
+    def testPackOverlapMap(self):
+        """Test that overlapping regions are detected"""
+        with test_util.capture_sys_output() as (stdout, stderr):
+            with self.assertRaises(ValueError) as e:
+                self._DoTestFile('14_pack_overlap.dts', map=True)
+        map_fname = tools.GetOutputFilename('image.map')
+        self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname,
+                         stdout.getvalue())
+
+        # We should not get an inmage, but there should be a map file
+        self.assertFalse(os.path.exists(tools.GetOutputFilename('image.bin')))
+        self.assertTrue(os.path.exists(map_fname))
+        map_data = tools.ReadFile(map_fname)
+        self.assertEqual('''ImagePos    Offset      Size  Name
+<none>    00000000  00000007  main-section
+<none>     00000000  00000004  u-boot
+<none>     00000003  00000004  u-boot-align
+''', map_data)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/tools/binman/image.py b/tools/binman/image.py
index e113a60ac9a..f237ae302df 100644
--- a/tools/binman/image.py
+++ b/tools/binman/image.py
@@ -139,10 +139,15 @@ class Image:
         return self._section.GetEntries()
 
     def WriteMap(self):
-        """Write a map of the image to a .map file"""
+        """Write a map of the image to a .map file
+
+        Returns:
+            Filename of map file written
+        """
         filename = '%s.map' % self._name
         fname = tools.GetOutputFilename(filename)
         with open(fname, 'w') as fd:
             print('%8s  %8s  %8s  %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
                   file=fd)
             self._section.WriteMap(fd, 0)
+        return fname
-- 
2.19.0.397.gdd90340f6a-goog



More information about the U-Boot mailing list