]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - tools/dtoc/test_dtoc.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2012 The Chromium OS Authors.
5 """Tests for the dtb_platdata module
7 This includes unit tests for some functions and functional tests for
16 from dtb_platdata
import conv_name_to_c
17 from dtb_platdata
import get_compat_name
18 from dtb_platdata
import get_value
19 from dtb_platdata
import tab_to
24 our_path
= os
.path
.dirname(os
.path
.realpath(__file__
))
30 * This file was generated by dtoc from a .dtb (device tree binary) file.
34 #include <linux/libfdt.h>'''
39 * This file was generated by dtoc from a .dtb (device tree binary) file.
44 #include <dt-structs.h>
48 def get_dtb_file(dts_fname
):
49 """Compile a .dts file to a .dtb
52 dts_fname: Filename of .dts file in the current directory
55 Filename of compiled file in output directory
57 return fdt_util
.EnsureCompiled(os
.path
.join(our_path
, dts_fname
))
60 class TestDtoc(unittest
.TestCase
):
64 tools
.PrepareOutputDir(None)
67 def tearDownClass(cls
):
68 tools
._RemoveOutputDir
()
71 """Test conversion of device tree names to C identifiers"""
72 self
.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
73 self
.assertEqual('vendor_clock_frequency',
74 conv_name_to_c('vendor,clock-frequency'))
75 self
.assertEqual('rockchip_rk3399_sdhci_5_1',
76 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
78 def test_tab_to(self
):
79 """Test operation of tab_to() function"""
80 self
.assertEqual('fred ', tab_to(0, 'fred'))
81 self
.assertEqual('fred\t', tab_to(1, 'fred'))
82 self
.assertEqual('fred was here ', tab_to(1, 'fred was here'))
83 self
.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
84 self
.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
85 self
.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
87 def test_get_value(self
):
88 """Test operation of get_value() function"""
89 self
.assertEqual('0x45',
90 get_value(fdt
.TYPE_INT
, struct
.pack('>I', 0x45)))
91 self
.assertEqual('0x45',
92 get_value(fdt
.TYPE_BYTE
, struct
.pack('<I', 0x45)))
93 self
.assertEqual('0x0',
94 get_value(fdt
.TYPE_BYTE
, struct
.pack('>I', 0x45)))
95 self
.assertEqual('"test"', get_value(fdt
.TYPE_STRING
, 'test'))
96 self
.assertEqual('true', get_value(fdt
.TYPE_BOOL
, None))
98 def test_get_compat_name(self
):
99 """Test operation of get_compat_name() function"""
100 Prop
= collections
.namedtuple('Prop', ['value'])
101 Node
= collections
.namedtuple('Node', ['props'])
103 prop
= Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
104 node
= Node({'compatible': prop
})
105 self
.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
106 get_compat_name(node
))
108 prop
= Prop(['rockchip,rk3399-sdhci-5.1'])
109 node
= Node({'compatible': prop
})
110 self
.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
111 get_compat_name(node
))
113 prop
= Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
114 node
= Node({'compatible': prop
})
115 self
.assertEqual(('rockchip_rk3399_sdhci_5_1',
116 ['arasan_sdhci_5_1', 'third']),
117 get_compat_name(node
))
119 def test_empty_file(self
):
120 """Test output from a device tree file with no nodes"""
121 dtb_file
= get_dtb_file('dtoc_test_empty.dts')
122 output
= tools
.GetOutputFilename('output')
123 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
124 with
open(output
) as infile
:
125 lines
= infile
.read().splitlines()
126 self
.assertEqual(HEADER
.splitlines(), lines
)
128 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
129 with
open(output
) as infile
:
130 lines
= infile
.read().splitlines()
131 self
.assertEqual(C_HEADER
.splitlines() + [''], lines
)
133 def test_simple(self
):
134 """Test output from some simple nodes with various types of data"""
135 dtb_file
= get_dtb_file('dtoc_test_simple.dts')
136 output
= tools
.GetOutputFilename('output')
137 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
138 with
open(output
) as infile
:
140 self
.assertEqual(HEADER
+ '''
141 struct dtd_sandbox_i2c_test {
143 struct dtd_sandbox_pmic_test {
147 struct dtd_sandbox_spl_test {
149 \tunsigned char\tbytearray[3];
150 \tunsigned char\tbyteval;
151 \tfdt32_t\t\tintarray[4];
153 \tunsigned char\tlongbytearray[9];
154 \tconst char *\tstringarray[3];
155 \tconst char *\tstringval;
157 struct dtd_sandbox_spl_test_2 {
161 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
162 with
open(output
) as infile
:
164 self
.assertEqual(C_HEADER
+ '''
165 static struct dtd_sandbox_spl_test dtv_spl_test = {
166 \t.bytearray\t\t= {0x6, 0x0, 0x0},
168 \t.intval\t\t\t= 0x1,
169 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
171 \t.stringval\t\t= "message",
172 \t.boolval\t\t= true,
173 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
174 \t.stringarray\t\t= {"multi-word", "message", ""},
176 U_BOOT_DEVICE(spl_test) = {
177 \t.name\t\t= "sandbox_spl_test",
178 \t.platdata\t= &dtv_spl_test,
179 \t.platdata_size\t= sizeof(dtv_spl_test),
182 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
183 \t.bytearray\t\t= {0x1, 0x23, 0x34},
185 \t.intval\t\t\t= 0x3,
186 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
188 \t.stringval\t\t= "message2",
189 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
190 \t.stringarray\t\t= {"another", "multi-word", "message"},
192 U_BOOT_DEVICE(spl_test2) = {
193 \t.name\t\t= "sandbox_spl_test",
194 \t.platdata\t= &dtv_spl_test2,
195 \t.platdata_size\t= sizeof(dtv_spl_test2),
198 static struct dtd_sandbox_spl_test dtv_spl_test3 = {
199 \t.stringarray\t\t= {"one", "", ""},
201 U_BOOT_DEVICE(spl_test3) = {
202 \t.name\t\t= "sandbox_spl_test",
203 \t.platdata\t= &dtv_spl_test3,
204 \t.platdata_size\t= sizeof(dtv_spl_test3),
207 static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
209 U_BOOT_DEVICE(spl_test4) = {
210 \t.name\t\t= "sandbox_spl_test_2",
211 \t.platdata\t= &dtv_spl_test4,
212 \t.platdata_size\t= sizeof(dtv_spl_test4),
215 static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
217 U_BOOT_DEVICE(i2c_at_0) = {
218 \t.name\t\t= "sandbox_i2c_test",
219 \t.platdata\t= &dtv_i2c_at_0,
220 \t.platdata_size\t= sizeof(dtv_i2c_at_0),
223 static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
224 \t.low_power\t\t= true,
225 \t.reg\t\t\t= {0x9, 0x0},
227 U_BOOT_DEVICE(pmic_at_9) = {
228 \t.name\t\t= "sandbox_pmic_test",
229 \t.platdata\t= &dtv_pmic_at_9,
230 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
235 def test_phandle(self
):
236 """Test output from a node containing a phandle reference"""
237 dtb_file
= get_dtb_file('dtoc_test_phandle.dts')
238 output
= tools
.GetOutputFilename('output')
239 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
240 with
open(output
) as infile
:
242 self
.assertEqual(HEADER
+ '''
244 \tstruct phandle_2_arg clocks[4];
251 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
252 with
open(output
) as infile
:
254 self
.assertEqual(C_HEADER
+ '''
255 static struct dtd_target dtv_phandle_target = {
256 \t.intval\t\t\t= 0x0,
258 U_BOOT_DEVICE(phandle_target) = {
259 \t.name\t\t= "target",
260 \t.platdata\t= &dtv_phandle_target,
261 \t.platdata_size\t= sizeof(dtv_phandle_target),
264 static struct dtd_target dtv_phandle2_target = {
265 \t.intval\t\t\t= 0x1,
267 U_BOOT_DEVICE(phandle2_target) = {
268 \t.name\t\t= "target",
269 \t.platdata\t= &dtv_phandle2_target,
270 \t.platdata_size\t= sizeof(dtv_phandle2_target),
273 static struct dtd_target dtv_phandle3_target = {
274 \t.intval\t\t\t= 0x2,
276 U_BOOT_DEVICE(phandle3_target) = {
277 \t.name\t\t= "target",
278 \t.platdata\t= &dtv_phandle3_target,
279 \t.platdata_size\t= sizeof(dtv_phandle3_target),
282 static struct dtd_source dtv_phandle_source = {
284 \t\t\t{&dtv_phandle_target, {}},
285 \t\t\t{&dtv_phandle2_target, {11}},
286 \t\t\t{&dtv_phandle3_target, {12, 13}},
287 \t\t\t{&dtv_phandle_target, {}},},
289 U_BOOT_DEVICE(phandle_source) = {
290 \t.name\t\t= "source",
291 \t.platdata\t= &dtv_phandle_source,
292 \t.platdata_size\t= sizeof(dtv_phandle_source),
297 def test_aliases(self
):
298 """Test output from a node with multiple compatible strings"""
299 dtb_file
= get_dtb_file('dtoc_test_aliases.dts')
300 output
= tools
.GetOutputFilename('output')
301 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
302 with
open(output
) as infile
:
304 self
.assertEqual(HEADER
+ '''
308 #define dtd_compat2_1_fred dtd_compat1
309 #define dtd_compat3 dtd_compat1
312 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
313 with
open(output
) as infile
:
315 self
.assertEqual(C_HEADER
+ '''
316 static struct dtd_compat1 dtv_spl_test = {
317 \t.intval\t\t\t= 0x1,
319 U_BOOT_DEVICE(spl_test) = {
320 \t.name\t\t= "compat1",
321 \t.platdata\t= &dtv_spl_test,
322 \t.platdata_size\t= sizeof(dtv_spl_test),
327 def test_addresses64(self
):
328 """Test output from a node with a 'reg' property with na=2, ns=2"""
329 dtb_file
= get_dtb_file('dtoc_test_addr64.dts')
330 output
= tools
.GetOutputFilename('output')
331 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
332 with
open(output
) as infile
:
334 self
.assertEqual(HEADER
+ '''
346 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
347 with
open(output
) as infile
:
349 self
.assertEqual(C_HEADER
+ '''
350 static struct dtd_test1 dtv_test1 = {
351 \t.reg\t\t\t= {0x1234, 0x5678},
353 U_BOOT_DEVICE(test1) = {
354 \t.name\t\t= "test1",
355 \t.platdata\t= &dtv_test1,
356 \t.platdata_size\t= sizeof(dtv_test1),
359 static struct dtd_test2 dtv_test2 = {
360 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
362 U_BOOT_DEVICE(test2) = {
363 \t.name\t\t= "test2",
364 \t.platdata\t= &dtv_test2,
365 \t.platdata_size\t= sizeof(dtv_test2),
368 static struct dtd_test3 dtv_test3 = {
369 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
371 U_BOOT_DEVICE(test3) = {
372 \t.name\t\t= "test3",
373 \t.platdata\t= &dtv_test3,
374 \t.platdata_size\t= sizeof(dtv_test3),
379 def test_addresses32(self
):
380 """Test output from a node with a 'reg' property with na=1, ns=1"""
381 dtb_file
= get_dtb_file('dtoc_test_addr32.dts')
382 output
= tools
.GetOutputFilename('output')
383 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
384 with
open(output
) as infile
:
386 self
.assertEqual(HEADER
+ '''
395 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
396 with
open(output
) as infile
:
398 self
.assertEqual(C_HEADER
+ '''
399 static struct dtd_test1 dtv_test1 = {
400 \t.reg\t\t\t= {0x1234, 0x5678},
402 U_BOOT_DEVICE(test1) = {
403 \t.name\t\t= "test1",
404 \t.platdata\t= &dtv_test1,
405 \t.platdata_size\t= sizeof(dtv_test1),
408 static struct dtd_test2 dtv_test2 = {
409 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
411 U_BOOT_DEVICE(test2) = {
412 \t.name\t\t= "test2",
413 \t.platdata\t= &dtv_test2,
414 \t.platdata_size\t= sizeof(dtv_test2),
419 def test_addresses64_32(self
):
420 """Test output from a node with a 'reg' property with na=2, ns=1"""
421 dtb_file
= get_dtb_file('dtoc_test_addr64_32.dts')
422 output
= tools
.GetOutputFilename('output')
423 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
424 with
open(output
) as infile
:
426 self
.assertEqual(HEADER
+ '''
438 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
439 with
open(output
) as infile
:
441 self
.assertEqual(C_HEADER
+ '''
442 static struct dtd_test1 dtv_test1 = {
443 \t.reg\t\t\t= {0x123400000000, 0x5678},
445 U_BOOT_DEVICE(test1) = {
446 \t.name\t\t= "test1",
447 \t.platdata\t= &dtv_test1,
448 \t.platdata_size\t= sizeof(dtv_test1),
451 static struct dtd_test2 dtv_test2 = {
452 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
454 U_BOOT_DEVICE(test2) = {
455 \t.name\t\t= "test2",
456 \t.platdata\t= &dtv_test2,
457 \t.platdata_size\t= sizeof(dtv_test2),
460 static struct dtd_test3 dtv_test3 = {
461 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
463 U_BOOT_DEVICE(test3) = {
464 \t.name\t\t= "test3",
465 \t.platdata\t= &dtv_test3,
466 \t.platdata_size\t= sizeof(dtv_test3),
471 def test_addresses32_64(self
):
472 """Test output from a node with a 'reg' property with na=1, ns=2"""
473 dtb_file
= get_dtb_file('dtoc_test_addr32_64.dts')
474 output
= tools
.GetOutputFilename('output')
475 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
476 with
open(output
) as infile
:
478 self
.assertEqual(HEADER
+ '''
490 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
491 with
open(output
) as infile
:
493 self
.assertEqual(C_HEADER
+ '''
494 static struct dtd_test1 dtv_test1 = {
495 \t.reg\t\t\t= {0x1234, 0x567800000000},
497 U_BOOT_DEVICE(test1) = {
498 \t.name\t\t= "test1",
499 \t.platdata\t= &dtv_test1,
500 \t.platdata_size\t= sizeof(dtv_test1),
503 static struct dtd_test2 dtv_test2 = {
504 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
506 U_BOOT_DEVICE(test2) = {
507 \t.name\t\t= "test2",
508 \t.platdata\t= &dtv_test2,
509 \t.platdata_size\t= sizeof(dtv_test2),
512 static struct dtd_test3 dtv_test3 = {
513 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
515 U_BOOT_DEVICE(test3) = {
516 \t.name\t\t= "test3",
517 \t.platdata\t= &dtv_test3,
518 \t.platdata_size\t= sizeof(dtv_test3),