blob: 5b631419a921b323819fa092552140b6aa1fdd5f [file] [log] [blame]
Simon Glassfa8974d2016-07-04 11:58:08 -06001#!/usr/bin/python
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glassfa8974d2016-07-04 11:58:08 -06003#
4# Copyright (C) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
Simon Glassfa8974d2016-07-04 11:58:08 -06007
Simon Glassefc9bf62016-07-25 18:59:10 -06008import os
Simon Glassfa8974d2016-07-04 11:58:08 -06009import struct
Paul Burton307f1fa2016-09-27 16:03:57 +010010import sys
Simon Glassefc9bf62016-07-25 18:59:10 -060011import tempfile
12
13import command
14import tools
Simon Glassfa8974d2016-07-04 11:58:08 -060015
Simon Glass9c526332018-07-06 10:27:28 -060016VERSION3 = sys.version_info > (3, 0)
17
18def 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')
23
Simon Glassfa8974d2016-07-04 11:58:08 -060024def fdt32_to_cpu(val):
25 """Convert a device tree cell to an integer
26
27 Args:
28 Value to convert (4-character string representing the cell value)
29
30 Return:
31 A native-endian integer value
32 """
Simon Glass9c526332018-07-06 10:27:28 -060033 if VERSION3:
34 # This code is not reached in Python 2
35 val = get_plain_bytes(val) # pragma: no cover
Simon Glass29462c32016-07-25 18:59:17 -060036 return struct.unpack('>I', val)[0]
Simon Glassefc9bf62016-07-25 18:59:10 -060037
Simon Glassfc3ae9c2017-08-29 14:15:48 -060038def fdt_cells_to_cpu(val, cells):
39 """Convert one or two cells to a long integer
40
41 Args:
42 Value to convert (array of one or more 4-character strings)
43
44 Return:
45 A native-endian long value
46 """
Simon Glass1b1fe412017-08-29 14:15:50 -060047 if not cells:
48 return 0
Simon Glassfc3ae9c2017-08-29 14:15:48 -060049 out = long(fdt32_to_cpu(val[0]))
50 if cells == 2:
51 out = out << 32 | fdt32_to_cpu(val[1])
52 return out
53
Simon Glass3bce93d2018-07-06 10:27:37 -060054def EnsureCompiled(fname, capture_stderr=False):
Simon Glassefc9bf62016-07-25 18:59:10 -060055 """Compile an fdt .dts source file into a .dtb binary blob if needed.
56
57 Args:
58 fname: Filename (if .dts it will be compiled). It not it will be
59 left alone
60
61 Returns:
62 Filename of resulting .dtb file
63 """
64 _, ext = os.path.splitext(fname)
65 if ext != '.dts':
66 return fname
67
68 dts_input = tools.GetOutputFilename('source.dts')
69 dtb_output = tools.GetOutputFilename('source.dtb')
70
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__']
74 args += ['-Ulinux']
75 for path in search_paths:
76 args.extend(['-I', path])
77 args += ['-o', dts_input, fname]
78 command.Run('cc', *args)
79
80 # If we don't have a directory, put it in the tools tempdir
81 search_list = []
82 for path in search_paths:
83 search_list.extend(['-i', path])
Simon Glassc5c7b1a2017-11-12 21:52:09 -070084 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
85 '-W', 'no-unit_address_vs_reg']
Simon Glassefc9bf62016-07-25 18:59:10 -060086 args.extend(search_list)
87 args.append(dts_input)
Simon Glassade2ef62017-12-24 12:12:07 -070088 dtc = os.environ.get('DTC') or 'dtc'
Simon Glass3bce93d2018-07-06 10:27:37 -060089 command.Run(dtc, *args, capture_stderr=capture_stderr)
Simon Glassefc9bf62016-07-25 18:59:10 -060090 return dtb_output
Simon Glass4066b312016-07-25 18:59:18 -060091
92def GetInt(node, propname, default=None):
93 prop = node.props.get(propname)
94 if not prop:
95 return default
Simon Glass9c526332018-07-06 10:27:28 -060096 if isinstance(prop.value, list):
97 raise ValueError("Node '%s' property '%s' has list value: expecting "
Simon Glass4066b312016-07-25 18:59:18 -060098 "a single integer" % (node.name, propname))
Simon Glass9c526332018-07-06 10:27:28 -060099 value = fdt32_to_cpu(prop.value)
Simon Glass4066b312016-07-25 18:59:18 -0600100 return value
101
102def GetString(node, propname, default=None):
103 prop = node.props.get(propname)
104 if not prop:
105 return default
106 value = prop.value
Simon Glass9c526332018-07-06 10:27:28 -0600107 if isinstance(value, list):
108 raise ValueError("Node '%s' property '%s' has list value: expecting "
Simon Glass4066b312016-07-25 18:59:18 -0600109 "a single string" % (node.name, propname))
110 return value
111
112def GetBool(node, propname, default=False):
113 if propname in node.props:
114 return True
115 return default