xref: /openbmc/u-boot/tools/dtoc/fdt_util.py (revision 12308b12)
1#!/usr/bin/python
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Copyright (C) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
7
8import os
9import struct
10import sys
11import tempfile
12
13import command
14import tools
15
16VERSION3 = 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
24def 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    """
33    if VERSION3:
34        # This code is not reached in Python 2
35        val = get_plain_bytes(val)  # pragma: no cover
36    return struct.unpack('>I', val)[0]
37
38def 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    """
47    if not cells:
48        return 0
49    out = long(fdt32_to_cpu(val[0]))
50    if cells == 2:
51        out = out << 32 | fdt32_to_cpu(val[1])
52    return out
53
54def EnsureCompiled(fname, capture_stderr=False):
55    """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])
84    args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb',
85            '-W', 'no-unit_address_vs_reg']
86    args.extend(search_list)
87    args.append(dts_input)
88    dtc = os.environ.get('DTC') or 'dtc'
89    command.Run(dtc, *args, capture_stderr=capture_stderr)
90    return dtb_output
91
92def GetInt(node, propname, default=None):
93    prop = node.props.get(propname)
94    if not prop:
95        return default
96    if isinstance(prop.value, list):
97        raise ValueError("Node '%s' property '%s' has list value: expecting "
98                         "a single integer" % (node.name, propname))
99    value = fdt32_to_cpu(prop.value)
100    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
107    if isinstance(value, list):
108        raise ValueError("Node '%s' property '%s' has list value: expecting "
109                         "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
116