blob: d9ba4aca80aeb1db460f730688015b9820334818 [file] [log] [blame]
Simon Glassa2a6f042016-07-04 11:58:11 -06001#!/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 Glass1f941b62016-07-25 18:59:04 -06009import struct
10import sys
11
12import fdt
13from fdt import Fdt, NodeBase, PropBase
Simon Glassa2a6f042016-07-04 11:58:11 -060014import fdt_util
15import libfdt
Simon Glassa2a6f042016-07-04 11:58:11 -060016
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 Glass1f941b62016-07-25 18:59:04 -060023class Prop(PropBase):
Simon Glassa2a6f042016-07-04 11:58:11 -060024 """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 Glass1f941b62016-07-25 18:59:04 -060032 def __init__(self, node, offset, name, bytes):
33 PropBase.__init__(self, node, offset, name)
34 self.bytes = bytes
Simon Glassa2a6f042016-07-04 11:58:11 -060035 if not bytes:
Simon Glassb1a5e262016-07-25 18:59:05 -060036 self.type = fdt.TYPE_BOOL
Simon Glassa2a6f042016-07-04 11:58:11 -060037 self.value = True
38 return
Simon Glassb1a5e262016-07-25 18:59:05 -060039 self.type, self.value = self.BytesToValue(bytes)
Simon Glassa2a6f042016-07-04 11:58:11 -060040
Simon Glass1f941b62016-07-25 18:59:04 -060041class Node(NodeBase):
Simon Glassa2a6f042016-07-04 11:58:11 -060042 """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 Glass1f941b62016-07-25 18:59:04 -060054 NodeBase.__init__(self, fdt, offset, name, path)
Simon Glassa2a6f042016-07-04 11:58:11 -060055
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 Glass1f941b62016-07-25 18:59:04 -060064 offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self._offset)
Simon Glassa2a6f042016-07-04 11:58:11 -060065 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 Glass1f941b62016-07-25 18:59:04 -060076class FdtNormal(Fdt):
77 """Provides simple access to a flat device tree blob using libfdt.
Simon Glassa2a6f042016-07-04 11:58:11 -060078
79 Properties:
Simon Glass1f941b62016-07-25 18:59:04 -060080 _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 Glassa2a6f042016-07-04 11:58:11 -060083 """
Simon Glassa2a6f042016-07-04 11:58:11 -060084 def __init__(self, fname):
Simon Glass1f941b62016-07-25 18:59:04 -060085 Fdt.__init__(self, fname)
Simon Glassefc9bf62016-07-25 18:59:10 -060086 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 Glassa2a6f042016-07-04 11:58:11 -060091
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 Glassa2a6f042016-07-04 11:58:11 -0600100 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 Glass1f941b62016-07-25 18:59:04 -0600120 prop = Prop(node, poffset, libfdt.String(self._fdt, dprop.nameoff),
121 libfdt.Data(dprop))
Simon Glassa2a6f042016-07-04 11:58:11 -0600122 props_dict[prop.name] = prop
123
124 poffset = libfdt.fdt_next_property_offset(self._fdt, poffset)
125 return props_dict
Simon Glass1f941b62016-07-25 18:59:04 -0600126
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