857a98e435f2c5ca42b7101de3a55acd6afe12e8
[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 };
220
221 /* Node /i2c@0/pmic@9 index 1 */
222 static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
223 \t.low_power\t\t= true,
224 \t.reg\t\t\t= {0x9, 0x0},
225 };
226 U_BOOT_DEVICE(pmic_at_9) = {
227 \t.name\t\t= "sandbox_pmic_test",
228 \t.platdata\t= &dtv_pmic_at_9,
229 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
230 };
231
232 /* Node /spl-test index 2 */
233 static struct dtd_sandbox_spl_test dtv_spl_test = {
234 \t.boolval\t\t= true,
235 \t.bytearray\t\t= {0x6, 0x0, 0x0},
236 \t.byteval\t\t= 0x5,
237 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
238 \t.intval\t\t\t= 0x1,
239 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
240 \t\t0x11},
241 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
242 \t.stringarray\t\t= {"multi-word", "message", ""},
243 \t.stringval\t\t= "message",
244 };
245 U_BOOT_DEVICE(spl_test) = {
246 \t.name\t\t= "sandbox_spl_test",
247 \t.platdata\t= &dtv_spl_test,
248 \t.platdata_size\t= sizeof(dtv_spl_test),
249 };
250
251 /* Node /spl-test2 index 3 */
252 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
253 \t.acpi_name\t\t= "\\\\_SB.GPO0",
254 \t.bytearray\t\t= {0x1, 0x23, 0x34},
255 \t.byteval\t\t= 0x8,
256 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
257 \t.intval\t\t\t= 0x3,
258 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
259 \t\t0x0},
260 \t.stringarray\t\t= {"another", "multi-word", "message"},
261 \t.stringval\t\t= "message2",
262 };
263 U_BOOT_DEVICE(spl_test2) = {
264 \t.name\t\t= "sandbox_spl_test",
265 \t.platdata\t= &dtv_spl_test2,
266 \t.platdata_size\t= sizeof(dtv_spl_test2),
267 };
268
269 /* Node /spl-test3 index 4 */
270 static struct dtd_sandbox_spl_test dtv_spl_test3 = {
271 \t.stringarray\t\t= {"one", "", ""},
272 };
273 U_BOOT_DEVICE(spl_test3) = {
274 \t.name\t\t= "sandbox_spl_test",
275 \t.platdata\t= &dtv_spl_test3,
276 \t.platdata_size\t= sizeof(dtv_spl_test3),
277 };
278
279 /* Node /spl-test4 index 5 */
280 static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
281 };
282 U_BOOT_DEVICE(spl_test4) = {
283 \t.name\t\t= "sandbox_spl_test_2",
284 \t.platdata\t= &dtv_spl_test4,
285 \t.platdata_size\t= sizeof(dtv_spl_test4),
286 };
287
288 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
289
290     def test_driver_alias(self):
291         """Test output from a device tree file with a driver alias"""
292         dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
293         output = tools.GetOutputFilename('output')
294         self.run_test(['struct'], dtb_file, output)
295         with open(output) as infile:
296             data = infile.read()
297         self._CheckStrings(HEADER + '''
298 struct dtd_sandbox_gpio {
299 \tconst char *\tgpio_bank_name;
300 \tbool\t\tgpio_controller;
301 \tfdt32_t\t\tsandbox_gpio_count;
302 };
303 ''', data)
304
305         self.run_test(['platdata'], dtb_file, output)
306         with open(output) as infile:
307             data = infile.read()
308         self._CheckStrings(C_HEADER + '''
309 /* Node /gpios@0 index 0 */
310 static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
311 \t.gpio_bank_name\t\t= "a",
312 \t.gpio_controller\t= true,
313 \t.sandbox_gpio_count\t= 0x14,
314 };
315 U_BOOT_DEVICE(gpios_at_0) = {
316 \t.name\t\t= "sandbox_gpio",
317 \t.platdata\t= &dtv_gpios_at_0,
318 \t.platdata_size\t= sizeof(dtv_gpios_at_0),
319 };
320
321 void dm_populate_phandle_data(void) {
322 }
323 ''', data)
324
325     def test_invalid_driver(self):
326         """Test output from a device tree file with an invalid driver"""
327         dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
328         output = tools.GetOutputFilename('output')
329         with test_util.capture_sys_output() as (stdout, stderr):
330             dtb_platdata.run_steps(['struct'], dtb_file, False, output)
331         with open(output) as infile:
332             data = infile.read()
333         self._CheckStrings(HEADER + '''
334 struct dtd_invalid {
335 };
336 ''', data)
337
338         with test_util.capture_sys_output() as (stdout, stderr):
339             dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
340         with open(output) as infile:
341             data = infile.read()
342         self._CheckStrings(C_HEADER + '''
343 /* Node /spl-test index 0 */
344 static struct dtd_invalid dtv_spl_test = {
345 };
346 U_BOOT_DEVICE(spl_test) = {
347 \t.name\t\t= "invalid",
348 \t.platdata\t= &dtv_spl_test,
349 \t.platdata_size\t= sizeof(dtv_spl_test),
350 };
351
352 void dm_populate_phandle_data(void) {
353 }
354 ''', data)
355
356     def test_phandle(self):
357         """Test output from a node containing a phandle reference"""
358         dtb_file = get_dtb_file('dtoc_test_phandle.dts')
359         output = tools.GetOutputFilename('output')
360         self.run_test(['struct'], dtb_file, output)
361         with open(output) as infile:
362             data = infile.read()
363         self._CheckStrings(HEADER + '''
364 struct dtd_source {
365 \tstruct phandle_2_arg clocks[4];
366 };
367 struct dtd_target {
368 \tfdt32_t\t\tintval;
369 };
370 ''', data)
371
372         self.run_test(['platdata'], dtb_file, output)
373         with open(output) as infile:
374             data = infile.read()
375         self._CheckStrings(C_HEADER + '''
376 /* Node /phandle2-target index 0 */
377 static struct dtd_target dtv_phandle2_target = {
378 \t.intval\t\t\t= 0x1,
379 };
380 U_BOOT_DEVICE(phandle2_target) = {
381 \t.name\t\t= "target",
382 \t.platdata\t= &dtv_phandle2_target,
383 \t.platdata_size\t= sizeof(dtv_phandle2_target),
384 };
385
386 /* Node /phandle3-target index 1 */
387 static struct dtd_target dtv_phandle3_target = {
388 \t.intval\t\t\t= 0x2,
389 };
390 U_BOOT_DEVICE(phandle3_target) = {
391 \t.name\t\t= "target",
392 \t.platdata\t= &dtv_phandle3_target,
393 \t.platdata_size\t= sizeof(dtv_phandle3_target),
394 };
395
396 /* Node /phandle-target index 4 */
397 static struct dtd_target dtv_phandle_target = {
398 \t.intval\t\t\t= 0x0,
399 };
400 U_BOOT_DEVICE(phandle_target) = {
401 \t.name\t\t= "target",
402 \t.platdata\t= &dtv_phandle_target,
403 \t.platdata_size\t= sizeof(dtv_phandle_target),
404 };
405
406 /* Node /phandle-source index 2 */
407 static struct dtd_source dtv_phandle_source = {
408 \t.clocks\t\t\t= {
409 \t\t\t{NULL, {}},
410 \t\t\t{NULL, {11}},
411 \t\t\t{NULL, {12, 13}},
412 \t\t\t{NULL, {}},},
413 };
414 U_BOOT_DEVICE(phandle_source) = {
415 \t.name\t\t= "source",
416 \t.platdata\t= &dtv_phandle_source,
417 \t.platdata_size\t= sizeof(dtv_phandle_source),
418 };
419
420 /* Node /phandle-source2 index 3 */
421 static struct dtd_source dtv_phandle_source2 = {
422 \t.clocks\t\t\t= {
423 \t\t\t{NULL, {}},},
424 };
425 U_BOOT_DEVICE(phandle_source2) = {
426 \t.name\t\t= "source",
427 \t.platdata\t= &dtv_phandle_source2,
428 \t.platdata_size\t= sizeof(dtv_phandle_source2),
429 };
430
431 void dm_populate_phandle_data(void) {
432 \tdtv_phandle_source.clocks[0].node = DM_GET_DEVICE(phandle_target);
433 \tdtv_phandle_source.clocks[1].node = DM_GET_DEVICE(phandle2_target);
434 \tdtv_phandle_source.clocks[2].node = DM_GET_DEVICE(phandle3_target);
435 \tdtv_phandle_source.clocks[3].node = DM_GET_DEVICE(phandle_target);
436 \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
437 }
438 ''', data)
439
440     def test_phandle_single(self):
441         """Test output from a node containing a phandle reference"""
442         dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
443         output = tools.GetOutputFilename('output')
444         self.run_test(['struct'], dtb_file, output)
445         with open(output) as infile:
446             data = infile.read()
447         self._CheckStrings(HEADER + '''
448 struct dtd_source {
449 \tstruct phandle_0_arg clocks[1];
450 };
451 struct dtd_target {
452 \tfdt32_t\t\tintval;
453 };
454 ''', data)
455
456     def test_phandle_reorder(self):
457         """Test that phandle targets are generated before their references"""
458         dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
459         output = tools.GetOutputFilename('output')
460         self.run_test(['platdata'], dtb_file, output)
461         with open(output) as infile:
462             data = infile.read()
463         self._CheckStrings(C_HEADER + '''
464 /* Node /phandle-target index 1 */
465 static struct dtd_target dtv_phandle_target = {
466 };
467 U_BOOT_DEVICE(phandle_target) = {
468 \t.name\t\t= "target",
469 \t.platdata\t= &dtv_phandle_target,
470 \t.platdata_size\t= sizeof(dtv_phandle_target),
471 };
472
473 /* Node /phandle-source2 index 0 */
474 static struct dtd_source dtv_phandle_source2 = {
475 \t.clocks\t\t\t= {
476 \t\t\t{NULL, {}},},
477 };
478 U_BOOT_DEVICE(phandle_source2) = {
479 \t.name\t\t= "source",
480 \t.platdata\t= &dtv_phandle_source2,
481 \t.platdata_size\t= sizeof(dtv_phandle_source2),
482 };
483
484 void dm_populate_phandle_data(void) {
485 \tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
486 }
487 ''', data)
488
489     def test_phandle_cd_gpio(self):
490         """Test that phandle targets are generated when unsing cd-gpios"""
491         dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
492         output = tools.GetOutputFilename('output')
493         dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True)
494         with open(output) as infile:
495             data = infile.read()
496         self._CheckStrings(C_HEADER + '''
497 /* Node /phandle2-target index 0 */
498 static struct dtd_target dtv_phandle2_target = {
499 \t.intval\t\t\t= 0x1,
500 };
501 U_BOOT_DEVICE(phandle2_target) = {
502 \t.name\t\t= "target",
503 \t.platdata\t= &dtv_phandle2_target,
504 \t.platdata_size\t= sizeof(dtv_phandle2_target),
505 };
506
507 /* Node /phandle3-target index 1 */
508 static struct dtd_target dtv_phandle3_target = {
509 \t.intval\t\t\t= 0x2,
510 };
511 U_BOOT_DEVICE(phandle3_target) = {
512 \t.name\t\t= "target",
513 \t.platdata\t= &dtv_phandle3_target,
514 \t.platdata_size\t= sizeof(dtv_phandle3_target),
515 };
516
517 /* Node /phandle-target index 4 */
518 static struct dtd_target dtv_phandle_target = {
519 \t.intval\t\t\t= 0x0,
520 };
521 U_BOOT_DEVICE(phandle_target) = {
522 \t.name\t\t= "target",
523 \t.platdata\t= &dtv_phandle_target,
524 \t.platdata_size\t= sizeof(dtv_phandle_target),
525 };
526
527 /* Node /phandle-source index 2 */
528 static struct dtd_source dtv_phandle_source = {
529 \t.cd_gpios\t\t= {
530 \t\t\t{NULL, {}},
531 \t\t\t{NULL, {11}},
532 \t\t\t{NULL, {12, 13}},
533 \t\t\t{NULL, {}},},
534 };
535 U_BOOT_DEVICE(phandle_source) = {
536 \t.name\t\t= "source",
537 \t.platdata\t= &dtv_phandle_source,
538 \t.platdata_size\t= sizeof(dtv_phandle_source),
539 };
540
541 /* Node /phandle-source2 index 3 */
542 static struct dtd_source dtv_phandle_source2 = {
543 \t.cd_gpios\t\t= {
544 \t\t\t{NULL, {}},},
545 };
546 U_BOOT_DEVICE(phandle_source2) = {
547 \t.name\t\t= "source",
548 \t.platdata\t= &dtv_phandle_source2,
549 \t.platdata_size\t= sizeof(dtv_phandle_source2),
550 };
551
552 void dm_populate_phandle_data(void) {
553 \tdtv_phandle_source.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
554 \tdtv_phandle_source.cd_gpios[1].node = DM_GET_DEVICE(phandle2_target);
555 \tdtv_phandle_source.cd_gpios[2].node = DM_GET_DEVICE(phandle3_target);
556 \tdtv_phandle_source.cd_gpios[3].node = DM_GET_DEVICE(phandle_target);
557 \tdtv_phandle_source2.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
558 }
559 ''', data)
560
561     def test_phandle_bad(self):
562         """Test a node containing an invalid phandle fails"""
563         dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
564                                 capture_stderr=True)
565         output = tools.GetOutputFilename('output')
566         with self.assertRaises(ValueError) as e:
567             self.run_test(['struct'], dtb_file, output)
568         self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
569                       str(e.exception))
570
571     def test_phandle_bad2(self):
572         """Test a phandle target missing its #*-cells property"""
573         dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
574                                 capture_stderr=True)
575         output = tools.GetOutputFilename('output')
576         with self.assertRaises(ValueError) as e:
577             self.run_test(['struct'], dtb_file, output)
578         self.assertIn("Node 'phandle-target' has no cells property",
579                       str(e.exception))
580
581     def test_addresses64(self):
582         """Test output from a node with a 'reg' property with na=2, ns=2"""
583         dtb_file = get_dtb_file('dtoc_test_addr64.dts')
584         output = tools.GetOutputFilename('output')
585         self.run_test(['struct'], dtb_file, output)
586         with open(output) as infile:
587             data = infile.read()
588         self._CheckStrings(HEADER + '''
589 struct dtd_test1 {
590 \tfdt64_t\t\treg[2];
591 };
592 struct dtd_test2 {
593 \tfdt64_t\t\treg[2];
594 };
595 struct dtd_test3 {
596 \tfdt64_t\t\treg[4];
597 };
598 ''', data)
599
600         self.run_test(['platdata'], dtb_file, output)
601         with open(output) as infile:
602             data = infile.read()
603         self._CheckStrings(C_HEADER + '''
604 /* Node /test1 index 0 */
605 static struct dtd_test1 dtv_test1 = {
606 \t.reg\t\t\t= {0x1234, 0x5678},
607 };
608 U_BOOT_DEVICE(test1) = {
609 \t.name\t\t= "test1",
610 \t.platdata\t= &dtv_test1,
611 \t.platdata_size\t= sizeof(dtv_test1),
612 };
613
614 /* Node /test2 index 1 */
615 static struct dtd_test2 dtv_test2 = {
616 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
617 };
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),
622 };
623
624 /* Node /test3 index 2 */
625 static struct dtd_test3 dtv_test3 = {
626 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
627 };
628 U_BOOT_DEVICE(test3) = {
629 \t.name\t\t= "test3",
630 \t.platdata\t= &dtv_test3,
631 \t.platdata_size\t= sizeof(dtv_test3),
632 };
633
634 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
635
636     def test_addresses32(self):
637         """Test output from a node with a 'reg' property with na=1, ns=1"""
638         dtb_file = get_dtb_file('dtoc_test_addr32.dts')
639         output = tools.GetOutputFilename('output')
640         self.run_test(['struct'], dtb_file, output)
641         with open(output) as infile:
642             data = infile.read()
643         self._CheckStrings(HEADER + '''
644 struct dtd_test1 {
645 \tfdt32_t\t\treg[2];
646 };
647 struct dtd_test2 {
648 \tfdt32_t\t\treg[4];
649 };
650 ''', data)
651
652         self.run_test(['platdata'], dtb_file, output)
653         with open(output) as infile:
654             data = infile.read()
655         self._CheckStrings(C_HEADER + '''
656 /* Node /test1 index 0 */
657 static struct dtd_test1 dtv_test1 = {
658 \t.reg\t\t\t= {0x1234, 0x5678},
659 };
660 U_BOOT_DEVICE(test1) = {
661 \t.name\t\t= "test1",
662 \t.platdata\t= &dtv_test1,
663 \t.platdata_size\t= sizeof(dtv_test1),
664 };
665
666 /* Node /test2 index 1 */
667 static struct dtd_test2 dtv_test2 = {
668 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
669 };
670 U_BOOT_DEVICE(test2) = {
671 \t.name\t\t= "test2",
672 \t.platdata\t= &dtv_test2,
673 \t.platdata_size\t= sizeof(dtv_test2),
674 };
675
676 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
677
678     def test_addresses64_32(self):
679         """Test output from a node with a 'reg' property with na=2, ns=1"""
680         dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
681         output = tools.GetOutputFilename('output')
682         self.run_test(['struct'], dtb_file, output)
683         with open(output) as infile:
684             data = infile.read()
685         self._CheckStrings(HEADER + '''
686 struct dtd_test1 {
687 \tfdt64_t\t\treg[2];
688 };
689 struct dtd_test2 {
690 \tfdt64_t\t\treg[2];
691 };
692 struct dtd_test3 {
693 \tfdt64_t\t\treg[4];
694 };
695 ''', data)
696
697         self.run_test(['platdata'], dtb_file, output)
698         with open(output) as infile:
699             data = infile.read()
700         self._CheckStrings(C_HEADER + '''
701 /* Node /test1 index 0 */
702 static struct dtd_test1 dtv_test1 = {
703 \t.reg\t\t\t= {0x123400000000, 0x5678},
704 };
705 U_BOOT_DEVICE(test1) = {
706 \t.name\t\t= "test1",
707 \t.platdata\t= &dtv_test1,
708 \t.platdata_size\t= sizeof(dtv_test1),
709 };
710
711 /* Node /test2 index 1 */
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 /* Node /test3 index 2 */
722 static struct dtd_test3 dtv_test3 = {
723 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
724 };
725 U_BOOT_DEVICE(test3) = {
726 \t.name\t\t= "test3",
727 \t.platdata\t= &dtv_test3,
728 \t.platdata_size\t= sizeof(dtv_test3),
729 };
730
731 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
732
733     def test_addresses32_64(self):
734         """Test output from a node with a 'reg' property with na=1, ns=2"""
735         dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
736         output = tools.GetOutputFilename('output')
737         self.run_test(['struct'], dtb_file, output)
738         with open(output) as infile:
739             data = infile.read()
740         self._CheckStrings(HEADER + '''
741 struct dtd_test1 {
742 \tfdt64_t\t\treg[2];
743 };
744 struct dtd_test2 {
745 \tfdt64_t\t\treg[2];
746 };
747 struct dtd_test3 {
748 \tfdt64_t\t\treg[4];
749 };
750 ''', data)
751
752         self.run_test(['platdata'], dtb_file, output)
753         with open(output) as infile:
754             data = infile.read()
755         self._CheckStrings(C_HEADER + '''
756 /* Node /test1 index 0 */
757 static struct dtd_test1 dtv_test1 = {
758 \t.reg\t\t\t= {0x1234, 0x567800000000},
759 };
760 U_BOOT_DEVICE(test1) = {
761 \t.name\t\t= "test1",
762 \t.platdata\t= &dtv_test1,
763 \t.platdata_size\t= sizeof(dtv_test1),
764 };
765
766 /* Node /test2 index 1 */
767 static struct dtd_test2 dtv_test2 = {
768 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
769 };
770 U_BOOT_DEVICE(test2) = {
771 \t.name\t\t= "test2",
772 \t.platdata\t= &dtv_test2,
773 \t.platdata_size\t= sizeof(dtv_test2),
774 };
775
776 /* Node /test3 index 2 */
777 static struct dtd_test3 dtv_test3 = {
778 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
779 };
780 U_BOOT_DEVICE(test3) = {
781 \t.name\t\t= "test3",
782 \t.platdata\t= &dtv_test3,
783 \t.platdata_size\t= sizeof(dtv_test3),
784 };
785
786 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
787
788     def test_bad_reg(self):
789         """Test that a reg property with an invalid type generates an error"""
790         # Capture stderr since dtc will emit warnings for this file
791         dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
792         output = tools.GetOutputFilename('output')
793         with self.assertRaises(ValueError) as e:
794             self.run_test(['struct'], dtb_file, output)
795         self.assertIn("Node 'spl-test' reg property is not an int",
796                       str(e.exception))
797
798     def test_bad_reg2(self):
799         """Test that a reg property with an invalid cell count is detected"""
800         # Capture stderr since dtc will emit warnings for this file
801         dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
802         output = tools.GetOutputFilename('output')
803         with self.assertRaises(ValueError) as e:
804             self.run_test(['struct'], dtb_file, output)
805         self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
806                       str(e.exception))
807
808     def test_add_prop(self):
809         """Test that a subequent node can add a new property to a struct"""
810         dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
811         output = tools.GetOutputFilename('output')
812         self.run_test(['struct'], dtb_file, output)
813         with open(output) as infile:
814             data = infile.read()
815         self._CheckStrings(HEADER + '''
816 struct dtd_sandbox_spl_test {
817 \tfdt32_t\t\tintarray;
818 \tfdt32_t\t\tintval;
819 };
820 ''', data)
821
822         self.run_test(['platdata'], dtb_file, output)
823         with open(output) as infile:
824             data = infile.read()
825         self._CheckStrings(C_HEADER + '''
826 /* Node /spl-test index 0 */
827 static struct dtd_sandbox_spl_test dtv_spl_test = {
828 \t.intval\t\t\t= 0x1,
829 };
830 U_BOOT_DEVICE(spl_test) = {
831 \t.name\t\t= "sandbox_spl_test",
832 \t.platdata\t= &dtv_spl_test,
833 \t.platdata_size\t= sizeof(dtv_spl_test),
834 };
835
836 /* Node /spl-test2 index 1 */
837 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
838 \t.intarray\t\t= 0x5,
839 };
840 U_BOOT_DEVICE(spl_test2) = {
841 \t.name\t\t= "sandbox_spl_test",
842 \t.platdata\t= &dtv_spl_test2,
843 \t.platdata_size\t= sizeof(dtv_spl_test2),
844 };
845
846 ''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
847
848     def testStdout(self):
849         """Test output to stdout"""
850         dtb_file = get_dtb_file('dtoc_test_simple.dts')
851         with test_util.capture_sys_output() as (stdout, stderr):
852             self.run_test(['struct'], dtb_file, '-')
853
854     def testNoCommand(self):
855         """Test running dtoc without a command"""
856         with self.assertRaises(ValueError) as e:
857             self.run_test([], '', '')
858         self.assertIn("Please specify a command: struct, platdata",
859                       str(e.exception))
860
861     def testBadCommand(self):
862         """Test running dtoc with an invalid command"""
863         dtb_file = get_dtb_file('dtoc_test_simple.dts')
864         output = tools.GetOutputFilename('output')
865         with self.assertRaises(ValueError) as e:
866             self.run_test(['invalid-cmd'], dtb_file, output)
867         self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
868                       str(e.exception))
869
870     def testScanDrivers(self):
871         """Test running dtoc with additional drivers to scan"""
872         dtb_file = get_dtb_file('dtoc_test_simple.dts')
873         output = tools.GetOutputFilename('output')
874         with test_util.capture_sys_output() as (stdout, stderr):
875             dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
876                                [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
877
878     def testUnicodeError(self):
879         """Test running dtoc with an invalid unicode file
880
881         To be able to perform this test without adding a weird text file which
882         would produce issues when using checkpatch.pl or patman, generate the
883         file at runtime and then process it.
884         """
885         dtb_file = get_dtb_file('dtoc_test_simple.dts')
886         output = tools.GetOutputFilename('output')
887         driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
888         with open(driver_fn, 'wb+') as df:
889             df.write(b'\x81')
890
891         with test_util.capture_sys_output() as (stdout, stderr):
892             dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
893                                [driver_fn])