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 | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 8 | import os |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 9 | import struct |
Paul Burton | 307f1fa | 2016-09-27 16:03:57 +0100 | [diff] [blame] | 10 | import sys |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 11 | import tempfile |
| 12 | |
| 13 | import command |
| 14 | import tools |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 15 | |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 16 | VERSION3 = sys.version_info > (3, 0) |
| 17 | |
| 18 | def 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 | |
Simon Glass | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 24 | def 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 | """ |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 33 | if VERSION3: |
| 34 | # This code is not reached in Python 2 |
| 35 | val = get_plain_bytes(val) # pragma: no cover |
Simon Glass | 29462c3 | 2016-07-25 18:59:17 -0600 | [diff] [blame] | 36 | return struct.unpack('>I', val)[0] |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 37 | |
Simon Glass | fc3ae9c | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 38 | def 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 | """ |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 47 | if not cells: |
| 48 | return 0 |
Simon Glass | fc3ae9c | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 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 | |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 54 | def EnsureCompiled(fname, capture_stderr=False): |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 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]) |
Simon Glass | c5c7b1a | 2017-11-12 21:52:09 -0700 | [diff] [blame] | 84 | args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb', |
| 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 | ade2ef6 | 2017-12-24 12:12:07 -0700 | [diff] [blame] | 88 | dtc = os.environ.get('DTC') or 'dtc' |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 89 | command.Run(dtc, *args, capture_stderr=capture_stderr) |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 90 | return dtb_output |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 91 | |
| 92 | def GetInt(node, propname, default=None): |
| 93 | prop = node.props.get(propname) |
| 94 | if not prop: |
| 95 | return default |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 96 | if isinstance(prop.value, list): |
| 97 | raise ValueError("Node '%s' property '%s' has list value: expecting " |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 98 | "a single integer" % (node.name, propname)) |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 99 | value = fdt32_to_cpu(prop.value) |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 100 | return value |
| 101 | |
| 102 | def GetString(node, propname, default=None): |
| 103 | prop = node.props.get(propname) |
| 104 | if not prop: |
| 105 | return default |
| 106 | value = prop.value |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 107 | if isinstance(value, list): |
| 108 | raise ValueError("Node '%s' property '%s' has list value: expecting " |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 109 | "a single string" % (node.name, propname)) |
| 110 | return value |
| 111 | |
| 112 | def GetBool(node, propname, default=False): |
| 113 | if propname in node.props: |
| 114 | return True |
| 115 | return default |