]> git.sur5r.net Git - u-boot/blob - tools/binman/etype/u_boot_ucode.py
10130a28114c178d3578731d6d2bf3139f40e1a0
[u-boot] / tools / binman / etype / u_boot_ucode.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 a U-Boot binary with an embedded microcode pointer
6 #
7
8 from entry import Entry
9 from blob import Entry_blob
10 import tools
11
12 class Entry_u_boot_ucode(Entry_blob):
13     """U-Boot microcode block
14
15     U-Boot on x86 needs a single block of microcode. This is collected from
16     the various microcode update nodes in the device tree. It is also unable
17     to read the microcode from the device tree on platforms that use FSP
18     (Firmware Support Package) binaries, because the API requires that the
19     microcode is supplied before there is any SRAM available to use (i.e.
20     the FSP sets up the SRAM / cache-as-RAM but does so in the call that
21     requires the microcode!). To keep things simple, all x86 platforms handle
22     microcode the same way in U-Boot (even non-FSP platforms). This is that
23     a table is placed at _dt_ucode_base_size containing the base address and
24     size of the microcode. This is either passed to the FSP (for FSP
25     platforms), or used to set up the microcode (for non-FSP platforms).
26     This all happens in the build system since it is the only way to get
27     the microcode into a single blob and accessible without SRAM.
28
29     There are two cases to handle. If there is only one microcode blob in
30     the device tree, then the ucode pointer it set to point to that. This
31     entry (u-boot-ucode) is empty. If there is more than one update, then
32     this entry holds the concatenation of all updates, and the device tree
33     entry (u-boot-dtb-with-ucode) is updated to remove the microcode. This
34     last step ensures that that the microcode appears in one contiguous
35     block in the image and is not unnecessarily duplicated in the device
36     tree. It is referred to as 'collation' here.
37
38     Entry types that have a part to play in handling microcode:
39
40         Entry_u_boot_with_ucode_ptr:
41             Contains u-boot-nodtb.bin (i.e. U-Boot without the device tree).
42             It updates it with the address and size of the microcode so that
43             U-Boot can find it early on start-up.
44         Entry_u_boot_dtb_with_ucode:
45             Contains u-boot.dtb. It stores the microcode in a
46             'self.ucode_data' property, which is then read by this class to
47             obtain the microcode if needed. If collation is performed, it
48             removes the microcode from the device tree.
49         Entry_u_boot_ucode:
50             This class. If collation is enabled it reads the microcode from
51             the Entry_u_boot_dtb_with_ucode entry, and uses it as the
52             contents of this entry.
53     """
54     def __init__(self, image, etype, node):
55         Entry_blob.__init__(self, image, etype, node)
56
57     def ObtainContents(self):
58         # If the image does not need microcode, there is nothing to do
59         ucode_dest_entry = self.image.FindEntryType('u-boot-with-ucode-ptr')
60         ucode_dest_entry_spl = self.image.FindEntryType(
61             'u-boot-spl-with-ucode-ptr')
62         if ((not ucode_dest_entry or not ucode_dest_entry.target_pos) and
63             (not ucode_dest_entry_spl or not ucode_dest_entry_spl.target_pos)):
64             self.data = ''
65             return True
66
67         # Get the microcode from the device tree entry
68         fdt_entry = self.image.FindEntryType('u-boot-dtb-with-ucode')
69         if not fdt_entry or not fdt_entry.ucode_data:
70             return False
71
72         if not fdt_entry.collate:
73             # This section can be empty
74             self.data = ''
75             return True
76
77         # Write it out to a file
78         dtb_name = 'u-boot-ucode.bin'
79         fname = tools.GetOutputFilename(dtb_name)
80         with open(fname, 'wb') as fd:
81             fd.write(fdt_entry.ucode_data)
82
83         self._pathname = fname
84         self.ReadContents()
85
86         return True