dtoc: Update fdt tests to increase code coverage
[platform/kernel/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 import collections
12 import os
13 import struct
14 import unittest
15
16 import dtb_platdata
17 from dtb_platdata import conv_name_to_c
18 from dtb_platdata import get_compat_name
19 from dtb_platdata import get_value
20 from dtb_platdata import tab_to
21 import fdt
22 import fdt_util
23 import tools
24
25 our_path = os.path.dirname(os.path.realpath(__file__))
26
27
28 HEADER = '''/*
29  * DO NOT MODIFY
30  *
31  * This file was generated by dtoc from a .dtb (device tree binary) file.
32  */
33
34 #include <stdbool.h>
35 #include <linux/libfdt.h>'''
36
37 C_HEADER = '''/*
38  * DO NOT MODIFY
39  *
40  * This file was generated by dtoc from a .dtb (device tree binary) file.
41  */
42
43 #include <common.h>
44 #include <dm.h>
45 #include <dt-structs.h>
46 '''
47
48
49 def get_dtb_file(dts_fname):
50     """Compile a .dts file to a .dtb
51
52     Args:
53         dts_fname: Filename of .dts file in the current directory
54
55     Returns:
56         Filename of compiled file in output directory
57     """
58     return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname))
59
60
61 class TestDtoc(unittest.TestCase):
62     """Tests for dtoc"""
63     @classmethod
64     def setUpClass(cls):
65         tools.PrepareOutputDir(None)
66
67     @classmethod
68     def tearDownClass(cls):
69         tools._RemoveOutputDir()
70
71     def _WritePythonString(self, fname, data):
72         """Write a string with tabs expanded as done in this Python file
73
74         Args:
75             fname: Filename to write to
76             data: Raw string to convert
77         """
78         data = data.replace('\t', '\\t')
79         with open(fname, 'w') as fd:
80             fd.write(data)
81
82     def _CheckStrings(self, expected, actual):
83         """Check that a string matches its expected value
84
85         If the strings do not match, they are written to the /tmp directory in
86         the same Python format as is used here in the test. This allows for
87         easy comparison and update of the tests.
88
89         Args:
90             expected: Expected string
91             actual: Actual string
92         """
93         if expected != actual:
94             self._WritePythonString('/tmp/binman.expected', expected)
95             self._WritePythonString('/tmp/binman.actual', actual)
96             print 'Failures written to /tmp/binman.{expected,actual}'
97         self.assertEquals(expected, actual)
98
99     def test_name(self):
100         """Test conversion of device tree names to C identifiers"""
101         self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
102         self.assertEqual('vendor_clock_frequency',
103                          conv_name_to_c('vendor,clock-frequency'))
104         self.assertEqual('rockchip_rk3399_sdhci_5_1',
105                          conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
106
107     def test_tab_to(self):
108         """Test operation of tab_to() function"""
109         self.assertEqual('fred ', tab_to(0, 'fred'))
110         self.assertEqual('fred\t', tab_to(1, 'fred'))
111         self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
112         self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
113         self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
114         self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
115
116     def test_get_value(self):
117         """Test operation of get_value() function"""
118         self.assertEqual('0x45',
119                          get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
120         self.assertEqual('0x45',
121                          get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
122         self.assertEqual('0x0',
123                          get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
124         self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
125         self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
126
127     def test_get_compat_name(self):
128         """Test operation of get_compat_name() function"""
129         Prop = collections.namedtuple('Prop', ['value'])
130         Node = collections.namedtuple('Node', ['props'])
131
132         prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
133         node = Node({'compatible': prop})
134         self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
135                          get_compat_name(node))
136
137         prop = Prop(['rockchip,rk3399-sdhci-5.1'])
138         node = Node({'compatible': prop})
139         self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
140                          get_compat_name(node))
141
142         prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
143         node = Node({'compatible': prop})
144         self.assertEqual(('rockchip_rk3399_sdhci_5_1',
145                           ['arasan_sdhci_5_1', 'third']),
146                          get_compat_name(node))
147
148     def test_empty_file(self):
149         """Test output from a device tree file with no nodes"""
150         dtb_file = get_dtb_file('dtoc_test_empty.dts')
151         output = tools.GetOutputFilename('output')
152         dtb_platdata.run_steps(['struct'], dtb_file, False, output)
153         with open(output) as infile:
154             lines = infile.read().splitlines()
155         self.assertEqual(HEADER.splitlines(), lines)
156
157         dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
158         with open(output) as infile:
159             lines = infile.read().splitlines()
160         self.assertEqual(C_HEADER.splitlines() + [''], lines)
161
162     def test_simple(self):
163         """Test output from some simple nodes with various types of data"""
164         dtb_file = get_dtb_file('dtoc_test_simple.dts')
165         output = tools.GetOutputFilename('output')
166         dtb_platdata.run_steps(['struct'], dtb_file, False, output)
167         with open(output) as infile:
168             data = infile.read()
169         self._CheckStrings(HEADER + '''
170 struct dtd_sandbox_i2c_test {
171 };
172 struct dtd_sandbox_pmic_test {
173 \tbool\t\tlow_power;
174 \tfdt64_t\t\treg[2];
175 };
176 struct dtd_sandbox_spl_test {
177 \tbool\t\tboolval;
178 \tunsigned char\tbytearray[3];
179 \tunsigned char\tbyteval;
180 \tfdt32_t\t\tintarray[4];
181 \tfdt32_t\t\tintval;
182 \tunsigned char\tlongbytearray[9];
183 \tunsigned char\tnotstring[5];
184 \tconst char *\tstringarray[3];
185 \tconst char *\tstringval;
186 };
187 struct dtd_sandbox_spl_test_2 {
188 };
189 ''', data)
190
191         dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
192         with open(output) as infile:
193             data = infile.read()
194         self._CheckStrings(C_HEADER + '''
195 static struct dtd_sandbox_spl_test dtv_spl_test = {
196 \t.bytearray\t\t= {0x6, 0x0, 0x0},
197 \t.byteval\t\t= 0x5,
198 \t.intval\t\t\t= 0x1,
199 \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
200 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
201 \t\t0x11},
202 \t.stringval\t\t= "message",
203 \t.boolval\t\t= true,
204 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
205 \t.stringarray\t\t= {"multi-word", "message", ""},
206 };
207 U_BOOT_DEVICE(spl_test) = {
208 \t.name\t\t= "sandbox_spl_test",
209 \t.platdata\t= &dtv_spl_test,
210 \t.platdata_size\t= sizeof(dtv_spl_test),
211 };
212
213 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
214 \t.bytearray\t\t= {0x1, 0x23, 0x34},
215 \t.byteval\t\t= 0x8,
216 \t.intval\t\t\t= 0x3,
217 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
218 \t\t0x0},
219 \t.stringval\t\t= "message2",
220 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
221 \t.stringarray\t\t= {"another", "multi-word", "message"},
222 };
223 U_BOOT_DEVICE(spl_test2) = {
224 \t.name\t\t= "sandbox_spl_test",
225 \t.platdata\t= &dtv_spl_test2,
226 \t.platdata_size\t= sizeof(dtv_spl_test2),
227 };
228
229 static struct dtd_sandbox_spl_test dtv_spl_test3 = {
230 \t.stringarray\t\t= {"one", "", ""},
231 };
232 U_BOOT_DEVICE(spl_test3) = {
233 \t.name\t\t= "sandbox_spl_test",
234 \t.platdata\t= &dtv_spl_test3,
235 \t.platdata_size\t= sizeof(dtv_spl_test3),
236 };
237
238 static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
239 };
240 U_BOOT_DEVICE(spl_test4) = {
241 \t.name\t\t= "sandbox_spl_test_2",
242 \t.platdata\t= &dtv_spl_test4,
243 \t.platdata_size\t= sizeof(dtv_spl_test4),
244 };
245
246 static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
247 };
248 U_BOOT_DEVICE(i2c_at_0) = {
249 \t.name\t\t= "sandbox_i2c_test",
250 \t.platdata\t= &dtv_i2c_at_0,
251 \t.platdata_size\t= sizeof(dtv_i2c_at_0),
252 };
253
254 static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
255 \t.low_power\t\t= true,
256 \t.reg\t\t\t= {0x9, 0x0},
257 };
258 U_BOOT_DEVICE(pmic_at_9) = {
259 \t.name\t\t= "sandbox_pmic_test",
260 \t.platdata\t= &dtv_pmic_at_9,
261 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
262 };
263
264 ''', data)
265
266     def test_phandle(self):
267         """Test output from a node containing a phandle reference"""
268         dtb_file = get_dtb_file('dtoc_test_phandle.dts')
269         output = tools.GetOutputFilename('output')
270         dtb_platdata.run_steps(['struct'], dtb_file, False, output)
271         with open(output) as infile:
272             data = infile.read()
273         self._CheckStrings(HEADER + '''
274 struct dtd_source {
275 \tstruct phandle_2_arg clocks[4];
276 };
277 struct dtd_target {
278 \tfdt32_t\t\tintval;
279 };
280 ''', data)
281
282         dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
283         with open(output) as infile:
284             data = infile.read()
285         self._CheckStrings(C_HEADER + '''
286 static struct dtd_target dtv_phandle_target = {
287 \t.intval\t\t\t= 0x0,
288 };
289 U_BOOT_DEVICE(phandle_target) = {
290 \t.name\t\t= "target",
291 \t.platdata\t= &dtv_phandle_target,
292 \t.platdata_size\t= sizeof(dtv_phandle_target),
293 };
294
295 static struct dtd_target dtv_phandle2_target = {
296 \t.intval\t\t\t= 0x1,
297 };
298 U_BOOT_DEVICE(phandle2_target) = {
299 \t.name\t\t= "target",
300 \t.platdata\t= &dtv_phandle2_target,
301 \t.platdata_size\t= sizeof(dtv_phandle2_target),
302 };
303
304 static struct dtd_target dtv_phandle3_target = {
305 \t.intval\t\t\t= 0x2,
306 };
307 U_BOOT_DEVICE(phandle3_target) = {
308 \t.name\t\t= "target",
309 \t.platdata\t= &dtv_phandle3_target,
310 \t.platdata_size\t= sizeof(dtv_phandle3_target),
311 };
312
313 static struct dtd_source dtv_phandle_source = {
314 \t.clocks\t\t\t= {
315 \t\t\t{&dtv_phandle_target, {}},
316 \t\t\t{&dtv_phandle2_target, {11}},
317 \t\t\t{&dtv_phandle3_target, {12, 13}},
318 \t\t\t{&dtv_phandle_target, {}},},
319 };
320 U_BOOT_DEVICE(phandle_source) = {
321 \t.name\t\t= "source",
322 \t.platdata\t= &dtv_phandle_source,
323 \t.platdata_size\t= sizeof(dtv_phandle_source),
324 };
325
326 ''', data)
327
328     def test_aliases(self):
329         """Test output from a node with multiple compatible strings"""
330         dtb_file = get_dtb_file('dtoc_test_aliases.dts')
331         output = tools.GetOutputFilename('output')
332         dtb_platdata.run_steps(['struct'], dtb_file, False, output)
333         with open(output) as infile:
334             data = infile.read()
335         self._CheckStrings(HEADER + '''
336 struct dtd_compat1 {
337 \tfdt32_t\t\tintval;
338 };
339 #define dtd_compat2_1_fred dtd_compat1
340 #define dtd_compat3 dtd_compat1
341 ''', data)
342
343         dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
344         with open(output) as infile:
345             data = infile.read()
346         self._CheckStrings(C_HEADER + '''
347 static struct dtd_compat1 dtv_spl_test = {
348 \t.intval\t\t\t= 0x1,
349 };
350 U_BOOT_DEVICE(spl_test) = {
351 \t.name\t\t= "compat1",
352 \t.platdata\t= &dtv_spl_test,
353 \t.platdata_size\t= sizeof(dtv_spl_test),
354 };
355
356 ''', data)
357
358     def test_addresses64(self):
359         """Test output from a node with a 'reg' property with na=2, ns=2"""
360         dtb_file = get_dtb_file('dtoc_test_addr64.dts')
361         output = tools.GetOutputFilename('output')
362         dtb_platdata.run_steps(['struct'], dtb_file, False, output)
363         with open(output) as infile:
364             data = infile.read()
365         self._CheckStrings(HEADER + '''
366 struct dtd_test1 {
367 \tfdt64_t\t\treg[2];
368 };
369 struct dtd_test2 {
370 \tfdt64_t\t\treg[2];
371 };
372 struct dtd_test3 {
373 \tfdt64_t\t\treg[4];
374 };
375 ''', data)
376
377         dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
378         with open(output) as infile:
379             data = infile.read()
380         self._CheckStrings(C_HEADER + '''
381 static struct dtd_test1 dtv_test1 = {
382 \t.reg\t\t\t= {0x1234, 0x5678},
383 };
384 U_BOOT_DEVICE(test1) = {
385 \t.name\t\t= "test1",
386 \t.platdata\t= &dtv_test1,
387 \t.platdata_size\t= sizeof(dtv_test1),
388 };
389
390 static struct dtd_test2 dtv_test2 = {
391 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
392 };
393 U_BOOT_DEVICE(test2) = {
394 \t.name\t\t= "test2",
395 \t.platdata\t= &dtv_test2,
396 \t.platdata_size\t= sizeof(dtv_test2),
397 };
398
399 static struct dtd_test3 dtv_test3 = {
400 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
401 };
402 U_BOOT_DEVICE(test3) = {
403 \t.name\t\t= "test3",
404 \t.platdata\t= &dtv_test3,
405 \t.platdata_size\t= sizeof(dtv_test3),
406 };
407
408 ''', data)
409
410     def test_addresses32(self):
411         """Test output from a node with a 'reg' property with na=1, ns=1"""
412         dtb_file = get_dtb_file('dtoc_test_addr32.dts')
413         output = tools.GetOutputFilename('output')
414         dtb_platdata.run_steps(['struct'], dtb_file, False, output)
415         with open(output) as infile:
416             data = infile.read()
417         self._CheckStrings(HEADER + '''
418 struct dtd_test1 {
419 \tfdt32_t\t\treg[2];
420 };
421 struct dtd_test2 {
422 \tfdt32_t\t\treg[4];
423 };
424 ''', data)
425
426         dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
427         with open(output) as infile:
428             data = infile.read()
429         self._CheckStrings(C_HEADER + '''
430 static struct dtd_test1 dtv_test1 = {
431 \t.reg\t\t\t= {0x1234, 0x5678},
432 };
433 U_BOOT_DEVICE(test1) = {
434 \t.name\t\t= "test1",
435 \t.platdata\t= &dtv_test1,
436 \t.platdata_size\t= sizeof(dtv_test1),
437 };
438
439 static struct dtd_test2 dtv_test2 = {
440 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
441 };
442 U_BOOT_DEVICE(test2) = {
443 \t.name\t\t= "test2",
444 \t.platdata\t= &dtv_test2,
445 \t.platdata_size\t= sizeof(dtv_test2),
446 };
447
448 ''', data)
449
450     def test_addresses64_32(self):
451         """Test output from a node with a 'reg' property with na=2, ns=1"""
452         dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
453         output = tools.GetOutputFilename('output')
454         dtb_platdata.run_steps(['struct'], dtb_file, False, output)
455         with open(output) as infile:
456             data = infile.read()
457         self._CheckStrings(HEADER + '''
458 struct dtd_test1 {
459 \tfdt64_t\t\treg[2];
460 };
461 struct dtd_test2 {
462 \tfdt64_t\t\treg[2];
463 };
464 struct dtd_test3 {
465 \tfdt64_t\t\treg[4];
466 };
467 ''', data)
468
469         dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
470         with open(output) as infile:
471             data = infile.read()
472         self._CheckStrings(C_HEADER + '''
473 static struct dtd_test1 dtv_test1 = {
474 \t.reg\t\t\t= {0x123400000000, 0x5678},
475 };
476 U_BOOT_DEVICE(test1) = {
477 \t.name\t\t= "test1",
478 \t.platdata\t= &dtv_test1,
479 \t.platdata_size\t= sizeof(dtv_test1),
480 };
481
482 static struct dtd_test2 dtv_test2 = {
483 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
484 };
485 U_BOOT_DEVICE(test2) = {
486 \t.name\t\t= "test2",
487 \t.platdata\t= &dtv_test2,
488 \t.platdata_size\t= sizeof(dtv_test2),
489 };
490
491 static struct dtd_test3 dtv_test3 = {
492 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
493 };
494 U_BOOT_DEVICE(test3) = {
495 \t.name\t\t= "test3",
496 \t.platdata\t= &dtv_test3,
497 \t.platdata_size\t= sizeof(dtv_test3),
498 };
499
500 ''', data)
501
502     def test_addresses32_64(self):
503         """Test output from a node with a 'reg' property with na=1, ns=2"""
504         dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
505         output = tools.GetOutputFilename('output')
506         dtb_platdata.run_steps(['struct'], dtb_file, False, output)
507         with open(output) as infile:
508             data = infile.read()
509         self._CheckStrings(HEADER + '''
510 struct dtd_test1 {
511 \tfdt64_t\t\treg[2];
512 };
513 struct dtd_test2 {
514 \tfdt64_t\t\treg[2];
515 };
516 struct dtd_test3 {
517 \tfdt64_t\t\treg[4];
518 };
519 ''', data)
520
521         dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
522         with open(output) as infile:
523             data = infile.read()
524         self._CheckStrings(C_HEADER + '''
525 static struct dtd_test1 dtv_test1 = {
526 \t.reg\t\t\t= {0x1234, 0x567800000000},
527 };
528 U_BOOT_DEVICE(test1) = {
529 \t.name\t\t= "test1",
530 \t.platdata\t= &dtv_test1,
531 \t.platdata_size\t= sizeof(dtv_test1),
532 };
533
534 static struct dtd_test2 dtv_test2 = {
535 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
536 };
537 U_BOOT_DEVICE(test2) = {
538 \t.name\t\t= "test2",
539 \t.platdata\t= &dtv_test2,
540 \t.platdata_size\t= sizeof(dtv_test2),
541 };
542
543 static struct dtd_test3 dtv_test3 = {
544 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
545 };
546 U_BOOT_DEVICE(test3) = {
547 \t.name\t\t= "test3",
548 \t.platdata\t= &dtv_test3,
549 \t.platdata_size\t= sizeof(dtv_test3),
550 };
551
552 ''', data)