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