blob: 8004918eb58f36f2c66bd6825ef9e0eb37e768fc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3#
Simon Glass2574ef62016-11-25 20:15:51 -07004# Base class for all entries
5#
6
Simon Glass30732662018-06-01 09:38:20 -06007from __future__ import print_function
8
Simon Glass2574ef62016-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 Glass691198c2018-06-01 09:38:15 -060019import os
20import sys
Simon Glass2574ef62016-11-25 20:15:51 -070021import tools
22
23modules = {}
24
Simon Glass691198c2018-06-01 09:38:15 -060025our_path = os.path.dirname(os.path.realpath(__file__))
26
Simon Glass2574ef62016-11-25 20:15:51 -070027class Entry(object):
Simon Glassad5a7712018-06-01 09:38:14 -060028 """An Entry in the section
Simon Glass2574ef62016-11-25 20:15:51 -070029
30 An entry corresponds to a single node in the device-tree description
Simon Glassad5a7712018-06-01 09:38:14 -060031 of the section. Each entry ends up being a part of the final section.
Simon Glass2574ef62016-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 Glass3a9a2b82018-07-17 13:25:28 -060039 section: Section object containing this entry
Simon Glass2574ef62016-11-25 20:15:51 -070040 node: The node that created this entry
Simon Glasse8561af2018-08-01 15:22:37 -060041 offset: Offset of entry within the section, None if not known yet (in
42 which case it will be calculated by Pack())
Simon Glass2574ef62016-11-25 20:15:51 -070043 size: Entry size in bytes, None if not known
44 contents_size: Size of contents in bytes, 0 by default
Simon Glasse8561af2018-08-01 15:22:37 -060045 align: Entry start offset alignment, or None
Simon Glass2574ef62016-11-25 20:15:51 -070046 align_size: Entry size alignment, or None
Simon Glasse8561af2018-08-01 15:22:37 -060047 align_end: Entry end offset alignment, or None
Simon Glass2574ef62016-11-25 20:15:51 -070048 pad_before: Number of pad bytes before the contents, 0 if none
49 pad_after: Number of pad bytes after the contents, 0 if none
50 data: Contents of entry (string of bytes)
51 """
Simon Glass3b78d532018-06-01 09:38:21 -060052 def __init__(self, section, etype, node, read_node=True, name_prefix=''):
Simon Glassad5a7712018-06-01 09:38:14 -060053 self.section = section
Simon Glass2574ef62016-11-25 20:15:51 -070054 self.etype = etype
55 self._node = node
Simon Glass3b78d532018-06-01 09:38:21 -060056 self.name = node and (name_prefix + node.name) or 'none'
Simon Glasse8561af2018-08-01 15:22:37 -060057 self.offset = None
Simon Glass2574ef62016-11-25 20:15:51 -070058 self.size = None
Simon Glass2e1169f2018-07-06 10:27:19 -060059 self.data = ''
Simon Glass2574ef62016-11-25 20:15:51 -070060 self.contents_size = 0
61 self.align = None
62 self.align_size = None
63 self.align_end = None
64 self.pad_before = 0
65 self.pad_after = 0
Simon Glasse8561af2018-08-01 15:22:37 -060066 self.offset_unset = False
Simon Glass9dcc8612018-08-01 15:22:42 -060067 self.image_pos = None
Simon Glass2574ef62016-11-25 20:15:51 -070068 if read_node:
69 self.ReadNode()
70
71 @staticmethod
Simon Glassad5a7712018-06-01 09:38:14 -060072 def Create(section, node, etype=None):
Simon Glass2574ef62016-11-25 20:15:51 -070073 """Create a new entry for a node.
74
75 Args:
Simon Glass3a9a2b82018-07-17 13:25:28 -060076 section: Section object containing this node
Simon Glass2574ef62016-11-25 20:15:51 -070077 node: Node object containing information about the entry to create
78 etype: Entry type to use, or None to work it out (used for tests)
79
80 Returns:
81 A new Entry object of the correct type (a subclass of Entry)
82 """
83 if not etype:
84 etype = fdt_util.GetString(node, 'type', node.name)
Simon Glasse76a3e62018-06-01 09:38:11 -060085
86 # Convert something like 'u-boot@0' to 'u_boot' since we are only
87 # interested in the type.
Simon Glass2574ef62016-11-25 20:15:51 -070088 module_name = etype.replace('-', '_')
Simon Glasse76a3e62018-06-01 09:38:11 -060089 if '@' in module_name:
90 module_name = module_name.split('@')[0]
Simon Glass2574ef62016-11-25 20:15:51 -070091 module = modules.get(module_name)
92
Simon Glass691198c2018-06-01 09:38:15 -060093 # Also allow entry-type modules to be brought in from the etype directory.
94
Simon Glass2574ef62016-11-25 20:15:51 -070095 # Import the module if we have not already done so.
96 if not module:
Simon Glass691198c2018-06-01 09:38:15 -060097 old_path = sys.path
98 sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glass2574ef62016-11-25 20:15:51 -070099 try:
100 if have_importlib:
101 module = importlib.import_module(module_name)
102 else:
103 module = __import__(module_name)
104 except ImportError:
105 raise ValueError("Unknown entry type '%s' in node '%s'" %
106 (etype, node.path))
Simon Glass691198c2018-06-01 09:38:15 -0600107 finally:
108 sys.path = old_path
Simon Glass2574ef62016-11-25 20:15:51 -0700109 modules[module_name] = module
110
111 # Call its constructor to get the object we want.
112 obj = getattr(module, 'Entry_%s' % module_name)
Simon Glassad5a7712018-06-01 09:38:14 -0600113 return obj(section, etype, node)
Simon Glass2574ef62016-11-25 20:15:51 -0700114
115 def ReadNode(self):
116 """Read entry information from the node
117
118 This reads all the fields we recognise from the node, ready for use.
119 """
Simon Glasse8561af2018-08-01 15:22:37 -0600120 self.offset = fdt_util.GetInt(self._node, 'offset')
Simon Glass2574ef62016-11-25 20:15:51 -0700121 self.size = fdt_util.GetInt(self._node, 'size')
122 self.align = fdt_util.GetInt(self._node, 'align')
123 if tools.NotPowerOfTwo(self.align):
124 raise ValueError("Node '%s': Alignment %s must be a power of two" %
125 (self._node.path, self.align))
126 self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0)
127 self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0)
128 self.align_size = fdt_util.GetInt(self._node, 'align-size')
129 if tools.NotPowerOfTwo(self.align_size):
130 raise ValueError("Node '%s': Alignment size %s must be a power "
131 "of two" % (self._node.path, self.align_size))
132 self.align_end = fdt_util.GetInt(self._node, 'align-end')
Simon Glasse8561af2018-08-01 15:22:37 -0600133 self.offset_unset = fdt_util.GetBool(self._node, 'offset-unset')
Simon Glass2574ef62016-11-25 20:15:51 -0700134
Simon Glasse22f8fa2018-07-06 10:27:41 -0600135 def AddMissingProperties(self):
136 """Add new properties to the device tree as needed for this entry"""
Simon Glass9dcc8612018-08-01 15:22:42 -0600137 for prop in ['offset', 'size', 'image-pos']:
Simon Glasse22f8fa2018-07-06 10:27:41 -0600138 if not prop in self._node.props:
139 self._node.AddZeroProp(prop)
140
141 def SetCalculatedProperties(self):
142 """Set the value of device-tree properties calculated by binman"""
Simon Glasse8561af2018-08-01 15:22:37 -0600143 self._node.SetInt('offset', self.offset)
Simon Glasse22f8fa2018-07-06 10:27:41 -0600144 self._node.SetInt('size', self.size)
Simon Glass9dcc8612018-08-01 15:22:42 -0600145 self._node.SetInt('image-pos', self.image_pos)
Simon Glasse22f8fa2018-07-06 10:27:41 -0600146
Simon Glass92307732018-07-06 10:27:40 -0600147 def ProcessFdt(self, fdt):
148 return True
149
Simon Glass3b78d532018-06-01 09:38:21 -0600150 def SetPrefix(self, prefix):
151 """Set the name prefix for a node
152
153 Args:
154 prefix: Prefix to set, or '' to not use a prefix
155 """
156 if prefix:
157 self.name = prefix + self.name
158
Simon Glass2e1169f2018-07-06 10:27:19 -0600159 def SetContents(self, data):
160 """Set the contents of an entry
161
162 This sets both the data and content_size properties
163
164 Args:
165 data: Data to set to the contents (string)
166 """
167 self.data = data
168 self.contents_size = len(self.data)
169
170 def ProcessContentsUpdate(self, data):
171 """Update the contens of an entry, after the size is fixed
172
173 This checks that the new data is the same size as the old.
174
175 Args:
176 data: Data to set to the contents (string)
177
178 Raises:
179 ValueError if the new data size is not the same as the old
180 """
181 if len(data) != self.contents_size:
182 self.Raise('Cannot update entry size from %d to %d' %
183 (len(data), self.contents_size))
184 self.SetContents(data)
185
Simon Glass2574ef62016-11-25 20:15:51 -0700186 def ObtainContents(self):
187 """Figure out the contents of an entry.
188
189 Returns:
190 True if the contents were found, False if another call is needed
191 after the other entries are processed.
192 """
193 # No contents by default: subclasses can implement this
194 return True
195
Simon Glasse8561af2018-08-01 15:22:37 -0600196 def Pack(self, offset):
Simon Glassad5a7712018-06-01 09:38:14 -0600197 """Figure out how to pack the entry into the section
Simon Glass2574ef62016-11-25 20:15:51 -0700198
199 Most of the time the entries are not fully specified. There may be
200 an alignment but no size. In that case we take the size from the
201 contents of the entry.
202
Simon Glasse8561af2018-08-01 15:22:37 -0600203 If an entry has no hard-coded offset, it will be placed at @offset.
Simon Glass2574ef62016-11-25 20:15:51 -0700204
Simon Glasse8561af2018-08-01 15:22:37 -0600205 Once this function is complete, both the offset and size of the
Simon Glass2574ef62016-11-25 20:15:51 -0700206 entry will be know.
207
208 Args:
Simon Glasse8561af2018-08-01 15:22:37 -0600209 Current section offset pointer
Simon Glass2574ef62016-11-25 20:15:51 -0700210
211 Returns:
Simon Glasse8561af2018-08-01 15:22:37 -0600212 New section offset pointer (after this entry)
Simon Glass2574ef62016-11-25 20:15:51 -0700213 """
Simon Glasse8561af2018-08-01 15:22:37 -0600214 if self.offset is None:
215 if self.offset_unset:
216 self.Raise('No offset set with offset-unset: should another '
217 'entry provide this correct offset?')
218 self.offset = tools.Align(offset, self.align)
Simon Glass2574ef62016-11-25 20:15:51 -0700219 needed = self.pad_before + self.contents_size + self.pad_after
220 needed = tools.Align(needed, self.align_size)
221 size = self.size
222 if not size:
223 size = needed
Simon Glasse8561af2018-08-01 15:22:37 -0600224 new_offset = self.offset + size
225 aligned_offset = tools.Align(new_offset, self.align_end)
226 if aligned_offset != new_offset:
227 size = aligned_offset - self.offset
228 new_offset = aligned_offset
Simon Glass2574ef62016-11-25 20:15:51 -0700229
230 if not self.size:
231 self.size = size
232
233 if self.size < needed:
234 self.Raise("Entry contents size is %#x (%d) but entry size is "
235 "%#x (%d)" % (needed, needed, self.size, self.size))
236 # Check that the alignment is correct. It could be wrong if the
Simon Glasse8561af2018-08-01 15:22:37 -0600237 # and offset or size values were provided (i.e. not calculated), but
Simon Glass2574ef62016-11-25 20:15:51 -0700238 # conflict with the provided alignment values
239 if self.size != tools.Align(self.size, self.align_size):
240 self.Raise("Size %#x (%d) does not match align-size %#x (%d)" %
241 (self.size, self.size, self.align_size, self.align_size))
Simon Glasse8561af2018-08-01 15:22:37 -0600242 if self.offset != tools.Align(self.offset, self.align):
243 self.Raise("Offset %#x (%d) does not match align %#x (%d)" %
244 (self.offset, self.offset, self.align, self.align))
Simon Glass2574ef62016-11-25 20:15:51 -0700245
Simon Glasse8561af2018-08-01 15:22:37 -0600246 return new_offset
Simon Glass2574ef62016-11-25 20:15:51 -0700247
248 def Raise(self, msg):
249 """Convenience function to raise an error referencing a node"""
250 raise ValueError("Node '%s': %s" % (self._node.path, msg))
251
252 def GetPath(self):
253 """Get the path of a node
254
255 Returns:
256 Full path of the node for this entry
257 """
258 return self._node.path
259
260 def GetData(self):
261 return self.data
262
Simon Glasse8561af2018-08-01 15:22:37 -0600263 def GetOffsets(self):
Simon Glass2574ef62016-11-25 20:15:51 -0700264 return {}
265
Simon Glasse8561af2018-08-01 15:22:37 -0600266 def SetOffsetSize(self, pos, size):
267 self.offset = pos
Simon Glass2574ef62016-11-25 20:15:51 -0700268 self.size = size
269
Simon Glass9dcc8612018-08-01 15:22:42 -0600270 def SetImagePos(self, image_pos):
271 """Set the position in the image
272
273 Args:
274 image_pos: Position of this entry in the image
275 """
276 self.image_pos = image_pos + self.offset
277
Simon Glass2574ef62016-11-25 20:15:51 -0700278 def ProcessContents(self):
279 pass
Simon Glass4ca8e042017-11-13 18:55:01 -0700280
Simon Glass8a6f56e2018-06-01 09:38:13 -0600281 def WriteSymbols(self, section):
Simon Glass4ca8e042017-11-13 18:55:01 -0700282 """Write symbol values into binary files for access at run time
283
284 Args:
Simon Glass8a6f56e2018-06-01 09:38:13 -0600285 section: Section containing the entry
Simon Glass4ca8e042017-11-13 18:55:01 -0700286 """
287 pass
Simon Glassa91e1152018-06-01 09:38:16 -0600288
Simon Glasse8561af2018-08-01 15:22:37 -0600289 def CheckOffset(self):
290 """Check that the entry offsets are correct
Simon Glassa91e1152018-06-01 09:38:16 -0600291
Simon Glasse8561af2018-08-01 15:22:37 -0600292 This is used for entries which have extra offset requirements (other
Simon Glassa91e1152018-06-01 09:38:16 -0600293 than having to be fully inside their section). Sub-classes can implement
294 this function and raise if there is a problem.
295 """
296 pass
Simon Glass30732662018-06-01 09:38:20 -0600297
Simon Glass3a9a2b82018-07-17 13:25:28 -0600298 @staticmethod
299 def WriteMapLine(fd, indent, name, offset, size):
300 print('%s%08x %08x %s' % (' ' * indent, offset, size, name), file=fd)
301
Simon Glass30732662018-06-01 09:38:20 -0600302 def WriteMap(self, fd, indent):
303 """Write a map of the entry to a .map file
304
305 Args:
306 fd: File to write the map to
307 indent: Curent indent level of map (0=none, 1=one level, etc.)
308 """
Simon Glass3a9a2b82018-07-17 13:25:28 -0600309 self.WriteMapLine(fd, indent, self.name, self.offset, self.size)