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
9import fdt
10from entry import Entry
11from blob import Entry_blob
12import tools
13
14class 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, section, etype, node):
21        Entry_blob.__init__(self, section, etype, node)
22        self.ucode_data = ''
23        self.collate = False
24        self.ucode_offset = None
25        self.ucode_size = None
26        self.ucode = None
27        self.ready = False
28
29    def GetDefaultFilename(self):
30        return 'u-boot.dtb'
31
32    def ProcessFdt(self, fdt):
33        # If the section does not need microcode, there is nothing to do
34        ucode_dest_entry = self.section.FindEntryType(
35            'u-boot-spl-with-ucode-ptr')
36        if not ucode_dest_entry or not ucode_dest_entry.target_pos:
37            ucode_dest_entry = self.section.FindEntryType(
38                'u-boot-with-ucode-ptr')
39        if not ucode_dest_entry or not ucode_dest_entry.target_pos:
40            return True
41
42        # Remove the microcode
43        fname = self.GetDefaultFilename()
44        fdt = control.GetFdt(fname)
45        self.ucode = fdt.GetNode('/microcode')
46        if not self.ucode:
47            raise self.Raise("No /microcode node found in '%s'" % fname)
48
49        # There's no need to collate it (move all microcode into one place)
50        # if we only have one chunk of microcode.
51        self.collate = len(self.ucode.subnodes) > 1
52        for node in self.ucode.subnodes:
53            data_prop = node.props.get('data')
54            if data_prop:
55                self.ucode_data += ''.join(data_prop.bytes)
56                if self.collate:
57                    node.DeleteProp('data')
58        return True
59
60    def ObtainContents(self):
61        # Call the base class just in case it does something important.
62        Entry_blob.ObtainContents(self)
63        self._pathname = control.GetFdtPath(self._filename)
64        self.ReadContents()
65        if self.ucode:
66            for node in self.ucode.subnodes:
67                data_prop = node.props.get('data')
68                if data_prop and not self.collate:
69                    # Find the offset in the device tree of the ucode data
70                    self.ucode_offset = data_prop.GetOffset() + 12
71                    self.ucode_size = len(data_prop.bytes)
72        self.ready = True
73        return True
74