1 /*	$NetBSD: node_offset_by_compatible.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_node_offset_by_compatible()
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 #include <stdarg.h>
14 
15 #include <libfdt.h>
16 
17 #include "tests.h"
18 #include "testdata.h"
19 
check_search(void * fdt,const char * compat,...)20 static void check_search(void *fdt, const char *compat, ...)
21 {
22 	va_list ap;
23 	int offset = -1, target;
24 
25 	va_start(ap, compat);
26 	do {
27 		target = va_arg(ap, int);
28 		verbose_printf("Searching (target = %d): %d ->",
29 			       target, offset);
30 		offset = fdt_node_offset_by_compatible(fdt, offset, compat);
31 		verbose_printf("%d\n", offset);
32 
33 		if (offset != target)
34 			FAIL("fdt_node_offset_by_compatible(%s) returns %d "
35 			     "instead of %d", compat, offset, target);
36 	} while (target >= 0);
37 
38 	va_end(ap);
39 }
40 
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 	void *fdt;
44 	int subnode1_offset, subnode2_offset;
45 	int subsubnode1_offset, subsubnode2_offset;
46 
47 	test_init(argc, argv);
48 	fdt = load_blob_arg(argc, argv);
49 
50 	subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
51 	subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
52 	subsubnode1_offset = fdt_path_offset(fdt, "/subnode@1/subsubnode");
53 	subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode@0");
54 
55 	if ((subnode1_offset < 0) || (subnode2_offset < 0)
56 	    || (subsubnode1_offset < 0) || (subsubnode2_offset < 0))
57 		FAIL("Can't find required nodes");
58 
59 	check_search(fdt, "test_tree1", 0, -FDT_ERR_NOTFOUND);
60 	check_search(fdt, "subnode1", subnode1_offset, -FDT_ERR_NOTFOUND);
61 	check_search(fdt, "subsubnode1", subsubnode1_offset, -FDT_ERR_NOTFOUND);
62 	check_search(fdt, "subsubnode2", subsubnode2_offset, -FDT_ERR_NOTFOUND);
63 	/* Eek.. HACK to make this work whatever the order in the
64 	 * example tree */
65 	if (subsubnode1_offset < subsubnode2_offset)
66 		check_search(fdt, "subsubnode", subsubnode1_offset,
67 			     subsubnode2_offset, -FDT_ERR_NOTFOUND);
68 	else
69 		check_search(fdt, "subsubnode", subsubnode2_offset,
70 			     subsubnode1_offset, -FDT_ERR_NOTFOUND);
71 	check_search(fdt, "nothing-like-this", -FDT_ERR_NOTFOUND);
72 
73 	PASS();
74 }
75