blob: cb88d2ee11ba67a06d628cd174ee1e1791d69d84 [file] [log] [blame]
Marek Vasutbf527662023-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 Vasutbf527662023-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 Glass347a8452023-07-12 09:04:37 -060018#include <serial.h>
Marek Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -060030static int test_num_l(struct unit_test_state *uts, const char *name,
31 ulong value)
Marek Vasutbf527662023-05-31 03:03:58 +020032{
Simon Glass2c522af2023-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 Vasutbf527662023-05-31 03:03:58 +020037}
38
Simon Glass2c522af2023-07-11 20:46:30 -060039static int test_num_ll(struct unit_test_state *uts, const char *name,
40 unsigned long long value)
Marek Vasutbf527662023-05-31 03:03:58 +020041{
Simon Glass2c522af2023-07-11 20:46:30 -060042 ut_assert_nextline("%-12s= 0x%.*llx", name, 2 * (int)sizeof(ulong),
43 value);
44
45 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +020046}
47
Simon Glass2c522af2023-07-11 20:46:30 -060048static int test_eth(struct unit_test_state *uts)
Marek Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -060068
69 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +020070}
71
Simon Glass2c522af2023-07-11 20:46:30 -060072static int test_video_info(struct unit_test_state *uts)
Marek Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -060084 ut_assertok(test_num_ll(uts, "FB base",
85 (ulong)upriv->fb));
Marek Vasutbf527662023-05-31 03:03:58 +020086 if (upriv->copy_fb) {
Simon Glass2c522af2023-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 Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -060097
98 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +020099}
100
Simon Glass2c522af2023-07-11 20:46:30 -0600101static int lmb_test_dump_region(struct unit_test_state *uts,
102 struct lmb_region *rgn, char *name)
Marek Vasutbf527662023-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 Glass80a8a2e2023-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 Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -0600131
132 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +0200133}
134
Simon Glass2c522af2023-07-11 20:46:30 -0600135static int lmb_test_dump_all(struct unit_test_state *uts, struct lmb *lmb)
Marek Vasutbf527662023-05-31 03:03:58 +0200136{
137 ut_assert_nextline("lmb_dump_all:");
Simon Glass3535f602023-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 Glass2c522af2023-07-11 20:46:30 -0600140
141 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +0200142}
143
Marek Vasut2696f3a2023-10-07 23:41:05 +0200144static int bdinfo_check_mem(struct unit_test_state *uts)
Marek Vasutbf527662023-05-31 03:03:58 +0200145{
146 struct bd_info *bd = gd->bd;
147 int i;
148
Marek Vasutbf527662023-05-31 03:03:58 +0200149 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
150 if (bd->bi_dram[i].size) {
Simon Glass2c522af2023-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 Vasutbf527662023-05-31 03:03:58 +0200156 }
157 }
158
Marek Vasut2696f3a2023-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 Vasutbf527662023-05-31 03:03:58 +0200168 /* CONFIG_SYS_HAS_SRAM testing not supported */
Simon Glass2c522af2023-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 Vasutbf527662023-05-31 03:03:58 +0200172 ut_assert_nextline("baudrate = %lu bps",
173 env_get_ulong("baudrate", 10, 1234));
Simon Glass2c522af2023-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 Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -0600179 ut_assertok(test_eth(uts));
Marek Vasutbf527662023-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 Glass2c522af2023-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 Vasutbf527662023-05-31 03:03:58 +0200191
192 if (IS_ENABLED(CONFIG_VIDEO))
Simon Glass3535f602023-10-01 19:15:17 -0600193 ut_assertok(test_video_info(uts));
Marek Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -0600197 ut_assertok(test_num_l(uts, "multi_dtb_fit", (ulong)gd->multi_dtb_fit));
Marek Vasutbf527662023-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 Glass3535f602023-10-01 19:15:17 -0600204 ut_assertok(lmb_test_dump_all(uts, &lmb));
Marek Vasutbf527662023-05-31 03:03:58 +0200205 if (IS_ENABLED(CONFIG_OF_REAL))
206 ut_assert_nextline("devicetree = %s", fdtdec_get_srcname());
207 }
208
Simon Glass347a8452023-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 Glassb279f512023-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 Glass3535f602023-10-01 19:15:17 -0600229 if (IS_ENABLED(CONFIG_X86))
230 ut_check_skip_to_linen(uts, " high end =");
231
Marek Vasut4823b052023-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 Vasutbf527662023-05-31 03:03:58 +0200243 ut_assertok(ut_check_console_end(uts));
244
245 return 0;
246}
Simon Glass725c4382024-08-22 07:57:48 -0600247BDINFO_TEST(bdinfo_test_full, UTF_CONSOLE_REC);
Marek Vasutbf527662023-05-31 03:03:58 +0200248
Marek Vasut8827a382023-10-07 23:41:04 +0200249static int bdinfo_test_help(struct unit_test_state *uts)
250{
251 /* Test BDINFO unknown option help text print */
252 ut_assertok(console_record_reset_enable());
Marek Vasutbe4a5f72023-12-10 16:01:56 +0100253 if (!CONFIG_IS_ENABLED(GETOPT)) {
254 ut_asserteq(0, run_commandf("bdinfo -h"));
255 ut_assertok(bdinfo_test_all(uts));
256 } else {
257 ut_asserteq(1, run_commandf("bdinfo -h"));
258 ut_assert_nextlinen("bdinfo: invalid option -- h");
259 ut_assert_nextlinen("bdinfo - print Board Info structure");
260 ut_assert_nextline_empty();
261 ut_assert_nextlinen("Usage:");
262 ut_assert_nextlinen("bdinfo");
263 }
Marek Vasut8827a382023-10-07 23:41:04 +0200264 ut_assertok(ut_check_console_end(uts));
265
266 return 0;
267}
Simon Glass725c4382024-08-22 07:57:48 -0600268BDINFO_TEST(bdinfo_test_help, UTF_CONSOLE_REC);
Marek Vasut8827a382023-10-07 23:41:04 +0200269
Marek Vasut2696f3a2023-10-07 23:41:05 +0200270static int bdinfo_test_memory(struct unit_test_state *uts)
271{
272 /* Test BDINFO memory layout only print */
273 ut_assertok(console_record_reset_enable());
274 ut_assertok(run_commandf("bdinfo -m"));
Marek Vasutbe4a5f72023-12-10 16:01:56 +0100275 if (!CONFIG_IS_ENABLED(GETOPT))
276 ut_assertok(bdinfo_test_all(uts));
277 else
278 ut_assertok(bdinfo_check_mem(uts));
Marek Vasut2696f3a2023-10-07 23:41:05 +0200279 ut_assertok(ut_check_console_end(uts));
280
281 return 0;
282}
Simon Glass725c4382024-08-22 07:57:48 -0600283BDINFO_TEST(bdinfo_test_memory, UTF_CONSOLE_REC);
Marek Vasut2696f3a2023-10-07 23:41:05 +0200284
Marek Vasut3ff2d792023-10-07 23:41:06 +0200285static int bdinfo_test_eth(struct unit_test_state *uts)
286{
287 /* Test BDINFO ethernet settings only print */
288 ut_assertok(console_record_reset_enable());
289 ut_assertok(run_commandf("bdinfo -e"));
Marek Vasutbe4a5f72023-12-10 16:01:56 +0100290 if (!CONFIG_IS_ENABLED(GETOPT))
291 ut_assertok(bdinfo_test_all(uts));
292 else if (IS_ENABLED(CONFIG_CMD_NET))
Marek Vasut3ff2d792023-10-07 23:41:06 +0200293 ut_assertok(test_eth(uts));
294 ut_assertok(ut_check_console_end(uts));
295
296 return 0;
297}
Simon Glass725c4382024-08-22 07:57:48 -0600298BDINFO_TEST(bdinfo_test_eth, UTF_CONSOLE_REC);
Marek Vasut3ff2d792023-10-07 23:41:06 +0200299
Marek Vasutbf527662023-05-31 03:03:58 +0200300int do_ut_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
301{
302 struct unit_test *tests = UNIT_TEST_SUITE_START(bdinfo_test);
303 const int n_ents = UNIT_TEST_SUITE_COUNT(bdinfo_test);
304
305 return cmd_ut_category("bdinfo", "bdinfo_test_", tests, n_ents, argc, argv);
306}