Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 1f941b6 | 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 | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 7 | |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 8 | from enum import IntEnum |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 9 | import struct |
| 10 | import sys |
| 11 | |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 12 | from dtoc import fdt_util |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 13 | import libfdt |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 14 | from libfdt import QUIET_NOTFOUND |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 15 | from patman import tools |
Simon Glass | 1f941b6 | 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 | a944093 | 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 | 059ae52 | 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 | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 24 | |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 25 | # A list of types we support |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 26 | class Type(IntEnum): |
Simon Glass | fd4078e | 2021-07-28 19:23:09 -0600 | [diff] [blame] | 27 | # Types in order from widest to narrowest |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 28 | (BYTE, INT, STRING, BOOL, INT64) = range(5) |
| 29 | |
Simon Glass | fd4078e | 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 | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 32 | |
Simon Glass | fd4078e | 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 | c9a032c | 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 | fd4078e | 2021-07-28 19:23:09 -0600 | [diff] [blame] | 39 | from widest (BYTE) to narrowest (INT64). |
Simon Glass | c9a032c | 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 | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 48 | |
Simon Glass | 1f941b6 | 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 | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 54 | |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 55 | def BytesToValue(data): |
Simon Glass | b73d448 | 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 | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 59 | A bytes value (which on Python 2 is an alias for str) |
Simon Glass | b73d448 | 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 | c9a032c | 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 | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 69 | """ |
Simon Glass | d8f593f | 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 | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 73 | is_string = True |
| 74 | count = len(strings) - 1 |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 75 | if count > 0 and not len(strings[-1]): |
Simon Glass | b73d448 | 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 | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 81 | if ch < 32 or ch > 127: |
Simon Glass | b73d448 | 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 | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 87 | if count == 1: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 88 | return Type.STRING, strings[0].decode() |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 89 | else: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 90 | return Type.STRING, [s.decode() for s in strings[:-1]] |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 91 | if size % 4: |
| 92 | if size == 1: |
Simon Glass | 632b84c | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 93 | return Type.BYTE, chr(data[0]) |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 94 | else: |
Simon Glass | 632b84c | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 95 | return Type.BYTE, [chr(ch) for ch in list(data)] |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 96 | val = [] |
| 97 | for i in range(0, size, 4): |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 98 | val.append(data[i:i + 4]) |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 99 | if size == 4: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 100 | return Type.INT, val[0] |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 101 | else: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 102 | return Type.INT, val |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 103 | |
| 104 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 105 | class Prop: |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 106 | """A device tree property |
| 107 | |
| 108 | Properties: |
Simon Glass | 459df47 | 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 | 1f941b6 | 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 | 74b472e | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 116 | def __init__(self, node, offset, name, data): |
Simon Glass | 1f941b6 | 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 | 74b472e | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 121 | self.bytes = bytes(data) |
Simon Glass | 459df47 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 122 | self.dirty = offset is None |
Simon Glass | 74b472e | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 123 | if not data: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 124 | self.type = Type.BOOL |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 125 | self.value = True |
| 126 | return |
Simon Glass | 74b472e | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 127 | self.type, self.value = BytesToValue(bytes(data)) |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 128 | |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 129 | def RefreshOffset(self, poffset): |
| 130 | self._offset = poffset |
| 131 | |
Simon Glass | 248ccd2 | 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 | fd4078e | 2021-07-28 19:23:09 -0600 | [diff] [blame] | 155 | if self.type.needs_widening(newprop.type): |
Simon Glass | 4311832 | 2021-07-28 19:23:11 -0600 | [diff] [blame] | 156 | |
| 157 | # A boolean has an empty value: if it exists it is True and if not |
| 158 | # it is False. So when widening we always start with an empty list |
| 159 | # since the only valid integer property would be an empty list of |
| 160 | # integers. |
| 161 | # e.g. this is a boolean: |
| 162 | # some-prop; |
| 163 | # and it would be widened to int list by: |
| 164 | # some-prop = <1 2>; |
| 165 | if self.type == Type.BOOL: |
| 166 | self.type = Type.INT |
| 167 | self.value = [self.GetEmpty(self.type)] |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 168 | if self.type == Type.INT and newprop.type == Type.BYTE: |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 169 | if type(self.value) == list: |
| 170 | new_value = [] |
| 171 | for val in self.value: |
Simon Glass | 632b84c | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 172 | new_value += [chr(by) for by in val] |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 173 | else: |
Simon Glass | 632b84c | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 174 | new_value = [chr(by) for by in self.value] |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 175 | self.value = new_value |
Simon Glass | 248ccd2 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 176 | self.type = newprop.type |
| 177 | |
Simon Glass | a7d6698 | 2021-07-28 19:23:10 -0600 | [diff] [blame] | 178 | if type(newprop.value) == list: |
| 179 | if type(self.value) != list: |
| 180 | self.value = [self.value] |
Simon Glass | 248ccd2 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 181 | |
Simon Glass | a7d6698 | 2021-07-28 19:23:10 -0600 | [diff] [blame] | 182 | if len(newprop.value) > len(self.value): |
| 183 | val = self.GetEmpty(self.type) |
| 184 | while len(self.value) < len(newprop.value): |
| 185 | self.value.append(val) |
Simon Glass | 248ccd2 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 186 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 187 | @classmethod |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 188 | def GetEmpty(self, type): |
| 189 | """Get an empty / zero value of the given type |
| 190 | |
| 191 | Returns: |
| 192 | A single value of the given type |
| 193 | """ |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 194 | if type == Type.BYTE: |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 195 | return chr(0) |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 196 | elif type == Type.INT: |
Simon Glass | c330b14 | 2018-09-14 04:57:14 -0600 | [diff] [blame] | 197 | return struct.pack('>I', 0); |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 198 | elif type == Type.STRING: |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 199 | return '' |
| 200 | else: |
| 201 | return True |
| 202 | |
Simon Glass | 54cec6d | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 203 | def GetOffset(self): |
| 204 | """Get the offset of a property |
| 205 | |
Simon Glass | 54cec6d | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 206 | Returns: |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 207 | The offset of the property (struct fdt_property) within the file |
Simon Glass | 54cec6d | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 208 | """ |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 209 | self._node._fdt.CheckCache() |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 210 | return self._node._fdt.GetStructOffset(self._offset) |
Simon Glass | 54cec6d | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 211 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 212 | def SetInt(self, val): |
| 213 | """Set the integer value of the property |
| 214 | |
| 215 | The device tree is marked dirty so that the value will be written to |
| 216 | the block on the next sync. |
| 217 | |
| 218 | Args: |
| 219 | val: Integer value (32-bit, single cell) |
| 220 | """ |
| 221 | self.bytes = struct.pack('>I', val); |
Simon Glass | a2b9acb | 2018-10-01 12:22:49 -0600 | [diff] [blame] | 222 | self.value = self.bytes |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 223 | self.type = Type.INT |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 224 | self.dirty = True |
| 225 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 226 | def SetData(self, bytes): |
| 227 | """Set the value of a property as bytes |
| 228 | |
| 229 | Args: |
| 230 | bytes: New property value to set |
| 231 | """ |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 232 | self.bytes = bytes |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 233 | self.type, self.value = BytesToValue(bytes) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 234 | self.dirty = True |
| 235 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 236 | def Sync(self, auto_resize=False): |
| 237 | """Sync property changes back to the device tree |
| 238 | |
| 239 | This updates the device tree blob with any changes to this property |
| 240 | since the last sync. |
| 241 | |
| 242 | Args: |
| 243 | auto_resize: Resize the device tree automatically if it does not |
| 244 | have enough space for the update |
| 245 | |
| 246 | Raises: |
| 247 | FdtException if auto_resize is False and there is not enough space |
| 248 | """ |
Simon Glass | 459df47 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 249 | if self.dirty: |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 250 | node = self._node |
| 251 | fdt_obj = node._fdt._fdt_obj |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 252 | node_name = fdt_obj.get_name(node._offset) |
| 253 | if node_name and node_name != node.name: |
| 254 | raise ValueError("Internal error, node '%s' name mismatch '%s'" % |
| 255 | (node.path, node_name)) |
| 256 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 257 | if auto_resize: |
| 258 | while fdt_obj.setprop(node.Offset(), self.name, self.bytes, |
| 259 | (libfdt.NOSPACE,)) == -libfdt.NOSPACE: |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 260 | fdt_obj.resize(fdt_obj.totalsize() + 1024 + |
| 261 | len(self.bytes)) |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 262 | fdt_obj.setprop(node.Offset(), self.name, self.bytes) |
| 263 | else: |
| 264 | fdt_obj.setprop(node.Offset(), self.name, self.bytes) |
Simon Glass | 459df47 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 265 | self.dirty = False |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 266 | |
| 267 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 268 | class Node: |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 269 | """A device tree node |
| 270 | |
| 271 | Properties: |
Simon Glass | 459df47 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 272 | parent: Parent Node |
| 273 | offset: Integer offset in the device tree (None if to be synced) |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 274 | name: Device tree node tname |
| 275 | path: Full path to node, along with the node name itself |
| 276 | _fdt: Device tree object |
| 277 | subnodes: A list of subnodes for this node, each a Node object |
| 278 | props: A dict of properties for this node, each a Prop object. |
| 279 | Keyed by property name |
| 280 | """ |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 281 | def __init__(self, fdt, parent, offset, name, path): |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 282 | self._fdt = fdt |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 283 | self.parent = parent |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 284 | self._offset = offset |
| 285 | self.name = name |
| 286 | self.path = path |
| 287 | self.subnodes = [] |
| 288 | self.props = {} |
| 289 | |
Simon Glass | e2d6528 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 290 | def GetFdt(self): |
| 291 | """Get the Fdt object for this node |
| 292 | |
| 293 | Returns: |
| 294 | Fdt object |
| 295 | """ |
| 296 | return self._fdt |
| 297 | |
Simon Glass | aa1a5d7 | 2018-07-17 13:25:41 -0600 | [diff] [blame] | 298 | def FindNode(self, name): |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 299 | """Find a node given its name |
| 300 | |
| 301 | Args: |
| 302 | name: Node name to look for |
| 303 | Returns: |
| 304 | Node object if found, else None |
| 305 | """ |
| 306 | for subnode in self.subnodes: |
| 307 | if subnode.name == name: |
| 308 | return subnode |
| 309 | return None |
| 310 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 311 | def Offset(self): |
| 312 | """Returns the offset of a node, after checking the cache |
| 313 | |
| 314 | This should be used instead of self._offset directly, to ensure that |
| 315 | the cache does not contain invalid offsets. |
| 316 | """ |
| 317 | self._fdt.CheckCache() |
| 318 | return self._offset |
| 319 | |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 320 | def Scan(self): |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 321 | """Scan a node's properties and subnodes |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 322 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 323 | This fills in the props and subnodes properties, recursively |
| 324 | searching into subnodes so that the entire tree is built. |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 325 | """ |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 326 | fdt_obj = self._fdt._fdt_obj |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 327 | self.props = self._fdt.GetProps(self) |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 328 | phandle = fdt_obj.get_phandle(self.Offset()) |
Simon Glass | a3f9444 | 2017-08-29 14:15:52 -0600 | [diff] [blame] | 329 | if phandle: |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 330 | self._fdt.phandle_to_node[phandle] = self |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 331 | |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 332 | offset = fdt_obj.first_subnode(self.Offset(), QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 333 | while offset >= 0: |
| 334 | sep = '' if self.path[-1] == '/' else '/' |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 335 | name = fdt_obj.get_name(offset) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 336 | path = self.path + sep + name |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 337 | node = Node(self._fdt, self, offset, name, path) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 338 | self.subnodes.append(node) |
| 339 | |
| 340 | node.Scan() |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 341 | offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 342 | |
| 343 | def Refresh(self, my_offset): |
| 344 | """Fix up the _offset for each node, recursively |
| 345 | |
| 346 | Note: This does not take account of property offsets - these will not |
| 347 | be updated. |
| 348 | """ |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 349 | fdt_obj = self._fdt._fdt_obj |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 350 | if self._offset != my_offset: |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 351 | self._offset = my_offset |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 352 | name = fdt_obj.get_name(self._offset) |
| 353 | if name and self.name != name: |
| 354 | raise ValueError("Internal error, node '%s' name mismatch '%s'" % |
| 355 | (self.path, name)) |
| 356 | |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 357 | offset = fdt_obj.first_subnode(self._offset, QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 358 | for subnode in self.subnodes: |
Simon Glass | 0023861 | 2022-02-08 11:49:52 -0700 | [diff] [blame] | 359 | if subnode._offset is None: |
| 360 | continue |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 361 | if subnode.name != fdt_obj.get_name(offset): |
| 362 | raise ValueError('Internal error, node name mismatch %s != %s' % |
| 363 | (subnode.name, fdt_obj.get_name(offset))) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 364 | subnode.Refresh(offset) |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 365 | offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND) |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 366 | if offset != -libfdt.FDT_ERR_NOTFOUND: |
| 367 | raise ValueError('Internal error, offset == %d' % offset) |
| 368 | |
| 369 | poffset = fdt_obj.first_property_offset(self._offset, QUIET_NOTFOUND) |
| 370 | while poffset >= 0: |
| 371 | p = fdt_obj.get_property_by_offset(poffset) |
| 372 | prop = self.props.get(p.name) |
| 373 | if not prop: |
Simon Glass | 93f18a1 | 2021-03-21 18:24:34 +1300 | [diff] [blame] | 374 | raise ValueError("Internal error, node '%s' property '%s' missing, " |
| 375 | 'offset %d' % (self.path, p.name, poffset)) |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 376 | prop.RefreshOffset(poffset) |
| 377 | poffset = fdt_obj.next_property_offset(poffset, QUIET_NOTFOUND) |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 378 | |
Simon Glass | c719e42 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 379 | def DeleteProp(self, prop_name): |
| 380 | """Delete a property of a node |
| 381 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 382 | The property is deleted and the offset cache is invalidated. |
Simon Glass | c719e42 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 383 | |
| 384 | Args: |
| 385 | prop_name: Name of the property to delete |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 386 | Raises: |
| 387 | ValueError if the property does not exist |
Simon Glass | c719e42 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 388 | """ |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 389 | CheckErr(self._fdt._fdt_obj.delprop(self.Offset(), prop_name), |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 390 | "Node '%s': delete property: '%s'" % (self.path, prop_name)) |
| 391 | del self.props[prop_name] |
| 392 | self._fdt.Invalidate() |
Simon Glass | c719e42 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 393 | |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 394 | def AddZeroProp(self, prop_name): |
| 395 | """Add a new property to the device tree with an integer value of 0. |
| 396 | |
| 397 | Args: |
| 398 | prop_name: Name of property |
| 399 | """ |
Simon Glass | 6bbfafb | 2019-05-17 22:00:33 -0600 | [diff] [blame] | 400 | self.props[prop_name] = Prop(self, None, prop_name, |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 401 | tools.get_bytes(0, 4)) |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 402 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 403 | def AddEmptyProp(self, prop_name, len): |
| 404 | """Add a property with a fixed data size, for filling in later |
| 405 | |
| 406 | The device tree is marked dirty so that the value will be written to |
| 407 | the blob on the next sync. |
| 408 | |
| 409 | Args: |
| 410 | prop_name: Name of property |
| 411 | len: Length of data in property |
| 412 | """ |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 413 | value = tools.get_bytes(0, len) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 414 | self.props[prop_name] = Prop(self, None, prop_name, value) |
| 415 | |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 416 | def _CheckProp(self, prop_name): |
| 417 | """Check if a property is present |
| 418 | |
| 419 | Args: |
| 420 | prop_name: Name of property |
| 421 | |
| 422 | Returns: |
| 423 | self |
| 424 | |
| 425 | Raises: |
| 426 | ValueError if the property is missing |
| 427 | """ |
| 428 | if prop_name not in self.props: |
| 429 | raise ValueError("Fdt '%s', node '%s': Missing property '%s'" % |
| 430 | (self._fdt._fname, self.path, prop_name)) |
| 431 | return self |
| 432 | |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 433 | def SetInt(self, prop_name, val): |
| 434 | """Update an integer property int the device tree. |
| 435 | |
| 436 | This is not allowed to change the size of the FDT. |
| 437 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 438 | The device tree is marked dirty so that the value will be written to |
| 439 | the blob on the next sync. |
| 440 | |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 441 | Args: |
| 442 | prop_name: Name of property |
| 443 | val: Value to set |
| 444 | """ |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 445 | self._CheckProp(prop_name).props[prop_name].SetInt(val) |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 446 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 447 | def SetData(self, prop_name, val): |
| 448 | """Set the data value of a property |
| 449 | |
| 450 | The device tree is marked dirty so that the value will be written to |
| 451 | the blob on the next sync. |
| 452 | |
| 453 | Args: |
| 454 | prop_name: Name of property to set |
| 455 | val: Data value to set |
| 456 | """ |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 457 | self._CheckProp(prop_name).props[prop_name].SetData(val) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 458 | |
| 459 | def SetString(self, prop_name, val): |
| 460 | """Set the string value of a property |
| 461 | |
| 462 | The device tree is marked dirty so that the value will be written to |
| 463 | the blob on the next sync. |
| 464 | |
| 465 | Args: |
| 466 | prop_name: Name of property to set |
| 467 | val: String value to set (will be \0-terminated in DT) |
| 468 | """ |
Simon Glass | 39e2233 | 2019-10-31 07:43:04 -0600 | [diff] [blame] | 469 | if type(val) == str: |
| 470 | val = val.encode('utf-8') |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 471 | self._CheckProp(prop_name).props[prop_name].SetData(val + b'\0') |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 472 | |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 473 | def AddData(self, prop_name, val): |
| 474 | """Add a new property to a node |
| 475 | |
| 476 | The device tree is marked dirty so that the value will be written to |
| 477 | the blob on the next sync. |
| 478 | |
| 479 | Args: |
| 480 | prop_name: Name of property to add |
| 481 | val: Bytes value of property |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 482 | |
| 483 | Returns: |
| 484 | Prop added |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 485 | """ |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 486 | prop = Prop(self, None, prop_name, val) |
| 487 | self.props[prop_name] = prop |
| 488 | return prop |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 489 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 490 | def AddString(self, prop_name, val): |
| 491 | """Add a new string property to a node |
| 492 | |
| 493 | The device tree is marked dirty so that the value will be written to |
| 494 | the blob on the next sync. |
| 495 | |
| 496 | Args: |
| 497 | prop_name: Name of property to add |
| 498 | val: String value of property |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 499 | |
| 500 | Returns: |
| 501 | Prop added |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 502 | """ |
Simon Glass | d35a852 | 2021-01-06 21:35:10 -0700 | [diff] [blame] | 503 | val = bytes(val, 'utf-8') |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 504 | return self.AddData(prop_name, val + b'\0') |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 505 | |
Simon Glass | 452be42 | 2022-02-08 11:49:50 -0700 | [diff] [blame] | 506 | def AddStringList(self, prop_name, val): |
| 507 | """Add a new string-list property to a node |
| 508 | |
| 509 | The device tree is marked dirty so that the value will be written to |
| 510 | the blob on the next sync. |
| 511 | |
| 512 | Args: |
| 513 | prop_name: Name of property to add |
| 514 | val (list of str): List of strings to add |
| 515 | |
| 516 | Returns: |
| 517 | Prop added |
| 518 | """ |
| 519 | out = b'' |
| 520 | for string in val: |
| 521 | out += bytes(string, 'utf-8') + b'\0' |
| 522 | return self.AddData(prop_name, out) |
| 523 | |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 524 | def AddInt(self, prop_name, val): |
| 525 | """Add a new integer property to a node |
| 526 | |
| 527 | The device tree is marked dirty so that the value will be written to |
| 528 | the blob on the next sync. |
| 529 | |
| 530 | Args: |
| 531 | prop_name: Name of property to add |
| 532 | val: Integer value of property |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 533 | |
| 534 | Returns: |
| 535 | Prop added |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 536 | """ |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 537 | return self.AddData(prop_name, struct.pack('>I', val)) |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 538 | |
Simon Glass | f3a1796 | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 539 | def AddSubnode(self, name): |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 540 | """Add a new subnode to the node |
| 541 | |
| 542 | Args: |
| 543 | name: name of node to add |
| 544 | |
| 545 | Returns: |
| 546 | New subnode that was created |
| 547 | """ |
Simon Glass | f3a1796 | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 548 | path = self.path + '/' + name |
| 549 | subnode = Node(self._fdt, self, None, name, path) |
| 550 | self.subnodes.append(subnode) |
| 551 | return subnode |
| 552 | |
Simon Glass | b9b5cb3 | 2022-02-08 11:49:51 -0700 | [diff] [blame] | 553 | def Delete(self): |
| 554 | """Delete a node |
| 555 | |
| 556 | The node is deleted and the offset cache is invalidated. |
| 557 | |
| 558 | Args: |
| 559 | node (Node): Node to delete |
| 560 | |
| 561 | Raises: |
| 562 | ValueError if the node does not exist |
| 563 | """ |
| 564 | CheckErr(self._fdt._fdt_obj.del_node(self.Offset()), |
| 565 | "Node '%s': delete" % self.path) |
| 566 | parent = self.parent |
| 567 | self._fdt.Invalidate() |
| 568 | parent.subnodes.remove(self) |
| 569 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 570 | def Sync(self, auto_resize=False): |
| 571 | """Sync node changes back to the device tree |
| 572 | |
| 573 | This updates the device tree blob with any changes to this node and its |
| 574 | subnodes since the last sync. |
| 575 | |
| 576 | Args: |
| 577 | auto_resize: Resize the device tree automatically if it does not |
| 578 | have enough space for the update |
| 579 | |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 580 | Returns: |
| 581 | True if the node had to be added, False if it already existed |
| 582 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 583 | Raises: |
| 584 | FdtException if auto_resize is False and there is not enough space |
| 585 | """ |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 586 | added = False |
Simon Glass | f3a1796 | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 587 | if self._offset is None: |
| 588 | # The subnode doesn't exist yet, so add it |
| 589 | fdt_obj = self._fdt._fdt_obj |
| 590 | if auto_resize: |
| 591 | while True: |
| 592 | offset = fdt_obj.add_subnode(self.parent._offset, self.name, |
| 593 | (libfdt.NOSPACE,)) |
| 594 | if offset != -libfdt.NOSPACE: |
| 595 | break |
| 596 | fdt_obj.resize(fdt_obj.totalsize() + 1024) |
| 597 | else: |
| 598 | offset = fdt_obj.add_subnode(self.parent._offset, self.name) |
| 599 | self._offset = offset |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 600 | added = True |
| 601 | |
| 602 | # Sync the existing subnodes first, so that we can rely on the offsets |
| 603 | # being correct. As soon as we add new subnodes, it pushes all the |
| 604 | # existing subnodes up. |
| 605 | for node in reversed(self.subnodes): |
| 606 | if node._offset is not None: |
| 607 | node.Sync(auto_resize) |
Simon Glass | f3a1796 | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 608 | |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 609 | # Sync subnodes in reverse so that we get the expected order. Each |
| 610 | # new node goes at the start of the subnode list. This avoids an O(n^2) |
| 611 | # rescan of node offsets. |
| 612 | num_added = 0 |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 613 | for node in reversed(self.subnodes): |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 614 | if node.Sync(auto_resize): |
| 615 | num_added += 1 |
| 616 | if num_added: |
| 617 | # Reorder our list of nodes to put the new ones first, since that's |
| 618 | # what libfdt does |
| 619 | old_count = len(self.subnodes) - num_added |
| 620 | subnodes = self.subnodes[old_count:] + self.subnodes[:old_count] |
| 621 | self.subnodes = subnodes |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 622 | |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 623 | # Sync properties now, whose offsets should not have been disturbed, |
| 624 | # since properties come before subnodes. This is done after all the |
| 625 | # subnode processing above, since updating properties can disturb the |
| 626 | # offsets of those subnodes. |
| 627 | # Properties are synced in reverse order, with new properties added |
| 628 | # before existing properties are synced. This ensures that the offsets |
| 629 | # of earlier properties are not disturbed. |
| 630 | # Note that new properties will have an offset of None here, which |
| 631 | # Python cannot sort against int. So use a large value instead so that |
| 632 | # new properties are added first. |
Simon Glass | a57cfee | 2019-05-17 22:00:38 -0600 | [diff] [blame] | 633 | prop_list = sorted(self.props.values(), |
| 634 | key=lambda prop: prop._offset or 1 << 31, |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 635 | reverse=True) |
| 636 | for prop in prop_list: |
| 637 | prop.Sync(auto_resize) |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 638 | return added |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 639 | |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 640 | |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 641 | class Fdt: |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 642 | """Provides simple access to a flat device tree blob using libfdts. |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 643 | |
| 644 | Properties: |
| 645 | fname: Filename of fdt |
| 646 | _root: Root of device tree (a Node object) |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 647 | name: Helpful name for this Fdt for the user (useful when creating the |
| 648 | DT from data rather than a file) |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 649 | """ |
| 650 | def __init__(self, fname): |
| 651 | self._fname = fname |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 652 | self._cached_offsets = False |
Simon Glass | a3f9444 | 2017-08-29 14:15:52 -0600 | [diff] [blame] | 653 | self.phandle_to_node = {} |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 654 | self.name = '' |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 655 | if self._fname: |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 656 | self.name = self._fname |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 657 | self._fname = fdt_util.EnsureCompiled(self._fname) |
| 658 | |
Simon Glass | 116236f | 2019-05-14 15:53:43 -0600 | [diff] [blame] | 659 | with open(self._fname, 'rb') as fd: |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 660 | self._fdt_obj = libfdt.Fdt(fd.read()) |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 661 | |
Simon Glass | b8a4929 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 662 | @staticmethod |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 663 | def FromData(data, name=''): |
Simon Glass | b8a4929 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 664 | """Create a new Fdt object from the given data |
| 665 | |
| 666 | Args: |
| 667 | data: Device-tree data blob |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 668 | name: Helpful name for this Fdt for the user |
Simon Glass | b8a4929 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 669 | |
| 670 | Returns: |
| 671 | Fdt object containing the data |
| 672 | """ |
| 673 | fdt = Fdt(None) |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 674 | fdt._fdt_obj = libfdt.Fdt(bytes(data)) |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 675 | fdt.name = name |
Simon Glass | b8a4929 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 676 | return fdt |
| 677 | |
Simon Glass | e2d6528 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 678 | def LookupPhandle(self, phandle): |
| 679 | """Look up a phandle |
| 680 | |
| 681 | Args: |
| 682 | phandle: Phandle to look up (int) |
| 683 | |
| 684 | Returns: |
| 685 | Node object the phandle points to |
| 686 | """ |
| 687 | return self.phandle_to_node.get(phandle) |
| 688 | |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 689 | def Scan(self, root='/'): |
| 690 | """Scan a device tree, building up a tree of Node objects |
| 691 | |
| 692 | This fills in the self._root property |
| 693 | |
| 694 | Args: |
| 695 | root: Ignored |
| 696 | |
| 697 | TODO(sjg@chromium.org): Implement the 'root' parameter |
| 698 | """ |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 699 | self._cached_offsets = True |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 700 | self._root = self.Node(self, None, 0, '/', '/') |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 701 | self._root.Scan() |
| 702 | |
| 703 | def GetRoot(self): |
| 704 | """Get the root Node of the device tree |
| 705 | |
| 706 | Returns: |
| 707 | The root Node object |
| 708 | """ |
| 709 | return self._root |
| 710 | |
| 711 | def GetNode(self, path): |
| 712 | """Look up a node from its path |
| 713 | |
| 714 | Args: |
| 715 | path: Path to look up, e.g. '/microcode/update@0' |
| 716 | Returns: |
| 717 | Node object, or None if not found |
| 718 | """ |
| 719 | node = self._root |
Simon Glass | c5eddc8 | 2018-07-06 10:27:30 -0600 | [diff] [blame] | 720 | parts = path.split('/') |
| 721 | if len(parts) < 2: |
| 722 | return None |
Simon Glass | 3b9a829 | 2019-07-20 12:23:39 -0600 | [diff] [blame] | 723 | if len(parts) == 2 and parts[1] == '': |
| 724 | return node |
Simon Glass | c5eddc8 | 2018-07-06 10:27:30 -0600 | [diff] [blame] | 725 | for part in parts[1:]: |
Simon Glass | aa1a5d7 | 2018-07-17 13:25:41 -0600 | [diff] [blame] | 726 | node = node.FindNode(part) |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 727 | if not node: |
| 728 | return None |
| 729 | return node |
| 730 | |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 731 | def Flush(self): |
| 732 | """Flush device tree changes back to the file |
| 733 | |
| 734 | If the device tree has changed in memory, write it back to the file. |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 735 | """ |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 736 | with open(self._fname, 'wb') as fd: |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 737 | fd.write(self._fdt_obj.as_bytearray()) |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 738 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 739 | def Sync(self, auto_resize=False): |
| 740 | """Make sure any DT changes are written to the blob |
| 741 | |
| 742 | Args: |
| 743 | auto_resize: Resize the device tree automatically if it does not |
| 744 | have enough space for the update |
| 745 | |
| 746 | Raises: |
| 747 | FdtException if auto_resize is False and there is not enough space |
| 748 | """ |
Simon Glass | b2e8ac4 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 749 | self.CheckCache() |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 750 | self._root.Sync(auto_resize) |
Simon Glass | b2e8ac4 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 751 | self.Refresh() |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 752 | |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 753 | def Pack(self): |
| 754 | """Pack the device tree down to its minimum size |
| 755 | |
| 756 | When nodes and properties shrink or are deleted, wasted space can |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 757 | build up in the device tree binary. |
| 758 | """ |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 759 | CheckErr(self._fdt_obj.pack(), 'pack') |
Simon Glass | b2e8ac4 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 760 | self.Refresh() |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 761 | |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 762 | def GetContents(self): |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 763 | """Get the contents of the FDT |
| 764 | |
| 765 | Returns: |
| 766 | The FDT contents as a string of bytes |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 767 | """ |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 768 | return bytes(self._fdt_obj.as_bytearray()) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 769 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 770 | def GetFdtObj(self): |
| 771 | """Get the contents of the FDT |
| 772 | |
| 773 | Returns: |
| 774 | The FDT contents as a libfdt.Fdt object |
| 775 | """ |
| 776 | return self._fdt_obj |
| 777 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 778 | def GetProps(self, node): |
| 779 | """Get all properties from a node. |
| 780 | |
| 781 | Args: |
| 782 | node: Full path to node name to look in. |
| 783 | |
| 784 | Returns: |
| 785 | A dictionary containing all the properties, indexed by node name. |
| 786 | The entries are Prop objects. |
| 787 | |
| 788 | Raises: |
| 789 | ValueError: if the node does not exist. |
| 790 | """ |
| 791 | props_dict = {} |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 792 | poffset = self._fdt_obj.first_property_offset(node._offset, |
| 793 | QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 794 | while poffset >= 0: |
| 795 | p = self._fdt_obj.get_property_by_offset(poffset) |
Simon Glass | 70cd0d7 | 2018-07-06 10:27:20 -0600 | [diff] [blame] | 796 | prop = Prop(node, poffset, p.name, p) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 797 | props_dict[prop.name] = prop |
| 798 | |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 799 | poffset = self._fdt_obj.next_property_offset(poffset, |
| 800 | QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 801 | return props_dict |
| 802 | |
| 803 | def Invalidate(self): |
| 804 | """Mark our offset cache as invalid""" |
| 805 | self._cached_offsets = False |
| 806 | |
| 807 | def CheckCache(self): |
| 808 | """Refresh the offset cache if needed""" |
| 809 | if self._cached_offsets: |
| 810 | return |
| 811 | self.Refresh() |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 812 | |
| 813 | def Refresh(self): |
| 814 | """Refresh the offset cache""" |
| 815 | self._root.Refresh(0) |
Simon Glass | b2e8ac4 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 816 | self._cached_offsets = True |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 817 | |
| 818 | def GetStructOffset(self, offset): |
| 819 | """Get the file offset of a given struct offset |
| 820 | |
| 821 | Args: |
| 822 | offset: Offset within the 'struct' region of the device tree |
| 823 | Returns: |
| 824 | Position of @offset within the device tree binary |
| 825 | """ |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 826 | return self._fdt_obj.off_dt_struct() + offset |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 827 | |
| 828 | @classmethod |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 829 | def Node(self, fdt, parent, offset, name, path): |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 830 | """Create a new node |
| 831 | |
| 832 | This is used by Fdt.Scan() to create a new node using the correct |
| 833 | class. |
| 834 | |
| 835 | Args: |
| 836 | fdt: Fdt object |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 837 | parent: Parent node, or None if this is the root node |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 838 | offset: Offset of node |
| 839 | name: Node name |
| 840 | path: Full path to node |
| 841 | """ |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 842 | node = Node(fdt, parent, offset, name, path) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 843 | return node |
Simon Glass | a944093 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 844 | |
Simon Glass | 74f5feb | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 845 | def GetFilename(self): |
| 846 | """Get the filename of the device tree |
| 847 | |
| 848 | Returns: |
| 849 | String filename |
| 850 | """ |
| 851 | return self._fname |
| 852 | |
Simon Glass | a944093 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 853 | def FdtScan(fname): |
Simon Glass | c38fee04 | 2018-07-06 10:27:32 -0600 | [diff] [blame] | 854 | """Returns a new Fdt object""" |
Simon Glass | a944093 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 855 | dtb = Fdt(fname) |
| 856 | dtb.Scan() |
| 857 | return dtb |