blob: e09a929a5e41589194227aede7ae5c70f2fdee6e [file] [log] [blame]
Simon Glassf3c6a1d2022-07-13 06:06:59 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for fdt command
4 *
Marek Vasutd578efb2023-04-22 14:59:58 +02005 * Copyright 2022 Google LLC
Simon Glassf3c6a1d2022-07-13 06:06:59 -06006 */
7
Simon Glassf3c6a1d2022-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 Vasutf2def362023-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 Glassf3c6a1d2022-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 Vasut15503d02023-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 Glassf3c6a1d2022-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 Glass84328cf2022-10-11 09:47:12 -0600160 ut_assert_nextline("Working FDT set to 0");
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600161 ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0));
Simon Glass5ad263f2023-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 Glassf3c6a1d2022-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 Glass84328cf2022-10-11 09:47:12 -0600175 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-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 Glass84328cf2022-10-11 09:47:12 -0600182 ut_assert_nextline("Working FDT set to 0");
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300183 ut_assertok(run_commandf("fdt addr %08lx", addr));
Simon Glass84328cf2022-10-11 09:47:12 -0600184 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-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 Glass84328cf2022-10-11 09:47:12 -0600188 ut_assert_nextline("Working FDT set to 0");
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600189
Simon Glass84328cf2022-10-11 09:47:12 -0600190 /* Set the control FDT */
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600191 fdt_blob = gd->fdt_blob;
192 gd->fdt_blob = NULL;
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300193 ret = run_commandf("fdt addr -c %08lx", addr);
Simon Glassf3c6a1d2022-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 Bachinine604bc02023-03-20 11:23:13 +0300202 ut_asserteq(1, run_commandf("fdt addr %08lx", addr));
Simon Glassf3c6a1d2022-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 Glass84328cf2022-10-11 09:47:12 -0600209 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-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}
216FDT_TEST(fdt_test_addr, UT_TESTF_CONSOLE_REC);
217
218/* Test 'fdt addr' resizing an fdt */
Marek Vasut6e04a892023-03-02 04:08:25 +0100219static int fdt_test_addr_resize(struct unit_test_state *uts)
Simon Glassf3c6a1d2022-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 Bachinine604bc02023-03-20 11:23:13 +0300231 ut_assertok(run_commandf("fdt addr %08lx %x", addr, newsize));
Simon Glass84328cf2022-10-11 09:47:12 -0600232 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600233 ut_assertok(ut_check_console_end(uts));
234
235 /* Try shrinking it */
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300236 ut_assertok(run_commandf("fdt addr %08lx %zx", addr, sizeof(fdt) / 4));
Simon Glass84328cf2022-10-11 09:47:12 -0600237 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-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 Bachinine604bc02023-03-20 11:23:13 +0300243 ut_assertok(run_commandf("fdt addr -q %08lx %zx", addr, sizeof(fdt) / 4));
Simon Glassf3c6a1d2022-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}
Marek Vasut6e04a892023-03-02 04:08:25 +0100250FDT_TEST(fdt_test_addr_resize, UT_TESTF_CONSOLE_REC);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600251
Marek Vasutddf86202023-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 Bachinine604bc02023-03-20 11:23:13 +0300272 ut_assertok(run_commandf("fdt move %08lx %08lx %x", addr, newaddr, ts));
Marek Vasutddf86202023-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 Bachinine604bc02023-03-20 11:23:13 +0300278 ut_assertok(run_commandf("cmp.b %08lx %08lx %x", addr, newaddr, ts));
Marek Vasutddf86202023-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}
284FDT_TEST(fdt_test_move, UT_TESTF_CONSOLE_REC);
285
Marek Vasut6ab4fe22023-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}
308FDT_TEST(fdt_test_resize, UT_TESTF_CONSOLE_REC);
309
Marek Vasut76a652a2023-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}
445FDT_TEST(fdt_test_print, UT_TESTF_CONSOLE_REC);
446
447static int fdt_test_list(struct unit_test_state *uts)
448{
449 return fdt_test_print_list(uts, false);
450}
451FDT_TEST(fdt_test_list, UT_TESTF_CONSOLE_REC);
452
Marek Vasutfbeca452023-03-02 04:08:26 +0100453/* Test 'fdt get value' reading an fdt */
Marek Vasut9f90cad2023-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 ? : ""));
462 if (strres) {
463 ut_asserteq_str(strres, env_get("var"));
464 } else {
465 ut_asserteq(intres, env_get_hex("var", 0x1234));
466 }
467 ut_assertok(ut_check_console_end(uts));
468
469 return 0;
470}
471
Marek Vasutb7517b42023-03-02 04:08:28 +0100472static int fdt_test_get_value_common(struct unit_test_state *uts,
473 const char *node)
Marek Vasut78c060d2022-11-14 22:50:00 +0100474{
Marek Vasutb7517b42023-03-02 04:08:28 +0100475 /* Test getting default element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100476 fdt_test_get_value_string(uts, node, "clock-names", NULL, "fixed", 0);
Marek Vasut78c060d2022-11-14 22:50:00 +0100477
Marek Vasutb7517b42023-03-02 04:08:28 +0100478 /* Test getting 0th element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100479 fdt_test_get_value_string(uts, node, "clock-names", "0", "fixed", 0);
Marek Vasut78c060d2022-11-14 22:50:00 +0100480
Marek Vasutb7517b42023-03-02 04:08:28 +0100481 /* Test getting 1st element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100482 fdt_test_get_value_string(uts, node, "clock-names", "1", "i2c", 0);
Marek Vasut78c060d2022-11-14 22:50:00 +0100483
Marek Vasutb7517b42023-03-02 04:08:28 +0100484 /* Test getting 2nd element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100485 fdt_test_get_value_string(uts, node, "clock-names", "2", "spi", 0);
486
487 /*
488 * Test getting default element of $node node regs property.
489 * The result here is highly unusual, the non-index value read from
490 * integer array is a string of concatenated values from the array,
491 * but only if the array is shorter than 40 characters. Anything
492 * longer is an error. This is a special case for handling hashes.
493 */
494 fdt_test_get_value_string(uts, node, "regs", NULL, "3412000000100000", 0);
495
496 /* Test getting 0th element of $node node regs property */
497 fdt_test_get_value_string(uts, node, "regs", "0", NULL, 0x1234);
498
499 /* Test getting 1st element of $node node regs property */
500 fdt_test_get_value_string(uts, node, "regs", "1", NULL, 0x1000);
Marek Vasut78c060d2022-11-14 22:50:00 +0100501
Marek Vasutb7517b42023-03-02 04:08:28 +0100502 /* Test missing 10th element of $node node clock-names property */
Marek Vasut78c060d2022-11-14 22:50:00 +0100503 ut_assertok(console_record_reset_enable());
Marek Vasutb7517b42023-03-02 04:08:28 +0100504 ut_asserteq(1, run_commandf("fdt get value ften %s clock-names 10", node));
Marek Vasut78c060d2022-11-14 22:50:00 +0100505 ut_assertok(ut_check_console_end(uts));
506
Marek Vasut9f90cad2023-03-02 04:08:29 +0100507 /* Test missing 10th element of $node node regs property */
508 ut_assertok(console_record_reset_enable());
509 ut_asserteq(1, run_commandf("fdt get value ften %s regs 10", node));
510 ut_assertok(ut_check_console_end(uts));
511
Marek Vasutb7517b42023-03-02 04:08:28 +0100512 /* Test getting default element of $node node nonexistent property */
Marek Vasut78c060d2022-11-14 22:50:00 +0100513 ut_assertok(console_record_reset_enable());
Marek Vasutb7517b42023-03-02 04:08:28 +0100514 ut_asserteq(1, run_commandf("fdt get value fnone %s nonexistent", node));
Marek Vasut78c060d2022-11-14 22:50:00 +0100515 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
516 ut_assertok(ut_check_console_end(uts));
517
Marek Vasutb7517b42023-03-02 04:08:28 +0100518 return 0;
519}
520
521static int fdt_test_get_value(struct unit_test_state *uts)
522{
523 char fdt[4096];
524 ulong addr;
525 int ret;
526
527 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
528 addr = map_to_sysmem(fdt);
529 set_working_fdt_addr(addr);
530
531 ret = fdt_test_get_value_common(uts, "/test-node@1234");
532 if (!ret)
533 ret = fdt_test_get_value_common(uts, "testnodealias");
534 if (ret)
535 return ret;
536
Marek Vasut78c060d2022-11-14 22:50:00 +0100537 /* Test getting default element of /nonexistent node */
538 ut_assertok(console_record_reset_enable());
539 ut_asserteq(1, run_command("fdt get value fnode /nonexistent nonexistent", 1));
540 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
541 ut_assertok(ut_check_console_end(uts));
542
Marek Vasutb7517b42023-03-02 04:08:28 +0100543 /* Test getting default element of bad alias */
544 ut_assertok(console_record_reset_enable());
545 ut_asserteq(1, run_command("fdt get value vbadalias badalias nonexistent", 1));
546 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
547 ut_assertok(ut_check_console_end(uts));
548
549 /* Test getting default element of nonexistent alias */
550 ut_assertok(console_record_reset_enable());
551 ut_asserteq(1, run_command("fdt get value vnoalias noalias nonexistent", 1));
552 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
553 ut_assertok(ut_check_console_end(uts));
554
Marek Vasut78c060d2022-11-14 22:50:00 +0100555 return 0;
556}
Marek Vasutfbeca452023-03-02 04:08:26 +0100557FDT_TEST(fdt_test_get_value, UT_TESTF_CONSOLE_REC);
Marek Vasut78c060d2022-11-14 22:50:00 +0100558
Marek Vasut84499932023-03-02 04:08:33 +0100559static int fdt_test_get_name(struct unit_test_state *uts)
560{
561 char fdt[4096];
562 ulong addr;
563
564 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
565 addr = map_to_sysmem(fdt);
566 set_working_fdt_addr(addr);
567
568 /* Test getting name of node 0 in /, which is /aliases node */
569 ut_assertok(console_record_reset_enable());
570 ut_assertok(run_command("fdt get name nzero / 0", 0));
571 ut_asserteq_str("aliases", env_get("nzero"));
572 ut_assertok(ut_check_console_end(uts));
573
574 /* Test getting name of node 1 in /, which is /test-node@1234 node */
575 ut_assertok(console_record_reset_enable());
576 ut_assertok(run_command("fdt get name none / 1", 0));
577 ut_asserteq_str("test-node@1234", env_get("none"));
578 ut_assertok(ut_check_console_end(uts));
579
580 /* Test getting name of node -1 in /, which is /aliases node, same as 0 */
581 ut_assertok(console_record_reset_enable());
582 ut_assertok(run_command("fdt get name nmone / -1", 0));
583 ut_asserteq_str("aliases", env_get("nmone"));
584 ut_assertok(ut_check_console_end(uts));
585
586 /* Test getting name of node 2 in /, which does not exist */
587 ut_assertok(console_record_reset_enable());
588 ut_asserteq(1, run_command("fdt get name ntwo / 2", 1));
589 ut_assert_nextline("libfdt node not found");
590 ut_assertok(ut_check_console_end(uts));
591
592 /* Test getting name of node 0 in /test-node@1234, which is /subnode node */
593 ut_assertok(console_record_reset_enable());
594 ut_assertok(run_command("fdt get name snzero /test-node@1234 0", 0));
595 ut_asserteq_str("subnode", env_get("snzero"));
596 ut_assertok(run_command("fdt get name asnzero testnodealias 0", 0));
597 ut_asserteq_str("subnode", env_get("asnzero"));
598 ut_assertok(ut_check_console_end(uts));
599
600 /* Test getting name of node 1 in /test-node@1234, which does not exist */
601 ut_assertok(console_record_reset_enable());
602 ut_asserteq(1, run_command("fdt get name snone /test-node@1234 1", 1));
603 ut_assert_nextline("libfdt node not found");
604 ut_asserteq(1, run_command("fdt get name asnone testnodealias 1", 1));
605 ut_assert_nextline("libfdt node not found");
606 ut_assertok(ut_check_console_end(uts));
607
608 /* Test getting name of node -1 in /test-node@1234, which is /subnode node, same as 0 */
609 ut_assertok(console_record_reset_enable());
610 ut_assertok(run_command("fdt get name snmone /test-node@1234 -1", 0));
611 ut_asserteq_str("subnode", env_get("snmone"));
612 ut_assertok(run_command("fdt get name asnmone testnodealias -1", 0));
613 ut_asserteq_str("subnode", env_get("asnmone"));
614 ut_assertok(ut_check_console_end(uts));
615
616 /* Test getting name of nonexistent node */
617 ut_assertok(console_record_reset_enable());
618 ut_asserteq(1, run_command("fdt get name nonode /nonexistent 0", 1));
619 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
620 ut_assertok(ut_check_console_end(uts));
621
622 /* Test getting name of bad alias */
623 ut_assertok(console_record_reset_enable());
624 ut_asserteq(1, run_command("fdt get name vbadalias badalias 0", 1));
625 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
626 ut_assertok(ut_check_console_end(uts));
627
628 /* Test getting name of nonexistent alias */
629 ut_assertok(console_record_reset_enable());
630 ut_asserteq(1, run_command("fdt get name vnoalias noalias 0", 1));
631 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
632 ut_assertok(ut_check_console_end(uts));
633
634 return 0;
635}
636FDT_TEST(fdt_test_get_name, UT_TESTF_CONSOLE_REC);
637
Marek Vasutcadef862023-03-02 04:08:34 +0100638static int fdt_test_get_addr_common(struct unit_test_state *uts, char *fdt,
639 const char *path, const char *prop)
640{
641 unsigned int offset;
642 int path_offset;
643 void *prop_ptr;
644 int len = 0;
645
646 path_offset = fdt_path_offset(fdt, path);
647 ut_assert(path_offset >= 0);
648 prop_ptr = (void *)fdt_getprop(fdt, path_offset, prop, &len);
649 ut_assertnonnull(prop_ptr);
650 offset = (char *)prop_ptr - fdt;
651
652 ut_assertok(console_record_reset_enable());
653 ut_assertok(run_commandf("fdt get addr pstr %s %s", path, prop));
654 ut_asserteq((ulong)map_sysmem(env_get_hex("fdtaddr", 0x1234), 0),
655 (ulong)(map_sysmem(env_get_hex("pstr", 0x1234), 0) - offset));
656 ut_assertok(ut_check_console_end(uts));
657
658 return 0;
659}
660
661static int fdt_test_get_addr(struct unit_test_state *uts)
662{
663 char fdt[4096];
664 ulong addr;
665
666 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
667 addr = map_to_sysmem(fdt);
668 set_working_fdt_addr(addr);
669
670 /* Test getting address of root node / string property "compatible" */
671 fdt_test_get_addr_common(uts, fdt, "/", "compatible");
672
673 /* Test getting address of node /test-node@1234 stringlist property "clock-names" */
674 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-names");
675 fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-names");
676
677 /* Test getting address of node /test-node@1234 u32 property "clock-frequency" */
678 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-frequency");
679 fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-frequency");
680
681 /* Test getting address of node /test-node@1234 empty property "u-boot,empty-property" */
682 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "u-boot,empty-property");
683 fdt_test_get_addr_common(uts, fdt, "testnodealias", "u-boot,empty-property");
684
685 /* Test getting address of node /test-node@1234 array property "regs" */
686 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "regs");
687 fdt_test_get_addr_common(uts, fdt, "testnodealias", "regs");
688
689 /* Test getting address of node /test-node@1234/subnode non-existent property "noprop" */
690 ut_assertok(console_record_reset_enable());
691 ut_asserteq(1, run_command("fdt get addr pnoprop /test-node@1234/subnode noprop", 1));
692 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
693 ut_assertok(ut_check_console_end(uts));
694
695 /* Test getting address of non-existent node /test-node@1234/nonode@1 property "noprop" */
696 ut_assertok(console_record_reset_enable());
697 ut_asserteq(1, run_command("fdt get addr pnonode /test-node@1234/nonode@1 noprop", 1));
698 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
699 ut_assertok(ut_check_console_end(uts));
700
701 return 0;
702}
703FDT_TEST(fdt_test_get_addr, UT_TESTF_CONSOLE_REC);
704
Marek Vasutce1b0482023-03-02 04:08:35 +0100705static int fdt_test_get_size_common(struct unit_test_state *uts,
706 const char *path, const char *prop,
707 const unsigned int val)
708{
709 ut_assertok(console_record_reset_enable());
710 if (prop) {
711 ut_assertok(run_commandf("fdt get size sstr %s %s", path, prop));
712 } else {
713 ut_assertok(run_commandf("fdt get size sstr %s", path));
714 }
715 ut_asserteq(val, env_get_hex("sstr", 0x1234));
716 ut_assertok(ut_check_console_end(uts));
717
718 return 0;
719}
720
721static int fdt_test_get_size(struct unit_test_state *uts)
722{
723 char fdt[4096];
724 ulong addr;
725
726 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
727 addr = map_to_sysmem(fdt);
728 set_working_fdt_addr(addr);
729
730 /* Test getting size of root node / string property "compatible" */
731 fdt_test_get_size_common(uts, "/", "compatible", 16);
732
733 /* Test getting size of node /test-node@1234 stringlist property "clock-names" */
734 fdt_test_get_size_common(uts, "/test-node@1234", "clock-names", 26);
735 fdt_test_get_size_common(uts, "testnodealias", "clock-names", 26);
736
737 /* Test getting size of node /test-node@1234 u32 property "clock-frequency" */
738 fdt_test_get_size_common(uts, "/test-node@1234", "clock-frequency", 4);
739 fdt_test_get_size_common(uts, "testnodealias", "clock-frequency", 4);
740
741 /* Test getting size of node /test-node@1234 empty property "u-boot,empty-property" */
742 fdt_test_get_size_common(uts, "/test-node@1234", "u-boot,empty-property", 0);
743 fdt_test_get_size_common(uts, "testnodealias", "u-boot,empty-property", 0);
744
745 /* Test getting size of node /test-node@1234 array property "regs" */
746 fdt_test_get_size_common(uts, "/test-node@1234", "regs", 8);
747 fdt_test_get_size_common(uts, "testnodealias", "regs", 8);
748
749 /* Test getting node count of node / */
750 fdt_test_get_size_common(uts, "/", NULL, 2);
751
752 /* Test getting node count of node /test-node@1234/subnode */
753 fdt_test_get_size_common(uts, "/test-node@1234/subnode", NULL, 0);
754 fdt_test_get_size_common(uts, "subnodealias", NULL, 0);
755
756 /* Test getting size of node /test-node@1234/subnode non-existent property "noprop" */
757 ut_assertok(console_record_reset_enable());
758 ut_asserteq(1, run_command("fdt get size pnoprop /test-node@1234/subnode noprop", 1));
759 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
760 ut_asserteq(1, run_command("fdt get size pnoprop subnodealias noprop", 1));
761 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
762 ut_assertok(ut_check_console_end(uts));
763
764 /* Test getting size of non-existent node /test-node@1234/nonode@1 property "noprop" */
765 ut_assertok(console_record_reset_enable());
766 ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1 noprop", 1));
767 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
768 ut_assertok(ut_check_console_end(uts));
769
770 /* Test getting node count of non-existent node /test-node@1234/nonode@1 */
771 ut_assertok(console_record_reset_enable());
772 ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1", 1));
773 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
774 ut_assertok(ut_check_console_end(uts));
775
776 /* Test getting node count of bad alias badalias */
777 ut_assertok(console_record_reset_enable());
778 ut_asserteq(1, run_command("fdt get size pnonode badalias noprop", 1));
779 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
780 ut_assertok(ut_check_console_end(uts));
781
782 /* Test getting node count of non-existent alias noalias */
783 ut_assertok(console_record_reset_enable());
784 ut_asserteq(1, run_command("fdt get size pnonode noalias", 1));
785 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
786 ut_assertok(ut_check_console_end(uts));
787
788 return 0;
789}
790FDT_TEST(fdt_test_get_size, UT_TESTF_CONSOLE_REC);
791
Marek Vasutf180de42023-03-02 04:08:36 +0100792static int fdt_test_set_single(struct unit_test_state *uts,
793 const char *path, const char *prop,
794 const char *sval, int ival, bool integer)
795{
796 /*
797 * Set single element string/integer/<empty> property into DT, that is:
798 * => fdt set /path property string
799 * => fdt set /path property integer
800 * => fdt set /path property
801 */
802 ut_assertok(console_record_reset_enable());
Marek Vasutbc8c1402023-03-10 04:33:14 +0100803 if (sval)
Marek Vasutf180de42023-03-02 04:08:36 +0100804 ut_assertok(run_commandf("fdt set %s %s %s", path, prop, sval));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100805 else if (integer)
Marek Vasutf180de42023-03-02 04:08:36 +0100806 ut_assertok(run_commandf("fdt set %s %s <%d>", path, prop, ival));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100807 else
Marek Vasutf180de42023-03-02 04:08:36 +0100808 ut_assertok(run_commandf("fdt set %s %s", path, prop));
Marek Vasutf180de42023-03-02 04:08:36 +0100809
810 /* Validate the property is present and has correct value. */
811 ut_assertok(run_commandf("fdt get value svar %s %s", path, prop));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100812 if (sval)
Marek Vasutf180de42023-03-02 04:08:36 +0100813 ut_asserteq_str(sval, env_get("svar"));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100814 else if (integer)
Marek Vasutf180de42023-03-02 04:08:36 +0100815 ut_asserteq(ival, env_get_hex("svar", 0x1234));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100816 else
Marek Vasutf180de42023-03-02 04:08:36 +0100817 ut_assertnull(env_get("svar"));
Marek Vasutf180de42023-03-02 04:08:36 +0100818 ut_assertok(ut_check_console_end(uts));
819
820 return 0;
821}
822
823static int fdt_test_set_multi(struct unit_test_state *uts,
824 const char *path, const char *prop,
825 const char *sval1, const char *sval2,
826 int ival1, int ival2)
827{
828 /*
829 * Set multi element string/integer array property in DT, that is:
830 * => fdt set /path property <string1 string2>
831 * => fdt set /path property <integer1 integer2>
832 *
833 * The set is done twice in here deliberately, The first set adds
834 * the property with an extra trailing element in its array to make
835 * the array longer, the second set is the expected final content of
836 * the array property. The longer array is used to verify that the
837 * new array is correctly sized and read past the new array length
838 * triggers failure.
839 */
840 ut_assertok(console_record_reset_enable());
841 if (sval1 && sval2) {
842 ut_assertok(run_commandf("fdt set %s %s %s %s end", path, prop, sval1, sval2));
843 ut_assertok(run_commandf("fdt set %s %s %s %s", path, prop, sval1, sval2));
844 } else {
845 ut_assertok(run_commandf("fdt set %s %s <%d %d 10>", path, prop, ival1, ival2));
846 ut_assertok(run_commandf("fdt set %s %s <%d %d>", path, prop, ival1, ival2));
847 }
848
849 /*
850 * Validate the property is present and has correct value.
851 *
852 * The "end/10" above and "svarn" below is used to validate that
853 * previous 'fdt set' to longer array does not polute newly set
854 * shorter array.
855 */
856 ut_assertok(run_commandf("fdt get value svar1 %s %s 0", path, prop));
857 ut_assertok(run_commandf("fdt get value svar2 %s %s 1", path, prop));
858 ut_asserteq(1, run_commandf("fdt get value svarn %s %s 2", path, prop));
859 if (sval1 && sval2) {
860 ut_asserteq_str(sval1, env_get("svar1"));
861 ut_asserteq_str(sval2, env_get("svar2"));
862 ut_assertnull(env_get("svarn"));
863 } else {
864 ut_asserteq(ival1, env_get_hex("svar1", 0x1234));
865 ut_asserteq(ival2, env_get_hex("svar2", 0x1234));
866 ut_assertnull(env_get("svarn"));
867 }
868 ut_assertok(ut_check_console_end(uts));
869
870 return 0;
871}
872
873static int fdt_test_set_node(struct unit_test_state *uts,
874 const char *path, const char *prop)
875{
876 fdt_test_set_single(uts, path, prop, "new", 0, false);
877 fdt_test_set_single(uts, path, prop, "rewrite", 0, false);
878 fdt_test_set_single(uts, path, prop, NULL, 42, true);
879 fdt_test_set_single(uts, path, prop, NULL, 0, false);
880 fdt_test_set_multi(uts, path, prop, NULL, NULL, 42, 1701);
881 fdt_test_set_multi(uts, path, prop, NULL, NULL, 74656, 9);
882 fdt_test_set_multi(uts, path, prop, "42", "1701", 0, 0);
883 fdt_test_set_multi(uts, path, prop, "74656", "9", 0, 0);
884
885 return 0;
886}
887
888static int fdt_test_set(struct unit_test_state *uts)
889{
890 char fdt[8192];
891 ulong addr;
892
893 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
894 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
895 addr = map_to_sysmem(fdt);
896 set_working_fdt_addr(addr);
897
898 /* Test setting of root node / existing property "compatible" */
899 fdt_test_set_node(uts, "/", "compatible");
900
901 /* Test setting of root node / new property "newproperty" */
902 fdt_test_set_node(uts, "/", "newproperty");
903
904 /* Test setting of subnode existing property "compatible" */
905 fdt_test_set_node(uts, "/test-node@1234/subnode", "compatible");
906 fdt_test_set_node(uts, "subnodealias", "compatible");
907
908 /* Test setting of subnode new property "newproperty" */
909 fdt_test_set_node(uts, "/test-node@1234/subnode", "newproperty");
910 fdt_test_set_node(uts, "subnodealias", "newproperty");
911
912 /* Test setting property of non-existent node */
913 ut_assertok(console_record_reset_enable());
914 ut_asserteq(1, run_command("fdt set /no-node noprop", 1));
915 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
916 ut_assertok(ut_check_console_end(uts));
917
918 /* Test setting property of non-existent alias */
919 ut_assertok(console_record_reset_enable());
920 ut_asserteq(1, run_command("fdt set noalias noprop", 1));
921 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
922 ut_assertok(ut_check_console_end(uts));
923
924 /* Test setting property of bad alias */
925 ut_assertok(console_record_reset_enable());
926 ut_asserteq(1, run_command("fdt set badalias noprop", 1));
927 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
928 ut_assertok(ut_check_console_end(uts));
929
930 return 0;
931}
932FDT_TEST(fdt_test_set, UT_TESTF_CONSOLE_REC);
933
Marek Vasute4d26c92023-03-02 04:08:37 +0100934static int fdt_test_mknode(struct unit_test_state *uts)
935{
936 char fdt[8192];
937 ulong addr;
938
939 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
940 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
941 addr = map_to_sysmem(fdt);
942 set_working_fdt_addr(addr);
943
944 /* Test creation of new node in / */
945 ut_assertok(console_record_reset_enable());
946 ut_assertok(run_commandf("fdt mknode / newnode"));
947 ut_assertok(run_commandf("fdt list /newnode"));
948 ut_assert_nextline("newnode {");
949 ut_assert_nextline("};");
950 ut_assertok(ut_check_console_end(uts));
951
952 /* Test creation of new node in /test-node@1234 */
953 ut_assertok(console_record_reset_enable());
954 ut_assertok(run_commandf("fdt mknode /test-node@1234 newsubnode"));
955 ut_assertok(run_commandf("fdt list /test-node@1234/newsubnode"));
956 ut_assert_nextline("newsubnode {");
957 ut_assert_nextline("};");
958 ut_assertok(ut_check_console_end(uts));
959
960 /* Test creation of new node in /test-node@1234 by alias */
961 ut_assertok(console_record_reset_enable());
962 ut_assertok(run_commandf("fdt mknode testnodealias newersubnode"));
963 ut_assertok(run_commandf("fdt list testnodealias/newersubnode"));
964 ut_assert_nextline("newersubnode {");
965 ut_assert_nextline("};");
966 ut_assertok(ut_check_console_end(uts));
967
968 /* Test creation of new node in /test-node@1234 over existing node */
969 ut_assertok(console_record_reset_enable());
970 ut_asserteq(1, run_commandf("fdt mknode testnodealias newsubnode"));
971 ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
972 ut_assertok(ut_check_console_end(uts));
973
974 /* Test creation of new node in /test-node@1234 by alias over existing node */
975 ut_assertok(console_record_reset_enable());
976 ut_asserteq(1, run_commandf("fdt mknode testnodealias newersubnode"));
977 ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
978 ut_assertok(ut_check_console_end(uts));
979
980 /* Test creation of new node in non-existent node */
981 ut_assertok(console_record_reset_enable());
982 ut_asserteq(1, run_commandf("fdt mknode /no-node newnosubnode"));
983 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
984 ut_assertok(ut_check_console_end(uts));
985
986 /* Test creation of new node in non-existent alias */
987 ut_assertok(console_record_reset_enable());
988 ut_asserteq(1, run_commandf("fdt mknode noalias newfailsubnode"));
989 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
990 ut_assertok(ut_check_console_end(uts));
991
992 /* Test creation of new node in bad alias */
993 ut_assertok(console_record_reset_enable());
994 ut_asserteq(1, run_commandf("fdt mknode badalias newbadsubnode"));
995 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
996 ut_assertok(ut_check_console_end(uts));
997
998 return 0;
999}
1000FDT_TEST(fdt_test_mknode, UT_TESTF_CONSOLE_REC);
1001
Marek Vasutc3e350f2023-03-02 04:08:38 +01001002static int fdt_test_rm(struct unit_test_state *uts)
1003{
1004 char fdt[4096];
1005 ulong addr;
1006
1007 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
1008 addr = map_to_sysmem(fdt);
1009 set_working_fdt_addr(addr);
1010
1011 /* Test removal of property in root node / */
1012 ut_assertok(console_record_reset_enable());
1013 ut_assertok(run_commandf("fdt print / compatible"));
1014 ut_assert_nextline("compatible = \"u-boot,fdt-test\"");
1015 ut_assertok(run_commandf("fdt rm / compatible"));
1016 ut_asserteq(1, run_commandf("fdt print / compatible"));
1017 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1018 ut_assertok(ut_check_console_end(uts));
1019
1020 /* Test removal of property clock-names in subnode /test-node@1234 */
1021 ut_assertok(console_record_reset_enable());
1022 ut_assertok(run_commandf("fdt print /test-node@1234 clock-names"));
1023 ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
1024 ut_assertok(run_commandf("fdt rm /test-node@1234 clock-names"));
1025 ut_asserteq(1, run_commandf("fdt print /test-node@1234 clock-names"));
1026 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1027 ut_assertok(ut_check_console_end(uts));
1028
1029 /* Test removal of property u-boot,empty-property in subnode /test-node@1234 by alias */
1030 ut_assertok(console_record_reset_enable());
1031 ut_assertok(run_commandf("fdt print testnodealias u-boot,empty-property"));
1032 ut_assert_nextline("testnodealias u-boot,empty-property");
1033 ut_assertok(run_commandf("fdt rm testnodealias u-boot,empty-property"));
1034 ut_asserteq(1, run_commandf("fdt print testnodealias u-boot,empty-property"));
1035 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1036 ut_assertok(ut_check_console_end(uts));
1037
1038 /* Test removal of non-existent property noprop in subnode /test-node@1234 */
1039 ut_assertok(console_record_reset_enable());
1040 ut_asserteq(1, run_commandf("fdt rm /test-node@1234 noprop"));
1041 ut_assert_nextline("libfdt fdt_delprop(): FDT_ERR_NOTFOUND");
1042 ut_assertok(ut_check_console_end(uts));
1043
1044 /* Test removal of non-existent node /no-node@5678 */
1045 ut_assertok(console_record_reset_enable());
1046 ut_asserteq(1, run_commandf("fdt rm /no-node@5678"));
1047 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1048 ut_assertok(ut_check_console_end(uts));
1049
1050 /* Test removal of subnode /test-node@1234/subnode by alias */
1051 ut_assertok(console_record_reset_enable());
1052 ut_assertok(run_commandf("fdt rm subnodealias"));
1053 ut_asserteq(1, run_commandf("fdt print /test-node@1234/subnode"));
1054 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1055 ut_assertok(ut_check_console_end(uts));
1056
1057 /* Test removal of node by non-existent alias */
1058 ut_assertok(console_record_reset_enable());
1059 ut_asserteq(1, run_commandf("fdt rm noalias"));
1060 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
1061 ut_assertok(ut_check_console_end(uts));
1062
1063 /* Test removal of node by bad alias */
1064 ut_assertok(console_record_reset_enable());
1065 ut_asserteq(1, run_commandf("fdt rm noalias"));
1066 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
1067 ut_assertok(ut_check_console_end(uts));
1068
1069 /* Test removal of node /test-node@1234 */
1070 ut_assertok(console_record_reset_enable());
1071 ut_assertok(run_commandf("fdt rm /test-node@1234"));
1072 ut_asserteq(1, run_commandf("fdt print /test-node@1234"));
1073 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1074 ut_assertok(ut_check_console_end(uts));
1075
1076 /* Test removal of node / */
1077 ut_assertok(console_record_reset_enable());
1078 ut_assertok(run_commandf("fdt rm /"));
1079 ut_asserteq(1, run_commandf("fdt print /"));
1080 ut_assertok(ut_check_console_end(uts));
1081
1082 return 0;
1083}
1084FDT_TEST(fdt_test_rm, UT_TESTF_CONSOLE_REC);
1085
Marek Vasut2081b1d2023-03-02 04:08:40 +01001086static int fdt_test_bootcpu(struct unit_test_state *uts)
1087{
1088 char fdt[256];
1089 ulong addr;
1090 int i;
1091
1092 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1093 addr = map_to_sysmem(fdt);
1094 set_working_fdt_addr(addr);
1095
1096 /* Test getting default bootcpu entry */
1097 ut_assertok(console_record_reset_enable());
1098 ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
1099 ut_asserteq(0, env_get_ulong("bootcpu", 10, 0x1234));
1100 ut_assertok(ut_check_console_end(uts));
1101
1102 /* Test setting and getting new bootcpu entry, twice, to test overwrite */
1103 for (i = 42; i <= 43; i++) {
1104 ut_assertok(console_record_reset_enable());
1105 ut_assertok(run_commandf("fdt bootcpu %d", i));
1106 ut_assertok(ut_check_console_end(uts));
1107
1108 /* Test getting new bootcpu entry */
1109 ut_assertok(console_record_reset_enable());
1110 ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
1111 ut_asserteq(i, env_get_ulong("bootcpu", 10, 0x1234));
1112 ut_assertok(ut_check_console_end(uts));
1113 }
1114
1115 return 0;
1116}
1117FDT_TEST(fdt_test_bootcpu, UT_TESTF_CONSOLE_REC);
1118
Marek Vasut06c13ed2023-03-02 04:08:39 +01001119static int fdt_test_header_get(struct unit_test_state *uts,
1120 const char *field, const unsigned long val)
1121{
1122 /* Test getting valid header entry */
1123 ut_assertok(console_record_reset_enable());
1124 ut_assertok(run_commandf("fdt header get fvar %s", field));
1125 ut_asserteq(val, env_get_hex("fvar", 0x1234));
1126 ut_assertok(ut_check_console_end(uts));
1127
1128 /* Test getting malformed header entry */
1129 ut_assertok(console_record_reset_enable());
1130 ut_asserteq(1, run_commandf("fdt header get fvar typo%stypo", field));
1131 ut_assertok(ut_check_console_end(uts));
1132
1133 return 0;
1134}
1135
1136static int fdt_test_header(struct unit_test_state *uts)
1137{
1138 char fdt[256];
1139 ulong addr;
1140
1141 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1142 addr = map_to_sysmem(fdt);
1143 set_working_fdt_addr(addr);
1144
1145 /* Test header print */
1146 ut_assertok(console_record_reset_enable());
1147 ut_assertok(run_commandf("fdt header"));
1148 ut_assert_nextline("magic:\t\t\t0x%x", fdt_magic(fdt));
1149 ut_assert_nextline("totalsize:\t\t0x%x (%d)", fdt_totalsize(fdt), fdt_totalsize(fdt));
1150 ut_assert_nextline("off_dt_struct:\t\t0x%x", fdt_off_dt_struct(fdt));
1151 ut_assert_nextline("off_dt_strings:\t\t0x%x", fdt_off_dt_strings(fdt));
1152 ut_assert_nextline("off_mem_rsvmap:\t\t0x%x", fdt_off_mem_rsvmap(fdt));
1153 ut_assert_nextline("version:\t\t%d", fdt_version(fdt));
1154 ut_assert_nextline("last_comp_version:\t%d", fdt_last_comp_version(fdt));
1155 ut_assert_nextline("boot_cpuid_phys:\t0x%x", fdt_boot_cpuid_phys(fdt));
1156 ut_assert_nextline("size_dt_strings:\t0x%x", fdt_size_dt_strings(fdt));
1157 ut_assert_nextline("size_dt_struct:\t\t0x%x", fdt_size_dt_struct(fdt));
1158 ut_assert_nextline("number mem_rsv:\t\t0x%x", fdt_num_mem_rsv(fdt));
1159 ut_assert_nextline_empty();
1160 ut_assertok(ut_check_console_end(uts));
1161
1162 /* Test header get */
1163 fdt_test_header_get(uts, "magic", fdt_magic(fdt));
1164 fdt_test_header_get(uts, "totalsize", fdt_totalsize(fdt));
1165 fdt_test_header_get(uts, "off_dt_struct", fdt_off_dt_struct(fdt));
1166 fdt_test_header_get(uts, "off_dt_strings", fdt_off_dt_strings(fdt));
1167 fdt_test_header_get(uts, "off_mem_rsvmap", fdt_off_mem_rsvmap(fdt));
1168 fdt_test_header_get(uts, "version", fdt_version(fdt));
1169 fdt_test_header_get(uts, "last_comp_version", fdt_last_comp_version(fdt));
1170 fdt_test_header_get(uts, "boot_cpuid_phys", fdt_boot_cpuid_phys(fdt));
1171 fdt_test_header_get(uts, "size_dt_strings", fdt_size_dt_strings(fdt));
1172 fdt_test_header_get(uts, "size_dt_struct", fdt_size_dt_struct(fdt));
1173
1174 return 0;
1175}
1176FDT_TEST(fdt_test_header, UT_TESTF_CONSOLE_REC);
1177
Marek Vasut65b6f462023-03-02 04:08:41 +01001178static int fdt_test_memory_cells(struct unit_test_state *uts,
1179 const unsigned int cells)
1180{
1181 unsigned char *pada, *pads;
1182 unsigned char *seta, *sets;
1183 char fdt[8192];
1184 const int size = sizeof(fdt);
1185 fdt32_t *regs;
1186 ulong addr;
1187 char *spc;
1188 int i;
1189
1190 /* Create DT with node /memory { regs = <0x100 0x200>; } and #*cells */
1191 ut_assertnonnull(regs = calloc(2 * cells, sizeof(*regs)));
1192 ut_assertnonnull(pada = calloc(12, cells));
1193 ut_assertnonnull(pads = calloc(12, cells));
1194 ut_assertnonnull(seta = calloc(12, cells));
1195 ut_assertnonnull(sets = calloc(12, cells));
1196 for (i = cells; i >= 1; i--) {
1197 regs[cells - 1] = cpu_to_fdt32(i * 0x10000);
1198 regs[(cells * 2) - 1] = cpu_to_fdt32(~i);
1199 snprintf(seta + (8 * (cells - i)), 9, "%08x", i * 0x10000);
1200 snprintf(sets + (8 * (cells - i)), 9, "%08x", ~i);
1201 spc = (i != 1) ? " " : "";
1202 snprintf(pada + (11 * (cells - i)), 12, "0x%08x%s", i * 0x10000, spc);
1203 snprintf(pads + (11 * (cells - i)), 12, "0x%08x%s", ~i, spc);
1204 }
1205
1206 ut_assertok(fdt_create(fdt, size));
1207 ut_assertok(fdt_finish_reservemap(fdt));
1208 ut_assert(fdt_begin_node(fdt, "") >= 0);
1209 ut_assertok(fdt_property_u32(fdt, "#address-cells", cells));
1210 ut_assertok(fdt_property_u32(fdt, "#size-cells", cells));
1211 ut_assert(fdt_begin_node(fdt, "memory") >= 0);
1212 ut_assertok(fdt_property_string(fdt, "device_type", "memory"));
1213 ut_assertok(fdt_property(fdt, "reg", &regs, cells * 2));
1214 ut_assertok(fdt_end_node(fdt));
1215 ut_assertok(fdt_end_node(fdt));
1216 ut_assertok(fdt_finish(fdt));
1217 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1218 addr = map_to_sysmem(fdt);
1219 set_working_fdt_addr(addr);
1220
1221 /* Test updating the memory node */
1222 ut_assertok(console_record_reset_enable());
1223 ut_assertok(run_commandf("fdt memory 0x%s 0x%s", seta, sets));
1224 ut_assertok(run_commandf("fdt print /memory"));
1225 ut_assert_nextline("memory {");
1226 ut_assert_nextline("\tdevice_type = \"memory\";");
1227 ut_assert_nextline("\treg = <%s %s>;", pada, pads);
1228 ut_assert_nextline("};");
1229 ut_assertok(ut_check_console_end(uts));
1230
1231 free(sets);
1232 free(seta);
1233 free(pads);
1234 free(pada);
1235 free(regs);
1236
1237 return 0;
1238}
1239
1240static int fdt_test_memory(struct unit_test_state *uts)
1241{
1242 /*
1243 * Test memory fixup for 32 and 64 bit systems, anything bigger is
1244 * so far unsupported and fails because of simple_stroull() being
1245 * 64bit tops in the 'fdt memory' command implementation.
1246 */
1247 fdt_test_memory_cells(uts, 1);
1248 fdt_test_memory_cells(uts, 2);
1249
1250 /*
1251 * The 'fdt memory' command is limited to /memory node, it does
1252 * not support any other valid DT memory node format, which is
1253 * either one or multiple /memory@adresss nodes. Therefore, this
1254 * DT variant is not tested here.
1255 */
1256
1257 return 0;
1258}
1259FDT_TEST(fdt_test_memory, UT_TESTF_CONSOLE_REC);
1260
Marek Vasutcd162df2023-03-02 04:08:42 +01001261static int fdt_test_rsvmem(struct unit_test_state *uts)
1262{
1263 char fdt[8192];
1264 ulong addr;
1265
1266 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1267 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1268 fdt_add_mem_rsv(fdt, 0x42, 0x1701);
1269 fdt_add_mem_rsv(fdt, 0x74656, 0x9);
1270 addr = map_to_sysmem(fdt);
1271 set_working_fdt_addr(addr);
1272
1273 /* Test default reserved memory node presence */
1274 ut_assertok(console_record_reset_enable());
1275 ut_assertok(run_commandf("fdt rsvmem print"));
1276 ut_assert_nextline("index\t\t start\t\t size");
1277 ut_assert_nextline("------------------------------------------------");
1278 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
1279 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
1280 ut_assertok(ut_check_console_end(uts));
1281
1282 /* Test add new reserved memory node */
1283 ut_assertok(console_record_reset_enable());
1284 ut_assertok(run_commandf("fdt rsvmem add 0x1234 0x5678"));
1285 ut_assertok(run_commandf("fdt rsvmem print"));
1286 ut_assert_nextline("index\t\t start\t\t size");
1287 ut_assert_nextline("------------------------------------------------");
1288 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
1289 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
1290 ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x1234, 0x5678);
1291 ut_assertok(ut_check_console_end(uts));
1292
1293 /* Test delete reserved memory node */
1294 ut_assertok(console_record_reset_enable());
1295 ut_assertok(run_commandf("fdt rsvmem delete 0"));
1296 ut_assertok(run_commandf("fdt rsvmem print"));
1297 ut_assert_nextline("index\t\t start\t\t size");
1298 ut_assert_nextline("------------------------------------------------");
1299 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
1300 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
1301 ut_assertok(ut_check_console_end(uts));
1302
1303 /* Test re-add new reserved memory node */
1304 ut_assertok(console_record_reset_enable());
1305 ut_assertok(run_commandf("fdt rsvmem add 0x42 0x1701"));
1306 ut_assertok(run_commandf("fdt rsvmem print"));
1307 ut_assert_nextline("index\t\t start\t\t size");
1308 ut_assert_nextline("------------------------------------------------");
1309 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
1310 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
1311 ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x42, 0x1701);
1312 ut_assertok(ut_check_console_end(uts));
1313
1314 /* Test delete nonexistent reserved memory node */
1315 ut_assertok(console_record_reset_enable());
1316 ut_asserteq(1, run_commandf("fdt rsvmem delete 10"));
1317 ut_assert_nextline("libfdt fdt_del_mem_rsv(): FDT_ERR_NOTFOUND");
1318 ut_assertok(ut_check_console_end(uts));
1319
1320 return 0;
1321}
1322FDT_TEST(fdt_test_rsvmem, UT_TESTF_CONSOLE_REC);
1323
Marek Vasut8d450da2023-03-02 04:08:43 +01001324static int fdt_test_chosen(struct unit_test_state *uts)
1325{
1326 const char *env_bootargs = env_get("bootargs");
1327 char fdt[8192];
1328 ulong addr;
1329
1330 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1331 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1332 addr = map_to_sysmem(fdt);
1333 set_working_fdt_addr(addr);
1334
1335 /* Test default chosen node presence, fail as there is no /chosen node */
1336 ut_assertok(console_record_reset_enable());
1337 ut_asserteq(1, run_commandf("fdt print /chosen"));
1338 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1339 ut_assertok(ut_check_console_end(uts));
1340
1341 /* Test add new chosen node without initrd */
1342 ut_assertok(console_record_reset_enable());
1343 ut_assertok(run_commandf("fdt chosen"));
1344 ut_assertok(run_commandf("fdt print /chosen"));
1345 ut_assert_nextline("chosen {");
1346 ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
1347 if (env_bootargs)
1348 ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
Tim Harvey009d1562024-06-18 14:06:09 -07001349 if (IS_ENABLED(CONFIG_DM_RNG) &&
1350 !IS_ENABLED(CONFIG_MEASURED_BOOT) &&
1351 !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT))
1352 ut_assert_nextlinen("\tkaslr-seed = ");
Marek Vasut8d450da2023-03-02 04:08:43 +01001353 ut_assert_nextline("};");
1354 ut_assertok(ut_check_console_end(uts));
1355
1356 /* Test add new chosen node with initrd */
1357 ut_assertok(console_record_reset_enable());
1358 ut_assertok(run_commandf("fdt chosen 0x1234 0x5678"));
1359 ut_assertok(run_commandf("fdt print /chosen"));
1360 ut_assert_nextline("chosen {");
1361 ut_assert_nextline("\tlinux,initrd-end = <0x%08x 0x%08x>;",
1362 upper_32_bits(0x1234 + 0x5678 - 1),
1363 lower_32_bits(0x1234 + 0x5678 - 1));
1364 ut_assert_nextline("\tlinux,initrd-start = <0x%08x 0x%08x>;",
1365 upper_32_bits(0x1234), lower_32_bits(0x1234));
1366 ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
1367 if (env_bootargs)
1368 ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
Tim Harvey009d1562024-06-18 14:06:09 -07001369 if (IS_ENABLED(CONFIG_DM_RNG) &&
1370 !IS_ENABLED(CONFIG_MEASURED_BOOT) &&
1371 !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT))
1372 ut_assert_nextlinen("\tkaslr-seed = ");
Marek Vasut8d450da2023-03-02 04:08:43 +01001373 ut_assert_nextline("};");
1374 ut_assertok(ut_check_console_end(uts));
1375
1376 return 0;
1377}
1378FDT_TEST(fdt_test_chosen, UT_TESTF_CONSOLE_REC);
1379
Marek Vasutdc0ccea2023-03-02 04:08:44 +01001380static int fdt_test_apply(struct unit_test_state *uts)
1381{
1382 char fdt[8192], fdto[8192];
1383 ulong addr, addro;
1384
1385 /* Create base DT with __symbols__ node */
1386 ut_assertok(fdt_create(fdt, sizeof(fdt)));
1387 ut_assertok(fdt_finish_reservemap(fdt));
1388 ut_assert(fdt_begin_node(fdt, "") >= 0);
1389 ut_assert(fdt_begin_node(fdt, "__symbols__") >= 0);
1390 ut_assertok(fdt_end_node(fdt));
1391 ut_assertok(fdt_end_node(fdt));
1392 ut_assertok(fdt_finish(fdt));
1393 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1394 addr = map_to_sysmem(fdt);
1395 set_working_fdt_addr(addr);
1396
1397 /* Create DTO which adds single property to root node / */
1398 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1399 ut_assertok(fdt_finish_reservemap(fdto));
1400 ut_assert(fdt_begin_node(fdto, "") >= 0);
1401 ut_assert(fdt_begin_node(fdto, "fragment") >= 0);
1402 ut_assertok(fdt_property_string(fdto, "target-path", "/"));
1403 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1404 ut_assertok(fdt_property_string(fdto, "newstring", "newvalue"));
1405 ut_assertok(fdt_end_node(fdto));
1406 ut_assertok(fdt_end_node(fdto));
1407 ut_assertok(fdt_finish(fdto));
1408 addro = map_to_sysmem(fdto);
1409
1410 /* Test default DT print */
1411 ut_assertok(console_record_reset_enable());
1412 ut_assertok(run_commandf("fdt print /"));
1413 ut_assert_nextline("/ {");
1414 ut_assert_nextline("\t__symbols__ {");
1415 ut_assert_nextline("\t};");
1416 ut_assert_nextline("};");
1417 ut_assertok(ut_check_console_end(uts));
1418
1419 /* Test simple DTO application */
1420 ut_assertok(console_record_reset_enable());
Evgeny Bachinine604bc02023-03-20 11:23:13 +03001421 ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
Marek Vasutdc0ccea2023-03-02 04:08:44 +01001422 ut_assertok(run_commandf("fdt print /"));
1423 ut_assert_nextline("/ {");
1424 ut_assert_nextline("\tnewstring = \"newvalue\";");
1425 ut_assert_nextline("\t__symbols__ {");
1426 ut_assert_nextline("\t};");
1427 ut_assert_nextline("};");
1428 ut_assertok(ut_check_console_end(uts));
1429
1430 /*
1431 * Create complex DTO which:
1432 * - modifies newstring property in root node /
1433 * - adds new properties to root node /
1434 * - adds new subnode with properties to root node /
1435 * - adds phandle to the subnode and therefore __symbols__ node
1436 */
1437 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1438 ut_assertok(fdt_finish_reservemap(fdto));
1439 ut_assert(fdt_begin_node(fdto, "") >= 0);
1440 ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
1441 ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
1442
1443 ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
1444 ut_assertok(fdt_property_string(fdto, "target-path", "/"));
1445 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1446 ut_assertok(fdt_property_string(fdto, "newstring", "newervalue"));
1447 ut_assertok(fdt_property_u32(fdto, "newu32", 0x12345678));
1448 ut_assertok(fdt_property(fdto, "empty-property", NULL, 0));
1449 ut_assert(fdt_begin_node(fdto, "subnode") >= 0);
1450 ut_assertok(fdt_property_string(fdto, "subnewstring", "newervalue"));
1451 ut_assertok(fdt_property_u32(fdto, "subnewu32", 0x12345678));
1452 ut_assertok(fdt_property(fdto, "subempty-property", NULL, 0));
1453 ut_assertok(fdt_property_u32(fdto, "phandle", 0x01));
1454 ut_assertok(fdt_end_node(fdto));
1455 ut_assertok(fdt_end_node(fdto));
1456 ut_assertok(fdt_end_node(fdto));
1457
1458 ut_assert(fdt_begin_node(fdto, "__symbols__") >= 0);
1459 ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0/__overlay__/subnode"));
1460 ut_assertok(fdt_end_node(fdto));
1461 ut_assertok(fdt_finish(fdto));
1462 addro = map_to_sysmem(fdto);
1463
1464 /* Test complex DTO application */
1465 ut_assertok(console_record_reset_enable());
Evgeny Bachinine604bc02023-03-20 11:23:13 +03001466 ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
Marek Vasutdc0ccea2023-03-02 04:08:44 +01001467 ut_assertok(run_commandf("fdt print /"));
1468 ut_assert_nextline("/ {");
1469 ut_assert_nextline("\tempty-property;");
1470 ut_assert_nextline("\tnewu32 = <0x12345678>;");
1471 ut_assert_nextline("\tnewstring = \"newervalue\";");
1472 ut_assert_nextline("\tsubnode {");
1473 ut_assert_nextline("\t\tphandle = <0x00000001>;");
1474 ut_assert_nextline("\t\tsubempty-property;");
1475 ut_assert_nextline("\t\tsubnewu32 = <0x12345678>;");
1476 ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
1477 ut_assert_nextline("\t};");
1478 ut_assert_nextline("\t__symbols__ {");
1479 ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
1480 ut_assert_nextline("\t};");
1481 ut_assert_nextline("};");
1482 ut_assertok(ut_check_console_end(uts));
1483
1484 /*
1485 * Create complex DTO which:
1486 * - modifies subnewu32 property in subnode via phandle and uses __fixups__ node
1487 */
1488 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1489 ut_assertok(fdt_finish_reservemap(fdto));
1490 ut_assert(fdt_begin_node(fdto, "") >= 0);
1491 ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
1492 ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
1493
1494 ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
1495 ut_assertok(fdt_property_u32(fdto, "target", 0xffffffff));
1496 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1497 ut_assertok(fdt_property_u32(fdto, "subnewu32", 0xabcdef01));
1498 ut_assertok(fdt_end_node(fdto));
1499 ut_assertok(fdt_end_node(fdto));
1500
1501 ut_assert(fdt_begin_node(fdto, "__fixups__") >= 0);
1502 ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0:target:0"));
1503 ut_assertok(fdt_end_node(fdto));
1504 ut_assertok(fdt_end_node(fdto));
1505 ut_assertok(fdt_finish(fdto));
1506 addro = map_to_sysmem(fdto);
1507
1508 /* Test complex DTO application */
1509 ut_assertok(console_record_reset_enable());
Evgeny Bachinine604bc02023-03-20 11:23:13 +03001510 ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
Marek Vasutdc0ccea2023-03-02 04:08:44 +01001511 ut_assertok(run_commandf("fdt print /"));
1512 ut_assert_nextline("/ {");
1513 ut_assert_nextline("\tempty-property;");
1514 ut_assert_nextline("\tnewu32 = <0x12345678>;");
1515 ut_assert_nextline("\tnewstring = \"newervalue\";");
1516 ut_assert_nextline("\tsubnode {");
1517 ut_assert_nextline("\t\tphandle = <0x00000001>;");
1518 ut_assert_nextline("\t\tsubempty-property;");
1519 ut_assert_nextline("\t\tsubnewu32 = <0xabcdef01>;");
1520 ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
1521 ut_assert_nextline("\t};");
1522 ut_assert_nextline("\t__symbols__ {");
1523 ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
1524 ut_assert_nextline("\t};");
1525 ut_assert_nextline("};");
1526 ut_assertok(ut_check_console_end(uts));
1527
1528 return 0;
1529}
1530FDT_TEST(fdt_test_apply, UT_TESTF_CONSOLE_REC);
1531
Simon Glassf3c6a1d2022-07-13 06:06:59 -06001532int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1533{
1534 struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);
1535 const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test);
1536
1537 return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv);
1538}