Marek Vasut | 2cd173c | 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 | |
| 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 Glass | 3ddee31 | 2023-07-12 09:04:37 -0600 | [diff] [blame] | 19 | #include <serial.h> |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 20 | #include <video.h> |
| 21 | #include <vsprintf.h> |
| 22 | #include <asm/cache.h> |
| 23 | #include <asm/global_data.h> |
| 24 | #include <display_options.h> |
| 25 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
| 28 | /* Declare a new bdinfo test */ |
| 29 | #define BDINFO_TEST(_name, _flags) UNIT_TEST(_name, _flags, bdinfo_test) |
| 30 | |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 31 | static int test_num_l(struct unit_test_state *uts, const char *name, |
| 32 | ulong value) |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 33 | { |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 34 | ut_assert_nextline("%-12s= 0x%0*lx", name, 2 * (int)sizeof(value), |
| 35 | value); |
| 36 | |
| 37 | return 0; |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 40 | static int test_num_ll(struct unit_test_state *uts, const char *name, |
| 41 | unsigned long long value) |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 42 | { |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 43 | ut_assert_nextline("%-12s= 0x%.*llx", name, 2 * (int)sizeof(ulong), |
| 44 | value); |
| 45 | |
| 46 | return 0; |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 49 | static int test_eth(struct unit_test_state *uts) |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 50 | { |
| 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 Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 69 | |
| 70 | return 0; |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 73 | static int test_video_info(struct unit_test_state *uts) |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 74 | { |
| 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 Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 85 | ut_assertok(test_num_ll(uts, "FB base", |
| 86 | (ulong)upriv->fb)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 87 | if (upriv->copy_fb) { |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 88 | 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 Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 92 | } |
| 93 | ut_assert_nextline("%-12s= %dx%dx%d", "FB size", |
| 94 | upriv->xsize, upriv->ysize, |
| 95 | 1 << upriv->bpix); |
| 96 | } |
| 97 | } |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 98 | |
| 99 | return 0; |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 102 | static int lmb_test_dump_region(struct unit_test_state *uts, |
| 103 | struct lmb_region *rgn, char *name) |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 104 | { |
| 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 | |
Simon Glass | a44bd85 | 2023-10-01 19:15:16 -0600 | [diff] [blame] | 117 | /* |
| 118 | * this entry includes the stack (get_sp()) on many platforms |
| 119 | * so will different each time lmb_init_and_reserve() is called. |
| 120 | * We could instead have the bdinfo command put its lmb region |
| 121 | * in a known location, so we can check it directly, rather than |
| 122 | * calling lmb_init_and_reserve() to create a new (and hopefully |
| 123 | * identical one). But for now this seems good enough. |
| 124 | */ |
| 125 | if (!IS_ENABLED(CONFIG_SANDBOX) && i == 3) { |
| 126 | ut_assert_nextlinen(" %s[%d]\t[", name, i); |
| 127 | continue; |
| 128 | } |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 129 | ut_assert_nextline(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x", |
| 130 | name, i, base, end, size, flags); |
| 131 | } |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 132 | |
| 133 | return 0; |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 136 | static int lmb_test_dump_all(struct unit_test_state *uts, struct lmb *lmb) |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 137 | { |
| 138 | ut_assert_nextline("lmb_dump_all:"); |
Simon Glass | 43b9a32 | 2023-10-01 19:15:17 -0600 | [diff] [blame] | 139 | ut_assertok(lmb_test_dump_region(uts, &lmb->memory, "memory")); |
| 140 | ut_assertok(lmb_test_dump_region(uts, &lmb->reserved, "reserved")); |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 141 | |
| 142 | return 0; |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Marek Vasut | 3c5fd09 | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 145 | static int bdinfo_check_mem(struct unit_test_state *uts) |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 146 | { |
| 147 | struct bd_info *bd = gd->bd; |
| 148 | int i; |
| 149 | |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 150 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) { |
| 151 | if (bd->bi_dram[i].size) { |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 152 | ut_assertok(test_num_l(uts, "DRAM bank", i)); |
| 153 | ut_assertok(test_num_ll(uts, "-> start", |
| 154 | bd->bi_dram[i].start)); |
| 155 | ut_assertok(test_num_ll(uts, "-> size", |
| 156 | bd->bi_dram[i].size)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Marek Vasut | 3c5fd09 | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int bdinfo_test_all(struct unit_test_state *uts) |
| 164 | { |
| 165 | ut_assertok(test_num_l(uts, "boot_params", 0)); |
| 166 | |
| 167 | ut_assertok(bdinfo_check_mem(uts)); |
| 168 | |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 169 | /* CONFIG_SYS_HAS_SRAM testing not supported */ |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 170 | ut_assertok(test_num_l(uts, "flashstart", 0)); |
| 171 | ut_assertok(test_num_l(uts, "flashsize", 0)); |
| 172 | ut_assertok(test_num_l(uts, "flashoffset", 0)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 173 | ut_assert_nextline("baudrate = %lu bps", |
| 174 | env_get_ulong("baudrate", 10, 1234)); |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 175 | ut_assertok(test_num_l(uts, "relocaddr", gd->relocaddr)); |
| 176 | ut_assertok(test_num_l(uts, "reloc off", gd->reloc_off)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 177 | ut_assert_nextline("%-12s= %u-bit", "Build", (uint)sizeof(void *) * 8); |
| 178 | |
| 179 | if (IS_ENABLED(CONFIG_CMD_NET)) |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 180 | ut_assertok(test_eth(uts)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * Make sure environment variable "fdtcontroladdr" address |
| 184 | * matches mapped control DT address. |
| 185 | */ |
| 186 | ut_assert(map_to_sysmem(gd->fdt_blob) == env_get_hex("fdtcontroladdr", 0x1234)); |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 187 | ut_assertok(test_num_l(uts, "fdt_blob", |
| 188 | (ulong)map_to_sysmem(gd->fdt_blob))); |
| 189 | ut_assertok(test_num_l(uts, "new_fdt", |
| 190 | (ulong)map_to_sysmem(gd->new_fdt))); |
| 191 | ut_assertok(test_num_l(uts, "fdt_size", (ulong)gd->fdt_size)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 192 | |
| 193 | if (IS_ENABLED(CONFIG_VIDEO)) |
Simon Glass | 43b9a32 | 2023-10-01 19:15:17 -0600 | [diff] [blame] | 194 | ut_assertok(test_video_info(uts)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 195 | |
| 196 | /* The gd->multi_dtb_fit may not be available, hence, #if below. */ |
| 197 | #if CONFIG_IS_ENABLED(MULTI_DTB_FIT) |
Simon Glass | 0a5e069 | 2023-07-11 20:46:30 -0600 | [diff] [blame] | 198 | ut_assertok(test_num_l(uts, "multi_dtb_fit", (ulong)gd->multi_dtb_fit)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 199 | #endif |
| 200 | |
| 201 | if (IS_ENABLED(CONFIG_LMB) && gd->fdt_blob) { |
| 202 | struct lmb lmb; |
| 203 | |
| 204 | lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob); |
Simon Glass | 43b9a32 | 2023-10-01 19:15:17 -0600 | [diff] [blame] | 205 | ut_assertok(lmb_test_dump_all(uts, &lmb)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 206 | if (IS_ENABLED(CONFIG_OF_REAL)) |
| 207 | ut_assert_nextline("devicetree = %s", fdtdec_get_srcname()); |
| 208 | } |
| 209 | |
Simon Glass | 3ddee31 | 2023-07-12 09:04:37 -0600 | [diff] [blame] | 210 | if (IS_ENABLED(CONFIG_DM_SERIAL)) { |
| 211 | struct serial_device_info info; |
| 212 | |
| 213 | ut_assertnonnull(gd->cur_serial_dev); |
| 214 | ut_assertok(serial_getinfo(gd->cur_serial_dev, &info)); |
| 215 | |
| 216 | ut_assertok(test_num_l(uts, "serial addr", info.addr)); |
| 217 | ut_assertok(test_num_l(uts, " width", info.reg_width)); |
| 218 | ut_assertok(test_num_l(uts, " shift", info.reg_shift)); |
| 219 | ut_assertok(test_num_l(uts, " offset", info.reg_offset)); |
| 220 | ut_assertok(test_num_l(uts, " clock", info.clock)); |
| 221 | } |
| 222 | |
Simon Glass | 5087c77 | 2023-07-15 21:38:46 -0600 | [diff] [blame] | 223 | if (IS_ENABLED(CONFIG_CMD_BDINFO_EXTRA)) { |
| 224 | ut_assert_nextlinen("stack ptr"); |
| 225 | ut_assertok(test_num_ll(uts, "ram_top ptr", |
| 226 | (unsigned long long)gd->ram_top)); |
| 227 | ut_assertok(test_num_l(uts, "malloc base", gd_malloc_start())); |
| 228 | } |
| 229 | |
Simon Glass | 43b9a32 | 2023-10-01 19:15:17 -0600 | [diff] [blame] | 230 | if (IS_ENABLED(CONFIG_X86)) |
| 231 | ut_check_skip_to_linen(uts, " high end ="); |
| 232 | |
Marek Vasut | eb0b1db | 2023-10-07 23:41:03 +0200 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int bdinfo_test_full(struct unit_test_state *uts) |
| 237 | { |
| 238 | /* Test BDINFO full print */ |
| 239 | ut_assertok(console_record_reset_enable()); |
| 240 | ut_assertok(run_commandf("bdinfo")); |
| 241 | ut_assertok(bdinfo_test_all(uts)); |
| 242 | ut_assertok(run_commandf("bdinfo -a")); |
| 243 | ut_assertok(bdinfo_test_all(uts)); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 244 | ut_assertok(ut_check_console_end(uts)); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Marek Vasut | 391e5c3 | 2023-10-07 23:41:02 +0200 | [diff] [blame] | 249 | BDINFO_TEST(bdinfo_test_full, UT_TESTF_CONSOLE_REC); |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 250 | |
Marek Vasut | 2409924 | 2023-10-07 23:41:04 +0200 | [diff] [blame] | 251 | static int bdinfo_test_help(struct unit_test_state *uts) |
| 252 | { |
| 253 | /* Test BDINFO unknown option help text print */ |
| 254 | ut_assertok(console_record_reset_enable()); |
Marek Vasut | 949f0fe | 2023-12-10 16:01:56 +0100 | [diff] [blame] | 255 | if (!CONFIG_IS_ENABLED(GETOPT)) { |
| 256 | ut_asserteq(0, run_commandf("bdinfo -h")); |
| 257 | ut_assertok(bdinfo_test_all(uts)); |
| 258 | } else { |
| 259 | ut_asserteq(1, run_commandf("bdinfo -h")); |
| 260 | ut_assert_nextlinen("bdinfo: invalid option -- h"); |
| 261 | ut_assert_nextlinen("bdinfo - print Board Info structure"); |
| 262 | ut_assert_nextline_empty(); |
| 263 | ut_assert_nextlinen("Usage:"); |
| 264 | ut_assert_nextlinen("bdinfo"); |
| 265 | } |
Marek Vasut | 2409924 | 2023-10-07 23:41:04 +0200 | [diff] [blame] | 266 | ut_assertok(ut_check_console_end(uts)); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | BDINFO_TEST(bdinfo_test_help, UT_TESTF_CONSOLE_REC); |
| 272 | |
Marek Vasut | 3c5fd09 | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 273 | static int bdinfo_test_memory(struct unit_test_state *uts) |
| 274 | { |
| 275 | /* Test BDINFO memory layout only print */ |
| 276 | ut_assertok(console_record_reset_enable()); |
| 277 | ut_assertok(run_commandf("bdinfo -m")); |
Marek Vasut | 949f0fe | 2023-12-10 16:01:56 +0100 | [diff] [blame] | 278 | if (!CONFIG_IS_ENABLED(GETOPT)) |
| 279 | ut_assertok(bdinfo_test_all(uts)); |
| 280 | else |
| 281 | ut_assertok(bdinfo_check_mem(uts)); |
Marek Vasut | 3c5fd09 | 2023-10-07 23:41:05 +0200 | [diff] [blame] | 282 | ut_assertok(ut_check_console_end(uts)); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | BDINFO_TEST(bdinfo_test_memory, UT_TESTF_CONSOLE_REC); |
| 288 | |
Marek Vasut | f14a03b | 2023-10-07 23:41:06 +0200 | [diff] [blame] | 289 | static int bdinfo_test_eth(struct unit_test_state *uts) |
| 290 | { |
| 291 | /* Test BDINFO ethernet settings only print */ |
| 292 | ut_assertok(console_record_reset_enable()); |
| 293 | ut_assertok(run_commandf("bdinfo -e")); |
Marek Vasut | 949f0fe | 2023-12-10 16:01:56 +0100 | [diff] [blame] | 294 | if (!CONFIG_IS_ENABLED(GETOPT)) |
| 295 | ut_assertok(bdinfo_test_all(uts)); |
| 296 | else if (IS_ENABLED(CONFIG_CMD_NET)) |
Marek Vasut | f14a03b | 2023-10-07 23:41:06 +0200 | [diff] [blame] | 297 | ut_assertok(test_eth(uts)); |
| 298 | ut_assertok(ut_check_console_end(uts)); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | BDINFO_TEST(bdinfo_test_eth, UT_TESTF_CONSOLE_REC); |
| 304 | |
Marek Vasut | 2cd173c | 2023-05-31 03:03:58 +0200 | [diff] [blame] | 305 | int do_ut_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 306 | { |
| 307 | struct unit_test *tests = UNIT_TEST_SUITE_START(bdinfo_test); |
| 308 | const int n_ents = UNIT_TEST_SUITE_COUNT(bdinfo_test); |
| 309 | |
| 310 | return cmd_ut_category("bdinfo", "bdinfo_test_", tests, n_ents, argc, argv); |
| 311 | } |