2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright (c) 2012 The Chromium OS Authors.
6 """Tests for the dtb_platdata module
8 This includes unit tests for some functions and functional tests for the dtoc
19 from dtoc import dtb_platdata
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
25 from dtoc import fdt_util
26 from patman import test_util
27 from patman import tools
29 our_path = os.path.dirname(os.path.realpath(__file__))
35 * This file was generated by dtoc from a .dtb (device tree binary) file.
39 #include <linux/libfdt.h>'''
44 * This file was generated by dtoc from a .dtb (device tree binary) file.
49 #include <dt-structs.h>
52 C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
57 def get_dtb_file(dts_fname, capture_stderr=False):
58 """Compile a .dts file to a .dtb
61 dts_fname: Filename of .dts file in the current directory
62 capture_stderr: True to capture and discard stderr output
65 Filename of compiled file in output directory
67 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
68 capture_stderr=capture_stderr)
71 class TestDtoc(unittest.TestCase):
75 tools.PrepareOutputDir(None)
79 def tearDownClass(cls):
80 tools._RemoveOutputDir()
82 def _WritePythonString(self, fname, data):
83 """Write a string with tabs expanded as done in this Python file
86 fname: Filename to write to
87 data: Raw string to convert
89 data = data.replace('\t', '\\t')
90 with open(fname, 'w') as fd:
93 def _CheckStrings(self, expected, actual):
94 """Check that a string matches its expected value
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.
101 expected: Expected string
102 actual: Actual string
104 if expected != actual:
105 self._WritePythonString('/tmp/binman.expected', expected)
106 self._WritePythonString('/tmp/binman.actual', actual)
107 print('Failures written to /tmp/binman.{expected,actual}')
108 self.assertEquals(expected, actual)
111 def run_test(self, args, dtb_file, output):
112 dtb_platdata.run_steps(args, dtb_file, False, output, True)
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'))
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'))
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))
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'])
147 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
148 node = Node({'compatible': prop})
149 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
150 get_compat_name(node))
152 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
153 node = Node({'compatible': prop})
154 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
155 get_compat_name(node))
157 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
158 node = Node({'compatible': prop})
159 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
160 'arasan_sdhci_5_1', 'third']),
161 get_compat_name(node))
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')
167 self.run_test(['struct'], dtb_file, output)
168 with open(output) as infile:
169 lines = infile.read().splitlines()
170 self.assertEqual(HEADER.splitlines(), lines)
172 self.run_test(['platdata'], dtb_file, output)
173 with open(output) as infile:
174 lines = infile.read().splitlines()
175 self.assertEqual(C_HEADER.splitlines() + [''] +
176 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
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')
182 self.run_test(['struct'], dtb_file, output)
183 with open(output) as infile:
185 self._CheckStrings(HEADER + '''
186 struct dtd_sandbox_i2c_test {
188 struct dtd_sandbox_pmic_test {
192 struct dtd_sandbox_spl_test {
193 \tconst char * acpi_name;
195 \tunsigned char\tbytearray[3];
196 \tunsigned char\tbyteval;
197 \tfdt32_t\t\tintarray[4];
199 \tunsigned char\tlongbytearray[9];
200 \tunsigned char\tnotstring[5];
201 \tconst char *\tstringarray[3];
202 \tconst char *\tstringval;
204 struct dtd_sandbox_spl_test_2 {
208 self.run_test(['platdata'], dtb_file, output)
209 with open(output) as infile:
211 self._CheckStrings(C_HEADER + '''
212 static struct dtd_sandbox_spl_test dtv_spl_test = {
213 \t.boolval\t\t= true,
214 \t.bytearray\t\t= {0x6, 0x0, 0x0},
216 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
217 \t.intval\t\t\t= 0x1,
218 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
220 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
221 \t.stringarray\t\t= {"multi-word", "message", ""},
222 \t.stringval\t\t= "message",
224 U_BOOT_DEVICE(spl_test) = {
225 \t.name\t\t= "sandbox_spl_test",
226 \t.platdata\t= &dtv_spl_test,
227 \t.platdata_size\t= sizeof(dtv_spl_test),
230 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
231 \t.acpi_name\t\t= "\\\\_SB.GPO0",
232 \t.bytearray\t\t= {0x1, 0x23, 0x34},
234 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
235 \t.intval\t\t\t= 0x3,
236 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
238 \t.stringarray\t\t= {"another", "multi-word", "message"},
239 \t.stringval\t\t= "message2",
241 U_BOOT_DEVICE(spl_test2) = {
242 \t.name\t\t= "sandbox_spl_test",
243 \t.platdata\t= &dtv_spl_test2,
244 \t.platdata_size\t= sizeof(dtv_spl_test2),
247 static struct dtd_sandbox_spl_test dtv_spl_test3 = {
248 \t.stringarray\t\t= {"one", "", ""},
250 U_BOOT_DEVICE(spl_test3) = {
251 \t.name\t\t= "sandbox_spl_test",
252 \t.platdata\t= &dtv_spl_test3,
253 \t.platdata_size\t= sizeof(dtv_spl_test3),
256 static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
258 U_BOOT_DEVICE(spl_test4) = {
259 \t.name\t\t= "sandbox_spl_test_2",
260 \t.platdata\t= &dtv_spl_test4,
261 \t.platdata_size\t= sizeof(dtv_spl_test4),
264 static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
266 U_BOOT_DEVICE(i2c_at_0) = {
267 \t.name\t\t= "sandbox_i2c_test",
268 \t.platdata\t= &dtv_i2c_at_0,
269 \t.platdata_size\t= sizeof(dtv_i2c_at_0),
272 static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
273 \t.low_power\t\t= true,
274 \t.reg\t\t\t= {0x9, 0x0},
276 U_BOOT_DEVICE(pmic_at_9) = {
277 \t.name\t\t= "sandbox_pmic_test",
278 \t.platdata\t= &dtv_pmic_at_9,
279 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
282 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
284 def test_driver_alias(self):
285 """Test output from a device tree file with a driver alias"""
286 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
287 output = tools.GetOutputFilename('output')
288 self.run_test(['struct'], dtb_file, output)
289 with open(output) as infile:
291 self._CheckStrings(HEADER + '''
292 struct dtd_sandbox_gpio {
293 \tconst char *\tgpio_bank_name;
294 \tbool\t\tgpio_controller;
295 \tfdt32_t\t\tsandbox_gpio_count;
297 #define dtd_sandbox_gpio_alias dtd_sandbox_gpio
300 self.run_test(['platdata'], dtb_file, output)
301 with open(output) as infile:
303 self._CheckStrings(C_HEADER + '''
304 static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
305 \t.gpio_bank_name\t\t= "a",
306 \t.gpio_controller\t= true,
307 \t.sandbox_gpio_count\t= 0x14,
309 U_BOOT_DEVICE(gpios_at_0) = {
310 \t.name\t\t= "sandbox_gpio",
311 \t.platdata\t= &dtv_gpios_at_0,
312 \t.platdata_size\t= sizeof(dtv_gpios_at_0),
315 void dm_populate_phandle_data(void) {
319 def test_invalid_driver(self):
320 """Test output from a device tree file with an invalid driver"""
321 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
322 output = tools.GetOutputFilename('output')
323 with test_util.capture_sys_output() as (stdout, stderr):
324 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
325 with open(output) as infile:
327 self._CheckStrings(HEADER + '''
332 with test_util.capture_sys_output() as (stdout, stderr):
333 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
334 with open(output) as infile:
336 self._CheckStrings(C_HEADER + '''
337 static struct dtd_invalid dtv_spl_test = {
339 U_BOOT_DEVICE(spl_test) = {
340 \t.name\t\t= "invalid",
341 \t.platdata\t= &dtv_spl_test,
342 \t.platdata_size\t= sizeof(dtv_spl_test),
345 void dm_populate_phandle_data(void) {
349 def test_phandle(self):
350 """Test output from a node containing a phandle reference"""
351 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
352 output = tools.GetOutputFilename('output')
353 self.run_test(['struct'], dtb_file, output)
354 with open(output) as infile:
356 self._CheckStrings(HEADER + '''
358 \tstruct phandle_2_arg clocks[4];
365 self.run_test(['platdata'], dtb_file, output)
366 with open(output) as infile:
368 self._CheckStrings(C_HEADER + '''
369 static struct dtd_target dtv_phandle_target = {
370 \t.intval\t\t\t= 0x0,
372 U_BOOT_DEVICE(phandle_target) = {
373 \t.name\t\t= "target",
374 \t.platdata\t= &dtv_phandle_target,
375 \t.platdata_size\t= sizeof(dtv_phandle_target),
378 static struct dtd_target dtv_phandle2_target = {
379 \t.intval\t\t\t= 0x1,
381 U_BOOT_DEVICE(phandle2_target) = {
382 \t.name\t\t= "target",
383 \t.platdata\t= &dtv_phandle2_target,
384 \t.platdata_size\t= sizeof(dtv_phandle2_target),
387 static struct dtd_target dtv_phandle3_target = {
388 \t.intval\t\t\t= 0x2,
390 U_BOOT_DEVICE(phandle3_target) = {
391 \t.name\t\t= "target",
392 \t.platdata\t= &dtv_phandle3_target,
393 \t.platdata_size\t= sizeof(dtv_phandle3_target),
396 static struct dtd_source dtv_phandle_source = {
400 \t\t\t{NULL, {12, 13}},
403 U_BOOT_DEVICE(phandle_source) = {
404 \t.name\t\t= "source",
405 \t.platdata\t= &dtv_phandle_source,
406 \t.platdata_size\t= sizeof(dtv_phandle_source),
409 static struct dtd_source dtv_phandle_source2 = {
413 U_BOOT_DEVICE(phandle_source2) = {
414 \t.name\t\t= "source",
415 \t.platdata\t= &dtv_phandle_source2,
416 \t.platdata_size\t= sizeof(dtv_phandle_source2),
419 void dm_populate_phandle_data(void) {
420 \tdtv_phandle_source.clocks[0].node = DM_GET_DEVICE(phandle_target);
421 \tdtv_phandle_source.clocks[1].node = DM_GET_DEVICE(phandle2_target);
422 \tdtv_phandle_source.clocks[2].node = DM_GET_DEVICE(phandle3_target);
423 \tdtv_phandle_source.clocks[3].node = DM_GET_DEVICE(phandle_target);
424 \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
428 def test_phandle_single(self):
429 """Test output from a node containing a phandle reference"""
430 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
431 output = tools.GetOutputFilename('output')
432 self.run_test(['struct'], dtb_file, output)
433 with open(output) as infile:
435 self._CheckStrings(HEADER + '''
437 \tstruct phandle_0_arg clocks[1];
444 def test_phandle_reorder(self):
445 """Test that phandle targets are generated before their references"""
446 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
447 output = tools.GetOutputFilename('output')
448 self.run_test(['platdata'], dtb_file, output)
449 with open(output) as infile:
451 self._CheckStrings(C_HEADER + '''
452 static struct dtd_target dtv_phandle_target = {
454 U_BOOT_DEVICE(phandle_target) = {
455 \t.name\t\t= "target",
456 \t.platdata\t= &dtv_phandle_target,
457 \t.platdata_size\t= sizeof(dtv_phandle_target),
460 static struct dtd_source dtv_phandle_source2 = {
464 U_BOOT_DEVICE(phandle_source2) = {
465 \t.name\t\t= "source",
466 \t.platdata\t= &dtv_phandle_source2,
467 \t.platdata_size\t= sizeof(dtv_phandle_source2),
470 void dm_populate_phandle_data(void) {
471 \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
475 def test_phandle_cd_gpio(self):
476 """Test that phandle targets are generated when unsing cd-gpios"""
477 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
478 output = tools.GetOutputFilename('output')
479 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True)
480 with open(output) as infile:
482 self._CheckStrings(C_HEADER + '''
483 static struct dtd_target dtv_phandle_target = {
484 \t.intval\t\t\t= 0x0,
486 U_BOOT_DEVICE(phandle_target) = {
487 \t.name\t\t= "target",
488 \t.platdata\t= &dtv_phandle_target,
489 \t.platdata_size\t= sizeof(dtv_phandle_target),
492 static struct dtd_target dtv_phandle2_target = {
493 \t.intval\t\t\t= 0x1,
495 U_BOOT_DEVICE(phandle2_target) = {
496 \t.name\t\t= "target",
497 \t.platdata\t= &dtv_phandle2_target,
498 \t.platdata_size\t= sizeof(dtv_phandle2_target),
501 static struct dtd_target dtv_phandle3_target = {
502 \t.intval\t\t\t= 0x2,
504 U_BOOT_DEVICE(phandle3_target) = {
505 \t.name\t\t= "target",
506 \t.platdata\t= &dtv_phandle3_target,
507 \t.platdata_size\t= sizeof(dtv_phandle3_target),
510 static struct dtd_source dtv_phandle_source = {
514 \t\t\t{NULL, {12, 13}},
517 U_BOOT_DEVICE(phandle_source) = {
518 \t.name\t\t= "source",
519 \t.platdata\t= &dtv_phandle_source,
520 \t.platdata_size\t= sizeof(dtv_phandle_source),
523 static struct dtd_source dtv_phandle_source2 = {
527 U_BOOT_DEVICE(phandle_source2) = {
528 \t.name\t\t= "source",
529 \t.platdata\t= &dtv_phandle_source2,
530 \t.platdata_size\t= sizeof(dtv_phandle_source2),
533 void dm_populate_phandle_data(void) {
534 \tdtv_phandle_source.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
535 \tdtv_phandle_source.cd_gpios[1].node = DM_GET_DEVICE(phandle2_target);
536 \tdtv_phandle_source.cd_gpios[2].node = DM_GET_DEVICE(phandle3_target);
537 \tdtv_phandle_source.cd_gpios[3].node = DM_GET_DEVICE(phandle_target);
538 \tdtv_phandle_source2.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
542 def test_phandle_bad(self):
543 """Test a node containing an invalid phandle fails"""
544 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
546 output = tools.GetOutputFilename('output')
547 with self.assertRaises(ValueError) as e:
548 self.run_test(['struct'], dtb_file, output)
549 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
552 def test_phandle_bad2(self):
553 """Test a phandle target missing its #*-cells property"""
554 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
556 output = tools.GetOutputFilename('output')
557 with self.assertRaises(ValueError) as e:
558 self.run_test(['struct'], dtb_file, output)
559 self.assertIn("Node 'phandle-target' has no cells property",
562 def test_aliases(self):
563 """Test output from a node with multiple compatible strings"""
564 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
565 output = tools.GetOutputFilename('output')
566 self.run_test(['struct'], dtb_file, output)
567 with open(output) as infile:
569 self._CheckStrings(HEADER + '''
573 struct dtd_simple_bus {
576 #define dtd_compat2_1_fred dtd_compat1
577 #define dtd_compat3 dtd_compat1
580 self.run_test(['platdata'], dtb_file, output)
581 with open(output) as infile:
583 self._CheckStrings(C_HEADER + '''
584 static struct dtd_compat1 dtv_spl_test = {
585 \t.intval\t\t\t= 0x1,
587 U_BOOT_DEVICE(spl_test) = {
588 \t.name\t\t= "compat1",
589 \t.platdata\t= &dtv_spl_test,
590 \t.platdata_size\t= sizeof(dtv_spl_test),
593 static struct dtd_simple_bus dtv_spl_test2 = {
594 \t.intval\t\t\t= 0x1,
596 U_BOOT_DEVICE(spl_test2) = {
597 \t.name\t\t= "simple_bus",
598 \t.platdata\t= &dtv_spl_test2,
599 \t.platdata_size\t= sizeof(dtv_spl_test2),
602 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
604 def test_addresses64(self):
605 """Test output from a node with a 'reg' property with na=2, ns=2"""
606 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
607 output = tools.GetOutputFilename('output')
608 self.run_test(['struct'], dtb_file, output)
609 with open(output) as infile:
611 self._CheckStrings(HEADER + '''
623 self.run_test(['platdata'], dtb_file, output)
624 with open(output) as infile:
626 self._CheckStrings(C_HEADER + '''
627 static struct dtd_test1 dtv_test1 = {
628 \t.reg\t\t\t= {0x1234, 0x5678},
630 U_BOOT_DEVICE(test1) = {
631 \t.name\t\t= "test1",
632 \t.platdata\t= &dtv_test1,
633 \t.platdata_size\t= sizeof(dtv_test1),
636 static struct dtd_test2 dtv_test2 = {
637 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
639 U_BOOT_DEVICE(test2) = {
640 \t.name\t\t= "test2",
641 \t.platdata\t= &dtv_test2,
642 \t.platdata_size\t= sizeof(dtv_test2),
645 static struct dtd_test3 dtv_test3 = {
646 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
648 U_BOOT_DEVICE(test3) = {
649 \t.name\t\t= "test3",
650 \t.platdata\t= &dtv_test3,
651 \t.platdata_size\t= sizeof(dtv_test3),
654 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
656 def test_addresses32(self):
657 """Test output from a node with a 'reg' property with na=1, ns=1"""
658 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
659 output = tools.GetOutputFilename('output')
660 self.run_test(['struct'], dtb_file, output)
661 with open(output) as infile:
663 self._CheckStrings(HEADER + '''
672 self.run_test(['platdata'], dtb_file, output)
673 with open(output) as infile:
675 self._CheckStrings(C_HEADER + '''
676 static struct dtd_test1 dtv_test1 = {
677 \t.reg\t\t\t= {0x1234, 0x5678},
679 U_BOOT_DEVICE(test1) = {
680 \t.name\t\t= "test1",
681 \t.platdata\t= &dtv_test1,
682 \t.platdata_size\t= sizeof(dtv_test1),
685 static struct dtd_test2 dtv_test2 = {
686 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
688 U_BOOT_DEVICE(test2) = {
689 \t.name\t\t= "test2",
690 \t.platdata\t= &dtv_test2,
691 \t.platdata_size\t= sizeof(dtv_test2),
694 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
696 def test_addresses64_32(self):
697 """Test output from a node with a 'reg' property with na=2, ns=1"""
698 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
699 output = tools.GetOutputFilename('output')
700 self.run_test(['struct'], dtb_file, output)
701 with open(output) as infile:
703 self._CheckStrings(HEADER + '''
715 self.run_test(['platdata'], dtb_file, output)
716 with open(output) as infile:
718 self._CheckStrings(C_HEADER + '''
719 static struct dtd_test1 dtv_test1 = {
720 \t.reg\t\t\t= {0x123400000000, 0x5678},
722 U_BOOT_DEVICE(test1) = {
723 \t.name\t\t= "test1",
724 \t.platdata\t= &dtv_test1,
725 \t.platdata_size\t= sizeof(dtv_test1),
728 static struct dtd_test2 dtv_test2 = {
729 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
731 U_BOOT_DEVICE(test2) = {
732 \t.name\t\t= "test2",
733 \t.platdata\t= &dtv_test2,
734 \t.platdata_size\t= sizeof(dtv_test2),
737 static struct dtd_test3 dtv_test3 = {
738 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
740 U_BOOT_DEVICE(test3) = {
741 \t.name\t\t= "test3",
742 \t.platdata\t= &dtv_test3,
743 \t.platdata_size\t= sizeof(dtv_test3),
746 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
748 def test_addresses32_64(self):
749 """Test output from a node with a 'reg' property with na=1, ns=2"""
750 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
751 output = tools.GetOutputFilename('output')
752 self.run_test(['struct'], dtb_file, output)
753 with open(output) as infile:
755 self._CheckStrings(HEADER + '''
767 self.run_test(['platdata'], dtb_file, output)
768 with open(output) as infile:
770 self._CheckStrings(C_HEADER + '''
771 static struct dtd_test1 dtv_test1 = {
772 \t.reg\t\t\t= {0x1234, 0x567800000000},
774 U_BOOT_DEVICE(test1) = {
775 \t.name\t\t= "test1",
776 \t.platdata\t= &dtv_test1,
777 \t.platdata_size\t= sizeof(dtv_test1),
780 static struct dtd_test2 dtv_test2 = {
781 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
783 U_BOOT_DEVICE(test2) = {
784 \t.name\t\t= "test2",
785 \t.platdata\t= &dtv_test2,
786 \t.platdata_size\t= sizeof(dtv_test2),
789 static struct dtd_test3 dtv_test3 = {
790 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
792 U_BOOT_DEVICE(test3) = {
793 \t.name\t\t= "test3",
794 \t.platdata\t= &dtv_test3,
795 \t.platdata_size\t= sizeof(dtv_test3),
798 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
800 def test_bad_reg(self):
801 """Test that a reg property with an invalid type generates an error"""
802 # Capture stderr since dtc will emit warnings for this file
803 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
804 output = tools.GetOutputFilename('output')
805 with self.assertRaises(ValueError) as e:
806 self.run_test(['struct'], dtb_file, output)
807 self.assertIn("Node 'spl-test' reg property is not an int",
810 def test_bad_reg2(self):
811 """Test that a reg property with an invalid cell count is detected"""
812 # Capture stderr since dtc will emit warnings for this file
813 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
814 output = tools.GetOutputFilename('output')
815 with self.assertRaises(ValueError) as e:
816 self.run_test(['struct'], dtb_file, output)
817 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
820 def test_add_prop(self):
821 """Test that a subequent node can add a new property to a struct"""
822 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
823 output = tools.GetOutputFilename('output')
824 self.run_test(['struct'], dtb_file, output)
825 with open(output) as infile:
827 self._CheckStrings(HEADER + '''
828 struct dtd_sandbox_spl_test {
829 \tfdt32_t\t\tintarray;
834 self.run_test(['platdata'], dtb_file, output)
835 with open(output) as infile:
837 self._CheckStrings(C_HEADER + '''
838 static struct dtd_sandbox_spl_test dtv_spl_test = {
839 \t.intval\t\t\t= 0x1,
841 U_BOOT_DEVICE(spl_test) = {
842 \t.name\t\t= "sandbox_spl_test",
843 \t.platdata\t= &dtv_spl_test,
844 \t.platdata_size\t= sizeof(dtv_spl_test),
847 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
848 \t.intarray\t\t= 0x5,
850 U_BOOT_DEVICE(spl_test2) = {
851 \t.name\t\t= "sandbox_spl_test",
852 \t.platdata\t= &dtv_spl_test2,
853 \t.platdata_size\t= sizeof(dtv_spl_test2),
856 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
858 def testStdout(self):
859 """Test output to stdout"""
860 dtb_file = get_dtb_file('dtoc_test_simple.dts')
861 with test_util.capture_sys_output() as (stdout, stderr):
862 self.run_test(['struct'], dtb_file, '-')
864 def testNoCommand(self):
865 """Test running dtoc without a command"""
866 with self.assertRaises(ValueError) as e:
867 self.run_test([], '', '')
868 self.assertIn("Please specify a command: struct, platdata",
871 def testBadCommand(self):
872 """Test running dtoc with an invalid command"""
873 dtb_file = get_dtb_file('dtoc_test_simple.dts')
874 output = tools.GetOutputFilename('output')
875 with self.assertRaises(ValueError) as e:
876 self.run_test(['invalid-cmd'], dtb_file, output)
877 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
880 def testScanDrivers(self):
881 """Test running dtoc with additional drivers to scan"""
882 dtb_file = get_dtb_file('dtoc_test_simple.dts')
883 output = tools.GetOutputFilename('output')
884 with test_util.capture_sys_output() as (stdout, stderr):
885 dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
886 [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
888 def testUnicodeError(self):
889 """Test running dtoc with an invalid unicode file
891 To be able to perform this test without adding a weird text file which
892 would produce issues when using checkpatch.pl or patman, generate the
893 file at runtime and then process it.
895 dtb_file = get_dtb_file('dtoc_test_simple.dts')
896 output = tools.GetOutputFilename('output')
897 driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
898 with open(driver_fn, 'wb+') as df:
901 with test_util.capture_sys_output() as (stdout, stderr):
902 dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,