Commit 163ed6c342cfd15b623a46f3755203c712374a9a

Authored by Simon Glass
1 parent fe1ae3ecc3

binman: Allow writing a map file when something goes wrong

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@chromium.org>

Showing 4 changed files with 46 additions and 8 deletions Side-by-side Diff

tools/binman/control.py
... ... @@ -163,9 +163,15 @@
163 163 # completed and written, but that does not seem important.
164 164 image.GetEntryContents()
165 165 image.GetEntryOffsets()
166   - image.PackEntries()
167   - image.CheckSize()
168   - image.CheckEntries()
  166 + try:
  167 + image.PackEntries()
  168 + image.CheckSize()
  169 + image.CheckEntries()
  170 + except Exception as e:
  171 + if options.map:
  172 + fname = image.WriteMap()
  173 + print "Wrote map file '%s' to show errors" % fname
  174 + raise
169 175 image.SetImagePos()
170 176 if options.update_fdt:
171 177 image.SetCalculatedProperties()
tools/binman/entry.py
... ... @@ -391,9 +391,16 @@
391 391 pass
392 392  
393 393 @staticmethod
  394 + def GetStr(value):
  395 + if value is None:
  396 + return '<none> '
  397 + return '%08x' % value
  398 +
  399 + @staticmethod
394 400 def WriteMapLine(fd, indent, name, offset, size, image_pos):
395   - print('%08x %s%08x %08x %s' % (image_pos, ' ' * indent, offset,
396   - size, name), file=fd)
  401 + print('%s %s%s %s %s' % (Entry.GetStr(image_pos), ' ' * indent,
  402 + Entry.GetStr(offset), Entry.GetStr(size),
  403 + name), file=fd)
397 404  
398 405 def WriteMap(self, fd, indent):
399 406 """Write a map of the entry to a .map file
tools/binman/ftest.py
... ... @@ -1607,8 +1607,9 @@
1607 1607  
1608 1608 def testExpandSizeBad(self):
1609 1609 """Test an expanding entry which fails to provide contents"""
1610   - with self.assertRaises(ValueError) as e:
1611   - self._DoReadFileDtb('89_expand_size_bad.dts', map=True)
  1610 + with test_util.capture_sys_output() as (stdout, stderr):
  1611 + with self.assertRaises(ValueError) as e:
  1612 + self._DoReadFileDtb('89_expand_size_bad.dts', map=True)
1612 1613 self.assertIn("Node '/binman/_testing': Cannot obtain contents when "
1613 1614 'expanding entry', str(e.exception))
1614 1615  
... ... @@ -1723,6 +1724,25 @@
1723 1724 with open(self.TestFile('bss_data')) as fd:
1724 1725 TestFunctional._MakeInputFile('-boot', fd.read())
1725 1726 data = self._DoReadFile('97_elf_strip.dts')
  1727 +
  1728 + def testPackOverlapMap(self):
  1729 + """Test that overlapping regions are detected"""
  1730 + with test_util.capture_sys_output() as (stdout, stderr):
  1731 + with self.assertRaises(ValueError) as e:
  1732 + self._DoTestFile('14_pack_overlap.dts', map=True)
  1733 + map_fname = tools.GetOutputFilename('image.map')
  1734 + self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname,
  1735 + stdout.getvalue())
  1736 +
  1737 + # We should not get an inmage, but there should be a map file
  1738 + self.assertFalse(os.path.exists(tools.GetOutputFilename('image.bin')))
  1739 + self.assertTrue(os.path.exists(map_fname))
  1740 + map_data = tools.ReadFile(map_fname)
  1741 + self.assertEqual('''ImagePos Offset Size Name
  1742 +<none> 00000000 00000007 main-section
  1743 +<none> 00000000 00000004 u-boot
  1744 +<none> 00000003 00000004 u-boot-align
  1745 +''', map_data)
1726 1746  
1727 1747  
1728 1748 if __name__ == "__main__":
tools/binman/image.py
... ... @@ -139,11 +139,16 @@
139 139 return self._section.GetEntries()
140 140  
141 141 def WriteMap(self):
142   - """Write a map of the image to a .map file"""
  142 + """Write a map of the image to a .map file
  143 +
  144 + Returns:
  145 + Filename of map file written
  146 + """
143 147 filename = '%s.map' % self._name
144 148 fname = tools.GetOutputFilename(filename)
145 149 with open(fname, 'w') as fd:
146 150 print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
147 151 file=fd)
148 152 self._section.WriteMap(fd, 0)
  153 + return fname