blob: 22e8c7e3d26edd79144436a4c4681c530d9f4e31 [file] [log] [blame]
Simon Glassf3c6a1d2022-07-13 06:06:59 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for fdt command
4 *
5 * Copyright 2022 Google LLCmap_to_sysmem(fdt));
6 */
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));
163 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
164 ut_assertok(ut_check_console_end(uts));
165
166 /* Set up a working FDT and try again */
167 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
168 addr = map_to_sysmem(fdt);
169 set_working_fdt_addr(addr);
Simon Glass84328cf2022-10-11 09:47:12 -0600170 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600171 ut_assertok(run_command("fdt addr", 0));
172 ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt));
173 ut_assertok(ut_check_console_end(uts));
174
175 /* Set the working FDT */
176 set_working_fdt_addr(0);
Simon Glass84328cf2022-10-11 09:47:12 -0600177 ut_assert_nextline("Working FDT set to 0");
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600178 ut_assertok(run_commandf("fdt addr %08x", addr));
Simon Glass84328cf2022-10-11 09:47:12 -0600179 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600180 ut_asserteq(addr, map_to_sysmem(working_fdt));
181 ut_assertok(ut_check_console_end(uts));
182 set_working_fdt_addr(0);
Simon Glass84328cf2022-10-11 09:47:12 -0600183 ut_assert_nextline("Working FDT set to 0");
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600184
Simon Glass84328cf2022-10-11 09:47:12 -0600185 /* Set the control FDT */
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600186 fdt_blob = gd->fdt_blob;
187 gd->fdt_blob = NULL;
188 ret = run_commandf("fdt addr -c %08x", addr);
189 new_fdt = gd->fdt_blob;
190 gd->fdt_blob = fdt_blob;
191 ut_assertok(ret);
192 ut_asserteq(addr, map_to_sysmem(new_fdt));
193 ut_assertok(ut_check_console_end(uts));
194
195 /* Test setting an invalid FDT */
196 fdt[0] = 123;
197 ut_asserteq(1, run_commandf("fdt addr %08x", addr));
198 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
199 ut_assertok(ut_check_console_end(uts));
200
201 /* Test detecting an invalid FDT */
202 fdt[0] = 123;
203 set_working_fdt_addr(addr);
Simon Glass84328cf2022-10-11 09:47:12 -0600204 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600205 ut_asserteq(1, run_commandf("fdt addr"));
206 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
207 ut_assertok(ut_check_console_end(uts));
208
209 return 0;
210}
211FDT_TEST(fdt_test_addr, UT_TESTF_CONSOLE_REC);
212
213/* Test 'fdt addr' resizing an fdt */
Marek Vasut6e04a892023-03-02 04:08:25 +0100214static int fdt_test_addr_resize(struct unit_test_state *uts)
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600215{
216 char fdt[256];
217 const int newsize = sizeof(fdt) / 2;
218 ulong addr;
219
220 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
221 addr = map_to_sysmem(fdt);
222 set_working_fdt_addr(addr);
223
224 /* Test setting and resizing the working FDT to a larger size */
225 ut_assertok(console_record_reset_enable());
226 ut_assertok(run_commandf("fdt addr %08x %x", addr, newsize));
Simon Glass84328cf2022-10-11 09:47:12 -0600227 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600228 ut_assertok(ut_check_console_end(uts));
229
230 /* Try shrinking it */
231 ut_assertok(run_commandf("fdt addr %08x %x", addr, sizeof(fdt) / 4));
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_assert_nextline("New length %d < existing length %d, ignoring",
234 (int)sizeof(fdt) / 4, newsize);
235 ut_assertok(ut_check_console_end(uts));
236
237 /* ...quietly */
238 ut_assertok(run_commandf("fdt addr -q %08x %x", addr, sizeof(fdt) / 4));
Simon Glass84328cf2022-10-11 09:47:12 -0600239 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600240 ut_assertok(ut_check_console_end(uts));
241
242 /* We cannot easily provoke errors in fdt_open_into(), so ignore that */
243
244 return 0;
245}
Marek Vasut6e04a892023-03-02 04:08:25 +0100246FDT_TEST(fdt_test_addr_resize, UT_TESTF_CONSOLE_REC);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600247
Marek Vasutddf86202023-03-02 04:08:30 +0100248static int fdt_test_move(struct unit_test_state *uts)
249{
250 char fdt[256];
251 ulong addr, newaddr = 0x10000;
252 const int size = sizeof(fdt);
253 uint32_t ts;
254 void *buf;
255
256 /* Original source DT */
257 ut_assertok(make_test_fdt(uts, fdt, size));
258 ts = fdt_totalsize(fdt);
259 addr = map_to_sysmem(fdt);
260 set_working_fdt_addr(addr);
261
262 /* Moved target DT location */
263 buf = map_sysmem(newaddr, size);
264 memset(buf, 0, size);
265
266 /* Test moving the working FDT to a new location */
267 ut_assertok(console_record_reset_enable());
268 ut_assertok(run_commandf("fdt move %08x %08x %x", addr, newaddr, ts));
269 ut_assert_nextline("Working FDT set to %lx", newaddr);
270 ut_assertok(ut_check_console_end(uts));
271
272 /* Compare the source and destination DTs */
273 ut_assertok(console_record_reset_enable());
274 ut_assertok(run_commandf("cmp.b %08x %08x %x", addr, newaddr, ts));
275 ut_assert_nextline("Total of %d byte(s) were the same", ts);
276 ut_assertok(ut_check_console_end(uts));
277
278 return 0;
279}
280FDT_TEST(fdt_test_move, UT_TESTF_CONSOLE_REC);
281
Marek Vasut6ab4fe22023-03-02 04:08:31 +0100282static int fdt_test_resize(struct unit_test_state *uts)
283{
284 char fdt[256];
285 const unsigned int newsize = 0x2000;
286 uint32_t ts;
287 ulong addr;
288
289 /* Original source DT */
290 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
291 fdt_shrink_to_minimum(fdt, 0); /* Resize with 0 extra bytes */
292 ts = fdt_totalsize(fdt);
293 addr = map_to_sysmem(fdt);
294 set_working_fdt_addr(addr);
295
296 /* Test resizing the working FDT and verify the new space was added */
297 ut_assertok(console_record_reset_enable());
298 ut_assertok(run_commandf("fdt resize %x", newsize));
299 ut_asserteq(ts + newsize, fdt_totalsize(fdt));
300 ut_assertok(ut_check_console_end(uts));
301
302 return 0;
303}
304FDT_TEST(fdt_test_resize, UT_TESTF_CONSOLE_REC);
305
Marek Vasut76a652a2023-03-02 04:08:32 +0100306static int fdt_test_print_list_common(struct unit_test_state *uts,
307 const char *opc, const char *node)
308{
309 /*
310 * Test printing/listing the working FDT
311 * subnode $node/subnode
312 */
313 ut_assertok(console_record_reset_enable());
314 ut_assertok(run_commandf("fdt %s %s/subnode", opc, node));
315 ut_assert_nextline("subnode {");
316 ut_assert_nextline("\t#address-cells = <0x00000000>;");
317 ut_assert_nextline("\t#size-cells = <0x00000000>;");
318 ut_assert_nextline("\tcompatible = \"u-boot,fdt-subnode-test-device\";");
319 ut_assert_nextline("};");
320 ut_assertok(ut_check_console_end(uts));
321
322 /*
323 * Test printing/listing the working FDT
324 * path / string property model
325 */
326 ut_assertok(console_record_reset_enable());
327 ut_assertok(run_commandf("fdt %s / model", opc));
328 ut_assert_nextline("model = \"U-Boot FDT test\"");
329 ut_assertok(ut_check_console_end(uts));
330
331 /*
332 * Test printing/listing the working FDT
333 * path $node string property compatible
334 */
335 ut_assertok(console_record_reset_enable());
336 ut_assertok(run_commandf("fdt %s %s compatible", opc, node));
337 ut_assert_nextline("compatible = \"u-boot,fdt-test-device1\"");
338 ut_assertok(ut_check_console_end(uts));
339
340 /*
341 * Test printing/listing the working FDT
342 * path $node stringlist property clock-names
343 */
344 ut_assertok(console_record_reset_enable());
345 ut_assertok(run_commandf("fdt %s %s clock-names", opc, node));
346 ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
347 ut_assertok(ut_check_console_end(uts));
348
349 /*
350 * Test printing/listing the working FDT
351 * path $node u32 property clock-frequency
352 */
353 ut_assertok(console_record_reset_enable());
354 ut_assertok(run_commandf("fdt %s %s clock-frequency", opc, node));
355 ut_assert_nextline("clock-frequency = <0x00fde800>");
356 ut_assertok(ut_check_console_end(uts));
357
358 /*
359 * Test printing/listing the working FDT
360 * path $node empty property u-boot,empty-property
361 */
362 ut_assertok(console_record_reset_enable());
363 ut_assertok(run_commandf("fdt %s %s u-boot,empty-property", opc, node));
364 /*
365 * This is the only 'fdt print' / 'fdt list' incantation which
366 * prefixes the property with node path. This has been in U-Boot
367 * since the beginning of the command 'fdt', keep it.
368 */
369 ut_assert_nextline("%s u-boot,empty-property", node);
370 ut_assertok(ut_check_console_end(uts));
371
372 /*
373 * Test printing/listing the working FDT
374 * path $node prop-encoded array property regs
375 */
376 ut_assertok(console_record_reset_enable());
377 ut_assertok(run_commandf("fdt %s %s regs", opc, node));
378 ut_assert_nextline("regs = <0x00001234 0x00001000>");
379 ut_assertok(ut_check_console_end(uts));
380
381 return 0;
382}
383
384static int fdt_test_print_list(struct unit_test_state *uts, bool print)
385{
386 const char *opc = print ? "print" : "list";
387 char fdt[4096];
388 ulong addr;
389 int ret;
390
391 /* Original source DT */
392 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
393 addr = map_to_sysmem(fdt);
394 set_working_fdt_addr(addr);
395
396 /* Test printing/listing the working FDT -- node / */
397 ut_assertok(console_record_reset_enable());
398 ut_assertok(run_commandf("fdt %s", opc));
399 ut_assert_nextline("/ {");
400 ut_assert_nextline("\t#address-cells = <0x00000001>;");
401 ut_assert_nextline("\t#size-cells = <0x00000001>;");
402 ut_assert_nextline("\tcompatible = \"u-boot,fdt-test\";");
403 ut_assert_nextline("\tmodel = \"U-Boot FDT test\";");
404 ut_assert_nextline("\taliases {");
405 if (print) {
406 ut_assert_nextline("\t\tbadalias = \"/bad/alias\";");
407 ut_assert_nextline("\t\tsubnodealias = \"/test-node@1234/subnode\";");
408 ut_assert_nextline("\t\ttestnodealias = \"/test-node@1234\";");
409 }
410 ut_assert_nextline("\t};");
411 ut_assert_nextline("\ttest-node@1234 {");
412 if (print) {
413 ut_assert_nextline("\t\t#address-cells = <0x00000000>;");
414 ut_assert_nextline("\t\t#size-cells = <0x00000000>;");
415 ut_assert_nextline("\t\tcompatible = \"u-boot,fdt-test-device1\";");
416 ut_assert_nextline("\t\tclock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\";");
417 ut_assert_nextline("\t\tu-boot,empty-property;");
418 ut_assert_nextline("\t\tclock-frequency = <0x00fde800>;");
419 ut_assert_nextline("\t\tregs = <0x00001234 0x00001000>;");
420 ut_assert_nextline("\t\tsubnode {");
421 ut_assert_nextline("\t\t\t#address-cells = <0x00000000>;");
422 ut_assert_nextline("\t\t\t#size-cells = <0x00000000>;");
423 ut_assert_nextline("\t\t\tcompatible = \"u-boot,fdt-subnode-test-device\";");
424 ut_assert_nextline("\t\t};");
425 }
426 ut_assert_nextline("\t};");
427 ut_assert_nextline("};");
428 ut_assertok(ut_check_console_end(uts));
429
430 ret = fdt_test_print_list_common(uts, opc, "/test-node@1234");
431 if (!ret)
432 ret = fdt_test_print_list_common(uts, opc, "testnodealias");
433
434 return 0;
435}
436
437static int fdt_test_print(struct unit_test_state *uts)
438{
439 return fdt_test_print_list(uts, true);
440}
441FDT_TEST(fdt_test_print, UT_TESTF_CONSOLE_REC);
442
443static int fdt_test_list(struct unit_test_state *uts)
444{
445 return fdt_test_print_list(uts, false);
446}
447FDT_TEST(fdt_test_list, UT_TESTF_CONSOLE_REC);
448
Marek Vasutfbeca452023-03-02 04:08:26 +0100449/* Test 'fdt get value' reading an fdt */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100450static int fdt_test_get_value_string(struct unit_test_state *uts,
451 const char *node, const char *prop,
452 const char *idx, const char *strres,
453 const int intres)
454{
455 ut_assertok(console_record_reset_enable());
456 ut_assertok(run_commandf("fdt get value var %s %s %s",
457 node, prop, idx ? : ""));
458 if (strres) {
459 ut_asserteq_str(strres, env_get("var"));
460 } else {
461 ut_asserteq(intres, env_get_hex("var", 0x1234));
462 }
463 ut_assertok(ut_check_console_end(uts));
464
465 return 0;
466}
467
Marek Vasutb7517b42023-03-02 04:08:28 +0100468static int fdt_test_get_value_common(struct unit_test_state *uts,
469 const char *node)
Marek Vasut78c060d2022-11-14 22:50:00 +0100470{
Marek Vasutb7517b42023-03-02 04:08:28 +0100471 /* Test getting default element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100472 fdt_test_get_value_string(uts, node, "clock-names", NULL, "fixed", 0);
Marek Vasut78c060d2022-11-14 22:50:00 +0100473
Marek Vasutb7517b42023-03-02 04:08:28 +0100474 /* Test getting 0th element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100475 fdt_test_get_value_string(uts, node, "clock-names", "0", "fixed", 0);
Marek Vasut78c060d2022-11-14 22:50:00 +0100476
Marek Vasutb7517b42023-03-02 04:08:28 +0100477 /* Test getting 1st element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100478 fdt_test_get_value_string(uts, node, "clock-names", "1", "i2c", 0);
Marek Vasut78c060d2022-11-14 22:50:00 +0100479
Marek Vasutb7517b42023-03-02 04:08:28 +0100480 /* Test getting 2nd element of $node node clock-names property */
Marek Vasut9f90cad2023-03-02 04:08:29 +0100481 fdt_test_get_value_string(uts, node, "clock-names", "2", "spi", 0);
482
483 /*
484 * Test getting default element of $node node regs property.
485 * The result here is highly unusual, the non-index value read from
486 * integer array is a string of concatenated values from the array,
487 * but only if the array is shorter than 40 characters. Anything
488 * longer is an error. This is a special case for handling hashes.
489 */
490 fdt_test_get_value_string(uts, node, "regs", NULL, "3412000000100000", 0);
491
492 /* Test getting 0th element of $node node regs property */
493 fdt_test_get_value_string(uts, node, "regs", "0", NULL, 0x1234);
494
495 /* Test getting 1st element of $node node regs property */
496 fdt_test_get_value_string(uts, node, "regs", "1", NULL, 0x1000);
Marek Vasut78c060d2022-11-14 22:50:00 +0100497
Marek Vasutb7517b42023-03-02 04:08:28 +0100498 /* Test missing 10th element of $node node clock-names property */
Marek Vasut78c060d2022-11-14 22:50:00 +0100499 ut_assertok(console_record_reset_enable());
Marek Vasutb7517b42023-03-02 04:08:28 +0100500 ut_asserteq(1, run_commandf("fdt get value ften %s clock-names 10", node));
Marek Vasut78c060d2022-11-14 22:50:00 +0100501 ut_assertok(ut_check_console_end(uts));
502
Marek Vasut9f90cad2023-03-02 04:08:29 +0100503 /* Test missing 10th element of $node node regs property */
504 ut_assertok(console_record_reset_enable());
505 ut_asserteq(1, run_commandf("fdt get value ften %s regs 10", node));
506 ut_assertok(ut_check_console_end(uts));
507
Marek Vasutb7517b42023-03-02 04:08:28 +0100508 /* Test getting default element of $node node nonexistent property */
Marek Vasut78c060d2022-11-14 22:50:00 +0100509 ut_assertok(console_record_reset_enable());
Marek Vasutb7517b42023-03-02 04:08:28 +0100510 ut_asserteq(1, run_commandf("fdt get value fnone %s nonexistent", node));
Marek Vasut78c060d2022-11-14 22:50:00 +0100511 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
512 ut_assertok(ut_check_console_end(uts));
513
Marek Vasutb7517b42023-03-02 04:08:28 +0100514 return 0;
515}
516
517static int fdt_test_get_value(struct unit_test_state *uts)
518{
519 char fdt[4096];
520 ulong addr;
521 int ret;
522
523 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
524 addr = map_to_sysmem(fdt);
525 set_working_fdt_addr(addr);
526
527 ret = fdt_test_get_value_common(uts, "/test-node@1234");
528 if (!ret)
529 ret = fdt_test_get_value_common(uts, "testnodealias");
530 if (ret)
531 return ret;
532
Marek Vasut78c060d2022-11-14 22:50:00 +0100533 /* Test getting default element of /nonexistent node */
534 ut_assertok(console_record_reset_enable());
535 ut_asserteq(1, run_command("fdt get value fnode /nonexistent nonexistent", 1));
536 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
537 ut_assertok(ut_check_console_end(uts));
538
Marek Vasutb7517b42023-03-02 04:08:28 +0100539 /* Test getting default element of bad alias */
540 ut_assertok(console_record_reset_enable());
541 ut_asserteq(1, run_command("fdt get value vbadalias badalias nonexistent", 1));
542 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
543 ut_assertok(ut_check_console_end(uts));
544
545 /* Test getting default element of nonexistent alias */
546 ut_assertok(console_record_reset_enable());
547 ut_asserteq(1, run_command("fdt get value vnoalias noalias nonexistent", 1));
548 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
549 ut_assertok(ut_check_console_end(uts));
550
Marek Vasut78c060d2022-11-14 22:50:00 +0100551 return 0;
552}
Marek Vasutfbeca452023-03-02 04:08:26 +0100553FDT_TEST(fdt_test_get_value, UT_TESTF_CONSOLE_REC);
Marek Vasut78c060d2022-11-14 22:50:00 +0100554
Marek Vasut84499932023-03-02 04:08:33 +0100555static int fdt_test_get_name(struct unit_test_state *uts)
556{
557 char fdt[4096];
558 ulong addr;
559
560 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
561 addr = map_to_sysmem(fdt);
562 set_working_fdt_addr(addr);
563
564 /* Test getting name of node 0 in /, which is /aliases node */
565 ut_assertok(console_record_reset_enable());
566 ut_assertok(run_command("fdt get name nzero / 0", 0));
567 ut_asserteq_str("aliases", env_get("nzero"));
568 ut_assertok(ut_check_console_end(uts));
569
570 /* Test getting name of node 1 in /, which is /test-node@1234 node */
571 ut_assertok(console_record_reset_enable());
572 ut_assertok(run_command("fdt get name none / 1", 0));
573 ut_asserteq_str("test-node@1234", env_get("none"));
574 ut_assertok(ut_check_console_end(uts));
575
576 /* Test getting name of node -1 in /, which is /aliases node, same as 0 */
577 ut_assertok(console_record_reset_enable());
578 ut_assertok(run_command("fdt get name nmone / -1", 0));
579 ut_asserteq_str("aliases", env_get("nmone"));
580 ut_assertok(ut_check_console_end(uts));
581
582 /* Test getting name of node 2 in /, which does not exist */
583 ut_assertok(console_record_reset_enable());
584 ut_asserteq(1, run_command("fdt get name ntwo / 2", 1));
585 ut_assert_nextline("libfdt node not found");
586 ut_assertok(ut_check_console_end(uts));
587
588 /* Test getting name of node 0 in /test-node@1234, which is /subnode node */
589 ut_assertok(console_record_reset_enable());
590 ut_assertok(run_command("fdt get name snzero /test-node@1234 0", 0));
591 ut_asserteq_str("subnode", env_get("snzero"));
592 ut_assertok(run_command("fdt get name asnzero testnodealias 0", 0));
593 ut_asserteq_str("subnode", env_get("asnzero"));
594 ut_assertok(ut_check_console_end(uts));
595
596 /* Test getting name of node 1 in /test-node@1234, which does not exist */
597 ut_assertok(console_record_reset_enable());
598 ut_asserteq(1, run_command("fdt get name snone /test-node@1234 1", 1));
599 ut_assert_nextline("libfdt node not found");
600 ut_asserteq(1, run_command("fdt get name asnone testnodealias 1", 1));
601 ut_assert_nextline("libfdt node not found");
602 ut_assertok(ut_check_console_end(uts));
603
604 /* Test getting name of node -1 in /test-node@1234, which is /subnode node, same as 0 */
605 ut_assertok(console_record_reset_enable());
606 ut_assertok(run_command("fdt get name snmone /test-node@1234 -1", 0));
607 ut_asserteq_str("subnode", env_get("snmone"));
608 ut_assertok(run_command("fdt get name asnmone testnodealias -1", 0));
609 ut_asserteq_str("subnode", env_get("asnmone"));
610 ut_assertok(ut_check_console_end(uts));
611
612 /* Test getting name of nonexistent node */
613 ut_assertok(console_record_reset_enable());
614 ut_asserteq(1, run_command("fdt get name nonode /nonexistent 0", 1));
615 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
616 ut_assertok(ut_check_console_end(uts));
617
618 /* Test getting name of bad alias */
619 ut_assertok(console_record_reset_enable());
620 ut_asserteq(1, run_command("fdt get name vbadalias badalias 0", 1));
621 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
622 ut_assertok(ut_check_console_end(uts));
623
624 /* Test getting name of nonexistent alias */
625 ut_assertok(console_record_reset_enable());
626 ut_asserteq(1, run_command("fdt get name vnoalias noalias 0", 1));
627 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
628 ut_assertok(ut_check_console_end(uts));
629
630 return 0;
631}
632FDT_TEST(fdt_test_get_name, UT_TESTF_CONSOLE_REC);
633
Marek Vasutcadef862023-03-02 04:08:34 +0100634static int fdt_test_get_addr_common(struct unit_test_state *uts, char *fdt,
635 const char *path, const char *prop)
636{
637 unsigned int offset;
638 int path_offset;
639 void *prop_ptr;
640 int len = 0;
641
642 path_offset = fdt_path_offset(fdt, path);
643 ut_assert(path_offset >= 0);
644 prop_ptr = (void *)fdt_getprop(fdt, path_offset, prop, &len);
645 ut_assertnonnull(prop_ptr);
646 offset = (char *)prop_ptr - fdt;
647
648 ut_assertok(console_record_reset_enable());
649 ut_assertok(run_commandf("fdt get addr pstr %s %s", path, prop));
650 ut_asserteq((ulong)map_sysmem(env_get_hex("fdtaddr", 0x1234), 0),
651 (ulong)(map_sysmem(env_get_hex("pstr", 0x1234), 0) - offset));
652 ut_assertok(ut_check_console_end(uts));
653
654 return 0;
655}
656
657static int fdt_test_get_addr(struct unit_test_state *uts)
658{
659 char fdt[4096];
660 ulong addr;
661
662 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
663 addr = map_to_sysmem(fdt);
664 set_working_fdt_addr(addr);
665
666 /* Test getting address of root node / string property "compatible" */
667 fdt_test_get_addr_common(uts, fdt, "/", "compatible");
668
669 /* Test getting address of node /test-node@1234 stringlist property "clock-names" */
670 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-names");
671 fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-names");
672
673 /* Test getting address of node /test-node@1234 u32 property "clock-frequency" */
674 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-frequency");
675 fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-frequency");
676
677 /* Test getting address of node /test-node@1234 empty property "u-boot,empty-property" */
678 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "u-boot,empty-property");
679 fdt_test_get_addr_common(uts, fdt, "testnodealias", "u-boot,empty-property");
680
681 /* Test getting address of node /test-node@1234 array property "regs" */
682 fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "regs");
683 fdt_test_get_addr_common(uts, fdt, "testnodealias", "regs");
684
685 /* Test getting address of node /test-node@1234/subnode non-existent property "noprop" */
686 ut_assertok(console_record_reset_enable());
687 ut_asserteq(1, run_command("fdt get addr pnoprop /test-node@1234/subnode noprop", 1));
688 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
689 ut_assertok(ut_check_console_end(uts));
690
691 /* Test getting address of non-existent node /test-node@1234/nonode@1 property "noprop" */
692 ut_assertok(console_record_reset_enable());
693 ut_asserteq(1, run_command("fdt get addr pnonode /test-node@1234/nonode@1 noprop", 1));
694 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
695 ut_assertok(ut_check_console_end(uts));
696
697 return 0;
698}
699FDT_TEST(fdt_test_get_addr, UT_TESTF_CONSOLE_REC);
700
Marek Vasutce1b0482023-03-02 04:08:35 +0100701static int fdt_test_get_size_common(struct unit_test_state *uts,
702 const char *path, const char *prop,
703 const unsigned int val)
704{
705 ut_assertok(console_record_reset_enable());
706 if (prop) {
707 ut_assertok(run_commandf("fdt get size sstr %s %s", path, prop));
708 } else {
709 ut_assertok(run_commandf("fdt get size sstr %s", path));
710 }
711 ut_asserteq(val, env_get_hex("sstr", 0x1234));
712 ut_assertok(ut_check_console_end(uts));
713
714 return 0;
715}
716
717static int fdt_test_get_size(struct unit_test_state *uts)
718{
719 char fdt[4096];
720 ulong addr;
721
722 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
723 addr = map_to_sysmem(fdt);
724 set_working_fdt_addr(addr);
725
726 /* Test getting size of root node / string property "compatible" */
727 fdt_test_get_size_common(uts, "/", "compatible", 16);
728
729 /* Test getting size of node /test-node@1234 stringlist property "clock-names" */
730 fdt_test_get_size_common(uts, "/test-node@1234", "clock-names", 26);
731 fdt_test_get_size_common(uts, "testnodealias", "clock-names", 26);
732
733 /* Test getting size of node /test-node@1234 u32 property "clock-frequency" */
734 fdt_test_get_size_common(uts, "/test-node@1234", "clock-frequency", 4);
735 fdt_test_get_size_common(uts, "testnodealias", "clock-frequency", 4);
736
737 /* Test getting size of node /test-node@1234 empty property "u-boot,empty-property" */
738 fdt_test_get_size_common(uts, "/test-node@1234", "u-boot,empty-property", 0);
739 fdt_test_get_size_common(uts, "testnodealias", "u-boot,empty-property", 0);
740
741 /* Test getting size of node /test-node@1234 array property "regs" */
742 fdt_test_get_size_common(uts, "/test-node@1234", "regs", 8);
743 fdt_test_get_size_common(uts, "testnodealias", "regs", 8);
744
745 /* Test getting node count of node / */
746 fdt_test_get_size_common(uts, "/", NULL, 2);
747
748 /* Test getting node count of node /test-node@1234/subnode */
749 fdt_test_get_size_common(uts, "/test-node@1234/subnode", NULL, 0);
750 fdt_test_get_size_common(uts, "subnodealias", NULL, 0);
751
752 /* Test getting size of node /test-node@1234/subnode non-existent property "noprop" */
753 ut_assertok(console_record_reset_enable());
754 ut_asserteq(1, run_command("fdt get size pnoprop /test-node@1234/subnode noprop", 1));
755 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
756 ut_asserteq(1, run_command("fdt get size pnoprop subnodealias noprop", 1));
757 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
758 ut_assertok(ut_check_console_end(uts));
759
760 /* Test getting size of non-existent node /test-node@1234/nonode@1 property "noprop" */
761 ut_assertok(console_record_reset_enable());
762 ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1 noprop", 1));
763 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
764 ut_assertok(ut_check_console_end(uts));
765
766 /* Test getting node count of non-existent node /test-node@1234/nonode@1 */
767 ut_assertok(console_record_reset_enable());
768 ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1", 1));
769 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
770 ut_assertok(ut_check_console_end(uts));
771
772 /* Test getting node count of bad alias badalias */
773 ut_assertok(console_record_reset_enable());
774 ut_asserteq(1, run_command("fdt get size pnonode badalias noprop", 1));
775 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
776 ut_assertok(ut_check_console_end(uts));
777
778 /* Test getting node count of non-existent alias noalias */
779 ut_assertok(console_record_reset_enable());
780 ut_asserteq(1, run_command("fdt get size pnonode noalias", 1));
781 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
782 ut_assertok(ut_check_console_end(uts));
783
784 return 0;
785}
786FDT_TEST(fdt_test_get_size, UT_TESTF_CONSOLE_REC);
787
Marek Vasutf180de42023-03-02 04:08:36 +0100788static int fdt_test_set_single(struct unit_test_state *uts,
789 const char *path, const char *prop,
790 const char *sval, int ival, bool integer)
791{
792 /*
793 * Set single element string/integer/<empty> property into DT, that is:
794 * => fdt set /path property string
795 * => fdt set /path property integer
796 * => fdt set /path property
797 */
798 ut_assertok(console_record_reset_enable());
Marek Vasutbc8c1402023-03-10 04:33:14 +0100799 if (sval)
Marek Vasutf180de42023-03-02 04:08:36 +0100800 ut_assertok(run_commandf("fdt set %s %s %s", path, prop, sval));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100801 else if (integer)
Marek Vasutf180de42023-03-02 04:08:36 +0100802 ut_assertok(run_commandf("fdt set %s %s <%d>", path, prop, ival));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100803 else
Marek Vasutf180de42023-03-02 04:08:36 +0100804 ut_assertok(run_commandf("fdt set %s %s", path, prop));
Marek Vasutf180de42023-03-02 04:08:36 +0100805
806 /* Validate the property is present and has correct value. */
807 ut_assertok(run_commandf("fdt get value svar %s %s", path, prop));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100808 if (sval)
Marek Vasutf180de42023-03-02 04:08:36 +0100809 ut_asserteq_str(sval, env_get("svar"));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100810 else if (integer)
Marek Vasutf180de42023-03-02 04:08:36 +0100811 ut_asserteq(ival, env_get_hex("svar", 0x1234));
Marek Vasutbc8c1402023-03-10 04:33:14 +0100812 else
Marek Vasutf180de42023-03-02 04:08:36 +0100813 ut_assertnull(env_get("svar"));
Marek Vasutf180de42023-03-02 04:08:36 +0100814 ut_assertok(ut_check_console_end(uts));
815
816 return 0;
817}
818
819static int fdt_test_set_multi(struct unit_test_state *uts,
820 const char *path, const char *prop,
821 const char *sval1, const char *sval2,
822 int ival1, int ival2)
823{
824 /*
825 * Set multi element string/integer array property in DT, that is:
826 * => fdt set /path property <string1 string2>
827 * => fdt set /path property <integer1 integer2>
828 *
829 * The set is done twice in here deliberately, The first set adds
830 * the property with an extra trailing element in its array to make
831 * the array longer, the second set is the expected final content of
832 * the array property. The longer array is used to verify that the
833 * new array is correctly sized and read past the new array length
834 * triggers failure.
835 */
836 ut_assertok(console_record_reset_enable());
837 if (sval1 && sval2) {
838 ut_assertok(run_commandf("fdt set %s %s %s %s end", path, prop, sval1, sval2));
839 ut_assertok(run_commandf("fdt set %s %s %s %s", path, prop, sval1, sval2));
840 } else {
841 ut_assertok(run_commandf("fdt set %s %s <%d %d 10>", path, prop, ival1, ival2));
842 ut_assertok(run_commandf("fdt set %s %s <%d %d>", path, prop, ival1, ival2));
843 }
844
845 /*
846 * Validate the property is present and has correct value.
847 *
848 * The "end/10" above and "svarn" below is used to validate that
849 * previous 'fdt set' to longer array does not polute newly set
850 * shorter array.
851 */
852 ut_assertok(run_commandf("fdt get value svar1 %s %s 0", path, prop));
853 ut_assertok(run_commandf("fdt get value svar2 %s %s 1", path, prop));
854 ut_asserteq(1, run_commandf("fdt get value svarn %s %s 2", path, prop));
855 if (sval1 && sval2) {
856 ut_asserteq_str(sval1, env_get("svar1"));
857 ut_asserteq_str(sval2, env_get("svar2"));
858 ut_assertnull(env_get("svarn"));
859 } else {
860 ut_asserteq(ival1, env_get_hex("svar1", 0x1234));
861 ut_asserteq(ival2, env_get_hex("svar2", 0x1234));
862 ut_assertnull(env_get("svarn"));
863 }
864 ut_assertok(ut_check_console_end(uts));
865
866 return 0;
867}
868
869static int fdt_test_set_node(struct unit_test_state *uts,
870 const char *path, const char *prop)
871{
872 fdt_test_set_single(uts, path, prop, "new", 0, false);
873 fdt_test_set_single(uts, path, prop, "rewrite", 0, false);
874 fdt_test_set_single(uts, path, prop, NULL, 42, true);
875 fdt_test_set_single(uts, path, prop, NULL, 0, false);
876 fdt_test_set_multi(uts, path, prop, NULL, NULL, 42, 1701);
877 fdt_test_set_multi(uts, path, prop, NULL, NULL, 74656, 9);
878 fdt_test_set_multi(uts, path, prop, "42", "1701", 0, 0);
879 fdt_test_set_multi(uts, path, prop, "74656", "9", 0, 0);
880
881 return 0;
882}
883
884static int fdt_test_set(struct unit_test_state *uts)
885{
886 char fdt[8192];
887 ulong addr;
888
889 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
890 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
891 addr = map_to_sysmem(fdt);
892 set_working_fdt_addr(addr);
893
894 /* Test setting of root node / existing property "compatible" */
895 fdt_test_set_node(uts, "/", "compatible");
896
897 /* Test setting of root node / new property "newproperty" */
898 fdt_test_set_node(uts, "/", "newproperty");
899
900 /* Test setting of subnode existing property "compatible" */
901 fdt_test_set_node(uts, "/test-node@1234/subnode", "compatible");
902 fdt_test_set_node(uts, "subnodealias", "compatible");
903
904 /* Test setting of subnode new property "newproperty" */
905 fdt_test_set_node(uts, "/test-node@1234/subnode", "newproperty");
906 fdt_test_set_node(uts, "subnodealias", "newproperty");
907
908 /* Test setting property of non-existent node */
909 ut_assertok(console_record_reset_enable());
910 ut_asserteq(1, run_command("fdt set /no-node noprop", 1));
911 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
912 ut_assertok(ut_check_console_end(uts));
913
914 /* Test setting property of non-existent alias */
915 ut_assertok(console_record_reset_enable());
916 ut_asserteq(1, run_command("fdt set noalias noprop", 1));
917 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
918 ut_assertok(ut_check_console_end(uts));
919
920 /* Test setting property of bad alias */
921 ut_assertok(console_record_reset_enable());
922 ut_asserteq(1, run_command("fdt set badalias noprop", 1));
923 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
924 ut_assertok(ut_check_console_end(uts));
925
926 return 0;
927}
928FDT_TEST(fdt_test_set, UT_TESTF_CONSOLE_REC);
929
Marek Vasute4d26c92023-03-02 04:08:37 +0100930static int fdt_test_mknode(struct unit_test_state *uts)
931{
932 char fdt[8192];
933 ulong addr;
934
935 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
936 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
937 addr = map_to_sysmem(fdt);
938 set_working_fdt_addr(addr);
939
940 /* Test creation of new node in / */
941 ut_assertok(console_record_reset_enable());
942 ut_assertok(run_commandf("fdt mknode / newnode"));
943 ut_assertok(run_commandf("fdt list /newnode"));
944 ut_assert_nextline("newnode {");
945 ut_assert_nextline("};");
946 ut_assertok(ut_check_console_end(uts));
947
948 /* Test creation of new node in /test-node@1234 */
949 ut_assertok(console_record_reset_enable());
950 ut_assertok(run_commandf("fdt mknode /test-node@1234 newsubnode"));
951 ut_assertok(run_commandf("fdt list /test-node@1234/newsubnode"));
952 ut_assert_nextline("newsubnode {");
953 ut_assert_nextline("};");
954 ut_assertok(ut_check_console_end(uts));
955
956 /* Test creation of new node in /test-node@1234 by alias */
957 ut_assertok(console_record_reset_enable());
958 ut_assertok(run_commandf("fdt mknode testnodealias newersubnode"));
959 ut_assertok(run_commandf("fdt list testnodealias/newersubnode"));
960 ut_assert_nextline("newersubnode {");
961 ut_assert_nextline("};");
962 ut_assertok(ut_check_console_end(uts));
963
964 /* Test creation of new node in /test-node@1234 over existing node */
965 ut_assertok(console_record_reset_enable());
966 ut_asserteq(1, run_commandf("fdt mknode testnodealias newsubnode"));
967 ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
968 ut_assertok(ut_check_console_end(uts));
969
970 /* Test creation of new node in /test-node@1234 by alias over existing node */
971 ut_assertok(console_record_reset_enable());
972 ut_asserteq(1, run_commandf("fdt mknode testnodealias newersubnode"));
973 ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
974 ut_assertok(ut_check_console_end(uts));
975
976 /* Test creation of new node in non-existent node */
977 ut_assertok(console_record_reset_enable());
978 ut_asserteq(1, run_commandf("fdt mknode /no-node newnosubnode"));
979 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
980 ut_assertok(ut_check_console_end(uts));
981
982 /* Test creation of new node in non-existent alias */
983 ut_assertok(console_record_reset_enable());
984 ut_asserteq(1, run_commandf("fdt mknode noalias newfailsubnode"));
985 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
986 ut_assertok(ut_check_console_end(uts));
987
988 /* Test creation of new node in bad alias */
989 ut_assertok(console_record_reset_enable());
990 ut_asserteq(1, run_commandf("fdt mknode badalias newbadsubnode"));
991 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
992 ut_assertok(ut_check_console_end(uts));
993
994 return 0;
995}
996FDT_TEST(fdt_test_mknode, UT_TESTF_CONSOLE_REC);
997
Marek Vasutc3e350f2023-03-02 04:08:38 +0100998static int fdt_test_rm(struct unit_test_state *uts)
999{
1000 char fdt[4096];
1001 ulong addr;
1002
1003 ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
1004 addr = map_to_sysmem(fdt);
1005 set_working_fdt_addr(addr);
1006
1007 /* Test removal of property in root node / */
1008 ut_assertok(console_record_reset_enable());
1009 ut_assertok(run_commandf("fdt print / compatible"));
1010 ut_assert_nextline("compatible = \"u-boot,fdt-test\"");
1011 ut_assertok(run_commandf("fdt rm / compatible"));
1012 ut_asserteq(1, run_commandf("fdt print / compatible"));
1013 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1014 ut_assertok(ut_check_console_end(uts));
1015
1016 /* Test removal of property clock-names in subnode /test-node@1234 */
1017 ut_assertok(console_record_reset_enable());
1018 ut_assertok(run_commandf("fdt print /test-node@1234 clock-names"));
1019 ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
1020 ut_assertok(run_commandf("fdt rm /test-node@1234 clock-names"));
1021 ut_asserteq(1, run_commandf("fdt print /test-node@1234 clock-names"));
1022 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1023 ut_assertok(ut_check_console_end(uts));
1024
1025 /* Test removal of property u-boot,empty-property in subnode /test-node@1234 by alias */
1026 ut_assertok(console_record_reset_enable());
1027 ut_assertok(run_commandf("fdt print testnodealias u-boot,empty-property"));
1028 ut_assert_nextline("testnodealias u-boot,empty-property");
1029 ut_assertok(run_commandf("fdt rm testnodealias u-boot,empty-property"));
1030 ut_asserteq(1, run_commandf("fdt print testnodealias u-boot,empty-property"));
1031 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
1032 ut_assertok(ut_check_console_end(uts));
1033
1034 /* Test removal of non-existent property noprop in subnode /test-node@1234 */
1035 ut_assertok(console_record_reset_enable());
1036 ut_asserteq(1, run_commandf("fdt rm /test-node@1234 noprop"));
1037 ut_assert_nextline("libfdt fdt_delprop(): FDT_ERR_NOTFOUND");
1038 ut_assertok(ut_check_console_end(uts));
1039
1040 /* Test removal of non-existent node /no-node@5678 */
1041 ut_assertok(console_record_reset_enable());
1042 ut_asserteq(1, run_commandf("fdt rm /no-node@5678"));
1043 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1044 ut_assertok(ut_check_console_end(uts));
1045
1046 /* Test removal of subnode /test-node@1234/subnode by alias */
1047 ut_assertok(console_record_reset_enable());
1048 ut_assertok(run_commandf("fdt rm subnodealias"));
1049 ut_asserteq(1, run_commandf("fdt print /test-node@1234/subnode"));
1050 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1051 ut_assertok(ut_check_console_end(uts));
1052
1053 /* Test removal of node by non-existent alias */
1054 ut_assertok(console_record_reset_enable());
1055 ut_asserteq(1, run_commandf("fdt rm noalias"));
1056 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
1057 ut_assertok(ut_check_console_end(uts));
1058
1059 /* Test removal of node by bad alias */
1060 ut_assertok(console_record_reset_enable());
1061 ut_asserteq(1, run_commandf("fdt rm noalias"));
1062 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
1063 ut_assertok(ut_check_console_end(uts));
1064
1065 /* Test removal of node /test-node@1234 */
1066 ut_assertok(console_record_reset_enable());
1067 ut_assertok(run_commandf("fdt rm /test-node@1234"));
1068 ut_asserteq(1, run_commandf("fdt print /test-node@1234"));
1069 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1070 ut_assertok(ut_check_console_end(uts));
1071
1072 /* Test removal of node / */
1073 ut_assertok(console_record_reset_enable());
1074 ut_assertok(run_commandf("fdt rm /"));
1075 ut_asserteq(1, run_commandf("fdt print /"));
1076 ut_assertok(ut_check_console_end(uts));
1077
1078 return 0;
1079}
1080FDT_TEST(fdt_test_rm, UT_TESTF_CONSOLE_REC);
1081
Marek Vasut2081b1d2023-03-02 04:08:40 +01001082static int fdt_test_bootcpu(struct unit_test_state *uts)
1083{
1084 char fdt[256];
1085 ulong addr;
1086 int i;
1087
1088 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1089 addr = map_to_sysmem(fdt);
1090 set_working_fdt_addr(addr);
1091
1092 /* Test getting default bootcpu entry */
1093 ut_assertok(console_record_reset_enable());
1094 ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
1095 ut_asserteq(0, env_get_ulong("bootcpu", 10, 0x1234));
1096 ut_assertok(ut_check_console_end(uts));
1097
1098 /* Test setting and getting new bootcpu entry, twice, to test overwrite */
1099 for (i = 42; i <= 43; i++) {
1100 ut_assertok(console_record_reset_enable());
1101 ut_assertok(run_commandf("fdt bootcpu %d", i));
1102 ut_assertok(ut_check_console_end(uts));
1103
1104 /* Test getting new bootcpu entry */
1105 ut_assertok(console_record_reset_enable());
1106 ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
1107 ut_asserteq(i, env_get_ulong("bootcpu", 10, 0x1234));
1108 ut_assertok(ut_check_console_end(uts));
1109 }
1110
1111 return 0;
1112}
1113FDT_TEST(fdt_test_bootcpu, UT_TESTF_CONSOLE_REC);
1114
Marek Vasut06c13ed2023-03-02 04:08:39 +01001115static int fdt_test_header_get(struct unit_test_state *uts,
1116 const char *field, const unsigned long val)
1117{
1118 /* Test getting valid header entry */
1119 ut_assertok(console_record_reset_enable());
1120 ut_assertok(run_commandf("fdt header get fvar %s", field));
1121 ut_asserteq(val, env_get_hex("fvar", 0x1234));
1122 ut_assertok(ut_check_console_end(uts));
1123
1124 /* Test getting malformed header entry */
1125 ut_assertok(console_record_reset_enable());
1126 ut_asserteq(1, run_commandf("fdt header get fvar typo%stypo", field));
1127 ut_assertok(ut_check_console_end(uts));
1128
1129 return 0;
1130}
1131
1132static int fdt_test_header(struct unit_test_state *uts)
1133{
1134 char fdt[256];
1135 ulong addr;
1136
1137 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1138 addr = map_to_sysmem(fdt);
1139 set_working_fdt_addr(addr);
1140
1141 /* Test header print */
1142 ut_assertok(console_record_reset_enable());
1143 ut_assertok(run_commandf("fdt header"));
1144 ut_assert_nextline("magic:\t\t\t0x%x", fdt_magic(fdt));
1145 ut_assert_nextline("totalsize:\t\t0x%x (%d)", fdt_totalsize(fdt), fdt_totalsize(fdt));
1146 ut_assert_nextline("off_dt_struct:\t\t0x%x", fdt_off_dt_struct(fdt));
1147 ut_assert_nextline("off_dt_strings:\t\t0x%x", fdt_off_dt_strings(fdt));
1148 ut_assert_nextline("off_mem_rsvmap:\t\t0x%x", fdt_off_mem_rsvmap(fdt));
1149 ut_assert_nextline("version:\t\t%d", fdt_version(fdt));
1150 ut_assert_nextline("last_comp_version:\t%d", fdt_last_comp_version(fdt));
1151 ut_assert_nextline("boot_cpuid_phys:\t0x%x", fdt_boot_cpuid_phys(fdt));
1152 ut_assert_nextline("size_dt_strings:\t0x%x", fdt_size_dt_strings(fdt));
1153 ut_assert_nextline("size_dt_struct:\t\t0x%x", fdt_size_dt_struct(fdt));
1154 ut_assert_nextline("number mem_rsv:\t\t0x%x", fdt_num_mem_rsv(fdt));
1155 ut_assert_nextline_empty();
1156 ut_assertok(ut_check_console_end(uts));
1157
1158 /* Test header get */
1159 fdt_test_header_get(uts, "magic", fdt_magic(fdt));
1160 fdt_test_header_get(uts, "totalsize", fdt_totalsize(fdt));
1161 fdt_test_header_get(uts, "off_dt_struct", fdt_off_dt_struct(fdt));
1162 fdt_test_header_get(uts, "off_dt_strings", fdt_off_dt_strings(fdt));
1163 fdt_test_header_get(uts, "off_mem_rsvmap", fdt_off_mem_rsvmap(fdt));
1164 fdt_test_header_get(uts, "version", fdt_version(fdt));
1165 fdt_test_header_get(uts, "last_comp_version", fdt_last_comp_version(fdt));
1166 fdt_test_header_get(uts, "boot_cpuid_phys", fdt_boot_cpuid_phys(fdt));
1167 fdt_test_header_get(uts, "size_dt_strings", fdt_size_dt_strings(fdt));
1168 fdt_test_header_get(uts, "size_dt_struct", fdt_size_dt_struct(fdt));
1169
1170 return 0;
1171}
1172FDT_TEST(fdt_test_header, UT_TESTF_CONSOLE_REC);
1173
Marek Vasut65b6f462023-03-02 04:08:41 +01001174static int fdt_test_memory_cells(struct unit_test_state *uts,
1175 const unsigned int cells)
1176{
1177 unsigned char *pada, *pads;
1178 unsigned char *seta, *sets;
1179 char fdt[8192];
1180 const int size = sizeof(fdt);
1181 fdt32_t *regs;
1182 ulong addr;
1183 char *spc;
1184 int i;
1185
1186 /* Create DT with node /memory { regs = <0x100 0x200>; } and #*cells */
1187 ut_assertnonnull(regs = calloc(2 * cells, sizeof(*regs)));
1188 ut_assertnonnull(pada = calloc(12, cells));
1189 ut_assertnonnull(pads = calloc(12, cells));
1190 ut_assertnonnull(seta = calloc(12, cells));
1191 ut_assertnonnull(sets = calloc(12, cells));
1192 for (i = cells; i >= 1; i--) {
1193 regs[cells - 1] = cpu_to_fdt32(i * 0x10000);
1194 regs[(cells * 2) - 1] = cpu_to_fdt32(~i);
1195 snprintf(seta + (8 * (cells - i)), 9, "%08x", i * 0x10000);
1196 snprintf(sets + (8 * (cells - i)), 9, "%08x", ~i);
1197 spc = (i != 1) ? " " : "";
1198 snprintf(pada + (11 * (cells - i)), 12, "0x%08x%s", i * 0x10000, spc);
1199 snprintf(pads + (11 * (cells - i)), 12, "0x%08x%s", ~i, spc);
1200 }
1201
1202 ut_assertok(fdt_create(fdt, size));
1203 ut_assertok(fdt_finish_reservemap(fdt));
1204 ut_assert(fdt_begin_node(fdt, "") >= 0);
1205 ut_assertok(fdt_property_u32(fdt, "#address-cells", cells));
1206 ut_assertok(fdt_property_u32(fdt, "#size-cells", cells));
1207 ut_assert(fdt_begin_node(fdt, "memory") >= 0);
1208 ut_assertok(fdt_property_string(fdt, "device_type", "memory"));
1209 ut_assertok(fdt_property(fdt, "reg", &regs, cells * 2));
1210 ut_assertok(fdt_end_node(fdt));
1211 ut_assertok(fdt_end_node(fdt));
1212 ut_assertok(fdt_finish(fdt));
1213 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1214 addr = map_to_sysmem(fdt);
1215 set_working_fdt_addr(addr);
1216
1217 /* Test updating the memory node */
1218 ut_assertok(console_record_reset_enable());
1219 ut_assertok(run_commandf("fdt memory 0x%s 0x%s", seta, sets));
1220 ut_assertok(run_commandf("fdt print /memory"));
1221 ut_assert_nextline("memory {");
1222 ut_assert_nextline("\tdevice_type = \"memory\";");
1223 ut_assert_nextline("\treg = <%s %s>;", pada, pads);
1224 ut_assert_nextline("};");
1225 ut_assertok(ut_check_console_end(uts));
1226
1227 free(sets);
1228 free(seta);
1229 free(pads);
1230 free(pada);
1231 free(regs);
1232
1233 return 0;
1234}
1235
1236static int fdt_test_memory(struct unit_test_state *uts)
1237{
1238 /*
1239 * Test memory fixup for 32 and 64 bit systems, anything bigger is
1240 * so far unsupported and fails because of simple_stroull() being
1241 * 64bit tops in the 'fdt memory' command implementation.
1242 */
1243 fdt_test_memory_cells(uts, 1);
1244 fdt_test_memory_cells(uts, 2);
1245
1246 /*
1247 * The 'fdt memory' command is limited to /memory node, it does
1248 * not support any other valid DT memory node format, which is
1249 * either one or multiple /memory@adresss nodes. Therefore, this
1250 * DT variant is not tested here.
1251 */
1252
1253 return 0;
1254}
1255FDT_TEST(fdt_test_memory, UT_TESTF_CONSOLE_REC);
1256
Marek Vasutcd162df2023-03-02 04:08:42 +01001257static int fdt_test_rsvmem(struct unit_test_state *uts)
1258{
1259 char fdt[8192];
1260 ulong addr;
1261
1262 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1263 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1264 fdt_add_mem_rsv(fdt, 0x42, 0x1701);
1265 fdt_add_mem_rsv(fdt, 0x74656, 0x9);
1266 addr = map_to_sysmem(fdt);
1267 set_working_fdt_addr(addr);
1268
1269 /* Test default reserved memory node presence */
1270 ut_assertok(console_record_reset_enable());
1271 ut_assertok(run_commandf("fdt rsvmem print"));
1272 ut_assert_nextline("index\t\t start\t\t size");
1273 ut_assert_nextline("------------------------------------------------");
1274 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
1275 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
1276 ut_assertok(ut_check_console_end(uts));
1277
1278 /* Test add new reserved memory node */
1279 ut_assertok(console_record_reset_enable());
1280 ut_assertok(run_commandf("fdt rsvmem add 0x1234 0x5678"));
1281 ut_assertok(run_commandf("fdt rsvmem print"));
1282 ut_assert_nextline("index\t\t start\t\t size");
1283 ut_assert_nextline("------------------------------------------------");
1284 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
1285 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
1286 ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x1234, 0x5678);
1287 ut_assertok(ut_check_console_end(uts));
1288
1289 /* Test delete reserved memory node */
1290 ut_assertok(console_record_reset_enable());
1291 ut_assertok(run_commandf("fdt rsvmem delete 0"));
1292 ut_assertok(run_commandf("fdt rsvmem print"));
1293 ut_assert_nextline("index\t\t start\t\t size");
1294 ut_assert_nextline("------------------------------------------------");
1295 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
1296 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
1297 ut_assertok(ut_check_console_end(uts));
1298
1299 /* Test re-add new reserved memory node */
1300 ut_assertok(console_record_reset_enable());
1301 ut_assertok(run_commandf("fdt rsvmem add 0x42 0x1701"));
1302 ut_assertok(run_commandf("fdt rsvmem print"));
1303 ut_assert_nextline("index\t\t start\t\t size");
1304 ut_assert_nextline("------------------------------------------------");
1305 ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
1306 ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
1307 ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x42, 0x1701);
1308 ut_assertok(ut_check_console_end(uts));
1309
1310 /* Test delete nonexistent reserved memory node */
1311 ut_assertok(console_record_reset_enable());
1312 ut_asserteq(1, run_commandf("fdt rsvmem delete 10"));
1313 ut_assert_nextline("libfdt fdt_del_mem_rsv(): FDT_ERR_NOTFOUND");
1314 ut_assertok(ut_check_console_end(uts));
1315
1316 return 0;
1317}
1318FDT_TEST(fdt_test_rsvmem, UT_TESTF_CONSOLE_REC);
1319
Marek Vasut8d450da2023-03-02 04:08:43 +01001320static int fdt_test_chosen(struct unit_test_state *uts)
1321{
1322 const char *env_bootargs = env_get("bootargs");
1323 char fdt[8192];
1324 ulong addr;
1325
1326 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
1327 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1328 addr = map_to_sysmem(fdt);
1329 set_working_fdt_addr(addr);
1330
1331 /* Test default chosen node presence, fail as there is no /chosen node */
1332 ut_assertok(console_record_reset_enable());
1333 ut_asserteq(1, run_commandf("fdt print /chosen"));
1334 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
1335 ut_assertok(ut_check_console_end(uts));
1336
1337 /* Test add new chosen node without initrd */
1338 ut_assertok(console_record_reset_enable());
1339 ut_assertok(run_commandf("fdt chosen"));
1340 ut_assertok(run_commandf("fdt print /chosen"));
1341 ut_assert_nextline("chosen {");
1342 ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
1343 if (env_bootargs)
1344 ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
1345 ut_assert_nextline("};");
1346 ut_assertok(ut_check_console_end(uts));
1347
1348 /* Test add new chosen node with initrd */
1349 ut_assertok(console_record_reset_enable());
1350 ut_assertok(run_commandf("fdt chosen 0x1234 0x5678"));
1351 ut_assertok(run_commandf("fdt print /chosen"));
1352 ut_assert_nextline("chosen {");
1353 ut_assert_nextline("\tlinux,initrd-end = <0x%08x 0x%08x>;",
1354 upper_32_bits(0x1234 + 0x5678 - 1),
1355 lower_32_bits(0x1234 + 0x5678 - 1));
1356 ut_assert_nextline("\tlinux,initrd-start = <0x%08x 0x%08x>;",
1357 upper_32_bits(0x1234), lower_32_bits(0x1234));
1358 ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
1359 if (env_bootargs)
1360 ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
1361 ut_assert_nextline("};");
1362 ut_assertok(ut_check_console_end(uts));
1363
1364 return 0;
1365}
1366FDT_TEST(fdt_test_chosen, UT_TESTF_CONSOLE_REC);
1367
Marek Vasutdc0ccea2023-03-02 04:08:44 +01001368static int fdt_test_apply(struct unit_test_state *uts)
1369{
1370 char fdt[8192], fdto[8192];
1371 ulong addr, addro;
1372
1373 /* Create base DT with __symbols__ node */
1374 ut_assertok(fdt_create(fdt, sizeof(fdt)));
1375 ut_assertok(fdt_finish_reservemap(fdt));
1376 ut_assert(fdt_begin_node(fdt, "") >= 0);
1377 ut_assert(fdt_begin_node(fdt, "__symbols__") >= 0);
1378 ut_assertok(fdt_end_node(fdt));
1379 ut_assertok(fdt_end_node(fdt));
1380 ut_assertok(fdt_finish(fdt));
1381 fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
1382 addr = map_to_sysmem(fdt);
1383 set_working_fdt_addr(addr);
1384
1385 /* Create DTO which adds single property to root node / */
1386 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1387 ut_assertok(fdt_finish_reservemap(fdto));
1388 ut_assert(fdt_begin_node(fdto, "") >= 0);
1389 ut_assert(fdt_begin_node(fdto, "fragment") >= 0);
1390 ut_assertok(fdt_property_string(fdto, "target-path", "/"));
1391 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1392 ut_assertok(fdt_property_string(fdto, "newstring", "newvalue"));
1393 ut_assertok(fdt_end_node(fdto));
1394 ut_assertok(fdt_end_node(fdto));
1395 ut_assertok(fdt_finish(fdto));
1396 addro = map_to_sysmem(fdto);
1397
1398 /* Test default DT print */
1399 ut_assertok(console_record_reset_enable());
1400 ut_assertok(run_commandf("fdt print /"));
1401 ut_assert_nextline("/ {");
1402 ut_assert_nextline("\t__symbols__ {");
1403 ut_assert_nextline("\t};");
1404 ut_assert_nextline("};");
1405 ut_assertok(ut_check_console_end(uts));
1406
1407 /* Test simple DTO application */
1408 ut_assertok(console_record_reset_enable());
1409 ut_assertok(run_commandf("fdt apply 0x%08x", addro));
1410 ut_assertok(run_commandf("fdt print /"));
1411 ut_assert_nextline("/ {");
1412 ut_assert_nextline("\tnewstring = \"newvalue\";");
1413 ut_assert_nextline("\t__symbols__ {");
1414 ut_assert_nextline("\t};");
1415 ut_assert_nextline("};");
1416 ut_assertok(ut_check_console_end(uts));
1417
1418 /*
1419 * Create complex DTO which:
1420 * - modifies newstring property in root node /
1421 * - adds new properties to root node /
1422 * - adds new subnode with properties to root node /
1423 * - adds phandle to the subnode and therefore __symbols__ node
1424 */
1425 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1426 ut_assertok(fdt_finish_reservemap(fdto));
1427 ut_assert(fdt_begin_node(fdto, "") >= 0);
1428 ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
1429 ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
1430
1431 ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
1432 ut_assertok(fdt_property_string(fdto, "target-path", "/"));
1433 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1434 ut_assertok(fdt_property_string(fdto, "newstring", "newervalue"));
1435 ut_assertok(fdt_property_u32(fdto, "newu32", 0x12345678));
1436 ut_assertok(fdt_property(fdto, "empty-property", NULL, 0));
1437 ut_assert(fdt_begin_node(fdto, "subnode") >= 0);
1438 ut_assertok(fdt_property_string(fdto, "subnewstring", "newervalue"));
1439 ut_assertok(fdt_property_u32(fdto, "subnewu32", 0x12345678));
1440 ut_assertok(fdt_property(fdto, "subempty-property", NULL, 0));
1441 ut_assertok(fdt_property_u32(fdto, "phandle", 0x01));
1442 ut_assertok(fdt_end_node(fdto));
1443 ut_assertok(fdt_end_node(fdto));
1444 ut_assertok(fdt_end_node(fdto));
1445
1446 ut_assert(fdt_begin_node(fdto, "__symbols__") >= 0);
1447 ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0/__overlay__/subnode"));
1448 ut_assertok(fdt_end_node(fdto));
1449 ut_assertok(fdt_finish(fdto));
1450 addro = map_to_sysmem(fdto);
1451
1452 /* Test complex DTO application */
1453 ut_assertok(console_record_reset_enable());
1454 ut_assertok(run_commandf("fdt apply 0x%08x", addro));
1455 ut_assertok(run_commandf("fdt print /"));
1456 ut_assert_nextline("/ {");
1457 ut_assert_nextline("\tempty-property;");
1458 ut_assert_nextline("\tnewu32 = <0x12345678>;");
1459 ut_assert_nextline("\tnewstring = \"newervalue\";");
1460 ut_assert_nextline("\tsubnode {");
1461 ut_assert_nextline("\t\tphandle = <0x00000001>;");
1462 ut_assert_nextline("\t\tsubempty-property;");
1463 ut_assert_nextline("\t\tsubnewu32 = <0x12345678>;");
1464 ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
1465 ut_assert_nextline("\t};");
1466 ut_assert_nextline("\t__symbols__ {");
1467 ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
1468 ut_assert_nextline("\t};");
1469 ut_assert_nextline("};");
1470 ut_assertok(ut_check_console_end(uts));
1471
1472 /*
1473 * Create complex DTO which:
1474 * - modifies subnewu32 property in subnode via phandle and uses __fixups__ node
1475 */
1476 ut_assertok(fdt_create(fdto, sizeof(fdto)));
1477 ut_assertok(fdt_finish_reservemap(fdto));
1478 ut_assert(fdt_begin_node(fdto, "") >= 0);
1479 ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
1480 ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
1481
1482 ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
1483 ut_assertok(fdt_property_u32(fdto, "target", 0xffffffff));
1484 ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
1485 ut_assertok(fdt_property_u32(fdto, "subnewu32", 0xabcdef01));
1486 ut_assertok(fdt_end_node(fdto));
1487 ut_assertok(fdt_end_node(fdto));
1488
1489 ut_assert(fdt_begin_node(fdto, "__fixups__") >= 0);
1490 ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0:target:0"));
1491 ut_assertok(fdt_end_node(fdto));
1492 ut_assertok(fdt_end_node(fdto));
1493 ut_assertok(fdt_finish(fdto));
1494 addro = map_to_sysmem(fdto);
1495
1496 /* Test complex DTO application */
1497 ut_assertok(console_record_reset_enable());
1498 ut_assertok(run_commandf("fdt apply 0x%08x", addro));
1499 ut_assertok(run_commandf("fdt print /"));
1500 ut_assert_nextline("/ {");
1501 ut_assert_nextline("\tempty-property;");
1502 ut_assert_nextline("\tnewu32 = <0x12345678>;");
1503 ut_assert_nextline("\tnewstring = \"newervalue\";");
1504 ut_assert_nextline("\tsubnode {");
1505 ut_assert_nextline("\t\tphandle = <0x00000001>;");
1506 ut_assert_nextline("\t\tsubempty-property;");
1507 ut_assert_nextline("\t\tsubnewu32 = <0xabcdef01>;");
1508 ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
1509 ut_assert_nextline("\t};");
1510 ut_assert_nextline("\t__symbols__ {");
1511 ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
1512 ut_assert_nextline("\t};");
1513 ut_assert_nextline("};");
1514 ut_assertok(ut_check_console_end(uts));
1515
1516 return 0;
1517}
1518FDT_TEST(fdt_test_apply, UT_TESTF_CONSOLE_REC);
1519
Simon Glassf3c6a1d2022-07-13 06:06:59 -06001520int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1521{
1522 struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);
1523 const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test);
1524
1525 return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv);
1526}