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 U-Boot device tree with the microcode removed
6#
7
8import control
9from entry import Entry
10from blob import Entry_blob
11import tools
12
13class Entry_u_boot_dtb_with_ucode(Entry_blob):
14    """A U-Boot device tree file, with the microcode removed
15
16    Properties / Entry arguments:
17        - filename: Filename of u-boot.dtb (default 'u-boot.dtb')
18
19    See Entry_u_boot_ucode for full details of the three entries involved in
20    this process. This entry provides the U-Boot device-tree file, which
21    contains the microcode. If the microcode is not being collated into one
22    place then the offset and size of the microcode is recorded by this entry,
23    for use by u_boot_with_ucode_ptr. If it is being collated, then this
24    entry deletes the microcode from the device tree (to save space) and makes
25    it available to u_boot_ucode.
26    """
27    def __init__(self, section, etype, node):
28        Entry_blob.__init__(self, section, etype, node)
29        self.ucode_data = ''
30        self.collate = False
31        self.ucode_offset = None
32        self.ucode_size = None
33        self.ucode = None
34        self.ready = False
35
36    def GetDefaultFilename(self):
37        return 'u-boot.dtb'
38
39    def ProcessFdt(self, fdt):
40        # So the module can be loaded without it
41        import fdt
42
43        # If the section does not need microcode, there is nothing to do
44        ucode_dest_entry = self.section.FindEntryType(
45            'u-boot-spl-with-ucode-ptr')
46        if not ucode_dest_entry or not ucode_dest_entry.target_offset:
47            ucode_dest_entry = self.section.FindEntryType(
48                'u-boot-with-ucode-ptr')
49        if not ucode_dest_entry or not ucode_dest_entry.target_offset:
50            return True
51
52        # Remove the microcode
53        fname = self.GetDefaultFilename()
54        fdt = control.GetFdt(fname)
55        self.ucode = fdt.GetNode('/microcode')
56        if not self.ucode:
57            raise self.Raise("No /microcode node found in '%s'" % fname)
58
59        # There's no need to collate it (move all microcode into one place)
60        # if we only have one chunk of microcode.
61        self.collate = len(self.ucode.subnodes) > 1
62        for node in self.ucode.subnodes:
63            data_prop = node.props.get('data')
64            if data_prop:
65                self.ucode_data += ''.join(data_prop.bytes)
66                if self.collate:
67                    node.DeleteProp('data')
68        return True
69
70    def ObtainContents(self):
71        # Call the base class just in case it does something important.
72        Entry_blob.ObtainContents(self)
73        self._pathname = control.GetFdtPath(self._filename)
74        self.ReadBlobContents()
75        if self.ucode:
76            for node in self.ucode.subnodes:
77                data_prop = node.props.get('data')
78                if data_prop and not self.collate:
79                    # Find the offset in the device tree of the ucode data
80                    self.ucode_offset = data_prop.GetOffset() + 12
81                    self.ucode_size = len(data_prop.bytes)
82        self.ready = True
83        return True
84