blob: 34e1ef6510b81c11edc27b0f3fb44dc6fc2652a6 [file] [log] [blame]
Marek Vasut2cd173c2023-05-31 03:03:58 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for bdinfo command
4 *
5 * Copyright 2023 Marek Vasut <marek.vasut+renesas@mailbox.org>
6 */
7
Sughosh Ganu291bf9c2024-08-26 17:29:18 +05308#include <alist.h>
Marek Vasut2cd173c2023-05-31 03:03:58 +02009#include <console.h>
10#include <mapmem.h>
11#include <asm/global_data.h>
12#include <dm/uclass.h>
13#include <test/suites.h>
14#include <test/ut.h>
15#include <dm.h>
16#include <env.h>
17#include <lmb.h>
18#include <net.h>
Simon Glass3ddee312023-07-12 09:04:37 -060019#include <serial.h>
Marek Vasut2cd173c2023-05-31 03:03:58 +020020#include <video.h>
21#include <vsprintf.h>
22#include <asm/cache.h>
23#include <asm/global_data.h>
24#include <display_options.h>
25
26DECLARE_GLOBAL_DATA_PTR;
27
28/* Declare a new bdinfo test */
29#define BDINFO_TEST(_name, _flags) UNIT_TEST(_name, _flags, bdinfo_test)
30
Simon Glass0a5e0692023-07-11 20:46:30 -060031static int test_num_l(struct unit_test_state *uts, const char *name,
32 ulong value)
Marek Vasut2cd173c2023-05-31 03:03:58 +020033{
Simon Glass0a5e0692023-07-11 20:46:30 -060034 ut_assert_nextline("%-12s= 0x%0*lx", name, 2 * (int)sizeof(value),
35 value);
36
37 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +020038}
39
Simon Glass0a5e0692023-07-11 20:46:30 -060040static int test_num_ll(struct unit_test_state *uts, const char *name,
41 unsigned long long value)
Marek Vasut2cd173c2023-05-31 03:03:58 +020042{
Simon Glass0a5e0692023-07-11 20:46:30 -060043 ut_assert_nextline("%-12s= 0x%.*llx", name, 2 * (int)sizeof(ulong),
44 value);
45
46 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +020047}
48
Simon Glass0a5e0692023-07-11 20:46:30 -060049static int test_eth(struct unit_test_state *uts)
Marek Vasut2cd173c2023-05-31 03:03:58 +020050{
51 const int idx = eth_get_dev_index();
52 uchar enetaddr[6];
53 char name[10];
54 int ret;
55
56 if (idx)
57 sprintf(name, "eth%iaddr", idx);
58 else
59 strcpy(name, "ethaddr");
60
61 ret = eth_env_get_enetaddr_by_index("eth", idx, enetaddr);
62
63 ut_assert_nextline("current eth = %s", eth_get_name());
64 if (!ret)
65 ut_assert_nextline("%-12s= (not set)", name);
66 else
67 ut_assert_nextline("%-12s= %pM", name, enetaddr);
68 ut_assert_nextline("IP addr = %s", env_get("ipaddr"));
Simon Glass0a5e0692023-07-11 20:46:30 -060069
70 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +020071}
72
Simon Glass0a5e0692023-07-11 20:46:30 -060073static int test_video_info(struct unit_test_state *uts)
Marek Vasut2cd173c2023-05-31 03:03:58 +020074{
75 const struct udevice *dev;
76 struct uclass *uc;
77
78 uclass_id_foreach_dev(UCLASS_VIDEO, dev, uc) {
79 ut_assert_nextline("%-12s= %s %sactive", "Video", dev->name,
80 device_active(dev) ? "" : "in");
81 if (device_active(dev)) {
82 struct video_priv *upriv = dev_get_uclass_priv(dev);
83 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
84
Simon Glass0a5e0692023-07-11 20:46:30 -060085 ut_assertok(test_num_ll(uts, "FB base",
86 (ulong)upriv->fb));
Marek Vasut2cd173c2023-05-31 03:03:58 +020087 if (upriv->copy_fb) {
Simon Glass0a5e0692023-07-11 20:46:30 -060088 ut_assertok(test_num_ll(uts, "FB copy",
89 (ulong)upriv->copy_fb));
90 ut_assertok(test_num_l(uts, " copy size",
91 plat->copy_size));
Marek Vasut2cd173c2023-05-31 03:03:58 +020092 }
93 ut_assert_nextline("%-12s= %dx%dx%d", "FB size",
94 upriv->xsize, upriv->ysize,
95 1 << upriv->bpix);
96 }
97 }
Simon Glass0a5e0692023-07-11 20:46:30 -060098
99 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +0200100}
101
Simon Glass0a5e0692023-07-11 20:46:30 -0600102static int lmb_test_dump_region(struct unit_test_state *uts,
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530103 struct alist *lmb_rgn_lst, char *name)
Marek Vasut2cd173c2023-05-31 03:03:58 +0200104{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530105 struct lmb_region *rgn = lmb_rgn_lst->data;
Marek Vasut2cd173c2023-05-31 03:03:58 +0200106 unsigned long long base, size, end;
107 enum lmb_flags flags;
108 int i;
109
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530110 ut_assert_nextline(" %s.count = 0x%hx", name, lmb_rgn_lst->count);
Marek Vasut2cd173c2023-05-31 03:03:58 +0200111
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530112 for (i = 0; i < lmb_rgn_lst->count; i++) {
113 base = rgn[i].base;
114 size = rgn[i].size;
Marek Vasut2cd173c2023-05-31 03:03:58 +0200115 end = base + size - 1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530116 flags = rgn[i].flags;
Marek Vasut2cd173c2023-05-31 03:03:58 +0200117
Simon Glassa44bd852023-10-01 19:15:16 -0600118 if (!IS_ENABLED(CONFIG_SANDBOX) && i == 3) {
119 ut_assert_nextlinen(" %s[%d]\t[", name, i);
120 continue;
121 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530122 ut_assert_nextlinen(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: ",
123 name, i, base, end, size);
Marek Vasut2cd173c2023-05-31 03:03:58 +0200124 }
Simon Glass0a5e0692023-07-11 20:46:30 -0600125
126 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +0200127}
128
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530129static int lmb_test_dump_all(struct unit_test_state *uts)
Marek Vasut2cd173c2023-05-31 03:03:58 +0200130{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530131 struct lmb *lmb = lmb_get();
132
Marek Vasut2cd173c2023-05-31 03:03:58 +0200133 ut_assert_nextline("lmb_dump_all:");
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530134 ut_assertok(lmb_test_dump_region(uts, &lmb->free_mem, "memory"));
135 ut_assertok(lmb_test_dump_region(uts, &lmb->used_mem, "reserved"));
Simon Glass0a5e0692023-07-11 20:46:30 -0600136
137 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +0200138}
139
Marek Vasut3c5fd092023-10-07 23:41:05 +0200140static int bdinfo_check_mem(struct unit_test_state *uts)
Marek Vasut2cd173c2023-05-31 03:03:58 +0200141{
142 struct bd_info *bd = gd->bd;
143 int i;
144
Marek Vasut2cd173c2023-05-31 03:03:58 +0200145 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
146 if (bd->bi_dram[i].size) {
Simon Glass0a5e0692023-07-11 20:46:30 -0600147 ut_assertok(test_num_l(uts, "DRAM bank", i));
148 ut_assertok(test_num_ll(uts, "-> start",
149 bd->bi_dram[i].start));
150 ut_assertok(test_num_ll(uts, "-> size",
151 bd->bi_dram[i].size));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200152 }
153 }
154
Marek Vasut3c5fd092023-10-07 23:41:05 +0200155 return 0;
156}
157
158static int bdinfo_test_all(struct unit_test_state *uts)
159{
160 ut_assertok(test_num_l(uts, "boot_params", 0));
161
162 ut_assertok(bdinfo_check_mem(uts));
163
Marek Vasut2cd173c2023-05-31 03:03:58 +0200164 /* CONFIG_SYS_HAS_SRAM testing not supported */
Simon Glass0a5e0692023-07-11 20:46:30 -0600165 ut_assertok(test_num_l(uts, "flashstart", 0));
166 ut_assertok(test_num_l(uts, "flashsize", 0));
167 ut_assertok(test_num_l(uts, "flashoffset", 0));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200168 ut_assert_nextline("baudrate = %lu bps",
169 env_get_ulong("baudrate", 10, 1234));
Simon Glass0a5e0692023-07-11 20:46:30 -0600170 ut_assertok(test_num_l(uts, "relocaddr", gd->relocaddr));
171 ut_assertok(test_num_l(uts, "reloc off", gd->reloc_off));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200172 ut_assert_nextline("%-12s= %u-bit", "Build", (uint)sizeof(void *) * 8);
173
174 if (IS_ENABLED(CONFIG_CMD_NET))
Simon Glass0a5e0692023-07-11 20:46:30 -0600175 ut_assertok(test_eth(uts));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200176
177 /*
178 * Make sure environment variable "fdtcontroladdr" address
179 * matches mapped control DT address.
180 */
181 ut_assert(map_to_sysmem(gd->fdt_blob) == env_get_hex("fdtcontroladdr", 0x1234));
Simon Glass0a5e0692023-07-11 20:46:30 -0600182 ut_assertok(test_num_l(uts, "fdt_blob",
183 (ulong)map_to_sysmem(gd->fdt_blob)));
184 ut_assertok(test_num_l(uts, "new_fdt",
185 (ulong)map_to_sysmem(gd->new_fdt)));
186 ut_assertok(test_num_l(uts, "fdt_size", (ulong)gd->fdt_size));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200187
188 if (IS_ENABLED(CONFIG_VIDEO))
Simon Glass43b9a322023-10-01 19:15:17 -0600189 ut_assertok(test_video_info(uts));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200190
191 /* The gd->multi_dtb_fit may not be available, hence, #if below. */
192#if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
Simon Glass0a5e0692023-07-11 20:46:30 -0600193 ut_assertok(test_num_l(uts, "multi_dtb_fit", (ulong)gd->multi_dtb_fit));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200194#endif
195
196 if (IS_ENABLED(CONFIG_LMB) && gd->fdt_blob) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530197 ut_assertok(lmb_test_dump_all(uts));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200198 if (IS_ENABLED(CONFIG_OF_REAL))
199 ut_assert_nextline("devicetree = %s", fdtdec_get_srcname());
200 }
201
Simon Glass3ddee312023-07-12 09:04:37 -0600202 if (IS_ENABLED(CONFIG_DM_SERIAL)) {
203 struct serial_device_info info;
204
205 ut_assertnonnull(gd->cur_serial_dev);
206 ut_assertok(serial_getinfo(gd->cur_serial_dev, &info));
207
208 ut_assertok(test_num_l(uts, "serial addr", info.addr));
209 ut_assertok(test_num_l(uts, " width", info.reg_width));
210 ut_assertok(test_num_l(uts, " shift", info.reg_shift));
211 ut_assertok(test_num_l(uts, " offset", info.reg_offset));
212 ut_assertok(test_num_l(uts, " clock", info.clock));
213 }
214
Simon Glass5087c772023-07-15 21:38:46 -0600215 if (IS_ENABLED(CONFIG_CMD_BDINFO_EXTRA)) {
216 ut_assert_nextlinen("stack ptr");
217 ut_assertok(test_num_ll(uts, "ram_top ptr",
218 (unsigned long long)gd->ram_top));
219 ut_assertok(test_num_l(uts, "malloc base", gd_malloc_start()));
220 }
221
Simon Glass43b9a322023-10-01 19:15:17 -0600222 if (IS_ENABLED(CONFIG_X86))
223 ut_check_skip_to_linen(uts, " high end =");
224
Marek Vasuteb0b1db2023-10-07 23:41:03 +0200225 return 0;
226}
227
228static int bdinfo_test_full(struct unit_test_state *uts)
229{
230 /* Test BDINFO full print */
231 ut_assertok(console_record_reset_enable());
232 ut_assertok(run_commandf("bdinfo"));
233 ut_assertok(bdinfo_test_all(uts));
234 ut_assertok(run_commandf("bdinfo -a"));
235 ut_assertok(bdinfo_test_all(uts));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200236 ut_assertok(ut_check_console_end(uts));
237
238 return 0;
239}
240
Marek Vasut391e5c32023-10-07 23:41:02 +0200241BDINFO_TEST(bdinfo_test_full, UT_TESTF_CONSOLE_REC);
Marek Vasut2cd173c2023-05-31 03:03:58 +0200242
Marek Vasut24099242023-10-07 23:41:04 +0200243static int bdinfo_test_help(struct unit_test_state *uts)
244{
245 /* Test BDINFO unknown option help text print */
246 ut_assertok(console_record_reset_enable());
Marek Vasut949f0fe2023-12-10 16:01:56 +0100247 if (!CONFIG_IS_ENABLED(GETOPT)) {
248 ut_asserteq(0, run_commandf("bdinfo -h"));
249 ut_assertok(bdinfo_test_all(uts));
250 } else {
251 ut_asserteq(1, run_commandf("bdinfo -h"));
252 ut_assert_nextlinen("bdinfo: invalid option -- h");
253 ut_assert_nextlinen("bdinfo - print Board Info structure");
254 ut_assert_nextline_empty();
255 ut_assert_nextlinen("Usage:");
256 ut_assert_nextlinen("bdinfo");
257 }
Marek Vasut24099242023-10-07 23:41:04 +0200258 ut_assertok(ut_check_console_end(uts));
259
260 return 0;
261}
262
263BDINFO_TEST(bdinfo_test_help, UT_TESTF_CONSOLE_REC);
264
Marek Vasut3c5fd092023-10-07 23:41:05 +0200265static int bdinfo_test_memory(struct unit_test_state *uts)
266{
267 /* Test BDINFO memory layout only print */
268 ut_assertok(console_record_reset_enable());
269 ut_assertok(run_commandf("bdinfo -m"));
Marek Vasut949f0fe2023-12-10 16:01:56 +0100270 if (!CONFIG_IS_ENABLED(GETOPT))
271 ut_assertok(bdinfo_test_all(uts));
272 else
273 ut_assertok(bdinfo_check_mem(uts));
Marek Vasut3c5fd092023-10-07 23:41:05 +0200274 ut_assertok(ut_check_console_end(uts));
275
276 return 0;
277}
278
279BDINFO_TEST(bdinfo_test_memory, UT_TESTF_CONSOLE_REC);
280
Marek Vasutf14a03b2023-10-07 23:41:06 +0200281static int bdinfo_test_eth(struct unit_test_state *uts)
282{
283 /* Test BDINFO ethernet settings only print */
284 ut_assertok(console_record_reset_enable());
285 ut_assertok(run_commandf("bdinfo -e"));
Marek Vasut949f0fe2023-12-10 16:01:56 +0100286 if (!CONFIG_IS_ENABLED(GETOPT))
287 ut_assertok(bdinfo_test_all(uts));
288 else if (IS_ENABLED(CONFIG_CMD_NET))
Marek Vasutf14a03b2023-10-07 23:41:06 +0200289 ut_assertok(test_eth(uts));
290 ut_assertok(ut_check_console_end(uts));
291
292 return 0;
293}
294
295BDINFO_TEST(bdinfo_test_eth, UT_TESTF_CONSOLE_REC);
296
Marek Vasut2cd173c2023-05-31 03:03:58 +0200297int do_ut_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
298{
299 struct unit_test *tests = UNIT_TEST_SUITE_START(bdinfo_test);
300 const int n_ents = UNIT_TEST_SUITE_COUNT(bdinfo_test);
301
302 return cmd_ut_category("bdinfo", "bdinfo_test_", tests, n_ents, argc, argv);
303}