fmap.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 entry import Entry
  8. import fmap_util
  9. class Entry_fmap(Entry):
  10. """An entry which contains an Fmap section
  11. Properties / Entry arguments:
  12. None
  13. FMAP is a simple format used by flashrom, an open-source utility for
  14. reading and writing the SPI flash, typically on x86 CPUs. The format
  15. provides flashrom with a list of areas, so it knows what it in the flash.
  16. It can then read or write just a single area, instead of the whole flash.
  17. The format is defined by the flashrom project, in the file lib/fmap.h -
  18. see www.flashrom.org/Flashrom for more information.
  19. When used, this entry will be populated with an FMAP which reflects the
  20. entries in the current image. Note that any hierarchy is squashed, since
  21. FMAP does not support this.
  22. """
  23. def __init__(self, section, etype, node):
  24. Entry.__init__(self, section, etype, node)
  25. def _GetFmap(self):
  26. """Build an FMAP from the entries in the current image
  27. Returns:
  28. FMAP binary data
  29. """
  30. def _AddEntries(areas, entry):
  31. entries = entry.GetEntries()
  32. if entries:
  33. for subentry in entries.values():
  34. _AddEntries(areas, subentry)
  35. else:
  36. pos = entry.image_pos
  37. if pos is not None:
  38. pos -= entry.section.GetRootSkipAtStart()
  39. areas.append(fmap_util.FmapArea(pos or 0, entry.size or 0,
  40. entry.name, 0))
  41. entries = self.section._image.GetEntries()
  42. areas = []
  43. for entry in entries.values():
  44. _AddEntries(areas, entry)
  45. return fmap_util.EncodeFmap(self.section.GetImageSize() or 0, self.name,
  46. areas)
  47. def ObtainContents(self):
  48. """Obtain a placeholder for the fmap contents"""
  49. self.SetContents(self._GetFmap())
  50. return True
  51. def ProcessContents(self):
  52. self.SetContents(self._GetFmap())