blob: b023a1e14a5a2ec245d77530e18a2268fbf246d6 [file] [log] [blame]
Simon Glass3c19dc82019-10-31 07:42:55 -06001#!/usr/bin/env python3
Tom Rini83d290c2018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glassc0791922017-06-18 22:09:06 -06003# Copyright (c) 2012 The Chromium OS Authors.
4#
Simon Glassc0791922017-06-18 22:09:06 -06005
6"""Tests for the dtb_platdata module
7
Simon Glass3def0cf2018-07-06 10:27:20 -06008This includes unit tests for some functions and functional tests for the dtoc
9tool.
Simon Glassc0791922017-06-18 22:09:06 -060010"""
11
12import collections
13import os
14import struct
Walter Lozano6c74d1b2020-07-28 19:06:23 -030015import tempfile
Simon Glassc0791922017-06-18 22:09:06 -060016import unittest
17
Simon Glassc0791922017-06-18 22:09:06 -060018from dtb_platdata import conv_name_to_c
19from dtb_platdata import get_compat_name
20from dtb_platdata import get_value
21from dtb_platdata import tab_to
Simon Glass67b5ec52020-12-28 20:34:47 -070022from dtoc import dtb_platdata
Simon Glassbf776672020-04-17 18:09:04 -060023from dtoc import fdt
24from dtoc import fdt_util
25from patman import test_util
26from patman import tools
Simon Glassc0791922017-06-18 22:09:06 -060027
Simon Glass67b5ec52020-12-28 20:34:47 -070028OUR_PATH = os.path.dirname(os.path.realpath(__file__))
Simon Glassc0791922017-06-18 22:09:06 -060029
30
Simon Glassaab660f2017-11-12 21:52:17 -070031HEADER = '''/*
32 * DO NOT MODIFY
33 *
34 * This file was generated by dtoc from a .dtb (device tree binary) file.
35 */
36
37#include <stdbool.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090038#include <linux/libfdt.h>'''
Simon Glassaab660f2017-11-12 21:52:17 -070039
40C_HEADER = '''/*
41 * DO NOT MODIFY
42 *
43 * This file was generated by dtoc from a .dtb (device tree binary) file.
44 */
45
Simon Glasscb43ac12020-10-03 11:31:41 -060046/* Allow use of U_BOOT_DEVICE() in this file */
47#define DT_PLATDATA_C
48
Simon Glassaab660f2017-11-12 21:52:17 -070049#include <common.h>
50#include <dm.h>
51#include <dt-structs.h>
52'''
53
Walter Lozano51f12632020-06-25 01:10:13 -030054C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
55}
56'''
Simon Glassaab660f2017-11-12 21:52:17 -070057
Simon Glass67b5ec52020-12-28 20:34:47 -070058# This is a test so is allowed to access private things in the module it is
59# testing
60# pylint: disable=W0212
Simon Glassfe57c782018-07-06 10:27:37 -060061
62def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glassc0791922017-06-18 22:09:06 -060063 """Compile a .dts file to a .dtb
64
65 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -070066 dts_fname (str): Filename of .dts file in the current directory
67 capture_stderr (bool): True to capture and discard stderr output
Simon Glassc0791922017-06-18 22:09:06 -060068
69 Returns:
Simon Glass67b5ec52020-12-28 20:34:47 -070070 str: Filename of compiled file in output directory
Simon Glassc0791922017-06-18 22:09:06 -060071 """
Simon Glass67b5ec52020-12-28 20:34:47 -070072 return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, dts_fname),
Simon Glassfe57c782018-07-06 10:27:37 -060073 capture_stderr=capture_stderr)
Simon Glassc0791922017-06-18 22:09:06 -060074
75
76class TestDtoc(unittest.TestCase):
77 """Tests for dtoc"""
78 @classmethod
79 def setUpClass(cls):
80 tools.PrepareOutputDir(None)
Simon Glassf02d0eb2020-07-07 21:32:06 -060081 cls.maxDiff = None
Simon Glassc0791922017-06-18 22:09:06 -060082
83 @classmethod
84 def tearDownClass(cls):
Simon Glass67b5ec52020-12-28 20:34:47 -070085 tools.FinaliseOutputDir()
Simon Glassc0791922017-06-18 22:09:06 -060086
Simon Glass67b5ec52020-12-28 20:34:47 -070087 @staticmethod
88 def _write_python_string(fname, data):
Simon Glass57f0bc42018-07-06 10:27:25 -060089 """Write a string with tabs expanded as done in this Python file
90
91 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -070092 fname (str): Filename to write to
93 data (str): Raw string to convert
Simon Glass57f0bc42018-07-06 10:27:25 -060094 """
95 data = data.replace('\t', '\\t')
Simon Glass67b5ec52020-12-28 20:34:47 -070096 with open(fname, 'w') as fout:
97 fout.write(data)
Simon Glass57f0bc42018-07-06 10:27:25 -060098
Simon Glass67b5ec52020-12-28 20:34:47 -070099 def _check_strings(self, expected, actual):
Simon Glass57f0bc42018-07-06 10:27:25 -0600100 """Check that a string matches its expected value
101
102 If the strings do not match, they are written to the /tmp directory in
103 the same Python format as is used here in the test. This allows for
104 easy comparison and update of the tests.
105
106 Args:
Simon Glass67b5ec52020-12-28 20:34:47 -0700107 expected (str): Expected string
108 actual (str): Actual string
Simon Glass57f0bc42018-07-06 10:27:25 -0600109 """
110 if expected != actual:
Simon Glass67b5ec52020-12-28 20:34:47 -0700111 self._write_python_string('/tmp/binman.expected', expected)
112 self._write_python_string('/tmp/binman.actual', actual)
Simon Glass90a81322019-05-17 22:00:31 -0600113 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glass67b5ec52020-12-28 20:34:47 -0700114 self.assertEqual(expected, actual)
Simon Glass57f0bc42018-07-06 10:27:25 -0600115
Simon Glass67b5ec52020-12-28 20:34:47 -0700116 @staticmethod
117 def run_test(args, dtb_file, output):
118 """Run a test using dtoc
Walter Lozano361e7332020-06-25 01:10:08 -0300119
Simon Glass67b5ec52020-12-28 20:34:47 -0700120 Args:
121 args (list of str): List of arguments for dtoc
122 dtb_file (str): Filename of .dtb file
123 output (str): Filename of output file
124 """
Simon Glass192c1112020-12-28 20:34:50 -0700125 dtb_platdata.run_steps(args, dtb_file, False, output, [], True)
Walter Lozano361e7332020-06-25 01:10:08 -0300126
Simon Glassc0791922017-06-18 22:09:06 -0600127 def test_name(self):
128 """Test conversion of device tree names to C identifiers"""
129 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
130 self.assertEqual('vendor_clock_frequency',
131 conv_name_to_c('vendor,clock-frequency'))
132 self.assertEqual('rockchip_rk3399_sdhci_5_1',
133 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
134
135 def test_tab_to(self):
136 """Test operation of tab_to() function"""
137 self.assertEqual('fred ', tab_to(0, 'fred'))
138 self.assertEqual('fred\t', tab_to(1, 'fred'))
139 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
140 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
141 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
142 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
143
144 def test_get_value(self):
145 """Test operation of get_value() function"""
146 self.assertEqual('0x45',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700147 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glassc0791922017-06-18 22:09:06 -0600148 self.assertEqual('0x45',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700149 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glassc0791922017-06-18 22:09:06 -0600150 self.assertEqual('0x0',
Simon Glass5ea9dcc2020-11-08 20:36:17 -0700151 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
152 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
153 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glassc0791922017-06-18 22:09:06 -0600154
155 def test_get_compat_name(self):
156 """Test operation of get_compat_name() function"""
157 Prop = collections.namedtuple('Prop', ['value'])
158 Node = collections.namedtuple('Node', ['props'])
159
160 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
161 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300162 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600163 get_compat_name(node))
164
165 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
166 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300167 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600168 get_compat_name(node))
169
170 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
171 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300172 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
Simon Glass67b5ec52020-12-28 20:34:47 -0700173 'arasan_sdhci_5_1', 'third']),
Simon Glassc0791922017-06-18 22:09:06 -0600174 get_compat_name(node))
175
176 def test_empty_file(self):
177 """Test output from a device tree file with no nodes"""
178 dtb_file = get_dtb_file('dtoc_test_empty.dts')
179 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300180 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600181 with open(output) as infile:
182 lines = infile.read().splitlines()
Simon Glassaab660f2017-11-12 21:52:17 -0700183 self.assertEqual(HEADER.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600184
Walter Lozano361e7332020-06-25 01:10:08 -0300185 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600186 with open(output) as infile:
187 lines = infile.read().splitlines()
Walter Lozano51f12632020-06-25 01:10:13 -0300188 self.assertEqual(C_HEADER.splitlines() + [''] +
189 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600190
Simon Glassde846cb2020-12-28 20:34:49 -0700191 struct_text = HEADER + '''
Simon Glass5ec741f2017-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 Glassc0791922017-06-18 22:09:06 -0600198struct dtd_sandbox_spl_test {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600199\tconst char * acpi_name;
Simon Glassc0791922017-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 Glass2a2d91d2018-07-06 10:27:28 -0600206\tunsigned char\tnotstring[5];
Simon Glassc0791922017-06-18 22:09:06 -0600207\tconst char *\tstringarray[3];
208\tconst char *\tstringval;
209};
Simon Glassde846cb2020-12-28 20:34:49 -0700210'''
Simon Glassc0791922017-06-18 22:09:06 -0600211
Simon Glassde846cb2020-12-28 20:34:49 -0700212 platdata_text = C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600213/* Node /i2c@0 index 0 */
214static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
215};
216U_BOOT_DEVICE(i2c_at_0) = {
217\t.name\t\t= "sandbox_i2c_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700218\t.plat\t= &dtv_i2c_at_0,
Simon Glass4f500862020-12-03 16:55:19 -0700219\t.plat_size\t= sizeof(dtv_i2c_at_0),
Simon Glasse41651f2020-10-03 11:31:35 -0600220\t.parent_idx\t= -1,
Simon Glass1b272732020-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};
228U_BOOT_DEVICE(pmic_at_9) = {
229\t.name\t\t= "sandbox_pmic_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700230\t.plat\t= &dtv_pmic_at_9,
Simon Glass4f500862020-12-03 16:55:19 -0700231\t.plat_size\t= sizeof(dtv_pmic_at_9),
Simon Glasse41651f2020-10-03 11:31:35 -0600232\t.parent_idx\t= 0,
Simon Glass1b272732020-10-03 11:31:25 -0600233};
234
235/* Node /spl-test index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300236static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass1953ce72019-05-17 22:00:32 -0600237\t.boolval\t\t= true,
Simon Glassc0791922017-06-18 22:09:06 -0600238\t.bytearray\t\t= {0x6, 0x0, 0x0},
239\t.byteval\t\t= 0x5,
Simon Glass1953ce72019-05-17 22:00:32 -0600240\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600241\t.intval\t\t\t= 0x1,
Simon Glass21d54ac2017-08-29 14:15:49 -0600242\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
243\t\t0x11},
Simon Glass1953ce72019-05-17 22:00:32 -0600244\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600245\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glass1953ce72019-05-17 22:00:32 -0600246\t.stringval\t\t= "message",
Simon Glassc0791922017-06-18 22:09:06 -0600247};
248U_BOOT_DEVICE(spl_test) = {
249\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700250\t.plat\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700251\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600252\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600253};
254
Simon Glass1b272732020-10-03 11:31:25 -0600255/* Node /spl-test2 index 3 */
Walter Lozano51f12632020-06-25 01:10:13 -0300256static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600257\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glassc0791922017-06-18 22:09:06 -0600258\t.bytearray\t\t= {0x1, 0x23, 0x34},
259\t.byteval\t\t= 0x8,
Simon Glass1953ce72019-05-17 22:00:32 -0600260\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600261\t.intval\t\t\t= 0x3,
Simon Glasse144caf2020-10-03 11:31:27 -0600262\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
Simon Glass21d54ac2017-08-29 14:15:49 -0600263\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600264\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glass1953ce72019-05-17 22:00:32 -0600265\t.stringval\t\t= "message2",
Simon Glassc0791922017-06-18 22:09:06 -0600266};
267U_BOOT_DEVICE(spl_test2) = {
268\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700269\t.plat\t= &dtv_spl_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700270\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600271\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600272};
273
Simon Glass1b272732020-10-03 11:31:25 -0600274/* Node /spl-test3 index 4 */
Walter Lozano51f12632020-06-25 01:10:13 -0300275static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glasse144caf2020-10-03 11:31:27 -0600276\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
277\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600278\t.stringarray\t\t= {"one", "", ""},
279};
280U_BOOT_DEVICE(spl_test3) = {
281\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700282\t.plat\t= &dtv_spl_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700283\t.plat_size\t= sizeof(dtv_spl_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600284\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600285};
286
Simon Glassde846cb2020-12-28 20:34:49 -0700287''' + C_EMPTY_POPULATE_PHANDLE_DATA
288
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 Glassc0791922017-06-18 22:09:06 -0600304
Walter Lozanodac82282020-07-03 08:07:17 -0300305 def test_driver_alias(self):
306 """Test output from a device tree file with a driver alias"""
307 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
308 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300309 self.run_test(['struct'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300310 with open(output) as infile:
311 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700312 self._check_strings(HEADER + '''
Walter Lozanodac82282020-07-03 08:07:17 -0300313struct dtd_sandbox_gpio {
314\tconst char *\tgpio_bank_name;
315\tbool\t\tgpio_controller;
316\tfdt32_t\t\tsandbox_gpio_count;
317};
Walter Lozanodac82282020-07-03 08:07:17 -0300318''', data)
319
Walter Lozano361e7332020-06-25 01:10:08 -0300320 self.run_test(['platdata'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300321 with open(output) as infile:
322 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700323 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600324/* Node /gpios@0 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300325static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanodac82282020-07-03 08:07:17 -0300326\t.gpio_bank_name\t\t= "a",
327\t.gpio_controller\t= true,
328\t.sandbox_gpio_count\t= 0x14,
329};
330U_BOOT_DEVICE(gpios_at_0) = {
331\t.name\t\t= "sandbox_gpio",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700332\t.plat\t= &dtv_gpios_at_0,
Simon Glass4f500862020-12-03 16:55:19 -0700333\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glasse41651f2020-10-03 11:31:35 -0600334\t.parent_idx\t= -1,
Walter Lozanodac82282020-07-03 08:07:17 -0300335};
336
Walter Lozano51f12632020-06-25 01:10:13 -0300337void dm_populate_phandle_data(void) {
338}
Walter Lozanodac82282020-07-03 08:07:17 -0300339''', data)
340
Walter Lozano361e7332020-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 Glass67b5ec52020-12-28 20:34:47 -0700345 with test_util.capture_sys_output() as _:
Simon Glass192c1112020-12-28 20:34:50 -0700346 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [])
Walter Lozano361e7332020-06-25 01:10:08 -0300347 with open(output) as infile:
348 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700349 self._check_strings(HEADER + '''
Walter Lozano361e7332020-06-25 01:10:08 -0300350struct dtd_invalid {
351};
352''', data)
353
Simon Glass67b5ec52020-12-28 20:34:47 -0700354 with test_util.capture_sys_output() as _:
Simon Glass192c1112020-12-28 20:34:50 -0700355 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [])
Walter Lozano361e7332020-06-25 01:10:08 -0300356 with open(output) as infile:
357 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700358 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600359/* Node /spl-test index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300360static struct dtd_invalid dtv_spl_test = {
Walter Lozano361e7332020-06-25 01:10:08 -0300361};
362U_BOOT_DEVICE(spl_test) = {
363\t.name\t\t= "invalid",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700364\t.plat\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700365\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600366\t.parent_idx\t= -1,
Walter Lozano361e7332020-06-25 01:10:08 -0300367};
368
Walter Lozano51f12632020-06-25 01:10:13 -0300369void dm_populate_phandle_data(void) {
370}
Walter Lozano361e7332020-06-25 01:10:08 -0300371''', data)
372
Simon Glassc0791922017-06-18 22:09:06 -0600373 def test_phandle(self):
374 """Test output from a node containing a phandle reference"""
375 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
376 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300377 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600378 with open(output) as infile:
379 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700380 self._check_strings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600381struct dtd_source {
Simon Glass634eba42017-08-29 14:15:59 -0600382\tstruct phandle_2_arg clocks[4];
Simon Glassc0791922017-06-18 22:09:06 -0600383};
384struct dtd_target {
385\tfdt32_t\t\tintval;
386};
387''', data)
388
Walter Lozano361e7332020-06-25 01:10:08 -0300389 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600390 with open(output) as infile:
391 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700392 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600393/* Node /phandle2-target index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300394static struct dtd_target dtv_phandle2_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600395\t.intval\t\t\t= 0x1,
396};
397U_BOOT_DEVICE(phandle2_target) = {
398\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700399\t.plat\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -0700400\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600401\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600402};
403
Simon Glass1b272732020-10-03 11:31:25 -0600404/* Node /phandle3-target index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300405static struct dtd_target dtv_phandle3_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600406\t.intval\t\t\t= 0x2,
407};
408U_BOOT_DEVICE(phandle3_target) = {
409\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700410\t.plat\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -0700411\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600412\t.parent_idx\t= -1,
Simon Glass634eba42017-08-29 14:15:59 -0600413};
414
Simon Glass1b272732020-10-03 11:31:25 -0600415/* Node /phandle-target index 4 */
416static struct dtd_target dtv_phandle_target = {
417\t.intval\t\t\t= 0x0,
418};
419U_BOOT_DEVICE(phandle_target) = {
420\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700421\t.plat\t= &dtv_phandle_target,
Simon Glass4f500862020-12-03 16:55:19 -0700422\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600423\t.parent_idx\t= -1,
Simon Glass1b272732020-10-03 11:31:25 -0600424};
425
426/* Node /phandle-source index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300427static struct dtd_source dtv_phandle_source = {
Simon Glass35d50372017-08-29 14:15:57 -0600428\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600429\t\t\t{4, {}},
430\t\t\t{0, {11}},
431\t\t\t{1, {12, 13}},
432\t\t\t{4, {}},},
Simon Glassc0791922017-06-18 22:09:06 -0600433};
434U_BOOT_DEVICE(phandle_source) = {
435\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700436\t.plat\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -0700437\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -0600438\t.parent_idx\t= -1,
Simon Glassc0791922017-06-18 22:09:06 -0600439};
440
Simon Glass1b272732020-10-03 11:31:25 -0600441/* Node /phandle-source2 index 3 */
Walter Lozano51f12632020-06-25 01:10:13 -0300442static struct dtd_source dtv_phandle_source2 = {
Simon Glass760b7172018-07-06 10:27:31 -0600443\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600444\t\t\t{4, {}},},
Simon Glass760b7172018-07-06 10:27:31 -0600445};
446U_BOOT_DEVICE(phandle_source2) = {
447\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700448\t.plat\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700449\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600450\t.parent_idx\t= -1,
Simon Glass760b7172018-07-06 10:27:31 -0600451};
452
Walter Lozano51f12632020-06-25 01:10:13 -0300453void dm_populate_phandle_data(void) {
Walter Lozano51f12632020-06-25 01:10:13 -0300454}
Simon Glassc0791922017-06-18 22:09:06 -0600455''', data)
456
Simon Glass8512ea22018-07-06 10:27:35 -0600457 def test_phandle_single(self):
458 """Test output from a node containing a phandle reference"""
459 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
460 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300461 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600462 with open(output) as infile:
463 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700464 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -0600465struct dtd_source {
466\tstruct phandle_0_arg clocks[1];
467};
468struct dtd_target {
469\tfdt32_t\t\tintval;
470};
471''', data)
472
473 def test_phandle_reorder(self):
474 """Test that phandle targets are generated before their references"""
475 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
476 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300477 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600478 with open(output) as infile:
479 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700480 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600481/* Node /phandle-target index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300482static struct dtd_target dtv_phandle_target = {
Simon Glass8512ea22018-07-06 10:27:35 -0600483};
484U_BOOT_DEVICE(phandle_target) = {
485\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700486\t.plat\t= &dtv_phandle_target,
Simon Glass4f500862020-12-03 16:55:19 -0700487\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600488\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600489};
490
Simon Glass1b272732020-10-03 11:31:25 -0600491/* Node /phandle-source2 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300492static struct dtd_source dtv_phandle_source2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600493\t.clocks\t\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600494\t\t\t{1, {}},},
Simon Glass8512ea22018-07-06 10:27:35 -0600495};
496U_BOOT_DEVICE(phandle_source2) = {
497\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700498\t.plat\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700499\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600500\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600501};
502
Walter Lozano51f12632020-06-25 01:10:13 -0300503void dm_populate_phandle_data(void) {
Walter Lozano51f12632020-06-25 01:10:13 -0300504}
Simon Glass8512ea22018-07-06 10:27:35 -0600505''', data)
506
Walter Lozano6c3fc502020-06-25 01:10:17 -0300507 def test_phandle_cd_gpio(self):
508 """Test that phandle targets are generated when unsing cd-gpios"""
509 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
510 output = tools.GetOutputFilename('output')
Simon Glass192c1112020-12-28 20:34:50 -0700511 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [], True)
Walter Lozano6c3fc502020-06-25 01:10:17 -0300512 with open(output) as infile:
513 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700514 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600515/* Node /phandle2-target index 0 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300516static struct dtd_target dtv_phandle2_target = {
517\t.intval\t\t\t= 0x1,
518};
519U_BOOT_DEVICE(phandle2_target) = {
520\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700521\t.plat\t= &dtv_phandle2_target,
Simon Glass4f500862020-12-03 16:55:19 -0700522\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600523\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300524};
525
Simon Glass1b272732020-10-03 11:31:25 -0600526/* Node /phandle3-target index 1 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300527static struct dtd_target dtv_phandle3_target = {
528\t.intval\t\t\t= 0x2,
529};
530U_BOOT_DEVICE(phandle3_target) = {
531\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700532\t.plat\t= &dtv_phandle3_target,
Simon Glass4f500862020-12-03 16:55:19 -0700533\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600534\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300535};
536
Simon Glass1b272732020-10-03 11:31:25 -0600537/* Node /phandle-target index 4 */
538static struct dtd_target dtv_phandle_target = {
539\t.intval\t\t\t= 0x0,
540};
541U_BOOT_DEVICE(phandle_target) = {
542\t.name\t\t= "target",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700543\t.plat\t= &dtv_phandle_target,
Simon Glass4f500862020-12-03 16:55:19 -0700544\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glasse41651f2020-10-03 11:31:35 -0600545\t.parent_idx\t= -1,
Simon Glass1b272732020-10-03 11:31:25 -0600546};
547
548/* Node /phandle-source index 2 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300549static struct dtd_source dtv_phandle_source = {
550\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600551\t\t\t{4, {}},
552\t\t\t{0, {11}},
553\t\t\t{1, {12, 13}},
554\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -0300555};
556U_BOOT_DEVICE(phandle_source) = {
557\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700558\t.plat\t= &dtv_phandle_source,
Simon Glass4f500862020-12-03 16:55:19 -0700559\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glasse41651f2020-10-03 11:31:35 -0600560\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300561};
562
Simon Glass1b272732020-10-03 11:31:25 -0600563/* Node /phandle-source2 index 3 */
Walter Lozano6c3fc502020-06-25 01:10:17 -0300564static struct dtd_source dtv_phandle_source2 = {
565\t.cd_gpios\t\t= {
Simon Glass8a38abf2020-10-03 11:31:40 -0600566\t\t\t{4, {}},},
Walter Lozano6c3fc502020-06-25 01:10:17 -0300567};
568U_BOOT_DEVICE(phandle_source2) = {
569\t.name\t\t= "source",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700570\t.plat\t= &dtv_phandle_source2,
Simon Glass4f500862020-12-03 16:55:19 -0700571\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glasse41651f2020-10-03 11:31:35 -0600572\t.parent_idx\t= -1,
Walter Lozano6c3fc502020-06-25 01:10:17 -0300573};
574
575void dm_populate_phandle_data(void) {
Walter Lozano6c3fc502020-06-25 01:10:17 -0300576}
577''', data)
578
Simon Glass8512ea22018-07-06 10:27:35 -0600579 def test_phandle_bad(self):
580 """Test a node containing an invalid phandle fails"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600581 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
582 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600583 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700584 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300585 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600586 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
Simon Glass67b5ec52020-12-28 20:34:47 -0700587 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600588
589 def test_phandle_bad2(self):
590 """Test a phandle target missing its #*-cells property"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600591 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
592 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600593 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700594 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300595 self.run_test(['struct'], dtb_file, output)
Walter Lozanoad340172020-06-25 01:10:16 -0300596 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glass67b5ec52020-12-28 20:34:47 -0700597 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600598
Simon Glassc20ee0e2017-08-29 14:15:50 -0600599 def test_addresses64(self):
600 """Test output from a node with a 'reg' property with na=2, ns=2"""
601 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
602 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300603 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600604 with open(output) as infile:
605 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700606 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600607struct dtd_test1 {
608\tfdt64_t\t\treg[2];
609};
610struct dtd_test2 {
611\tfdt64_t\t\treg[2];
612};
613struct dtd_test3 {
614\tfdt64_t\t\treg[4];
615};
616''', data)
617
Walter Lozano361e7332020-06-25 01:10:08 -0300618 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600619 with open(output) as infile:
620 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700621 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600622/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300623static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600624\t.reg\t\t\t= {0x1234, 0x5678},
625};
626U_BOOT_DEVICE(test1) = {
627\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700628\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700629\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600630\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600631};
632
Simon Glass1b272732020-10-03 11:31:25 -0600633/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300634static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600635\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
636};
637U_BOOT_DEVICE(test2) = {
638\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700639\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700640\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600641\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600642};
643
Simon Glass1b272732020-10-03 11:31:25 -0600644/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300645static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600646\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
647};
648U_BOOT_DEVICE(test3) = {
649\t.name\t\t= "test3",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700650\t.plat\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700651\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600652\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600653};
654
Walter Lozano51f12632020-06-25 01:10:13 -0300655''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600656
657 def test_addresses32(self):
658 """Test output from a node with a 'reg' property with na=1, ns=1"""
659 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
660 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300661 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600662 with open(output) as infile:
663 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700664 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600665struct dtd_test1 {
666\tfdt32_t\t\treg[2];
667};
668struct dtd_test2 {
669\tfdt32_t\t\treg[4];
670};
671''', data)
672
Walter Lozano361e7332020-06-25 01:10:08 -0300673 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600674 with open(output) as infile:
675 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700676 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600677/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300678static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600679\t.reg\t\t\t= {0x1234, 0x5678},
680};
681U_BOOT_DEVICE(test1) = {
682\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700683\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700684\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600685\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600686};
687
Simon Glass1b272732020-10-03 11:31:25 -0600688/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300689static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600690\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
691};
692U_BOOT_DEVICE(test2) = {
693\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700694\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700695\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600696\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600697};
698
Walter Lozano51f12632020-06-25 01:10:13 -0300699''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600700
701 def test_addresses64_32(self):
702 """Test output from a node with a 'reg' property with na=2, ns=1"""
703 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
704 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300705 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600706 with open(output) as infile:
707 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700708 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600709struct dtd_test1 {
710\tfdt64_t\t\treg[2];
711};
712struct dtd_test2 {
713\tfdt64_t\t\treg[2];
714};
715struct dtd_test3 {
716\tfdt64_t\t\treg[4];
717};
718''', data)
719
Walter Lozano361e7332020-06-25 01:10:08 -0300720 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600721 with open(output) as infile:
722 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700723 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600724/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300725static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600726\t.reg\t\t\t= {0x123400000000, 0x5678},
727};
728U_BOOT_DEVICE(test1) = {
729\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700730\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700731\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600732\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600733};
734
Simon Glass1b272732020-10-03 11:31:25 -0600735/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300736static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600737\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
738};
739U_BOOT_DEVICE(test2) = {
740\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700741\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700742\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600743\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600744};
745
Simon Glass1b272732020-10-03 11:31:25 -0600746/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300747static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600748\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
749};
750U_BOOT_DEVICE(test3) = {
751\t.name\t\t= "test3",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700752\t.plat\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700753\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600754\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600755};
756
Walter Lozano51f12632020-06-25 01:10:13 -0300757''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600758
759 def test_addresses32_64(self):
760 """Test output from a node with a 'reg' property with na=1, ns=2"""
761 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
762 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300763 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600764 with open(output) as infile:
765 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700766 self._check_strings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600767struct dtd_test1 {
768\tfdt64_t\t\treg[2];
769};
770struct dtd_test2 {
771\tfdt64_t\t\treg[2];
772};
773struct dtd_test3 {
774\tfdt64_t\t\treg[4];
775};
776''', data)
777
Walter Lozano361e7332020-06-25 01:10:08 -0300778 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600779 with open(output) as infile:
780 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700781 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600782/* Node /test1 index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300783static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600784\t.reg\t\t\t= {0x1234, 0x567800000000},
785};
786U_BOOT_DEVICE(test1) = {
787\t.name\t\t= "test1",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700788\t.plat\t= &dtv_test1,
Simon Glass4f500862020-12-03 16:55:19 -0700789\t.plat_size\t= sizeof(dtv_test1),
Simon Glasse41651f2020-10-03 11:31:35 -0600790\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600791};
792
Simon Glass1b272732020-10-03 11:31:25 -0600793/* Node /test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300794static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600795\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
796};
797U_BOOT_DEVICE(test2) = {
798\t.name\t\t= "test2",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700799\t.plat\t= &dtv_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700800\t.plat_size\t= sizeof(dtv_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600801\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600802};
803
Simon Glass1b272732020-10-03 11:31:25 -0600804/* Node /test3 index 2 */
Walter Lozano51f12632020-06-25 01:10:13 -0300805static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600806\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
807};
808U_BOOT_DEVICE(test3) = {
809\t.name\t\t= "test3",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700810\t.plat\t= &dtv_test3,
Simon Glass4f500862020-12-03 16:55:19 -0700811\t.plat_size\t= sizeof(dtv_test3),
Simon Glasse41651f2020-10-03 11:31:35 -0600812\t.parent_idx\t= -1,
Simon Glassc20ee0e2017-08-29 14:15:50 -0600813};
814
Walter Lozano51f12632020-06-25 01:10:13 -0300815''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass8512ea22018-07-06 10:27:35 -0600816
817 def test_bad_reg(self):
818 """Test that a reg property with an invalid type generates an error"""
Simon Glassfe57c782018-07-06 10:27:37 -0600819 # Capture stderr since dtc will emit warnings for this file
820 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600821 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700822 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300823 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600824 self.assertIn("Node 'spl-test' reg property is not an int",
Simon Glass67b5ec52020-12-28 20:34:47 -0700825 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600826
827 def test_bad_reg2(self):
828 """Test that a reg property with an invalid cell count is detected"""
Simon Glassfe57c782018-07-06 10:27:37 -0600829 # Capture stderr since dtc will emit warnings for this file
830 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600831 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700832 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300833 self.run_test(['struct'], dtb_file, output)
Simon Glass67b5ec52020-12-28 20:34:47 -0700834 self.assertIn(
835 "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
836 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600837
838 def test_add_prop(self):
839 """Test that a subequent node can add a new property to a struct"""
840 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
841 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300842 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600843 with open(output) as infile:
844 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700845 self._check_strings(HEADER + '''
Simon Glass8512ea22018-07-06 10:27:35 -0600846struct dtd_sandbox_spl_test {
847\tfdt32_t\t\tintarray;
848\tfdt32_t\t\tintval;
849};
850''', data)
851
Walter Lozano361e7332020-06-25 01:10:08 -0300852 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600853 with open(output) as infile:
854 data = infile.read()
Simon Glass67b5ec52020-12-28 20:34:47 -0700855 self._check_strings(C_HEADER + '''
Simon Glass1b272732020-10-03 11:31:25 -0600856/* Node /spl-test index 0 */
Walter Lozano51f12632020-06-25 01:10:13 -0300857static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass8512ea22018-07-06 10:27:35 -0600858\t.intval\t\t\t= 0x1,
859};
860U_BOOT_DEVICE(spl_test) = {
861\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700862\t.plat\t= &dtv_spl_test,
Simon Glass4f500862020-12-03 16:55:19 -0700863\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glasse41651f2020-10-03 11:31:35 -0600864\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600865};
866
Simon Glass1b272732020-10-03 11:31:25 -0600867/* Node /spl-test2 index 1 */
Walter Lozano51f12632020-06-25 01:10:13 -0300868static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600869\t.intarray\t\t= 0x5,
870};
871U_BOOT_DEVICE(spl_test2) = {
872\t.name\t\t= "sandbox_spl_test",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700873\t.plat\t= &dtv_spl_test2,
Simon Glass4f500862020-12-03 16:55:19 -0700874\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glasse41651f2020-10-03 11:31:35 -0600875\t.parent_idx\t= -1,
Simon Glass8512ea22018-07-06 10:27:35 -0600876};
877
Walter Lozano51f12632020-06-25 01:10:13 -0300878''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass8512ea22018-07-06 10:27:35 -0600879
Simon Glass67b5ec52020-12-28 20:34:47 -0700880 def test_stdout(self):
Simon Glass8512ea22018-07-06 10:27:35 -0600881 """Test output to stdout"""
882 dtb_file = get_dtb_file('dtoc_test_simple.dts')
Simon Glassde846cb2020-12-28 20:34:49 -0700883 with test_util.capture_sys_output() as (stdout, _):
Simon Glassf62cea02020-12-28 20:34:48 -0700884 self.run_test(['struct'], dtb_file, None)
Simon Glassde846cb2020-12-28 20:34:49 -0700885 self._check_strings(self.struct_text, stdout.getvalue())
Simon Glass8512ea22018-07-06 10:27:35 -0600886
Simon Glass67b5ec52020-12-28 20:34:47 -0700887 def test_no_command(self):
Simon Glass8512ea22018-07-06 10:27:35 -0600888 """Test running dtoc without a command"""
Simon Glass67b5ec52020-12-28 20:34:47 -0700889 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300890 self.run_test([], '', '')
Simon Glass8512ea22018-07-06 10:27:35 -0600891 self.assertIn("Please specify a command: struct, platdata",
Simon Glass67b5ec52020-12-28 20:34:47 -0700892 str(exc.exception))
Simon Glass8512ea22018-07-06 10:27:35 -0600893
Simon Glass67b5ec52020-12-28 20:34:47 -0700894 def test_bad_command(self):
Simon Glass8512ea22018-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 Glass67b5ec52020-12-28 20:34:47 -0700898 with self.assertRaises(ValueError) as exc:
Walter Lozano361e7332020-06-25 01:10:08 -0300899 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600900 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
Simon Glass67b5ec52020-12-28 20:34:47 -0700901 str(exc.exception))
Walter Lozano6c74d1b2020-07-28 19:06:23 -0300902
Simon Glass67b5ec52020-12-28 20:34:47 -0700903 @staticmethod
904 def test_scan_drivers():
Walter Lozano6c74d1b2020-07-28 19:06:23 -0300905 """Test running dtoc with additional drivers to scan"""
906 dtb_file = get_dtb_file('dtoc_test_simple.dts')
907 output = tools.GetOutputFilename('output')
Simon Glass67b5ec52020-12-28 20:34:47 -0700908 with test_util.capture_sys_output() as _:
909 dtb_platdata.run_steps(
Simon Glass192c1112020-12-28 20:34:50 -0700910 ['struct'], dtb_file, False, output, [], True,
Simon Glass67b5ec52020-12-28 20:34:47 -0700911 [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
Walter Lozano6c74d1b2020-07-28 19:06:23 -0300912
Simon Glass67b5ec52020-12-28 20:34:47 -0700913 @staticmethod
914 def test_unicode_error():
Walter Lozano6c74d1b2020-07-28 19:06:23 -0300915 """Test running dtoc with an invalid unicode file
916
917 To be able to perform this test without adding a weird text file which
918 would produce issues when using checkpatch.pl or patman, generate the
919 file at runtime and then process it.
920 """
921 dtb_file = get_dtb_file('dtoc_test_simple.dts')
922 output = tools.GetOutputFilename('output')
923 driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
Simon Glass67b5ec52020-12-28 20:34:47 -0700924 with open(driver_fn, 'wb+') as fout:
925 fout.write(b'\x81')
Walter Lozano6c74d1b2020-07-28 19:06:23 -0300926
Simon Glass67b5ec52020-12-28 20:34:47 -0700927 with test_util.capture_sys_output() as _:
Simon Glass192c1112020-12-28 20:34:50 -0700928 dtb_platdata.run_steps(['struct'], dtb_file, False, output, [],
929 True, [driver_fn])
Simon Glass7d637c12020-12-23 08:11:23 -0700930
Simon Glass67b5ec52020-12-28 20:34:47 -0700931 def test_driver(self):
Simon Glass7d637c12020-12-23 08:11:23 -0700932 """Test the Driver class"""
933 drv1 = dtb_platdata.Driver('fred')
934 drv2 = dtb_platdata.Driver('mary')
935 drv3 = dtb_platdata.Driver('fred')
936 self.assertEqual("Driver(name='fred')", str(drv1))
937 self.assertEqual(drv1, drv3)
938 self.assertNotEqual(drv1, drv2)
939 self.assertNotEqual(drv2, drv3)