Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 1 | // 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 Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 8 | #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 Glass | 347a845 | 2023-07-12 09:04:37 -0600 | [diff] [blame] | 18 | #include <serial.h> |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 19 | #include <video.h> |
| 20 | #include <vsprintf.h> |
| 21 | #include <asm/cache.h> |
| 22 | #include <asm/global_data.h> |
| 23 | #include <display_options.h> |
| 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
| 27 | /* Declare a new bdinfo test */ |
| 28 | #define BDINFO_TEST(_name, _flags) UNIT_TEST(_name, _flags, bdinfo_test) |
| 29 | |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 30 | static int test_num_l(struct unit_test_state *uts, const char *name, |
| 31 | ulong value) |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 32 | { |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 33 | ut_assert_nextline("%-12s= 0x%0*lx", name, 2 * (int)sizeof(value), |
| 34 | value); |
| 35 | |
| 36 | return 0; |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 39 | static int test_num_ll(struct unit_test_state *uts, const char *name, |
| 40 | unsigned long long value) |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 41 | { |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 42 | ut_assert_nextline("%-12s= 0x%.*llx", name, 2 * (int)sizeof(ulong), |
| 43 | value); |
| 44 | |
| 45 | return 0; |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 48 | static int test_eth(struct unit_test_state *uts) |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 49 | { |
| 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 Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 68 | |
| 69 | return 0; |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 72 | static int test_video_info(struct unit_test_state *uts) |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 73 | { |
| 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 Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 84 | ut_assertok(test_num_ll(uts, "FB base", |
| 85 | (ulong)upriv->fb)); |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 86 | if (upriv->copy_fb) { |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 87 | 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 Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 91 | } |
| 92 | ut_assert_nextline("%-12s= %dx%dx%d", "FB size", |
| 93 | upriv->xsize, upriv->ysize, |
| 94 | 1 << upriv->bpix); |
| 95 | } |
| 96 | } |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 97 | |
| 98 | return 0; |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 101 | static int lmb_test_dump_region(struct unit_test_state *uts, |
| 102 | struct lmb_region *rgn, char *name) |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 103 | { |
| 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 Glass | 80a8a2e | 2023-10-01 19:15:16 -0600 | [diff] [blame] | 116 | /* |
| 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 Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 128 | ut_assert_nextline(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x", |
| 129 | name, i, base, end, size, flags); |
| 130 | } |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 131 | |
| 132 | return 0; |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 135 | static int lmb_test_dump_all(struct unit_test_state *uts, struct lmb *lmb) |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 136 | { |
| 137 | ut_assert_nextline("lmb_dump_all:"); |
Simon Glass | 3535f60 | 2023-10-01 19:15:17 -0600 | [diff] [blame] | 138 | ut_assertok(lmb_test_dump_region(uts, &lmb->memory, "memory")); |
| 139 | ut_assertok(lmb_test_dump_region(uts, &lmb->reserved, "reserved")); |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 140 | |
| 141 | return 0; |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Marek Vasut | 2696f3a | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 144 | static int bdinfo_check_mem(struct unit_test_state *uts) |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 145 | { |
| 146 | struct bd_info *bd = gd->bd; |
| 147 | int i; |
| 148 | |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 149 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) { |
| 150 | if (bd->bi_dram[i].size) { |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 151 | 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 Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Marek Vasut | 2696f3a | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static 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 Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 168 | /* CONFIG_SYS_HAS_SRAM testing not supported */ |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 169 | 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 Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 172 | ut_assert_nextline("baudrate = %lu bps", |
| 173 | env_get_ulong("baudrate", 10, 1234)); |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 174 | ut_assertok(test_num_l(uts, "relocaddr", gd->relocaddr)); |
| 175 | ut_assertok(test_num_l(uts, "reloc off", gd->reloc_off)); |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 176 | ut_assert_nextline("%-12s= %u-bit", "Build", (uint)sizeof(void *) * 8); |
| 177 | |
| 178 | if (IS_ENABLED(CONFIG_CMD_NET)) |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 179 | ut_assertok(test_eth(uts)); |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 180 | |
| 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 Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 186 | 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 Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 191 | |
| 192 | if (IS_ENABLED(CONFIG_VIDEO)) |
Simon Glass | 3535f60 | 2023-10-01 19:15:17 -0600 | [diff] [blame] | 193 | ut_assertok(test_video_info(uts)); |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 194 | |
| 195 | /* The gd->multi_dtb_fit may not be available, hence, #if below. */ |
| 196 | #if CONFIG_IS_ENABLED(MULTI_DTB_FIT) |
Simon Glass | 2c522af | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 197 | ut_assertok(test_num_l(uts, "multi_dtb_fit", (ulong)gd->multi_dtb_fit)); |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 198 | #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 Glass | 3535f60 | 2023-10-01 19:15:17 -0600 | [diff] [blame] | 204 | ut_assertok(lmb_test_dump_all(uts, &lmb)); |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 205 | if (IS_ENABLED(CONFIG_OF_REAL)) |
| 206 | ut_assert_nextline("devicetree = %s", fdtdec_get_srcname()); |
| 207 | } |
| 208 | |
Simon Glass | 347a845 | 2023-07-12 09:04:37 -0600 | [diff] [blame] | 209 | 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 Glass | b279f51 | 2023-07-15 21:38:46 -0600 | [diff] [blame] | 222 | 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 Glass | 3535f60 | 2023-10-01 19:15:17 -0600 | [diff] [blame] | 229 | if (IS_ENABLED(CONFIG_X86)) |
| 230 | ut_check_skip_to_linen(uts, " high end ="); |
| 231 | |
Marek Vasut | 4823b05 | 2023-10-07 23:41:03 +0200 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int bdinfo_test_full(struct unit_test_state *uts) |
| 236 | { |
| 237 | /* Test BDINFO full print */ |
Marek Vasut | 4823b05 | 2023-10-07 23:41:03 +0200 | [diff] [blame] | 238 | ut_assertok(run_commandf("bdinfo")); |
| 239 | ut_assertok(bdinfo_test_all(uts)); |
| 240 | ut_assertok(run_commandf("bdinfo -a")); |
| 241 | ut_assertok(bdinfo_test_all(uts)); |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 242 | ut_assertok(ut_check_console_end(uts)); |
| 243 | |
| 244 | return 0; |
| 245 | } |
Simon Glass | 9b99762 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 246 | BDINFO_TEST(bdinfo_test_full, UTF_CONSOLE); |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 247 | |
Marek Vasut | 8827a38 | 2023-10-07 23:41:04 +0200 | [diff] [blame] | 248 | static int bdinfo_test_help(struct unit_test_state *uts) |
| 249 | { |
| 250 | /* Test BDINFO unknown option help text print */ |
Marek Vasut | be4a5f7 | 2023-12-10 16:01:56 +0100 | [diff] [blame] | 251 | if (!CONFIG_IS_ENABLED(GETOPT)) { |
| 252 | ut_asserteq(0, run_commandf("bdinfo -h")); |
| 253 | ut_assertok(bdinfo_test_all(uts)); |
| 254 | } else { |
| 255 | ut_asserteq(1, run_commandf("bdinfo -h")); |
| 256 | ut_assert_nextlinen("bdinfo: invalid option -- h"); |
| 257 | ut_assert_nextlinen("bdinfo - print Board Info structure"); |
| 258 | ut_assert_nextline_empty(); |
| 259 | ut_assert_nextlinen("Usage:"); |
| 260 | ut_assert_nextlinen("bdinfo"); |
| 261 | } |
Marek Vasut | 8827a38 | 2023-10-07 23:41:04 +0200 | [diff] [blame] | 262 | ut_assertok(ut_check_console_end(uts)); |
| 263 | |
| 264 | return 0; |
| 265 | } |
Simon Glass | 9b99762 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 266 | BDINFO_TEST(bdinfo_test_help, UTF_CONSOLE); |
Marek Vasut | 8827a38 | 2023-10-07 23:41:04 +0200 | [diff] [blame] | 267 | |
Marek Vasut | 2696f3a | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 268 | static int bdinfo_test_memory(struct unit_test_state *uts) |
| 269 | { |
| 270 | /* Test BDINFO memory layout only print */ |
Marek Vasut | 2696f3a | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 271 | ut_assertok(run_commandf("bdinfo -m")); |
Marek Vasut | be4a5f7 | 2023-12-10 16:01:56 +0100 | [diff] [blame] | 272 | if (!CONFIG_IS_ENABLED(GETOPT)) |
| 273 | ut_assertok(bdinfo_test_all(uts)); |
| 274 | else |
| 275 | ut_assertok(bdinfo_check_mem(uts)); |
Marek Vasut | 2696f3a | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 276 | ut_assertok(ut_check_console_end(uts)); |
| 277 | |
| 278 | return 0; |
| 279 | } |
Simon Glass | 9b99762 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 280 | BDINFO_TEST(bdinfo_test_memory, UTF_CONSOLE); |
Marek Vasut | 2696f3a | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 281 | |
Marek Vasut | 3ff2d79 | 2023-10-07 23:41:06 +0200 | [diff] [blame] | 282 | static int bdinfo_test_eth(struct unit_test_state *uts) |
| 283 | { |
| 284 | /* Test BDINFO ethernet settings only print */ |
Marek Vasut | 3ff2d79 | 2023-10-07 23:41:06 +0200 | [diff] [blame] | 285 | ut_assertok(run_commandf("bdinfo -e")); |
Marek Vasut | be4a5f7 | 2023-12-10 16:01:56 +0100 | [diff] [blame] | 286 | if (!CONFIG_IS_ENABLED(GETOPT)) |
| 287 | ut_assertok(bdinfo_test_all(uts)); |
| 288 | else if (IS_ENABLED(CONFIG_CMD_NET)) |
Marek Vasut | 3ff2d79 | 2023-10-07 23:41:06 +0200 | [diff] [blame] | 289 | ut_assertok(test_eth(uts)); |
| 290 | ut_assertok(ut_check_console_end(uts)); |
| 291 | |
| 292 | return 0; |
| 293 | } |
Simon Glass | 9b99762 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 294 | BDINFO_TEST(bdinfo_test_eth, UTF_CONSOLE); |
Marek Vasut | 3ff2d79 | 2023-10-07 23:41:06 +0200 | [diff] [blame] | 295 | |
Marek Vasut | bf52766 | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 296 | int do_ut_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 297 | { |
| 298 | struct unit_test *tests = UNIT_TEST_SUITE_START(bdinfo_test); |
| 299 | const int n_ents = UNIT_TEST_SUITE_COUNT(bdinfo_test); |
| 300 | |
| 301 | return cmd_ut_category("bdinfo", "bdinfo_test_", tests, n_ents, argc, argv); |
| 302 | } |