Merge tag 'u-boot-rockchip-20200501' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / tools / binman / etype / intel_descriptor.py
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
8 import struct
9
10 from binman.entry import Entry
11 from binman.etype.blob import Entry_blob
12
13 FD_SIGNATURE   = struct.pack('<L', 0x0ff0a55a)
14 MAX_REGIONS    = 5
15
16 # Region numbers supported by the Intel firmware format
17 (REGION_DESCRIPTOR, REGION_BIOS, REGION_ME, REGION_GBE,
18         REGION_PDATA) = range(5)
19
20 class Region:
21     def __init__(self, data, frba, region_num):
22         pos = frba + region_num * 4
23         val = struct.unpack('<L', data[pos:pos + 4])[0]
24         self.base = (val & 0xfff) << 12
25         self.limit = ((val & 0x0fff0000) >> 4) | 0xfff
26         self.size = self.limit - self.base + 1
27
28 class Entry_intel_descriptor(Entry_blob):
29     """Intel flash descriptor block (4KB)
30
31     Properties / Entry arguments:
32         filename: Filename of file containing the descriptor. This is typically
33             a 4KB binary file, sometimes called 'descriptor.bin'
34
35     This entry is placed at the start of flash and provides information about
36     the SPI flash regions. In particular it provides the base address and
37     size of the ME (Management Engine) region, allowing us to place the ME
38     binary in the right place.
39
40     With this entry in your image, the position of the 'intel-me' entry will be
41     fixed in the image, which avoids you needed to specify an offset for that
42     region. This is useful, because it is not possible to change the position
43     of the ME region without updating the descriptor.
44
45     See README.x86 for information about x86 binary blobs.
46     """
47     def __init__(self, section, etype, node):
48         Entry_blob.__init__(self, section, etype, node)
49         self._regions = []
50
51     def Pack(self, offset):
52         """Put this entry at the start of the image"""
53         if self.offset is None:
54             offset = self.section.GetStartOffset()
55         return Entry_blob.Pack(self, offset)
56
57     def GetOffsets(self):
58         offset = self.data.find(FD_SIGNATURE)
59         if offset == -1:
60             self.Raise('Cannot find Intel Flash Descriptor (FD) signature')
61         flvalsig, flmap0, flmap1, flmap2 = struct.unpack('<LLLL',
62                                                 self.data[offset:offset + 16])
63         frba = ((flmap0 >> 16) & 0xff) << 4
64         for i in range(MAX_REGIONS):
65             self._regions.append(Region(self.data, frba, i))
66
67         # Set the offset for ME (Management Engine) and IFWI (Integrated
68         # Firmware Image), for now, since the others are not used.
69         info = {}
70         if self.HasSibling('intel-me'):
71             info['intel-me'] = [self._regions[REGION_ME].base,
72                                 self._regions[REGION_ME].size]
73         if self.HasSibling('intel-ifwi'):
74             info['intel-ifwi'] = [self._regions[REGION_BIOS].base, None]
75         return info