blob: 2e4dd2b24d2e2784ae9ca542f6ff2a967bf90bb7 [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 *
Simon Glass6b208842020-12-28 20:35:00 -070035 * Defines the structs used to hold devicetree data.
36 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glasseb37e2d2017-11-12 21:52:17 -070037 */
38
39#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090040#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070041
42C_HEADER = '''/*
43 * DO NOT MODIFY
44 *
Simon Glass6b208842020-12-28 20:35:00 -070045 * Declares the U_BOOT_DRIVER() records and platform data.
46 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glasseb37e2d2017-11-12 21:52:17 -070047 */
48
Simon Glass1d8364a2020-12-28 20:34:54 -070049/* Allow use of U_BOOT_DRVINFO() in this file */
Simon Glass4c73d7b2020-10-03 11:31:41 -060050#define DT_PLATDATA_C
51
Simon Glasseb37e2d2017-11-12 21:52:17 -070052#include <common.h>
53#include <dm.h>
54#include <dt-structs.h>
55'''
56
Walter Lozanodc5b4372020-06-25 01:10:13 -030057C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
58}
59'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070060
Simon Glassa60cabd2020-12-28 20:34:47 -070061# This is a test so is allowed to access private things in the module it is
62# testing
63# pylint: disable=W0212
Simon Glass3bce93d2018-07-06 10:27:37 -060064
65def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060066 """Compile a .dts file to a .dtb
67
68 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070069 dts_fname (str): Filename of .dts file in the current directory
70 capture_stderr (bool): True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060071
72 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070073 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060074 """
Simon Glassa60cabd2020-12-28 20:34:47 -070075 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060076 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060077
78
79class TestDtoc(unittest.TestCase):
80 """Tests for dtoc"""
81 @classmethod
82 def setUpClass(cls):
83 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -060084 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -060085
86 @classmethod
87 def tearDownClass(cls):
Simon Glassa60cabd2020-12-28 20:34:47 -070088 tools.FinaliseOutputDir()
Simon Glass9d2eb922017-06-18 22:09:06 -060089
Simon Glassa60cabd2020-12-28 20:34:47 -070090 @staticmethod
91 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -060092 """Write a string with tabs expanded as done in this Python file
93
94 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070095 fname (str): Filename to write to
96 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -060097 """
98 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -070099 with open(fname, 'w') as fout:
100 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -0600101
Simon Glassa60cabd2020-12-28 20:34:47 -0700102 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-07-06 10:27:25 -0600103 """Check that a string matches its expected value
104
105 If the strings do not match, they are written to the /tmp directory in
106 the same Python format as is used here in the test. This allows for
107 easy comparison and update of the tests.
108
109 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700110 expected (str): Expected string
111 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600112 """
113 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700114 self._write_python_string('/tmp/binman.expected', expected)
115 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600116 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700117 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600118
Simon Glassa60cabd2020-12-28 20:34:47 -0700119 @staticmethod
120 def run_test(args, dtb_file, output):
121 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300122
Simon Glassa60cabd2020-12-28 20:34:47 -0700123 Args:
124 args (list of str): List of arguments for dtoc
125 dtb_file (str): Filename of .dtb file
126 output (str): Filename of output file
127 """
Simon Glass6a65d8a2020-12-28 20:34:50 -0700128 dtb_platdata.run_steps(args, dtb_file, False, output, [], True)
Walter Lozanoa324e412020-06-25 01:10:08 -0300129
Simon Glass9d2eb922017-06-18 22:09:06 -0600130 def test_name(self):
131 """Test conversion of device tree names to C identifiers"""
132 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
133 self.assertEqual('vendor_clock_frequency',
134 conv_name_to_c('vendor,clock-frequency'))
135 self.assertEqual('rockchip_rk3399_sdhci_5_1',
136 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
137
138 def test_tab_to(self):
139 """Test operation of tab_to() function"""
140 self.assertEqual('fred ', tab_to(0, 'fred'))
141 self.assertEqual('fred\t', tab_to(1, 'fred'))
142 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
143 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
144 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
145 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
146
147 def test_get_value(self):
148 """Test operation of get_value() function"""
149 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700150 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600151 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700152 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600153 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700154 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
155 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
156 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600157
158 def test_get_compat_name(self):
159 """Test operation of get_compat_name() function"""
160 Prop = collections.namedtuple('Prop', ['value'])
161 Node = collections.namedtuple('Node', ['props'])
162
163 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
164 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300165 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600166 get_compat_name(node))
167
168 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
169 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300170 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600171 get_compat_name(node))
172
173 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
174 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300175 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700176 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600177 get_compat_name(node))
178
179 def test_empty_file(self):
180 """Test output from a device tree file with no nodes"""
181 dtb_file = get_dtb_file('dtoc_test_empty.dts')
182 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300183 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600184 with open(output) as infile:
185 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700186 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600187
Walter Lozanoa324e412020-06-25 01:10:08 -0300188 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600189 with open(output) as infile:
190 lines = infile.read().splitlines()
Walter Lozanodc5b4372020-06-25 01:10:13 -0300191 self.assertEqual(C_HEADER.splitlines() + [''] +
192 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600193
Simon Glass07ee48e2020-12-28 20:34:49 -0700194 struct_text = HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600195struct dtd_sandbox_i2c_test {
196};
197struct dtd_sandbox_pmic_test {
198\tbool\t\tlow_power;
199\tfdt64_t\t\treg[2];
200};
Simon Glass9d2eb922017-06-18 22:09:06 -0600201struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600202\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600203\tbool\t\tboolval;
204\tunsigned char\tbytearray[3];
205\tunsigned char\tbyteval;
206\tfdt32_t\t\tintarray[4];
207\tfdt32_t\t\tintval;
208\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600209\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600210\tconst char *\tstringarray[3];
211\tconst char *\tstringval;
212};
Simon Glass07ee48e2020-12-28 20:34:49 -0700213'''
Simon Glass9d2eb922017-06-18 22:09:06 -0600214
Simon Glass07ee48e2020-12-28 20:34:49 -0700215 platdata_text = C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600216/* Node /i2c@0 index 0 */
217static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
218};
Simon Glass1d8364a2020-12-28 20:34:54 -0700219U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glass192f8132020-10-03 11:31:25 -0600220\t.name\t\t= "sandbox_i2c_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700221\t.plat\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700222\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600223\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600224};
225
226/* Node /i2c@0/pmic@9 index 1 */
227static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
228\t.low_power\t\t= true,
229\t.reg\t\t\t= {0x9, 0x0},
230};
Simon Glass1d8364a2020-12-28 20:34:54 -0700231U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glass192f8132020-10-03 11:31:25 -0600232\t.name\t\t= "sandbox_pmic_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700233\t.plat\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700234\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600235\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600236};
237
238/* Node /spl-test index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300239static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600240\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600241\t.bytearray\t\t= {0x6, 0x0, 0x0},
242\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600243\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600244\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600245\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
246\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600247\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600248\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600249\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600250};
Simon Glass1d8364a2020-12-28 20:34:54 -0700251U_BOOT_DRVINFO(spl_test) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600252\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700253\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700254\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600255\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600256};
257
Simon Glass192f8132020-10-03 11:31:25 -0600258/* Node /spl-test2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300259static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600260\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600261\t.bytearray\t\t= {0x1, 0x23, 0x34},
262\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600263\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600264\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600265\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600266\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600267\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600268\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600269};
Simon Glass1d8364a2020-12-28 20:34:54 -0700270U_BOOT_DRVINFO(spl_test2) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600271\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700272\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700273\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600274\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600275};
276
Simon Glass192f8132020-10-03 11:31:25 -0600277/* Node /spl-test3 index 4 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300278static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600279\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
280\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600281\t.stringarray\t\t= {"one", "", ""},
282};
Simon Glass1d8364a2020-12-28 20:34:54 -0700283U_BOOT_DRVINFO(spl_test3) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600284\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700285\t.plat\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700286\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600287\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600288};
289
Simon Glass07ee48e2020-12-28 20:34:49 -0700290''' + C_EMPTY_POPULATE_PHANDLE_DATA
291
292 def test_simple(self):
293 """Test output from some simple nodes with various types of data"""
294 dtb_file = get_dtb_file('dtoc_test_simple.dts')
295 output = tools.GetOutputFilename('output')
296 self.run_test(['struct'], dtb_file, output)
297 with open(output) as infile:
298 data = infile.read()
299
300 self._check_strings(self.struct_text, data)
301
302 self.run_test(['platdata'], dtb_file, output)
303 with open(output) as infile:
304 data = infile.read()
305
306 self._check_strings(self.platdata_text, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600307
Simon Glass4e8e8462020-12-28 20:34:52 -0700308 # Try the 'all' command
309 self.run_test(['all'], dtb_file, output)
310 data = tools.ReadFile(output, binary=False)
311 self._check_strings(self.platdata_text + self.struct_text, data)
312
Walter Lozanoe675d962020-07-03 08:07:17 -0300313 def test_driver_alias(self):
314 """Test output from a device tree file with a driver alias"""
315 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
316 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300317 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300318 with open(output) as infile:
319 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700320 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300321struct dtd_sandbox_gpio {
322\tconst char *\tgpio_bank_name;
323\tbool\t\tgpio_controller;
324\tfdt32_t\t\tsandbox_gpio_count;
325};
Walter Lozanoe675d962020-07-03 08:07:17 -0300326''', data)
327
Walter Lozanoa324e412020-06-25 01:10:08 -0300328 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300329 with open(output) as infile:
330 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700331 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600332/* Node /gpios@0 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300333static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300334\t.gpio_bank_name\t\t= "a",
335\t.gpio_controller\t= true,
336\t.sandbox_gpio_count\t= 0x14,
337};
Simon Glass1d8364a2020-12-28 20:34:54 -0700338U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300339\t.name\t\t= "sandbox_gpio",
Simon Glass71fa5b42020-12-03 16:55:18 -0700340\t.plat\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700341\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600342\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300343};
344
Walter Lozanodc5b4372020-06-25 01:10:13 -0300345void dm_populate_phandle_data(void) {
346}
Walter Lozanoe675d962020-07-03 08:07:17 -0300347''', data)
348
Walter Lozanoa324e412020-06-25 01:10:08 -0300349 def test_invalid_driver(self):
350 """Test output from a device tree file with an invalid driver"""
351 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
352 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700353 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700354 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [])
Walter Lozanoa324e412020-06-25 01:10:08 -0300355 with open(output) as infile:
356 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700357 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300358struct dtd_invalid {
359};
360''', data)
361
Simon Glassa60cabd2020-12-28 20:34:47 -0700362 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700363 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [])
Walter Lozanoa324e412020-06-25 01:10:08 -0300364 with open(output) as infile:
365 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700366 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600367/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300368static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300369};
Simon Glass1d8364a2020-12-28 20:34:54 -0700370U_BOOT_DRVINFO(spl_test) = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300371\t.name\t\t= "invalid",
Simon Glass71fa5b42020-12-03 16:55:18 -0700372\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700373\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600374\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300375};
376
Walter Lozanodc5b4372020-06-25 01:10:13 -0300377void dm_populate_phandle_data(void) {
378}
Walter Lozanoa324e412020-06-25 01:10:08 -0300379''', data)
380
Simon Glass9d2eb922017-06-18 22:09:06 -0600381 def test_phandle(self):
382 """Test output from a node containing a phandle reference"""
383 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
384 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300385 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600386 with open(output) as infile:
387 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700388 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600389struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600390\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600391};
392struct dtd_target {
393\tfdt32_t\t\tintval;
394};
395''', data)
396
Walter Lozanoa324e412020-06-25 01:10:08 -0300397 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600398 with open(output) as infile:
399 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700400 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600401/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300402static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600403\t.intval\t\t\t= 0x1,
404};
Simon Glass1d8364a2020-12-28 20:34:54 -0700405U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600406\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700407\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700408\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600409\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600410};
411
Simon Glass192f8132020-10-03 11:31:25 -0600412/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300413static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600414\t.intval\t\t\t= 0x2,
415};
Simon Glass1d8364a2020-12-28 20:34:54 -0700416U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600417\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700418\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700419\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600420\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600421};
422
Simon Glass192f8132020-10-03 11:31:25 -0600423/* Node /phandle-target index 4 */
424static struct dtd_target dtv_phandle_target = {
425\t.intval\t\t\t= 0x0,
426};
Simon Glass1d8364a2020-12-28 20:34:54 -0700427U_BOOT_DRVINFO(phandle_target) = {
Simon Glass192f8132020-10-03 11:31:25 -0600428\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700429\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700430\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600431\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600432};
433
434/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300435static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600436\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600437\t\t\t{4, {}},
438\t\t\t{0, {11}},
439\t\t\t{1, {12, 13}},
440\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600441};
Simon Glass1d8364a2020-12-28 20:34:54 -0700442U_BOOT_DRVINFO(phandle_source) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600443\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700444\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700445\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600446\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600447};
448
Simon Glass192f8132020-10-03 11:31:25 -0600449/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300450static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600451\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600452\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600453};
Simon Glass1d8364a2020-12-28 20:34:54 -0700454U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass609e2b12018-07-06 10:27:31 -0600455\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700456\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700457\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600458\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -0600459};
460
Walter Lozanodc5b4372020-06-25 01:10:13 -0300461void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300462}
Simon Glass9d2eb922017-06-18 22:09:06 -0600463''', data)
464
Simon Glass961c1ce2018-07-06 10:27:35 -0600465 def test_phandle_single(self):
466 """Test output from a node containing a phandle reference"""
467 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
468 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300469 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600470 with open(output) as infile:
471 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700472 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600473struct dtd_source {
474\tstruct phandle_0_arg clocks[1];
475};
476struct dtd_target {
477\tfdt32_t\t\tintval;
478};
479''', data)
480
481 def test_phandle_reorder(self):
482 """Test that phandle targets are generated before their references"""
483 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
484 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300485 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600486 with open(output) as infile:
487 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700488 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600489/* Node /phandle-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300490static struct dtd_target dtv_phandle_target = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600491};
Simon Glass1d8364a2020-12-28 20:34:54 -0700492U_BOOT_DRVINFO(phandle_target) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600493\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700494\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700495\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600496\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600497};
498
Simon Glass192f8132020-10-03 11:31:25 -0600499/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300500static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600501\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600502\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -0600503};
Simon Glass1d8364a2020-12-28 20:34:54 -0700504U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600505\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700506\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700507\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600508\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600509};
510
Walter Lozanodc5b4372020-06-25 01:10:13 -0300511void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300512}
Simon Glass961c1ce2018-07-06 10:27:35 -0600513''', data)
514
Walter Lozano5541fc02020-06-25 01:10:17 -0300515 def test_phandle_cd_gpio(self):
516 """Test that phandle targets are generated when unsing cd-gpios"""
517 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
518 output = tools.GetOutputFilename('output')
Simon Glass6a65d8a2020-12-28 20:34:50 -0700519 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [], True)
Walter Lozano5541fc02020-06-25 01:10:17 -0300520 with open(output) as infile:
521 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700522 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600523/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300524static struct dtd_target dtv_phandle2_target = {
525\t.intval\t\t\t= 0x1,
526};
Simon Glass1d8364a2020-12-28 20:34:54 -0700527U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300528\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700529\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700530\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600531\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300532};
533
Simon Glass192f8132020-10-03 11:31:25 -0600534/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300535static struct dtd_target dtv_phandle3_target = {
536\t.intval\t\t\t= 0x2,
537};
Simon Glass1d8364a2020-12-28 20:34:54 -0700538U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300539\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700540\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700541\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600542\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300543};
544
Simon Glass192f8132020-10-03 11:31:25 -0600545/* Node /phandle-target index 4 */
546static struct dtd_target dtv_phandle_target = {
547\t.intval\t\t\t= 0x0,
548};
Simon Glass1d8364a2020-12-28 20:34:54 -0700549U_BOOT_DRVINFO(phandle_target) = {
Simon Glass192f8132020-10-03 11:31:25 -0600550\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700551\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700552\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600553\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600554};
555
556/* Node /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300557static struct dtd_source dtv_phandle_source = {
558\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600559\t\t\t{4, {}},
560\t\t\t{0, {11}},
561\t\t\t{1, {12, 13}},
562\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300563};
Simon Glass1d8364a2020-12-28 20:34:54 -0700564U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300565\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700566\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700567\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600568\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300569};
570
Simon Glass192f8132020-10-03 11:31:25 -0600571/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300572static struct dtd_source dtv_phandle_source2 = {
573\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600574\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300575};
Simon Glass1d8364a2020-12-28 20:34:54 -0700576U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300577\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700578\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700579\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600580\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300581};
582
583void dm_populate_phandle_data(void) {
Walter Lozano5541fc02020-06-25 01:10:17 -0300584}
585''', data)
586
Simon Glass961c1ce2018-07-06 10:27:35 -0600587 def test_phandle_bad(self):
588 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600589 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
590 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600591 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700592 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300593 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600594 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -0700595 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600596
597 def test_phandle_bad2(self):
598 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600599 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
600 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600601 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700602 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300603 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -0300604 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -0700605 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600606
Simon Glass1b1fe412017-08-29 14:15:50 -0600607 def test_addresses64(self):
608 """Test output from a node with a 'reg' property with na=2, ns=2"""
609 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
610 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300611 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600612 with open(output) as infile:
613 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700614 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600615struct dtd_test1 {
616\tfdt64_t\t\treg[2];
617};
618struct dtd_test2 {
619\tfdt64_t\t\treg[2];
620};
621struct dtd_test3 {
622\tfdt64_t\t\treg[4];
623};
624''', data)
625
Walter Lozanoa324e412020-06-25 01:10:08 -0300626 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600627 with open(output) as infile:
628 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700629 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600630/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300631static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600632\t.reg\t\t\t= {0x1234, 0x5678},
633};
Simon Glass1d8364a2020-12-28 20:34:54 -0700634U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600635\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700636\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700637\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600638\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600639};
640
Simon Glass192f8132020-10-03 11:31:25 -0600641/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300642static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600643\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
644};
Simon Glass1d8364a2020-12-28 20:34:54 -0700645U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600646\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700647\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700648\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600649\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600650};
651
Simon Glass192f8132020-10-03 11:31:25 -0600652/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300653static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600654\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
655};
Simon Glass1d8364a2020-12-28 20:34:54 -0700656U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600657\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700658\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700659\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600660\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600661};
662
Walter Lozanodc5b4372020-06-25 01:10:13 -0300663''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600664
665 def test_addresses32(self):
666 """Test output from a node with a 'reg' property with na=1, ns=1"""
667 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
668 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300669 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600670 with open(output) as infile:
671 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700672 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600673struct dtd_test1 {
674\tfdt32_t\t\treg[2];
675};
676struct dtd_test2 {
677\tfdt32_t\t\treg[4];
678};
679''', data)
680
Walter Lozanoa324e412020-06-25 01:10:08 -0300681 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600682 with open(output) as infile:
683 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700684 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600685/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300686static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600687\t.reg\t\t\t= {0x1234, 0x5678},
688};
Simon Glass1d8364a2020-12-28 20:34:54 -0700689U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600690\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700691\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700692\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600693\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600694};
695
Simon Glass192f8132020-10-03 11:31:25 -0600696/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300697static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600698\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
699};
Simon Glass1d8364a2020-12-28 20:34:54 -0700700U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600701\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700702\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700703\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600704\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600705};
706
Walter Lozanodc5b4372020-06-25 01:10:13 -0300707''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600708
709 def test_addresses64_32(self):
710 """Test output from a node with a 'reg' property with na=2, ns=1"""
711 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
712 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300713 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600714 with open(output) as infile:
715 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700716 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600717struct dtd_test1 {
718\tfdt64_t\t\treg[2];
719};
720struct dtd_test2 {
721\tfdt64_t\t\treg[2];
722};
723struct dtd_test3 {
724\tfdt64_t\t\treg[4];
725};
726''', data)
727
Walter Lozanoa324e412020-06-25 01:10:08 -0300728 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600729 with open(output) as infile:
730 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700731 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600732/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300733static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600734\t.reg\t\t\t= {0x123400000000, 0x5678},
735};
Simon Glass1d8364a2020-12-28 20:34:54 -0700736U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600737\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700738\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700739\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600740\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600741};
742
Simon Glass192f8132020-10-03 11:31:25 -0600743/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300744static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600745\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
746};
Simon Glass1d8364a2020-12-28 20:34:54 -0700747U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600748\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700749\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700750\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600751\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600752};
753
Simon Glass192f8132020-10-03 11:31:25 -0600754/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300755static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600756\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
757};
Simon Glass1d8364a2020-12-28 20:34:54 -0700758U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600759\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700760\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700761\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600762\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600763};
764
Walter Lozanodc5b4372020-06-25 01:10:13 -0300765''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600766
767 def test_addresses32_64(self):
768 """Test output from a node with a 'reg' property with na=1, ns=2"""
769 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
770 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300771 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600772 with open(output) as infile:
773 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700774 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600775struct dtd_test1 {
776\tfdt64_t\t\treg[2];
777};
778struct dtd_test2 {
779\tfdt64_t\t\treg[2];
780};
781struct dtd_test3 {
782\tfdt64_t\t\treg[4];
783};
784''', data)
785
Walter Lozanoa324e412020-06-25 01:10:08 -0300786 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600787 with open(output) as infile:
788 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700789 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600790/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300791static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600792\t.reg\t\t\t= {0x1234, 0x567800000000},
793};
Simon Glass1d8364a2020-12-28 20:34:54 -0700794U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600795\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700796\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700797\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600798\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600799};
800
Simon Glass192f8132020-10-03 11:31:25 -0600801/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300802static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600803\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
804};
Simon Glass1d8364a2020-12-28 20:34:54 -0700805U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600806\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700807\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700808\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600809\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600810};
811
Simon Glass192f8132020-10-03 11:31:25 -0600812/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300813static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600814\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
815};
Simon Glass1d8364a2020-12-28 20:34:54 -0700816U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600817\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700818\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700819\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600820\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600821};
822
Walter Lozanodc5b4372020-06-25 01:10:13 -0300823''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600824
825 def test_bad_reg(self):
826 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600827 # Capture stderr since dtc will emit warnings for this file
828 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600829 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700830 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300831 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600832 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -0700833 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600834
835 def test_bad_reg2(self):
836 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600837 # Capture stderr since dtc will emit warnings for this file
838 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600839 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700840 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300841 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -0700842 self.assertIn(
843 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
844 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600845
846 def test_add_prop(self):
847 """Test that a subequent node can add a new property to a struct"""
848 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
849 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300850 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600851 with open(output) as infile:
852 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700853 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600854struct dtd_sandbox_spl_test {
855\tfdt32_t\t\tintarray;
856\tfdt32_t\t\tintval;
857};
858''', data)
859
Walter Lozanoa324e412020-06-25 01:10:08 -0300860 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600861 with open(output) as infile:
862 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700863 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600864/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300865static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600866\t.intval\t\t\t= 0x1,
867};
Simon Glass1d8364a2020-12-28 20:34:54 -0700868U_BOOT_DRVINFO(spl_test) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600869\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700870\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700871\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600872\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600873};
874
Simon Glass192f8132020-10-03 11:31:25 -0600875/* Node /spl-test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300876static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600877\t.intarray\t\t= 0x5,
878};
Simon Glass1d8364a2020-12-28 20:34:54 -0700879U_BOOT_DRVINFO(spl_test2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600880\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700881\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700882\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600883\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600884};
885
Walter Lozanodc5b4372020-06-25 01:10:13 -0300886''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600887
Simon Glassa60cabd2020-12-28 20:34:47 -0700888 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600889 """Test output to stdout"""
890 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass07ee48e2020-12-28 20:34:49 -0700891 with test_util.capture_sys_output() as (stdout, _):
Simon Glass6ca0c7a2020-12-28 20:34:48 -0700892 self.run_test(['struct'], dtb_file, None)
Simon Glass07ee48e2020-12-28 20:34:49 -0700893 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass961c1ce2018-07-06 10:27:35 -0600894
Simon Glassc3a310a82020-12-28 20:34:51 -0700895 def test_multi_to_file(self):
896 """Test output of multiple pieces to a single file"""
897 dtb_file = get_dtb_file('dtoc_test_simple.dts')
898 output = tools.GetOutputFilename('output')
Simon Glass4e8e8462020-12-28 20:34:52 -0700899 self.run_test(['all'], dtb_file, output)
Simon Glassc3a310a82020-12-28 20:34:51 -0700900 data = tools.ReadFile(output, binary=False)
Simon Glass4e8e8462020-12-28 20:34:52 -0700901 self._check_strings(self.platdata_text + self.struct_text, data)
Simon Glassc3a310a82020-12-28 20:34:51 -0700902
Simon Glassa60cabd2020-12-28 20:34:47 -0700903 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600904 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -0700905 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300906 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600907 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -0700908 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600909
Simon Glassa60cabd2020-12-28 20:34:47 -0700910 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600911 """Test running dtoc with an invalid command"""
912 dtb_file = get_dtb_file('dtoc_test_simple.dts')
913 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700914 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300915 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass4e8e8462020-12-28 20:34:52 -0700916 self.assertIn("Unknown command 'invalid-cmd': (use: platdata, struct)",
Simon Glassa60cabd2020-12-28 20:34:47 -0700917 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -0300918
Simon Glassa60cabd2020-12-28 20:34:47 -0700919 @staticmethod
920 def test_scan_drivers():
Walter Lozanod82062b2020-07-28 19:06:23 -0300921 """Test running dtoc with additional drivers to scan"""
922 dtb_file = get_dtb_file('dtoc_test_simple.dts')
923 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700924 with test_util.capture_sys_output() as _:
925 dtb_platdata.run_steps(
Simon Glass6a65d8a2020-12-28 20:34:50 -0700926 ['struct'], dtb_file, False, output, [], True,
Simon Glassa60cabd2020-12-28 20:34:47 -0700927 [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
Walter Lozanod82062b2020-07-28 19:06:23 -0300928
Simon Glassa60cabd2020-12-28 20:34:47 -0700929 @staticmethod
930 def test_unicode_error():
Walter Lozanod82062b2020-07-28 19:06:23 -0300931 """Test running dtoc with an invalid unicode file
932
933 To be able to perform this test without adding a weird text file which
934 would produce issues when using checkpatch.pl or patman, generate the
935 file at runtime and then process it.
936 """
937 dtb_file = get_dtb_file('dtoc_test_simple.dts')
938 output = tools.GetOutputFilename('output')
939 driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
Simon Glassa60cabd2020-12-28 20:34:47 -0700940 with open(driver_fn, 'wb+') as fout:
941 fout.write(b'\x81')
Walter Lozanod82062b2020-07-28 19:06:23 -0300942
Simon Glassa60cabd2020-12-28 20:34:47 -0700943 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700944 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [],
945 True, [driver_fn])
Simon Glassb42ed512020-12-23 08:11:23 -0700946
Simon Glassa60cabd2020-12-28 20:34:47 -0700947 def test_driver(self):
Simon Glassb42ed512020-12-23 08:11:23 -0700948 """Test the Driver class"""
949 drv1 = dtb_platdata.Driver('fred')
950 drv2 = dtb_platdata.Driver('mary')
951 drv3 = dtb_platdata.Driver('fred')
952 self.assertEqual("Driver(name='fred')", str(drv1))
953 self.assertEqual(drv1, drv3)
954 self.assertNotEqual(drv1, drv2)
955 self.assertNotEqual(drv2, drv3)
Simon Glass4e8e8462020-12-28 20:34:52 -0700956
957 def test_output_conflict(self):
958 """Test a conflict between and output dirs and output file"""
959 with self.assertRaises(ValueError) as exc:
960 dtb_platdata.run_steps(['all'], None, False, 'out', ['cdir'], True)
961 self.assertIn("Must specify either output or output_dirs, not both",
962 str(exc.exception))
963
964 def test_output_dirs(self):
965 """Test outputting files to a directory"""
966 # Remove the directory so that files from other tests are not there
967 tools._RemoveOutputDir()
968 tools.PrepareOutputDir(None)
969
970 # This should create the .dts and .dtb in the output directory
971 dtb_file = get_dtb_file('dtoc_test_simple.dts')
972 outdir = tools.GetOutputDir()
973 fnames = glob.glob(outdir + '/*')
974 self.assertEqual(2, len(fnames))
975
976 dtb_platdata.run_steps(['all'], dtb_file, False, None, [outdir], True)
977 fnames = glob.glob(outdir + '/*')
978 self.assertEqual(4, len(fnames))
979
980 leafs = set(os.path.basename(fname) for fname in fnames)
981 self.assertEqual(
982 {'dt-structs-gen.h', 'source.dts', 'dt-platdata.c', 'source.dtb'},
983 leafs)