1 // SPDX-License-Identifier: GPL-2.0+
3 * Some very basic tests for fdtdec, accessed through test_fdtdec command.
4 * They are easiest to use with sandbox.
6 * Copyright (c) 2011 The Chromium OS Authors.
11 #include <linux/libfdt.h>
15 /* The size of our test fdt blob */
16 #define FDT_SIZE (16 * 1024)
18 #define CHECK(op) ({ \
21 printf("%s: %s: %s\n", __func__, #op, \
29 #define CHECKVAL(op, expected) ({ \
31 if (err != expected) { \
32 printf("%s: %s: expected %d, but returned %d\n",\
33 __func__, #op, expected, err); \
40 #define CHECKOK(op) CHECKVAL(op, 0)
42 /* maximum number of nodes / aliases to generate */
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
57 static int make_fdt(void *fdt, int size, const char *aliases,
60 char name[20], value[20];
62 #if defined(DEBUG) && defined(CONFIG_SANDBOX)
66 CHECK(fdt_create(fdt, size));
67 CHECK(fdt_finish_reservemap(fdt));
68 CHECK(fdt_begin_node(fdt, ""));
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');
77 CHECK(fdt_end_node(fdt));
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)));
85 CHECK(fdt_property_string(fdt, "status", "disabled"));
86 CHECK(fdt_end_node(fdt));
89 CHECK(fdt_end_node(fdt));
90 CHECK(fdt_finish(fdt));
92 #if defined(DEBUG) && defined(CONFIG_SANDBOX)
93 fd = os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT | OS_O_WRONLY);
95 printf("Could not open .dtb file to write\n");
98 os_write(fd, fdt, size);
104 static int run_test(const char *aliases, const char *nodes, const char *expect)
111 blob = malloc(FDT_SIZE);
113 printf("%s: out of memory\n", __func__);
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",
121 list, ARRAY_SIZE(list)), (int)strlen(expect));
123 /* Check we got the right ones */
124 for (i = 0, s = expect; *s; s++, i++) {
129 name = list[i] ? fdt_get_name(blob, list[i], NULL) : NULL;
131 got = name[3] + 'a' - '0';
134 printf("Position %d: Expected '%c', got '%c' ('%s')\n",
145 static int make_fdt_carveout_device(void *fdt, uint32_t na, uint32_t ns)
147 const char *basename = "/display";
148 struct fdt_memory carveout = {
149 #ifdef CONFIG_PHYS_64BIT
150 .start = 0x180000000,
157 fdt32_t cells[4], *ptr = cells;
158 uint32_t upper, lower;
163 /* store one or two address cells */
164 upper = upper_32_bits(carveout.start);
165 lower = lower_32_bits(carveout.start);
167 if (na > 1 && upper > 0)
168 snprintf(name, sizeof(name), "%s@%x,%x", basename, upper,
171 snprintf(name, sizeof(name), "%s@%x", basename, lower);
174 *ptr++ = cpu_to_fdt32(upper);
176 *ptr++ = cpu_to_fdt32(lower);
178 /* store one or two size cells */
179 size = carveout.end - carveout.start + 1;
180 upper = upper_32_bits(size);
181 lower = lower_32_bits(size);
184 *ptr++ = cpu_to_fdt32(upper);
186 *ptr++ = cpu_to_fdt32(lower);
188 offset = CHECK(fdt_add_subnode(fdt, 0, name + 1));
189 CHECK(fdt_setprop(fdt, offset, "reg", cells, (na + ns) * sizeof(*cells)));
191 return fdtdec_set_carveout(fdt, name, "memory-region", 0,
192 "framebuffer", &carveout);
195 static int check_fdt_carveout(void *fdt, uint32_t address_cells,
198 #ifdef CONFIG_PHYS_64BIT
199 const char *name = "/display@1,80000000";
200 const struct fdt_memory expected = {
201 .start = 0x180000000,
205 const char *name = "/display@80000000";
206 const struct fdt_memory expected = {
211 struct fdt_memory carveout;
213 printf("carveout: %pap-%pap na=%u ns=%u: ", &expected.start,
214 &expected.end, address_cells, size_cells);
216 CHECK(fdtdec_get_carveout(fdt, name, "memory-region", 0, &carveout));
218 if ((carveout.start != expected.start) ||
219 (carveout.end != expected.end)) {
220 printf("carveout: %pap-%pap, expected %pap-%pap\n",
221 &carveout.start, &carveout.end,
222 &expected.start, &expected.end);
230 static int make_fdt_carveout(void *fdt, int size, uint32_t address_cells,
233 fdt32_t na = cpu_to_fdt32(address_cells);
234 fdt32_t ns = cpu_to_fdt32(size_cells);
235 #if defined(DEBUG) && defined(CONFIG_SANDBOX)
241 CHECK(fdt_create(fdt, size));
242 CHECK(fdt_finish_reservemap(fdt));
243 CHECK(fdt_begin_node(fdt, ""));
244 CHECK(fdt_property(fdt, "#address-cells", &na, sizeof(na)));
245 CHECK(fdt_property(fdt, "#size-cells", &ns, sizeof(ns)));
246 CHECK(fdt_end_node(fdt));
247 CHECK(fdt_finish(fdt));
248 CHECK(fdt_pack(fdt));
250 CHECK(fdt_open_into(fdt, fdt, FDT_SIZE));
252 err = make_fdt_carveout_device(fdt, address_cells, size_cells);
254 #if defined(DEBUG) && defined(CONFIG_SANDBOX)
255 snprintf(filename, sizeof(filename), "/tmp/fdtdec-carveout-%u-%u.dtb",
256 address_cells, size_cells);
258 fd = os_open(filename, OS_O_CREAT | OS_O_WRONLY);
260 printf("could not open .dtb file to write\n");
264 os_write(fd, fdt, size);
272 static int check_carveout(void)
276 fdt = malloc(FDT_SIZE);
278 printf("%s: out of memory\n", __func__);
282 #ifndef CONFIG_PHYS_64BIT
283 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 1), 0);
284 CHECKOK(check_fdt_carveout(fdt, 1, 1));
285 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 2), 0);
286 CHECKOK(check_fdt_carveout(fdt, 1, 2));
288 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 1), -FDT_ERR_BADVALUE);
289 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 2), -FDT_ERR_BADVALUE);
291 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 2, 1), 0);
292 CHECKOK(check_fdt_carveout(fdt, 2, 1));
293 CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 2, 2), 0);
294 CHECKOK(check_fdt_carveout(fdt, 2, 2));
300 static int do_test_fdtdec(cmd_tbl_t *cmdtp, int flag, int argc,
304 CHECKOK(run_test("", "", ""));
305 CHECKOK(run_test("1e 3d", "", ""));
308 * 'a' represents 0, 'b' represents 1, etc.
309 * The first character is the alias number, the second is the node
310 * number. So the params mean:
311 * 0a 1b : point alias 0 to node 0 (a), alias 1 to node 1(b)
312 * ab : to create nodes 0 and 1 (a and b)
313 * ab : we expect the function to return two nodes, in
316 CHECKOK(run_test("0a 1b", "ab", "ab"));
318 CHECKOK(run_test("0a 1c", "ab", "ab"));
319 CHECKOK(run_test("1c", "ab", "ab"));
320 CHECKOK(run_test("1b", "ab", "ab"));
321 CHECKOK(run_test("0b", "ab", "ba"));
322 CHECKOK(run_test("0b 2d", "dbc", "bcd"));
323 CHECKOK(run_test("0d 3a 1c 2b", "dbac", "dcba"));
325 /* things with holes */
326 CHECKOK(run_test("1b 3d", "dbc", "cb d"));
327 CHECKOK(run_test("1e 3d", "dbc", "bc d"));
330 CHECKOK(run_test("", "dbac", "dbac"));
333 CHECKOK(run_test("0d 3a 1c 2b", "dBac", "dc a"));
334 CHECKOK(run_test("0b 2d", "DBc", "c"));
335 CHECKOK(run_test("0b 4d 2c", "DBc", " c"));
337 /* conflicting aliases - first one gets it */
338 CHECKOK(run_test("2a 1a 0a", "a", " a"));
339 CHECKOK(run_test("0a 1a 2a", "a", "a"));
341 CHECKOK(check_carveout());
343 printf("Test passed\n");
348 test_fdtdec, 3, 1, do_test_fdtdec,
350 "Run tests for fdtdec library");