Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 3 | # |
| 4 | # Copyright (C) 2016 Google, Inc |
| 5 | # Written by Simon Glass <sjg@chromium.org> |
| 6 | # |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 7 | |
| 8 | import struct |
| 9 | import sys |
| 10 | |
| 11 | import fdt_util |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 12 | import libfdt |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 13 | from libfdt import QUIET_NOTFOUND |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 14 | |
| 15 | # This deals with a device tree, presenting it as an assortment of Node and |
| 16 | # Prop objects, representing nodes and properties, respectively. This file |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 17 | # contains the base classes and defines the high-level API. You can use |
| 18 | # FdtScan() as a convenience function to create and scan an Fdt. |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 19 | |
| 20 | # This implementation uses a libfdt Python library to access the device tree, |
| 21 | # so it is fairly efficient. |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 22 | |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 23 | # A list of types we support |
Simon Glass | fbdfd22 | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 24 | (TYPE_BYTE, TYPE_INT, TYPE_STRING, TYPE_BOOL, TYPE_INT64) = range(5) |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 25 | |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 26 | def CheckErr(errnum, msg): |
| 27 | if errnum: |
| 28 | raise ValueError('Error %d: %s: %s' % |
| 29 | (errnum, libfdt.fdt_strerror(errnum), msg)) |
| 30 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 31 | class Prop: |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 32 | """A device tree property |
| 33 | |
| 34 | Properties: |
| 35 | name: Property name (as per the device tree) |
| 36 | value: Property value as a string of bytes, or a list of strings of |
| 37 | bytes |
| 38 | type: Value type |
| 39 | """ |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 40 | def __init__(self, node, offset, name, bytes): |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 41 | self._node = node |
| 42 | self._offset = offset |
| 43 | self.name = name |
| 44 | self.value = None |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 45 | self.bytes = str(bytes) |
| 46 | if not bytes: |
| 47 | self.type = TYPE_BOOL |
| 48 | self.value = True |
| 49 | return |
| 50 | self.type, self.value = self.BytesToValue(bytes) |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 51 | |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 52 | def RefreshOffset(self, poffset): |
| 53 | self._offset = poffset |
| 54 | |
Simon Glass | c322a85 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 55 | def Widen(self, newprop): |
| 56 | """Figure out which property type is more general |
| 57 | |
| 58 | Given a current property and a new property, this function returns the |
| 59 | one that is less specific as to type. The less specific property will |
| 60 | be ble to represent the data in the more specific property. This is |
| 61 | used for things like: |
| 62 | |
| 63 | node1 { |
| 64 | compatible = "fred"; |
| 65 | value = <1>; |
| 66 | }; |
| 67 | node1 { |
| 68 | compatible = "fred"; |
| 69 | value = <1 2>; |
| 70 | }; |
| 71 | |
| 72 | He we want to use an int array for 'value'. The first property |
| 73 | suggests that a single int is enough, but the second one shows that |
| 74 | it is not. Calling this function with these two propertes would |
| 75 | update the current property to be like the second, since it is less |
| 76 | specific. |
| 77 | """ |
| 78 | if newprop.type < self.type: |
| 79 | self.type = newprop.type |
| 80 | |
| 81 | if type(newprop.value) == list and type(self.value) != list: |
| 82 | self.value = [self.value] |
| 83 | |
| 84 | if type(self.value) == list and len(newprop.value) > len(self.value): |
| 85 | val = self.GetEmpty(self.type) |
| 86 | while len(self.value) < len(newprop.value): |
| 87 | self.value.append(val) |
| 88 | |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 89 | def BytesToValue(self, bytes): |
| 90 | """Converts a string of bytes into a type and value |
| 91 | |
| 92 | Args: |
| 93 | A string containing bytes |
| 94 | |
| 95 | Return: |
| 96 | A tuple: |
| 97 | Type of data |
| 98 | Data, either a single element or a list of elements. Each element |
| 99 | is one of: |
| 100 | TYPE_STRING: string value from the property |
| 101 | TYPE_INT: a byte-swapped integer stored as a 4-byte string |
| 102 | TYPE_BYTE: a byte stored as a single-byte string |
| 103 | """ |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 104 | bytes = str(bytes) |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 105 | size = len(bytes) |
| 106 | strings = bytes.split('\0') |
| 107 | is_string = True |
| 108 | count = len(strings) - 1 |
| 109 | if count > 0 and not strings[-1]: |
| 110 | for string in strings[:-1]: |
| 111 | if not string: |
| 112 | is_string = False |
| 113 | break |
| 114 | for ch in string: |
| 115 | if ch < ' ' or ch > '~': |
| 116 | is_string = False |
| 117 | break |
| 118 | else: |
| 119 | is_string = False |
| 120 | if is_string: |
| 121 | if count == 1: |
| 122 | return TYPE_STRING, strings[0] |
| 123 | else: |
| 124 | return TYPE_STRING, strings[:-1] |
| 125 | if size % 4: |
| 126 | if size == 1: |
| 127 | return TYPE_BYTE, bytes[0] |
| 128 | else: |
| 129 | return TYPE_BYTE, list(bytes) |
| 130 | val = [] |
| 131 | for i in range(0, size, 4): |
| 132 | val.append(bytes[i:i + 4]) |
| 133 | if size == 4: |
| 134 | return TYPE_INT, val[0] |
| 135 | else: |
| 136 | return TYPE_INT, val |
| 137 | |
Simon Glass | 2ba9875 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 138 | @classmethod |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 139 | def GetEmpty(self, type): |
| 140 | """Get an empty / zero value of the given type |
| 141 | |
| 142 | Returns: |
| 143 | A single value of the given type |
| 144 | """ |
| 145 | if type == TYPE_BYTE: |
| 146 | return chr(0) |
| 147 | elif type == TYPE_INT: |
| 148 | return struct.pack('<I', 0); |
| 149 | elif type == TYPE_STRING: |
| 150 | return '' |
| 151 | else: |
| 152 | return True |
| 153 | |
Simon Glass | babdbde | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 154 | def GetOffset(self): |
| 155 | """Get the offset of a property |
| 156 | |
Simon Glass | babdbde | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 157 | Returns: |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 158 | The offset of the property (struct fdt_property) within the file |
Simon Glass | babdbde | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 159 | """ |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 160 | self._node._fdt.CheckCache() |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 161 | return self._node._fdt.GetStructOffset(self._offset) |
Simon Glass | babdbde | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 162 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 163 | class Node: |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 164 | """A device tree node |
| 165 | |
| 166 | Properties: |
| 167 | offset: Integer offset in the device tree |
| 168 | name: Device tree node tname |
| 169 | path: Full path to node, along with the node name itself |
| 170 | _fdt: Device tree object |
| 171 | subnodes: A list of subnodes for this node, each a Node object |
| 172 | props: A dict of properties for this node, each a Prop object. |
| 173 | Keyed by property name |
| 174 | """ |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 175 | def __init__(self, fdt, parent, offset, name, path): |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 176 | self._fdt = fdt |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 177 | self.parent = parent |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 178 | self._offset = offset |
| 179 | self.name = name |
| 180 | self.path = path |
| 181 | self.subnodes = [] |
| 182 | self.props = {} |
| 183 | |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 184 | def _FindNode(self, name): |
| 185 | """Find a node given its name |
| 186 | |
| 187 | Args: |
| 188 | name: Node name to look for |
| 189 | Returns: |
| 190 | Node object if found, else None |
| 191 | """ |
| 192 | for subnode in self.subnodes: |
| 193 | if subnode.name == name: |
| 194 | return subnode |
| 195 | return None |
| 196 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 197 | def Offset(self): |
| 198 | """Returns the offset of a node, after checking the cache |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 199 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 200 | This should be used instead of self._offset directly, to ensure that |
| 201 | the cache does not contain invalid offsets. |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 202 | """ |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 203 | self._fdt.CheckCache() |
| 204 | return self._offset |
| 205 | |
| 206 | def Scan(self): |
| 207 | """Scan a node's properties and subnodes |
| 208 | |
| 209 | This fills in the props and subnodes properties, recursively |
| 210 | searching into subnodes so that the entire tree is built. |
| 211 | """ |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 212 | fdt_obj = self._fdt._fdt_obj |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 213 | self.props = self._fdt.GetProps(self) |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 214 | phandle = fdt_obj.get_phandle(self.Offset()) |
Simon Glass | 09264e0 | 2017-08-29 14:15:52 -0600 | [diff] [blame] | 215 | if phandle: |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 216 | self._fdt.phandle_to_node[phandle] = self |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 217 | |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 218 | offset = fdt_obj.first_subnode(self.Offset(), QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 219 | while offset >= 0: |
| 220 | sep = '' if self.path[-1] == '/' else '/' |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 221 | name = fdt_obj.get_name(offset) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 222 | path = self.path + sep + name |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 223 | node = Node(self._fdt, self, offset, name, path) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 224 | self.subnodes.append(node) |
| 225 | |
| 226 | node.Scan() |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 227 | offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 228 | |
| 229 | def Refresh(self, my_offset): |
| 230 | """Fix up the _offset for each node, recursively |
| 231 | |
| 232 | Note: This does not take account of property offsets - these will not |
| 233 | be updated. |
| 234 | """ |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 235 | fdt_obj = self._fdt._fdt_obj |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 236 | if self._offset != my_offset: |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 237 | self._offset = my_offset |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 238 | offset = fdt_obj.first_subnode(self._offset, QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 239 | for subnode in self.subnodes: |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 240 | if subnode.name != fdt_obj.get_name(offset): |
| 241 | raise ValueError('Internal error, node name mismatch %s != %s' % |
| 242 | (subnode.name, fdt_obj.get_name(offset))) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 243 | subnode.Refresh(offset) |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 244 | offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND) |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 245 | if offset != -libfdt.FDT_ERR_NOTFOUND: |
| 246 | raise ValueError('Internal error, offset == %d' % offset) |
| 247 | |
| 248 | poffset = fdt_obj.first_property_offset(self._offset, QUIET_NOTFOUND) |
| 249 | while poffset >= 0: |
| 250 | p = fdt_obj.get_property_by_offset(poffset) |
| 251 | prop = self.props.get(p.name) |
| 252 | if not prop: |
| 253 | raise ValueError("Internal error, property '%s' missing, " |
| 254 | 'offset %d' % (p.name, poffset)) |
| 255 | prop.RefreshOffset(poffset) |
| 256 | poffset = fdt_obj.next_property_offset(poffset, QUIET_NOTFOUND) |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 257 | |
Simon Glass | 2a70d89 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 258 | def DeleteProp(self, prop_name): |
| 259 | """Delete a property of a node |
| 260 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 261 | The property is deleted and the offset cache is invalidated. |
Simon Glass | 2a70d89 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 262 | |
| 263 | Args: |
| 264 | prop_name: Name of the property to delete |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 265 | Raises: |
| 266 | ValueError if the property does not exist |
Simon Glass | 2a70d89 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 267 | """ |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 268 | CheckErr(self._fdt._fdt_obj.delprop(self.Offset(), prop_name), |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 269 | "Node '%s': delete property: '%s'" % (self.path, prop_name)) |
| 270 | del self.props[prop_name] |
| 271 | self._fdt.Invalidate() |
Simon Glass | 2a70d89 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 272 | |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 273 | class Fdt: |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 274 | """Provides simple access to a flat device tree blob using libfdts. |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 275 | |
| 276 | Properties: |
| 277 | fname: Filename of fdt |
| 278 | _root: Root of device tree (a Node object) |
| 279 | """ |
| 280 | def __init__(self, fname): |
| 281 | self._fname = fname |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 282 | self._cached_offsets = False |
Simon Glass | 09264e0 | 2017-08-29 14:15:52 -0600 | [diff] [blame] | 283 | self.phandle_to_node = {} |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 284 | if self._fname: |
| 285 | self._fname = fdt_util.EnsureCompiled(self._fname) |
| 286 | |
| 287 | with open(self._fname) as fd: |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 288 | self._fdt_obj = libfdt.Fdt(fd.read()) |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 289 | |
| 290 | def Scan(self, root='/'): |
| 291 | """Scan a device tree, building up a tree of Node objects |
| 292 | |
| 293 | This fills in the self._root property |
| 294 | |
| 295 | Args: |
| 296 | root: Ignored |
| 297 | |
| 298 | TODO(sjg@chromium.org): Implement the 'root' parameter |
| 299 | """ |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 300 | self._cached_offsets = True |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 301 | self._root = self.Node(self, None, 0, '/', '/') |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 302 | self._root.Scan() |
| 303 | |
| 304 | def GetRoot(self): |
| 305 | """Get the root Node of the device tree |
| 306 | |
| 307 | Returns: |
| 308 | The root Node object |
| 309 | """ |
| 310 | return self._root |
| 311 | |
| 312 | def GetNode(self, path): |
| 313 | """Look up a node from its path |
| 314 | |
| 315 | Args: |
| 316 | path: Path to look up, e.g. '/microcode/update@0' |
| 317 | Returns: |
| 318 | Node object, or None if not found |
| 319 | """ |
| 320 | node = self._root |
Simon Glass | b9066ff | 2018-07-06 10:27:30 -0600 | [diff] [blame] | 321 | parts = path.split('/') |
| 322 | if len(parts) < 2: |
| 323 | return None |
| 324 | for part in parts[1:]: |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 325 | node = node._FindNode(part) |
| 326 | if not node: |
| 327 | return None |
| 328 | return node |
| 329 | |
Simon Glass | da5f749 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 330 | def Flush(self): |
| 331 | """Flush device tree changes back to the file |
| 332 | |
| 333 | If the device tree has changed in memory, write it back to the file. |
Simon Glass | da5f749 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 334 | """ |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 335 | with open(self._fname, 'wb') as fd: |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 336 | fd.write(self._fdt_obj.as_bytearray()) |
Simon Glass | da5f749 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 337 | |
| 338 | def Pack(self): |
| 339 | """Pack the device tree down to its minimum size |
| 340 | |
| 341 | When nodes and properties shrink or are deleted, wasted space can |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 342 | build up in the device tree binary. |
Simon Glass | da5f749 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 343 | """ |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 344 | CheckErr(self._fdt_obj.pack(), 'pack') |
| 345 | self.Invalidate() |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 346 | |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 347 | def GetContents(self): |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 348 | """Get the contents of the FDT |
| 349 | |
| 350 | Returns: |
| 351 | The FDT contents as a string of bytes |
| 352 | """ |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 353 | return self._fdt_obj.as_bytearray() |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 354 | |
Simon Glass | 2ba9875 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 355 | def GetFdtObj(self): |
| 356 | """Get the contents of the FDT |
| 357 | |
| 358 | Returns: |
| 359 | The FDT contents as a libfdt.Fdt object |
| 360 | """ |
| 361 | return self._fdt_obj |
| 362 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 363 | def GetProps(self, node): |
| 364 | """Get all properties from a node. |
| 365 | |
| 366 | Args: |
| 367 | node: Full path to node name to look in. |
| 368 | |
| 369 | Returns: |
| 370 | A dictionary containing all the properties, indexed by node name. |
| 371 | The entries are Prop objects. |
| 372 | |
| 373 | Raises: |
| 374 | ValueError: if the node does not exist. |
| 375 | """ |
| 376 | props_dict = {} |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 377 | poffset = self._fdt_obj.first_property_offset(node._offset, |
| 378 | QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 379 | while poffset >= 0: |
| 380 | p = self._fdt_obj.get_property_by_offset(poffset) |
Simon Glass | 3def0cf | 2018-07-06 10:27:20 -0600 | [diff] [blame] | 381 | prop = Prop(node, poffset, p.name, p) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 382 | props_dict[prop.name] = prop |
| 383 | |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 384 | poffset = self._fdt_obj.next_property_offset(poffset, |
| 385 | QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 386 | return props_dict |
| 387 | |
| 388 | def Invalidate(self): |
| 389 | """Mark our offset cache as invalid""" |
| 390 | self._cached_offsets = False |
| 391 | |
| 392 | def CheckCache(self): |
| 393 | """Refresh the offset cache if needed""" |
| 394 | if self._cached_offsets: |
| 395 | return |
| 396 | self.Refresh() |
| 397 | self._cached_offsets = True |
| 398 | |
| 399 | def Refresh(self): |
| 400 | """Refresh the offset cache""" |
| 401 | self._root.Refresh(0) |
| 402 | |
| 403 | def GetStructOffset(self, offset): |
| 404 | """Get the file offset of a given struct offset |
| 405 | |
| 406 | Args: |
| 407 | offset: Offset within the 'struct' region of the device tree |
| 408 | Returns: |
| 409 | Position of @offset within the device tree binary |
| 410 | """ |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 411 | return self._fdt_obj.off_dt_struct() + offset |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 412 | |
| 413 | @classmethod |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 414 | def Node(self, fdt, parent, offset, name, path): |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 415 | """Create a new node |
| 416 | |
| 417 | This is used by Fdt.Scan() to create a new node using the correct |
| 418 | class. |
| 419 | |
| 420 | Args: |
| 421 | fdt: Fdt object |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 422 | parent: Parent node, or None if this is the root node |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 423 | offset: Offset of node |
| 424 | name: Node name |
| 425 | path: Full path to node |
| 426 | """ |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 427 | node = Node(fdt, parent, offset, name, path) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 428 | return node |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 429 | |
| 430 | def FdtScan(fname): |
Simon Glass | dfe5f5b | 2018-07-06 10:27:32 -0600 | [diff] [blame^] | 431 | """Returns a new Fdt object""" |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 432 | dtb = Fdt(fname) |
| 433 | dtb.Scan() |
| 434 | return dtb |