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 | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 189 | /* Check bootdev ordering with the bootdev-order property */ |
| 190 | static int bootdev_test_order(struct unit_test_state *uts) |
| 191 | { |
| 192 | struct bootflow_iter iter; |
| 193 | struct bootflow bflow; |
| 194 | |
| 195 | /* |
| 196 | * First try the order set by the bootdev-order property |
| 197 | * Like all sandbox unit tests this relies on the devicetree setting up |
| 198 | * the required devices: |
| 199 | * |
| 200 | * mmc0 - nothing connected |
| 201 | * mmc1 - connected to mmc1.img file |
| 202 | * mmc2 - nothing connected |
| 203 | */ |
| 204 | ut_assertok(env_set("boot_targets", NULL)); |
| 205 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 206 | ut_asserteq(2, iter.num_devs); |
| 207 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 208 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name); |
| 209 | bootflow_iter_uninit(&iter); |
| 210 | |
| 211 | /* Use the environment variable to override it */ |
| 212 | ut_assertok(env_set("boot_targets", "mmc1 mmc2")); |
| 213 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 214 | ut_asserteq(2, iter.num_devs); |
| 215 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[0]->name); |
| 216 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name); |
| 217 | bootflow_iter_uninit(&iter); |
| 218 | |
| 219 | /* |
| 220 | * Now drop both orderings, to check the default (prioriy/sequence) |
| 221 | * ordering |
| 222 | */ |
| 223 | ut_assertok(env_set("boot_targets", NULL)); |
| 224 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 225 | |
| 226 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 227 | ut_asserteq(3, iter.num_devs); |
| 228 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 229 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name); |
| 230 | ut_asserteq_str("mmc0.bootdev", iter.dev_order[2]->name); |
| 231 | |
| 232 | /* |
| 233 | * Check that adding aliases for the bootdevs works. We just fake it by |
| 234 | * setting the sequence numbers directly. |
| 235 | */ |
| 236 | iter.dev_order[0]->seq_ = 0; |
| 237 | iter.dev_order[1]->seq_ = 3; |
| 238 | iter.dev_order[2]->seq_ = 2; |
| 239 | bootflow_iter_uninit(&iter); |
| 240 | |
| 241 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 242 | ut_asserteq(3, iter.num_devs); |
| 243 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 244 | ut_asserteq_str("mmc0.bootdev", iter.dev_order[1]->name); |
| 245 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[2]->name); |
| 246 | bootflow_iter_uninit(&iter); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 251 | |
| 252 | /* Check bootdev ordering with the uclass priority */ |
| 253 | static int bootdev_test_prio(struct unit_test_state *uts) |
| 254 | { |
| 255 | struct bootdev_uc_plat *ucp; |
| 256 | struct bootflow_iter iter; |
| 257 | struct bootflow bflow; |
| 258 | struct udevice *blk; |
| 259 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 260 | test_set_skip_delays(true); |
| 261 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 262 | /* Start up USB which gives us three additional bootdevs */ |
| 263 | usb_started = false; |
| 264 | ut_assertok(run_command("usb start", 0)); |
| 265 | |
| 266 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 267 | |
| 268 | /* 3 MMC and 3 USB bootdevs: MMC should come before USB */ |
| 269 | console_record_reset_enable(); |
| 270 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 271 | ut_asserteq(6, iter.num_devs); |
| 272 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 273 | ut_asserteq_str("usb_mass_storage.lun0.bootdev", |
| 274 | iter.dev_order[3]->name); |
| 275 | |
| 276 | ut_assertok(bootdev_get_sibling_blk(iter.dev_order[3], &blk)); |
| 277 | ut_asserteq_str("usb_mass_storage.lun0", blk->name); |
| 278 | |
| 279 | /* adjust the priority of the first USB bootdev to the highest */ |
| 280 | ucp = dev_get_uclass_plat(iter.dev_order[3]); |
| 281 | ucp->prio = 1; |
| 282 | |
| 283 | bootflow_iter_uninit(&iter); |
| 284 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 285 | ut_asserteq(6, iter.num_devs); |
| 286 | ut_asserteq_str("usb_mass_storage.lun0.bootdev", |
| 287 | iter.dev_order[0]->name); |
| 288 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 293 | |
| 294 | /* Check listing hunters */ |
| 295 | static int bootdev_test_hunter(struct unit_test_state *uts) |
| 296 | { |
| 297 | struct bootstd_priv *std; |
| 298 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 299 | test_set_skip_delays(true); |
| 300 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 301 | /* get access to the used hunters */ |
| 302 | ut_assertok(bootstd_get_priv(&std)); |
| 303 | |
| 304 | console_record_reset_enable(); |
| 305 | bootdev_list_hunters(std); |
| 306 | ut_assert_nextline("Prio Used Uclass Hunter"); |
| 307 | ut_assert_nextlinen("----"); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 308 | ut_assert_nextline(" 6 ethernet eth_bootdev"); |
| 309 | ut_assert_nextline(" 5 ide ide_bootdev"); |
| 310 | ut_assert_nextline(" 2 mmc mmc_bootdev"); |
| 311 | ut_assert_nextline(" 4 nvme nvme_bootdev"); |
| 312 | ut_assert_nextline(" 4 scsi scsi_bootdev"); |
| 313 | ut_assert_nextline(" 4 spi_flash sf_bootdev"); |
| 314 | ut_assert_nextline(" 5 usb usb_bootdev"); |
| 315 | ut_assert_nextline(" 4 virtio virtio_bootdev"); |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 316 | ut_assert_nextline("(total hunters: 8)"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 317 | ut_assert_console_end(); |
| 318 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 319 | ut_assertok(bootdev_hunt("usb1", false)); |
| 320 | ut_assert_nextline( |
| 321 | "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] | 322 | ut_assert_console_end(); |
| 323 | |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 324 | /* USB is fifth in the list, so bit 6 */ |
| 325 | ut_asserteq(BIT(6), std->hunters_used); |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 326 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 330 | |
| 331 | /* Check 'bootdev hunt' command */ |
| 332 | static int bootdev_test_cmd_hunt(struct unit_test_state *uts) |
| 333 | { |
| 334 | struct bootstd_priv *std; |
| 335 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 336 | test_set_skip_delays(true); |
| 337 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 338 | /* get access to the used hunters */ |
| 339 | ut_assertok(bootstd_get_priv(&std)); |
| 340 | |
| 341 | console_record_reset_enable(); |
| 342 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 343 | ut_assert_nextline("Prio Used Uclass Hunter"); |
| 344 | ut_assert_nextlinen("----"); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 345 | ut_assert_nextline(" 6 ethernet eth_bootdev"); |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 346 | ut_assert_skip_to_line("(total hunters: 8)"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 347 | ut_assert_console_end(); |
| 348 | |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 349 | /* Use the MMC hunter and see that it updates */ |
| 350 | ut_assertok(run_command("bootdev hunt mmc", 0)); |
| 351 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 352 | ut_assert_skip_to_line(" 5 ide ide_bootdev"); |
| 353 | ut_assert_nextline(" 2 * mmc mmc_bootdev"); |
| 354 | ut_assert_skip_to_line("(total hunters: 8)"); |
| 355 | ut_assert_console_end(); |
| 356 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 357 | /* Scan all hunters */ |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 358 | sandbox_set_eth_enable(false); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 359 | test_set_skip_delays(true); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 360 | ut_assertok(run_command("bootdev hunt", 0)); |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 361 | ut_assert_nextline("Hunting with: ethernet"); |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame] | 362 | ut_assert_nextline("Hunting with: ide"); |
| 363 | ut_assert_nextline("Bus 0: not available "); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 364 | |
| 365 | /* mmc hunter has already been used so should not run again */ |
| 366 | |
Simon Glass | 0838287 | 2023-01-17 10:47:47 -0700 | [diff] [blame] | 367 | ut_assert_nextline("Hunting with: nvme"); |
Simon Glass | 9985e9c | 2023-01-17 10:47:45 -0700 | [diff] [blame] | 368 | ut_assert_nextline("Hunting with: scsi"); |
| 369 | ut_assert_nextline("scanning bus for devices..."); |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 370 | ut_assert_skip_to_line("Hunting with: spi_flash"); |
| 371 | ut_assert_nextline("Hunting with: usb"); |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 372 | ut_assert_nextline( |
| 373 | "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] | 374 | ut_assert_nextline("Hunting with: virtio"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 375 | ut_assert_console_end(); |
| 376 | |
| 377 | /* List available hunters */ |
| 378 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 379 | ut_assert_nextlinen("Prio"); |
| 380 | ut_assert_nextlinen("----"); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 381 | ut_assert_nextline(" 6 * ethernet eth_bootdev"); |
| 382 | ut_assert_nextline(" 5 * ide ide_bootdev"); |
| 383 | ut_assert_nextline(" 2 * mmc mmc_bootdev"); |
| 384 | ut_assert_nextline(" 4 * nvme nvme_bootdev"); |
| 385 | ut_assert_nextline(" 4 * scsi scsi_bootdev"); |
| 386 | ut_assert_nextline(" 4 * spi_flash sf_bootdev"); |
| 387 | ut_assert_nextline(" 5 * usb usb_bootdev"); |
| 388 | ut_assert_nextline(" 4 * virtio virtio_bootdev"); |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 389 | ut_assert_nextline("(total hunters: 8)"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 390 | ut_assert_console_end(); |
| 391 | |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 392 | ut_asserteq(GENMASK(7, 0), std->hunters_used); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 397 | UT_TESTF_ETH_BOOTDEV); |
Simon Glass | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 398 | |
| 399 | /* Check that only bootable partitions are processed */ |
| 400 | static int bootdev_test_bootable(struct unit_test_state *uts) |
| 401 | { |
| 402 | struct bootflow_iter iter; |
| 403 | struct bootflow bflow; |
| 404 | struct udevice *blk; |
| 405 | |
| 406 | memset(&iter, '\0', sizeof(iter)); |
| 407 | memset(&bflow, '\0', sizeof(bflow)); |
| 408 | iter.part = 0; |
| 409 | ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk)); |
| 410 | iter.dev = blk; |
| 411 | ut_assertok(device_find_next_child(&iter.dev)); |
| 412 | uclass_first_device(UCLASS_BOOTMETH, &bflow.method); |
| 413 | |
| 414 | /* |
| 415 | * initially we don't have any knowledge of which partitions are |
| 416 | * bootable, but mmc1 has two partitions, with the first one being |
| 417 | * bootable |
| 418 | */ |
| 419 | iter.part = 2; |
| 420 | ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); |
| 421 | ut_asserteq(0, iter.first_bootable); |
| 422 | |
| 423 | /* scan with part == 0 to get the partition info */ |
| 424 | iter.part = 0; |
| 425 | ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); |
| 426 | ut_asserteq(1, iter.first_bootable); |
| 427 | |
| 428 | /* now it will refuse to use non-bootable partitions */ |
| 429 | iter.part = 2; |
| 430 | ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | 3e9f6be | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 435 | |
| 436 | /* Check hunting for bootdev of a particular priority */ |
| 437 | static int bootdev_test_hunt_prio(struct unit_test_state *uts) |
| 438 | { |
| 439 | test_set_skip_delays(true); |
| 440 | |
| 441 | console_record_reset_enable(); |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 442 | ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false)); |
Simon Glass | 3e9f6be | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 443 | ut_assert_nextline("scanning bus for devices..."); |
| 444 | ut_assert_skip_to_line(" Type: Hard Disk"); |
| 445 | ut_assert_nextlinen(" Capacity:"); |
| 446 | ut_assert_console_end(); |
| 447 | |
| 448 | /* now try a different priority, verbosely */ |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 449 | ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true)); |
Simon Glass | 3e9f6be | 2023-01-17 10:48:07 -0700 | [diff] [blame] | 450 | ut_assert_nextline("Hunting with: ide"); |
| 451 | ut_assert_nextline("Bus 0: not available "); |
| 452 | ut_assert_nextline("Hunting with: usb"); |
| 453 | ut_assert_nextline( |
| 454 | "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); |
| 455 | ut_assert_console_end(); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | 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^] | 460 | |
| 461 | /* Check hunting for bootdevs with a particular label */ |
| 462 | static int bootdev_test_hunt_label(struct unit_test_state *uts) |
| 463 | { |
| 464 | struct udevice *dev, *old; |
| 465 | struct bootstd_priv *std; |
| 466 | int mflags; |
| 467 | |
| 468 | /* get access to the used hunters */ |
| 469 | ut_assertok(bootstd_get_priv(&std)); |
| 470 | |
| 471 | /* scan an unknown uclass */ |
| 472 | console_record_reset_enable(); |
| 473 | old = (void *)&mflags; /* arbitrary pointer to check against dev */ |
| 474 | dev = old; |
| 475 | mflags = 123; |
| 476 | ut_asserteq(-EINVAL, |
| 477 | bootdev_hunt_and_find_by_label("fred", &dev, &mflags)); |
| 478 | ut_assert_nextline("Unknown uclass 'fred' in label"); |
| 479 | ut_asserteq_ptr(old, dev); |
| 480 | ut_asserteq(123, mflags); |
| 481 | ut_assert_console_end(); |
| 482 | ut_asserteq(0, std->hunters_used); |
| 483 | |
| 484 | /* scan an invalid mmc controllers */ |
| 485 | ut_asserteq(-ENOENT, |
| 486 | bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags)); |
| 487 | ut_asserteq_ptr(old, dev); |
| 488 | ut_asserteq(123, mflags); |
| 489 | ut_assert_nextline("Unknown seq 4 for label 'mmc4'"); |
| 490 | ut_assert_console_end(); |
| 491 | |
| 492 | ut_assertok(bootstd_test_check_mmc_hunter(uts)); |
| 493 | |
| 494 | /* scan for a particular mmc controller */ |
| 495 | ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags)); |
| 496 | ut_assertnonnull(dev); |
| 497 | ut_asserteq_str("mmc1.bootdev", dev->name); |
| 498 | ut_asserteq(0, mflags); |
| 499 | ut_assert_console_end(); |
| 500 | |
| 501 | /* scan all of usb */ |
| 502 | test_set_skip_delays(true); |
| 503 | ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags)); |
| 504 | ut_assertnonnull(dev); |
| 505 | ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name); |
| 506 | ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags); |
| 507 | ut_assert_nextlinen("Bus usb@1: scanning bus usb@1"); |
| 508 | ut_assert_console_end(); |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | BOOTSTD_TEST(bootdev_test_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |