2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright (C) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
16 def fdt32_to_cpu(val):
17 """Convert a device tree cell to an integer
20 Value to convert (4-character string representing the cell value)
23 A native-endian integer value
25 if sys.version_info > (3, 0):
26 if isinstance(val, bytes):
27 val = val.decode('utf-8')
28 val = val.encode('raw_unicode_escape')
29 return struct.unpack('>I', val)[0]
31 def fdt_cells_to_cpu(val, cells):
32 """Convert one or two cells to a long integer
35 Value to convert (array of one or more 4-character strings)
38 A native-endian long value
42 out = long(fdt32_to_cpu(val[0]))
44 out = out << 32 | fdt32_to_cpu(val[1])
47 def EnsureCompiled(fname):
48 """Compile an fdt .dts source file into a .dtb binary blob if needed.
51 fname: Filename (if .dts it will be compiled). It not it will be
55 Filename of resulting .dtb file
57 _, ext = os.path.splitext(fname)
61 dts_input = tools.GetOutputFilename('source.dts')
62 dtb_output = tools.GetOutputFilename('source.dtb')
64 search_paths = [os.path.join(os.getcwd(), 'include')]
65 root, _ = os.path.splitext(fname)
66 args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
68 for path in search_paths:
69 args.extend(['-I', path])
70 args += ['-o', dts_input, fname]
71 command.Run('cc', *args)
73 # If we don't have a directory, put it in the tools tempdir
75 for path in search_paths:
76 search_list.extend(['-i', path])
77 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
78 '-W', 'no-unit_address_vs_reg']
79 args.extend(search_list)
80 args.append(dts_input)
81 dtc = os.environ.get('DTC') or 'dtc'
82 command.Run(dtc, *args)
85 def GetInt(node, propname, default=None):
86 prop = node.props.get(propname)
89 value = fdt32_to_cpu(prop.value)
90 if type(value) == type(list):
91 raise ValueError("Node '%s' property '%' has list value: expecting"
92 "a single integer" % (node.name, propname))
95 def GetString(node, propname, default=None):
96 prop = node.props.get(propname)
100 if type(value) == type(list):
101 raise ValueError("Node '%s' property '%' has list value: expecting"
102 "a single string" % (node.name, propname))
105 def GetBool(node, propname, default=False):
106 if propname in node.props: