blob: f6defe16c5a694a9af4e5e476aa550edf4ab6225 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass88941952012-01-17 08:20:51 +00002/*
3 * Some very basic tests for fdtdec, accessed through test_fdtdec command.
4 * They are easiest to use with sandbox.
5 *
6 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass88941952012-01-17 08:20:51 +00007 */
8
9#include <common.h>
10#include <fdtdec.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Simon Glass88941952012-01-17 08:20:51 +000012#include <malloc.h>
13#include <os.h>
14
15/* The size of our test fdt blob */
16#define FDT_SIZE (16 * 1024)
17
Thierry Reding3a28cc12019-03-21 19:10:06 +010018#define CHECK(op) ({ \
19 int err = op; \
20 if (err < 0) { \
21 printf("%s: %s: %s\n", __func__, #op, \
22 fdt_strerror(err)); \
23 return err; \
24 } \
25 \
26 err; \
27 })
Simon Glass88941952012-01-17 08:20:51 +000028
Thierry Reding3a28cc12019-03-21 19:10:06 +010029#define CHECKVAL(op, expected) ({ \
30 int err = op; \
31 if (err != expected) { \
32 printf("%s: %s: expected %d, but returned %d\n",\
33 __func__, #op, expected, err); \
34 return err; \
35 } \
36 \
37 err; \
38 })
Simon Glass88941952012-01-17 08:20:51 +000039
Simon Glass88941952012-01-17 08:20:51 +000040#define CHECKOK(op) CHECKVAL(op, 0)
41
42/* maximum number of nodes / aliases to generate */
43#define MAX_NODES 20
44
45/*
46 * Make a test fdt
47 *
48 * @param fdt Device tree pointer
49 * @param size Size of device tree blob
50 * @param aliases Specifies alias assignments. Format is a list of items
51 * separated by space. Items are #a where
52 * # is the alias number
53 * a is the node to point to
54 * @param nodes Specifies nodes to generate (a=0, b=1), upper case
55 * means to create a disabled node
56 */
57static int make_fdt(void *fdt, int size, const char *aliases,
58 const char *nodes)
59{
60 char name[20], value[20];
61 const char *s;
Thierry Redinge957b462019-03-21 19:10:05 +010062#if defined(DEBUG) && defined(CONFIG_SANDBOX)
Simon Glass88941952012-01-17 08:20:51 +000063 int fd;
Thierry Redinge957b462019-03-21 19:10:05 +010064#endif
Simon Glass88941952012-01-17 08:20:51 +000065
66 CHECK(fdt_create(fdt, size));
67 CHECK(fdt_finish_reservemap(fdt));
68 CHECK(fdt_begin_node(fdt, ""));
69
70 CHECK(fdt_begin_node(fdt, "aliases"));
71 for (s = aliases; *s;) {
72 sprintf(name, "i2c%c", *s);
73 sprintf(value, "/i2c%d@0", s[1] - 'a');
74 CHECK(fdt_property_string(fdt, name, value));
75 s += 2 + (s[2] != '\0');
76 }
77 CHECK(fdt_end_node(fdt));
78
79 for (s = nodes; *s; s++) {
80 sprintf(value, "i2c%d@0", (*s & 0xdf) - 'A');
81 CHECK(fdt_begin_node(fdt, value));
82 CHECK(fdt_property_string(fdt, "compatible",
83 fdtdec_get_compatible(COMPAT_UNKNOWN)));
84 if (*s <= 'Z')
85 CHECK(fdt_property_string(fdt, "status", "disabled"));
86 CHECK(fdt_end_node(fdt));
87 }
88
89 CHECK(fdt_end_node(fdt));
90 CHECK(fdt_finish(fdt));
91 CHECK(fdt_pack(fdt));
92#if defined(DEBUG) && defined(CONFIG_SANDBOX)
93 fd = os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT | OS_O_WRONLY);
94 if (fd == -1) {
95 printf("Could not open .dtb file to write\n");
96 return -1;
97 }
98 os_write(fd, fdt, size);
99 os_close(fd);
100#endif
101 return 0;
102}
103
104static int run_test(const char *aliases, const char *nodes, const char *expect)
105{
106 int list[MAX_NODES];
107 const char *s;
108 void *blob;
109 int i;
110
111 blob = malloc(FDT_SIZE);
112 if (!blob) {
113 printf("%s: out of memory\n", __func__);
114 return 1;
115 }
116
117 printf("aliases=%s, nodes=%s, expect=%s: ", aliases, nodes, expect);
118 CHECKVAL(make_fdt(blob, FDT_SIZE, aliases, nodes), 0);
119 CHECKVAL(fdtdec_find_aliases_for_id(blob, "i2c",
120 COMPAT_UNKNOWN,
Thierry Reding3a28cc12019-03-21 19:10:06 +0100121 list, ARRAY_SIZE(list)), (int)strlen(expect));
Simon Glass88941952012-01-17 08:20:51 +0000122
123 /* Check we got the right ones */
124 for (i = 0, s = expect; *s; s++, i++) {
125 int want = *s;
126 const char *name;
127 int got = ' ';
128
129 name = list[i] ? fdt_get_name(blob, list[i], NULL) : NULL;
130 if (name)
131 got = name[3] + 'a' - '0';
132
133 if (got != want) {
134 printf("Position %d: Expected '%c', got '%c' ('%s')\n",
135 i, want, got, name);
136 return 1;
137 }
138 }
139
140 printf("pass\n");
141 return 0;
142}
143
Thierry Redingea89acb2019-03-21 19:10:07 +0100144static int make_fdt_carveout_device(void *fdt, uint32_t na, uint32_t ns)
145{
146 const char *basename = "/display";
147 struct fdt_memory carveout = {
148#ifdef CONFIG_PHYS_64BIT
149 .start = 0x180000000,
150 .end = 0x18fffffff,
151#else
152 .start = 0x80000000,
153 .end = 0x8fffffff,
154#endif
155 };
156 fdt32_t cells[4], *ptr = cells;
157 uint32_t upper, lower;
158 char name[32];
159 int offset;
160
161 /* store one or two address cells */
162 lower = fdt_addr_unpack(carveout.start, &upper);
163
164 if (na > 1 && upper > 0)
165 snprintf(name, sizeof(name), "%s@%x,%x", basename, upper,
166 lower);
167 else
168 snprintf(name, sizeof(name), "%s@%x", basename, lower);
169
170 if (na > 1)
171 *ptr++ = cpu_to_fdt32(upper);
172
173 *ptr++ = cpu_to_fdt32(lower);
174
175 /* store one or two size cells */
176 lower = fdt_size_unpack(carveout.end - carveout.start + 1, &upper);
177
178 if (ns > 1)
179 *ptr++ = cpu_to_fdt32(upper);
180
181 *ptr++ = cpu_to_fdt32(lower);
182
183 offset = CHECK(fdt_add_subnode(fdt, 0, name + 1));
184 CHECK(fdt_setprop(fdt, offset, "reg", cells, (na + ns) * sizeof(*cells)));
185
186 return fdtdec_set_carveout(fdt, name, "memory-region", 0,
187 "framebuffer", &carveout);
188}
189
190static int check_fdt_carveout(void *fdt, uint32_t address_cells,
191 uint32_t size_cells)
192{
193#ifdef CONFIG_PHYS_64BIT
194 const char *name = "/display@1,80000000";
195 const struct fdt_memory expected = {
196 .start = 0x180000000,
197 .end = 0x18fffffff,
198 };
199#else
200 const char *name = "/display@80000000";
201 const struct fdt_memory expected = {
202 .start = 0x80000000,
203 .end = 0x8fffffff,
204 };
205#endif
206 struct fdt_memory carveout;
207
208 printf("carveout: %pap-%pap na=%u ns=%u: ", &expected.start,
209 &expected.end, address_cells, size_cells);
210
211 CHECK(fdtdec_get_carveout(fdt, name, "memory-region", 0, &carveout));
212
213 if ((carveout.start != expected.start) ||
214 (carveout.end != expected.end)) {
215 printf("carveout: %pap-%pap, expected %pap-%pap\n",
216 &carveout.start, &carveout.end,
217 &expected.start, &expected.end);
218 return 1;
219 }
220
221 printf("pass\n");
222 return 0;
223}
224
225static int make_fdt_carveout(void *fdt, int size, uint32_t address_cells,
226 uint32_t size_cells)
227{
228 fdt32_t na = cpu_to_fdt32(address_cells);
229 fdt32_t ns = cpu_to_fdt32(size_cells);
230#if defined(DEBUG) && defined(CONFIG_SANDBOX)
231 char filename[512];
232 int fd;
233#endif
234 int err;
235
236 CHECK(fdt_create(fdt, size));
237 CHECK(fdt_finish_reservemap(fdt));
238 CHECK(fdt_begin_node(fdt, ""));
239 CHECK(fdt_property(fdt, "#address-cells", &na, sizeof(na)));
240 CHECK(fdt_property(fdt, "#size-cells", &ns, sizeof(ns)));
241 CHECK(fdt_end_node(fdt));
242 CHECK(fdt_finish(fdt));
243 CHECK(fdt_pack(fdt));
244
245 CHECK(fdt_open_into(fdt, fdt, FDT_SIZE));
246
247 err = make_fdt_carveout_device(fdt, address_cells, size_cells);
248
249#if defined(DEBUG) && defined(CONFIG_SANDBOX)
250 snprintf(filename, sizeof(filename), "/tmp/fdtdec-carveout-%u-%u.dtb",
251 address_cells, size_cells);
252
253 fd = os_open(filename, OS_O_CREAT | OS_O_WRONLY);
254 if (fd < 0) {
255 printf("could not open .dtb file to write\n");
256 goto out;
257 }
258
259 os_write(fd, fdt, size);
260 os_close(fd);
261
262out:
263#endif
264 return err;
265}
266
267static int check_carveout(void)
268{
269 void *fdt;
270
271 fdt = malloc(FDT_SIZE);
272 if (!fdt) {
273 printf("%s: out of memory\n", __func__);
274 return 1;
275 }
276
277#ifndef CONFIG_PHYS_64BIT
278 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 1), 0);
279 CHECKOK(check_fdt_carveout(fdt, 1, 1));
280 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 2), 0);
281 CHECKOK(check_fdt_carveout(fdt, 1, 2));
282#else
283 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 1), -FDT_ERR_BADVALUE);
284 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 2), -FDT_ERR_BADVALUE);
285#endif
286 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 2, 1), 0);
287 CHECKOK(check_fdt_carveout(fdt, 2, 1));
288 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 2, 2), 0);
289 CHECKOK(check_fdt_carveout(fdt, 2, 2));
290
291 return 0;
292}
293
Simon Glass88941952012-01-17 08:20:51 +0000294static int do_test_fdtdec(cmd_tbl_t *cmdtp, int flag, int argc,
295 char * const argv[])
296{
297 /* basic tests */
298 CHECKOK(run_test("", "", ""));
299 CHECKOK(run_test("1e 3d", "", ""));
300
301 /*
302 * 'a' represents 0, 'b' represents 1, etc.
303 * The first character is the alias number, the second is the node
304 * number. So the params mean:
305 * 0a 1b : point alias 0 to node 0 (a), alias 1 to node 1(b)
306 * ab : to create nodes 0 and 1 (a and b)
307 * ab : we expect the function to return two nodes, in
308 * the order 0, 1
309 */
310 CHECKOK(run_test("0a 1b", "ab", "ab"));
311
312 CHECKOK(run_test("0a 1c", "ab", "ab"));
313 CHECKOK(run_test("1c", "ab", "ab"));
314 CHECKOK(run_test("1b", "ab", "ab"));
315 CHECKOK(run_test("0b", "ab", "ba"));
316 CHECKOK(run_test("0b 2d", "dbc", "bcd"));
317 CHECKOK(run_test("0d 3a 1c 2b", "dbac", "dcba"));
318
319 /* things with holes */
320 CHECKOK(run_test("1b 3d", "dbc", "cb d"));
321 CHECKOK(run_test("1e 3d", "dbc", "bc d"));
322
323 /* no aliases */
324 CHECKOK(run_test("", "dbac", "dbac"));
325
326 /* disabled nodes */
327 CHECKOK(run_test("0d 3a 1c 2b", "dBac", "dc a"));
328 CHECKOK(run_test("0b 2d", "DBc", "c"));
329 CHECKOK(run_test("0b 4d 2c", "DBc", " c"));
330
331 /* conflicting aliases - first one gets it */
332 CHECKOK(run_test("2a 1a 0a", "a", " a"));
333 CHECKOK(run_test("0a 1a 2a", "a", "a"));
334
Thierry Redingea89acb2019-03-21 19:10:07 +0100335 CHECKOK(check_carveout());
336
Simon Glass88941952012-01-17 08:20:51 +0000337 printf("Test passed\n");
338 return 0;
339}
340
341U_BOOT_CMD(
342 test_fdtdec, 3, 1, do_test_fdtdec,
343 "test_fdtdec",
344 "Run tests for fdtdec library");