Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (C) 2016 Google, Inc |
| 4 | # Written by Simon Glass <sjg@chromium.org> |
| 5 | # |
| 6 | # SPDX-License-Identifier: GPL-2.0+ |
| 7 | # |
| 8 | |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 9 | import struct |
| 10 | import sys |
| 11 | |
| 12 | import fdt |
| 13 | from fdt import Fdt, NodeBase, PropBase |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 14 | import fdt_util |
| 15 | import libfdt |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 16 | |
| 17 | # This deals with a device tree, presenting it as a list of Node and Prop |
| 18 | # objects, representing nodes and properties, respectively. |
| 19 | # |
| 20 | # This implementation uses a libfdt Python library to access the device tree, |
| 21 | # so it is fairly efficient. |
| 22 | |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 23 | class Prop(PropBase): |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 24 | """A device tree property |
| 25 | |
| 26 | Properties: |
| 27 | name: Property name (as per the device tree) |
| 28 | value: Property value as a string of bytes, or a list of strings of |
| 29 | bytes |
| 30 | type: Value type |
| 31 | """ |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 32 | def __init__(self, node, offset, name, bytes): |
| 33 | PropBase.__init__(self, node, offset, name) |
| 34 | self.bytes = bytes |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 35 | if not bytes: |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 36 | self.type = fdt.TYPE_BOOL |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 37 | self.value = True |
| 38 | return |
Simon Glass | b1a5e26 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 39 | self.type, self.value = self.BytesToValue(bytes) |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 40 | |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 41 | class Node(NodeBase): |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 42 | """A device tree node |
| 43 | |
| 44 | Properties: |
| 45 | offset: Integer offset in the device tree |
| 46 | name: Device tree node tname |
| 47 | path: Full path to node, along with the node name itself |
| 48 | _fdt: Device tree object |
| 49 | subnodes: A list of subnodes for this node, each a Node object |
| 50 | props: A dict of properties for this node, each a Prop object. |
| 51 | Keyed by property name |
| 52 | """ |
| 53 | def __init__(self, fdt, offset, name, path): |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 54 | NodeBase.__init__(self, fdt, offset, name, path) |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 55 | |
| 56 | def Scan(self): |
| 57 | """Scan a node's properties and subnodes |
| 58 | |
| 59 | This fills in the props and subnodes properties, recursively |
| 60 | searching into subnodes so that the entire tree is built. |
| 61 | """ |
| 62 | self.props = self._fdt.GetProps(self.path) |
| 63 | |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 64 | offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self._offset) |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 65 | while offset >= 0: |
| 66 | sep = '' if self.path[-1] == '/' else '/' |
| 67 | name = libfdt.Name(self._fdt.GetFdt(), offset) |
| 68 | path = self.path + sep + name |
| 69 | node = Node(self._fdt, offset, name, path) |
| 70 | self.subnodes.append(node) |
| 71 | |
| 72 | node.Scan() |
| 73 | offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset) |
| 74 | |
| 75 | |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 76 | class FdtNormal(Fdt): |
| 77 | """Provides simple access to a flat device tree blob using libfdt. |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 78 | |
| 79 | Properties: |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 80 | _fdt: Device tree contents (bytearray) |
| 81 | _cached_offsets: True if all the nodes have a valid _offset property, |
| 82 | False if something has changed to invalidate the offsets |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 83 | """ |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 84 | def __init__(self, fname): |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 85 | Fdt.__init__(self, fname) |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 86 | if self._fname: |
| 87 | self._fname = fdt_util.EnsureCompiled(self._fname) |
| 88 | |
| 89 | with open(self._fname) as fd: |
| 90 | self._fdt = fd.read() |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 91 | |
| 92 | def GetFdt(self): |
| 93 | """Get the contents of the FDT |
| 94 | |
| 95 | Returns: |
| 96 | The FDT contents as a string of bytes |
| 97 | """ |
| 98 | return self._fdt |
| 99 | |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 100 | def GetProps(self, node): |
| 101 | """Get all properties from a node. |
| 102 | |
| 103 | Args: |
| 104 | node: Full path to node name to look in. |
| 105 | |
| 106 | Returns: |
| 107 | A dictionary containing all the properties, indexed by node name. |
| 108 | The entries are Prop objects. |
| 109 | |
| 110 | Raises: |
| 111 | ValueError: if the node does not exist. |
| 112 | """ |
| 113 | offset = libfdt.fdt_path_offset(self._fdt, node) |
| 114 | if offset < 0: |
| 115 | libfdt.Raise(offset) |
| 116 | props_dict = {} |
| 117 | poffset = libfdt.fdt_first_property_offset(self._fdt, offset) |
| 118 | while poffset >= 0: |
| 119 | dprop, plen = libfdt.fdt_get_property_by_offset(self._fdt, poffset) |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 120 | prop = Prop(node, poffset, libfdt.String(self._fdt, dprop.nameoff), |
| 121 | libfdt.Data(dprop)) |
Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 122 | props_dict[prop.name] = prop |
| 123 | |
| 124 | poffset = libfdt.fdt_next_property_offset(self._fdt, poffset) |
| 125 | return props_dict |
Simon Glass | 1f941b6 | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 126 | |
| 127 | @classmethod |
| 128 | def Node(self, fdt, offset, name, path): |
| 129 | """Create a new node |
| 130 | |
| 131 | This is used by Fdt.Scan() to create a new node using the correct |
| 132 | class. |
| 133 | |
| 134 | Args: |
| 135 | fdt: Fdt object |
| 136 | offset: Offset of node |
| 137 | name: Node name |
| 138 | path: Full path to node |
| 139 | """ |
| 140 | node = Node(fdt, offset, name, path) |
| 141 | return node |