1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Testcase for fdt_node_offset_by_compatible()
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdint.h>
11 #include <stdarg.h>
12
13 #include <libfdt.h>
14
15 #include "tests.h"
16 #include "testdata.h"
17
check_search(void * fdt,const char * compat,...)18 static void check_search(void *fdt, const char *compat, ...)
19 {
20 va_list ap;
21 int offset = -1, target;
22
23 va_start(ap, compat);
24 do {
25 target = va_arg(ap, int);
26 verbose_printf("Searching (target = %d): %d ->",
27 target, offset);
28 offset = fdt_node_offset_by_compatible(fdt, offset, compat);
29 verbose_printf("%d\n", offset);
30
31 if (offset != target)
32 FAIL("fdt_node_offset_by_compatible(%s) returns %d "
33 "instead of %d", compat, offset, target);
34 } while (target >= 0);
35
36 va_end(ap);
37 }
38
main(int argc,char * argv[])39 int main(int argc, char *argv[])
40 {
41 void *fdt;
42 int subnode1_offset, subnode2_offset;
43 int subsubnode1_offset, subsubnode2_offset;
44
45 test_init(argc, argv);
46 fdt = load_blob_arg(argc, argv);
47
48 subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
49 subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
50 subsubnode1_offset = fdt_path_offset(fdt, "/subnode@1/subsubnode");
51 subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode@0");
52
53 if ((subnode1_offset < 0) || (subnode2_offset < 0)
54 || (subsubnode1_offset < 0) || (subsubnode2_offset < 0))
55 FAIL("Can't find required nodes");
56
57 check_search(fdt, "test_tree1", 0, -FDT_ERR_NOTFOUND);
58 check_search(fdt, "subnode1", subnode1_offset, -FDT_ERR_NOTFOUND);
59 check_search(fdt, "subsubnode1", subsubnode1_offset, -FDT_ERR_NOTFOUND);
60 check_search(fdt, "subsubnode2", subsubnode2_offset, -FDT_ERR_NOTFOUND);
61 /* Eek.. HACK to make this work whatever the order in the
62 * example tree */
63 if (subsubnode1_offset < subsubnode2_offset)
64 check_search(fdt, "subsubnode", subsubnode1_offset,
65 subsubnode2_offset, -FDT_ERR_NOTFOUND);
66 else
67 check_search(fdt, "subsubnode", subsubnode2_offset,
68 subsubnode1_offset, -FDT_ERR_NOTFOUND);
69 check_search(fdt, "nothing-like-this", -FDT_ERR_NOTFOUND);
70
71 PASS();
72 }
73