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 | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 15 | from u_boot_pylib import tools |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 16 | from u_boot_pylib import tout |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 17 | |
| 18 | # This deals with a device tree, presenting it as an assortment of Node and |
| 19 | # Prop objects, representing nodes and properties, respectively. This file |
Simon Glass | a944093 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 20 | # contains the base classes and defines the high-level API. You can use |
| 21 | # FdtScan() as a convenience function to create and scan an Fdt. |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 22 | |
| 23 | # This implementation uses a libfdt Python library to access the device tree, |
| 24 | # so it is fairly efficient. |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 25 | |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 26 | # A list of types we support |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 27 | class Type(IntEnum): |
Simon Glass | fd4078e | 2021-07-28 19:23:09 -0600 | [diff] [blame] | 28 | # Types in order from widest to narrowest |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 29 | (BYTE, INT, STRING, BOOL, INT64) = range(5) |
| 30 | |
Simon Glass | fd4078e | 2021-07-28 19:23:09 -0600 | [diff] [blame] | 31 | def needs_widening(self, other): |
| 32 | """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] | 33 | |
Simon Glass | fd4078e | 2021-07-28 19:23:09 -0600 | [diff] [blame] | 34 | A wider type is one that can hold a wider array of information than |
| 35 | another one, or is less restrictive, so it can hold the information of |
| 36 | another type as well as its own. This is similar to the concept of |
| 37 | type-widening in C. |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 38 | |
| 39 | This uses a simple arithmetic comparison, since type values are in order |
Simon Glass | fd4078e | 2021-07-28 19:23:09 -0600 | [diff] [blame] | 40 | from widest (BYTE) to narrowest (INT64). |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 41 | |
| 42 | Args: |
| 43 | other: Other type to compare against |
| 44 | |
| 45 | Return: |
| 46 | True if the other type is wider |
| 47 | """ |
| 48 | return self.value > other.value |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 49 | |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 50 | def CheckErr(errnum, msg): |
| 51 | if errnum: |
| 52 | raise ValueError('Error %d: %s: %s' % |
| 53 | (errnum, libfdt.fdt_strerror(errnum), msg)) |
| 54 | |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 55 | |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 56 | def BytesToValue(data): |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 57 | """Converts a string of bytes into a type and value |
| 58 | |
| 59 | Args: |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 60 | A bytes value (which on Python 2 is an alias for str) |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 61 | |
| 62 | Return: |
| 63 | A tuple: |
| 64 | Type of data |
| 65 | Data, either a single element or a list of elements. Each element |
| 66 | is one of: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 67 | Type.STRING: str/bytes value from the property |
| 68 | Type.INT: a byte-swapped integer stored as a 4-byte str/bytes |
| 69 | Type.BYTE: a byte stored as a single-byte str/bytes |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 70 | """ |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 71 | data = bytes(data) |
| 72 | size = len(data) |
| 73 | strings = data.split(b'\0') |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 74 | is_string = True |
| 75 | count = len(strings) - 1 |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 76 | if count > 0 and not len(strings[-1]): |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 77 | for string in strings[:-1]: |
| 78 | if not string: |
| 79 | is_string = False |
| 80 | break |
| 81 | for ch in string: |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 82 | if ch < 32 or ch > 127: |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 83 | is_string = False |
| 84 | break |
| 85 | else: |
| 86 | is_string = False |
| 87 | if is_string: |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 88 | if count == 1: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 89 | return Type.STRING, strings[0].decode() |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 90 | else: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 91 | return Type.STRING, [s.decode() for s in strings[:-1]] |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 92 | if size % 4: |
| 93 | if size == 1: |
Simon Glass | 632b84c | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 94 | return Type.BYTE, chr(data[0]) |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 95 | else: |
Simon Glass | 632b84c | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 96 | return Type.BYTE, [chr(ch) for ch in list(data)] |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 97 | val = [] |
| 98 | for i in range(0, size, 4): |
Simon Glass | d8f593f | 2019-05-17 22:00:35 -0600 | [diff] [blame] | 99 | val.append(data[i:i + 4]) |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 100 | if size == 4: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 101 | return Type.INT, val[0] |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 102 | else: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 103 | return Type.INT, val |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 104 | |
| 105 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 106 | class Prop: |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 107 | """A device tree property |
| 108 | |
| 109 | Properties: |
Simon Glass | 459df47 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 110 | node: Node containing this property |
| 111 | offset: Offset of the property (None if still to be synced) |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 112 | name: Property name (as per the device tree) |
| 113 | value: Property value as a string of bytes, or a list of strings of |
| 114 | bytes |
| 115 | type: Value type |
| 116 | """ |
Simon Glass | 74b472e | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 117 | def __init__(self, node, offset, name, data): |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 118 | self._node = node |
| 119 | self._offset = offset |
| 120 | self.name = name |
| 121 | self.value = None |
Simon Glass | 74b472e | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 122 | self.bytes = bytes(data) |
Simon Glass | 459df47 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 123 | self.dirty = offset is None |
Simon Glass | 74b472e | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 124 | if not data: |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 125 | self.type = Type.BOOL |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 126 | self.value = True |
| 127 | return |
Simon Glass | 74b472e | 2019-05-17 22:00:37 -0600 | [diff] [blame] | 128 | self.type, self.value = BytesToValue(bytes(data)) |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 129 | |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 130 | def RefreshOffset(self, poffset): |
| 131 | self._offset = poffset |
| 132 | |
Simon Glass | 248ccd2 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 133 | def Widen(self, newprop): |
| 134 | """Figure out which property type is more general |
| 135 | |
| 136 | Given a current property and a new property, this function returns the |
| 137 | one that is less specific as to type. The less specific property will |
| 138 | be ble to represent the data in the more specific property. This is |
| 139 | used for things like: |
| 140 | |
| 141 | node1 { |
| 142 | compatible = "fred"; |
| 143 | value = <1>; |
| 144 | }; |
| 145 | node1 { |
| 146 | compatible = "fred"; |
| 147 | value = <1 2>; |
| 148 | }; |
| 149 | |
| 150 | He we want to use an int array for 'value'. The first property |
| 151 | suggests that a single int is enough, but the second one shows that |
| 152 | it is not. Calling this function with these two propertes would |
| 153 | update the current property to be like the second, since it is less |
| 154 | specific. |
| 155 | """ |
Simon Glass | fd4078e | 2021-07-28 19:23:09 -0600 | [diff] [blame] | 156 | if self.type.needs_widening(newprop.type): |
Simon Glass | 4311832 | 2021-07-28 19:23:11 -0600 | [diff] [blame] | 157 | |
| 158 | # A boolean has an empty value: if it exists it is True and if not |
| 159 | # it is False. So when widening we always start with an empty list |
| 160 | # since the only valid integer property would be an empty list of |
| 161 | # integers. |
| 162 | # e.g. this is a boolean: |
| 163 | # some-prop; |
| 164 | # and it would be widened to int list by: |
| 165 | # some-prop = <1 2>; |
| 166 | if self.type == Type.BOOL: |
| 167 | self.type = Type.INT |
| 168 | self.value = [self.GetEmpty(self.type)] |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 169 | if self.type == Type.INT and newprop.type == Type.BYTE: |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 170 | if type(self.value) == list: |
| 171 | new_value = [] |
| 172 | for val in self.value: |
Simon Glass | 632b84c | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 173 | new_value += [chr(by) for by in val] |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 174 | else: |
Simon Glass | 632b84c | 2020-11-08 20:36:20 -0700 | [diff] [blame] | 175 | new_value = [chr(by) for by in self.value] |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 176 | self.value = new_value |
Simon Glass | 248ccd2 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 177 | self.type = newprop.type |
| 178 | |
Simon Glass | a7d6698 | 2021-07-28 19:23:10 -0600 | [diff] [blame] | 179 | if type(newprop.value) == list: |
| 180 | if type(self.value) != list: |
| 181 | self.value = [self.value] |
Simon Glass | 248ccd2 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 182 | |
Simon Glass | a7d6698 | 2021-07-28 19:23:10 -0600 | [diff] [blame] | 183 | if len(newprop.value) > len(self.value): |
| 184 | val = self.GetEmpty(self.type) |
| 185 | while len(self.value) < len(newprop.value): |
| 186 | self.value.append(val) |
Simon Glass | 248ccd2 | 2016-07-25 18:59:06 -0600 | [diff] [blame] | 187 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 188 | @classmethod |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 189 | def GetEmpty(self, type): |
| 190 | """Get an empty / zero value of the given type |
| 191 | |
| 192 | Returns: |
| 193 | A single value of the given type |
| 194 | """ |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 195 | if type == Type.BYTE: |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 196 | return chr(0) |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 197 | elif type == Type.INT: |
Simon Glass | c330b14 | 2018-09-14 04:57:14 -0600 | [diff] [blame] | 198 | return struct.pack('>I', 0); |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 199 | elif type == Type.STRING: |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 200 | return '' |
| 201 | else: |
| 202 | return True |
| 203 | |
Simon Glass | 54cec6d | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 204 | def GetOffset(self): |
| 205 | """Get the offset of a property |
| 206 | |
Simon Glass | 54cec6d | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 207 | Returns: |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 208 | The offset of the property (struct fdt_property) within the file |
Simon Glass | 54cec6d | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 209 | """ |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 210 | self._node._fdt.CheckCache() |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 211 | return self._node._fdt.GetStructOffset(self._offset) |
Simon Glass | 54cec6d | 2016-07-25 18:59:16 -0600 | [diff] [blame] | 212 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 213 | def SetInt(self, val): |
| 214 | """Set the integer value of the property |
| 215 | |
| 216 | The device tree is marked dirty so that the value will be written to |
| 217 | the block on the next sync. |
| 218 | |
| 219 | Args: |
| 220 | val: Integer value (32-bit, single cell) |
| 221 | """ |
| 222 | self.bytes = struct.pack('>I', val); |
Simon Glass | a2b9acb | 2018-10-01 12:22:49 -0600 | [diff] [blame] | 223 | self.value = self.bytes |
Simon Glass | c9a032c | 2020-11-08 20:36:17 -0700 | [diff] [blame] | 224 | self.type = Type.INT |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 225 | self.dirty = True |
| 226 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 227 | def SetData(self, bytes): |
| 228 | """Set the value of a property as bytes |
| 229 | |
| 230 | Args: |
| 231 | bytes: New property value to set |
| 232 | """ |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 233 | self.bytes = bytes |
Simon Glass | b73d448 | 2019-05-17 22:00:34 -0600 | [diff] [blame] | 234 | self.type, self.value = BytesToValue(bytes) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 235 | self.dirty = True |
| 236 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 237 | def Sync(self, auto_resize=False): |
| 238 | """Sync property changes back to the device tree |
| 239 | |
| 240 | This updates the device tree blob with any changes to this property |
| 241 | since the last sync. |
| 242 | |
| 243 | Args: |
| 244 | auto_resize: Resize the device tree automatically if it does not |
| 245 | have enough space for the update |
| 246 | |
| 247 | Raises: |
| 248 | FdtException if auto_resize is False and there is not enough space |
| 249 | """ |
Simon Glass | 459df47 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 250 | if self.dirty: |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 251 | node = self._node |
Simon Glass | 638567d | 2023-07-22 21:43:54 -0600 | [diff] [blame] | 252 | tout.debug(f'sync {node.path}: {self.name}') |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 253 | fdt_obj = node._fdt._fdt_obj |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 254 | node_name = fdt_obj.get_name(node._offset) |
| 255 | if node_name and node_name != node.name: |
| 256 | raise ValueError("Internal error, node '%s' name mismatch '%s'" % |
| 257 | (node.path, node_name)) |
| 258 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 259 | if auto_resize: |
| 260 | while fdt_obj.setprop(node.Offset(), self.name, self.bytes, |
| 261 | (libfdt.NOSPACE,)) == -libfdt.NOSPACE: |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 262 | fdt_obj.resize(fdt_obj.totalsize() + 1024 + |
| 263 | len(self.bytes)) |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 264 | fdt_obj.setprop(node.Offset(), self.name, self.bytes) |
| 265 | else: |
| 266 | fdt_obj.setprop(node.Offset(), self.name, self.bytes) |
Simon Glass | 459df47 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 267 | self.dirty = False |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 268 | |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 269 | def purge(self): |
| 270 | """Set a property offset to None |
| 271 | |
| 272 | The property remains in the tree structure and will be recreated when |
| 273 | the FDT is synced |
| 274 | """ |
| 275 | self._offset = None |
Simon Glass | 02e505e | 2023-07-22 21:43:53 -0600 | [diff] [blame] | 276 | self.dirty = True |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 277 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 278 | class Node: |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 279 | """A device tree node |
| 280 | |
| 281 | Properties: |
Simon Glass | 459df47 | 2021-03-21 18:24:35 +1300 | [diff] [blame] | 282 | parent: Parent Node |
| 283 | offset: Integer offset in the device tree (None if to be synced) |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 284 | name: Device tree node tname |
| 285 | path: Full path to node, along with the node name itself |
| 286 | _fdt: Device tree object |
| 287 | subnodes: A list of subnodes for this node, each a Node object |
| 288 | props: A dict of properties for this node, each a Prop object. |
| 289 | Keyed by property name |
| 290 | """ |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 291 | def __init__(self, fdt, parent, offset, name, path): |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 292 | self._fdt = fdt |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 293 | self.parent = parent |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 294 | self._offset = offset |
| 295 | self.name = name |
| 296 | self.path = path |
| 297 | self.subnodes = [] |
| 298 | self.props = {} |
| 299 | |
Simon Glass | e2d6528 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 300 | def GetFdt(self): |
| 301 | """Get the Fdt object for this node |
| 302 | |
| 303 | Returns: |
| 304 | Fdt object |
| 305 | """ |
| 306 | return self._fdt |
| 307 | |
Simon Glass | aa1a5d7 | 2018-07-17 13:25:41 -0600 | [diff] [blame] | 308 | def FindNode(self, name): |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 309 | """Find a node given its name |
| 310 | |
| 311 | Args: |
| 312 | name: Node name to look for |
| 313 | Returns: |
| 314 | Node object if found, else None |
| 315 | """ |
| 316 | for subnode in self.subnodes: |
| 317 | if subnode.name == name: |
| 318 | return subnode |
| 319 | return None |
| 320 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 321 | def Offset(self): |
| 322 | """Returns the offset of a node, after checking the cache |
| 323 | |
| 324 | This should be used instead of self._offset directly, to ensure that |
| 325 | the cache does not contain invalid offsets. |
| 326 | """ |
| 327 | self._fdt.CheckCache() |
| 328 | return self._offset |
| 329 | |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 330 | def Scan(self): |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 331 | """Scan a node's properties and subnodes |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 332 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 333 | This fills in the props and subnodes properties, recursively |
| 334 | searching into subnodes so that the entire tree is built. |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 335 | """ |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 336 | fdt_obj = self._fdt._fdt_obj |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 337 | self.props = self._fdt.GetProps(self) |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 338 | phandle = fdt_obj.get_phandle(self.Offset()) |
Simon Glass | a3f9444 | 2017-08-29 14:15:52 -0600 | [diff] [blame] | 339 | if phandle: |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 340 | dup = self._fdt.phandle_to_node.get(phandle) |
| 341 | if dup: |
Simon Glass | c3296d7 | 2023-08-23 19:18:02 -0600 | [diff] [blame] | 342 | raise ValueError( |
| 343 | f'Duplicate phandle {phandle} in nodes {dup.path} and {self.path}') |
| 344 | |
| 345 | self._fdt.phandle_to_node[phandle] = self |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 346 | |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 347 | offset = fdt_obj.first_subnode(self.Offset(), QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 348 | while offset >= 0: |
| 349 | sep = '' if self.path[-1] == '/' else '/' |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 350 | name = fdt_obj.get_name(offset) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 351 | path = self.path + sep + name |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 352 | node = Node(self._fdt, self, offset, name, path) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 353 | self.subnodes.append(node) |
| 354 | |
| 355 | node.Scan() |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 356 | offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 357 | |
| 358 | def Refresh(self, my_offset): |
| 359 | """Fix up the _offset for each node, recursively |
| 360 | |
| 361 | Note: This does not take account of property offsets - these will not |
| 362 | be updated. |
| 363 | """ |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 364 | fdt_obj = self._fdt._fdt_obj |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 365 | if self._offset != my_offset: |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 366 | self._offset = my_offset |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 367 | name = fdt_obj.get_name(self._offset) |
| 368 | if name and self.name != name: |
| 369 | raise ValueError("Internal error, node '%s' name mismatch '%s'" % |
| 370 | (self.path, name)) |
| 371 | |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 372 | offset = fdt_obj.first_subnode(self._offset, QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 373 | for subnode in self.subnodes: |
Simon Glass | 0023861 | 2022-02-08 11:49:52 -0700 | [diff] [blame] | 374 | if subnode._offset is None: |
| 375 | continue |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 376 | if subnode.name != fdt_obj.get_name(offset): |
| 377 | raise ValueError('Internal error, node name mismatch %s != %s' % |
| 378 | (subnode.name, fdt_obj.get_name(offset))) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 379 | subnode.Refresh(offset) |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 380 | offset = fdt_obj.next_subnode(offset, QUIET_NOTFOUND) |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 381 | if offset != -libfdt.FDT_ERR_NOTFOUND: |
| 382 | raise ValueError('Internal error, offset == %d' % offset) |
| 383 | |
| 384 | poffset = fdt_obj.first_property_offset(self._offset, QUIET_NOTFOUND) |
| 385 | while poffset >= 0: |
| 386 | p = fdt_obj.get_property_by_offset(poffset) |
| 387 | prop = self.props.get(p.name) |
| 388 | if not prop: |
Simon Glass | 93f18a1 | 2021-03-21 18:24:34 +1300 | [diff] [blame] | 389 | raise ValueError("Internal error, node '%s' property '%s' missing, " |
| 390 | 'offset %d' % (self.path, p.name, poffset)) |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 391 | prop.RefreshOffset(poffset) |
| 392 | poffset = fdt_obj.next_property_offset(poffset, QUIET_NOTFOUND) |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 393 | |
Simon Glass | c719e42 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 394 | def DeleteProp(self, prop_name): |
| 395 | """Delete a property of a node |
| 396 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 397 | The property is deleted and the offset cache is invalidated. |
Simon Glass | c719e42 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 398 | |
| 399 | Args: |
| 400 | prop_name: Name of the property to delete |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 401 | Raises: |
| 402 | ValueError if the property does not exist |
Simon Glass | c719e42 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 403 | """ |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 404 | CheckErr(self._fdt._fdt_obj.delprop(self.Offset(), prop_name), |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 405 | "Node '%s': delete property: '%s'" % (self.path, prop_name)) |
| 406 | del self.props[prop_name] |
| 407 | self._fdt.Invalidate() |
Simon Glass | c719e42 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 408 | |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 409 | def AddZeroProp(self, prop_name): |
| 410 | """Add a new property to the device tree with an integer value of 0. |
| 411 | |
| 412 | Args: |
| 413 | prop_name: Name of property |
| 414 | """ |
Simon Glass | 6bbfafb | 2019-05-17 22:00:33 -0600 | [diff] [blame] | 415 | self.props[prop_name] = Prop(self, None, prop_name, |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 416 | tools.get_bytes(0, 4)) |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 417 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 418 | def AddEmptyProp(self, prop_name, len): |
| 419 | """Add a property with a fixed data size, for filling in later |
| 420 | |
| 421 | The device tree is marked dirty so that the value will be written to |
| 422 | the blob on the next sync. |
| 423 | |
| 424 | Args: |
| 425 | prop_name: Name of property |
| 426 | len: Length of data in property |
| 427 | """ |
Simon Glass | 8002552 | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 428 | value = tools.get_bytes(0, len) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 429 | self.props[prop_name] = Prop(self, None, prop_name, value) |
| 430 | |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 431 | def _CheckProp(self, prop_name): |
| 432 | """Check if a property is present |
| 433 | |
| 434 | Args: |
| 435 | prop_name: Name of property |
| 436 | |
| 437 | Returns: |
| 438 | self |
| 439 | |
| 440 | Raises: |
| 441 | ValueError if the property is missing |
| 442 | """ |
| 443 | if prop_name not in self.props: |
| 444 | raise ValueError("Fdt '%s', node '%s': Missing property '%s'" % |
| 445 | (self._fdt._fname, self.path, prop_name)) |
| 446 | return self |
| 447 | |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 448 | def SetInt(self, prop_name, val): |
| 449 | """Update an integer property int the device tree. |
| 450 | |
| 451 | This is not allowed to change the size of the FDT. |
| 452 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 453 | The device tree is marked dirty so that the value will be written to |
| 454 | the blob on the next sync. |
| 455 | |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 456 | Args: |
| 457 | prop_name: Name of property |
| 458 | val: Value to set |
| 459 | """ |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 460 | self._CheckProp(prop_name).props[prop_name].SetInt(val) |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 461 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 462 | def SetData(self, prop_name, val): |
| 463 | """Set the data value of a property |
| 464 | |
| 465 | The device tree is marked dirty so that the value will be written to |
| 466 | the blob on the next sync. |
| 467 | |
| 468 | Args: |
| 469 | prop_name: Name of property to set |
| 470 | val: Data value to set |
| 471 | """ |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 472 | self._CheckProp(prop_name).props[prop_name].SetData(val) |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 473 | |
| 474 | def SetString(self, prop_name, val): |
| 475 | """Set the string value of a property |
| 476 | |
| 477 | The device tree is marked dirty so that the value will be written to |
| 478 | the blob on the next sync. |
| 479 | |
| 480 | Args: |
| 481 | prop_name: Name of property to set |
| 482 | val: String value to set (will be \0-terminated in DT) |
| 483 | """ |
Simon Glass | 39e2233 | 2019-10-31 07:43:04 -0600 | [diff] [blame] | 484 | if type(val) == str: |
| 485 | val = val.encode('utf-8') |
Simon Glass | a683a5f | 2019-07-20 12:23:37 -0600 | [diff] [blame] | 486 | self._CheckProp(prop_name).props[prop_name].SetData(val + b'\0') |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 487 | |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 488 | def AddData(self, prop_name, val): |
| 489 | """Add a new property to a node |
| 490 | |
| 491 | The device tree is marked dirty so that the value will be written to |
| 492 | the blob on the next sync. |
| 493 | |
| 494 | Args: |
| 495 | prop_name: Name of property to add |
| 496 | val: Bytes value of property |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 497 | |
| 498 | Returns: |
| 499 | Prop added |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 500 | """ |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 501 | prop = Prop(self, None, prop_name, val) |
| 502 | self.props[prop_name] = prop |
| 503 | return prop |
Simon Glass | f67c99c | 2020-07-09 18:39:44 -0600 | [diff] [blame] | 504 | |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 505 | def AddString(self, prop_name, val): |
| 506 | """Add a new string property to a node |
| 507 | |
| 508 | The device tree is marked dirty so that the value will be written to |
| 509 | the blob on the next sync. |
| 510 | |
| 511 | Args: |
| 512 | prop_name: Name of property to add |
| 513 | val: String value of property |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 514 | |
| 515 | Returns: |
| 516 | Prop added |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 517 | """ |
Simon Glass | d35a852 | 2021-01-06 21:35:10 -0700 | [diff] [blame] | 518 | val = bytes(val, 'utf-8') |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 519 | return self.AddData(prop_name, val + b'\0') |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 520 | |
Simon Glass | 452be42 | 2022-02-08 11:49:50 -0700 | [diff] [blame] | 521 | def AddStringList(self, prop_name, val): |
| 522 | """Add a new string-list property to a node |
| 523 | |
| 524 | The device tree is marked dirty so that the value will be written to |
| 525 | the blob on the next sync. |
| 526 | |
| 527 | Args: |
| 528 | prop_name: Name of property to add |
| 529 | val (list of str): List of strings to add |
| 530 | |
| 531 | Returns: |
| 532 | Prop added |
| 533 | """ |
Simon Glass | 120fa00 | 2022-03-05 20:18:56 -0700 | [diff] [blame] | 534 | out = b'\0'.join(bytes(s, 'utf-8') for s in val) + b'\0' if val else b'' |
Simon Glass | 452be42 | 2022-02-08 11:49:50 -0700 | [diff] [blame] | 535 | return self.AddData(prop_name, out) |
| 536 | |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 537 | def AddInt(self, prop_name, val): |
| 538 | """Add a new integer property to a node |
| 539 | |
| 540 | The device tree is marked dirty so that the value will be written to |
| 541 | the blob on the next sync. |
| 542 | |
| 543 | Args: |
| 544 | prop_name: Name of property to add |
| 545 | val: Integer value of property |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 546 | |
| 547 | Returns: |
| 548 | Prop added |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 549 | """ |
Simon Glass | d8bee46 | 2021-03-21 18:24:39 +1300 | [diff] [blame] | 550 | return self.AddData(prop_name, struct.pack('>I', val)) |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 551 | |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 552 | def Subnode(self, name): |
| 553 | """Create new subnode for the node |
Simon Glass | ccd2526 | 2018-09-14 04:57:16 -0600 | [diff] [blame] | 554 | |
| 555 | Args: |
| 556 | name: name of node to add |
| 557 | |
| 558 | Returns: |
| 559 | New subnode that was created |
| 560 | """ |
Simon Glass | f3a1796 | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 561 | path = self.path + '/' + name |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 562 | return Node(self._fdt, self, None, name, path) |
| 563 | |
| 564 | def AddSubnode(self, name): |
| 565 | """Add a new subnode to the node, after all other subnodes |
| 566 | |
| 567 | Args: |
| 568 | name: name of node to add |
| 569 | |
| 570 | Returns: |
| 571 | New subnode that was created |
| 572 | """ |
| 573 | subnode = self.Subnode(name) |
Simon Glass | f3a1796 | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 574 | self.subnodes.append(subnode) |
| 575 | return subnode |
| 576 | |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 577 | def insert_subnode(self, name): |
| 578 | """Add a new subnode to the node, before all other subnodes |
| 579 | |
| 580 | This deletes other subnodes and sets their offset to None, so that they |
| 581 | will be recreated after this one. |
| 582 | |
| 583 | Args: |
| 584 | name: name of node to add |
| 585 | |
| 586 | Returns: |
| 587 | New subnode that was created |
| 588 | """ |
| 589 | # Deleting a node invalidates the offsets of all following nodes, so |
| 590 | # process in reverse order so that the offset of each node remains valid |
| 591 | # until deletion. |
| 592 | for subnode in reversed(self.subnodes): |
| 593 | subnode.purge(True) |
| 594 | subnode = self.Subnode(name) |
| 595 | self.subnodes.insert(0, subnode) |
| 596 | return subnode |
| 597 | |
| 598 | def purge(self, delete_it=False): |
| 599 | """Purge this node, setting offset to None and deleting from FDT""" |
| 600 | if self._offset is not None: |
| 601 | if delete_it: |
| 602 | CheckErr(self._fdt._fdt_obj.del_node(self.Offset()), |
| 603 | "Node '%s': delete" % self.path) |
| 604 | self._offset = None |
| 605 | self._fdt.Invalidate() |
| 606 | |
| 607 | for prop in self.props.values(): |
| 608 | prop.purge() |
| 609 | |
| 610 | for subnode in self.subnodes: |
| 611 | subnode.purge(False) |
| 612 | |
| 613 | def move_to_first(self): |
| 614 | """Move the current node to first in its parent's node list""" |
| 615 | parent = self.parent |
| 616 | if parent.subnodes and parent.subnodes[0] == self: |
| 617 | return |
| 618 | for subnode in reversed(parent.subnodes): |
| 619 | subnode.purge(True) |
| 620 | |
| 621 | new_subnodes = [self] |
| 622 | for subnode in parent.subnodes: |
| 623 | #subnode.purge(False) |
| 624 | if subnode != self: |
| 625 | new_subnodes.append(subnode) |
| 626 | parent.subnodes = new_subnodes |
| 627 | |
Simon Glass | b9b5cb3 | 2022-02-08 11:49:51 -0700 | [diff] [blame] | 628 | def Delete(self): |
| 629 | """Delete a node |
| 630 | |
| 631 | The node is deleted and the offset cache is invalidated. |
| 632 | |
| 633 | Args: |
| 634 | node (Node): Node to delete |
| 635 | |
| 636 | Raises: |
| 637 | ValueError if the node does not exist |
| 638 | """ |
| 639 | CheckErr(self._fdt._fdt_obj.del_node(self.Offset()), |
| 640 | "Node '%s': delete" % self.path) |
| 641 | parent = self.parent |
| 642 | self._fdt.Invalidate() |
| 643 | parent.subnodes.remove(self) |
| 644 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 645 | def Sync(self, auto_resize=False): |
| 646 | """Sync node changes back to the device tree |
| 647 | |
| 648 | This updates the device tree blob with any changes to this node and its |
| 649 | subnodes since the last sync. |
| 650 | |
| 651 | Args: |
| 652 | auto_resize: Resize the device tree automatically if it does not |
| 653 | have enough space for the update |
| 654 | |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 655 | Returns: |
| 656 | True if the node had to be added, False if it already existed |
| 657 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 658 | Raises: |
| 659 | FdtException if auto_resize is False and there is not enough space |
| 660 | """ |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 661 | added = False |
Simon Glass | f3a1796 | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 662 | if self._offset is None: |
| 663 | # The subnode doesn't exist yet, so add it |
| 664 | fdt_obj = self._fdt._fdt_obj |
| 665 | if auto_resize: |
| 666 | while True: |
| 667 | offset = fdt_obj.add_subnode(self.parent._offset, self.name, |
| 668 | (libfdt.NOSPACE,)) |
| 669 | if offset != -libfdt.NOSPACE: |
| 670 | break |
| 671 | fdt_obj.resize(fdt_obj.totalsize() + 1024) |
| 672 | else: |
| 673 | offset = fdt_obj.add_subnode(self.parent._offset, self.name) |
| 674 | self._offset = offset |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 675 | added = True |
| 676 | |
| 677 | # Sync the existing subnodes first, so that we can rely on the offsets |
| 678 | # being correct. As soon as we add new subnodes, it pushes all the |
| 679 | # existing subnodes up. |
| 680 | for node in reversed(self.subnodes): |
| 681 | if node._offset is not None: |
| 682 | node.Sync(auto_resize) |
Simon Glass | f3a1796 | 2018-09-14 04:57:15 -0600 | [diff] [blame] | 683 | |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 684 | # Sync subnodes in reverse so that we get the expected order. Each |
| 685 | # new node goes at the start of the subnode list. This avoids an O(n^2) |
| 686 | # rescan of node offsets. |
| 687 | num_added = 0 |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 688 | for node in reversed(self.subnodes): |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 689 | if node.Sync(auto_resize): |
| 690 | num_added += 1 |
| 691 | if num_added: |
| 692 | # Reorder our list of nodes to put the new ones first, since that's |
| 693 | # what libfdt does |
| 694 | old_count = len(self.subnodes) - num_added |
| 695 | subnodes = self.subnodes[old_count:] + self.subnodes[:old_count] |
| 696 | self.subnodes = subnodes |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 697 | |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 698 | # Sync properties now, whose offsets should not have been disturbed, |
| 699 | # since properties come before subnodes. This is done after all the |
| 700 | # subnode processing above, since updating properties can disturb the |
| 701 | # offsets of those subnodes. |
| 702 | # Properties are synced in reverse order, with new properties added |
| 703 | # before existing properties are synced. This ensures that the offsets |
| 704 | # of earlier properties are not disturbed. |
| 705 | # Note that new properties will have an offset of None here, which |
| 706 | # Python cannot sort against int. So use a large value instead so that |
| 707 | # new properties are added first. |
Simon Glass | a57cfee | 2019-05-17 22:00:38 -0600 | [diff] [blame] | 708 | prop_list = sorted(self.props.values(), |
| 709 | key=lambda prop: prop._offset or 1 << 31, |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 710 | reverse=True) |
| 711 | for prop in prop_list: |
| 712 | prop.Sync(auto_resize) |
Simon Glass | 3be798a | 2021-03-21 18:24:38 +1300 | [diff] [blame] | 713 | return added |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 714 | |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 715 | def merge_props(self, src, copy_phandles): |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 716 | """Copy missing properties (except 'phandle') from another node |
| 717 | |
| 718 | Args: |
| 719 | src (Node): Node containing properties to copy |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 720 | copy_phandles (bool): True to copy phandle properties in nodes |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 721 | |
| 722 | Adds properties which are present in src but not in this node. Any |
| 723 | 'phandle' property is not copied since this might result in two nodes |
| 724 | with the same phandle, thus making phandle references ambiguous. |
| 725 | """ |
Simon Glass | 638567d | 2023-07-22 21:43:54 -0600 | [diff] [blame] | 726 | tout.debug(f'copy to {self.path}: {src.path}') |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 727 | for name, src_prop in src.props.items(): |
Simon Glass | 638567d | 2023-07-22 21:43:54 -0600 | [diff] [blame] | 728 | done = False |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 729 | if name not in self.props: |
| 730 | if copy_phandles or name != 'phandle': |
| 731 | self.props[name] = Prop(self, None, name, src_prop.bytes) |
| 732 | done = True |
Simon Glass | 638567d | 2023-07-22 21:43:54 -0600 | [diff] [blame] | 733 | tout.debug(f" {name}{'' if done else ' - ignored'}") |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 734 | |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 735 | def copy_node(self, src, copy_phandles=False): |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 736 | """Copy a node and all its subnodes into this node |
| 737 | |
| 738 | Args: |
| 739 | src (Node): Node to copy |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 740 | copy_phandles (bool): True to copy phandle properties in nodes |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 741 | |
| 742 | Returns: |
| 743 | Node: Resulting destination node |
| 744 | |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 745 | This works recursively, with copy_phandles being set to True for the |
| 746 | recursive calls |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 747 | |
| 748 | The new node is put before all other nodes. If the node already |
| 749 | exists, just its subnodes and properties are copied, placing them before |
| 750 | any existing subnodes. Properties which exist in the destination node |
| 751 | already are not copied. |
| 752 | """ |
| 753 | dst = self.FindNode(src.name) |
| 754 | if dst: |
| 755 | dst.move_to_first() |
| 756 | else: |
| 757 | dst = self.insert_subnode(src.name) |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 758 | dst.merge_props(src, copy_phandles) |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 759 | |
| 760 | # Process in reverse order so that they appear correctly in the result, |
| 761 | # since copy_node() puts the node first in the list |
| 762 | for node in reversed(src.subnodes): |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 763 | dst.copy_node(node, True) |
Simon Glass | adf8e05 | 2023-07-18 07:24:02 -0600 | [diff] [blame] | 764 | return dst |
| 765 | |
Simon Glass | cd97119 | 2023-07-18 07:24:03 -0600 | [diff] [blame] | 766 | def copy_subnodes_from_phandles(self, phandle_list): |
| 767 | """Copy subnodes of a list of nodes into another node |
| 768 | |
| 769 | Args: |
| 770 | phandle_list (list of int): List of phandles of nodes to copy |
| 771 | |
| 772 | For each node in the phandle list, its subnodes and their properties are |
| 773 | copied recursively. Note that it does not copy the node itself, nor its |
| 774 | properties. |
| 775 | """ |
| 776 | # Process in reverse order, since new nodes are inserted at the start of |
| 777 | # the destination's node list. We want them to appear in order of the |
| 778 | # phandle list |
| 779 | for phandle in phandle_list.__reversed__(): |
| 780 | parent = self.GetFdt().LookupPhandle(phandle) |
| 781 | tout.debug(f'adding template {parent.path} to node {self.path}') |
| 782 | for node in parent.subnodes.__reversed__(): |
| 783 | dst = self.copy_node(node) |
| 784 | |
Manorit Chawdhry | b63e9b0 | 2023-12-29 16:16:25 +0530 | [diff] [blame] | 785 | tout.debug(f'merge props from {parent.path} to {self.path}') |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 786 | self.merge_props(parent, False) |
Simon Glass | cd97119 | 2023-07-18 07:24:03 -0600 | [diff] [blame] | 787 | |
Simon Glass | e80c556 | 2018-07-06 10:27:38 -0600 | [diff] [blame] | 788 | |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 789 | class Fdt: |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 790 | """Provides simple access to a flat device tree blob using libfdts. |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 791 | |
| 792 | Properties: |
| 793 | fname: Filename of fdt |
| 794 | _root: Root of device tree (a Node object) |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 795 | name: Helpful name for this Fdt for the user (useful when creating the |
| 796 | DT from data rather than a file) |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 797 | """ |
| 798 | def __init__(self, fname): |
| 799 | self._fname = fname |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 800 | self._cached_offsets = False |
Simon Glass | a3f9444 | 2017-08-29 14:15:52 -0600 | [diff] [blame] | 801 | self.phandle_to_node = {} |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 802 | self.name = '' |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 803 | if self._fname: |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 804 | self.name = self._fname |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 805 | self._fname = fdt_util.EnsureCompiled(self._fname) |
| 806 | |
Simon Glass | 116236f | 2019-05-14 15:53:43 -0600 | [diff] [blame] | 807 | with open(self._fname, 'rb') as fd: |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 808 | self._fdt_obj = libfdt.Fdt(fd.read()) |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 809 | |
Simon Glass | b8a4929 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 810 | @staticmethod |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 811 | def FromData(data, name=''): |
Simon Glass | b8a4929 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 812 | """Create a new Fdt object from the given data |
| 813 | |
| 814 | Args: |
| 815 | data: Device-tree data blob |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 816 | name: Helpful name for this Fdt for the user |
Simon Glass | b8a4929 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 817 | |
| 818 | Returns: |
| 819 | Fdt object containing the data |
| 820 | """ |
| 821 | fdt = Fdt(None) |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 822 | fdt._fdt_obj = libfdt.Fdt(bytes(data)) |
Simon Glass | 50cfc6e | 2019-07-20 12:23:38 -0600 | [diff] [blame] | 823 | fdt.name = name |
Simon Glass | b8a4929 | 2018-09-14 04:57:17 -0600 | [diff] [blame] | 824 | return fdt |
| 825 | |
Simon Glass | e2d6528 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 826 | def LookupPhandle(self, phandle): |
| 827 | """Look up a phandle |
| 828 | |
| 829 | Args: |
| 830 | phandle: Phandle to look up (int) |
| 831 | |
| 832 | Returns: |
| 833 | Node object the phandle points to |
| 834 | """ |
| 835 | return self.phandle_to_node.get(phandle) |
| 836 | |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 837 | def Scan(self, root='/'): |
| 838 | """Scan a device tree, building up a tree of Node objects |
| 839 | |
| 840 | This fills in the self._root property |
| 841 | |
| 842 | Args: |
| 843 | root: Ignored |
| 844 | |
| 845 | TODO(sjg@chromium.org): Implement the 'root' parameter |
| 846 | """ |
Simon Glass | 48ec1ff | 2023-07-22 21:43:55 -0600 | [diff] [blame] | 847 | self.phandle_to_node = {} |
Simon Glass | 4df8a0c | 2018-07-06 10:27:29 -0600 | [diff] [blame] | 848 | self._cached_offsets = True |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 849 | self._root = self.Node(self, None, 0, '/', '/') |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 850 | self._root.Scan() |
| 851 | |
| 852 | def GetRoot(self): |
| 853 | """Get the root Node of the device tree |
| 854 | |
| 855 | Returns: |
| 856 | The root Node object |
| 857 | """ |
| 858 | return self._root |
| 859 | |
| 860 | def GetNode(self, path): |
| 861 | """Look up a node from its path |
| 862 | |
| 863 | Args: |
| 864 | path: Path to look up, e.g. '/microcode/update@0' |
| 865 | Returns: |
| 866 | Node object, or None if not found |
| 867 | """ |
| 868 | node = self._root |
Simon Glass | c5eddc8 | 2018-07-06 10:27:30 -0600 | [diff] [blame] | 869 | parts = path.split('/') |
| 870 | if len(parts) < 2: |
| 871 | return None |
Simon Glass | 3b9a829 | 2019-07-20 12:23:39 -0600 | [diff] [blame] | 872 | if len(parts) == 2 and parts[1] == '': |
| 873 | return node |
Simon Glass | c5eddc8 | 2018-07-06 10:27:30 -0600 | [diff] [blame] | 874 | for part in parts[1:]: |
Simon Glass | aa1a5d7 | 2018-07-17 13:25:41 -0600 | [diff] [blame] | 875 | node = node.FindNode(part) |
Simon Glass | cc346a7 | 2016-07-25 18:59:07 -0600 | [diff] [blame] | 876 | if not node: |
| 877 | return None |
| 878 | return node |
| 879 | |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 880 | def Flush(self): |
| 881 | """Flush device tree changes back to the file |
| 882 | |
| 883 | 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] | 884 | """ |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 885 | with open(self._fname, 'wb') as fd: |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 886 | fd.write(self._fdt_obj.as_bytearray()) |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 887 | |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 888 | def Sync(self, auto_resize=False): |
| 889 | """Make sure any DT changes are written to the blob |
| 890 | |
| 891 | Args: |
| 892 | auto_resize: Resize the device tree automatically if it does not |
| 893 | have enough space for the update |
| 894 | |
| 895 | Raises: |
| 896 | FdtException if auto_resize is False and there is not enough space |
| 897 | """ |
Simon Glass | b2e8ac4 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 898 | self.CheckCache() |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 899 | self._root.Sync(auto_resize) |
Simon Glass | b2e8ac4 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 900 | self.Refresh() |
Simon Glass | eddd729 | 2018-09-14 04:57:13 -0600 | [diff] [blame] | 901 | |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 902 | def Pack(self): |
| 903 | """Pack the device tree down to its minimum size |
| 904 | |
| 905 | When nodes and properties shrink or are deleted, wasted space can |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 906 | build up in the device tree binary. |
| 907 | """ |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 908 | CheckErr(self._fdt_obj.pack(), 'pack') |
Simon Glass | b2e8ac4 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 909 | self.Refresh() |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 910 | |
Simon Glass | 792d239 | 2018-07-06 10:27:27 -0600 | [diff] [blame] | 911 | def GetContents(self): |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 912 | """Get the contents of the FDT |
| 913 | |
| 914 | Returns: |
| 915 | The FDT contents as a string of bytes |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 916 | """ |
Simon Glass | 1cd4008 | 2019-05-17 22:00:36 -0600 | [diff] [blame] | 917 | return bytes(self._fdt_obj.as_bytearray()) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 918 | |
Simon Glass | 0ed5075 | 2018-07-06 10:27:24 -0600 | [diff] [blame] | 919 | def GetFdtObj(self): |
| 920 | """Get the contents of the FDT |
| 921 | |
| 922 | Returns: |
| 923 | The FDT contents as a libfdt.Fdt object |
| 924 | """ |
| 925 | return self._fdt_obj |
| 926 | |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 927 | def GetProps(self, node): |
| 928 | """Get all properties from a node. |
| 929 | |
| 930 | Args: |
| 931 | node: Full path to node name to look in. |
| 932 | |
| 933 | Returns: |
| 934 | A dictionary containing all the properties, indexed by node name. |
| 935 | The entries are Prop objects. |
| 936 | |
| 937 | Raises: |
| 938 | ValueError: if the node does not exist. |
| 939 | """ |
| 940 | props_dict = {} |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 941 | poffset = self._fdt_obj.first_property_offset(node._offset, |
| 942 | QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 943 | while poffset >= 0: |
| 944 | p = self._fdt_obj.get_property_by_offset(poffset) |
Simon Glass | 70cd0d7 | 2018-07-06 10:27:20 -0600 | [diff] [blame] | 945 | prop = Prop(node, poffset, p.name, p) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 946 | props_dict[prop.name] = prop |
| 947 | |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 948 | poffset = self._fdt_obj.next_property_offset(poffset, |
| 949 | QUIET_NOTFOUND) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 950 | return props_dict |
| 951 | |
| 952 | def Invalidate(self): |
| 953 | """Mark our offset cache as invalid""" |
| 954 | self._cached_offsets = False |
| 955 | |
| 956 | def CheckCache(self): |
| 957 | """Refresh the offset cache if needed""" |
| 958 | if self._cached_offsets: |
| 959 | return |
| 960 | self.Refresh() |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 961 | |
| 962 | def Refresh(self): |
| 963 | """Refresh the offset cache""" |
| 964 | self._root.Refresh(0) |
Simon Glass | b2e8ac4 | 2021-03-21 18:24:36 +1300 | [diff] [blame] | 965 | self._cached_offsets = True |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 966 | |
| 967 | def GetStructOffset(self, offset): |
| 968 | """Get the file offset of a given struct offset |
| 969 | |
| 970 | Args: |
| 971 | offset: Offset within the 'struct' region of the device tree |
| 972 | Returns: |
| 973 | Position of @offset within the device tree binary |
| 974 | """ |
Simon Glass | 151aeed | 2018-07-06 10:27:26 -0600 | [diff] [blame] | 975 | return self._fdt_obj.off_dt_struct() + offset |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 976 | |
| 977 | @classmethod |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 978 | def Node(self, fdt, parent, offset, name, path): |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 979 | """Create a new node |
| 980 | |
| 981 | This is used by Fdt.Scan() to create a new node using the correct |
| 982 | class. |
| 983 | |
| 984 | Args: |
| 985 | fdt: Fdt object |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 986 | parent: Parent node, or None if this is the root node |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 987 | offset: Offset of node |
| 988 | name: Node name |
| 989 | path: Full path to node |
| 990 | """ |
Simon Glass | 80ef705 | 2017-08-29 14:15:47 -0600 | [diff] [blame] | 991 | node = Node(fdt, parent, offset, name, path) |
Simon Glass | 059ae52 | 2017-05-27 07:38:28 -0600 | [diff] [blame] | 992 | return node |
Simon Glass | a944093 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 993 | |
Simon Glass | 74f5feb | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 994 | def GetFilename(self): |
| 995 | """Get the filename of the device tree |
| 996 | |
| 997 | Returns: |
| 998 | String filename |
| 999 | """ |
| 1000 | return self._fname |
| 1001 | |
Simon Glass | a944093 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 1002 | def FdtScan(fname): |
Simon Glass | c38fee04 | 2018-07-06 10:27:32 -0600 | [diff] [blame] | 1003 | """Returns a new Fdt object""" |
Simon Glass | a944093 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 1004 | dtb = Fdt(fname) |
| 1005 | dtb.Scan() |
| 1006 | return dtb |