blob: 3811d33e4203f30c0e5723381924a7e401e1b99d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassbf7fd502016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3#
Simon Glassbf7fd502016-11-25 20:15:51 -07004# Base class for all entries
5#
6
Simon Glass3b0c3822018-06-01 09:38:20 -06007from __future__ import print_function
8
Simon Glassbf7fd502016-11-25 20:15:51 -07009# importlib was introduced in Python 2.7 but there was a report of it not
10# working in 2.7.12, so we work around this:
11# http://lists.denx.de/pipermail/u-boot/2016-October/269729.html
12try:
13 import importlib
14 have_importlib = True
15except:
16 have_importlib = False
17
18import fdt_util
Simon Glassbadf0ec2018-06-01 09:38:15 -060019import os
20import sys
Simon Glassbf7fd502016-11-25 20:15:51 -070021import tools
22
23modules = {}
24
Simon Glassbadf0ec2018-06-01 09:38:15 -060025our_path = os.path.dirname(os.path.realpath(__file__))
26
Simon Glassbf7fd502016-11-25 20:15:51 -070027class Entry(object):
Simon Glass25ac0e62018-06-01 09:38:14 -060028 """An Entry in the section
Simon Glassbf7fd502016-11-25 20:15:51 -070029
30 An entry corresponds to a single node in the device-tree description
Simon Glass25ac0e62018-06-01 09:38:14 -060031 of the section. Each entry ends up being a part of the final section.
Simon Glassbf7fd502016-11-25 20:15:51 -070032 Entries can be placed either right next to each other, or with padding
33 between them. The type of the entry determines the data that is in it.
34
35 This class is not used by itself. All entry objects are subclasses of
36 Entry.
37
38 Attributes:
Simon Glass25ac0e62018-06-01 09:38:14 -060039 section: The section containing this entry
Simon Glassbf7fd502016-11-25 20:15:51 -070040 node: The node that created this entry
Simon Glass25ac0e62018-06-01 09:38:14 -060041 pos: Absolute position of entry within the section, None if not known
Simon Glassbf7fd502016-11-25 20:15:51 -070042 size: Entry size in bytes, None if not known
43 contents_size: Size of contents in bytes, 0 by default
44 align: Entry start position alignment, or None
45 align_size: Entry size alignment, or None
46 align_end: Entry end position alignment, or None
47 pad_before: Number of pad bytes before the contents, 0 if none
48 pad_after: Number of pad bytes after the contents, 0 if none
49 data: Contents of entry (string of bytes)
50 """
Simon Glass25ac0e62018-06-01 09:38:14 -060051 def __init__(self, section, etype, node, read_node=True):
52 self.section = section
Simon Glassbf7fd502016-11-25 20:15:51 -070053 self.etype = etype
54 self._node = node
Simon Glass3b0c3822018-06-01 09:38:20 -060055 self.name = node and node.name or 'none'
Simon Glassbf7fd502016-11-25 20:15:51 -070056 self.pos = None
57 self.size = None
58 self.contents_size = 0
59 self.align = None
60 self.align_size = None
61 self.align_end = None
62 self.pad_before = 0
63 self.pad_after = 0
64 self.pos_unset = False
65 if read_node:
66 self.ReadNode()
67
68 @staticmethod
Simon Glass25ac0e62018-06-01 09:38:14 -060069 def Create(section, node, etype=None):
Simon Glassbf7fd502016-11-25 20:15:51 -070070 """Create a new entry for a node.
71
72 Args:
Simon Glass25ac0e62018-06-01 09:38:14 -060073 section: Image object containing this node
Simon Glassbf7fd502016-11-25 20:15:51 -070074 node: Node object containing information about the entry to create
75 etype: Entry type to use, or None to work it out (used for tests)
76
77 Returns:
78 A new Entry object of the correct type (a subclass of Entry)
79 """
80 if not etype:
81 etype = fdt_util.GetString(node, 'type', node.name)
Simon Glassdd57c132018-06-01 09:38:11 -060082
83 # Convert something like 'u-boot@0' to 'u_boot' since we are only
84 # interested in the type.
Simon Glassbf7fd502016-11-25 20:15:51 -070085 module_name = etype.replace('-', '_')
Simon Glassdd57c132018-06-01 09:38:11 -060086 if '@' in module_name:
87 module_name = module_name.split('@')[0]
Simon Glassbf7fd502016-11-25 20:15:51 -070088 module = modules.get(module_name)
89
Simon Glassbadf0ec2018-06-01 09:38:15 -060090 # Also allow entry-type modules to be brought in from the etype directory.
91
Simon Glassbf7fd502016-11-25 20:15:51 -070092 # Import the module if we have not already done so.
93 if not module:
Simon Glassbadf0ec2018-06-01 09:38:15 -060094 old_path = sys.path
95 sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glassbf7fd502016-11-25 20:15:51 -070096 try:
97 if have_importlib:
98 module = importlib.import_module(module_name)
99 else:
100 module = __import__(module_name)
101 except ImportError:
102 raise ValueError("Unknown entry type '%s' in node '%s'" %
103 (etype, node.path))
Simon Glassbadf0ec2018-06-01 09:38:15 -0600104 finally:
105 sys.path = old_path
Simon Glassbf7fd502016-11-25 20:15:51 -0700106 modules[module_name] = module
107
108 # Call its constructor to get the object we want.
109 obj = getattr(module, 'Entry_%s' % module_name)
Simon Glass25ac0e62018-06-01 09:38:14 -0600110 return obj(section, etype, node)
Simon Glassbf7fd502016-11-25 20:15:51 -0700111
112 def ReadNode(self):
113 """Read entry information from the node
114
115 This reads all the fields we recognise from the node, ready for use.
116 """
117 self.pos = fdt_util.GetInt(self._node, 'pos')
118 self.size = fdt_util.GetInt(self._node, 'size')
119 self.align = fdt_util.GetInt(self._node, 'align')
120 if tools.NotPowerOfTwo(self.align):
121 raise ValueError("Node '%s': Alignment %s must be a power of two" %
122 (self._node.path, self.align))
123 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
124 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
125 self.align_size = fdt_util.GetInt(self._node, 'align-size')
126 if tools.NotPowerOfTwo(self.align_size):
127 raise ValueError("Node '%s': Alignment size %s must be a power "
128 "of two" % (self._node.path, self.align_size))
129 self.align_end = fdt_util.GetInt(self._node, 'align-end')
130 self.pos_unset = fdt_util.GetBool(self._node, 'pos-unset')
131
132 def ObtainContents(self):
133 """Figure out the contents of an entry.
134
135 Returns:
136 True if the contents were found, False if another call is needed
137 after the other entries are processed.
138 """
139 # No contents by default: subclasses can implement this
140 return True
141
142 def Pack(self, pos):
Simon Glass25ac0e62018-06-01 09:38:14 -0600143 """Figure out how to pack the entry into the section
Simon Glassbf7fd502016-11-25 20:15:51 -0700144
145 Most of the time the entries are not fully specified. There may be
146 an alignment but no size. In that case we take the size from the
147 contents of the entry.
148
149 If an entry has no hard-coded position, it will be placed at @pos.
150
151 Once this function is complete, both the position and size of the
152 entry will be know.
153
154 Args:
Simon Glass25ac0e62018-06-01 09:38:14 -0600155 Current section position pointer
Simon Glassbf7fd502016-11-25 20:15:51 -0700156
157 Returns:
Simon Glass25ac0e62018-06-01 09:38:14 -0600158 New section position pointer (after this entry)
Simon Glassbf7fd502016-11-25 20:15:51 -0700159 """
160 if self.pos is None:
161 if self.pos_unset:
162 self.Raise('No position set with pos-unset: should another '
163 'entry provide this correct position?')
164 self.pos = tools.Align(pos, self.align)
165 needed = self.pad_before + self.contents_size + self.pad_after
166 needed = tools.Align(needed, self.align_size)
167 size = self.size
168 if not size:
169 size = needed
170 new_pos = self.pos + size
171 aligned_pos = tools.Align(new_pos, self.align_end)
172 if aligned_pos != new_pos:
173 size = aligned_pos - self.pos
174 new_pos = aligned_pos
175
176 if not self.size:
177 self.size = size
178
179 if self.size < needed:
180 self.Raise("Entry contents size is %#x (%d) but entry size is "
181 "%#x (%d)" % (needed, needed, self.size, self.size))
182 # Check that the alignment is correct. It could be wrong if the
183 # and pos or size values were provided (i.e. not calculated), but
184 # conflict with the provided alignment values
185 if self.size != tools.Align(self.size, self.align_size):
186 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
187 (self.size, self.size, self.align_size, self.align_size))
188 if self.pos != tools.Align(self.pos, self.align):
189 self.Raise("Position %#x (%d) does not match align %#x (%d)" %
190 (self.pos, self.pos, self.align, self.align))
191
192 return new_pos
193
194 def Raise(self, msg):
195 """Convenience function to raise an error referencing a node"""
196 raise ValueError("Node '%s': %s" % (self._node.path, msg))
197
198 def GetPath(self):
199 """Get the path of a node
200
201 Returns:
202 Full path of the node for this entry
203 """
204 return self._node.path
205
206 def GetData(self):
207 return self.data
208
209 def GetPositions(self):
210 return {}
211
212 def SetPositionSize(self, pos, size):
213 self.pos = pos
214 self.size = size
215
216 def ProcessContents(self):
217 pass
Simon Glass19790632017-11-13 18:55:01 -0700218
Simon Glassf55382b2018-06-01 09:38:13 -0600219 def WriteSymbols(self, section):
Simon Glass19790632017-11-13 18:55:01 -0700220 """Write symbol values into binary files for access at run time
221
222 Args:
Simon Glassf55382b2018-06-01 09:38:13 -0600223 section: Section containing the entry
Simon Glass19790632017-11-13 18:55:01 -0700224 """
225 pass
Simon Glass18546952018-06-01 09:38:16 -0600226
227 def CheckPosition(self):
228 """Check that the entry positions are correct
229
230 This is used for entries which have extra position requirements (other
231 than having to be fully inside their section). Sub-classes can implement
232 this function and raise if there is a problem.
233 """
234 pass
Simon Glass3b0c3822018-06-01 09:38:20 -0600235
236 def WriteMap(self, fd, indent):
237 """Write a map of the entry to a .map file
238
239 Args:
240 fd: File to write the map to
241 indent: Curent indent level of map (0=none, 1=one level, etc.)
242 """
243 print('%s%08x %08x %s' % (' ' * indent, self.pos, self.size,
244 self.name), file=fd)