fmap_util.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 ConvertName(field_names, fields):
  43. """Convert a name to something flashrom likes
  44. Flashrom requires upper case, underscores instead of hyphens. We remove any
  45. null characters as well. This updates the 'name' value in fields.
  46. Args:
  47. field_names: List of field names for this struct
  48. fields: Dict:
  49. key: Field name
  50. value: value of that field (string for the ones we support)
  51. """
  52. name_index = field_names.index('name')
  53. fields[name_index] = fields[name_index].replace('\0', '').replace('-', '_').upper()
  54. def DecodeFmap(data):
  55. """Decode a flashmap into a header and list of areas
  56. Args:
  57. data: Data block containing the FMAP
  58. Returns:
  59. Tuple:
  60. header: FmapHeader object
  61. List of FmapArea objects
  62. """
  63. fields = list(struct.unpack(FMAP_HEADER_FORMAT, data[:FMAP_HEADER_LEN]))
  64. ConvertName(FMAP_HEADER_NAMES, fields)
  65. header = FmapHeader(*fields)
  66. areas = []
  67. data = data[FMAP_HEADER_LEN:]
  68. for area in range(header.nareas):
  69. fields = list(struct.unpack(FMAP_AREA_FORMAT, data[:FMAP_AREA_LEN]))
  70. ConvertName(FMAP_AREA_NAMES, fields)
  71. areas.append(FmapArea(*fields))
  72. data = data[FMAP_AREA_LEN:]
  73. return header, areas
  74. def EncodeFmap(image_size, name, areas):
  75. """Create a new FMAP from a list of areas
  76. Args:
  77. image_size: Size of image, to put in the header
  78. name: Name of image, to put in the header
  79. areas: List of FmapArea objects
  80. Returns:
  81. String containing the FMAP created
  82. """
  83. def _FormatBlob(fmt, names, obj):
  84. params = [getattr(obj, name) for name in names]
  85. return struct.pack(fmt, *params)
  86. values = FmapHeader(FMAP_SIGNATURE, 1, 0, 0, image_size, name, len(areas))
  87. blob = _FormatBlob(FMAP_HEADER_FORMAT, FMAP_HEADER_NAMES, values)
  88. for area in areas:
  89. blob += _FormatBlob(FMAP_AREA_FORMAT, FMAP_AREA_NAMES, area)
  90. return blob