blob: 5470855217586452172a4c6515dc48b188f6d886 [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
8#include <common.h>
9#include <console.h>
10#include <fdt_support.h>
11#include <mapmem.h>
12#include <asm/global_data.h>
13#include <linux/libfdt.h>
14#include <test/suites.h>
15#include <test/ut.h>
16
17DECLARE_GLOBAL_DATA_PTR;
Marek Vasutf2def362023-03-02 04:08:45 +010018/*
19 * Missing tests:
20 * fdt boardsetup - Do board-specific set up
21 * fdt checksign [<addr>] - check FIT signature
22 * <addr> - address of key blob
23 * default gd->fdt_blob
24 */
Simon Glassf3c6a1d2022-07-13 06:06:59 -060025
26/* Declare a new fdt test */
27#define FDT_TEST(_name, _flags) UNIT_TEST(_name, _flags, fdt_test)
28
29/**
30 * make_test_fdt() - Create an FDT with just a root node
31 *
32 * The size is set to the minimum needed
33 *
34 * @uts: Test state
35 * @fdt: Place to write FDT
36 * @size: Maximum size of space for fdt
37 */
38static int make_test_fdt(struct unit_test_state *uts, void *fdt, int size)
39{
40 ut_assertok(fdt_create(fdt, size));
41 ut_assertok(fdt_finish_reservemap(fdt));
42 ut_assert(fdt_begin_node(fdt, "") >= 0);
43 ut_assertok(fdt_end_node(fdt));
44 ut_assertok(fdt_finish(fdt));
45
46 return 0;
47}
48
Marek Vasut15503d02023-03-02 04:08:27 +010049/**
50 * make_fuller_fdt() - Create an FDT with root node and properties
51 *
52 * The size is set to the minimum needed
53 *
54 * @uts: Test state
55 * @fdt: Place to write FDT
56 * @size: Maximum size of space for fdt
57 */
58static int make_fuller_fdt(struct unit_test_state *uts, void *fdt, int size)
59{
60 fdt32_t regs[2] = { cpu_to_fdt32(0x1234), cpu_to_fdt32(0x1000) };
61
62 /*
63 * Assemble the following DT for test purposes:
64 *
65 * / {
66 * #address-cells = <0x00000001>;
67 * #size-cells = <0x00000001>;
68 * compatible = "u-boot,fdt-test";
69 * model = "U-Boot FDT test";
70 *
71 * aliases {
72 * badalias = "/bad/alias";
73 * subnodealias = "/test-node@1234/subnode";
74 * testnodealias = "/test-node@1234";
75 * };
76 *
77 * test-node@1234 {
78 * #address-cells = <0x00000000>;
79 * #size-cells = <0x00000000>;
80 * compatible = "u-boot,fdt-test-device1";
81 * clock-names = "fixed", "i2c", "spi", "uart2", "uart1";
82 * u-boot,empty-property;
83 * clock-frequency = <0x00fde800>;
84 * regs = <0x00001234 0x00001000>;
85 *
86 * subnode {
87 * #address-cells = <0x00000000>;
88 * #size-cells = <0x00000000>;
89 * compatible = "u-boot,fdt-subnode-test-device";
90 * };
91 * };
92 * };
93 */
94
95 ut_assertok(fdt_create(fdt, size));
96 ut_assertok(fdt_finish_reservemap(fdt));
97 ut_assert(fdt_begin_node(fdt, "") >= 0);
98
99 ut_assertok(fdt_property_u32(fdt, "#address-cells", 1));
100 ut_assertok(fdt_property_u32(fdt, "#size-cells", 1));
101 /* <string> */
102 ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test"));
103 /* <string> */
104 ut_assertok(fdt_property_string(fdt, "model", "U-Boot FDT test"));
105
106 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
107 /* <string> */
108 ut_assertok(fdt_property_string(fdt, "badalias", "/bad/alias"));
109 /* <string> */
110 ut_assertok(fdt_property_string(fdt, "subnodealias", "/test-node@1234/subnode"));
111 /* <string> */
112 ut_assertok(fdt_property_string(fdt, "testnodealias", "/test-node@1234"));
113 ut_assertok(fdt_end_node(fdt));
114
115 ut_assert(fdt_begin_node(fdt, "test-node@1234") >= 0);
116 ut_assertok(fdt_property_cell(fdt, "#address-cells", 0));
117 ut_assertok(fdt_property_cell(fdt, "#size-cells", 0));
118 /* <string> */
119 ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test-device1"));
120 /* <stringlist> */
121 ut_assertok(fdt_property(fdt, "clock-names", "fixed\0i2c\0spi\0uart2\0uart1\0", 26));
122 /* <empty> */
123 ut_assertok(fdt_property(fdt, "u-boot,empty-property", NULL, 0));
124 /*
125 * <u32>
126 * This value is deliberate as it used to break cmd/fdt.c
127 * is_printable_string() implementation.
128 */
129 ut_assertok(fdt_property_u32(fdt, "clock-frequency", 16640000));
130 /* <prop-encoded-array> */
131 ut_assertok(fdt_property(fdt, "regs", &regs, sizeof(regs)));
132 ut_assert(fdt_begin_node(fdt, "subnode") >= 0);
133 ut_assertok(fdt_property_cell(fdt, "#address-cells", 0));
134 ut_assertok(fdt_property_cell(fdt, "#size-cells", 0));
135 ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-subnode-test-device"));
136 ut_assertok(fdt_end_node(fdt));
137 ut_assertok(fdt_end_node(fdt));
138
139 ut_assertok(fdt_end_node(fdt));
140 ut_assertok(fdt_finish(fdt));
141
142 return 0;
143}
144
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600145/* Test 'fdt addr' getting/setting address */
146static int fdt_test_addr(struct unit_test_state *uts)
147{
148 const void *fdt_blob, *new_fdt;
149 char fdt[256];
150 ulong addr;
151 int ret;
152
153 ut_assertok(console_record_reset_enable());
154 ut_assertok(run_command("fdt addr -c", 0));
155 ut_assert_nextline("Control fdt: %08lx",
156 (ulong)map_to_sysmem(gd->fdt_blob));
157 ut_assertok(ut_check_console_end(uts));
158
159 /* The working fdt is not set, so this should fail */
160 set_working_fdt_addr(0);
Simon Glass84328cf2022-10-11 09:47:12 -0600161 ut_assert_nextline("Working FDT set to 0");
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600162 ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0));
Simon Glass5ad263f2023-10-01 19:15:18 -0600163
164 /*
165 * sandbox fails the check for !blob since the 0 pointer is mapped to
166 * memory somewhere other than at 0x0
167 */
168 if (IS_ENABLED(CONFIG_SANDBOX))
169 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600170 ut_assertok(ut_check_console_end(uts));
171
172 /* Set up a working FDT and try again */
173 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
174 addr = map_to_sysmem(fdt);
175 set_working_fdt_addr(addr);
Simon Glass84328cf2022-10-11 09:47:12 -0600176 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600177 ut_assertok(run_command("fdt addr", 0));
178 ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt));
179 ut_assertok(ut_check_console_end(uts));
180
181 /* Set the working FDT */
182 set_working_fdt_addr(0);
Simon Glass84328cf2022-10-11 09:47:12 -0600183 ut_assert_nextline("Working FDT set to 0");
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300184 ut_assertok(run_commandf("fdt addr %08lx", addr));
Simon Glass84328cf2022-10-11 09:47:12 -0600185 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600186 ut_asserteq(addr, map_to_sysmem(working_fdt));
187 ut_assertok(ut_check_console_end(uts));
188 set_working_fdt_addr(0);
Simon Glass84328cf2022-10-11 09:47:12 -0600189 ut_assert_nextline("Working FDT set to 0");
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600190
Simon Glass84328cf2022-10-11 09:47:12 -0600191 /* Set the control FDT */
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600192 fdt_blob = gd->fdt_blob;
193 gd->fdt_blob = NULL;
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300194 ret = run_commandf("fdt addr -c %08lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600195 new_fdt = gd->fdt_blob;
196 gd->fdt_blob = fdt_blob;
197 ut_assertok(ret);
198 ut_asserteq(addr, map_to_sysmem(new_fdt));
199 ut_assertok(ut_check_console_end(uts));
200
201 /* Test setting an invalid FDT */
202 fdt[0] = 123;
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300203 ut_asserteq(1, run_commandf("fdt addr %08lx", addr));
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600204 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
205 ut_assertok(ut_check_console_end(uts));
206
207 /* Test detecting an invalid FDT */
208 fdt[0] = 123;
209 set_working_fdt_addr(addr);
Simon Glass84328cf2022-10-11 09:47:12 -0600210 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600211 ut_asserteq(1, run_commandf("fdt addr"));
212 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
213 ut_assertok(ut_check_console_end(uts));
214
215 return 0;
216}
217FDT_TEST(fdt_test_addr, UT_TESTF_CONSOLE_REC);
218
219/* Test 'fdt addr' resizing an fdt */
Marek Vasut6e04a892023-03-02 04:08:25 +0100220static int fdt_test_addr_resize(struct unit_test_state *uts)
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600221{
222 char fdt[256];
223 const int newsize = sizeof(fdt) / 2;
224 ulong addr;
225
226 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
227 addr = map_to_sysmem(fdt);
228 set_working_fdt_addr(addr);
229
230 /* Test setting and resizing the working FDT to a larger size */
231 ut_assertok(console_record_reset_enable());
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300232 ut_assertok(run_commandf("fdt addr %08lx %x", addr, newsize));
Simon Glass84328cf2022-10-11 09:47:12 -0600233 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600234 ut_assertok(ut_check_console_end(uts));
235
236 /* Try shrinking it */
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300237 ut_assertok(run_commandf("fdt addr %08lx %zx", addr, sizeof(fdt) / 4));
Simon Glass84328cf2022-10-11 09:47:12 -0600238 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600239 ut_assert_nextline("New length %d < existing length %d, ignoring",
240 (int)sizeof(fdt) / 4, newsize);
241 ut_assertok(ut_check_console_end(uts));
242
243 /* ...quietly */
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300244 ut_assertok(run_commandf("fdt addr -q %08lx %zx", addr, sizeof(fdt) / 4));
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600245 ut_assertok(ut_check_console_end(uts));
246
247 /* We cannot easily provoke errors in fdt_open_into(), so ignore that */
248
249 return 0;
250}
Marek Vasut6e04a892023-03-02 04:08:25 +0100251FDT_TEST(fdt_test_addr_resize, UT_TESTF_CONSOLE_REC);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600252
Marek Vasutddf86202023-03-02 04:08:30 +0100253static int fdt_test_move(struct unit_test_state *uts)
254{
255 char fdt[256];
256 ulong addr, newaddr = 0x10000;
257 const int size = sizeof(fdt);
258 uint32_t ts;
259 void *buf;
260
261 /* Original source DT */
262 ut_assertok(make_test_fdt(uts, fdt, size));
263 ts = fdt_totalsize(fdt);
264 addr = map_to_sysmem(fdt);
265 set_working_fdt_addr(addr);
266
267 /* Moved target DT location */
268 buf = map_sysmem(newaddr, size);
269 memset(buf, 0, size);
270
271 /* Test moving the working FDT to a new location */
272 ut_assertok(console_record_reset_enable());
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300273 ut_assertok(run_commandf("fdt move %08lx %08lx %x", addr, newaddr, ts));
Marek Vasutddf86202023-03-02 04:08:30 +0100274 ut_assert_nextline("Working FDT set to %lx", newaddr);
275 ut_assertok(ut_check_console_end(uts));
276
277 /* Compare the source and destination DTs */
278 ut_assertok(console_record_reset_enable());
Evgeny Bachinine604bc02023-03-20 11:23:13 +0300279 ut_assertok(run_commandf("cmp.b %08lx %08lx %x", addr, newaddr, ts));
Marek Vasutddf86202023-03-02 04:08:30 +0100280 ut_assert_nextline("Total of %d byte(s) were the same", ts);
281 ut_assertok(ut_check_console_end(uts));
282
283 return 0;
284}
285FDT_TEST(fdt_test_move, UT_TESTF_CONSOLE_REC);
286
Marek Vasut6ab4fe22023-03-02 04:08:31 +0100287static int fdt_test_resize(struct unit_test_state *uts)
288{
289 char fdt[256];
290 const unsigned int newsize = 0x2000;
291 uint32_t ts;
292 ulong addr;
293
294 /* Original source DT */
295 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
296 fdt_shrink_to_minimum(fdt, 0); /* Resize with 0 extra bytes */
297 ts = fdt_totalsize(fdt);
298 addr = map_to_sysmem(fdt);
299 set_working_fdt_addr(addr);
300
301 /* Test resizing the working FDT and verify the new space was added */
302 ut_assertok(console_record_reset_enable());
303 ut_assertok(run_commandf("fdt resize %x", newsize));
304 ut_asserteq(ts + newsize, fdt_totalsize(fdt));
305 ut_assertok(ut_check_console_end(uts));
306
307 return 0;
308}
309FDT_TEST(fdt_test_resize, UT_TESTF_CONSOLE_REC);
310
Marek Vasut76a652a2023-03-02 04:08:32 +0100311static int fdt_test_print_list_common(struct unit_test_state *uts,
312 const char *opc, const char *node)
313{
314 /*
315 * Test printing/listing the working FDT
316 * subnode $node/subnode
317 */
318 ut_assertok(console_record_reset_enable());
319 ut_assertok(run_commandf("fdt %s %s/subnode", opc, node));
320 ut_assert_nextline("subnode {");
321 ut_assert_nextline("\t#address-cells = <0x00000000>;");
322 ut_assert_nextline("\t#size-cells = <0x00000000>;");
323 ut_assert_nextline("\tcompatible = \"u-boot,fdt-subnode-test-device\";");
324 ut_assert_nextline("};");
325 ut_assertok(ut_check_console_end(uts));
326
327 /*
328 * Test printing/listing the working FDT
329 * path / string property model
330 */
331 ut_assertok(console_record_reset_enable());
332 ut_assertok(run_commandf("fdt %s / model", opc));
333 ut_assert_nextline("model = \"U-Boot FDT test\"");
334 ut_assertok(ut_check_console_end(uts));
335
336 /*
337 * Test printing/listing the working FDT
338 * path $node string property compatible
339 */
340 ut_assertok(console_record_reset_enable());
341 ut_assertok(run_commandf("fdt %s %s compatible", opc, node));
342 ut_assert_nextline("compatible = \"u-boot,fdt-test-device1\"");
343 ut_assertok(ut_check_console_end(uts));
344
345 /*
346 * Test printing/listing the working FDT
347 * path $node stringlist property clock-names
348 */
349 ut_assertok(console_record_reset_enable());
350 ut_assertok(run_commandf("fdt %s %s clock-names", opc, node));
351 ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
352 ut_assertok(ut_check_console_end(uts));
353
354 /*
355 * Test printing/listing the working FDT
356 * path $node u32 property clock-frequency
357 */
358 ut_assertok(console_record_reset_enable());
359 ut_assertok(run_commandf("fdt %s %s clock-frequency", opc, node));
360 ut_assert_nextline("clock-frequency = <0x00fde800>");
361 ut_assertok(ut_check_console_end(uts));
362
363 /*
364 * Test printing/listing the working FDT
365 * path $node empty property u-boot,empty-property
366 */
367 ut_assertok(console_record_reset_enable());
368 ut_assertok(run_commandf("fdt %s %s u-boot,empty-property", opc, node));
369 /*
370 * This is the only 'fdt print' / 'fdt list' incantation which
371 * prefixes the property with node path. This has been in U-Boot
372 * since the beginning of the command 'fdt', keep it.
373 */
374 ut_assert_nextline("%s u-boot,empty-property", node);
375 ut_assertok(ut_check_console_end(uts));
376
377 /*
378 * Test printing/listing the working FDT
379 * path $node prop-encoded array property regs
380 */
381 ut_assertok(console_record_reset_enable());
382 ut_assertok(run_commandf("fdt %s %s regs", opc, node));
383 ut_assert_nextline("regs = <0x00001234 0x00001000>");
384 ut_assertok(ut_check_console_end(uts));
385
386 return 0;
387}
388
389static int fdt_test_print_list(struct unit_test_state *uts, bool print)
390{
391 const char *opc = print ? "print" : "list";
392 char fdt[4096];
393 ulong addr;
394 int ret;
395
396 /* Original source DT */
397 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
398 addr = map_to_sysmem(fdt);
399 set_working_fdt_addr(addr);
400
401 /* Test printing/listing the working FDT -- node / */
402 ut_assertok(console_record_reset_enable());
403 ut_assertok(run_commandf("fdt %s", opc));
404 ut_assert_nextline("/ {");
405 ut_assert_nextline("\t#address-cells = <0x00000001>;");
406 ut_assert_nextline("\t#size-cells = <0x00000001>;");
407 ut_assert_nextline("\tcompatible = \"u-boot,fdt-test\";");
408 ut_assert_nextline("\tmodel = \"U-Boot FDT test\";");
409 ut_assert_nextline("\taliases {");
410 if (print) {
411 ut_assert_nextline("\t\tbadalias = \"/bad/alias\";");
412 ut_assert_nextline("\t\tsubnodealias = \"/test-node@1234/subnode\";");
413 ut_assert_nextline("\t\ttestnodealias = \"/test-node@1234\";");
414 }
415 ut_assert_nextline("\t};");
416 ut_assert_nextline("\ttest-node@1234 {");
417 if (print) {
418 ut_assert_nextline("\t\t#address-cells = <0x00000000>;");
419 ut_assert_nextline("\t\t#size-cells = <0x00000000>;");
420 ut_assert_nextline("\t\tcompatible = \"u-boot,fdt-test-device1\";");
421 ut_assert_nextline("\t\tclock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\";");
422 ut_assert_nextline("\t\tu-boot,empty-property;");
423 ut_assert_nextline("\t\tclock-frequency = <0x00fde800>;");
424 ut_assert_nextline("\t\tregs = <0x00001234 0x00001000>;");
425 ut_assert_nextline("\t\tsubnode {");
426 ut_assert_nextline("\t\t\t#address-cells = <0x00000000>;");
427 ut_assert_nextline("\t\t\t#size-cells = <0x00000000>;");
428 ut_assert_nextline("\t\t\tcompatible = \"u-boot,fdt-subnode-test-device\";");
429 ut_assert_nextline("\t\t};");
430 }
431 ut_assert_nextline("\t};");
432 ut_assert_nextline("};");
433 ut_assertok(ut_check_console_end(uts));
434
435 ret = fdt_test_print_list_common(uts, opc, "/test-node@1234");
436 if (!ret)
437 ret = fdt_test_print_list_common(uts, opc, "testnodealias");
438
439 return 0;
440}
441
442static int fdt_test_print(struct unit_test_state *uts)
443{
444 return fdt_test_print_list(uts, true);
445}
446FDT_TEST(fdt_test_print, UT_TESTF_CONSOLE_REC);
447
448static int fdt_test_list(struct unit_test_state *uts)
449{
450 return fdt_test_print_list(uts, false);
451}
452FDT_TEST(fdt_test_list, UT_TESTF_CONSOLE_REC);
453
Marek Vasutfbeca452023-03-02 04:08:26 +0100454/* Test 'fdt get value' reading an fdt */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100455static int fdt_test_get_value_string(struct unit_test_state *uts,
456 const char *node, const char *prop,
457 const char *idx, const char *strres,
458 const int intres)
459{
460 ut_assertok(console_record_reset_enable());
461 ut_assertok(run_commandf("fdt get value var %s %s %s",
462 node, prop, idx ? : ""));
463 if (strres) {
464 ut_asserteq_str(strres, env_get("var"));
465 } else {
466 ut_asserteq(intres, env_get_hex("var", 0x1234));
467 }
468 ut_assertok(ut_check_console_end(uts));
469
470 return 0;
471}
472
Marek Vasutb7517b42023-03-02 04:08:28 +0100473static int fdt_test_get_value_common(struct unit_test_state *uts,
474 const char *node)
Marek Vasut78c060d2022-11-14 22:50:00 +0100475{
Marek Vasutb7517b42023-03-02 04:08:28 +0100476 /* Test getting default element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100477 fdt_test_get_value_string(uts, node, "clock-names", NULL, "fixed", 0);
Marek Vasut78c060d2022-11-14 22:50:00 +0100478
Marek Vasutb7517b42023-03-02 04:08:28 +0100479 /* Test getting 0th element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100480 fdt_test_get_value_string(uts, node, "clock-names", "0", "fixed", 0);
Marek Vasut78c060d2022-11-14 22:50:00 +0100481
Marek Vasutb7517b42023-03-02 04:08:28 +0100482 /* Test getting 1st element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100483 fdt_test_get_value_string(uts, node, "clock-names", "1", "i2c", 0);
Marek Vasut78c060d2022-11-14 22:50:00 +0100484
Marek Vasutb7517b42023-03-02 04:08:28 +0100485 /* Test getting 2nd element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100486 fdt_test_get_value_string(uts, node, "clock-names", "2", "spi", 0);
487
488 /*
489 * Test getting default element of $node node regs property.
490 * The result here is highly unusual, the non-index value read from
491 * integer array is a string of concatenated values from the array,
492 * but only if the array is shorter than 40 characters. Anything
493 * longer is an error. This is a special case for handling hashes.
494 */
495 fdt_test_get_value_string(uts, node, "regs", NULL, "3412000000100000", 0);
496
497 /* Test getting 0th element of $node node regs property */
498 fdt_test_get_value_string(uts, node, "regs", "0", NULL, 0x1234);
499
500 /* Test getting 1st element of $node node regs property */
501 fdt_test_get_value_string(uts, node, "regs", "1", NULL, 0x1000);
Marek Vasut78c060d2022-11-14 22:50:00 +0100502
Marek Vasutb7517b42023-03-02 04:08:28 +0100503 /* Test missing 10th element of $node node clock-names property */
Marek Vasut78c060d2022-11-14 22:50:00 +0100504 ut_assertok(console_record_reset_enable());
Marek Vasutb7517b42023-03-02 04:08:28 +0100505 ut_asserteq(1, run_commandf("fdt get value ften %s clock-names 10", node));
Marek Vasut78c060d2022-11-14 22:50:00 +0100506 ut_assertok(ut_check_console_end(uts));
507
Marek Vasut9f90cad2023-03-02 04:08:29 +0100508 /* Test missing 10th element of $node node regs property */
509 ut_assertok(console_record_reset_enable());
510 ut_asserteq(1, run_commandf("fdt get value ften %s regs 10", node));
511 ut_assertok(ut_check_console_end(uts));
512
Marek Vasutb7517b42023-03-02 04:08:28 +0100513 /* Test getting default element of $node node nonexistent property */
Marek Vasut78c060d2022-11-14 22:50:00 +0100514 ut_assertok(console_record_reset_enable());
Marek Vasutb7517b42023-03-02 04:08:28 +0100515 ut_asserteq(1, run_commandf("fdt get value fnone %s nonexistent", node));
Marek Vasut78c060d2022-11-14 22:50:00 +0100516 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
517 ut_assertok(ut_check_console_end(uts));
518
Marek Vasutb7517b42023-03-02 04:08:28 +0100519 return 0;
520}
521
522static int fdt_test_get_value(struct unit_test_state *uts)
523{
524 char fdt[4096];
525 ulong addr;
526 int ret;
527
528 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
529 addr = map_to_sysmem(fdt);
530 set_working_fdt_addr(addr);
531
532 ret = fdt_test_get_value_common(uts, "/test-node@1234");
533 if (!ret)
534 ret = fdt_test_get_value_common(uts, "testnodealias");
535 if (ret)
536 return ret;
537
Marek Vasut78c060d2022-11-14 22:50:00 +0100538 /* Test getting default element of /nonexistent node */
539 ut_assertok(console_record_reset_enable());
540 ut_asserteq(1, run_command("fdt get value fnode /nonexistent nonexistent", 1));
541 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
542 ut_assertok(ut_check_console_end(uts));
543
Marek Vasutb7517b42023-03-02 04:08:28 +0100544 /* Test getting default element of bad alias */
545 ut_assertok(console_record_reset_enable());
546 ut_asserteq(1, run_command("fdt get value vbadalias badalias nonexistent", 1));
547 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
548 ut_assertok(ut_check_console_end(uts));
549
550 /* Test getting default element of nonexistent alias */
551 ut_assertok(console_record_reset_enable());
552 ut_asserteq(1, run_command("fdt get value vnoalias noalias nonexistent", 1));
553 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
554 ut_assertok(ut_check_console_end(uts));
555
Marek Vasut78c060d2022-11-14 22:50:00 +0100556 return 0;
557}
Marek Vasutfbeca452023-03-02 04:08:26 +0100558FDT_TEST(fdt_test_get_value, UT_TESTF_CONSOLE_REC);
Marek Vasut78c060d2022-11-14 22:50:00 +0100559
Marek Vasut84499932023-03-02 04:08:33 +0100560static int fdt_test_get_name(struct unit_test_state *uts)
561{
562 char fdt[4096];
563 ulong addr;
564
565 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
566 addr = map_to_sysmem(fdt);
567 set_working_fdt_addr(addr);
568
569 /* Test getting name of node 0 in /, which is /aliases node */
570 ut_assertok(console_record_reset_enable());
571 ut_assertok(run_command("fdt get name nzero / 0", 0));
572 ut_asserteq_str("aliases", env_get("nzero"));
573 ut_assertok(ut_check_console_end(uts));
574
575 /* Test getting name of node 1 in /, which is /test-node@1234 node */
576 ut_assertok(console_record_reset_enable());
577 ut_assertok(run_command("fdt get name none / 1", 0));
578 ut_asserteq_str("test-node@1234", env_get("none"));
579 ut_assertok(ut_check_console_end(uts));
580
581 /* Test getting name of node -1 in /, which is /aliases node, same as 0 */
582 ut_assertok(console_record_reset_enable());
583 ut_assertok(run_command("fdt get name nmone / -1", 0));
584 ut_asserteq_str("aliases", env_get("nmone"));
585 ut_assertok(ut_check_console_end(uts));
586
587 /* Test getting name of node 2 in /, which does not exist */
588 ut_assertok(console_record_reset_enable());
589 ut_asserteq(1, run_command("fdt get name ntwo / 2", 1));
590 ut_assert_nextline("libfdt node not found");
591 ut_assertok(ut_check_console_end(uts));
592
593 /* Test getting name of node 0 in /test-node@1234, which is /subnode node */
594 ut_assertok(console_record_reset_enable());
595 ut_assertok(run_command("fdt get name snzero /test-node@1234 0", 0));
596 ut_asserteq_str("subnode", env_get("snzero"));
597 ut_assertok(run_command("fdt get name asnzero testnodealias 0", 0));
598 ut_asserteq_str("subnode", env_get("asnzero"));
599 ut_assertok(ut_check_console_end(uts));
600
601 /* Test getting name of node 1 in /test-node@1234, which does not exist */
602 ut_assertok(console_record_reset_enable());
603 ut_asserteq(1, run_command("fdt get name snone /test-node@1234 1", 1));
604 ut_assert_nextline("libfdt node not found");
605 ut_asserteq(1, run_command("fdt get name asnone testnodealias 1", 1));
606 ut_assert_nextline("libfdt node not found");
607 ut_assertok(ut_check_console_end(uts));
608
609 /* Test getting name of node -1 in /test-node@1234, which is /subnode node, same as 0 */
610 ut_assertok(console_record_reset_enable());
611 ut_assertok(run_command("fdt get name snmone /test-node@1234 -1", 0));
612 ut_asserteq_str("subnode", env_get("snmone"));
613 ut_assertok(run_command("fdt get name asnmone testnodealias -1", 0));
614 ut_asserteq_str("subnode", env_get("asnmone"));
615 ut_assertok(ut_check_console_end(uts));
616
617 /* Test getting name of nonexistent node */
618 ut_assertok(console_record_reset_enable());
619 ut_asserteq(1, run_command("fdt get name nonode /nonexistent 0", 1));
620 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
621 ut_assertok(ut_check_console_end(uts));
622
623 /* Test getting name of bad alias */
624 ut_assertok(console_record_reset_enable());
625 ut_asserteq(1, run_command("fdt get name vbadalias badalias 0", 1));
626 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
627 ut_assertok(ut_check_console_end(uts));
628
629 /* Test getting name of nonexistent alias */
630 ut_assertok(console_record_reset_enable());
631 ut_asserteq(1, run_command("fdt get name vnoalias noalias 0", 1));
632 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
633 ut_assertok(ut_check_console_end(uts));
634
635 return 0;
636}
637FDT_TEST(fdt_test_get_name, UT_TESTF_CONSOLE_REC);
638
Marek Vasutcadef862023-03-02 04:08:34 +0100639static int fdt_test_get_addr_common(struct unit_test_state *uts, char *fdt,
640 const char *path, const char *prop)
641{
642 unsigned int offset;
643 int path_offset;
644 void *prop_ptr;
645 int len = 0;
646
647 path_offset = fdt_path_offset(fdt, path);
648 ut_assert(path_offset >= 0);
649 prop_ptr = (void *)fdt_getprop(fdt, path_offset, prop, &len);
650 ut_assertnonnull(prop_ptr);
651 offset = (char *)prop_ptr - fdt;
652
653 ut_assertok(console_record_reset_enable());
654 ut_assertok(run_commandf("fdt get addr pstr %s %s", path, prop));
655 ut_asserteq((ulong)map_sysmem(env_get_hex("fdtaddr", 0x1234), 0),
656 (ulong)(map_sysmem(env_get_hex("pstr", 0x1234), 0) - offset));
657 ut_assertok(ut_check_console_end(uts));
658
659 return 0;
660}
661
662static int fdt_test_get_addr(struct unit_test_state *uts)
663{
664 char fdt[4096];
665 ulong addr;
666
667 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
668 addr = map_to_sysmem(fdt);
669 set_working_fdt_addr(addr);
670
671 /* Test getting address of root node / string property "compatible" */
672 fdt_test_get_addr_common(uts, fdt, "/", "compatible");
673
674 /* Test getting address of node /test-node@1234 stringlist property "clock-names" */
675 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-names");
676 fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-names");
677
678 /* Test getting address of node /test-node@1234 u32 property "clock-frequency" */
679 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-frequency");
680 fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-frequency");
681
682 /* Test getting address of node /test-node@1234 empty property "u-boot,empty-property" */
683 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "u-boot,empty-property");
684 fdt_test_get_addr_common(uts, fdt, "testnodealias", "u-boot,empty-property");
685
686 /* Test getting address of node /test-node@1234 array property "regs" */
687 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "regs");
688 fdt_test_get_addr_common(uts, fdt, "testnodealias", "regs");
689
690 /* Test getting address of node /test-node@1234/subnode non-existent property "noprop" */
691 ut_assertok(console_record_reset_enable());
692 ut_asserteq(1, run_command("fdt get addr pnoprop /test-node@1234/subnode noprop", 1));
693 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
694 ut_assertok(ut_check_console_end(uts));
695
696 /* Test getting address of non-existent node /test-node@1234/nonode@1 property "noprop" */
697 ut_assertok(console_record_reset_enable());
698 ut_asserteq(1, run_command("fdt get addr pnonode /test-node@1234/nonode@1 noprop", 1));
699 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
700 ut_assertok(ut_check_console_end(uts));
701
702 return 0;
703}
704FDT_TEST(fdt_test_get_addr, UT_TESTF_CONSOLE_REC);
705
Marek Vasutce1b0482023-03-02 04:08:35 +0100706static int fdt_test_get_size_common(struct unit_test_state *uts,
707 const char *path, const char *prop,
708 const unsigned int val)
709{
710 ut_assertok(console_record_reset_enable());
711 if (prop) {
712 ut_assertok(run_commandf("fdt get size sstr %s %s", path, prop));
713 } else {
714 ut_assertok(run_commandf("fdt get size sstr %s", path));
715 }
716 ut_asserteq(val, env_get_hex("sstr", 0x1234));
717 ut_assertok(ut_check_console_end(uts));
718
719 return 0;
720}
721
722static int fdt_test_get_size(struct unit_test_state *uts)
723{
724 char fdt[4096];
725 ulong addr;
726
727 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
728 addr = map_to_sysmem(fdt);
729 set_working_fdt_addr(addr);
730
731 /* Test getting size of root node / string property "compatible" */
732 fdt_test_get_size_common(uts, "/", "compatible", 16);
733
734 /* Test getting size of node /test-node@1234 stringlist property "clock-names" */
735 fdt_test_get_size_common(uts, "/test-node@1234", "clock-names", 26);
736 fdt_test_get_size_common(uts, "testnodealias", "clock-names", 26);
737
738 /* Test getting size of node /test-node@1234 u32 property "clock-frequency" */
739 fdt_test_get_size_common(uts, "/test-node@1234", "clock-frequency", 4);
740 fdt_test_get_size_common(uts, "testnodealias", "clock-frequency", 4);
741
742 /* Test getting size of node /test-node@1234 empty property "u-boot,empty-property" */
743 fdt_test_get_size_common(uts, "/test-node@1234", "u-boot,empty-property", 0);
744 fdt_test_get_size_common(uts, "testnodealias", "u-boot,empty-property", 0);
745
746 /* Test getting size of node /test-node@1234 array property "regs" */
747 fdt_test_get_size_common(uts, "/test-node@1234", "regs", 8);
748 fdt_test_get_size_common(uts, "testnodealias", "regs", 8);
749
750 /* Test getting node count of node / */
751 fdt_test_get_size_common(uts, "/", NULL, 2);
752
753 /* Test getting node count of node /test-node@1234/subnode */
754 fdt_test_get_size_common(uts, "/test-node@1234/subnode", NULL, 0);
755 fdt_test_get_size_common(uts, "subnodealias", NULL, 0);
756
757 /* Test getting size of node /test-node@1234/subnode non-existent property "noprop" */
758 ut_assertok(console_record_reset_enable());
759 ut_asserteq(1, run_command("fdt get size pnoprop /test-node@1234/subnode noprop", 1));
760 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
761 ut_asserteq(1, run_command("fdt get size pnoprop subnodealias noprop", 1));
762 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
763 ut_assertok(ut_check_console_end(uts));
764
765 /* Test getting size of non-existent node /test-node@1234/nonode@1 property "noprop" */
766 ut_assertok(console_record_reset_enable());
767 ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1 noprop", 1));
768 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
769 ut_assertok(ut_check_console_end(uts));
770
771 /* Test getting node count of non-existent node /test-node@1234/nonode@1 */
772 ut_assertok(console_record_reset_enable());
773 ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1", 1));
774 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
775 ut_assertok(ut_check_console_end(uts));
776
777 /* Test getting node count of bad alias badalias */
778 ut_assertok(console_record_reset_enable());
779 ut_asserteq(1, run_command("fdt get size pnonode badalias noprop", 1));
780 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
781 ut_assertok(ut_check_console_end(uts));
782
783 /* Test getting node count of non-existent alias noalias */
784 ut_assertok(console_record_reset_enable());
785 ut_asserteq(1, run_command("fdt get size pnonode noalias", 1));
786 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
787 ut_assertok(ut_check_console_end(uts));
788
789 return 0;
790}
791FDT_TEST(fdt_test_get_size, UT_TESTF_CONSOLE_REC);
792
Marek Vasutf180de42023-03-02 04:08:36 +0100793static int fdt_test_set_single(struct unit_test_state *uts,
794 const char *path, const char *prop,
795 const char *sval, int ival, bool integer)
796{
797 /*
798 * Set single element string/integer/<empty> property into DT, that is:
799 * => fdt set /path property string
800 * => fdt set /path property integer
801 * => fdt set /path property
802 */
803 ut_assertok(console_record_reset_enable());
Marek Vasutbc8c1402023-03-10 04:33:14 +0100804 if (sval)
Marek Vasutf180de42023-03-02 04:08:36 +0100805 ut_assertok(run_commandf("fdt set %s %s %s", path, prop, sval));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100806 else if (integer)
Marek Vasutf180de42023-03-02 04:08:36 +0100807 ut_assertok(run_commandf("fdt set %s %s <%d>", path, prop, ival));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100808 else
Marek Vasutf180de42023-03-02 04:08:36 +0100809 ut_assertok(run_commandf("fdt set %s %s", path, prop));
Marek Vasutf180de42023-03-02 04:08:36 +0100810
811 /* Validate the property is present and has correct value. */
812 ut_assertok(run_commandf("fdt get value svar %s %s", path, prop));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100813 if (sval)
Marek Vasutf180de42023-03-02 04:08:36 +0100814 ut_asserteq_str(sval, env_get("svar"));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100815 else if (integer)
Marek Vasutf180de42023-03-02 04:08:36 +0100816 ut_asserteq(ival, env_get_hex("svar", 0x1234));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100817 else
Marek Vasutf180de42023-03-02 04:08:36 +0100818 ut_assertnull(env_get("svar"));
Marek Vasutf180de42023-03-02 04:08:36 +0100819 ut_assertok(ut_check_console_end(uts));
820
821 return 0;
822}
823
824static int fdt_test_set_multi(struct unit_test_state *uts,
825 const char *path, const char *prop,
826 const char *sval1, const char *sval2,
827 int ival1, int ival2)
828{
829 /*
830 * Set multi element string/integer array property in DT, that is:
831 * => fdt set /path property <string1 string2>
832 * => fdt set /path property <integer1 integer2>
833 *
834 * The set is done twice in here deliberately, The first set adds
835 * the property with an extra trailing element in its array to make
836 * the array longer, the second set is the expected final content of
837 * the array property. The longer array is used to verify that the
838 * new array is correctly sized and read past the new array length
839 * triggers failure.
840 */
841 ut_assertok(console_record_reset_enable());
842 if (sval1 && sval2) {
843 ut_assertok(run_commandf("fdt set %s %s %s %s end", path, prop, sval1, sval2));
844 ut_assertok(run_commandf("fdt set %s %s %s %s", path, prop, sval1, sval2));
845 } else {
846 ut_assertok(run_commandf("fdt set %s %s <%d %d 10>", path, prop, ival1, ival2));
847 ut_assertok(run_commandf("fdt set %s %s <%d %d>", path, prop, ival1, ival2));
848 }
849
850 /*
851 * Validate the property is present and has correct value.
852 *
853 * The "end/10" above and "svarn" below is used to validate that
854 * previous 'fdt set' to longer array does not polute newly set
855 * shorter array.
856 */
857 ut_assertok(run_commandf("fdt get value svar1 %s %s 0", path, prop));
858 ut_assertok(run_commandf("fdt get value svar2 %s %s 1", path, prop));
859 ut_asserteq(1, run_commandf("fdt get value svarn %s %s 2", path, prop));
860 if (sval1 && sval2) {
861 ut_asserteq_str(sval1, env_get("svar1"));
862 ut_asserteq_str(sval2, env_get("svar2"));
863 ut_assertnull(env_get("svarn"));
864 } else {
865 ut_asserteq(ival1, env_get_hex("svar1", 0x1234));
866 ut_asserteq(ival2, env_get_hex("svar2", 0x1234));
867 ut_assertnull(env_get("svarn"));
868 }
869 ut_assertok(ut_check_console_end(uts));
870
871 return 0;
872}
873
874static int fdt_test_set_node(struct unit_test_state *uts,
875 const char *path, const char *prop)
876{
877 fdt_test_set_single(uts, path, prop, "new", 0, false);
878 fdt_test_set_single(uts, path, prop, "rewrite", 0, false);
879 fdt_test_set_single(uts, path, prop, NULL, 42, true);
880 fdt_test_set_single(uts, path, prop, NULL, 0, false);
881 fdt_test_set_multi(uts, path, prop, NULL, NULL, 42, 1701);
882 fdt_test_set_multi(uts, path, prop, NULL, NULL, 74656, 9);
883 fdt_test_set_multi(uts, path, prop, "42", "1701", 0, 0);
884 fdt_test_set_multi(uts, path, prop, "74656", "9", 0, 0);
885
886 return 0;
887}
888
889static int fdt_test_set(struct unit_test_state *uts)
890{
891 char fdt[8192];
892 ulong addr;
893
894 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
895 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
896 addr = map_to_sysmem(fdt);
897 set_working_fdt_addr(addr);
898
899 /* Test setting of root node / existing property "compatible" */
900 fdt_test_set_node(uts, "/", "compatible");
901
902 /* Test setting of root node / new property "newproperty" */
903 fdt_test_set_node(uts, "/", "newproperty");
904
905 /* Test setting of subnode existing property "compatible" */
906 fdt_test_set_node(uts, "/test-node@1234/subnode", "compatible");
907 fdt_test_set_node(uts, "subnodealias", "compatible");
908
909 /* Test setting of subnode new property "newproperty" */
910 fdt_test_set_node(uts, "/test-node@1234/subnode", "newproperty");
911 fdt_test_set_node(uts, "subnodealias", "newproperty");
912
913 /* Test setting property of non-existent node */
914 ut_assertok(console_record_reset_enable());
915 ut_asserteq(1, run_command("fdt set /no-node noprop", 1));
916 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
917 ut_assertok(ut_check_console_end(uts));
918
919 /* Test setting property of non-existent alias */
920 ut_assertok(console_record_reset_enable());
921 ut_asserteq(1, run_command("fdt set noalias noprop", 1));
922 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
923 ut_assertok(ut_check_console_end(uts));
924
925 /* Test setting property of bad alias */
926 ut_assertok(console_record_reset_enable());
927 ut_asserteq(1, run_command("fdt set badalias noprop", 1));
928 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
929 ut_assertok(ut_check_console_end(uts));
930
931 return 0;
932}
933FDT_TEST(fdt_test_set, UT_TESTF_CONSOLE_REC);
934
Marek Vasute4d26c92023-03-02 04:08:37 +0100935static int fdt_test_mknode(struct unit_test_state *uts)
936{
937 char fdt[8192];
938 ulong addr;
939
940 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
941 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
942 addr = map_to_sysmem(fdt);
943 set_working_fdt_addr(addr);
944
945 /* Test creation of new node in / */
946 ut_assertok(console_record_reset_enable());
947 ut_assertok(run_commandf("fdt mknode / newnode"));
948 ut_assertok(run_commandf("fdt list /newnode"));
949 ut_assert_nextline("newnode {");
950 ut_assert_nextline("};");
951 ut_assertok(ut_check_console_end(uts));
952
953 /* Test creation of new node in /test-node@1234 */
954 ut_assertok(console_record_reset_enable());
955 ut_assertok(run_commandf("fdt mknode /test-node@1234 newsubnode"));
956 ut_assertok(run_commandf("fdt list /test-node@1234/newsubnode"));
957 ut_assert_nextline("newsubnode {");
958 ut_assert_nextline("};");
959 ut_assertok(ut_check_console_end(uts));
960
961 /* Test creation of new node in /test-node@1234 by alias */
962 ut_assertok(console_record_reset_enable());
963 ut_assertok(run_commandf("fdt mknode testnodealias newersubnode"));
964 ut_assertok(run_commandf("fdt list testnodealias/newersubnode"));
965 ut_assert_nextline("newersubnode {");
966 ut_assert_nextline("};");
967 ut_assertok(ut_check_console_end(uts));
968
969 /* Test creation of new node in /test-node@1234 over existing node */
970 ut_assertok(console_record_reset_enable());
971 ut_asserteq(1, run_commandf("fdt mknode testnodealias newsubnode"));
972 ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
973 ut_assertok(ut_check_console_end(uts));
974
975 /* Test creation of new node in /test-node@1234 by alias over existing node */
976 ut_assertok(console_record_reset_enable());
977 ut_asserteq(1, run_commandf("fdt mknode testnodealias newersubnode"));
978 ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
979 ut_assertok(ut_check_console_end(uts));
980
981 /* Test creation of new node in non-existent node */
982 ut_assertok(console_record_reset_enable());
983 ut_asserteq(1, run_commandf("fdt mknode /no-node newnosubnode"));
984 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
985 ut_assertok(ut_check_console_end(uts));
986
987 /* Test creation of new node in non-existent alias */
988 ut_assertok(console_record_reset_enable());
989 ut_asserteq(1, run_commandf("fdt mknode noalias newfailsubnode"));
990 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
991 ut_assertok(ut_check_console_end(uts));
992
993 /* Test creation of new node in bad alias */
994 ut_assertok(console_record_reset_enable());
995 ut_asserteq(1, run_commandf("fdt mknode badalias newbadsubnode"));
996 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
997 ut_assertok(ut_check_console_end(uts));
998
999 return 0;
1000}
1001FDT_TEST(fdt_test_mknode, UT_TESTF_CONSOLE_REC);
1002
Marek Vasutc3e350f2023-03-02 04:08:38 +01001003static int fdt_test_rm(struct unit_test_state *uts)
1004{
1005 char fdt[4096];
1006 ulong addr;
1007
1008 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
1009 addr = map_to_sysmem(fdt);
1010 set_working_fdt_addr(addr);
1011
1012 /* Test removal of property in root node / */
1013 ut_assertok(console_record_reset_enable());
1014 ut_assertok(run_commandf("fdt print / compatible"));
1015 ut_assert_nextline("compatible = \"u-boot,fdt-test\"");
1016 ut_assertok(run_commandf("fdt rm / compatible"));
1017 ut_asserteq(1, run_commandf("fdt print / compatible"));
1018 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1019 ut_assertok(ut_check_console_end(uts));
1020
1021 /* Test removal of property clock-names in subnode /test-node@1234 */
1022 ut_assertok(console_record_reset_enable());
1023 ut_assertok(run_commandf("fdt print /test-node@1234 clock-names"));
1024 ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
1025 ut_assertok(run_commandf("fdt rm /test-node@1234 clock-names"));
1026 ut_asserteq(1, run_commandf("fdt print /test-node@1234 clock-names"));
1027 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1028 ut_assertok(ut_check_console_end(uts));
1029
1030 /* Test removal of property u-boot,empty-property in subnode /test-node@1234 by alias */
1031 ut_assertok(console_record_reset_enable());
1032 ut_assertok(run_commandf("fdt print testnodealias u-boot,empty-property"));
1033 ut_assert_nextline("testnodealias u-boot,empty-property");
1034 ut_assertok(run_commandf("fdt rm testnodealias u-boot,empty-property"));
1035 ut_asserteq(1, run_commandf("fdt print testnodealias u-boot,empty-property"));
1036 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1037 ut_assertok(ut_check_console_end(uts));
1038
1039 /* Test removal of non-existent property noprop in subnode /test-node@1234 */
1040 ut_assertok(console_record_reset_enable());
1041 ut_asserteq(1, run_commandf("fdt rm /test-node@1234 noprop"));
1042 ut_assert_nextline("libfdt fdt_delprop(): FDT_ERR_NOTFOUND");
1043 ut_assertok(ut_check_console_end(uts));
1044
1045 /* Test removal of non-existent node /no-node@5678 */
1046 ut_assertok(console_record_reset_enable());
1047 ut_asserteq(1, run_commandf("fdt rm /no-node@5678"));
1048 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1049 ut_assertok(ut_check_console_end(uts));
1050
1051 /* Test removal of subnode /test-node@1234/subnode by alias */
1052 ut_assertok(console_record_reset_enable());
1053 ut_assertok(run_commandf("fdt rm subnodealias"));
1054 ut_asserteq(1, run_commandf("fdt print /test-node@1234/subnode"));
1055 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1056 ut_assertok(ut_check_console_end(uts));
1057
1058 /* Test removal of node by non-existent alias */
1059 ut_assertok(console_record_reset_enable());
1060 ut_asserteq(1, run_commandf("fdt rm noalias"));
1061 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
1062 ut_assertok(ut_check_console_end(uts));
1063
1064 /* Test removal of node by bad alias */
1065 ut_assertok(console_record_reset_enable());
1066 ut_asserteq(1, run_commandf("fdt rm noalias"));
1067 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
1068 ut_assertok(ut_check_console_end(uts));
1069
1070 /* Test removal of node /test-node@1234 */
1071 ut_assertok(console_record_reset_enable());
1072 ut_assertok(run_commandf("fdt rm /test-node@1234"));
1073 ut_asserteq(1, run_commandf("fdt print /test-node@1234"));
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 / */
1078 ut_assertok(console_record_reset_enable());
1079 ut_assertok(run_commandf("fdt rm /"));
1080 ut_asserteq(1, run_commandf("fdt print /"));
1081 ut_assertok(ut_check_console_end(uts));
1082
1083 return 0;
1084}
1085FDT_TEST(fdt_test_rm, UT_TESTF_CONSOLE_REC);
1086
Marek Vasut2081b1d2023-03-02 04:08:40 +01001087static int fdt_test_bootcpu(struct unit_test_state *uts)
1088{
1089 char fdt[256];
1090 ulong addr;
1091 int i;
1092
1093 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1094 addr = map_to_sysmem(fdt);
1095 set_working_fdt_addr(addr);
1096
1097 /* Test getting default bootcpu entry */
1098 ut_assertok(console_record_reset_enable());
1099 ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
1100 ut_asserteq(0, env_get_ulong("bootcpu", 10, 0x1234));
1101 ut_assertok(ut_check_console_end(uts));
1102
1103 /* Test setting and getting new bootcpu entry, twice, to test overwrite */
1104 for (i = 42; i <= 43; i++) {
1105 ut_assertok(console_record_reset_enable());
1106 ut_assertok(run_commandf("fdt bootcpu %d", i));
1107 ut_assertok(ut_check_console_end(uts));
1108
1109 /* Test getting new bootcpu entry */
1110 ut_assertok(console_record_reset_enable());
1111 ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
1112 ut_asserteq(i, env_get_ulong("bootcpu", 10, 0x1234));
1113 ut_assertok(ut_check_console_end(uts));
1114 }
1115
1116 return 0;
1117}
1118FDT_TEST(fdt_test_bootcpu, UT_TESTF_CONSOLE_REC);
1119
Marek Vasut06c13ed2023-03-02 04:08:39 +01001120static int fdt_test_header_get(struct unit_test_state *uts,
1121 const char *field, const unsigned long val)
1122{
1123 /* Test getting valid header entry */
1124 ut_assertok(console_record_reset_enable());
1125 ut_assertok(run_commandf("fdt header get fvar %s", field));
1126 ut_asserteq(val, env_get_hex("fvar", 0x1234));
1127 ut_assertok(ut_check_console_end(uts));
1128
1129 /* Test getting malformed header entry */
1130 ut_assertok(console_record_reset_enable());
1131 ut_asserteq(1, run_commandf("fdt header get fvar typo%stypo", field));
1132 ut_assertok(ut_check_console_end(uts));
1133
1134 return 0;
1135}
1136
1137static int fdt_test_header(struct unit_test_state *uts)
1138{
1139 char fdt[256];
1140 ulong addr;
1141
1142 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1143 addr = map_to_sysmem(fdt);
1144 set_working_fdt_addr(addr);
1145
1146 /* Test header print */
1147 ut_assertok(console_record_reset_enable());
1148 ut_assertok(run_commandf("fdt header"));
1149 ut_assert_nextline("magic:\t\t\t0x%x", fdt_magic(fdt));
1150 ut_assert_nextline("totalsize:\t\t0x%x (%d)", fdt_totalsize(fdt), fdt_totalsize(fdt));
1151 ut_assert_nextline("off_dt_struct:\t\t0x%x", fdt_off_dt_struct(fdt));
1152 ut_assert_nextline("off_dt_strings:\t\t0x%x", fdt_off_dt_strings(fdt));
1153 ut_assert_nextline("off_mem_rsvmap:\t\t0x%x", fdt_off_mem_rsvmap(fdt));
1154 ut_assert_nextline("version:\t\t%d", fdt_version(fdt));
1155 ut_assert_nextline("last_comp_version:\t%d", fdt_last_comp_version(fdt));
1156 ut_assert_nextline("boot_cpuid_phys:\t0x%x", fdt_boot_cpuid_phys(fdt));
1157 ut_assert_nextline("size_dt_strings:\t0x%x", fdt_size_dt_strings(fdt));
1158 ut_assert_nextline("size_dt_struct:\t\t0x%x", fdt_size_dt_struct(fdt));
1159 ut_assert_nextline("number mem_rsv:\t\t0x%x", fdt_num_mem_rsv(fdt));
1160 ut_assert_nextline_empty();
1161 ut_assertok(ut_check_console_end(uts));
1162
1163 /* Test header get */
1164 fdt_test_header_get(uts, "magic", fdt_magic(fdt));
1165 fdt_test_header_get(uts, "totalsize", fdt_totalsize(fdt));
1166 fdt_test_header_get(uts, "off_dt_struct", fdt_off_dt_struct(fdt));
1167 fdt_test_header_get(uts, "off_dt_strings", fdt_off_dt_strings(fdt));
1168 fdt_test_header_get(uts, "off_mem_rsvmap", fdt_off_mem_rsvmap(fdt));
1169 fdt_test_header_get(uts, "version", fdt_version(fdt));
1170 fdt_test_header_get(uts, "last_comp_version", fdt_last_comp_version(fdt));
1171 fdt_test_header_get(uts, "boot_cpuid_phys", fdt_boot_cpuid_phys(fdt));
1172 fdt_test_header_get(uts, "size_dt_strings", fdt_size_dt_strings(fdt));
1173 fdt_test_header_get(uts, "size_dt_struct", fdt_size_dt_struct(fdt));
1174
1175 return 0;
1176}
1177FDT_TEST(fdt_test_header, UT_TESTF_CONSOLE_REC);
1178
Marek Vasut65b6f462023-03-02 04:08:41 +01001179static int fdt_test_memory_cells(struct unit_test_state *uts,
1180 const unsigned int cells)
1181{
1182 unsigned char *pada, *pads;
1183 unsigned char *seta, *sets;
1184 char fdt[8192];
1185 const int size = sizeof(fdt);
1186 fdt32_t *regs;
1187 ulong addr;
1188 char *spc;
1189 int i;
1190
1191 /* Create DT with node /memory { regs = <0x100 0x200>; } and #*cells */
1192 ut_assertnonnull(regs = calloc(2 * cells, sizeof(*regs)));
1193 ut_assertnonnull(pada = calloc(12, cells));
1194 ut_assertnonnull(pads = calloc(12, cells));
1195 ut_assertnonnull(seta = calloc(12, cells));
1196 ut_assertnonnull(sets = calloc(12, cells));
1197 for (i = cells; i >= 1; i--) {
1198 regs[cells - 1] = cpu_to_fdt32(i * 0x10000);
1199 regs[(cells * 2) - 1] = cpu_to_fdt32(~i);
1200 snprintf(seta + (8 * (cells - i)), 9, "%08x", i * 0x10000);
1201 snprintf(sets + (8 * (cells - i)), 9, "%08x", ~i);
1202 spc = (i != 1) ? " " : "";
1203 snprintf(pada + (11 * (cells - i)), 12, "0x%08x%s", i * 0x10000, spc);
1204 snprintf(pads + (11 * (cells - i)), 12, "0x%08x%s", ~i, spc);
1205 }
1206
1207 ut_assertok(fdt_create(fdt, size));
1208 ut_assertok(fdt_finish_reservemap(fdt));
1209 ut_assert(fdt_begin_node(fdt, "") >= 0);
1210 ut_assertok(fdt_property_u32(fdt, "#address-cells", cells));
1211 ut_assertok(fdt_property_u32(fdt, "#size-cells", cells));
1212 ut_assert(fdt_begin_node(fdt, "memory") >= 0);
1213 ut_assertok(fdt_property_string(fdt, "device_type", "memory"));
1214 ut_assertok(fdt_property(fdt, "reg", &regs, cells * 2));
1215 ut_assertok(fdt_end_node(fdt));
1216 ut_assertok(fdt_end_node(fdt));
1217 ut_assertok(fdt_finish(fdt));
1218 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1219 addr = map_to_sysmem(fdt);
1220 set_working_fdt_addr(addr);
1221
1222 /* Test updating the memory node */
1223 ut_assertok(console_record_reset_enable());
1224 ut_assertok(run_commandf("fdt memory 0x%s 0x%s", seta, sets));
1225 ut_assertok(run_commandf("fdt print /memory"));
1226 ut_assert_nextline("memory {");
1227 ut_assert_nextline("\tdevice_type = \"memory\";");
1228 ut_assert_nextline("\treg = <%s %s>;", pada, pads);
1229 ut_assert_nextline("};");
1230 ut_assertok(ut_check_console_end(uts));
1231
1232 free(sets);
1233 free(seta);
1234 free(pads);
1235 free(pada);
1236 free(regs);
1237
1238 return 0;
1239}
1240
1241static int fdt_test_memory(struct unit_test_state *uts)
1242{
1243 /*
1244 * Test memory fixup for 32 and 64 bit systems, anything bigger is
1245 * so far unsupported and fails because of simple_stroull() being
1246 * 64bit tops in the 'fdt memory' command implementation.
1247 */
1248 fdt_test_memory_cells(uts, 1);
1249 fdt_test_memory_cells(uts, 2);
1250
1251 /*
1252 * The 'fdt memory' command is limited to /memory node, it does
1253 * not support any other valid DT memory node format, which is
1254 * either one or multiple /memory@adresss nodes. Therefore, this
1255 * DT variant is not tested here.
1256 */
1257
1258 return 0;
1259}
1260FDT_TEST(fdt_test_memory, UT_TESTF_CONSOLE_REC);
1261
Marek Vasutcd162df2023-03-02 04:08:42 +01001262static int fdt_test_rsvmem(struct unit_test_state *uts)
1263{
1264 char fdt[8192];
1265 ulong addr;
1266
1267 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1268 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1269 fdt_add_mem_rsv(fdt, 0x42, 0x1701);
1270 fdt_add_mem_rsv(fdt, 0x74656, 0x9);
1271 addr = map_to_sysmem(fdt);
1272 set_working_fdt_addr(addr);
1273
1274 /* Test default reserved memory node presence */
1275 ut_assertok(console_record_reset_enable());
1276 ut_assertok(run_commandf("fdt rsvmem print"));
1277 ut_assert_nextline("index\t\t start\t\t size");
1278 ut_assert_nextline("------------------------------------------------");
1279 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
1280 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
1281 ut_assertok(ut_check_console_end(uts));
1282
1283 /* Test add new reserved memory node */
1284 ut_assertok(console_record_reset_enable());
1285 ut_assertok(run_commandf("fdt rsvmem add 0x1234 0x5678"));
1286 ut_assertok(run_commandf("fdt rsvmem print"));
1287 ut_assert_nextline("index\t\t start\t\t size");
1288 ut_assert_nextline("------------------------------------------------");
1289 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
1290 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
1291 ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x1234, 0x5678);
1292 ut_assertok(ut_check_console_end(uts));
1293
1294 /* Test delete reserved memory node */
1295 ut_assertok(console_record_reset_enable());
1296 ut_assertok(run_commandf("fdt rsvmem delete 0"));
1297 ut_assertok(run_commandf("fdt rsvmem print"));
1298 ut_assert_nextline("index\t\t start\t\t size");
1299 ut_assert_nextline("------------------------------------------------");
1300 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
1301 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
1302 ut_assertok(ut_check_console_end(uts));
1303
1304 /* Test re-add new reserved memory node */
1305 ut_assertok(console_record_reset_enable());
1306 ut_assertok(run_commandf("fdt rsvmem add 0x42 0x1701"));
1307 ut_assertok(run_commandf("fdt rsvmem print"));
1308 ut_assert_nextline("index\t\t start\t\t size");
1309 ut_assert_nextline("------------------------------------------------");
1310 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
1311 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
1312 ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x42, 0x1701);
1313 ut_assertok(ut_check_console_end(uts));
1314
1315 /* Test delete nonexistent reserved memory node */
1316 ut_assertok(console_record_reset_enable());
1317 ut_asserteq(1, run_commandf("fdt rsvmem delete 10"));
1318 ut_assert_nextline("libfdt fdt_del_mem_rsv(): FDT_ERR_NOTFOUND");
1319 ut_assertok(ut_check_console_end(uts));
1320
1321 return 0;
1322}
1323FDT_TEST(fdt_test_rsvmem, UT_TESTF_CONSOLE_REC);
1324
Marek Vasut8d450da2023-03-02 04:08:43 +01001325static int fdt_test_chosen(struct unit_test_state *uts)
1326{
1327 const char *env_bootargs = env_get("bootargs");
1328 char fdt[8192];
1329 ulong addr;
1330
1331 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1332 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1333 addr = map_to_sysmem(fdt);
1334 set_working_fdt_addr(addr);
1335
1336 /* Test default chosen node presence, fail as there is no /chosen node */
1337 ut_assertok(console_record_reset_enable());
1338 ut_asserteq(1, run_commandf("fdt print /chosen"));
1339 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1340 ut_assertok(ut_check_console_end(uts));
1341
1342 /* Test add new chosen node without initrd */
1343 ut_assertok(console_record_reset_enable());
1344 ut_assertok(run_commandf("fdt chosen"));
1345 ut_assertok(run_commandf("fdt print /chosen"));
1346 ut_assert_nextline("chosen {");
1347 ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
1348 if (env_bootargs)
1349 ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
1350 ut_assert_nextline("};");
1351 ut_assertok(ut_check_console_end(uts));
1352
1353 /* Test add new chosen node with initrd */
1354 ut_assertok(console_record_reset_enable());
1355 ut_assertok(run_commandf("fdt chosen 0x1234 0x5678"));
1356 ut_assertok(run_commandf("fdt print /chosen"));
1357 ut_assert_nextline("chosen {");
1358 ut_assert_nextline("\tlinux,initrd-end = <0x%08x 0x%08x>;",
1359 upper_32_bits(0x1234 + 0x5678 - 1),
1360 lower_32_bits(0x1234 + 0x5678 - 1));
1361 ut_assert_nextline("\tlinux,initrd-start = <0x%08x 0x%08x>;",
1362 upper_32_bits(0x1234), lower_32_bits(0x1234));
1363 ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
1364 if (env_bootargs)
1365 ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
1366 ut_assert_nextline("};");
1367 ut_assertok(ut_check_console_end(uts));
1368
1369 return 0;
1370}
1371FDT_TEST(fdt_test_chosen, UT_TESTF_CONSOLE_REC);
1372
Marek Vasutdc0ccea2023-03-02 04:08:44 +01001373static int fdt_test_apply(struct unit_test_state *uts)
1374{
1375 char fdt[8192], fdto[8192];
1376 ulong addr, addro;
1377
1378 /* Create base DT with __symbols__ node */
1379 ut_assertok(fdt_create(fdt, sizeof(fdt)));
1380 ut_assertok(fdt_finish_reservemap(fdt));
1381 ut_assert(fdt_begin_node(fdt, "") >= 0);
1382 ut_assert(fdt_begin_node(fdt, "__symbols__") >= 0);
1383 ut_assertok(fdt_end_node(fdt));
1384 ut_assertok(fdt_end_node(fdt));
1385 ut_assertok(fdt_finish(fdt));
1386 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1387 addr = map_to_sysmem(fdt);
1388 set_working_fdt_addr(addr);
1389
1390 /* Create DTO which adds single property to root node / */
1391 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1392 ut_assertok(fdt_finish_reservemap(fdto));
1393 ut_assert(fdt_begin_node(fdto, "") >= 0);
1394 ut_assert(fdt_begin_node(fdto, "fragment") >= 0);
1395 ut_assertok(fdt_property_string(fdto, "target-path", "/"));
1396 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1397 ut_assertok(fdt_property_string(fdto, "newstring", "newvalue"));
1398 ut_assertok(fdt_end_node(fdto));
1399 ut_assertok(fdt_end_node(fdto));
1400 ut_assertok(fdt_finish(fdto));
1401 addro = map_to_sysmem(fdto);
1402
1403 /* Test default DT print */
1404 ut_assertok(console_record_reset_enable());
1405 ut_assertok(run_commandf("fdt print /"));
1406 ut_assert_nextline("/ {");
1407 ut_assert_nextline("\t__symbols__ {");
1408 ut_assert_nextline("\t};");
1409 ut_assert_nextline("};");
1410 ut_assertok(ut_check_console_end(uts));
1411
1412 /* Test simple DTO application */
1413 ut_assertok(console_record_reset_enable());
Evgeny Bachinine604bc02023-03-20 11:23:13 +03001414 ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
Marek Vasutdc0ccea2023-03-02 04:08:44 +01001415 ut_assertok(run_commandf("fdt print /"));
1416 ut_assert_nextline("/ {");
1417 ut_assert_nextline("\tnewstring = \"newvalue\";");
1418 ut_assert_nextline("\t__symbols__ {");
1419 ut_assert_nextline("\t};");
1420 ut_assert_nextline("};");
1421 ut_assertok(ut_check_console_end(uts));
1422
1423 /*
1424 * Create complex DTO which:
1425 * - modifies newstring property in root node /
1426 * - adds new properties to root node /
1427 * - adds new subnode with properties to root node /
1428 * - adds phandle to the subnode and therefore __symbols__ node
1429 */
1430 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1431 ut_assertok(fdt_finish_reservemap(fdto));
1432 ut_assert(fdt_begin_node(fdto, "") >= 0);
1433 ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
1434 ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
1435
1436 ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
1437 ut_assertok(fdt_property_string(fdto, "target-path", "/"));
1438 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1439 ut_assertok(fdt_property_string(fdto, "newstring", "newervalue"));
1440 ut_assertok(fdt_property_u32(fdto, "newu32", 0x12345678));
1441 ut_assertok(fdt_property(fdto, "empty-property", NULL, 0));
1442 ut_assert(fdt_begin_node(fdto, "subnode") >= 0);
1443 ut_assertok(fdt_property_string(fdto, "subnewstring", "newervalue"));
1444 ut_assertok(fdt_property_u32(fdto, "subnewu32", 0x12345678));
1445 ut_assertok(fdt_property(fdto, "subempty-property", NULL, 0));
1446 ut_assertok(fdt_property_u32(fdto, "phandle", 0x01));
1447 ut_assertok(fdt_end_node(fdto));
1448 ut_assertok(fdt_end_node(fdto));
1449 ut_assertok(fdt_end_node(fdto));
1450
1451 ut_assert(fdt_begin_node(fdto, "__symbols__") >= 0);
1452 ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0/__overlay__/subnode"));
1453 ut_assertok(fdt_end_node(fdto));
1454 ut_assertok(fdt_finish(fdto));
1455 addro = map_to_sysmem(fdto);
1456
1457 /* Test complex DTO application */
1458 ut_assertok(console_record_reset_enable());
Evgeny Bachinine604bc02023-03-20 11:23:13 +03001459 ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
Marek Vasutdc0ccea2023-03-02 04:08:44 +01001460 ut_assertok(run_commandf("fdt print /"));
1461 ut_assert_nextline("/ {");
1462 ut_assert_nextline("\tempty-property;");
1463 ut_assert_nextline("\tnewu32 = <0x12345678>;");
1464 ut_assert_nextline("\tnewstring = \"newervalue\";");
1465 ut_assert_nextline("\tsubnode {");
1466 ut_assert_nextline("\t\tphandle = <0x00000001>;");
1467 ut_assert_nextline("\t\tsubempty-property;");
1468 ut_assert_nextline("\t\tsubnewu32 = <0x12345678>;");
1469 ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
1470 ut_assert_nextline("\t};");
1471 ut_assert_nextline("\t__symbols__ {");
1472 ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
1473 ut_assert_nextline("\t};");
1474 ut_assert_nextline("};");
1475 ut_assertok(ut_check_console_end(uts));
1476
1477 /*
1478 * Create complex DTO which:
1479 * - modifies subnewu32 property in subnode via phandle and uses __fixups__ node
1480 */
1481 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1482 ut_assertok(fdt_finish_reservemap(fdto));
1483 ut_assert(fdt_begin_node(fdto, "") >= 0);
1484 ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
1485 ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
1486
1487 ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
1488 ut_assertok(fdt_property_u32(fdto, "target", 0xffffffff));
1489 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1490 ut_assertok(fdt_property_u32(fdto, "subnewu32", 0xabcdef01));
1491 ut_assertok(fdt_end_node(fdto));
1492 ut_assertok(fdt_end_node(fdto));
1493
1494 ut_assert(fdt_begin_node(fdto, "__fixups__") >= 0);
1495 ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0:target:0"));
1496 ut_assertok(fdt_end_node(fdto));
1497 ut_assertok(fdt_end_node(fdto));
1498 ut_assertok(fdt_finish(fdto));
1499 addro = map_to_sysmem(fdto);
1500
1501 /* Test complex DTO application */
1502 ut_assertok(console_record_reset_enable());
Evgeny Bachinine604bc02023-03-20 11:23:13 +03001503 ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
Marek Vasutdc0ccea2023-03-02 04:08:44 +01001504 ut_assertok(run_commandf("fdt print /"));
1505 ut_assert_nextline("/ {");
1506 ut_assert_nextline("\tempty-property;");
1507 ut_assert_nextline("\tnewu32 = <0x12345678>;");
1508 ut_assert_nextline("\tnewstring = \"newervalue\";");
1509 ut_assert_nextline("\tsubnode {");
1510 ut_assert_nextline("\t\tphandle = <0x00000001>;");
1511 ut_assert_nextline("\t\tsubempty-property;");
1512 ut_assert_nextline("\t\tsubnewu32 = <0xabcdef01>;");
1513 ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
1514 ut_assert_nextline("\t};");
1515 ut_assert_nextline("\t__symbols__ {");
1516 ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
1517 ut_assert_nextline("\t};");
1518 ut_assert_nextline("};");
1519 ut_assertok(ut_check_console_end(uts));
1520
1521 return 0;
1522}
1523FDT_TEST(fdt_test_apply, UT_TESTF_CONSOLE_REC);
1524
Simon Glassf3c6a1d2022-07-13 06:06:59 -06001525int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1526{
1527 struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);
1528 const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test);
1529
1530 return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv);
1531}