1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2012 The Chromium OS Authors.
5 """Tests for the dtb_platdata module
7 This includes unit tests for some functions and functional tests for the dtoc
11 from __future__ import print_function
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
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>
53 def get_dtb_file(dts_fname, capture_stderr=False):
54 """Compile a .dts file to a .dtb
57 dts_fname: Filename of .dts file in the current directory
58 capture_stderr: True to capture and discard stderr output
61 Filename of compiled file in output directory
63 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
64 capture_stderr=capture_stderr)
67 class TestDtoc(unittest.TestCase):
71 tools.PrepareOutputDir(None)
74 def tearDownClass(cls):
75 tools._RemoveOutputDir()
77 def _WritePythonString(self, fname, data):
78 """Write a string with tabs expanded as done in this Python file
81 fname: Filename to write to
82 data: Raw string to convert
84 data = data.replace('\t', '\\t')
85 with open(fname, 'w') as fd:
88 def _CheckStrings(self, expected, actual):
89 """Check that a string matches its expected value
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.
96 expected: Expected string
99 if expected != actual:
100 self._WritePythonString('/tmp/binman.expected', expected)
101 self._WritePythonString('/tmp/binman.actual', actual)
102 print('Failures written to /tmp/binman.{expected,actual}')
103 self.assertEquals(expected, actual)
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'))
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'))
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))
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'])
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))
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))
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))
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()
161 self.assertEqual(HEADER.splitlines(), lines)
163 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
164 with open(output) as infile:
165 lines = infile.read().splitlines()
166 self.assertEqual(C_HEADER.splitlines() + [''], lines)
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:
175 self._CheckStrings(HEADER + '''
176 struct dtd_sandbox_i2c_test {
178 struct dtd_sandbox_pmic_test {
182 struct dtd_sandbox_spl_test {
184 \tunsigned char\tbytearray[3];
185 \tunsigned char\tbyteval;
186 \tfdt32_t\t\tintarray[4];
188 \tunsigned char\tlongbytearray[9];
189 \tunsigned char\tnotstring[5];
190 \tconst char *\tstringarray[3];
191 \tconst char *\tstringval;
193 struct dtd_sandbox_spl_test_2 {
197 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
198 with open(output) as infile:
200 self._CheckStrings(C_HEADER + '''
201 static const struct dtd_sandbox_spl_test dtv_spl_test = {
202 \t.boolval\t\t= true,
203 \t.bytearray\t\t= {0x6, 0x0, 0x0},
205 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
206 \t.intval\t\t\t= 0x1,
207 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
209 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
210 \t.stringarray\t\t= {"multi-word", "message", ""},
211 \t.stringval\t\t= "message",
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),
219 static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
220 \t.bytearray\t\t= {0x1, 0x23, 0x34},
222 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
223 \t.intval\t\t\t= 0x3,
224 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
226 \t.stringarray\t\t= {"another", "multi-word", "message"},
227 \t.stringval\t\t= "message2",
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),
235 static const struct dtd_sandbox_spl_test dtv_spl_test3 = {
236 \t.stringarray\t\t= {"one", "", ""},
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),
244 static const struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
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),
252 static const struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
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),
260 static const struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
261 \t.low_power\t\t= true,
262 \t.reg\t\t\t= {0x9, 0x0},
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),
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:
279 self._CheckStrings(HEADER + '''
281 \tstruct phandle_2_arg clocks[4];
288 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
289 with open(output) as infile:
291 self._CheckStrings(C_HEADER + '''
292 static const struct dtd_target dtv_phandle_target = {
293 \t.intval\t\t\t= 0x0,
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),
301 static const struct dtd_target dtv_phandle2_target = {
302 \t.intval\t\t\t= 0x1,
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),
310 static const struct dtd_target dtv_phandle3_target = {
311 \t.intval\t\t\t= 0x2,
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),
319 static const struct dtd_source dtv_phandle_source = {
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, {}},},
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),
332 static const struct dtd_source dtv_phandle_source2 = {
334 \t\t\t{&dtv_phandle_target, {}},},
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),
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:
351 self._CheckStrings(HEADER + '''
353 \tstruct phandle_0_arg clocks[1];
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:
367 self._CheckStrings(C_HEADER + '''
368 static const struct dtd_target dtv_phandle_target = {
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),
376 static const struct dtd_source dtv_phandle_source2 = {
378 \t\t\t{&dtv_phandle_target, {}},},
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),
388 def test_phandle_bad(self):
389 """Test a node containing an invalid phandle fails"""
390 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
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'",
398 def test_phandle_bad2(self):
399 """Test a phandle target missing its #*-cells property"""
400 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
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",
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:
415 self._CheckStrings(HEADER + '''
419 #define dtd_compat2_1_fred dtd_compat1
420 #define dtd_compat3 dtd_compat1
423 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
424 with open(output) as infile:
426 self._CheckStrings(C_HEADER + '''
427 static const struct dtd_compat1 dtv_spl_test = {
428 \t.intval\t\t\t= 0x1,
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),
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:
445 self._CheckStrings(HEADER + '''
457 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
458 with open(output) as infile:
460 self._CheckStrings(C_HEADER + '''
461 static const struct dtd_test1 dtv_test1 = {
462 \t.reg\t\t\t= {0x1234, 0x5678},
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),
470 static const struct dtd_test2 dtv_test2 = {
471 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
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),
479 static const struct dtd_test3 dtv_test3 = {
480 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
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),
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:
497 self._CheckStrings(HEADER + '''
506 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
507 with open(output) as infile:
509 self._CheckStrings(C_HEADER + '''
510 static const struct dtd_test1 dtv_test1 = {
511 \t.reg\t\t\t= {0x1234, 0x5678},
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),
519 static const struct dtd_test2 dtv_test2 = {
520 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
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),
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:
537 self._CheckStrings(HEADER + '''
549 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
550 with open(output) as infile:
552 self._CheckStrings(C_HEADER + '''
553 static const struct dtd_test1 dtv_test1 = {
554 \t.reg\t\t\t= {0x123400000000, 0x5678},
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),
562 static const struct dtd_test2 dtv_test2 = {
563 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
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),
571 static const struct dtd_test3 dtv_test3 = {
572 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
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),
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:
589 self._CheckStrings(HEADER + '''
601 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
602 with open(output) as infile:
604 self._CheckStrings(C_HEADER + '''
605 static const struct dtd_test1 dtv_test1 = {
606 \t.reg\t\t\t= {0x1234, 0x567800000000},
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),
614 static const struct dtd_test2 dtv_test2 = {
615 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
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),
623 static const struct dtd_test3 dtv_test3 = {
624 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
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),
634 def test_bad_reg(self):
635 """Test that a reg property with an invalid type generates an error"""
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)
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",
644 def test_bad_reg2(self):
645 """Test that a reg property with an invalid cell count is detected"""
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)
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)",
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:
661 self._CheckStrings(HEADER + '''
662 struct dtd_sandbox_spl_test {
663 \tfdt32_t\t\tintarray;
668 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
669 with open(output) as infile:
671 self._CheckStrings(C_HEADER + '''
672 static const struct dtd_sandbox_spl_test dtv_spl_test = {
673 \t.intval\t\t\t= 0x1,
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),
681 static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
682 \t.intarray\t\t= 0x5,
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),
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, '-')
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",
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)",