blob: bb419ab2394ecfae019b89fc2a292d03160ae7db [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
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100110 ut_assert_nextline(" %s.count = %#x", 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 }
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100122 ut_assert_nextlinen(" %s[%d]\t[%#llx-%#llx], %#llx bytes, flags: ",
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530123 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)));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200184
185 if (IS_ENABLED(CONFIG_VIDEO))
Simon Glass43b9a322023-10-01 19:15:17 -0600186 ut_assertok(test_video_info(uts));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200187
188 /* The gd->multi_dtb_fit may not be available, hence, #if below. */
189#if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
Simon Glass0a5e0692023-07-11 20:46:30 -0600190 ut_assertok(test_num_l(uts, "multi_dtb_fit", (ulong)gd->multi_dtb_fit));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200191#endif
192
193 if (IS_ENABLED(CONFIG_LMB) && gd->fdt_blob) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530194 ut_assertok(lmb_test_dump_all(uts));
Marek Vasut2cd173c2023-05-31 03:03:58 +0200195 if (IS_ENABLED(CONFIG_OF_REAL))
196 ut_assert_nextline("devicetree = %s", fdtdec_get_srcname());
197 }
198
Simon Glass3ddee312023-07-12 09:04:37 -0600199 if (IS_ENABLED(CONFIG_DM_SERIAL)) {
200 struct serial_device_info info;
201
202 ut_assertnonnull(gd->cur_serial_dev);
203 ut_assertok(serial_getinfo(gd->cur_serial_dev, &info));
204
205 ut_assertok(test_num_l(uts, "serial addr", info.addr));
206 ut_assertok(test_num_l(uts, " width", info.reg_width));
207 ut_assertok(test_num_l(uts, " shift", info.reg_shift));
208 ut_assertok(test_num_l(uts, " offset", info.reg_offset));
209 ut_assertok(test_num_l(uts, " clock", info.clock));
210 }
211
Simon Glass5087c772023-07-15 21:38:46 -0600212 if (IS_ENABLED(CONFIG_CMD_BDINFO_EXTRA)) {
213 ut_assert_nextlinen("stack ptr");
214 ut_assertok(test_num_ll(uts, "ram_top ptr",
215 (unsigned long long)gd->ram_top));
216 ut_assertok(test_num_l(uts, "malloc base", gd_malloc_start()));
217 }
218
Simon Glass43b9a322023-10-01 19:15:17 -0600219 if (IS_ENABLED(CONFIG_X86))
220 ut_check_skip_to_linen(uts, " high end =");
221
Marek Vasuteb0b1db2023-10-07 23:41:03 +0200222 return 0;
223}
224
225static int bdinfo_test_full(struct unit_test_state *uts)
226{
227 /* Test BDINFO full print */
Marek Vasuteb0b1db2023-10-07 23:41:03 +0200228 ut_assertok(run_commandf("bdinfo"));
229 ut_assertok(bdinfo_test_all(uts));
230 ut_assertok(run_commandf("bdinfo -a"));
231 ut_assertok(bdinfo_test_all(uts));
Simon Glassc579bd42024-08-22 07:58:03 -0600232 ut_assert_console_end();
Marek Vasut2cd173c2023-05-31 03:03:58 +0200233
234 return 0;
235}
Simon Glass11fcfa32024-08-22 07:57:50 -0600236BDINFO_TEST(bdinfo_test_full, UTF_CONSOLE);
Marek Vasut2cd173c2023-05-31 03:03:58 +0200237
Marek Vasut24099242023-10-07 23:41:04 +0200238static int bdinfo_test_help(struct unit_test_state *uts)
239{
240 /* Test BDINFO unknown option help text print */
Marek Vasut949f0fe2023-12-10 16:01:56 +0100241 if (!CONFIG_IS_ENABLED(GETOPT)) {
242 ut_asserteq(0, run_commandf("bdinfo -h"));
243 ut_assertok(bdinfo_test_all(uts));
244 } else {
245 ut_asserteq(1, run_commandf("bdinfo -h"));
246 ut_assert_nextlinen("bdinfo: invalid option -- h");
247 ut_assert_nextlinen("bdinfo - print Board Info structure");
248 ut_assert_nextline_empty();
249 ut_assert_nextlinen("Usage:");
250 ut_assert_nextlinen("bdinfo");
251 }
Simon Glassc579bd42024-08-22 07:58:03 -0600252 ut_assert_console_end();
Marek Vasut24099242023-10-07 23:41:04 +0200253
254 return 0;
255}
Simon Glass11fcfa32024-08-22 07:57:50 -0600256BDINFO_TEST(bdinfo_test_help, UTF_CONSOLE);
Marek Vasut24099242023-10-07 23:41:04 +0200257
Marek Vasut3c5fd092023-10-07 23:41:05 +0200258static int bdinfo_test_memory(struct unit_test_state *uts)
259{
260 /* Test BDINFO memory layout only print */
Marek Vasut3c5fd092023-10-07 23:41:05 +0200261 ut_assertok(run_commandf("bdinfo -m"));
Marek Vasut949f0fe2023-12-10 16:01:56 +0100262 if (!CONFIG_IS_ENABLED(GETOPT))
263 ut_assertok(bdinfo_test_all(uts));
264 else
265 ut_assertok(bdinfo_check_mem(uts));
Simon Glassc579bd42024-08-22 07:58:03 -0600266 ut_assert_console_end();
Marek Vasut3c5fd092023-10-07 23:41:05 +0200267
268 return 0;
269}
Simon Glass11fcfa32024-08-22 07:57:50 -0600270BDINFO_TEST(bdinfo_test_memory, UTF_CONSOLE);
Marek Vasut3c5fd092023-10-07 23:41:05 +0200271
Marek Vasutf14a03b2023-10-07 23:41:06 +0200272static int bdinfo_test_eth(struct unit_test_state *uts)
273{
274 /* Test BDINFO ethernet settings only print */
Marek Vasutf14a03b2023-10-07 23:41:06 +0200275 ut_assertok(run_commandf("bdinfo -e"));
Marek Vasut949f0fe2023-12-10 16:01:56 +0100276 if (!CONFIG_IS_ENABLED(GETOPT))
277 ut_assertok(bdinfo_test_all(uts));
278 else if (IS_ENABLED(CONFIG_CMD_NET))
Marek Vasutf14a03b2023-10-07 23:41:06 +0200279 ut_assertok(test_eth(uts));
Simon Glassc579bd42024-08-22 07:58:03 -0600280 ut_assert_console_end();
Marek Vasutf14a03b2023-10-07 23:41:06 +0200281
282 return 0;
283}
Simon Glass11fcfa32024-08-22 07:57:50 -0600284BDINFO_TEST(bdinfo_test_eth, UTF_CONSOLE);
Marek Vasutf14a03b2023-10-07 23:41:06 +0200285
Marek Vasut2cd173c2023-05-31 03:03:58 +0200286int do_ut_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
287{
288 struct unit_test *tests = UNIT_TEST_SUITE_START(bdinfo_test);
289 const int n_ents = UNIT_TEST_SUITE_COUNT(bdinfo_test);
290
291 return cmd_ut_category("bdinfo", "bdinfo_test_", tests, n_ents, argc, argv);
292}