1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for fdt command
5 * Copyright 2022 Google LLCmap_to_sysmem(fdt));
10 #include <fdt_support.h>
12 #include <asm/global_data.h>
13 #include <linux/libfdt.h>
14 #include <test/suites.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 /* Declare a new fdt test */
20 #define FDT_TEST(_name, _flags) UNIT_TEST(_name, _flags, fdt_test)
23 * make_test_fdt() - Create an FDT with just a root node
25 * The size is set to the minimum needed
28 * @fdt: Place to write FDT
29 * @size: Maximum size of space for fdt
31 static int make_test_fdt(struct unit_test_state *uts, void *fdt, int size)
33 ut_assertok(fdt_create(fdt, size));
34 ut_assertok(fdt_finish_reservemap(fdt));
35 ut_assert(fdt_begin_node(fdt, "") >= 0);
36 ut_assertok(fdt_end_node(fdt));
37 ut_assertok(fdt_finish(fdt));
42 /* Test 'fdt addr' getting/setting address */
43 static int fdt_test_addr(struct unit_test_state *uts)
45 const void *fdt_blob, *new_fdt;
50 ut_assertok(console_record_reset_enable());
51 ut_assertok(run_command("fdt addr -c", 0));
52 ut_assert_nextline("Control fdt: %08lx",
53 (ulong)map_to_sysmem(gd->fdt_blob));
54 ut_assertok(ut_check_console_end(uts));
56 /* The working fdt is not set, so this should fail */
57 set_working_fdt_addr(0);
58 ut_assert_nextline("Working FDT set to 0");
59 ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0));
60 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
61 ut_assertok(ut_check_console_end(uts));
63 /* Set up a working FDT and try again */
64 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
65 addr = map_to_sysmem(fdt);
66 set_working_fdt_addr(addr);
67 ut_assert_nextline("Working FDT set to %lx", addr);
68 ut_assertok(run_command("fdt addr", 0));
69 ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt));
70 ut_assertok(ut_check_console_end(uts));
72 /* Set the working FDT */
73 set_working_fdt_addr(0);
74 ut_assert_nextline("Working FDT set to 0");
75 ut_assertok(run_commandf("fdt addr %08x", addr));
76 ut_assert_nextline("Working FDT set to %lx", addr);
77 ut_asserteq(addr, map_to_sysmem(working_fdt));
78 ut_assertok(ut_check_console_end(uts));
79 set_working_fdt_addr(0);
80 ut_assert_nextline("Working FDT set to 0");
82 /* Set the control FDT */
83 fdt_blob = gd->fdt_blob;
85 ret = run_commandf("fdt addr -c %08x", addr);
86 new_fdt = gd->fdt_blob;
87 gd->fdt_blob = fdt_blob;
89 ut_asserteq(addr, map_to_sysmem(new_fdt));
90 ut_assertok(ut_check_console_end(uts));
92 /* Test setting an invalid FDT */
94 ut_asserteq(1, run_commandf("fdt addr %08x", addr));
95 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
96 ut_assertok(ut_check_console_end(uts));
98 /* Test detecting an invalid FDT */
100 set_working_fdt_addr(addr);
101 ut_assert_nextline("Working FDT set to %lx", addr);
102 ut_asserteq(1, run_commandf("fdt addr"));
103 ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
104 ut_assertok(ut_check_console_end(uts));
108 FDT_TEST(fdt_test_addr, UT_TESTF_CONSOLE_REC);
110 /* Test 'fdt addr' resizing an fdt */
111 static int fdt_test_resize(struct unit_test_state *uts)
114 const int newsize = sizeof(fdt) / 2;
117 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
118 addr = map_to_sysmem(fdt);
119 set_working_fdt_addr(addr);
121 /* Test setting and resizing the working FDT to a larger size */
122 ut_assertok(console_record_reset_enable());
123 ut_assertok(run_commandf("fdt addr %08x %x", addr, newsize));
124 ut_assert_nextline("Working FDT set to %lx", addr);
125 ut_assertok(ut_check_console_end(uts));
127 /* Try shrinking it */
128 ut_assertok(run_commandf("fdt addr %08x %x", addr, sizeof(fdt) / 4));
129 ut_assert_nextline("Working FDT set to %lx", addr);
130 ut_assert_nextline("New length %d < existing length %d, ignoring",
131 (int)sizeof(fdt) / 4, newsize);
132 ut_assertok(ut_check_console_end(uts));
135 ut_assertok(run_commandf("fdt addr -q %08x %x", addr, sizeof(fdt) / 4));
136 ut_assert_nextline("Working FDT set to %lx", addr);
137 ut_assertok(ut_check_console_end(uts));
139 /* We cannot easily provoke errors in fdt_open_into(), so ignore that */
143 FDT_TEST(fdt_test_resize, UT_TESTF_CONSOLE_REC);
145 /* Test 'fdt get' reading an fdt */
146 static int fdt_test_get(struct unit_test_state *uts)
150 addr = map_to_sysmem(gd->fdt_blob);
151 set_working_fdt_addr(addr);
153 /* Test getting default element of /clk-test node clock-names property */
154 ut_assertok(console_record_reset_enable());
155 ut_assertok(run_command("fdt get value fdflt /clk-test clock-names", 0));
156 ut_asserteq_str("fixed", env_get("fdflt"));
157 ut_assertok(ut_check_console_end(uts));
159 /* Test getting 0th element of /clk-test node clock-names property */
160 ut_assertok(console_record_reset_enable());
161 ut_assertok(run_command("fdt get value fzero /clk-test clock-names 0", 0));
162 ut_asserteq_str("fixed", env_get("fzero"));
163 ut_assertok(ut_check_console_end(uts));
165 /* Test getting 1st element of /clk-test node clock-names property */
166 ut_assertok(console_record_reset_enable());
167 ut_assertok(run_command("fdt get value fone /clk-test clock-names 1", 0));
168 ut_asserteq_str("i2c", env_get("fone"));
169 ut_assertok(ut_check_console_end(uts));
171 /* Test getting 2nd element of /clk-test node clock-names property */
172 ut_assertok(console_record_reset_enable());
173 ut_assertok(run_command("fdt get value ftwo /clk-test clock-names 2", 0));
174 ut_asserteq_str("spi", env_get("ftwo"));
175 ut_assertok(ut_check_console_end(uts));
177 /* Test missing 10th element of /clk-test node clock-names property */
178 ut_assertok(console_record_reset_enable());
179 ut_asserteq(1, run_command("fdt get value ftwo /clk-test clock-names 10", 0));
180 ut_assertok(ut_check_console_end(uts));
182 /* Test getting default element of /clk-test node nonexistent property */
183 ut_assertok(console_record_reset_enable());
184 ut_asserteq(1, run_command("fdt get value fnone /clk-test nonexistent", 1));
185 ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
186 ut_assertok(ut_check_console_end(uts));
188 /* Test getting default element of /nonexistent node */
189 ut_assertok(console_record_reset_enable());
190 ut_asserteq(1, run_command("fdt get value fnode /nonexistent nonexistent", 1));
191 ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
192 ut_assertok(ut_check_console_end(uts));
196 FDT_TEST(fdt_test_get, UT_TESTF_CONSOLE_REC);
198 int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
200 struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);
201 const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test);
203 return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv);