3 # Copyright (C) 2016 Google, Inc
4 # Written by Simon Glass <sjg@chromium.org>
6 # SPDX-License-Identifier: GPL-2.0+
17 def fdt32_to_cpu(val):
18 """Convert a device tree cell to an integer
21 Value to convert (4-character string representing the cell value)
24 A native-endian integer value
26 if sys.version_info > (3, 0):
27 val = val.encode('raw_unicode_escape')
28 return struct.unpack('>I', val)[0]
30 def EnsureCompiled(fname):
31 """Compile an fdt .dts source file into a .dtb binary blob if needed.
34 fname: Filename (if .dts it will be compiled). It not it will be
38 Filename of resulting .dtb file
40 _, ext = os.path.splitext(fname)
44 dts_input = tools.GetOutputFilename('source.dts')
45 dtb_output = tools.GetOutputFilename('source.dtb')
47 search_paths = [os.path.join(os.getcwd(), 'include')]
48 root, _ = os.path.splitext(fname)
49 args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
51 for path in search_paths:
52 args.extend(['-I', path])
53 args += ['-o', dts_input, fname]
54 command.Run('cc', *args)
56 # If we don't have a directory, put it in the tools tempdir
58 for path in search_paths:
59 search_list.extend(['-i', path])
60 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb']
61 args.extend(search_list)
62 args.append(dts_input)
63 command.Run('dtc', *args)
66 def GetInt(node, propname, default=None):
67 prop = node.props.get(propname)
70 value = fdt32_to_cpu(prop.value)
71 if type(value) == type(list):
72 raise ValueError("Node '%s' property '%' has list value: expecting"
73 "a single integer" % (node.name, propname))
76 def GetString(node, propname, default=None):
77 prop = node.props.get(propname)
81 if type(value) == type(list):
82 raise ValueError("Node '%s' property '%' has list value: expecting"
83 "a single string" % (node.name, propname))
86 def GetBool(node, propname, default=False):
87 if propname in node.props: