2019-06-20 23:19:41 +02:00
|
|
|
# SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
|
2017-03-17 23:14:31 +01:00
|
|
|
# pylibfdt - Tests for Flat Device Tree manipulation in Python
|
|
|
|
# Copyright (C) 2017 Google, Inc.
|
|
|
|
# Written by Simon Glass <sjg@chromium.org>
|
|
|
|
#
|
|
|
|
|
2018-06-06 23:37:05 +02:00
|
|
|
import struct
|
2017-03-17 23:14:31 +01:00
|
|
|
import sys
|
|
|
|
import types
|
|
|
|
import unittest
|
|
|
|
|
2017-08-19 19:17:55 +02:00
|
|
|
sys.path.insert(0, '../pylibfdt')
|
2017-03-17 23:14:31 +01:00
|
|
|
import libfdt
|
2018-07-10 22:49:07 +02:00
|
|
|
from libfdt import Fdt, FdtSw, FdtException, QUIET_NOTFOUND, QUIET_ALL
|
|
|
|
|
|
|
|
TEST_ADDR_1H = 0xdeadbeef
|
|
|
|
TEST_ADDR_1L = 0x00000000
|
|
|
|
TEST_ADDR_1 = (TEST_ADDR_1H << 32) | TEST_ADDR_1L
|
|
|
|
TEST_ADDR_1 = 0x8000000000000000
|
|
|
|
TEST_SIZE_1H = 0x00000000
|
|
|
|
TEST_SIZE_1L = 0x00100000
|
|
|
|
TEST_SIZE_1 = (TEST_SIZE_1H << 32) | TEST_SIZE_1L
|
|
|
|
TEST_ADDR_2H = 0
|
|
|
|
TEST_ADDR_2L = 123456789
|
|
|
|
TEST_ADDR_2 = (TEST_ADDR_2H << 32) | TEST_ADDR_2L
|
|
|
|
TEST_SIZE_2H = 0
|
2018-11-23 07:47:41 +01:00
|
|
|
TEST_SIZE_2L = 0o10000
|
2018-07-10 22:49:07 +02:00
|
|
|
TEST_SIZE_2 = (TEST_SIZE_2H << 32) | TEST_SIZE_2L
|
2018-06-06 23:37:05 +02:00
|
|
|
|
|
|
|
TEST_VALUE_1 = 0xdeadbeef
|
2018-07-10 22:49:07 +02:00
|
|
|
TEST_VALUE_2 = 123456789
|
2018-06-06 23:37:05 +02:00
|
|
|
|
|
|
|
TEST_VALUE64_1H = 0xdeadbeef
|
|
|
|
TEST_VALUE64_1L = 0x01abcdef
|
|
|
|
TEST_VALUE64_1 = (TEST_VALUE64_1H << 32) | TEST_VALUE64_1L
|
|
|
|
|
2018-07-10 22:49:07 +02:00
|
|
|
PHANDLE_1 = 0x2000
|
|
|
|
PHANDLE_2 = 0x2001
|
|
|
|
|
2019-02-18 17:48:54 +01:00
|
|
|
TEST_BYTES_1 = b'hello world'
|
|
|
|
|
2018-06-06 23:37:05 +02:00
|
|
|
TEST_STRING_1 = 'hello world'
|
2018-06-14 05:44:43 +02:00
|
|
|
TEST_STRING_2 = 'hi world'
|
2019-02-18 17:48:52 +01:00
|
|
|
TEST_STRING_3 = u'unicode \u01d3'
|
2018-06-06 23:37:05 +02:00
|
|
|
|
|
|
|
|
2017-03-17 23:14:31 +01:00
|
|
|
def get_err(err_code):
|
|
|
|
"""Convert an error code into an error message
|
|
|
|
|
|
|
|
Args:
|
|
|
|
err_code: Error code value (FDT_ERR_...)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
String error code
|
|
|
|
"""
|
2017-08-19 19:17:55 +02:00
|
|
|
return 'pylibfdt error %d: %s' % (-err_code, libfdt.strerror(-err_code))
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def _ReadFdt(fname):
|
|
|
|
"""Read a device tree file into an Fdt object, ready for use
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fname: Filename to read from
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Fdt bytearray suitable for passing to libfdt functions
|
|
|
|
"""
|
2019-10-09 12:20:23 +02:00
|
|
|
with open(fname, mode='rb') as f:
|
|
|
|
return libfdt.Fdt(f.read())
|
2017-03-17 23:14:31 +01:00
|
|
|
|
2018-07-10 22:49:07 +02:00
|
|
|
class PyLibfdtBasicTests(unittest.TestCase):
|
|
|
|
"""Test class for basic pylibfdt access functions
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
Properties:
|
|
|
|
fdt: Device tree file used for testing
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Read in the device tree we use for testing"""
|
|
|
|
self.fdt = _ReadFdt('test_tree1.dtb')
|
2018-06-13 07:37:31 +02:00
|
|
|
self.fdt2 = _ReadFdt('test_props.dtb')
|
2019-07-03 07:08:07 +02:00
|
|
|
self.fdt3 = _ReadFdt('aliases.dtb')
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def GetPropList(self, node_path):
|
|
|
|
"""Read a list of properties from a node
|
|
|
|
|
|
|
|
Args:
|
|
|
|
node_path: Full path to node, e.g. '/subnode@1/subsubnode'
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of property names for that node, e.g. ['compatible', 'reg']
|
|
|
|
"""
|
|
|
|
prop_list = []
|
|
|
|
node = self.fdt.path_offset(node_path)
|
|
|
|
poffset = self.fdt.first_property_offset(node, QUIET_NOTFOUND)
|
|
|
|
while poffset > 0:
|
|
|
|
prop = self.fdt.get_property_by_offset(poffset)
|
|
|
|
prop_list.append(prop.name)
|
|
|
|
poffset = self.fdt.next_property_offset(poffset, QUIET_NOTFOUND)
|
|
|
|
return prop_list
|
|
|
|
|
2018-09-11 11:28:39 +02:00
|
|
|
def GetSubnodes(self, node_path):
|
|
|
|
"""Read a list of subnodes from a node
|
|
|
|
|
|
|
|
Args:
|
|
|
|
node_path: Full path to node, e.g. '/subnode@1/subsubnode'
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of subnode names for that node, e.g. ['subsubnode', 'ss1']
|
|
|
|
"""
|
|
|
|
subnode_list = []
|
|
|
|
node = self.fdt.path_offset(node_path)
|
|
|
|
offset = self.fdt.first_subnode(node, QUIET_NOTFOUND)
|
|
|
|
while offset > 0:
|
|
|
|
name = self.fdt.get_name(offset)
|
|
|
|
subnode_list.append(name)
|
|
|
|
offset = self.fdt.next_subnode(offset, QUIET_NOTFOUND)
|
|
|
|
return subnode_list
|
|
|
|
|
2017-03-17 23:14:31 +01:00
|
|
|
def testImport(self):
|
|
|
|
"""Check that we can import the library correctly"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(type(libfdt), types.ModuleType)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def testBadFdt(self):
|
|
|
|
"""Check that a filename provided accidentally is not accepted"""
|
|
|
|
with self.assertRaises(FdtException) as e:
|
2019-02-18 17:48:52 +01:00
|
|
|
fdt = libfdt.Fdt(b'a string')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.BADMAGIC)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
2017-08-19 19:18:01 +02:00
|
|
|
def testSubnodeOffset(self):
|
|
|
|
"""check that we can locate a subnode by name"""
|
|
|
|
node1 = self.fdt.path_offset('/subnode@1')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.subnode_offset(0, 'subnode@1'), node1)
|
2017-08-19 19:18:01 +02:00
|
|
|
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.subnode_offset(0, 'missing')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
|
2017-08-19 19:18:01 +02:00
|
|
|
|
|
|
|
node2 = self.fdt.path_offset('/subnode@1/subsubnode')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.subnode_offset(node1, 'subsubnode'), node2)
|
2017-08-19 19:18:01 +02:00
|
|
|
|
2017-03-17 23:14:31 +01:00
|
|
|
def testPathOffset(self):
|
|
|
|
"""Check that we can find the offset of a node"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.path_offset('/'), 0)
|
2017-03-17 23:14:31 +01:00
|
|
|
self.assertTrue(self.fdt.path_offset('/subnode@1') > 0)
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.path_offset('/wibble')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
|
|
|
|
self.assertEqual(self.fdt.path_offset('/wibble', QUIET_NOTFOUND),
|
2017-03-17 23:14:31 +01:00
|
|
|
-libfdt.NOTFOUND)
|
|
|
|
|
|
|
|
def testPropertyOffset(self):
|
|
|
|
"""Walk through all the properties in the root node"""
|
|
|
|
offset = self.fdt.first_property_offset(0)
|
|
|
|
self.assertTrue(offset > 0)
|
|
|
|
for i in range(5):
|
|
|
|
next_offset = self.fdt.next_property_offset(offset)
|
|
|
|
self.assertTrue(next_offset > offset)
|
|
|
|
offset = next_offset
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.next_property_offset(offset, QUIET_NOTFOUND),
|
2017-03-17 23:14:31 +01:00
|
|
|
-libfdt.NOTFOUND)
|
|
|
|
|
|
|
|
def testPropertyOffsetExceptions(self):
|
|
|
|
"""Check that exceptions are raised as expected"""
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.first_property_offset(107)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
# Quieten the NOTFOUND exception and check that a BADOFFSET
|
|
|
|
# exception is still raised.
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.first_property_offset(107, QUIET_NOTFOUND)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
|
2017-03-17 23:14:31 +01:00
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.next_property_offset(107, QUIET_NOTFOUND)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
# Check that NOTFOUND can be quietened.
|
|
|
|
node = self.fdt.path_offset('/subnode@1/ss1')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.first_property_offset(node, QUIET_NOTFOUND),
|
2017-03-17 23:14:31 +01:00
|
|
|
-libfdt.NOTFOUND)
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.first_property_offset(node)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def testGetName(self):
|
|
|
|
"""Check that we can get the name of a node"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.get_name(0), '')
|
2017-03-17 23:14:31 +01:00
|
|
|
node = self.fdt.path_offset('/subnode@1/subsubnode')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.get_name(node), 'subsubnode')
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.get_name(-2)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def testGetPropertyByOffset(self):
|
|
|
|
"""Check that we can read the name and contents of a property"""
|
|
|
|
root = 0
|
|
|
|
poffset = self.fdt.first_property_offset(root)
|
|
|
|
prop = self.fdt.get_property_by_offset(poffset)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(prop.name, 'compatible')
|
|
|
|
self.assertEqual(prop, b'test_tree1\0')
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.get_property_by_offset(-2)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
|
|
|
|
self.assertEqual(
|
2017-03-17 23:14:31 +01:00
|
|
|
-libfdt.BADOFFSET,
|
|
|
|
self.fdt.get_property_by_offset(-2, [libfdt.BADOFFSET]))
|
|
|
|
|
|
|
|
def testGetProp(self):
|
|
|
|
"""Check that we can read the contents of a property by name"""
|
|
|
|
root = self.fdt.path_offset('/')
|
|
|
|
value = self.fdt.getprop(root, "compatible")
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(value, b'test_tree1\0')
|
|
|
|
self.assertEqual(-libfdt.NOTFOUND, self.fdt.getprop(root, 'missing',
|
2017-03-17 23:14:31 +01:00
|
|
|
QUIET_NOTFOUND))
|
|
|
|
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.getprop(root, 'missing')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
node = self.fdt.path_offset('/subnode@1/subsubnode')
|
|
|
|
value = self.fdt.getprop(node, "compatible")
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(value, b'subsubnode1\0subsubnode\0')
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def testStrError(self):
|
|
|
|
"""Check that we can get an error string"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(libfdt.strerror(-libfdt.NOTFOUND),
|
2017-03-17 23:14:31 +01:00
|
|
|
'FDT_ERR_NOTFOUND')
|
|
|
|
|
2018-06-06 23:37:01 +02:00
|
|
|
def testNextNodeOffset(self):
|
|
|
|
"""Check that we can walk through nodes"""
|
|
|
|
node_list = []
|
|
|
|
node = 0
|
|
|
|
depth = 0
|
|
|
|
while depth >= 0:
|
|
|
|
node_list.append([depth, self.fdt.get_name(node)])
|
|
|
|
node, depth = self.fdt.next_node(node, depth, (libfdt.BADOFFSET,))
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(node_list, [
|
2018-06-06 23:37:01 +02:00
|
|
|
[0, ''],
|
|
|
|
[1, 'subnode@1'],
|
|
|
|
[2, 'subsubnode'],
|
|
|
|
[2, 'ss1'],
|
|
|
|
[1, 'subnode@2'],
|
|
|
|
[2, 'subsubnode@0'],
|
|
|
|
[2, 'ss2'],
|
|
|
|
])
|
|
|
|
|
2017-03-17 23:14:31 +01:00
|
|
|
def testFirstNextSubnodeOffset(self):
|
|
|
|
"""Check that we can walk through subnodes"""
|
|
|
|
node_list = []
|
|
|
|
node = self.fdt.first_subnode(0, QUIET_NOTFOUND)
|
|
|
|
while node >= 0:
|
|
|
|
node_list.append(self.fdt.get_name(node))
|
|
|
|
node = self.fdt.next_subnode(node, QUIET_NOTFOUND)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(node_list, ['subnode@1', 'subnode@2'])
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def testFirstNextSubnodeOffsetExceptions(self):
|
|
|
|
"""Check except handling for first/next subnode functions"""
|
|
|
|
node = self.fdt.path_offset('/subnode@1/subsubnode', QUIET_NOTFOUND)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.first_subnode(node, QUIET_NOTFOUND),
|
2017-03-17 23:14:31 +01:00
|
|
|
-libfdt.NOTFOUND)
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.first_subnode(node)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
node = self.fdt.path_offset('/subnode@1/ss1', QUIET_NOTFOUND)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.next_subnode(node, QUIET_NOTFOUND),
|
2017-03-17 23:14:31 +01:00
|
|
|
-libfdt.NOTFOUND)
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.next_subnode(node)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def testDeleteProperty(self):
|
|
|
|
"""Test that we can delete a property"""
|
|
|
|
node_name = '/subnode@1'
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.GetPropList(node_name),
|
2017-03-17 23:14:31 +01:00
|
|
|
['compatible', 'reg', 'prop-int'])
|
|
|
|
node = self.fdt.path_offset('/%s' % node_name)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.delprop(node, 'reg'), 0)
|
|
|
|
self.assertEqual(self.GetPropList(node_name),
|
2017-03-17 23:14:31 +01:00
|
|
|
['compatible', 'prop-int'])
|
|
|
|
|
|
|
|
def testHeader(self):
|
|
|
|
"""Test that we can access the header values"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.magic(), 0xd00dfeed)
|
|
|
|
self.assertEqual(self.fdt.totalsize(), len(self.fdt._fdt))
|
|
|
|
self.assertEqual(self.fdt.off_dt_struct(), 88)
|
|
|
|
self.assertEqual(self.fdt.off_dt_strings(), 652)
|
|
|
|
self.assertEqual(self.fdt.off_mem_rsvmap(), 40)
|
|
|
|
self.assertEqual(self.fdt.version(), 17)
|
|
|
|
self.assertEqual(self.fdt.last_comp_version(), 16)
|
|
|
|
self.assertEqual(self.fdt.boot_cpuid_phys(), 0)
|
|
|
|
self.assertEqual(self.fdt.size_dt_strings(), 105)
|
|
|
|
self.assertEqual(self.fdt.size_dt_struct(), 564)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def testPack(self):
|
|
|
|
"""Test that we can pack the tree after deleting something"""
|
|
|
|
orig_size = self.fdt.totalsize()
|
|
|
|
node = self.fdt.path_offset('/subnode@2', QUIET_NOTFOUND)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.delprop(node, 'prop-int'), 0)
|
|
|
|
self.assertEqual(orig_size, self.fdt.totalsize())
|
|
|
|
self.assertEqual(self.fdt.pack(), 0)
|
2017-03-17 23:14:31 +01:00
|
|
|
self.assertTrue(self.fdt.totalsize() < orig_size)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.totalsize(), len(self.fdt.as_bytearray()))
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def testBadPropertyOffset(self):
|
|
|
|
"""Test that bad property offsets are detected"""
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.get_property_by_offset(13)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
|
2017-03-17 23:14:31 +01:00
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.first_property_offset(3)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
|
2017-03-17 23:14:31 +01:00
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.next_property_offset(3)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
|
2017-03-17 23:14:31 +01:00
|
|
|
|
|
|
|
def testBadPathOffset(self):
|
|
|
|
"""Test that bad path names are detected"""
|
2019-11-13 02:24:10 +01:00
|
|
|
with self.assertRaisesRegex(FdtException, get_err(libfdt.BADPATH)):
|
2017-03-17 23:14:31 +01:00
|
|
|
self.fdt.path_offset('not-present')
|
|
|
|
|
|
|
|
def testQuietAll(self):
|
|
|
|
"""Check that exceptions can be masked by QUIET_ALL"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(-libfdt.NOTFOUND,
|
2017-03-17 23:14:31 +01:00
|
|
|
self.fdt.path_offset('/missing', QUIET_ALL))
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(-libfdt.BADOFFSET,
|
2017-03-17 23:14:31 +01:00
|
|
|
self.fdt.get_property_by_offset(13, QUIET_ALL))
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(-libfdt.BADPATH,
|
2017-03-17 23:14:31 +01:00
|
|
|
self.fdt.path_offset('missing', QUIET_ALL))
|
|
|
|
|
2017-08-19 19:17:54 +02:00
|
|
|
def testIntegers(self):
|
|
|
|
"""Check that integers can be passed and returned"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(0, libfdt.fdt_get_phandle(self.fdt._fdt, 0))
|
2017-08-19 19:17:54 +02:00
|
|
|
node2 = self.fdt.path_offset('/subnode@2')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(0x2000, libfdt.fdt_get_phandle(self.fdt._fdt, node2))
|
2017-03-17 23:14:31 +01:00
|
|
|
|
2017-08-19 19:17:57 +02:00
|
|
|
def testGetPhandle(self):
|
|
|
|
"""Test for the get_phandle() method"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(0, self.fdt.get_phandle(0))
|
2017-08-19 19:17:57 +02:00
|
|
|
node2 = self.fdt.path_offset('/subnode@2')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(0x2000, self.fdt.get_phandle(node2))
|
2017-08-19 19:17:57 +02:00
|
|
|
|
2019-07-03 07:08:07 +02:00
|
|
|
def testGetAlias(self):
|
|
|
|
"""Test for the get_alias() method"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual("/subnode@1", self.fdt3.get_alias('s1'))
|
|
|
|
self.assertEqual("/subnode@1/subsubnode", self.fdt3.get_alias('ss1'))
|
|
|
|
self.assertEqual("/subnode@1/subsubnode/subsubsubnode", self.fdt3.get_alias('sss1'))
|
2019-07-03 07:08:07 +02:00
|
|
|
|
2017-08-19 19:17:58 +02:00
|
|
|
def testParentOffset(self):
|
|
|
|
"""Test for the parent_offset() method"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(-libfdt.NOTFOUND,
|
2017-08-19 19:17:58 +02:00
|
|
|
self.fdt.parent_offset(0, QUIET_NOTFOUND))
|
|
|
|
with self.assertRaises(FdtException) as e:
|
|
|
|
self.fdt.parent_offset(0)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
|
2017-08-19 19:17:58 +02:00
|
|
|
|
|
|
|
node1 = self.fdt.path_offset('/subnode@2')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(0, self.fdt.parent_offset(node1))
|
2017-08-19 19:17:58 +02:00
|
|
|
node2 = self.fdt.path_offset('/subnode@2/subsubnode@0')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(node1, self.fdt.parent_offset(node2))
|
2017-08-19 19:17:58 +02:00
|
|
|
|
2017-08-19 19:17:59 +02:00
|
|
|
def testNodeOffsetByPhandle(self):
|
|
|
|
"""Test for the node_offset_by_phandle() method"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(-libfdt.NOTFOUND,
|
2017-08-19 19:17:59 +02:00
|
|
|
self.fdt.node_offset_by_phandle(1, QUIET_NOTFOUND))
|
|
|
|
node1 = self.fdt.path_offset('/subnode@2')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(node1, self.fdt.node_offset_by_phandle(0x2000))
|
2017-08-19 19:17:59 +02:00
|
|
|
node2 = self.fdt.path_offset('/subnode@2/subsubnode@0')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(node2, self.fdt.node_offset_by_phandle(0x2001))
|
2017-08-19 19:17:59 +02:00
|
|
|
|
2018-06-13 07:37:31 +02:00
|
|
|
def get_prop(self, name):
|
|
|
|
return self.fdt2.getprop(0, name)
|
|
|
|
|
|
|
|
def testGetIntProperties(self):
|
|
|
|
"""Test that we can access properties as integers"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(0xdeadbeef, self.get_prop("prop-hex32").as_uint32())
|
|
|
|
self.assertEqual(123, self.get_prop("prop-uint32").as_uint32())
|
|
|
|
self.assertEqual(-2, self.get_prop("prop-int32").as_int32())
|
|
|
|
self.assertEqual(9223372036854775807,
|
2018-06-13 07:37:31 +02:00
|
|
|
self.get_prop("prop-uint64").as_uint64())
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(-2, self.get_prop("prop-int64").as_int64())
|
2017-08-19 19:17:59 +02:00
|
|
|
|
2018-06-06 23:37:03 +02:00
|
|
|
def testReserveMap(self):
|
|
|
|
"""Test that we can access the memory reserve map"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(2, self.fdt.num_mem_rsv())
|
|
|
|
self.assertEqual([ 0xdeadbeef00000000, 0x100000],
|
2018-06-06 23:37:03 +02:00
|
|
|
self.fdt.get_mem_rsv(0))
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual([123456789, 0o10000], self.fdt.get_mem_rsv(1))
|
2018-06-06 23:37:03 +02:00
|
|
|
|
2018-06-06 23:37:04 +02:00
|
|
|
def testEmpty(self):
|
|
|
|
"""Test that we can create an empty tree"""
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(-libfdt.NOSPACE,
|
2018-06-06 23:37:04 +02:00
|
|
|
Fdt.create_empty_tree(1, (libfdt.NOSPACE,)))
|
|
|
|
fdt = Fdt.create_empty_tree(128)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(128, fdt.totalsize())
|
2018-06-06 23:37:04 +02:00
|
|
|
|
|
|
|
def testOpenInto(self):
|
|
|
|
"""Test that we can resize a tree"""
|
|
|
|
fdt = Fdt.create_empty_tree(128)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(128, fdt.totalsize())
|
2018-06-06 23:37:04 +02:00
|
|
|
fdt.resize(256)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(256, fdt.totalsize())
|
2018-06-06 23:37:04 +02:00
|
|
|
fdt.pack()
|
|
|
|
self.assertTrue(fdt.totalsize() < 128)
|
|
|
|
|
2018-06-06 23:37:05 +02:00
|
|
|
def testSetProp(self):
|
|
|
|
"""Test that we can update and create properties"""
|
|
|
|
node = self.fdt.path_offset('/subnode@1')
|
2019-02-18 17:48:54 +01:00
|
|
|
self.fdt.setprop(node, 'compatible', TEST_BYTES_1)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(TEST_BYTES_1, self.fdt.getprop(node, 'compatible'))
|
2018-06-06 23:37:05 +02:00
|
|
|
|
|
|
|
# Check that this property is missing, and that we don't have space to
|
|
|
|
# add it
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(-libfdt.NOTFOUND,
|
2018-06-06 23:37:05 +02:00
|
|
|
self.fdt.getprop(node, 'missing', QUIET_NOTFOUND))
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(-libfdt.NOSPACE,
|
2019-02-18 17:48:54 +01:00
|
|
|
self.fdt.setprop(node, 'missing', TEST_BYTES_1,
|
2018-06-06 23:37:05 +02:00
|
|
|
quiet=(libfdt.NOSPACE,)))
|
|
|
|
|
|
|
|
# Expand the device tree so we now have room
|
|
|
|
self.fdt.resize(self.fdt.totalsize() + 50)
|
2019-02-18 17:48:54 +01:00
|
|
|
self.fdt.setprop(node, 'missing', TEST_BYTES_1)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(TEST_BYTES_1, self.fdt.getprop(node, 'missing'))
|
2018-06-06 23:37:05 +02:00
|
|
|
|
|
|
|
def testSetPropU32(self):
|
|
|
|
"""Test that we can update and create integer properties"""
|
|
|
|
node = 0
|
|
|
|
prop = 'prop-int'
|
|
|
|
self.fdt.setprop_u32(node, prop, TEST_VALUE_1)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(struct.pack('>I', TEST_VALUE_1),
|
2018-06-06 23:37:05 +02:00
|
|
|
self.fdt.getprop(node, prop))
|
|
|
|
|
|
|
|
def testSetPropU64(self):
|
|
|
|
"""Test that we can update and create integer properties"""
|
|
|
|
node = 0
|
|
|
|
prop = 'prop-int64'
|
|
|
|
self.fdt.setprop_u64(node, prop, TEST_VALUE64_1)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(struct.pack('>Q', TEST_VALUE64_1),
|
2018-06-06 23:37:05 +02:00
|
|
|
self.fdt.getprop(node, prop))
|
|
|
|
|
2018-06-14 05:44:43 +02:00
|
|
|
def testSetPropStr(self):
|
|
|
|
"""Test that we can set a property to a particular string"""
|
|
|
|
node = 0
|
|
|
|
prop = 'prop-str'
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(TEST_STRING_1, self.fdt.getprop(node, prop).as_str())
|
2018-06-14 05:44:43 +02:00
|
|
|
self.fdt.setprop_str(node, prop, TEST_STRING_2)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(TEST_STRING_2, self.fdt.getprop(node, prop).as_str())
|
2018-06-14 05:44:43 +02:00
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.fdt.getprop(node, 'prop-int').as_str()
|
|
|
|
self.assertIn('lacks nul termination', str(e.exception))
|
|
|
|
|
|
|
|
node2 = self.fdt.path_offset('/subnode@1/subsubnode')
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.fdt.getprop(node2, 'compatible').as_str()
|
|
|
|
self.assertIn('embedded nul', str(e.exception))
|
|
|
|
|
|
|
|
# Expand the device tree so we now have room
|
|
|
|
self.fdt.resize(self.fdt.totalsize() + 50)
|
|
|
|
prop = 'prop-unicode'
|
|
|
|
self.fdt.setprop_str(node, prop, TEST_STRING_3)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(TEST_STRING_3,
|
2018-06-14 05:44:43 +02:00
|
|
|
self.fdt.getprop(node, prop).as_str())
|
|
|
|
|
2018-06-14 05:44:44 +02:00
|
|
|
def testSetName(self):
|
|
|
|
"""Test that we can update a node name"""
|
|
|
|
node = self.fdt.path_offset('/subnode@1')
|
|
|
|
old_val = self.fdt.get_name(node)
|
|
|
|
self.fdt.set_name(node, 'test')
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual('test', self.fdt.get_name(node))
|
2018-06-14 05:44:44 +02:00
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.fdt.set_name(node, 'some\0name')
|
|
|
|
self.assertIn('embedded nul', str(e.exception))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
self.fdt.set_name(node, 'name\0')
|
|
|
|
self.assertIn('embedded nul', str(e.exception))
|
|
|
|
|
2018-09-11 11:28:39 +02:00
|
|
|
def testAddDeleteNodes(self):
|
|
|
|
"""Test that we can add and delete nodes"""
|
|
|
|
node_name = '/subnode@1'
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.GetSubnodes(node_name), ['subsubnode', 'ss1'])
|
2018-09-11 11:28:39 +02:00
|
|
|
node = self.fdt.path_offset('%s/subsubnode' % node_name)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.fdt.del_node(node, 'subsubnode'), 0)
|
|
|
|
self.assertEqual(self.GetSubnodes(node_name), ['ss1'])
|
2018-09-11 11:28:39 +02:00
|
|
|
|
|
|
|
node = self.fdt.path_offset(node_name)
|
|
|
|
offset = self.fdt.add_subnode(node, 'more')
|
|
|
|
self.assertTrue(offset > 0)
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(self.GetSubnodes(node_name), ['more', 'ss1'])
|
2018-09-11 11:28:39 +02:00
|
|
|
|
2018-06-06 23:37:03 +02:00
|
|
|
|
2018-07-10 22:49:07 +02:00
|
|
|
class PyLibfdtSwTests(unittest.TestCase):
|
|
|
|
"""Test class for pylibfdt sequential-write DT creation
|
|
|
|
"""
|
|
|
|
def assertOk(self, err_code):
|
2019-11-13 02:24:10 +01:00
|
|
|
self.assertEqual(0, err_code)
|
2018-07-10 22:49:07 +02:00
|
|
|
|
|
|
|
def testCreate(self):
|
|
|
|
# First check the minimum size and also the FdtSw() constructor
|
2019-11-13 02:24:10 +01:00
|
|
|
with self.assertRaisesRegex(FdtException, get_err(libfdt.NOSPACE)):
|
|
|
|
self.assertEqual(-libfdt.NOSPACE, FdtSw(3))
|
2018-07-10 22:49:07 +02:00
|
|
|
|
|
|
|
sw = FdtSw()
|
|
|
|
sw.add_reservemap_entry(TEST_ADDR_1, TEST_SIZE_1)
|
|
|
|
sw.add_reservemap_entry(TEST_ADDR_2, TEST_SIZE_2)
|
|
|
|
sw.finish_reservemap()
|
|
|
|
|
|
|
|
sw.begin_node('')
|
|
|
|
sw.property_string('compatible', 'test_tree1')
|
|
|
|
sw.property_u32('prop-int', TEST_VALUE_1)
|
|
|
|
|
|
|
|
sw.property_u32('prop-int', TEST_VALUE_1)
|
|
|
|
sw.property_u64('prop-int64', TEST_VALUE64_1)
|
|
|
|
sw.property_string('prop-str', TEST_STRING_1)
|
|
|
|
sw.property_u32('#address-cells', 1)
|
|
|
|
sw.property_u32('#size-cells', 0)
|
|
|
|
|
|
|
|
sw.begin_node('subnode@1')
|
|
|
|
sw.property_string('compatible', 'subnode1')
|
|
|
|
sw.property_u32('reg', 1)
|
|
|
|
sw.property_cell('prop-int', TEST_VALUE_1)
|
2019-10-25 03:02:26 +02:00
|
|
|
sw.property('data', b'\x00data\x01')
|
2018-07-10 22:49:07 +02:00
|
|
|
sw.begin_node('subsubnode')
|
2019-10-25 03:02:26 +02:00
|
|
|
sw.property('compatible', b'subsubnode1\0subsubnode')
|
2018-07-10 22:49:07 +02:00
|
|
|
sw.property_cell('prop-int', TEST_VALUE_1)
|
|
|
|
sw.end_node()
|
|
|
|
sw.begin_node('ss1')
|
|
|
|
sw.end_node()
|
|
|
|
sw.end_node()
|
|
|
|
|
|
|
|
for i in range(2, 11):
|
|
|
|
with sw.add_node('subnode@%d' % i):
|
|
|
|
sw.property_u32('reg', 2)
|
|
|
|
sw.property_cell('linux,phandle', PHANDLE_1)
|
|
|
|
sw.property_cell('prop-int', TEST_VALUE_2)
|
|
|
|
sw.property_u32('#address-cells', 1)
|
|
|
|
sw.property_u32('#size-cells', 0)
|
|
|
|
with sw.add_node('subsubnode@0'):
|
|
|
|
sw.property_u32('reg', 0)
|
|
|
|
sw.property_cell('phandle', PHANDLE_2)
|
2019-10-25 03:02:26 +02:00
|
|
|
sw.property('compatible', b'subsubnode2\0subsubnode')
|
2018-07-10 22:49:07 +02:00
|
|
|
sw.property_cell('prop-int', TEST_VALUE_2)
|
|
|
|
with sw.add_node('ss2'):
|
|
|
|
pass
|
|
|
|
sw.end_node()
|
|
|
|
|
|
|
|
fdt = sw.as_fdt()
|
|
|
|
self.assertEqual(2, fdt.num_mem_rsv())
|
|
|
|
self.assertEqual([TEST_ADDR_1, TEST_SIZE_1], fdt.get_mem_rsv(0))
|
|
|
|
|
|
|
|
# Make sure we can add a few more things
|
|
|
|
with sw.add_node('another'):
|
|
|
|
sw.property_u32('reg', 3)
|
|
|
|
|
|
|
|
# Make sure we can read from the tree too
|
|
|
|
node = sw.path_offset('/subnode@1')
|
2019-02-18 17:48:52 +01:00
|
|
|
self.assertEqual(b'subnode1\0', sw.getprop(node, 'compatible'))
|
2018-07-10 22:49:07 +02:00
|
|
|
|
|
|
|
# Make sure we did at least two resizes
|
|
|
|
self.assertTrue(len(fdt.as_bytearray()) > FdtSw.INC_SIZE * 2)
|
|
|
|
|
|
|
|
|
|
|
|
class PyLibfdtRoTests(unittest.TestCase):
|
|
|
|
"""Test class for read-only pylibfdt access functions
|
|
|
|
|
|
|
|
This just tests a few simple cases. Most of the tests are in
|
|
|
|
PyLibfdtBasicTests.
|
|
|
|
|
|
|
|
Properties:
|
|
|
|
fdt: Device tree file used for testing
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Read in the device tree we use for testing"""
|
2019-10-09 12:20:23 +02:00
|
|
|
with open('test_tree1.dtb', mode='rb') as f:
|
|
|
|
self.fdt = libfdt.FdtRo(f.read())
|
2018-07-10 22:49:07 +02:00
|
|
|
|
|
|
|
def testAccess(self):
|
|
|
|
"""Basic sanity check for the FdtRo class"""
|
|
|
|
node = self.fdt.path_offset('/subnode@1')
|
2019-02-18 17:48:52 +01:00
|
|
|
self.assertEqual(b'subnode1\0',
|
2018-07-10 22:49:07 +02:00
|
|
|
self.fdt.getprop(node, 'compatible'))
|
|
|
|
node = self.fdt.first_subnode(node)
|
2019-02-18 17:48:52 +01:00
|
|
|
self.assertEqual(b'this is a placeholder string\0string2\0',
|
2018-07-10 22:49:07 +02:00
|
|
|
self.fdt.getprop(node, 'placeholder'))
|
|
|
|
|
|
|
|
|
2017-03-17 23:14:31 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|