binman: Add support for building x86 ROMs
[oweals/u-boot.git] / tools / binman / etype / u_boot_dtb_with_ucode.py
1 # Copyright (c) 2016 Google, Inc
2 ## Written by Simon Glass <sjg@chromium.org>
3
4 # SPDX-License-Identifier:      GPL-2.0+
5 #
6 # Entry-type module for U-Boot device tree with the microcode removed
7 #
8
9 import fdt_select
10 from entry import Entry
11 from blob import Entry_blob
12 import tools
13
14 class Entry_u_boot_dtb_with_ucode(Entry_blob):
15     """A U-Boot device tree file, with the microcode removed
16
17     See Entry_u_boot_ucode for full details of the 3 entries involved in this
18     process.
19     """
20     def __init__(self, image, etype, node):
21         Entry_blob.__init__(self, image, etype, node)
22         self.ucode_data = ''
23         self.collate = False
24         self.ucode_offset = None
25         self.ucode_size = None
26
27     def GetDefaultFilename(self):
28         return 'u-boot.dtb'
29
30     def ObtainContents(self):
31         Entry_blob.ObtainContents(self)
32
33         # If the image does not need microcode, there is nothing to do
34         ucode_dest_entry = self.image.FindEntryType('u-boot-with-ucode-ptr')
35         if not ucode_dest_entry or not ucode_dest_entry.target_pos:
36             return True
37
38         # Create a new file to hold the copied device tree
39         dtb_name = 'u-boot-dtb-with-ucode.dtb'
40         fname = tools.GetOutputFilename(dtb_name)
41         with open(fname, 'wb') as fd:
42             fd.write(self.data)
43
44         # Remove the microcode
45         fdt = fdt_select.FdtScan(fname)
46         fdt.Scan()
47         ucode = fdt.GetNode('/microcode')
48         if not ucode:
49             raise self.Raise("No /microcode node found in '%s'" % fname)
50
51         # There's no need to collate it (move all microcode into one place)
52         # if we only have one chunk of microcode.
53         self.collate = len(ucode.subnodes) > 1
54         for node in ucode.subnodes:
55             data_prop = node.props.get('data')
56             if data_prop:
57                 self.ucode_data += ''.join(data_prop.bytes)
58                 if not self.collate:
59                     poffset = data_prop.GetOffset()
60                     if poffset is None:
61                         # We cannot obtain a property offset. Collate instead.
62                         self.collate = True
63                     else:
64                         # Find the offset in the device tree of the ucode data
65                         self.ucode_offset = poffset + 12
66                         self.ucode_size = len(data_prop.bytes)
67                 if self.collate:
68                     prop = node.DeleteProp('data')
69         if self.collate:
70             fdt.Pack()
71             fdt.Flush()
72
73             # Make this file the contents of this entry
74             self._pathname = fname
75             self.ReadContents()
76         return True