intel_descriptor.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Entry-type module for Intel flash descriptor
  6. #
  7. import struct
  8. from binman.entry import Entry
  9. from binman.etype.blob_ext import Entry_blob_ext
  10. FD_SIGNATURE = struct.pack('<L', 0x0ff0a55a)
  11. MAX_REGIONS = 5
  12. # Region numbers supported by the Intel firmware format
  13. (REGION_DESCRIPTOR, REGION_BIOS, REGION_ME, REGION_GBE,
  14. REGION_PDATA) = range(5)
  15. class Region:
  16. def __init__(self, data, frba, region_num):
  17. pos = frba + region_num * 4
  18. val = struct.unpack('<L', data[pos:pos + 4])[0]
  19. self.base = (val & 0xfff) << 12
  20. self.limit = ((val & 0x0fff0000) >> 4) | 0xfff
  21. self.size = self.limit - self.base + 1
  22. class Entry_intel_descriptor(Entry_blob_ext):
  23. """Intel flash descriptor block (4KB)
  24. Properties / Entry arguments:
  25. filename: Filename of file containing the descriptor. This is typically
  26. a 4KB binary file, sometimes called 'descriptor.bin'
  27. This entry is placed at the start of flash and provides information about
  28. the SPI flash regions. In particular it provides the base address and
  29. size of the ME (Management Engine) region, allowing us to place the ME
  30. binary in the right place.
  31. With this entry in your image, the position of the 'intel-me' entry will be
  32. fixed in the image, which avoids you needed to specify an offset for that
  33. region. This is useful, because it is not possible to change the position
  34. of the ME region without updating the descriptor.
  35. See README.x86 for information about x86 binary blobs.
  36. """
  37. def __init__(self, section, etype, node):
  38. super().__init__(section, etype, node)
  39. self._regions = []
  40. def Pack(self, offset):
  41. """Put this entry at the start of the image"""
  42. if self.offset is None:
  43. offset = self.section.GetStartOffset()
  44. return super().Pack(offset)
  45. def GetOffsets(self):
  46. info = {}
  47. if self.missing:
  48. # Return zero offsets so that these entries get placed somewhere
  49. if self.HasSibling('intel-me'):
  50. info['intel-me'] = [0, None]
  51. return info
  52. offset = self.data.find(FD_SIGNATURE)
  53. if offset == -1:
  54. self.Raise('Cannot find Intel Flash Descriptor (FD) signature')
  55. flvalsig, flmap0, flmap1, flmap2 = struct.unpack('<LLLL',
  56. self.data[offset:offset + 16])
  57. frba = ((flmap0 >> 16) & 0xff) << 4
  58. for i in range(MAX_REGIONS):
  59. self._regions.append(Region(self.data, frba, i))
  60. # Set the offset for ME (Management Engine) and IFWI (Integrated
  61. # Firmware Image), for now, since the others are not used.
  62. if self.HasSibling('intel-me'):
  63. info['intel-me'] = [self._regions[REGION_ME].base,
  64. self._regions[REGION_ME].size]
  65. if self.HasSibling('intel-ifwi'):
  66. info['intel-ifwi'] = [self._regions[REGION_BIOS].base, None]
  67. return info