blob: 413d45daa42d156761edc381fa763d7a8157b208 [file] [log] [blame]
Simon Glassa06a34b2016-07-25 18:59:04 -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
9import struct
10import sys
11
12import fdt_util
13
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.
19
20def CheckErr(errnum, msg):
21 if errnum:
22 raise ValueError('Error %d: %s: %s' %
23 (errnum, libfdt.fdt_strerror(errnum), msg))
24
25class PropBase:
26 """A device tree property
27
28 Properties:
29 name: Property name (as per the device tree)
30 value: Property value as a string of bytes, or a list of strings of
31 bytes
32 type: Value type
33 """
34 def __init__(self, node, offset, name):
35 self._node = node
36 self._offset = offset
37 self.name = name
38 self.value = None
39
40class NodeBase:
41 """A device tree node
42
43 Properties:
44 offset: Integer offset in the device tree
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 """
52 def __init__(self, fdt, offset, name, path):
53 self._fdt = fdt
54 self._offset = offset
55 self.name = name
56 self.path = path
57 self.subnodes = []
58 self.props = {}
59
60class Fdt:
61 """Provides simple access to a flat device tree blob.
62
63 Properties:
64 fname: Filename of fdt
65 _root: Root of device tree (a Node object)
66 """
67 def __init__(self, fname):
68 self._fname = fname