dm: Support parent devices with of-platdata
[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 tempfile
17 import unittest
18
19 from dtoc import dtb_platdata
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
24 from dtoc import fdt
25 from dtoc import fdt_util
26 from patman import test_util
27 from patman import tools
28
29 our_path = os.path.dirname(os.path.realpath(__file__))
30
31
32 HEADER = '''/*
33  * DO NOT MODIFY
34  *
35  * This file was generated by dtoc from a .dtb (device tree binary) file.
36  */
37
38 #include <stdbool.h>
39 #include <linux/libfdt.h>'''
40
41 C_HEADER = '''/*
42  * DO NOT MODIFY
43  *
44  * This file was generated by dtoc from a .dtb (device tree binary) file.
45  */
46
47 #include <common.h>
48 #include <dm.h>
49 #include <dt-structs.h>
50 '''
51
52 C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
53 }
54 '''
55
56
57 def get_dtb_file(dts_fname, capture_stderr=False):
58     """Compile a .dts file to a .dtb
59
60     Args:
61         dts_fname: Filename of .dts file in the current directory
62         capture_stderr: True to capture and discard stderr output
63
64     Returns:
65         Filename of compiled file in output directory
66     """
67     return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
68                                    capture_stderr=capture_stderr)
69
70
71 class TestDtoc(unittest.TestCase):
72     """Tests for dtoc"""
73     @classmethod
74     def setUpClass(cls):
75         tools.PrepareOutputDir(None)
76         cls.maxDiff = None
77
78     @classmethod
79     def tearDownClass(cls):
80         tools._RemoveOutputDir()
81
82     def _WritePythonString(self, fname, data):
83         """Write a string with tabs expanded as done in this Python file
84
85         Args:
86             fname: Filename to write to
87             data: Raw string to convert
88         """
89         data = data.replace('\t', '\\t')
90         with open(fname, 'w') as fd:
91             fd.write(data)
92
93     def _CheckStrings(self, expected, actual):
94         """Check that a string matches its expected value
95
96         If the strings do not match, they are written to the /tmp directory in
97         the same Python format as is used here in the test. This allows for
98         easy comparison and update of the tests.
99
100         Args:
101             expected: Expected string
102             actual: Actual string
103         """
104         if expected != actual:
105             self._WritePythonString('/tmp/binman.expected', expected)
106             self._WritePythonString('/tmp/binman.actual', actual)
107             print('Failures written to /tmp/binman.{expected,actual}')
108         self.assertEquals(expected, actual)
109
110
111     def run_test(self, args, dtb_file, output):
112         dtb_platdata.run_steps(args, dtb_file, False, output, True)
113
114     def test_name(self):
115         """Test conversion of device tree names to C identifiers"""
116         self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
117         self.assertEqual('vendor_clock_frequency',
118                          conv_name_to_c('vendor,clock-frequency'))
119         self.assertEqual('rockchip_rk3399_sdhci_5_1',
120                          conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
121
122     def test_tab_to(self):
123         """Test operation of tab_to() function"""
124         self.assertEqual('fred ', tab_to(0, 'fred'))
125         self.assertEqual('fred\t', tab_to(1, 'fred'))
126         self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
127         self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
128         self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
129         self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
130
131     def test_get_value(self):
132         """Test operation of get_value() function"""
133         self.assertEqual('0x45',
134                          get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
135         self.assertEqual('0x45',
136                          get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
137         self.assertEqual('0x0',
138                          get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
139         self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
140         self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
141
142     def test_get_compat_name(self):
143         """Test operation of get_compat_name() function"""
144         Prop = collections.namedtuple('Prop', ['value'])
145         Node = collections.namedtuple('Node', ['props'])
146
147         prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
148         node = Node({'compatible': prop})
149         self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
150                          get_compat_name(node))
151
152         prop = Prop(['rockchip,rk3399-sdhci-5.1'])
153         node = Node({'compatible': prop})
154         self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
155                          get_compat_name(node))
156
157         prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
158         node = Node({'compatible': prop})
159         self.assertEqual((['rockchip_rk3399_sdhci_5_1',
160                           'arasan_sdhci_5_1', 'third']),
161                          get_compat_name(node))
162
163     def test_empty_file(self):
164         """Test output from a device tree file with no nodes"""
165         dtb_file = get_dtb_file('dtoc_test_empty.dts')
166         output = tools.GetOutputFilename('output')
167         self.run_test(['struct'], dtb_file, output)
168         with open(output) as infile:
169             lines = infile.read().splitlines()
170         self.assertEqual(HEADER.splitlines(), lines)
171
172         self.run_test(['platdata'], dtb_file, output)
173         with open(output) as infile:
174             lines = infile.read().splitlines()
175         self.assertEqual(C_HEADER.splitlines() + [''] +
176                          C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
177
178     def test_simple(self):
179         """Test output from some simple nodes with various types of data"""
180         dtb_file = get_dtb_file('dtoc_test_simple.dts')
181         output = tools.GetOutputFilename('output')
182         self.run_test(['struct'], dtb_file, output)
183         with open(output) as infile:
184             data = infile.read()
185         self._CheckStrings(HEADER + '''
186 struct dtd_sandbox_i2c_test {
187 };
188 struct dtd_sandbox_pmic_test {
189 \tbool\t\tlow_power;
190 \tfdt64_t\t\treg[2];
191 };
192 struct dtd_sandbox_spl_test {
193 \tconst char *  acpi_name;
194 \tbool\t\tboolval;
195 \tunsigned char\tbytearray[3];
196 \tunsigned char\tbyteval;
197 \tfdt32_t\t\tintarray[4];
198 \tfdt32_t\t\tintval;
199 \tunsigned char\tlongbytearray[9];
200 \tunsigned char\tnotstring[5];
201 \tconst char *\tstringarray[3];
202 \tconst char *\tstringval;
203 };
204 struct dtd_sandbox_spl_test_2 {
205 };
206 ''', data)
207
208         self.run_test(['platdata'], dtb_file, output)
209         with open(output) as infile:
210             data = infile.read()
211         self._CheckStrings(C_HEADER + '''
212 /* Node /i2c@0 index 0 */
213 static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
214 };
215 U_BOOT_DEVICE(i2c_at_0) = {
216 \t.name\t\t= "sandbox_i2c_test",
217 \t.platdata\t= &dtv_i2c_at_0,
218 \t.platdata_size\t= sizeof(dtv_i2c_at_0),
219 \t.parent_idx\t= -1,
220 };
221
222 /* Node /i2c@0/pmic@9 index 1 */
223 static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
224 \t.low_power\t\t= true,
225 \t.reg\t\t\t= {0x9, 0x0},
226 };
227 U_BOOT_DEVICE(pmic_at_9) = {
228 \t.name\t\t= "sandbox_pmic_test",
229 \t.platdata\t= &dtv_pmic_at_9,
230 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
231 \t.parent_idx\t= 0,
232 };
233
234 /* Node /spl-test index 2 */
235 static struct dtd_sandbox_spl_test dtv_spl_test = {
236 \t.boolval\t\t= true,
237 \t.bytearray\t\t= {0x6, 0x0, 0x0},
238 \t.byteval\t\t= 0x5,
239 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
240 \t.intval\t\t\t= 0x1,
241 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
242 \t\t0x11},
243 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
244 \t.stringarray\t\t= {"multi-word", "message", ""},
245 \t.stringval\t\t= "message",
246 };
247 U_BOOT_DEVICE(spl_test) = {
248 \t.name\t\t= "sandbox_spl_test",
249 \t.platdata\t= &dtv_spl_test,
250 \t.platdata_size\t= sizeof(dtv_spl_test),
251 \t.parent_idx\t= -1,
252 };
253
254 /* Node /spl-test2 index 3 */
255 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
256 \t.acpi_name\t\t= "\\\\_SB.GPO0",
257 \t.bytearray\t\t= {0x1, 0x23, 0x34},
258 \t.byteval\t\t= 0x8,
259 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
260 \t.intval\t\t\t= 0x3,
261 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
262 \t\t0x0},
263 \t.stringarray\t\t= {"another", "multi-word", "message"},
264 \t.stringval\t\t= "message2",
265 };
266 U_BOOT_DEVICE(spl_test2) = {
267 \t.name\t\t= "sandbox_spl_test",
268 \t.platdata\t= &dtv_spl_test2,
269 \t.platdata_size\t= sizeof(dtv_spl_test2),
270 \t.parent_idx\t= -1,
271 };
272
273 /* Node /spl-test3 index 4 */
274 static struct dtd_sandbox_spl_test dtv_spl_test3 = {
275 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
276 \t\t0x0},
277 \t.stringarray\t\t= {"one", "", ""},
278 };
279 U_BOOT_DEVICE(spl_test3) = {
280 \t.name\t\t= "sandbox_spl_test",
281 \t.platdata\t= &dtv_spl_test3,
282 \t.platdata_size\t= sizeof(dtv_spl_test3),
283 \t.parent_idx\t= -1,
284 };
285
286 /* Node /spl-test4 index 5 */
287 static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
288 };
289 U_BOOT_DEVICE(spl_test4) = {
290 \t.name\t\t= "sandbox_spl_test_2",
291 \t.platdata\t= &dtv_spl_test4,
292 \t.platdata_size\t= sizeof(dtv_spl_test4),
293 \t.parent_idx\t= -1,
294 };
295
296 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
297
298     def test_driver_alias(self):
299         """Test output from a device tree file with a driver alias"""
300         dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
301         output = tools.GetOutputFilename('output')
302         self.run_test(['struct'], dtb_file, output)
303         with open(output) as infile:
304             data = infile.read()
305         self._CheckStrings(HEADER + '''
306 struct dtd_sandbox_gpio {
307 \tconst char *\tgpio_bank_name;
308 \tbool\t\tgpio_controller;
309 \tfdt32_t\t\tsandbox_gpio_count;
310 };
311 ''', data)
312
313         self.run_test(['platdata'], dtb_file, output)
314         with open(output) as infile:
315             data = infile.read()
316         self._CheckStrings(C_HEADER + '''
317 /* Node /gpios@0 index 0 */
318 static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
319 \t.gpio_bank_name\t\t= "a",
320 \t.gpio_controller\t= true,
321 \t.sandbox_gpio_count\t= 0x14,
322 };
323 U_BOOT_DEVICE(gpios_at_0) = {
324 \t.name\t\t= "sandbox_gpio",
325 \t.platdata\t= &dtv_gpios_at_0,
326 \t.platdata_size\t= sizeof(dtv_gpios_at_0),
327 \t.parent_idx\t= -1,
328 };
329
330 void dm_populate_phandle_data(void) {
331 }
332 ''', data)
333
334     def test_invalid_driver(self):
335         """Test output from a device tree file with an invalid driver"""
336         dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
337         output = tools.GetOutputFilename('output')
338         with test_util.capture_sys_output() as (stdout, stderr):
339             dtb_platdata.run_steps(['struct'], dtb_file, False, output)
340         with open(output) as infile:
341             data = infile.read()
342         self._CheckStrings(HEADER + '''
343 struct dtd_invalid {
344 };
345 ''', data)
346
347         with test_util.capture_sys_output() as (stdout, stderr):
348             dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
349         with open(output) as infile:
350             data = infile.read()
351         self._CheckStrings(C_HEADER + '''
352 /* Node /spl-test index 0 */
353 static struct dtd_invalid dtv_spl_test = {
354 };
355 U_BOOT_DEVICE(spl_test) = {
356 \t.name\t\t= "invalid",
357 \t.platdata\t= &dtv_spl_test,
358 \t.platdata_size\t= sizeof(dtv_spl_test),
359 \t.parent_idx\t= -1,
360 };
361
362 void dm_populate_phandle_data(void) {
363 }
364 ''', data)
365
366     def test_phandle(self):
367         """Test output from a node containing a phandle reference"""
368         dtb_file = get_dtb_file('dtoc_test_phandle.dts')
369         output = tools.GetOutputFilename('output')
370         self.run_test(['struct'], dtb_file, output)
371         with open(output) as infile:
372             data = infile.read()
373         self._CheckStrings(HEADER + '''
374 struct dtd_source {
375 \tstruct phandle_2_arg clocks[4];
376 };
377 struct dtd_target {
378 \tfdt32_t\t\tintval;
379 };
380 ''', data)
381
382         self.run_test(['platdata'], dtb_file, output)
383         with open(output) as infile:
384             data = infile.read()
385         self._CheckStrings(C_HEADER + '''
386 /* Node /phandle2-target index 0 */
387 static struct dtd_target dtv_phandle2_target = {
388 \t.intval\t\t\t= 0x1,
389 };
390 U_BOOT_DEVICE(phandle2_target) = {
391 \t.name\t\t= "target",
392 \t.platdata\t= &dtv_phandle2_target,
393 \t.platdata_size\t= sizeof(dtv_phandle2_target),
394 \t.parent_idx\t= -1,
395 };
396
397 /* Node /phandle3-target index 1 */
398 static struct dtd_target dtv_phandle3_target = {
399 \t.intval\t\t\t= 0x2,
400 };
401 U_BOOT_DEVICE(phandle3_target) = {
402 \t.name\t\t= "target",
403 \t.platdata\t= &dtv_phandle3_target,
404 \t.platdata_size\t= sizeof(dtv_phandle3_target),
405 \t.parent_idx\t= -1,
406 };
407
408 /* Node /phandle-target index 4 */
409 static struct dtd_target dtv_phandle_target = {
410 \t.intval\t\t\t= 0x0,
411 };
412 U_BOOT_DEVICE(phandle_target) = {
413 \t.name\t\t= "target",
414 \t.platdata\t= &dtv_phandle_target,
415 \t.platdata_size\t= sizeof(dtv_phandle_target),
416 \t.parent_idx\t= -1,
417 };
418
419 /* Node /phandle-source index 2 */
420 static struct dtd_source dtv_phandle_source = {
421 \t.clocks\t\t\t= {
422 \t\t\t{NULL, {}},
423 \t\t\t{NULL, {11}},
424 \t\t\t{NULL, {12, 13}},
425 \t\t\t{NULL, {}},},
426 };
427 U_BOOT_DEVICE(phandle_source) = {
428 \t.name\t\t= "source",
429 \t.platdata\t= &dtv_phandle_source,
430 \t.platdata_size\t= sizeof(dtv_phandle_source),
431 \t.parent_idx\t= -1,
432 };
433
434 /* Node /phandle-source2 index 3 */
435 static struct dtd_source dtv_phandle_source2 = {
436 \t.clocks\t\t\t= {
437 \t\t\t{NULL, {}},},
438 };
439 U_BOOT_DEVICE(phandle_source2) = {
440 \t.name\t\t= "source",
441 \t.platdata\t= &dtv_phandle_source2,
442 \t.platdata_size\t= sizeof(dtv_phandle_source2),
443 \t.parent_idx\t= -1,
444 };
445
446 void dm_populate_phandle_data(void) {
447 \tdtv_phandle_source.clocks[0].node = DM_GET_DEVICE(phandle_target);
448 \tdtv_phandle_source.clocks[1].node = DM_GET_DEVICE(phandle2_target);
449 \tdtv_phandle_source.clocks[2].node = DM_GET_DEVICE(phandle3_target);
450 \tdtv_phandle_source.clocks[3].node = DM_GET_DEVICE(phandle_target);
451 \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
452 }
453 ''', data)
454
455     def test_phandle_single(self):
456         """Test output from a node containing a phandle reference"""
457         dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
458         output = tools.GetOutputFilename('output')
459         self.run_test(['struct'], dtb_file, output)
460         with open(output) as infile:
461             data = infile.read()
462         self._CheckStrings(HEADER + '''
463 struct dtd_source {
464 \tstruct phandle_0_arg clocks[1];
465 };
466 struct dtd_target {
467 \tfdt32_t\t\tintval;
468 };
469 ''', data)
470
471     def test_phandle_reorder(self):
472         """Test that phandle targets are generated before their references"""
473         dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
474         output = tools.GetOutputFilename('output')
475         self.run_test(['platdata'], dtb_file, output)
476         with open(output) as infile:
477             data = infile.read()
478         self._CheckStrings(C_HEADER + '''
479 /* Node /phandle-target index 1 */
480 static struct dtd_target dtv_phandle_target = {
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 \t.parent_idx\t= -1,
487 };
488
489 /* Node /phandle-source2 index 0 */
490 static struct dtd_source dtv_phandle_source2 = {
491 \t.clocks\t\t\t= {
492 \t\t\t{NULL, {}},},
493 };
494 U_BOOT_DEVICE(phandle_source2) = {
495 \t.name\t\t= "source",
496 \t.platdata\t= &dtv_phandle_source2,
497 \t.platdata_size\t= sizeof(dtv_phandle_source2),
498 \t.parent_idx\t= -1,
499 };
500
501 void dm_populate_phandle_data(void) {
502 \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
503 }
504 ''', data)
505
506     def test_phandle_cd_gpio(self):
507         """Test that phandle targets are generated when unsing cd-gpios"""
508         dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
509         output = tools.GetOutputFilename('output')
510         dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True)
511         with open(output) as infile:
512             data = infile.read()
513         self._CheckStrings(C_HEADER + '''
514 /* Node /phandle2-target index 0 */
515 static struct dtd_target dtv_phandle2_target = {
516 \t.intval\t\t\t= 0x1,
517 };
518 U_BOOT_DEVICE(phandle2_target) = {
519 \t.name\t\t= "target",
520 \t.platdata\t= &dtv_phandle2_target,
521 \t.platdata_size\t= sizeof(dtv_phandle2_target),
522 \t.parent_idx\t= -1,
523 };
524
525 /* Node /phandle3-target index 1 */
526 static struct dtd_target dtv_phandle3_target = {
527 \t.intval\t\t\t= 0x2,
528 };
529 U_BOOT_DEVICE(phandle3_target) = {
530 \t.name\t\t= "target",
531 \t.platdata\t= &dtv_phandle3_target,
532 \t.platdata_size\t= sizeof(dtv_phandle3_target),
533 \t.parent_idx\t= -1,
534 };
535
536 /* Node /phandle-target index 4 */
537 static struct dtd_target dtv_phandle_target = {
538 \t.intval\t\t\t= 0x0,
539 };
540 U_BOOT_DEVICE(phandle_target) = {
541 \t.name\t\t= "target",
542 \t.platdata\t= &dtv_phandle_target,
543 \t.platdata_size\t= sizeof(dtv_phandle_target),
544 \t.parent_idx\t= -1,
545 };
546
547 /* Node /phandle-source index 2 */
548 static struct dtd_source dtv_phandle_source = {
549 \t.cd_gpios\t\t= {
550 \t\t\t{NULL, {}},
551 \t\t\t{NULL, {11}},
552 \t\t\t{NULL, {12, 13}},
553 \t\t\t{NULL, {}},},
554 };
555 U_BOOT_DEVICE(phandle_source) = {
556 \t.name\t\t= "source",
557 \t.platdata\t= &dtv_phandle_source,
558 \t.platdata_size\t= sizeof(dtv_phandle_source),
559 \t.parent_idx\t= -1,
560 };
561
562 /* Node /phandle-source2 index 3 */
563 static struct dtd_source dtv_phandle_source2 = {
564 \t.cd_gpios\t\t= {
565 \t\t\t{NULL, {}},},
566 };
567 U_BOOT_DEVICE(phandle_source2) = {
568 \t.name\t\t= "source",
569 \t.platdata\t= &dtv_phandle_source2,
570 \t.platdata_size\t= sizeof(dtv_phandle_source2),
571 \t.parent_idx\t= -1,
572 };
573
574 void dm_populate_phandle_data(void) {
575 \tdtv_phandle_source.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
576 \tdtv_phandle_source.cd_gpios[1].node = DM_GET_DEVICE(phandle2_target);
577 \tdtv_phandle_source.cd_gpios[2].node = DM_GET_DEVICE(phandle3_target);
578 \tdtv_phandle_source.cd_gpios[3].node = DM_GET_DEVICE(phandle_target);
579 \tdtv_phandle_source2.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
580 }
581 ''', data)
582
583     def test_phandle_bad(self):
584         """Test a node containing an invalid phandle fails"""
585         dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
586                                 capture_stderr=True)
587         output = tools.GetOutputFilename('output')
588         with self.assertRaises(ValueError) as e:
589             self.run_test(['struct'], dtb_file, output)
590         self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
591                       str(e.exception))
592
593     def test_phandle_bad2(self):
594         """Test a phandle target missing its #*-cells property"""
595         dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
596                                 capture_stderr=True)
597         output = tools.GetOutputFilename('output')
598         with self.assertRaises(ValueError) as e:
599             self.run_test(['struct'], dtb_file, output)
600         self.assertIn("Node 'phandle-target' has no cells property",
601                       str(e.exception))
602
603     def test_addresses64(self):
604         """Test output from a node with a 'reg' property with na=2, ns=2"""
605         dtb_file = get_dtb_file('dtoc_test_addr64.dts')
606         output = tools.GetOutputFilename('output')
607         self.run_test(['struct'], dtb_file, output)
608         with open(output) as infile:
609             data = infile.read()
610         self._CheckStrings(HEADER + '''
611 struct dtd_test1 {
612 \tfdt64_t\t\treg[2];
613 };
614 struct dtd_test2 {
615 \tfdt64_t\t\treg[2];
616 };
617 struct dtd_test3 {
618 \tfdt64_t\t\treg[4];
619 };
620 ''', data)
621
622         self.run_test(['platdata'], dtb_file, output)
623         with open(output) as infile:
624             data = infile.read()
625         self._CheckStrings(C_HEADER + '''
626 /* Node /test1 index 0 */
627 static struct dtd_test1 dtv_test1 = {
628 \t.reg\t\t\t= {0x1234, 0x5678},
629 };
630 U_BOOT_DEVICE(test1) = {
631 \t.name\t\t= "test1",
632 \t.platdata\t= &dtv_test1,
633 \t.platdata_size\t= sizeof(dtv_test1),
634 \t.parent_idx\t= -1,
635 };
636
637 /* Node /test2 index 1 */
638 static struct dtd_test2 dtv_test2 = {
639 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
640 };
641 U_BOOT_DEVICE(test2) = {
642 \t.name\t\t= "test2",
643 \t.platdata\t= &dtv_test2,
644 \t.platdata_size\t= sizeof(dtv_test2),
645 \t.parent_idx\t= -1,
646 };
647
648 /* Node /test3 index 2 */
649 static struct dtd_test3 dtv_test3 = {
650 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
651 };
652 U_BOOT_DEVICE(test3) = {
653 \t.name\t\t= "test3",
654 \t.platdata\t= &dtv_test3,
655 \t.platdata_size\t= sizeof(dtv_test3),
656 \t.parent_idx\t= -1,
657 };
658
659 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
660
661     def test_addresses32(self):
662         """Test output from a node with a 'reg' property with na=1, ns=1"""
663         dtb_file = get_dtb_file('dtoc_test_addr32.dts')
664         output = tools.GetOutputFilename('output')
665         self.run_test(['struct'], dtb_file, output)
666         with open(output) as infile:
667             data = infile.read()
668         self._CheckStrings(HEADER + '''
669 struct dtd_test1 {
670 \tfdt32_t\t\treg[2];
671 };
672 struct dtd_test2 {
673 \tfdt32_t\t\treg[4];
674 };
675 ''', data)
676
677         self.run_test(['platdata'], dtb_file, output)
678         with open(output) as infile:
679             data = infile.read()
680         self._CheckStrings(C_HEADER + '''
681 /* Node /test1 index 0 */
682 static struct dtd_test1 dtv_test1 = {
683 \t.reg\t\t\t= {0x1234, 0x5678},
684 };
685 U_BOOT_DEVICE(test1) = {
686 \t.name\t\t= "test1",
687 \t.platdata\t= &dtv_test1,
688 \t.platdata_size\t= sizeof(dtv_test1),
689 \t.parent_idx\t= -1,
690 };
691
692 /* Node /test2 index 1 */
693 static struct dtd_test2 dtv_test2 = {
694 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
695 };
696 U_BOOT_DEVICE(test2) = {
697 \t.name\t\t= "test2",
698 \t.platdata\t= &dtv_test2,
699 \t.platdata_size\t= sizeof(dtv_test2),
700 \t.parent_idx\t= -1,
701 };
702
703 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
704
705     def test_addresses64_32(self):
706         """Test output from a node with a 'reg' property with na=2, ns=1"""
707         dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
708         output = tools.GetOutputFilename('output')
709         self.run_test(['struct'], dtb_file, output)
710         with open(output) as infile:
711             data = infile.read()
712         self._CheckStrings(HEADER + '''
713 struct dtd_test1 {
714 \tfdt64_t\t\treg[2];
715 };
716 struct dtd_test2 {
717 \tfdt64_t\t\treg[2];
718 };
719 struct dtd_test3 {
720 \tfdt64_t\t\treg[4];
721 };
722 ''', data)
723
724         self.run_test(['platdata'], dtb_file, output)
725         with open(output) as infile:
726             data = infile.read()
727         self._CheckStrings(C_HEADER + '''
728 /* Node /test1 index 0 */
729 static struct dtd_test1 dtv_test1 = {
730 \t.reg\t\t\t= {0x123400000000, 0x5678},
731 };
732 U_BOOT_DEVICE(test1) = {
733 \t.name\t\t= "test1",
734 \t.platdata\t= &dtv_test1,
735 \t.platdata_size\t= sizeof(dtv_test1),
736 \t.parent_idx\t= -1,
737 };
738
739 /* Node /test2 index 1 */
740 static struct dtd_test2 dtv_test2 = {
741 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
742 };
743 U_BOOT_DEVICE(test2) = {
744 \t.name\t\t= "test2",
745 \t.platdata\t= &dtv_test2,
746 \t.platdata_size\t= sizeof(dtv_test2),
747 \t.parent_idx\t= -1,
748 };
749
750 /* Node /test3 index 2 */
751 static struct dtd_test3 dtv_test3 = {
752 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
753 };
754 U_BOOT_DEVICE(test3) = {
755 \t.name\t\t= "test3",
756 \t.platdata\t= &dtv_test3,
757 \t.platdata_size\t= sizeof(dtv_test3),
758 \t.parent_idx\t= -1,
759 };
760
761 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
762
763     def test_addresses32_64(self):
764         """Test output from a node with a 'reg' property with na=1, ns=2"""
765         dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
766         output = tools.GetOutputFilename('output')
767         self.run_test(['struct'], dtb_file, output)
768         with open(output) as infile:
769             data = infile.read()
770         self._CheckStrings(HEADER + '''
771 struct dtd_test1 {
772 \tfdt64_t\t\treg[2];
773 };
774 struct dtd_test2 {
775 \tfdt64_t\t\treg[2];
776 };
777 struct dtd_test3 {
778 \tfdt64_t\t\treg[4];
779 };
780 ''', data)
781
782         self.run_test(['platdata'], dtb_file, output)
783         with open(output) as infile:
784             data = infile.read()
785         self._CheckStrings(C_HEADER + '''
786 /* Node /test1 index 0 */
787 static struct dtd_test1 dtv_test1 = {
788 \t.reg\t\t\t= {0x1234, 0x567800000000},
789 };
790 U_BOOT_DEVICE(test1) = {
791 \t.name\t\t= "test1",
792 \t.platdata\t= &dtv_test1,
793 \t.platdata_size\t= sizeof(dtv_test1),
794 \t.parent_idx\t= -1,
795 };
796
797 /* Node /test2 index 1 */
798 static struct dtd_test2 dtv_test2 = {
799 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
800 };
801 U_BOOT_DEVICE(test2) = {
802 \t.name\t\t= "test2",
803 \t.platdata\t= &dtv_test2,
804 \t.platdata_size\t= sizeof(dtv_test2),
805 \t.parent_idx\t= -1,
806 };
807
808 /* Node /test3 index 2 */
809 static struct dtd_test3 dtv_test3 = {
810 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
811 };
812 U_BOOT_DEVICE(test3) = {
813 \t.name\t\t= "test3",
814 \t.platdata\t= &dtv_test3,
815 \t.platdata_size\t= sizeof(dtv_test3),
816 \t.parent_idx\t= -1,
817 };
818
819 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
820
821     def test_bad_reg(self):
822         """Test that a reg property with an invalid type generates an error"""
823         # Capture stderr since dtc will emit warnings for this file
824         dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
825         output = tools.GetOutputFilename('output')
826         with self.assertRaises(ValueError) as e:
827             self.run_test(['struct'], dtb_file, output)
828         self.assertIn("Node 'spl-test' reg property is not an int",
829                       str(e.exception))
830
831     def test_bad_reg2(self):
832         """Test that a reg property with an invalid cell count is detected"""
833         # Capture stderr since dtc will emit warnings for this file
834         dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
835         output = tools.GetOutputFilename('output')
836         with self.assertRaises(ValueError) as e:
837             self.run_test(['struct'], dtb_file, output)
838         self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
839                       str(e.exception))
840
841     def test_add_prop(self):
842         """Test that a subequent node can add a new property to a struct"""
843         dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
844         output = tools.GetOutputFilename('output')
845         self.run_test(['struct'], dtb_file, output)
846         with open(output) as infile:
847             data = infile.read()
848         self._CheckStrings(HEADER + '''
849 struct dtd_sandbox_spl_test {
850 \tfdt32_t\t\tintarray;
851 \tfdt32_t\t\tintval;
852 };
853 ''', data)
854
855         self.run_test(['platdata'], dtb_file, output)
856         with open(output) as infile:
857             data = infile.read()
858         self._CheckStrings(C_HEADER + '''
859 /* Node /spl-test index 0 */
860 static struct dtd_sandbox_spl_test dtv_spl_test = {
861 \t.intval\t\t\t= 0x1,
862 };
863 U_BOOT_DEVICE(spl_test) = {
864 \t.name\t\t= "sandbox_spl_test",
865 \t.platdata\t= &dtv_spl_test,
866 \t.platdata_size\t= sizeof(dtv_spl_test),
867 \t.parent_idx\t= -1,
868 };
869
870 /* Node /spl-test2 index 1 */
871 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
872 \t.intarray\t\t= 0x5,
873 };
874 U_BOOT_DEVICE(spl_test2) = {
875 \t.name\t\t= "sandbox_spl_test",
876 \t.platdata\t= &dtv_spl_test2,
877 \t.platdata_size\t= sizeof(dtv_spl_test2),
878 \t.parent_idx\t= -1,
879 };
880
881 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
882
883     def testStdout(self):
884         """Test output to stdout"""
885         dtb_file = get_dtb_file('dtoc_test_simple.dts')
886         with test_util.capture_sys_output() as (stdout, stderr):
887             self.run_test(['struct'], dtb_file, '-')
888
889     def testNoCommand(self):
890         """Test running dtoc without a command"""
891         with self.assertRaises(ValueError) as e:
892             self.run_test([], '', '')
893         self.assertIn("Please specify a command: struct, platdata",
894                       str(e.exception))
895
896     def testBadCommand(self):
897         """Test running dtoc with an invalid command"""
898         dtb_file = get_dtb_file('dtoc_test_simple.dts')
899         output = tools.GetOutputFilename('output')
900         with self.assertRaises(ValueError) as e:
901             self.run_test(['invalid-cmd'], dtb_file, output)
902         self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
903                       str(e.exception))
904
905     def testScanDrivers(self):
906         """Test running dtoc with additional drivers to scan"""
907         dtb_file = get_dtb_file('dtoc_test_simple.dts')
908         output = tools.GetOutputFilename('output')
909         with test_util.capture_sys_output() as (stdout, stderr):
910             dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
911                                [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
912
913     def testUnicodeError(self):
914         """Test running dtoc with an invalid unicode file
915
916         To be able to perform this test without adding a weird text file which
917         would produce issues when using checkpatch.pl or patman, generate the
918         file at runtime and then process it.
919         """
920         dtb_file = get_dtb_file('dtoc_test_simple.dts')
921         output = tools.GetOutputFilename('output')
922         driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
923         with open(driver_fn, 'wb+') as df:
924             df.write(b'\x81')
925
926         with test_util.capture_sys_output() as (stdout, stderr):
927             dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
928                                [driver_fn])