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 | |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 16 | from patman import command |
| 17 | from patman import tools |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 18 | |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 19 | def fdt32_to_cpu(val): |
| 20 | """Convert a device tree cell to an integer |
| 21 | |
| 22 | Args: |
| 23 | Value to convert (4-character string representing the cell value) |
| 24 | |
| 25 | Return: |
| 26 | A native-endian integer value |
| 27 | """ |
Simon Glass | 29462c3 | 2016-07-25 18:59:17 -0600 | [diff] [blame] | 28 | return struct.unpack('>I', val)[0] |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 29 | |
Simon Glass | fc3ae9c | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 30 | def fdt_cells_to_cpu(val, cells): |
| 31 | """Convert one or two cells to a long integer |
| 32 | |
| 33 | Args: |
| 34 | Value to convert (array of one or more 4-character strings) |
| 35 | |
| 36 | Return: |
Simon Glass | c69b450 | 2019-05-17 22:00:42 -0600 | [diff] [blame] | 37 | A native-endian integer value |
Simon Glass | fc3ae9c | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 38 | """ |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 39 | if not cells: |
| 40 | return 0 |
Simon Glass | c69b450 | 2019-05-17 22:00:42 -0600 | [diff] [blame] | 41 | out = int(fdt32_to_cpu(val[0])) |
Simon Glass | fc3ae9c | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 42 | if cells == 2: |
| 43 | out = out << 32 | fdt32_to_cpu(val[1]) |
| 44 | return out |
| 45 | |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 46 | def EnsureCompiled(fname, tmpdir=None, capture_stderr=False): |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 47 | """Compile an fdt .dts source file into a .dtb binary blob if needed. |
| 48 | |
| 49 | Args: |
| 50 | fname: Filename (if .dts it will be compiled). It not it will be |
| 51 | left alone |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 52 | tmpdir: Temporary directory for output files, or None to use the |
| 53 | tools-module output directory |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 54 | |
| 55 | Returns: |
| 56 | Filename of resulting .dtb file |
| 57 | """ |
| 58 | _, ext = os.path.splitext(fname) |
| 59 | if ext != '.dts': |
| 60 | return fname |
| 61 | |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 62 | if tmpdir: |
| 63 | dts_input = os.path.join(tmpdir, 'source.dts') |
| 64 | dtb_output = os.path.join(tmpdir, 'source.dtb') |
| 65 | else: |
| 66 | dts_input = tools.GetOutputFilename('source.dts') |
| 67 | dtb_output = tools.GetOutputFilename('source.dtb') |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 68 | |
| 69 | search_paths = [os.path.join(os.getcwd(), 'include')] |
| 70 | root, _ = os.path.splitext(fname) |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 71 | cc, args = tools.GetTargetCompileTool('cc') |
| 72 | args += ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__'] |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 73 | args += ['-Ulinux'] |
| 74 | for path in search_paths: |
| 75 | args.extend(['-I', path]) |
| 76 | args += ['-o', dts_input, fname] |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 77 | command.Run(cc, *args) |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 78 | |
| 79 | # If we don't have a directory, put it in the tools tempdir |
| 80 | search_list = [] |
| 81 | for path in search_paths: |
| 82 | search_list.extend(['-i', path]) |
Alper Nebi Yasak | 5cd321d | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 83 | dtc, args = tools.GetTargetCompileTool('dtc') |
| 84 | args += ['-I', 'dts', '-o', dtb_output, '-O', 'dtb', |
Simon Glass | c5c7b1a | 2017-11-12 21:52:09 -0700 | [diff] [blame] | 85 | '-W', 'no-unit_address_vs_reg'] |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 86 | args.extend(search_list) |
| 87 | args.append(dts_input) |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 88 | command.Run(dtc, *args, capture_stderr=capture_stderr) |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 89 | return dtb_output |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 90 | |
| 91 | def GetInt(node, propname, default=None): |
Simon Glass | fe599c7 | 2018-07-17 13:25:31 -0600 | [diff] [blame] | 92 | """Get an integer from a property |
| 93 | |
| 94 | Args: |
| 95 | node: Node object to read from |
| 96 | propname: property name to read |
| 97 | default: Default value to use if the node/property do not exist |
| 98 | |
| 99 | Returns: |
| 100 | Integer value read, or default if none |
| 101 | """ |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 102 | prop = node.props.get(propname) |
| 103 | if not prop: |
| 104 | return default |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 105 | if isinstance(prop.value, list): |
| 106 | raise ValueError("Node '%s' property '%s' has list value: expecting " |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 107 | "a single integer" % (node.name, propname)) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 108 | value = fdt32_to_cpu(prop.value) |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 109 | return value |
| 110 | |
| 111 | def GetString(node, propname, default=None): |
Simon Glass | fe599c7 | 2018-07-17 13:25:31 -0600 | [diff] [blame] | 112 | """Get a string from a property |
| 113 | |
| 114 | Args: |
| 115 | node: Node object to read from |
| 116 | propname: property name to read |
| 117 | default: Default value to use if the node/property do not exist |
| 118 | |
| 119 | Returns: |
| 120 | String value read, or default if none |
| 121 | """ |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 122 | prop = node.props.get(propname) |
| 123 | if not prop: |
| 124 | return default |
| 125 | value = prop.value |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 126 | if isinstance(value, list): |
| 127 | raise ValueError("Node '%s' property '%s' has list value: expecting " |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 128 | "a single string" % (node.name, propname)) |
| 129 | return value |
| 130 | |
| 131 | def GetBool(node, propname, default=False): |
Simon Glass | fe599c7 | 2018-07-17 13:25:31 -0600 | [diff] [blame] | 132 | """Get an boolean from a property |
| 133 | |
| 134 | Args: |
| 135 | node: Node object to read from |
| 136 | propname: property name to read |
| 137 | default: Default value to use if the node/property do not exist |
| 138 | |
| 139 | Returns: |
| 140 | Boolean value read, or default if none (if you set this to True the |
| 141 | function will always return True) |
| 142 | """ |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 143 | if propname in node.props: |
| 144 | return True |
| 145 | return default |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 146 | |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 147 | def GetByte(node, propname, default=None): |
| 148 | """Get an byte from a property |
| 149 | |
| 150 | Args: |
| 151 | node: Node object to read from |
| 152 | propname: property name to read |
| 153 | default: Default value to use if the node/property do not exist |
| 154 | |
| 155 | Returns: |
| 156 | Byte value read, or default if none |
| 157 | """ |
| 158 | prop = node.props.get(propname) |
| 159 | if not prop: |
| 160 | return default |
| 161 | value = prop.value |
| 162 | if isinstance(value, list): |
| 163 | raise ValueError("Node '%s' property '%s' has list value: expecting " |
| 164 | "a single byte" % (node.name, propname)) |
| 165 | if len(value) != 1: |
| 166 | raise ValueError("Node '%s' property '%s' has length %d, expecting %d" % |
| 167 | (node.name, propname, len(value), 1)) |
| 168 | return ord(value[0]) |
| 169 | |
Simon Glass | e2d6528 | 2018-07-17 13:25:46 -0600 | [diff] [blame] | 170 | def GetPhandleList(node, propname): |
| 171 | """Get a list of phandles from a property |
| 172 | |
| 173 | Args: |
| 174 | node: Node object to read from |
| 175 | propname: property name to read |
| 176 | |
| 177 | Returns: |
| 178 | List of phandles read, each an integer |
| 179 | """ |
| 180 | prop = node.props.get(propname) |
| 181 | if not prop: |
| 182 | return None |
| 183 | value = prop.value |
| 184 | if not isinstance(value, list): |
| 185 | value = [value] |
| 186 | return [fdt32_to_cpu(v) for v in value] |
| 187 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 188 | def GetDatatype(node, propname, datatype): |
| 189 | """Get a value of a given type from a property |
| 190 | |
| 191 | Args: |
| 192 | node: Node object to read from |
| 193 | propname: property name to read |
| 194 | datatype: Type to read (str or int) |
| 195 | |
| 196 | Returns: |
| 197 | value read, or None if none |
| 198 | |
| 199 | Raises: |
| 200 | ValueError if datatype is not str or int |
| 201 | """ |
| 202 | if datatype == str: |
| 203 | return GetString(node, propname) |
| 204 | elif datatype == int: |
| 205 | return GetInt(node, propname) |
| 206 | raise ValueError("fdt_util internal error: Unknown data type '%s'" % |
| 207 | datatype) |