1 /*
2  * libfdt - Flat Device Tree manipulation
3  *	Testcase for fdt_node_offset_by_compatible()
4  * Copyright (C) 2006 David Gibson, IBM Corporation.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdint.h>
24 #include <stdarg.h>
25 
26 #include <libfdt.h>
27 
28 #include "tests.h"
29 #include "testdata.h"
30 
31 static void check_search(void *fdt, const char *compat, ...)
32 {
33 	va_list ap;
34 	int offset = -1, target;
35 
36 	va_start(ap, compat);
37 	do {
38 		target = va_arg(ap, int);
39 		verbose_printf("Searching (target = %d): %d ->",
40 			       target, offset);
41 		offset = fdt_node_offset_by_compatible(fdt, offset, compat);
42 		verbose_printf("%d\n", offset);
43 
44 		if (offset != target)
45 			FAIL("fdt_node_offset_by_compatible(%s) returns %d "
46 			     "instead of %d", compat, offset, target);
47 	} while (target >= 0);
48 
49 	va_end(ap);
50 }
51 
52 int main(int argc, char *argv[])
53 {
54 	void *fdt;
55 	int subnode1_offset, subnode2_offset;
56 	int subsubnode1_offset, subsubnode2_offset;
57 
58 	test_init(argc, argv);
59 	fdt = load_blob_arg(argc, argv);
60 
61 	subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
62 	subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
63 	subsubnode1_offset = fdt_path_offset(fdt, "/subnode@1/subsubnode");
64 	subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode@0");
65 
66 	if ((subnode1_offset < 0) || (subnode2_offset < 0)
67 	    || (subsubnode1_offset < 0) || (subsubnode2_offset < 0))
68 		FAIL("Can't find required nodes");
69 
70 	check_search(fdt, "test_tree1", 0, -FDT_ERR_NOTFOUND);
71 	check_search(fdt, "subnode1", subnode1_offset, -FDT_ERR_NOTFOUND);
72 	check_search(fdt, "subsubnode1", subsubnode1_offset, -FDT_ERR_NOTFOUND);
73 	check_search(fdt, "subsubnode2", subsubnode2_offset, -FDT_ERR_NOTFOUND);
74 	/* Eek.. HACK to make this work whatever the order in the
75 	 * example tree */
76 	if (subsubnode1_offset < subsubnode2_offset)
77 		check_search(fdt, "subsubnode", subsubnode1_offset,
78 			     subsubnode2_offset, -FDT_ERR_NOTFOUND);
79 	else
80 		check_search(fdt, "subsubnode", subsubnode2_offset,
81 			     subsubnode1_offset, -FDT_ERR_NOTFOUND);
82 	check_search(fdt, "nothing-like-this", -FDT_ERR_NOTFOUND);
83 
84 	PASS();
85 }
86