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