Simon Glass | ec564b4 | 2016-07-04 11:58:08 -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 | |
| 9 | import command |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 10 | import fdt |
| 11 | from fdt import Fdt, NodeBase, PropBase |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 12 | import fdt_util |
| 13 | import sys |
| 14 | |
| 15 | # This deals with a device tree, presenting it as a list of Node and Prop |
| 16 | # objects, representing nodes and properties, respectively. |
| 17 | # |
| 18 | # This implementation uses the fdtget tool to access the device tree, so it |
| 19 | # is not very efficient for larger trees. The tool is called once for each |
| 20 | # node and property in the tree. |
| 21 | |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 22 | class Prop(PropBase): |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 23 | """A device tree property |
| 24 | |
| 25 | Properties: |
| 26 | name: Property name (as per the device tree) |
| 27 | value: Property value as a string of bytes, or a list of strings of |
| 28 | bytes |
| 29 | type: Value type |
| 30 | """ |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 31 | def __init__(self, node, name, byte_list_str): |
| 32 | PropBase.__init__(self, node, 0, name) |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 33 | if not byte_list_str.strip(): |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 34 | self.type = fdt.TYPE_BOOL |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 35 | return |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 36 | self.bytes = [chr(int(byte, 16)) |
| 37 | for byte in byte_list_str.strip().split(' ')] |
Simon Glass | bc1dea3 | 2016-07-25 18:59:05 -0600 | [diff] [blame] | 38 | self.type, self.value = self.BytesToValue(''.join(self.bytes)) |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 39 | |
Simon Glass | d6a3391 | 2016-07-22 09:22:49 -0600 | [diff] [blame] | 40 | |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 41 | class Node(NodeBase): |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 42 | """A device tree node |
| 43 | |
| 44 | Properties: |
| 45 | name: Device tree node tname |
| 46 | path: Full path to node, along with the node name itself |
| 47 | _fdt: Device tree object |
| 48 | subnodes: A list of subnodes for this node, each a Node object |
| 49 | props: A dict of properties for this node, each a Prop object. |
| 50 | Keyed by property name |
| 51 | """ |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 52 | def __init__(self, fdt, offset, name, path): |
| 53 | NodeBase.__init__(self, fdt, offset, name, path) |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 54 | |
| 55 | def Scan(self): |
| 56 | """Scan a node's properties and subnodes |
| 57 | |
| 58 | This fills in the props and subnodes properties, recursively |
| 59 | searching into subnodes so that the entire tree is built. |
| 60 | """ |
Paul Burton | 4ae6549 | 2016-09-27 16:03:56 +0100 | [diff] [blame] | 61 | for name, byte_list_str in self._fdt.GetProps(self.path).items(): |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 62 | prop = Prop(self, name, byte_list_str) |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 63 | self.props[name] = prop |
| 64 | |
| 65 | for name in self._fdt.GetSubNodes(self.path): |
| 66 | sep = '' if self.path[-1] == '/' else '/' |
| 67 | path = self.path + sep + name |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 68 | node = Node(self._fdt, 0, name, path) |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 69 | self.subnodes.append(node) |
| 70 | |
| 71 | node.Scan() |
| 72 | |
Simon Glass | 2a70d89 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 73 | def DeleteProp(self, prop_name): |
| 74 | """Delete a property of a node |
| 75 | |
| 76 | The property is deleted using fdtput. |
| 77 | |
| 78 | Args: |
| 79 | prop_name: Name of the property to delete |
| 80 | Raises: |
| 81 | CommandError if the property does not exist |
| 82 | """ |
| 83 | args = [self._fdt._fname, '-d', self.path, prop_name] |
| 84 | command.Output('fdtput', *args) |
| 85 | del self.props[prop_name] |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 86 | |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 87 | class FdtFallback(Fdt): |
| 88 | """Provides simple access to a flat device tree blob using fdtget/fdtput |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 89 | |
| 90 | Properties: |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 91 | See superclass |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 92 | """ |
| 93 | |
| 94 | def __init__(self, fname): |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 95 | Fdt.__init__(self, fname) |
Simon Glass | 355c67c | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 96 | if self._fname: |
| 97 | self._fname = fdt_util.EnsureCompiled(self._fname) |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 98 | |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 99 | def GetSubNodes(self, node): |
| 100 | """Returns a list of sub-nodes of a given node |
| 101 | |
| 102 | Args: |
| 103 | node: Node name to return children from |
| 104 | |
| 105 | Returns: |
| 106 | List of children in the node (each a string node name) |
| 107 | |
| 108 | Raises: |
| 109 | CmdError: if the node does not exist. |
| 110 | """ |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 111 | out = command.Output('fdtget', self._fname, '-l', node) |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 112 | return out.strip().splitlines() |
| 113 | |
Simon Glass | 6b93c55 | 2016-07-25 18:59:11 -0600 | [diff] [blame] | 114 | def GetProps(self, node): |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 115 | """Get all properties from a node |
| 116 | |
| 117 | Args: |
| 118 | node: full path to node name to look in |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 119 | |
| 120 | Returns: |
| 121 | A dictionary containing all the properties, indexed by node name. |
| 122 | The entries are simply strings - no decoding of lists or numbers |
| 123 | is done. |
| 124 | |
| 125 | Raises: |
| 126 | CmdError: if the node does not exist. |
| 127 | """ |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 128 | out = command.Output('fdtget', self._fname, node, '-p') |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 129 | props = out.strip().splitlines() |
| 130 | props_dict = {} |
| 131 | for prop in props: |
| 132 | name = prop |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 133 | props_dict[prop] = self.GetProp(node, name) |
| 134 | return props_dict |
| 135 | |
| 136 | def GetProp(self, node, prop, default=None, typespec=None): |
| 137 | """Get a property from a device tree. |
| 138 | |
| 139 | This looks up the given node and property, and returns the value as a |
| 140 | string, |
| 141 | |
| 142 | If the node or property does not exist, this will return the default |
| 143 | value. |
| 144 | |
| 145 | Args: |
| 146 | node: Full path to node to look up. |
| 147 | prop: Property name to look up. |
| 148 | default: Default value to return if nothing is present in the fdt, |
| 149 | or None to raise in this case. This will be converted to a |
| 150 | string. |
| 151 | typespec: Type character to use (None for default, 's' for string) |
| 152 | |
| 153 | Returns: |
| 154 | string containing the property value. |
| 155 | |
| 156 | Raises: |
| 157 | CmdError: if the property does not exist and no default is provided. |
| 158 | """ |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 159 | args = [self._fname, node, prop, '-t', 'bx'] |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 160 | if default is not None: |
| 161 | args += ['-d', str(default)] |
| 162 | if typespec is not None: |
Simon Glass | 0734b70 | 2016-09-25 15:52:17 -0600 | [diff] [blame] | 163 | args += ['-t', typespec] |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 164 | out = command.Output('fdtget', *args) |
| 165 | return out.strip() |
Simon Glass | a06a34b | 2016-07-25 18:59:04 -0600 | [diff] [blame] | 166 | |
| 167 | @classmethod |
| 168 | def Node(self, fdt, offset, name, path): |
| 169 | """Create a new node |
| 170 | |
| 171 | This is used by Fdt.Scan() to create a new node using the correct |
| 172 | class. |
| 173 | |
| 174 | Args: |
| 175 | fdt: Fdt object |
| 176 | offset: Offset of node |
| 177 | name: Node name |
| 178 | path: Full path to node |
| 179 | """ |
| 180 | node = Node(fdt, offset, name, path) |
| 181 | return node |