blob: b915b278560411a776d2e3e679e6fc639787e02e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassc0791922017-06-18 22:09:06 -06002# Copyright (c) 2012 The Chromium OS Authors.
3#
Simon Glassc0791922017-06-18 22:09:06 -06004
5"""Tests for the dtb_platdata module
6
Simon Glass3def0cf2018-07-06 10:27:20 -06007This includes unit tests for some functions and functional tests for the dtoc
8tool.
Simon Glassc0791922017-06-18 22:09:06 -06009"""
10
Simon Glass90a81322019-05-17 22:00:31 -060011from __future__ import print_function
12
Simon Glassc0791922017-06-18 22:09:06 -060013import collections
14import os
15import struct
16import unittest
17
18import dtb_platdata
19from dtb_platdata import conv_name_to_c
20from dtb_platdata import get_compat_name
21from dtb_platdata import get_value
22from dtb_platdata import tab_to
23import fdt
24import fdt_util
Simon Glass8512ea22018-07-06 10:27:35 -060025import test_util
Simon Glassc0791922017-06-18 22:09:06 -060026import tools
27
28our_path = os.path.dirname(os.path.realpath(__file__))
29
30
Simon Glassaab660f2017-11-12 21:52:17 -070031HEADER = '''/*
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>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090038#include <linux/libfdt.h>'''
Simon Glassaab660f2017-11-12 21:52:17 -070039
40C_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
Simon Glassfe57c782018-07-06 10:27:37 -060052
53def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glassc0791922017-06-18 22:09:06 -060054 """Compile a .dts file to a .dtb
55
56 Args:
57 dts_fname: Filename of .dts file in the current directory
Simon Glassfe57c782018-07-06 10:27:37 -060058 capture_stderr: True to capture and discard stderr output
Simon Glassc0791922017-06-18 22:09:06 -060059
60 Returns:
61 Filename of compiled file in output directory
62 """
Simon Glassfe57c782018-07-06 10:27:37 -060063 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
64 capture_stderr=capture_stderr)
Simon Glassc0791922017-06-18 22:09:06 -060065
66
67class 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
Simon Glass57f0bc42018-07-06 10:27:25 -060077 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)
Simon Glass90a81322019-05-17 22:00:31 -0600102 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glass57f0bc42018-07-06 10:27:25 -0600103 self.assertEquals(expected, actual)
104
Simon Glassc0791922017-06-18 22:09:06 -0600105 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()
Simon Glassaab660f2017-11-12 21:52:17 -0700161 self.assertEqual(HEADER.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600162
163 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
164 with open(output) as infile:
165 lines = infile.read().splitlines()
Simon Glassaab660f2017-11-12 21:52:17 -0700166 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glassc0791922017-06-18 22:09:06 -0600167
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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600175 self._CheckStrings(HEADER + '''
Simon Glass5ec741f2017-08-29 14:15:51 -0600176struct dtd_sandbox_i2c_test {
177};
178struct dtd_sandbox_pmic_test {
179\tbool\t\tlow_power;
180\tfdt64_t\t\treg[2];
181};
Simon Glassc0791922017-06-18 22:09:06 -0600182struct 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];
Simon Glass2a2d91d2018-07-06 10:27:28 -0600189\tunsigned char\tnotstring[5];
Simon Glassc0791922017-06-18 22:09:06 -0600190\tconst char *\tstringarray[3];
191\tconst char *\tstringval;
192};
193struct 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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600200 self._CheckStrings(C_HEADER + '''
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100201static const struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass1953ce72019-05-17 22:00:32 -0600202\t.boolval\t\t= true,
Simon Glassc0791922017-06-18 22:09:06 -0600203\t.bytearray\t\t= {0x6, 0x0, 0x0},
204\t.byteval\t\t= 0x5,
Simon Glass1953ce72019-05-17 22:00:32 -0600205\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600206\t.intval\t\t\t= 0x1,
Simon Glass21d54ac2017-08-29 14:15:49 -0600207\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
208\t\t0x11},
Simon Glass1953ce72019-05-17 22:00:32 -0600209\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600210\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glass1953ce72019-05-17 22:00:32 -0600211\t.stringval\t\t= "message",
Simon Glassc0791922017-06-18 22:09:06 -0600212};
213U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100219static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glassc0791922017-06-18 22:09:06 -0600220\t.bytearray\t\t= {0x1, 0x23, 0x34},
221\t.byteval\t\t= 0x8,
Simon Glass1953ce72019-05-17 22:00:32 -0600222\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600223\t.intval\t\t\t= 0x3,
Simon Glass21d54ac2017-08-29 14:15:49 -0600224\t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
225\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600226\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glass1953ce72019-05-17 22:00:32 -0600227\t.stringval\t\t= "message2",
Simon Glassc0791922017-06-18 22:09:06 -0600228};
229U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100235static const struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glassc0791922017-06-18 22:09:06 -0600236\t.stringarray\t\t= {"one", "", ""},
237};
238U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100244static const struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
Simon Glassc0791922017-06-18 22:09:06 -0600245};
246U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100252static const struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
Simon Glass5ec741f2017-08-29 14:15:51 -0600253};
254U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100260static const struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
Simon Glass5ec741f2017-08-29 14:15:51 -0600261\t.low_power\t\t= true,
262\t.reg\t\t\t= {0x9, 0x0},
263};
264U_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
Simon Glassc0791922017-06-18 22:09:06 -0600270''', 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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600279 self._CheckStrings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600280struct dtd_source {
Simon Glass634eba42017-08-29 14:15:59 -0600281\tstruct phandle_2_arg clocks[4];
Simon Glassc0791922017-06-18 22:09:06 -0600282};
283struct 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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600291 self._CheckStrings(C_HEADER + '''
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100292static const struct dtd_target dtv_phandle_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600293\t.intval\t\t\t= 0x0,
Simon Glassc0791922017-06-18 22:09:06 -0600294};
295U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100301static const struct dtd_target dtv_phandle2_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600302\t.intval\t\t\t= 0x1,
303};
304U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100310static const struct dtd_target dtv_phandle3_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600311\t.intval\t\t\t= 0x2,
312};
313U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100319static const struct dtd_source dtv_phandle_source = {
Simon Glass35d50372017-08-29 14:15:57 -0600320\t.clocks\t\t\t= {
Simon Glass634eba42017-08-29 14:15:59 -0600321\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, {}},},
Simon Glassc0791922017-06-18 22:09:06 -0600325};
326U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100332static const struct dtd_source dtv_phandle_source2 = {
Simon Glass760b7172018-07-06 10:27:31 -0600333\t.clocks\t\t\t= {
334\t\t\t{&dtv_phandle_target, {}},},
335};
336U_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
Simon Glassc0791922017-06-18 22:09:06 -0600342''', data)
343
Simon Glass8512ea22018-07-06 10:27:35 -0600344 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 + '''
352struct dtd_source {
353\tstruct phandle_0_arg clocks[1];
354};
355struct 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 + '''
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100368static const struct dtd_target dtv_phandle_target = {
Simon Glass8512ea22018-07-06 10:27:35 -0600369};
370U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100376static const struct dtd_source dtv_phandle_source2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600377\t.clocks\t\t\t= {
378\t\t\t{&dtv_phandle_target, {}},},
379};
380U_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"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600390 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
391 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600392 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"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600400 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
401 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600402 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
Simon Glassc0791922017-06-18 22:09:06 -0600408 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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600415 self._CheckStrings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600416struct 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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600426 self._CheckStrings(C_HEADER + '''
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100427static const struct dtd_compat1 dtv_spl_test = {
Simon Glassc0791922017-06-18 22:09:06 -0600428\t.intval\t\t\t= 0x1,
429};
430U_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)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600437
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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600445 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600446struct dtd_test1 {
447\tfdt64_t\t\treg[2];
448};
449struct dtd_test2 {
450\tfdt64_t\t\treg[2];
451};
452struct 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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600460 self._CheckStrings(C_HEADER + '''
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100461static const struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600462\t.reg\t\t\t= {0x1234, 0x5678},
463};
464U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100470static const struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600471\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
472};
473U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100479static const struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600480\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
481};
482U_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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600497 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600498struct dtd_test1 {
499\tfdt32_t\t\treg[2];
500};
501struct 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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600509 self._CheckStrings(C_HEADER + '''
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100510static const struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600511\t.reg\t\t\t= {0x1234, 0x5678},
512};
513U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100519static const struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600520\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
521};
522U_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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600537 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600538struct dtd_test1 {
539\tfdt64_t\t\treg[2];
540};
541struct dtd_test2 {
542\tfdt64_t\t\treg[2];
543};
544struct 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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600552 self._CheckStrings(C_HEADER + '''
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100553static const struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600554\t.reg\t\t\t= {0x123400000000, 0x5678},
555};
556U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100562static const struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600563\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
564};
565U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100571static const struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600572\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
573};
574U_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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600589 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600590struct dtd_test1 {
591\tfdt64_t\t\treg[2];
592};
593struct dtd_test2 {
594\tfdt64_t\t\treg[2];
595};
596struct 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()
Simon Glass57f0bc42018-07-06 10:27:25 -0600604 self._CheckStrings(C_HEADER + '''
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100605static const struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600606\t.reg\t\t\t= {0x1234, 0x567800000000},
607};
608U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100614static const struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600615\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
616};
617U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100623static const struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600624\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
625};
626U_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)
Simon Glass8512ea22018-07-06 10:27:35 -0600633
634 def test_bad_reg(self):
635 """Test that a reg property with an invalid type generates an error"""
Simon Glassfe57c782018-07-06 10:27:37 -0600636 # Capture stderr since dtc will emit warnings for this file
637 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600638 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"""
Simon Glassfe57c782018-07-06 10:27:37 -0600646 # Capture stderr since dtc will emit warnings for this file
647 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600648 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 + '''
662struct 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 + '''
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100672static const struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass8512ea22018-07-06 10:27:35 -0600673\t.intval\t\t\t= 0x1,
674};
675U_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
Simon Goldschmidte4478d92019-01-21 10:07:55 +0100681static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600682\t.intarray\t\t= 0x5,
683};
684U_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))