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