Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Test for bootdev functions. All start with 'bootdev' |
| 4 | * |
| 5 | * Copyright 2021 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <bootstd.h> |
| 11 | #include <dm.h> |
| 12 | #include <bootdev.h> |
| 13 | #include <bootflow.h> |
| 14 | #include <mapmem.h> |
| 15 | #include <os.h> |
| 16 | #include <test/suites.h> |
| 17 | #include <test/ut.h> |
| 18 | #include "bootstd_common.h" |
| 19 | |
| 20 | /* Allow reseting the USB-started flag */ |
| 21 | extern char usb_started; |
| 22 | |
| 23 | /* Check 'bootdev list' command */ |
| 24 | static int bootdev_test_cmd_list(struct unit_test_state *uts) |
| 25 | { |
| 26 | int probed; |
| 27 | |
| 28 | console_record_reset_enable(); |
| 29 | for (probed = 0; probed < 2; probed++) { |
| 30 | int probe_ch = probed ? '+' : ' '; |
| 31 | |
| 32 | ut_assertok(run_command(probed ? "bootdev list -p" : |
| 33 | "bootdev list", 0)); |
| 34 | ut_assert_nextline("Seq Probed Status Uclass Name"); |
| 35 | ut_assert_nextlinen("---"); |
| 36 | ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 0, probe_ch, "OK", |
| 37 | "mmc", "mmc2.bootdev"); |
| 38 | ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 1, probe_ch, "OK", |
| 39 | "mmc", "mmc1.bootdev"); |
| 40 | ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 2, probe_ch, "OK", |
| 41 | "mmc", "mmc0.bootdev"); |
| 42 | ut_assert_nextlinen("---"); |
| 43 | ut_assert_nextline("(3 bootdevs)"); |
| 44 | ut_assert_console_end(); |
| 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | BOOTSTD_TEST(bootdev_test_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 50 | |
| 51 | /* Check 'bootdev select' and 'info' commands */ |
| 52 | static int bootdev_test_cmd_select(struct unit_test_state *uts) |
| 53 | { |
| 54 | struct bootstd_priv *std; |
| 55 | |
| 56 | /* get access to the CLI's cur_bootdev */ |
| 57 | ut_assertok(bootstd_get_priv(&std)); |
| 58 | |
| 59 | console_record_reset_enable(); |
| 60 | ut_asserteq(1, run_command("bootdev info", 0)); |
| 61 | ut_assert_nextlinen("Please use"); |
| 62 | ut_assert_console_end(); |
| 63 | |
| 64 | /* select by sequence */ |
| 65 | ut_assertok(run_command("bootdev select 0", 0)); |
| 66 | ut_assert_console_end(); |
| 67 | |
| 68 | ut_assertok(run_command("bootdev info", 0)); |
| 69 | ut_assert_nextline("Name: mmc2.bootdev"); |
| 70 | ut_assert_nextline("Sequence: 0"); |
| 71 | ut_assert_nextline("Status: Probed"); |
| 72 | ut_assert_nextline("Uclass: mmc"); |
| 73 | ut_assert_nextline("Bootflows: 0 (0 valid)"); |
| 74 | ut_assert_console_end(); |
| 75 | |
| 76 | /* select by bootdev name */ |
| 77 | ut_assertok(run_command("bootdev select mmc1.bootdev", 0)); |
| 78 | ut_assert_console_end(); |
| 79 | ut_assertnonnull(std->cur_bootdev); |
| 80 | ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name); |
| 81 | |
| 82 | /* select by bootdev label*/ |
| 83 | ut_assertok(run_command("bootdev select mmc1", 0)); |
| 84 | ut_assert_console_end(); |
| 85 | ut_assertnonnull(std->cur_bootdev); |
| 86 | ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name); |
| 87 | |
| 88 | /* deselect */ |
| 89 | ut_assertok(run_command("bootdev select", 0)); |
| 90 | ut_assert_console_end(); |
| 91 | ut_assertnull(std->cur_bootdev); |
| 92 | |
| 93 | ut_asserteq(1, run_command("bootdev info", 0)); |
| 94 | ut_assert_nextlinen("Please use"); |
| 95 | ut_assert_console_end(); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | BOOTSTD_TEST(bootdev_test_cmd_select, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 100 | |
| 101 | /* Check bootdev labels */ |
| 102 | static int bootdev_test_labels(struct unit_test_state *uts) |
| 103 | { |
| 104 | struct udevice *dev, *media; |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 105 | int mflags = 0; |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 106 | |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 107 | ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 108 | ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev)); |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 109 | ut_asserteq(0, mflags); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 110 | media = dev_get_parent(dev); |
| 111 | ut_asserteq(UCLASS_MMC, device_get_uclass_id(media)); |
| 112 | ut_asserteq_str("mmc2", media->name); |
| 113 | |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 114 | /* Check method flags */ |
| 115 | ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags)); |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 116 | ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY, |
| 117 | mflags); |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 118 | ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags)); |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 119 | ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY, |
| 120 | mflags); |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 121 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 122 | /* Check invalid uclass */ |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 123 | ut_asserteq(-EINVAL, bootdev_find_by_label("fred0", &dev, &mflags)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 124 | |
| 125 | /* Check unknown sequence number */ |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 126 | ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags)); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 130 | BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 131 | UT_TESTF_ETH_BOOTDEV); |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 132 | |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 133 | /* Check bootdev_find_by_any() */ |
| 134 | static int bootdev_test_any(struct unit_test_state *uts) |
| 135 | { |
| 136 | struct udevice *dev, *media; |
| 137 | int mflags; |
| 138 | |
| 139 | /* |
| 140 | * with ethernet enabled we have 8 devices ahead of the mmc ones: |
| 141 | * |
| 142 | * ut_assertok(run_command("bootdev list", 0)); |
| 143 | * Seq Probed Status Uclass Name |
| 144 | * --- ------ ------ -------- ------------------ |
| 145 | * 0 [ + ] OK ethernet eth@10002000.bootdev |
| 146 | * 1 [ ] OK ethernet eth@10003000.bootdev |
| 147 | * 2 [ ] OK ethernet sbe5.bootdev |
| 148 | * 3 [ ] OK ethernet eth@10004000.bootdev |
| 149 | * 4 [ ] OK ethernet phy-test-eth.bootdev |
| 150 | * 5 [ ] OK ethernet dsa-test-eth.bootdev |
| 151 | * 6 [ ] OK ethernet dsa-test@0.bootdev |
| 152 | * 7 [ ] OK ethernet dsa-test@1.bootdev |
| 153 | * 8 [ ] OK mmc mmc2.bootdev |
| 154 | * 9 [ + ] OK mmc mmc1.bootdev |
| 155 | * a [ ] OK mmc mmc0.bootdev |
| 156 | */ |
| 157 | console_record_reset_enable(); |
| 158 | ut_assertok(bootdev_find_by_any("8", &dev, &mflags)); |
| 159 | ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev)); |
| 160 | ut_asserteq(BOOTFLOW_METHF_SINGLE_DEV, mflags); |
| 161 | media = dev_get_parent(dev); |
| 162 | ut_asserteq(UCLASS_MMC, device_get_uclass_id(media)); |
| 163 | ut_asserteq_str("mmc2", media->name); |
| 164 | ut_assert_console_end(); |
| 165 | |
| 166 | /* there should not be this many bootdevs */ |
| 167 | ut_asserteq(-ENODEV, bootdev_find_by_any("50", &dev, &mflags)); |
| 168 | ut_assert_nextline("Cannot find '50' (err=-19)"); |
| 169 | ut_assert_console_end(); |
| 170 | |
| 171 | /* Check method flags */ |
| 172 | ut_assertok(bootdev_find_by_any("pxe", &dev, &mflags)); |
| 173 | ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY, |
| 174 | mflags); |
| 175 | |
| 176 | /* Check invalid uclass */ |
| 177 | mflags = 123; |
| 178 | ut_asserteq(-EINVAL, bootdev_find_by_any("fred0", &dev, &mflags)); |
| 179 | ut_assert_nextline("Unknown uclass 'fred0' in label"); |
| 180 | ut_assert_nextline("Cannot find bootdev 'fred0' (err=-22)"); |
| 181 | ut_asserteq(123, mflags); |
| 182 | ut_assert_console_end(); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | BOOTSTD_TEST(bootdev_test_any, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 187 | UT_TESTF_ETH_BOOTDEV); |
| 188 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 189 | #if 0 /* disable for now */ |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 190 | /* Check bootdev ordering with the bootdev-order property */ |
| 191 | static int bootdev_test_order(struct unit_test_state *uts) |
| 192 | { |
| 193 | struct bootflow_iter iter; |
| 194 | struct bootflow bflow; |
| 195 | |
| 196 | /* |
| 197 | * First try the order set by the bootdev-order property |
| 198 | * Like all sandbox unit tests this relies on the devicetree setting up |
| 199 | * the required devices: |
| 200 | * |
| 201 | * mmc0 - nothing connected |
| 202 | * mmc1 - connected to mmc1.img file |
| 203 | * mmc2 - nothing connected |
| 204 | */ |
| 205 | ut_assertok(env_set("boot_targets", NULL)); |
| 206 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 207 | ut_asserteq(2, iter.num_devs); |
| 208 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 209 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name); |
| 210 | bootflow_iter_uninit(&iter); |
| 211 | |
| 212 | /* Use the environment variable to override it */ |
| 213 | ut_assertok(env_set("boot_targets", "mmc1 mmc2")); |
| 214 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 215 | ut_asserteq(2, iter.num_devs); |
| 216 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[0]->name); |
| 217 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name); |
| 218 | bootflow_iter_uninit(&iter); |
| 219 | |
| 220 | /* |
| 221 | * Now drop both orderings, to check the default (prioriy/sequence) |
| 222 | * ordering |
| 223 | */ |
| 224 | ut_assertok(env_set("boot_targets", NULL)); |
| 225 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 226 | |
| 227 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 228 | ut_asserteq(3, iter.num_devs); |
| 229 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 230 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name); |
| 231 | ut_asserteq_str("mmc0.bootdev", iter.dev_order[2]->name); |
| 232 | |
| 233 | /* |
| 234 | * Check that adding aliases for the bootdevs works. We just fake it by |
| 235 | * setting the sequence numbers directly. |
| 236 | */ |
| 237 | iter.dev_order[0]->seq_ = 0; |
| 238 | iter.dev_order[1]->seq_ = 3; |
| 239 | iter.dev_order[2]->seq_ = 2; |
| 240 | bootflow_iter_uninit(&iter); |
| 241 | |
| 242 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 243 | ut_asserteq(3, iter.num_devs); |
| 244 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 245 | ut_asserteq_str("mmc0.bootdev", iter.dev_order[1]->name); |
| 246 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[2]->name); |
| 247 | bootflow_iter_uninit(&iter); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 252 | |
| 253 | /* Check bootdev ordering with the uclass priority */ |
| 254 | static int bootdev_test_prio(struct unit_test_state *uts) |
| 255 | { |
| 256 | struct bootdev_uc_plat *ucp; |
| 257 | struct bootflow_iter iter; |
| 258 | struct bootflow bflow; |
| 259 | struct udevice *blk; |
| 260 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 261 | test_set_skip_delays(true); |
| 262 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 263 | /* Start up USB which gives us three additional bootdevs */ |
| 264 | usb_started = false; |
| 265 | ut_assertok(run_command("usb start", 0)); |
| 266 | |
| 267 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 268 | |
| 269 | /* 3 MMC and 3 USB bootdevs: MMC should come before USB */ |
| 270 | console_record_reset_enable(); |
| 271 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 272 | ut_asserteq(6, iter.num_devs); |
| 273 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 274 | ut_asserteq_str("usb_mass_storage.lun0.bootdev", |
| 275 | iter.dev_order[3]->name); |
| 276 | |
| 277 | ut_assertok(bootdev_get_sibling_blk(iter.dev_order[3], &blk)); |
| 278 | ut_asserteq_str("usb_mass_storage.lun0", blk->name); |
| 279 | |
| 280 | /* adjust the priority of the first USB bootdev to the highest */ |
| 281 | ucp = dev_get_uclass_plat(iter.dev_order[3]); |
| 282 | ucp->prio = 1; |
| 283 | |
| 284 | bootflow_iter_uninit(&iter); |
| 285 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 286 | ut_asserteq(6, iter.num_devs); |
| 287 | ut_asserteq_str("usb_mass_storage.lun0.bootdev", |
| 288 | iter.dev_order[0]->name); |
| 289 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 294 | #endif |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 295 | |
| 296 | /* Check listing hunters */ |
| 297 | static int bootdev_test_hunter(struct unit_test_state *uts) |
| 298 | { |
| 299 | struct bootstd_priv *std; |
| 300 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 301 | test_set_skip_delays(true); |
| 302 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 303 | /* get access to the used hunters */ |
| 304 | ut_assertok(bootstd_get_priv(&std)); |
| 305 | |
| 306 | console_record_reset_enable(); |
| 307 | bootdev_list_hunters(std); |
| 308 | ut_assert_nextline("Prio Used Uclass Hunter"); |
| 309 | ut_assert_nextlinen("----"); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 310 | ut_assert_nextline(" 6 ethernet eth_bootdev"); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 311 | ut_assert_nextline(" 1 simple_bus (none)"); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 312 | ut_assert_nextline(" 5 ide ide_bootdev"); |
| 313 | ut_assert_nextline(" 2 mmc mmc_bootdev"); |
| 314 | ut_assert_nextline(" 4 nvme nvme_bootdev"); |
| 315 | ut_assert_nextline(" 4 scsi scsi_bootdev"); |
| 316 | ut_assert_nextline(" 4 spi_flash sf_bootdev"); |
| 317 | ut_assert_nextline(" 5 usb usb_bootdev"); |
| 318 | ut_assert_nextline(" 4 virtio virtio_bootdev"); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 319 | ut_assert_nextline("(total hunters: 9)"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 320 | ut_assert_console_end(); |
| 321 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 322 | ut_assertok(bootdev_hunt("usb1", false)); |
| 323 | ut_assert_nextline( |
| 324 | "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); |
Simon Glass | 1248d2b | 2023-01-17 10:47:34 -0700 | [diff] [blame] | 325 | ut_assert_console_end(); |
| 326 | |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 327 | /* USB is sixth in the list, so bit 7 */ |
| 328 | ut_asserteq(BIT(7), std->hunters_used); |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 329 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 333 | |
| 334 | /* Check 'bootdev hunt' command */ |
| 335 | static int bootdev_test_cmd_hunt(struct unit_test_state *uts) |
| 336 | { |
| 337 | struct bootstd_priv *std; |
| 338 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 339 | test_set_skip_delays(true); |
| 340 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 341 | /* get access to the used hunters */ |
| 342 | ut_assertok(bootstd_get_priv(&std)); |
| 343 | |
| 344 | console_record_reset_enable(); |
| 345 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 346 | ut_assert_nextline("Prio Used Uclass Hunter"); |
| 347 | ut_assert_nextlinen("----"); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 348 | ut_assert_nextline(" 6 ethernet eth_bootdev"); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 349 | ut_assert_skip_to_line("(total hunters: 9)"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 350 | ut_assert_console_end(); |
| 351 | |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 352 | /* Use the MMC hunter and see that it updates */ |
| 353 | ut_assertok(run_command("bootdev hunt mmc", 0)); |
| 354 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 355 | ut_assert_skip_to_line(" 5 ide ide_bootdev"); |
| 356 | ut_assert_nextline(" 2 * mmc mmc_bootdev"); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 357 | ut_assert_skip_to_line("(total hunters: 9)"); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 358 | ut_assert_console_end(); |
| 359 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 360 | /* Scan all hunters */ |
Simon Glass | ac06b26a | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 361 | test_set_eth_enable(false); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 362 | test_set_skip_delays(true); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 363 | ut_assertok(run_command("bootdev hunt", 0)); |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 364 | ut_assert_nextline("Hunting with: ethernet"); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 365 | |
| 366 | /* This is the extension feature which has no uclass at present */ |
| 367 | ut_assert_nextline("Hunting with: simple_bus"); |
| 368 | ut_assert_nextline("Found 2 extension board(s)."); |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame] | 369 | ut_assert_nextline("Hunting with: ide"); |
| 370 | ut_assert_nextline("Bus 0: not available "); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 371 | |
| 372 | /* mmc hunter has already been used so should not run again */ |
| 373 | |
Simon Glass | 0838287 | 2023-01-17 10:47:47 -0700 | [diff] [blame] | 374 | ut_assert_nextline("Hunting with: nvme"); |
Simon Glass | 9985e9c | 2023-01-17 10:47:45 -0700 | [diff] [blame] | 375 | ut_assert_nextline("Hunting with: scsi"); |
| 376 | ut_assert_nextline("scanning bus for devices..."); |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 377 | ut_assert_skip_to_line("Hunting with: spi_flash"); |
| 378 | ut_assert_nextline("Hunting with: usb"); |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 379 | ut_assert_nextline( |
| 380 | "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 381 | ut_assert_nextline("Hunting with: virtio"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 382 | ut_assert_console_end(); |
| 383 | |
| 384 | /* List available hunters */ |
| 385 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 386 | ut_assert_nextlinen("Prio"); |
| 387 | ut_assert_nextlinen("----"); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 388 | ut_assert_nextline(" 6 * ethernet eth_bootdev"); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 389 | ut_assert_nextline(" 1 * simple_bus (none)"); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 390 | ut_assert_nextline(" 5 * ide ide_bootdev"); |
| 391 | ut_assert_nextline(" 2 * mmc mmc_bootdev"); |
| 392 | ut_assert_nextline(" 4 * nvme nvme_bootdev"); |
| 393 | ut_assert_nextline(" 4 * scsi scsi_bootdev"); |
| 394 | ut_assert_nextline(" 4 * spi_flash sf_bootdev"); |
| 395 | ut_assert_nextline(" 5 * usb usb_bootdev"); |
| 396 | ut_assert_nextline(" 4 * virtio virtio_bootdev"); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 397 | ut_assert_nextline("(total hunters: 9)"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 398 | ut_assert_console_end(); |
| 399 | |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 400 | ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 405 | UT_TESTF_ETH_BOOTDEV); |
Simon Glass | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 406 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 407 | /* Check searching for bootdevs using the hunters */ |
| 408 | static int bootdev_test_hunt_scan(struct unit_test_state *uts) |
| 409 | { |
| 410 | struct bootflow_iter iter; |
| 411 | struct bootstd_priv *std; |
| 412 | struct bootflow bflow; |
| 413 | |
| 414 | /* get access to the used hunters */ |
| 415 | ut_assertok(bootstd_get_priv(&std)); |
| 416 | |
| 417 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 418 | ut_assertok(bootflow_scan_first(&iter, BOOTFLOWF_SHOW | BOOTFLOWF_HUNT | |
| 419 | BOOTFLOWF_SKIP_GLOBAL, &bflow)); |
| 420 | ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | BOOTSTD_TEST(bootdev_test_hunt_scan, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 425 | |
Simon Glass | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 426 | /* Check that only bootable partitions are processed */ |
| 427 | static int bootdev_test_bootable(struct unit_test_state *uts) |
| 428 | { |
| 429 | struct bootflow_iter iter; |
| 430 | struct bootflow bflow; |
| 431 | struct udevice *blk; |
| 432 | |
| 433 | memset(&iter, '\0', sizeof(iter)); |
| 434 | memset(&bflow, '\0', sizeof(bflow)); |
| 435 | iter.part = 0; |
| 436 | ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk)); |
| 437 | iter.dev = blk; |
| 438 | ut_assertok(device_find_next_child(&iter.dev)); |
| 439 | uclass_first_device(UCLASS_BOOTMETH, &bflow.method); |
| 440 | |
| 441 | /* |
| 442 | * initially we don't have any knowledge of which partitions are |
| 443 | * bootable, but mmc1 has two partitions, with the first one being |
| 444 | * bootable |
| 445 | */ |
| 446 | iter.part = 2; |
| 447 | ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); |
| 448 | ut_asserteq(0, iter.first_bootable); |
| 449 | |
| 450 | /* scan with part == 0 to get the partition info */ |
| 451 | iter.part = 0; |
| 452 | ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); |
| 453 | ut_asserteq(1, iter.first_bootable); |
| 454 | |
| 455 | /* now it will refuse to use non-bootable partitions */ |
| 456 | iter.part = 2; |
| 457 | ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | 3e9f6be | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 462 | |
| 463 | /* Check hunting for bootdev of a particular priority */ |
| 464 | static int bootdev_test_hunt_prio(struct unit_test_state *uts) |
| 465 | { |
| 466 | test_set_skip_delays(true); |
| 467 | |
| 468 | console_record_reset_enable(); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 469 | ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false)); |
Simon Glass | 3e9f6be | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 470 | ut_assert_nextline("scanning bus for devices..."); |
| 471 | ut_assert_skip_to_line(" Type: Hard Disk"); |
| 472 | ut_assert_nextlinen(" Capacity:"); |
| 473 | ut_assert_console_end(); |
| 474 | |
| 475 | /* now try a different priority, verbosely */ |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 476 | ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true)); |
Simon Glass | 3e9f6be | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 477 | ut_assert_nextline("Hunting with: ide"); |
| 478 | ut_assert_nextline("Bus 0: not available "); |
| 479 | ut_assert_nextline("Hunting with: usb"); |
| 480 | ut_assert_nextline( |
| 481 | "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); |
| 482 | ut_assert_console_end(); |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | BOOTSTD_TEST(bootdev_test_hunt_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | de567b1 | 2023-01-17 10:48:09 -0700 | [diff] [blame] | 487 | |
| 488 | /* Check hunting for bootdevs with a particular label */ |
| 489 | static int bootdev_test_hunt_label(struct unit_test_state *uts) |
| 490 | { |
| 491 | struct udevice *dev, *old; |
| 492 | struct bootstd_priv *std; |
| 493 | int mflags; |
| 494 | |
| 495 | /* get access to the used hunters */ |
| 496 | ut_assertok(bootstd_get_priv(&std)); |
| 497 | |
| 498 | /* scan an unknown uclass */ |
| 499 | console_record_reset_enable(); |
| 500 | old = (void *)&mflags; /* arbitrary pointer to check against dev */ |
| 501 | dev = old; |
| 502 | mflags = 123; |
| 503 | ut_asserteq(-EINVAL, |
| 504 | bootdev_hunt_and_find_by_label("fred", &dev, &mflags)); |
| 505 | ut_assert_nextline("Unknown uclass 'fred' in label"); |
| 506 | ut_asserteq_ptr(old, dev); |
| 507 | ut_asserteq(123, mflags); |
| 508 | ut_assert_console_end(); |
| 509 | ut_asserteq(0, std->hunters_used); |
| 510 | |
| 511 | /* scan an invalid mmc controllers */ |
| 512 | ut_asserteq(-ENOENT, |
| 513 | bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags)); |
| 514 | ut_asserteq_ptr(old, dev); |
| 515 | ut_asserteq(123, mflags); |
| 516 | ut_assert_nextline("Unknown seq 4 for label 'mmc4'"); |
| 517 | ut_assert_console_end(); |
| 518 | |
| 519 | ut_assertok(bootstd_test_check_mmc_hunter(uts)); |
| 520 | |
| 521 | /* scan for a particular mmc controller */ |
| 522 | ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags)); |
| 523 | ut_assertnonnull(dev); |
| 524 | ut_asserteq_str("mmc1.bootdev", dev->name); |
| 525 | ut_asserteq(0, mflags); |
| 526 | ut_assert_console_end(); |
| 527 | |
| 528 | /* scan all of usb */ |
| 529 | test_set_skip_delays(true); |
| 530 | ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags)); |
| 531 | ut_assertnonnull(dev); |
| 532 | ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name); |
| 533 | ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags); |
| 534 | ut_assert_nextlinen("Bus usb@1: scanning bus usb@1"); |
| 535 | ut_assert_console_end(); |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | BOOTSTD_TEST(bootdev_test_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | ac06b26a | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 540 | |
| 541 | /* Check iterating to the next label in a list */ |
| 542 | static int bootdev_test_next_label(struct unit_test_state *uts) |
| 543 | { |
| 544 | const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL}; |
| 545 | struct bootflow_iter iter; |
| 546 | struct bootstd_priv *std; |
| 547 | struct bootflow bflow; |
| 548 | struct udevice *dev; |
| 549 | int mflags; |
| 550 | |
| 551 | test_set_eth_enable(false); |
| 552 | |
| 553 | /* get access to the used hunters */ |
| 554 | ut_assertok(bootstd_get_priv(&std)); |
| 555 | |
| 556 | memset(&iter, '\0', sizeof(iter)); |
| 557 | memset(&bflow, '\0', sizeof(bflow)); |
| 558 | iter.part = 0; |
| 559 | uclass_first_device(UCLASS_BOOTMETH, &bflow.method); |
| 560 | iter.cur_label = -1; |
| 561 | iter.labels = labels; |
| 562 | |
| 563 | dev = NULL; |
| 564 | mflags = 123; |
| 565 | ut_assertok(bootdev_next_label(&iter, &dev, &mflags)); |
| 566 | console_record_reset_enable(); |
| 567 | ut_assert_console_end(); |
| 568 | ut_assertnonnull(dev); |
| 569 | ut_asserteq_str("mmc0.bootdev", dev->name); |
| 570 | ut_asserteq(0, mflags); |
| 571 | |
| 572 | ut_assertok(bootstd_test_check_mmc_hunter(uts)); |
| 573 | |
| 574 | ut_assertok(bootdev_next_label(&iter, &dev, &mflags)); |
| 575 | ut_assert_nextline("scanning bus for devices..."); |
| 576 | ut_assert_skip_to_line( |
| 577 | " Capacity: 1.9 MB = 0.0 GB (4095 x 512)"); |
| 578 | ut_assert_console_end(); |
| 579 | ut_assertnonnull(dev); |
| 580 | ut_asserteq_str("scsi.id0lun0.bootdev", dev->name); |
| 581 | ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags); |
| 582 | |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 583 | /* SCSI is sixth in the list, so bit 5 */ |
| 584 | ut_asserteq(BIT(MMC_HUNTER) | BIT(5), std->hunters_used); |
Simon Glass | ac06b26a | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 585 | |
| 586 | ut_assertok(bootdev_next_label(&iter, &dev, &mflags)); |
| 587 | ut_assert_console_end(); |
| 588 | ut_assertnonnull(dev); |
| 589 | ut_asserteq_str("eth@10002000.bootdev", dev->name); |
| 590 | ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY, |
| 591 | mflags); |
| 592 | |
| 593 | /* dhcp: Ethernet is first so bit 0 */ |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 594 | ut_asserteq(BIT(MMC_HUNTER) | BIT(5) | BIT(0), std->hunters_used); |
Simon Glass | ac06b26a | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 595 | |
| 596 | ut_assertok(bootdev_next_label(&iter, &dev, &mflags)); |
| 597 | ut_assert_console_end(); |
| 598 | ut_assertnonnull(dev); |
| 599 | ut_asserteq_str("eth@10002000.bootdev", dev->name); |
| 600 | ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY, |
| 601 | mflags); |
| 602 | |
| 603 | /* pxe: Ethernet is first so bit 0 */ |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 604 | ut_asserteq(BIT(MMC_HUNTER) | BIT(5) | BIT(0), std->hunters_used); |
Simon Glass | ac06b26a | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 605 | |
| 606 | mflags = 123; |
| 607 | ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags)); |
| 608 | ut_asserteq(123, mflags); |
| 609 | ut_assert_console_end(); |
| 610 | |
| 611 | /* no change */ |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 612 | ut_asserteq(BIT(MMC_HUNTER) | BIT(5) | BIT(0), std->hunters_used); |
Simon Glass | ac06b26a | 2023-01-17 10:48:10 -0700 | [diff] [blame] | 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | BOOTSTD_TEST(bootdev_test_next_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 617 | UT_TESTF_ETH_BOOTDEV | UT_TESTF_SF_BOOTDEV); |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 618 | |
| 619 | |
| 620 | /* Check iterating to the next prioirty in a list */ |
| 621 | static int bootdev_test_next_prio(struct unit_test_state *uts) |
| 622 | { |
| 623 | struct bootflow_iter iter; |
| 624 | struct bootstd_priv *std; |
| 625 | struct bootflow bflow; |
| 626 | struct udevice *dev; |
| 627 | int ret; |
| 628 | |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame^] | 629 | test_set_eth_enable(false); |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 630 | test_set_skip_delays(true); |
| 631 | |
| 632 | /* get access to the used hunters */ |
| 633 | ut_assertok(bootstd_get_priv(&std)); |
| 634 | |
| 635 | memset(&iter, '\0', sizeof(iter)); |
| 636 | memset(&bflow, '\0', sizeof(bflow)); |
| 637 | iter.part = 0; |
| 638 | uclass_first_device(UCLASS_BOOTMETH, &bflow.method); |
| 639 | iter.cur_prio = 0; |
| 640 | iter.flags = BOOTFLOWF_SHOW; |
| 641 | |
| 642 | dev = NULL; |
| 643 | console_record_reset_enable(); |
| 644 | ut_assertok(bootdev_next_prio(&iter, &dev)); |
| 645 | ut_assertnonnull(dev); |
| 646 | ut_asserteq_str("mmc2.bootdev", dev->name); |
| 647 | |
| 648 | /* hunt flag not set, so this should not use any hunters */ |
| 649 | ut_asserteq(0, std->hunters_used); |
| 650 | ut_assert_console_end(); |
| 651 | |
| 652 | /* now try again with hunting enabled */ |
| 653 | iter.flags = BOOTFLOWF_SHOW | BOOTFLOWF_HUNT; |
| 654 | iter.cur_prio = 0; |
| 655 | iter.part = 0; |
| 656 | |
| 657 | ut_assertok(bootdev_next_prio(&iter, &dev)); |
| 658 | ut_asserteq_str("mmc2.bootdev", dev->name); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 659 | ut_assert_nextline("Hunting with: simple_bus"); |
| 660 | ut_assert_nextline("Found 2 extension board(s)."); |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 661 | ut_assert_nextline("Hunting with: mmc"); |
| 662 | ut_assert_console_end(); |
| 663 | |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 664 | ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used); |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 665 | |
| 666 | ut_assertok(bootdev_next_prio(&iter, &dev)); |
| 667 | ut_asserteq_str("mmc1.bootdev", dev->name); |
| 668 | |
| 669 | ut_assertok(bootdev_next_prio(&iter, &dev)); |
| 670 | ut_asserteq_str("mmc0.bootdev", dev->name); |
| 671 | ut_assert_console_end(); |
| 672 | |
| 673 | ut_assertok(bootdev_next_prio(&iter, &dev)); |
| 674 | ut_asserteq_str("spi.bin@0.bootdev", dev->name); |
| 675 | ut_assert_skip_to_line("Hunting with: spi_flash"); |
| 676 | |
| 677 | /* |
| 678 | * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it |
| 679 | * starts looking at the devices, so we se virtio as well |
| 680 | */ |
| 681 | ut_assert_nextline("Hunting with: virtio"); |
| 682 | ut_assert_nextlinen("SF: Detected m25p16"); |
| 683 | |
| 684 | ut_assertok(bootdev_next_prio(&iter, &dev)); |
| 685 | ut_asserteq_str("spi.bin@1.bootdev", dev->name); |
| 686 | ut_assert_nextlinen("SF: Detected m25p16"); |
| 687 | ut_assert_console_end(); |
| 688 | |
| 689 | /* keep going until there are no more bootdevs */ |
| 690 | do { |
| 691 | ret = bootdev_next_prio(&iter, &dev); |
| 692 | } while (!ret); |
| 693 | ut_asserteq(-ENODEV, ret); |
| 694 | ut_assertnull(dev); |
Simon Glass | e4cf106 | 2023-01-17 10:48:13 -0700 | [diff] [blame] | 695 | ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used); |
Simon Glass | 660a995 | 2023-01-17 10:48:11 -0700 | [diff] [blame] | 696 | |
| 697 | ut_assert_skip_to_line("Hunting with: ethernet"); |
| 698 | ut_assert_console_end(); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | BOOTSTD_TEST(bootdev_test_next_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 703 | UT_TESTF_SF_BOOTDEV); |