1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <common.h>
26#include <fdtdec.h>
27#include <libfdt.h>
28#include <malloc.h>
29#include <os.h>
30
31
32#define FDT_SIZE (16 * 1024)
33
34
35
36
37
38
39
40
41
42static int fdt_checkerr(const char *oper_name, int err)
43{
44 if (err) {
45 printf("%s: %s: %s\n", __func__, oper_name, fdt_strerror(err));
46 return -1;
47 }
48
49 return 0;
50}
51
52
53
54
55
56
57
58
59
60
61static int checkval(const char *oper_name, int expected, int value)
62{
63 if (expected != value) {
64 printf("%s: %s: expected %d, but returned %d\n", __func__,
65 oper_name, expected, value);
66 return -1;
67 }
68
69 return 0;
70}
71
72#define CHECK(op) if (fdt_checkerr(#op, op)) return -1
73#define CHECKVAL(op, expected) \
74 if (checkval(#op, expected, op)) \
75 return -1
76#define CHECKOK(op) CHECKVAL(op, 0)
77
78
79#define MAX_NODES 20
80
81
82
83
84
85
86
87
88
89
90
91
92
93static int make_fdt(void *fdt, int size, const char *aliases,
94 const char *nodes)
95{
96 char name[20], value[20];
97 const char *s;
98 int fd;
99
100 CHECK(fdt_create(fdt, size));
101 CHECK(fdt_finish_reservemap(fdt));
102 CHECK(fdt_begin_node(fdt, ""));
103
104 CHECK(fdt_begin_node(fdt, "aliases"));
105 for (s = aliases; *s;) {
106 sprintf(name, "i2c%c", *s);
107 sprintf(value, "/i2c%d@0", s[1] - 'a');
108 CHECK(fdt_property_string(fdt, name, value));
109 s += 2 + (s[2] != '\0');
110 }
111 CHECK(fdt_end_node(fdt));
112
113 for (s = nodes; *s; s++) {
114 sprintf(value, "i2c%d@0", (*s & 0xdf) - 'A');
115 CHECK(fdt_begin_node(fdt, value));
116 CHECK(fdt_property_string(fdt, "compatible",
117 fdtdec_get_compatible(COMPAT_UNKNOWN)));
118 if (*s <= 'Z')
119 CHECK(fdt_property_string(fdt, "status", "disabled"));
120 CHECK(fdt_end_node(fdt));
121 }
122
123 CHECK(fdt_end_node(fdt));
124 CHECK(fdt_finish(fdt));
125 CHECK(fdt_pack(fdt));
126#if defined(DEBUG) && defined(CONFIG_SANDBOX)
127 fd = os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT | OS_O_WRONLY);
128 if (fd == -1) {
129 printf("Could not open .dtb file to write\n");
130 return -1;
131 }
132 os_write(fd, fdt, size);
133 os_close(fd);
134#endif
135 return 0;
136}
137
138static int run_test(const char *aliases, const char *nodes, const char *expect)
139{
140 int list[MAX_NODES];
141 const char *s;
142 void *blob;
143 int i;
144
145 blob = malloc(FDT_SIZE);
146 if (!blob) {
147 printf("%s: out of memory\n", __func__);
148 return 1;
149 }
150
151 printf("aliases=%s, nodes=%s, expect=%s: ", aliases, nodes, expect);
152 CHECKVAL(make_fdt(blob, FDT_SIZE, aliases, nodes), 0);
153 CHECKVAL(fdtdec_find_aliases_for_id(blob, "i2c",
154 COMPAT_UNKNOWN,
155 list, ARRAY_SIZE(list)), strlen(expect));
156
157
158 for (i = 0, s = expect; *s; s++, i++) {
159 int want = *s;
160 const char *name;
161 int got = ' ';
162
163 name = list[i] ? fdt_get_name(blob, list[i], NULL) : NULL;
164 if (name)
165 got = name[3] + 'a' - '0';
166
167 if (got != want) {
168 printf("Position %d: Expected '%c', got '%c' ('%s')\n",
169 i, want, got, name);
170 return 1;
171 }
172 }
173
174 printf("pass\n");
175 return 0;
176}
177
178static int do_test_fdtdec(cmd_tbl_t *cmdtp, int flag, int argc,
179 char * const argv[])
180{
181
182 CHECKOK(run_test("", "", ""));
183 CHECKOK(run_test("1e 3d", "", ""));
184
185
186
187
188
189
190
191
192
193
194 CHECKOK(run_test("0a 1b", "ab", "ab"));
195
196 CHECKOK(run_test("0a 1c", "ab", "ab"));
197 CHECKOK(run_test("1c", "ab", "ab"));
198 CHECKOK(run_test("1b", "ab", "ab"));
199 CHECKOK(run_test("0b", "ab", "ba"));
200 CHECKOK(run_test("0b 2d", "dbc", "bcd"));
201 CHECKOK(run_test("0d 3a 1c 2b", "dbac", "dcba"));
202
203
204 CHECKOK(run_test("1b 3d", "dbc", "cb d"));
205 CHECKOK(run_test("1e 3d", "dbc", "bc d"));
206
207
208 CHECKOK(run_test("", "dbac", "dbac"));
209
210
211 CHECKOK(run_test("0d 3a 1c 2b", "dBac", "dc a"));
212 CHECKOK(run_test("0b 2d", "DBc", "c"));
213 CHECKOK(run_test("0b 4d 2c", "DBc", " c"));
214
215
216 CHECKOK(run_test("2a 1a 0a", "a", " a"));
217 CHECKOK(run_test("0a 1a 2a", "a", "a"));
218
219 printf("Test passed\n");
220 return 0;
221}
222
223U_BOOT_CMD(
224 test_fdtdec, 3, 1, do_test_fdtdec,
225 "test_fdtdec",
226 "Run tests for fdtdec library");
227