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 | |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 8 | from enum import IntEnum |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 9 | import struct |
| 10 | import sys |
| 11 | |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 12 | from dtoc import fdt_util |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 13 | import libfdt |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 14 | from libfdt import QUIET_NOTFOUND |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 15 | from patman import tools |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 16 | |
| 17 | # This deals with a device tree, presenting it as an assortment of Node and |
| 18 | # Prop objects, representing nodes and properties, respectively. This file |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 19 | # contains the base classes and defines the high-level API. You can use |
| 20 | # FdtScan() as a convenience function to create and scan an Fdt. |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 21 | |
| 22 | # This implementation uses a libfdt Python library to access the device tree, |
| 23 | # so it is fairly efficient. |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 24 | |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 25 | # A list of types we support |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 26 | class Type(IntEnum): |
Simon Glass | df82de8 | 2021-07-28 19:23:09 -0600 | [diff] [blame^] | 27 | # Types in order from widest to narrowest |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 28 | (BYTE, INT, STRING, BOOL, INT64) = range(5) |
| 29 | |
Simon Glass | df82de8 | 2021-07-28 19:23:09 -0600 | [diff] [blame^] | 30 | def needs_widening(self, other): |
| 31 | """Check if this type needs widening to hold a value from another type |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 32 | |
Simon Glass | df82de8 | 2021-07-28 19:23:09 -0600 | [diff] [blame^] | 33 | A wider type is one that can hold a wider array of information than |
| 34 | another one, or is less restrictive, so it can hold the information of |
| 35 | another type as well as its own. This is similar to the concept of |
| 36 | type-widening in C. |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 37 | |
| 38 | This uses a simple arithmetic comparison, since type values are in order |
Simon Glass | df82de8 | 2021-07-28 19:23:09 -0600 | [diff] [blame^] | 39 | from widest (BYTE) to narrowest (INT64). |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 40 | |
| 41 | Args: |
| 42 | other: Other type to compare against |
| 43 | |
| 44 | Return: |
| 45 | True if the other type is wider |
| 46 | """ |
| 47 | return self.value > other.value |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 48 | |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 49 | def CheckErr(errnum, msg): |
| 50 | if errnum: |
| 51 | raise ValueError('Error %d: %s: %s' % |
| 52 | (errnum, libfdt.fdt_strerror(errnum), msg)) |
| 53 | |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 54 | |
Simon Glass | 2b6ed5e | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 55 | def BytesToValue(data): |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 56 | """Converts a string of bytes into a type and value |
| 57 | |
| 58 | Args: |
Simon Glass | 2b6ed5e | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 59 | A bytes value (which on Python 2 is an alias for str) |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 60 | |
| 61 | Return: |
| 62 | A tuple: |
| 63 | Type of data |
| 64 | Data, either a single element or a list of elements. Each element |
| 65 | is one of: |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 66 | Type.STRING: str/bytes value from the property |
| 67 | Type.INT: a byte-swapped integer stored as a 4-byte str/bytes |
| 68 | Type.BYTE: a byte stored as a single-byte str/bytes |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 69 | """ |
Simon Glass | 2b6ed5e | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 70 | data = bytes(data) |
| 71 | size = len(data) |
| 72 | strings = data.split(b'\0') |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 73 | is_string = True |
| 74 | count = len(strings) - 1 |
Simon Glass | 2b6ed5e | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 75 | if count > 0 and not len(strings[-1]): |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 76 | for string in strings[:-1]: |
| 77 | if not string: |
| 78 | is_string = False |
| 79 | break |
| 80 | for ch in string: |
Simon Glass | 2b6ed5e | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 81 | if ch < 32 or ch > 127: |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 82 | is_string = False |
| 83 | break |
| 84 | else: |
| 85 | is_string = False |
| 86 | if is_string: |
Simon Glass | 2b6ed5e | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 87 | if count == 1: |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 88 | return Type.STRING, strings[0].decode() |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 89 | else: |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 90 | return Type.STRING, [s.decode() for s in strings[:-1]] |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 91 | if size % 4: |
| 92 | if size == 1: |
Simon Glass | 479dd30 | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 93 | return Type.BYTE, chr(data[0]) |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 94 | else: |
Simon Glass | 479dd30 | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 95 | return Type.BYTE, [chr(ch) for ch in list(data)] |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 96 | val = [] |
| 97 | for i in range(0, size, 4): |
Simon Glass | 2b6ed5e | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 98 | val.append(data[i:i + 4]) |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 99 | if size == 4: |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 100 | return Type.INT, val[0] |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 101 | else: |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 102 | return Type.INT, val |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 103 | |
| 104 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 105 | class Prop: |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 106 | """A device tree property |
| 107 | |
| 108 | Properties: |
Simon Glass | 37ba984 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 109 | node: Node containing this property |
| 110 | offset: Offset of the property (None if still to be synced) |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 111 | name: Property name (as per the device tree) |
| 112 | value: Property value as a string of bytes, or a list of strings of |
| 113 | bytes |
| 114 | type: Value type |
| 115 | """ |
Simon Glass | 928527f | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 116 | def __init__(self, node, offset, name, data): |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 117 | self._node = node |
| 118 | self._offset = offset |
| 119 | self.name = name |
| 120 | self.value = None |
Simon Glass | 928527f | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 121 | self.bytes = bytes(data) |
Simon Glass | 37ba984 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 122 | self.dirty = offset is None |
Simon Glass | 928527f | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 123 | if not data: |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 124 | self.type = Type.BOOL |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 125 | self.value = True |
| 126 | return |
Simon Glass | 928527f | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 127 | self.type, self.value = BytesToValue(bytes(data)) |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 128 | |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 129 | def RefreshOffset(self, poffset): |
| 130 | self._offset = poffset |
| 131 | |
Simon Glass | c322a85 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 132 | def Widen(self, newprop): |
| 133 | """Figure out which property type is more general |
| 134 | |
| 135 | Given a current property and a new property, this function returns the |
| 136 | one that is less specific as to type. The less specific property will |
| 137 | be ble to represent the data in the more specific property. This is |
| 138 | used for things like: |
| 139 | |
| 140 | node1 { |
| 141 | compatible = "fred"; |
| 142 | value = <1>; |
| 143 | }; |
| 144 | node1 { |
| 145 | compatible = "fred"; |
| 146 | value = <1 2>; |
| 147 | }; |
| 148 | |
| 149 | He we want to use an int array for 'value'. The first property |
| 150 | suggests that a single int is enough, but the second one shows that |
| 151 | it is not. Calling this function with these two propertes would |
| 152 | update the current property to be like the second, since it is less |
| 153 | specific. |
| 154 | """ |
Simon Glass | df82de8 | 2021-07-28 19:23:09 -0600 | [diff] [blame^] | 155 | if self.type.needs_widening(newprop.type): |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 156 | if self.type == Type.INT and newprop.type == Type.BYTE: |
Simon Glass | e144caf | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 157 | if type(self.value) == list: |
| 158 | new_value = [] |
| 159 | for val in self.value: |
Simon Glass | 479dd30 | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 160 | new_value += [chr(by) for by in val] |
Simon Glass | e144caf | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 161 | else: |
Simon Glass | 479dd30 | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 162 | new_value = [chr(by) for by in self.value] |
Simon Glass | e144caf | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 163 | self.value = new_value |
Simon Glass | c322a85 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 164 | self.type = newprop.type |
| 165 | |
| 166 | if type(newprop.value) == list and type(self.value) != list: |
| 167 | self.value = [self.value] |
| 168 | |
| 169 | if type(self.value) == list and len(newprop.value) > len(self.value): |
| 170 | val = self.GetEmpty(self.type) |
| 171 | while len(self.value) < len(newprop.value): |
| 172 | self.value.append(val) |
| 173 | |
Simon Glass | 2ba9875 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 174 | @classmethod |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 175 | def GetEmpty(self, type): |
| 176 | """Get an empty / zero value of the given type |
| 177 | |
| 178 | Returns: |
| 179 | A single value of the given type |
| 180 | """ |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 181 | if type == Type.BYTE: |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 182 | return chr(0) |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 183 | elif type == Type.INT: |
Simon Glass | af53f5a | 2018-09-14 04:57:14 -0600 | [diff] [blame] | 184 | return struct.pack('>I', 0); |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 185 | elif type == Type.STRING: |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 186 | return '' |
| 187 | else: |
| 188 | return True |
| 189 | |
Simon Glass | babdbde | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 190 | def GetOffset(self): |
| 191 | """Get the offset of a property |
| 192 | |
Simon Glass | babdbde | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 193 | Returns: |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 194 | The offset of the property (struct fdt_property) within the file |
Simon Glass | babdbde | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 195 | """ |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 196 | self._node._fdt.CheckCache() |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 197 | return self._node._fdt.GetStructOffset(self._offset) |
Simon Glass | babdbde | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 198 | |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 199 | def SetInt(self, val): |
| 200 | """Set the integer value of the property |
| 201 | |
| 202 | The device tree is marked dirty so that the value will be written to |
| 203 | the block on the next sync. |
| 204 | |
| 205 | Args: |
| 206 | val: Integer value (32-bit, single cell) |
| 207 | """ |
| 208 | self.bytes = struct.pack('>I', val); |
Simon Glass | 41b781d | 2018-10-01 12:22:49 -0600 | [diff] [blame] | 209 | self.value = self.bytes |
Simon Glass | 5ea9dcc | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 210 | self.type = Type.INT |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 211 | self.dirty = True |
| 212 | |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 213 | def SetData(self, bytes): |
| 214 | """Set the value of a property as bytes |
| 215 | |
| 216 | Args: |
| 217 | bytes: New property value to set |
| 218 | """ |
Simon Glass | f6b6481 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 219 | self.bytes = bytes |
Simon Glass | 7e6952d | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 220 | self.type, self.value = BytesToValue(bytes) |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 221 | self.dirty = True |
| 222 | |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 223 | def Sync(self, auto_resize=False): |
| 224 | """Sync property changes back to the device tree |
| 225 | |
| 226 | This updates the device tree blob with any changes to this property |
| 227 | since the last sync. |
| 228 | |
| 229 | Args: |
| 230 | auto_resize: Resize the device tree automatically if it does not |
| 231 | have enough space for the update |
| 232 | |
| 233 | Raises: |
| 234 | FdtException if auto_resize is False and there is not enough space |
| 235 | """ |
Simon Glass | 37ba984 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 236 | if self.dirty: |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 237 | node = self._node |
| 238 | fdt_obj = node._fdt._fdt_obj |
Simon Glass | 5d1bec3 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 239 | node_name = fdt_obj.get_name(node._offset) |
| 240 | if node_name and node_name != node.name: |
| 241 | raise ValueError("Internal error, node '%s' name mismatch '%s'" % |
| 242 | (node.path, node_name)) |
| 243 | |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 244 | if auto_resize: |
| 245 | while fdt_obj.setprop(node.Offset(), self.name, self.bytes, |
| 246 | (libfdt.NOSPACE,)) == -libfdt.NOSPACE: |
Simon Glass | c063917 | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 247 | fdt_obj.resize(fdt_obj.totalsize() + 1024 + |
| 248 | len(self.bytes)) |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 249 | fdt_obj.setprop(node.Offset(), self.name, self.bytes) |
| 250 | else: |
| 251 | fdt_obj.setprop(node.Offset(), self.name, self.bytes) |
Simon Glass | 37ba984 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 252 | self.dirty = False |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 253 | |
| 254 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 255 | class Node: |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 256 | """A device tree node |
| 257 | |
| 258 | Properties: |
Simon Glass | 37ba984 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 259 | parent: Parent Node |
| 260 | offset: Integer offset in the device tree (None if to be synced) |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 261 | name: Device tree node tname |
| 262 | path: Full path to node, along with the node name itself |
| 263 | _fdt: Device tree object |
| 264 | subnodes: A list of subnodes for this node, each a Node object |
| 265 | props: A dict of properties for this node, each a Prop object. |
| 266 | Keyed by property name |
| 267 | """ |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 268 | def __init__(self, fdt, parent, offset, name, path): |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 269 | self._fdt = fdt |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 270 | self.parent = parent |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 271 | self._offset = offset |
| 272 | self.name = name |
| 273 | self.path = path |
| 274 | self.subnodes = [] |
| 275 | self.props = {} |
| 276 | |
Simon Glass | 94a7c60 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 277 | def GetFdt(self): |
| 278 | """Get the Fdt object for this node |
| 279 | |
| 280 | Returns: |
| 281 | Fdt object |
| 282 | """ |
| 283 | return self._fdt |
| 284 | |
Simon Glass | 1d85888 | 2018-07-17 13:25:41 -0600 | [diff] [blame] | 285 | def FindNode(self, name): |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 286 | """Find a node given its name |
| 287 | |
| 288 | Args: |
| 289 | name: Node name to look for |
| 290 | Returns: |
| 291 | Node object if found, else None |
| 292 | """ |
| 293 | for subnode in self.subnodes: |
| 294 | if subnode.name == name: |
| 295 | return subnode |
| 296 | return None |
| 297 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 298 | def Offset(self): |
| 299 | """Returns the offset of a node, after checking the cache |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 300 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 301 | This should be used instead of self._offset directly, to ensure that |
| 302 | the cache does not contain invalid offsets. |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 303 | """ |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 304 | self._fdt.CheckCache() |
| 305 | return self._offset |
| 306 | |
| 307 | def Scan(self): |
| 308 | """Scan a node's properties and subnodes |
| 309 | |
| 310 | This fills in the props and subnodes properties, recursively |
| 311 | searching into subnodes so that the entire tree is built. |
| 312 | """ |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 313 | fdt_obj = self._fdt._fdt_obj |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 314 | self.props = self._fdt.GetProps(self) |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 315 | phandle = fdt_obj.get_phandle(self.Offset()) |
Simon Glass | 09264e0 | 2017-08-29 14:15:52 -0600 | [diff] [blame] | 316 | if phandle: |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 317 | self._fdt.phandle_to_node[phandle] = self |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 318 | |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 319 | offset = fdt_obj.first_subnode(self.Offset(), QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 320 | while offset >= 0: |
| 321 | sep = '' if self.path[-1] == '/' else '/' |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 322 | name = fdt_obj.get_name(offset) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 323 | path = self.path + sep + name |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 324 | node = Node(self._fdt, self, offset, name, path) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 325 | self.subnodes.append(node) |
| 326 | |
| 327 | node.Scan() |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 328 | offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 329 | |
| 330 | def Refresh(self, my_offset): |
| 331 | """Fix up the _offset for each node, recursively |
| 332 | |
| 333 | Note: This does not take account of property offsets - these will not |
| 334 | be updated. |
| 335 | """ |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 336 | fdt_obj = self._fdt._fdt_obj |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 337 | if self._offset != my_offset: |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 338 | self._offset = my_offset |
Simon Glass | 5d1bec3 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 339 | name = fdt_obj.get_name(self._offset) |
| 340 | if name and self.name != name: |
| 341 | raise ValueError("Internal error, node '%s' name mismatch '%s'" % |
| 342 | (self.path, name)) |
| 343 | |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 344 | offset = fdt_obj.first_subnode(self._offset, QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 345 | for subnode in self.subnodes: |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 346 | if subnode.name != fdt_obj.get_name(offset): |
| 347 | raise ValueError('Internal error, node name mismatch %s != %s' % |
| 348 | (subnode.name, fdt_obj.get_name(offset))) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 349 | subnode.Refresh(offset) |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 350 | offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND) |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 351 | if offset != -libfdt.FDT_ERR_NOTFOUND: |
| 352 | raise ValueError('Internal error, offset == %d' % offset) |
| 353 | |
| 354 | poffset = fdt_obj.first_property_offset(self._offset, QUIET_NOTFOUND) |
| 355 | while poffset >= 0: |
| 356 | p = fdt_obj.get_property_by_offset(poffset) |
| 357 | prop = self.props.get(p.name) |
| 358 | if not prop: |
Simon Glass | acd9861 | 2021-03-21 18:24:34 +1300 | [diff] [blame] | 359 | raise ValueError("Internal error, node '%s' property '%s' missing, " |
| 360 | 'offset %d' % (self.path, p.name, poffset)) |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 361 | prop.RefreshOffset(poffset) |
| 362 | poffset = fdt_obj.next_property_offset(poffset, QUIET_NOTFOUND) |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 363 | |
Simon Glass | 2a70d89 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 364 | def DeleteProp(self, prop_name): |
| 365 | """Delete a property of a node |
| 366 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 367 | The property is deleted and the offset cache is invalidated. |
Simon Glass | 2a70d89 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 368 | |
| 369 | Args: |
| 370 | prop_name: Name of the property to delete |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 371 | Raises: |
| 372 | ValueError if the property does not exist |
Simon Glass | 2a70d89 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 373 | """ |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 374 | CheckErr(self._fdt._fdt_obj.delprop(self.Offset(), prop_name), |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 375 | "Node '%s': delete property: '%s'" % (self.path, prop_name)) |
| 376 | del self.props[prop_name] |
| 377 | self._fdt.Invalidate() |
Simon Glass | 2a70d89 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 378 | |
Simon Glass | 116adec | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 379 | def AddZeroProp(self, prop_name): |
| 380 | """Add a new property to the device tree with an integer value of 0. |
| 381 | |
| 382 | Args: |
| 383 | prop_name: Name of property |
| 384 | """ |
Simon Glass | 194b8d5 | 2019-05-17 22:00:33 -0600 | [diff] [blame] | 385 | self.props[prop_name] = Prop(self, None, prop_name, |
| 386 | tools.GetBytes(0, 4)) |
Simon Glass | 116adec | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 387 | |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 388 | def AddEmptyProp(self, prop_name, len): |
| 389 | """Add a property with a fixed data size, for filling in later |
| 390 | |
| 391 | The device tree is marked dirty so that the value will be written to |
| 392 | the blob on the next sync. |
| 393 | |
| 394 | Args: |
| 395 | prop_name: Name of property |
| 396 | len: Length of data in property |
| 397 | """ |
Simon Glass | 194b8d5 | 2019-05-17 22:00:33 -0600 | [diff] [blame] | 398 | value = tools.GetBytes(0, len) |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 399 | self.props[prop_name] = Prop(self, None, prop_name, value) |
| 400 | |
Simon Glass | d9dad10 | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 401 | def _CheckProp(self, prop_name): |
| 402 | """Check if a property is present |
| 403 | |
| 404 | Args: |
| 405 | prop_name: Name of property |
| 406 | |
| 407 | Returns: |
| 408 | self |
| 409 | |
| 410 | Raises: |
| 411 | ValueError if the property is missing |
| 412 | """ |
| 413 | if prop_name not in self.props: |
| 414 | raise ValueError("Fdt '%s', node '%s': Missing property '%s'" % |
| 415 | (self._fdt._fname, self.path, prop_name)) |
| 416 | return self |
| 417 | |
Simon Glass | 116adec | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 418 | def SetInt(self, prop_name, val): |
| 419 | """Update an integer property int the device tree. |
| 420 | |
| 421 | This is not allowed to change the size of the FDT. |
| 422 | |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 423 | The device tree is marked dirty so that the value will be written to |
| 424 | the blob on the next sync. |
| 425 | |
Simon Glass | 116adec | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 426 | Args: |
| 427 | prop_name: Name of property |
| 428 | val: Value to set |
| 429 | """ |
Simon Glass | d9dad10 | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 430 | self._CheckProp(prop_name).props[prop_name].SetInt(val) |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 431 | |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 432 | def SetData(self, prop_name, val): |
| 433 | """Set the data value of a property |
| 434 | |
| 435 | The device tree is marked dirty so that the value will be written to |
| 436 | the blob on the next sync. |
| 437 | |
| 438 | Args: |
| 439 | prop_name: Name of property to set |
| 440 | val: Data value to set |
| 441 | """ |
Simon Glass | d9dad10 | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 442 | self._CheckProp(prop_name).props[prop_name].SetData(val) |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 443 | |
| 444 | def SetString(self, prop_name, val): |
| 445 | """Set the string value of a property |
| 446 | |
| 447 | The device tree is marked dirty so that the value will be written to |
| 448 | the blob on the next sync. |
| 449 | |
| 450 | Args: |
| 451 | prop_name: Name of property to set |
| 452 | val: String value to set (will be \0-terminated in DT) |
| 453 | """ |
Simon Glass | a90df2b | 2019-10-31 07:43:04 -0600 | [diff] [blame] | 454 | if type(val) == str: |
| 455 | val = val.encode('utf-8') |
Simon Glass | d9dad10 | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 456 | self._CheckProp(prop_name).props[prop_name].SetData(val + b'\0') |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 457 | |
Simon Glass | c063917 | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 458 | def AddData(self, prop_name, val): |
| 459 | """Add a new property to a node |
| 460 | |
| 461 | The device tree is marked dirty so that the value will be written to |
| 462 | the blob on the next sync. |
| 463 | |
| 464 | Args: |
| 465 | prop_name: Name of property to add |
| 466 | val: Bytes value of property |
Simon Glass | 5d1bec3 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 467 | |
| 468 | Returns: |
| 469 | Prop added |
Simon Glass | c063917 | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 470 | """ |
Simon Glass | 5d1bec3 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 471 | prop = Prop(self, None, prop_name, val) |
| 472 | self.props[prop_name] = prop |
| 473 | return prop |
Simon Glass | c063917 | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 474 | |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 475 | def AddString(self, prop_name, val): |
| 476 | """Add a new string property to a node |
| 477 | |
| 478 | The device tree is marked dirty so that the value will be written to |
| 479 | the blob on the next sync. |
| 480 | |
| 481 | Args: |
| 482 | prop_name: Name of property to add |
| 483 | val: String value of property |
Simon Glass | 5d1bec3 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 484 | |
| 485 | Returns: |
| 486 | Prop added |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 487 | """ |
Simon Glass | 9fc6ebd | 2021-01-06 21:35:10 -0700 | [diff] [blame] | 488 | val = bytes(val, 'utf-8') |
Simon Glass | 5d1bec3 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 489 | return self.AddData(prop_name, val + b'\0') |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 490 | |
Simon Glass | 6eb9932 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 491 | def AddInt(self, prop_name, val): |
| 492 | """Add a new integer property to a node |
| 493 | |
| 494 | The device tree is marked dirty so that the value will be written to |
| 495 | the blob on the next sync. |
| 496 | |
| 497 | Args: |
| 498 | prop_name: Name of property to add |
| 499 | val: Integer value of property |
Simon Glass | 5d1bec3 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 500 | |
| 501 | Returns: |
| 502 | Prop added |
Simon Glass | 6eb9932 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 503 | """ |
Simon Glass | 5d1bec3 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 504 | return self.AddData(prop_name, struct.pack('>I', val)) |
Simon Glass | 6eb9932 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 505 | |
Simon Glass | e21c27a | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 506 | def AddSubnode(self, name): |
Simon Glass | 6434961 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 507 | """Add a new subnode to the node |
| 508 | |
| 509 | Args: |
| 510 | name: name of node to add |
| 511 | |
| 512 | Returns: |
| 513 | New subnode that was created |
| 514 | """ |
Simon Glass | e21c27a | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 515 | path = self.path + '/' + name |
| 516 | subnode = Node(self._fdt, self, None, name, path) |
| 517 | self.subnodes.append(subnode) |
| 518 | return subnode |
| 519 | |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 520 | def Sync(self, auto_resize=False): |
| 521 | """Sync node changes back to the device tree |
| 522 | |
| 523 | This updates the device tree blob with any changes to this node and its |
| 524 | subnodes since the last sync. |
| 525 | |
| 526 | Args: |
| 527 | auto_resize: Resize the device tree automatically if it does not |
| 528 | have enough space for the update |
| 529 | |
Simon Glass | f617665 | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 530 | Returns: |
| 531 | True if the node had to be added, False if it already existed |
| 532 | |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 533 | Raises: |
| 534 | FdtException if auto_resize is False and there is not enough space |
| 535 | """ |
Simon Glass | f617665 | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 536 | added = False |
Simon Glass | e21c27a | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 537 | if self._offset is None: |
| 538 | # The subnode doesn't exist yet, so add it |
| 539 | fdt_obj = self._fdt._fdt_obj |
| 540 | if auto_resize: |
| 541 | while True: |
| 542 | offset = fdt_obj.add_subnode(self.parent._offset, self.name, |
| 543 | (libfdt.NOSPACE,)) |
| 544 | if offset != -libfdt.NOSPACE: |
| 545 | break |
| 546 | fdt_obj.resize(fdt_obj.totalsize() + 1024) |
| 547 | else: |
| 548 | offset = fdt_obj.add_subnode(self.parent._offset, self.name) |
| 549 | self._offset = offset |
Simon Glass | f617665 | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 550 | added = True |
Simon Glass | e21c27a | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 551 | |
Simon Glass | f617665 | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 552 | # Sync the existing subnodes first, so that we can rely on the offsets |
| 553 | # being correct. As soon as we add new subnodes, it pushes all the |
| 554 | # existing subnodes up. |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 555 | for node in reversed(self.subnodes): |
Simon Glass | f617665 | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 556 | if node._offset is not None: |
| 557 | node.Sync(auto_resize) |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 558 | |
Simon Glass | f617665 | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 559 | # Sync subnodes in reverse so that we get the expected order. Each |
| 560 | # new node goes at the start of the subnode list. This avoids an O(n^2) |
| 561 | # rescan of node offsets. |
| 562 | num_added = 0 |
| 563 | for node in reversed(self.subnodes): |
| 564 | if node.Sync(auto_resize): |
| 565 | num_added += 1 |
| 566 | if num_added: |
| 567 | # Reorder our list of nodes to put the new ones first, since that's |
| 568 | # what libfdt does |
| 569 | old_count = len(self.subnodes) - num_added |
| 570 | subnodes = self.subnodes[old_count:] + self.subnodes[:old_count] |
| 571 | self.subnodes = subnodes |
| 572 | |
| 573 | # Sync properties now, whose offsets should not have been disturbed, |
| 574 | # since properties come before subnodes. This is done after all the |
| 575 | # subnode processing above, since updating properties can disturb the |
| 576 | # offsets of those subnodes. |
| 577 | # Properties are synced in reverse order, with new properties added |
| 578 | # before existing properties are synced. This ensures that the offsets |
| 579 | # of earlier properties are not disturbed. |
| 580 | # Note that new properties will have an offset of None here, which |
| 581 | # Python cannot sort against int. So use a large value instead so that |
| 582 | # new properties are added first. |
Simon Glass | 6351805 | 2019-05-17 22:00:38 -0600 | [diff] [blame] | 583 | prop_list = sorted(self.props.values(), |
| 584 | key=lambda prop: prop._offset or 1 << 31, |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 585 | reverse=True) |
| 586 | for prop in prop_list: |
| 587 | prop.Sync(auto_resize) |
Simon Glass | f617665 | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 588 | return added |
Simon Glass | 116adec | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 589 | |
| 590 | |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 591 | class Fdt: |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 592 | """Provides simple access to a flat device tree blob using libfdts. |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 593 | |
| 594 | Properties: |
| 595 | fname: Filename of fdt |
| 596 | _root: Root of device tree (a Node object) |
Simon Glass | 880e9ee | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 597 | name: Helpful name for this Fdt for the user (useful when creating the |
| 598 | DT from data rather than a file) |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 599 | """ |
| 600 | def __init__(self, fname): |
| 601 | self._fname = fname |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 602 | self._cached_offsets = False |
Simon Glass | 09264e0 | 2017-08-29 14:15:52 -0600 | [diff] [blame] | 603 | self.phandle_to_node = {} |
Simon Glass | 880e9ee | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 604 | self.name = '' |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 605 | if self._fname: |
Simon Glass | 880e9ee | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 606 | self.name = self._fname |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 607 | self._fname = fdt_util.EnsureCompiled(self._fname) |
| 608 | |
Simon Glass | 3e4b51e | 2019-05-14 15:53:43 -0600 | [diff] [blame] | 609 | with open(self._fname, 'rb') as fd: |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 610 | self._fdt_obj = libfdt.Fdt(fd.read()) |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 611 | |
Simon Glass | 746aee3 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 612 | @staticmethod |
Simon Glass | 880e9ee | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 613 | def FromData(data, name=''): |
Simon Glass | 746aee3 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 614 | """Create a new Fdt object from the given data |
| 615 | |
| 616 | Args: |
| 617 | data: Device-tree data blob |
Simon Glass | 880e9ee | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 618 | name: Helpful name for this Fdt for the user |
Simon Glass | 746aee3 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 619 | |
| 620 | Returns: |
| 621 | Fdt object containing the data |
| 622 | """ |
| 623 | fdt = Fdt(None) |
Simon Glass | f6b6481 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 624 | fdt._fdt_obj = libfdt.Fdt(bytes(data)) |
Simon Glass | 880e9ee | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 625 | fdt.name = name |
Simon Glass | 746aee3 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 626 | return fdt |
| 627 | |
Simon Glass | 94a7c60 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 628 | def LookupPhandle(self, phandle): |
| 629 | """Look up a phandle |
| 630 | |
| 631 | Args: |
| 632 | phandle: Phandle to look up (int) |
| 633 | |
| 634 | Returns: |
| 635 | Node object the phandle points to |
| 636 | """ |
| 637 | return self.phandle_to_node.get(phandle) |
| 638 | |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 639 | def Scan(self, root='/'): |
| 640 | """Scan a device tree, building up a tree of Node objects |
| 641 | |
| 642 | This fills in the self._root property |
| 643 | |
| 644 | Args: |
| 645 | root: Ignored |
| 646 | |
| 647 | TODO(sjg@chromium.org): Implement the 'root' parameter |
| 648 | """ |
Simon Glass | f9b88b3 | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 649 | self._cached_offsets = True |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 650 | self._root = self.Node(self, None, 0, '/', '/') |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 651 | self._root.Scan() |
| 652 | |
| 653 | def GetRoot(self): |
| 654 | """Get the root Node of the device tree |
| 655 | |
| 656 | Returns: |
| 657 | The root Node object |
| 658 | """ |
| 659 | return self._root |
| 660 | |
| 661 | def GetNode(self, path): |
| 662 | """Look up a node from its path |
| 663 | |
| 664 | Args: |
| 665 | path: Path to look up, e.g. '/microcode/update@0' |
| 666 | Returns: |
| 667 | Node object, or None if not found |
| 668 | """ |
| 669 | node = self._root |
Simon Glass | b9066ff | 2018-07-06 10:27:30 -0600 | [diff] [blame] | 670 | parts = path.split('/') |
| 671 | if len(parts) < 2: |
| 672 | return None |
Simon Glass | e44bc83 | 2019-07-20 12:23:39 -0600 | [diff] [blame] | 673 | if len(parts) == 2 and parts[1] == '': |
| 674 | return node |
Simon Glass | b9066ff | 2018-07-06 10:27:30 -0600 | [diff] [blame] | 675 | for part in parts[1:]: |
Simon Glass | 1d85888 | 2018-07-17 13:25:41 -0600 | [diff] [blame] | 676 | node = node.FindNode(part) |
Simon Glass | f7a2aee | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 677 | if not node: |
| 678 | return None |
| 679 | return node |
| 680 | |
Simon Glass | da5f749 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 681 | def Flush(self): |
| 682 | """Flush device tree changes back to the file |
| 683 | |
| 684 | 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] | 685 | """ |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 686 | with open(self._fname, 'wb') as fd: |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 687 | fd.write(self._fdt_obj.as_bytearray()) |
Simon Glass | da5f749 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 688 | |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 689 | def Sync(self, auto_resize=False): |
| 690 | """Make sure any DT changes are written to the blob |
| 691 | |
| 692 | Args: |
| 693 | auto_resize: Resize the device tree automatically if it does not |
| 694 | have enough space for the update |
| 695 | |
| 696 | Raises: |
| 697 | FdtException if auto_resize is False and there is not enough space |
| 698 | """ |
Simon Glass | 71719e1 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 699 | self.CheckCache() |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 700 | self._root.Sync(auto_resize) |
Simon Glass | 71719e1 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 701 | self.Refresh() |
Simon Glass | fa80c25 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 702 | |
Simon Glass | da5f749 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 703 | def Pack(self): |
| 704 | """Pack the device tree down to its minimum size |
| 705 | |
| 706 | When nodes and properties shrink or are deleted, wasted space can |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 707 | build up in the device tree binary. |
Simon Glass | da5f749 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 708 | """ |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 709 | CheckErr(self._fdt_obj.pack(), 'pack') |
Simon Glass | 71719e1 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 710 | self.Refresh() |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 711 | |
Simon Glass | 9606624 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 712 | def GetContents(self): |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 713 | """Get the contents of the FDT |
| 714 | |
| 715 | Returns: |
| 716 | The FDT contents as a string of bytes |
| 717 | """ |
Simon Glass | f6b6481 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 718 | return bytes(self._fdt_obj.as_bytearray()) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 719 | |
Simon Glass | 2ba9875 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 720 | def GetFdtObj(self): |
| 721 | """Get the contents of the FDT |
| 722 | |
| 723 | Returns: |
| 724 | The FDT contents as a libfdt.Fdt object |
| 725 | """ |
| 726 | return self._fdt_obj |
| 727 | |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 728 | def GetProps(self, node): |
| 729 | """Get all properties from a node. |
| 730 | |
| 731 | Args: |
| 732 | node: Full path to node name to look in. |
| 733 | |
| 734 | Returns: |
| 735 | A dictionary containing all the properties, indexed by node name. |
| 736 | The entries are Prop objects. |
| 737 | |
| 738 | Raises: |
| 739 | ValueError: if the node does not exist. |
| 740 | """ |
| 741 | props_dict = {} |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 742 | poffset = self._fdt_obj.first_property_offset(node._offset, |
| 743 | QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 744 | while poffset >= 0: |
| 745 | p = self._fdt_obj.get_property_by_offset(poffset) |
Simon Glass | 3def0cf | 2018-07-06 10:27:20 -0600 | [diff] [blame] | 746 | prop = Prop(node, poffset, p.name, p) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 747 | props_dict[prop.name] = prop |
| 748 | |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 749 | poffset = self._fdt_obj.next_property_offset(poffset, |
| 750 | QUIET_NOTFOUND) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 751 | return props_dict |
| 752 | |
| 753 | def Invalidate(self): |
| 754 | """Mark our offset cache as invalid""" |
| 755 | self._cached_offsets = False |
| 756 | |
| 757 | def CheckCache(self): |
| 758 | """Refresh the offset cache if needed""" |
| 759 | if self._cached_offsets: |
| 760 | return |
| 761 | self.Refresh() |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 762 | |
| 763 | def Refresh(self): |
| 764 | """Refresh the offset cache""" |
| 765 | self._root.Refresh(0) |
Simon Glass | 71719e1 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 766 | self._cached_offsets = True |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 767 | |
| 768 | def GetStructOffset(self, offset): |
| 769 | """Get the file offset of a given struct offset |
| 770 | |
| 771 | Args: |
| 772 | offset: Offset within the 'struct' region of the device tree |
| 773 | Returns: |
| 774 | Position of @offset within the device tree binary |
| 775 | """ |
Simon Glass | 117f57b | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 776 | return self._fdt_obj.off_dt_struct() + offset |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 777 | |
| 778 | @classmethod |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 779 | def Node(self, fdt, parent, offset, name, path): |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 780 | """Create a new node |
| 781 | |
| 782 | This is used by Fdt.Scan() to create a new node using the correct |
| 783 | class. |
| 784 | |
| 785 | Args: |
| 786 | fdt: Fdt object |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 787 | parent: Parent node, or None if this is the root node |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 788 | offset: Offset of node |
| 789 | name: Node name |
| 790 | path: Full path to node |
| 791 | """ |
Simon Glass | 979ab02 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 792 | node = Node(fdt, parent, offset, name, path) |
Simon Glass | 7b75b44 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 793 | return node |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 794 | |
Simon Glass | f6e0249 | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 795 | def GetFilename(self): |
| 796 | """Get the filename of the device tree |
| 797 | |
| 798 | Returns: |
| 799 | String filename |
| 800 | """ |
| 801 | return self._fname |
| 802 | |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 803 | def FdtScan(fname): |
Simon Glass | dfe5f5b | 2018-07-06 10:27:32 -0600 | [diff] [blame] | 804 | """Returns a new Fdt object""" |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 805 | dtb = Fdt(fname) |
| 806 | dtb.Scan() |
| 807 | return dtb |