fmap_util.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2018 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Support for flashrom's FMAP format. This supports a header followed by a
  6. # number of 'areas', describing regions of a firmware storage device,
  7. # generally SPI flash.
  8. import collections
  9. import struct
  10. # constants imported from lib/fmap.h
  11. FMAP_SIGNATURE = '__FMAP__'
  12. FMAP_VER_MAJOR = 1
  13. FMAP_VER_MINOR = 0
  14. FMAP_STRLEN = 32
  15. FMAP_AREA_STATIC = 1 << 0
  16. FMAP_AREA_COMPRESSED = 1 << 1
  17. FMAP_AREA_RO = 1 << 2
  18. FMAP_HEADER_LEN = 56
  19. FMAP_AREA_LEN = 42
  20. FMAP_HEADER_FORMAT = '<8sBBQI%dsH'% (FMAP_STRLEN)
  21. FMAP_AREA_FORMAT = '<II%dsH' % (FMAP_STRLEN)
  22. FMAP_HEADER_NAMES = (
  23. 'signature',
  24. 'ver_major',
  25. 'ver_minor',
  26. 'base',
  27. 'image_size',
  28. 'name',
  29. 'nareas',
  30. )
  31. FMAP_AREA_NAMES = (
  32. 'offset',
  33. 'size',
  34. 'name',
  35. 'flags',
  36. )
  37. # These are the two data structures supported by flashrom, a header (which
  38. # appears once at the start) and an area (which is repeated until the end of
  39. # the list of areas)
  40. FmapHeader = collections.namedtuple('FmapHeader', FMAP_HEADER_NAMES)
  41. FmapArea = collections.namedtuple('FmapArea', FMAP_AREA_NAMES)
  42. def NameToFmap(name):
  43. return name.replace('\0', '').replace('-', '_').upper()
  44. def ConvertName(field_names, fields):
  45. """Convert a name to something flashrom likes
  46. Flashrom requires upper case, underscores instead of hyphens. We remove any
  47. null characters as well. This updates the 'name' value in fields.
  48. Args:
  49. field_names: List of field names for this struct
  50. fields: Dict:
  51. key: Field name
  52. value: value of that field (string for the ones we support)
  53. """
  54. name_index = field_names.index('name')
  55. fields[name_index] = NameToFmap(fields[name_index])
  56. def DecodeFmap(data):
  57. """Decode a flashmap into a header and list of areas
  58. Args:
  59. data: Data block containing the FMAP
  60. Returns:
  61. Tuple:
  62. header: FmapHeader object
  63. List of FmapArea objects
  64. """
  65. fields = list(struct.unpack(FMAP_HEADER_FORMAT, data[:FMAP_HEADER_LEN]))
  66. ConvertName(FMAP_HEADER_NAMES, fields)
  67. header = FmapHeader(*fields)
  68. areas = []
  69. data = data[FMAP_HEADER_LEN:]
  70. for area in range(header.nareas):
  71. fields = list(struct.unpack(FMAP_AREA_FORMAT, data[:FMAP_AREA_LEN]))
  72. ConvertName(FMAP_AREA_NAMES, fields)
  73. areas.append(FmapArea(*fields))
  74. data = data[FMAP_AREA_LEN:]
  75. return header, areas
  76. def EncodeFmap(image_size, name, areas):
  77. """Create a new FMAP from a list of areas
  78. Args:
  79. image_size: Size of image, to put in the header
  80. name: Name of image, to put in the header
  81. areas: List of FmapArea objects
  82. Returns:
  83. String containing the FMAP created
  84. """
  85. def _FormatBlob(fmt, names, obj):
  86. params = [getattr(obj, name) for name in names]
  87. ConvertName(names, params)
  88. return struct.pack(fmt, *params)
  89. values = FmapHeader(FMAP_SIGNATURE, 1, 0, 0, image_size, name, len(areas))
  90. blob = _FormatBlob(FMAP_HEADER_FORMAT, FMAP_HEADER_NAMES, values)
  91. for area in areas:
  92. blob += _FormatBlob(FMAP_AREA_FORMAT, FMAP_AREA_NAMES, area)
  93. return blob