blob: f68a15dd26155a3564d20f6e51650f5609485d61 [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 Lozano361e7332020-06-25 01:10:08 -030015import sys
Walter Lozano6c74d1b2020-07-28 19:06:23 -030016import tempfile
Simon Glassc0791922017-06-18 22:09:06 -060017import unittest
18
Simon Glassbf776672020-04-17 18:09:04 -060019from dtoc import dtb_platdata
Simon Glassc0791922017-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 Glassbf776672020-04-17 18:09:04 -060024from dtoc import fdt
25from dtoc import fdt_util
26from patman import test_util
27from patman import tools
Simon Glassc0791922017-06-18 22:09:06 -060028
29our_path = os.path.dirname(os.path.realpath(__file__))
30
31
Simon Glassaab660f2017-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 Yamadab08c8c42018-03-05 01:20:11 +090039#include <linux/libfdt.h>'''
Simon Glassaab660f2017-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
47#include <common.h>
48#include <dm.h>
49#include <dt-structs.h>
50'''
51
Walter Lozano51f12632020-06-25 01:10:13 -030052C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
53}
54'''
Simon Glassaab660f2017-11-12 21:52:17 -070055
Simon Glassfe57c782018-07-06 10:27:37 -060056
57def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glassc0791922017-06-18 22:09:06 -060058 """Compile a .dts file to a .dtb
59
60 Args:
61 dts_fname: Filename of .dts file in the current directory
Simon Glassfe57c782018-07-06 10:27:37 -060062 capture_stderr: True to capture and discard stderr output
Simon Glassc0791922017-06-18 22:09:06 -060063
64 Returns:
65 Filename of compiled file in output directory
66 """
Simon Glassfe57c782018-07-06 10:27:37 -060067 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
68 capture_stderr=capture_stderr)
Simon Glassc0791922017-06-18 22:09:06 -060069
70
71class TestDtoc(unittest.TestCase):
72 """Tests for dtoc"""
73 @classmethod
74 def setUpClass(cls):
75 tools.PrepareOutputDir(None)
Simon Glassf02d0eb2020-07-07 21:32:06 -060076 cls.maxDiff = None
Simon Glassc0791922017-06-18 22:09:06 -060077
78 @classmethod
79 def tearDownClass(cls):
80 tools._RemoveOutputDir()
81
Simon Glass57f0bc42018-07-06 10:27:25 -060082 def _WritePythonString(self, fname, data):
83 """Write a string with tabs expanded as done in this Python file
84
85 Args:
86 fname: Filename to write to
87 data: Raw string to convert
88 """
89 data = data.replace('\t', '\\t')
90 with open(fname, 'w') as fd:
91 fd.write(data)
92
93 def _CheckStrings(self, expected, actual):
94 """Check that a string matches its expected value
95
96 If the strings do not match, they are written to the /tmp directory in
97 the same Python format as is used here in the test. This allows for
98 easy comparison and update of the tests.
99
100 Args:
101 expected: Expected string
102 actual: Actual string
103 """
104 if expected != actual:
105 self._WritePythonString('/tmp/binman.expected', expected)
106 self._WritePythonString('/tmp/binman.actual', actual)
Simon Glass90a81322019-05-17 22:00:31 -0600107 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glass57f0bc42018-07-06 10:27:25 -0600108 self.assertEquals(expected, actual)
109
Walter Lozano361e7332020-06-25 01:10:08 -0300110
111 def run_test(self, args, dtb_file, output):
112 dtb_platdata.run_steps(args, dtb_file, False, output, True)
113
Simon Glassc0791922017-06-18 22:09:06 -0600114 def test_name(self):
115 """Test conversion of device tree names to C identifiers"""
116 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
117 self.assertEqual('vendor_clock_frequency',
118 conv_name_to_c('vendor,clock-frequency'))
119 self.assertEqual('rockchip_rk3399_sdhci_5_1',
120 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
121
122 def test_tab_to(self):
123 """Test operation of tab_to() function"""
124 self.assertEqual('fred ', tab_to(0, 'fred'))
125 self.assertEqual('fred\t', tab_to(1, 'fred'))
126 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
127 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
128 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
129 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
130
131 def test_get_value(self):
132 """Test operation of get_value() function"""
133 self.assertEqual('0x45',
134 get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
135 self.assertEqual('0x45',
136 get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
137 self.assertEqual('0x0',
138 get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
139 self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
140 self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
141
142 def test_get_compat_name(self):
143 """Test operation of get_compat_name() function"""
144 Prop = collections.namedtuple('Prop', ['value'])
145 Node = collections.namedtuple('Node', ['props'])
146
147 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
148 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300149 self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600150 get_compat_name(node))
151
152 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
153 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300154 self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
Simon Glassc0791922017-06-18 22:09:06 -0600155 get_compat_name(node))
156
157 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
158 node = Node({'compatible': prop})
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300159 self.assertEqual((['rockchip_rk3399_sdhci_5_1',
160 'arasan_sdhci_5_1', 'third']),
Simon Glassc0791922017-06-18 22:09:06 -0600161 get_compat_name(node))
162
163 def test_empty_file(self):
164 """Test output from a device tree file with no nodes"""
165 dtb_file = get_dtb_file('dtoc_test_empty.dts')
166 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300167 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600168 with open(output) as infile:
169 lines = infile.read().splitlines()
Simon Glassaab660f2017-11-12 21:52:17 -0700170 self.assertEqual(HEADER.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600171
Walter Lozano361e7332020-06-25 01:10:08 -0300172 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600173 with open(output) as infile:
174 lines = infile.read().splitlines()
Walter Lozano51f12632020-06-25 01:10:13 -0300175 self.assertEqual(C_HEADER.splitlines() + [''] +
176 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
Simon Glassc0791922017-06-18 22:09:06 -0600177
178 def test_simple(self):
179 """Test output from some simple nodes with various types of data"""
180 dtb_file = get_dtb_file('dtoc_test_simple.dts')
181 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300182 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600183 with open(output) as infile:
184 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600185 self._CheckStrings(HEADER + '''
Simon Glass5ec741f2017-08-29 14:15:51 -0600186struct dtd_sandbox_i2c_test {
187};
188struct dtd_sandbox_pmic_test {
189\tbool\t\tlow_power;
190\tfdt64_t\t\treg[2];
191};
Simon Glassc0791922017-06-18 22:09:06 -0600192struct dtd_sandbox_spl_test {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600193\tconst char * acpi_name;
Simon Glassc0791922017-06-18 22:09:06 -0600194\tbool\t\tboolval;
195\tunsigned char\tbytearray[3];
196\tunsigned char\tbyteval;
197\tfdt32_t\t\tintarray[4];
198\tfdt32_t\t\tintval;
199\tunsigned char\tlongbytearray[9];
Simon Glass2a2d91d2018-07-06 10:27:28 -0600200\tunsigned char\tnotstring[5];
Simon Glassc0791922017-06-18 22:09:06 -0600201\tconst char *\tstringarray[3];
202\tconst char *\tstringval;
203};
204struct dtd_sandbox_spl_test_2 {
205};
206''', data)
207
Walter Lozano361e7332020-06-25 01:10:08 -0300208 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600209 with open(output) as infile:
210 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600211 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300212static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass1953ce72019-05-17 22:00:32 -0600213\t.boolval\t\t= true,
Simon Glassc0791922017-06-18 22:09:06 -0600214\t.bytearray\t\t= {0x6, 0x0, 0x0},
215\t.byteval\t\t= 0x5,
Simon Glass1953ce72019-05-17 22:00:32 -0600216\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600217\t.intval\t\t\t= 0x1,
Simon Glass21d54ac2017-08-29 14:15:49 -0600218\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
219\t\t0x11},
Simon Glass1953ce72019-05-17 22:00:32 -0600220\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600221\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glass1953ce72019-05-17 22:00:32 -0600222\t.stringval\t\t= "message",
Simon Glassc0791922017-06-18 22:09:06 -0600223};
224U_BOOT_DEVICE(spl_test) = {
225\t.name\t\t= "sandbox_spl_test",
226\t.platdata\t= &dtv_spl_test,
227\t.platdata_size\t= sizeof(dtv_spl_test),
228};
229
Walter Lozano51f12632020-06-25 01:10:13 -0300230static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glassf02d0eb2020-07-07 21:32:06 -0600231\t.acpi_name\t\t= "\\\\_SB.GPO0",
Simon Glassc0791922017-06-18 22:09:06 -0600232\t.bytearray\t\t= {0x1, 0x23, 0x34},
233\t.byteval\t\t= 0x8,
Simon Glass1953ce72019-05-17 22:00:32 -0600234\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600235\t.intval\t\t\t= 0x3,
Simon Glass21d54ac2017-08-29 14:15:49 -0600236\t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
237\t\t0x0},
Simon Glassc0791922017-06-18 22:09:06 -0600238\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glass1953ce72019-05-17 22:00:32 -0600239\t.stringval\t\t= "message2",
Simon Glassc0791922017-06-18 22:09:06 -0600240};
241U_BOOT_DEVICE(spl_test2) = {
242\t.name\t\t= "sandbox_spl_test",
243\t.platdata\t= &dtv_spl_test2,
244\t.platdata_size\t= sizeof(dtv_spl_test2),
245};
246
Walter Lozano51f12632020-06-25 01:10:13 -0300247static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glassc0791922017-06-18 22:09:06 -0600248\t.stringarray\t\t= {"one", "", ""},
249};
250U_BOOT_DEVICE(spl_test3) = {
251\t.name\t\t= "sandbox_spl_test",
252\t.platdata\t= &dtv_spl_test3,
253\t.platdata_size\t= sizeof(dtv_spl_test3),
254};
255
Walter Lozano51f12632020-06-25 01:10:13 -0300256static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
Simon Glassc0791922017-06-18 22:09:06 -0600257};
258U_BOOT_DEVICE(spl_test4) = {
259\t.name\t\t= "sandbox_spl_test_2",
260\t.platdata\t= &dtv_spl_test4,
261\t.platdata_size\t= sizeof(dtv_spl_test4),
262};
263
Walter Lozano51f12632020-06-25 01:10:13 -0300264static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
Simon Glass5ec741f2017-08-29 14:15:51 -0600265};
266U_BOOT_DEVICE(i2c_at_0) = {
267\t.name\t\t= "sandbox_i2c_test",
268\t.platdata\t= &dtv_i2c_at_0,
269\t.platdata_size\t= sizeof(dtv_i2c_at_0),
270};
271
Walter Lozano51f12632020-06-25 01:10:13 -0300272static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
Simon Glass5ec741f2017-08-29 14:15:51 -0600273\t.low_power\t\t= true,
274\t.reg\t\t\t= {0x9, 0x0},
275};
276U_BOOT_DEVICE(pmic_at_9) = {
277\t.name\t\t= "sandbox_pmic_test",
278\t.platdata\t= &dtv_pmic_at_9,
279\t.platdata_size\t= sizeof(dtv_pmic_at_9),
280};
281
Walter Lozano51f12632020-06-25 01:10:13 -0300282''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc0791922017-06-18 22:09:06 -0600283
Walter Lozanodac82282020-07-03 08:07:17 -0300284 def test_driver_alias(self):
285 """Test output from a device tree file with a driver alias"""
286 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
287 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300288 self.run_test(['struct'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300289 with open(output) as infile:
290 data = infile.read()
291 self._CheckStrings(HEADER + '''
292struct dtd_sandbox_gpio {
293\tconst char *\tgpio_bank_name;
294\tbool\t\tgpio_controller;
295\tfdt32_t\t\tsandbox_gpio_count;
296};
297#define dtd_sandbox_gpio_alias dtd_sandbox_gpio
298''', data)
299
Walter Lozano361e7332020-06-25 01:10:08 -0300300 self.run_test(['platdata'], dtb_file, output)
Walter Lozanodac82282020-07-03 08:07:17 -0300301 with open(output) as infile:
302 data = infile.read()
303 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300304static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanodac82282020-07-03 08:07:17 -0300305\t.gpio_bank_name\t\t= "a",
306\t.gpio_controller\t= true,
307\t.sandbox_gpio_count\t= 0x14,
308};
309U_BOOT_DEVICE(gpios_at_0) = {
310\t.name\t\t= "sandbox_gpio",
311\t.platdata\t= &dtv_gpios_at_0,
312\t.platdata_size\t= sizeof(dtv_gpios_at_0),
313};
314
Walter Lozano51f12632020-06-25 01:10:13 -0300315void dm_populate_phandle_data(void) {
316}
Walter Lozanodac82282020-07-03 08:07:17 -0300317''', data)
318
Walter Lozano361e7332020-06-25 01:10:08 -0300319 def test_invalid_driver(self):
320 """Test output from a device tree file with an invalid driver"""
321 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
322 output = tools.GetOutputFilename('output')
323 with test_util.capture_sys_output() as (stdout, stderr):
324 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
325 with open(output) as infile:
326 data = infile.read()
327 self._CheckStrings(HEADER + '''
328struct dtd_invalid {
329};
330''', data)
331
332 with test_util.capture_sys_output() as (stdout, stderr):
333 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
334 with open(output) as infile:
335 data = infile.read()
336 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300337static struct dtd_invalid dtv_spl_test = {
Walter Lozano361e7332020-06-25 01:10:08 -0300338};
339U_BOOT_DEVICE(spl_test) = {
340\t.name\t\t= "invalid",
341\t.platdata\t= &dtv_spl_test,
342\t.platdata_size\t= sizeof(dtv_spl_test),
343};
344
Walter Lozano51f12632020-06-25 01:10:13 -0300345void dm_populate_phandle_data(void) {
346}
Walter Lozano361e7332020-06-25 01:10:08 -0300347''', data)
348
Simon Glassc0791922017-06-18 22:09:06 -0600349 def test_phandle(self):
350 """Test output from a node containing a phandle reference"""
351 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
352 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300353 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600354 with open(output) as infile:
355 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600356 self._CheckStrings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600357struct dtd_source {
Simon Glass634eba42017-08-29 14:15:59 -0600358\tstruct phandle_2_arg clocks[4];
Simon Glassc0791922017-06-18 22:09:06 -0600359};
360struct dtd_target {
361\tfdt32_t\t\tintval;
362};
363''', data)
364
Walter Lozano361e7332020-06-25 01:10:08 -0300365 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600366 with open(output) as infile:
367 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600368 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300369static struct dtd_target dtv_phandle_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600370\t.intval\t\t\t= 0x0,
Simon Glassc0791922017-06-18 22:09:06 -0600371};
372U_BOOT_DEVICE(phandle_target) = {
373\t.name\t\t= "target",
374\t.platdata\t= &dtv_phandle_target,
375\t.platdata_size\t= sizeof(dtv_phandle_target),
376};
377
Walter Lozano51f12632020-06-25 01:10:13 -0300378static struct dtd_target dtv_phandle2_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600379\t.intval\t\t\t= 0x1,
380};
381U_BOOT_DEVICE(phandle2_target) = {
382\t.name\t\t= "target",
383\t.platdata\t= &dtv_phandle2_target,
384\t.platdata_size\t= sizeof(dtv_phandle2_target),
385};
386
Walter Lozano51f12632020-06-25 01:10:13 -0300387static struct dtd_target dtv_phandle3_target = {
Simon Glass634eba42017-08-29 14:15:59 -0600388\t.intval\t\t\t= 0x2,
389};
390U_BOOT_DEVICE(phandle3_target) = {
391\t.name\t\t= "target",
392\t.platdata\t= &dtv_phandle3_target,
393\t.platdata_size\t= sizeof(dtv_phandle3_target),
394};
395
Walter Lozano51f12632020-06-25 01:10:13 -0300396static struct dtd_source dtv_phandle_source = {
Simon Glass35d50372017-08-29 14:15:57 -0600397\t.clocks\t\t\t= {
Walter Lozano51f12632020-06-25 01:10:13 -0300398\t\t\t{NULL, {}},
399\t\t\t{NULL, {11}},
400\t\t\t{NULL, {12, 13}},
401\t\t\t{NULL, {}},},
Simon Glassc0791922017-06-18 22:09:06 -0600402};
403U_BOOT_DEVICE(phandle_source) = {
404\t.name\t\t= "source",
405\t.platdata\t= &dtv_phandle_source,
406\t.platdata_size\t= sizeof(dtv_phandle_source),
407};
408
Walter Lozano51f12632020-06-25 01:10:13 -0300409static struct dtd_source dtv_phandle_source2 = {
Simon Glass760b7172018-07-06 10:27:31 -0600410\t.clocks\t\t\t= {
Walter Lozano51f12632020-06-25 01:10:13 -0300411\t\t\t{NULL, {}},},
Simon Glass760b7172018-07-06 10:27:31 -0600412};
413U_BOOT_DEVICE(phandle_source2) = {
414\t.name\t\t= "source",
415\t.platdata\t= &dtv_phandle_source2,
416\t.platdata_size\t= sizeof(dtv_phandle_source2),
417};
418
Walter Lozano51f12632020-06-25 01:10:13 -0300419void dm_populate_phandle_data(void) {
420\tdtv_phandle_source.clocks[0].node = DM_GET_DEVICE(phandle_target);
421\tdtv_phandle_source.clocks[1].node = DM_GET_DEVICE(phandle2_target);
422\tdtv_phandle_source.clocks[2].node = DM_GET_DEVICE(phandle3_target);
423\tdtv_phandle_source.clocks[3].node = DM_GET_DEVICE(phandle_target);
424\tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
425}
Simon Glassc0791922017-06-18 22:09:06 -0600426''', data)
427
Simon Glass8512ea22018-07-06 10:27:35 -0600428 def test_phandle_single(self):
429 """Test output from a node containing a phandle reference"""
430 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
431 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300432 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600433 with open(output) as infile:
434 data = infile.read()
435 self._CheckStrings(HEADER + '''
436struct dtd_source {
437\tstruct phandle_0_arg clocks[1];
438};
439struct dtd_target {
440\tfdt32_t\t\tintval;
441};
442''', data)
443
444 def test_phandle_reorder(self):
445 """Test that phandle targets are generated before their references"""
446 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
447 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300448 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600449 with open(output) as infile:
450 data = infile.read()
451 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300452static struct dtd_target dtv_phandle_target = {
Simon Glass8512ea22018-07-06 10:27:35 -0600453};
454U_BOOT_DEVICE(phandle_target) = {
455\t.name\t\t= "target",
456\t.platdata\t= &dtv_phandle_target,
457\t.platdata_size\t= sizeof(dtv_phandle_target),
458};
459
Walter Lozano51f12632020-06-25 01:10:13 -0300460static struct dtd_source dtv_phandle_source2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600461\t.clocks\t\t\t= {
Walter Lozano51f12632020-06-25 01:10:13 -0300462\t\t\t{NULL, {}},},
Simon Glass8512ea22018-07-06 10:27:35 -0600463};
464U_BOOT_DEVICE(phandle_source2) = {
465\t.name\t\t= "source",
466\t.platdata\t= &dtv_phandle_source2,
467\t.platdata_size\t= sizeof(dtv_phandle_source2),
468};
469
Walter Lozano51f12632020-06-25 01:10:13 -0300470void dm_populate_phandle_data(void) {
471\tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
472}
Simon Glass8512ea22018-07-06 10:27:35 -0600473''', data)
474
Walter Lozano6c3fc502020-06-25 01:10:17 -0300475 def test_phandle_cd_gpio(self):
476 """Test that phandle targets are generated when unsing cd-gpios"""
477 dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
478 output = tools.GetOutputFilename('output')
479 dtb_platdata.run_steps(['platdata'], dtb_file, False, output, True)
480 with open(output) as infile:
481 data = infile.read()
482 self._CheckStrings(C_HEADER + '''
483static struct dtd_target dtv_phandle_target = {
484\t.intval\t\t\t= 0x0,
485};
486U_BOOT_DEVICE(phandle_target) = {
487\t.name\t\t= "target",
488\t.platdata\t= &dtv_phandle_target,
489\t.platdata_size\t= sizeof(dtv_phandle_target),
490};
491
492static struct dtd_target dtv_phandle2_target = {
493\t.intval\t\t\t= 0x1,
494};
495U_BOOT_DEVICE(phandle2_target) = {
496\t.name\t\t= "target",
497\t.platdata\t= &dtv_phandle2_target,
498\t.platdata_size\t= sizeof(dtv_phandle2_target),
499};
500
501static struct dtd_target dtv_phandle3_target = {
502\t.intval\t\t\t= 0x2,
503};
504U_BOOT_DEVICE(phandle3_target) = {
505\t.name\t\t= "target",
506\t.platdata\t= &dtv_phandle3_target,
507\t.platdata_size\t= sizeof(dtv_phandle3_target),
508};
509
510static struct dtd_source dtv_phandle_source = {
511\t.cd_gpios\t\t= {
512\t\t\t{NULL, {}},
513\t\t\t{NULL, {11}},
514\t\t\t{NULL, {12, 13}},
515\t\t\t{NULL, {}},},
516};
517U_BOOT_DEVICE(phandle_source) = {
518\t.name\t\t= "source",
519\t.platdata\t= &dtv_phandle_source,
520\t.platdata_size\t= sizeof(dtv_phandle_source),
521};
522
523static struct dtd_source dtv_phandle_source2 = {
524\t.cd_gpios\t\t= {
525\t\t\t{NULL, {}},},
526};
527U_BOOT_DEVICE(phandle_source2) = {
528\t.name\t\t= "source",
529\t.platdata\t= &dtv_phandle_source2,
530\t.platdata_size\t= sizeof(dtv_phandle_source2),
531};
532
533void dm_populate_phandle_data(void) {
534\tdtv_phandle_source.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
535\tdtv_phandle_source.cd_gpios[1].node = DM_GET_DEVICE(phandle2_target);
536\tdtv_phandle_source.cd_gpios[2].node = DM_GET_DEVICE(phandle3_target);
537\tdtv_phandle_source.cd_gpios[3].node = DM_GET_DEVICE(phandle_target);
538\tdtv_phandle_source2.cd_gpios[0].node = DM_GET_DEVICE(phandle_target);
539}
540''', data)
541
Simon Glass8512ea22018-07-06 10:27:35 -0600542 def test_phandle_bad(self):
543 """Test a node containing an invalid phandle fails"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600544 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
545 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600546 output = tools.GetOutputFilename('output')
547 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300548 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600549 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
550 str(e.exception))
551
552 def test_phandle_bad2(self):
553 """Test a phandle target missing its #*-cells property"""
Simon Glass4b4bc062018-10-01 21:12:43 -0600554 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
555 capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600556 output = tools.GetOutputFilename('output')
557 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300558 self.run_test(['struct'], dtb_file, output)
Walter Lozanoad340172020-06-25 01:10:16 -0300559 self.assertIn("Node 'phandle-target' has no cells property",
Simon Glass8512ea22018-07-06 10:27:35 -0600560 str(e.exception))
561
Simon Glassc0791922017-06-18 22:09:06 -0600562 def test_aliases(self):
563 """Test output from a node with multiple compatible strings"""
564 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
565 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300566 self.run_test(['struct'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600567 with open(output) as infile:
568 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600569 self._CheckStrings(HEADER + '''
Simon Glassc0791922017-06-18 22:09:06 -0600570struct dtd_compat1 {
571\tfdt32_t\t\tintval;
572};
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300573struct dtd_simple_bus {
574\tfdt32_t\t\tintval;
575};
Simon Glassc0791922017-06-18 22:09:06 -0600576#define dtd_compat2_1_fred dtd_compat1
577#define dtd_compat3 dtd_compat1
578''', data)
579
Walter Lozano361e7332020-06-25 01:10:08 -0300580 self.run_test(['platdata'], dtb_file, output)
Simon Glassc0791922017-06-18 22:09:06 -0600581 with open(output) as infile:
582 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600583 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300584static struct dtd_compat1 dtv_spl_test = {
Simon Glassc0791922017-06-18 22:09:06 -0600585\t.intval\t\t\t= 0x1,
586};
587U_BOOT_DEVICE(spl_test) = {
588\t.name\t\t= "compat1",
589\t.platdata\t= &dtv_spl_test,
590\t.platdata_size\t= sizeof(dtv_spl_test),
591};
592
Walter Lozanodcb3ed62020-07-23 00:22:03 -0300593static struct dtd_simple_bus dtv_spl_test2 = {
594\t.intval\t\t\t= 0x1,
595};
596U_BOOT_DEVICE(spl_test2) = {
597\t.name\t\t= "simple_bus",
598\t.platdata\t= &dtv_spl_test2,
599\t.platdata_size\t= sizeof(dtv_spl_test2),
600};
601
Walter Lozano51f12632020-06-25 01:10:13 -0300602''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600603
604 def test_addresses64(self):
605 """Test output from a node with a 'reg' property with na=2, ns=2"""
606 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
607 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300608 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600609 with open(output) as infile:
610 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600611 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600612struct dtd_test1 {
613\tfdt64_t\t\treg[2];
614};
615struct dtd_test2 {
616\tfdt64_t\t\treg[2];
617};
618struct dtd_test3 {
619\tfdt64_t\t\treg[4];
620};
621''', data)
622
Walter Lozano361e7332020-06-25 01:10:08 -0300623 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600624 with open(output) as infile:
625 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600626 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300627static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600628\t.reg\t\t\t= {0x1234, 0x5678},
629};
630U_BOOT_DEVICE(test1) = {
631\t.name\t\t= "test1",
632\t.platdata\t= &dtv_test1,
633\t.platdata_size\t= sizeof(dtv_test1),
634};
635
Walter Lozano51f12632020-06-25 01:10:13 -0300636static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600637\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
638};
639U_BOOT_DEVICE(test2) = {
640\t.name\t\t= "test2",
641\t.platdata\t= &dtv_test2,
642\t.platdata_size\t= sizeof(dtv_test2),
643};
644
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",
650\t.platdata\t= &dtv_test3,
651\t.platdata_size\t= sizeof(dtv_test3),
652};
653
Walter Lozano51f12632020-06-25 01:10:13 -0300654''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600655
656 def test_addresses32(self):
657 """Test output from a node with a 'reg' property with na=1, ns=1"""
658 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
659 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300660 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600661 with open(output) as infile:
662 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600663 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600664struct dtd_test1 {
665\tfdt32_t\t\treg[2];
666};
667struct dtd_test2 {
668\tfdt32_t\t\treg[4];
669};
670''', data)
671
Walter Lozano361e7332020-06-25 01:10:08 -0300672 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600673 with open(output) as infile:
674 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600675 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300676static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600677\t.reg\t\t\t= {0x1234, 0x5678},
678};
679U_BOOT_DEVICE(test1) = {
680\t.name\t\t= "test1",
681\t.platdata\t= &dtv_test1,
682\t.platdata_size\t= sizeof(dtv_test1),
683};
684
Walter Lozano51f12632020-06-25 01:10:13 -0300685static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600686\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
687};
688U_BOOT_DEVICE(test2) = {
689\t.name\t\t= "test2",
690\t.platdata\t= &dtv_test2,
691\t.platdata_size\t= sizeof(dtv_test2),
692};
693
Walter Lozano51f12632020-06-25 01:10:13 -0300694''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600695
696 def test_addresses64_32(self):
697 """Test output from a node with a 'reg' property with na=2, ns=1"""
698 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
699 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300700 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600701 with open(output) as infile:
702 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600703 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600704struct dtd_test1 {
705\tfdt64_t\t\treg[2];
706};
707struct dtd_test2 {
708\tfdt64_t\t\treg[2];
709};
710struct dtd_test3 {
711\tfdt64_t\t\treg[4];
712};
713''', data)
714
Walter Lozano361e7332020-06-25 01:10:08 -0300715 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600716 with open(output) as infile:
717 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600718 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300719static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600720\t.reg\t\t\t= {0x123400000000, 0x5678},
721};
722U_BOOT_DEVICE(test1) = {
723\t.name\t\t= "test1",
724\t.platdata\t= &dtv_test1,
725\t.platdata_size\t= sizeof(dtv_test1),
726};
727
Walter Lozano51f12632020-06-25 01:10:13 -0300728static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600729\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
730};
731U_BOOT_DEVICE(test2) = {
732\t.name\t\t= "test2",
733\t.platdata\t= &dtv_test2,
734\t.platdata_size\t= sizeof(dtv_test2),
735};
736
Walter Lozano51f12632020-06-25 01:10:13 -0300737static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600738\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
739};
740U_BOOT_DEVICE(test3) = {
741\t.name\t\t= "test3",
742\t.platdata\t= &dtv_test3,
743\t.platdata_size\t= sizeof(dtv_test3),
744};
745
Walter Lozano51f12632020-06-25 01:10:13 -0300746''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600747
748 def test_addresses32_64(self):
749 """Test output from a node with a 'reg' property with na=1, ns=2"""
750 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
751 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300752 self.run_test(['struct'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600753 with open(output) as infile:
754 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600755 self._CheckStrings(HEADER + '''
Simon Glassc20ee0e2017-08-29 14:15:50 -0600756struct dtd_test1 {
757\tfdt64_t\t\treg[2];
758};
759struct dtd_test2 {
760\tfdt64_t\t\treg[2];
761};
762struct dtd_test3 {
763\tfdt64_t\t\treg[4];
764};
765''', data)
766
Walter Lozano361e7332020-06-25 01:10:08 -0300767 self.run_test(['platdata'], dtb_file, output)
Simon Glassc20ee0e2017-08-29 14:15:50 -0600768 with open(output) as infile:
769 data = infile.read()
Simon Glass57f0bc42018-07-06 10:27:25 -0600770 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300771static struct dtd_test1 dtv_test1 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600772\t.reg\t\t\t= {0x1234, 0x567800000000},
773};
774U_BOOT_DEVICE(test1) = {
775\t.name\t\t= "test1",
776\t.platdata\t= &dtv_test1,
777\t.platdata_size\t= sizeof(dtv_test1),
778};
779
Walter Lozano51f12632020-06-25 01:10:13 -0300780static struct dtd_test2 dtv_test2 = {
Simon Glassc20ee0e2017-08-29 14:15:50 -0600781\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
782};
783U_BOOT_DEVICE(test2) = {
784\t.name\t\t= "test2",
785\t.platdata\t= &dtv_test2,
786\t.platdata_size\t= sizeof(dtv_test2),
787};
788
Walter Lozano51f12632020-06-25 01:10:13 -0300789static struct dtd_test3 dtv_test3 = {
Simon Glassc20ee0e2017-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",
794\t.platdata\t= &dtv_test3,
795\t.platdata_size\t= sizeof(dtv_test3),
796};
797
Walter Lozano51f12632020-06-25 01:10:13 -0300798''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass8512ea22018-07-06 10:27:35 -0600799
800 def test_bad_reg(self):
801 """Test that a reg property with an invalid type generates an error"""
Simon Glassfe57c782018-07-06 10:27:37 -0600802 # Capture stderr since dtc will emit warnings for this file
803 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600804 output = tools.GetOutputFilename('output')
805 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300806 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600807 self.assertIn("Node 'spl-test' reg property is not an int",
808 str(e.exception))
809
810 def test_bad_reg2(self):
811 """Test that a reg property with an invalid cell count is detected"""
Simon Glassfe57c782018-07-06 10:27:37 -0600812 # Capture stderr since dtc will emit warnings for this file
813 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass8512ea22018-07-06 10:27:35 -0600814 output = tools.GetOutputFilename('output')
815 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300816 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600817 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
818 str(e.exception))
819
820 def test_add_prop(self):
821 """Test that a subequent node can add a new property to a struct"""
822 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
823 output = tools.GetOutputFilename('output')
Walter Lozano361e7332020-06-25 01:10:08 -0300824 self.run_test(['struct'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600825 with open(output) as infile:
826 data = infile.read()
827 self._CheckStrings(HEADER + '''
828struct dtd_sandbox_spl_test {
829\tfdt32_t\t\tintarray;
830\tfdt32_t\t\tintval;
831};
832''', data)
833
Walter Lozano361e7332020-06-25 01:10:08 -0300834 self.run_test(['platdata'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600835 with open(output) as infile:
836 data = infile.read()
837 self._CheckStrings(C_HEADER + '''
Walter Lozano51f12632020-06-25 01:10:13 -0300838static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass8512ea22018-07-06 10:27:35 -0600839\t.intval\t\t\t= 0x1,
840};
841U_BOOT_DEVICE(spl_test) = {
842\t.name\t\t= "sandbox_spl_test",
843\t.platdata\t= &dtv_spl_test,
844\t.platdata_size\t= sizeof(dtv_spl_test),
845};
846
Walter Lozano51f12632020-06-25 01:10:13 -0300847static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass8512ea22018-07-06 10:27:35 -0600848\t.intarray\t\t= 0x5,
849};
850U_BOOT_DEVICE(spl_test2) = {
851\t.name\t\t= "sandbox_spl_test",
852\t.platdata\t= &dtv_spl_test2,
853\t.platdata_size\t= sizeof(dtv_spl_test2),
854};
855
Walter Lozano51f12632020-06-25 01:10:13 -0300856''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass8512ea22018-07-06 10:27:35 -0600857
858 def testStdout(self):
859 """Test output to stdout"""
860 dtb_file = get_dtb_file('dtoc_test_simple.dts')
861 with test_util.capture_sys_output() as (stdout, stderr):
Walter Lozano361e7332020-06-25 01:10:08 -0300862 self.run_test(['struct'], dtb_file, '-')
Simon Glass8512ea22018-07-06 10:27:35 -0600863
864 def testNoCommand(self):
865 """Test running dtoc without a command"""
866 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300867 self.run_test([], '', '')
Simon Glass8512ea22018-07-06 10:27:35 -0600868 self.assertIn("Please specify a command: struct, platdata",
869 str(e.exception))
870
871 def testBadCommand(self):
872 """Test running dtoc with an invalid command"""
873 dtb_file = get_dtb_file('dtoc_test_simple.dts')
874 output = tools.GetOutputFilename('output')
875 with self.assertRaises(ValueError) as e:
Walter Lozano361e7332020-06-25 01:10:08 -0300876 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass8512ea22018-07-06 10:27:35 -0600877 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
878 str(e.exception))
Walter Lozano6c74d1b2020-07-28 19:06:23 -0300879
880 def testScanDrivers(self):
881 """Test running dtoc with additional drivers to scan"""
882 dtb_file = get_dtb_file('dtoc_test_simple.dts')
883 output = tools.GetOutputFilename('output')
884 with test_util.capture_sys_output() as (stdout, stderr):
885 dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
886 [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
887
888 def testUnicodeError(self):
889 """Test running dtoc with an invalid unicode file
890
891 To be able to perform this test without adding a weird text file which
892 would produce issues when using checkpatch.pl or patman, generate the
893 file at runtime and then process it.
894 """
895 dtb_file = get_dtb_file('dtoc_test_simple.dts')
896 output = tools.GetOutputFilename('output')
897 driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
898 with open(driver_fn, 'wb+') as df:
899 df.write(b'\x81')
900
901 with test_util.capture_sys_output() as (stdout, stderr):
902 dtb_platdata.run_steps(['struct'], dtb_file, False, output, True,
903 [driver_fn])