xref: /openbmc/u-boot/tools/binman/fdt_test.py (revision 7c1fb0a7)
1#
2# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
5# SPDX-License-Identifier:      GPL-2.0+
6#
7# Test for the fdt modules
8
9import os
10import sys
11import tempfile
12import unittest
13
14import fdt
15from fdt import FdtScan
16import fdt_util
17import tools
18
19class TestFdt(unittest.TestCase):
20    @classmethod
21    def setUpClass(self):
22        self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
23        self._indir = tempfile.mkdtemp(prefix='binmant.')
24        tools.PrepareOutputDir(self._indir, True)
25
26    def TestFile(self, fname):
27        return os.path.join(self._binman_dir, 'test', fname)
28
29    def GetCompiled(self, fname):
30        return fdt_util.EnsureCompiled(self.TestFile(fname))
31
32    def _DeleteProp(self, dt):
33        node = dt.GetNode('/microcode/update@0')
34        node.DeleteProp('data')
35
36    def testFdtNormal(self):
37        fname = self.GetCompiled('34_x86_ucode.dts')
38        dt = FdtScan(fname)
39        self._DeleteProp(dt)
40
41    def testFdtNormalProp(self):
42        fname = self.GetCompiled('45_prop_test.dts')
43        dt = FdtScan(fname)
44        node = dt.GetNode('/binman/intel-me')
45        self.assertEquals('intel-me', node.name)
46        val = fdt_util.GetString(node, 'filename')
47        self.assertEquals(str, type(val))
48        self.assertEquals('me.bin', val)
49
50        prop = node.props['intval']
51        self.assertEquals(fdt.TYPE_INT, prop.type)
52        self.assertEquals(3, fdt_util.GetInt(node, 'intval'))
53
54        prop = node.props['intarray']
55        self.assertEquals(fdt.TYPE_INT, prop.type)
56        self.assertEquals(list, type(prop.value))
57        self.assertEquals(2, len(prop.value))
58        self.assertEquals([5, 6],
59                          [fdt_util.fdt32_to_cpu(val) for val in prop.value])
60
61        prop = node.props['byteval']
62        self.assertEquals(fdt.TYPE_BYTE, prop.type)
63        self.assertEquals(chr(8), prop.value)
64
65        prop = node.props['bytearray']
66        self.assertEquals(fdt.TYPE_BYTE, prop.type)
67        self.assertEquals(list, type(prop.value))
68        self.assertEquals(str, type(prop.value[0]))
69        self.assertEquals(3, len(prop.value))
70        self.assertEquals([chr(1), '#', '4'], prop.value)
71
72        prop = node.props['longbytearray']
73        self.assertEquals(fdt.TYPE_INT, prop.type)
74        self.assertEquals(0x090a0b0c, fdt_util.GetInt(node, 'longbytearray'))
75
76        prop = node.props['stringval']
77        self.assertEquals(fdt.TYPE_STRING, prop.type)
78        self.assertEquals('message2', fdt_util.GetString(node, 'stringval'))
79
80        prop = node.props['stringarray']
81        self.assertEquals(fdt.TYPE_STRING, prop.type)
82        self.assertEquals(list, type(prop.value))
83        self.assertEquals(3, len(prop.value))
84        self.assertEquals(['another', 'multi-word', 'message'], prop.value)
85