blob: 35d9e84c3d1dfe0e46cc00991ca56b7abdfcb72b [file] [log] [blame]
Simon Glass74724482022-07-13 06:06:59 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for fdt command
4 *
Marek Vasutf53e9732023-04-22 14:59:58 +02005 * Copyright 2022 Google LLC
Simon Glass74724482022-07-13 06:06:59 -06006 */
7
Simon Glass74724482022-07-13 06:06:59 -06008#include <console.h>
9#include <fdt_support.h>
10#include <mapmem.h>
11#include <asm/global_data.h>
12#include <linux/libfdt.h>
13#include <test/suites.h>
14#include <test/ut.h>
15
16DECLARE_GLOBAL_DATA_PTR;
Marek Vasut3998b452023-03-02 04:08:45 +010017/*
18 * Missing tests:
19 * fdt boardsetup - Do board-specific set up
20 * fdt checksign [<addr>] - check FIT signature
21 * <addr> - address of key blob
22 * default gd->fdt_blob
23 */
Simon Glass74724482022-07-13 06:06:59 -060024
25/* Declare a new fdt test */
26#define FDT_TEST(_name, _flags) UNIT_TEST(_name, _flags, fdt_test)
27
28/**
29 * make_test_fdt() - Create an FDT with just a root node
30 *
31 * The size is set to the minimum needed
32 *
33 * @uts: Test state
34 * @fdt: Place to write FDT
35 * @size: Maximum size of space for fdt
36 */
37static int make_test_fdt(struct unit_test_state *uts, void *fdt, int size)
38{
39 ut_assertok(fdt_create(fdt, size));
40 ut_assertok(fdt_finish_reservemap(fdt));
41 ut_assert(fdt_begin_node(fdt, "") >= 0);
42 ut_assertok(fdt_end_node(fdt));
43 ut_assertok(fdt_finish(fdt));
44
45 return 0;
46}
47
Marek Vasutd6729762023-03-02 04:08:27 +010048/**
49 * make_fuller_fdt() - Create an FDT with root node and properties
50 *
51 * The size is set to the minimum needed
52 *
53 * @uts: Test state
54 * @fdt: Place to write FDT
55 * @size: Maximum size of space for fdt
56 */
57static int make_fuller_fdt(struct unit_test_state *uts, void *fdt, int size)
58{
59 fdt32_t regs[2] = { cpu_to_fdt32(0x1234), cpu_to_fdt32(0x1000) };
60
61 /*
62 * Assemble the following DT for test purposes:
63 *
64 * / {
65 * #address-cells = <0x00000001>;
66 * #size-cells = <0x00000001>;
67 * compatible = "u-boot,fdt-test";
68 * model = "U-Boot FDT test";
69 *
70 * aliases {
71 * badalias = "/bad/alias";
72 * subnodealias = "/test-node@1234/subnode";
73 * testnodealias = "/test-node@1234";
74 * };
75 *
76 * test-node@1234 {
77 * #address-cells = <0x00000000>;
78 * #size-cells = <0x00000000>;
79 * compatible = "u-boot,fdt-test-device1";
80 * clock-names = "fixed", "i2c", "spi", "uart2", "uart1";
81 * u-boot,empty-property;
82 * clock-frequency = <0x00fde800>;
83 * regs = <0x00001234 0x00001000>;
84 *
85 * subnode {
86 * #address-cells = <0x00000000>;
87 * #size-cells = <0x00000000>;
88 * compatible = "u-boot,fdt-subnode-test-device";
89 * };
90 * };
91 * };
92 */
93
94 ut_assertok(fdt_create(fdt, size));
95 ut_assertok(fdt_finish_reservemap(fdt));
96 ut_assert(fdt_begin_node(fdt, "") >= 0);
97
98 ut_assertok(fdt_property_u32(fdt, "#address-cells", 1));
99 ut_assertok(fdt_property_u32(fdt, "#size-cells", 1));
100 /* <string> */
101 ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test"));
102 /* <string> */
103 ut_assertok(fdt_property_string(fdt, "model", "U-Boot FDT test"));
104
105 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
106 /* <string> */
107 ut_assertok(fdt_property_string(fdt, "badalias", "/bad/alias"));
108 /* <string> */
109 ut_assertok(fdt_property_string(fdt, "subnodealias", "/test-node@1234/subnode"));
110 /* <string> */
111 ut_assertok(fdt_property_string(fdt, "testnodealias", "/test-node@1234"));
112 ut_assertok(fdt_end_node(fdt));
113
114 ut_assert(fdt_begin_node(fdt, "test-node@1234") >= 0);
115 ut_assertok(fdt_property_cell(fdt, "#address-cells", 0));
116 ut_assertok(fdt_property_cell(fdt, "#size-cells", 0));
117 /* <string> */
118 ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test-device1"));
119 /* <stringlist> */
120 ut_assertok(fdt_property(fdt, "clock-names", "fixed\0i2c\0spi\0uart2\0uart1\0", 26));
121 /* <empty> */
122 ut_assertok(fdt_property(fdt, "u-boot,empty-property", NULL, 0));
123 /*
124 * <u32>
125 * This value is deliberate as it used to break cmd/fdt.c
126 * is_printable_string() implementation.
127 */
128 ut_assertok(fdt_property_u32(fdt, "clock-frequency", 16640000));
129 /* <prop-encoded-array> */
130 ut_assertok(fdt_property(fdt, "regs", &regs, sizeof(regs)));
131 ut_assert(fdt_begin_node(fdt, "subnode") >= 0);
132 ut_assertok(fdt_property_cell(fdt, "#address-cells", 0));
133 ut_assertok(fdt_property_cell(fdt, "#size-cells", 0));
134 ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-subnode-test-device"));
135 ut_assertok(fdt_end_node(fdt));
136 ut_assertok(fdt_end_node(fdt));
137
138 ut_assertok(fdt_end_node(fdt));
139 ut_assertok(fdt_finish(fdt));
140
141 return 0;
142}
143
Simon Glass74724482022-07-13 06:06:59 -0600144/* Test 'fdt addr' getting/setting address */
145static int fdt_test_addr(struct unit_test_state *uts)
146{
147 const void *fdt_blob, *new_fdt;
148 char fdt[256];
149 ulong addr;
150 int ret;
151
152 ut_assertok(console_record_reset_enable());
153 ut_assertok(run_command("fdt addr -c", 0));
154 ut_assert_nextline("Control fdt: %08lx",
155 (ulong)map_to_sysmem(gd->fdt_blob));
156 ut_assertok(ut_check_console_end(uts));
157
158 /* The working fdt is not set, so this should fail */
159 set_working_fdt_addr(0);
Simon Glassbaf41412022-10-11 09:47:12 -0600160 ut_assert_nextline("Working FDT set to 0");
Simon Glass74724482022-07-13 06:06:59 -0600161 ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0));
Simon Glass40565bb2023-10-01 19:15:18 -0600162
163 /*
164 * sandbox fails the check for !blob since the 0 pointer is mapped to
165 * memory somewhere other than at 0x0
166 */
167 if (IS_ENABLED(CONFIG_SANDBOX))
168 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
Simon Glass74724482022-07-13 06:06:59 -0600169 ut_assertok(ut_check_console_end(uts));
170
171 /* Set up a working FDT and try again */
172 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
173 addr = map_to_sysmem(fdt);
174 set_working_fdt_addr(addr);
Simon Glassbaf41412022-10-11 09:47:12 -0600175 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glass74724482022-07-13 06:06:59 -0600176 ut_assertok(run_command("fdt addr", 0));
177 ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt));
178 ut_assertok(ut_check_console_end(uts));
179
180 /* Set the working FDT */
181 set_working_fdt_addr(0);
Simon Glassbaf41412022-10-11 09:47:12 -0600182 ut_assert_nextline("Working FDT set to 0");
Evgeny Bachinin0d73c232023-03-20 11:23:13 +0300183 ut_assertok(run_commandf("fdt addr %08lx", addr));
Simon Glassbaf41412022-10-11 09:47:12 -0600184 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glass74724482022-07-13 06:06:59 -0600185 ut_asserteq(addr, map_to_sysmem(working_fdt));
186 ut_assertok(ut_check_console_end(uts));
187 set_working_fdt_addr(0);
Simon Glassbaf41412022-10-11 09:47:12 -0600188 ut_assert_nextline("Working FDT set to 0");
Simon Glass74724482022-07-13 06:06:59 -0600189
Simon Glassbaf41412022-10-11 09:47:12 -0600190 /* Set the control FDT */
Simon Glass74724482022-07-13 06:06:59 -0600191 fdt_blob = gd->fdt_blob;
192 gd->fdt_blob = NULL;
Evgeny Bachinin0d73c232023-03-20 11:23:13 +0300193 ret = run_commandf("fdt addr -c %08lx", addr);
Simon Glass74724482022-07-13 06:06:59 -0600194 new_fdt = gd->fdt_blob;
195 gd->fdt_blob = fdt_blob;
196 ut_assertok(ret);
197 ut_asserteq(addr, map_to_sysmem(new_fdt));
198 ut_assertok(ut_check_console_end(uts));
199
200 /* Test setting an invalid FDT */
201 fdt[0] = 123;
Evgeny Bachinin0d73c232023-03-20 11:23:13 +0300202 ut_asserteq(1, run_commandf("fdt addr %08lx", addr));
Simon Glass74724482022-07-13 06:06:59 -0600203 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
204 ut_assertok(ut_check_console_end(uts));
205
206 /* Test detecting an invalid FDT */
207 fdt[0] = 123;
208 set_working_fdt_addr(addr);
Simon Glassbaf41412022-10-11 09:47:12 -0600209 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glass74724482022-07-13 06:06:59 -0600210 ut_asserteq(1, run_commandf("fdt addr"));
211 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
212 ut_assertok(ut_check_console_end(uts));
213
214 return 0;
215}
Simon Glass9b997622024-08-22 07:57:50 -0600216FDT_TEST(fdt_test_addr, UTF_CONSOLE);
Simon Glass74724482022-07-13 06:06:59 -0600217
218/* Test 'fdt addr' resizing an fdt */
Marek Vasutab40faf2023-03-02 04:08:25 +0100219static int fdt_test_addr_resize(struct unit_test_state *uts)
Simon Glass74724482022-07-13 06:06:59 -0600220{
221 char fdt[256];
222 const int newsize = sizeof(fdt) / 2;
223 ulong addr;
224
225 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
226 addr = map_to_sysmem(fdt);
227 set_working_fdt_addr(addr);
228
229 /* Test setting and resizing the working FDT to a larger size */
230 ut_assertok(console_record_reset_enable());
Evgeny Bachinin0d73c232023-03-20 11:23:13 +0300231 ut_assertok(run_commandf("fdt addr %08lx %x", addr, newsize));
Simon Glassbaf41412022-10-11 09:47:12 -0600232 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glass74724482022-07-13 06:06:59 -0600233 ut_assertok(ut_check_console_end(uts));
234
235 /* Try shrinking it */
Evgeny Bachinin0d73c232023-03-20 11:23:13 +0300236 ut_assertok(run_commandf("fdt addr %08lx %zx", addr, sizeof(fdt) / 4));
Simon Glassbaf41412022-10-11 09:47:12 -0600237 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glass74724482022-07-13 06:06:59 -0600238 ut_assert_nextline("New length %d < existing length %d, ignoring",
239 (int)sizeof(fdt) / 4, newsize);
240 ut_assertok(ut_check_console_end(uts));
241
242 /* ...quietly */
Evgeny Bachinin0d73c232023-03-20 11:23:13 +0300243 ut_assertok(run_commandf("fdt addr -q %08lx %zx", addr, sizeof(fdt) / 4));
Simon Glass74724482022-07-13 06:06:59 -0600244 ut_assertok(ut_check_console_end(uts));
245
246 /* We cannot easily provoke errors in fdt_open_into(), so ignore that */
247
248 return 0;
249}
Simon Glass9b997622024-08-22 07:57:50 -0600250FDT_TEST(fdt_test_addr_resize, UTF_CONSOLE);
Simon Glass74724482022-07-13 06:06:59 -0600251
Marek Vasut05728722023-03-02 04:08:30 +0100252static int fdt_test_move(struct unit_test_state *uts)
253{
254 char fdt[256];
255 ulong addr, newaddr = 0x10000;
256 const int size = sizeof(fdt);
257 uint32_t ts;
258 void *buf;
259
260 /* Original source DT */
261 ut_assertok(make_test_fdt(uts, fdt, size));
262 ts = fdt_totalsize(fdt);
263 addr = map_to_sysmem(fdt);
264 set_working_fdt_addr(addr);
265
266 /* Moved target DT location */
267 buf = map_sysmem(newaddr, size);
268 memset(buf, 0, size);
269
270 /* Test moving the working FDT to a new location */
271 ut_assertok(console_record_reset_enable());
Evgeny Bachinin0d73c232023-03-20 11:23:13 +0300272 ut_assertok(run_commandf("fdt move %08lx %08lx %x", addr, newaddr, ts));
Marek Vasut05728722023-03-02 04:08:30 +0100273 ut_assert_nextline("Working FDT set to %lx", newaddr);
274 ut_assertok(ut_check_console_end(uts));
275
276 /* Compare the source and destination DTs */
277 ut_assertok(console_record_reset_enable());
Evgeny Bachinin0d73c232023-03-20 11:23:13 +0300278 ut_assertok(run_commandf("cmp.b %08lx %08lx %x", addr, newaddr, ts));
Marek Vasut05728722023-03-02 04:08:30 +0100279 ut_assert_nextline("Total of %d byte(s) were the same", ts);
280 ut_assertok(ut_check_console_end(uts));
281
282 return 0;
283}
Simon Glass9b997622024-08-22 07:57:50 -0600284FDT_TEST(fdt_test_move, UTF_CONSOLE);
Marek Vasut05728722023-03-02 04:08:30 +0100285
Marek Vasut8fa28352023-03-02 04:08:31 +0100286static int fdt_test_resize(struct unit_test_state *uts)
287{
288 char fdt[256];
289 const unsigned int newsize = 0x2000;
290 uint32_t ts;
291 ulong addr;
292
293 /* Original source DT */
294 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
295 fdt_shrink_to_minimum(fdt, 0); /* Resize with 0 extra bytes */
296 ts = fdt_totalsize(fdt);
297 addr = map_to_sysmem(fdt);
298 set_working_fdt_addr(addr);
299
300 /* Test resizing the working FDT and verify the new space was added */
301 ut_assertok(console_record_reset_enable());
302 ut_assertok(run_commandf("fdt resize %x", newsize));
303 ut_asserteq(ts + newsize, fdt_totalsize(fdt));
304 ut_assertok(ut_check_console_end(uts));
305
306 return 0;
307}
Simon Glass9b997622024-08-22 07:57:50 -0600308FDT_TEST(fdt_test_resize, UTF_CONSOLE);
Marek Vasut8fa28352023-03-02 04:08:31 +0100309
Marek Vasutb0cd7cc2023-03-02 04:08:32 +0100310static int fdt_test_print_list_common(struct unit_test_state *uts,
311 const char *opc, const char *node)
312{
313 /*
314 * Test printing/listing the working FDT
315 * subnode $node/subnode
316 */
317 ut_assertok(console_record_reset_enable());
318 ut_assertok(run_commandf("fdt %s %s/subnode", opc, node));
319 ut_assert_nextline("subnode {");
320 ut_assert_nextline("\t#address-cells = <0x00000000>;");
321 ut_assert_nextline("\t#size-cells = <0x00000000>;");
322 ut_assert_nextline("\tcompatible = \"u-boot,fdt-subnode-test-device\";");
323 ut_assert_nextline("};");
324 ut_assertok(ut_check_console_end(uts));
325
326 /*
327 * Test printing/listing the working FDT
328 * path / string property model
329 */
330 ut_assertok(console_record_reset_enable());
331 ut_assertok(run_commandf("fdt %s / model", opc));
332 ut_assert_nextline("model = \"U-Boot FDT test\"");
333 ut_assertok(ut_check_console_end(uts));
334
335 /*
336 * Test printing/listing the working FDT
337 * path $node string property compatible
338 */
339 ut_assertok(console_record_reset_enable());
340 ut_assertok(run_commandf("fdt %s %s compatible", opc, node));
341 ut_assert_nextline("compatible = \"u-boot,fdt-test-device1\"");
342 ut_assertok(ut_check_console_end(uts));
343
344 /*
345 * Test printing/listing the working FDT
346 * path $node stringlist property clock-names
347 */
348 ut_assertok(console_record_reset_enable());
349 ut_assertok(run_commandf("fdt %s %s clock-names", opc, node));
350 ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
351 ut_assertok(ut_check_console_end(uts));
352
353 /*
354 * Test printing/listing the working FDT
355 * path $node u32 property clock-frequency
356 */
357 ut_assertok(console_record_reset_enable());
358 ut_assertok(run_commandf("fdt %s %s clock-frequency", opc, node));
359 ut_assert_nextline("clock-frequency = <0x00fde800>");
360 ut_assertok(ut_check_console_end(uts));
361
362 /*
363 * Test printing/listing the working FDT
364 * path $node empty property u-boot,empty-property
365 */
366 ut_assertok(console_record_reset_enable());
367 ut_assertok(run_commandf("fdt %s %s u-boot,empty-property", opc, node));
368 /*
369 * This is the only 'fdt print' / 'fdt list' incantation which
370 * prefixes the property with node path. This has been in U-Boot
371 * since the beginning of the command 'fdt', keep it.
372 */
373 ut_assert_nextline("%s u-boot,empty-property", node);
374 ut_assertok(ut_check_console_end(uts));
375
376 /*
377 * Test printing/listing the working FDT
378 * path $node prop-encoded array property regs
379 */
380 ut_assertok(console_record_reset_enable());
381 ut_assertok(run_commandf("fdt %s %s regs", opc, node));
382 ut_assert_nextline("regs = <0x00001234 0x00001000>");
383 ut_assertok(ut_check_console_end(uts));
384
385 return 0;
386}
387
388static int fdt_test_print_list(struct unit_test_state *uts, bool print)
389{
390 const char *opc = print ? "print" : "list";
391 char fdt[4096];
392 ulong addr;
393 int ret;
394
395 /* Original source DT */
396 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
397 addr = map_to_sysmem(fdt);
398 set_working_fdt_addr(addr);
399
400 /* Test printing/listing the working FDT -- node / */
401 ut_assertok(console_record_reset_enable());
402 ut_assertok(run_commandf("fdt %s", opc));
403 ut_assert_nextline("/ {");
404 ut_assert_nextline("\t#address-cells = <0x00000001>;");
405 ut_assert_nextline("\t#size-cells = <0x00000001>;");
406 ut_assert_nextline("\tcompatible = \"u-boot,fdt-test\";");
407 ut_assert_nextline("\tmodel = \"U-Boot FDT test\";");
408 ut_assert_nextline("\taliases {");
409 if (print) {
410 ut_assert_nextline("\t\tbadalias = \"/bad/alias\";");
411 ut_assert_nextline("\t\tsubnodealias = \"/test-node@1234/subnode\";");
412 ut_assert_nextline("\t\ttestnodealias = \"/test-node@1234\";");
413 }
414 ut_assert_nextline("\t};");
415 ut_assert_nextline("\ttest-node@1234 {");
416 if (print) {
417 ut_assert_nextline("\t\t#address-cells = <0x00000000>;");
418 ut_assert_nextline("\t\t#size-cells = <0x00000000>;");
419 ut_assert_nextline("\t\tcompatible = \"u-boot,fdt-test-device1\";");
420 ut_assert_nextline("\t\tclock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\";");
421 ut_assert_nextline("\t\tu-boot,empty-property;");
422 ut_assert_nextline("\t\tclock-frequency = <0x00fde800>;");
423 ut_assert_nextline("\t\tregs = <0x00001234 0x00001000>;");
424 ut_assert_nextline("\t\tsubnode {");
425 ut_assert_nextline("\t\t\t#address-cells = <0x00000000>;");
426 ut_assert_nextline("\t\t\t#size-cells = <0x00000000>;");
427 ut_assert_nextline("\t\t\tcompatible = \"u-boot,fdt-subnode-test-device\";");
428 ut_assert_nextline("\t\t};");
429 }
430 ut_assert_nextline("\t};");
431 ut_assert_nextline("};");
432 ut_assertok(ut_check_console_end(uts));
433
434 ret = fdt_test_print_list_common(uts, opc, "/test-node@1234");
435 if (!ret)
436 ret = fdt_test_print_list_common(uts, opc, "testnodealias");
437
438 return 0;
439}
440
441static int fdt_test_print(struct unit_test_state *uts)
442{
443 return fdt_test_print_list(uts, true);
444}
Simon Glass9b997622024-08-22 07:57:50 -0600445FDT_TEST(fdt_test_print, UTF_CONSOLE);
Marek Vasutb0cd7cc2023-03-02 04:08:32 +0100446
447static int fdt_test_list(struct unit_test_state *uts)
448{
449 return fdt_test_print_list(uts, false);
450}
Simon Glass9b997622024-08-22 07:57:50 -0600451FDT_TEST(fdt_test_list, UTF_CONSOLE);
Marek Vasutb0cd7cc2023-03-02 04:08:32 +0100452
Marek Vasut025b9d82023-03-02 04:08:26 +0100453/* Test 'fdt get value' reading an fdt */
Marek Vasutf1df20a2023-03-02 04:08:29 +0100454static int fdt_test_get_value_string(struct unit_test_state *uts,
455 const char *node, const char *prop,
456 const char *idx, const char *strres,
457 const int intres)
458{
459 ut_assertok(console_record_reset_enable());
460 ut_assertok(run_commandf("fdt get value var %s %s %s",
461 node, prop, idx ? : ""));
Simon Glassb8133032024-08-22 07:57:55 -0600462 if (strres)
Marek Vasutf1df20a2023-03-02 04:08:29 +0100463 ut_asserteq_str(strres, env_get("var"));
Simon Glassb8133032024-08-22 07:57:55 -0600464 else
Marek Vasutf1df20a2023-03-02 04:08:29 +0100465 ut_asserteq(intres, env_get_hex("var", 0x1234));
Marek Vasutf1df20a2023-03-02 04:08:29 +0100466 ut_assertok(ut_check_console_end(uts));
467
468 return 0;
469}
470
Marek Vasut39e07272023-03-02 04:08:28 +0100471static int fdt_test_get_value_common(struct unit_test_state *uts,
472 const char *node)
473{
474 /* Test getting default element of $node node clock-names property */
Simon Glassb8133032024-08-22 07:57:55 -0600475 ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", NULL,
476 "fixed", 0));
Marek Vasut39e07272023-03-02 04:08:28 +0100477
478 /* Test getting 0th element of $node node clock-names property */
Simon Glassb8133032024-08-22 07:57:55 -0600479 ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", "0",
480 "fixed", 0));
Marek Vasut39e07272023-03-02 04:08:28 +0100481
482 /* Test getting 1st element of $node node clock-names property */
Simon Glassb8133032024-08-22 07:57:55 -0600483 ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", "1",
484 "i2c", 0));
Marek Vasut39e07272023-03-02 04:08:28 +0100485
486 /* Test getting 2nd element of $node node clock-names property */
Simon Glassb8133032024-08-22 07:57:55 -0600487 ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", "2",
488 "spi", 0));
Marek Vasutf1df20a2023-03-02 04:08:29 +0100489
490 /*
491 * Test getting default element of $node node regs property.
492 * The result here is highly unusual, the non-index value read from
493 * integer array is a string of concatenated values from the array,
494 * but only if the array is shorter than 40 characters. Anything
495 * longer is an error. This is a special case for handling hashes.
496 */
Simon Glassb8133032024-08-22 07:57:55 -0600497 ut_assertok(fdt_test_get_value_string(uts, node, "regs", NULL,
498 "3412000000100000", 0));
Marek Vasutf1df20a2023-03-02 04:08:29 +0100499
500 /* Test getting 0th element of $node node regs property */
Simon Glassb8133032024-08-22 07:57:55 -0600501 ut_assertok(fdt_test_get_value_string(uts, node, "regs", "0", NULL,
502 0x1234));
Marek Vasutf1df20a2023-03-02 04:08:29 +0100503
504 /* Test getting 1st element of $node node regs property */
Simon Glassb8133032024-08-22 07:57:55 -0600505 ut_assertok(fdt_test_get_value_string(uts, node, "regs", "1", NULL,
506 0x1000));
Marek Vasut39e07272023-03-02 04:08:28 +0100507
508 /* Test missing 10th element of $node node clock-names property */
509 ut_assertok(console_record_reset_enable());
510 ut_asserteq(1, run_commandf("fdt get value ften %s clock-names 10", node));
511 ut_assertok(ut_check_console_end(uts));
512
Marek Vasutf1df20a2023-03-02 04:08:29 +0100513 /* Test missing 10th element of $node node regs property */
514 ut_assertok(console_record_reset_enable());
515 ut_asserteq(1, run_commandf("fdt get value ften %s regs 10", node));
516 ut_assertok(ut_check_console_end(uts));
517
Marek Vasut39e07272023-03-02 04:08:28 +0100518 /* Test getting default element of $node node nonexistent property */
519 ut_assertok(console_record_reset_enable());
520 ut_asserteq(1, run_commandf("fdt get value fnone %s nonexistent", node));
521 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
522 ut_assertok(ut_check_console_end(uts));
523
524 return 0;
525}
526
Marek Vasut025b9d82023-03-02 04:08:26 +0100527static int fdt_test_get_value(struct unit_test_state *uts)
Marek Vasutd83615b2022-11-14 22:50:00 +0100528{
Marek Vasutd6729762023-03-02 04:08:27 +0100529 char fdt[4096];
Marek Vasutd83615b2022-11-14 22:50:00 +0100530 ulong addr;
531
Marek Vasutd6729762023-03-02 04:08:27 +0100532 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
533 addr = map_to_sysmem(fdt);
Marek Vasutd83615b2022-11-14 22:50:00 +0100534 set_working_fdt_addr(addr);
535
Simon Glassb8133032024-08-22 07:57:55 -0600536 ut_assertok(fdt_test_get_value_common(uts, "/test-node@1234"));
537 ut_assertok(fdt_test_get_value_common(uts, "testnodealias"));
Marek Vasutd83615b2022-11-14 22:50:00 +0100538
539 /* Test getting default element of /nonexistent node */
540 ut_assertok(console_record_reset_enable());
541 ut_asserteq(1, run_command("fdt get value fnode /nonexistent nonexistent", 1));
542 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
543 ut_assertok(ut_check_console_end(uts));
544
Marek Vasut39e07272023-03-02 04:08:28 +0100545 /* Test getting default element of bad alias */
546 ut_assertok(console_record_reset_enable());
547 ut_asserteq(1, run_command("fdt get value vbadalias badalias nonexistent", 1));
548 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
549 ut_assertok(ut_check_console_end(uts));
550
551 /* Test getting default element of nonexistent alias */
552 ut_assertok(console_record_reset_enable());
553 ut_asserteq(1, run_command("fdt get value vnoalias noalias nonexistent", 1));
554 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
555 ut_assertok(ut_check_console_end(uts));
556
Marek Vasutd83615b2022-11-14 22:50:00 +0100557 return 0;
558}
Simon Glass9b997622024-08-22 07:57:50 -0600559FDT_TEST(fdt_test_get_value, UTF_CONSOLE);
Marek Vasutd83615b2022-11-14 22:50:00 +0100560
Marek Vasut890d3fc2023-03-02 04:08:33 +0100561static int fdt_test_get_name(struct unit_test_state *uts)
562{
563 char fdt[4096];
564 ulong addr;
565
566 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
567 addr = map_to_sysmem(fdt);
568 set_working_fdt_addr(addr);
569
570 /* Test getting name of node 0 in /, which is /aliases node */
571 ut_assertok(console_record_reset_enable());
572 ut_assertok(run_command("fdt get name nzero / 0", 0));
573 ut_asserteq_str("aliases", env_get("nzero"));
574 ut_assertok(ut_check_console_end(uts));
575
576 /* Test getting name of node 1 in /, which is /test-node@1234 node */
577 ut_assertok(console_record_reset_enable());
578 ut_assertok(run_command("fdt get name none / 1", 0));
579 ut_asserteq_str("test-node@1234", env_get("none"));
580 ut_assertok(ut_check_console_end(uts));
581
582 /* Test getting name of node -1 in /, which is /aliases node, same as 0 */
583 ut_assertok(console_record_reset_enable());
584 ut_assertok(run_command("fdt get name nmone / -1", 0));
585 ut_asserteq_str("aliases", env_get("nmone"));
586 ut_assertok(ut_check_console_end(uts));
587
588 /* Test getting name of node 2 in /, which does not exist */
589 ut_assertok(console_record_reset_enable());
590 ut_asserteq(1, run_command("fdt get name ntwo / 2", 1));
591 ut_assert_nextline("libfdt node not found");
592 ut_assertok(ut_check_console_end(uts));
593
594 /* Test getting name of node 0 in /test-node@1234, which is /subnode node */
595 ut_assertok(console_record_reset_enable());
596 ut_assertok(run_command("fdt get name snzero /test-node@1234 0", 0));
597 ut_asserteq_str("subnode", env_get("snzero"));
598 ut_assertok(run_command("fdt get name asnzero testnodealias 0", 0));
599 ut_asserteq_str("subnode", env_get("asnzero"));
600 ut_assertok(ut_check_console_end(uts));
601
602 /* Test getting name of node 1 in /test-node@1234, which does not exist */
603 ut_assertok(console_record_reset_enable());
604 ut_asserteq(1, run_command("fdt get name snone /test-node@1234 1", 1));
605 ut_assert_nextline("libfdt node not found");
606 ut_asserteq(1, run_command("fdt get name asnone testnodealias 1", 1));
607 ut_assert_nextline("libfdt node not found");
608 ut_assertok(ut_check_console_end(uts));
609
610 /* Test getting name of node -1 in /test-node@1234, which is /subnode node, same as 0 */
611 ut_assertok(console_record_reset_enable());
612 ut_assertok(run_command("fdt get name snmone /test-node@1234 -1", 0));
613 ut_asserteq_str("subnode", env_get("snmone"));
614 ut_assertok(run_command("fdt get name asnmone testnodealias -1", 0));
615 ut_asserteq_str("subnode", env_get("asnmone"));
616 ut_assertok(ut_check_console_end(uts));
617
618 /* Test getting name of nonexistent node */
619 ut_assertok(console_record_reset_enable());
620 ut_asserteq(1, run_command("fdt get name nonode /nonexistent 0", 1));
621 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
622 ut_assertok(ut_check_console_end(uts));
623
624 /* Test getting name of bad alias */
625 ut_assertok(console_record_reset_enable());
626 ut_asserteq(1, run_command("fdt get name vbadalias badalias 0", 1));
627 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
628 ut_assertok(ut_check_console_end(uts));
629
630 /* Test getting name of nonexistent alias */
631 ut_assertok(console_record_reset_enable());
632 ut_asserteq(1, run_command("fdt get name vnoalias noalias 0", 1));
633 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
634 ut_assertok(ut_check_console_end(uts));
635
636 return 0;
637}
Simon Glass9b997622024-08-22 07:57:50 -0600638FDT_TEST(fdt_test_get_name, UTF_CONSOLE);
Marek Vasut890d3fc2023-03-02 04:08:33 +0100639
Marek Vasutb6d1dec2023-03-02 04:08:34 +0100640static int fdt_test_get_addr_common(struct unit_test_state *uts, char *fdt,
641 const char *path, const char *prop)
642{
643 unsigned int offset;
644 int path_offset;
645 void *prop_ptr;
646 int len = 0;
647
648 path_offset = fdt_path_offset(fdt, path);
649 ut_assert(path_offset >= 0);
650 prop_ptr = (void *)fdt_getprop(fdt, path_offset, prop, &len);
651 ut_assertnonnull(prop_ptr);
652 offset = (char *)prop_ptr - fdt;
653
654 ut_assertok(console_record_reset_enable());
655 ut_assertok(run_commandf("fdt get addr pstr %s %s", path, prop));
656 ut_asserteq((ulong)map_sysmem(env_get_hex("fdtaddr", 0x1234), 0),
657 (ulong)(map_sysmem(env_get_hex("pstr", 0x1234), 0) - offset));
658 ut_assertok(ut_check_console_end(uts));
659
660 return 0;
661}
662
663static int fdt_test_get_addr(struct unit_test_state *uts)
664{
665 char fdt[4096];
666 ulong addr;
667
668 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
669 addr = map_to_sysmem(fdt);
670 set_working_fdt_addr(addr);
671
672 /* Test getting address of root node / string property "compatible" */
Simon Glassb8133032024-08-22 07:57:55 -0600673 ut_assertok(fdt_test_get_addr_common(uts, fdt, "/", "compatible"));
Marek Vasutb6d1dec2023-03-02 04:08:34 +0100674
675 /* Test getting address of node /test-node@1234 stringlist property "clock-names" */
Simon Glassb8133032024-08-22 07:57:55 -0600676 ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234",
677 "clock-names"));
678 ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias",
679 "clock-names"));
Marek Vasutb6d1dec2023-03-02 04:08:34 +0100680
681 /* Test getting address of node /test-node@1234 u32 property "clock-frequency" */
Simon Glassb8133032024-08-22 07:57:55 -0600682 ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234",
683 "clock-frequency"));
684 ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias",
685 "clock-frequency"));
Marek Vasutb6d1dec2023-03-02 04:08:34 +0100686
687 /* Test getting address of node /test-node@1234 empty property "u-boot,empty-property" */
Simon Glassb8133032024-08-22 07:57:55 -0600688 ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234",
689 "u-boot,empty-property"));
690 ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias",
691 "u-boot,empty-property"));
Marek Vasutb6d1dec2023-03-02 04:08:34 +0100692
693 /* Test getting address of node /test-node@1234 array property "regs" */
Simon Glassb8133032024-08-22 07:57:55 -0600694 ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234",
695 "regs"));
696 ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias",
697 "regs"));
Marek Vasutb6d1dec2023-03-02 04:08:34 +0100698
699 /* Test getting address of node /test-node@1234/subnode non-existent property "noprop" */
700 ut_assertok(console_record_reset_enable());
701 ut_asserteq(1, run_command("fdt get addr pnoprop /test-node@1234/subnode noprop", 1));
702 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
703 ut_assertok(ut_check_console_end(uts));
704
705 /* Test getting address of non-existent node /test-node@1234/nonode@1 property "noprop" */
706 ut_assertok(console_record_reset_enable());
707 ut_asserteq(1, run_command("fdt get addr pnonode /test-node@1234/nonode@1 noprop", 1));
708 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
709 ut_assertok(ut_check_console_end(uts));
710
711 return 0;
712}
Simon Glass9b997622024-08-22 07:57:50 -0600713FDT_TEST(fdt_test_get_addr, UTF_CONSOLE);
Marek Vasutb6d1dec2023-03-02 04:08:34 +0100714
Marek Vasut66e975a2023-03-02 04:08:35 +0100715static int fdt_test_get_size_common(struct unit_test_state *uts,
716 const char *path, const char *prop,
717 const unsigned int val)
718{
719 ut_assertok(console_record_reset_enable());
720 if (prop) {
721 ut_assertok(run_commandf("fdt get size sstr %s %s", path, prop));
722 } else {
723 ut_assertok(run_commandf("fdt get size sstr %s", path));
724 }
725 ut_asserteq(val, env_get_hex("sstr", 0x1234));
726 ut_assertok(ut_check_console_end(uts));
727
728 return 0;
729}
730
731static int fdt_test_get_size(struct unit_test_state *uts)
732{
733 char fdt[4096];
734 ulong addr;
735
736 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
737 addr = map_to_sysmem(fdt);
738 set_working_fdt_addr(addr);
739
740 /* Test getting size of root node / string property "compatible" */
Simon Glassb8133032024-08-22 07:57:55 -0600741 ut_assertok(fdt_test_get_size_common(uts, "/", "compatible", 16));
Marek Vasut66e975a2023-03-02 04:08:35 +0100742
743 /* Test getting size of node /test-node@1234 stringlist property "clock-names" */
Simon Glassb8133032024-08-22 07:57:55 -0600744 ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234",
745 "clock-names", 26));
746 ut_assertok(fdt_test_get_size_common(uts, "testnodealias",
747 "clock-names", 26));
Marek Vasut66e975a2023-03-02 04:08:35 +0100748
749 /* Test getting size of node /test-node@1234 u32 property "clock-frequency" */
Simon Glassb8133032024-08-22 07:57:55 -0600750 ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234",
751 "clock-frequency", 4));
752 ut_assertok(fdt_test_get_size_common(uts, "testnodealias",
753 "clock-frequency", 4));
Marek Vasut66e975a2023-03-02 04:08:35 +0100754
755 /* Test getting size of node /test-node@1234 empty property "u-boot,empty-property" */
Simon Glassb8133032024-08-22 07:57:55 -0600756 ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234",
757 "u-boot,empty-property", 0));
758 ut_assertok(fdt_test_get_size_common(uts, "testnodealias",
759 "u-boot,empty-property", 0));
Marek Vasut66e975a2023-03-02 04:08:35 +0100760
761 /* Test getting size of node /test-node@1234 array property "regs" */
Simon Glassb8133032024-08-22 07:57:55 -0600762 ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234", "regs",
763 8));
764 ut_assertok(fdt_test_get_size_common(uts, "testnodealias", "regs", 8));
Marek Vasut66e975a2023-03-02 04:08:35 +0100765
766 /* Test getting node count of node / */
Simon Glassb8133032024-08-22 07:57:55 -0600767 ut_assertok(fdt_test_get_size_common(uts, "/", NULL, 2));
Marek Vasut66e975a2023-03-02 04:08:35 +0100768
769 /* Test getting node count of node /test-node@1234/subnode */
Simon Glassb8133032024-08-22 07:57:55 -0600770 ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234/subnode",
771 NULL, 0));
772 ut_assertok(fdt_test_get_size_common(uts, "subnodealias", NULL, 0));
Marek Vasut66e975a2023-03-02 04:08:35 +0100773
774 /* Test getting size of node /test-node@1234/subnode non-existent property "noprop" */
775 ut_assertok(console_record_reset_enable());
776 ut_asserteq(1, run_command("fdt get size pnoprop /test-node@1234/subnode noprop", 1));
777 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
778 ut_asserteq(1, run_command("fdt get size pnoprop subnodealias noprop", 1));
779 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
780 ut_assertok(ut_check_console_end(uts));
781
782 /* Test getting size of non-existent node /test-node@1234/nonode@1 property "noprop" */
783 ut_assertok(console_record_reset_enable());
784 ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1 noprop", 1));
785 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
786 ut_assertok(ut_check_console_end(uts));
787
788 /* Test getting node count of non-existent node /test-node@1234/nonode@1 */
789 ut_assertok(console_record_reset_enable());
790 ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1", 1));
791 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
792 ut_assertok(ut_check_console_end(uts));
793
794 /* Test getting node count of bad alias badalias */
795 ut_assertok(console_record_reset_enable());
796 ut_asserteq(1, run_command("fdt get size pnonode badalias noprop", 1));
797 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
798 ut_assertok(ut_check_console_end(uts));
799
800 /* Test getting node count of non-existent alias noalias */
801 ut_assertok(console_record_reset_enable());
802 ut_asserteq(1, run_command("fdt get size pnonode noalias", 1));
803 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
804 ut_assertok(ut_check_console_end(uts));
805
806 return 0;
807}
Simon Glass9b997622024-08-22 07:57:50 -0600808FDT_TEST(fdt_test_get_size, UTF_CONSOLE);
Marek Vasut66e975a2023-03-02 04:08:35 +0100809
Marek Vasut8bd49a82023-03-02 04:08:36 +0100810static int fdt_test_set_single(struct unit_test_state *uts,
811 const char *path, const char *prop,
812 const char *sval, int ival, bool integer)
813{
814 /*
815 * Set single element string/integer/<empty> property into DT, that is:
816 * => fdt set /path property string
817 * => fdt set /path property integer
818 * => fdt set /path property
819 */
820 ut_assertok(console_record_reset_enable());
Marek Vasuta96dea22023-03-10 04:33:14 +0100821 if (sval)
Marek Vasut8bd49a82023-03-02 04:08:36 +0100822 ut_assertok(run_commandf("fdt set %s %s %s", path, prop, sval));
Marek Vasuta96dea22023-03-10 04:33:14 +0100823 else if (integer)
Marek Vasut8bd49a82023-03-02 04:08:36 +0100824 ut_assertok(run_commandf("fdt set %s %s <%d>", path, prop, ival));
Marek Vasuta96dea22023-03-10 04:33:14 +0100825 else
Marek Vasut8bd49a82023-03-02 04:08:36 +0100826 ut_assertok(run_commandf("fdt set %s %s", path, prop));
Marek Vasut8bd49a82023-03-02 04:08:36 +0100827
828 /* Validate the property is present and has correct value. */
829 ut_assertok(run_commandf("fdt get value svar %s %s", path, prop));
Marek Vasuta96dea22023-03-10 04:33:14 +0100830 if (sval)
Marek Vasut8bd49a82023-03-02 04:08:36 +0100831 ut_asserteq_str(sval, env_get("svar"));
Marek Vasuta96dea22023-03-10 04:33:14 +0100832 else if (integer)
Marek Vasut8bd49a82023-03-02 04:08:36 +0100833 ut_asserteq(ival, env_get_hex("svar", 0x1234));
Marek Vasuta96dea22023-03-10 04:33:14 +0100834 else
Marek Vasut8bd49a82023-03-02 04:08:36 +0100835 ut_assertnull(env_get("svar"));
Marek Vasut8bd49a82023-03-02 04:08:36 +0100836 ut_assertok(ut_check_console_end(uts));
837
838 return 0;
839}
840
841static int fdt_test_set_multi(struct unit_test_state *uts,
842 const char *path, const char *prop,
843 const char *sval1, const char *sval2,
844 int ival1, int ival2)
845{
846 /*
847 * Set multi element string/integer array property in DT, that is:
848 * => fdt set /path property <string1 string2>
849 * => fdt set /path property <integer1 integer2>
850 *
851 * The set is done twice in here deliberately, The first set adds
852 * the property with an extra trailing element in its array to make
853 * the array longer, the second set is the expected final content of
854 * the array property. The longer array is used to verify that the
855 * new array is correctly sized and read past the new array length
856 * triggers failure.
857 */
858 ut_assertok(console_record_reset_enable());
859 if (sval1 && sval2) {
860 ut_assertok(run_commandf("fdt set %s %s %s %s end", path, prop, sval1, sval2));
861 ut_assertok(run_commandf("fdt set %s %s %s %s", path, prop, sval1, sval2));
862 } else {
863 ut_assertok(run_commandf("fdt set %s %s <%d %d 10>", path, prop, ival1, ival2));
864 ut_assertok(run_commandf("fdt set %s %s <%d %d>", path, prop, ival1, ival2));
865 }
866
867 /*
868 * Validate the property is present and has correct value.
869 *
870 * The "end/10" above and "svarn" below is used to validate that
871 * previous 'fdt set' to longer array does not polute newly set
872 * shorter array.
873 */
874 ut_assertok(run_commandf("fdt get value svar1 %s %s 0", path, prop));
875 ut_assertok(run_commandf("fdt get value svar2 %s %s 1", path, prop));
876 ut_asserteq(1, run_commandf("fdt get value svarn %s %s 2", path, prop));
877 if (sval1 && sval2) {
878 ut_asserteq_str(sval1, env_get("svar1"));
879 ut_asserteq_str(sval2, env_get("svar2"));
880 ut_assertnull(env_get("svarn"));
881 } else {
882 ut_asserteq(ival1, env_get_hex("svar1", 0x1234));
883 ut_asserteq(ival2, env_get_hex("svar2", 0x1234));
884 ut_assertnull(env_get("svarn"));
885 }
886 ut_assertok(ut_check_console_end(uts));
887
888 return 0;
889}
890
891static int fdt_test_set_node(struct unit_test_state *uts,
892 const char *path, const char *prop)
893{
Simon Glassb8133032024-08-22 07:57:55 -0600894 ut_assertok(fdt_test_set_single(uts, path, prop, "new", 0, false));
895 ut_assertok(fdt_test_set_single(uts, path, prop, "rewrite", 0, false));
896 ut_assertok(fdt_test_set_single(uts, path, prop, NULL, 42, true));
897 ut_assertok(fdt_test_set_single(uts, path, prop, NULL, 0, false));
898 ut_assertok(fdt_test_set_multi(uts, path, prop, NULL, NULL, 42, 1701));
899 ut_assertok(fdt_test_set_multi(uts, path, prop, NULL, NULL, 74656, 9));
900 ut_assertok(fdt_test_set_multi(uts, path, prop, "42", "1701", 0, 0));
901 ut_assertok(fdt_test_set_multi(uts, path, prop, "74656", "9", 0, 0));
Marek Vasut8bd49a82023-03-02 04:08:36 +0100902
903 return 0;
904}
905
906static int fdt_test_set(struct unit_test_state *uts)
907{
908 char fdt[8192];
909 ulong addr;
910
911 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
912 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
913 addr = map_to_sysmem(fdt);
914 set_working_fdt_addr(addr);
915
916 /* Test setting of root node / existing property "compatible" */
Simon Glassb8133032024-08-22 07:57:55 -0600917 ut_assertok(fdt_test_set_node(uts, "/", "compatible"));
Marek Vasut8bd49a82023-03-02 04:08:36 +0100918
919 /* Test setting of root node / new property "newproperty" */
Simon Glassb8133032024-08-22 07:57:55 -0600920 ut_assertok(fdt_test_set_node(uts, "/", "newproperty"));
Marek Vasut8bd49a82023-03-02 04:08:36 +0100921
922 /* Test setting of subnode existing property "compatible" */
Simon Glassb8133032024-08-22 07:57:55 -0600923 ut_assertok(fdt_test_set_node(uts, "/test-node@1234/subnode",
924 "compatible"));
925 ut_assertok(fdt_test_set_node(uts, "subnodealias", "compatible"));
Marek Vasut8bd49a82023-03-02 04:08:36 +0100926
927 /* Test setting of subnode new property "newproperty" */
Simon Glassb8133032024-08-22 07:57:55 -0600928 ut_assertok(fdt_test_set_node(uts, "/test-node@1234/subnode",
929 "newproperty"));
930 ut_assertok(fdt_test_set_node(uts, "subnodealias", "newproperty"));
Marek Vasut8bd49a82023-03-02 04:08:36 +0100931
932 /* Test setting property of non-existent node */
933 ut_assertok(console_record_reset_enable());
934 ut_asserteq(1, run_command("fdt set /no-node noprop", 1));
935 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
936 ut_assertok(ut_check_console_end(uts));
937
938 /* Test setting property of non-existent alias */
939 ut_assertok(console_record_reset_enable());
940 ut_asserteq(1, run_command("fdt set noalias noprop", 1));
941 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
942 ut_assertok(ut_check_console_end(uts));
943
944 /* Test setting property of bad alias */
945 ut_assertok(console_record_reset_enable());
946 ut_asserteq(1, run_command("fdt set badalias noprop", 1));
947 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
948 ut_assertok(ut_check_console_end(uts));
949
950 return 0;
951}
Simon Glass9b997622024-08-22 07:57:50 -0600952FDT_TEST(fdt_test_set, UTF_CONSOLE);
Marek Vasut8bd49a82023-03-02 04:08:36 +0100953
Marek Vasut6c285942023-03-02 04:08:37 +0100954static int fdt_test_mknode(struct unit_test_state *uts)
955{
956 char fdt[8192];
957 ulong addr;
958
959 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
960 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
961 addr = map_to_sysmem(fdt);
962 set_working_fdt_addr(addr);
963
964 /* Test creation of new node in / */
965 ut_assertok(console_record_reset_enable());
966 ut_assertok(run_commandf("fdt mknode / newnode"));
967 ut_assertok(run_commandf("fdt list /newnode"));
968 ut_assert_nextline("newnode {");
969 ut_assert_nextline("};");
970 ut_assertok(ut_check_console_end(uts));
971
972 /* Test creation of new node in /test-node@1234 */
973 ut_assertok(console_record_reset_enable());
974 ut_assertok(run_commandf("fdt mknode /test-node@1234 newsubnode"));
975 ut_assertok(run_commandf("fdt list /test-node@1234/newsubnode"));
976 ut_assert_nextline("newsubnode {");
977 ut_assert_nextline("};");
978 ut_assertok(ut_check_console_end(uts));
979
980 /* Test creation of new node in /test-node@1234 by alias */
981 ut_assertok(console_record_reset_enable());
982 ut_assertok(run_commandf("fdt mknode testnodealias newersubnode"));
983 ut_assertok(run_commandf("fdt list testnodealias/newersubnode"));
984 ut_assert_nextline("newersubnode {");
985 ut_assert_nextline("};");
986 ut_assertok(ut_check_console_end(uts));
987
988 /* Test creation of new node in /test-node@1234 over existing node */
989 ut_assertok(console_record_reset_enable());
990 ut_asserteq(1, run_commandf("fdt mknode testnodealias newsubnode"));
991 ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
992 ut_assertok(ut_check_console_end(uts));
993
994 /* Test creation of new node in /test-node@1234 by alias over existing node */
995 ut_assertok(console_record_reset_enable());
996 ut_asserteq(1, run_commandf("fdt mknode testnodealias newersubnode"));
997 ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
998 ut_assertok(ut_check_console_end(uts));
999
1000 /* Test creation of new node in non-existent node */
1001 ut_assertok(console_record_reset_enable());
1002 ut_asserteq(1, run_commandf("fdt mknode /no-node newnosubnode"));
1003 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1004 ut_assertok(ut_check_console_end(uts));
1005
1006 /* Test creation of new node in non-existent alias */
1007 ut_assertok(console_record_reset_enable());
1008 ut_asserteq(1, run_commandf("fdt mknode noalias newfailsubnode"));
1009 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
1010 ut_assertok(ut_check_console_end(uts));
1011
1012 /* Test creation of new node in bad alias */
1013 ut_assertok(console_record_reset_enable());
1014 ut_asserteq(1, run_commandf("fdt mknode badalias newbadsubnode"));
1015 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1016 ut_assertok(ut_check_console_end(uts));
1017
1018 return 0;
1019}
Simon Glass9b997622024-08-22 07:57:50 -06001020FDT_TEST(fdt_test_mknode, UTF_CONSOLE);
Marek Vasut6c285942023-03-02 04:08:37 +01001021
Marek Vasute46a4382023-03-02 04:08:38 +01001022static int fdt_test_rm(struct unit_test_state *uts)
1023{
1024 char fdt[4096];
1025 ulong addr;
1026
1027 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
1028 addr = map_to_sysmem(fdt);
1029 set_working_fdt_addr(addr);
1030
1031 /* Test removal of property in root node / */
1032 ut_assertok(console_record_reset_enable());
1033 ut_assertok(run_commandf("fdt print / compatible"));
1034 ut_assert_nextline("compatible = \"u-boot,fdt-test\"");
1035 ut_assertok(run_commandf("fdt rm / compatible"));
1036 ut_asserteq(1, run_commandf("fdt print / compatible"));
1037 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1038 ut_assertok(ut_check_console_end(uts));
1039
1040 /* Test removal of property clock-names in subnode /test-node@1234 */
1041 ut_assertok(console_record_reset_enable());
1042 ut_assertok(run_commandf("fdt print /test-node@1234 clock-names"));
1043 ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
1044 ut_assertok(run_commandf("fdt rm /test-node@1234 clock-names"));
1045 ut_asserteq(1, run_commandf("fdt print /test-node@1234 clock-names"));
1046 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1047 ut_assertok(ut_check_console_end(uts));
1048
1049 /* Test removal of property u-boot,empty-property in subnode /test-node@1234 by alias */
1050 ut_assertok(console_record_reset_enable());
1051 ut_assertok(run_commandf("fdt print testnodealias u-boot,empty-property"));
1052 ut_assert_nextline("testnodealias u-boot,empty-property");
1053 ut_assertok(run_commandf("fdt rm testnodealias u-boot,empty-property"));
1054 ut_asserteq(1, run_commandf("fdt print testnodealias u-boot,empty-property"));
1055 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1056 ut_assertok(ut_check_console_end(uts));
1057
1058 /* Test removal of non-existent property noprop in subnode /test-node@1234 */
1059 ut_assertok(console_record_reset_enable());
1060 ut_asserteq(1, run_commandf("fdt rm /test-node@1234 noprop"));
1061 ut_assert_nextline("libfdt fdt_delprop(): FDT_ERR_NOTFOUND");
1062 ut_assertok(ut_check_console_end(uts));
1063
1064 /* Test removal of non-existent node /no-node@5678 */
1065 ut_assertok(console_record_reset_enable());
1066 ut_asserteq(1, run_commandf("fdt rm /no-node@5678"));
1067 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1068 ut_assertok(ut_check_console_end(uts));
1069
1070 /* Test removal of subnode /test-node@1234/subnode by alias */
1071 ut_assertok(console_record_reset_enable());
1072 ut_assertok(run_commandf("fdt rm subnodealias"));
1073 ut_asserteq(1, run_commandf("fdt print /test-node@1234/subnode"));
1074 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1075 ut_assertok(ut_check_console_end(uts));
1076
1077 /* Test removal of node by non-existent alias */
1078 ut_assertok(console_record_reset_enable());
1079 ut_asserteq(1, run_commandf("fdt rm noalias"));
1080 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
1081 ut_assertok(ut_check_console_end(uts));
1082
1083 /* Test removal of node by bad alias */
1084 ut_assertok(console_record_reset_enable());
1085 ut_asserteq(1, run_commandf("fdt rm noalias"));
1086 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
1087 ut_assertok(ut_check_console_end(uts));
1088
1089 /* Test removal of node /test-node@1234 */
1090 ut_assertok(console_record_reset_enable());
1091 ut_assertok(run_commandf("fdt rm /test-node@1234"));
1092 ut_asserteq(1, run_commandf("fdt print /test-node@1234"));
1093 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1094 ut_assertok(ut_check_console_end(uts));
1095
1096 /* Test removal of node / */
1097 ut_assertok(console_record_reset_enable());
1098 ut_assertok(run_commandf("fdt rm /"));
1099 ut_asserteq(1, run_commandf("fdt print /"));
1100 ut_assertok(ut_check_console_end(uts));
1101
1102 return 0;
1103}
Simon Glass9b997622024-08-22 07:57:50 -06001104FDT_TEST(fdt_test_rm, UTF_CONSOLE);
Marek Vasute46a4382023-03-02 04:08:38 +01001105
Marek Vasut26281512023-03-02 04:08:40 +01001106static int fdt_test_bootcpu(struct unit_test_state *uts)
1107{
1108 char fdt[256];
1109 ulong addr;
1110 int i;
1111
1112 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1113 addr = map_to_sysmem(fdt);
1114 set_working_fdt_addr(addr);
1115
1116 /* Test getting default bootcpu entry */
1117 ut_assertok(console_record_reset_enable());
1118 ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
1119 ut_asserteq(0, env_get_ulong("bootcpu", 10, 0x1234));
1120 ut_assertok(ut_check_console_end(uts));
1121
1122 /* Test setting and getting new bootcpu entry, twice, to test overwrite */
1123 for (i = 42; i <= 43; i++) {
1124 ut_assertok(console_record_reset_enable());
1125 ut_assertok(run_commandf("fdt bootcpu %d", i));
1126 ut_assertok(ut_check_console_end(uts));
1127
1128 /* Test getting new bootcpu entry */
1129 ut_assertok(console_record_reset_enable());
1130 ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
1131 ut_asserteq(i, env_get_ulong("bootcpu", 10, 0x1234));
1132 ut_assertok(ut_check_console_end(uts));
1133 }
1134
1135 return 0;
1136}
Simon Glass9b997622024-08-22 07:57:50 -06001137FDT_TEST(fdt_test_bootcpu, UTF_CONSOLE);
Marek Vasut26281512023-03-02 04:08:40 +01001138
Marek Vasut927e03b2023-03-02 04:08:39 +01001139static int fdt_test_header_get(struct unit_test_state *uts,
1140 const char *field, const unsigned long val)
1141{
1142 /* Test getting valid header entry */
1143 ut_assertok(console_record_reset_enable());
1144 ut_assertok(run_commandf("fdt header get fvar %s", field));
1145 ut_asserteq(val, env_get_hex("fvar", 0x1234));
1146 ut_assertok(ut_check_console_end(uts));
1147
1148 /* Test getting malformed header entry */
1149 ut_assertok(console_record_reset_enable());
1150 ut_asserteq(1, run_commandf("fdt header get fvar typo%stypo", field));
1151 ut_assertok(ut_check_console_end(uts));
1152
1153 return 0;
1154}
1155
1156static int fdt_test_header(struct unit_test_state *uts)
1157{
1158 char fdt[256];
1159 ulong addr;
1160
1161 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1162 addr = map_to_sysmem(fdt);
1163 set_working_fdt_addr(addr);
1164
1165 /* Test header print */
1166 ut_assertok(console_record_reset_enable());
1167 ut_assertok(run_commandf("fdt header"));
1168 ut_assert_nextline("magic:\t\t\t0x%x", fdt_magic(fdt));
1169 ut_assert_nextline("totalsize:\t\t0x%x (%d)", fdt_totalsize(fdt), fdt_totalsize(fdt));
1170 ut_assert_nextline("off_dt_struct:\t\t0x%x", fdt_off_dt_struct(fdt));
1171 ut_assert_nextline("off_dt_strings:\t\t0x%x", fdt_off_dt_strings(fdt));
1172 ut_assert_nextline("off_mem_rsvmap:\t\t0x%x", fdt_off_mem_rsvmap(fdt));
1173 ut_assert_nextline("version:\t\t%d", fdt_version(fdt));
1174 ut_assert_nextline("last_comp_version:\t%d", fdt_last_comp_version(fdt));
1175 ut_assert_nextline("boot_cpuid_phys:\t0x%x", fdt_boot_cpuid_phys(fdt));
1176 ut_assert_nextline("size_dt_strings:\t0x%x", fdt_size_dt_strings(fdt));
1177 ut_assert_nextline("size_dt_struct:\t\t0x%x", fdt_size_dt_struct(fdt));
1178 ut_assert_nextline("number mem_rsv:\t\t0x%x", fdt_num_mem_rsv(fdt));
1179 ut_assert_nextline_empty();
1180 ut_assertok(ut_check_console_end(uts));
1181
1182 /* Test header get */
Simon Glassb8133032024-08-22 07:57:55 -06001183 ut_assertok(fdt_test_header_get(uts, "magic", fdt_magic(fdt)));
1184 ut_assertok(fdt_test_header_get(uts, "totalsize", fdt_totalsize(fdt)));
1185 ut_assertok(fdt_test_header_get(uts, "off_dt_struct",
1186 fdt_off_dt_struct(fdt)));
1187 ut_assertok(fdt_test_header_get(uts, "off_dt_strings",
1188 fdt_off_dt_strings(fdt)));
1189 ut_assertok(fdt_test_header_get(uts, "off_mem_rsvmap",
1190 fdt_off_mem_rsvmap(fdt)));
1191 ut_assertok(fdt_test_header_get(uts, "version", fdt_version(fdt)));
1192 ut_assertok(fdt_test_header_get(uts, "last_comp_version",
1193 fdt_last_comp_version(fdt)));
1194 ut_assertok(fdt_test_header_get(uts, "boot_cpuid_phys",
1195 fdt_boot_cpuid_phys(fdt)));
1196 ut_assertok(fdt_test_header_get(uts, "size_dt_strings",
1197 fdt_size_dt_strings(fdt)));
1198 ut_assertok(fdt_test_header_get(uts, "size_dt_struct",
1199 fdt_size_dt_struct(fdt)));
Marek Vasut927e03b2023-03-02 04:08:39 +01001200
1201 return 0;
1202}
Simon Glass9b997622024-08-22 07:57:50 -06001203FDT_TEST(fdt_test_header, UTF_CONSOLE);
Marek Vasut927e03b2023-03-02 04:08:39 +01001204
Marek Vasutd9b4c9f2023-03-02 04:08:41 +01001205static int fdt_test_memory_cells(struct unit_test_state *uts,
1206 const unsigned int cells)
1207{
1208 unsigned char *pada, *pads;
1209 unsigned char *seta, *sets;
1210 char fdt[8192];
1211 const int size = sizeof(fdt);
1212 fdt32_t *regs;
1213 ulong addr;
1214 char *spc;
1215 int i;
1216
1217 /* Create DT with node /memory { regs = <0x100 0x200>; } and #*cells */
1218 ut_assertnonnull(regs = calloc(2 * cells, sizeof(*regs)));
1219 ut_assertnonnull(pada = calloc(12, cells));
1220 ut_assertnonnull(pads = calloc(12, cells));
1221 ut_assertnonnull(seta = calloc(12, cells));
1222 ut_assertnonnull(sets = calloc(12, cells));
1223 for (i = cells; i >= 1; i--) {
1224 regs[cells - 1] = cpu_to_fdt32(i * 0x10000);
1225 regs[(cells * 2) - 1] = cpu_to_fdt32(~i);
1226 snprintf(seta + (8 * (cells - i)), 9, "%08x", i * 0x10000);
1227 snprintf(sets + (8 * (cells - i)), 9, "%08x", ~i);
1228 spc = (i != 1) ? " " : "";
1229 snprintf(pada + (11 * (cells - i)), 12, "0x%08x%s", i * 0x10000, spc);
1230 snprintf(pads + (11 * (cells - i)), 12, "0x%08x%s", ~i, spc);
1231 }
1232
1233 ut_assertok(fdt_create(fdt, size));
1234 ut_assertok(fdt_finish_reservemap(fdt));
1235 ut_assert(fdt_begin_node(fdt, "") >= 0);
1236 ut_assertok(fdt_property_u32(fdt, "#address-cells", cells));
1237 ut_assertok(fdt_property_u32(fdt, "#size-cells", cells));
1238 ut_assert(fdt_begin_node(fdt, "memory") >= 0);
1239 ut_assertok(fdt_property_string(fdt, "device_type", "memory"));
1240 ut_assertok(fdt_property(fdt, "reg", &regs, cells * 2));
1241 ut_assertok(fdt_end_node(fdt));
1242 ut_assertok(fdt_end_node(fdt));
1243 ut_assertok(fdt_finish(fdt));
1244 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1245 addr = map_to_sysmem(fdt);
1246 set_working_fdt_addr(addr);
1247
1248 /* Test updating the memory node */
1249 ut_assertok(console_record_reset_enable());
1250 ut_assertok(run_commandf("fdt memory 0x%s 0x%s", seta, sets));
1251 ut_assertok(run_commandf("fdt print /memory"));
1252 ut_assert_nextline("memory {");
1253 ut_assert_nextline("\tdevice_type = \"memory\";");
1254 ut_assert_nextline("\treg = <%s %s>;", pada, pads);
1255 ut_assert_nextline("};");
1256 ut_assertok(ut_check_console_end(uts));
1257
1258 free(sets);
1259 free(seta);
1260 free(pads);
1261 free(pada);
1262 free(regs);
1263
1264 return 0;
1265}
1266
1267static int fdt_test_memory(struct unit_test_state *uts)
1268{
1269 /*
1270 * Test memory fixup for 32 and 64 bit systems, anything bigger is
1271 * so far unsupported and fails because of simple_stroull() being
1272 * 64bit tops in the 'fdt memory' command implementation.
1273 */
Simon Glassb8133032024-08-22 07:57:55 -06001274 ut_assertok(fdt_test_memory_cells(uts, 1));
1275 ut_assertok(fdt_test_memory_cells(uts, 2));
Marek Vasutd9b4c9f2023-03-02 04:08:41 +01001276
1277 /*
1278 * The 'fdt memory' command is limited to /memory node, it does
1279 * not support any other valid DT memory node format, which is
1280 * either one or multiple /memory@adresss nodes. Therefore, this
1281 * DT variant is not tested here.
1282 */
1283
1284 return 0;
1285}
Simon Glass9b997622024-08-22 07:57:50 -06001286FDT_TEST(fdt_test_memory, UTF_CONSOLE);
Marek Vasutd9b4c9f2023-03-02 04:08:41 +01001287
Marek Vasut50daa2e2023-03-02 04:08:42 +01001288static int fdt_test_rsvmem(struct unit_test_state *uts)
1289{
1290 char fdt[8192];
1291 ulong addr;
1292
1293 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1294 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1295 fdt_add_mem_rsv(fdt, 0x42, 0x1701);
1296 fdt_add_mem_rsv(fdt, 0x74656, 0x9);
1297 addr = map_to_sysmem(fdt);
1298 set_working_fdt_addr(addr);
1299
1300 /* Test default reserved memory node presence */
1301 ut_assertok(console_record_reset_enable());
1302 ut_assertok(run_commandf("fdt rsvmem print"));
1303 ut_assert_nextline("index\t\t start\t\t size");
1304 ut_assert_nextline("------------------------------------------------");
1305 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
1306 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
1307 ut_assertok(ut_check_console_end(uts));
1308
1309 /* Test add new reserved memory node */
1310 ut_assertok(console_record_reset_enable());
1311 ut_assertok(run_commandf("fdt rsvmem add 0x1234 0x5678"));
1312 ut_assertok(run_commandf("fdt rsvmem print"));
1313 ut_assert_nextline("index\t\t start\t\t size");
1314 ut_assert_nextline("------------------------------------------------");
1315 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
1316 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
1317 ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x1234, 0x5678);
1318 ut_assertok(ut_check_console_end(uts));
1319
1320 /* Test delete reserved memory node */
1321 ut_assertok(console_record_reset_enable());
1322 ut_assertok(run_commandf("fdt rsvmem delete 0"));
1323 ut_assertok(run_commandf("fdt rsvmem print"));
1324 ut_assert_nextline("index\t\t start\t\t size");
1325 ut_assert_nextline("------------------------------------------------");
1326 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
1327 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
1328 ut_assertok(ut_check_console_end(uts));
1329
1330 /* Test re-add new reserved memory node */
1331 ut_assertok(console_record_reset_enable());
1332 ut_assertok(run_commandf("fdt rsvmem add 0x42 0x1701"));
1333 ut_assertok(run_commandf("fdt rsvmem print"));
1334 ut_assert_nextline("index\t\t start\t\t size");
1335 ut_assert_nextline("------------------------------------------------");
1336 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
1337 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
1338 ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x42, 0x1701);
1339 ut_assertok(ut_check_console_end(uts));
1340
1341 /* Test delete nonexistent reserved memory node */
1342 ut_assertok(console_record_reset_enable());
1343 ut_asserteq(1, run_commandf("fdt rsvmem delete 10"));
1344 ut_assert_nextline("libfdt fdt_del_mem_rsv(): FDT_ERR_NOTFOUND");
1345 ut_assertok(ut_check_console_end(uts));
1346
1347 return 0;
1348}
Simon Glass9b997622024-08-22 07:57:50 -06001349FDT_TEST(fdt_test_rsvmem, UTF_CONSOLE);
Marek Vasut50daa2e2023-03-02 04:08:42 +01001350
Marek Vasut77291e62023-03-02 04:08:43 +01001351static int fdt_test_chosen(struct unit_test_state *uts)
1352{
1353 const char *env_bootargs = env_get("bootargs");
1354 char fdt[8192];
1355 ulong addr;
1356
1357 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1358 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1359 addr = map_to_sysmem(fdt);
1360 set_working_fdt_addr(addr);
1361
1362 /* Test default chosen node presence, fail as there is no /chosen node */
1363 ut_assertok(console_record_reset_enable());
1364 ut_asserteq(1, run_commandf("fdt print /chosen"));
1365 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1366 ut_assertok(ut_check_console_end(uts));
1367
1368 /* Test add new chosen node without initrd */
1369 ut_assertok(console_record_reset_enable());
1370 ut_assertok(run_commandf("fdt chosen"));
1371 ut_assertok(run_commandf("fdt print /chosen"));
1372 ut_assert_nextline("chosen {");
1373 ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
1374 if (env_bootargs)
1375 ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
Tim Harveyf485a992024-06-18 14:06:09 -07001376 if (IS_ENABLED(CONFIG_DM_RNG) &&
1377 !IS_ENABLED(CONFIG_MEASURED_BOOT) &&
1378 !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT))
1379 ut_assert_nextlinen("\tkaslr-seed = ");
Marek Vasut77291e62023-03-02 04:08:43 +01001380 ut_assert_nextline("};");
1381 ut_assertok(ut_check_console_end(uts));
1382
1383 /* Test add new chosen node with initrd */
1384 ut_assertok(console_record_reset_enable());
1385 ut_assertok(run_commandf("fdt chosen 0x1234 0x5678"));
1386 ut_assertok(run_commandf("fdt print /chosen"));
1387 ut_assert_nextline("chosen {");
1388 ut_assert_nextline("\tlinux,initrd-end = <0x%08x 0x%08x>;",
1389 upper_32_bits(0x1234 + 0x5678 - 1),
1390 lower_32_bits(0x1234 + 0x5678 - 1));
1391 ut_assert_nextline("\tlinux,initrd-start = <0x%08x 0x%08x>;",
1392 upper_32_bits(0x1234), lower_32_bits(0x1234));
1393 ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
1394 if (env_bootargs)
1395 ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
Tim Harveyf485a992024-06-18 14:06:09 -07001396 if (IS_ENABLED(CONFIG_DM_RNG) &&
1397 !IS_ENABLED(CONFIG_MEASURED_BOOT) &&
1398 !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT))
1399 ut_assert_nextlinen("\tkaslr-seed = ");
Marek Vasut77291e62023-03-02 04:08:43 +01001400 ut_assert_nextline("};");
1401 ut_assertok(ut_check_console_end(uts));
1402
1403 return 0;
1404}
Simon Glass9b997622024-08-22 07:57:50 -06001405FDT_TEST(fdt_test_chosen, UTF_CONSOLE);
Marek Vasut77291e62023-03-02 04:08:43 +01001406
Marek Vasutc5fe73e2023-03-02 04:08:44 +01001407static int fdt_test_apply(struct unit_test_state *uts)
1408{
1409 char fdt[8192], fdto[8192];
1410 ulong addr, addro;
1411
1412 /* Create base DT with __symbols__ node */
1413 ut_assertok(fdt_create(fdt, sizeof(fdt)));
1414 ut_assertok(fdt_finish_reservemap(fdt));
1415 ut_assert(fdt_begin_node(fdt, "") >= 0);
1416 ut_assert(fdt_begin_node(fdt, "__symbols__") >= 0);
1417 ut_assertok(fdt_end_node(fdt));
1418 ut_assertok(fdt_end_node(fdt));
1419 ut_assertok(fdt_finish(fdt));
1420 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1421 addr = map_to_sysmem(fdt);
1422 set_working_fdt_addr(addr);
1423
1424 /* Create DTO which adds single property to root node / */
1425 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1426 ut_assertok(fdt_finish_reservemap(fdto));
1427 ut_assert(fdt_begin_node(fdto, "") >= 0);
1428 ut_assert(fdt_begin_node(fdto, "fragment") >= 0);
1429 ut_assertok(fdt_property_string(fdto, "target-path", "/"));
1430 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1431 ut_assertok(fdt_property_string(fdto, "newstring", "newvalue"));
1432 ut_assertok(fdt_end_node(fdto));
1433 ut_assertok(fdt_end_node(fdto));
1434 ut_assertok(fdt_finish(fdto));
1435 addro = map_to_sysmem(fdto);
1436
1437 /* Test default DT print */
1438 ut_assertok(console_record_reset_enable());
1439 ut_assertok(run_commandf("fdt print /"));
1440 ut_assert_nextline("/ {");
1441 ut_assert_nextline("\t__symbols__ {");
1442 ut_assert_nextline("\t};");
1443 ut_assert_nextline("};");
1444 ut_assertok(ut_check_console_end(uts));
1445
1446 /* Test simple DTO application */
1447 ut_assertok(console_record_reset_enable());
Evgeny Bachinin0d73c232023-03-20 11:23:13 +03001448 ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
Marek Vasutc5fe73e2023-03-02 04:08:44 +01001449 ut_assertok(run_commandf("fdt print /"));
1450 ut_assert_nextline("/ {");
1451 ut_assert_nextline("\tnewstring = \"newvalue\";");
1452 ut_assert_nextline("\t__symbols__ {");
1453 ut_assert_nextline("\t};");
1454 ut_assert_nextline("};");
1455 ut_assertok(ut_check_console_end(uts));
1456
1457 /*
1458 * Create complex DTO which:
1459 * - modifies newstring property in root node /
1460 * - adds new properties to root node /
1461 * - adds new subnode with properties to root node /
1462 * - adds phandle to the subnode and therefore __symbols__ node
1463 */
1464 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1465 ut_assertok(fdt_finish_reservemap(fdto));
1466 ut_assert(fdt_begin_node(fdto, "") >= 0);
1467 ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
1468 ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
1469
1470 ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
1471 ut_assertok(fdt_property_string(fdto, "target-path", "/"));
1472 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1473 ut_assertok(fdt_property_string(fdto, "newstring", "newervalue"));
1474 ut_assertok(fdt_property_u32(fdto, "newu32", 0x12345678));
1475 ut_assertok(fdt_property(fdto, "empty-property", NULL, 0));
1476 ut_assert(fdt_begin_node(fdto, "subnode") >= 0);
1477 ut_assertok(fdt_property_string(fdto, "subnewstring", "newervalue"));
1478 ut_assertok(fdt_property_u32(fdto, "subnewu32", 0x12345678));
1479 ut_assertok(fdt_property(fdto, "subempty-property", NULL, 0));
1480 ut_assertok(fdt_property_u32(fdto, "phandle", 0x01));
1481 ut_assertok(fdt_end_node(fdto));
1482 ut_assertok(fdt_end_node(fdto));
1483 ut_assertok(fdt_end_node(fdto));
1484
1485 ut_assert(fdt_begin_node(fdto, "__symbols__") >= 0);
1486 ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0/__overlay__/subnode"));
1487 ut_assertok(fdt_end_node(fdto));
1488 ut_assertok(fdt_finish(fdto));
1489 addro = map_to_sysmem(fdto);
1490
1491 /* Test complex DTO application */
1492 ut_assertok(console_record_reset_enable());
Evgeny Bachinin0d73c232023-03-20 11:23:13 +03001493 ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
Marek Vasutc5fe73e2023-03-02 04:08:44 +01001494 ut_assertok(run_commandf("fdt print /"));
1495 ut_assert_nextline("/ {");
1496 ut_assert_nextline("\tempty-property;");
1497 ut_assert_nextline("\tnewu32 = <0x12345678>;");
1498 ut_assert_nextline("\tnewstring = \"newervalue\";");
1499 ut_assert_nextline("\tsubnode {");
1500 ut_assert_nextline("\t\tphandle = <0x00000001>;");
1501 ut_assert_nextline("\t\tsubempty-property;");
1502 ut_assert_nextline("\t\tsubnewu32 = <0x12345678>;");
1503 ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
1504 ut_assert_nextline("\t};");
1505 ut_assert_nextline("\t__symbols__ {");
1506 ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
1507 ut_assert_nextline("\t};");
1508 ut_assert_nextline("};");
1509 ut_assertok(ut_check_console_end(uts));
1510
1511 /*
1512 * Create complex DTO which:
1513 * - modifies subnewu32 property in subnode via phandle and uses __fixups__ node
1514 */
1515 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1516 ut_assertok(fdt_finish_reservemap(fdto));
1517 ut_assert(fdt_begin_node(fdto, "") >= 0);
1518 ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
1519 ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
1520
1521 ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
1522 ut_assertok(fdt_property_u32(fdto, "target", 0xffffffff));
1523 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1524 ut_assertok(fdt_property_u32(fdto, "subnewu32", 0xabcdef01));
1525 ut_assertok(fdt_end_node(fdto));
1526 ut_assertok(fdt_end_node(fdto));
1527
1528 ut_assert(fdt_begin_node(fdto, "__fixups__") >= 0);
1529 ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0:target:0"));
1530 ut_assertok(fdt_end_node(fdto));
1531 ut_assertok(fdt_end_node(fdto));
1532 ut_assertok(fdt_finish(fdto));
1533 addro = map_to_sysmem(fdto);
1534
1535 /* Test complex DTO application */
1536 ut_assertok(console_record_reset_enable());
Evgeny Bachinin0d73c232023-03-20 11:23:13 +03001537 ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
Marek Vasutc5fe73e2023-03-02 04:08:44 +01001538 ut_assertok(run_commandf("fdt print /"));
1539 ut_assert_nextline("/ {");
1540 ut_assert_nextline("\tempty-property;");
1541 ut_assert_nextline("\tnewu32 = <0x12345678>;");
1542 ut_assert_nextline("\tnewstring = \"newervalue\";");
1543 ut_assert_nextline("\tsubnode {");
1544 ut_assert_nextline("\t\tphandle = <0x00000001>;");
1545 ut_assert_nextline("\t\tsubempty-property;");
1546 ut_assert_nextline("\t\tsubnewu32 = <0xabcdef01>;");
1547 ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
1548 ut_assert_nextline("\t};");
1549 ut_assert_nextline("\t__symbols__ {");
1550 ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
1551 ut_assert_nextline("\t};");
1552 ut_assert_nextline("};");
1553 ut_assertok(ut_check_console_end(uts));
1554
1555 return 0;
1556}
Simon Glass9b997622024-08-22 07:57:50 -06001557FDT_TEST(fdt_test_apply, UTF_CONSOLE);
Marek Vasutc5fe73e2023-03-02 04:08:44 +01001558
Simon Glass74724482022-07-13 06:06:59 -06001559int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1560{
1561 struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);
1562 const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test);
1563
1564 return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv);
1565}