fmap.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2018 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Entry-type module for a Flash map, as used by the flashrom SPI flash tool
  6. #
  7. from binman.entry import Entry
  8. from binman import fmap_util
  9. from patman import tools
  10. from patman.tools import ToHexSize
  11. from patman import tout
  12. class Entry_fmap(Entry):
  13. """An entry which contains an Fmap section
  14. Properties / Entry arguments:
  15. None
  16. FMAP is a simple format used by flashrom, an open-source utility for
  17. reading and writing the SPI flash, typically on x86 CPUs. The format
  18. provides flashrom with a list of areas, so it knows what it in the flash.
  19. It can then read or write just a single area, instead of the whole flash.
  20. The format is defined by the flashrom project, in the file lib/fmap.h -
  21. see www.flashrom.org/Flashrom for more information.
  22. When used, this entry will be populated with an FMAP which reflects the
  23. entries in the current image. Note that any hierarchy is squashed, since
  24. FMAP does not support this. Sections are represented as an area appearing
  25. before its contents, so that it is possible to reconstruct the hierarchy
  26. from the FMAP by using the offset information. This convention does not
  27. seem to be documented, but is used in Chromium OS.
  28. CBFS entries appear as a single entry, i.e. the sub-entries are ignored.
  29. """
  30. def __init__(self, section, etype, node):
  31. super().__init__(section, etype, node)
  32. def _GetFmap(self):
  33. """Build an FMAP from the entries in the current image
  34. Returns:
  35. FMAP binary data
  36. """
  37. def _AddEntries(areas, entry):
  38. entries = entry.GetEntries()
  39. tout.Debug("fmap: Add entry '%s' type '%s' (%s subentries)" %
  40. (entry.GetPath(), entry.etype, ToHexSize(entries)))
  41. if entries and entry.etype != 'cbfs':
  42. # Create an area for the section, which encompasses all entries
  43. # within it
  44. if entry.image_pos is None:
  45. pos = 0
  46. else:
  47. pos = entry.image_pos - entry.GetRootSkipAtStart()
  48. # Drop @ symbols in name
  49. name = entry.name.replace('@', '')
  50. areas.append(
  51. fmap_util.FmapArea(pos, entry.size or 0, name, 0))
  52. for subentry in entries.values():
  53. _AddEntries(areas, subentry)
  54. else:
  55. pos = entry.image_pos
  56. if pos is not None:
  57. pos -= entry.section.GetRootSkipAtStart()
  58. areas.append(fmap_util.FmapArea(pos or 0, entry.size or 0,
  59. entry.name, 0))
  60. entries = self.GetImage().GetEntries()
  61. areas = []
  62. for entry in entries.values():
  63. _AddEntries(areas, entry)
  64. return fmap_util.EncodeFmap(self.section.GetImageSize() or 0, self.name,
  65. areas)
  66. def ObtainContents(self):
  67. """Obtain a placeholder for the fmap contents"""
  68. self.SetContents(self._GetFmap())
  69. return True
  70. def ProcessContents(self):
  71. return self.ProcessContentsUpdate(self._GetFmap())