blob: ae3ec509c13019dfaa1b779b2c7f837684a2374e [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
Walter Lozanoa324e412020-06-25 01:10:08 -030015import sys
Simon Glass9d2eb922017-06-18 22:09:06 -060016import unittest
17
Simon Glassa997ea52020-04-17 18:09:04 -060018from dtoc import dtb_platdata
Simon Glass9d2eb922017-06-18 22:09:06 -060019from 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
Simon Glassa997ea52020-04-17 18:09:04 -060023from dtoc import fdt
24from dtoc import fdt_util
25from patman import test_util
26from patman import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060027
28our_path = os.path.dirname(os.path.realpath(__file__))
29
30
Simon Glasseb37e2d2017-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 Yamada75f82d02018-03-05 01:20:11 +090038#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-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 Glass3bce93d2018-07-06 10:27:37 -060052
53def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-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 Glass3bce93d2018-07-06 10:27:37 -060058 capture_stderr: True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060059
60 Returns:
61 Filename of compiled file in output directory
62 """
Simon Glass3bce93d2018-07-06 10:27:37 -060063 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
64 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-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 Glassc47c2b32018-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 Glass61b88e52019-05-17 22:00:31 -0600102 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassc47c2b32018-07-06 10:27:25 -0600103 self.assertEquals(expected, actual)
104
Walter Lozanoa324e412020-06-25 01:10:08 -0300105
106 def run_test(self, args, dtb_file, output):
107 dtb_platdata.run_steps(args, dtb_file, False, output, True)
108
Simon Glass9d2eb922017-06-18 22:09:06 -0600109 def test_name(self):
110 """Test conversion of device tree names to C identifiers"""
111 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
112 self.assertEqual('vendor_clock_frequency',
113 conv_name_to_c('vendor,clock-frequency'))
114 self.assertEqual('rockchip_rk3399_sdhci_5_1',
115 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
116
117 def test_tab_to(self):
118 """Test operation of tab_to() function"""
119 self.assertEqual('fred ', tab_to(0, 'fred'))
120 self.assertEqual('fred\t', tab_to(1, 'fred'))
121 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
122 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
123 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
124 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
125
126 def test_get_value(self):
127 """Test operation of get_value() function"""
128 self.assertEqual('0x45',
129 get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
130 self.assertEqual('0x45',
131 get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
132 self.assertEqual('0x0',
133 get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
134 self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
135 self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
136
137 def test_get_compat_name(self):
138 """Test operation of get_compat_name() function"""
139 Prop = collections.namedtuple('Prop', ['value'])
140 Node = collections.namedtuple('Node', ['props'])
141
142 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
143 node = Node({'compatible': prop})
144 self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
145 get_compat_name(node))
146
147 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
148 node = Node({'compatible': prop})
149 self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
150 get_compat_name(node))
151
152 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
153 node = Node({'compatible': prop})
154 self.assertEqual(('rockchip_rk3399_sdhci_5_1',
155 ['arasan_sdhci_5_1', 'third']),
156 get_compat_name(node))
157
158 def test_empty_file(self):
159 """Test output from a device tree file with no nodes"""
160 dtb_file = get_dtb_file('dtoc_test_empty.dts')
161 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300162 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600163 with open(output) as infile:
164 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700165 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600166
Walter Lozanoa324e412020-06-25 01:10:08 -0300167 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600168 with open(output) as infile:
169 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700170 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600171
172 def test_simple(self):
173 """Test output from some simple nodes with various types of data"""
174 dtb_file = get_dtb_file('dtoc_test_simple.dts')
175 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300176 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600177 with open(output) as infile:
178 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600179 self._CheckStrings(HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600180struct dtd_sandbox_i2c_test {
181};
182struct dtd_sandbox_pmic_test {
183\tbool\t\tlow_power;
184\tfdt64_t\t\treg[2];
185};
Simon Glass9d2eb922017-06-18 22:09:06 -0600186struct dtd_sandbox_spl_test {
187\tbool\t\tboolval;
188\tunsigned char\tbytearray[3];
189\tunsigned char\tbyteval;
190\tfdt32_t\t\tintarray[4];
191\tfdt32_t\t\tintval;
192\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600193\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600194\tconst char *\tstringarray[3];
195\tconst char *\tstringval;
196};
197struct dtd_sandbox_spl_test_2 {
198};
199''', data)
200
Walter Lozanoa324e412020-06-25 01:10:08 -0300201 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600202 with open(output) as infile:
203 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600204 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100205static const struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600206\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600207\t.bytearray\t\t= {0x6, 0x0, 0x0},
208\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600209\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600210\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600211\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
212\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600213\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600214\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600215\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600216};
217U_BOOT_DEVICE(spl_test) = {
218\t.name\t\t= "sandbox_spl_test",
219\t.platdata\t= &dtv_spl_test,
220\t.platdata_size\t= sizeof(dtv_spl_test),
221};
222
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100223static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600224\t.bytearray\t\t= {0x1, 0x23, 0x34},
225\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600226\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600227\t.intval\t\t\t= 0x3,
Simon Glass131e0b02017-08-29 14:15:49 -0600228\t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
229\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600230\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600231\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600232};
233U_BOOT_DEVICE(spl_test2) = {
234\t.name\t\t= "sandbox_spl_test",
235\t.platdata\t= &dtv_spl_test2,
236\t.platdata_size\t= sizeof(dtv_spl_test2),
237};
238
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100239static const struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600240\t.stringarray\t\t= {"one", "", ""},
241};
242U_BOOT_DEVICE(spl_test3) = {
243\t.name\t\t= "sandbox_spl_test",
244\t.platdata\t= &dtv_spl_test3,
245\t.platdata_size\t= sizeof(dtv_spl_test3),
246};
247
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100248static const struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600249};
250U_BOOT_DEVICE(spl_test4) = {
251\t.name\t\t= "sandbox_spl_test_2",
252\t.platdata\t= &dtv_spl_test4,
253\t.platdata_size\t= sizeof(dtv_spl_test4),
254};
255
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100256static const struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600257};
258U_BOOT_DEVICE(i2c_at_0) = {
259\t.name\t\t= "sandbox_i2c_test",
260\t.platdata\t= &dtv_i2c_at_0,
261\t.platdata_size\t= sizeof(dtv_i2c_at_0),
262};
263
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100264static const struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600265\t.low_power\t\t= true,
266\t.reg\t\t\t= {0x9, 0x0},
267};
268U_BOOT_DEVICE(pmic_at_9) = {
269\t.name\t\t= "sandbox_pmic_test",
270\t.platdata\t= &dtv_pmic_at_9,
271\t.platdata_size\t= sizeof(dtv_pmic_at_9),
272};
273
Simon Glass9d2eb922017-06-18 22:09:06 -0600274''', data)
275
Walter Lozanoe675d962020-07-03 08:07:17 -0300276 def test_driver_alias(self):
277 """Test output from a device tree file with a driver alias"""
278 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
279 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300280 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300281 with open(output) as infile:
282 data = infile.read()
283 self._CheckStrings(HEADER + '''
284struct dtd_sandbox_gpio {
285\tconst char *\tgpio_bank_name;
286\tbool\t\tgpio_controller;
287\tfdt32_t\t\tsandbox_gpio_count;
288};
289#define dtd_sandbox_gpio_alias dtd_sandbox_gpio
290''', data)
291
Walter Lozanoa324e412020-06-25 01:10:08 -0300292 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300293 with open(output) as infile:
294 data = infile.read()
295 self._CheckStrings(C_HEADER + '''
296static const struct dtd_sandbox_gpio dtv_gpios_at_0 = {
297\t.gpio_bank_name\t\t= "a",
298\t.gpio_controller\t= true,
299\t.sandbox_gpio_count\t= 0x14,
300};
301U_BOOT_DEVICE(gpios_at_0) = {
302\t.name\t\t= "sandbox_gpio",
303\t.platdata\t= &dtv_gpios_at_0,
304\t.platdata_size\t= sizeof(dtv_gpios_at_0),
305};
306
307''', data)
308
Walter Lozanoa324e412020-06-25 01:10:08 -0300309 def test_invalid_driver(self):
310 """Test output from a device tree file with an invalid driver"""
311 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
312 output = tools.GetOutputFilename('output')
313 with test_util.capture_sys_output() as (stdout, stderr):
314 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
315 with open(output) as infile:
316 data = infile.read()
317 self._CheckStrings(HEADER + '''
318struct dtd_invalid {
319};
320''', data)
321
322 with test_util.capture_sys_output() as (stdout, stderr):
323 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
324 with open(output) as infile:
325 data = infile.read()
326 self._CheckStrings(C_HEADER + '''
327static const struct dtd_invalid dtv_spl_test = {
328};
329U_BOOT_DEVICE(spl_test) = {
330\t.name\t\t= "invalid",
331\t.platdata\t= &dtv_spl_test,
332\t.platdata_size\t= sizeof(dtv_spl_test),
333};
334
335''', data)
336
Simon Glass9d2eb922017-06-18 22:09:06 -0600337 def test_phandle(self):
338 """Test output from a node containing a phandle reference"""
339 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
340 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300341 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600342 with open(output) as infile:
343 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600344 self._CheckStrings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600345struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600346\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600347};
348struct dtd_target {
349\tfdt32_t\t\tintval;
350};
351''', data)
352
Walter Lozanoa324e412020-06-25 01:10:08 -0300353 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600354 with open(output) as infile:
355 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600356 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100357static const struct dtd_target dtv_phandle_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600358\t.intval\t\t\t= 0x0,
Simon Glass9d2eb922017-06-18 22:09:06 -0600359};
360U_BOOT_DEVICE(phandle_target) = {
361\t.name\t\t= "target",
362\t.platdata\t= &dtv_phandle_target,
363\t.platdata_size\t= sizeof(dtv_phandle_target),
364};
365
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100366static const struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600367\t.intval\t\t\t= 0x1,
368};
369U_BOOT_DEVICE(phandle2_target) = {
370\t.name\t\t= "target",
371\t.platdata\t= &dtv_phandle2_target,
372\t.platdata_size\t= sizeof(dtv_phandle2_target),
373};
374
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100375static const struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600376\t.intval\t\t\t= 0x2,
377};
378U_BOOT_DEVICE(phandle3_target) = {
379\t.name\t\t= "target",
380\t.platdata\t= &dtv_phandle3_target,
381\t.platdata_size\t= sizeof(dtv_phandle3_target),
382};
383
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100384static const struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600385\t.clocks\t\t\t= {
Simon Glass3deeb472017-08-29 14:15:59 -0600386\t\t\t{&dtv_phandle_target, {}},
387\t\t\t{&dtv_phandle2_target, {11}},
388\t\t\t{&dtv_phandle3_target, {12, 13}},
389\t\t\t{&dtv_phandle_target, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600390};
391U_BOOT_DEVICE(phandle_source) = {
392\t.name\t\t= "source",
393\t.platdata\t= &dtv_phandle_source,
394\t.platdata_size\t= sizeof(dtv_phandle_source),
395};
396
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100397static const struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600398\t.clocks\t\t\t= {
399\t\t\t{&dtv_phandle_target, {}},},
400};
401U_BOOT_DEVICE(phandle_source2) = {
402\t.name\t\t= "source",
403\t.platdata\t= &dtv_phandle_source2,
404\t.platdata_size\t= sizeof(dtv_phandle_source2),
405};
406
Simon Glass9d2eb922017-06-18 22:09:06 -0600407''', data)
408
Simon Glass961c1ce2018-07-06 10:27:35 -0600409 def test_phandle_single(self):
410 """Test output from a node containing a phandle reference"""
411 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
412 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300413 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600414 with open(output) as infile:
415 data = infile.read()
416 self._CheckStrings(HEADER + '''
417struct dtd_source {
418\tstruct phandle_0_arg clocks[1];
419};
420struct dtd_target {
421\tfdt32_t\t\tintval;
422};
423''', data)
424
425 def test_phandle_reorder(self):
426 """Test that phandle targets are generated before their references"""
427 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
428 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300429 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600430 with open(output) as infile:
431 data = infile.read()
432 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100433static const struct dtd_target dtv_phandle_target = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600434};
435U_BOOT_DEVICE(phandle_target) = {
436\t.name\t\t= "target",
437\t.platdata\t= &dtv_phandle_target,
438\t.platdata_size\t= sizeof(dtv_phandle_target),
439};
440
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100441static const struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600442\t.clocks\t\t\t= {
443\t\t\t{&dtv_phandle_target, {}},},
444};
445U_BOOT_DEVICE(phandle_source2) = {
446\t.name\t\t= "source",
447\t.platdata\t= &dtv_phandle_source2,
448\t.platdata_size\t= sizeof(dtv_phandle_source2),
449};
450
451''', data)
452
453 def test_phandle_bad(self):
454 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600455 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
456 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600457 output = tools.GetOutputFilename('output')
458 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300459 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600460 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
461 str(e.exception))
462
463 def test_phandle_bad2(self):
464 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600465 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
466 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600467 output = tools.GetOutputFilename('output')
468 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300469 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600470 self.assertIn("Node 'phandle-target' has no '#clock-cells' property",
471 str(e.exception))
472
Simon Glass9d2eb922017-06-18 22:09:06 -0600473 def test_aliases(self):
474 """Test output from a node with multiple compatible strings"""
475 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
476 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300477 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600478 with open(output) as infile:
479 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600480 self._CheckStrings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600481struct dtd_compat1 {
482\tfdt32_t\t\tintval;
483};
484#define dtd_compat2_1_fred dtd_compat1
485#define dtd_compat3 dtd_compat1
486''', data)
487
Walter Lozanoa324e412020-06-25 01:10:08 -0300488 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600489 with open(output) as infile:
490 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600491 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100492static const struct dtd_compat1 dtv_spl_test = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600493\t.intval\t\t\t= 0x1,
494};
495U_BOOT_DEVICE(spl_test) = {
496\t.name\t\t= "compat1",
497\t.platdata\t= &dtv_spl_test,
498\t.platdata_size\t= sizeof(dtv_spl_test),
499};
500
501''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600502
503 def test_addresses64(self):
504 """Test output from a node with a 'reg' property with na=2, ns=2"""
505 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
506 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300507 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600508 with open(output) as infile:
509 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600510 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600511struct dtd_test1 {
512\tfdt64_t\t\treg[2];
513};
514struct dtd_test2 {
515\tfdt64_t\t\treg[2];
516};
517struct dtd_test3 {
518\tfdt64_t\t\treg[4];
519};
520''', data)
521
Walter Lozanoa324e412020-06-25 01:10:08 -0300522 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600523 with open(output) as infile:
524 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600525 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100526static const struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600527\t.reg\t\t\t= {0x1234, 0x5678},
528};
529U_BOOT_DEVICE(test1) = {
530\t.name\t\t= "test1",
531\t.platdata\t= &dtv_test1,
532\t.platdata_size\t= sizeof(dtv_test1),
533};
534
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100535static const struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600536\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
537};
538U_BOOT_DEVICE(test2) = {
539\t.name\t\t= "test2",
540\t.platdata\t= &dtv_test2,
541\t.platdata_size\t= sizeof(dtv_test2),
542};
543
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100544static const struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600545\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
546};
547U_BOOT_DEVICE(test3) = {
548\t.name\t\t= "test3",
549\t.platdata\t= &dtv_test3,
550\t.platdata_size\t= sizeof(dtv_test3),
551};
552
553''', data)
554
555 def test_addresses32(self):
556 """Test output from a node with a 'reg' property with na=1, ns=1"""
557 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
558 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300559 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600560 with open(output) as infile:
561 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600562 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600563struct dtd_test1 {
564\tfdt32_t\t\treg[2];
565};
566struct dtd_test2 {
567\tfdt32_t\t\treg[4];
568};
569''', data)
570
Walter Lozanoa324e412020-06-25 01:10:08 -0300571 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600572 with open(output) as infile:
573 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600574 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100575static const struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600576\t.reg\t\t\t= {0x1234, 0x5678},
577};
578U_BOOT_DEVICE(test1) = {
579\t.name\t\t= "test1",
580\t.platdata\t= &dtv_test1,
581\t.platdata_size\t= sizeof(dtv_test1),
582};
583
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100584static const struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600585\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
586};
587U_BOOT_DEVICE(test2) = {
588\t.name\t\t= "test2",
589\t.platdata\t= &dtv_test2,
590\t.platdata_size\t= sizeof(dtv_test2),
591};
592
593''', data)
594
595 def test_addresses64_32(self):
596 """Test output from a node with a 'reg' property with na=2, ns=1"""
597 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
598 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300599 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600600 with open(output) as infile:
601 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600602 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600603struct dtd_test1 {
604\tfdt64_t\t\treg[2];
605};
606struct dtd_test2 {
607\tfdt64_t\t\treg[2];
608};
609struct dtd_test3 {
610\tfdt64_t\t\treg[4];
611};
612''', data)
613
Walter Lozanoa324e412020-06-25 01:10:08 -0300614 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600615 with open(output) as infile:
616 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600617 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100618static const struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600619\t.reg\t\t\t= {0x123400000000, 0x5678},
620};
621U_BOOT_DEVICE(test1) = {
622\t.name\t\t= "test1",
623\t.platdata\t= &dtv_test1,
624\t.platdata_size\t= sizeof(dtv_test1),
625};
626
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100627static const struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600628\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
629};
630U_BOOT_DEVICE(test2) = {
631\t.name\t\t= "test2",
632\t.platdata\t= &dtv_test2,
633\t.platdata_size\t= sizeof(dtv_test2),
634};
635
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100636static const struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600637\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
638};
639U_BOOT_DEVICE(test3) = {
640\t.name\t\t= "test3",
641\t.platdata\t= &dtv_test3,
642\t.platdata_size\t= sizeof(dtv_test3),
643};
644
645''', data)
646
647 def test_addresses32_64(self):
648 """Test output from a node with a 'reg' property with na=1, ns=2"""
649 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
650 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300651 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600652 with open(output) as infile:
653 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600654 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600655struct dtd_test1 {
656\tfdt64_t\t\treg[2];
657};
658struct dtd_test2 {
659\tfdt64_t\t\treg[2];
660};
661struct dtd_test3 {
662\tfdt64_t\t\treg[4];
663};
664''', data)
665
Walter Lozanoa324e412020-06-25 01:10:08 -0300666 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600667 with open(output) as infile:
668 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600669 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100670static const struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600671\t.reg\t\t\t= {0x1234, 0x567800000000},
672};
673U_BOOT_DEVICE(test1) = {
674\t.name\t\t= "test1",
675\t.platdata\t= &dtv_test1,
676\t.platdata_size\t= sizeof(dtv_test1),
677};
678
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100679static const struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600680\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
681};
682U_BOOT_DEVICE(test2) = {
683\t.name\t\t= "test2",
684\t.platdata\t= &dtv_test2,
685\t.platdata_size\t= sizeof(dtv_test2),
686};
687
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100688static const struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600689\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
690};
691U_BOOT_DEVICE(test3) = {
692\t.name\t\t= "test3",
693\t.platdata\t= &dtv_test3,
694\t.platdata_size\t= sizeof(dtv_test3),
695};
696
697''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600698
699 def test_bad_reg(self):
700 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600701 # Capture stderr since dtc will emit warnings for this file
702 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600703 output = tools.GetOutputFilename('output')
704 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300705 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600706 self.assertIn("Node 'spl-test' reg property is not an int",
707 str(e.exception))
708
709 def test_bad_reg2(self):
710 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600711 # Capture stderr since dtc will emit warnings for this file
712 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600713 output = tools.GetOutputFilename('output')
714 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300715 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600716 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
717 str(e.exception))
718
719 def test_add_prop(self):
720 """Test that a subequent node can add a new property to a struct"""
721 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
722 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300723 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600724 with open(output) as infile:
725 data = infile.read()
726 self._CheckStrings(HEADER + '''
727struct dtd_sandbox_spl_test {
728\tfdt32_t\t\tintarray;
729\tfdt32_t\t\tintval;
730};
731''', data)
732
Walter Lozanoa324e412020-06-25 01:10:08 -0300733 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600734 with open(output) as infile:
735 data = infile.read()
736 self._CheckStrings(C_HEADER + '''
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100737static const struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600738\t.intval\t\t\t= 0x1,
739};
740U_BOOT_DEVICE(spl_test) = {
741\t.name\t\t= "sandbox_spl_test",
742\t.platdata\t= &dtv_spl_test,
743\t.platdata_size\t= sizeof(dtv_spl_test),
744};
745
Simon Goldschmidtba940d52019-01-21 10:07:55 +0100746static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600747\t.intarray\t\t= 0x5,
748};
749U_BOOT_DEVICE(spl_test2) = {
750\t.name\t\t= "sandbox_spl_test",
751\t.platdata\t= &dtv_spl_test2,
752\t.platdata_size\t= sizeof(dtv_spl_test2),
753};
754
755''', data)
756
757 def testStdout(self):
758 """Test output to stdout"""
759 dtb_file = get_dtb_file('dtoc_test_simple.dts')
760 with test_util.capture_sys_output() as (stdout, stderr):
Walter Lozanoa324e412020-06-25 01:10:08 -0300761 self.run_test(['struct'], dtb_file, '-')
Simon Glass961c1ce2018-07-06 10:27:35 -0600762
763 def testNoCommand(self):
764 """Test running dtoc without a command"""
765 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300766 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600767 self.assertIn("Please specify a command: struct, platdata",
768 str(e.exception))
769
770 def testBadCommand(self):
771 """Test running dtoc with an invalid command"""
772 dtb_file = get_dtb_file('dtoc_test_simple.dts')
773 output = tools.GetOutputFilename('output')
774 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300775 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600776 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
777 str(e.exception))