xref: /netbsd-src/external/gpl2/dtc/dist/tests/subnode_offset.c (revision cc7d2833ecf67da5a5ddc470841931eb9f6723e4)
1 /*	$NetBSD: subnode_offset.c,v 1.1.1.3 2019/12/22 12:34:06 skrll Exp $	*/
2 
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 /*
5  * libfdt - Flat Device Tree manipulation
6  *	Testcase for fdt_subnode_offset()
7  * Copyright (C) 2006 David Gibson, IBM Corporation.
8  */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdint.h>
13 
14 #include <libfdt.h>
15 
16 #include "tests.h"
17 #include "testdata.h"
18 
check_subnode(struct fdt_header * fdt,int parent,const char * name)19 static int check_subnode(struct fdt_header *fdt, int parent, const char *name)
20 {
21 	int offset;
22 	const struct fdt_node_header *nh;
23 	uint32_t tag;
24 
25 	verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
26 	offset = fdt_subnode_offset(fdt, parent, name);
27 	verbose_printf("offset %d...", offset);
28 	if (offset < 0)
29 		FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
30 	nh = fdt_offset_ptr(fdt, offset, sizeof(*nh));
31 	verbose_printf("pointer %p\n", nh);
32 	if (! nh)
33 		FAIL("NULL retrieving subnode \"%s\"", name);
34 
35 	tag = fdt32_to_cpu(nh->tag);
36 
37 	if (tag != FDT_BEGIN_NODE)
38 		FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
39 	if (!nodename_eq(nh->name, name))
40 		FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
41 		     nh->name, name);
42 
43 	return offset;
44 }
45 
main(int argc,char * argv[])46 int main(int argc, char *argv[])
47 {
48 	void *fdt;
49 	int subnode1_offset, subnode2_offset;
50 	int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
51 	int ss12_off, ss21_off;
52 
53 	test_init(argc, argv);
54 	fdt = load_blob_arg(argc, argv);
55 
56 	subnode1_offset = check_subnode(fdt, 0, "subnode@1");
57 	subnode2_offset = check_subnode(fdt, 0, "subnode@2");
58 
59 	if (subnode1_offset == subnode2_offset)
60 		FAIL("Different subnodes have same offset");
61 
62 	check_property_cell(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
63 	check_property_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
64 
65 	subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
66 	subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0");
67 	subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode");
68 
69 	check_property_cell(fdt, subsubnode1_offset, "prop-int", TEST_VALUE_1);
70 	check_property_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
71 	check_property_cell(fdt, subsubnode2_offset2, "prop-int", TEST_VALUE_2);
72 
73 	if (subsubnode2_offset != subsubnode2_offset2)
74 		FAIL("Different offsets with and without unit address");
75 
76 	check_subnode(fdt, subnode1_offset, "ss1");
77 	ss21_off = fdt_subnode_offset(fdt, subnode2_offset, "ss1");
78 	if (ss21_off != -FDT_ERR_NOTFOUND)
79 		FAIL("Incorrectly found ss1 in subnode2");
80 
81 	ss12_off = fdt_subnode_offset(fdt, subnode1_offset, "ss2");
82 	if (ss12_off != -FDT_ERR_NOTFOUND)
83 		FAIL("Incorrectly found ss2 in subnode1");
84 	check_subnode(fdt, subnode2_offset, "ss2");
85 
86 	PASS();
87 }
88