2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright (C) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
16 VERSION3 = sys.version_info > (3, 0)
18 def get_plain_bytes(val):
19 """Handle Python 3 strings"""
20 if isinstance(val, bytes):
21 val = val.decode('utf-8')
22 return val.encode('raw_unicode_escape')
24 def fdt32_to_cpu(val):
25 """Convert a device tree cell to an integer
28 Value to convert (4-character string representing the cell value)
31 A native-endian integer value
34 # This code is not reached in Python 2
35 val = get_plain_bytes(val) # pragma: no cover
36 return struct.unpack('>I', val)[0]
38 def fdt_cells_to_cpu(val, cells):
39 """Convert one or two cells to a long integer
42 Value to convert (array of one or more 4-character strings)
45 A native-endian long value
49 out = long(fdt32_to_cpu(val[0]))
51 out = out << 32 | fdt32_to_cpu(val[1])
54 def EnsureCompiled(fname, capture_stderr=False):
55 """Compile an fdt .dts source file into a .dtb binary blob if needed.
58 fname: Filename (if .dts it will be compiled). It not it will be
62 Filename of resulting .dtb file
64 _, ext = os.path.splitext(fname)
68 dts_input = tools.GetOutputFilename('source.dts')
69 dtb_output = tools.GetOutputFilename('source.dtb')
71 search_paths = [os.path.join(os.getcwd(), 'include')]
72 root, _ = os.path.splitext(fname)
73 args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
75 for path in search_paths:
76 args.extend(['-I', path])
77 args += ['-o', dts_input, fname]
78 command.Run('cc', *args)
80 # If we don't have a directory, put it in the tools tempdir
82 for path in search_paths:
83 search_list.extend(['-i', path])
84 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
85 '-W', 'no-unit_address_vs_reg']
86 args.extend(search_list)
87 args.append(dts_input)
88 dtc = os.environ.get('DTC') or 'dtc'
89 command.Run(dtc, *args, capture_stderr=capture_stderr)
92 def GetInt(node, propname, default=None):
93 prop = node.props.get(propname)
96 if isinstance(prop.value, list):
97 raise ValueError("Node '%s' property '%s' has list value: expecting "
98 "a single integer" % (node.name, propname))
99 value = fdt32_to_cpu(prop.value)
102 def GetString(node, propname, default=None):
103 prop = node.props.get(propname)
107 if isinstance(value, list):
108 raise ValueError("Node '%s' property '%s' has list value: expecting "
109 "a single string" % (node.name, propname))
112 def GetBool(node, propname, default=False):
113 if propname in node.props: