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