blob: 209542c84922a36b52dc594b806605b85944554f [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
Simon Glass9d2eb922017-06-18 22:09:06 -060016import unittest
17
Simon Glassa997ea52020-04-17 18:09:04 -060018from dtoc import dtb_platdata
Simon Glass9d2eb922017-06-18 22:09:06 -060019from dtb_platdata import conv_name_to_c
20from dtb_platdata import get_compat_name
21from dtb_platdata import get_value
22from dtb_platdata import tab_to
Simon Glassa997ea52020-04-17 18:09:04 -060023from dtoc import fdt
24from dtoc import fdt_util
25from patman import test_util
26from patman import tools
Simon Glass9d2eb922017-06-18 22:09:06 -060027
28our_path = os.path.dirname(os.path.realpath(__file__))
29
30
Simon Glasseb37e2d2017-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 Yamada75f82d02018-03-05 01:20:11 +090038#include <linux/libfdt.h>'''
Simon Glasseb37e2d2017-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
46#include <common.h>
47#include <dm.h>
48#include <dt-structs.h>
49'''
50
Walter Lozanodc5b4372020-06-25 01:10:13 -030051C_EMPTY_POPULATE_PHANDLE_DATA = '''void dm_populate_phandle_data(void) {
52}
53'''
Simon Glasseb37e2d2017-11-12 21:52:17 -070054
Simon Glass3bce93d2018-07-06 10:27:37 -060055
56def get_dtb_file(dts_fname, capture_stderr=False):
Simon Glass9d2eb922017-06-18 22:09:06 -060057 """Compile a .dts file to a .dtb
58
59 Args:
60 dts_fname: Filename of .dts file in the current directory
Simon Glass3bce93d2018-07-06 10:27:37 -060061 capture_stderr: True to capture and discard stderr output
Simon Glass9d2eb922017-06-18 22:09:06 -060062
63 Returns:
64 Filename of compiled file in output directory
65 """
Simon Glass3bce93d2018-07-06 10:27:37 -060066 return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
67 capture_stderr=capture_stderr)
Simon Glass9d2eb922017-06-18 22:09:06 -060068
69
70class TestDtoc(unittest.TestCase):
71 """Tests for dtoc"""
72 @classmethod
73 def setUpClass(cls):
74 tools.PrepareOutputDir(None)
75
76 @classmethod
77 def tearDownClass(cls):
78 tools._RemoveOutputDir()
79
Simon Glassc47c2b32018-07-06 10:27:25 -060080 def _WritePythonString(self, fname, data):
81 """Write a string with tabs expanded as done in this Python file
82
83 Args:
84 fname: Filename to write to
85 data: Raw string to convert
86 """
87 data = data.replace('\t', '\\t')
88 with open(fname, 'w') as fd:
89 fd.write(data)
90
91 def _CheckStrings(self, expected, actual):
92 """Check that a string matches its expected value
93
94 If the strings do not match, they are written to the /tmp directory in
95 the same Python format as is used here in the test. This allows for
96 easy comparison and update of the tests.
97
98 Args:
99 expected: Expected string
100 actual: Actual string
101 """
102 if expected != actual:
103 self._WritePythonString('/tmp/binman.expected', expected)
104 self._WritePythonString('/tmp/binman.actual', actual)
Simon Glass61b88e52019-05-17 22:00:31 -0600105 print('Failures written to /tmp/binman.{expected,actual}')
Simon Glassc47c2b32018-07-06 10:27:25 -0600106 self.assertEquals(expected, actual)
107
Walter Lozanoa324e412020-06-25 01:10:08 -0300108
109 def run_test(self, args, dtb_file, output):
110 dtb_platdata.run_steps(args, dtb_file, False, output, True)
111
Simon Glass9d2eb922017-06-18 22:09:06 -0600112 def test_name(self):
113 """Test conversion of device tree names to C identifiers"""
114 self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
115 self.assertEqual('vendor_clock_frequency',
116 conv_name_to_c('vendor,clock-frequency'))
117 self.assertEqual('rockchip_rk3399_sdhci_5_1',
118 conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
119
120 def test_tab_to(self):
121 """Test operation of tab_to() function"""
122 self.assertEqual('fred ', tab_to(0, 'fred'))
123 self.assertEqual('fred\t', tab_to(1, 'fred'))
124 self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
125 self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
126 self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
127 self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
128
129 def test_get_value(self):
130 """Test operation of get_value() function"""
131 self.assertEqual('0x45',
132 get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
133 self.assertEqual('0x45',
134 get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
135 self.assertEqual('0x0',
136 get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
137 self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
138 self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
139
140 def test_get_compat_name(self):
141 """Test operation of get_compat_name() function"""
142 Prop = collections.namedtuple('Prop', ['value'])
143 Node = collections.namedtuple('Node', ['props'])
144
145 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
146 node = Node({'compatible': prop})
147 self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
148 get_compat_name(node))
149
150 prop = Prop(['rockchip,rk3399-sdhci-5.1'])
151 node = Node({'compatible': prop})
152 self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
153 get_compat_name(node))
154
155 prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
156 node = Node({'compatible': prop})
157 self.assertEqual(('rockchip_rk3399_sdhci_5_1',
158 ['arasan_sdhci_5_1', 'third']),
159 get_compat_name(node))
160
161 def test_empty_file(self):
162 """Test output from a device tree file with no nodes"""
163 dtb_file = get_dtb_file('dtoc_test_empty.dts')
164 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300165 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600166 with open(output) as infile:
167 lines = infile.read().splitlines()
Simon Glasseb37e2d2017-11-12 21:52:17 -0700168 self.assertEqual(HEADER.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600169
Walter Lozanoa324e412020-06-25 01:10:08 -0300170 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600171 with open(output) as infile:
172 lines = infile.read().splitlines()
Walter Lozanodc5b4372020-06-25 01:10:13 -0300173 self.assertEqual(C_HEADER.splitlines() + [''] +
174 C_EMPTY_POPULATE_PHANDLE_DATA.splitlines(), lines)
Simon Glass9d2eb922017-06-18 22:09:06 -0600175
176 def test_simple(self):
177 """Test output from some simple nodes with various types of data"""
178 dtb_file = get_dtb_file('dtoc_test_simple.dts')
179 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300180 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600181 with open(output) as infile:
182 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600183 self._CheckStrings(HEADER + '''
Simon Glass90e5f0a2017-08-29 14:15:51 -0600184struct dtd_sandbox_i2c_test {
185};
186struct dtd_sandbox_pmic_test {
187\tbool\t\tlow_power;
188\tfdt64_t\t\treg[2];
189};
Simon Glass9d2eb922017-06-18 22:09:06 -0600190struct dtd_sandbox_spl_test {
191\tbool\t\tboolval;
192\tunsigned char\tbytearray[3];
193\tunsigned char\tbyteval;
194\tfdt32_t\t\tintarray[4];
195\tfdt32_t\t\tintval;
196\tunsigned char\tlongbytearray[9];
Simon Glass9c526332018-07-06 10:27:28 -0600197\tunsigned char\tnotstring[5];
Simon Glass9d2eb922017-06-18 22:09:06 -0600198\tconst char *\tstringarray[3];
199\tconst char *\tstringval;
200};
201struct dtd_sandbox_spl_test_2 {
202};
203''', data)
204
Walter Lozanoa324e412020-06-25 01:10:08 -0300205 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600206 with open(output) as infile:
207 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600208 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300209static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glassc82de562019-05-17 22:00:32 -0600210\t.boolval\t\t= true,
Simon Glass9d2eb922017-06-18 22:09:06 -0600211\t.bytearray\t\t= {0x6, 0x0, 0x0},
212\t.byteval\t\t= 0x5,
Simon Glassc82de562019-05-17 22:00:32 -0600213\t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600214\t.intval\t\t\t= 0x1,
Simon Glass131e0b02017-08-29 14:15:49 -0600215\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
216\t\t0x11},
Simon Glassc82de562019-05-17 22:00:32 -0600217\t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600218\t.stringarray\t\t= {"multi-word", "message", ""},
Simon Glassc82de562019-05-17 22:00:32 -0600219\t.stringval\t\t= "message",
Simon Glass9d2eb922017-06-18 22:09:06 -0600220};
221U_BOOT_DEVICE(spl_test) = {
222\t.name\t\t= "sandbox_spl_test",
223\t.platdata\t= &dtv_spl_test,
224\t.platdata_size\t= sizeof(dtv_spl_test),
225};
226
Walter Lozanodc5b4372020-06-25 01:10:13 -0300227static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600228\t.bytearray\t\t= {0x1, 0x23, 0x34},
229\t.byteval\t\t= 0x8,
Simon Glassc82de562019-05-17 22:00:32 -0600230\t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600231\t.intval\t\t\t= 0x3,
Simon Glass131e0b02017-08-29 14:15:49 -0600232\t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
233\t\t0x0},
Simon Glass9d2eb922017-06-18 22:09:06 -0600234\t.stringarray\t\t= {"another", "multi-word", "message"},
Simon Glassc82de562019-05-17 22:00:32 -0600235\t.stringval\t\t= "message2",
Simon Glass9d2eb922017-06-18 22:09:06 -0600236};
237U_BOOT_DEVICE(spl_test2) = {
238\t.name\t\t= "sandbox_spl_test",
239\t.platdata\t= &dtv_spl_test2,
240\t.platdata_size\t= sizeof(dtv_spl_test2),
241};
242
Walter Lozanodc5b4372020-06-25 01:10:13 -0300243static struct dtd_sandbox_spl_test dtv_spl_test3 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600244\t.stringarray\t\t= {"one", "", ""},
245};
246U_BOOT_DEVICE(spl_test3) = {
247\t.name\t\t= "sandbox_spl_test",
248\t.platdata\t= &dtv_spl_test3,
249\t.platdata_size\t= sizeof(dtv_spl_test3),
250};
251
Walter Lozanodc5b4372020-06-25 01:10:13 -0300252static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600253};
254U_BOOT_DEVICE(spl_test4) = {
255\t.name\t\t= "sandbox_spl_test_2",
256\t.platdata\t= &dtv_spl_test4,
257\t.platdata_size\t= sizeof(dtv_spl_test4),
258};
259
Walter Lozanodc5b4372020-06-25 01:10:13 -0300260static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600261};
262U_BOOT_DEVICE(i2c_at_0) = {
263\t.name\t\t= "sandbox_i2c_test",
264\t.platdata\t= &dtv_i2c_at_0,
265\t.platdata_size\t= sizeof(dtv_i2c_at_0),
266};
267
Walter Lozanodc5b4372020-06-25 01:10:13 -0300268static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
Simon Glass90e5f0a2017-08-29 14:15:51 -0600269\t.low_power\t\t= true,
270\t.reg\t\t\t= {0x9, 0x0},
271};
272U_BOOT_DEVICE(pmic_at_9) = {
273\t.name\t\t= "sandbox_pmic_test",
274\t.platdata\t= &dtv_pmic_at_9,
275\t.platdata_size\t= sizeof(dtv_pmic_at_9),
276};
277
Walter Lozanodc5b4372020-06-25 01:10:13 -0300278''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass9d2eb922017-06-18 22:09:06 -0600279
Walter Lozanoe675d962020-07-03 08:07:17 -0300280 def test_driver_alias(self):
281 """Test output from a device tree file with a driver alias"""
282 dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
283 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300284 self.run_test(['struct'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300285 with open(output) as infile:
286 data = infile.read()
287 self._CheckStrings(HEADER + '''
288struct dtd_sandbox_gpio {
289\tconst char *\tgpio_bank_name;
290\tbool\t\tgpio_controller;
291\tfdt32_t\t\tsandbox_gpio_count;
292};
293#define dtd_sandbox_gpio_alias dtd_sandbox_gpio
294''', data)
295
Walter Lozanoa324e412020-06-25 01:10:08 -0300296 self.run_test(['platdata'], dtb_file, output)
Walter Lozanoe675d962020-07-03 08:07:17 -0300297 with open(output) as infile:
298 data = infile.read()
299 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300300static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
Walter Lozanoe675d962020-07-03 08:07:17 -0300301\t.gpio_bank_name\t\t= "a",
302\t.gpio_controller\t= true,
303\t.sandbox_gpio_count\t= 0x14,
304};
305U_BOOT_DEVICE(gpios_at_0) = {
306\t.name\t\t= "sandbox_gpio",
307\t.platdata\t= &dtv_gpios_at_0,
308\t.platdata_size\t= sizeof(dtv_gpios_at_0),
309};
310
Walter Lozanodc5b4372020-06-25 01:10:13 -0300311void dm_populate_phandle_data(void) {
312}
Walter Lozanoe675d962020-07-03 08:07:17 -0300313''', data)
314
Walter Lozanoa324e412020-06-25 01:10:08 -0300315 def test_invalid_driver(self):
316 """Test output from a device tree file with an invalid driver"""
317 dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
318 output = tools.GetOutputFilename('output')
319 with test_util.capture_sys_output() as (stdout, stderr):
320 dtb_platdata.run_steps(['struct'], dtb_file, False, output)
321 with open(output) as infile:
322 data = infile.read()
323 self._CheckStrings(HEADER + '''
324struct dtd_invalid {
325};
326''', data)
327
328 with test_util.capture_sys_output() as (stdout, stderr):
329 dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
330 with open(output) as infile:
331 data = infile.read()
332 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300333static struct dtd_invalid dtv_spl_test = {
Walter Lozanoa324e412020-06-25 01:10:08 -0300334};
335U_BOOT_DEVICE(spl_test) = {
336\t.name\t\t= "invalid",
337\t.platdata\t= &dtv_spl_test,
338\t.platdata_size\t= sizeof(dtv_spl_test),
339};
340
Walter Lozanodc5b4372020-06-25 01:10:13 -0300341void dm_populate_phandle_data(void) {
342}
Walter Lozanoa324e412020-06-25 01:10:08 -0300343''', data)
344
Simon Glass9d2eb922017-06-18 22:09:06 -0600345 def test_phandle(self):
346 """Test output from a node containing a phandle reference"""
347 dtb_file = get_dtb_file('dtoc_test_phandle.dts')
348 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300349 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600350 with open(output) as infile:
351 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600352 self._CheckStrings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600353struct dtd_source {
Simon Glass3deeb472017-08-29 14:15:59 -0600354\tstruct phandle_2_arg clocks[4];
Simon Glass9d2eb922017-06-18 22:09:06 -0600355};
356struct dtd_target {
357\tfdt32_t\t\tintval;
358};
359''', data)
360
Walter Lozanoa324e412020-06-25 01:10:08 -0300361 self.run_test(['platdata'], 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(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300365static struct dtd_target dtv_phandle_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600366\t.intval\t\t\t= 0x0,
Simon Glass9d2eb922017-06-18 22:09:06 -0600367};
368U_BOOT_DEVICE(phandle_target) = {
369\t.name\t\t= "target",
370\t.platdata\t= &dtv_phandle_target,
371\t.platdata_size\t= sizeof(dtv_phandle_target),
372};
373
Walter Lozanodc5b4372020-06-25 01:10:13 -0300374static struct dtd_target dtv_phandle2_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600375\t.intval\t\t\t= 0x1,
376};
377U_BOOT_DEVICE(phandle2_target) = {
378\t.name\t\t= "target",
379\t.platdata\t= &dtv_phandle2_target,
380\t.platdata_size\t= sizeof(dtv_phandle2_target),
381};
382
Walter Lozanodc5b4372020-06-25 01:10:13 -0300383static struct dtd_target dtv_phandle3_target = {
Simon Glass3deeb472017-08-29 14:15:59 -0600384\t.intval\t\t\t= 0x2,
385};
386U_BOOT_DEVICE(phandle3_target) = {
387\t.name\t\t= "target",
388\t.platdata\t= &dtv_phandle3_target,
389\t.platdata_size\t= sizeof(dtv_phandle3_target),
390};
391
Walter Lozanodc5b4372020-06-25 01:10:13 -0300392static struct dtd_source dtv_phandle_source = {
Simon Glassd0cd0752017-08-29 14:15:57 -0600393\t.clocks\t\t\t= {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300394\t\t\t{NULL, {}},
395\t\t\t{NULL, {11}},
396\t\t\t{NULL, {12, 13}},
397\t\t\t{NULL, {}},},
Simon Glass9d2eb922017-06-18 22:09:06 -0600398};
399U_BOOT_DEVICE(phandle_source) = {
400\t.name\t\t= "source",
401\t.platdata\t= &dtv_phandle_source,
402\t.platdata_size\t= sizeof(dtv_phandle_source),
403};
404
Walter Lozanodc5b4372020-06-25 01:10:13 -0300405static struct dtd_source dtv_phandle_source2 = {
Simon Glass609e2b12018-07-06 10:27:31 -0600406\t.clocks\t\t\t= {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300407\t\t\t{NULL, {}},},
Simon Glass609e2b12018-07-06 10:27:31 -0600408};
409U_BOOT_DEVICE(phandle_source2) = {
410\t.name\t\t= "source",
411\t.platdata\t= &dtv_phandle_source2,
412\t.platdata_size\t= sizeof(dtv_phandle_source2),
413};
414
Walter Lozanodc5b4372020-06-25 01:10:13 -0300415void dm_populate_phandle_data(void) {
416\tdtv_phandle_source.clocks[0].node = DM_GET_DEVICE(phandle_target);
417\tdtv_phandle_source.clocks[1].node = DM_GET_DEVICE(phandle2_target);
418\tdtv_phandle_source.clocks[2].node = DM_GET_DEVICE(phandle3_target);
419\tdtv_phandle_source.clocks[3].node = DM_GET_DEVICE(phandle_target);
420\tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
421}
Simon Glass9d2eb922017-06-18 22:09:06 -0600422''', data)
423
Simon Glass961c1ce2018-07-06 10:27:35 -0600424 def test_phandle_single(self):
425 """Test output from a node containing a phandle reference"""
426 dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
427 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300428 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600429 with open(output) as infile:
430 data = infile.read()
431 self._CheckStrings(HEADER + '''
432struct dtd_source {
433\tstruct phandle_0_arg clocks[1];
434};
435struct dtd_target {
436\tfdt32_t\t\tintval;
437};
438''', data)
439
440 def test_phandle_reorder(self):
441 """Test that phandle targets are generated before their references"""
442 dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
443 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300444 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600445 with open(output) as infile:
446 data = infile.read()
447 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300448static struct dtd_target dtv_phandle_target = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600449};
450U_BOOT_DEVICE(phandle_target) = {
451\t.name\t\t= "target",
452\t.platdata\t= &dtv_phandle_target,
453\t.platdata_size\t= sizeof(dtv_phandle_target),
454};
455
Walter Lozanodc5b4372020-06-25 01:10:13 -0300456static struct dtd_source dtv_phandle_source2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600457\t.clocks\t\t\t= {
Walter Lozanodc5b4372020-06-25 01:10:13 -0300458\t\t\t{NULL, {}},},
Simon Glass961c1ce2018-07-06 10:27:35 -0600459};
460U_BOOT_DEVICE(phandle_source2) = {
461\t.name\t\t= "source",
462\t.platdata\t= &dtv_phandle_source2,
463\t.platdata_size\t= sizeof(dtv_phandle_source2),
464};
465
Walter Lozanodc5b4372020-06-25 01:10:13 -0300466void dm_populate_phandle_data(void) {
467\tdtv_phandle_source2.clocks[0].node = DM_GET_DEVICE(phandle_target);
468}
Simon Glass961c1ce2018-07-06 10:27:35 -0600469''', data)
470
471 def test_phandle_bad(self):
472 """Test a node containing an invalid phandle fails"""
Simon Glass04150022018-10-01 21:12:43 -0600473 dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
474 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600475 output = tools.GetOutputFilename('output')
476 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300477 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600478 self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
479 str(e.exception))
480
481 def test_phandle_bad2(self):
482 """Test a phandle target missing its #*-cells property"""
Simon Glass04150022018-10-01 21:12:43 -0600483 dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
484 capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600485 output = tools.GetOutputFilename('output')
486 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300487 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600488 self.assertIn("Node 'phandle-target' has no '#clock-cells' property",
489 str(e.exception))
490
Simon Glass9d2eb922017-06-18 22:09:06 -0600491 def test_aliases(self):
492 """Test output from a node with multiple compatible strings"""
493 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
494 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300495 self.run_test(['struct'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600496 with open(output) as infile:
497 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600498 self._CheckStrings(HEADER + '''
Simon Glass9d2eb922017-06-18 22:09:06 -0600499struct dtd_compat1 {
500\tfdt32_t\t\tintval;
501};
502#define dtd_compat2_1_fred dtd_compat1
503#define dtd_compat3 dtd_compat1
504''', data)
505
Walter Lozanoa324e412020-06-25 01:10:08 -0300506 self.run_test(['platdata'], dtb_file, output)
Simon Glass9d2eb922017-06-18 22:09:06 -0600507 with open(output) as infile:
508 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600509 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300510static struct dtd_compat1 dtv_spl_test = {
Simon Glass9d2eb922017-06-18 22:09:06 -0600511\t.intval\t\t\t= 0x1,
512};
513U_BOOT_DEVICE(spl_test) = {
514\t.name\t\t= "compat1",
515\t.platdata\t= &dtv_spl_test,
516\t.platdata_size\t= sizeof(dtv_spl_test),
517};
518
Walter Lozanodc5b4372020-06-25 01:10:13 -0300519''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600520
521 def test_addresses64(self):
522 """Test output from a node with a 'reg' property with na=2, ns=2"""
523 dtb_file = get_dtb_file('dtoc_test_addr64.dts')
524 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300525 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600526 with open(output) as infile:
527 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600528 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600529struct dtd_test1 {
530\tfdt64_t\t\treg[2];
531};
532struct dtd_test2 {
533\tfdt64_t\t\treg[2];
534};
535struct dtd_test3 {
536\tfdt64_t\t\treg[4];
537};
538''', data)
539
Walter Lozanoa324e412020-06-25 01:10:08 -0300540 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600541 with open(output) as infile:
542 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600543 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300544static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600545\t.reg\t\t\t= {0x1234, 0x5678},
546};
547U_BOOT_DEVICE(test1) = {
548\t.name\t\t= "test1",
549\t.platdata\t= &dtv_test1,
550\t.platdata_size\t= sizeof(dtv_test1),
551};
552
Walter Lozanodc5b4372020-06-25 01:10:13 -0300553static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600554\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
555};
556U_BOOT_DEVICE(test2) = {
557\t.name\t\t= "test2",
558\t.platdata\t= &dtv_test2,
559\t.platdata_size\t= sizeof(dtv_test2),
560};
561
Walter Lozanodc5b4372020-06-25 01:10:13 -0300562static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600563\t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
564};
565U_BOOT_DEVICE(test3) = {
566\t.name\t\t= "test3",
567\t.platdata\t= &dtv_test3,
568\t.platdata_size\t= sizeof(dtv_test3),
569};
570
Walter Lozanodc5b4372020-06-25 01:10:13 -0300571''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600572
573 def test_addresses32(self):
574 """Test output from a node with a 'reg' property with na=1, ns=1"""
575 dtb_file = get_dtb_file('dtoc_test_addr32.dts')
576 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300577 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600578 with open(output) as infile:
579 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600580 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600581struct dtd_test1 {
582\tfdt32_t\t\treg[2];
583};
584struct dtd_test2 {
585\tfdt32_t\t\treg[4];
586};
587''', data)
588
Walter Lozanoa324e412020-06-25 01:10:08 -0300589 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600590 with open(output) as infile:
591 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600592 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300593static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600594\t.reg\t\t\t= {0x1234, 0x5678},
595};
596U_BOOT_DEVICE(test1) = {
597\t.name\t\t= "test1",
598\t.platdata\t= &dtv_test1,
599\t.platdata_size\t= sizeof(dtv_test1),
600};
601
Walter Lozanodc5b4372020-06-25 01:10:13 -0300602static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600603\t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
604};
605U_BOOT_DEVICE(test2) = {
606\t.name\t\t= "test2",
607\t.platdata\t= &dtv_test2,
608\t.platdata_size\t= sizeof(dtv_test2),
609};
610
Walter Lozanodc5b4372020-06-25 01:10:13 -0300611''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600612
613 def test_addresses64_32(self):
614 """Test output from a node with a 'reg' property with na=2, ns=1"""
615 dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
616 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300617 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600618 with open(output) as infile:
619 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600620 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600621struct dtd_test1 {
622\tfdt64_t\t\treg[2];
623};
624struct dtd_test2 {
625\tfdt64_t\t\treg[2];
626};
627struct dtd_test3 {
628\tfdt64_t\t\treg[4];
629};
630''', data)
631
Walter Lozanoa324e412020-06-25 01:10:08 -0300632 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600633 with open(output) as infile:
634 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600635 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300636static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600637\t.reg\t\t\t= {0x123400000000, 0x5678},
638};
639U_BOOT_DEVICE(test1) = {
640\t.name\t\t= "test1",
641\t.platdata\t= &dtv_test1,
642\t.platdata_size\t= sizeof(dtv_test1),
643};
644
Walter Lozanodc5b4372020-06-25 01:10:13 -0300645static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600646\t.reg\t\t\t= {0x1234567890123456, 0x98765432},
647};
648U_BOOT_DEVICE(test2) = {
649\t.name\t\t= "test2",
650\t.platdata\t= &dtv_test2,
651\t.platdata_size\t= sizeof(dtv_test2),
652};
653
Walter Lozanodc5b4372020-06-25 01:10:13 -0300654static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600655\t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
656};
657U_BOOT_DEVICE(test3) = {
658\t.name\t\t= "test3",
659\t.platdata\t= &dtv_test3,
660\t.platdata_size\t= sizeof(dtv_test3),
661};
662
Walter Lozanodc5b4372020-06-25 01:10:13 -0300663''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass1b1fe412017-08-29 14:15:50 -0600664
665 def test_addresses32_64(self):
666 """Test output from a node with a 'reg' property with na=1, ns=2"""
667 dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
668 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300669 self.run_test(['struct'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600670 with open(output) as infile:
671 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600672 self._CheckStrings(HEADER + '''
Simon Glass1b1fe412017-08-29 14:15:50 -0600673struct dtd_test1 {
674\tfdt64_t\t\treg[2];
675};
676struct dtd_test2 {
677\tfdt64_t\t\treg[2];
678};
679struct dtd_test3 {
680\tfdt64_t\t\treg[4];
681};
682''', data)
683
Walter Lozanoa324e412020-06-25 01:10:08 -0300684 self.run_test(['platdata'], dtb_file, output)
Simon Glass1b1fe412017-08-29 14:15:50 -0600685 with open(output) as infile:
686 data = infile.read()
Simon Glassc47c2b32018-07-06 10:27:25 -0600687 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300688static struct dtd_test1 dtv_test1 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600689\t.reg\t\t\t= {0x1234, 0x567800000000},
690};
691U_BOOT_DEVICE(test1) = {
692\t.name\t\t= "test1",
693\t.platdata\t= &dtv_test1,
694\t.platdata_size\t= sizeof(dtv_test1),
695};
696
Walter Lozanodc5b4372020-06-25 01:10:13 -0300697static struct dtd_test2 dtv_test2 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600698\t.reg\t\t\t= {0x12345678, 0x9876543210987654},
699};
700U_BOOT_DEVICE(test2) = {
701\t.name\t\t= "test2",
702\t.platdata\t= &dtv_test2,
703\t.platdata_size\t= sizeof(dtv_test2),
704};
705
Walter Lozanodc5b4372020-06-25 01:10:13 -0300706static struct dtd_test3 dtv_test3 = {
Simon Glass1b1fe412017-08-29 14:15:50 -0600707\t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
708};
709U_BOOT_DEVICE(test3) = {
710\t.name\t\t= "test3",
711\t.platdata\t= &dtv_test3,
712\t.platdata_size\t= sizeof(dtv_test3),
713};
714
Walter Lozanodc5b4372020-06-25 01:10:13 -0300715''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600716
717 def test_bad_reg(self):
718 """Test that a reg property with an invalid type generates an error"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600719 # Capture stderr since dtc will emit warnings for this file
720 dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600721 output = tools.GetOutputFilename('output')
722 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300723 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600724 self.assertIn("Node 'spl-test' reg property is not an int",
725 str(e.exception))
726
727 def test_bad_reg2(self):
728 """Test that a reg property with an invalid cell count is detected"""
Simon Glass3bce93d2018-07-06 10:27:37 -0600729 # Capture stderr since dtc will emit warnings for this file
730 dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
Simon Glass961c1ce2018-07-06 10:27:35 -0600731 output = tools.GetOutputFilename('output')
732 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300733 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600734 self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
735 str(e.exception))
736
737 def test_add_prop(self):
738 """Test that a subequent node can add a new property to a struct"""
739 dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
740 output = tools.GetOutputFilename('output')
Walter Lozanoa324e412020-06-25 01:10:08 -0300741 self.run_test(['struct'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600742 with open(output) as infile:
743 data = infile.read()
744 self._CheckStrings(HEADER + '''
745struct dtd_sandbox_spl_test {
746\tfdt32_t\t\tintarray;
747\tfdt32_t\t\tintval;
748};
749''', data)
750
Walter Lozanoa324e412020-06-25 01:10:08 -0300751 self.run_test(['platdata'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600752 with open(output) as infile:
753 data = infile.read()
754 self._CheckStrings(C_HEADER + '''
Walter Lozanodc5b4372020-06-25 01:10:13 -0300755static struct dtd_sandbox_spl_test dtv_spl_test = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600756\t.intval\t\t\t= 0x1,
757};
758U_BOOT_DEVICE(spl_test) = {
759\t.name\t\t= "sandbox_spl_test",
760\t.platdata\t= &dtv_spl_test,
761\t.platdata_size\t= sizeof(dtv_spl_test),
762};
763
Walter Lozanodc5b4372020-06-25 01:10:13 -0300764static struct dtd_sandbox_spl_test dtv_spl_test2 = {
Simon Glass961c1ce2018-07-06 10:27:35 -0600765\t.intarray\t\t= 0x5,
766};
767U_BOOT_DEVICE(spl_test2) = {
768\t.name\t\t= "sandbox_spl_test",
769\t.platdata\t= &dtv_spl_test2,
770\t.platdata_size\t= sizeof(dtv_spl_test2),
771};
772
Walter Lozanodc5b4372020-06-25 01:10:13 -0300773''' + C_EMPTY_POPULATE_PHANDLE_DATA, data)
Simon Glass961c1ce2018-07-06 10:27:35 -0600774
775 def testStdout(self):
776 """Test output to stdout"""
777 dtb_file = get_dtb_file('dtoc_test_simple.dts')
778 with test_util.capture_sys_output() as (stdout, stderr):
Walter Lozanoa324e412020-06-25 01:10:08 -0300779 self.run_test(['struct'], dtb_file, '-')
Simon Glass961c1ce2018-07-06 10:27:35 -0600780
781 def testNoCommand(self):
782 """Test running dtoc without a command"""
783 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300784 self.run_test([], '', '')
Simon Glass961c1ce2018-07-06 10:27:35 -0600785 self.assertIn("Please specify a command: struct, platdata",
786 str(e.exception))
787
788 def testBadCommand(self):
789 """Test running dtoc with an invalid command"""
790 dtb_file = get_dtb_file('dtoc_test_simple.dts')
791 output = tools.GetOutputFilename('output')
792 with self.assertRaises(ValueError) as e:
Walter Lozanoa324e412020-06-25 01:10:08 -0300793 self.run_test(['invalid-cmd'], dtb_file, output)
Simon Glass961c1ce2018-07-06 10:27:35 -0600794 self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
795 str(e.exception))