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
18 from dtoc 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
24 from dtoc import fdt_util
25 from patman import test_util
26 from patman import tools
28 our_path = os.path.dirname(os.path.realpath(__file__))
34 * This file was generated by dtoc from a .dtb (device tree binary) file.
38 #include <linux/libfdt.h>'''
43 * This file was generated by dtoc from a .dtb (device tree binary) file.
48 #include <dt-structs.h>
51 C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
56 def get_dtb_file(dts_fname, capture_stderr=False):
57 """Compile a .dts file to a .dtb
60 dts_fname: Filename of .dts file in the current directory
61 capture_stderr: True to capture and discard stderr output
64 Filename of compiled file in output directory
66 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
67 capture_stderr=capture_stderr)
70 class TestDtoc(unittest.TestCase):
74 tools.PrepareOutputDir(None)
77 def tearDownClass(cls):
78 tools._RemoveOutputDir()
80 def _WritePythonString(self, fname, data):
81 """Write a string with tabs expanded as done in this Python file
84 fname: Filename to write to
85 data: Raw string to convert
87 data = data.replace('\t', '\\t')
88 with open(fname, 'w') as fd:
91 def _CheckStrings(self, expected, actual):
92 """Check that a string matches its expected value
94 If the strings do not match, they are written to the /tmp directory in
95 the same Python format as is used here in the test. This allows for
96 easy comparison and update of the tests.
99 expected: Expected string
100 actual: Actual string
102 if expected != actual:
103 self._WritePythonString('/tmp/binman.expected', expected)
104 self._WritePythonString('/tmp/binman.actual', actual)
105 print('Failures written to /tmp/binman.{expected,actual}')
106 self.assertEquals(expected, actual)
109 def run_test(self, args, dtb_file, output):
110 dtb_platdata.run_steps(args, dtb_file, False, output, True)
113 """Test conversion of device tree names to C identifiers"""
114 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
115 self.assertEqual('vendor_clock_frequency',
116 conv_name_to_c('vendor,clock-frequency'))
117 self.assertEqual('rockchip_rk3399_sdhci_5_1',
118 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
120 def test_tab_to(self):
121 """Test operation of tab_to() function"""
122 self.assertEqual('fred ', tab_to(0, 'fred'))
123 self.assertEqual('fred\t', tab_to(1, 'fred'))
124 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
125 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
126 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
127 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
129 def test_get_value(self):
130 """Test operation of get_value() function"""
131 self.assertEqual('0x45',
132 get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
133 self.assertEqual('0x45',
134 get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
135 self.assertEqual('0x0',
136 get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
137 self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
138 self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
140 def test_get_compat_name(self):
141 """Test operation of get_compat_name() function"""
142 Prop = collections.namedtuple('Prop', ['value'])
143 Node = collections.namedtuple('Node', ['props'])
145 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
146 node = Node({'compatible': prop})
147 self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
148 get_compat_name(node))
150 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
151 node = Node({'compatible': prop})
152 self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
153 get_compat_name(node))
155 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
156 node = Node({'compatible': prop})
157 self.assertEqual(('rockchip_rk3399_sdhci_5_1',
158 ['arasan_sdhci_5_1', 'third']),
159 get_compat_name(node))
161 def test_empty_file(self):
162 """Test output from a device tree file with no nodes"""
163 dtb_file = get_dtb_file('dtoc_test_empty.dts')
164 output = tools.GetOutputFilename('output')
165 self.run_test(['struct'], dtb_file, output)
166 with open(output) as infile:
167 lines = infile.read().splitlines()
168 self.assertEqual(HEADER.splitlines(), lines)
170 self.run_test(['platdata'], dtb_file, output)
171 with open(output) as infile:
172 lines = infile.read().splitlines()
173 self.assertEqual(C_HEADER.splitlines() + [''] +
174 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
176 def test_simple(self):
177 """Test output from some simple nodes with various types of data"""
178 dtb_file = get_dtb_file('dtoc_test_simple.dts')
179 output = tools.GetOutputFilename('output')
180 self.run_test(['struct'], dtb_file, output)
181 with open(output) as infile:
183 self._CheckStrings(HEADER + '''
184 struct dtd_sandbox_i2c_test {
186 struct dtd_sandbox_pmic_test {
190 struct dtd_sandbox_spl_test {
192 \tunsigned char\tbytearray[3];
193 \tunsigned char\tbyteval;
194 \tfdt32_t\t\tintarray[4];
196 \tunsigned char\tlongbytearray[9];
197 \tunsigned char\tnotstring[5];
198 \tconst char *\tstringarray[3];
199 \tconst char *\tstringval;
201 struct dtd_sandbox_spl_test_2 {
205 self.run_test(['platdata'], dtb_file, output)
206 with open(output) as infile:
208 self._CheckStrings(C_HEADER + '''
209 static struct dtd_sandbox_spl_test dtv_spl_test = {
210 \t.boolval\t\t= true,
211 \t.bytearray\t\t= {0x6, 0x0, 0x0},
213 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
214 \t.intval\t\t\t= 0x1,
215 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
217 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
218 \t.stringarray\t\t= {"multi-word", "message", ""},
219 \t.stringval\t\t= "message",
221 U_BOOT_DEVICE(spl_test) = {
222 \t.name\t\t= "sandbox_spl_test",
223 \t.platdata\t= &dtv_spl_test,
224 \t.platdata_size\t= sizeof(dtv_spl_test),
227 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
228 \t.bytearray\t\t= {0x1, 0x23, 0x34},
230 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
231 \t.intval\t\t\t= 0x3,
232 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
234 \t.stringarray\t\t= {"another", "multi-word", "message"},
235 \t.stringval\t\t= "message2",
237 U_BOOT_DEVICE(spl_test2) = {
238 \t.name\t\t= "sandbox_spl_test",
239 \t.platdata\t= &dtv_spl_test2,
240 \t.platdata_size\t= sizeof(dtv_spl_test2),
243 static struct dtd_sandbox_spl_test dtv_spl_test3 = {
244 \t.stringarray\t\t= {"one", "", ""},
246 U_BOOT_DEVICE(spl_test3) = {
247 \t.name\t\t= "sandbox_spl_test",
248 \t.platdata\t= &dtv_spl_test3,
249 \t.platdata_size\t= sizeof(dtv_spl_test3),
252 static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
254 U_BOOT_DEVICE(spl_test4) = {
255 \t.name\t\t= "sandbox_spl_test_2",
256 \t.platdata\t= &dtv_spl_test4,
257 \t.platdata_size\t= sizeof(dtv_spl_test4),
260 static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
262 U_BOOT_DEVICE(i2c_at_0) = {
263 \t.name\t\t= "sandbox_i2c_test",
264 \t.platdata\t= &dtv_i2c_at_0,
265 \t.platdata_size\t= sizeof(dtv_i2c_at_0),
268 static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
269 \t.low_power\t\t= true,
270 \t.reg\t\t\t= {0x9, 0x0},
272 U_BOOT_DEVICE(pmic_at_9) = {
273 \t.name\t\t= "sandbox_pmic_test",
274 \t.platdata\t= &dtv_pmic_at_9,
275 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
278 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
280 def test_driver_alias(self):
281 """Test output from a device tree file with a driver alias"""
282 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
283 output = tools.GetOutputFilename('output')
284 self.run_test(['struct'], dtb_file, output)
285 with open(output) as infile:
287 self._CheckStrings(HEADER + '''
288 struct dtd_sandbox_gpio {
289 \tconst char *\tgpio_bank_name;
290 \tbool\t\tgpio_controller;
291 \tfdt32_t\t\tsandbox_gpio_count;
293 #define dtd_sandbox_gpio_alias dtd_sandbox_gpio
296 self.run_test(['platdata'], dtb_file, output)
297 with open(output) as infile:
299 self._CheckStrings(C_HEADER + '''
300 static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
301 \t.gpio_bank_name\t\t= "a",
302 \t.gpio_controller\t= true,
303 \t.sandbox_gpio_count\t= 0x14,
305 U_BOOT_DEVICE(gpios_at_0) = {
306 \t.name\t\t= "sandbox_gpio",
307 \t.platdata\t= &dtv_gpios_at_0,
308 \t.platdata_size\t= sizeof(dtv_gpios_at_0),
311 void dm_populate_phandle_data(void) {
315 def test_invalid_driver(self):
316 """Test output from a device tree file with an invalid driver"""
317 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
318 output = tools.GetOutputFilename('output')
319 with test_util.capture_sys_output() as (stdout, stderr):
320 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
321 with open(output) as infile:
323 self._CheckStrings(HEADER + '''
328 with test_util.capture_sys_output() as (stdout, stderr):
329 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
330 with open(output) as infile:
332 self._CheckStrings(C_HEADER + '''
333 static struct dtd_invalid dtv_spl_test = {
335 U_BOOT_DEVICE(spl_test) = {
336 \t.name\t\t= "invalid",
337 \t.platdata\t= &dtv_spl_test,
338 \t.platdata_size\t= sizeof(dtv_spl_test),
341 void dm_populate_phandle_data(void) {
345 def test_phandle(self):
346 """Test output from a node containing a phandle reference"""
347 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
348 output = tools.GetOutputFilename('output')
349 self.run_test(['struct'], dtb_file, output)
350 with open(output) as infile:
352 self._CheckStrings(HEADER + '''
354 \tstruct phandle_2_arg clocks[4];
361 self.run_test(['platdata'], dtb_file, output)
362 with open(output) as infile:
364 self._CheckStrings(C_HEADER + '''
365 static struct dtd_target dtv_phandle_target = {
366 \t.intval\t\t\t= 0x0,
368 U_BOOT_DEVICE(phandle_target) = {
369 \t.name\t\t= "target",
370 \t.platdata\t= &dtv_phandle_target,
371 \t.platdata_size\t= sizeof(dtv_phandle_target),
374 static struct dtd_target dtv_phandle2_target = {
375 \t.intval\t\t\t= 0x1,
377 U_BOOT_DEVICE(phandle2_target) = {
378 \t.name\t\t= "target",
379 \t.platdata\t= &dtv_phandle2_target,
380 \t.platdata_size\t= sizeof(dtv_phandle2_target),
383 static struct dtd_target dtv_phandle3_target = {
384 \t.intval\t\t\t= 0x2,
386 U_BOOT_DEVICE(phandle3_target) = {
387 \t.name\t\t= "target",
388 \t.platdata\t= &dtv_phandle3_target,
389 \t.platdata_size\t= sizeof(dtv_phandle3_target),
392 static struct dtd_source dtv_phandle_source = {
396 \t\t\t{NULL, {12, 13}},
399 U_BOOT_DEVICE(phandle_source) = {
400 \t.name\t\t= "source",
401 \t.platdata\t= &dtv_phandle_source,
402 \t.platdata_size\t= sizeof(dtv_phandle_source),
405 static struct dtd_source dtv_phandle_source2 = {
409 U_BOOT_DEVICE(phandle_source2) = {
410 \t.name\t\t= "source",
411 \t.platdata\t= &dtv_phandle_source2,
412 \t.platdata_size\t= sizeof(dtv_phandle_source2),
415 void dm_populate_phandle_data(void) {
416 \tdtv_phandle_source.clocks[0].node = DM_GET_DEVICE(phandle_target);
417 \tdtv_phandle_source.clocks[1].node = DM_GET_DEVICE(phandle2_target);
418 \tdtv_phandle_source.clocks[2].node = DM_GET_DEVICE(phandle3_target);
419 \tdtv_phandle_source.clocks[3].node = DM_GET_DEVICE(phandle_target);
420 \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
424 def test_phandle_single(self):
425 """Test output from a node containing a phandle reference"""
426 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
427 output = tools.GetOutputFilename('output')
428 self.run_test(['struct'], dtb_file, output)
429 with open(output) as infile:
431 self._CheckStrings(HEADER + '''
433 \tstruct phandle_0_arg clocks[1];
440 def test_phandle_reorder(self):
441 """Test that phandle targets are generated before their references"""
442 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
443 output = tools.GetOutputFilename('output')
444 self.run_test(['platdata'], dtb_file, output)
445 with open(output) as infile:
447 self._CheckStrings(C_HEADER + '''
448 static struct dtd_target dtv_phandle_target = {
450 U_BOOT_DEVICE(phandle_target) = {
451 \t.name\t\t= "target",
452 \t.platdata\t= &dtv_phandle_target,
453 \t.platdata_size\t= sizeof(dtv_phandle_target),
456 static struct dtd_source dtv_phandle_source2 = {
460 U_BOOT_DEVICE(phandle_source2) = {
461 \t.name\t\t= "source",
462 \t.platdata\t= &dtv_phandle_source2,
463 \t.platdata_size\t= sizeof(dtv_phandle_source2),
466 void dm_populate_phandle_data(void) {
467 \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
471 def test_phandle_cd_gpio(self):
472 """Test that phandle targets are generated when unsing cd-gpios"""
473 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
474 output = tools.GetOutputFilename('output')
475 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True)
476 with open(output) as infile:
478 self._CheckStrings(C_HEADER + '''
479 static struct dtd_target dtv_phandle_target = {
480 \t.intval\t\t\t= 0x0,
482 U_BOOT_DEVICE(phandle_target) = {
483 \t.name\t\t= "target",
484 \t.platdata\t= &dtv_phandle_target,
485 \t.platdata_size\t= sizeof(dtv_phandle_target),
488 static struct dtd_target dtv_phandle2_target = {
489 \t.intval\t\t\t= 0x1,
491 U_BOOT_DEVICE(phandle2_target) = {
492 \t.name\t\t= "target",
493 \t.platdata\t= &dtv_phandle2_target,
494 \t.platdata_size\t= sizeof(dtv_phandle2_target),
497 static struct dtd_target dtv_phandle3_target = {
498 \t.intval\t\t\t= 0x2,
500 U_BOOT_DEVICE(phandle3_target) = {
501 \t.name\t\t= "target",
502 \t.platdata\t= &dtv_phandle3_target,
503 \t.platdata_size\t= sizeof(dtv_phandle3_target),
506 static struct dtd_source dtv_phandle_source = {
510 \t\t\t{NULL, {12, 13}},
513 U_BOOT_DEVICE(phandle_source) = {
514 \t.name\t\t= "source",
515 \t.platdata\t= &dtv_phandle_source,
516 \t.platdata_size\t= sizeof(dtv_phandle_source),
519 static struct dtd_source dtv_phandle_source2 = {
523 U_BOOT_DEVICE(phandle_source2) = {
524 \t.name\t\t= "source",
525 \t.platdata\t= &dtv_phandle_source2,
526 \t.platdata_size\t= sizeof(dtv_phandle_source2),
529 void dm_populate_phandle_data(void) {
530 \tdtv_phandle_source.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
531 \tdtv_phandle_source.cd_gpios[1].node = DM_GET_DEVICE(phandle2_target);
532 \tdtv_phandle_source.cd_gpios[2].node = DM_GET_DEVICE(phandle3_target);
533 \tdtv_phandle_source.cd_gpios[3].node = DM_GET_DEVICE(phandle_target);
534 \tdtv_phandle_source2.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
538 def test_phandle_bad(self):
539 """Test a node containing an invalid phandle fails"""
540 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
542 output = tools.GetOutputFilename('output')
543 with self.assertRaises(ValueError) as e:
544 self.run_test(['struct'], dtb_file, output)
545 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
548 def test_phandle_bad2(self):
549 """Test a phandle target missing its #*-cells property"""
550 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
552 output = tools.GetOutputFilename('output')
553 with self.assertRaises(ValueError) as e:
554 self.run_test(['struct'], dtb_file, output)
555 self.assertIn("Node 'phandle-target' has no cells property",
558 def test_aliases(self):
559 """Test output from a node with multiple compatible strings"""
560 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
561 output = tools.GetOutputFilename('output')
562 self.run_test(['struct'], dtb_file, output)
563 with open(output) as infile:
565 self._CheckStrings(HEADER + '''
569 #define dtd_compat2_1_fred dtd_compat1
570 #define dtd_compat3 dtd_compat1
573 self.run_test(['platdata'], dtb_file, output)
574 with open(output) as infile:
576 self._CheckStrings(C_HEADER + '''
577 static struct dtd_compat1 dtv_spl_test = {
578 \t.intval\t\t\t= 0x1,
580 U_BOOT_DEVICE(spl_test) = {
581 \t.name\t\t= "compat1",
582 \t.platdata\t= &dtv_spl_test,
583 \t.platdata_size\t= sizeof(dtv_spl_test),
586 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
588 def test_addresses64(self):
589 """Test output from a node with a 'reg' property with na=2, ns=2"""
590 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
591 output = tools.GetOutputFilename('output')
592 self.run_test(['struct'], dtb_file, output)
593 with open(output) as infile:
595 self._CheckStrings(HEADER + '''
607 self.run_test(['platdata'], dtb_file, output)
608 with open(output) as infile:
610 self._CheckStrings(C_HEADER + '''
611 static struct dtd_test1 dtv_test1 = {
612 \t.reg\t\t\t= {0x1234, 0x5678},
614 U_BOOT_DEVICE(test1) = {
615 \t.name\t\t= "test1",
616 \t.platdata\t= &dtv_test1,
617 \t.platdata_size\t= sizeof(dtv_test1),
620 static struct dtd_test2 dtv_test2 = {
621 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
623 U_BOOT_DEVICE(test2) = {
624 \t.name\t\t= "test2",
625 \t.platdata\t= &dtv_test2,
626 \t.platdata_size\t= sizeof(dtv_test2),
629 static struct dtd_test3 dtv_test3 = {
630 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
632 U_BOOT_DEVICE(test3) = {
633 \t.name\t\t= "test3",
634 \t.platdata\t= &dtv_test3,
635 \t.platdata_size\t= sizeof(dtv_test3),
638 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
640 def test_addresses32(self):
641 """Test output from a node with a 'reg' property with na=1, ns=1"""
642 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
643 output = tools.GetOutputFilename('output')
644 self.run_test(['struct'], dtb_file, output)
645 with open(output) as infile:
647 self._CheckStrings(HEADER + '''
656 self.run_test(['platdata'], dtb_file, output)
657 with open(output) as infile:
659 self._CheckStrings(C_HEADER + '''
660 static struct dtd_test1 dtv_test1 = {
661 \t.reg\t\t\t= {0x1234, 0x5678},
663 U_BOOT_DEVICE(test1) = {
664 \t.name\t\t= "test1",
665 \t.platdata\t= &dtv_test1,
666 \t.platdata_size\t= sizeof(dtv_test1),
669 static struct dtd_test2 dtv_test2 = {
670 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
672 U_BOOT_DEVICE(test2) = {
673 \t.name\t\t= "test2",
674 \t.platdata\t= &dtv_test2,
675 \t.platdata_size\t= sizeof(dtv_test2),
678 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
680 def test_addresses64_32(self):
681 """Test output from a node with a 'reg' property with na=2, ns=1"""
682 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
683 output = tools.GetOutputFilename('output')
684 self.run_test(['struct'], dtb_file, output)
685 with open(output) as infile:
687 self._CheckStrings(HEADER + '''
699 self.run_test(['platdata'], dtb_file, output)
700 with open(output) as infile:
702 self._CheckStrings(C_HEADER + '''
703 static struct dtd_test1 dtv_test1 = {
704 \t.reg\t\t\t= {0x123400000000, 0x5678},
706 U_BOOT_DEVICE(test1) = {
707 \t.name\t\t= "test1",
708 \t.platdata\t= &dtv_test1,
709 \t.platdata_size\t= sizeof(dtv_test1),
712 static struct dtd_test2 dtv_test2 = {
713 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
715 U_BOOT_DEVICE(test2) = {
716 \t.name\t\t= "test2",
717 \t.platdata\t= &dtv_test2,
718 \t.platdata_size\t= sizeof(dtv_test2),
721 static struct dtd_test3 dtv_test3 = {
722 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
724 U_BOOT_DEVICE(test3) = {
725 \t.name\t\t= "test3",
726 \t.platdata\t= &dtv_test3,
727 \t.platdata_size\t= sizeof(dtv_test3),
730 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
732 def test_addresses32_64(self):
733 """Test output from a node with a 'reg' property with na=1, ns=2"""
734 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
735 output = tools.GetOutputFilename('output')
736 self.run_test(['struct'], dtb_file, output)
737 with open(output) as infile:
739 self._CheckStrings(HEADER + '''
751 self.run_test(['platdata'], dtb_file, output)
752 with open(output) as infile:
754 self._CheckStrings(C_HEADER + '''
755 static struct dtd_test1 dtv_test1 = {
756 \t.reg\t\t\t= {0x1234, 0x567800000000},
758 U_BOOT_DEVICE(test1) = {
759 \t.name\t\t= "test1",
760 \t.platdata\t= &dtv_test1,
761 \t.platdata_size\t= sizeof(dtv_test1),
764 static struct dtd_test2 dtv_test2 = {
765 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
767 U_BOOT_DEVICE(test2) = {
768 \t.name\t\t= "test2",
769 \t.platdata\t= &dtv_test2,
770 \t.platdata_size\t= sizeof(dtv_test2),
773 static struct dtd_test3 dtv_test3 = {
774 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
776 U_BOOT_DEVICE(test3) = {
777 \t.name\t\t= "test3",
778 \t.platdata\t= &dtv_test3,
779 \t.platdata_size\t= sizeof(dtv_test3),
782 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
784 def test_bad_reg(self):
785 """Test that a reg property with an invalid type generates an error"""
786 # Capture stderr since dtc will emit warnings for this file
787 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
788 output = tools.GetOutputFilename('output')
789 with self.assertRaises(ValueError) as e:
790 self.run_test(['struct'], dtb_file, output)
791 self.assertIn("Node 'spl-test' reg property is not an int",
794 def test_bad_reg2(self):
795 """Test that a reg property with an invalid cell count is detected"""
796 # Capture stderr since dtc will emit warnings for this file
797 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
798 output = tools.GetOutputFilename('output')
799 with self.assertRaises(ValueError) as e:
800 self.run_test(['struct'], dtb_file, output)
801 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
804 def test_add_prop(self):
805 """Test that a subequent node can add a new property to a struct"""
806 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
807 output = tools.GetOutputFilename('output')
808 self.run_test(['struct'], dtb_file, output)
809 with open(output) as infile:
811 self._CheckStrings(HEADER + '''
812 struct dtd_sandbox_spl_test {
813 \tfdt32_t\t\tintarray;
818 self.run_test(['platdata'], dtb_file, output)
819 with open(output) as infile:
821 self._CheckStrings(C_HEADER + '''
822 static struct dtd_sandbox_spl_test dtv_spl_test = {
823 \t.intval\t\t\t= 0x1,
825 U_BOOT_DEVICE(spl_test) = {
826 \t.name\t\t= "sandbox_spl_test",
827 \t.platdata\t= &dtv_spl_test,
828 \t.platdata_size\t= sizeof(dtv_spl_test),
831 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
832 \t.intarray\t\t= 0x5,
834 U_BOOT_DEVICE(spl_test2) = {
835 \t.name\t\t= "sandbox_spl_test",
836 \t.platdata\t= &dtv_spl_test2,
837 \t.platdata_size\t= sizeof(dtv_spl_test2),
840 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
842 def testStdout(self):
843 """Test output to stdout"""
844 dtb_file = get_dtb_file('dtoc_test_simple.dts')
845 with test_util.capture_sys_output() as (stdout, stderr):
846 self.run_test(['struct'], dtb_file, '-')
848 def testNoCommand(self):
849 """Test running dtoc without a command"""
850 with self.assertRaises(ValueError) as e:
851 self.run_test([], '', '')
852 self.assertIn("Please specify a command: struct, platdata",
855 def testBadCommand(self):
856 """Test running dtoc with an invalid command"""
857 dtb_file = get_dtb_file('dtoc_test_simple.dts')
858 output = tools.GetOutputFilename('output')
859 with self.assertRaises(ValueError) as e:
860 self.run_test(['invalid-cmd'], dtb_file, output)
861 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",