blob: bec6ee947a86467fd896c4b78fcb4342848f4a3d [file] [log] [blame]
Simon Glassfa8974d2016-07-04 11:58:08 -06001#!/usr/bin/python
2#
3# Copyright (C) 2016 Google, Inc
4# Written by Simon Glass <sjg@chromium.org>
5#
6# SPDX-License-Identifier: GPL-2.0+
7#
8
Simon Glassefc9bf62016-07-25 18:59:10 -06009import os
Simon Glassfa8974d2016-07-04 11:58:08 -060010import struct
Paul Burton307f1fa2016-09-27 16:03:57 +010011import sys
Simon Glassefc9bf62016-07-25 18:59:10 -060012import tempfile
13
14import command
15import tools
Simon Glassfa8974d2016-07-04 11:58:08 -060016
Simon Glassfa8974d2016-07-04 11:58:08 -060017def fdt32_to_cpu(val):
18 """Convert a device tree cell to an integer
19
20 Args:
21 Value to convert (4-character string representing the cell value)
22
23 Return:
24 A native-endian integer value
25 """
Paul Burton307f1fa2016-09-27 16:03:57 +010026 if sys.version_info > (3, 0):
George McCollister77bb05e2017-03-30 09:44:25 -050027 if isinstance(val, bytes):
28 val = val.decode('utf-8')
Paul Burton307f1fa2016-09-27 16:03:57 +010029 val = val.encode('raw_unicode_escape')
Simon Glass29462c32016-07-25 18:59:17 -060030 return struct.unpack('>I', val)[0]
Simon Glassefc9bf62016-07-25 18:59:10 -060031
Simon Glassfc3ae9c2017-08-29 14:15:48 -060032def fdt_cells_to_cpu(val, cells):
33 """Convert one or two cells to a long integer
34
35 Args:
36 Value to convert (array of one or more 4-character strings)
37
38 Return:
39 A native-endian long value
40 """
41 out = long(fdt32_to_cpu(val[0]))
42 if cells == 2:
43 out = out << 32 | fdt32_to_cpu(val[1])
44 return out
45
Simon Glassefc9bf62016-07-25 18:59:10 -060046def EnsureCompiled(fname):
47 """Compile an fdt .dts source file into a .dtb binary blob if needed.
48
49 Args:
50 fname: Filename (if .dts it will be compiled). It not it will be
51 left alone
52
53 Returns:
54 Filename of resulting .dtb file
55 """
56 _, ext = os.path.splitext(fname)
57 if ext != '.dts':
58 return fname
59
60 dts_input = tools.GetOutputFilename('source.dts')
61 dtb_output = tools.GetOutputFilename('source.dtb')
62
63 search_paths = [os.path.join(os.getcwd(), 'include')]
64 root, _ = os.path.splitext(fname)
65 args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
66 args += ['-Ulinux']
67 for path in search_paths:
68 args.extend(['-I', path])
69 args += ['-o', dts_input, fname]
70 command.Run('cc', *args)
71
72 # If we don't have a directory, put it in the tools tempdir
73 search_list = []
74 for path in search_paths:
75 search_list.extend(['-i', path])
76 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb']
77 args.extend(search_list)
78 args.append(dts_input)
79 command.Run('dtc', *args)
80 return dtb_output
Simon Glass4066b312016-07-25 18:59:18 -060081
82def GetInt(node, propname, default=None):
83 prop = node.props.get(propname)
84 if not prop:
85 return default
86 value = fdt32_to_cpu(prop.value)
87 if type(value) == type(list):
88 raise ValueError("Node '%s' property '%' has list value: expecting"
89 "a single integer" % (node.name, propname))
90 return value
91
92def GetString(node, propname, default=None):
93 prop = node.props.get(propname)
94 if not prop:
95 return default
96 value = prop.value
97 if type(value) == type(list):
98 raise ValueError("Node '%s' property '%' has list value: expecting"
99 "a single string" % (node.name, propname))
100 return value
101
102def GetBool(node, propname, default=False):
103 if propname in node.props:
104 return True
105 return default