blob: d961d67b8fcb0f00bf2e345121c4daf1c2db2080 [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
16import unittest
17
Simon Glass9d2eb922017-06-18 22:09:06 -060018from dtb_platdata import get_value
19from dtb_platdata import tab_to
Simon Glassa60cabd2020-12-28 20:34:47 -070020from dtoc import dtb_platdata
Simon Glassa997ea52020-04-17 18:09:04 -060021from dtoc import fdt
22from dtoc import fdt_util
Simon Glass9065bc92020-12-28 20:35:06 -070023from dtoc.src_scan import conv_name_to_c
24from dtoc.src_scan import get_compat_name
Simon Glassa997ea52020-04-17 18:09:04 -060025from 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 *
Simon Glass6b208842020-12-28 20:35:00 -070034 * Defines the structs used to hold devicetree data.
35 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glasseb37e2d2017-11-12 21:52:17 -070036 */
37
38#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090039#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070040
41C_HEADER = '''/*
42 * DO NOT MODIFY
43 *
Simon Glass6b208842020-12-28 20:35:00 -070044 * Declares the U_BOOT_DRIVER() records and platform data.
45 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glasseb37e2d2017-11-12 21:52:17 -070046 */
47
Simon Glass1d8364a2020-12-28 20:34:54 -070048/* Allow use of U_BOOT_DRVINFO() in this file */
Simon Glassbeddd7a2020-12-28 20:35:01 -070049#define DT_PLAT_C
Simon Glass4c73d7b2020-10-03 11:31:41 -060050
Simon Glasseb37e2d2017-11-12 21:52:17 -070051#include <common.h>
52#include <dm.h>
53#include <dt-structs.h>
54'''
55
Simon Glassa60cabd2020-12-28 20:34:47 -070056# This is a test so is allowed to access private things in the module it is
57# testing
58# pylint: disable=W0212
Simon Glass3bce93d2018-07-06 10:27:37 -060059
60def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060061 """Compile a .dts file to a .dtb
62
63 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070064 dts_fname (str): Filename of .dts file in the current directory
65 capture_stderr (bool): True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060066
67 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070068 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060069 """
Simon Glassa60cabd2020-12-28 20:34:47 -070070 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060071 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060072
73
74class TestDtoc(unittest.TestCase):
75 """Tests for dtoc"""
76 @classmethod
77 def setUpClass(cls):
78 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -060079 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -060080
81 @classmethod
82 def tearDownClass(cls):
Simon Glassa60cabd2020-12-28 20:34:47 -070083 tools.FinaliseOutputDir()
Simon Glass9d2eb922017-06-18 22:09:06 -060084
Simon Glassa60cabd2020-12-28 20:34:47 -070085 @staticmethod
86 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -060087 """Write a string with tabs expanded as done in this Python file
88
89 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070090 fname (str): Filename to write to
91 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -060092 """
93 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -070094 with open(fname, 'w') as fout:
95 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -060096
Simon Glassa60cabd2020-12-28 20:34:47 -070097 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-07-06 10:27:25 -060098 """Check that a string matches its expected value
99
100 If the strings do not match, they are written to the /tmp directory in
101 the same Python format as is used here in the test. This allows for
102 easy comparison and update of the tests.
103
104 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700105 expected (str): Expected string
106 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600107 """
108 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700109 self._write_python_string('/tmp/binman.expected', expected)
110 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600111 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700112 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600113
Simon Glassa60cabd2020-12-28 20:34:47 -0700114 @staticmethod
115 def run_test(args, dtb_file, output):
116 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300117
Simon Glassa60cabd2020-12-28 20:34:47 -0700118 Args:
119 args (list of str): List of arguments for dtoc
120 dtb_file (str): Filename of .dtb file
121 output (str): Filename of output file
122 """
Simon Glass6a65d8a2020-12-28 20:34:50 -0700123 dtb_platdata.run_steps(args, dtb_file, False, output, [], True)
Walter Lozanoa324e412020-06-25 01:10:08 -0300124
Simon Glass9d2eb922017-06-18 22:09:06 -0600125 def test_name(self):
126 """Test conversion of device tree names to C identifiers"""
127 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
128 self.assertEqual('vendor_clock_frequency',
129 conv_name_to_c('vendor,clock-frequency'))
130 self.assertEqual('rockchip_rk3399_sdhci_5_1',
131 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
132
133 def test_tab_to(self):
134 """Test operation of tab_to() function"""
135 self.assertEqual('fred ', tab_to(0, 'fred'))
136 self.assertEqual('fred\t', tab_to(1, 'fred'))
137 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
138 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
139 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
140 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
141
142 def test_get_value(self):
143 """Test operation of get_value() function"""
144 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700145 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600146 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700147 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600148 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700149 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
150 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
151 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600152
153 def test_get_compat_name(self):
154 """Test operation of get_compat_name() function"""
155 Prop = collections.namedtuple('Prop', ['value'])
156 Node = collections.namedtuple('Node', ['props'])
157
158 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
159 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300160 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600161 get_compat_name(node))
162
163 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
164 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300165 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600166 get_compat_name(node))
167
168 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
169 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300170 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700171 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600172 get_compat_name(node))
173
174 def test_empty_file(self):
175 """Test output from a device tree file with no nodes"""
176 dtb_file = get_dtb_file('dtoc_test_empty.dts')
177 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300178 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600179 with open(output) as infile:
180 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700181 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600182
Walter Lozanoa324e412020-06-25 01:10:08 -0300183 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600184 with open(output) as infile:
185 lines = infile.read().splitlines()
Simon Glass2500de22020-12-28 20:35:05 -0700186 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600187
Simon Glass07ee48e2020-12-28 20:34:49 -0700188 struct_text = HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600189struct dtd_sandbox_i2c_test {
190};
191struct dtd_sandbox_pmic_test {
192\tbool\t\tlow_power;
193\tfdt64_t\t\treg[2];
194};
Simon Glass9d2eb922017-06-18 22:09:06 -0600195struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600196\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600197\tbool\t\tboolval;
198\tunsigned char\tbytearray[3];
199\tunsigned char\tbyteval;
200\tfdt32_t\t\tintarray[4];
201\tfdt32_t\t\tintval;
202\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600203\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600204\tconst char *\tstringarray[3];
205\tconst char *\tstringval;
206};
Simon Glass07ee48e2020-12-28 20:34:49 -0700207'''
Simon Glass9d2eb922017-06-18 22:09:06 -0600208
Simon Glass07ee48e2020-12-28 20:34:49 -0700209 platdata_text = C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600210/* Node /i2c@0 index 0 */
211static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
212};
Simon Glass1d8364a2020-12-28 20:34:54 -0700213U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glass192f8132020-10-03 11:31:25 -0600214\t.name\t\t= "sandbox_i2c_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700215\t.plat\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700216\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600217\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600218};
219
220/* Node /i2c@0/pmic@9 index 1 */
221static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
222\t.low_power\t\t= true,
223\t.reg\t\t\t= {0x9, 0x0},
224};
Simon Glass1d8364a2020-12-28 20:34:54 -0700225U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glass192f8132020-10-03 11:31:25 -0600226\t.name\t\t= "sandbox_pmic_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700227\t.plat\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700228\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600229\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600230};
231
232/* Node /spl-test index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300233static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600234\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600235\t.bytearray\t\t= {0x6, 0x0, 0x0},
236\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600237\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600238\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600239\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
240\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600241\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600242\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600243\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600244};
Simon Glass1d8364a2020-12-28 20:34:54 -0700245U_BOOT_DRVINFO(spl_test) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600246\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700247\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700248\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600249\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600250};
251
Simon Glass192f8132020-10-03 11:31:25 -0600252/* Node /spl-test2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300253static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600254\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600255\t.bytearray\t\t= {0x1, 0x23, 0x34},
256\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600257\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600258\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600259\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600260\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600261\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600262\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600263};
Simon Glass1d8364a2020-12-28 20:34:54 -0700264U_BOOT_DRVINFO(spl_test2) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600265\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700266\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700267\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600268\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600269};
270
Simon Glass192f8132020-10-03 11:31:25 -0600271/* Node /spl-test3 index 4 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300272static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600273\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
274\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600275\t.stringarray\t\t= {"one", "", ""},
276};
Simon Glass1d8364a2020-12-28 20:34:54 -0700277U_BOOT_DRVINFO(spl_test3) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600278\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700279\t.plat\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700280\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600281\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600282};
283
Simon Glass2500de22020-12-28 20:35:05 -0700284'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700285
286 def test_simple(self):
287 """Test output from some simple nodes with various types of data"""
288 dtb_file = get_dtb_file('dtoc_test_simple.dts')
289 output = tools.GetOutputFilename('output')
290 self.run_test(['struct'], dtb_file, output)
291 with open(output) as infile:
292 data = infile.read()
293
294 self._check_strings(self.struct_text, data)
295
296 self.run_test(['platdata'], dtb_file, output)
297 with open(output) as infile:
298 data = infile.read()
299
300 self._check_strings(self.platdata_text, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600301
Simon Glass4e8e8462020-12-28 20:34:52 -0700302 # Try the 'all' command
303 self.run_test(['all'], dtb_file, output)
304 data = tools.ReadFile(output, binary=False)
305 self._check_strings(self.platdata_text + self.struct_text, data)
306
Walter Lozanoe675d962020-07-03 08:07:17 -0300307 def test_driver_alias(self):
308 """Test output from a device tree file with a driver alias"""
309 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
310 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300311 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300312 with open(output) as infile:
313 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700314 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300315struct dtd_sandbox_gpio {
316\tconst char *\tgpio_bank_name;
317\tbool\t\tgpio_controller;
318\tfdt32_t\t\tsandbox_gpio_count;
319};
Walter Lozanoe675d962020-07-03 08:07:17 -0300320''', data)
321
Walter Lozanoa324e412020-06-25 01:10:08 -0300322 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300323 with open(output) as infile:
324 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700325 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600326/* Node /gpios@0 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300327static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300328\t.gpio_bank_name\t\t= "a",
329\t.gpio_controller\t= true,
330\t.sandbox_gpio_count\t= 0x14,
331};
Simon Glass1d8364a2020-12-28 20:34:54 -0700332U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300333\t.name\t\t= "sandbox_gpio",
Simon Glass71fa5b42020-12-03 16:55:18 -0700334\t.plat\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700335\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600336\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300337};
338
339''', data)
340
Walter Lozanoa324e412020-06-25 01:10:08 -0300341 def test_invalid_driver(self):
342 """Test output from a device tree file with an invalid driver"""
343 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
344 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700345 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700346 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [])
Walter Lozanoa324e412020-06-25 01:10:08 -0300347 with open(output) as infile:
348 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700349 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300350struct dtd_invalid {
351};
352''', data)
353
Simon Glassa60cabd2020-12-28 20:34:47 -0700354 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700355 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [])
Walter Lozanoa324e412020-06-25 01:10:08 -0300356 with open(output) as infile:
357 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700358 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600359/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300360static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300361};
Simon Glass1d8364a2020-12-28 20:34:54 -0700362U_BOOT_DRVINFO(spl_test) = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300363\t.name\t\t= "invalid",
Simon Glass71fa5b42020-12-03 16:55:18 -0700364\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700365\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600366\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300367};
368
369''', data)
370
Simon Glass9d2eb922017-06-18 22:09:06 -0600371 def test_phandle(self):
372 """Test output from a node containing a phandle reference"""
373 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
374 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300375 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600376 with open(output) as infile:
377 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700378 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600379struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600380\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600381};
382struct dtd_target {
383\tfdt32_t\t\tintval;
384};
385''', data)
386
Walter Lozanoa324e412020-06-25 01:10:08 -0300387 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600388 with open(output) as infile:
389 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700390 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600391/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300392static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600393\t.intval\t\t\t= 0x1,
394};
Simon Glass1d8364a2020-12-28 20:34:54 -0700395U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600396\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700397\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700398\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600399\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600400};
401
Simon Glass192f8132020-10-03 11:31:25 -0600402/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300403static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600404\t.intval\t\t\t= 0x2,
405};
Simon Glass1d8364a2020-12-28 20:34:54 -0700406U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600407\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700408\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700409\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600410\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600411};
412
Simon Glass192f8132020-10-03 11:31:25 -0600413/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300414static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600415\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600416\t\t\t{4, {}},
417\t\t\t{0, {11}},
418\t\t\t{1, {12, 13}},
419\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600420};
Simon Glass1d8364a2020-12-28 20:34:54 -0700421U_BOOT_DRVINFO(phandle_source) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600422\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700423\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700424\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600425\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600426};
427
Simon Glass192f8132020-10-03 11:31:25 -0600428/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300429static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600430\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600431\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600432};
Simon Glass1d8364a2020-12-28 20:34:54 -0700433U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass609e2b12018-07-06 10:27:31 -0600434\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700435\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700436\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600437\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -0600438};
439
Simon Glass16382ce2020-12-28 20:35:04 -0700440/* Node /phandle-target index 4 */
441static struct dtd_target dtv_phandle_target = {
442\t.intval\t\t\t= 0x0,
443};
444U_BOOT_DRVINFO(phandle_target) = {
445\t.name\t\t= "target",
446\t.plat\t= &dtv_phandle_target,
447\t.plat_size\t= sizeof(dtv_phandle_target),
448\t.parent_idx\t= -1,
449};
450
Simon Glass9d2eb922017-06-18 22:09:06 -0600451''', data)
452
Simon Glass961c1ce2018-07-06 10:27:35 -0600453 def test_phandle_single(self):
454 """Test output from a node containing a phandle reference"""
455 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
456 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300457 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600458 with open(output) as infile:
459 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700460 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600461struct dtd_source {
462\tstruct phandle_0_arg clocks[1];
463};
464struct dtd_target {
465\tfdt32_t\t\tintval;
466};
467''', data)
468
469 def test_phandle_reorder(self):
470 """Test that phandle targets are generated before their references"""
471 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
472 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300473 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600474 with open(output) as infile:
475 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700476 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600477/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300478static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600479\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600480\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -0600481};
Simon Glass1d8364a2020-12-28 20:34:54 -0700482U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600483\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700484\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700485\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600486\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600487};
488
Simon Glass16382ce2020-12-28 20:35:04 -0700489/* Node /phandle-target index 1 */
490static struct dtd_target dtv_phandle_target = {
491};
492U_BOOT_DRVINFO(phandle_target) = {
493\t.name\t\t= "target",
494\t.plat\t= &dtv_phandle_target,
495\t.plat_size\t= sizeof(dtv_phandle_target),
496\t.parent_idx\t= -1,
497};
498
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')
Simon Glass6a65d8a2020-12-28 20:34:50 -0700505 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [], True)
Walter Lozano5541fc02020-06-25 01:10:17 -0300506 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};
Simon Glass1d8364a2020-12-28 20:34:54 -0700513U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300514\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};
Simon Glass1d8364a2020-12-28 20:34:54 -0700524U_BOOT_DRVINFO(phandle3_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300525\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-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300532static struct dtd_source dtv_phandle_source = {
533\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600534\t\t\t{4, {}},
535\t\t\t{0, {11}},
536\t\t\t{1, {12, 13}},
537\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300538};
Simon Glass1d8364a2020-12-28 20:34:54 -0700539U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300540\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700541\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700542\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600543\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300544};
545
Simon Glass192f8132020-10-03 11:31:25 -0600546/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300547static struct dtd_source dtv_phandle_source2 = {
548\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600549\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300550};
Simon Glass1d8364a2020-12-28 20:34:54 -0700551U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300552\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700553\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700554\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600555\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300556};
557
Simon Glass16382ce2020-12-28 20:35:04 -0700558/* Node /phandle-target index 4 */
559static struct dtd_target dtv_phandle_target = {
560\t.intval\t\t\t= 0x0,
561};
562U_BOOT_DRVINFO(phandle_target) = {
563\t.name\t\t= "target",
564\t.plat\t= &dtv_phandle_target,
565\t.plat_size\t= sizeof(dtv_phandle_target),
566\t.parent_idx\t= -1,
567};
568
Walter Lozano5541fc02020-06-25 01:10:17 -0300569''', data)
570
Simon Glass961c1ce2018-07-06 10:27:35 -0600571 def test_phandle_bad(self):
572 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600573 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
574 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600575 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700576 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300577 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600578 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -0700579 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600580
581 def test_phandle_bad2(self):
582 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600583 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
584 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600585 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700586 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300587 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -0300588 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -0700589 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600590
Simon Glass1b1fe412017-08-29 14:15:50 -0600591 def test_addresses64(self):
592 """Test output from a node with a 'reg' property with na=2, ns=2"""
593 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
594 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300595 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600596 with open(output) as infile:
597 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700598 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600599struct dtd_test1 {
600\tfdt64_t\t\treg[2];
601};
602struct dtd_test2 {
603\tfdt64_t\t\treg[2];
604};
605struct dtd_test3 {
606\tfdt64_t\t\treg[4];
607};
608''', data)
609
Walter Lozanoa324e412020-06-25 01:10:08 -0300610 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600611 with open(output) as infile:
612 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700613 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600614/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300615static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600616\t.reg\t\t\t= {0x1234, 0x5678},
617};
Simon Glass1d8364a2020-12-28 20:34:54 -0700618U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600619\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700620\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700621\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600622\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600623};
624
Simon Glass192f8132020-10-03 11:31:25 -0600625/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300626static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600627\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
628};
Simon Glass1d8364a2020-12-28 20:34:54 -0700629U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600630\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700631\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700632\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600633\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600634};
635
Simon Glass192f8132020-10-03 11:31:25 -0600636/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300637static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600638\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
639};
Simon Glass1d8364a2020-12-28 20:34:54 -0700640U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600641\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700642\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700643\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600644\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600645};
646
Simon Glass2500de22020-12-28 20:35:05 -0700647''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600648
649 def test_addresses32(self):
650 """Test output from a node with a 'reg' property with na=1, ns=1"""
651 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
652 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300653 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600654 with open(output) as infile:
655 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700656 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600657struct dtd_test1 {
658\tfdt32_t\t\treg[2];
659};
660struct dtd_test2 {
661\tfdt32_t\t\treg[4];
662};
663''', data)
664
Walter Lozanoa324e412020-06-25 01:10:08 -0300665 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600666 with open(output) as infile:
667 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700668 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600669/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300670static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600671\t.reg\t\t\t= {0x1234, 0x5678},
672};
Simon Glass1d8364a2020-12-28 20:34:54 -0700673U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600674\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700675\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700676\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600677\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600678};
679
Simon Glass192f8132020-10-03 11:31:25 -0600680/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300681static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600682\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
683};
Simon Glass1d8364a2020-12-28 20:34:54 -0700684U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600685\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700686\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700687\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600688\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600689};
690
Simon Glass2500de22020-12-28 20:35:05 -0700691''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600692
693 def test_addresses64_32(self):
694 """Test output from a node with a 'reg' property with na=2, ns=1"""
695 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
696 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300697 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600698 with open(output) as infile:
699 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700700 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600701struct dtd_test1 {
702\tfdt64_t\t\treg[2];
703};
704struct dtd_test2 {
705\tfdt64_t\t\treg[2];
706};
707struct dtd_test3 {
708\tfdt64_t\t\treg[4];
709};
710''', data)
711
Walter Lozanoa324e412020-06-25 01:10:08 -0300712 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600713 with open(output) as infile:
714 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700715 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600716/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300717static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600718\t.reg\t\t\t= {0x123400000000, 0x5678},
719};
Simon Glass1d8364a2020-12-28 20:34:54 -0700720U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600721\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700722\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700723\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600724\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600725};
726
Simon Glass192f8132020-10-03 11:31:25 -0600727/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300728static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600729\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
730};
Simon Glass1d8364a2020-12-28 20:34:54 -0700731U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600732\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700733\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700734\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600735\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600736};
737
Simon Glass192f8132020-10-03 11:31:25 -0600738/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300739static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600740\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
741};
Simon Glass1d8364a2020-12-28 20:34:54 -0700742U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600743\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700744\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700745\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600746\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600747};
748
Simon Glass2500de22020-12-28 20:35:05 -0700749''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600750
751 def test_addresses32_64(self):
752 """Test output from a node with a 'reg' property with na=1, ns=2"""
753 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
754 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300755 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600756 with open(output) as infile:
757 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700758 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600759struct dtd_test1 {
760\tfdt64_t\t\treg[2];
761};
762struct dtd_test2 {
763\tfdt64_t\t\treg[2];
764};
765struct dtd_test3 {
766\tfdt64_t\t\treg[4];
767};
768''', data)
769
Walter Lozanoa324e412020-06-25 01:10:08 -0300770 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600771 with open(output) as infile:
772 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700773 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600774/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300775static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600776\t.reg\t\t\t= {0x1234, 0x567800000000},
777};
Simon Glass1d8364a2020-12-28 20:34:54 -0700778U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600779\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700780\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700781\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600782\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600783};
784
Simon Glass192f8132020-10-03 11:31:25 -0600785/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300786static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600787\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
788};
Simon Glass1d8364a2020-12-28 20:34:54 -0700789U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600790\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700791\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700792\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600793\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600794};
795
Simon Glass192f8132020-10-03 11:31:25 -0600796/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300797static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600798\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
799};
Simon Glass1d8364a2020-12-28 20:34:54 -0700800U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600801\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700802\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700803\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600804\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600805};
806
Simon Glass2500de22020-12-28 20:35:05 -0700807''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600808
809 def test_bad_reg(self):
810 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600811 # Capture stderr since dtc will emit warnings for this file
812 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600813 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700814 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300815 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600816 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -0700817 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600818
819 def test_bad_reg2(self):
820 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600821 # Capture stderr since dtc will emit warnings for this file
822 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600823 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700824 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300825 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -0700826 self.assertIn(
827 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
828 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600829
830 def test_add_prop(self):
831 """Test that a subequent node can add a new property to a struct"""
832 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
833 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300834 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600835 with open(output) as infile:
836 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700837 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600838struct dtd_sandbox_spl_test {
839\tfdt32_t\t\tintarray;
840\tfdt32_t\t\tintval;
841};
842''', data)
843
Walter Lozanoa324e412020-06-25 01:10:08 -0300844 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600845 with open(output) as infile:
846 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700847 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600848/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300849static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600850\t.intval\t\t\t= 0x1,
851};
Simon Glass1d8364a2020-12-28 20:34:54 -0700852U_BOOT_DRVINFO(spl_test) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600853\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700854\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700855\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600856\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600857};
858
Simon Glass192f8132020-10-03 11:31:25 -0600859/* Node /spl-test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300860static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600861\t.intarray\t\t= 0x5,
862};
Simon Glass1d8364a2020-12-28 20:34:54 -0700863U_BOOT_DRVINFO(spl_test2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600864\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700865\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700866\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600867\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600868};
869
Simon Glass2500de22020-12-28 20:35:05 -0700870''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600871
Simon Glassa60cabd2020-12-28 20:34:47 -0700872 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600873 """Test output to stdout"""
874 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass07ee48e2020-12-28 20:34:49 -0700875 with test_util.capture_sys_output() as (stdout, _):
Simon Glass6ca0c7a2020-12-28 20:34:48 -0700876 self.run_test(['struct'], dtb_file, None)
Simon Glass07ee48e2020-12-28 20:34:49 -0700877 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass961c1ce2018-07-06 10:27:35 -0600878
Simon Glassc3a310a82020-12-28 20:34:51 -0700879 def test_multi_to_file(self):
880 """Test output of multiple pieces to a single file"""
881 dtb_file = get_dtb_file('dtoc_test_simple.dts')
882 output = tools.GetOutputFilename('output')
Simon Glass4e8e8462020-12-28 20:34:52 -0700883 self.run_test(['all'], dtb_file, output)
Simon Glassc3a310a82020-12-28 20:34:51 -0700884 data = tools.ReadFile(output, binary=False)
Simon Glass4e8e8462020-12-28 20:34:52 -0700885 self._check_strings(self.platdata_text + self.struct_text, data)
Simon Glassc3a310a82020-12-28 20:34:51 -0700886
Simon Glassa60cabd2020-12-28 20:34:47 -0700887 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600888 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -0700889 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300890 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600891 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -0700892 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600893
Simon Glassa60cabd2020-12-28 20:34:47 -0700894 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600895 """Test running dtoc with an invalid command"""
896 dtb_file = get_dtb_file('dtoc_test_simple.dts')
897 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700898 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300899 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass4e8e8462020-12-28 20:34:52 -0700900 self.assertIn("Unknown command 'invalid-cmd': (use: platdata, struct)",
Simon Glassa60cabd2020-12-28 20:34:47 -0700901 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -0300902
Simon Glass4e8e8462020-12-28 20:34:52 -0700903 def test_output_conflict(self):
904 """Test a conflict between and output dirs and output file"""
905 with self.assertRaises(ValueError) as exc:
906 dtb_platdata.run_steps(['all'], None, False, 'out', ['cdir'], True)
907 self.assertIn("Must specify either output or output_dirs, not both",
908 str(exc.exception))
909
910 def test_output_dirs(self):
911 """Test outputting files to a directory"""
912 # Remove the directory so that files from other tests are not there
913 tools._RemoveOutputDir()
914 tools.PrepareOutputDir(None)
915
916 # This should create the .dts and .dtb in the output directory
917 dtb_file = get_dtb_file('dtoc_test_simple.dts')
918 outdir = tools.GetOutputDir()
919 fnames = glob.glob(outdir + '/*')
920 self.assertEqual(2, len(fnames))
921
922 dtb_platdata.run_steps(['all'], dtb_file, False, None, [outdir], True)
923 fnames = glob.glob(outdir + '/*')
924 self.assertEqual(4, len(fnames))
925
926 leafs = set(os.path.basename(fname) for fname in fnames)
927 self.assertEqual(
Simon Glassbeddd7a2020-12-28 20:35:01 -0700928 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb'},
Simon Glass4e8e8462020-12-28 20:34:52 -0700929 leafs)