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
17 from dtoc import dtb_platdata
18 from dtb_platdata import conv_name_to_c
19 from dtb_platdata import get_compat_name
20 from dtb_platdata import get_value
21 from dtb_platdata import tab_to
23 from dtoc import fdt_util
24 from patman import test_util
25 from patman import tools
27 our_path = os.path.dirname(os.path.realpath(__file__))
33 * This file was generated by dtoc from a .dtb (device tree binary) file.
37 #include <linux/libfdt.h>'''
42 * This file was generated by dtoc from a .dtb (device tree binary) file.
47 #include <dt-structs.h>
52 def get_dtb_file(dts_fname, capture_stderr=False):
53 """Compile a .dts file to a .dtb
56 dts_fname: Filename of .dts file in the current directory
57 capture_stderr: True to capture and discard stderr output
60 Filename of compiled file in output directory
62 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
63 capture_stderr=capture_stderr)
66 class TestDtoc(unittest.TestCase):
70 tools.PrepareOutputDir(None)
73 def tearDownClass(cls):
74 tools._RemoveOutputDir()
76 def _WritePythonString(self, fname, data):
77 """Write a string with tabs expanded as done in this Python file
80 fname: Filename to write to
81 data: Raw string to convert
83 data = data.replace('\t', '\\t')
84 with open(fname, 'w') as fd:
87 def _CheckStrings(self, expected, actual):
88 """Check that a string matches its expected value
90 If the strings do not match, they are written to the /tmp directory in
91 the same Python format as is used here in the test. This allows for
92 easy comparison and update of the tests.
95 expected: Expected string
98 if expected != actual:
99 self._WritePythonString('/tmp/binman.expected', expected)
100 self._WritePythonString('/tmp/binman.actual', actual)
101 print('Failures written to /tmp/binman.{expected,actual}')
102 self.assertEquals(expected, actual)
105 """Test conversion of device tree names to C identifiers"""
106 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
107 self.assertEqual('vendor_clock_frequency',
108 conv_name_to_c('vendor,clock-frequency'))
109 self.assertEqual('rockchip_rk3399_sdhci_5_1',
110 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
112 def test_tab_to(self):
113 """Test operation of tab_to() function"""
114 self.assertEqual('fred ', tab_to(0, 'fred'))
115 self.assertEqual('fred\t', tab_to(1, 'fred'))
116 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
117 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
118 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
119 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
121 def test_get_value(self):
122 """Test operation of get_value() function"""
123 self.assertEqual('0x45',
124 get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
125 self.assertEqual('0x45',
126 get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
127 self.assertEqual('0x0',
128 get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
129 self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
130 self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
132 def test_get_compat_name(self):
133 """Test operation of get_compat_name() function"""
134 Prop = collections.namedtuple('Prop', ['value'])
135 Node = collections.namedtuple('Node', ['props'])
137 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
138 node = Node({'compatible': prop})
139 self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
140 get_compat_name(node))
142 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
143 node = Node({'compatible': prop})
144 self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
145 get_compat_name(node))
147 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
148 node = Node({'compatible': prop})
149 self.assertEqual(('rockchip_rk3399_sdhci_5_1',
150 ['arasan_sdhci_5_1', 'third']),
151 get_compat_name(node))
153 def test_empty_file(self):
154 """Test output from a device tree file with no nodes"""
155 dtb_file = get_dtb_file('dtoc_test_empty.dts')
156 output = tools.GetOutputFilename('output')
157 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
158 with open(output) as infile:
159 lines = infile.read().splitlines()
160 self.assertEqual(HEADER.splitlines(), lines)
162 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
163 with open(output) as infile:
164 lines = infile.read().splitlines()
165 self.assertEqual(C_HEADER.splitlines() + [''], lines)
167 def test_simple(self):
168 """Test output from some simple nodes with various types of data"""
169 dtb_file = get_dtb_file('dtoc_test_simple.dts')
170 output = tools.GetOutputFilename('output')
171 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
172 with open(output) as infile:
174 self._CheckStrings(HEADER + '''
175 struct dtd_sandbox_i2c_test {
177 struct dtd_sandbox_pmic_test {
181 struct dtd_sandbox_spl_test {
183 \tunsigned char\tbytearray[3];
184 \tunsigned char\tbyteval;
185 \tfdt32_t\t\tintarray[4];
187 \tunsigned char\tlongbytearray[9];
188 \tunsigned char\tnotstring[5];
189 \tconst char *\tstringarray[3];
190 \tconst char *\tstringval;
192 struct dtd_sandbox_spl_test_2 {
196 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
197 with open(output) as infile:
199 self._CheckStrings(C_HEADER + '''
200 static const struct dtd_sandbox_spl_test dtv_spl_test = {
201 \t.boolval\t\t= true,
202 \t.bytearray\t\t= {0x6, 0x0, 0x0},
204 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
205 \t.intval\t\t\t= 0x1,
206 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
208 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
209 \t.stringarray\t\t= {"multi-word", "message", ""},
210 \t.stringval\t\t= "message",
212 U_BOOT_DEVICE(spl_test) = {
213 \t.name\t\t= "sandbox_spl_test",
214 \t.platdata\t= &dtv_spl_test,
215 \t.platdata_size\t= sizeof(dtv_spl_test),
218 static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
219 \t.bytearray\t\t= {0x1, 0x23, 0x34},
221 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
222 \t.intval\t\t\t= 0x3,
223 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
225 \t.stringarray\t\t= {"another", "multi-word", "message"},
226 \t.stringval\t\t= "message2",
228 U_BOOT_DEVICE(spl_test2) = {
229 \t.name\t\t= "sandbox_spl_test",
230 \t.platdata\t= &dtv_spl_test2,
231 \t.platdata_size\t= sizeof(dtv_spl_test2),
234 static const struct dtd_sandbox_spl_test dtv_spl_test3 = {
235 \t.stringarray\t\t= {"one", "", ""},
237 U_BOOT_DEVICE(spl_test3) = {
238 \t.name\t\t= "sandbox_spl_test",
239 \t.platdata\t= &dtv_spl_test3,
240 \t.platdata_size\t= sizeof(dtv_spl_test3),
243 static const struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
245 U_BOOT_DEVICE(spl_test4) = {
246 \t.name\t\t= "sandbox_spl_test_2",
247 \t.platdata\t= &dtv_spl_test4,
248 \t.platdata_size\t= sizeof(dtv_spl_test4),
251 static const struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
253 U_BOOT_DEVICE(i2c_at_0) = {
254 \t.name\t\t= "sandbox_i2c_test",
255 \t.platdata\t= &dtv_i2c_at_0,
256 \t.platdata_size\t= sizeof(dtv_i2c_at_0),
259 static const struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
260 \t.low_power\t\t= true,
261 \t.reg\t\t\t= {0x9, 0x0},
263 U_BOOT_DEVICE(pmic_at_9) = {
264 \t.name\t\t= "sandbox_pmic_test",
265 \t.platdata\t= &dtv_pmic_at_9,
266 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
271 def test_phandle(self):
272 """Test output from a node containing a phandle reference"""
273 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
274 output = tools.GetOutputFilename('output')
275 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
276 with open(output) as infile:
278 self._CheckStrings(HEADER + '''
280 \tstruct phandle_2_arg clocks[4];
287 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
288 with open(output) as infile:
290 self._CheckStrings(C_HEADER + '''
291 static const struct dtd_target dtv_phandle_target = {
292 \t.intval\t\t\t= 0x0,
294 U_BOOT_DEVICE(phandle_target) = {
295 \t.name\t\t= "target",
296 \t.platdata\t= &dtv_phandle_target,
297 \t.platdata_size\t= sizeof(dtv_phandle_target),
300 static const struct dtd_target dtv_phandle2_target = {
301 \t.intval\t\t\t= 0x1,
303 U_BOOT_DEVICE(phandle2_target) = {
304 \t.name\t\t= "target",
305 \t.platdata\t= &dtv_phandle2_target,
306 \t.platdata_size\t= sizeof(dtv_phandle2_target),
309 static const struct dtd_target dtv_phandle3_target = {
310 \t.intval\t\t\t= 0x2,
312 U_BOOT_DEVICE(phandle3_target) = {
313 \t.name\t\t= "target",
314 \t.platdata\t= &dtv_phandle3_target,
315 \t.platdata_size\t= sizeof(dtv_phandle3_target),
318 static const struct dtd_source dtv_phandle_source = {
320 \t\t\t{&dtv_phandle_target, {}},
321 \t\t\t{&dtv_phandle2_target, {11}},
322 \t\t\t{&dtv_phandle3_target, {12, 13}},
323 \t\t\t{&dtv_phandle_target, {}},},
325 U_BOOT_DEVICE(phandle_source) = {
326 \t.name\t\t= "source",
327 \t.platdata\t= &dtv_phandle_source,
328 \t.platdata_size\t= sizeof(dtv_phandle_source),
331 static const struct dtd_source dtv_phandle_source2 = {
333 \t\t\t{&dtv_phandle_target, {}},},
335 U_BOOT_DEVICE(phandle_source2) = {
336 \t.name\t\t= "source",
337 \t.platdata\t= &dtv_phandle_source2,
338 \t.platdata_size\t= sizeof(dtv_phandle_source2),
343 def test_phandle_single(self):
344 """Test output from a node containing a phandle reference"""
345 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
346 output = tools.GetOutputFilename('output')
347 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
348 with open(output) as infile:
350 self._CheckStrings(HEADER + '''
352 \tstruct phandle_0_arg clocks[1];
359 def test_phandle_reorder(self):
360 """Test that phandle targets are generated before their references"""
361 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
362 output = tools.GetOutputFilename('output')
363 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
364 with open(output) as infile:
366 self._CheckStrings(C_HEADER + '''
367 static const struct dtd_target dtv_phandle_target = {
369 U_BOOT_DEVICE(phandle_target) = {
370 \t.name\t\t= "target",
371 \t.platdata\t= &dtv_phandle_target,
372 \t.platdata_size\t= sizeof(dtv_phandle_target),
375 static const struct dtd_source dtv_phandle_source2 = {
377 \t\t\t{&dtv_phandle_target, {}},},
379 U_BOOT_DEVICE(phandle_source2) = {
380 \t.name\t\t= "source",
381 \t.platdata\t= &dtv_phandle_source2,
382 \t.platdata_size\t= sizeof(dtv_phandle_source2),
387 def test_phandle_bad(self):
388 """Test a node containing an invalid phandle fails"""
389 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
391 output = tools.GetOutputFilename('output')
392 with self.assertRaises(ValueError) as e:
393 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
394 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
397 def test_phandle_bad2(self):
398 """Test a phandle target missing its #*-cells property"""
399 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
401 output = tools.GetOutputFilename('output')
402 with self.assertRaises(ValueError) as e:
403 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
404 self.assertIn("Node 'phandle-target' has no '#clock-cells' property",
407 def test_aliases(self):
408 """Test output from a node with multiple compatible strings"""
409 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
410 output = tools.GetOutputFilename('output')
411 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
412 with open(output) as infile:
414 self._CheckStrings(HEADER + '''
418 #define dtd_compat2_1_fred dtd_compat1
419 #define dtd_compat3 dtd_compat1
422 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
423 with open(output) as infile:
425 self._CheckStrings(C_HEADER + '''
426 static const struct dtd_compat1 dtv_spl_test = {
427 \t.intval\t\t\t= 0x1,
429 U_BOOT_DEVICE(spl_test) = {
430 \t.name\t\t= "compat1",
431 \t.platdata\t= &dtv_spl_test,
432 \t.platdata_size\t= sizeof(dtv_spl_test),
437 def test_addresses64(self):
438 """Test output from a node with a 'reg' property with na=2, ns=2"""
439 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
440 output = tools.GetOutputFilename('output')
441 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
442 with open(output) as infile:
444 self._CheckStrings(HEADER + '''
456 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
457 with open(output) as infile:
459 self._CheckStrings(C_HEADER + '''
460 static const struct dtd_test1 dtv_test1 = {
461 \t.reg\t\t\t= {0x1234, 0x5678},
463 U_BOOT_DEVICE(test1) = {
464 \t.name\t\t= "test1",
465 \t.platdata\t= &dtv_test1,
466 \t.platdata_size\t= sizeof(dtv_test1),
469 static const struct dtd_test2 dtv_test2 = {
470 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
472 U_BOOT_DEVICE(test2) = {
473 \t.name\t\t= "test2",
474 \t.platdata\t= &dtv_test2,
475 \t.platdata_size\t= sizeof(dtv_test2),
478 static const struct dtd_test3 dtv_test3 = {
479 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
481 U_BOOT_DEVICE(test3) = {
482 \t.name\t\t= "test3",
483 \t.platdata\t= &dtv_test3,
484 \t.platdata_size\t= sizeof(dtv_test3),
489 def test_addresses32(self):
490 """Test output from a node with a 'reg' property with na=1, ns=1"""
491 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
492 output = tools.GetOutputFilename('output')
493 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
494 with open(output) as infile:
496 self._CheckStrings(HEADER + '''
505 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
506 with open(output) as infile:
508 self._CheckStrings(C_HEADER + '''
509 static const struct dtd_test1 dtv_test1 = {
510 \t.reg\t\t\t= {0x1234, 0x5678},
512 U_BOOT_DEVICE(test1) = {
513 \t.name\t\t= "test1",
514 \t.platdata\t= &dtv_test1,
515 \t.platdata_size\t= sizeof(dtv_test1),
518 static const struct dtd_test2 dtv_test2 = {
519 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
521 U_BOOT_DEVICE(test2) = {
522 \t.name\t\t= "test2",
523 \t.platdata\t= &dtv_test2,
524 \t.platdata_size\t= sizeof(dtv_test2),
529 def test_addresses64_32(self):
530 """Test output from a node with a 'reg' property with na=2, ns=1"""
531 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
532 output = tools.GetOutputFilename('output')
533 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
534 with open(output) as infile:
536 self._CheckStrings(HEADER + '''
548 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
549 with open(output) as infile:
551 self._CheckStrings(C_HEADER + '''
552 static const struct dtd_test1 dtv_test1 = {
553 \t.reg\t\t\t= {0x123400000000, 0x5678},
555 U_BOOT_DEVICE(test1) = {
556 \t.name\t\t= "test1",
557 \t.platdata\t= &dtv_test1,
558 \t.platdata_size\t= sizeof(dtv_test1),
561 static const struct dtd_test2 dtv_test2 = {
562 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
564 U_BOOT_DEVICE(test2) = {
565 \t.name\t\t= "test2",
566 \t.platdata\t= &dtv_test2,
567 \t.platdata_size\t= sizeof(dtv_test2),
570 static const struct dtd_test3 dtv_test3 = {
571 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
573 U_BOOT_DEVICE(test3) = {
574 \t.name\t\t= "test3",
575 \t.platdata\t= &dtv_test3,
576 \t.platdata_size\t= sizeof(dtv_test3),
581 def test_addresses32_64(self):
582 """Test output from a node with a 'reg' property with na=1, ns=2"""
583 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
584 output = tools.GetOutputFilename('output')
585 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
586 with open(output) as infile:
588 self._CheckStrings(HEADER + '''
600 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
601 with open(output) as infile:
603 self._CheckStrings(C_HEADER + '''
604 static const struct dtd_test1 dtv_test1 = {
605 \t.reg\t\t\t= {0x1234, 0x567800000000},
607 U_BOOT_DEVICE(test1) = {
608 \t.name\t\t= "test1",
609 \t.platdata\t= &dtv_test1,
610 \t.platdata_size\t= sizeof(dtv_test1),
613 static const struct dtd_test2 dtv_test2 = {
614 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
616 U_BOOT_DEVICE(test2) = {
617 \t.name\t\t= "test2",
618 \t.platdata\t= &dtv_test2,
619 \t.platdata_size\t= sizeof(dtv_test2),
622 static const struct dtd_test3 dtv_test3 = {
623 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
625 U_BOOT_DEVICE(test3) = {
626 \t.name\t\t= "test3",
627 \t.platdata\t= &dtv_test3,
628 \t.platdata_size\t= sizeof(dtv_test3),
633 def test_bad_reg(self):
634 """Test that a reg property with an invalid type generates an error"""
635 # Capture stderr since dtc will emit warnings for this file
636 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
637 output = tools.GetOutputFilename('output')
638 with self.assertRaises(ValueError) as e:
639 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
640 self.assertIn("Node 'spl-test' reg property is not an int",
643 def test_bad_reg2(self):
644 """Test that a reg property with an invalid cell count is detected"""
645 # Capture stderr since dtc will emit warnings for this file
646 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
647 output = tools.GetOutputFilename('output')
648 with self.assertRaises(ValueError) as e:
649 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
650 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
653 def test_add_prop(self):
654 """Test that a subequent node can add a new property to a struct"""
655 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
656 output = tools.GetOutputFilename('output')
657 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
658 with open(output) as infile:
660 self._CheckStrings(HEADER + '''
661 struct dtd_sandbox_spl_test {
662 \tfdt32_t\t\tintarray;
667 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
668 with open(output) as infile:
670 self._CheckStrings(C_HEADER + '''
671 static const struct dtd_sandbox_spl_test dtv_spl_test = {
672 \t.intval\t\t\t= 0x1,
674 U_BOOT_DEVICE(spl_test) = {
675 \t.name\t\t= "sandbox_spl_test",
676 \t.platdata\t= &dtv_spl_test,
677 \t.platdata_size\t= sizeof(dtv_spl_test),
680 static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
681 \t.intarray\t\t= 0x5,
683 U_BOOT_DEVICE(spl_test2) = {
684 \t.name\t\t= "sandbox_spl_test",
685 \t.platdata\t= &dtv_spl_test2,
686 \t.platdata_size\t= sizeof(dtv_spl_test2),
691 def testStdout(self):
692 """Test output to stdout"""
693 dtb_file = get_dtb_file('dtoc_test_simple.dts')
694 with test_util.capture_sys_output() as (stdout, stderr):
695 dtb_platdata.run_steps(['struct'], dtb_file, False, '-')
697 def testNoCommand(self):
698 """Test running dtoc without a command"""
699 with self.assertRaises(ValueError) as e:
700 dtb_platdata.run_steps([], '', False, '')
701 self.assertIn("Please specify a command: struct, platdata",
704 def testBadCommand(self):
705 """Test running dtoc with an invalid command"""
706 dtb_file = get_dtb_file('dtoc_test_simple.dts')
707 output = tools.GetOutputFilename('output')
708 with self.assertRaises(ValueError) as e:
709 dtb_platdata.run_steps(['invalid-cmd'], dtb_file, False, output)
710 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",