blob: 027848c3e24c0f5736e9d3919e54ffe5ca8056aa [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
Marek Vasut2cd173c2023-05-31 03:03:58 +02008#include <console.h>
9#include <mapmem.h>
10#include <asm/global_data.h>
11#include <dm/uclass.h>
12#include <test/suites.h>
13#include <test/ut.h>
14#include <dm.h>
15#include <env.h>
16#include <lmb.h>
17#include <net.h>
Simon Glass3ddee312023-07-12 09:04:37 -060018#include <serial.h>
Marek Vasut2cd173c2023-05-31 03:03:58 +020019#include <video.h>
20#include <vsprintf.h>
21#include <asm/cache.h>
22#include <asm/global_data.h>
23#include <display_options.h>
24
25DECLARE_GLOBAL_DATA_PTR;
26
27/* Declare a new bdinfo test */
28#define BDINFO_TEST(_name, _flags) UNIT_TEST(_name, _flags, bdinfo_test)
29
Simon Glass0a5e0692023-07-11 20:46:30 -060030static int test_num_l(struct unit_test_state *uts, const char *name,
31 ulong value)
Marek Vasut2cd173c2023-05-31 03:03:58 +020032{
Simon Glass0a5e0692023-07-11 20:46:30 -060033 ut_assert_nextline("%-12s= 0x%0*lx", name, 2 * (int)sizeof(value),
34 value);
35
36 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +020037}
38
Simon Glass0a5e0692023-07-11 20:46:30 -060039static int test_num_ll(struct unit_test_state *uts, const char *name,
40 unsigned long long value)
Marek Vasut2cd173c2023-05-31 03:03:58 +020041{
Simon Glass0a5e0692023-07-11 20:46:30 -060042 ut_assert_nextline("%-12s= 0x%.*llx", name, 2 * (int)sizeof(ulong),
43 value);
44
45 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +020046}
47
Simon Glass0a5e0692023-07-11 20:46:30 -060048static int test_eth(struct unit_test_state *uts)
Marek Vasut2cd173c2023-05-31 03:03:58 +020049{
50 const int idx = eth_get_dev_index();
51 uchar enetaddr[6];
52 char name[10];
53 int ret;
54
55 if (idx)
56 sprintf(name, "eth%iaddr", idx);
57 else
58 strcpy(name, "ethaddr");
59
60 ret = eth_env_get_enetaddr_by_index("eth", idx, enetaddr);
61
62 ut_assert_nextline("current eth = %s", eth_get_name());
63 if (!ret)
64 ut_assert_nextline("%-12s= (not set)", name);
65 else
66 ut_assert_nextline("%-12s= %pM", name, enetaddr);
67 ut_assert_nextline("IP addr = %s", env_get("ipaddr"));
Simon Glass0a5e0692023-07-11 20:46:30 -060068
69 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +020070}
71
Simon Glass0a5e0692023-07-11 20:46:30 -060072static int test_video_info(struct unit_test_state *uts)
Marek Vasut2cd173c2023-05-31 03:03:58 +020073{
74 const struct udevice *dev;
75 struct uclass *uc;
76
77 uclass_id_foreach_dev(UCLASS_VIDEO, dev, uc) {
78 ut_assert_nextline("%-12s= %s %sactive", "Video", dev->name,
79 device_active(dev) ? "" : "in");
80 if (device_active(dev)) {
81 struct video_priv *upriv = dev_get_uclass_priv(dev);
82 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
83
Simon Glass0a5e0692023-07-11 20:46:30 -060084 ut_assertok(test_num_ll(uts, "FB base",
85 (ulong)upriv->fb));
Marek Vasut2cd173c2023-05-31 03:03:58 +020086 if (upriv->copy_fb) {
Simon Glass0a5e0692023-07-11 20:46:30 -060087 ut_assertok(test_num_ll(uts, "FB copy",
88 (ulong)upriv->copy_fb));
89 ut_assertok(test_num_l(uts, " copy size",
90 plat->copy_size));
Marek Vasut2cd173c2023-05-31 03:03:58 +020091 }
92 ut_assert_nextline("%-12s= %dx%dx%d", "FB size",
93 upriv->xsize, upriv->ysize,
94 1 << upriv->bpix);
95 }
96 }
Simon Glass0a5e0692023-07-11 20:46:30 -060097
98 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +020099}
100
Simon Glass0a5e0692023-07-11 20:46:30 -0600101static int lmb_test_dump_region(struct unit_test_state *uts,
102 struct lmb_region *rgn, char *name)
Marek Vasut2cd173c2023-05-31 03:03:58 +0200103{
104 unsigned long long base, size, end;
105 enum lmb_flags flags;
106 int i;
107
108 ut_assert_nextline(" %s.cnt = 0x%lx / max = 0x%lx", name, rgn->cnt, rgn->max);
109
110 for (i = 0; i < rgn->cnt; i++) {
111 base = rgn->region[i].base;
112 size = rgn->region[i].size;
113 end = base + size - 1;
114 flags = rgn->region[i].flags;
115
Simon Glassa44bd852023-10-01 19:15:16 -0600116 /*
117 * this entry includes the stack (get_sp()) on many platforms
118 * so will different each time lmb_init_and_reserve() is called.
119 * We could instead have the bdinfo command put its lmb region
120 * in a known location, so we can check it directly, rather than
121 * calling lmb_init_and_reserve() to create a new (and hopefully
122 * identical one). But for now this seems good enough.
123 */
124 if (!IS_ENABLED(CONFIG_SANDBOX) && i == 3) {
125 ut_assert_nextlinen(" %s[%d]\t[", name, i);
126 continue;
127 }
Marek Vasut2cd173c2023-05-31 03:03:58 +0200128 ut_assert_nextline(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x",
129 name, i, base, end, size, flags);
130 }
Simon Glass0a5e0692023-07-11 20:46:30 -0600131
132 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +0200133}
134
Simon Glass0a5e0692023-07-11 20:46:30 -0600135static int lmb_test_dump_all(struct unit_test_state *uts, struct lmb *lmb)
Marek Vasut2cd173c2023-05-31 03:03:58 +0200136{
137 ut_assert_nextline("lmb_dump_all:");
Simon Glass43b9a322023-10-01 19:15:17 -0600138 ut_assertok(lmb_test_dump_region(uts, &lmb->memory, "memory"));
139 ut_assertok(lmb_test_dump_region(uts, &lmb->reserved, "reserved"));
Simon Glass0a5e0692023-07-11 20:46:30 -0600140
141 return 0;
Marek Vasut2cd173c2023-05-31 03:03:58 +0200142}
143
Marek Vasut3c5fd092023-10-07 23:41:05 +0200144static int bdinfo_check_mem(struct unit_test_state *uts)
Marek Vasut2cd173c2023-05-31 03:03:58 +0200145{
146 struct bd_info *bd = gd->bd;
147 int i;
148
Marek Vasut2cd173c2023-05-31 03:03:58 +0200149 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
150 if (bd->bi_dram[i].size) {
Simon Glass0a5e0692023-07-11 20:46:30 -0600151 ut_assertok(test_num_l(uts, "DRAM bank", i));
152 ut_assertok(test_num_ll(uts, "-> start",
153 bd->bi_dram[i].start));
154 ut_assertok(test_num_ll(uts, "-> size",
155 bd->bi_dram[i].size));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200156 }
157 }
158
Marek Vasut3c5fd092023-10-07 23:41:05 +0200159 return 0;
160}
161
162static int bdinfo_test_all(struct unit_test_state *uts)
163{
164 ut_assertok(test_num_l(uts, "boot_params", 0));
165
166 ut_assertok(bdinfo_check_mem(uts));
167
Marek Vasut2cd173c2023-05-31 03:03:58 +0200168 /* CONFIG_SYS_HAS_SRAM testing not supported */
Simon Glass0a5e0692023-07-11 20:46:30 -0600169 ut_assertok(test_num_l(uts, "flashstart", 0));
170 ut_assertok(test_num_l(uts, "flashsize", 0));
171 ut_assertok(test_num_l(uts, "flashoffset", 0));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200172 ut_assert_nextline("baudrate = %lu bps",
173 env_get_ulong("baudrate", 10, 1234));
Simon Glass0a5e0692023-07-11 20:46:30 -0600174 ut_assertok(test_num_l(uts, "relocaddr", gd->relocaddr));
175 ut_assertok(test_num_l(uts, "reloc off", gd->reloc_off));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200176 ut_assert_nextline("%-12s= %u-bit", "Build", (uint)sizeof(void *) * 8);
177
178 if (IS_ENABLED(CONFIG_CMD_NET))
Simon Glass0a5e0692023-07-11 20:46:30 -0600179 ut_assertok(test_eth(uts));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200180
181 /*
182 * Make sure environment variable "fdtcontroladdr" address
183 * matches mapped control DT address.
184 */
185 ut_assert(map_to_sysmem(gd->fdt_blob) == env_get_hex("fdtcontroladdr", 0x1234));
Simon Glass0a5e0692023-07-11 20:46:30 -0600186 ut_assertok(test_num_l(uts, "fdt_blob",
187 (ulong)map_to_sysmem(gd->fdt_blob)));
188 ut_assertok(test_num_l(uts, "new_fdt",
189 (ulong)map_to_sysmem(gd->new_fdt)));
190 ut_assertok(test_num_l(uts, "fdt_size", (ulong)gd->fdt_size));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200191
192 if (IS_ENABLED(CONFIG_VIDEO))
Simon Glass43b9a322023-10-01 19:15:17 -0600193 ut_assertok(test_video_info(uts));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200194
195 /* The gd->multi_dtb_fit may not be available, hence, #if below. */
196#if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
Simon Glass0a5e0692023-07-11 20:46:30 -0600197 ut_assertok(test_num_l(uts, "multi_dtb_fit", (ulong)gd->multi_dtb_fit));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200198#endif
199
200 if (IS_ENABLED(CONFIG_LMB) && gd->fdt_blob) {
201 struct lmb lmb;
202
203 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Glass43b9a322023-10-01 19:15:17 -0600204 ut_assertok(lmb_test_dump_all(uts, &lmb));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200205 if (IS_ENABLED(CONFIG_OF_REAL))
206 ut_assert_nextline("devicetree = %s", fdtdec_get_srcname());
207 }
208
Simon Glass3ddee312023-07-12 09:04:37 -0600209 if (IS_ENABLED(CONFIG_DM_SERIAL)) {
210 struct serial_device_info info;
211
212 ut_assertnonnull(gd->cur_serial_dev);
213 ut_assertok(serial_getinfo(gd->cur_serial_dev, &info));
214
215 ut_assertok(test_num_l(uts, "serial addr", info.addr));
216 ut_assertok(test_num_l(uts, " width", info.reg_width));
217 ut_assertok(test_num_l(uts, " shift", info.reg_shift));
218 ut_assertok(test_num_l(uts, " offset", info.reg_offset));
219 ut_assertok(test_num_l(uts, " clock", info.clock));
220 }
221
Simon Glass5087c772023-07-15 21:38:46 -0600222 if (IS_ENABLED(CONFIG_CMD_BDINFO_EXTRA)) {
223 ut_assert_nextlinen("stack ptr");
224 ut_assertok(test_num_ll(uts, "ram_top ptr",
225 (unsigned long long)gd->ram_top));
226 ut_assertok(test_num_l(uts, "malloc base", gd_malloc_start()));
227 }
228
Simon Glass43b9a322023-10-01 19:15:17 -0600229 if (IS_ENABLED(CONFIG_X86))
230 ut_check_skip_to_linen(uts, " high end =");
231
Marek Vasuteb0b1db2023-10-07 23:41:03 +0200232 return 0;
233}
234
235static int bdinfo_test_full(struct unit_test_state *uts)
236{
237 /* Test BDINFO full print */
238 ut_assertok(console_record_reset_enable());
239 ut_assertok(run_commandf("bdinfo"));
240 ut_assertok(bdinfo_test_all(uts));
241 ut_assertok(run_commandf("bdinfo -a"));
242 ut_assertok(bdinfo_test_all(uts));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200243 ut_assertok(ut_check_console_end(uts));
244
245 return 0;
246}
247
Marek Vasut391e5c32023-10-07 23:41:02 +0200248BDINFO_TEST(bdinfo_test_full, UT_TESTF_CONSOLE_REC);
Marek Vasut2cd173c2023-05-31 03:03:58 +0200249
Marek Vasut24099242023-10-07 23:41:04 +0200250static int bdinfo_test_help(struct unit_test_state *uts)
251{
252 /* Test BDINFO unknown option help text print */
253 ut_assertok(console_record_reset_enable());
Marek Vasut949f0fe2023-12-10 16:01:56 +0100254 if (!CONFIG_IS_ENABLED(GETOPT)) {
255 ut_asserteq(0, run_commandf("bdinfo -h"));
256 ut_assertok(bdinfo_test_all(uts));
257 } else {
258 ut_asserteq(1, run_commandf("bdinfo -h"));
259 ut_assert_nextlinen("bdinfo: invalid option -- h");
260 ut_assert_nextlinen("bdinfo - print Board Info structure");
261 ut_assert_nextline_empty();
262 ut_assert_nextlinen("Usage:");
263 ut_assert_nextlinen("bdinfo");
264 }
Marek Vasut24099242023-10-07 23:41:04 +0200265 ut_assertok(ut_check_console_end(uts));
266
267 return 0;
268}
269
270BDINFO_TEST(bdinfo_test_help, UT_TESTF_CONSOLE_REC);
271
Marek Vasut3c5fd092023-10-07 23:41:05 +0200272static int bdinfo_test_memory(struct unit_test_state *uts)
273{
274 /* Test BDINFO memory layout only print */
275 ut_assertok(console_record_reset_enable());
276 ut_assertok(run_commandf("bdinfo -m"));
Marek Vasut949f0fe2023-12-10 16:01:56 +0100277 if (!CONFIG_IS_ENABLED(GETOPT))
278 ut_assertok(bdinfo_test_all(uts));
279 else
280 ut_assertok(bdinfo_check_mem(uts));
Marek Vasut3c5fd092023-10-07 23:41:05 +0200281 ut_assertok(ut_check_console_end(uts));
282
283 return 0;
284}
285
286BDINFO_TEST(bdinfo_test_memory, UT_TESTF_CONSOLE_REC);
287
Marek Vasutf14a03b2023-10-07 23:41:06 +0200288static int bdinfo_test_eth(struct unit_test_state *uts)
289{
290 /* Test BDINFO ethernet settings only print */
291 ut_assertok(console_record_reset_enable());
292 ut_assertok(run_commandf("bdinfo -e"));
Marek Vasut949f0fe2023-12-10 16:01:56 +0100293 if (!CONFIG_IS_ENABLED(GETOPT))
294 ut_assertok(bdinfo_test_all(uts));
295 else if (IS_ENABLED(CONFIG_CMD_NET))
Marek Vasutf14a03b2023-10-07 23:41:06 +0200296 ut_assertok(test_eth(uts));
297 ut_assertok(ut_check_console_end(uts));
298
299 return 0;
300}
301
302BDINFO_TEST(bdinfo_test_eth, UT_TESTF_CONSOLE_REC);
303
Marek Vasut2cd173c2023-05-31 03:03:58 +0200304int do_ut_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
305{
306 struct unit_test *tests = UNIT_TEST_SUITE_START(bdinfo_test);
307 const int n_ents = UNIT_TEST_SUITE_COUNT(bdinfo_test);
308
309 return cmd_ut_category("bdinfo", "bdinfo_test_", tests, n_ents, argc, argv);
310}