blob: 9744bd16df926254c564f2b7d6ee6de4ab2c9dd1 [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
8#include <common.h>
9#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 Glass347a8452023-07-12 09:04:37 -060019#include <serial.h>
Marek Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -060031static int test_num_l(struct unit_test_state *uts, const char *name,
32 ulong value)
Marek Vasutbf527662023-05-31 03:03:58 +020033{
Simon Glass2c522af2023-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 Vasutbf527662023-05-31 03:03:58 +020038}
39
Simon Glass2c522af2023-07-11 20:46:30 -060040static int test_num_ll(struct unit_test_state *uts, const char *name,
41 unsigned long long value)
Marek Vasutbf527662023-05-31 03:03:58 +020042{
Simon Glass2c522af2023-07-11 20:46:30 -060043 ut_assert_nextline("%-12s= 0x%.*llx", name, 2 * (int)sizeof(ulong),
44 value);
45
46 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +020047}
48
Simon Glass2c522af2023-07-11 20:46:30 -060049static int test_eth(struct unit_test_state *uts)
Marek Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -060069
70 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +020071}
72
Simon Glass2c522af2023-07-11 20:46:30 -060073static int test_video_info(struct unit_test_state *uts)
Marek Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -060085 ut_assertok(test_num_ll(uts, "FB base",
86 (ulong)upriv->fb));
Marek Vasutbf527662023-05-31 03:03:58 +020087 if (upriv->copy_fb) {
Simon Glass2c522af2023-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 Vasutbf527662023-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 Glass2c522af2023-07-11 20:46:30 -060098
99 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +0200100}
101
Simon Glass2c522af2023-07-11 20:46:30 -0600102static int lmb_test_dump_region(struct unit_test_state *uts,
103 struct lmb_region *rgn, char *name)
Marek Vasutbf527662023-05-31 03:03:58 +0200104{
105 unsigned long long base, size, end;
106 enum lmb_flags flags;
107 int i;
108
109 ut_assert_nextline(" %s.cnt = 0x%lx / max = 0x%lx", name, rgn->cnt, rgn->max);
110
111 for (i = 0; i < rgn->cnt; i++) {
112 base = rgn->region[i].base;
113 size = rgn->region[i].size;
114 end = base + size - 1;
115 flags = rgn->region[i].flags;
116
117 ut_assert_nextline(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x",
118 name, i, base, end, size, flags);
119 }
Simon Glass2c522af2023-07-11 20:46:30 -0600120
121 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +0200122}
123
Simon Glass2c522af2023-07-11 20:46:30 -0600124static int lmb_test_dump_all(struct unit_test_state *uts, struct lmb *lmb)
Marek Vasutbf527662023-05-31 03:03:58 +0200125{
126 ut_assert_nextline("lmb_dump_all:");
127 lmb_test_dump_region(uts, &lmb->memory, "memory");
128 lmb_test_dump_region(uts, &lmb->reserved, "reserved");
Simon Glass2c522af2023-07-11 20:46:30 -0600129
130 return 0;
Marek Vasutbf527662023-05-31 03:03:58 +0200131}
132
Marek Vasut2696f3a2023-10-07 23:41:05 +0200133static int bdinfo_check_mem(struct unit_test_state *uts)
Marek Vasutbf527662023-05-31 03:03:58 +0200134{
135 struct bd_info *bd = gd->bd;
136 int i;
137
Marek Vasutbf527662023-05-31 03:03:58 +0200138 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
139 if (bd->bi_dram[i].size) {
Simon Glass2c522af2023-07-11 20:46:30 -0600140 ut_assertok(test_num_l(uts, "DRAM bank", i));
141 ut_assertok(test_num_ll(uts, "-> start",
142 bd->bi_dram[i].start));
143 ut_assertok(test_num_ll(uts, "-> size",
144 bd->bi_dram[i].size));
Marek Vasutbf527662023-05-31 03:03:58 +0200145 }
146 }
147
Marek Vasut2696f3a2023-10-07 23:41:05 +0200148 return 0;
149}
150
151static int bdinfo_test_all(struct unit_test_state *uts)
152{
153 ut_assertok(test_num_l(uts, "boot_params", 0));
154
155 ut_assertok(bdinfo_check_mem(uts));
156
Marek Vasutbf527662023-05-31 03:03:58 +0200157 /* CONFIG_SYS_HAS_SRAM testing not supported */
Simon Glass2c522af2023-07-11 20:46:30 -0600158 ut_assertok(test_num_l(uts, "flashstart", 0));
159 ut_assertok(test_num_l(uts, "flashsize", 0));
160 ut_assertok(test_num_l(uts, "flashoffset", 0));
Marek Vasutbf527662023-05-31 03:03:58 +0200161 ut_assert_nextline("baudrate = %lu bps",
162 env_get_ulong("baudrate", 10, 1234));
Simon Glass2c522af2023-07-11 20:46:30 -0600163 ut_assertok(test_num_l(uts, "relocaddr", gd->relocaddr));
164 ut_assertok(test_num_l(uts, "reloc off", gd->reloc_off));
Marek Vasutbf527662023-05-31 03:03:58 +0200165 ut_assert_nextline("%-12s= %u-bit", "Build", (uint)sizeof(void *) * 8);
166
167 if (IS_ENABLED(CONFIG_CMD_NET))
Simon Glass2c522af2023-07-11 20:46:30 -0600168 ut_assertok(test_eth(uts));
Marek Vasutbf527662023-05-31 03:03:58 +0200169
170 /*
171 * Make sure environment variable "fdtcontroladdr" address
172 * matches mapped control DT address.
173 */
174 ut_assert(map_to_sysmem(gd->fdt_blob) == env_get_hex("fdtcontroladdr", 0x1234));
Simon Glass2c522af2023-07-11 20:46:30 -0600175 ut_assertok(test_num_l(uts, "fdt_blob",
176 (ulong)map_to_sysmem(gd->fdt_blob)));
177 ut_assertok(test_num_l(uts, "new_fdt",
178 (ulong)map_to_sysmem(gd->new_fdt)));
179 ut_assertok(test_num_l(uts, "fdt_size", (ulong)gd->fdt_size));
Marek Vasutbf527662023-05-31 03:03:58 +0200180
181 if (IS_ENABLED(CONFIG_VIDEO))
182 test_video_info(uts);
183
184 /* The gd->multi_dtb_fit may not be available, hence, #if below. */
185#if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
Simon Glass2c522af2023-07-11 20:46:30 -0600186 ut_assertok(test_num_l(uts, "multi_dtb_fit", (ulong)gd->multi_dtb_fit));
Marek Vasutbf527662023-05-31 03:03:58 +0200187#endif
188
189 if (IS_ENABLED(CONFIG_LMB) && gd->fdt_blob) {
190 struct lmb lmb;
191
192 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
193 lmb_test_dump_all(uts, &lmb);
194 if (IS_ENABLED(CONFIG_OF_REAL))
195 ut_assert_nextline("devicetree = %s", fdtdec_get_srcname());
196 }
197
Simon Glass347a8452023-07-12 09:04:37 -0600198 if (IS_ENABLED(CONFIG_DM_SERIAL)) {
199 struct serial_device_info info;
200
201 ut_assertnonnull(gd->cur_serial_dev);
202 ut_assertok(serial_getinfo(gd->cur_serial_dev, &info));
203
204 ut_assertok(test_num_l(uts, "serial addr", info.addr));
205 ut_assertok(test_num_l(uts, " width", info.reg_width));
206 ut_assertok(test_num_l(uts, " shift", info.reg_shift));
207 ut_assertok(test_num_l(uts, " offset", info.reg_offset));
208 ut_assertok(test_num_l(uts, " clock", info.clock));
209 }
210
Simon Glassb279f512023-07-15 21:38:46 -0600211 if (IS_ENABLED(CONFIG_CMD_BDINFO_EXTRA)) {
212 ut_assert_nextlinen("stack ptr");
213 ut_assertok(test_num_ll(uts, "ram_top ptr",
214 (unsigned long long)gd->ram_top));
215 ut_assertok(test_num_l(uts, "malloc base", gd_malloc_start()));
216 }
217
Marek Vasut4823b052023-10-07 23:41:03 +0200218 return 0;
219}
220
221static int bdinfo_test_full(struct unit_test_state *uts)
222{
223 /* Test BDINFO full print */
224 ut_assertok(console_record_reset_enable());
225 ut_assertok(run_commandf("bdinfo"));
226 ut_assertok(bdinfo_test_all(uts));
227 ut_assertok(run_commandf("bdinfo -a"));
228 ut_assertok(bdinfo_test_all(uts));
Marek Vasutbf527662023-05-31 03:03:58 +0200229 ut_assertok(ut_check_console_end(uts));
230
231 return 0;
232}
233
Marek Vasut5bd32a92023-10-07 23:41:02 +0200234BDINFO_TEST(bdinfo_test_full, UT_TESTF_CONSOLE_REC);
Marek Vasutbf527662023-05-31 03:03:58 +0200235
Marek Vasut8827a382023-10-07 23:41:04 +0200236static int bdinfo_test_help(struct unit_test_state *uts)
237{
238 /* Test BDINFO unknown option help text print */
239 ut_assertok(console_record_reset_enable());
240 ut_asserteq(1, run_commandf("bdinfo -h"));
241 ut_assert_nextlinen("bdinfo: invalid option -- h");
242 ut_assert_nextlinen("bdinfo - print Board Info structure");
243 ut_assert_nextline_empty();
244 ut_assert_nextlinen("Usage:");
245 ut_assert_nextlinen("bdinfo");
246 ut_assertok(ut_check_console_end(uts));
247
248 return 0;
249}
250
251BDINFO_TEST(bdinfo_test_help, UT_TESTF_CONSOLE_REC);
252
Marek Vasut2696f3a2023-10-07 23:41:05 +0200253static int bdinfo_test_memory(struct unit_test_state *uts)
254{
255 /* Test BDINFO memory layout only print */
256 ut_assertok(console_record_reset_enable());
257 ut_assertok(run_commandf("bdinfo -m"));
258 ut_assertok(bdinfo_check_mem(uts));
259 ut_assertok(ut_check_console_end(uts));
260
261 return 0;
262}
263
264BDINFO_TEST(bdinfo_test_memory, UT_TESTF_CONSOLE_REC);
265
Marek Vasut3ff2d792023-10-07 23:41:06 +0200266static int bdinfo_test_eth(struct unit_test_state *uts)
267{
268 /* Test BDINFO ethernet settings only print */
269 ut_assertok(console_record_reset_enable());
270 ut_assertok(run_commandf("bdinfo -e"));
271 if (IS_ENABLED(CONFIG_CMD_NET))
272 ut_assertok(test_eth(uts));
273 ut_assertok(ut_check_console_end(uts));
274
275 return 0;
276}
277
278BDINFO_TEST(bdinfo_test_eth, UT_TESTF_CONSOLE_REC);
279
Marek Vasutbf527662023-05-31 03:03:58 +0200280int do_ut_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
281{
282 struct unit_test *tests = UNIT_TEST_SUITE_START(bdinfo_test);
283 const int n_ents = UNIT_TEST_SUITE_COUNT(bdinfo_test);
284
285 return cmd_ut_category("bdinfo", "bdinfo_test_", tests, n_ents, argc, argv);
286}