Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 3 | # |
| 4 | # Copyright (C) 2016 Google, Inc |
| 5 | # Written by Simon Glass <sjg@chromium.org> |
| 6 | # |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 7 | |
Simon Glass | fe599c7 | 2018-07-17 13:25:31 -0600 | [diff] [blame] | 8 | # Utility functions for reading from a device tree. Once the upstream pylibfdt |
| 9 | # implementation advances far enough, we should be able to drop these. |
| 10 | |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 11 | import os |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 12 | import struct |
Paul Burton | 307f1fa | 2016-09-27 16:03:57 +0100 | [diff] [blame] | 13 | import sys |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 14 | import tempfile |
| 15 | |
| 16 | import command |
| 17 | import tools |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 18 | |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 19 | VERSION3 = sys.version_info > (3, 0) |
| 20 | |
| 21 | def get_plain_bytes(val): |
| 22 | """Handle Python 3 strings""" |
| 23 | if isinstance(val, bytes): |
| 24 | val = val.decode('utf-8') |
| 25 | return val.encode('raw_unicode_escape') |
| 26 | |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 27 | def fdt32_to_cpu(val): |
| 28 | """Convert a device tree cell to an integer |
| 29 | |
| 30 | Args: |
| 31 | Value to convert (4-character string representing the cell value) |
| 32 | |
| 33 | Return: |
| 34 | A native-endian integer value |
| 35 | """ |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 36 | if VERSION3: |
| 37 | # This code is not reached in Python 2 |
| 38 | val = get_plain_bytes(val) # pragma: no cover |
Simon Glass | 29462c3 | 2016-07-25 18:59:17 -0600 | [diff] [blame] | 39 | return struct.unpack('>I', val)[0] |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 40 | |
Simon Glass | fc3ae9c | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 41 | def fdt_cells_to_cpu(val, cells): |
| 42 | """Convert one or two cells to a long integer |
| 43 | |
| 44 | Args: |
| 45 | Value to convert (array of one or more 4-character strings) |
| 46 | |
| 47 | Return: |
| 48 | A native-endian long value |
| 49 | """ |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 50 | if not cells: |
| 51 | return 0 |
Simon Glass | fc3ae9c | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 52 | out = long(fdt32_to_cpu(val[0])) |
| 53 | if cells == 2: |
| 54 | out = out << 32 | fdt32_to_cpu(val[1]) |
| 55 | return out |
| 56 | |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 57 | def EnsureCompiled(fname, capture_stderr=False): |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 58 | """Compile an fdt .dts source file into a .dtb binary blob if needed. |
| 59 | |
| 60 | Args: |
| 61 | fname: Filename (if .dts it will be compiled). It not it will be |
| 62 | left alone |
| 63 | |
| 64 | Returns: |
| 65 | Filename of resulting .dtb file |
| 66 | """ |
| 67 | _, ext = os.path.splitext(fname) |
| 68 | if ext != '.dts': |
| 69 | return fname |
| 70 | |
| 71 | dts_input = tools.GetOutputFilename('source.dts') |
| 72 | dtb_output = tools.GetOutputFilename('source.dtb') |
| 73 | |
| 74 | search_paths = [os.path.join(os.getcwd(), 'include')] |
| 75 | root, _ = os.path.splitext(fname) |
| 76 | args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__'] |
| 77 | args += ['-Ulinux'] |
| 78 | for path in search_paths: |
| 79 | args.extend(['-I', path]) |
| 80 | args += ['-o', dts_input, fname] |
| 81 | command.Run('cc', *args) |
| 82 | |
| 83 | # If we don't have a directory, put it in the tools tempdir |
| 84 | search_list = [] |
| 85 | for path in search_paths: |
| 86 | search_list.extend(['-i', path]) |
Simon Glass | c5c7b1a | 2017-11-12 21:52:09 -0700 | [diff] [blame] | 87 | args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb', |
| 88 | '-W', 'no-unit_address_vs_reg'] |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 89 | args.extend(search_list) |
| 90 | args.append(dts_input) |
Simon Glass | ade2ef6 | 2017-12-24 12:12:07 -0700 | [diff] [blame] | 91 | dtc = os.environ.get('DTC') or 'dtc' |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 92 | command.Run(dtc, *args, capture_stderr=capture_stderr) |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 93 | return dtb_output |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 94 | |
| 95 | def GetInt(node, propname, default=None): |
Simon Glass | fe599c7 | 2018-07-17 13:25:31 -0600 | [diff] [blame] | 96 | """Get an integer from a property |
| 97 | |
| 98 | Args: |
| 99 | node: Node object to read from |
| 100 | propname: property name to read |
| 101 | default: Default value to use if the node/property do not exist |
| 102 | |
| 103 | Returns: |
| 104 | Integer value read, or default if none |
| 105 | """ |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 106 | prop = node.props.get(propname) |
| 107 | if not prop: |
| 108 | return default |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 109 | if isinstance(prop.value, list): |
| 110 | raise ValueError("Node '%s' property '%s' has list value: expecting " |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 111 | "a single integer" % (node.name, propname)) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 112 | value = fdt32_to_cpu(prop.value) |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 113 | return value |
| 114 | |
| 115 | def GetString(node, propname, default=None): |
Simon Glass | fe599c7 | 2018-07-17 13:25:31 -0600 | [diff] [blame] | 116 | """Get a string from a property |
| 117 | |
| 118 | Args: |
| 119 | node: Node object to read from |
| 120 | propname: property name to read |
| 121 | default: Default value to use if the node/property do not exist |
| 122 | |
| 123 | Returns: |
| 124 | String value read, or default if none |
| 125 | """ |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 126 | prop = node.props.get(propname) |
| 127 | if not prop: |
| 128 | return default |
| 129 | value = prop.value |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 130 | if isinstance(value, list): |
| 131 | raise ValueError("Node '%s' property '%s' has list value: expecting " |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 132 | "a single string" % (node.name, propname)) |
| 133 | return value |
| 134 | |
| 135 | def GetBool(node, propname, default=False): |
Simon Glass | fe599c7 | 2018-07-17 13:25:31 -0600 | [diff] [blame] | 136 | """Get an boolean from a property |
| 137 | |
| 138 | Args: |
| 139 | node: Node object to read from |
| 140 | propname: property name to read |
| 141 | default: Default value to use if the node/property do not exist |
| 142 | |
| 143 | Returns: |
| 144 | Boolean value read, or default if none (if you set this to True the |
| 145 | function will always return True) |
| 146 | """ |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 147 | if propname in node.props: |
| 148 | return True |
| 149 | return default |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame^] | 150 | |
| 151 | def GetDatatype(node, propname, datatype): |
| 152 | """Get a value of a given type from a property |
| 153 | |
| 154 | Args: |
| 155 | node: Node object to read from |
| 156 | propname: property name to read |
| 157 | datatype: Type to read (str or int) |
| 158 | |
| 159 | Returns: |
| 160 | value read, or None if none |
| 161 | |
| 162 | Raises: |
| 163 | ValueError if datatype is not str or int |
| 164 | """ |
| 165 | if datatype == str: |
| 166 | return GetString(node, propname) |
| 167 | elif datatype == int: |
| 168 | return GetInt(node, propname) |
| 169 | raise ValueError("fdt_util internal error: Unknown data type '%s'" % |
| 170 | datatype) |