blob: 7ef872954630184983cc4f56c47295bbaa44884c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass2574ef62016-11-25 20:15:51 -07005# Test for the fdt modules
6
7import os
8import sys
9import tempfile
10import unittest
11
Simon Glassc585dd42020-04-17 18:09:03 -060012from dtoc import fdt
13from dtoc import fdt_util
14from dtoc.fdt import FdtScan
Simon Glass131444f2023-02-23 18:18:04 -070015from u_boot_pylib import tools
Simon Glass2574ef62016-11-25 20:15:51 -070016
17class TestFdt(unittest.TestCase):
18 @classmethod
19 def setUpClass(self):
20 self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
21 self._indir = tempfile.mkdtemp(prefix='binmant.')
Simon Glass80025522022-01-29 14:14:04 -070022 tools.prepare_output_dir(self._indir, True)
Simon Glass2574ef62016-11-25 20:15:51 -070023
Simon Glass752e7552018-10-01 21:12:41 -060024 @classmethod
25 def tearDownClass(self):
Simon Glass80025522022-01-29 14:14:04 -070026 tools._finalise_for_test()
Simon Glass752e7552018-10-01 21:12:41 -060027
Simon Glass2574ef62016-11-25 20:15:51 -070028 def TestFile(self, fname):
29 return os.path.join(self._binman_dir, 'test', fname)
30
31 def GetCompiled(self, fname):
32 return fdt_util.EnsureCompiled(self.TestFile(fname))
33
Simon Glass28c71052017-05-27 07:38:20 -060034 def _DeleteProp(self, dt):
35 node = dt.GetNode('/microcode/update@0')
Simon Glass2574ef62016-11-25 20:15:51 -070036 node.DeleteProp('data')
37
38 def testFdtNormal(self):
Simon Glass511f6582018-10-01 12:22:30 -060039 fname = self.GetCompiled('034_x86_ucode.dts')
Simon Glass28c71052017-05-27 07:38:20 -060040 dt = FdtScan(fname)
41 self._DeleteProp(dt)
Simon Glass2574ef62016-11-25 20:15:51 -070042
Simon Glassd022a7d2017-05-27 07:38:21 -060043 def testFdtNormalProp(self):
Simon Glass511f6582018-10-01 12:22:30 -060044 fname = self.GetCompiled('045_prop_test.dts')
Simon Glassd022a7d2017-05-27 07:38:21 -060045 dt = FdtScan(fname)
46 node = dt.GetNode('/binman/intel-me')
47 self.assertEquals('intel-me', node.name)
48 val = fdt_util.GetString(node, 'filename')
49 self.assertEquals(str, type(val))
50 self.assertEquals('me.bin', val)
51
52 prop = node.props['intval']
Simon Glassc9a032c2020-11-08 20:36:17 -070053 self.assertEquals(fdt.Type.INT, prop.type)
Simon Glassd022a7d2017-05-27 07:38:21 -060054 self.assertEquals(3, fdt_util.GetInt(node, 'intval'))
55
56 prop = node.props['intarray']
Simon Glassc9a032c2020-11-08 20:36:17 -070057 self.assertEquals(fdt.Type.INT, prop.type)
Simon Glassd022a7d2017-05-27 07:38:21 -060058 self.assertEquals(list, type(prop.value))
59 self.assertEquals(2, len(prop.value))
60 self.assertEquals([5, 6],
61 [fdt_util.fdt32_to_cpu(val) for val in prop.value])
62
63 prop = node.props['byteval']
Simon Glassc9a032c2020-11-08 20:36:17 -070064 self.assertEquals(fdt.Type.BYTE, prop.type)
Simon Glassd022a7d2017-05-27 07:38:21 -060065 self.assertEquals(chr(8), prop.value)
66
67 prop = node.props['bytearray']
Simon Glassc9a032c2020-11-08 20:36:17 -070068 self.assertEquals(fdt.Type.BYTE, prop.type)
Simon Glassd022a7d2017-05-27 07:38:21 -060069 self.assertEquals(list, type(prop.value))
70 self.assertEquals(str, type(prop.value[0]))
71 self.assertEquals(3, len(prop.value))
72 self.assertEquals([chr(1), '#', '4'], prop.value)
73
74 prop = node.props['longbytearray']
Simon Glassc9a032c2020-11-08 20:36:17 -070075 self.assertEquals(fdt.Type.INT, prop.type)
Simon Glassd022a7d2017-05-27 07:38:21 -060076 self.assertEquals(0x090a0b0c, fdt_util.GetInt(node, 'longbytearray'))
77
78 prop = node.props['stringval']
Simon Glassc9a032c2020-11-08 20:36:17 -070079 self.assertEquals(fdt.Type.STRING, prop.type)
Simon Glassd022a7d2017-05-27 07:38:21 -060080 self.assertEquals('message2', fdt_util.GetString(node, 'stringval'))
81
82 prop = node.props['stringarray']
Simon Glassc9a032c2020-11-08 20:36:17 -070083 self.assertEquals(fdt.Type.STRING, prop.type)
Simon Glassd022a7d2017-05-27 07:38:21 -060084 self.assertEquals(list, type(prop.value))
85 self.assertEquals(3, len(prop.value))
86 self.assertEquals(['another', 'multi-word', 'message'], prop.value)