blob: a5836e04b7a2c7e5853aa35e59a6cec4d175a784 [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
Walter Lozanod82062b2020-07-28 19:06:23 -030016import tempfile
Simon Glass9d2eb922017-06-18 22:09:06 -060017import unittest
18
Simon Glassa997ea52020-04-17 18:09:04 -060019from dtoc import dtb_platdata
Simon Glass9d2eb922017-06-18 22:09:06 -060020from dtb_platdata import conv_name_to_c
21from dtb_platdata import get_compat_name
22from dtb_platdata import get_value
23from dtb_platdata import tab_to
Simon Glassa997ea52020-04-17 18:09:04 -060024from dtoc import fdt
25from dtoc import fdt_util
26from patman import test_util
27from patman import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060028
29our_path = os.path.dirname(os.path.realpath(__file__))
30
31
Simon Glasseb37e2d2017-11-12 21:52:17 -070032HEADER = '''/*
33 * DO NOT MODIFY
34 *
35 * This file was generated by dtoc from a .dtb (device tree binary) file.
36 */
37
38#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090039#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070040
41C_HEADER = '''/*
42 * DO NOT MODIFY
43 *
44 * This file was generated by dtoc from a .dtb (device tree binary) file.
45 */
46
Simon Glass4c73d7b2020-10-03 11:31:41 -060047/* Allow use of U_BOOT_DEVICE() in this file */
48#define DT_PLATDATA_C
49
Simon Glasseb37e2d2017-11-12 21:52:17 -070050#include <common.h>
51#include <dm.h>
52#include <dt-structs.h>
53'''
54
Walter Lozanodc5b4372020-06-25 01:10:13 -030055C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
56}
57'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070058
Simon Glass3bce93d2018-07-06 10:27:37 -060059
60def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060061 """Compile a .dts file to a .dtb
62
63 Args:
64 dts_fname: Filename of .dts file in the current directory
Simon Glass3bce93d2018-07-06 10:27:37 -060065 capture_stderr: True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060066
67 Returns:
68 Filename of compiled file in output directory
69 """
Simon Glass3bce93d2018-07-06 10:27:37 -060070 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
71 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060072
73
74class TestDtoc(unittest.TestCase):
75 """Tests for dtoc"""
76 @classmethod
77 def setUpClass(cls):
78 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -060079 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -060080
81 @classmethod
82 def tearDownClass(cls):
83 tools._RemoveOutputDir()
84
Simon Glassc47c2b32018-07-06 10:27:25 -060085 def _WritePythonString(self, fname, data):
86 """Write a string with tabs expanded as done in this Python file
87
88 Args:
89 fname: Filename to write to
90 data: Raw string to convert
91 """
92 data = data.replace('\t', '\\t')
93 with open(fname, 'w') as fd:
94 fd.write(data)
95
96 def _CheckStrings(self, expected, actual):
97 """Check that a string matches its expected value
98
99 If the strings do not match, they are written to the /tmp directory in
100 the same Python format as is used here in the test. This allows for
101 easy comparison and update of the tests.
102
103 Args:
104 expected: Expected string
105 actual: Actual string
106 """
107 if expected != actual:
108 self._WritePythonString('/tmp/binman.expected', expected)
109 self._WritePythonString('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600110 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassc47c2b32018-07-06 10:27:25 -0600111 self.assertEquals(expected, actual)
112
Walter Lozanoa324e412020-06-25 01:10:08 -0300113
114 def run_test(self, args, dtb_file, output):
115 dtb_platdata.run_steps(args, dtb_file, False, output, True)
116
Simon Glass9d2eb922017-06-18 22:09:06 -0600117 def test_name(self):
118 """Test conversion of device tree names to C identifiers"""
119 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
120 self.assertEqual('vendor_clock_frequency',
121 conv_name_to_c('vendor,clock-frequency'))
122 self.assertEqual('rockchip_rk3399_sdhci_5_1',
123 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
124
125 def test_tab_to(self):
126 """Test operation of tab_to() function"""
127 self.assertEqual('fred ', tab_to(0, 'fred'))
128 self.assertEqual('fred\t', tab_to(1, 'fred'))
129 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
130 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
131 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
132 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
133
134 def test_get_value(self):
135 """Test operation of get_value() function"""
136 self.assertEqual('0x45',
137 get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
138 self.assertEqual('0x45',
139 get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
140 self.assertEqual('0x0',
141 get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
142 self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
143 self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
144
145 def test_get_compat_name(self):
146 """Test operation of get_compat_name() function"""
147 Prop = collections.namedtuple('Prop', ['value'])
148 Node = collections.namedtuple('Node', ['props'])
149
150 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
151 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300152 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600153 get_compat_name(node))
154
155 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
156 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300157 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600158 get_compat_name(node))
159
160 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
161 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300162 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
163 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600164 get_compat_name(node))
165
166 def test_empty_file(self):
167 """Test output from a device tree file with no nodes"""
168 dtb_file = get_dtb_file('dtoc_test_empty.dts')
169 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300170 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600171 with open(output) as infile:
172 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700173 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600174
Walter Lozanoa324e412020-06-25 01:10:08 -0300175 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600176 with open(output) as infile:
177 lines = infile.read().splitlines()
Walter Lozanodc5b4372020-06-25 01:10:13 -0300178 self.assertEqual(C_HEADER.splitlines() + [''] +
179 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600180
181 def test_simple(self):
182 """Test output from some simple nodes with various types of data"""
183 dtb_file = get_dtb_file('dtoc_test_simple.dts')
184 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300185 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600186 with open(output) as infile:
187 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600188 self._CheckStrings(HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600189struct dtd_sandbox_i2c_test {
190};
191struct dtd_sandbox_pmic_test {
192\tbool\t\tlow_power;
193\tfdt64_t\t\treg[2];
194};
Simon Glass9d2eb922017-06-18 22:09:06 -0600195struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600196\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600197\tbool\t\tboolval;
198\tunsigned char\tbytearray[3];
199\tunsigned char\tbyteval;
200\tfdt32_t\t\tintarray[4];
201\tfdt32_t\t\tintval;
202\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600203\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600204\tconst char *\tstringarray[3];
205\tconst char *\tstringval;
206};
207struct dtd_sandbox_spl_test_2 {
208};
209''', data)
210
Walter Lozanoa324e412020-06-25 01:10:08 -0300211 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600212 with open(output) as infile:
213 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600214 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600215/* Node /i2c@0 index 0 */
216static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
217};
218U_BOOT_DEVICE(i2c_at_0) = {
219\t.name\t\t= "sandbox_i2c_test",
220\t.platdata\t= &dtv_i2c_at_0,
221\t.platdata_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600222\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600223};
224
225/* Node /i2c@0/pmic@9 index 1 */
226static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
227\t.low_power\t\t= true,
228\t.reg\t\t\t= {0x9, 0x0},
229};
230U_BOOT_DEVICE(pmic_at_9) = {
231\t.name\t\t= "sandbox_pmic_test",
232\t.platdata\t= &dtv_pmic_at_9,
233\t.platdata_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600234\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600235};
236
237/* Node /spl-test index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300238static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600239\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600240\t.bytearray\t\t= {0x6, 0x0, 0x0},
241\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600242\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600243\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600244\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
245\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600246\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600247\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600248\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600249};
250U_BOOT_DEVICE(spl_test) = {
251\t.name\t\t= "sandbox_spl_test",
252\t.platdata\t= &dtv_spl_test,
253\t.platdata_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600254\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600255};
256
Simon Glass192f8132020-10-03 11:31:25 -0600257/* Node /spl-test2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300258static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600259\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600260\t.bytearray\t\t= {0x1, 0x23, 0x34},
261\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600262\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600263\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600264\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600265\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600266\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600267\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600268};
269U_BOOT_DEVICE(spl_test2) = {
270\t.name\t\t= "sandbox_spl_test",
271\t.platdata\t= &dtv_spl_test2,
272\t.platdata_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600273\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600274};
275
Simon Glass192f8132020-10-03 11:31:25 -0600276/* Node /spl-test3 index 4 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300277static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600278\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
279\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600280\t.stringarray\t\t= {"one", "", ""},
281};
282U_BOOT_DEVICE(spl_test3) = {
283\t.name\t\t= "sandbox_spl_test",
284\t.platdata\t= &dtv_spl_test3,
285\t.platdata_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600286\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600287};
288
Simon Glass192f8132020-10-03 11:31:25 -0600289/* Node /spl-test4 index 5 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300290static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600291};
292U_BOOT_DEVICE(spl_test4) = {
293\t.name\t\t= "sandbox_spl_test_2",
294\t.platdata\t= &dtv_spl_test4,
295\t.platdata_size\t= sizeof(dtv_spl_test4),
Simon Glass36b15e22020-10-03 11:31:35 -0600296\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600297};
298
Walter Lozanodc5b4372020-06-25 01:10:13 -0300299''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600300
Walter Lozanoe675d962020-07-03 08:07:17 -0300301 def test_driver_alias(self):
302 """Test output from a device tree file with a driver alias"""
303 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
304 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300305 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300306 with open(output) as infile:
307 data = infile.read()
308 self._CheckStrings(HEADER + '''
309struct dtd_sandbox_gpio {
310\tconst char *\tgpio_bank_name;
311\tbool\t\tgpio_controller;
312\tfdt32_t\t\tsandbox_gpio_count;
313};
Walter Lozanoe675d962020-07-03 08:07:17 -0300314''', data)
315
Walter Lozanoa324e412020-06-25 01:10:08 -0300316 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300317 with open(output) as infile:
318 data = infile.read()
319 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600320/* Node /gpios@0 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300321static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300322\t.gpio_bank_name\t\t= "a",
323\t.gpio_controller\t= true,
324\t.sandbox_gpio_count\t= 0x14,
325};
326U_BOOT_DEVICE(gpios_at_0) = {
327\t.name\t\t= "sandbox_gpio",
328\t.platdata\t= &dtv_gpios_at_0,
329\t.platdata_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600330\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300331};
332
Walter Lozanodc5b4372020-06-25 01:10:13 -0300333void dm_populate_phandle_data(void) {
334}
Walter Lozanoe675d962020-07-03 08:07:17 -0300335''', data)
336
Walter Lozanoa324e412020-06-25 01:10:08 -0300337 def test_invalid_driver(self):
338 """Test output from a device tree file with an invalid driver"""
339 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
340 output = tools.GetOutputFilename('output')
341 with test_util.capture_sys_output() as (stdout, stderr):
342 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
343 with open(output) as infile:
344 data = infile.read()
345 self._CheckStrings(HEADER + '''
346struct dtd_invalid {
347};
348''', data)
349
350 with test_util.capture_sys_output() as (stdout, stderr):
351 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
352 with open(output) as infile:
353 data = infile.read()
354 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600355/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300356static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300357};
358U_BOOT_DEVICE(spl_test) = {
359\t.name\t\t= "invalid",
360\t.platdata\t= &dtv_spl_test,
361\t.platdata_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600362\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300363};
364
Walter Lozanodc5b4372020-06-25 01:10:13 -0300365void dm_populate_phandle_data(void) {
366}
Walter Lozanoa324e412020-06-25 01:10:08 -0300367''', data)
368
Simon Glass9d2eb922017-06-18 22:09:06 -0600369 def test_phandle(self):
370 """Test output from a node containing a phandle reference"""
371 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
372 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300373 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600374 with open(output) as infile:
375 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600376 self._CheckStrings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600377struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600378\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600379};
380struct dtd_target {
381\tfdt32_t\t\tintval;
382};
383''', data)
384
Walter Lozanoa324e412020-06-25 01:10:08 -0300385 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600386 with open(output) as infile:
387 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600388 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600389/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300390static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600391\t.intval\t\t\t= 0x1,
392};
393U_BOOT_DEVICE(phandle2_target) = {
394\t.name\t\t= "target",
395\t.platdata\t= &dtv_phandle2_target,
396\t.platdata_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600397\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600398};
399
Simon Glass192f8132020-10-03 11:31:25 -0600400/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300401static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600402\t.intval\t\t\t= 0x2,
403};
404U_BOOT_DEVICE(phandle3_target) = {
405\t.name\t\t= "target",
406\t.platdata\t= &dtv_phandle3_target,
407\t.platdata_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600408\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600409};
410
Simon Glass192f8132020-10-03 11:31:25 -0600411/* Node /phandle-target index 4 */
412static struct dtd_target dtv_phandle_target = {
413\t.intval\t\t\t= 0x0,
414};
415U_BOOT_DEVICE(phandle_target) = {
416\t.name\t\t= "target",
417\t.platdata\t= &dtv_phandle_target,
418\t.platdata_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600419\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600420};
421
422/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300423static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600424\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600425\t\t\t{4, {}},
426\t\t\t{0, {11}},
427\t\t\t{1, {12, 13}},
428\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600429};
430U_BOOT_DEVICE(phandle_source) = {
431\t.name\t\t= "source",
432\t.platdata\t= &dtv_phandle_source,
433\t.platdata_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600434\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600435};
436
Simon Glass192f8132020-10-03 11:31:25 -0600437/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300438static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600439\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600440\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600441};
442U_BOOT_DEVICE(phandle_source2) = {
443\t.name\t\t= "source",
444\t.platdata\t= &dtv_phandle_source2,
445\t.platdata_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600446\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -0600447};
448
Walter Lozanodc5b4372020-06-25 01:10:13 -0300449void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300450}
Simon Glass9d2eb922017-06-18 22:09:06 -0600451''', data)
452
Simon Glass961c1ce2018-07-06 10:27:35 -0600453 def test_phandle_single(self):
454 """Test output from a node containing a phandle reference"""
455 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
456 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300457 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600458 with open(output) as infile:
459 data = infile.read()
460 self._CheckStrings(HEADER + '''
461struct dtd_source {
462\tstruct phandle_0_arg clocks[1];
463};
464struct dtd_target {
465\tfdt32_t\t\tintval;
466};
467''', data)
468
469 def test_phandle_reorder(self):
470 """Test that phandle targets are generated before their references"""
471 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
472 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300473 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600474 with open(output) as infile:
475 data = infile.read()
476 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600477/* Node /phandle-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300478static struct dtd_target dtv_phandle_target = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600479};
480U_BOOT_DEVICE(phandle_target) = {
481\t.name\t\t= "target",
482\t.platdata\t= &dtv_phandle_target,
483\t.platdata_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600484\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600485};
486
Simon Glass192f8132020-10-03 11:31:25 -0600487/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300488static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600489\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600490\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -0600491};
492U_BOOT_DEVICE(phandle_source2) = {
493\t.name\t\t= "source",
494\t.platdata\t= &dtv_phandle_source2,
495\t.platdata_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600496\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600497};
498
Walter Lozanodc5b4372020-06-25 01:10:13 -0300499void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300500}
Simon Glass961c1ce2018-07-06 10:27:35 -0600501''', data)
502
Walter Lozano5541fc02020-06-25 01:10:17 -0300503 def test_phandle_cd_gpio(self):
504 """Test that phandle targets are generated when unsing cd-gpios"""
505 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
506 output = tools.GetOutputFilename('output')
507 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True)
508 with open(output) as infile:
509 data = infile.read()
510 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600511/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300512static struct dtd_target dtv_phandle2_target = {
513\t.intval\t\t\t= 0x1,
514};
515U_BOOT_DEVICE(phandle2_target) = {
516\t.name\t\t= "target",
517\t.platdata\t= &dtv_phandle2_target,
518\t.platdata_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600519\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300520};
521
Simon Glass192f8132020-10-03 11:31:25 -0600522/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300523static struct dtd_target dtv_phandle3_target = {
524\t.intval\t\t\t= 0x2,
525};
526U_BOOT_DEVICE(phandle3_target) = {
527\t.name\t\t= "target",
528\t.platdata\t= &dtv_phandle3_target,
529\t.platdata_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600530\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300531};
532
Simon Glass192f8132020-10-03 11:31:25 -0600533/* Node /phandle-target index 4 */
534static struct dtd_target dtv_phandle_target = {
535\t.intval\t\t\t= 0x0,
536};
537U_BOOT_DEVICE(phandle_target) = {
538\t.name\t\t= "target",
539\t.platdata\t= &dtv_phandle_target,
540\t.platdata_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600541\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600542};
543
544/* Node /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300545static struct dtd_source dtv_phandle_source = {
546\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600547\t\t\t{4, {}},
548\t\t\t{0, {11}},
549\t\t\t{1, {12, 13}},
550\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300551};
552U_BOOT_DEVICE(phandle_source) = {
553\t.name\t\t= "source",
554\t.platdata\t= &dtv_phandle_source,
555\t.platdata_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600556\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300557};
558
Simon Glass192f8132020-10-03 11:31:25 -0600559/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300560static struct dtd_source dtv_phandle_source2 = {
561\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600562\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300563};
564U_BOOT_DEVICE(phandle_source2) = {
565\t.name\t\t= "source",
566\t.platdata\t= &dtv_phandle_source2,
567\t.platdata_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600568\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300569};
570
571void dm_populate_phandle_data(void) {
Walter Lozano5541fc02020-06-25 01:10:17 -0300572}
573''', data)
574
Simon Glass961c1ce2018-07-06 10:27:35 -0600575 def test_phandle_bad(self):
576 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600577 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
578 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600579 output = tools.GetOutputFilename('output')
580 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300581 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600582 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
583 str(e.exception))
584
585 def test_phandle_bad2(self):
586 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600587 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
588 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600589 output = tools.GetOutputFilename('output')
590 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300591 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -0300592 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glass961c1ce2018-07-06 10:27:35 -0600593 str(e.exception))
594
Simon Glass1b1fe412017-08-29 14:15:50 -0600595 def test_addresses64(self):
596 """Test output from a node with a 'reg' property with na=2, ns=2"""
597 dtb_file = get_dtb_file('dtoc_test_addr64.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 Glass192f8132020-10-03 11:31:25 -0600618/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300619static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600620\t.reg\t\t\t= {0x1234, 0x5678},
621};
622U_BOOT_DEVICE(test1) = {
623\t.name\t\t= "test1",
624\t.platdata\t= &dtv_test1,
625\t.platdata_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600626\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600627};
628
Simon Glass192f8132020-10-03 11:31:25 -0600629/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300630static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600631\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
632};
633U_BOOT_DEVICE(test2) = {
634\t.name\t\t= "test2",
635\t.platdata\t= &dtv_test2,
636\t.platdata_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600637\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600638};
639
Simon Glass192f8132020-10-03 11:31:25 -0600640/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300641static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600642\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
643};
644U_BOOT_DEVICE(test3) = {
645\t.name\t\t= "test3",
646\t.platdata\t= &dtv_test3,
647\t.platdata_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600648\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600649};
650
Walter Lozanodc5b4372020-06-25 01:10:13 -0300651''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600652
653 def test_addresses32(self):
654 """Test output from a node with a 'reg' property with na=1, ns=1"""
655 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
656 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300657 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600658 with open(output) as infile:
659 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600660 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600661struct dtd_test1 {
662\tfdt32_t\t\treg[2];
663};
664struct dtd_test2 {
665\tfdt32_t\t\treg[4];
666};
667''', data)
668
Walter Lozanoa324e412020-06-25 01:10:08 -0300669 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600670 with open(output) as infile:
671 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600672 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600673/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300674static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600675\t.reg\t\t\t= {0x1234, 0x5678},
676};
677U_BOOT_DEVICE(test1) = {
678\t.name\t\t= "test1",
679\t.platdata\t= &dtv_test1,
680\t.platdata_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600681\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600682};
683
Simon Glass192f8132020-10-03 11:31:25 -0600684/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300685static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600686\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
687};
688U_BOOT_DEVICE(test2) = {
689\t.name\t\t= "test2",
690\t.platdata\t= &dtv_test2,
691\t.platdata_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600692\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600693};
694
Walter Lozanodc5b4372020-06-25 01:10:13 -0300695''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600696
697 def test_addresses64_32(self):
698 """Test output from a node with a 'reg' property with na=2, ns=1"""
699 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
700 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300701 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600702 with open(output) as infile:
703 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600704 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600705struct dtd_test1 {
706\tfdt64_t\t\treg[2];
707};
708struct dtd_test2 {
709\tfdt64_t\t\treg[2];
710};
711struct dtd_test3 {
712\tfdt64_t\t\treg[4];
713};
714''', data)
715
Walter Lozanoa324e412020-06-25 01:10:08 -0300716 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600717 with open(output) as infile:
718 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600719 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600720/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300721static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600722\t.reg\t\t\t= {0x123400000000, 0x5678},
723};
724U_BOOT_DEVICE(test1) = {
725\t.name\t\t= "test1",
726\t.platdata\t= &dtv_test1,
727\t.platdata_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600728\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600729};
730
Simon Glass192f8132020-10-03 11:31:25 -0600731/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300732static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600733\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
734};
735U_BOOT_DEVICE(test2) = {
736\t.name\t\t= "test2",
737\t.platdata\t= &dtv_test2,
738\t.platdata_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600739\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600740};
741
Simon Glass192f8132020-10-03 11:31:25 -0600742/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300743static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600744\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
745};
746U_BOOT_DEVICE(test3) = {
747\t.name\t\t= "test3",
748\t.platdata\t= &dtv_test3,
749\t.platdata_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600750\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600751};
752
Walter Lozanodc5b4372020-06-25 01:10:13 -0300753''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600754
755 def test_addresses32_64(self):
756 """Test output from a node with a 'reg' property with na=1, ns=2"""
757 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
758 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300759 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600760 with open(output) as infile:
761 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600762 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600763struct dtd_test1 {
764\tfdt64_t\t\treg[2];
765};
766struct dtd_test2 {
767\tfdt64_t\t\treg[2];
768};
769struct dtd_test3 {
770\tfdt64_t\t\treg[4];
771};
772''', data)
773
Walter Lozanoa324e412020-06-25 01:10:08 -0300774 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600775 with open(output) as infile:
776 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600777 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600778/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300779static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600780\t.reg\t\t\t= {0x1234, 0x567800000000},
781};
782U_BOOT_DEVICE(test1) = {
783\t.name\t\t= "test1",
784\t.platdata\t= &dtv_test1,
785\t.platdata_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600786\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600787};
788
Simon Glass192f8132020-10-03 11:31:25 -0600789/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300790static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600791\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
792};
793U_BOOT_DEVICE(test2) = {
794\t.name\t\t= "test2",
795\t.platdata\t= &dtv_test2,
796\t.platdata_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600797\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600798};
799
Simon Glass192f8132020-10-03 11:31:25 -0600800/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300801static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600802\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
803};
804U_BOOT_DEVICE(test3) = {
805\t.name\t\t= "test3",
806\t.platdata\t= &dtv_test3,
807\t.platdata_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600808\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600809};
810
Walter Lozanodc5b4372020-06-25 01:10:13 -0300811''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600812
813 def test_bad_reg(self):
814 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600815 # Capture stderr since dtc will emit warnings for this file
816 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600817 output = tools.GetOutputFilename('output')
818 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300819 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600820 self.assertIn("Node 'spl-test' reg property is not an int",
821 str(e.exception))
822
823 def test_bad_reg2(self):
824 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600825 # Capture stderr since dtc will emit warnings for this file
826 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600827 output = tools.GetOutputFilename('output')
828 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300829 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600830 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
831 str(e.exception))
832
833 def test_add_prop(self):
834 """Test that a subequent node can add a new property to a struct"""
835 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
836 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300837 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600838 with open(output) as infile:
839 data = infile.read()
840 self._CheckStrings(HEADER + '''
841struct dtd_sandbox_spl_test {
842\tfdt32_t\t\tintarray;
843\tfdt32_t\t\tintval;
844};
845''', data)
846
Walter Lozanoa324e412020-06-25 01:10:08 -0300847 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600848 with open(output) as infile:
849 data = infile.read()
850 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600851/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300852static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600853\t.intval\t\t\t= 0x1,
854};
855U_BOOT_DEVICE(spl_test) = {
856\t.name\t\t= "sandbox_spl_test",
857\t.platdata\t= &dtv_spl_test,
858\t.platdata_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600859\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600860};
861
Simon Glass192f8132020-10-03 11:31:25 -0600862/* Node /spl-test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300863static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600864\t.intarray\t\t= 0x5,
865};
866U_BOOT_DEVICE(spl_test2) = {
867\t.name\t\t= "sandbox_spl_test",
868\t.platdata\t= &dtv_spl_test2,
869\t.platdata_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600870\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600871};
872
Walter Lozanodc5b4372020-06-25 01:10:13 -0300873''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600874
875 def testStdout(self):
876 """Test output to stdout"""
877 dtb_file = get_dtb_file('dtoc_test_simple.dts')
878 with test_util.capture_sys_output() as (stdout, stderr):
Walter Lozanoa324e412020-06-25 01:10:08 -0300879 self.run_test(['struct'], dtb_file, '-')
Simon Glass961c1ce2018-07-06 10:27:35 -0600880
881 def testNoCommand(self):
882 """Test running dtoc without a command"""
883 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300884 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600885 self.assertIn("Please specify a command: struct, platdata",
886 str(e.exception))
887
888 def testBadCommand(self):
889 """Test running dtoc with an invalid command"""
890 dtb_file = get_dtb_file('dtoc_test_simple.dts')
891 output = tools.GetOutputFilename('output')
892 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300893 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600894 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
895 str(e.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -0300896
897 def testScanDrivers(self):
898 """Test running dtoc with additional drivers to scan"""
899 dtb_file = get_dtb_file('dtoc_test_simple.dts')
900 output = tools.GetOutputFilename('output')
901 with test_util.capture_sys_output() as (stdout, stderr):
902 dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
903 [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
904
905 def testUnicodeError(self):
906 """Test running dtoc with an invalid unicode file
907
908 To be able to perform this test without adding a weird text file which
909 would produce issues when using checkpatch.pl or patman, generate the
910 file at runtime and then process it.
911 """
912 dtb_file = get_dtb_file('dtoc_test_simple.dts')
913 output = tools.GetOutputFilename('output')
914 driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
915 with open(driver_fn, 'wb+') as df:
916 df.write(b'\x81')
917
918 with test_util.capture_sys_output() as (stdout, stderr):
919 dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
920 [driver_fn])