def setUp(self): self.node_a = fdt.Node('/') self.node_a.append(fdt.Property('prop')) self.node_a.append(fdt.PropStrings('prop_str', 'test', 'test')) self.node_a.append(fdt.PropWords('prop_word', 0x11111111, 0x55555555)) self.node_a.append(fdt.PropBytes('prop_byte', [0x10, 0x50])) self.node_a.append(fdt.Node('sub_node'))
def test_merge(self): root_node = self.node_a.copy() root_node.append(fdt.Node('node_a', props=[fdt.Property('prop_a')])) root_node.append(fdt.Node('node_b', props=[fdt.Property('prop_b')])) root_node.append(fdt.Node('node_c', props=[fdt.Property('prop_c')])) node = self.node_a.copy() node.set_name("test_node") root_node.merge(node) self.assertNotEqual(root_node, node)
def test_merge(self): root_node = copy.deepcopy(self.node_a) root_node.append(fdt.Node('node_a', [fdt.Property('prop_a')]), 'sub_node') root_node.append(fdt.Node('node_b', [fdt.Property('prop_b')]), 'sub_node/node_a') root_node.append(fdt.Node('node_c', [fdt.Property('prop_c')]), 'sub_node/node_a/node_b') node = copy.deepcopy(self.node_a) node.name = "test_node" root_node.merge(node) self.assertNotEqual(root_node, node)
def test_append(self): root_node = self.node_a.copy() root_node.append(fdt.Node('node_a', props=[fdt.Property('prop_a')])) root_node.append(fdt.Node('node_b', props=[fdt.Property('prop_b')])) root_node.append(fdt.Node('node_c', props=[fdt.Property('prop_c')])) node = root_node.get_subnode('node_a') self.assertIsInstance(node, fdt.Node) prop = node.get_property('prop_a') self.assertIsInstance(prop, fdt.Property) node.remove_property('prop_a') prop = node.get_property('prop_a') self.assertIsNone(prop, fdt.Property)
def test_append(self): root_node = copy.deepcopy(self.node_a) root_node.append(fdt.Node('node_a', [fdt.Property('prop_a')]), 'sub_node') root_node.append(fdt.Node('node_b', [fdt.Property('prop_b')]), 'sub_node/node_a') root_node.append(fdt.Node('node_c', [fdt.Property('prop_c')]), 'sub_node/node_a/node_b') node = root_node.get_subnode('sub_node/node_a/node_b/node_c') self.assertIsInstance(node, fdt.Node) prop = root_node.get_property('sub_node/node_a/node_b/node_c/prop_c') self.assertIsInstance(prop, fdt.Property) root_node.remove_property('sub_node/node_a/node_b/node_c/prop_c') prop = root_node.get_property('sub_node/node_a/node_b/node_c/prop_c') self.assertIsNone(prop, fdt.Property)
def test_node_set_property(): root_node = fdt.Node('/') # add properties into node by set_property method root_node.set_property('prop', None) root_node.set_property('int_prop', 1000) root_node.set_property('list_int_prop', [1, 2]) root_node.set_property('str_prop', 'value') root_node.set_property('list_str_prop', ['value1', 'value2']) root_node.set_property('bytes_prop', b'\x00\x55\x66') # validate types and values assert len(root_node.props) == 6 assert isinstance(root_node.get_property('prop'), fdt.Property) assert isinstance(root_node.get_property('int_prop'), fdt.PropWords) assert root_node.get_property('int_prop').value == 1000 assert isinstance(root_node.get_property('list_int_prop'), fdt.PropWords) assert root_node.get_property('list_int_prop').data == [1, 2] assert isinstance(root_node.get_property('str_prop'), fdt.PropStrings) assert root_node.get_property('str_prop').value == 'value' assert isinstance(root_node.get_property('list_str_prop'), fdt.PropStrings) assert root_node.get_property('list_str_prop').data == ['value1', 'value2'] assert isinstance(root_node.get_property('bytes_prop'), fdt.PropBytes) assert root_node.get_property('bytes_prop').data == b'\x00\x55\x66' # update property in node by set_property method root_node.set_property('list_int_prop', [1, 2, 3]) # validate property value assert root_node.get_property('list_int_prop').data == [1, 2, 3]
def test_01(data_dir): with open(os.path.join(data_dir, "addresses.dts")) as f: data = f.read() fdt_obj = fdt.parse_dts(data) assert fdt_obj.get_property('compatible').value == "test_addresses" assert fdt_obj.get_property('#address-cells').value == 2 assert fdt_obj.get_property('#size-cells').value == 2 assert fdt_obj.get_node('identity-bus@0') == fdt.Node('identity-bus@0') assert fdt_obj.get_node('simple-bus@1000000') == fdt.Node('simple-bus@1000000', fdt.PropWords('#address-cells', 2), fdt.PropWords('#size-cells', 1)) with pytest.raises(Exception): _ = fdt_obj.to_dtb() data = fdt_obj.to_dtb(17) assert isinstance(data, bytes) assert len(data) == 254
def to_its(self, rpath=None, tabsize=4): """ Export to ITS format :param rpath: :param tabsize: :return: """ data = {} images = fdt.Node("images") for img in self.img_info: img_name = img.name.replace('@', '_') + '.bin' img_clone = img.copy() img_clone.append(fdt.PropIncBin("data", None, img_name, rpath)) images.append(img_clone) data[img_name] = self.img_data[img.name] # Check default config if self.def_config is None: raise Exception("Default config not defined") if self.def_config not in [cnf.name for cnf in self.configs]: raise Exception("Default config \"{}\" doesn't exist".format( self.def_config)) # Add images and configs root_node = fdt.Node('/') root_node.append(fdt.PropStrings("description", self.description)) root_node.append(images) configs = fdt.Node("configurations", nodes=self.configs) configs.append(fdt.PropStrings("default", self.def_config)) root_node.append(configs) # Crete ITS its = "/dts-v1/;\n" its += '\n' its += root_node.to_dts(tabsize) return its, data
def test_fdt_constructor(): fdt_obj = fdt.FDT() assert isinstance(fdt_obj, fdt.FDT) assert fdt_obj.root == fdt.Node('/')
def test_node(): # create node object node = fdt.Node('/') node.append(fdt.Property('prop')) node.append(fdt.PropStrings('prop_str', 'test', 'test')) node.append(fdt.PropWords('prop_word', 0x11111111, 0x55555555)) node.append(fdt.PropBytes('prop_byte', [0x10, 0x50])) subnode0 = fdt.Node('subnode0') subnode0.append(fdt.Property('prop0')) subnode0.append(fdt.PropStrings('prop_str0', 'test')) subnode1 = fdt.Node('subnode1') subnode1.append(fdt.Property('prop1')) subnode1.append(fdt.PropWords('prop_word1', 0x11111111)) subnode0.append(subnode1) node.append(subnode0) assert isinstance(node, fdt.Node) assert node.name == '/' assert len(node.props) == 4 assert len(node.nodes) == 1 # Use only node constructor new_node = fdt.Node('/', fdt.Property('prop'), fdt.PropStrings('prop_str', 'test', 'test'), fdt.PropWords('prop_word', 0x11111111, 0x55555555), fdt.PropBytes('prop_byte', [0x10, 0x50]), fdt.Node('subnode0', fdt.Property('prop0'), fdt.PropStrings('prop_str0', 'test'), fdt.Node('subnode1', fdt.Property('prop1'), fdt.PropWords('prop_word1', 0x11111111) ) ) ) assert node == new_node with pytest.raises(AssertionError): node.append('test') with pytest.raises(Exception): node.append(256) with pytest.raises(Exception): node.append(fdt.Property('prop')) copy_node = node.copy() assert copy_node == node copy_node.set_property('prop_word', [0x10, 0x50]) assert copy_node != node # export to dts str_data = node.to_dts() out = "/ {\n" out += " prop;\n" out += " prop_str = \"test\", \"test\";\n" out += " prop_word = <0x11111111 0x55555555>;\n" out += " prop_byte = [10 50];\n" out += " subnode0 {\n" out += " prop0;\n" out += " prop_str0 = \"test\";\n" out += " subnode1 {\n" out += " prop1;\n" out += " prop_word1 = <0x11111111>;\n" out += " };\n" out += " };\n" out += "};\n" assert str_data == out
def to_itb(self, padding=0, align=None, size=None): """ Export to ITB format :param padding: :param align: :param size: :return: """ img_blob = bytes() img_offset = padding fdt_obj = fdt.FDT() fdt_obj.add_item( fdt.PropWords( "timestamp", int(time.time()) if self.time_stamp is None else self.time_stamp)) fdt_obj.add_item(fdt.PropStrings("description", self.description)) # Add images node = fdt.Node("images") for image in self.img_info: if image.name not in self.img_data: raise Exception("export: data is None") cimg = image.copy() data = self.img_data[image.name] if padding: img_blob += data img_offset += len(data) cimg.append(fdt.PropWords("data-size", len(data))) cimg.append(fdt.PropWords("data-position", img_offset)) else: cimg.append(fdt.PropBytes("data", data=data)) node.append(cimg) fdt_obj.add_item(node) # Check default config if self.def_config is None: raise Exception("Default config not defined") if self.def_config not in [cnf.name for cnf in self.configs]: raise Exception("Default config \"{}\" doesn't exist".format( self.def_config)) # Add configs node = fdt.Node("configurations") node.append(fdt.PropStrings("default", self.def_config)) for cfg in self.configs: node.append(cfg) fdt_obj.add_item(node) # Generate FDT blob itb = fdt_obj.to_dtb(17) # ... if padding: itb_align = padding - len(itb) if itb_align < 0: raise Exception() if itb_align > 0: itb += bytes([0] * itb_align) itb += img_blob return itb