1 /* $NetBSD: node_offset_by_prop_value.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_path_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 #include <stdarg.h>
14
15 #include <libfdt.h>
16
17 #include "tests.h"
18 #include "testdata.h"
19
vcheck_search(void * fdt,const char * propname,const void * propval,int proplen,va_list ap)20 static void vcheck_search(void *fdt, const char *propname, const void *propval,
21 int proplen, va_list ap)
22 {
23 int offset = -1, target;
24
25 do {
26 target = va_arg(ap, int);
27 verbose_printf("Searching (target = %d): %d ->",
28 target, offset);
29 offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
30 propval, proplen);
31 verbose_printf("%d\n", offset);
32
33 if (offset != target)
34 FAIL("fdt_node_offset_by_prop_value() returns %d "
35 "instead of %d", offset, target);
36 } while (target >= 0);
37 }
38
check_search(void * fdt,const char * propname,const void * propval,int proplen,...)39 static void check_search(void *fdt, const char *propname, const void *propval,
40 int proplen, ...)
41 {
42 va_list ap;
43
44 va_start(ap, proplen);
45 vcheck_search(fdt, propname, propval, proplen, ap);
46 va_end(ap);
47 }
48
check_search_str(void * fdt,const char * propname,const char * propval,...)49 static void check_search_str(void *fdt, const char *propname,
50 const char *propval, ...)
51 {
52 va_list ap;
53
54 va_start(ap, propval);
55 vcheck_search(fdt, propname, propval, strlen(propval)+1, ap);
56 va_end(ap);
57 }
58
59 #define check_search_cell(fdt, propname, propval, ...) \
60 { \
61 fdt32_t val = cpu_to_fdt32(propval); \
62 check_search((fdt), (propname), &val, sizeof(val), \
63 ##__VA_ARGS__); \
64 }
65
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 void *fdt;
69 int subnode1_offset, subnode2_offset;
70 int subsubnode1_offset, subsubnode2_offset;
71
72 test_init(argc, argv);
73 fdt = load_blob_arg(argc, argv);
74
75 subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
76 subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
77 subsubnode1_offset = fdt_path_offset(fdt, "/subnode@1/subsubnode");
78 subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode@0");
79
80 if ((subnode1_offset < 0) || (subnode2_offset < 0)
81 || (subsubnode1_offset < 0) || (subsubnode2_offset < 0))
82 FAIL("Can't find required nodes");
83
84 check_search_cell(fdt, "prop-int", TEST_VALUE_1, 0, subnode1_offset,
85 subsubnode1_offset, -FDT_ERR_NOTFOUND);
86
87 check_search_cell(fdt, "prop-int", TEST_VALUE_2, subnode2_offset,
88 subsubnode2_offset, -FDT_ERR_NOTFOUND);
89
90 check_search_str(fdt, "prop-str", TEST_STRING_1, 0, -FDT_ERR_NOTFOUND);
91
92 check_search_str(fdt, "prop-str", "no such string", -FDT_ERR_NOTFOUND);
93
94 check_search_cell(fdt, "prop-int", TEST_VALUE_1+1, -FDT_ERR_NOTFOUND);
95
96 check_search(fdt, "no-such-prop", NULL, 0, -FDT_ERR_NOTFOUND);
97
98 PASS();
99 }
100