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
12 from __future__ import print_function
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
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>
54 def get_dtb_file(dts_fname, capture_stderr=False):
55 """Compile a .dts file to a .dtb
58 dts_fname: Filename of .dts file in the current directory
59 capture_stderr: True to capture and discard stderr output
62 Filename of compiled file in output directory
64 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
65 capture_stderr=capture_stderr)
68 class TestDtoc(unittest.TestCase):
72 tools.PrepareOutputDir(None)
75 def tearDownClass(cls):
76 tools._RemoveOutputDir()
78 def _WritePythonString(self, fname, data):
79 """Write a string with tabs expanded as done in this Python file
82 fname: Filename to write to
83 data: Raw string to convert
85 data = data.replace('\t', '\\t')
86 with open(fname, 'w') as fd:
89 def _CheckStrings(self, expected, actual):
90 """Check that a string matches its expected value
92 If the strings do not match, they are written to the /tmp directory in
93 the same Python format as is used here in the test. This allows for
94 easy comparison and update of the tests.
97 expected: Expected string
100 if expected != actual:
101 self._WritePythonString('/tmp/binman.expected', expected)
102 self._WritePythonString('/tmp/binman.actual', actual)
103 print('Failures written to /tmp/binman.{expected,actual}')
104 self.assertEquals(expected, actual)
107 """Test conversion of device tree names to C identifiers"""
108 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
109 self.assertEqual('vendor_clock_frequency',
110 conv_name_to_c('vendor,clock-frequency'))
111 self.assertEqual('rockchip_rk3399_sdhci_5_1',
112 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
114 def test_tab_to(self):
115 """Test operation of tab_to() function"""
116 self.assertEqual('fred ', tab_to(0, 'fred'))
117 self.assertEqual('fred\t', tab_to(1, 'fred'))
118 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
119 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
120 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
121 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
123 def test_get_value(self):
124 """Test operation of get_value() function"""
125 self.assertEqual('0x45',
126 get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
127 self.assertEqual('0x45',
128 get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
129 self.assertEqual('0x0',
130 get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
131 self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
132 self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
134 def test_get_compat_name(self):
135 """Test operation of get_compat_name() function"""
136 Prop = collections.namedtuple('Prop', ['value'])
137 Node = collections.namedtuple('Node', ['props'])
139 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
140 node = Node({'compatible': prop})
141 self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
142 get_compat_name(node))
144 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
145 node = Node({'compatible': prop})
146 self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
147 get_compat_name(node))
149 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
150 node = Node({'compatible': prop})
151 self.assertEqual(('rockchip_rk3399_sdhci_5_1',
152 ['arasan_sdhci_5_1', 'third']),
153 get_compat_name(node))
155 def test_empty_file(self):
156 """Test output from a device tree file with no nodes"""
157 dtb_file = get_dtb_file('dtoc_test_empty.dts')
158 output = tools.GetOutputFilename('output')
159 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
160 with open(output) as infile:
161 lines = infile.read().splitlines()
162 self.assertEqual(HEADER.splitlines(), lines)
164 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
165 with open(output) as infile:
166 lines = infile.read().splitlines()
167 self.assertEqual(C_HEADER.splitlines() + [''], lines)
169 def test_simple(self):
170 """Test output from some simple nodes with various types of data"""
171 dtb_file = get_dtb_file('dtoc_test_simple.dts')
172 output = tools.GetOutputFilename('output')
173 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
174 with open(output) as infile:
176 self._CheckStrings(HEADER + '''
177 struct dtd_sandbox_i2c_test {
179 struct dtd_sandbox_pmic_test {
183 struct dtd_sandbox_spl_test {
185 \tunsigned char\tbytearray[3];
186 \tunsigned char\tbyteval;
187 \tfdt32_t\t\tintarray[4];
189 \tunsigned char\tlongbytearray[9];
190 \tunsigned char\tnotstring[5];
191 \tconst char *\tstringarray[3];
192 \tconst char *\tstringval;
194 struct dtd_sandbox_spl_test_2 {
198 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
199 with open(output) as infile:
201 self._CheckStrings(C_HEADER + '''
202 static const struct dtd_sandbox_spl_test dtv_spl_test = {
203 \t.boolval\t\t= true,
204 \t.bytearray\t\t= {0x6, 0x0, 0x0},
206 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
207 \t.intval\t\t\t= 0x1,
208 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
210 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
211 \t.stringarray\t\t= {"multi-word", "message", ""},
212 \t.stringval\t\t= "message",
214 U_BOOT_DEVICE(spl_test) = {
215 \t.name\t\t= "sandbox_spl_test",
216 \t.platdata\t= &dtv_spl_test,
217 \t.platdata_size\t= sizeof(dtv_spl_test),
220 static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
221 \t.bytearray\t\t= {0x1, 0x23, 0x34},
223 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
224 \t.intval\t\t\t= 0x3,
225 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
227 \t.stringarray\t\t= {"another", "multi-word", "message"},
228 \t.stringval\t\t= "message2",
230 U_BOOT_DEVICE(spl_test2) = {
231 \t.name\t\t= "sandbox_spl_test",
232 \t.platdata\t= &dtv_spl_test2,
233 \t.platdata_size\t= sizeof(dtv_spl_test2),
236 static const struct dtd_sandbox_spl_test dtv_spl_test3 = {
237 \t.stringarray\t\t= {"one", "", ""},
239 U_BOOT_DEVICE(spl_test3) = {
240 \t.name\t\t= "sandbox_spl_test",
241 \t.platdata\t= &dtv_spl_test3,
242 \t.platdata_size\t= sizeof(dtv_spl_test3),
245 static const struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
247 U_BOOT_DEVICE(spl_test4) = {
248 \t.name\t\t= "sandbox_spl_test_2",
249 \t.platdata\t= &dtv_spl_test4,
250 \t.platdata_size\t= sizeof(dtv_spl_test4),
253 static const struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
255 U_BOOT_DEVICE(i2c_at_0) = {
256 \t.name\t\t= "sandbox_i2c_test",
257 \t.platdata\t= &dtv_i2c_at_0,
258 \t.platdata_size\t= sizeof(dtv_i2c_at_0),
261 static const struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
262 \t.low_power\t\t= true,
263 \t.reg\t\t\t= {0x9, 0x0},
265 U_BOOT_DEVICE(pmic_at_9) = {
266 \t.name\t\t= "sandbox_pmic_test",
267 \t.platdata\t= &dtv_pmic_at_9,
268 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
273 def test_phandle(self):
274 """Test output from a node containing a phandle reference"""
275 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
276 output = tools.GetOutputFilename('output')
277 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
278 with open(output) as infile:
280 self._CheckStrings(HEADER + '''
282 \tstruct phandle_2_arg clocks[4];
289 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
290 with open(output) as infile:
292 self._CheckStrings(C_HEADER + '''
293 static const struct dtd_target dtv_phandle_target = {
294 \t.intval\t\t\t= 0x0,
296 U_BOOT_DEVICE(phandle_target) = {
297 \t.name\t\t= "target",
298 \t.platdata\t= &dtv_phandle_target,
299 \t.platdata_size\t= sizeof(dtv_phandle_target),
302 static const struct dtd_target dtv_phandle2_target = {
303 \t.intval\t\t\t= 0x1,
305 U_BOOT_DEVICE(phandle2_target) = {
306 \t.name\t\t= "target",
307 \t.platdata\t= &dtv_phandle2_target,
308 \t.platdata_size\t= sizeof(dtv_phandle2_target),
311 static const struct dtd_target dtv_phandle3_target = {
312 \t.intval\t\t\t= 0x2,
314 U_BOOT_DEVICE(phandle3_target) = {
315 \t.name\t\t= "target",
316 \t.platdata\t= &dtv_phandle3_target,
317 \t.platdata_size\t= sizeof(dtv_phandle3_target),
320 static const struct dtd_source dtv_phandle_source = {
322 \t\t\t{&dtv_phandle_target, {}},
323 \t\t\t{&dtv_phandle2_target, {11}},
324 \t\t\t{&dtv_phandle3_target, {12, 13}},
325 \t\t\t{&dtv_phandle_target, {}},},
327 U_BOOT_DEVICE(phandle_source) = {
328 \t.name\t\t= "source",
329 \t.platdata\t= &dtv_phandle_source,
330 \t.platdata_size\t= sizeof(dtv_phandle_source),
333 static const struct dtd_source dtv_phandle_source2 = {
335 \t\t\t{&dtv_phandle_target, {}},},
337 U_BOOT_DEVICE(phandle_source2) = {
338 \t.name\t\t= "source",
339 \t.platdata\t= &dtv_phandle_source2,
340 \t.platdata_size\t= sizeof(dtv_phandle_source2),
345 def test_phandle_single(self):
346 """Test output from a node containing a phandle reference"""
347 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
348 output = tools.GetOutputFilename('output')
349 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
350 with open(output) as infile:
352 self._CheckStrings(HEADER + '''
354 \tstruct phandle_0_arg clocks[1];
361 def test_phandle_reorder(self):
362 """Test that phandle targets are generated before their references"""
363 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
364 output = tools.GetOutputFilename('output')
365 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
366 with open(output) as infile:
368 self._CheckStrings(C_HEADER + '''
369 static const struct dtd_target dtv_phandle_target = {
371 U_BOOT_DEVICE(phandle_target) = {
372 \t.name\t\t= "target",
373 \t.platdata\t= &dtv_phandle_target,
374 \t.platdata_size\t= sizeof(dtv_phandle_target),
377 static const struct dtd_source dtv_phandle_source2 = {
379 \t\t\t{&dtv_phandle_target, {}},},
381 U_BOOT_DEVICE(phandle_source2) = {
382 \t.name\t\t= "source",
383 \t.platdata\t= &dtv_phandle_source2,
384 \t.platdata_size\t= sizeof(dtv_phandle_source2),
389 def test_phandle_bad(self):
390 """Test a node containing an invalid phandle fails"""
391 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
393 output = tools.GetOutputFilename('output')
394 with self.assertRaises(ValueError) as e:
395 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
396 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
399 def test_phandle_bad2(self):
400 """Test a phandle target missing its #*-cells property"""
401 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
403 output = tools.GetOutputFilename('output')
404 with self.assertRaises(ValueError) as e:
405 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
406 self.assertIn("Node 'phandle-target' has no '#clock-cells' property",
409 def test_aliases(self):
410 """Test output from a node with multiple compatible strings"""
411 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
412 output = tools.GetOutputFilename('output')
413 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
414 with open(output) as infile:
416 self._CheckStrings(HEADER + '''
420 #define dtd_compat2_1_fred dtd_compat1
421 #define dtd_compat3 dtd_compat1
424 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
425 with open(output) as infile:
427 self._CheckStrings(C_HEADER + '''
428 static const struct dtd_compat1 dtv_spl_test = {
429 \t.intval\t\t\t= 0x1,
431 U_BOOT_DEVICE(spl_test) = {
432 \t.name\t\t= "compat1",
433 \t.platdata\t= &dtv_spl_test,
434 \t.platdata_size\t= sizeof(dtv_spl_test),
439 def test_addresses64(self):
440 """Test output from a node with a 'reg' property with na=2, ns=2"""
441 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
442 output = tools.GetOutputFilename('output')
443 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
444 with open(output) as infile:
446 self._CheckStrings(HEADER + '''
458 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
459 with open(output) as infile:
461 self._CheckStrings(C_HEADER + '''
462 static const struct dtd_test1 dtv_test1 = {
463 \t.reg\t\t\t= {0x1234, 0x5678},
465 U_BOOT_DEVICE(test1) = {
466 \t.name\t\t= "test1",
467 \t.platdata\t= &dtv_test1,
468 \t.platdata_size\t= sizeof(dtv_test1),
471 static const struct dtd_test2 dtv_test2 = {
472 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
474 U_BOOT_DEVICE(test2) = {
475 \t.name\t\t= "test2",
476 \t.platdata\t= &dtv_test2,
477 \t.platdata_size\t= sizeof(dtv_test2),
480 static const struct dtd_test3 dtv_test3 = {
481 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
483 U_BOOT_DEVICE(test3) = {
484 \t.name\t\t= "test3",
485 \t.platdata\t= &dtv_test3,
486 \t.platdata_size\t= sizeof(dtv_test3),
491 def test_addresses32(self):
492 """Test output from a node with a 'reg' property with na=1, ns=1"""
493 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
494 output = tools.GetOutputFilename('output')
495 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
496 with open(output) as infile:
498 self._CheckStrings(HEADER + '''
507 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
508 with open(output) as infile:
510 self._CheckStrings(C_HEADER + '''
511 static const struct dtd_test1 dtv_test1 = {
512 \t.reg\t\t\t= {0x1234, 0x5678},
514 U_BOOT_DEVICE(test1) = {
515 \t.name\t\t= "test1",
516 \t.platdata\t= &dtv_test1,
517 \t.platdata_size\t= sizeof(dtv_test1),
520 static const struct dtd_test2 dtv_test2 = {
521 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
523 U_BOOT_DEVICE(test2) = {
524 \t.name\t\t= "test2",
525 \t.platdata\t= &dtv_test2,
526 \t.platdata_size\t= sizeof(dtv_test2),
531 def test_addresses64_32(self):
532 """Test output from a node with a 'reg' property with na=2, ns=1"""
533 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
534 output = tools.GetOutputFilename('output')
535 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
536 with open(output) as infile:
538 self._CheckStrings(HEADER + '''
550 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
551 with open(output) as infile:
553 self._CheckStrings(C_HEADER + '''
554 static const struct dtd_test1 dtv_test1 = {
555 \t.reg\t\t\t= {0x123400000000, 0x5678},
557 U_BOOT_DEVICE(test1) = {
558 \t.name\t\t= "test1",
559 \t.platdata\t= &dtv_test1,
560 \t.platdata_size\t= sizeof(dtv_test1),
563 static const struct dtd_test2 dtv_test2 = {
564 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
566 U_BOOT_DEVICE(test2) = {
567 \t.name\t\t= "test2",
568 \t.platdata\t= &dtv_test2,
569 \t.platdata_size\t= sizeof(dtv_test2),
572 static const struct dtd_test3 dtv_test3 = {
573 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
575 U_BOOT_DEVICE(test3) = {
576 \t.name\t\t= "test3",
577 \t.platdata\t= &dtv_test3,
578 \t.platdata_size\t= sizeof(dtv_test3),
583 def test_addresses32_64(self):
584 """Test output from a node with a 'reg' property with na=1, ns=2"""
585 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
586 output = tools.GetOutputFilename('output')
587 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
588 with open(output) as infile:
590 self._CheckStrings(HEADER + '''
602 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
603 with open(output) as infile:
605 self._CheckStrings(C_HEADER + '''
606 static const struct dtd_test1 dtv_test1 = {
607 \t.reg\t\t\t= {0x1234, 0x567800000000},
609 U_BOOT_DEVICE(test1) = {
610 \t.name\t\t= "test1",
611 \t.platdata\t= &dtv_test1,
612 \t.platdata_size\t= sizeof(dtv_test1),
615 static const struct dtd_test2 dtv_test2 = {
616 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
618 U_BOOT_DEVICE(test2) = {
619 \t.name\t\t= "test2",
620 \t.platdata\t= &dtv_test2,
621 \t.platdata_size\t= sizeof(dtv_test2),
624 static const struct dtd_test3 dtv_test3 = {
625 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
627 U_BOOT_DEVICE(test3) = {
628 \t.name\t\t= "test3",
629 \t.platdata\t= &dtv_test3,
630 \t.platdata_size\t= sizeof(dtv_test3),
635 def test_bad_reg(self):
636 """Test that a reg property with an invalid type generates an error"""
637 # Capture stderr since dtc will emit warnings for this file
638 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
639 output = tools.GetOutputFilename('output')
640 with self.assertRaises(ValueError) as e:
641 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
642 self.assertIn("Node 'spl-test' reg property is not an int",
645 def test_bad_reg2(self):
646 """Test that a reg property with an invalid cell count is detected"""
647 # Capture stderr since dtc will emit warnings for this file
648 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
649 output = tools.GetOutputFilename('output')
650 with self.assertRaises(ValueError) as e:
651 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
652 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
655 def test_add_prop(self):
656 """Test that a subequent node can add a new property to a struct"""
657 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
658 output = tools.GetOutputFilename('output')
659 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
660 with open(output) as infile:
662 self._CheckStrings(HEADER + '''
663 struct dtd_sandbox_spl_test {
664 \tfdt32_t\t\tintarray;
669 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
670 with open(output) as infile:
672 self._CheckStrings(C_HEADER + '''
673 static const struct dtd_sandbox_spl_test dtv_spl_test = {
674 \t.intval\t\t\t= 0x1,
676 U_BOOT_DEVICE(spl_test) = {
677 \t.name\t\t= "sandbox_spl_test",
678 \t.platdata\t= &dtv_spl_test,
679 \t.platdata_size\t= sizeof(dtv_spl_test),
682 static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
683 \t.intarray\t\t= 0x5,
685 U_BOOT_DEVICE(spl_test2) = {
686 \t.name\t\t= "sandbox_spl_test",
687 \t.platdata\t= &dtv_spl_test2,
688 \t.platdata_size\t= sizeof(dtv_spl_test2),
693 def testStdout(self):
694 """Test output to stdout"""
695 dtb_file = get_dtb_file('dtoc_test_simple.dts')
696 with test_util.capture_sys_output() as (stdout, stderr):
697 dtb_platdata.run_steps(['struct'], dtb_file, False, '-')
699 def testNoCommand(self):
700 """Test running dtoc without a command"""
701 with self.assertRaises(ValueError) as e:
702 dtb_platdata.run_steps([], '', False, '')
703 self.assertIn("Please specify a command: struct, platdata",
706 def testBadCommand(self):
707 """Test running dtoc with an invalid command"""
708 dtb_file = get_dtb_file('dtoc_test_simple.dts')
709 output = tools.GetOutputFilename('output')
710 with self.assertRaises(ValueError) as e:
711 dtb_platdata.run_steps(['invalid-cmd'], dtb_file, False, output)
712 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",