Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. |
| 3 | # |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 4 | |
| 5 | """Tests for the dtb_platdata module |
| 6 | |
Simon Glass | 3def0cf | 2018-07-06 10:27:20 -0600 | [diff] [blame] | 7 | This includes unit tests for some functions and functional tests for the dtoc |
| 8 | tool. |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 9 | """ |
| 10 | |
Simon Glass | 90a8132 | 2019-05-17 22:00:31 -0600 | [diff] [blame^] | 11 | from __future__ import print_function |
| 12 | |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 13 | import collections |
| 14 | import os |
| 15 | import struct |
| 16 | import unittest |
| 17 | |
| 18 | import dtb_platdata |
| 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 |
| 23 | import fdt |
| 24 | import fdt_util |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 25 | import test_util |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 26 | import tools |
| 27 | |
| 28 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 29 | |
| 30 | |
Simon Glass | aab660f | 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 | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 38 | #include <linux/libfdt.h>''' |
Simon Glass | aab660f | 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 | fe57c78 | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 52 | |
| 53 | def get_dtb_file(dts_fname, capture_stderr=False): |
Simon Glass | c079192 | 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 | fe57c78 | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 58 | capture_stderr: True to capture and discard stderr output |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 59 | |
| 60 | Returns: |
| 61 | Filename of compiled file in output directory |
| 62 | """ |
Simon Glass | fe57c78 | 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 | c079192 | 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 | 57f0bc4 | 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 | 90a8132 | 2019-05-17 22:00:31 -0600 | [diff] [blame^] | 102 | print('Failures written to /tmp/binman.{expected,actual}') |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 103 | self.assertEquals(expected, actual) |
| 104 | |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 105 | def test_name(self): |
| 106 | """Test conversion of device tree names to C identifiers""" |
| 107 | self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12')) |
| 108 | self.assertEqual('vendor_clock_frequency', |
| 109 | conv_name_to_c('vendor,clock-frequency')) |
| 110 | self.assertEqual('rockchip_rk3399_sdhci_5_1', |
| 111 | conv_name_to_c('rockchip,rk3399-sdhci-5.1')) |
| 112 | |
| 113 | def test_tab_to(self): |
| 114 | """Test operation of tab_to() function""" |
| 115 | self.assertEqual('fred ', tab_to(0, 'fred')) |
| 116 | self.assertEqual('fred\t', tab_to(1, 'fred')) |
| 117 | self.assertEqual('fred was here ', tab_to(1, 'fred was here')) |
| 118 | self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here')) |
| 119 | self.assertEqual('exactly8 ', tab_to(1, 'exactly8')) |
| 120 | self.assertEqual('exactly8\t', tab_to(2, 'exactly8')) |
| 121 | |
| 122 | def test_get_value(self): |
| 123 | """Test operation of get_value() function""" |
| 124 | self.assertEqual('0x45', |
| 125 | get_value(fdt.TYPE_INT, struct.pack('>I', 0x45))) |
| 126 | self.assertEqual('0x45', |
| 127 | get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45))) |
| 128 | self.assertEqual('0x0', |
| 129 | get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45))) |
| 130 | self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test')) |
| 131 | self.assertEqual('true', get_value(fdt.TYPE_BOOL, None)) |
| 132 | |
| 133 | def test_get_compat_name(self): |
| 134 | """Test operation of get_compat_name() function""" |
| 135 | Prop = collections.namedtuple('Prop', ['value']) |
| 136 | Node = collections.namedtuple('Node', ['props']) |
| 137 | |
| 138 | prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1']) |
| 139 | node = Node({'compatible': prop}) |
| 140 | self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']), |
| 141 | get_compat_name(node)) |
| 142 | |
| 143 | prop = Prop(['rockchip,rk3399-sdhci-5.1']) |
| 144 | node = Node({'compatible': prop}) |
| 145 | self.assertEqual(('rockchip_rk3399_sdhci_5_1', []), |
| 146 | get_compat_name(node)) |
| 147 | |
| 148 | prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third']) |
| 149 | node = Node({'compatible': prop}) |
| 150 | self.assertEqual(('rockchip_rk3399_sdhci_5_1', |
| 151 | ['arasan_sdhci_5_1', 'third']), |
| 152 | get_compat_name(node)) |
| 153 | |
| 154 | def test_empty_file(self): |
| 155 | """Test output from a device tree file with no nodes""" |
| 156 | dtb_file = get_dtb_file('dtoc_test_empty.dts') |
| 157 | output = tools.GetOutputFilename('output') |
| 158 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 159 | with open(output) as infile: |
| 160 | lines = infile.read().splitlines() |
Simon Glass | aab660f | 2017-11-12 21:52:17 -0700 | [diff] [blame] | 161 | self.assertEqual(HEADER.splitlines(), lines) |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 162 | |
| 163 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 164 | with open(output) as infile: |
| 165 | lines = infile.read().splitlines() |
Simon Glass | aab660f | 2017-11-12 21:52:17 -0700 | [diff] [blame] | 166 | self.assertEqual(C_HEADER.splitlines() + [''], lines) |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 167 | |
| 168 | def test_simple(self): |
| 169 | """Test output from some simple nodes with various types of data""" |
| 170 | dtb_file = get_dtb_file('dtoc_test_simple.dts') |
| 171 | output = tools.GetOutputFilename('output') |
| 172 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 173 | with open(output) as infile: |
| 174 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 175 | self._CheckStrings(HEADER + ''' |
Simon Glass | 5ec741f | 2017-08-29 14:15:51 -0600 | [diff] [blame] | 176 | struct dtd_sandbox_i2c_test { |
| 177 | }; |
| 178 | struct dtd_sandbox_pmic_test { |
| 179 | \tbool\t\tlow_power; |
| 180 | \tfdt64_t\t\treg[2]; |
| 181 | }; |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 182 | struct dtd_sandbox_spl_test { |
| 183 | \tbool\t\tboolval; |
| 184 | \tunsigned char\tbytearray[3]; |
| 185 | \tunsigned char\tbyteval; |
| 186 | \tfdt32_t\t\tintarray[4]; |
| 187 | \tfdt32_t\t\tintval; |
| 188 | \tunsigned char\tlongbytearray[9]; |
Simon Glass | 2a2d91d | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 189 | \tunsigned char\tnotstring[5]; |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 190 | \tconst char *\tstringarray[3]; |
| 191 | \tconst char *\tstringval; |
| 192 | }; |
| 193 | struct dtd_sandbox_spl_test_2 { |
| 194 | }; |
| 195 | ''', data) |
| 196 | |
| 197 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 198 | with open(output) as infile: |
| 199 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 200 | self._CheckStrings(C_HEADER + ''' |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 201 | static const struct dtd_sandbox_spl_test dtv_spl_test = { |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 202 | \t.bytearray\t\t= {0x6, 0x0, 0x0}, |
| 203 | \t.byteval\t\t= 0x5, |
| 204 | \t.intval\t\t\t= 0x1, |
Simon Glass | 2a2d91d | 2018-07-06 10:27:28 -0600 | [diff] [blame] | 205 | \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0}, |
Simon Glass | 21d54ac | 2017-08-29 14:15:49 -0600 | [diff] [blame] | 206 | \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, |
| 207 | \t\t0x11}, |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 208 | \t.stringval\t\t= "message", |
| 209 | \t.boolval\t\t= true, |
| 210 | \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0}, |
| 211 | \t.stringarray\t\t= {"multi-word", "message", ""}, |
| 212 | }; |
| 213 | U_BOOT_DEVICE(spl_test) = { |
| 214 | \t.name\t\t= "sandbox_spl_test", |
| 215 | \t.platdata\t= &dtv_spl_test, |
| 216 | \t.platdata_size\t= sizeof(dtv_spl_test), |
| 217 | }; |
| 218 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 219 | static const struct dtd_sandbox_spl_test dtv_spl_test2 = { |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 220 | \t.bytearray\t\t= {0x1, 0x23, 0x34}, |
| 221 | \t.byteval\t\t= 0x8, |
| 222 | \t.intval\t\t\t= 0x3, |
Simon Glass | 21d54ac | 2017-08-29 14:15:49 -0600 | [diff] [blame] | 223 | \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 224 | \t\t0x0}, |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 225 | \t.stringval\t\t= "message2", |
| 226 | \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0}, |
| 227 | \t.stringarray\t\t= {"another", "multi-word", "message"}, |
| 228 | }; |
| 229 | U_BOOT_DEVICE(spl_test2) = { |
| 230 | \t.name\t\t= "sandbox_spl_test", |
| 231 | \t.platdata\t= &dtv_spl_test2, |
| 232 | \t.platdata_size\t= sizeof(dtv_spl_test2), |
| 233 | }; |
| 234 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 235 | static const struct dtd_sandbox_spl_test dtv_spl_test3 = { |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 236 | \t.stringarray\t\t= {"one", "", ""}, |
| 237 | }; |
| 238 | U_BOOT_DEVICE(spl_test3) = { |
| 239 | \t.name\t\t= "sandbox_spl_test", |
| 240 | \t.platdata\t= &dtv_spl_test3, |
| 241 | \t.platdata_size\t= sizeof(dtv_spl_test3), |
| 242 | }; |
| 243 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 244 | static const struct dtd_sandbox_spl_test_2 dtv_spl_test4 = { |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 245 | }; |
| 246 | U_BOOT_DEVICE(spl_test4) = { |
| 247 | \t.name\t\t= "sandbox_spl_test_2", |
| 248 | \t.platdata\t= &dtv_spl_test4, |
| 249 | \t.platdata_size\t= sizeof(dtv_spl_test4), |
| 250 | }; |
| 251 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 252 | static const struct dtd_sandbox_i2c_test dtv_i2c_at_0 = { |
Simon Glass | 5ec741f | 2017-08-29 14:15:51 -0600 | [diff] [blame] | 253 | }; |
| 254 | U_BOOT_DEVICE(i2c_at_0) = { |
| 255 | \t.name\t\t= "sandbox_i2c_test", |
| 256 | \t.platdata\t= &dtv_i2c_at_0, |
| 257 | \t.platdata_size\t= sizeof(dtv_i2c_at_0), |
| 258 | }; |
| 259 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 260 | static const struct dtd_sandbox_pmic_test dtv_pmic_at_9 = { |
Simon Glass | 5ec741f | 2017-08-29 14:15:51 -0600 | [diff] [blame] | 261 | \t.low_power\t\t= true, |
| 262 | \t.reg\t\t\t= {0x9, 0x0}, |
| 263 | }; |
| 264 | U_BOOT_DEVICE(pmic_at_9) = { |
| 265 | \t.name\t\t= "sandbox_pmic_test", |
| 266 | \t.platdata\t= &dtv_pmic_at_9, |
| 267 | \t.platdata_size\t= sizeof(dtv_pmic_at_9), |
| 268 | }; |
| 269 | |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 270 | ''', data) |
| 271 | |
| 272 | def test_phandle(self): |
| 273 | """Test output from a node containing a phandle reference""" |
| 274 | dtb_file = get_dtb_file('dtoc_test_phandle.dts') |
| 275 | output = tools.GetOutputFilename('output') |
| 276 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 277 | with open(output) as infile: |
| 278 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 279 | self._CheckStrings(HEADER + ''' |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 280 | struct dtd_source { |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 281 | \tstruct phandle_2_arg clocks[4]; |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 282 | }; |
| 283 | struct dtd_target { |
| 284 | \tfdt32_t\t\tintval; |
| 285 | }; |
| 286 | ''', data) |
| 287 | |
| 288 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 289 | with open(output) as infile: |
| 290 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 291 | self._CheckStrings(C_HEADER + ''' |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 292 | static const struct dtd_target dtv_phandle_target = { |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 293 | \t.intval\t\t\t= 0x0, |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 294 | }; |
| 295 | U_BOOT_DEVICE(phandle_target) = { |
| 296 | \t.name\t\t= "target", |
| 297 | \t.platdata\t= &dtv_phandle_target, |
| 298 | \t.platdata_size\t= sizeof(dtv_phandle_target), |
| 299 | }; |
| 300 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 301 | static const struct dtd_target dtv_phandle2_target = { |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 302 | \t.intval\t\t\t= 0x1, |
| 303 | }; |
| 304 | U_BOOT_DEVICE(phandle2_target) = { |
| 305 | \t.name\t\t= "target", |
| 306 | \t.platdata\t= &dtv_phandle2_target, |
| 307 | \t.platdata_size\t= sizeof(dtv_phandle2_target), |
| 308 | }; |
| 309 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 310 | static const struct dtd_target dtv_phandle3_target = { |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 311 | \t.intval\t\t\t= 0x2, |
| 312 | }; |
| 313 | U_BOOT_DEVICE(phandle3_target) = { |
| 314 | \t.name\t\t= "target", |
| 315 | \t.platdata\t= &dtv_phandle3_target, |
| 316 | \t.platdata_size\t= sizeof(dtv_phandle3_target), |
| 317 | }; |
| 318 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 319 | static const struct dtd_source dtv_phandle_source = { |
Simon Glass | 35d5037 | 2017-08-29 14:15:57 -0600 | [diff] [blame] | 320 | \t.clocks\t\t\t= { |
Simon Glass | 634eba4 | 2017-08-29 14:15:59 -0600 | [diff] [blame] | 321 | \t\t\t{&dtv_phandle_target, {}}, |
| 322 | \t\t\t{&dtv_phandle2_target, {11}}, |
| 323 | \t\t\t{&dtv_phandle3_target, {12, 13}}, |
| 324 | \t\t\t{&dtv_phandle_target, {}},}, |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 325 | }; |
| 326 | U_BOOT_DEVICE(phandle_source) = { |
| 327 | \t.name\t\t= "source", |
| 328 | \t.platdata\t= &dtv_phandle_source, |
| 329 | \t.platdata_size\t= sizeof(dtv_phandle_source), |
| 330 | }; |
| 331 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 332 | static const struct dtd_source dtv_phandle_source2 = { |
Simon Glass | 760b717 | 2018-07-06 10:27:31 -0600 | [diff] [blame] | 333 | \t.clocks\t\t\t= { |
| 334 | \t\t\t{&dtv_phandle_target, {}},}, |
| 335 | }; |
| 336 | U_BOOT_DEVICE(phandle_source2) = { |
| 337 | \t.name\t\t= "source", |
| 338 | \t.platdata\t= &dtv_phandle_source2, |
| 339 | \t.platdata_size\t= sizeof(dtv_phandle_source2), |
| 340 | }; |
| 341 | |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 342 | ''', data) |
| 343 | |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 344 | def test_phandle_single(self): |
| 345 | """Test output from a node containing a phandle reference""" |
| 346 | dtb_file = get_dtb_file('dtoc_test_phandle_single.dts') |
| 347 | output = tools.GetOutputFilename('output') |
| 348 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 349 | with open(output) as infile: |
| 350 | data = infile.read() |
| 351 | self._CheckStrings(HEADER + ''' |
| 352 | struct dtd_source { |
| 353 | \tstruct phandle_0_arg clocks[1]; |
| 354 | }; |
| 355 | struct dtd_target { |
| 356 | \tfdt32_t\t\tintval; |
| 357 | }; |
| 358 | ''', data) |
| 359 | |
| 360 | def test_phandle_reorder(self): |
| 361 | """Test that phandle targets are generated before their references""" |
| 362 | dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts') |
| 363 | output = tools.GetOutputFilename('output') |
| 364 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 365 | with open(output) as infile: |
| 366 | data = infile.read() |
| 367 | self._CheckStrings(C_HEADER + ''' |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 368 | static const struct dtd_target dtv_phandle_target = { |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 369 | }; |
| 370 | U_BOOT_DEVICE(phandle_target) = { |
| 371 | \t.name\t\t= "target", |
| 372 | \t.platdata\t= &dtv_phandle_target, |
| 373 | \t.platdata_size\t= sizeof(dtv_phandle_target), |
| 374 | }; |
| 375 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 376 | static const struct dtd_source dtv_phandle_source2 = { |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 377 | \t.clocks\t\t\t= { |
| 378 | \t\t\t{&dtv_phandle_target, {}},}, |
| 379 | }; |
| 380 | U_BOOT_DEVICE(phandle_source2) = { |
| 381 | \t.name\t\t= "source", |
| 382 | \t.platdata\t= &dtv_phandle_source2, |
| 383 | \t.platdata_size\t= sizeof(dtv_phandle_source2), |
| 384 | }; |
| 385 | |
| 386 | ''', data) |
| 387 | |
| 388 | def test_phandle_bad(self): |
| 389 | """Test a node containing an invalid phandle fails""" |
Simon Glass | 4b4bc06 | 2018-10-01 21:12:43 -0600 | [diff] [blame] | 390 | dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts', |
| 391 | capture_stderr=True) |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 392 | output = tools.GetOutputFilename('output') |
| 393 | with self.assertRaises(ValueError) as e: |
| 394 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 395 | self.assertIn("Cannot parse 'clocks' in node 'phandle-source'", |
| 396 | str(e.exception)) |
| 397 | |
| 398 | def test_phandle_bad2(self): |
| 399 | """Test a phandle target missing its #*-cells property""" |
Simon Glass | 4b4bc06 | 2018-10-01 21:12:43 -0600 | [diff] [blame] | 400 | dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts', |
| 401 | capture_stderr=True) |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 402 | output = tools.GetOutputFilename('output') |
| 403 | with self.assertRaises(ValueError) as e: |
| 404 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 405 | self.assertIn("Node 'phandle-target' has no '#clock-cells' property", |
| 406 | str(e.exception)) |
| 407 | |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 408 | def test_aliases(self): |
| 409 | """Test output from a node with multiple compatible strings""" |
| 410 | dtb_file = get_dtb_file('dtoc_test_aliases.dts') |
| 411 | output = tools.GetOutputFilename('output') |
| 412 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 413 | with open(output) as infile: |
| 414 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 415 | self._CheckStrings(HEADER + ''' |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 416 | struct dtd_compat1 { |
| 417 | \tfdt32_t\t\tintval; |
| 418 | }; |
| 419 | #define dtd_compat2_1_fred dtd_compat1 |
| 420 | #define dtd_compat3 dtd_compat1 |
| 421 | ''', data) |
| 422 | |
| 423 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 424 | with open(output) as infile: |
| 425 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 426 | self._CheckStrings(C_HEADER + ''' |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 427 | static const struct dtd_compat1 dtv_spl_test = { |
Simon Glass | c079192 | 2017-06-18 22:09:06 -0600 | [diff] [blame] | 428 | \t.intval\t\t\t= 0x1, |
| 429 | }; |
| 430 | U_BOOT_DEVICE(spl_test) = { |
| 431 | \t.name\t\t= "compat1", |
| 432 | \t.platdata\t= &dtv_spl_test, |
| 433 | \t.platdata_size\t= sizeof(dtv_spl_test), |
| 434 | }; |
| 435 | |
| 436 | ''', data) |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 437 | |
| 438 | def test_addresses64(self): |
| 439 | """Test output from a node with a 'reg' property with na=2, ns=2""" |
| 440 | dtb_file = get_dtb_file('dtoc_test_addr64.dts') |
| 441 | output = tools.GetOutputFilename('output') |
| 442 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 443 | with open(output) as infile: |
| 444 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 445 | self._CheckStrings(HEADER + ''' |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 446 | struct dtd_test1 { |
| 447 | \tfdt64_t\t\treg[2]; |
| 448 | }; |
| 449 | struct dtd_test2 { |
| 450 | \tfdt64_t\t\treg[2]; |
| 451 | }; |
| 452 | struct dtd_test3 { |
| 453 | \tfdt64_t\t\treg[4]; |
| 454 | }; |
| 455 | ''', data) |
| 456 | |
| 457 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 458 | with open(output) as infile: |
| 459 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 460 | self._CheckStrings(C_HEADER + ''' |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 461 | static const struct dtd_test1 dtv_test1 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 462 | \t.reg\t\t\t= {0x1234, 0x5678}, |
| 463 | }; |
| 464 | U_BOOT_DEVICE(test1) = { |
| 465 | \t.name\t\t= "test1", |
| 466 | \t.platdata\t= &dtv_test1, |
| 467 | \t.platdata_size\t= sizeof(dtv_test1), |
| 468 | }; |
| 469 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 470 | static const struct dtd_test2 dtv_test2 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 471 | \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654}, |
| 472 | }; |
| 473 | U_BOOT_DEVICE(test2) = { |
| 474 | \t.name\t\t= "test2", |
| 475 | \t.platdata\t= &dtv_test2, |
| 476 | \t.platdata_size\t= sizeof(dtv_test2), |
| 477 | }; |
| 478 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 479 | static const struct dtd_test3 dtv_test3 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 480 | \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3}, |
| 481 | }; |
| 482 | U_BOOT_DEVICE(test3) = { |
| 483 | \t.name\t\t= "test3", |
| 484 | \t.platdata\t= &dtv_test3, |
| 485 | \t.platdata_size\t= sizeof(dtv_test3), |
| 486 | }; |
| 487 | |
| 488 | ''', data) |
| 489 | |
| 490 | def test_addresses32(self): |
| 491 | """Test output from a node with a 'reg' property with na=1, ns=1""" |
| 492 | dtb_file = get_dtb_file('dtoc_test_addr32.dts') |
| 493 | output = tools.GetOutputFilename('output') |
| 494 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 495 | with open(output) as infile: |
| 496 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 497 | self._CheckStrings(HEADER + ''' |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 498 | struct dtd_test1 { |
| 499 | \tfdt32_t\t\treg[2]; |
| 500 | }; |
| 501 | struct dtd_test2 { |
| 502 | \tfdt32_t\t\treg[4]; |
| 503 | }; |
| 504 | ''', data) |
| 505 | |
| 506 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 507 | with open(output) as infile: |
| 508 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 509 | self._CheckStrings(C_HEADER + ''' |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 510 | static const struct dtd_test1 dtv_test1 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 511 | \t.reg\t\t\t= {0x1234, 0x5678}, |
| 512 | }; |
| 513 | U_BOOT_DEVICE(test1) = { |
| 514 | \t.name\t\t= "test1", |
| 515 | \t.platdata\t= &dtv_test1, |
| 516 | \t.platdata_size\t= sizeof(dtv_test1), |
| 517 | }; |
| 518 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 519 | static const struct dtd_test2 dtv_test2 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 520 | \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3}, |
| 521 | }; |
| 522 | U_BOOT_DEVICE(test2) = { |
| 523 | \t.name\t\t= "test2", |
| 524 | \t.platdata\t= &dtv_test2, |
| 525 | \t.platdata_size\t= sizeof(dtv_test2), |
| 526 | }; |
| 527 | |
| 528 | ''', data) |
| 529 | |
| 530 | def test_addresses64_32(self): |
| 531 | """Test output from a node with a 'reg' property with na=2, ns=1""" |
| 532 | dtb_file = get_dtb_file('dtoc_test_addr64_32.dts') |
| 533 | output = tools.GetOutputFilename('output') |
| 534 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 535 | with open(output) as infile: |
| 536 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 537 | self._CheckStrings(HEADER + ''' |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 538 | struct dtd_test1 { |
| 539 | \tfdt64_t\t\treg[2]; |
| 540 | }; |
| 541 | struct dtd_test2 { |
| 542 | \tfdt64_t\t\treg[2]; |
| 543 | }; |
| 544 | struct dtd_test3 { |
| 545 | \tfdt64_t\t\treg[4]; |
| 546 | }; |
| 547 | ''', data) |
| 548 | |
| 549 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 550 | with open(output) as infile: |
| 551 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 552 | self._CheckStrings(C_HEADER + ''' |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 553 | static const struct dtd_test1 dtv_test1 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 554 | \t.reg\t\t\t= {0x123400000000, 0x5678}, |
| 555 | }; |
| 556 | U_BOOT_DEVICE(test1) = { |
| 557 | \t.name\t\t= "test1", |
| 558 | \t.platdata\t= &dtv_test1, |
| 559 | \t.platdata_size\t= sizeof(dtv_test1), |
| 560 | }; |
| 561 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 562 | static const struct dtd_test2 dtv_test2 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 563 | \t.reg\t\t\t= {0x1234567890123456, 0x98765432}, |
| 564 | }; |
| 565 | U_BOOT_DEVICE(test2) = { |
| 566 | \t.name\t\t= "test2", |
| 567 | \t.platdata\t= &dtv_test2, |
| 568 | \t.platdata_size\t= sizeof(dtv_test2), |
| 569 | }; |
| 570 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 571 | static const struct dtd_test3 dtv_test3 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 572 | \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3}, |
| 573 | }; |
| 574 | U_BOOT_DEVICE(test3) = { |
| 575 | \t.name\t\t= "test3", |
| 576 | \t.platdata\t= &dtv_test3, |
| 577 | \t.platdata_size\t= sizeof(dtv_test3), |
| 578 | }; |
| 579 | |
| 580 | ''', data) |
| 581 | |
| 582 | def test_addresses32_64(self): |
| 583 | """Test output from a node with a 'reg' property with na=1, ns=2""" |
| 584 | dtb_file = get_dtb_file('dtoc_test_addr32_64.dts') |
| 585 | output = tools.GetOutputFilename('output') |
| 586 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 587 | with open(output) as infile: |
| 588 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 589 | self._CheckStrings(HEADER + ''' |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 590 | struct dtd_test1 { |
| 591 | \tfdt64_t\t\treg[2]; |
| 592 | }; |
| 593 | struct dtd_test2 { |
| 594 | \tfdt64_t\t\treg[2]; |
| 595 | }; |
| 596 | struct dtd_test3 { |
| 597 | \tfdt64_t\t\treg[4]; |
| 598 | }; |
| 599 | ''', data) |
| 600 | |
| 601 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 602 | with open(output) as infile: |
| 603 | data = infile.read() |
Simon Glass | 57f0bc4 | 2018-07-06 10:27:25 -0600 | [diff] [blame] | 604 | self._CheckStrings(C_HEADER + ''' |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 605 | static const struct dtd_test1 dtv_test1 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 606 | \t.reg\t\t\t= {0x1234, 0x567800000000}, |
| 607 | }; |
| 608 | U_BOOT_DEVICE(test1) = { |
| 609 | \t.name\t\t= "test1", |
| 610 | \t.platdata\t= &dtv_test1, |
| 611 | \t.platdata_size\t= sizeof(dtv_test1), |
| 612 | }; |
| 613 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 614 | static const struct dtd_test2 dtv_test2 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 615 | \t.reg\t\t\t= {0x12345678, 0x9876543210987654}, |
| 616 | }; |
| 617 | U_BOOT_DEVICE(test2) = { |
| 618 | \t.name\t\t= "test2", |
| 619 | \t.platdata\t= &dtv_test2, |
| 620 | \t.platdata_size\t= sizeof(dtv_test2), |
| 621 | }; |
| 622 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 623 | static const struct dtd_test3 dtv_test3 = { |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 624 | \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3}, |
| 625 | }; |
| 626 | U_BOOT_DEVICE(test3) = { |
| 627 | \t.name\t\t= "test3", |
| 628 | \t.platdata\t= &dtv_test3, |
| 629 | \t.platdata_size\t= sizeof(dtv_test3), |
| 630 | }; |
| 631 | |
| 632 | ''', data) |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 633 | |
| 634 | def test_bad_reg(self): |
| 635 | """Test that a reg property with an invalid type generates an error""" |
Simon Glass | fe57c78 | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 636 | # Capture stderr since dtc will emit warnings for this file |
| 637 | dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True) |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 638 | output = tools.GetOutputFilename('output') |
| 639 | with self.assertRaises(ValueError) as e: |
| 640 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 641 | self.assertIn("Node 'spl-test' reg property is not an int", |
| 642 | str(e.exception)) |
| 643 | |
| 644 | def test_bad_reg2(self): |
| 645 | """Test that a reg property with an invalid cell count is detected""" |
Simon Glass | fe57c78 | 2018-07-06 10:27:37 -0600 | [diff] [blame] | 646 | # Capture stderr since dtc will emit warnings for this file |
| 647 | dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True) |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 648 | output = tools.GetOutputFilename('output') |
| 649 | with self.assertRaises(ValueError) as e: |
| 650 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 651 | self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)", |
| 652 | str(e.exception)) |
| 653 | |
| 654 | def test_add_prop(self): |
| 655 | """Test that a subequent node can add a new property to a struct""" |
| 656 | dtb_file = get_dtb_file('dtoc_test_add_prop.dts') |
| 657 | output = tools.GetOutputFilename('output') |
| 658 | dtb_platdata.run_steps(['struct'], dtb_file, False, output) |
| 659 | with open(output) as infile: |
| 660 | data = infile.read() |
| 661 | self._CheckStrings(HEADER + ''' |
| 662 | struct dtd_sandbox_spl_test { |
| 663 | \tfdt32_t\t\tintarray; |
| 664 | \tfdt32_t\t\tintval; |
| 665 | }; |
| 666 | ''', data) |
| 667 | |
| 668 | dtb_platdata.run_steps(['platdata'], dtb_file, False, output) |
| 669 | with open(output) as infile: |
| 670 | data = infile.read() |
| 671 | self._CheckStrings(C_HEADER + ''' |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 672 | static const struct dtd_sandbox_spl_test dtv_spl_test = { |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 673 | \t.intval\t\t\t= 0x1, |
| 674 | }; |
| 675 | U_BOOT_DEVICE(spl_test) = { |
| 676 | \t.name\t\t= "sandbox_spl_test", |
| 677 | \t.platdata\t= &dtv_spl_test, |
| 678 | \t.platdata_size\t= sizeof(dtv_spl_test), |
| 679 | }; |
| 680 | |
Simon Goldschmidt | e4478d9 | 2019-01-21 10:07:55 +0100 | [diff] [blame] | 681 | static const struct dtd_sandbox_spl_test dtv_spl_test2 = { |
Simon Glass | 8512ea2 | 2018-07-06 10:27:35 -0600 | [diff] [blame] | 682 | \t.intarray\t\t= 0x5, |
| 683 | }; |
| 684 | U_BOOT_DEVICE(spl_test2) = { |
| 685 | \t.name\t\t= "sandbox_spl_test", |
| 686 | \t.platdata\t= &dtv_spl_test2, |
| 687 | \t.platdata_size\t= sizeof(dtv_spl_test2), |
| 688 | }; |
| 689 | |
| 690 | ''', data) |
| 691 | |
| 692 | def testStdout(self): |
| 693 | """Test output to stdout""" |
| 694 | dtb_file = get_dtb_file('dtoc_test_simple.dts') |
| 695 | with test_util.capture_sys_output() as (stdout, stderr): |
| 696 | dtb_platdata.run_steps(['struct'], dtb_file, False, '-') |
| 697 | |
| 698 | def testNoCommand(self): |
| 699 | """Test running dtoc without a command""" |
| 700 | with self.assertRaises(ValueError) as e: |
| 701 | dtb_platdata.run_steps([], '', False, '') |
| 702 | self.assertIn("Please specify a command: struct, platdata", |
| 703 | str(e.exception)) |
| 704 | |
| 705 | def testBadCommand(self): |
| 706 | """Test running dtoc with an invalid command""" |
| 707 | dtb_file = get_dtb_file('dtoc_test_simple.dts') |
| 708 | output = tools.GetOutputFilename('output') |
| 709 | with self.assertRaises(ValueError) as e: |
| 710 | dtb_platdata.run_steps(['invalid-cmd'], dtb_file, False, output) |
| 711 | self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)", |
| 712 | str(e.exception)) |