Simon Glass | bfb0bb2 | 2019-10-31 07:42:55 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. |
| 4 | # |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 5 | |
| 6 | """Tests for the dtb_platdata module |
| 7 | |
Simon Glass | 70cd0d7 | 2018-07-06 10:27:20 -0600 | [diff] [blame] | 8 | This includes unit tests for some functions and functional tests for the dtoc |
| 9 | tool. |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 10 | """ |
| 11 | |
| 12 | import collections |
| 13 | import os |
| 14 | import struct |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 15 | import sys |
Walter Lozano | d82062b | 2020-07-28 19:06:23 -0300 | [diff] [blame] | 16 | import tempfile |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 17 | import unittest |
| 18 | |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 19 | from dtoc import dtb_platdata |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 20 | from dtb_platdata import conv_name_to_c |
| 21 | from dtb_platdata import get_compat_name |
| 22 | from dtb_platdata import get_value |
| 23 | from dtb_platdata import tab_to |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 24 | from dtoc import fdt |
| 25 | from dtoc import fdt_util |
| 26 | from patman import test_util |
| 27 | from patman import tools |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 28 | |
| 29 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 30 | |
| 31 | |
Simon Glass | eb37e2d | 2017-11-12 21:52:17 -0700 | [diff] [blame] | 32 | HEADER = '''/* |
| 33 | * DO NOT MODIFY |
| 34 | * |
| 35 | * This file was generated by dtoc from a .dtb (device tree binary) file. |
| 36 | */ |
| 37 | |
| 38 | #include <stdbool.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 39 | #include <linux/libfdt.h>''' |
Simon Glass | eb37e2d | 2017-11-12 21:52:17 -0700 | [diff] [blame] | 40 | |
| 41 | C_HEADER = '''/* |
| 42 | * DO NOT MODIFY |
| 43 | * |
| 44 | * This file was generated by dtoc from a .dtb (device tree binary) file. |
| 45 | */ |
| 46 | |
| 47 | #include <common.h> |
| 48 | #include <dm.h> |
| 49 | #include <dt-structs.h> |
| 50 | ''' |
| 51 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 52 | C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) { |
| 53 | } |
| 54 | ''' |
Simon Glass | eb37e2d | 2017-11-12 21:52:17 -0700 | [diff] [blame] | 55 | |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 56 | |
| 57 | def get_dtb_file(dts_fname, capture_stderr=False): |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 58 | """Compile a .dts file to a .dtb |
| 59 | |
| 60 | Args: |
| 61 | dts_fname: Filename of .dts file in the current directory |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 62 | capture_stderr: True to capture and discard stderr output |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 63 | |
| 64 | Returns: |
| 65 | Filename of compiled file in output directory |
| 66 | """ |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 67 | return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname), |
| 68 | capture_stderr=capture_stderr) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 69 | |
| 70 | |
| 71 | class TestDtoc(unittest.TestCase): |
| 72 | """Tests for dtoc""" |
| 73 | @classmethod |
| 74 | def setUpClass(cls): |
| 75 | tools.PrepareOutputDir(None) |
Simon Glass | 7f5e226 | 2020-07-07 21:32:06 -0600 | [diff] [blame] | 76 | cls.maxDiff = None |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 77 | |
| 78 | @classmethod |
| 79 | def tearDownClass(cls): |
| 80 | tools._RemoveOutputDir() |
| 81 | |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 82 | def _WritePythonString(self, fname, data): |
| 83 | """Write a string with tabs expanded as done in this Python file |
| 84 | |
| 85 | Args: |
| 86 | fname: Filename to write to |
| 87 | data: Raw string to convert |
| 88 | """ |
| 89 | data = data.replace('\t', '\\t') |
| 90 | with open(fname, 'w') as fd: |
| 91 | fd.write(data) |
| 92 | |
| 93 | def _CheckStrings(self, expected, actual): |
| 94 | """Check that a string matches its expected value |
| 95 | |
| 96 | If the strings do not match, they are written to the /tmp directory in |
| 97 | the same Python format as is used here in the test. This allows for |
| 98 | easy comparison and update of the tests. |
| 99 | |
| 100 | Args: |
| 101 | expected: Expected string |
| 102 | actual: Actual string |
| 103 | """ |
| 104 | if expected != actual: |
| 105 | self._WritePythonString('/tmp/binman.expected', expected) |
| 106 | self._WritePythonString('/tmp/binman.actual', actual) |
Simon Glass | 61b88e5 | 2019-05-17 22:00:31 -0600 | [diff] [blame] | 107 | print('Failures written to /tmp/binman.{expected,actual}') |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 108 | self.assertEquals(expected, actual) |
| 109 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 110 | |
| 111 | def run_test(self, args, dtb_file, output): |
| 112 | dtb_platdata.run_steps(args, dtb_file, False, output, True) |
| 113 | |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 114 | def test_name(self): |
| 115 | """Test conversion of device tree names to C identifiers""" |
| 116 | self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12')) |
| 117 | self.assertEqual('vendor_clock_frequency', |
| 118 | conv_name_to_c('vendor,clock-frequency')) |
| 119 | self.assertEqual('rockchip_rk3399_sdhci_5_1', |
| 120 | conv_name_to_c('rockchip,rk3399-sdhci-5.1')) |
| 121 | |
| 122 | def test_tab_to(self): |
| 123 | """Test operation of tab_to() function""" |
| 124 | self.assertEqual('fred ', tab_to(0, 'fred')) |
| 125 | self.assertEqual('fred\t', tab_to(1, 'fred')) |
| 126 | self.assertEqual('fred was here ', tab_to(1, 'fred was here')) |
| 127 | self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here')) |
| 128 | self.assertEqual('exactly8 ', tab_to(1, 'exactly8')) |
| 129 | self.assertEqual('exactly8\t', tab_to(2, 'exactly8')) |
| 130 | |
| 131 | def test_get_value(self): |
| 132 | """Test operation of get_value() function""" |
| 133 | self.assertEqual('0x45', |
| 134 | get_value(fdt.TYPE_INT, struct.pack('>I', 0x45))) |
| 135 | self.assertEqual('0x45', |
| 136 | get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45))) |
| 137 | self.assertEqual('0x0', |
| 138 | get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45))) |
| 139 | self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test')) |
| 140 | self.assertEqual('true', get_value(fdt.TYPE_BOOL, None)) |
| 141 | |
| 142 | def test_get_compat_name(self): |
| 143 | """Test operation of get_compat_name() function""" |
| 144 | Prop = collections.namedtuple('Prop', ['value']) |
| 145 | Node = collections.namedtuple('Node', ['props']) |
| 146 | |
| 147 | prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1']) |
| 148 | node = Node({'compatible': prop}) |
Walter Lozano | 5fe734c | 2020-07-23 00:22:03 -0300 | [diff] [blame] | 149 | self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']), |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 150 | get_compat_name(node)) |
| 151 | |
| 152 | prop = Prop(['rockchip,rk3399-sdhci-5.1']) |
| 153 | node = Node({'compatible': prop}) |
Walter Lozano | 5fe734c | 2020-07-23 00:22:03 -0300 | [diff] [blame] | 154 | self.assertEqual((['rockchip_rk3399_sdhci_5_1']), |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 155 | get_compat_name(node)) |
| 156 | |
| 157 | prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third']) |
| 158 | node = Node({'compatible': prop}) |
Walter Lozano | 5fe734c | 2020-07-23 00:22:03 -0300 | [diff] [blame] | 159 | self.assertEqual((['rockchip_rk3399_sdhci_5_1', |
| 160 | 'arasan_sdhci_5_1', 'third']), |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 161 | get_compat_name(node)) |
| 162 | |
| 163 | def test_empty_file(self): |
| 164 | """Test output from a device tree file with no nodes""" |
| 165 | dtb_file = get_dtb_file('dtoc_test_empty.dts') |
| 166 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 167 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 168 | with open(output) as infile: |
| 169 | lines = infile.read().splitlines() |
Simon Glass | eb37e2d | 2017-11-12 21:52:17 -0700 | [diff] [blame] | 170 | self.assertEqual(HEADER.splitlines(), lines) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 171 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 172 | self.run_test(['platdata'], dtb_file, output) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 173 | with open(output) as infile: |
| 174 | lines = infile.read().splitlines() |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 175 | self.assertEqual(C_HEADER.splitlines() + [''] + |
| 176 | C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 177 | |
| 178 | def test_simple(self): |
| 179 | """Test output from some simple nodes with various types of data""" |
| 180 | dtb_file = get_dtb_file('dtoc_test_simple.dts') |
| 181 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 182 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 183 | with open(output) as infile: |
| 184 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 185 | self._CheckStrings(HEADER + ''' |
Simon Glass | 90e5f0a | 2017-08-29 14:15:51 -0600 | [diff] [blame] | 186 | struct dtd_sandbox_i2c_test { |
| 187 | }; |
| 188 | struct dtd_sandbox_pmic_test { |
| 189 | \tbool\t\tlow_power; |
| 190 | \tfdt64_t\t\treg[2]; |
| 191 | }; |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 192 | struct dtd_sandbox_spl_test { |
Simon Glass | 7f5e226 | 2020-07-07 21:32:06 -0600 | [diff] [blame] | 193 | \tconst char * acpi_name; |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 194 | \tbool\t\tboolval; |
| 195 | \tunsigned char\tbytearray[3]; |
| 196 | \tunsigned char\tbyteval; |
| 197 | \tfdt32_t\t\tintarray[4]; |
| 198 | \tfdt32_t\t\tintval; |
| 199 | \tunsigned char\tlongbytearray[9]; |
Simon Glass | 9c52633 | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 200 | \tunsigned char\tnotstring[5]; |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 201 | \tconst char *\tstringarray[3]; |
| 202 | \tconst char *\tstringval; |
| 203 | }; |
| 204 | struct dtd_sandbox_spl_test_2 { |
| 205 | }; |
| 206 | ''', data) |
| 207 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 208 | self.run_test(['platdata'], dtb_file, output) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 209 | with open(output) as infile: |
| 210 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 211 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 212 | /* Node /i2c@0 index 0 */ |
| 213 | static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = { |
| 214 | }; |
| 215 | U_BOOT_DEVICE(i2c_at_0) = { |
| 216 | \t.name\t\t= "sandbox_i2c_test", |
| 217 | \t.platdata\t= &dtv_i2c_at_0, |
| 218 | \t.platdata_size\t= sizeof(dtv_i2c_at_0), |
| 219 | }; |
| 220 | |
| 221 | /* Node /i2c@0/pmic@9 index 1 */ |
| 222 | static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = { |
| 223 | \t.low_power\t\t= true, |
| 224 | \t.reg\t\t\t= {0x9, 0x0}, |
| 225 | }; |
| 226 | U_BOOT_DEVICE(pmic_at_9) = { |
| 227 | \t.name\t\t= "sandbox_pmic_test", |
| 228 | \t.platdata\t= &dtv_pmic_at_9, |
| 229 | \t.platdata_size\t= sizeof(dtv_pmic_at_9), |
| 230 | }; |
| 231 | |
| 232 | /* Node /spl-test index 2 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 233 | static struct dtd_sandbox_spl_test dtv_spl_test = { |
Simon Glass | c82de56 | 2019-05-17 22:00:32 -0600 | [diff] [blame] | 234 | \t.boolval\t\t= true, |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 235 | \t.bytearray\t\t= {0x6, 0x0, 0x0}, |
| 236 | \t.byteval\t\t= 0x5, |
Simon Glass | c82de56 | 2019-05-17 22:00:32 -0600 | [diff] [blame] | 237 | \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0}, |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 238 | \t.intval\t\t\t= 0x1, |
Simon Glass | 131e0b0 | 2017-08-29 14:15:49 -0600 | [diff] [blame] | 239 | \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, |
| 240 | \t\t0x11}, |
Simon Glass | c82de56 | 2019-05-17 22:00:32 -0600 | [diff] [blame] | 241 | \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0}, |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 242 | \t.stringarray\t\t= {"multi-word", "message", ""}, |
Simon Glass | c82de56 | 2019-05-17 22:00:32 -0600 | [diff] [blame] | 243 | \t.stringval\t\t= "message", |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 244 | }; |
| 245 | U_BOOT_DEVICE(spl_test) = { |
| 246 | \t.name\t\t= "sandbox_spl_test", |
| 247 | \t.platdata\t= &dtv_spl_test, |
| 248 | \t.platdata_size\t= sizeof(dtv_spl_test), |
| 249 | }; |
| 250 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 251 | /* Node /spl-test2 index 3 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 252 | static struct dtd_sandbox_spl_test dtv_spl_test2 = { |
Simon Glass | 7f5e226 | 2020-07-07 21:32:06 -0600 | [diff] [blame] | 253 | \t.acpi_name\t\t= "\\\\_SB.GPO0", |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 254 | \t.bytearray\t\t= {0x1, 0x23, 0x34}, |
| 255 | \t.byteval\t\t= 0x8, |
Simon Glass | c82de56 | 2019-05-17 22:00:32 -0600 | [diff] [blame] | 256 | \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0}, |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 257 | \t.intval\t\t\t= 0x3, |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 258 | \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0, |
Simon Glass | 131e0b0 | 2017-08-29 14:15:49 -0600 | [diff] [blame] | 259 | \t\t0x0}, |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 260 | \t.stringarray\t\t= {"another", "multi-word", "message"}, |
Simon Glass | c82de56 | 2019-05-17 22:00:32 -0600 | [diff] [blame] | 261 | \t.stringval\t\t= "message2", |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 262 | }; |
| 263 | U_BOOT_DEVICE(spl_test2) = { |
| 264 | \t.name\t\t= "sandbox_spl_test", |
| 265 | \t.platdata\t= &dtv_spl_test2, |
| 266 | \t.platdata_size\t= sizeof(dtv_spl_test2), |
| 267 | }; |
| 268 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 269 | /* Node /spl-test3 index 4 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 270 | static struct dtd_sandbox_spl_test dtv_spl_test3 = { |
Simon Glass | 8034e4d | 2020-10-03 11:31:27 -0600 | [diff] [blame] | 271 | \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, |
| 272 | \t\t0x0}, |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 273 | \t.stringarray\t\t= {"one", "", ""}, |
| 274 | }; |
| 275 | U_BOOT_DEVICE(spl_test3) = { |
| 276 | \t.name\t\t= "sandbox_spl_test", |
| 277 | \t.platdata\t= &dtv_spl_test3, |
| 278 | \t.platdata_size\t= sizeof(dtv_spl_test3), |
| 279 | }; |
| 280 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 281 | /* Node /spl-test4 index 5 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 282 | static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = { |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 283 | }; |
| 284 | U_BOOT_DEVICE(spl_test4) = { |
| 285 | \t.name\t\t= "sandbox_spl_test_2", |
| 286 | \t.platdata\t= &dtv_spl_test4, |
| 287 | \t.platdata_size\t= sizeof(dtv_spl_test4), |
| 288 | }; |
| 289 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 290 | ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 291 | |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 292 | def test_driver_alias(self): |
| 293 | """Test output from a device tree file with a driver alias""" |
| 294 | dtb_file = get_dtb_file('dtoc_test_driver_alias.dts') |
| 295 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 296 | self.run_test(['struct'], dtb_file, output) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 297 | with open(output) as infile: |
| 298 | data = infile.read() |
| 299 | self._CheckStrings(HEADER + ''' |
| 300 | struct dtd_sandbox_gpio { |
| 301 | \tconst char *\tgpio_bank_name; |
| 302 | \tbool\t\tgpio_controller; |
| 303 | \tfdt32_t\t\tsandbox_gpio_count; |
| 304 | }; |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 305 | ''', data) |
| 306 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 307 | self.run_test(['platdata'], dtb_file, output) |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 308 | with open(output) as infile: |
| 309 | data = infile.read() |
| 310 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 311 | /* Node /gpios@0 index 0 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 312 | static struct dtd_sandbox_gpio dtv_gpios_at_0 = { |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 313 | \t.gpio_bank_name\t\t= "a", |
| 314 | \t.gpio_controller\t= true, |
| 315 | \t.sandbox_gpio_count\t= 0x14, |
| 316 | }; |
| 317 | U_BOOT_DEVICE(gpios_at_0) = { |
| 318 | \t.name\t\t= "sandbox_gpio", |
| 319 | \t.platdata\t= &dtv_gpios_at_0, |
| 320 | \t.platdata_size\t= sizeof(dtv_gpios_at_0), |
| 321 | }; |
| 322 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 323 | void dm_populate_phandle_data(void) { |
| 324 | } |
Walter Lozano | e675d96 | 2020-07-03 08:07:17 -0300 | [diff] [blame] | 325 | ''', data) |
| 326 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 327 | def test_invalid_driver(self): |
| 328 | """Test output from a device tree file with an invalid driver""" |
| 329 | dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts') |
| 330 | output = tools.GetOutputFilename('output') |
| 331 | with test_util.capture_sys_output() as (stdout, stderr): |
| 332 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 333 | with open(output) as infile: |
| 334 | data = infile.read() |
| 335 | self._CheckStrings(HEADER + ''' |
| 336 | struct dtd_invalid { |
| 337 | }; |
| 338 | ''', data) |
| 339 | |
| 340 | with test_util.capture_sys_output() as (stdout, stderr): |
| 341 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 342 | with open(output) as infile: |
| 343 | data = infile.read() |
| 344 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 345 | /* Node /spl-test index 0 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 346 | static struct dtd_invalid dtv_spl_test = { |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 347 | }; |
| 348 | U_BOOT_DEVICE(spl_test) = { |
| 349 | \t.name\t\t= "invalid", |
| 350 | \t.platdata\t= &dtv_spl_test, |
| 351 | \t.platdata_size\t= sizeof(dtv_spl_test), |
| 352 | }; |
| 353 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 354 | void dm_populate_phandle_data(void) { |
| 355 | } |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 356 | ''', data) |
| 357 | |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 358 | def test_phandle(self): |
| 359 | """Test output from a node containing a phandle reference""" |
| 360 | dtb_file = get_dtb_file('dtoc_test_phandle.dts') |
| 361 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 362 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 363 | with open(output) as infile: |
| 364 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 365 | self._CheckStrings(HEADER + ''' |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 366 | struct dtd_source { |
Simon Glass | 3deeb47 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 367 | \tstruct phandle_2_arg clocks[4]; |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 368 | }; |
| 369 | struct dtd_target { |
| 370 | \tfdt32_t\t\tintval; |
| 371 | }; |
| 372 | ''', data) |
| 373 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 374 | self.run_test(['platdata'], dtb_file, output) |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 375 | with open(output) as infile: |
| 376 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 377 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 378 | /* Node /phandle2-target index 0 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 379 | static struct dtd_target dtv_phandle2_target = { |
Simon Glass | 3deeb47 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 380 | \t.intval\t\t\t= 0x1, |
| 381 | }; |
| 382 | U_BOOT_DEVICE(phandle2_target) = { |
| 383 | \t.name\t\t= "target", |
| 384 | \t.platdata\t= &dtv_phandle2_target, |
| 385 | \t.platdata_size\t= sizeof(dtv_phandle2_target), |
| 386 | }; |
| 387 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 388 | /* Node /phandle3-target index 1 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 389 | static struct dtd_target dtv_phandle3_target = { |
Simon Glass | 3deeb47 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 390 | \t.intval\t\t\t= 0x2, |
| 391 | }; |
| 392 | U_BOOT_DEVICE(phandle3_target) = { |
| 393 | \t.name\t\t= "target", |
| 394 | \t.platdata\t= &dtv_phandle3_target, |
| 395 | \t.platdata_size\t= sizeof(dtv_phandle3_target), |
| 396 | }; |
| 397 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 398 | /* Node /phandle-target index 4 */ |
| 399 | static struct dtd_target dtv_phandle_target = { |
| 400 | \t.intval\t\t\t= 0x0, |
| 401 | }; |
| 402 | U_BOOT_DEVICE(phandle_target) = { |
| 403 | \t.name\t\t= "target", |
| 404 | \t.platdata\t= &dtv_phandle_target, |
| 405 | \t.platdata_size\t= sizeof(dtv_phandle_target), |
| 406 | }; |
| 407 | |
| 408 | /* Node /phandle-source index 2 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 409 | static struct dtd_source dtv_phandle_source = { |
Simon Glass | d0cd075 | 2017-08-29 14:15:57 -0600 | [diff] [blame] | 410 | \t.clocks\t\t\t= { |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 411 | \t\t\t{NULL, {}}, |
| 412 | \t\t\t{NULL, {11}}, |
| 413 | \t\t\t{NULL, {12, 13}}, |
| 414 | \t\t\t{NULL, {}},}, |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 415 | }; |
| 416 | U_BOOT_DEVICE(phandle_source) = { |
| 417 | \t.name\t\t= "source", |
| 418 | \t.platdata\t= &dtv_phandle_source, |
| 419 | \t.platdata_size\t= sizeof(dtv_phandle_source), |
| 420 | }; |
| 421 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 422 | /* Node /phandle-source2 index 3 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 423 | static struct dtd_source dtv_phandle_source2 = { |
Simon Glass | 609e2b1 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 424 | \t.clocks\t\t\t= { |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 425 | \t\t\t{NULL, {}},}, |
Simon Glass | 609e2b1 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 426 | }; |
| 427 | U_BOOT_DEVICE(phandle_source2) = { |
| 428 | \t.name\t\t= "source", |
| 429 | \t.platdata\t= &dtv_phandle_source2, |
| 430 | \t.platdata_size\t= sizeof(dtv_phandle_source2), |
| 431 | }; |
| 432 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 433 | void dm_populate_phandle_data(void) { |
| 434 | \tdtv_phandle_source.clocks[0].node = DM_GET_DEVICE(phandle_target); |
| 435 | \tdtv_phandle_source.clocks[1].node = DM_GET_DEVICE(phandle2_target); |
| 436 | \tdtv_phandle_source.clocks[2].node = DM_GET_DEVICE(phandle3_target); |
| 437 | \tdtv_phandle_source.clocks[3].node = DM_GET_DEVICE(phandle_target); |
| 438 | \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target); |
| 439 | } |
Simon Glass | 9d2eb92 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 440 | ''', data) |
| 441 | |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 442 | def test_phandle_single(self): |
| 443 | """Test output from a node containing a phandle reference""" |
| 444 | dtb_file = get_dtb_file('dtoc_test_phandle_single.dts') |
| 445 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 446 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 447 | with open(output) as infile: |
| 448 | data = infile.read() |
| 449 | self._CheckStrings(HEADER + ''' |
| 450 | struct dtd_source { |
| 451 | \tstruct phandle_0_arg clocks[1]; |
| 452 | }; |
| 453 | struct dtd_target { |
| 454 | \tfdt32_t\t\tintval; |
| 455 | }; |
| 456 | ''', data) |
| 457 | |
| 458 | def test_phandle_reorder(self): |
| 459 | """Test that phandle targets are generated before their references""" |
| 460 | dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts') |
| 461 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 462 | self.run_test(['platdata'], dtb_file, output) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 463 | with open(output) as infile: |
| 464 | data = infile.read() |
| 465 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 466 | /* Node /phandle-target index 1 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 467 | static struct dtd_target dtv_phandle_target = { |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 468 | }; |
| 469 | U_BOOT_DEVICE(phandle_target) = { |
| 470 | \t.name\t\t= "target", |
| 471 | \t.platdata\t= &dtv_phandle_target, |
| 472 | \t.platdata_size\t= sizeof(dtv_phandle_target), |
| 473 | }; |
| 474 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 475 | /* Node /phandle-source2 index 0 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 476 | static struct dtd_source dtv_phandle_source2 = { |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 477 | \t.clocks\t\t\t= { |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 478 | \t\t\t{NULL, {}},}, |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 479 | }; |
| 480 | U_BOOT_DEVICE(phandle_source2) = { |
| 481 | \t.name\t\t= "source", |
| 482 | \t.platdata\t= &dtv_phandle_source2, |
| 483 | \t.platdata_size\t= sizeof(dtv_phandle_source2), |
| 484 | }; |
| 485 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 486 | void dm_populate_phandle_data(void) { |
| 487 | \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target); |
| 488 | } |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 489 | ''', data) |
| 490 | |
Walter Lozano | 5541fc0 | 2020-06-25 01:10:17 -0300 | [diff] [blame] | 491 | def test_phandle_cd_gpio(self): |
| 492 | """Test that phandle targets are generated when unsing cd-gpios""" |
| 493 | dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts') |
| 494 | output = tools.GetOutputFilename('output') |
| 495 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True) |
| 496 | with open(output) as infile: |
| 497 | data = infile.read() |
| 498 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 499 | /* Node /phandle2-target index 0 */ |
Walter Lozano | 5541fc0 | 2020-06-25 01:10:17 -0300 | [diff] [blame] | 500 | static struct dtd_target dtv_phandle2_target = { |
| 501 | \t.intval\t\t\t= 0x1, |
| 502 | }; |
| 503 | U_BOOT_DEVICE(phandle2_target) = { |
| 504 | \t.name\t\t= "target", |
| 505 | \t.platdata\t= &dtv_phandle2_target, |
| 506 | \t.platdata_size\t= sizeof(dtv_phandle2_target), |
| 507 | }; |
| 508 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 509 | /* Node /phandle3-target index 1 */ |
Walter Lozano | 5541fc0 | 2020-06-25 01:10:17 -0300 | [diff] [blame] | 510 | static struct dtd_target dtv_phandle3_target = { |
| 511 | \t.intval\t\t\t= 0x2, |
| 512 | }; |
| 513 | U_BOOT_DEVICE(phandle3_target) = { |
| 514 | \t.name\t\t= "target", |
| 515 | \t.platdata\t= &dtv_phandle3_target, |
| 516 | \t.platdata_size\t= sizeof(dtv_phandle3_target), |
| 517 | }; |
| 518 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 519 | /* Node /phandle-target index 4 */ |
| 520 | static struct dtd_target dtv_phandle_target = { |
| 521 | \t.intval\t\t\t= 0x0, |
| 522 | }; |
| 523 | U_BOOT_DEVICE(phandle_target) = { |
| 524 | \t.name\t\t= "target", |
| 525 | \t.platdata\t= &dtv_phandle_target, |
| 526 | \t.platdata_size\t= sizeof(dtv_phandle_target), |
| 527 | }; |
| 528 | |
| 529 | /* Node /phandle-source index 2 */ |
Walter Lozano | 5541fc0 | 2020-06-25 01:10:17 -0300 | [diff] [blame] | 530 | static struct dtd_source dtv_phandle_source = { |
| 531 | \t.cd_gpios\t\t= { |
| 532 | \t\t\t{NULL, {}}, |
| 533 | \t\t\t{NULL, {11}}, |
| 534 | \t\t\t{NULL, {12, 13}}, |
| 535 | \t\t\t{NULL, {}},}, |
| 536 | }; |
| 537 | U_BOOT_DEVICE(phandle_source) = { |
| 538 | \t.name\t\t= "source", |
| 539 | \t.platdata\t= &dtv_phandle_source, |
| 540 | \t.platdata_size\t= sizeof(dtv_phandle_source), |
| 541 | }; |
| 542 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 543 | /* Node /phandle-source2 index 3 */ |
Walter Lozano | 5541fc0 | 2020-06-25 01:10:17 -0300 | [diff] [blame] | 544 | static struct dtd_source dtv_phandle_source2 = { |
| 545 | \t.cd_gpios\t\t= { |
| 546 | \t\t\t{NULL, {}},}, |
| 547 | }; |
| 548 | U_BOOT_DEVICE(phandle_source2) = { |
| 549 | \t.name\t\t= "source", |
| 550 | \t.platdata\t= &dtv_phandle_source2, |
| 551 | \t.platdata_size\t= sizeof(dtv_phandle_source2), |
| 552 | }; |
| 553 | |
| 554 | void dm_populate_phandle_data(void) { |
| 555 | \tdtv_phandle_source.cd_gpios[0].node = DM_GET_DEVICE(phandle_target); |
| 556 | \tdtv_phandle_source.cd_gpios[1].node = DM_GET_DEVICE(phandle2_target); |
| 557 | \tdtv_phandle_source.cd_gpios[2].node = DM_GET_DEVICE(phandle3_target); |
| 558 | \tdtv_phandle_source.cd_gpios[3].node = DM_GET_DEVICE(phandle_target); |
| 559 | \tdtv_phandle_source2.cd_gpios[0].node = DM_GET_DEVICE(phandle_target); |
| 560 | } |
| 561 | ''', data) |
| 562 | |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 563 | def test_phandle_bad(self): |
| 564 | """Test a node containing an invalid phandle fails""" |
Simon Glass | 0415002 | 2018-10-01 21:12:43 -0600 | [diff] [blame] | 565 | dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts', |
| 566 | capture_stderr=True) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 567 | output = tools.GetOutputFilename('output') |
| 568 | with self.assertRaises(ValueError) as e: |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 569 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 570 | self.assertIn("Cannot parse 'clocks' in node 'phandle-source'", |
| 571 | str(e.exception)) |
| 572 | |
| 573 | def test_phandle_bad2(self): |
| 574 | """Test a phandle target missing its #*-cells property""" |
Simon Glass | 0415002 | 2018-10-01 21:12:43 -0600 | [diff] [blame] | 575 | dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts', |
| 576 | capture_stderr=True) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 577 | output = tools.GetOutputFilename('output') |
| 578 | with self.assertRaises(ValueError) as e: |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 579 | self.run_test(['struct'], dtb_file, output) |
Walter Lozano | 179f0b6 | 2020-06-25 01:10:16 -0300 | [diff] [blame] | 580 | self.assertIn("Node 'phandle-target' has no cells property", |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 581 | str(e.exception)) |
| 582 | |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 583 | def test_addresses64(self): |
| 584 | """Test output from a node with a 'reg' property with na=2, ns=2""" |
| 585 | dtb_file = get_dtb_file('dtoc_test_addr64.dts') |
| 586 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 587 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 588 | with open(output) as infile: |
| 589 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 590 | self._CheckStrings(HEADER + ''' |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 591 | struct dtd_test1 { |
| 592 | \tfdt64_t\t\treg[2]; |
| 593 | }; |
| 594 | struct dtd_test2 { |
| 595 | \tfdt64_t\t\treg[2]; |
| 596 | }; |
| 597 | struct dtd_test3 { |
| 598 | \tfdt64_t\t\treg[4]; |
| 599 | }; |
| 600 | ''', data) |
| 601 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 602 | self.run_test(['platdata'], dtb_file, output) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 603 | with open(output) as infile: |
| 604 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 605 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 606 | /* Node /test1 index 0 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 607 | static struct dtd_test1 dtv_test1 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 608 | \t.reg\t\t\t= {0x1234, 0x5678}, |
| 609 | }; |
| 610 | U_BOOT_DEVICE(test1) = { |
| 611 | \t.name\t\t= "test1", |
| 612 | \t.platdata\t= &dtv_test1, |
| 613 | \t.platdata_size\t= sizeof(dtv_test1), |
| 614 | }; |
| 615 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 616 | /* Node /test2 index 1 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 617 | static struct dtd_test2 dtv_test2 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 618 | \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654}, |
| 619 | }; |
| 620 | U_BOOT_DEVICE(test2) = { |
| 621 | \t.name\t\t= "test2", |
| 622 | \t.platdata\t= &dtv_test2, |
| 623 | \t.platdata_size\t= sizeof(dtv_test2), |
| 624 | }; |
| 625 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 626 | /* Node /test3 index 2 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 627 | static struct dtd_test3 dtv_test3 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 628 | \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3}, |
| 629 | }; |
| 630 | U_BOOT_DEVICE(test3) = { |
| 631 | \t.name\t\t= "test3", |
| 632 | \t.platdata\t= &dtv_test3, |
| 633 | \t.platdata_size\t= sizeof(dtv_test3), |
| 634 | }; |
| 635 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 636 | ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 637 | |
| 638 | def test_addresses32(self): |
| 639 | """Test output from a node with a 'reg' property with na=1, ns=1""" |
| 640 | dtb_file = get_dtb_file('dtoc_test_addr32.dts') |
| 641 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 642 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 643 | with open(output) as infile: |
| 644 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 645 | self._CheckStrings(HEADER + ''' |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 646 | struct dtd_test1 { |
| 647 | \tfdt32_t\t\treg[2]; |
| 648 | }; |
| 649 | struct dtd_test2 { |
| 650 | \tfdt32_t\t\treg[4]; |
| 651 | }; |
| 652 | ''', data) |
| 653 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 654 | self.run_test(['platdata'], dtb_file, output) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 655 | with open(output) as infile: |
| 656 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 657 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 658 | /* Node /test1 index 0 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 659 | static struct dtd_test1 dtv_test1 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 660 | \t.reg\t\t\t= {0x1234, 0x5678}, |
| 661 | }; |
| 662 | U_BOOT_DEVICE(test1) = { |
| 663 | \t.name\t\t= "test1", |
| 664 | \t.platdata\t= &dtv_test1, |
| 665 | \t.platdata_size\t= sizeof(dtv_test1), |
| 666 | }; |
| 667 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 668 | /* Node /test2 index 1 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 669 | static struct dtd_test2 dtv_test2 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 670 | \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3}, |
| 671 | }; |
| 672 | U_BOOT_DEVICE(test2) = { |
| 673 | \t.name\t\t= "test2", |
| 674 | \t.platdata\t= &dtv_test2, |
| 675 | \t.platdata_size\t= sizeof(dtv_test2), |
| 676 | }; |
| 677 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 678 | ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 679 | |
| 680 | def test_addresses64_32(self): |
| 681 | """Test output from a node with a 'reg' property with na=2, ns=1""" |
| 682 | dtb_file = get_dtb_file('dtoc_test_addr64_32.dts') |
| 683 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 684 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 685 | with open(output) as infile: |
| 686 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 687 | self._CheckStrings(HEADER + ''' |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 688 | struct dtd_test1 { |
| 689 | \tfdt64_t\t\treg[2]; |
| 690 | }; |
| 691 | struct dtd_test2 { |
| 692 | \tfdt64_t\t\treg[2]; |
| 693 | }; |
| 694 | struct dtd_test3 { |
| 695 | \tfdt64_t\t\treg[4]; |
| 696 | }; |
| 697 | ''', data) |
| 698 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 699 | self.run_test(['platdata'], dtb_file, output) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 700 | with open(output) as infile: |
| 701 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 702 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 703 | /* Node /test1 index 0 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 704 | static struct dtd_test1 dtv_test1 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 705 | \t.reg\t\t\t= {0x123400000000, 0x5678}, |
| 706 | }; |
| 707 | U_BOOT_DEVICE(test1) = { |
| 708 | \t.name\t\t= "test1", |
| 709 | \t.platdata\t= &dtv_test1, |
| 710 | \t.platdata_size\t= sizeof(dtv_test1), |
| 711 | }; |
| 712 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 713 | /* Node /test2 index 1 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 714 | static struct dtd_test2 dtv_test2 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 715 | \t.reg\t\t\t= {0x1234567890123456, 0x98765432}, |
| 716 | }; |
| 717 | U_BOOT_DEVICE(test2) = { |
| 718 | \t.name\t\t= "test2", |
| 719 | \t.platdata\t= &dtv_test2, |
| 720 | \t.platdata_size\t= sizeof(dtv_test2), |
| 721 | }; |
| 722 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 723 | /* Node /test3 index 2 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 724 | static struct dtd_test3 dtv_test3 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 725 | \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3}, |
| 726 | }; |
| 727 | U_BOOT_DEVICE(test3) = { |
| 728 | \t.name\t\t= "test3", |
| 729 | \t.platdata\t= &dtv_test3, |
| 730 | \t.platdata_size\t= sizeof(dtv_test3), |
| 731 | }; |
| 732 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 733 | ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 734 | |
| 735 | def test_addresses32_64(self): |
| 736 | """Test output from a node with a 'reg' property with na=1, ns=2""" |
| 737 | dtb_file = get_dtb_file('dtoc_test_addr32_64.dts') |
| 738 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 739 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 740 | with open(output) as infile: |
| 741 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 742 | self._CheckStrings(HEADER + ''' |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 743 | struct dtd_test1 { |
| 744 | \tfdt64_t\t\treg[2]; |
| 745 | }; |
| 746 | struct dtd_test2 { |
| 747 | \tfdt64_t\t\treg[2]; |
| 748 | }; |
| 749 | struct dtd_test3 { |
| 750 | \tfdt64_t\t\treg[4]; |
| 751 | }; |
| 752 | ''', data) |
| 753 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 754 | self.run_test(['platdata'], dtb_file, output) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 755 | with open(output) as infile: |
| 756 | data = infile.read() |
Simon Glass | c47c2b3 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 757 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 758 | /* Node /test1 index 0 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 759 | static struct dtd_test1 dtv_test1 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 760 | \t.reg\t\t\t= {0x1234, 0x567800000000}, |
| 761 | }; |
| 762 | U_BOOT_DEVICE(test1) = { |
| 763 | \t.name\t\t= "test1", |
| 764 | \t.platdata\t= &dtv_test1, |
| 765 | \t.platdata_size\t= sizeof(dtv_test1), |
| 766 | }; |
| 767 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 768 | /* Node /test2 index 1 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 769 | static struct dtd_test2 dtv_test2 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 770 | \t.reg\t\t\t= {0x12345678, 0x9876543210987654}, |
| 771 | }; |
| 772 | U_BOOT_DEVICE(test2) = { |
| 773 | \t.name\t\t= "test2", |
| 774 | \t.platdata\t= &dtv_test2, |
| 775 | \t.platdata_size\t= sizeof(dtv_test2), |
| 776 | }; |
| 777 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 778 | /* Node /test3 index 2 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 779 | static struct dtd_test3 dtv_test3 = { |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 780 | \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3}, |
| 781 | }; |
| 782 | U_BOOT_DEVICE(test3) = { |
| 783 | \t.name\t\t= "test3", |
| 784 | \t.platdata\t= &dtv_test3, |
| 785 | \t.platdata_size\t= sizeof(dtv_test3), |
| 786 | }; |
| 787 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 788 | ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 789 | |
| 790 | def test_bad_reg(self): |
| 791 | """Test that a reg property with an invalid type generates an error""" |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 792 | # Capture stderr since dtc will emit warnings for this file |
| 793 | dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 794 | output = tools.GetOutputFilename('output') |
| 795 | with self.assertRaises(ValueError) as e: |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 796 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 797 | self.assertIn("Node 'spl-test' reg property is not an int", |
| 798 | str(e.exception)) |
| 799 | |
| 800 | def test_bad_reg2(self): |
| 801 | """Test that a reg property with an invalid cell count is detected""" |
Simon Glass | 3bce93d | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 802 | # Capture stderr since dtc will emit warnings for this file |
| 803 | dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 804 | output = tools.GetOutputFilename('output') |
| 805 | with self.assertRaises(ValueError) as e: |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 806 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 807 | self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)", |
| 808 | str(e.exception)) |
| 809 | |
| 810 | def test_add_prop(self): |
| 811 | """Test that a subequent node can add a new property to a struct""" |
| 812 | dtb_file = get_dtb_file('dtoc_test_add_prop.dts') |
| 813 | output = tools.GetOutputFilename('output') |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 814 | self.run_test(['struct'], dtb_file, output) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 815 | with open(output) as infile: |
| 816 | data = infile.read() |
| 817 | self._CheckStrings(HEADER + ''' |
| 818 | struct dtd_sandbox_spl_test { |
| 819 | \tfdt32_t\t\tintarray; |
| 820 | \tfdt32_t\t\tintval; |
| 821 | }; |
| 822 | ''', data) |
| 823 | |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 824 | self.run_test(['platdata'], dtb_file, output) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 825 | with open(output) as infile: |
| 826 | data = infile.read() |
| 827 | self._CheckStrings(C_HEADER + ''' |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 828 | /* Node /spl-test index 0 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 829 | static struct dtd_sandbox_spl_test dtv_spl_test = { |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 830 | \t.intval\t\t\t= 0x1, |
| 831 | }; |
| 832 | U_BOOT_DEVICE(spl_test) = { |
| 833 | \t.name\t\t= "sandbox_spl_test", |
| 834 | \t.platdata\t= &dtv_spl_test, |
| 835 | \t.platdata_size\t= sizeof(dtv_spl_test), |
| 836 | }; |
| 837 | |
Simon Glass | 192f813 | 2020-10-03 11:31:25 -0600 | [diff] [blame] | 838 | /* Node /spl-test2 index 1 */ |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 839 | static struct dtd_sandbox_spl_test dtv_spl_test2 = { |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 840 | \t.intarray\t\t= 0x5, |
| 841 | }; |
| 842 | U_BOOT_DEVICE(spl_test2) = { |
| 843 | \t.name\t\t= "sandbox_spl_test", |
| 844 | \t.platdata\t= &dtv_spl_test2, |
| 845 | \t.platdata_size\t= sizeof(dtv_spl_test2), |
| 846 | }; |
| 847 | |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 848 | ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 849 | |
| 850 | def testStdout(self): |
| 851 | """Test output to stdout""" |
| 852 | dtb_file = get_dtb_file('dtoc_test_simple.dts') |
| 853 | with test_util.capture_sys_output() as (stdout, stderr): |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 854 | self.run_test(['struct'], dtb_file, '-') |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 855 | |
| 856 | def testNoCommand(self): |
| 857 | """Test running dtoc without a command""" |
| 858 | with self.assertRaises(ValueError) as e: |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 859 | self.run_test([], '', '') |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 860 | self.assertIn("Please specify a command: struct, platdata", |
| 861 | str(e.exception)) |
| 862 | |
| 863 | def testBadCommand(self): |
| 864 | """Test running dtoc with an invalid command""" |
| 865 | dtb_file = get_dtb_file('dtoc_test_simple.dts') |
| 866 | output = tools.GetOutputFilename('output') |
| 867 | with self.assertRaises(ValueError) as e: |
Walter Lozano | a324e41 | 2020-06-25 01:10:08 -0300 | [diff] [blame] | 868 | self.run_test(['invalid-cmd'], dtb_file, output) |
Simon Glass | 961c1ce | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 869 | self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)", |
| 870 | str(e.exception)) |
Walter Lozano | d82062b | 2020-07-28 19:06:23 -0300 | [diff] [blame] | 871 | |
| 872 | def testScanDrivers(self): |
| 873 | """Test running dtoc with additional drivers to scan""" |
| 874 | dtb_file = get_dtb_file('dtoc_test_simple.dts') |
| 875 | output = tools.GetOutputFilename('output') |
| 876 | with test_util.capture_sys_output() as (stdout, stderr): |
| 877 | dtb_platdata.run_steps(['struct'], dtb_file, False, output, True, |
| 878 | [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx']) |
| 879 | |
| 880 | def testUnicodeError(self): |
| 881 | """Test running dtoc with an invalid unicode file |
| 882 | |
| 883 | To be able to perform this test without adding a weird text file which |
| 884 | would produce issues when using checkpatch.pl or patman, generate the |
| 885 | file at runtime and then process it. |
| 886 | """ |
| 887 | dtb_file = get_dtb_file('dtoc_test_simple.dts') |
| 888 | output = tools.GetOutputFilename('output') |
| 889 | driver_fn = '/tmp/' + next(tempfile._get_candidate_names()) |
| 890 | with open(driver_fn, 'wb+') as df: |
| 891 | df.write(b'\x81') |
| 892 | |
| 893 | with test_util.capture_sys_output() as (stdout, stderr): |
| 894 | dtb_platdata.run_steps(['struct'], dtb_file, False, output, True, |
| 895 | [driver_fn]) |