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