fmap_util.py 3.3 KB

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