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 | fa8974d | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 16 | def 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 | """ |
Paul Burton | 307f1fa | 2016-09-27 16:03:57 +0100 | [diff] [blame] | 25 | if sys.version_info > (3, 0): |
George McCollister | 77bb05e | 2017-03-30 09:44:25 -0500 | [diff] [blame] | 26 | if isinstance(val, bytes): |
| 27 | val = val.decode('utf-8') |
Paul Burton | 307f1fa | 2016-09-27 16:03:57 +0100 | [diff] [blame] | 28 | val = val.encode('raw_unicode_escape') |
Simon Glass | 29462c3 | 2016-07-25 18:59:17 -0600 | [diff] [blame] | 29 | return struct.unpack('>I', val)[0] |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 30 | |
Simon Glass | fc3ae9c | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 31 | def fdt_cells_to_cpu(val, cells): |
| 32 | """Convert one or two cells to a long integer |
| 33 | |
| 34 | Args: |
| 35 | Value to convert (array of one or more 4-character strings) |
| 36 | |
| 37 | Return: |
| 38 | A native-endian long value |
| 39 | """ |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 40 | if not cells: |
| 41 | return 0 |
Simon Glass | fc3ae9c | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 42 | out = long(fdt32_to_cpu(val[0])) |
| 43 | if cells == 2: |
| 44 | out = out << 32 | fdt32_to_cpu(val[1]) |
| 45 | return out |
| 46 | |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 47 | def EnsureCompiled(fname): |
| 48 | """Compile an fdt .dts source file into a .dtb binary blob if needed. |
| 49 | |
| 50 | Args: |
| 51 | fname: Filename (if .dts it will be compiled). It not it will be |
| 52 | left alone |
| 53 | |
| 54 | Returns: |
| 55 | Filename of resulting .dtb file |
| 56 | """ |
| 57 | _, ext = os.path.splitext(fname) |
| 58 | if ext != '.dts': |
| 59 | return fname |
| 60 | |
| 61 | dts_input = tools.GetOutputFilename('source.dts') |
| 62 | dtb_output = tools.GetOutputFilename('source.dtb') |
| 63 | |
| 64 | search_paths = [os.path.join(os.getcwd(), 'include')] |
| 65 | root, _ = os.path.splitext(fname) |
| 66 | args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__'] |
| 67 | args += ['-Ulinux'] |
| 68 | for path in search_paths: |
| 69 | args.extend(['-I', path]) |
| 70 | args += ['-o', dts_input, fname] |
| 71 | command.Run('cc', *args) |
| 72 | |
| 73 | # If we don't have a directory, put it in the tools tempdir |
| 74 | search_list = [] |
| 75 | for path in search_paths: |
| 76 | search_list.extend(['-i', path]) |
Simon Glass | c5c7b1a | 2017-11-12 21:52:09 -0700 | [diff] [blame] | 77 | args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb', |
| 78 | '-W', 'no-unit_address_vs_reg'] |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 79 | args.extend(search_list) |
| 80 | args.append(dts_input) |
Simon Glass | ade2ef6 | 2017-12-24 12:12:07 -0700 | [diff] [blame] | 81 | dtc = os.environ.get('DTC') or 'dtc' |
| 82 | command.Run(dtc, *args) |
Simon Glass | efc9bf6 | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 83 | return dtb_output |
Simon Glass | 4066b31 | 2016-07-25 18:59:18 -0600 | [diff] [blame] | 84 | |
| 85 | def GetInt(node, propname, default=None): |
| 86 | prop = node.props.get(propname) |
| 87 | if not prop: |
| 88 | return default |
| 89 | value = fdt32_to_cpu(prop.value) |
| 90 | if type(value) == type(list): |
| 91 | raise ValueError("Node '%s' property '%' has list value: expecting" |
| 92 | "a single integer" % (node.name, propname)) |
| 93 | return value |
| 94 | |
| 95 | def GetString(node, propname, default=None): |
| 96 | prop = node.props.get(propname) |
| 97 | if not prop: |
| 98 | return default |
| 99 | value = prop.value |
| 100 | if type(value) == type(list): |
| 101 | raise ValueError("Node '%s' property '%' has list value: expecting" |
| 102 | "a single string" % (node.name, propname)) |
| 103 | return value |
| 104 | |
| 105 | def GetBool(node, propname, default=False): |
| 106 | if propname in node.props: |
| 107 | return True |
| 108 | return default |