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