blob: b9dfae8d0e7b606c28fac252f82afcaef801fe49 [file] [log] [blame]
Simon Glassec564b42016-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 Glass355c67c2016-07-25 18:59:10 -06009import os
Simon Glassec564b42016-07-04 11:58:08 -060010import struct
Paul Burtonc4c5f9e2016-09-27 16:03:57 +010011import sys
Simon Glass355c67c2016-07-25 18:59:10 -060012import tempfile
13
14import command
15import tools
Simon Glassec564b42016-07-04 11:58:08 -060016
Simon Glassec564b42016-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 Burtonc4c5f9e2016-09-27 16:03:57 +010026 if sys.version_info > (3, 0):
George McCollisterf156b5b2017-03-30 09:44:25 -050027 if isinstance(val, bytes):
28 val = val.decode('utf-8')
Paul Burtonc4c5f9e2016-09-27 16:03:57 +010029 val = val.encode('raw_unicode_escape')
Simon Glass20024da2016-07-25 18:59:17 -060030 return struct.unpack('>I', val)[0]
Simon Glass355c67c2016-07-25 18:59:10 -060031
32def EnsureCompiled(fname):
33 """Compile an fdt .dts source file into a .dtb binary blob if needed.
34
35 Args:
36 fname: Filename (if .dts it will be compiled). It not it will be
37 left alone
38
39 Returns:
40 Filename of resulting .dtb file
41 """
42 _, ext = os.path.splitext(fname)
43 if ext != '.dts':
44 return fname
45
46 dts_input = tools.GetOutputFilename('source.dts')
47 dtb_output = tools.GetOutputFilename('source.dtb')
48
49 search_paths = [os.path.join(os.getcwd(), 'include')]
50 root, _ = os.path.splitext(fname)
51 args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
52 args += ['-Ulinux']
53 for path in search_paths:
54 args.extend(['-I', path])
55 args += ['-o', dts_input, fname]
56 command.Run('cc', *args)
57
58 # If we don't have a directory, put it in the tools tempdir
59 search_list = []
60 for path in search_paths:
61 search_list.extend(['-i', path])
62 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb']
63 args.extend(search_list)
64 args.append(dts_input)
65 command.Run('dtc', *args)
66 return dtb_output
Simon Glass8f224b32016-07-25 18:59:18 -060067
68def GetInt(node, propname, default=None):
69 prop = node.props.get(propname)
70 if not prop:
71 return default
72 value = fdt32_to_cpu(prop.value)
73 if type(value) == type(list):
74 raise ValueError("Node '%s' property '%' has list value: expecting"
75 "a single integer" % (node.name, propname))
76 return value
77
78def GetString(node, propname, default=None):
79 prop = node.props.get(propname)
80 if not prop:
81 return default
82 value = prop.value
83 if type(value) == type(list):
84 raise ValueError("Node '%s' property '%' has list value: expecting"
85 "a single string" % (node.name, propname))
86 return value
87
88def GetBool(node, propname, default=False):
89 if propname in node.props:
90 return True
91 return default