binman: Rename 'position' to 'offset'
[platform/kernel/u-boot.git] / tools / binman / etype / u_boot_with_ucode_ptr.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 import struct
9
10 import command
11 import elf
12 from entry import Entry
13 from blob import Entry_blob
14 import fdt_util
15 import tools
16
17 class Entry_u_boot_with_ucode_ptr(Entry_blob):
18     """U-Boot with embedded microcode pointer
19
20     See Entry_u_boot_ucode for full details of the 3 entries involved in this
21     process.
22     """
23     def __init__(self, section, etype, node):
24         Entry_blob.__init__(self, section, etype, node)
25         self.elf_fname = 'u-boot'
26         self.target_offset = None
27
28     def GetDefaultFilename(self):
29         return 'u-boot-nodtb.bin'
30
31     def ProcessFdt(self, fdt):
32         # Figure out where to put the microcode pointer
33         fname = tools.GetInputFilename(self.elf_fname)
34         sym = elf.GetSymbolAddress(fname, '_dt_ucode_base_size')
35         if sym:
36            self.target_offset = sym
37         elif not fdt_util.GetBool(self._node, 'optional-ucode'):
38             self.Raise('Cannot locate _dt_ucode_base_size symbol in u-boot')
39         return True
40
41     def ProcessContents(self):
42         # If the image does not need microcode, there is nothing to do
43         if not self.target_offset:
44             return
45
46         # Get the offset of the microcode
47         ucode_entry = self.section.FindEntryType('u-boot-ucode')
48         if not ucode_entry:
49             self.Raise('Cannot find microcode region u-boot-ucode')
50
51         # Check the target pos is in the section. If it is not, then U-Boot is
52         # being linked incorrectly, or is being placed at the wrong offset
53         # in the section.
54         #
55         # The section must be set up so that U-Boot is placed at the
56         # flash address to which it is linked. For example, if
57         # CONFIG_SYS_TEXT_BASE is 0xfff00000, and the ROM is 8MB, then
58         # the U-Boot region must start at offset 7MB in the section. In this
59         # case the ROM starts at 0xff800000, so the offset of the first
60         # entry in the section corresponds to that.
61         if (self.target_offset < self.offset or
62                 self.target_offset >= self.offset + self.size):
63             self.Raise('Microcode pointer _dt_ucode_base_size at %08x is '
64                 'outside the section ranging from %08x to %08x' %
65                 (self.target_offset, self.offset, self.offset + self.size))
66
67         # Get the microcode, either from u-boot-ucode or u-boot-dtb-with-ucode.
68         # If we have left the microcode in the device tree, then it will be
69         # in the former. If we extracted the microcode from the device tree
70         # and collated it in one place, it will be in the latter.
71         if ucode_entry.size:
72             offset, size = ucode_entry.offset, ucode_entry.size
73         else:
74             dtb_entry = self.section.FindEntryType('u-boot-dtb-with-ucode')
75             if not dtb_entry or not dtb_entry.ready:
76                 self.Raise('Cannot find microcode region u-boot-dtb-with-ucode')
77             offset = dtb_entry.offset + dtb_entry.ucode_offset
78             size = dtb_entry.ucode_size
79
80         # Write the microcode offset and size into the entry
81         offset_and_size = struct.pack('<2L', offset, size)
82         self.target_offset -= self.offset
83         self.ProcessContentsUpdate(self.data[:self.target_offset] +
84                                    offset_and_size +
85                                    self.data[self.target_offset + 8:])