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