]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/dtoc/fdt_util.py
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 if isinstance(val
, bytes
):
28 val
= val
.decode('utf-8')
29 val
= val
.encode('raw_unicode_escape')
30 return struct
.unpack('>I', val
)[0]
32 def fdt_cells_to_cpu(val
, cells
):
33 """Convert one or two cells to a long integer
36 Value to convert (array of one or more 4-character strings)
39 A native-endian long value
43 out
= long(fdt32_to_cpu(val
[0]))
45 out
= out
<< 32 |
fdt32_to_cpu(val
[1])
48 def EnsureCompiled(fname
):
49 """Compile an fdt .dts source file into a .dtb binary blob if needed.
52 fname: Filename (if .dts it will be compiled). It not it will be
56 Filename of resulting .dtb file
58 _
, ext
= os
.path
.splitext(fname
)
62 dts_input
= tools
.GetOutputFilename('source.dts')
63 dtb_output
= tools
.GetOutputFilename('source.dtb')
65 search_paths
= [os
.path
.join(os
.getcwd(), 'include')]
66 root
, _
= os
.path
.splitext(fname
)
67 args
= ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
69 for path
in search_paths
:
70 args
.extend(['-I', path
])
71 args
+= ['-o', dts_input
, fname
]
72 command
.Run('cc', *args
)
74 # If we don't have a directory, put it in the tools tempdir
76 for path
in search_paths
:
77 search_list
.extend(['-i', path
])
78 args
= ['-I', 'dts', '-o', dtb_output
, '-O', 'dtb']
79 args
.extend(search_list
)
80 args
.append(dts_input
)
81 command
.Run('dtc', *args
)
84 def GetInt(node
, propname
, default
=None):
85 prop
= node
.props
.get(propname
)
88 value
= fdt32_to_cpu(prop
.value
)
89 if type(value
) == type(list):
90 raise ValueError("Node '%s' property '%' has list value: expecting"
91 "a single integer" % (node
.name
, propname
))
94 def GetString(node
, propname
, default
=None):
95 prop
= node
.props
.get(propname
)
99 if type(value
) == type(list):
100 raise ValueError("Node '%s' property '%' has list value: expecting"
101 "a single string" % (node
.name
, propname
))
104 def GetBool(node
, propname
, default
=False):
105 if propname
in node
.props
: