]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/dtoc/test_dtoc.py
2 # Copyright (c) 2012 The Chromium OS Authors.
4 # SPDX-License-Identifier: GPL-2.0+
7 """Tests for the dtb_platdata module
9 This includes unit tests for some functions and functional tests for
18 from dtb_platdata
import conv_name_to_c
19 from dtb_platdata
import get_compat_name
20 from dtb_platdata
import get_value
21 from dtb_platdata
import tab_to
26 our_path
= os
.path
.dirname(os
.path
.realpath(__file__
))
29 def get_dtb_file(dts_fname
):
30 """Compile a .dts file to a .dtb
33 dts_fname: Filename of .dts file in the current directory
36 Filename of compiled file in output directory
38 return fdt_util
.EnsureCompiled(os
.path
.join(our_path
, dts_fname
))
41 class TestDtoc(unittest
.TestCase
):
45 tools
.PrepareOutputDir(None)
48 def tearDownClass(cls
):
49 tools
._RemoveOutputDir
()
52 """Test conversion of device tree names to C identifiers"""
53 self
.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
54 self
.assertEqual('vendor_clock_frequency',
55 conv_name_to_c('vendor,clock-frequency'))
56 self
.assertEqual('rockchip_rk3399_sdhci_5_1',
57 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
59 def test_tab_to(self
):
60 """Test operation of tab_to() function"""
61 self
.assertEqual('fred ', tab_to(0, 'fred'))
62 self
.assertEqual('fred\t', tab_to(1, 'fred'))
63 self
.assertEqual('fred was here ', tab_to(1, 'fred was here'))
64 self
.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
65 self
.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
66 self
.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
68 def test_get_value(self
):
69 """Test operation of get_value() function"""
70 self
.assertEqual('0x45',
71 get_value(fdt
.TYPE_INT
, struct
.pack('>I', 0x45)))
72 self
.assertEqual('0x45',
73 get_value(fdt
.TYPE_BYTE
, struct
.pack('<I', 0x45)))
74 self
.assertEqual('0x0',
75 get_value(fdt
.TYPE_BYTE
, struct
.pack('>I', 0x45)))
76 self
.assertEqual('"test"', get_value(fdt
.TYPE_STRING
, 'test'))
77 self
.assertEqual('true', get_value(fdt
.TYPE_BOOL
, None))
79 def test_get_compat_name(self
):
80 """Test operation of get_compat_name() function"""
81 Prop
= collections
.namedtuple('Prop', ['value'])
82 Node
= collections
.namedtuple('Node', ['props'])
84 prop
= Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
85 node
= Node({'compatible': prop
})
86 self
.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
87 get_compat_name(node
))
89 prop
= Prop(['rockchip,rk3399-sdhci-5.1'])
90 node
= Node({'compatible': prop
})
91 self
.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
92 get_compat_name(node
))
94 prop
= Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
95 node
= Node({'compatible': prop
})
96 self
.assertEqual(('rockchip_rk3399_sdhci_5_1',
97 ['arasan_sdhci_5_1', 'third']),
98 get_compat_name(node
))
100 def test_empty_file(self
):
101 """Test output from a device tree file with no nodes"""
102 dtb_file
= get_dtb_file('dtoc_test_empty.dts')
103 output
= tools
.GetOutputFilename('output')
104 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
105 with
open(output
) as infile
:
106 lines
= infile
.read().splitlines()
107 self
.assertEqual(['#include <stdbool.h>', '#include <libfdt.h>'], lines
)
109 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
110 with
open(output
) as infile
:
111 lines
= infile
.read().splitlines()
112 self
.assertEqual(['#include <common.h>', '#include <dm.h>',
113 '#include <dt-structs.h>', ''], lines
)
115 def test_simple(self
):
116 """Test output from some simple nodes with various types of data"""
117 dtb_file
= get_dtb_file('dtoc_test_simple.dts')
118 output
= tools
.GetOutputFilename('output')
119 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
120 with
open(output
) as infile
:
122 self
.assertEqual('''#include <stdbool.h>
124 struct dtd_sandbox_i2c_test {
126 struct dtd_sandbox_pmic_test {
130 struct dtd_sandbox_spl_test {
132 \tunsigned char\tbytearray[3];
133 \tunsigned char\tbyteval;
134 \tfdt32_t\t\tintarray[4];
136 \tunsigned char\tlongbytearray[9];
137 \tconst char *\tstringarray[3];
138 \tconst char *\tstringval;
140 struct dtd_sandbox_spl_test_2 {
144 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
145 with
open(output
) as infile
:
147 self
.assertEqual('''#include <common.h>
149 #include <dt-structs.h>
151 static struct dtd_sandbox_spl_test dtv_spl_test = {
152 \t.bytearray\t\t= {0x6, 0x0, 0x0},
154 \t.intval\t\t\t= 0x1,
155 \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
157 \t.stringval\t\t= "message",
158 \t.boolval\t\t= true,
159 \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
160 \t.stringarray\t\t= {"multi-word", "message", ""},
162 U_BOOT_DEVICE(spl_test) = {
163 \t.name\t\t= "sandbox_spl_test",
164 \t.platdata\t= &dtv_spl_test,
165 \t.platdata_size\t= sizeof(dtv_spl_test),
168 static struct dtd_sandbox_spl_test dtv_spl_test2 = {
169 \t.bytearray\t\t= {0x1, 0x23, 0x34},
171 \t.intval\t\t\t= 0x3,
172 \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
174 \t.stringval\t\t= "message2",
175 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
176 \t.stringarray\t\t= {"another", "multi-word", "message"},
178 U_BOOT_DEVICE(spl_test2) = {
179 \t.name\t\t= "sandbox_spl_test",
180 \t.platdata\t= &dtv_spl_test2,
181 \t.platdata_size\t= sizeof(dtv_spl_test2),
184 static struct dtd_sandbox_spl_test dtv_spl_test3 = {
185 \t.stringarray\t\t= {"one", "", ""},
187 U_BOOT_DEVICE(spl_test3) = {
188 \t.name\t\t= "sandbox_spl_test",
189 \t.platdata\t= &dtv_spl_test3,
190 \t.platdata_size\t= sizeof(dtv_spl_test3),
193 static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
195 U_BOOT_DEVICE(spl_test4) = {
196 \t.name\t\t= "sandbox_spl_test_2",
197 \t.platdata\t= &dtv_spl_test4,
198 \t.platdata_size\t= sizeof(dtv_spl_test4),
201 static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
203 U_BOOT_DEVICE(i2c_at_0) = {
204 \t.name\t\t= "sandbox_i2c_test",
205 \t.platdata\t= &dtv_i2c_at_0,
206 \t.platdata_size\t= sizeof(dtv_i2c_at_0),
209 static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
210 \t.low_power\t\t= true,
211 \t.reg\t\t\t= {0x9, 0x0},
213 U_BOOT_DEVICE(pmic_at_9) = {
214 \t.name\t\t= "sandbox_pmic_test",
215 \t.platdata\t= &dtv_pmic_at_9,
216 \t.platdata_size\t= sizeof(dtv_pmic_at_9),
221 def test_phandle(self
):
222 """Test output from a node containing a phandle reference"""
223 dtb_file
= get_dtb_file('dtoc_test_phandle.dts')
224 output
= tools
.GetOutputFilename('output')
225 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
226 with
open(output
) as infile
:
228 self
.assertEqual('''#include <stdbool.h>
231 \tstruct phandle_2_arg clocks[4];
238 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
239 with
open(output
) as infile
:
241 self
.assertEqual('''#include <common.h>
243 #include <dt-structs.h>
245 static struct dtd_target dtv_phandle_target = {
246 \t.intval\t\t\t= 0x0,
248 U_BOOT_DEVICE(phandle_target) = {
249 \t.name\t\t= "target",
250 \t.platdata\t= &dtv_phandle_target,
251 \t.platdata_size\t= sizeof(dtv_phandle_target),
254 static struct dtd_target dtv_phandle2_target = {
255 \t.intval\t\t\t= 0x1,
257 U_BOOT_DEVICE(phandle2_target) = {
258 \t.name\t\t= "target",
259 \t.platdata\t= &dtv_phandle2_target,
260 \t.platdata_size\t= sizeof(dtv_phandle2_target),
263 static struct dtd_target dtv_phandle3_target = {
264 \t.intval\t\t\t= 0x2,
266 U_BOOT_DEVICE(phandle3_target) = {
267 \t.name\t\t= "target",
268 \t.platdata\t= &dtv_phandle3_target,
269 \t.platdata_size\t= sizeof(dtv_phandle3_target),
272 static struct dtd_source dtv_phandle_source = {
274 \t\t\t{&dtv_phandle_target, {}},
275 \t\t\t{&dtv_phandle2_target, {11}},
276 \t\t\t{&dtv_phandle3_target, {12, 13}},
277 \t\t\t{&dtv_phandle_target, {}},},
279 U_BOOT_DEVICE(phandle_source) = {
280 \t.name\t\t= "source",
281 \t.platdata\t= &dtv_phandle_source,
282 \t.platdata_size\t= sizeof(dtv_phandle_source),
287 def test_aliases(self
):
288 """Test output from a node with multiple compatible strings"""
289 dtb_file
= get_dtb_file('dtoc_test_aliases.dts')
290 output
= tools
.GetOutputFilename('output')
291 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
292 with
open(output
) as infile
:
294 self
.assertEqual('''#include <stdbool.h>
299 #define dtd_compat2_1_fred dtd_compat1
300 #define dtd_compat3 dtd_compat1
303 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
304 with
open(output
) as infile
:
306 self
.assertEqual('''#include <common.h>
308 #include <dt-structs.h>
310 static struct dtd_compat1 dtv_spl_test = {
311 \t.intval\t\t\t= 0x1,
313 U_BOOT_DEVICE(spl_test) = {
314 \t.name\t\t= "compat1",
315 \t.platdata\t= &dtv_spl_test,
316 \t.platdata_size\t= sizeof(dtv_spl_test),
321 def test_addresses64(self
):
322 """Test output from a node with a 'reg' property with na=2, ns=2"""
323 dtb_file
= get_dtb_file('dtoc_test_addr64.dts')
324 output
= tools
.GetOutputFilename('output')
325 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
326 with
open(output
) as infile
:
328 self
.assertEqual('''#include <stdbool.h>
341 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
342 with
open(output
) as infile
:
344 self
.assertEqual('''#include <common.h>
346 #include <dt-structs.h>
348 static struct dtd_test1 dtv_test1 = {
349 \t.reg\t\t\t= {0x1234, 0x5678},
351 U_BOOT_DEVICE(test1) = {
352 \t.name\t\t= "test1",
353 \t.platdata\t= &dtv_test1,
354 \t.platdata_size\t= sizeof(dtv_test1),
357 static struct dtd_test2 dtv_test2 = {
358 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
360 U_BOOT_DEVICE(test2) = {
361 \t.name\t\t= "test2",
362 \t.platdata\t= &dtv_test2,
363 \t.platdata_size\t= sizeof(dtv_test2),
366 static struct dtd_test3 dtv_test3 = {
367 \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
369 U_BOOT_DEVICE(test3) = {
370 \t.name\t\t= "test3",
371 \t.platdata\t= &dtv_test3,
372 \t.platdata_size\t= sizeof(dtv_test3),
377 def test_addresses32(self
):
378 """Test output from a node with a 'reg' property with na=1, ns=1"""
379 dtb_file
= get_dtb_file('dtoc_test_addr32.dts')
380 output
= tools
.GetOutputFilename('output')
381 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
382 with
open(output
) as infile
:
384 self
.assertEqual('''#include <stdbool.h>
394 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
395 with
open(output
) as infile
:
397 self
.assertEqual('''#include <common.h>
399 #include <dt-structs.h>
401 static struct dtd_test1 dtv_test1 = {
402 \t.reg\t\t\t= {0x1234, 0x5678},
404 U_BOOT_DEVICE(test1) = {
405 \t.name\t\t= "test1",
406 \t.platdata\t= &dtv_test1,
407 \t.platdata_size\t= sizeof(dtv_test1),
410 static struct dtd_test2 dtv_test2 = {
411 \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
413 U_BOOT_DEVICE(test2) = {
414 \t.name\t\t= "test2",
415 \t.platdata\t= &dtv_test2,
416 \t.platdata_size\t= sizeof(dtv_test2),
421 def test_addresses64_32(self
):
422 """Test output from a node with a 'reg' property with na=2, ns=1"""
423 dtb_file
= get_dtb_file('dtoc_test_addr64_32.dts')
424 output
= tools
.GetOutputFilename('output')
425 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
426 with
open(output
) as infile
:
428 self
.assertEqual('''#include <stdbool.h>
441 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
442 with
open(output
) as infile
:
444 self
.assertEqual('''#include <common.h>
446 #include <dt-structs.h>
448 static struct dtd_test1 dtv_test1 = {
449 \t.reg\t\t\t= {0x123400000000, 0x5678},
451 U_BOOT_DEVICE(test1) = {
452 \t.name\t\t= "test1",
453 \t.platdata\t= &dtv_test1,
454 \t.platdata_size\t= sizeof(dtv_test1),
457 static struct dtd_test2 dtv_test2 = {
458 \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
460 U_BOOT_DEVICE(test2) = {
461 \t.name\t\t= "test2",
462 \t.platdata\t= &dtv_test2,
463 \t.platdata_size\t= sizeof(dtv_test2),
466 static struct dtd_test3 dtv_test3 = {
467 \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
469 U_BOOT_DEVICE(test3) = {
470 \t.name\t\t= "test3",
471 \t.platdata\t= &dtv_test3,
472 \t.platdata_size\t= sizeof(dtv_test3),
477 def test_addresses32_64(self
):
478 """Test output from a node with a 'reg' property with na=1, ns=2"""
479 dtb_file
= get_dtb_file('dtoc_test_addr32_64.dts')
480 output
= tools
.GetOutputFilename('output')
481 dtb_platdata
.run_steps(['struct'], dtb_file
, False, output
)
482 with
open(output
) as infile
:
484 self
.assertEqual('''#include <stdbool.h>
497 dtb_platdata
.run_steps(['platdata'], dtb_file
, False, output
)
498 with
open(output
) as infile
:
500 self
.assertEqual('''#include <common.h>
502 #include <dt-structs.h>
504 static struct dtd_test1 dtv_test1 = {
505 \t.reg\t\t\t= {0x1234, 0x567800000000},
507 U_BOOT_DEVICE(test1) = {
508 \t.name\t\t= "test1",
509 \t.platdata\t= &dtv_test1,
510 \t.platdata_size\t= sizeof(dtv_test1),
513 static struct dtd_test2 dtv_test2 = {
514 \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
516 U_BOOT_DEVICE(test2) = {
517 \t.name\t\t= "test2",
518 \t.platdata\t= &dtv_test2,
519 \t.platdata_size\t= sizeof(dtv_test2),
522 static struct dtd_test3 dtv_test3 = {
523 \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
525 U_BOOT_DEVICE(test3) = {
526 \t.name\t\t= "test3",
527 \t.platdata\t= &dtv_test3,
528 \t.platdata_size\t= sizeof(dtv_test3),