dtoc: Add methods for reading data from properties
authorSimon Glass <sjg@chromium.org>
Tue, 26 Jul 2016 00:59:18 +0000 (18:59 -0600)
committerSimon Glass <sjg@chromium.org>
Mon, 19 Sep 2016 03:04:39 +0000 (21:04 -0600)
Provide easy helpers for reading integer, string and boolean values from
device-tree properties.

Signed-off-by: Simon Glass <sjg@chromium.org>
tools/dtoc/fdt_util.py

index 32f41d72d79238cf157697e57f95dc26161de7ae..3a108381099e613a81fdf62e2f4727b8fb034b31 100644 (file)
@@ -59,3 +59,28 @@ def EnsureCompiled(fname):
     args.append(dts_input)
     command.Run('dtc', *args)
     return dtb_output
+
+def GetInt(node, propname, default=None):
+    prop = node.props.get(propname)
+    if not prop:
+        return default
+    value = fdt32_to_cpu(prop.value)
+    if type(value) == type(list):
+        raise ValueError("Node '%s' property '%' has list value: expecting"
+                         "a single integer" % (node.name, propname))
+    return value
+
+def GetString(node, propname, default=None):
+    prop = node.props.get(propname)
+    if not prop:
+        return default
+    value = prop.value
+    if type(value) == type(list):
+        raise ValueError("Node '%s' property '%' has list value: expecting"
+                         "a single string" % (node.name, propname))
+    return value
+
+def GetBool(node, propname, default=False):
+    if propname in node.props:
+        return True
+    return default