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 | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 12 | from dtoc import fdt |
| 13 | from dtoc import fdt_util |
| 14 | from dtoc.fdt import FdtScan |
Simon Glass | 4583c00 | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 15 | from u_boot_pylib import tools |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 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.') |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 22 | tools.prepare_output_dir(self._indir, True) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 23 | |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 24 | @classmethod |
| 25 | def tearDownClass(self): |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 26 | tools._finalise_for_test() |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 27 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 28 | 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 Glass | 1d88ebb | 2017-05-27 07:38:20 -0600 | [diff] [blame] | 34 | def _DeleteProp(self, dt): |
| 35 | node = dt.GetNode('/microcode/update@0') |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 36 | node.DeleteProp('data') |
| 37 | |
| 38 | def testFdtNormal(self): |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 39 | fname = self.GetCompiled('034_x86_ucode.dts') |
Simon Glass | 1d88ebb | 2017-05-27 07:38:20 -0600 | [diff] [blame] | 40 | dt = FdtScan(fname) |
| 41 | self._DeleteProp(dt) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 42 | |
Simon Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 43 | def testFdtNormalProp(self): |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 44 | fname = self.GetCompiled('045_prop_test.dts') |
Simon Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 45 | dt = FdtScan(fname) |
| 46 | node = dt.GetNode('/binman/intel-me') |
Brandon Maier | 357bfca | 2024-06-04 16:16:05 +0000 | [diff] [blame] | 47 | self.assertEqual('intel-me', node.name) |
Simon Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 48 | val = fdt_util.GetString(node, 'filename') |
Brandon Maier | 357bfca | 2024-06-04 16:16:05 +0000 | [diff] [blame] | 49 | self.assertEqual(str, type(val)) |
| 50 | self.assertEqual('me.bin', val) |
Simon Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 51 | |
| 52 | prop = node.props['intval'] |
Brandon Maier | 357bfca | 2024-06-04 16:16:05 +0000 | [diff] [blame] | 53 | self.assertEqual(fdt.Type.INT, prop.type) |
| 54 | self.assertEqual(3, fdt_util.GetInt(node, 'intval')) |
Simon Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 55 | |
| 56 | prop = node.props['intarray'] |
Brandon Maier | 357bfca | 2024-06-04 16:16:05 +0000 | [diff] [blame] | 57 | 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 Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 61 | [fdt_util.fdt32_to_cpu(val) for val in prop.value]) |
| 62 | |
| 63 | prop = node.props['byteval'] |
Brandon Maier | 357bfca | 2024-06-04 16:16:05 +0000 | [diff] [blame] | 64 | self.assertEqual(fdt.Type.BYTE, prop.type) |
| 65 | self.assertEqual(chr(8), prop.value) |
Simon Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 66 | |
| 67 | prop = node.props['bytearray'] |
Brandon Maier | 357bfca | 2024-06-04 16:16:05 +0000 | [diff] [blame] | 68 | 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 Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 73 | |
| 74 | prop = node.props['longbytearray'] |
Brandon Maier | 357bfca | 2024-06-04 16:16:05 +0000 | [diff] [blame] | 75 | self.assertEqual(fdt.Type.INT, prop.type) |
| 76 | self.assertEqual(0x090a0b0c, fdt_util.GetInt(node, 'longbytearray')) |
Simon Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 77 | |
| 78 | prop = node.props['stringval'] |
Brandon Maier | 357bfca | 2024-06-04 16:16:05 +0000 | [diff] [blame] | 79 | self.assertEqual(fdt.Type.STRING, prop.type) |
| 80 | self.assertEqual('message2', fdt_util.GetString(node, 'stringval')) |
Simon Glass | 4a28b00 | 2017-05-27 07:38:21 -0600 | [diff] [blame] | 81 | |
| 82 | prop = node.props['stringarray'] |
Brandon Maier | 357bfca | 2024-06-04 16:16:05 +0000 | [diff] [blame] | 83 | 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) |