blob: fb65f284ceb37667997acfebb72b41ee0b8eadb8 [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
Simon Glass4e8e8462020-12-28 20:34:52 -070013import glob
Simon Glass9d2eb922017-06-18 22:09:06 -060014import os
15import struct
Walter Lozanod82062b2020-07-28 19:06:23 -030016import tempfile
Simon Glass9d2eb922017-06-18 22:09:06 -060017import unittest
18
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 Glassa60cabd2020-12-28 20:34:47 -070023from dtoc import dtb_platdata
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
Simon Glassa60cabd2020-12-28 20:34:47 -070029OUR_PATH = os.path.dirname(os.path.realpath(__file__))
Simon Glass9d2eb922017-06-18 22:09:06 -060030
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 Glassa60cabd2020-12-28 20:34:47 -070059# This is a test so is allowed to access private things in the module it is
60# testing
61# pylint: disable=W0212
Simon Glass3bce93d2018-07-06 10:27:37 -060062
63def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060064 """Compile a .dts file to a .dtb
65
66 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070067 dts_fname (str): Filename of .dts file in the current directory
68 capture_stderr (bool): True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060069
70 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070071 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060072 """
Simon Glassa60cabd2020-12-28 20:34:47 -070073 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060074 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060075
76
77class TestDtoc(unittest.TestCase):
78 """Tests for dtoc"""
79 @classmethod
80 def setUpClass(cls):
81 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -060082 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -060083
84 @classmethod
85 def tearDownClass(cls):
Simon Glassa60cabd2020-12-28 20:34:47 -070086 tools.FinaliseOutputDir()
Simon Glass9d2eb922017-06-18 22:09:06 -060087
Simon Glassa60cabd2020-12-28 20:34:47 -070088 @staticmethod
89 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -060090 """Write a string with tabs expanded as done in this Python file
91
92 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070093 fname (str): Filename to write to
94 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -060095 """
96 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -070097 with open(fname, 'w') as fout:
98 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -060099
Simon Glassa60cabd2020-12-28 20:34:47 -0700100 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-07-06 10:27:25 -0600101 """Check that a string matches its expected value
102
103 If the strings do not match, they are written to the /tmp directory in
104 the same Python format as is used here in the test. This allows for
105 easy comparison and update of the tests.
106
107 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700108 expected (str): Expected string
109 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600110 """
111 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700112 self._write_python_string('/tmp/binman.expected', expected)
113 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600114 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700115 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600116
Simon Glassa60cabd2020-12-28 20:34:47 -0700117 @staticmethod
118 def run_test(args, dtb_file, output):
119 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300120
Simon Glassa60cabd2020-12-28 20:34:47 -0700121 Args:
122 args (list of str): List of arguments for dtoc
123 dtb_file (str): Filename of .dtb file
124 output (str): Filename of output file
125 """
Simon Glass6a65d8a2020-12-28 20:34:50 -0700126 dtb_platdata.run_steps(args, dtb_file, False, output, [], True)
Walter Lozanoa324e412020-06-25 01:10:08 -0300127
Simon Glass9d2eb922017-06-18 22:09:06 -0600128 def test_name(self):
129 """Test conversion of device tree names to C identifiers"""
130 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
131 self.assertEqual('vendor_clock_frequency',
132 conv_name_to_c('vendor,clock-frequency'))
133 self.assertEqual('rockchip_rk3399_sdhci_5_1',
134 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
135
136 def test_tab_to(self):
137 """Test operation of tab_to() function"""
138 self.assertEqual('fred ', tab_to(0, 'fred'))
139 self.assertEqual('fred\t', tab_to(1, 'fred'))
140 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
141 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
142 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
143 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
144
145 def test_get_value(self):
146 """Test operation of get_value() function"""
147 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700148 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600149 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700150 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600151 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700152 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
153 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
154 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600155
156 def test_get_compat_name(self):
157 """Test operation of get_compat_name() function"""
158 Prop = collections.namedtuple('Prop', ['value'])
159 Node = collections.namedtuple('Node', ['props'])
160
161 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
162 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300163 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600164 get_compat_name(node))
165
166 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
167 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300168 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600169 get_compat_name(node))
170
171 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
172 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300173 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700174 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600175 get_compat_name(node))
176
177 def test_empty_file(self):
178 """Test output from a device tree file with no nodes"""
179 dtb_file = get_dtb_file('dtoc_test_empty.dts')
180 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300181 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600182 with open(output) as infile:
183 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700184 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600185
Walter Lozanoa324e412020-06-25 01:10:08 -0300186 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600187 with open(output) as infile:
188 lines = infile.read().splitlines()
Walter Lozanodc5b4372020-06-25 01:10:13 -0300189 self.assertEqual(C_HEADER.splitlines() + [''] +
190 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600191
Simon Glass07ee48e2020-12-28 20:34:49 -0700192 struct_text = HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600193struct dtd_sandbox_i2c_test {
194};
195struct dtd_sandbox_pmic_test {
196\tbool\t\tlow_power;
197\tfdt64_t\t\treg[2];
198};
Simon Glass9d2eb922017-06-18 22:09:06 -0600199struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600200\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600201\tbool\t\tboolval;
202\tunsigned char\tbytearray[3];
203\tunsigned char\tbyteval;
204\tfdt32_t\t\tintarray[4];
205\tfdt32_t\t\tintval;
206\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600207\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600208\tconst char *\tstringarray[3];
209\tconst char *\tstringval;
210};
Simon Glass07ee48e2020-12-28 20:34:49 -0700211'''
Simon Glass9d2eb922017-06-18 22:09:06 -0600212
Simon Glass07ee48e2020-12-28 20:34:49 -0700213 platdata_text = C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600214/* Node /i2c@0 index 0 */
215static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
216};
217U_BOOT_DEVICE(i2c_at_0) = {
218\t.name\t\t= "sandbox_i2c_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700219\t.plat\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700220\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600221\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600222};
223
224/* Node /i2c@0/pmic@9 index 1 */
225static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
226\t.low_power\t\t= true,
227\t.reg\t\t\t= {0x9, 0x0},
228};
229U_BOOT_DEVICE(pmic_at_9) = {
230\t.name\t\t= "sandbox_pmic_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700231\t.plat\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700232\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600233\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600234};
235
236/* Node /spl-test index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300237static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600238\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600239\t.bytearray\t\t= {0x6, 0x0, 0x0},
240\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600241\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600242\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600243\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
244\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600245\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600246\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600247\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600248};
249U_BOOT_DEVICE(spl_test) = {
250\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700251\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700252\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600253\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600254};
255
Simon Glass192f8132020-10-03 11:31:25 -0600256/* Node /spl-test2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300257static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600258\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600259\t.bytearray\t\t= {0x1, 0x23, 0x34},
260\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600261\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600262\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600263\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600264\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600265\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600266\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600267};
268U_BOOT_DEVICE(spl_test2) = {
269\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700270\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700271\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600272\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600273};
274
Simon Glass192f8132020-10-03 11:31:25 -0600275/* Node /spl-test3 index 4 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300276static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600277\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
278\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600279\t.stringarray\t\t= {"one", "", ""},
280};
281U_BOOT_DEVICE(spl_test3) = {
282\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700283\t.plat\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700284\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600285\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600286};
287
Simon Glass07ee48e2020-12-28 20:34:49 -0700288''' + C_EMPTY_POPULATE_PHANDLE_DATA
289
290 def test_simple(self):
291 """Test output from some simple nodes with various types of data"""
292 dtb_file = get_dtb_file('dtoc_test_simple.dts')
293 output = tools.GetOutputFilename('output')
294 self.run_test(['struct'], dtb_file, output)
295 with open(output) as infile:
296 data = infile.read()
297
298 self._check_strings(self.struct_text, data)
299
300 self.run_test(['platdata'], dtb_file, output)
301 with open(output) as infile:
302 data = infile.read()
303
304 self._check_strings(self.platdata_text, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600305
Simon Glass4e8e8462020-12-28 20:34:52 -0700306 # Try the 'all' command
307 self.run_test(['all'], dtb_file, output)
308 data = tools.ReadFile(output, binary=False)
309 self._check_strings(self.platdata_text + self.struct_text, data)
310
Walter Lozanoe675d962020-07-03 08:07:17 -0300311 def test_driver_alias(self):
312 """Test output from a device tree file with a driver alias"""
313 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
314 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300315 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300316 with open(output) as infile:
317 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700318 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300319struct dtd_sandbox_gpio {
320\tconst char *\tgpio_bank_name;
321\tbool\t\tgpio_controller;
322\tfdt32_t\t\tsandbox_gpio_count;
323};
Walter Lozanoe675d962020-07-03 08:07:17 -0300324''', data)
325
Walter Lozanoa324e412020-06-25 01:10:08 -0300326 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300327 with open(output) as infile:
328 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700329 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600330/* Node /gpios@0 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300331static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300332\t.gpio_bank_name\t\t= "a",
333\t.gpio_controller\t= true,
334\t.sandbox_gpio_count\t= 0x14,
335};
336U_BOOT_DEVICE(gpios_at_0) = {
337\t.name\t\t= "sandbox_gpio",
Simon Glass71fa5b42020-12-03 16:55:18 -0700338\t.plat\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700339\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600340\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300341};
342
Walter Lozanodc5b4372020-06-25 01:10:13 -0300343void dm_populate_phandle_data(void) {
344}
Walter Lozanoe675d962020-07-03 08:07:17 -0300345''', data)
346
Walter Lozanoa324e412020-06-25 01:10:08 -0300347 def test_invalid_driver(self):
348 """Test output from a device tree file with an invalid driver"""
349 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
350 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700351 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700352 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [])
Walter Lozanoa324e412020-06-25 01:10:08 -0300353 with open(output) as infile:
354 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700355 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300356struct dtd_invalid {
357};
358''', data)
359
Simon Glassa60cabd2020-12-28 20:34:47 -0700360 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700361 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [])
Walter Lozanoa324e412020-06-25 01:10:08 -0300362 with open(output) as infile:
363 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700364 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600365/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300366static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300367};
368U_BOOT_DEVICE(spl_test) = {
369\t.name\t\t= "invalid",
Simon Glass71fa5b42020-12-03 16:55:18 -0700370\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700371\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600372\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300373};
374
Walter Lozanodc5b4372020-06-25 01:10:13 -0300375void dm_populate_phandle_data(void) {
376}
Walter Lozanoa324e412020-06-25 01:10:08 -0300377''', data)
378
Simon Glass9d2eb922017-06-18 22:09:06 -0600379 def test_phandle(self):
380 """Test output from a node containing a phandle reference"""
381 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
382 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300383 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600384 with open(output) as infile:
385 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700386 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600387struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600388\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600389};
390struct dtd_target {
391\tfdt32_t\t\tintval;
392};
393''', data)
394
Walter Lozanoa324e412020-06-25 01:10:08 -0300395 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600396 with open(output) as infile:
397 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700398 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600399/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300400static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600401\t.intval\t\t\t= 0x1,
402};
403U_BOOT_DEVICE(phandle2_target) = {
404\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700405\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700406\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600407\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600408};
409
Simon Glass192f8132020-10-03 11:31:25 -0600410/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300411static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600412\t.intval\t\t\t= 0x2,
413};
414U_BOOT_DEVICE(phandle3_target) = {
415\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700416\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700417\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600418\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600419};
420
Simon Glass192f8132020-10-03 11:31:25 -0600421/* Node /phandle-target index 4 */
422static struct dtd_target dtv_phandle_target = {
423\t.intval\t\t\t= 0x0,
424};
425U_BOOT_DEVICE(phandle_target) = {
426\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700427\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700428\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600429\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600430};
431
432/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300433static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600434\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600435\t\t\t{4, {}},
436\t\t\t{0, {11}},
437\t\t\t{1, {12, 13}},
438\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600439};
440U_BOOT_DEVICE(phandle_source) = {
441\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700442\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700443\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600444\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600445};
446
Simon Glass192f8132020-10-03 11:31:25 -0600447/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300448static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600449\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600450\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600451};
452U_BOOT_DEVICE(phandle_source2) = {
453\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700454\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700455\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600456\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -0600457};
458
Walter Lozanodc5b4372020-06-25 01:10:13 -0300459void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300460}
Simon Glass9d2eb922017-06-18 22:09:06 -0600461''', data)
462
Simon Glass961c1ce2018-07-06 10:27:35 -0600463 def test_phandle_single(self):
464 """Test output from a node containing a phandle reference"""
465 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
466 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300467 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600468 with open(output) as infile:
469 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700470 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600471struct dtd_source {
472\tstruct phandle_0_arg clocks[1];
473};
474struct dtd_target {
475\tfdt32_t\t\tintval;
476};
477''', data)
478
479 def test_phandle_reorder(self):
480 """Test that phandle targets are generated before their references"""
481 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
482 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300483 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600484 with open(output) as infile:
485 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700486 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600487/* Node /phandle-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300488static struct dtd_target dtv_phandle_target = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600489};
490U_BOOT_DEVICE(phandle_target) = {
491\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700492\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700493\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600494\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600495};
496
Simon Glass192f8132020-10-03 11:31:25 -0600497/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300498static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600499\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600500\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -0600501};
502U_BOOT_DEVICE(phandle_source2) = {
503\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700504\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700505\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600506\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600507};
508
Walter Lozanodc5b4372020-06-25 01:10:13 -0300509void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300510}
Simon Glass961c1ce2018-07-06 10:27:35 -0600511''', data)
512
Walter Lozano5541fc02020-06-25 01:10:17 -0300513 def test_phandle_cd_gpio(self):
514 """Test that phandle targets are generated when unsing cd-gpios"""
515 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
516 output = tools.GetOutputFilename('output')
Simon Glass6a65d8a2020-12-28 20:34:50 -0700517 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [], True)
Walter Lozano5541fc02020-06-25 01:10:17 -0300518 with open(output) as infile:
519 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700520 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600521/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300522static struct dtd_target dtv_phandle2_target = {
523\t.intval\t\t\t= 0x1,
524};
525U_BOOT_DEVICE(phandle2_target) = {
526\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700527\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700528\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600529\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300530};
531
Simon Glass192f8132020-10-03 11:31:25 -0600532/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300533static struct dtd_target dtv_phandle3_target = {
534\t.intval\t\t\t= 0x2,
535};
536U_BOOT_DEVICE(phandle3_target) = {
537\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700538\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700539\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600540\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300541};
542
Simon Glass192f8132020-10-03 11:31:25 -0600543/* Node /phandle-target index 4 */
544static struct dtd_target dtv_phandle_target = {
545\t.intval\t\t\t= 0x0,
546};
547U_BOOT_DEVICE(phandle_target) = {
548\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700549\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700550\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600551\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600552};
553
554/* Node /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300555static struct dtd_source dtv_phandle_source = {
556\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600557\t\t\t{4, {}},
558\t\t\t{0, {11}},
559\t\t\t{1, {12, 13}},
560\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300561};
562U_BOOT_DEVICE(phandle_source) = {
563\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700564\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700565\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600566\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300567};
568
Simon Glass192f8132020-10-03 11:31:25 -0600569/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300570static struct dtd_source dtv_phandle_source2 = {
571\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600572\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300573};
574U_BOOT_DEVICE(phandle_source2) = {
575\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700576\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700577\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600578\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300579};
580
581void dm_populate_phandle_data(void) {
Walter Lozano5541fc02020-06-25 01:10:17 -0300582}
583''', data)
584
Simon Glass961c1ce2018-07-06 10:27:35 -0600585 def test_phandle_bad(self):
586 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600587 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
588 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600589 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700590 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300591 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600592 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -0700593 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600594
595 def test_phandle_bad2(self):
596 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600597 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
598 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600599 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700600 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300601 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -0300602 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -0700603 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600604
Simon Glass1b1fe412017-08-29 14:15:50 -0600605 def test_addresses64(self):
606 """Test output from a node with a 'reg' property with na=2, ns=2"""
607 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
608 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300609 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600610 with open(output) as infile:
611 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700612 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600613struct dtd_test1 {
614\tfdt64_t\t\treg[2];
615};
616struct dtd_test2 {
617\tfdt64_t\t\treg[2];
618};
619struct dtd_test3 {
620\tfdt64_t\t\treg[4];
621};
622''', data)
623
Walter Lozanoa324e412020-06-25 01:10:08 -0300624 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600625 with open(output) as infile:
626 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700627 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600628/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300629static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600630\t.reg\t\t\t= {0x1234, 0x5678},
631};
632U_BOOT_DEVICE(test1) = {
633\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700634\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700635\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600636\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600637};
638
Simon Glass192f8132020-10-03 11:31:25 -0600639/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300640static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600641\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
642};
643U_BOOT_DEVICE(test2) = {
644\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700645\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700646\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600647\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600648};
649
Simon Glass192f8132020-10-03 11:31:25 -0600650/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300651static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600652\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
653};
654U_BOOT_DEVICE(test3) = {
655\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700656\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700657\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600658\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600659};
660
Walter Lozanodc5b4372020-06-25 01:10:13 -0300661''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600662
663 def test_addresses32(self):
664 """Test output from a node with a 'reg' property with na=1, ns=1"""
665 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
666 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300667 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600668 with open(output) as infile:
669 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700670 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600671struct dtd_test1 {
672\tfdt32_t\t\treg[2];
673};
674struct dtd_test2 {
675\tfdt32_t\t\treg[4];
676};
677''', data)
678
Walter Lozanoa324e412020-06-25 01:10:08 -0300679 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600680 with open(output) as infile:
681 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700682 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600683/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300684static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600685\t.reg\t\t\t= {0x1234, 0x5678},
686};
687U_BOOT_DEVICE(test1) = {
688\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700689\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700690\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600691\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600692};
693
Simon Glass192f8132020-10-03 11:31:25 -0600694/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300695static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600696\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
697};
698U_BOOT_DEVICE(test2) = {
699\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700700\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700701\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600702\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600703};
704
Walter Lozanodc5b4372020-06-25 01:10:13 -0300705''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600706
707 def test_addresses64_32(self):
708 """Test output from a node with a 'reg' property with na=2, ns=1"""
709 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
710 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300711 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600712 with open(output) as infile:
713 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700714 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600715struct dtd_test1 {
716\tfdt64_t\t\treg[2];
717};
718struct dtd_test2 {
719\tfdt64_t\t\treg[2];
720};
721struct dtd_test3 {
722\tfdt64_t\t\treg[4];
723};
724''', data)
725
Walter Lozanoa324e412020-06-25 01:10:08 -0300726 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600727 with open(output) as infile:
728 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700729 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600730/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300731static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600732\t.reg\t\t\t= {0x123400000000, 0x5678},
733};
734U_BOOT_DEVICE(test1) = {
735\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700736\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700737\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600738\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600739};
740
Simon Glass192f8132020-10-03 11:31:25 -0600741/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300742static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600743\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
744};
745U_BOOT_DEVICE(test2) = {
746\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700747\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700748\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600749\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600750};
751
Simon Glass192f8132020-10-03 11:31:25 -0600752/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300753static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600754\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
755};
756U_BOOT_DEVICE(test3) = {
757\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700758\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700759\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600760\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600761};
762
Walter Lozanodc5b4372020-06-25 01:10:13 -0300763''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600764
765 def test_addresses32_64(self):
766 """Test output from a node with a 'reg' property with na=1, ns=2"""
767 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
768 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300769 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600770 with open(output) as infile:
771 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700772 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600773struct dtd_test1 {
774\tfdt64_t\t\treg[2];
775};
776struct dtd_test2 {
777\tfdt64_t\t\treg[2];
778};
779struct dtd_test3 {
780\tfdt64_t\t\treg[4];
781};
782''', data)
783
Walter Lozanoa324e412020-06-25 01:10:08 -0300784 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600785 with open(output) as infile:
786 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700787 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600788/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300789static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600790\t.reg\t\t\t= {0x1234, 0x567800000000},
791};
792U_BOOT_DEVICE(test1) = {
793\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700794\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700795\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600796\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600797};
798
Simon Glass192f8132020-10-03 11:31:25 -0600799/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300800static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600801\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
802};
803U_BOOT_DEVICE(test2) = {
804\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700805\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700806\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600807\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600808};
809
Simon Glass192f8132020-10-03 11:31:25 -0600810/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300811static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600812\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
813};
814U_BOOT_DEVICE(test3) = {
815\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700816\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700817\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600818\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600819};
820
Walter Lozanodc5b4372020-06-25 01:10:13 -0300821''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600822
823 def test_bad_reg(self):
824 """Test that a reg property with an invalid type generates an error"""
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_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600827 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700828 with self.assertRaises(ValueError) as exc:
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 is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -0700831 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600832
833 def test_bad_reg2(self):
834 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600835 # Capture stderr since dtc will emit warnings for this file
836 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600837 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700838 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300839 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -0700840 self.assertIn(
841 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
842 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600843
844 def test_add_prop(self):
845 """Test that a subequent node can add a new property to a struct"""
846 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
847 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300848 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600849 with open(output) as infile:
850 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700851 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600852struct dtd_sandbox_spl_test {
853\tfdt32_t\t\tintarray;
854\tfdt32_t\t\tintval;
855};
856''', data)
857
Walter Lozanoa324e412020-06-25 01:10:08 -0300858 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600859 with open(output) as infile:
860 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700861 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600862/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300863static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600864\t.intval\t\t\t= 0x1,
865};
866U_BOOT_DEVICE(spl_test) = {
867\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700868\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700869\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600870\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600871};
872
Simon Glass192f8132020-10-03 11:31:25 -0600873/* Node /spl-test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300874static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600875\t.intarray\t\t= 0x5,
876};
877U_BOOT_DEVICE(spl_test2) = {
878\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700879\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700880\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600881\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600882};
883
Walter Lozanodc5b4372020-06-25 01:10:13 -0300884''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600885
Simon Glassa60cabd2020-12-28 20:34:47 -0700886 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600887 """Test output to stdout"""
888 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass07ee48e2020-12-28 20:34:49 -0700889 with test_util.capture_sys_output() as (stdout, _):
Simon Glass6ca0c7a2020-12-28 20:34:48 -0700890 self.run_test(['struct'], dtb_file, None)
Simon Glass07ee48e2020-12-28 20:34:49 -0700891 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass961c1ce2018-07-06 10:27:35 -0600892
Simon Glassc3a310a82020-12-28 20:34:51 -0700893 def test_multi_to_file(self):
894 """Test output of multiple pieces to a single file"""
895 dtb_file = get_dtb_file('dtoc_test_simple.dts')
896 output = tools.GetOutputFilename('output')
Simon Glass4e8e8462020-12-28 20:34:52 -0700897 self.run_test(['all'], dtb_file, output)
Simon Glassc3a310a82020-12-28 20:34:51 -0700898 data = tools.ReadFile(output, binary=False)
Simon Glass4e8e8462020-12-28 20:34:52 -0700899 self._check_strings(self.platdata_text + self.struct_text, data)
Simon Glassc3a310a82020-12-28 20:34:51 -0700900
Simon Glassa60cabd2020-12-28 20:34:47 -0700901 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600902 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -0700903 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300904 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600905 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -0700906 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600907
Simon Glassa60cabd2020-12-28 20:34:47 -0700908 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600909 """Test running dtoc with an invalid command"""
910 dtb_file = get_dtb_file('dtoc_test_simple.dts')
911 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700912 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300913 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass4e8e8462020-12-28 20:34:52 -0700914 self.assertIn("Unknown command 'invalid-cmd': (use: platdata, struct)",
Simon Glassa60cabd2020-12-28 20:34:47 -0700915 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -0300916
Simon Glassa60cabd2020-12-28 20:34:47 -0700917 @staticmethod
918 def test_scan_drivers():
Walter Lozanod82062b2020-07-28 19:06:23 -0300919 """Test running dtoc with additional drivers to scan"""
920 dtb_file = get_dtb_file('dtoc_test_simple.dts')
921 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700922 with test_util.capture_sys_output() as _:
923 dtb_platdata.run_steps(
Simon Glass6a65d8a2020-12-28 20:34:50 -0700924 ['struct'], dtb_file, False, output, [], True,
Simon Glassa60cabd2020-12-28 20:34:47 -0700925 [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
Walter Lozanod82062b2020-07-28 19:06:23 -0300926
Simon Glassa60cabd2020-12-28 20:34:47 -0700927 @staticmethod
928 def test_unicode_error():
Walter Lozanod82062b2020-07-28 19:06:23 -0300929 """Test running dtoc with an invalid unicode file
930
931 To be able to perform this test without adding a weird text file which
932 would produce issues when using checkpatch.pl or patman, generate the
933 file at runtime and then process it.
934 """
935 dtb_file = get_dtb_file('dtoc_test_simple.dts')
936 output = tools.GetOutputFilename('output')
937 driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
Simon Glassa60cabd2020-12-28 20:34:47 -0700938 with open(driver_fn, 'wb+') as fout:
939 fout.write(b'\x81')
Walter Lozanod82062b2020-07-28 19:06:23 -0300940
Simon Glassa60cabd2020-12-28 20:34:47 -0700941 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700942 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [],
943 True, [driver_fn])
Simon Glassb42ed512020-12-23 08:11:23 -0700944
Simon Glassa60cabd2020-12-28 20:34:47 -0700945 def test_driver(self):
Simon Glassb42ed512020-12-23 08:11:23 -0700946 """Test the Driver class"""
947 drv1 = dtb_platdata.Driver('fred')
948 drv2 = dtb_platdata.Driver('mary')
949 drv3 = dtb_platdata.Driver('fred')
950 self.assertEqual("Driver(name='fred')", str(drv1))
951 self.assertEqual(drv1, drv3)
952 self.assertNotEqual(drv1, drv2)
953 self.assertNotEqual(drv2, drv3)
Simon Glass4e8e8462020-12-28 20:34:52 -0700954
955 def test_output_conflict(self):
956 """Test a conflict between and output dirs and output file"""
957 with self.assertRaises(ValueError) as exc:
958 dtb_platdata.run_steps(['all'], None, False, 'out', ['cdir'], True)
959 self.assertIn("Must specify either output or output_dirs, not both",
960 str(exc.exception))
961
962 def test_output_dirs(self):
963 """Test outputting files to a directory"""
964 # Remove the directory so that files from other tests are not there
965 tools._RemoveOutputDir()
966 tools.PrepareOutputDir(None)
967
968 # This should create the .dts and .dtb in the output directory
969 dtb_file = get_dtb_file('dtoc_test_simple.dts')
970 outdir = tools.GetOutputDir()
971 fnames = glob.glob(outdir + '/*')
972 self.assertEqual(2, len(fnames))
973
974 dtb_platdata.run_steps(['all'], dtb_file, False, None, [outdir], True)
975 fnames = glob.glob(outdir + '/*')
976 self.assertEqual(4, len(fnames))
977
978 leafs = set(os.path.basename(fname) for fname in fnames)
979 self.assertEqual(
980 {'dt-structs-gen.h', 'source.dts', 'dt-platdata.c', 'source.dtb'},
981 leafs)