blob: 4bf90444e146216071603509e22cdbe85f1987d4 [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 Lozanod82062b2020-07-28 19:06:23 -030015import tempfile
Simon Glass9d2eb922017-06-18 22:09:06 -060016import unittest
17
Simon Glass9d2eb922017-06-18 22:09:06 -060018from dtb_platdata import conv_name_to_c
19from dtb_platdata import get_compat_name
20from dtb_platdata import get_value
21from dtb_platdata import tab_to
Simon Glassa60cabd2020-12-28 20:34:47 -070022from dtoc import dtb_platdata
Simon Glassa997ea52020-04-17 18:09:04 -060023from dtoc import fdt
24from dtoc import fdt_util
25from patman import test_util
26from patman import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060027
Simon Glassa60cabd2020-12-28 20:34:47 -070028OUR_PATH = os.path.dirname(os.path.realpath(__file__))
Simon Glass9d2eb922017-06-18 22:09:06 -060029
30
Simon Glasseb37e2d2017-11-12 21:52:17 -070031HEADER = '''/*
32 * DO NOT MODIFY
33 *
34 * This file was generated by dtoc from a .dtb (device tree binary) file.
35 */
36
37#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090038#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070039
40C_HEADER = '''/*
41 * DO NOT MODIFY
42 *
43 * This file was generated by dtoc from a .dtb (device tree binary) file.
44 */
45
Simon Glass4c73d7b2020-10-03 11:31:41 -060046/* Allow use of U_BOOT_DEVICE() in this file */
47#define DT_PLATDATA_C
48
Simon Glasseb37e2d2017-11-12 21:52:17 -070049#include <common.h>
50#include <dm.h>
51#include <dt-structs.h>
52'''
53
Walter Lozanodc5b4372020-06-25 01:10:13 -030054C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
55}
56'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070057
Simon Glassa60cabd2020-12-28 20:34:47 -070058# This is a test so is allowed to access private things in the module it is
59# testing
60# pylint: disable=W0212
Simon Glass3bce93d2018-07-06 10:27:37 -060061
62def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060063 """Compile a .dts file to a .dtb
64
65 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070066 dts_fname (str): Filename of .dts file in the current directory
67 capture_stderr (bool): True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060068
69 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070070 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060071 """
Simon Glassa60cabd2020-12-28 20:34:47 -070072 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060073 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060074
75
76class TestDtoc(unittest.TestCase):
77 """Tests for dtoc"""
78 @classmethod
79 def setUpClass(cls):
80 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -060081 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -060082
83 @classmethod
84 def tearDownClass(cls):
Simon Glassa60cabd2020-12-28 20:34:47 -070085 tools.FinaliseOutputDir()
Simon Glass9d2eb922017-06-18 22:09:06 -060086
Simon Glassa60cabd2020-12-28 20:34:47 -070087 @staticmethod
88 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -060089 """Write a string with tabs expanded as done in this Python file
90
91 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070092 fname (str): Filename to write to
93 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -060094 """
95 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -070096 with open(fname, 'w') as fout:
97 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -060098
Simon Glassa60cabd2020-12-28 20:34:47 -070099 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-07-06 10:27:25 -0600100 """Check that a string matches its expected value
101
102 If the strings do not match, they are written to the /tmp directory in
103 the same Python format as is used here in the test. This allows for
104 easy comparison and update of the tests.
105
106 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700107 expected (str): Expected string
108 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600109 """
110 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700111 self._write_python_string('/tmp/binman.expected', expected)
112 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600113 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700114 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600115
Simon Glassa60cabd2020-12-28 20:34:47 -0700116 @staticmethod
117 def run_test(args, dtb_file, output):
118 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300119
Simon Glassa60cabd2020-12-28 20:34:47 -0700120 Args:
121 args (list of str): List of arguments for dtoc
122 dtb_file (str): Filename of .dtb file
123 output (str): Filename of output file
124 """
Walter Lozanoa324e412020-06-25 01:10:08 -0300125 dtb_platdata.run_steps(args, dtb_file, False, output, True)
126
Simon Glass9d2eb922017-06-18 22:09:06 -0600127 def test_name(self):
128 """Test conversion of device tree names to C identifiers"""
129 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
130 self.assertEqual('vendor_clock_frequency',
131 conv_name_to_c('vendor,clock-frequency'))
132 self.assertEqual('rockchip_rk3399_sdhci_5_1',
133 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
134
135 def test_tab_to(self):
136 """Test operation of tab_to() function"""
137 self.assertEqual('fred ', tab_to(0, 'fred'))
138 self.assertEqual('fred\t', tab_to(1, 'fred'))
139 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
140 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
141 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
142 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
143
144 def test_get_value(self):
145 """Test operation of get_value() function"""
146 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700147 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600148 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700149 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600150 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700151 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
152 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
153 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600154
155 def test_get_compat_name(self):
156 """Test operation of get_compat_name() function"""
157 Prop = collections.namedtuple('Prop', ['value'])
158 Node = collections.namedtuple('Node', ['props'])
159
160 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
161 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300162 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600163 get_compat_name(node))
164
165 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
166 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300167 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600168 get_compat_name(node))
169
170 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
171 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300172 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700173 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600174 get_compat_name(node))
175
176 def test_empty_file(self):
177 """Test output from a device tree file with no nodes"""
178 dtb_file = get_dtb_file('dtoc_test_empty.dts')
179 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300180 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600181 with open(output) as infile:
182 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700183 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600184
Walter Lozanoa324e412020-06-25 01:10:08 -0300185 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600186 with open(output) as infile:
187 lines = infile.read().splitlines()
Walter Lozanodc5b4372020-06-25 01:10:13 -0300188 self.assertEqual(C_HEADER.splitlines() + [''] +
189 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600190
191 def test_simple(self):
192 """Test output from some simple nodes with various types of data"""
193 dtb_file = get_dtb_file('dtoc_test_simple.dts')
194 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300195 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600196 with open(output) as infile:
197 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700198 self._check_strings(HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600199struct dtd_sandbox_i2c_test {
200};
201struct dtd_sandbox_pmic_test {
202\tbool\t\tlow_power;
203\tfdt64_t\t\treg[2];
204};
Simon Glass9d2eb922017-06-18 22:09:06 -0600205struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600206\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600207\tbool\t\tboolval;
208\tunsigned char\tbytearray[3];
209\tunsigned char\tbyteval;
210\tfdt32_t\t\tintarray[4];
211\tfdt32_t\t\tintval;
212\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600213\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600214\tconst char *\tstringarray[3];
215\tconst char *\tstringval;
216};
Simon Glass9d2eb922017-06-18 22:09:06 -0600217''', data)
218
Walter Lozanoa324e412020-06-25 01:10:08 -0300219 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600220 with open(output) as infile:
221 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700222 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600223/* Node /i2c@0 index 0 */
224static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
225};
226U_BOOT_DEVICE(i2c_at_0) = {
227\t.name\t\t= "sandbox_i2c_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700228\t.plat\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700229\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600230\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600231};
232
233/* Node /i2c@0/pmic@9 index 1 */
234static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
235\t.low_power\t\t= true,
236\t.reg\t\t\t= {0x9, 0x0},
237};
238U_BOOT_DEVICE(pmic_at_9) = {
239\t.name\t\t= "sandbox_pmic_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700240\t.plat\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700241\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600242\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600243};
244
245/* Node /spl-test index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300246static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600247\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600248\t.bytearray\t\t= {0x6, 0x0, 0x0},
249\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600250\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600251\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600252\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
253\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600254\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600255\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600256\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600257};
258U_BOOT_DEVICE(spl_test) = {
259\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700260\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700261\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600262\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600263};
264
Simon Glass192f8132020-10-03 11:31:25 -0600265/* Node /spl-test2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300266static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600267\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600268\t.bytearray\t\t= {0x1, 0x23, 0x34},
269\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600270\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600271\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600272\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600273\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600274\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600275\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600276};
277U_BOOT_DEVICE(spl_test2) = {
278\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700279\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700280\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600281\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600282};
283
Simon Glass192f8132020-10-03 11:31:25 -0600284/* Node /spl-test3 index 4 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300285static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600286\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
287\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600288\t.stringarray\t\t= {"one", "", ""},
289};
290U_BOOT_DEVICE(spl_test3) = {
291\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700292\t.plat\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700293\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600294\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600295};
296
Walter Lozanodc5b4372020-06-25 01:10:13 -0300297''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600298
Walter Lozanoe675d962020-07-03 08:07:17 -0300299 def test_driver_alias(self):
300 """Test output from a device tree file with a driver alias"""
301 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
302 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300303 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300304 with open(output) as infile:
305 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700306 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300307struct dtd_sandbox_gpio {
308\tconst char *\tgpio_bank_name;
309\tbool\t\tgpio_controller;
310\tfdt32_t\t\tsandbox_gpio_count;
311};
Walter Lozanoe675d962020-07-03 08:07:17 -0300312''', data)
313
Walter Lozanoa324e412020-06-25 01:10:08 -0300314 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300315 with open(output) as infile:
316 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700317 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600318/* Node /gpios@0 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300319static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300320\t.gpio_bank_name\t\t= "a",
321\t.gpio_controller\t= true,
322\t.sandbox_gpio_count\t= 0x14,
323};
324U_BOOT_DEVICE(gpios_at_0) = {
325\t.name\t\t= "sandbox_gpio",
Simon Glass71fa5b42020-12-03 16:55:18 -0700326\t.plat\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700327\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600328\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300329};
330
Walter Lozanodc5b4372020-06-25 01:10:13 -0300331void dm_populate_phandle_data(void) {
332}
Walter Lozanoe675d962020-07-03 08:07:17 -0300333''', data)
334
Walter Lozanoa324e412020-06-25 01:10:08 -0300335 def test_invalid_driver(self):
336 """Test output from a device tree file with an invalid driver"""
337 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
338 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700339 with test_util.capture_sys_output() as _:
Walter Lozanoa324e412020-06-25 01:10:08 -0300340 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
341 with open(output) as infile:
342 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700343 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300344struct dtd_invalid {
345};
346''', data)
347
Simon Glassa60cabd2020-12-28 20:34:47 -0700348 with test_util.capture_sys_output() as _:
Walter Lozanoa324e412020-06-25 01:10:08 -0300349 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
350 with open(output) as infile:
351 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700352 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600353/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300354static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300355};
356U_BOOT_DEVICE(spl_test) = {
357\t.name\t\t= "invalid",
Simon Glass71fa5b42020-12-03 16:55:18 -0700358\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700359\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600360\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300361};
362
Walter Lozanodc5b4372020-06-25 01:10:13 -0300363void dm_populate_phandle_data(void) {
364}
Walter Lozanoa324e412020-06-25 01:10:08 -0300365''', data)
366
Simon Glass9d2eb922017-06-18 22:09:06 -0600367 def test_phandle(self):
368 """Test output from a node containing a phandle reference"""
369 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
370 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300371 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600372 with open(output) as infile:
373 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700374 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600375struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600376\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600377};
378struct dtd_target {
379\tfdt32_t\t\tintval;
380};
381''', data)
382
Walter Lozanoa324e412020-06-25 01:10:08 -0300383 self.run_test(['platdata'], 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(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600387/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300388static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600389\t.intval\t\t\t= 0x1,
390};
391U_BOOT_DEVICE(phandle2_target) = {
392\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700393\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700394\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600395\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600396};
397
Simon Glass192f8132020-10-03 11:31:25 -0600398/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300399static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600400\t.intval\t\t\t= 0x2,
401};
402U_BOOT_DEVICE(phandle3_target) = {
403\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700404\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700405\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600406\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600407};
408
Simon Glass192f8132020-10-03 11:31:25 -0600409/* Node /phandle-target index 4 */
410static struct dtd_target dtv_phandle_target = {
411\t.intval\t\t\t= 0x0,
412};
413U_BOOT_DEVICE(phandle_target) = {
414\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700415\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700416\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600417\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600418};
419
420/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300421static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600422\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600423\t\t\t{4, {}},
424\t\t\t{0, {11}},
425\t\t\t{1, {12, 13}},
426\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600427};
428U_BOOT_DEVICE(phandle_source) = {
429\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700430\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700431\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600432\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600433};
434
Simon Glass192f8132020-10-03 11:31:25 -0600435/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300436static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600437\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600438\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600439};
440U_BOOT_DEVICE(phandle_source2) = {
441\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700442\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700443\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600444\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -0600445};
446
Walter Lozanodc5b4372020-06-25 01:10:13 -0300447void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300448}
Simon Glass9d2eb922017-06-18 22:09:06 -0600449''', data)
450
Simon Glass961c1ce2018-07-06 10:27:35 -0600451 def test_phandle_single(self):
452 """Test output from a node containing a phandle reference"""
453 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
454 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300455 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600456 with open(output) as infile:
457 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700458 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600459struct dtd_source {
460\tstruct phandle_0_arg clocks[1];
461};
462struct dtd_target {
463\tfdt32_t\t\tintval;
464};
465''', data)
466
467 def test_phandle_reorder(self):
468 """Test that phandle targets are generated before their references"""
469 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
470 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300471 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600472 with open(output) as infile:
473 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700474 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600475/* Node /phandle-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300476static struct dtd_target dtv_phandle_target = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600477};
478U_BOOT_DEVICE(phandle_target) = {
479\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700480\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700481\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600482\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600483};
484
Simon Glass192f8132020-10-03 11:31:25 -0600485/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300486static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600487\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600488\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -0600489};
490U_BOOT_DEVICE(phandle_source2) = {
491\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700492\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700493\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600494\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600495};
496
Walter Lozanodc5b4372020-06-25 01:10:13 -0300497void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300498}
Simon Glass961c1ce2018-07-06 10:27:35 -0600499''', data)
500
Walter Lozano5541fc02020-06-25 01:10:17 -0300501 def test_phandle_cd_gpio(self):
502 """Test that phandle targets are generated when unsing cd-gpios"""
503 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
504 output = tools.GetOutputFilename('output')
505 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True)
506 with open(output) as infile:
507 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700508 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600509/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300510static struct dtd_target dtv_phandle2_target = {
511\t.intval\t\t\t= 0x1,
512};
513U_BOOT_DEVICE(phandle2_target) = {
514\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700515\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700516\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600517\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300518};
519
Simon Glass192f8132020-10-03 11:31:25 -0600520/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300521static struct dtd_target dtv_phandle3_target = {
522\t.intval\t\t\t= 0x2,
523};
524U_BOOT_DEVICE(phandle3_target) = {
525\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700526\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700527\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600528\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300529};
530
Simon Glass192f8132020-10-03 11:31:25 -0600531/* Node /phandle-target index 4 */
532static struct dtd_target dtv_phandle_target = {
533\t.intval\t\t\t= 0x0,
534};
535U_BOOT_DEVICE(phandle_target) = {
536\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700537\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700538\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600539\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600540};
541
542/* Node /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300543static struct dtd_source dtv_phandle_source = {
544\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600545\t\t\t{4, {}},
546\t\t\t{0, {11}},
547\t\t\t{1, {12, 13}},
548\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300549};
550U_BOOT_DEVICE(phandle_source) = {
551\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700552\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700553\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600554\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300555};
556
Simon Glass192f8132020-10-03 11:31:25 -0600557/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300558static struct dtd_source dtv_phandle_source2 = {
559\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600560\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300561};
562U_BOOT_DEVICE(phandle_source2) = {
563\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700564\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700565\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600566\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300567};
568
569void dm_populate_phandle_data(void) {
Walter Lozano5541fc02020-06-25 01:10:17 -0300570}
571''', data)
572
Simon Glass961c1ce2018-07-06 10:27:35 -0600573 def test_phandle_bad(self):
574 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600575 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
576 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600577 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700578 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300579 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600580 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -0700581 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600582
583 def test_phandle_bad2(self):
584 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600585 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
586 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600587 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700588 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300589 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -0300590 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -0700591 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600592
Simon Glass1b1fe412017-08-29 14:15:50 -0600593 def test_addresses64(self):
594 """Test output from a node with a 'reg' property with na=2, ns=2"""
595 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
596 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300597 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600598 with open(output) as infile:
599 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700600 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600601struct dtd_test1 {
602\tfdt64_t\t\treg[2];
603};
604struct dtd_test2 {
605\tfdt64_t\t\treg[2];
606};
607struct dtd_test3 {
608\tfdt64_t\t\treg[4];
609};
610''', data)
611
Walter Lozanoa324e412020-06-25 01:10:08 -0300612 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600613 with open(output) as infile:
614 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700615 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600616/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300617static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600618\t.reg\t\t\t= {0x1234, 0x5678},
619};
620U_BOOT_DEVICE(test1) = {
621\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700622\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700623\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600624\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600625};
626
Simon Glass192f8132020-10-03 11:31:25 -0600627/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300628static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600629\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
630};
631U_BOOT_DEVICE(test2) = {
632\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700633\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700634\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600635\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600636};
637
Simon Glass192f8132020-10-03 11:31:25 -0600638/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300639static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600640\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
641};
642U_BOOT_DEVICE(test3) = {
643\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700644\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700645\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600646\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600647};
648
Walter Lozanodc5b4372020-06-25 01:10:13 -0300649''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600650
651 def test_addresses32(self):
652 """Test output from a node with a 'reg' property with na=1, ns=1"""
653 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
654 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300655 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600656 with open(output) as infile:
657 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700658 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600659struct dtd_test1 {
660\tfdt32_t\t\treg[2];
661};
662struct dtd_test2 {
663\tfdt32_t\t\treg[4];
664};
665''', data)
666
Walter Lozanoa324e412020-06-25 01:10:08 -0300667 self.run_test(['platdata'], 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(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600671/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300672static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600673\t.reg\t\t\t= {0x1234, 0x5678},
674};
675U_BOOT_DEVICE(test1) = {
676\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700677\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700678\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600679\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600680};
681
Simon Glass192f8132020-10-03 11:31:25 -0600682/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300683static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600684\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
685};
686U_BOOT_DEVICE(test2) = {
687\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700688\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700689\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600690\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600691};
692
Walter Lozanodc5b4372020-06-25 01:10:13 -0300693''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600694
695 def test_addresses64_32(self):
696 """Test output from a node with a 'reg' property with na=2, ns=1"""
697 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
698 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300699 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600700 with open(output) as infile:
701 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700702 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600703struct dtd_test1 {
704\tfdt64_t\t\treg[2];
705};
706struct dtd_test2 {
707\tfdt64_t\t\treg[2];
708};
709struct dtd_test3 {
710\tfdt64_t\t\treg[4];
711};
712''', data)
713
Walter Lozanoa324e412020-06-25 01:10:08 -0300714 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600715 with open(output) as infile:
716 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700717 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600718/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300719static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600720\t.reg\t\t\t= {0x123400000000, 0x5678},
721};
722U_BOOT_DEVICE(test1) = {
723\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700724\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700725\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600726\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600727};
728
Simon Glass192f8132020-10-03 11:31:25 -0600729/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300730static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600731\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
732};
733U_BOOT_DEVICE(test2) = {
734\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700735\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700736\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600737\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600738};
739
Simon Glass192f8132020-10-03 11:31:25 -0600740/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300741static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600742\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
743};
744U_BOOT_DEVICE(test3) = {
745\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700746\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700747\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600748\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600749};
750
Walter Lozanodc5b4372020-06-25 01:10:13 -0300751''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600752
753 def test_addresses32_64(self):
754 """Test output from a node with a 'reg' property with na=1, ns=2"""
755 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
756 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300757 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600758 with open(output) as infile:
759 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700760 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600761struct dtd_test1 {
762\tfdt64_t\t\treg[2];
763};
764struct dtd_test2 {
765\tfdt64_t\t\treg[2];
766};
767struct dtd_test3 {
768\tfdt64_t\t\treg[4];
769};
770''', data)
771
Walter Lozanoa324e412020-06-25 01:10:08 -0300772 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600773 with open(output) as infile:
774 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700775 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600776/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300777static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600778\t.reg\t\t\t= {0x1234, 0x567800000000},
779};
780U_BOOT_DEVICE(test1) = {
781\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700782\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700783\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600784\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600785};
786
Simon Glass192f8132020-10-03 11:31:25 -0600787/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300788static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600789\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
790};
791U_BOOT_DEVICE(test2) = {
792\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700793\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700794\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600795\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600796};
797
Simon Glass192f8132020-10-03 11:31:25 -0600798/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300799static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600800\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
801};
802U_BOOT_DEVICE(test3) = {
803\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700804\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700805\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600806\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600807};
808
Walter Lozanodc5b4372020-06-25 01:10:13 -0300809''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600810
811 def test_bad_reg(self):
812 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600813 # Capture stderr since dtc will emit warnings for this file
814 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600815 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700816 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300817 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600818 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -0700819 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600820
821 def test_bad_reg2(self):
822 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600823 # Capture stderr since dtc will emit warnings for this file
824 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600825 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700826 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300827 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -0700828 self.assertIn(
829 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
830 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600831
832 def test_add_prop(self):
833 """Test that a subequent node can add a new property to a struct"""
834 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
835 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300836 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600837 with open(output) as infile:
838 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700839 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600840struct dtd_sandbox_spl_test {
841\tfdt32_t\t\tintarray;
842\tfdt32_t\t\tintval;
843};
844''', data)
845
Walter Lozanoa324e412020-06-25 01:10:08 -0300846 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600847 with open(output) as infile:
848 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700849 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600850/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300851static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600852\t.intval\t\t\t= 0x1,
853};
854U_BOOT_DEVICE(spl_test) = {
855\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700856\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700857\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600858\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600859};
860
Simon Glass192f8132020-10-03 11:31:25 -0600861/* Node /spl-test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300862static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600863\t.intarray\t\t= 0x5,
864};
865U_BOOT_DEVICE(spl_test2) = {
866\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700867\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700868\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600869\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600870};
871
Walter Lozanodc5b4372020-06-25 01:10:13 -0300872''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600873
Simon Glassa60cabd2020-12-28 20:34:47 -0700874 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600875 """Test output to stdout"""
876 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassa60cabd2020-12-28 20:34:47 -0700877 with test_util.capture_sys_output() as _:
Walter Lozanoa324e412020-06-25 01:10:08 -0300878 self.run_test(['struct'], dtb_file, '-')
Simon Glass961c1ce2018-07-06 10:27:35 -0600879
Simon Glassa60cabd2020-12-28 20:34:47 -0700880 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600881 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -0700882 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300883 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600884 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -0700885 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600886
Simon Glassa60cabd2020-12-28 20:34:47 -0700887 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600888 """Test running dtoc with an invalid command"""
889 dtb_file = get_dtb_file('dtoc_test_simple.dts')
890 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700891 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300892 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600893 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
Simon Glassa60cabd2020-12-28 20:34:47 -0700894 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -0300895
Simon Glassa60cabd2020-12-28 20:34:47 -0700896 @staticmethod
897 def test_scan_drivers():
Walter Lozanod82062b2020-07-28 19:06:23 -0300898 """Test running dtoc with additional drivers to scan"""
899 dtb_file = get_dtb_file('dtoc_test_simple.dts')
900 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700901 with test_util.capture_sys_output() as _:
902 dtb_platdata.run_steps(
903 ['struct'], dtb_file, False, output, True,
904 [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
Walter Lozanod82062b2020-07-28 19:06:23 -0300905
Simon Glassa60cabd2020-12-28 20:34:47 -0700906 @staticmethod
907 def test_unicode_error():
Walter Lozanod82062b2020-07-28 19:06:23 -0300908 """Test running dtoc with an invalid unicode file
909
910 To be able to perform this test without adding a weird text file which
911 would produce issues when using checkpatch.pl or patman, generate the
912 file at runtime and then process it.
913 """
914 dtb_file = get_dtb_file('dtoc_test_simple.dts')
915 output = tools.GetOutputFilename('output')
916 driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
Simon Glassa60cabd2020-12-28 20:34:47 -0700917 with open(driver_fn, 'wb+') as fout:
918 fout.write(b'\x81')
Walter Lozanod82062b2020-07-28 19:06:23 -0300919
Simon Glassa60cabd2020-12-28 20:34:47 -0700920 with test_util.capture_sys_output() as _:
Walter Lozanod82062b2020-07-28 19:06:23 -0300921 dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
Simon Glassa60cabd2020-12-28 20:34:47 -0700922 [driver_fn])
Simon Glassb42ed512020-12-23 08:11:23 -0700923
Simon Glassa60cabd2020-12-28 20:34:47 -0700924 def test_driver(self):
Simon Glassb42ed512020-12-23 08:11:23 -0700925 """Test the Driver class"""
926 drv1 = dtb_platdata.Driver('fred')
927 drv2 = dtb_platdata.Driver('mary')
928 drv3 = dtb_platdata.Driver('fred')
929 self.assertEqual("Driver(name='fred')", str(drv1))
930 self.assertEqual(drv1, drv3)
931 self.assertNotEqual(drv1, drv2)
932 self.assertNotEqual(drv2, drv3)