blob: a50285eafab05db47a4a86601982c84c60f2c2cf [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;
18
19/* Declare a new fdt test */
20#define FDT_TEST(_name, _flags) UNIT_TEST(_name, _flags, fdt_test)
21
22/**
23 * make_test_fdt() - Create an FDT with just a root node
24 *
25 * The size is set to the minimum needed
26 *
27 * @uts: Test state
28 * @fdt: Place to write FDT
29 * @size: Maximum size of space for fdt
30 */
31static int make_test_fdt(struct unit_test_state *uts, void *fdt, int size)
32{
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));
38
39 return 0;
40}
41
42/* Test 'fdt addr' getting/setting address */
43static int fdt_test_addr(struct unit_test_state *uts)
44{
45 const void *fdt_blob, *new_fdt;
46 char fdt[256];
47 ulong addr;
48 int ret;
49
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));
55
56 /* The working fdt is not set, so this should fail */
57 set_working_fdt_addr(0);
Simon Glass84328cf2022-10-11 09:47:12 -060058 ut_assert_nextline("Working FDT set to 0");
Simon Glassf3c6a1d2022-07-13 06:06:59 -060059 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));
62
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);
Simon Glass84328cf2022-10-11 09:47:12 -060067 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -060068 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));
71
72 /* Set the working FDT */
73 set_working_fdt_addr(0);
Simon Glass84328cf2022-10-11 09:47:12 -060074 ut_assert_nextline("Working FDT set to 0");
Simon Glassf3c6a1d2022-07-13 06:06:59 -060075 ut_assertok(run_commandf("fdt addr %08x", addr));
Simon Glass84328cf2022-10-11 09:47:12 -060076 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -060077 ut_asserteq(addr, map_to_sysmem(working_fdt));
78 ut_assertok(ut_check_console_end(uts));
79 set_working_fdt_addr(0);
Simon Glass84328cf2022-10-11 09:47:12 -060080 ut_assert_nextline("Working FDT set to 0");
Simon Glassf3c6a1d2022-07-13 06:06:59 -060081
Simon Glass84328cf2022-10-11 09:47:12 -060082 /* Set the control FDT */
Simon Glassf3c6a1d2022-07-13 06:06:59 -060083 fdt_blob = gd->fdt_blob;
84 gd->fdt_blob = NULL;
85 ret = run_commandf("fdt addr -c %08x", addr);
86 new_fdt = gd->fdt_blob;
87 gd->fdt_blob = fdt_blob;
88 ut_assertok(ret);
89 ut_asserteq(addr, map_to_sysmem(new_fdt));
90 ut_assertok(ut_check_console_end(uts));
91
92 /* Test setting an invalid FDT */
93 fdt[0] = 123;
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));
97
98 /* Test detecting an invalid FDT */
99 fdt[0] = 123;
100 set_working_fdt_addr(addr);
Simon Glass84328cf2022-10-11 09:47:12 -0600101 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600102 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));
105
106 return 0;
107}
108FDT_TEST(fdt_test_addr, UT_TESTF_CONSOLE_REC);
109
110/* Test 'fdt addr' resizing an fdt */
Marek Vasut3ae0f502023-03-02 04:08:25 +0100111static int fdt_test_addr_resize(struct unit_test_state *uts)
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600112{
113 char fdt[256];
114 const int newsize = sizeof(fdt) / 2;
115 ulong addr;
116
117 ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
118 addr = map_to_sysmem(fdt);
119 set_working_fdt_addr(addr);
120
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));
Simon Glass84328cf2022-10-11 09:47:12 -0600124 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600125 ut_assertok(ut_check_console_end(uts));
126
127 /* Try shrinking it */
128 ut_assertok(run_commandf("fdt addr %08x %x", addr, sizeof(fdt) / 4));
Simon Glass84328cf2022-10-11 09:47:12 -0600129 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600130 ut_assert_nextline("New length %d < existing length %d, ignoring",
131 (int)sizeof(fdt) / 4, newsize);
132 ut_assertok(ut_check_console_end(uts));
133
134 /* ...quietly */
135 ut_assertok(run_commandf("fdt addr -q %08x %x", addr, sizeof(fdt) / 4));
Simon Glass84328cf2022-10-11 09:47:12 -0600136 ut_assert_nextline("Working FDT set to %lx", addr);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600137 ut_assertok(ut_check_console_end(uts));
138
139 /* We cannot easily provoke errors in fdt_open_into(), so ignore that */
140
141 return 0;
142}
Marek Vasut3ae0f502023-03-02 04:08:25 +0100143FDT_TEST(fdt_test_addr_resize, UT_TESTF_CONSOLE_REC);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600144
Marek Vasut78c060d2022-11-14 22:50:00 +0100145/* Test 'fdt get' reading an fdt */
146static int fdt_test_get(struct unit_test_state *uts)
147{
148 ulong addr;
149
150 addr = map_to_sysmem(gd->fdt_blob);
151 set_working_fdt_addr(addr);
152
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));
158
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));
164
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));
170
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));
176
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));
181
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));
187
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));
193
194 return 0;
195}
196FDT_TEST(fdt_test_get, UT_TESTF_CONSOLE_REC);
197
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600198int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
199{
200 struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);
201 const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test);
202
203 return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv);
204}