blob: 3a108381099e613a81fdf62e2f4727b8fb034b31 [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
Simon Glass355c67c2016-07-25 18:59:10 -060011import tempfile
12
13import command
14import tools
Simon Glassec564b42016-07-04 11:58:08 -060015
Simon Glassec564b42016-07-04 11:58:08 -060016def fdt32_to_cpu(val):
17 """Convert a device tree cell to an integer
18
19 Args:
20 Value to convert (4-character string representing the cell value)
21
22 Return:
23 A native-endian integer value
24 """
Simon Glass20024da2016-07-25 18:59:17 -060025 return struct.unpack('>I', val)[0]
Simon Glass355c67c2016-07-25 18:59:10 -060026
27def EnsureCompiled(fname):
28 """Compile an fdt .dts source file into a .dtb binary blob if needed.
29
30 Args:
31 fname: Filename (if .dts it will be compiled). It not it will be
32 left alone
33
34 Returns:
35 Filename of resulting .dtb file
36 """
37 _, ext = os.path.splitext(fname)
38 if ext != '.dts':
39 return fname
40
41 dts_input = tools.GetOutputFilename('source.dts')
42 dtb_output = tools.GetOutputFilename('source.dtb')
43
44 search_paths = [os.path.join(os.getcwd(), 'include')]
45 root, _ = os.path.splitext(fname)
46 args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__']
47 args += ['-Ulinux']
48 for path in search_paths:
49 args.extend(['-I', path])
50 args += ['-o', dts_input, fname]
51 command.Run('cc', *args)
52
53 # If we don't have a directory, put it in the tools tempdir
54 search_list = []
55 for path in search_paths:
56 search_list.extend(['-i', path])
57 args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb']
58 args.extend(search_list)
59 args.append(dts_input)
60 command.Run('dtc', *args)
61 return dtb_output
Simon Glass8f224b32016-07-25 18:59:18 -060062
63def GetInt(node, propname, default=None):
64 prop = node.props.get(propname)
65 if not prop:
66 return default
67 value = fdt32_to_cpu(prop.value)
68 if type(value) == type(list):
69 raise ValueError("Node '%s' property '%' has list value: expecting"
70 "a single integer" % (node.name, propname))
71 return value
72
73def GetString(node, propname, default=None):
74 prop = node.props.get(propname)
75 if not prop:
76 return default
77 value = prop.value
78 if type(value) == type(list):
79 raise ValueError("Node '%s' property '%' has list value: expecting"
80 "a single string" % (node.name, propname))
81 return value
82
83def GetBool(node, propname, default=False):
84 if propname in node.props:
85 return True
86 return default