3 # Copyright (C) 2016 Google, Inc
4 # Written by Simon Glass <sjg@chromium.org>
6 # SPDX-License-Identifier: GPL-2.0+
14 # This deals with a device tree, presenting it as an assortment of Node and
15 # Prop objects, representing nodes and properties, respectively. This file
16 # contains the base classes and defines the high-level API. Most of the
17 # implementation is in the FdtFallback and FdtNormal subclasses. See
18 # fdt_select.py for how to create an Fdt object.
20 # A list of types we support
21 (TYPE_BYTE, TYPE_INT, TYPE_STRING, TYPE_BOOL) = range(4)
23 def CheckErr(errnum, msg):
25 raise ValueError('Error %d: %s: %s' %
26 (errnum, libfdt.fdt_strerror(errnum), msg))
29 """A device tree property
32 name: Property name (as per the device tree)
33 value: Property value as a string of bytes, or a list of strings of
37 def __init__(self, node, offset, name):
43 def BytesToValue(self, bytes):
44 """Converts a string of bytes into a type and value
47 A string containing bytes
52 Data, either a single element or a list of elements. Each element
54 TYPE_STRING: string value from the property
55 TYPE_INT: a byte-swapped integer stored as a 4-byte string
56 TYPE_BYTE: a byte stored as a single-byte string
59 strings = bytes.split('\0')
61 count = len(strings) - 1
62 if count > 0 and not strings[-1]:
63 for string in strings[:-1]:
68 if ch < ' ' or ch > '~':
75 return TYPE_STRING, strings[0]
77 return TYPE_STRING, strings[:-1]
80 return TYPE_BYTE, bytes[0]
82 return TYPE_BYTE, list(bytes)
84 for i in range(0, size, 4):
85 val.append(bytes[i:i + 4])
87 return TYPE_INT, val[0]
91 def GetEmpty(self, type):
92 """Get an empty / zero value of the given type
95 A single value of the given type
99 elif type == TYPE_INT:
100 return struct.pack('<I', 0);
101 elif type == TYPE_STRING:
107 """A device tree node
110 offset: Integer offset in the device tree
111 name: Device tree node tname
112 path: Full path to node, along with the node name itself
113 _fdt: Device tree object
114 subnodes: A list of subnodes for this node, each a Node object
115 props: A dict of properties for this node, each a Prop object.
116 Keyed by property name
118 def __init__(self, fdt, offset, name, path):
120 self._offset = offset
127 """Provides simple access to a flat device tree blob.
130 fname: Filename of fdt
131 _root: Root of device tree (a Node object)
133 def __init__(self, fname):