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