blob: 8498e8303c26131a0195b5f31921694725adec26 [file] [log] [blame]
Simon Glassbfb0bb22019-10-31 07:42:55 -06001#!/usr/bin/env python3
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass9d2eb922017-06-18 22:09:06 -06003# Copyright (c) 2012 The Chromium OS Authors.
4#
Simon Glass9d2eb922017-06-18 22:09:06 -06005
6"""Tests for the dtb_platdata module
7
Simon Glass70cd0d72018-07-06 10:27:20 -06008This includes unit tests for some functions and functional tests for the dtoc
9tool.
Simon Glass9d2eb922017-06-18 22:09:06 -060010"""
11
12import collections
13import os
14import struct
15import unittest
16
Simon Glassa997ea52020-04-17 18:09:04 -060017from dtoc import dtb_platdata
Simon Glass9d2eb922017-06-18 22:09:06 -060018from dtb_platdata import conv_name_to_c
19from dtb_platdata import get_compat_name
20from dtb_platdata import get_value
21from dtb_platdata import tab_to
Simon Glassa997ea52020-04-17 18:09:04 -060022from dtoc import fdt
23from dtoc import fdt_util
24from patman import test_util
25from patman import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060026
27our_path = os.path.dirname(os.path.realpath(__file__))
28
29
Simon Glasseb37e2d2017-11-12 21:52:17 -070030HEADER = '''/*
31 * DO NOT MODIFY
32 *
33 * This file was generated by dtoc from a .dtb (device tree binary) file.
34 */
35
36#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090037#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070038
39C_HEADER = '''/*
40 * DO NOT MODIFY
41 *
42 * This file was generated by dtoc from a .dtb (device tree binary) file.
43 */
44
45#include <common.h>
46#include <dm.h>
47#include <dt-structs.h>
48'''
49
50
Simon Glass3bce93d2018-07-06 10:27:37 -060051
52def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060053 """Compile a .dts file to a .dtb
54
55 Args:
56 dts_fname: Filename of .dts file in the current directory
Simon Glass3bce93d2018-07-06 10:27:37 -060057 capture_stderr: True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060058
59 Returns:
60 Filename of compiled file in output directory
61 """
Simon Glass3bce93d2018-07-06 10:27:37 -060062 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
63 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060064
65
66class TestDtoc(unittest.TestCase):
67 """Tests for dtoc"""
68 @classmethod
69 def setUpClass(cls):
70 tools.PrepareOutputDir(None)
71
72 @classmethod
73 def tearDownClass(cls):
74 tools._RemoveOutputDir()
75
Simon Glassc47c2b32018-07-06 10:27:25 -060076 def _WritePythonString(self, fname, data):
77 """Write a string with tabs expanded as done in this Python file
78
79 Args:
80 fname: Filename to write to
81 data: Raw string to convert
82 """
83 data = data.replace('\t', '\\t')
84 with open(fname, 'w') as fd:
85 fd.write(data)
86
87 def _CheckStrings(self, expected, actual):
88 """Check that a string matches its expected value
89
90 If the strings do not match, they are written to the /tmp directory in
91 the same Python format as is used here in the test. This allows for
92 easy comparison and update of the tests.
93
94 Args:
95 expected: Expected string
96 actual: Actual string
97 """
98 if expected != actual:
99 self._WritePythonString('/tmp/binman.expected', expected)
100 self._WritePythonString('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600101 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassc47c2b32018-07-06 10:27:25 -0600102 self.assertEquals(expected, actual)
103
Simon Glass9d2eb922017-06-18 22:09:06 -0600104 def test_name(self):
105 """Test conversion of device tree names to C identifiers"""
106 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
107 self.assertEqual('vendor_clock_frequency',
108 conv_name_to_c('vendor,clock-frequency'))
109 self.assertEqual('rockchip_rk3399_sdhci_5_1',
110 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
111
112 def test_tab_to(self):
113 """Test operation of tab_to() function"""
114 self.assertEqual('fred ', tab_to(0, 'fred'))
115 self.assertEqual('fred\t', tab_to(1, 'fred'))
116 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
117 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
118 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
119 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
120
121 def test_get_value(self):
122 """Test operation of get_value() function"""
123 self.assertEqual('0x45',
124 get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
125 self.assertEqual('0x45',
126 get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
127 self.assertEqual('0x0',
128 get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
129 self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
130 self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
131
132 def test_get_compat_name(self):
133 """Test operation of get_compat_name() function"""
134 Prop = collections.namedtuple('Prop', ['value'])
135 Node = collections.namedtuple('Node', ['props'])
136
137 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
138 node = Node({'compatible': prop})
139 self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
140 get_compat_name(node))
141
142 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
143 node = Node({'compatible': prop})
144 self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
145 get_compat_name(node))
146
147 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
148 node = Node({'compatible': prop})
149 self.assertEqual(('rockchip_rk3399_sdhci_5_1',
150 ['arasan_sdhci_5_1', 'third']),
151 get_compat_name(node))
152
153 def test_empty_file(self):
154 """Test output from a device tree file with no nodes"""
155 dtb_file = get_dtb_file('dtoc_test_empty.dts')
156 output = tools.GetOutputFilename('output')
157 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
158 with open(output) as infile:
159 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700160 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600161
162 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
163 with open(output) as infile:
164 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700165 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600166
167 def test_simple(self):
168 """Test output from some simple nodes with various types of data"""
169 dtb_file = get_dtb_file('dtoc_test_simple.dts')
170 output = tools.GetOutputFilename('output')
171 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
172 with open(output) as infile:
173 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600174 self._CheckStrings(HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600175struct dtd_sandbox_i2c_test {
176};
177struct dtd_sandbox_pmic_test {
178\tbool\t\tlow_power;
179\tfdt64_t\t\treg[2];
180};
Simon Glass9d2eb922017-06-18 22:09:06 -0600181struct dtd_sandbox_spl_test {
182\tbool\t\tboolval;
183\tunsigned char\tbytearray[3];
184\tunsigned char\tbyteval;
185\tfdt32_t\t\tintarray[4];
186\tfdt32_t\t\tintval;
187\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600188\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600189\tconst char *\tstringarray[3];
190\tconst char *\tstringval;
191};
192struct dtd_sandbox_spl_test_2 {
193};
194''', data)
195
196 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
197 with open(output) as infile:
198 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600199 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100200static const struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600201\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600202\t.bytearray\t\t= {0x6, 0x0, 0x0},
203\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600204\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600205\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600206\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
207\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600208\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600209\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600210\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600211};
212U_BOOT_DEVICE(spl_test) = {
213\t.name\t\t= "sandbox_spl_test",
214\t.platdata\t= &dtv_spl_test,
215\t.platdata_size\t= sizeof(dtv_spl_test),
216};
217
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100218static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600219\t.bytearray\t\t= {0x1, 0x23, 0x34},
220\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600221\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600222\t.intval\t\t\t= 0x3,
Simon Glass131e0b02017-08-29 14:15:49 -0600223\t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
224\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600225\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600226\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600227};
228U_BOOT_DEVICE(spl_test2) = {
229\t.name\t\t= "sandbox_spl_test",
230\t.platdata\t= &dtv_spl_test2,
231\t.platdata_size\t= sizeof(dtv_spl_test2),
232};
233
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100234static const struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600235\t.stringarray\t\t= {"one", "", ""},
236};
237U_BOOT_DEVICE(spl_test3) = {
238\t.name\t\t= "sandbox_spl_test",
239\t.platdata\t= &dtv_spl_test3,
240\t.platdata_size\t= sizeof(dtv_spl_test3),
241};
242
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100243static const struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600244};
245U_BOOT_DEVICE(spl_test4) = {
246\t.name\t\t= "sandbox_spl_test_2",
247\t.platdata\t= &dtv_spl_test4,
248\t.platdata_size\t= sizeof(dtv_spl_test4),
249};
250
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100251static const struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600252};
253U_BOOT_DEVICE(i2c_at_0) = {
254\t.name\t\t= "sandbox_i2c_test",
255\t.platdata\t= &dtv_i2c_at_0,
256\t.platdata_size\t= sizeof(dtv_i2c_at_0),
257};
258
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100259static const struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600260\t.low_power\t\t= true,
261\t.reg\t\t\t= {0x9, 0x0},
262};
263U_BOOT_DEVICE(pmic_at_9) = {
264\t.name\t\t= "sandbox_pmic_test",
265\t.platdata\t= &dtv_pmic_at_9,
266\t.platdata_size\t= sizeof(dtv_pmic_at_9),
267};
268
Simon Glass9d2eb922017-06-18 22:09:06 -0600269''', data)
270
271 def test_phandle(self):
272 """Test output from a node containing a phandle reference"""
273 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
274 output = tools.GetOutputFilename('output')
275 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
276 with open(output) as infile:
277 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600278 self._CheckStrings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600279struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600280\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600281};
282struct dtd_target {
283\tfdt32_t\t\tintval;
284};
285''', data)
286
287 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
288 with open(output) as infile:
289 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600290 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100291static const struct dtd_target dtv_phandle_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600292\t.intval\t\t\t= 0x0,
Simon Glass9d2eb922017-06-18 22:09:06 -0600293};
294U_BOOT_DEVICE(phandle_target) = {
295\t.name\t\t= "target",
296\t.platdata\t= &dtv_phandle_target,
297\t.platdata_size\t= sizeof(dtv_phandle_target),
298};
299
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100300static const struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600301\t.intval\t\t\t= 0x1,
302};
303U_BOOT_DEVICE(phandle2_target) = {
304\t.name\t\t= "target",
305\t.platdata\t= &dtv_phandle2_target,
306\t.platdata_size\t= sizeof(dtv_phandle2_target),
307};
308
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100309static const struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600310\t.intval\t\t\t= 0x2,
311};
312U_BOOT_DEVICE(phandle3_target) = {
313\t.name\t\t= "target",
314\t.platdata\t= &dtv_phandle3_target,
315\t.platdata_size\t= sizeof(dtv_phandle3_target),
316};
317
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100318static const struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600319\t.clocks\t\t\t= {
Simon Glass3deeb472017-08-29 14:15:59 -0600320\t\t\t{&dtv_phandle_target, {}},
321\t\t\t{&dtv_phandle2_target, {11}},
322\t\t\t{&dtv_phandle3_target, {12, 13}},
323\t\t\t{&dtv_phandle_target, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600324};
325U_BOOT_DEVICE(phandle_source) = {
326\t.name\t\t= "source",
327\t.platdata\t= &dtv_phandle_source,
328\t.platdata_size\t= sizeof(dtv_phandle_source),
329};
330
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100331static const struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600332\t.clocks\t\t\t= {
333\t\t\t{&dtv_phandle_target, {}},},
334};
335U_BOOT_DEVICE(phandle_source2) = {
336\t.name\t\t= "source",
337\t.platdata\t= &dtv_phandle_source2,
338\t.platdata_size\t= sizeof(dtv_phandle_source2),
339};
340
Simon Glass9d2eb922017-06-18 22:09:06 -0600341''', data)
342
Simon Glass961c1ce2018-07-06 10:27:35 -0600343 def test_phandle_single(self):
344 """Test output from a node containing a phandle reference"""
345 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
346 output = tools.GetOutputFilename('output')
347 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
348 with open(output) as infile:
349 data = infile.read()
350 self._CheckStrings(HEADER + '''
351struct dtd_source {
352\tstruct phandle_0_arg clocks[1];
353};
354struct dtd_target {
355\tfdt32_t\t\tintval;
356};
357''', data)
358
359 def test_phandle_reorder(self):
360 """Test that phandle targets are generated before their references"""
361 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
362 output = tools.GetOutputFilename('output')
363 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
364 with open(output) as infile:
365 data = infile.read()
366 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100367static const struct dtd_target dtv_phandle_target = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600368};
369U_BOOT_DEVICE(phandle_target) = {
370\t.name\t\t= "target",
371\t.platdata\t= &dtv_phandle_target,
372\t.platdata_size\t= sizeof(dtv_phandle_target),
373};
374
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100375static const struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600376\t.clocks\t\t\t= {
377\t\t\t{&dtv_phandle_target, {}},},
378};
379U_BOOT_DEVICE(phandle_source2) = {
380\t.name\t\t= "source",
381\t.platdata\t= &dtv_phandle_source2,
382\t.platdata_size\t= sizeof(dtv_phandle_source2),
383};
384
385''', data)
386
387 def test_phandle_bad(self):
388 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600389 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
390 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600391 output = tools.GetOutputFilename('output')
392 with self.assertRaises(ValueError) as e:
393 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
394 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
395 str(e.exception))
396
397 def test_phandle_bad2(self):
398 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600399 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
400 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600401 output = tools.GetOutputFilename('output')
402 with self.assertRaises(ValueError) as e:
403 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
404 self.assertIn("Node 'phandle-target' has no '#clock-cells' property",
405 str(e.exception))
406
Simon Glass9d2eb922017-06-18 22:09:06 -0600407 def test_aliases(self):
408 """Test output from a node with multiple compatible strings"""
409 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
410 output = tools.GetOutputFilename('output')
411 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
412 with open(output) as infile:
413 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600414 self._CheckStrings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600415struct dtd_compat1 {
416\tfdt32_t\t\tintval;
417};
418#define dtd_compat2_1_fred dtd_compat1
419#define dtd_compat3 dtd_compat1
420''', data)
421
422 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
423 with open(output) as infile:
424 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600425 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100426static const struct dtd_compat1 dtv_spl_test = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600427\t.intval\t\t\t= 0x1,
428};
429U_BOOT_DEVICE(spl_test) = {
430\t.name\t\t= "compat1",
431\t.platdata\t= &dtv_spl_test,
432\t.platdata_size\t= sizeof(dtv_spl_test),
433};
434
435''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600436
437 def test_addresses64(self):
438 """Test output from a node with a 'reg' property with na=2, ns=2"""
439 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
440 output = tools.GetOutputFilename('output')
441 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
442 with open(output) as infile:
443 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600444 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600445struct dtd_test1 {
446\tfdt64_t\t\treg[2];
447};
448struct dtd_test2 {
449\tfdt64_t\t\treg[2];
450};
451struct dtd_test3 {
452\tfdt64_t\t\treg[4];
453};
454''', data)
455
456 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
457 with open(output) as infile:
458 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600459 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100460static const struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600461\t.reg\t\t\t= {0x1234, 0x5678},
462};
463U_BOOT_DEVICE(test1) = {
464\t.name\t\t= "test1",
465\t.platdata\t= &dtv_test1,
466\t.platdata_size\t= sizeof(dtv_test1),
467};
468
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100469static const struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600470\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
471};
472U_BOOT_DEVICE(test2) = {
473\t.name\t\t= "test2",
474\t.platdata\t= &dtv_test2,
475\t.platdata_size\t= sizeof(dtv_test2),
476};
477
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100478static const struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600479\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
480};
481U_BOOT_DEVICE(test3) = {
482\t.name\t\t= "test3",
483\t.platdata\t= &dtv_test3,
484\t.platdata_size\t= sizeof(dtv_test3),
485};
486
487''', data)
488
489 def test_addresses32(self):
490 """Test output from a node with a 'reg' property with na=1, ns=1"""
491 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
492 output = tools.GetOutputFilename('output')
493 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
494 with open(output) as infile:
495 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600496 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600497struct dtd_test1 {
498\tfdt32_t\t\treg[2];
499};
500struct dtd_test2 {
501\tfdt32_t\t\treg[4];
502};
503''', data)
504
505 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
506 with open(output) as infile:
507 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600508 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100509static const struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600510\t.reg\t\t\t= {0x1234, 0x5678},
511};
512U_BOOT_DEVICE(test1) = {
513\t.name\t\t= "test1",
514\t.platdata\t= &dtv_test1,
515\t.platdata_size\t= sizeof(dtv_test1),
516};
517
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100518static const struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600519\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
520};
521U_BOOT_DEVICE(test2) = {
522\t.name\t\t= "test2",
523\t.platdata\t= &dtv_test2,
524\t.platdata_size\t= sizeof(dtv_test2),
525};
526
527''', data)
528
529 def test_addresses64_32(self):
530 """Test output from a node with a 'reg' property with na=2, ns=1"""
531 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
532 output = tools.GetOutputFilename('output')
533 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
534 with open(output) as infile:
535 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600536 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600537struct dtd_test1 {
538\tfdt64_t\t\treg[2];
539};
540struct dtd_test2 {
541\tfdt64_t\t\treg[2];
542};
543struct dtd_test3 {
544\tfdt64_t\t\treg[4];
545};
546''', data)
547
548 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
549 with open(output) as infile:
550 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600551 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100552static const struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600553\t.reg\t\t\t= {0x123400000000, 0x5678},
554};
555U_BOOT_DEVICE(test1) = {
556\t.name\t\t= "test1",
557\t.platdata\t= &dtv_test1,
558\t.platdata_size\t= sizeof(dtv_test1),
559};
560
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100561static const struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600562\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
563};
564U_BOOT_DEVICE(test2) = {
565\t.name\t\t= "test2",
566\t.platdata\t= &dtv_test2,
567\t.platdata_size\t= sizeof(dtv_test2),
568};
569
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100570static const struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600571\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
572};
573U_BOOT_DEVICE(test3) = {
574\t.name\t\t= "test3",
575\t.platdata\t= &dtv_test3,
576\t.platdata_size\t= sizeof(dtv_test3),
577};
578
579''', data)
580
581 def test_addresses32_64(self):
582 """Test output from a node with a 'reg' property with na=1, ns=2"""
583 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
584 output = tools.GetOutputFilename('output')
585 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
586 with open(output) as infile:
587 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600588 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600589struct dtd_test1 {
590\tfdt64_t\t\treg[2];
591};
592struct dtd_test2 {
593\tfdt64_t\t\treg[2];
594};
595struct dtd_test3 {
596\tfdt64_t\t\treg[4];
597};
598''', data)
599
600 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
601 with open(output) as infile:
602 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600603 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100604static const struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600605\t.reg\t\t\t= {0x1234, 0x567800000000},
606};
607U_BOOT_DEVICE(test1) = {
608\t.name\t\t= "test1",
609\t.platdata\t= &dtv_test1,
610\t.platdata_size\t= sizeof(dtv_test1),
611};
612
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100613static const struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600614\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
615};
616U_BOOT_DEVICE(test2) = {
617\t.name\t\t= "test2",
618\t.platdata\t= &dtv_test2,
619\t.platdata_size\t= sizeof(dtv_test2),
620};
621
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100622static const struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600623\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
624};
625U_BOOT_DEVICE(test3) = {
626\t.name\t\t= "test3",
627\t.platdata\t= &dtv_test3,
628\t.platdata_size\t= sizeof(dtv_test3),
629};
630
631''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600632
633 def test_bad_reg(self):
634 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600635 # Capture stderr since dtc will emit warnings for this file
636 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600637 output = tools.GetOutputFilename('output')
638 with self.assertRaises(ValueError) as e:
639 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
640 self.assertIn("Node 'spl-test' reg property is not an int",
641 str(e.exception))
642
643 def test_bad_reg2(self):
644 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600645 # Capture stderr since dtc will emit warnings for this file
646 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600647 output = tools.GetOutputFilename('output')
648 with self.assertRaises(ValueError) as e:
649 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
650 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
651 str(e.exception))
652
653 def test_add_prop(self):
654 """Test that a subequent node can add a new property to a struct"""
655 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
656 output = tools.GetOutputFilename('output')
657 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
658 with open(output) as infile:
659 data = infile.read()
660 self._CheckStrings(HEADER + '''
661struct dtd_sandbox_spl_test {
662\tfdt32_t\t\tintarray;
663\tfdt32_t\t\tintval;
664};
665''', data)
666
667 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
668 with open(output) as infile:
669 data = infile.read()
670 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100671static const struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600672\t.intval\t\t\t= 0x1,
673};
674U_BOOT_DEVICE(spl_test) = {
675\t.name\t\t= "sandbox_spl_test",
676\t.platdata\t= &dtv_spl_test,
677\t.platdata_size\t= sizeof(dtv_spl_test),
678};
679
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100680static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600681\t.intarray\t\t= 0x5,
682};
683U_BOOT_DEVICE(spl_test2) = {
684\t.name\t\t= "sandbox_spl_test",
685\t.platdata\t= &dtv_spl_test2,
686\t.platdata_size\t= sizeof(dtv_spl_test2),
687};
688
689''', data)
690
691 def testStdout(self):
692 """Test output to stdout"""
693 dtb_file = get_dtb_file('dtoc_test_simple.dts')
694 with test_util.capture_sys_output() as (stdout, stderr):
695 dtb_platdata.run_steps(['struct'], dtb_file, False, '-')
696
697 def testNoCommand(self):
698 """Test running dtoc without a command"""
699 with self.assertRaises(ValueError) as e:
700 dtb_platdata.run_steps([], '', False, '')
701 self.assertIn("Please specify a command: struct, platdata",
702 str(e.exception))
703
704 def testBadCommand(self):
705 """Test running dtoc with an invalid command"""
706 dtb_file = get_dtb_file('dtoc_test_simple.dts')
707 output = tools.GetOutputFilename('output')
708 with self.assertRaises(ValueError) as e:
709 dtb_platdata.run_steps(['invalid-cmd'], dtb_file, False, output)
710 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
711 str(e.exception))