Merge tag 'dm-pull-10jul20' of https://gitlab.denx.de/u-boot/custodians/u-boot-dm
[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 C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
52 }
53 '''
54
55
56 def get_dtb_file(dts_fname, capture_stderr=False):
57     """Compile a .dts file to a .dtb
58
59     Args:
60         dts_fname: Filename of .dts file in the current directory
61         capture_stderr: True to capture and discard stderr output
62
63     Returns:
64         Filename of compiled file in output directory
65     """
66     return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
67                                    capture_stderr=capture_stderr)
68
69
70 class TestDtoc(unittest.TestCase):
71     """Tests for dtoc"""
72     @classmethod
73     def setUpClass(cls):
74         tools.PrepareOutputDir(None)
75
76     @classmethod
77     def tearDownClass(cls):
78         tools._RemoveOutputDir()
79
80     def _WritePythonString(self, fname, data):
81         """Write a string with tabs expanded as done in this Python file
82
83         Args:
84             fname: Filename to write to
85             data: Raw string to convert
86         """
87         data = data.replace('\t', '\\t')
88         with open(fname, 'w') as fd:
89             fd.write(data)
90
91     def _CheckStrings(self, expected, actual):
92         """Check that a string matches its expected value
93
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.
97
98         Args:
99             expected: Expected string
100             actual: Actual string
101         """
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)
107
108
109     def run_test(self, args, dtb_file, output):
110         dtb_platdata.run_steps(args, dtb_file, False, output, True)
111
112     def test_name(self):
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'))
119
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'))
128
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))
139
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'])
144
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))
149
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))
154
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))
160
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)
169
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)
175
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:
182             data = infile.read()
183         self._CheckStrings(HEADER + '''
184 struct dtd_sandbox_i2c_test {
185 };
186 struct dtd_sandbox_pmic_test {
187 \tbool\t\tlow_power;
188 \tfdt64_t\t\treg[2];
189 };
190 struct dtd_sandbox_spl_test {
191 \tbool\t\tboolval;
192 \tunsigned char\tbytearray[3];
193 \tunsigned char\tbyteval;
194 \tfdt32_t\t\tintarray[4];
195 \tfdt32_t\t\tintval;
196 \tunsigned char\tlongbytearray[9];
197 \tunsigned char\tnotstring[5];
198 \tconst char *\tstringarray[3];
199 \tconst char *\tstringval;
200 };
201 struct dtd_sandbox_spl_test_2 {
202 };
203 ''', data)
204
205         self.run_test(['platdata'], dtb_file, output)
206         with open(output) as infile:
207             data = infile.read()
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},
212 \t.byteval\t\t= 0x5,
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,
216 \t\t0x11},
217 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
218 \t.stringarray\t\t= {"multi-word", "message", ""},
219 \t.stringval\t\t= "message",
220 };
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),
225 };
226
227 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
228 \t.bytearray\t\t= {0x1, 0x23, 0x34},
229 \t.byteval\t\t= 0x8,
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,
233 \t\t0x0},
234 \t.stringarray\t\t= {"another", "multi-word", "message"},
235 \t.stringval\t\t= "message2",
236 };
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),
241 };
242
243 static struct dtd_sandbox_spl_test dtv_spl_test3 = {
244 \t.stringarray\t\t= {"one", "", ""},
245 };
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),
250 };
251
252 static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
253 };
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),
258 };
259
260 static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
261 };
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),
266 };
267
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},
271 };
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),
276 };
277
278 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
279
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:
286             data = infile.read()
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;
292 };
293 #define dtd_sandbox_gpio_alias dtd_sandbox_gpio
294 ''', data)
295
296         self.run_test(['platdata'], dtb_file, output)
297         with open(output) as infile:
298             data = infile.read()
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,
304 };
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),
309 };
310
311 void dm_populate_phandle_data(void) {
312 }
313 ''', data)
314
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:
322             data = infile.read()
323         self._CheckStrings(HEADER + '''
324 struct dtd_invalid {
325 };
326 ''', data)
327
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:
331             data = infile.read()
332         self._CheckStrings(C_HEADER + '''
333 static struct dtd_invalid dtv_spl_test = {
334 };
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),
339 };
340
341 void dm_populate_phandle_data(void) {
342 }
343 ''', data)
344
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:
351             data = infile.read()
352         self._CheckStrings(HEADER + '''
353 struct dtd_source {
354 \tstruct phandle_2_arg clocks[4];
355 };
356 struct dtd_target {
357 \tfdt32_t\t\tintval;
358 };
359 ''', data)
360
361         self.run_test(['platdata'], dtb_file, output)
362         with open(output) as infile:
363             data = infile.read()
364         self._CheckStrings(C_HEADER + '''
365 static struct dtd_target dtv_phandle_target = {
366 \t.intval\t\t\t= 0x0,
367 };
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),
372 };
373
374 static struct dtd_target dtv_phandle2_target = {
375 \t.intval\t\t\t= 0x1,
376 };
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),
381 };
382
383 static struct dtd_target dtv_phandle3_target = {
384 \t.intval\t\t\t= 0x2,
385 };
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),
390 };
391
392 static struct dtd_source dtv_phandle_source = {
393 \t.clocks\t\t\t= {
394 \t\t\t{NULL, {}},
395 \t\t\t{NULL, {11}},
396 \t\t\t{NULL, {12, 13}},
397 \t\t\t{NULL, {}},},
398 };
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),
403 };
404
405 static struct dtd_source dtv_phandle_source2 = {
406 \t.clocks\t\t\t= {
407 \t\t\t{NULL, {}},},
408 };
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),
413 };
414
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);
421 }
422 ''', data)
423
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:
430             data = infile.read()
431         self._CheckStrings(HEADER + '''
432 struct dtd_source {
433 \tstruct phandle_0_arg clocks[1];
434 };
435 struct dtd_target {
436 \tfdt32_t\t\tintval;
437 };
438 ''', data)
439
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:
446             data = infile.read()
447         self._CheckStrings(C_HEADER + '''
448 static struct dtd_target dtv_phandle_target = {
449 };
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),
454 };
455
456 static struct dtd_source dtv_phandle_source2 = {
457 \t.clocks\t\t\t= {
458 \t\t\t{NULL, {}},},
459 };
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),
464 };
465
466 void dm_populate_phandle_data(void) {
467 \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
468 }
469 ''', data)
470
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:
477             data = infile.read()
478         self._CheckStrings(C_HEADER + '''
479 static struct dtd_target dtv_phandle_target = {
480 \t.intval\t\t\t= 0x0,
481 };
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),
486 };
487
488 static struct dtd_target dtv_phandle2_target = {
489 \t.intval\t\t\t= 0x1,
490 };
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),
495 };
496
497 static struct dtd_target dtv_phandle3_target = {
498 \t.intval\t\t\t= 0x2,
499 };
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),
504 };
505
506 static struct dtd_source dtv_phandle_source = {
507 \t.cd_gpios\t\t= {
508 \t\t\t{NULL, {}},
509 \t\t\t{NULL, {11}},
510 \t\t\t{NULL, {12, 13}},
511 \t\t\t{NULL, {}},},
512 };
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),
517 };
518
519 static struct dtd_source dtv_phandle_source2 = {
520 \t.cd_gpios\t\t= {
521 \t\t\t{NULL, {}},},
522 };
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),
527 };
528
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);
535 }
536 ''', data)
537
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',
541                                 capture_stderr=True)
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'",
546                       str(e.exception))
547
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',
551                                 capture_stderr=True)
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",
556                       str(e.exception))
557
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:
564             data = infile.read()
565         self._CheckStrings(HEADER + '''
566 struct dtd_compat1 {
567 \tfdt32_t\t\tintval;
568 };
569 #define dtd_compat2_1_fred dtd_compat1
570 #define dtd_compat3 dtd_compat1
571 ''', data)
572
573         self.run_test(['platdata'], dtb_file, output)
574         with open(output) as infile:
575             data = infile.read()
576         self._CheckStrings(C_HEADER + '''
577 static struct dtd_compat1 dtv_spl_test = {
578 \t.intval\t\t\t= 0x1,
579 };
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),
584 };
585
586 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
587
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:
594             data = infile.read()
595         self._CheckStrings(HEADER + '''
596 struct dtd_test1 {
597 \tfdt64_t\t\treg[2];
598 };
599 struct dtd_test2 {
600 \tfdt64_t\t\treg[2];
601 };
602 struct dtd_test3 {
603 \tfdt64_t\t\treg[4];
604 };
605 ''', data)
606
607         self.run_test(['platdata'], dtb_file, output)
608         with open(output) as infile:
609             data = infile.read()
610         self._CheckStrings(C_HEADER + '''
611 static struct dtd_test1 dtv_test1 = {
612 \t.reg\t\t\t= {0x1234, 0x5678},
613 };
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),
618 };
619
620 static struct dtd_test2 dtv_test2 = {
621 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
622 };
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),
627 };
628
629 static struct dtd_test3 dtv_test3 = {
630 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
631 };
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),
636 };
637
638 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
639
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:
646             data = infile.read()
647         self._CheckStrings(HEADER + '''
648 struct dtd_test1 {
649 \tfdt32_t\t\treg[2];
650 };
651 struct dtd_test2 {
652 \tfdt32_t\t\treg[4];
653 };
654 ''', data)
655
656         self.run_test(['platdata'], dtb_file, output)
657         with open(output) as infile:
658             data = infile.read()
659         self._CheckStrings(C_HEADER + '''
660 static struct dtd_test1 dtv_test1 = {
661 \t.reg\t\t\t= {0x1234, 0x5678},
662 };
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),
667 };
668
669 static struct dtd_test2 dtv_test2 = {
670 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
671 };
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),
676 };
677
678 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
679
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:
686             data = infile.read()
687         self._CheckStrings(HEADER + '''
688 struct dtd_test1 {
689 \tfdt64_t\t\treg[2];
690 };
691 struct dtd_test2 {
692 \tfdt64_t\t\treg[2];
693 };
694 struct dtd_test3 {
695 \tfdt64_t\t\treg[4];
696 };
697 ''', data)
698
699         self.run_test(['platdata'], dtb_file, output)
700         with open(output) as infile:
701             data = infile.read()
702         self._CheckStrings(C_HEADER + '''
703 static struct dtd_test1 dtv_test1 = {
704 \t.reg\t\t\t= {0x123400000000, 0x5678},
705 };
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),
710 };
711
712 static struct dtd_test2 dtv_test2 = {
713 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
714 };
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),
719 };
720
721 static struct dtd_test3 dtv_test3 = {
722 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
723 };
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),
728 };
729
730 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
731
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:
738             data = infile.read()
739         self._CheckStrings(HEADER + '''
740 struct dtd_test1 {
741 \tfdt64_t\t\treg[2];
742 };
743 struct dtd_test2 {
744 \tfdt64_t\t\treg[2];
745 };
746 struct dtd_test3 {
747 \tfdt64_t\t\treg[4];
748 };
749 ''', data)
750
751         self.run_test(['platdata'], dtb_file, output)
752         with open(output) as infile:
753             data = infile.read()
754         self._CheckStrings(C_HEADER + '''
755 static struct dtd_test1 dtv_test1 = {
756 \t.reg\t\t\t= {0x1234, 0x567800000000},
757 };
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),
762 };
763
764 static struct dtd_test2 dtv_test2 = {
765 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
766 };
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),
771 };
772
773 static struct dtd_test3 dtv_test3 = {
774 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
775 };
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),
780 };
781
782 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
783
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",
792                       str(e.exception))
793
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)",
802                       str(e.exception))
803
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:
810             data = infile.read()
811         self._CheckStrings(HEADER + '''
812 struct dtd_sandbox_spl_test {
813 \tfdt32_t\t\tintarray;
814 \tfdt32_t\t\tintval;
815 };
816 ''', data)
817
818         self.run_test(['platdata'], dtb_file, output)
819         with open(output) as infile:
820             data = infile.read()
821         self._CheckStrings(C_HEADER + '''
822 static struct dtd_sandbox_spl_test dtv_spl_test = {
823 \t.intval\t\t\t= 0x1,
824 };
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),
829 };
830
831 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
832 \t.intarray\t\t= 0x5,
833 };
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),
838 };
839
840 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
841
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, '-')
847
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",
853                       str(e.exception))
854
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)",
862                       str(e.exception))