dtoc: add option to disable warnings
[platform/kernel/u-boot.git] / tools / dtoc / test_dtoc.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright (c) 2012 The Chromium OS Authors.
4 #
5
6 """Tests for the dtb_platdata module
7
8 This includes unit tests for some functions and functional tests for the dtoc
9 tool.
10 """
11
12 import collections
13 import os
14 import struct
15 import sys
16 import unittest
17
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
23 from dtoc import fdt
24 from dtoc import fdt_util
25 from patman import test_util
26 from patman import tools
27
28 our_path = os.path.dirname(os.path.realpath(__file__))
29
30
31 HEADER = '''/*
32  * DO NOT MODIFY
33  *
34  * This file was generated by dtoc from a .dtb (device tree binary) file.
35  */
36
37 #include <stdbool.h>
38 #include <linux/libfdt.h>'''
39
40 C_HEADER = '''/*
41  * DO NOT MODIFY
42  *
43  * This file was generated by dtoc from a .dtb (device tree binary) file.
44  */
45
46 #include <common.h>
47 #include <dm.h>
48 #include <dt-structs.h>
49 '''
50
51
52
53 def get_dtb_file(dts_fname, capture_stderr=False):
54     """Compile a .dts file to a .dtb
55
56     Args:
57         dts_fname: Filename of .dts file in the current directory
58         capture_stderr: True to capture and discard stderr output
59
60     Returns:
61         Filename of compiled file in output directory
62     """
63     return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
64                                    capture_stderr=capture_stderr)
65
66
67 class TestDtoc(unittest.TestCase):
68     """Tests for dtoc"""
69     @classmethod
70     def setUpClass(cls):
71         tools.PrepareOutputDir(None)
72
73     @classmethod
74     def tearDownClass(cls):
75         tools._RemoveOutputDir()
76
77     def _WritePythonString(self, fname, data):
78         """Write a string with tabs expanded as done in this Python file
79
80         Args:
81             fname: Filename to write to
82             data: Raw string to convert
83         """
84         data = data.replace('\t', '\\t')
85         with open(fname, 'w') as fd:
86             fd.write(data)
87
88     def _CheckStrings(self, expected, actual):
89         """Check that a string matches its expected value
90
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.
94
95         Args:
96             expected: Expected string
97             actual: Actual string
98         """
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)
104
105
106     def run_test(self, args, dtb_file, output):
107         dtb_platdata.run_steps(args, dtb_file, False, output, True)
108
109     def test_name(self):
110         """Test conversion of device tree names to C identifiers"""
111         self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
112         self.assertEqual('vendor_clock_frequency',
113                          conv_name_to_c('vendor,clock-frequency'))
114         self.assertEqual('rockchip_rk3399_sdhci_5_1',
115                          conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
116
117     def test_tab_to(self):
118         """Test operation of tab_to() function"""
119         self.assertEqual('fred ', tab_to(0, 'fred'))
120         self.assertEqual('fred\t', tab_to(1, 'fred'))
121         self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
122         self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
123         self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
124         self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
125
126     def test_get_value(self):
127         """Test operation of get_value() function"""
128         self.assertEqual('0x45',
129                          get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
130         self.assertEqual('0x45',
131                          get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
132         self.assertEqual('0x0',
133                          get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
134         self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
135         self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
136
137     def test_get_compat_name(self):
138         """Test operation of get_compat_name() function"""
139         Prop = collections.namedtuple('Prop', ['value'])
140         Node = collections.namedtuple('Node', ['props'])
141
142         prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
143         node = Node({'compatible': prop})
144         self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
145                          get_compat_name(node))
146
147         prop = Prop(['rockchip,rk3399-sdhci-5.1'])
148         node = Node({'compatible': prop})
149         self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
150                          get_compat_name(node))
151
152         prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
153         node = Node({'compatible': prop})
154         self.assertEqual(('rockchip_rk3399_sdhci_5_1',
155                           ['arasan_sdhci_5_1', 'third']),
156                          get_compat_name(node))
157
158     def test_empty_file(self):
159         """Test output from a device tree file with no nodes"""
160         dtb_file = get_dtb_file('dtoc_test_empty.dts')
161         output = tools.GetOutputFilename('output')
162         self.run_test(['struct'], dtb_file, output)
163         with open(output) as infile:
164             lines = infile.read().splitlines()
165         self.assertEqual(HEADER.splitlines(), lines)
166
167         self.run_test(['platdata'], dtb_file, output)
168         with open(output) as infile:
169             lines = infile.read().splitlines()
170         self.assertEqual(C_HEADER.splitlines() + [''], lines)
171
172     def test_simple(self):
173         """Test output from some simple nodes with various types of data"""
174         dtb_file = get_dtb_file('dtoc_test_simple.dts')
175         output = tools.GetOutputFilename('output')
176         self.run_test(['struct'], dtb_file, output)
177         with open(output) as infile:
178             data = infile.read()
179         self._CheckStrings(HEADER + '''
180 struct dtd_sandbox_i2c_test {
181 };
182 struct dtd_sandbox_pmic_test {
183 \tbool\t\tlow_power;
184 \tfdt64_t\t\treg[2];
185 };
186 struct dtd_sandbox_spl_test {
187 \tbool\t\tboolval;
188 \tunsigned char\tbytearray[3];
189 \tunsigned char\tbyteval;
190 \tfdt32_t\t\tintarray[4];
191 \tfdt32_t\t\tintval;
192 \tunsigned char\tlongbytearray[9];
193 \tunsigned char\tnotstring[5];
194 \tconst char *\tstringarray[3];
195 \tconst char *\tstringval;
196 };
197 struct dtd_sandbox_spl_test_2 {
198 };
199 ''', data)
200
201         self.run_test(['platdata'], dtb_file, output)
202         with open(output) as infile:
203             data = infile.read()
204         self._CheckStrings(C_HEADER + '''
205 static const struct dtd_sandbox_spl_test dtv_spl_test = {
206 \t.boolval\t\t= true,
207 \t.bytearray\t\t= {0x6, 0x0, 0x0},
208 \t.byteval\t\t= 0x5,
209 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
210 \t.intval\t\t\t= 0x1,
211 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
212 \t\t0x11},
213 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
214 \t.stringarray\t\t= {"multi-word", "message", ""},
215 \t.stringval\t\t= "message",
216 };
217 U_BOOT_DEVICE(spl_test) = {
218 \t.name\t\t= "sandbox_spl_test",
219 \t.platdata\t= &dtv_spl_test,
220 \t.platdata_size\t= sizeof(dtv_spl_test),
221 };
222
223 static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
224 \t.bytearray\t\t= {0x1, 0x23, 0x34},
225 \t.byteval\t\t= 0x8,
226 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
227 \t.intval\t\t\t= 0x3,
228 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
229 \t\t0x0},
230 \t.stringarray\t\t= {"another", "multi-word", "message"},
231 \t.stringval\t\t= "message2",
232 };
233 U_BOOT_DEVICE(spl_test2) = {
234 \t.name\t\t= "sandbox_spl_test",
235 \t.platdata\t= &dtv_spl_test2,
236 \t.platdata_size\t= sizeof(dtv_spl_test2),
237 };
238
239 static const struct dtd_sandbox_spl_test dtv_spl_test3 = {
240 \t.stringarray\t\t= {"one", "", ""},
241 };
242 U_BOOT_DEVICE(spl_test3) = {
243 \t.name\t\t= "sandbox_spl_test",
244 \t.platdata\t= &dtv_spl_test3,
245 \t.platdata_size\t= sizeof(dtv_spl_test3),
246 };
247
248 static const struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
249 };
250 U_BOOT_DEVICE(spl_test4) = {
251 \t.name\t\t= "sandbox_spl_test_2",
252 \t.platdata\t= &dtv_spl_test4,
253 \t.platdata_size\t= sizeof(dtv_spl_test4),
254 };
255
256 static const struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
257 };
258 U_BOOT_DEVICE(i2c_at_0) = {
259 \t.name\t\t= "sandbox_i2c_test",
260 \t.platdata\t= &dtv_i2c_at_0,
261 \t.platdata_size\t= sizeof(dtv_i2c_at_0),
262 };
263
264 static const struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
265 \t.low_power\t\t= true,
266 \t.reg\t\t\t= {0x9, 0x0},
267 };
268 U_BOOT_DEVICE(pmic_at_9) = {
269 \t.name\t\t= "sandbox_pmic_test",
270 \t.platdata\t= &dtv_pmic_at_9,
271 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
272 };
273
274 ''', data)
275
276     def test_driver_alias(self):
277         """Test output from a device tree file with a driver alias"""
278         dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
279         output = tools.GetOutputFilename('output')
280         self.run_test(['struct'], dtb_file, output)
281         with open(output) as infile:
282             data = infile.read()
283         self._CheckStrings(HEADER + '''
284 struct dtd_sandbox_gpio {
285 \tconst char *\tgpio_bank_name;
286 \tbool\t\tgpio_controller;
287 \tfdt32_t\t\tsandbox_gpio_count;
288 };
289 #define dtd_sandbox_gpio_alias dtd_sandbox_gpio
290 ''', data)
291
292         self.run_test(['platdata'], dtb_file, output)
293         with open(output) as infile:
294             data = infile.read()
295         self._CheckStrings(C_HEADER + '''
296 static const struct dtd_sandbox_gpio dtv_gpios_at_0 = {
297 \t.gpio_bank_name\t\t= "a",
298 \t.gpio_controller\t= true,
299 \t.sandbox_gpio_count\t= 0x14,
300 };
301 U_BOOT_DEVICE(gpios_at_0) = {
302 \t.name\t\t= "sandbox_gpio",
303 \t.platdata\t= &dtv_gpios_at_0,
304 \t.platdata_size\t= sizeof(dtv_gpios_at_0),
305 };
306
307 ''', data)
308
309     def test_invalid_driver(self):
310         """Test output from a device tree file with an invalid driver"""
311         dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
312         output = tools.GetOutputFilename('output')
313         with test_util.capture_sys_output() as (stdout, stderr):
314             dtb_platdata.run_steps(['struct'], dtb_file, False, output)
315         with open(output) as infile:
316             data = infile.read()
317         self._CheckStrings(HEADER + '''
318 struct dtd_invalid {
319 };
320 ''', data)
321
322         with test_util.capture_sys_output() as (stdout, stderr):
323             dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
324         with open(output) as infile:
325             data = infile.read()
326         self._CheckStrings(C_HEADER + '''
327 static const struct dtd_invalid dtv_spl_test = {
328 };
329 U_BOOT_DEVICE(spl_test) = {
330 \t.name\t\t= "invalid",
331 \t.platdata\t= &dtv_spl_test,
332 \t.platdata_size\t= sizeof(dtv_spl_test),
333 };
334
335 ''', data)
336
337     def test_phandle(self):
338         """Test output from a node containing a phandle reference"""
339         dtb_file = get_dtb_file('dtoc_test_phandle.dts')
340         output = tools.GetOutputFilename('output')
341         self.run_test(['struct'], dtb_file, output)
342         with open(output) as infile:
343             data = infile.read()
344         self._CheckStrings(HEADER + '''
345 struct dtd_source {
346 \tstruct phandle_2_arg clocks[4];
347 };
348 struct dtd_target {
349 \tfdt32_t\t\tintval;
350 };
351 ''', data)
352
353         self.run_test(['platdata'], dtb_file, output)
354         with open(output) as infile:
355             data = infile.read()
356         self._CheckStrings(C_HEADER + '''
357 static const struct dtd_target dtv_phandle_target = {
358 \t.intval\t\t\t= 0x0,
359 };
360 U_BOOT_DEVICE(phandle_target) = {
361 \t.name\t\t= "target",
362 \t.platdata\t= &dtv_phandle_target,
363 \t.platdata_size\t= sizeof(dtv_phandle_target),
364 };
365
366 static const struct dtd_target dtv_phandle2_target = {
367 \t.intval\t\t\t= 0x1,
368 };
369 U_BOOT_DEVICE(phandle2_target) = {
370 \t.name\t\t= "target",
371 \t.platdata\t= &dtv_phandle2_target,
372 \t.platdata_size\t= sizeof(dtv_phandle2_target),
373 };
374
375 static const struct dtd_target dtv_phandle3_target = {
376 \t.intval\t\t\t= 0x2,
377 };
378 U_BOOT_DEVICE(phandle3_target) = {
379 \t.name\t\t= "target",
380 \t.platdata\t= &dtv_phandle3_target,
381 \t.platdata_size\t= sizeof(dtv_phandle3_target),
382 };
383
384 static const struct dtd_source dtv_phandle_source = {
385 \t.clocks\t\t\t= {
386 \t\t\t{&dtv_phandle_target, {}},
387 \t\t\t{&dtv_phandle2_target, {11}},
388 \t\t\t{&dtv_phandle3_target, {12, 13}},
389 \t\t\t{&dtv_phandle_target, {}},},
390 };
391 U_BOOT_DEVICE(phandle_source) = {
392 \t.name\t\t= "source",
393 \t.platdata\t= &dtv_phandle_source,
394 \t.platdata_size\t= sizeof(dtv_phandle_source),
395 };
396
397 static const struct dtd_source dtv_phandle_source2 = {
398 \t.clocks\t\t\t= {
399 \t\t\t{&dtv_phandle_target, {}},},
400 };
401 U_BOOT_DEVICE(phandle_source2) = {
402 \t.name\t\t= "source",
403 \t.platdata\t= &dtv_phandle_source2,
404 \t.platdata_size\t= sizeof(dtv_phandle_source2),
405 };
406
407 ''', data)
408
409     def test_phandle_single(self):
410         """Test output from a node containing a phandle reference"""
411         dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
412         output = tools.GetOutputFilename('output')
413         self.run_test(['struct'], dtb_file, output)
414         with open(output) as infile:
415             data = infile.read()
416         self._CheckStrings(HEADER + '''
417 struct dtd_source {
418 \tstruct phandle_0_arg clocks[1];
419 };
420 struct dtd_target {
421 \tfdt32_t\t\tintval;
422 };
423 ''', data)
424
425     def test_phandle_reorder(self):
426         """Test that phandle targets are generated before their references"""
427         dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
428         output = tools.GetOutputFilename('output')
429         self.run_test(['platdata'], dtb_file, output)
430         with open(output) as infile:
431             data = infile.read()
432         self._CheckStrings(C_HEADER + '''
433 static const struct dtd_target dtv_phandle_target = {
434 };
435 U_BOOT_DEVICE(phandle_target) = {
436 \t.name\t\t= "target",
437 \t.platdata\t= &dtv_phandle_target,
438 \t.platdata_size\t= sizeof(dtv_phandle_target),
439 };
440
441 static const struct dtd_source dtv_phandle_source2 = {
442 \t.clocks\t\t\t= {
443 \t\t\t{&dtv_phandle_target, {}},},
444 };
445 U_BOOT_DEVICE(phandle_source2) = {
446 \t.name\t\t= "source",
447 \t.platdata\t= &dtv_phandle_source2,
448 \t.platdata_size\t= sizeof(dtv_phandle_source2),
449 };
450
451 ''', data)
452
453     def test_phandle_bad(self):
454         """Test a node containing an invalid phandle fails"""
455         dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
456                                 capture_stderr=True)
457         output = tools.GetOutputFilename('output')
458         with self.assertRaises(ValueError) as e:
459             self.run_test(['struct'], dtb_file, output)
460         self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
461                       str(e.exception))
462
463     def test_phandle_bad2(self):
464         """Test a phandle target missing its #*-cells property"""
465         dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
466                                 capture_stderr=True)
467         output = tools.GetOutputFilename('output')
468         with self.assertRaises(ValueError) as e:
469             self.run_test(['struct'], dtb_file, output)
470         self.assertIn("Node 'phandle-target' has no '#clock-cells' property",
471                       str(e.exception))
472
473     def test_aliases(self):
474         """Test output from a node with multiple compatible strings"""
475         dtb_file = get_dtb_file('dtoc_test_aliases.dts')
476         output = tools.GetOutputFilename('output')
477         self.run_test(['struct'], dtb_file, output)
478         with open(output) as infile:
479             data = infile.read()
480         self._CheckStrings(HEADER + '''
481 struct dtd_compat1 {
482 \tfdt32_t\t\tintval;
483 };
484 #define dtd_compat2_1_fred dtd_compat1
485 #define dtd_compat3 dtd_compat1
486 ''', data)
487
488         self.run_test(['platdata'], dtb_file, output)
489         with open(output) as infile:
490             data = infile.read()
491         self._CheckStrings(C_HEADER + '''
492 static const struct dtd_compat1 dtv_spl_test = {
493 \t.intval\t\t\t= 0x1,
494 };
495 U_BOOT_DEVICE(spl_test) = {
496 \t.name\t\t= "compat1",
497 \t.platdata\t= &dtv_spl_test,
498 \t.platdata_size\t= sizeof(dtv_spl_test),
499 };
500
501 ''', data)
502
503     def test_addresses64(self):
504         """Test output from a node with a 'reg' property with na=2, ns=2"""
505         dtb_file = get_dtb_file('dtoc_test_addr64.dts')
506         output = tools.GetOutputFilename('output')
507         self.run_test(['struct'], dtb_file, output)
508         with open(output) as infile:
509             data = infile.read()
510         self._CheckStrings(HEADER + '''
511 struct dtd_test1 {
512 \tfdt64_t\t\treg[2];
513 };
514 struct dtd_test2 {
515 \tfdt64_t\t\treg[2];
516 };
517 struct dtd_test3 {
518 \tfdt64_t\t\treg[4];
519 };
520 ''', data)
521
522         self.run_test(['platdata'], dtb_file, output)
523         with open(output) as infile:
524             data = infile.read()
525         self._CheckStrings(C_HEADER + '''
526 static const struct dtd_test1 dtv_test1 = {
527 \t.reg\t\t\t= {0x1234, 0x5678},
528 };
529 U_BOOT_DEVICE(test1) = {
530 \t.name\t\t= "test1",
531 \t.platdata\t= &dtv_test1,
532 \t.platdata_size\t= sizeof(dtv_test1),
533 };
534
535 static const struct dtd_test2 dtv_test2 = {
536 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
537 };
538 U_BOOT_DEVICE(test2) = {
539 \t.name\t\t= "test2",
540 \t.platdata\t= &dtv_test2,
541 \t.platdata_size\t= sizeof(dtv_test2),
542 };
543
544 static const struct dtd_test3 dtv_test3 = {
545 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
546 };
547 U_BOOT_DEVICE(test3) = {
548 \t.name\t\t= "test3",
549 \t.platdata\t= &dtv_test3,
550 \t.platdata_size\t= sizeof(dtv_test3),
551 };
552
553 ''', data)
554
555     def test_addresses32(self):
556         """Test output from a node with a 'reg' property with na=1, ns=1"""
557         dtb_file = get_dtb_file('dtoc_test_addr32.dts')
558         output = tools.GetOutputFilename('output')
559         self.run_test(['struct'], dtb_file, output)
560         with open(output) as infile:
561             data = infile.read()
562         self._CheckStrings(HEADER + '''
563 struct dtd_test1 {
564 \tfdt32_t\t\treg[2];
565 };
566 struct dtd_test2 {
567 \tfdt32_t\t\treg[4];
568 };
569 ''', data)
570
571         self.run_test(['platdata'], dtb_file, output)
572         with open(output) as infile:
573             data = infile.read()
574         self._CheckStrings(C_HEADER + '''
575 static const struct dtd_test1 dtv_test1 = {
576 \t.reg\t\t\t= {0x1234, 0x5678},
577 };
578 U_BOOT_DEVICE(test1) = {
579 \t.name\t\t= "test1",
580 \t.platdata\t= &dtv_test1,
581 \t.platdata_size\t= sizeof(dtv_test1),
582 };
583
584 static const struct dtd_test2 dtv_test2 = {
585 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
586 };
587 U_BOOT_DEVICE(test2) = {
588 \t.name\t\t= "test2",
589 \t.platdata\t= &dtv_test2,
590 \t.platdata_size\t= sizeof(dtv_test2),
591 };
592
593 ''', data)
594
595     def test_addresses64_32(self):
596         """Test output from a node with a 'reg' property with na=2, ns=1"""
597         dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
598         output = tools.GetOutputFilename('output')
599         self.run_test(['struct'], dtb_file, output)
600         with open(output) as infile:
601             data = infile.read()
602         self._CheckStrings(HEADER + '''
603 struct dtd_test1 {
604 \tfdt64_t\t\treg[2];
605 };
606 struct dtd_test2 {
607 \tfdt64_t\t\treg[2];
608 };
609 struct dtd_test3 {
610 \tfdt64_t\t\treg[4];
611 };
612 ''', data)
613
614         self.run_test(['platdata'], dtb_file, output)
615         with open(output) as infile:
616             data = infile.read()
617         self._CheckStrings(C_HEADER + '''
618 static const struct dtd_test1 dtv_test1 = {
619 \t.reg\t\t\t= {0x123400000000, 0x5678},
620 };
621 U_BOOT_DEVICE(test1) = {
622 \t.name\t\t= "test1",
623 \t.platdata\t= &dtv_test1,
624 \t.platdata_size\t= sizeof(dtv_test1),
625 };
626
627 static const struct dtd_test2 dtv_test2 = {
628 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
629 };
630 U_BOOT_DEVICE(test2) = {
631 \t.name\t\t= "test2",
632 \t.platdata\t= &dtv_test2,
633 \t.platdata_size\t= sizeof(dtv_test2),
634 };
635
636 static const struct dtd_test3 dtv_test3 = {
637 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
638 };
639 U_BOOT_DEVICE(test3) = {
640 \t.name\t\t= "test3",
641 \t.platdata\t= &dtv_test3,
642 \t.platdata_size\t= sizeof(dtv_test3),
643 };
644
645 ''', data)
646
647     def test_addresses32_64(self):
648         """Test output from a node with a 'reg' property with na=1, ns=2"""
649         dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
650         output = tools.GetOutputFilename('output')
651         self.run_test(['struct'], dtb_file, output)
652         with open(output) as infile:
653             data = infile.read()
654         self._CheckStrings(HEADER + '''
655 struct dtd_test1 {
656 \tfdt64_t\t\treg[2];
657 };
658 struct dtd_test2 {
659 \tfdt64_t\t\treg[2];
660 };
661 struct dtd_test3 {
662 \tfdt64_t\t\treg[4];
663 };
664 ''', data)
665
666         self.run_test(['platdata'], dtb_file, output)
667         with open(output) as infile:
668             data = infile.read()
669         self._CheckStrings(C_HEADER + '''
670 static const struct dtd_test1 dtv_test1 = {
671 \t.reg\t\t\t= {0x1234, 0x567800000000},
672 };
673 U_BOOT_DEVICE(test1) = {
674 \t.name\t\t= "test1",
675 \t.platdata\t= &dtv_test1,
676 \t.platdata_size\t= sizeof(dtv_test1),
677 };
678
679 static const struct dtd_test2 dtv_test2 = {
680 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
681 };
682 U_BOOT_DEVICE(test2) = {
683 \t.name\t\t= "test2",
684 \t.platdata\t= &dtv_test2,
685 \t.platdata_size\t= sizeof(dtv_test2),
686 };
687
688 static const struct dtd_test3 dtv_test3 = {
689 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
690 };
691 U_BOOT_DEVICE(test3) = {
692 \t.name\t\t= "test3",
693 \t.platdata\t= &dtv_test3,
694 \t.platdata_size\t= sizeof(dtv_test3),
695 };
696
697 ''', data)
698
699     def test_bad_reg(self):
700         """Test that a reg property with an invalid type generates an error"""
701         # Capture stderr since dtc will emit warnings for this file
702         dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
703         output = tools.GetOutputFilename('output')
704         with self.assertRaises(ValueError) as e:
705             self.run_test(['struct'], dtb_file, output)
706         self.assertIn("Node 'spl-test' reg property is not an int",
707                       str(e.exception))
708
709     def test_bad_reg2(self):
710         """Test that a reg property with an invalid cell count is detected"""
711         # Capture stderr since dtc will emit warnings for this file
712         dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
713         output = tools.GetOutputFilename('output')
714         with self.assertRaises(ValueError) as e:
715             self.run_test(['struct'], dtb_file, output)
716         self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
717                       str(e.exception))
718
719     def test_add_prop(self):
720         """Test that a subequent node can add a new property to a struct"""
721         dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
722         output = tools.GetOutputFilename('output')
723         self.run_test(['struct'], dtb_file, output)
724         with open(output) as infile:
725             data = infile.read()
726         self._CheckStrings(HEADER + '''
727 struct dtd_sandbox_spl_test {
728 \tfdt32_t\t\tintarray;
729 \tfdt32_t\t\tintval;
730 };
731 ''', data)
732
733         self.run_test(['platdata'], dtb_file, output)
734         with open(output) as infile:
735             data = infile.read()
736         self._CheckStrings(C_HEADER + '''
737 static const struct dtd_sandbox_spl_test dtv_spl_test = {
738 \t.intval\t\t\t= 0x1,
739 };
740 U_BOOT_DEVICE(spl_test) = {
741 \t.name\t\t= "sandbox_spl_test",
742 \t.platdata\t= &dtv_spl_test,
743 \t.platdata_size\t= sizeof(dtv_spl_test),
744 };
745
746 static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
747 \t.intarray\t\t= 0x5,
748 };
749 U_BOOT_DEVICE(spl_test2) = {
750 \t.name\t\t= "sandbox_spl_test",
751 \t.platdata\t= &dtv_spl_test2,
752 \t.platdata_size\t= sizeof(dtv_spl_test2),
753 };
754
755 ''', data)
756
757     def testStdout(self):
758         """Test output to stdout"""
759         dtb_file = get_dtb_file('dtoc_test_simple.dts')
760         with test_util.capture_sys_output() as (stdout, stderr):
761             self.run_test(['struct'], dtb_file, '-')
762
763     def testNoCommand(self):
764         """Test running dtoc without a command"""
765         with self.assertRaises(ValueError) as e:
766             self.run_test([], '', '')
767         self.assertIn("Please specify a command: struct, platdata",
768                       str(e.exception))
769
770     def testBadCommand(self):
771         """Test running dtoc with an invalid command"""
772         dtb_file = get_dtb_file('dtoc_test_simple.dts')
773         output = tools.GetOutputFilename('output')
774         with self.assertRaises(ValueError) as e:
775             self.run_test(['invalid-cmd'], dtb_file, output)
776         self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
777                       str(e.exception))