blob: 49ab75b85dab79b8842e4bf3614c6fb8e15fd513 [file] [log] [blame]
Simon Glassbfb0bb22019-10-31 07:42:55 -06001#!/usr/bin/env python3
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass9d2eb922017-06-18 22:09:06 -06003# Copyright (c) 2012 The Chromium OS Authors.
4#
Simon Glass9d2eb922017-06-18 22:09:06 -06005
6"""Tests for the dtb_platdata module
7
Simon Glass70cd0d72018-07-06 10:27:20 -06008This includes unit tests for some functions and functional tests for the dtoc
9tool.
Simon Glass9d2eb922017-06-18 22:09:06 -060010"""
11
12import collections
13import os
14import struct
Walter Lozanoa324e412020-06-25 01:10:08 -030015import sys
Walter Lozanod82062b2020-07-28 19:06:23 -030016import tempfile
Simon Glass9d2eb922017-06-18 22:09:06 -060017import unittest
18
Simon Glassa997ea52020-04-17 18:09:04 -060019from dtoc import dtb_platdata
Simon Glass9d2eb922017-06-18 22:09:06 -060020from dtb_platdata import conv_name_to_c
21from dtb_platdata import get_compat_name
22from dtb_platdata import get_value
23from dtb_platdata import tab_to
Simon Glassa997ea52020-04-17 18:09:04 -060024from dtoc import fdt
25from dtoc import fdt_util
26from patman import test_util
27from patman import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060028
29our_path = os.path.dirname(os.path.realpath(__file__))
30
31
Simon Glasseb37e2d2017-11-12 21:52:17 -070032HEADER = '''/*
33 * DO NOT MODIFY
34 *
35 * This file was generated by dtoc from a .dtb (device tree binary) file.
36 */
37
38#include <stdbool.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090039#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070040
41C_HEADER = '''/*
42 * DO NOT MODIFY
43 *
44 * This file was generated by dtoc from a .dtb (device tree binary) file.
45 */
46
Simon Glass4c73d7b2020-10-03 11:31:41 -060047/* Allow use of U_BOOT_DEVICE() in this file */
48#define DT_PLATDATA_C
49
Simon Glasseb37e2d2017-11-12 21:52:17 -070050#include <common.h>
51#include <dm.h>
52#include <dt-structs.h>
53'''
54
Walter Lozanodc5b4372020-06-25 01:10:13 -030055C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
56}
57'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070058
Simon Glass3bce93d2018-07-06 10:27:37 -060059
60def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060061 """Compile a .dts file to a .dtb
62
63 Args:
64 dts_fname: Filename of .dts file in the current directory
Simon Glass3bce93d2018-07-06 10:27:37 -060065 capture_stderr: True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060066
67 Returns:
68 Filename of compiled file in output directory
69 """
Simon Glass3bce93d2018-07-06 10:27:37 -060070 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
71 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060072
73
74class TestDtoc(unittest.TestCase):
75 """Tests for dtoc"""
76 @classmethod
77 def setUpClass(cls):
78 tools.PrepareOutputDir(None)
Simon Glass7f5e2262020-07-07 21:32:06 -060079 cls.maxDiff = None
Simon Glass9d2eb922017-06-18 22:09:06 -060080
81 @classmethod
82 def tearDownClass(cls):
83 tools._RemoveOutputDir()
84
Simon Glassc47c2b32018-07-06 10:27:25 -060085 def _WritePythonString(self, fname, data):
86 """Write a string with tabs expanded as done in this Python file
87
88 Args:
89 fname: Filename to write to
90 data: Raw string to convert
91 """
92 data = data.replace('\t', '\\t')
93 with open(fname, 'w') as fd:
94 fd.write(data)
95
96 def _CheckStrings(self, expected, actual):
97 """Check that a string matches its expected value
98
99 If the strings do not match, they are written to the /tmp directory in
100 the same Python format as is used here in the test. This allows for
101 easy comparison and update of the tests.
102
103 Args:
104 expected: Expected string
105 actual: Actual string
106 """
107 if expected != actual:
108 self._WritePythonString('/tmp/binman.expected', expected)
109 self._WritePythonString('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600110 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassc47c2b32018-07-06 10:27:25 -0600111 self.assertEquals(expected, actual)
112
Walter Lozanoa324e412020-06-25 01:10:08 -0300113
114 def run_test(self, args, dtb_file, output):
115 dtb_platdata.run_steps(args, dtb_file, False, output, True)
116
Simon Glass9d2eb922017-06-18 22:09:06 -0600117 def test_name(self):
118 """Test conversion of device tree names to C identifiers"""
119 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
120 self.assertEqual('vendor_clock_frequency',
121 conv_name_to_c('vendor,clock-frequency'))
122 self.assertEqual('rockchip_rk3399_sdhci_5_1',
123 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
124
125 def test_tab_to(self):
126 """Test operation of tab_to() function"""
127 self.assertEqual('fred ', tab_to(0, 'fred'))
128 self.assertEqual('fred\t', tab_to(1, 'fred'))
129 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
130 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
131 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
132 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
133
134 def test_get_value(self):
135 """Test operation of get_value() function"""
136 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700137 get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600138 self.assertEqual('0x45',
Simon Glassc9a032c2020-11-08 20:36:17 -0700139 get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
Simon Glass9d2eb922017-06-18 22:09:06 -0600140 self.assertEqual('0x0',
Simon Glassc9a032c2020-11-08 20:36:17 -0700141 get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
142 self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
143 self.assertEqual('true', get_value(fdt.Type.BOOL, None))
Simon Glass9d2eb922017-06-18 22:09:06 -0600144
145 def test_get_compat_name(self):
146 """Test operation of get_compat_name() function"""
147 Prop = collections.namedtuple('Prop', ['value'])
148 Node = collections.namedtuple('Node', ['props'])
149
150 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
151 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300152 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600153 get_compat_name(node))
154
155 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
156 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300157 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600158 get_compat_name(node))
159
160 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
161 node = Node({'compatible': prop})
Walter Lozano5fe734c2020-07-23 00:22:03 -0300162 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
163 'arasan_sdhci_5_1', 'third']),
Simon Glass9d2eb922017-06-18 22:09:06 -0600164 get_compat_name(node))
165
166 def test_empty_file(self):
167 """Test output from a device tree file with no nodes"""
168 dtb_file = get_dtb_file('dtoc_test_empty.dts')
169 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300170 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600171 with open(output) as infile:
172 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700173 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600174
Walter Lozanoa324e412020-06-25 01:10:08 -0300175 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600176 with open(output) as infile:
177 lines = infile.read().splitlines()
Walter Lozanodc5b4372020-06-25 01:10:13 -0300178 self.assertEqual(C_HEADER.splitlines() + [''] +
179 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600180
181 def test_simple(self):
182 """Test output from some simple nodes with various types of data"""
183 dtb_file = get_dtb_file('dtoc_test_simple.dts')
184 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300185 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600186 with open(output) as infile:
187 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600188 self._CheckStrings(HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600189struct dtd_sandbox_i2c_test {
190};
191struct dtd_sandbox_pmic_test {
192\tbool\t\tlow_power;
193\tfdt64_t\t\treg[2];
194};
Simon Glass9d2eb922017-06-18 22:09:06 -0600195struct dtd_sandbox_spl_test {
Simon Glass7f5e2262020-07-07 21:32:06 -0600196\tconst char * acpi_name;
Simon Glass9d2eb922017-06-18 22:09:06 -0600197\tbool\t\tboolval;
198\tunsigned char\tbytearray[3];
199\tunsigned char\tbyteval;
200\tfdt32_t\t\tintarray[4];
201\tfdt32_t\t\tintval;
202\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600203\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600204\tconst char *\tstringarray[3];
205\tconst char *\tstringval;
206};
Simon Glass9d2eb922017-06-18 22:09:06 -0600207''', data)
208
Walter Lozanoa324e412020-06-25 01:10:08 -0300209 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600210 with open(output) as infile:
211 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600212 self._CheckStrings(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};
216U_BOOT_DEVICE(i2c_at_0) = {
217\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};
228U_BOOT_DEVICE(pmic_at_9) = {
229\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};
248U_BOOT_DEVICE(spl_test) = {
249\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};
267U_BOOT_DEVICE(spl_test2) = {
268\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};
280U_BOOT_DEVICE(spl_test3) = {
281\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
Walter Lozanodc5b4372020-06-25 01:10:13 -0300287''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600288
Walter Lozanoe675d962020-07-03 08:07:17 -0300289 def test_driver_alias(self):
290 """Test output from a device tree file with a driver alias"""
291 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
292 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300293 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300294 with open(output) as infile:
295 data = infile.read()
296 self._CheckStrings(HEADER + '''
297struct dtd_sandbox_gpio {
298\tconst char *\tgpio_bank_name;
299\tbool\t\tgpio_controller;
300\tfdt32_t\t\tsandbox_gpio_count;
301};
Walter Lozanoe675d962020-07-03 08:07:17 -0300302''', data)
303
Walter Lozanoa324e412020-06-25 01:10:08 -0300304 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300305 with open(output) as infile:
306 data = infile.read()
307 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600308/* Node /gpios@0 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300309static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300310\t.gpio_bank_name\t\t= "a",
311\t.gpio_controller\t= true,
312\t.sandbox_gpio_count\t= 0x14,
313};
314U_BOOT_DEVICE(gpios_at_0) = {
315\t.name\t\t= "sandbox_gpio",
Simon Glass71fa5b42020-12-03 16:55:18 -0700316\t.plat\t= &dtv_gpios_at_0,
Simon Glass39edb952020-12-03 16:55:19 -0700317\t.plat_size\t= sizeof(dtv_gpios_at_0),
Simon Glass36b15e22020-10-03 11:31:35 -0600318\t.parent_idx\t= -1,
Walter Lozanoe675d962020-07-03 08:07:17 -0300319};
320
Walter Lozanodc5b4372020-06-25 01:10:13 -0300321void dm_populate_phandle_data(void) {
322}
Walter Lozanoe675d962020-07-03 08:07:17 -0300323''', data)
324
Walter Lozanoa324e412020-06-25 01:10:08 -0300325 def test_invalid_driver(self):
326 """Test output from a device tree file with an invalid driver"""
327 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
328 output = tools.GetOutputFilename('output')
329 with test_util.capture_sys_output() as (stdout, stderr):
330 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
331 with open(output) as infile:
332 data = infile.read()
333 self._CheckStrings(HEADER + '''
334struct dtd_invalid {
335};
336''', data)
337
338 with test_util.capture_sys_output() as (stdout, stderr):
339 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
340 with open(output) as infile:
341 data = infile.read()
342 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600343/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300344static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300345};
346U_BOOT_DEVICE(spl_test) = {
347\t.name\t\t= "invalid",
Simon Glass71fa5b42020-12-03 16:55:18 -0700348\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700349\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600350\t.parent_idx\t= -1,
Walter Lozanoa324e412020-06-25 01:10:08 -0300351};
352
Walter Lozanodc5b4372020-06-25 01:10:13 -0300353void dm_populate_phandle_data(void) {
354}
Walter Lozanoa324e412020-06-25 01:10:08 -0300355''', data)
356
Simon Glass9d2eb922017-06-18 22:09:06 -0600357 def test_phandle(self):
358 """Test output from a node containing a phandle reference"""
359 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
360 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300361 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600362 with open(output) as infile:
363 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600364 self._CheckStrings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600365struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600366\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600367};
368struct dtd_target {
369\tfdt32_t\t\tintval;
370};
371''', data)
372
Walter Lozanoa324e412020-06-25 01:10:08 -0300373 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600374 with open(output) as infile:
375 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600376 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600377/* Node /phandle2-target index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300378static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600379\t.intval\t\t\t= 0x1,
380};
381U_BOOT_DEVICE(phandle2_target) = {
382\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700383\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700384\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600385\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600386};
387
Simon Glass192f8132020-10-03 11:31:25 -0600388/* Node /phandle3-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300389static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600390\t.intval\t\t\t= 0x2,
391};
392U_BOOT_DEVICE(phandle3_target) = {
393\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700394\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700395\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600396\t.parent_idx\t= -1,
Simon Glass3deeb472017-08-29 14:15:59 -0600397};
398
Simon Glass192f8132020-10-03 11:31:25 -0600399/* Node /phandle-target index 4 */
400static struct dtd_target dtv_phandle_target = {
401\t.intval\t\t\t= 0x0,
402};
403U_BOOT_DEVICE(phandle_target) = {
404\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700405\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700406\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600407\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600408};
409
410/* Node /phandle-source index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300411static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600412\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600413\t\t\t{4, {}},
414\t\t\t{0, {11}},
415\t\t\t{1, {12, 13}},
416\t\t\t{4, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600417};
418U_BOOT_DEVICE(phandle_source) = {
419\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700420\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700421\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600422\t.parent_idx\t= -1,
Simon Glass9d2eb922017-06-18 22:09:06 -0600423};
424
Simon Glass192f8132020-10-03 11:31:25 -0600425/* Node /phandle-source2 index 3 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300426static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600427\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600428\t\t\t{4, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600429};
430U_BOOT_DEVICE(phandle_source2) = {
431\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700432\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700433\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600434\t.parent_idx\t= -1,
Simon Glass609e2b12018-07-06 10:27:31 -0600435};
436
Walter Lozanodc5b4372020-06-25 01:10:13 -0300437void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300438}
Simon Glass9d2eb922017-06-18 22:09:06 -0600439''', data)
440
Simon Glass961c1ce2018-07-06 10:27:35 -0600441 def test_phandle_single(self):
442 """Test output from a node containing a phandle reference"""
443 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
444 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300445 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600446 with open(output) as infile:
447 data = infile.read()
448 self._CheckStrings(HEADER + '''
449struct dtd_source {
450\tstruct phandle_0_arg clocks[1];
451};
452struct dtd_target {
453\tfdt32_t\t\tintval;
454};
455''', data)
456
457 def test_phandle_reorder(self):
458 """Test that phandle targets are generated before their references"""
459 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
460 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300461 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600462 with open(output) as infile:
463 data = infile.read()
464 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600465/* Node /phandle-target index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300466static struct dtd_target dtv_phandle_target = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600467};
468U_BOOT_DEVICE(phandle_target) = {
469\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700470\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700471\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600472\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600473};
474
Simon Glass192f8132020-10-03 11:31:25 -0600475/* Node /phandle-source2 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300476static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600477\t.clocks\t\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600478\t\t\t{1, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -0600479};
480U_BOOT_DEVICE(phandle_source2) = {
481\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700482\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700483\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600484\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600485};
486
Walter Lozanodc5b4372020-06-25 01:10:13 -0300487void dm_populate_phandle_data(void) {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300488}
Simon Glass961c1ce2018-07-06 10:27:35 -0600489''', data)
490
Walter Lozano5541fc02020-06-25 01:10:17 -0300491 def test_phandle_cd_gpio(self):
492 """Test that phandle targets are generated when unsing cd-gpios"""
493 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
494 output = tools.GetOutputFilename('output')
495 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True)
496 with open(output) as infile:
497 data = infile.read()
498 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600499/* Node /phandle2-target index 0 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300500static struct dtd_target dtv_phandle2_target = {
501\t.intval\t\t\t= 0x1,
502};
503U_BOOT_DEVICE(phandle2_target) = {
504\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700505\t.plat\t= &dtv_phandle2_target,
Simon Glass39edb952020-12-03 16:55:19 -0700506\t.plat_size\t= sizeof(dtv_phandle2_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600507\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300508};
509
Simon Glass192f8132020-10-03 11:31:25 -0600510/* Node /phandle3-target index 1 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300511static struct dtd_target dtv_phandle3_target = {
512\t.intval\t\t\t= 0x2,
513};
514U_BOOT_DEVICE(phandle3_target) = {
515\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700516\t.plat\t= &dtv_phandle3_target,
Simon Glass39edb952020-12-03 16:55:19 -0700517\t.plat_size\t= sizeof(dtv_phandle3_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600518\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300519};
520
Simon Glass192f8132020-10-03 11:31:25 -0600521/* Node /phandle-target index 4 */
522static struct dtd_target dtv_phandle_target = {
523\t.intval\t\t\t= 0x0,
524};
525U_BOOT_DEVICE(phandle_target) = {
526\t.name\t\t= "target",
Simon Glass71fa5b42020-12-03 16:55:18 -0700527\t.plat\t= &dtv_phandle_target,
Simon Glass39edb952020-12-03 16:55:19 -0700528\t.plat_size\t= sizeof(dtv_phandle_target),
Simon Glass36b15e22020-10-03 11:31:35 -0600529\t.parent_idx\t= -1,
Simon Glass192f8132020-10-03 11:31:25 -0600530};
531
532/* Node /phandle-source index 2 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300533static struct dtd_source dtv_phandle_source = {
534\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600535\t\t\t{4, {}},
536\t\t\t{0, {11}},
537\t\t\t{1, {12, 13}},
538\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300539};
540U_BOOT_DEVICE(phandle_source) = {
541\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700542\t.plat\t= &dtv_phandle_source,
Simon Glass39edb952020-12-03 16:55:19 -0700543\t.plat_size\t= sizeof(dtv_phandle_source),
Simon Glass36b15e22020-10-03 11:31:35 -0600544\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300545};
546
Simon Glass192f8132020-10-03 11:31:25 -0600547/* Node /phandle-source2 index 3 */
Walter Lozano5541fc02020-06-25 01:10:17 -0300548static struct dtd_source dtv_phandle_source2 = {
549\t.cd_gpios\t\t= {
Simon Glass5792f4b2020-10-03 11:31:40 -0600550\t\t\t{4, {}},},
Walter Lozano5541fc02020-06-25 01:10:17 -0300551};
552U_BOOT_DEVICE(phandle_source2) = {
553\t.name\t\t= "source",
Simon Glass71fa5b42020-12-03 16:55:18 -0700554\t.plat\t= &dtv_phandle_source2,
Simon Glass39edb952020-12-03 16:55:19 -0700555\t.plat_size\t= sizeof(dtv_phandle_source2),
Simon Glass36b15e22020-10-03 11:31:35 -0600556\t.parent_idx\t= -1,
Walter Lozano5541fc02020-06-25 01:10:17 -0300557};
558
559void dm_populate_phandle_data(void) {
Walter Lozano5541fc02020-06-25 01:10:17 -0300560}
561''', data)
562
Simon Glass961c1ce2018-07-06 10:27:35 -0600563 def test_phandle_bad(self):
564 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600565 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
566 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600567 output = tools.GetOutputFilename('output')
568 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300569 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600570 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
571 str(e.exception))
572
573 def test_phandle_bad2(self):
574 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600575 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
576 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600577 output = tools.GetOutputFilename('output')
578 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300579 self.run_test(['struct'], dtb_file, output)
Walter Lozano179f0b62020-06-25 01:10:16 -0300580 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glass961c1ce2018-07-06 10:27:35 -0600581 str(e.exception))
582
Simon Glass1b1fe412017-08-29 14:15:50 -0600583 def test_addresses64(self):
584 """Test output from a node with a 'reg' property with na=2, ns=2"""
585 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
586 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300587 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600588 with open(output) as infile:
589 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600590 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600591struct dtd_test1 {
592\tfdt64_t\t\treg[2];
593};
594struct dtd_test2 {
595\tfdt64_t\t\treg[2];
596};
597struct dtd_test3 {
598\tfdt64_t\t\treg[4];
599};
600''', data)
601
Walter Lozanoa324e412020-06-25 01:10:08 -0300602 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600603 with open(output) as infile:
604 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600605 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600606/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300607static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600608\t.reg\t\t\t= {0x1234, 0x5678},
609};
610U_BOOT_DEVICE(test1) = {
611\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700612\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700613\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600614\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600615};
616
Simon Glass192f8132020-10-03 11:31:25 -0600617/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300618static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600619\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
620};
621U_BOOT_DEVICE(test2) = {
622\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700623\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700624\t.plat_size\t= sizeof(dtv_test2),
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 /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300629static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600630\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
631};
632U_BOOT_DEVICE(test3) = {
633\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700634\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700635\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600636\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600637};
638
Walter Lozanodc5b4372020-06-25 01:10:13 -0300639''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600640
641 def test_addresses32(self):
642 """Test output from a node with a 'reg' property with na=1, ns=1"""
643 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
644 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300645 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600646 with open(output) as infile:
647 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600648 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600649struct dtd_test1 {
650\tfdt32_t\t\treg[2];
651};
652struct dtd_test2 {
653\tfdt32_t\t\treg[4];
654};
655''', data)
656
Walter Lozanoa324e412020-06-25 01:10:08 -0300657 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600658 with open(output) as infile:
659 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600660 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600661/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300662static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600663\t.reg\t\t\t= {0x1234, 0x5678},
664};
665U_BOOT_DEVICE(test1) = {
666\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700667\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700668\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600669\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600670};
671
Simon Glass192f8132020-10-03 11:31:25 -0600672/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300673static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600674\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
675};
676U_BOOT_DEVICE(test2) = {
677\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700678\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700679\t.plat_size\t= sizeof(dtv_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600680\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600681};
682
Walter Lozanodc5b4372020-06-25 01:10:13 -0300683''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600684
685 def test_addresses64_32(self):
686 """Test output from a node with a 'reg' property with na=2, ns=1"""
687 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
688 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300689 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600690 with open(output) as infile:
691 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600692 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600693struct dtd_test1 {
694\tfdt64_t\t\treg[2];
695};
696struct dtd_test2 {
697\tfdt64_t\t\treg[2];
698};
699struct dtd_test3 {
700\tfdt64_t\t\treg[4];
701};
702''', data)
703
Walter Lozanoa324e412020-06-25 01:10:08 -0300704 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600705 with open(output) as infile:
706 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600707 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600708/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300709static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600710\t.reg\t\t\t= {0x123400000000, 0x5678},
711};
712U_BOOT_DEVICE(test1) = {
713\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700714\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700715\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600716\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600717};
718
Simon Glass192f8132020-10-03 11:31:25 -0600719/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300720static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600721\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
722};
723U_BOOT_DEVICE(test2) = {
724\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700725\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700726\t.plat_size\t= sizeof(dtv_test2),
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 /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300731static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600732\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
733};
734U_BOOT_DEVICE(test3) = {
735\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700736\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700737\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600738\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600739};
740
Walter Lozanodc5b4372020-06-25 01:10:13 -0300741''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600742
743 def test_addresses32_64(self):
744 """Test output from a node with a 'reg' property with na=1, ns=2"""
745 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
746 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300747 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600748 with open(output) as infile:
749 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600750 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600751struct dtd_test1 {
752\tfdt64_t\t\treg[2];
753};
754struct dtd_test2 {
755\tfdt64_t\t\treg[2];
756};
757struct dtd_test3 {
758\tfdt64_t\t\treg[4];
759};
760''', data)
761
Walter Lozanoa324e412020-06-25 01:10:08 -0300762 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600763 with open(output) as infile:
764 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600765 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600766/* Node /test1 index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300767static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600768\t.reg\t\t\t= {0x1234, 0x567800000000},
769};
770U_BOOT_DEVICE(test1) = {
771\t.name\t\t= "test1",
Simon Glass71fa5b42020-12-03 16:55:18 -0700772\t.plat\t= &dtv_test1,
Simon Glass39edb952020-12-03 16:55:19 -0700773\t.plat_size\t= sizeof(dtv_test1),
Simon Glass36b15e22020-10-03 11:31:35 -0600774\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600775};
776
Simon Glass192f8132020-10-03 11:31:25 -0600777/* Node /test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300778static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600779\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
780};
781U_BOOT_DEVICE(test2) = {
782\t.name\t\t= "test2",
Simon Glass71fa5b42020-12-03 16:55:18 -0700783\t.plat\t= &dtv_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700784\t.plat_size\t= sizeof(dtv_test2),
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 /test3 index 2 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300789static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600790\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
791};
792U_BOOT_DEVICE(test3) = {
793\t.name\t\t= "test3",
Simon Glass71fa5b42020-12-03 16:55:18 -0700794\t.plat\t= &dtv_test3,
Simon Glass39edb952020-12-03 16:55:19 -0700795\t.plat_size\t= sizeof(dtv_test3),
Simon Glass36b15e22020-10-03 11:31:35 -0600796\t.parent_idx\t= -1,
Simon Glass1b1fe412017-08-29 14:15:50 -0600797};
798
Walter Lozanodc5b4372020-06-25 01:10:13 -0300799''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600800
801 def test_bad_reg(self):
802 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600803 # Capture stderr since dtc will emit warnings for this file
804 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600805 output = tools.GetOutputFilename('output')
806 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300807 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600808 self.assertIn("Node 'spl-test' reg property is not an int",
809 str(e.exception))
810
811 def test_bad_reg2(self):
812 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600813 # Capture stderr since dtc will emit warnings for this file
814 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600815 output = tools.GetOutputFilename('output')
816 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300817 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600818 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
819 str(e.exception))
820
821 def test_add_prop(self):
822 """Test that a subequent node can add a new property to a struct"""
823 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
824 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300825 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600826 with open(output) as infile:
827 data = infile.read()
828 self._CheckStrings(HEADER + '''
829struct dtd_sandbox_spl_test {
830\tfdt32_t\t\tintarray;
831\tfdt32_t\t\tintval;
832};
833''', data)
834
Walter Lozanoa324e412020-06-25 01:10:08 -0300835 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600836 with open(output) as infile:
837 data = infile.read()
838 self._CheckStrings(C_HEADER + '''
Simon Glass192f8132020-10-03 11:31:25 -0600839/* Node /spl-test index 0 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300840static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600841\t.intval\t\t\t= 0x1,
842};
843U_BOOT_DEVICE(spl_test) = {
844\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700845\t.plat\t= &dtv_spl_test,
Simon Glass39edb952020-12-03 16:55:19 -0700846\t.plat_size\t= sizeof(dtv_spl_test),
Simon Glass36b15e22020-10-03 11:31:35 -0600847\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600848};
849
Simon Glass192f8132020-10-03 11:31:25 -0600850/* Node /spl-test2 index 1 */
Walter Lozanodc5b4372020-06-25 01:10:13 -0300851static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600852\t.intarray\t\t= 0x5,
853};
854U_BOOT_DEVICE(spl_test2) = {
855\t.name\t\t= "sandbox_spl_test",
Simon Glass71fa5b42020-12-03 16:55:18 -0700856\t.plat\t= &dtv_spl_test2,
Simon Glass39edb952020-12-03 16:55:19 -0700857\t.plat_size\t= sizeof(dtv_spl_test2),
Simon Glass36b15e22020-10-03 11:31:35 -0600858\t.parent_idx\t= -1,
Simon Glass961c1ce2018-07-06 10:27:35 -0600859};
860
Walter Lozanodc5b4372020-06-25 01:10:13 -0300861''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600862
863 def testStdout(self):
864 """Test output to stdout"""
865 dtb_file = get_dtb_file('dtoc_test_simple.dts')
866 with test_util.capture_sys_output() as (stdout, stderr):
Walter Lozanoa324e412020-06-25 01:10:08 -0300867 self.run_test(['struct'], dtb_file, '-')
Simon Glass961c1ce2018-07-06 10:27:35 -0600868
869 def testNoCommand(self):
870 """Test running dtoc without a command"""
871 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300872 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600873 self.assertIn("Please specify a command: struct, platdata",
874 str(e.exception))
875
876 def testBadCommand(self):
877 """Test running dtoc with an invalid command"""
878 dtb_file = get_dtb_file('dtoc_test_simple.dts')
879 output = tools.GetOutputFilename('output')
880 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300881 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600882 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
883 str(e.exception))
Walter Lozanod82062b2020-07-28 19:06:23 -0300884
885 def testScanDrivers(self):
886 """Test running dtoc with additional drivers to scan"""
887 dtb_file = get_dtb_file('dtoc_test_simple.dts')
888 output = tools.GetOutputFilename('output')
889 with test_util.capture_sys_output() as (stdout, stderr):
890 dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
891 [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
892
893 def testUnicodeError(self):
894 """Test running dtoc with an invalid unicode file
895
896 To be able to perform this test without adding a weird text file which
897 would produce issues when using checkpatch.pl or patman, generate the
898 file at runtime and then process it.
899 """
900 dtb_file = get_dtb_file('dtoc_test_simple.dts')
901 output = tools.GetOutputFilename('output')
902 driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
903 with open(driver_fn, 'wb+') as df:
904 df.write(b'\x81')
905
906 with test_util.capture_sys_output() as (stdout, stderr):
907 dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
908 [driver_fn])