blob: 564c17708207a259ce4a209944af09f3febf0f7b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassbf7fd502016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glassbf7fd502016-11-25 20:15:51 -07005# Test for the fdt modules
6
7import os
8import sys
9import tempfile
10import unittest
11
Simon Glass16287932020-04-17 18:09:03 -060012from dtoc import fdt
13from dtoc import fdt_util
14from dtoc.fdt import FdtScan
Simon Glass4583c002023-02-23 18:18:04 -070015from u_boot_pylib import tools
Simon Glassbf7fd502016-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 Glassc1aa66e2022-01-29 14:14:04 -070022 tools.prepare_output_dir(self._indir, True)
Simon Glassbf7fd502016-11-25 20:15:51 -070023
Simon Glasse0e62752018-10-01 21:12:41 -060024 @classmethod
25 def tearDownClass(self):
Simon Glassc1aa66e2022-01-29 14:14:04 -070026 tools._finalise_for_test()
Simon Glasse0e62752018-10-01 21:12:41 -060027
Simon Glassbf7fd502016-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 Glass1d88ebb2017-05-27 07:38:20 -060034 def _DeleteProp(self, dt):
35 node = dt.GetNode('/microcode/update@0')
Simon Glassbf7fd502016-11-25 20:15:51 -070036 node.DeleteProp('data')
37
38 def testFdtNormal(self):
Simon Glass741f2d62018-10-01 12:22:30 -060039 fname = self.GetCompiled('034_x86_ucode.dts')
Simon Glass1d88ebb2017-05-27 07:38:20 -060040 dt = FdtScan(fname)
41 self._DeleteProp(dt)
Simon Glassbf7fd502016-11-25 20:15:51 -070042
Simon Glass4a28b002017-05-27 07:38:21 -060043 def testFdtNormalProp(self):
Simon Glass741f2d62018-10-01 12:22:30 -060044 fname = self.GetCompiled('045_prop_test.dts')
Simon Glass4a28b002017-05-27 07:38:21 -060045 dt = FdtScan(fname)
46 node = dt.GetNode('/binman/intel-me')
Brandon Maier357bfca2024-06-04 16:16:05 +000047 self.assertEqual('intel-me', node.name)
Simon Glass4a28b002017-05-27 07:38:21 -060048 val = fdt_util.GetString(node, 'filename')
Brandon Maier357bfca2024-06-04 16:16:05 +000049 self.assertEqual(str, type(val))
50 self.assertEqual('me.bin', val)
Simon Glass4a28b002017-05-27 07:38:21 -060051
52 prop = node.props['intval']
Brandon Maier357bfca2024-06-04 16:16:05 +000053 self.assertEqual(fdt.Type.INT, prop.type)
54 self.assertEqual(3, fdt_util.GetInt(node, 'intval'))
Simon Glass4a28b002017-05-27 07:38:21 -060055
56 prop = node.props['intarray']
Brandon Maier357bfca2024-06-04 16:16:05 +000057 self.assertEqual(fdt.Type.INT, prop.type)
58 self.assertEqual(list, type(prop.value))
59 self.assertEqual(2, len(prop.value))
60 self.assertEqual([5, 6],
Simon Glass4a28b002017-05-27 07:38:21 -060061 [fdt_util.fdt32_to_cpu(val) for val in prop.value])
62
63 prop = node.props['byteval']
Brandon Maier357bfca2024-06-04 16:16:05 +000064 self.assertEqual(fdt.Type.BYTE, prop.type)
65 self.assertEqual(chr(8), prop.value)
Simon Glass4a28b002017-05-27 07:38:21 -060066
67 prop = node.props['bytearray']
Brandon Maier357bfca2024-06-04 16:16:05 +000068 self.assertEqual(fdt.Type.BYTE, prop.type)
69 self.assertEqual(list, type(prop.value))
70 self.assertEqual(str, type(prop.value[0]))
71 self.assertEqual(3, len(prop.value))
72 self.assertEqual([chr(1), '#', '4'], prop.value)
Simon Glass4a28b002017-05-27 07:38:21 -060073
74 prop = node.props['longbytearray']
Brandon Maier357bfca2024-06-04 16:16:05 +000075 self.assertEqual(fdt.Type.INT, prop.type)
76 self.assertEqual(0x090a0b0c, fdt_util.GetInt(node, 'longbytearray'))
Simon Glass4a28b002017-05-27 07:38:21 -060077
78 prop = node.props['stringval']
Brandon Maier357bfca2024-06-04 16:16:05 +000079 self.assertEqual(fdt.Type.STRING, prop.type)
80 self.assertEqual('message2', fdt_util.GetString(node, 'stringval'))
Simon Glass4a28b002017-05-27 07:38:21 -060081
82 prop = node.props['stringarray']
Brandon Maier357bfca2024-06-04 16:16:05 +000083 self.assertEqual(fdt.Type.STRING, prop.type)
84 self.assertEqual(list, type(prop.value))
85 self.assertEqual(3, len(prop.value))
86 self.assertEqual(['another', 'multi-word', 'message'], prop.value)