Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (C) 2016 Google, Inc |
| 4 | # Written by Simon Glass <sjg@chromium.org> |
| 5 | # |
| 6 | # SPDX-License-Identifier: GPL-2.0+ |
| 7 | # |
| 8 | |
Simon Glass | 355c67c | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 9 | import os |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 10 | import struct |
Paul Burton | c4c5f9e | 2016-09-27 16:03:57 +0100 | [diff] [blame] | 11 | import sys |
Simon Glass | 355c67c | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 12 | import tempfile |
| 13 | |
| 14 | import command |
| 15 | import tools |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 16 | |
Simon Glass | ec564b4 | 2016-07-04 11:58:08 -0600 | [diff] [blame] | 17 | def fdt32_to_cpu(val): |
| 18 | """Convert a device tree cell to an integer |
| 19 | |
| 20 | Args: |
| 21 | Value to convert (4-character string representing the cell value) |
| 22 | |
| 23 | Return: |
| 24 | A native-endian integer value |
| 25 | """ |
Paul Burton | c4c5f9e | 2016-09-27 16:03:57 +0100 | [diff] [blame] | 26 | if sys.version_info > (3, 0): |
George McCollister | f156b5b | 2017-03-30 09:44:25 -0500 | [diff] [blame] | 27 | if isinstance(val, bytes): |
| 28 | val = val.decode('utf-8') |
Paul Burton | c4c5f9e | 2016-09-27 16:03:57 +0100 | [diff] [blame] | 29 | val = val.encode('raw_unicode_escape') |
Simon Glass | 20024da | 2016-07-25 18:59:17 -0600 | [diff] [blame] | 30 | return struct.unpack('>I', val)[0] |
Simon Glass | 355c67c | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 31 | |
Simon Glass | fbdfd22 | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 32 | def fdt_cells_to_cpu(val, cells): |
| 33 | """Convert one or two cells to a long integer |
| 34 | |
| 35 | Args: |
| 36 | Value to convert (array of one or more 4-character strings) |
| 37 | |
| 38 | Return: |
| 39 | A native-endian long value |
| 40 | """ |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 41 | if not cells: |
| 42 | return 0 |
Simon Glass | fbdfd22 | 2017-08-29 14:15:48 -0600 | [diff] [blame] | 43 | out = long(fdt32_to_cpu(val[0])) |
| 44 | if cells == 2: |
| 45 | out = out << 32 | fdt32_to_cpu(val[1]) |
| 46 | return out |
| 47 | |
Simon Glass | 355c67c | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 48 | def EnsureCompiled(fname): |
| 49 | """Compile an fdt .dts source file into a .dtb binary blob if needed. |
| 50 | |
| 51 | Args: |
| 52 | fname: Filename (if .dts it will be compiled). It not it will be |
| 53 | left alone |
| 54 | |
| 55 | Returns: |
| 56 | Filename of resulting .dtb file |
| 57 | """ |
| 58 | _, ext = os.path.splitext(fname) |
| 59 | if ext != '.dts': |
| 60 | return fname |
| 61 | |
| 62 | dts_input = tools.GetOutputFilename('source.dts') |
| 63 | dtb_output = tools.GetOutputFilename('source.dtb') |
| 64 | |
| 65 | search_paths = [os.path.join(os.getcwd(), 'include')] |
| 66 | root, _ = os.path.splitext(fname) |
| 67 | args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__'] |
| 68 | args += ['-Ulinux'] |
| 69 | for path in search_paths: |
| 70 | args.extend(['-I', path]) |
| 71 | args += ['-o', dts_input, fname] |
| 72 | command.Run('cc', *args) |
| 73 | |
| 74 | # If we don't have a directory, put it in the tools tempdir |
| 75 | search_list = [] |
| 76 | for path in search_paths: |
| 77 | search_list.extend(['-i', path]) |
Simon Glass | d09682e | 2017-11-12 21:52:09 -0700 | [diff] [blame] | 78 | args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb', |
| 79 | '-W', 'no-unit_address_vs_reg'] |
Simon Glass | 355c67c | 2016-07-25 18:59:10 -0600 | [diff] [blame] | 80 | args.extend(search_list) |
| 81 | args.append(dts_input) |
| 82 | command.Run('dtc', *args) |
| 83 | return dtb_output |
Simon Glass | 8f224b3 | 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 |