blob: bcbf58c45bc8a4699eb07ac9c99b180fb8628468 [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
Simon Glass4f2059b2020-12-28 20:35:03 -070015import shutil
Simon Glass9d2eb922017-06-18 22:09:06 -060016import struct
Walter Lozanod82062b2020-07-28 19:06:23 -030017import tempfile
Simon Glass9d2eb922017-06-18 22:09:06 -060018import unittest
Simon Glass4f2059b2020-12-28 20:35:03 -070019from unittest import mock
Simon Glass9d2eb922017-06-18 22:09:06 -060020
Simon Glass9d2eb922017-06-18 22:09:06 -060021from dtb_platdata import conv_name_to_c
22from dtb_platdata import get_compat_name
23from dtb_platdata import get_value
24from dtb_platdata import tab_to
Simon Glassa60cabd2020-12-28 20:34:47 -070025from dtoc import dtb_platdata
Simon Glassa997ea52020-04-17 18:09:04 -060026from dtoc import fdt
27from dtoc import fdt_util
28from patman import test_util
29from patman import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060030
Simon Glassa60cabd2020-12-28 20:34:47 -070031OUR_PATH = os.path.dirname(os.path.realpath(__file__))
Simon Glass9d2eb922017-06-18 22:09:06 -060032
33
Simon Glasseb37e2d2017-11-12 21:52:17 -070034HEADER = '''/*
35 * DO NOT MODIFY
36 *
Simon Glass6b208842020-12-28 20:35:00 -070037 * Defines the structs used to hold devicetree data.
38 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glasseb37e2d2017-11-12 21:52:17 -070039 */
40
41#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090042#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070043
44C_HEADER = '''/*
45 * DO NOT MODIFY
46 *
Simon Glass6b208842020-12-28 20:35:00 -070047 * Declares the U_BOOT_DRIVER() records and platform data.
48 * This was generated by dtoc from a .dtb (device tree binary) file.
Simon Glasseb37e2d2017-11-12 21:52:17 -070049 */
50
Simon Glass1d8364a2020-12-28 20:34:54 -070051/* Allow use of U_BOOT_DRVINFO() in this file */
Simon Glassbeddd7a2020-12-28 20:35:01 -070052#define DT_PLAT_C
Simon Glass4c73d7b2020-10-03 11:31:41 -060053
Simon Glasseb37e2d2017-11-12 21:52:17 -070054#include <common.h>
55#include <dm.h>
56#include <dt-structs.h>
57'''
58
Simon Glassa60cabd2020-12-28 20:34:47 -070059# This is a test so is allowed to access private things in the module it is
60# testing
61# pylint: disable=W0212
Simon Glass3bce93d2018-07-06 10:27:37 -060062
63def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060064 """Compile a .dts file to a .dtb
65
66 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070067 dts_fname (str): Filename of .dts file in the current directory
68 capture_stderr (bool): True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060069
70 Returns:
Simon Glassa60cabd2020-12-28 20:34:47 -070071 str: Filename of compiled file in output directory
Simon Glass9d2eb922017-06-18 22:09:06 -060072 """
Simon Glassa60cabd2020-12-28 20:34:47 -070073 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, dts_fname),
Simon Glass3bce93d2018-07-06 10:27:37 -060074 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060075
76
77class TestDtoc(unittest.TestCase):
78 """Tests for dtoc"""
79 @classmethod
80 def setUpClass(cls):
81 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -060082 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -060083
84 @classmethod
85 def tearDownClass(cls):
Simon Glassa60cabd2020-12-28 20:34:47 -070086 tools.FinaliseOutputDir()
Simon Glass9d2eb922017-06-18 22:09:06 -060087
Simon Glassa60cabd2020-12-28 20:34:47 -070088 @staticmethod
89 def _write_python_string(fname, data):
Simon Glassc47c2b32018-07-06 10:27:25 -060090 """Write a string with tabs expanded as done in this Python file
91
92 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -070093 fname (str): Filename to write to
94 data (str): Raw string to convert
Simon Glassc47c2b32018-07-06 10:27:25 -060095 """
96 data = data.replace('\t', '\\t')
Simon Glassa60cabd2020-12-28 20:34:47 -070097 with open(fname, 'w') as fout:
98 fout.write(data)
Simon Glassc47c2b32018-07-06 10:27:25 -060099
Simon Glassa60cabd2020-12-28 20:34:47 -0700100 def _check_strings(self, expected, actual):
Simon Glassc47c2b32018-07-06 10:27:25 -0600101 """Check that a string matches its expected value
102
103 If the strings do not match, they are written to the /tmp directory in
104 the same Python format as is used here in the test. This allows for
105 easy comparison and update of the tests.
106
107 Args:
Simon Glassa60cabd2020-12-28 20:34:47 -0700108 expected (str): Expected string
109 actual (str): Actual string
Simon Glassc47c2b32018-07-06 10:27:25 -0600110 """
111 if expected != actual:
Simon Glassa60cabd2020-12-28 20:34:47 -0700112 self._write_python_string('/tmp/binman.expected', expected)
113 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600114 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassa60cabd2020-12-28 20:34:47 -0700115 self.assertEqual(expected, actual)
Simon Glassc47c2b32018-07-06 10:27:25 -0600116
Simon Glassa60cabd2020-12-28 20:34:47 -0700117 @staticmethod
118 def run_test(args, dtb_file, output):
119 """Run a test using dtoc
Walter Lozanoa324e412020-06-25 01:10:08 -0300120
Simon Glassa60cabd2020-12-28 20:34:47 -0700121 Args:
122 args (list of str): List of arguments for dtoc
123 dtb_file (str): Filename of .dtb file
124 output (str): Filename of output file
125 """
Simon Glass6a65d8a2020-12-28 20:34:50 -0700126 dtb_platdata.run_steps(args, dtb_file, False, output, [], True)
Walter Lozanoa324e412020-06-25 01:10:08 -0300127
Simon Glass9d2eb922017-06-18 22:09:06 -0600128 def test_name(self):
129 """Test conversion of device tree names to C identifiers"""
130 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
131 self.assertEqual('vendor_clock_frequency',
132 conv_name_to_c('vendor,clock-frequency'))
133 self.assertEqual('rockchip_rk3399_sdhci_5_1',
134 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
135
136 def test_tab_to(self):
137 """Test operation of tab_to() function"""
138 self.assertEqual('fred ', tab_to(0, 'fred'))
139 self.assertEqual('fred\t', tab_to(1, 'fred'))
140 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
141 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
142 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
143 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
144
145 def test_get_value(self):
146 """Test operation of get_value() function"""
147 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700148 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600149 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700150 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600151 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700152 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
153 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
154 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600155
156 def test_get_compat_name(self):
157 """Test operation of get_compat_name() function"""
158 Prop = collections.namedtuple('Prop', ['value'])
159 Node = collections.namedtuple('Node', ['props'])
160
161 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
162 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300163 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600164 get_compat_name(node))
165
166 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
167 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300168 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600169 get_compat_name(node))
170
171 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
172 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300173 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glassa60cabd2020-12-28 20:34:47 -0700174 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600175 get_compat_name(node))
176
177 def test_empty_file(self):
178 """Test output from a device tree file with no nodes"""
179 dtb_file = get_dtb_file('dtoc_test_empty.dts')
180 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300181 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600182 with open(output) as infile:
183 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700184 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600185
Walter Lozanoa324e412020-06-25 01:10:08 -0300186 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600187 with open(output) as infile:
188 lines = infile.read().splitlines()
Simon Glass2500de22020-12-28 20:35:05 -0700189 self.assertEqual(C_HEADER.splitlines() + [''], lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600190
Simon Glass07ee48e2020-12-28 20:34:49 -0700191 struct_text = HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600192struct dtd_sandbox_i2c_test {
193};
194struct dtd_sandbox_pmic_test {
195\tbool\t\tlow_power;
196\tfdt64_t\t\treg[2];
197};
Simon Glass9d2eb922017-06-18 22:09:06 -0600198struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600199\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600200\tbool\t\tboolval;
201\tunsigned char\tbytearray[3];
202\tunsigned char\tbyteval;
203\tfdt32_t\t\tintarray[4];
204\tfdt32_t\t\tintval;
205\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600206\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600207\tconst char *\tstringarray[3];
208\tconst char *\tstringval;
209};
Simon Glass07ee48e2020-12-28 20:34:49 -0700210'''
Simon Glass9d2eb922017-06-18 22:09:06 -0600211
Simon Glass07ee48e2020-12-28 20:34:49 -0700212 platdata_text = C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600213/* Node /i2c@0 index 0 */
214static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
215};
Simon Glass1d8364a2020-12-28 20:34:54 -0700216U_BOOT_DRVINFO(i2c_at_0) = {
Simon Glass192f8132020-10-03 11:31:25 -0600217\t.name\t\t= "sandbox_i2c_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700218\t.plat\t= &dtv_i2c_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700219\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600220\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600221};
222
223/* Node /i2c@0/pmic@9 index 1 */
224static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
225\t.low_power\t\t= true,
226\t.reg\t\t\t= {0x9, 0x0},
227};
Simon Glass1d8364a2020-12-28 20:34:54 -0700228U_BOOT_DRVINFO(pmic_at_9) = {
Simon Glass192f8132020-10-03 11:31:25 -0600229\t.name\t\t= "sandbox_pmic_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700230\t.plat\t= &dtv_pmic_at_9,
Simon Glass39edb952020-12-03 16:55:19 -0700231\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glass36b15e22020-10-03 11:31:35 -0600232\t.parent_idx\t= 0,
Simon Glass192f8132020-10-03 11:31:25 -0600233};
234
235/* Node /spl-test index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300236static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600237\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600238\t.bytearray\t\t= {0x6, 0x0, 0x0},
239\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600240\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600241\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600242\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
243\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600244\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600245\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600246\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600247};
Simon Glass1d8364a2020-12-28 20:34:54 -0700248U_BOOT_DRVINFO(spl_test) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600249\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700250\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700251\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600252\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600253};
254
Simon Glass192f8132020-10-03 11:31:25 -0600255/* Node /spl-test2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300256static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass7f5e2262020-07-07 21:32:06 -0600257\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glass9d2eb922017-06-18 22:09:06 -0600258\t.bytearray\t\t= {0x1, 0x23, 0x34},
259\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600260\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600261\t.intval\t\t\t= 0x3,
Simon Glass8034e4d2020-10-03 11:31:27 -0600262\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass131e0b02017-08-29 14:15:49 -0600263\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600264\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600265\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600266};
Simon Glass1d8364a2020-12-28 20:34:54 -0700267U_BOOT_DRVINFO(spl_test2) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600268\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700269\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700270\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600271\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600272};
273
Simon Glass192f8132020-10-03 11:31:25 -0600274/* Node /spl-test3 index 4 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300275static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass8034e4d2020-10-03 11:31:27 -0600276\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
277\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600278\t.stringarray\t\t= {"one", "", ""},
279};
Simon Glass1d8364a2020-12-28 20:34:54 -0700280U_BOOT_DRVINFO(spl_test3) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600281\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700282\t.plat\t= &dtv_spl_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700283\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600284\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600285};
286
Simon Glass2500de22020-12-28 20:35:05 -0700287'''
Simon Glass07ee48e2020-12-28 20:34:49 -0700288
289 def test_simple(self):
290 """Test output from some simple nodes with various types of data"""
291 dtb_file = get_dtb_file('dtoc_test_simple.dts')
292 output = tools.GetOutputFilename('output')
293 self.run_test(['struct'], dtb_file, output)
294 with open(output) as infile:
295 data = infile.read()
296
297 self._check_strings(self.struct_text, data)
298
299 self.run_test(['platdata'], dtb_file, output)
300 with open(output) as infile:
301 data = infile.read()
302
303 self._check_strings(self.platdata_text, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600304
Simon Glass4e8e8462020-12-28 20:34:52 -0700305 # Try the 'all' command
306 self.run_test(['all'], dtb_file, output)
307 data = tools.ReadFile(output, binary=False)
308 self._check_strings(self.platdata_text + self.struct_text, data)
309
Walter Lozanoe675d962020-07-03 08:07:17 -0300310 def test_driver_alias(self):
311 """Test output from a device tree file with a driver alias"""
312 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
313 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300314 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300315 with open(output) as infile:
316 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700317 self._check_strings(HEADER + '''
Walter Lozanoe675d962020-07-03 08:07:17 -0300318struct dtd_sandbox_gpio {
319\tconst char *\tgpio_bank_name;
320\tbool\t\tgpio_controller;
321\tfdt32_t\t\tsandbox_gpio_count;
322};
Walter Lozanoe675d962020-07-03 08:07:17 -0300323''', data)
324
Walter Lozanoa324e412020-06-25 01:10:08 -0300325 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300326 with open(output) as infile:
327 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700328 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600329/* Node /gpios@0 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300330static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300331\t.gpio_bank_name\t\t= "a",
332\t.gpio_controller\t= true,
333\t.sandbox_gpio_count\t= 0x14,
334};
Simon Glass1d8364a2020-12-28 20:34:54 -0700335U_BOOT_DRVINFO(gpios_at_0) = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300336\t.name\t\t= "sandbox_gpio",
Simon Glass71fa5b42020-12-03 16:55:18 -0700337\t.plat\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700338\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600339\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300340};
341
342''', data)
343
Walter Lozanoa324e412020-06-25 01:10:08 -0300344 def test_invalid_driver(self):
345 """Test output from a device tree file with an invalid driver"""
346 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
347 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700348 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700349 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [])
Walter Lozanoa324e412020-06-25 01:10:08 -0300350 with open(output) as infile:
351 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700352 self._check_strings(HEADER + '''
Walter Lozanoa324e412020-06-25 01:10:08 -0300353struct dtd_invalid {
354};
355''', data)
356
Simon Glassa60cabd2020-12-28 20:34:47 -0700357 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700358 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [])
Walter Lozanoa324e412020-06-25 01:10:08 -0300359 with open(output) as infile:
360 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700361 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600362/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300363static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300364};
Simon Glass1d8364a2020-12-28 20:34:54 -0700365U_BOOT_DRVINFO(spl_test) = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300366\t.name\t\t= "invalid",
Simon Glass71fa5b42020-12-03 16:55:18 -0700367\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700368\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600369\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300370};
371
372''', data)
373
Simon Glass9d2eb922017-06-18 22:09:06 -0600374 def test_phandle(self):
375 """Test output from a node containing a phandle reference"""
376 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
377 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300378 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600379 with open(output) as infile:
380 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700381 self._check_strings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600382struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600383\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600384};
385struct dtd_target {
386\tfdt32_t\t\tintval;
387};
388''', data)
389
Walter Lozanoa324e412020-06-25 01:10:08 -0300390 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600391 with open(output) as infile:
392 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700393 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600394/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300395static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600396\t.intval\t\t\t= 0x1,
397};
Simon Glass1d8364a2020-12-28 20:34:54 -0700398U_BOOT_DRVINFO(phandle2_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600399\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700400\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700401\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600402\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600403};
404
Simon Glass192f8132020-10-03 11:31:25 -0600405/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300406static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600407\t.intval\t\t\t= 0x2,
408};
Simon Glass1d8364a2020-12-28 20:34:54 -0700409U_BOOT_DRVINFO(phandle3_target) = {
Simon Glass3deeb472017-08-29 14:15:59 -0600410\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700411\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700412\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600413\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600414};
415
Simon Glass192f8132020-10-03 11:31:25 -0600416/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300417static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600418\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600419\t\t\t{4, {}},
420\t\t\t{0, {11}},
421\t\t\t{1, {12, 13}},
422\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600423};
Simon Glass1d8364a2020-12-28 20:34:54 -0700424U_BOOT_DRVINFO(phandle_source) = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600425\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700426\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700427\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600428\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600429};
430
Simon Glass192f8132020-10-03 11:31:25 -0600431/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300432static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600433\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600434\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600435};
Simon Glass1d8364a2020-12-28 20:34:54 -0700436U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass609e2b12018-07-06 10:27:31 -0600437\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700438\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700439\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600440\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -0600441};
442
Simon Glass16382ce2020-12-28 20:35:04 -0700443/* Node /phandle-target index 4 */
444static struct dtd_target dtv_phandle_target = {
445\t.intval\t\t\t= 0x0,
446};
447U_BOOT_DRVINFO(phandle_target) = {
448\t.name\t\t= "target",
449\t.plat\t= &dtv_phandle_target,
450\t.plat_size\t= sizeof(dtv_phandle_target),
451\t.parent_idx\t= -1,
452};
453
Simon Glass9d2eb922017-06-18 22:09:06 -0600454''', data)
455
Simon Glass961c1ce2018-07-06 10:27:35 -0600456 def test_phandle_single(self):
457 """Test output from a node containing a phandle reference"""
458 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
459 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300460 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600461 with open(output) as infile:
462 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700463 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600464struct dtd_source {
465\tstruct phandle_0_arg clocks[1];
466};
467struct dtd_target {
468\tfdt32_t\t\tintval;
469};
470''', data)
471
472 def test_phandle_reorder(self):
473 """Test that phandle targets are generated before their references"""
474 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
475 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300476 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600477 with open(output) as infile:
478 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700479 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600480/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300481static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600482\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600483\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -0600484};
Simon Glass1d8364a2020-12-28 20:34:54 -0700485U_BOOT_DRVINFO(phandle_source2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600486\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700487\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700488\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600489\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600490};
491
Simon Glass16382ce2020-12-28 20:35:04 -0700492/* Node /phandle-target index 1 */
493static struct dtd_target dtv_phandle_target = {
494};
495U_BOOT_DRVINFO(phandle_target) = {
496\t.name\t\t= "target",
497\t.plat\t= &dtv_phandle_target,
498\t.plat_size\t= sizeof(dtv_phandle_target),
499\t.parent_idx\t= -1,
500};
501
Simon Glass961c1ce2018-07-06 10:27:35 -0600502''', data)
503
Walter Lozano5541fc02020-06-25 01:10:17 -0300504 def test_phandle_cd_gpio(self):
505 """Test that phandle targets are generated when unsing cd-gpios"""
506 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
507 output = tools.GetOutputFilename('output')
Simon Glass6a65d8a2020-12-28 20:34:50 -0700508 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [], True)
Walter Lozano5541fc02020-06-25 01:10:17 -0300509 with open(output) as infile:
510 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700511 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600512/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300513static struct dtd_target dtv_phandle2_target = {
514\t.intval\t\t\t= 0x1,
515};
Simon Glass1d8364a2020-12-28 20:34:54 -0700516U_BOOT_DRVINFO(phandle2_target) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300517\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700518\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700519\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600520\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300521};
522
Simon Glass192f8132020-10-03 11:31:25 -0600523/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300524static struct dtd_target dtv_phandle3_target = {
525\t.intval\t\t\t= 0x2,
526};
Simon Glass1d8364a2020-12-28 20:34:54 -0700527U_BOOT_DRVINFO(phandle3_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_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700530\t.plat_size\t= sizeof(dtv_phandle3_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 /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300535static struct dtd_source dtv_phandle_source = {
536\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600537\t\t\t{4, {}},
538\t\t\t{0, {11}},
539\t\t\t{1, {12, 13}},
540\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300541};
Simon Glass1d8364a2020-12-28 20:34:54 -0700542U_BOOT_DRVINFO(phandle_source) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300543\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700544\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700545\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600546\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300547};
548
Simon Glass192f8132020-10-03 11:31:25 -0600549/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300550static struct dtd_source dtv_phandle_source2 = {
551\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600552\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300553};
Simon Glass1d8364a2020-12-28 20:34:54 -0700554U_BOOT_DRVINFO(phandle_source2) = {
Walter Lozano5541fc02020-06-25 01:10:17 -0300555\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700556\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700557\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600558\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300559};
560
Simon Glass16382ce2020-12-28 20:35:04 -0700561/* Node /phandle-target index 4 */
562static struct dtd_target dtv_phandle_target = {
563\t.intval\t\t\t= 0x0,
564};
565U_BOOT_DRVINFO(phandle_target) = {
566\t.name\t\t= "target",
567\t.plat\t= &dtv_phandle_target,
568\t.plat_size\t= sizeof(dtv_phandle_target),
569\t.parent_idx\t= -1,
570};
571
Walter Lozano5541fc02020-06-25 01:10:17 -0300572''', data)
573
Simon Glass961c1ce2018-07-06 10:27:35 -0600574 def test_phandle_bad(self):
575 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600576 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
577 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600578 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700579 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300580 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600581 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glassa60cabd2020-12-28 20:34:47 -0700582 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600583
584 def test_phandle_bad2(self):
585 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600586 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
587 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600588 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700589 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300590 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -0300591 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glassa60cabd2020-12-28 20:34:47 -0700592 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600593
Simon Glass1b1fe412017-08-29 14:15:50 -0600594 def test_addresses64(self):
595 """Test output from a node with a 'reg' property with na=2, ns=2"""
596 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
597 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300598 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600599 with open(output) as infile:
600 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700601 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600602struct dtd_test1 {
603\tfdt64_t\t\treg[2];
604};
605struct dtd_test2 {
606\tfdt64_t\t\treg[2];
607};
608struct dtd_test3 {
609\tfdt64_t\t\treg[4];
610};
611''', data)
612
Walter Lozanoa324e412020-06-25 01:10:08 -0300613 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600614 with open(output) as infile:
615 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700616 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600617/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300618static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600619\t.reg\t\t\t= {0x1234, 0x5678},
620};
Simon Glass1d8364a2020-12-28 20:34:54 -0700621U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600622\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700623\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700624\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600625\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600626};
627
Simon Glass192f8132020-10-03 11:31:25 -0600628/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300629static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600630\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
631};
Simon Glass1d8364a2020-12-28 20:34:54 -0700632U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600633\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700634\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700635\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600636\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600637};
638
Simon Glass192f8132020-10-03 11:31:25 -0600639/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300640static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600641\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
642};
Simon Glass1d8364a2020-12-28 20:34:54 -0700643U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600644\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700645\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700646\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600647\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600648};
649
Simon Glass2500de22020-12-28 20:35:05 -0700650''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600651
652 def test_addresses32(self):
653 """Test output from a node with a 'reg' property with na=1, ns=1"""
654 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
655 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300656 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600657 with open(output) as infile:
658 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700659 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600660struct dtd_test1 {
661\tfdt32_t\t\treg[2];
662};
663struct dtd_test2 {
664\tfdt32_t\t\treg[4];
665};
666''', data)
667
Walter Lozanoa324e412020-06-25 01:10:08 -0300668 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600669 with open(output) as infile:
670 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700671 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600672/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300673static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600674\t.reg\t\t\t= {0x1234, 0x5678},
675};
Simon Glass1d8364a2020-12-28 20:34:54 -0700676U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600677\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700678\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700679\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600680\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600681};
682
Simon Glass192f8132020-10-03 11:31:25 -0600683/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300684static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600685\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
686};
Simon Glass1d8364a2020-12-28 20:34:54 -0700687U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600688\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700689\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700690\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600691\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600692};
693
Simon Glass2500de22020-12-28 20:35:05 -0700694''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600695
696 def test_addresses64_32(self):
697 """Test output from a node with a 'reg' property with na=2, ns=1"""
698 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
699 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300700 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600701 with open(output) as infile:
702 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700703 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600704struct dtd_test1 {
705\tfdt64_t\t\treg[2];
706};
707struct dtd_test2 {
708\tfdt64_t\t\treg[2];
709};
710struct dtd_test3 {
711\tfdt64_t\t\treg[4];
712};
713''', data)
714
Walter Lozanoa324e412020-06-25 01:10:08 -0300715 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600716 with open(output) as infile:
717 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700718 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600719/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300720static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600721\t.reg\t\t\t= {0x123400000000, 0x5678},
722};
Simon Glass1d8364a2020-12-28 20:34:54 -0700723U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600724\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700725\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700726\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600727\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600728};
729
Simon Glass192f8132020-10-03 11:31:25 -0600730/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300731static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600732\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
733};
Simon Glass1d8364a2020-12-28 20:34:54 -0700734U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600735\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700736\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700737\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600738\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600739};
740
Simon Glass192f8132020-10-03 11:31:25 -0600741/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300742static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600743\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
744};
Simon Glass1d8364a2020-12-28 20:34:54 -0700745U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600746\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700747\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700748\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600749\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600750};
751
Simon Glass2500de22020-12-28 20:35:05 -0700752''', data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600753
754 def test_addresses32_64(self):
755 """Test output from a node with a 'reg' property with na=1, ns=2"""
756 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
757 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300758 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600759 with open(output) as infile:
760 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700761 self._check_strings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600762struct dtd_test1 {
763\tfdt64_t\t\treg[2];
764};
765struct dtd_test2 {
766\tfdt64_t\t\treg[2];
767};
768struct dtd_test3 {
769\tfdt64_t\t\treg[4];
770};
771''', data)
772
Walter Lozanoa324e412020-06-25 01:10:08 -0300773 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600774 with open(output) as infile:
775 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700776 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600777/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300778static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600779\t.reg\t\t\t= {0x1234, 0x567800000000},
780};
Simon Glass1d8364a2020-12-28 20:34:54 -0700781U_BOOT_DRVINFO(test1) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600782\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700783\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700784\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600785\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600786};
787
Simon Glass192f8132020-10-03 11:31:25 -0600788/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300789static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600790\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
791};
Simon Glass1d8364a2020-12-28 20:34:54 -0700792U_BOOT_DRVINFO(test2) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600793\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700794\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700795\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600796\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600797};
798
Simon Glass192f8132020-10-03 11:31:25 -0600799/* Node /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300800static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600801\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
802};
Simon Glass1d8364a2020-12-28 20:34:54 -0700803U_BOOT_DRVINFO(test3) = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600804\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700805\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700806\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600807\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600808};
809
Simon Glass2500de22020-12-28 20:35:05 -0700810''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600811
812 def test_bad_reg(self):
813 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600814 # Capture stderr since dtc will emit warnings for this file
815 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600816 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700817 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300818 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600819 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glassa60cabd2020-12-28 20:34:47 -0700820 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600821
822 def test_bad_reg2(self):
823 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600824 # Capture stderr since dtc will emit warnings for this file
825 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600826 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700827 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300828 self.run_test(['struct'], dtb_file, output)
Simon Glassa60cabd2020-12-28 20:34:47 -0700829 self.assertIn(
830 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
831 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600832
833 def test_add_prop(self):
834 """Test that a subequent node can add a new property to a struct"""
835 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
836 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300837 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600838 with open(output) as infile:
839 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700840 self._check_strings(HEADER + '''
Simon Glass961c1ce2018-07-06 10:27:35 -0600841struct dtd_sandbox_spl_test {
842\tfdt32_t\t\tintarray;
843\tfdt32_t\t\tintval;
844};
845''', data)
846
Walter Lozanoa324e412020-06-25 01:10:08 -0300847 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600848 with open(output) as infile:
849 data = infile.read()
Simon Glassa60cabd2020-12-28 20:34:47 -0700850 self._check_strings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600851/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300852static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600853\t.intval\t\t\t= 0x1,
854};
Simon Glass1d8364a2020-12-28 20:34:54 -0700855U_BOOT_DRVINFO(spl_test) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600856\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700857\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700858\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600859\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600860};
861
Simon Glass192f8132020-10-03 11:31:25 -0600862/* Node /spl-test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300863static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600864\t.intarray\t\t= 0x5,
865};
Simon Glass1d8364a2020-12-28 20:34:54 -0700866U_BOOT_DRVINFO(spl_test2) = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600867\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700868\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700869\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600870\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600871};
872
Simon Glass2500de22020-12-28 20:35:05 -0700873''', data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600874
Simon Glassa60cabd2020-12-28 20:34:47 -0700875 def test_stdout(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600876 """Test output to stdout"""
877 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glass07ee48e2020-12-28 20:34:49 -0700878 with test_util.capture_sys_output() as (stdout, _):
Simon Glass6ca0c7a2020-12-28 20:34:48 -0700879 self.run_test(['struct'], dtb_file, None)
Simon Glass07ee48e2020-12-28 20:34:49 -0700880 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass961c1ce2018-07-06 10:27:35 -0600881
Simon Glassc3a310a82020-12-28 20:34:51 -0700882 def test_multi_to_file(self):
883 """Test output of multiple pieces to a single file"""
884 dtb_file = get_dtb_file('dtoc_test_simple.dts')
885 output = tools.GetOutputFilename('output')
Simon Glass4e8e8462020-12-28 20:34:52 -0700886 self.run_test(['all'], dtb_file, output)
Simon Glassc3a310a82020-12-28 20:34:51 -0700887 data = tools.ReadFile(output, binary=False)
Simon Glass4e8e8462020-12-28 20:34:52 -0700888 self._check_strings(self.platdata_text + self.struct_text, data)
Simon Glassc3a310a82020-12-28 20:34:51 -0700889
Simon Glassa60cabd2020-12-28 20:34:47 -0700890 def test_no_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600891 """Test running dtoc without a command"""
Simon Glassa60cabd2020-12-28 20:34:47 -0700892 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300893 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600894 self.assertIn("Please specify a command: struct, platdata",
Simon Glassa60cabd2020-12-28 20:34:47 -0700895 str(exc.exception))
Simon Glass961c1ce2018-07-06 10:27:35 -0600896
Simon Glassa60cabd2020-12-28 20:34:47 -0700897 def test_bad_command(self):
Simon Glass961c1ce2018-07-06 10:27:35 -0600898 """Test running dtoc with an invalid command"""
899 dtb_file = get_dtb_file('dtoc_test_simple.dts')
900 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700901 with self.assertRaises(ValueError) as exc:
Walter Lozanoa324e412020-06-25 01:10:08 -0300902 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass4e8e8462020-12-28 20:34:52 -0700903 self.assertIn("Unknown command 'invalid-cmd': (use: platdata, struct)",
Simon Glassa60cabd2020-12-28 20:34:47 -0700904 str(exc.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -0300905
Simon Glassa60cabd2020-12-28 20:34:47 -0700906 @staticmethod
907 def test_scan_drivers():
Walter Lozanod82062b2020-07-28 19:06:23 -0300908 """Test running dtoc with additional drivers to scan"""
909 dtb_file = get_dtb_file('dtoc_test_simple.dts')
910 output = tools.GetOutputFilename('output')
Simon Glassa60cabd2020-12-28 20:34:47 -0700911 with test_util.capture_sys_output() as _:
912 dtb_platdata.run_steps(
Simon Glass6a65d8a2020-12-28 20:34:50 -0700913 ['struct'], dtb_file, False, output, [], True,
Simon Glassa60cabd2020-12-28 20:34:47 -0700914 [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
Walter Lozanod82062b2020-07-28 19:06:23 -0300915
Simon Glassa60cabd2020-12-28 20:34:47 -0700916 @staticmethod
917 def test_unicode_error():
Walter Lozanod82062b2020-07-28 19:06:23 -0300918 """Test running dtoc with an invalid unicode file
919
920 To be able to perform this test without adding a weird text file which
921 would produce issues when using checkpatch.pl or patman, generate the
922 file at runtime and then process it.
923 """
924 dtb_file = get_dtb_file('dtoc_test_simple.dts')
925 output = tools.GetOutputFilename('output')
926 driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
Simon Glassa60cabd2020-12-28 20:34:47 -0700927 with open(driver_fn, 'wb+') as fout:
928 fout.write(b'\x81')
Walter Lozanod82062b2020-07-28 19:06:23 -0300929
Simon Glassa60cabd2020-12-28 20:34:47 -0700930 with test_util.capture_sys_output() as _:
Simon Glass6a65d8a2020-12-28 20:34:50 -0700931 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [],
932 True, [driver_fn])
Simon Glassb42ed512020-12-23 08:11:23 -0700933
Simon Glassa60cabd2020-12-28 20:34:47 -0700934 def test_driver(self):
Simon Glassb42ed512020-12-23 08:11:23 -0700935 """Test the Driver class"""
936 drv1 = dtb_platdata.Driver('fred')
937 drv2 = dtb_platdata.Driver('mary')
938 drv3 = dtb_platdata.Driver('fred')
939 self.assertEqual("Driver(name='fred')", str(drv1))
940 self.assertEqual(drv1, drv3)
941 self.assertNotEqual(drv1, drv2)
942 self.assertNotEqual(drv2, drv3)
Simon Glass4e8e8462020-12-28 20:34:52 -0700943
944 def test_output_conflict(self):
945 """Test a conflict between and output dirs and output file"""
946 with self.assertRaises(ValueError) as exc:
947 dtb_platdata.run_steps(['all'], None, False, 'out', ['cdir'], True)
948 self.assertIn("Must specify either output or output_dirs, not both",
949 str(exc.exception))
950
951 def test_output_dirs(self):
952 """Test outputting files to a directory"""
953 # Remove the directory so that files from other tests are not there
954 tools._RemoveOutputDir()
955 tools.PrepareOutputDir(None)
956
957 # This should create the .dts and .dtb in the output directory
958 dtb_file = get_dtb_file('dtoc_test_simple.dts')
959 outdir = tools.GetOutputDir()
960 fnames = glob.glob(outdir + '/*')
961 self.assertEqual(2, len(fnames))
962
963 dtb_platdata.run_steps(['all'], dtb_file, False, None, [outdir], True)
964 fnames = glob.glob(outdir + '/*')
965 self.assertEqual(4, len(fnames))
966
967 leafs = set(os.path.basename(fname) for fname in fnames)
968 self.assertEqual(
Simon Glassbeddd7a2020-12-28 20:35:01 -0700969 {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb'},
Simon Glass4e8e8462020-12-28 20:34:52 -0700970 leafs)
Simon Glass4f2059b2020-12-28 20:35:03 -0700971
972 def test_scan_dirs(self):
973 """Test scanning of source directories"""
974 def add_file(fname):
975 pathname = os.path.join(indir, fname)
976 dirname = os.path.dirname(pathname)
977 os.makedirs(dirname, exist_ok=True)
978 tools.WriteFile(pathname, '', binary=False)
979 fname_list.append(pathname)
980
981 try:
982 outdir = tools.GetOutputDir()
983 indir = tempfile.mkdtemp(prefix='dtoc.')
984 dtb_file = get_dtb_file('dtoc_test_simple.dts')
985
986 fname_list = []
987 add_file('fname.c')
988 add_file('dir/fname2.c')
989
990 # Mock out scan_driver and check that it is called with the
991 # expected files
992 with mock.patch.object(dtb_platdata.DtbPlatdata, "scan_driver") \
993 as mocked:
994 dtb_platdata.run_steps(['all'], dtb_file, False, None, [outdir],
995 True, basedir=indir)
996 self.assertEqual(2, len(mocked.mock_calls))
997 self.assertEqual(mock.call(fname_list[0]),
998 mocked.mock_calls[0])
999 self.assertEqual(mock.call(fname_list[1]),
1000 mocked.mock_calls[1])
1001 finally:
1002 shutil.rmtree(indir)