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; |
| 105 | |
| 106 | ut_assertok(bootdev_find_by_label("mmc2", &dev)); |
| 107 | ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev)); |
| 108 | media = dev_get_parent(dev); |
| 109 | ut_asserteq(UCLASS_MMC, device_get_uclass_id(media)); |
| 110 | ut_asserteq_str("mmc2", media->name); |
| 111 | |
| 112 | /* Check invalid uclass */ |
| 113 | ut_asserteq(-EINVAL, bootdev_find_by_label("fred0", &dev)); |
| 114 | |
| 115 | /* Check unknown sequence number */ |
| 116 | ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev)); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 121 | |
| 122 | /* Check bootdev ordering with the bootdev-order property */ |
| 123 | static int bootdev_test_order(struct unit_test_state *uts) |
| 124 | { |
| 125 | struct bootflow_iter iter; |
| 126 | struct bootflow bflow; |
| 127 | |
| 128 | /* |
| 129 | * First try the order set by the bootdev-order property |
| 130 | * Like all sandbox unit tests this relies on the devicetree setting up |
| 131 | * the required devices: |
| 132 | * |
| 133 | * mmc0 - nothing connected |
| 134 | * mmc1 - connected to mmc1.img file |
| 135 | * mmc2 - nothing connected |
| 136 | */ |
| 137 | ut_assertok(env_set("boot_targets", NULL)); |
| 138 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 139 | ut_asserteq(2, iter.num_devs); |
| 140 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 141 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name); |
| 142 | bootflow_iter_uninit(&iter); |
| 143 | |
| 144 | /* Use the environment variable to override it */ |
| 145 | ut_assertok(env_set("boot_targets", "mmc1 mmc2")); |
| 146 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 147 | ut_asserteq(2, iter.num_devs); |
| 148 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[0]->name); |
| 149 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name); |
| 150 | bootflow_iter_uninit(&iter); |
| 151 | |
| 152 | /* |
| 153 | * Now drop both orderings, to check the default (prioriy/sequence) |
| 154 | * ordering |
| 155 | */ |
| 156 | ut_assertok(env_set("boot_targets", NULL)); |
| 157 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 158 | |
| 159 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 160 | ut_asserteq(3, iter.num_devs); |
| 161 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 162 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name); |
| 163 | ut_asserteq_str("mmc0.bootdev", iter.dev_order[2]->name); |
| 164 | |
| 165 | /* |
| 166 | * Check that adding aliases for the bootdevs works. We just fake it by |
| 167 | * setting the sequence numbers directly. |
| 168 | */ |
| 169 | iter.dev_order[0]->seq_ = 0; |
| 170 | iter.dev_order[1]->seq_ = 3; |
| 171 | iter.dev_order[2]->seq_ = 2; |
| 172 | bootflow_iter_uninit(&iter); |
| 173 | |
| 174 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 175 | ut_asserteq(3, iter.num_devs); |
| 176 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 177 | ut_asserteq_str("mmc0.bootdev", iter.dev_order[1]->name); |
| 178 | ut_asserteq_str("mmc1.bootdev", iter.dev_order[2]->name); |
| 179 | bootflow_iter_uninit(&iter); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 184 | |
| 185 | /* Check bootdev ordering with the uclass priority */ |
| 186 | static int bootdev_test_prio(struct unit_test_state *uts) |
| 187 | { |
| 188 | struct bootdev_uc_plat *ucp; |
| 189 | struct bootflow_iter iter; |
| 190 | struct bootflow bflow; |
| 191 | struct udevice *blk; |
| 192 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 193 | test_set_skip_delays(true); |
| 194 | |
Simon Glass | b255efc | 2022-04-24 23:31:24 -0600 | [diff] [blame] | 195 | /* Start up USB which gives us three additional bootdevs */ |
| 196 | usb_started = false; |
| 197 | ut_assertok(run_command("usb start", 0)); |
| 198 | |
| 199 | ut_assertok(bootstd_test_drop_bootdev_order(uts)); |
| 200 | |
| 201 | /* 3 MMC and 3 USB bootdevs: MMC should come before USB */ |
| 202 | console_record_reset_enable(); |
| 203 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 204 | ut_asserteq(6, iter.num_devs); |
| 205 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name); |
| 206 | ut_asserteq_str("usb_mass_storage.lun0.bootdev", |
| 207 | iter.dev_order[3]->name); |
| 208 | |
| 209 | ut_assertok(bootdev_get_sibling_blk(iter.dev_order[3], &blk)); |
| 210 | ut_asserteq_str("usb_mass_storage.lun0", blk->name); |
| 211 | |
| 212 | /* adjust the priority of the first USB bootdev to the highest */ |
| 213 | ucp = dev_get_uclass_plat(iter.dev_order[3]); |
| 214 | ucp->prio = 1; |
| 215 | |
| 216 | bootflow_iter_uninit(&iter); |
| 217 | ut_assertok(bootflow_scan_first(&iter, 0, &bflow)); |
| 218 | ut_asserteq(6, iter.num_devs); |
| 219 | ut_asserteq_str("usb_mass_storage.lun0.bootdev", |
| 220 | iter.dev_order[0]->name); |
| 221 | ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 226 | |
| 227 | /* Check listing hunters */ |
| 228 | static int bootdev_test_hunter(struct unit_test_state *uts) |
| 229 | { |
| 230 | struct bootstd_priv *std; |
| 231 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 232 | test_set_skip_delays(true); |
| 233 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 234 | /* get access to the used hunters */ |
| 235 | ut_assertok(bootstd_get_priv(&std)); |
| 236 | |
| 237 | console_record_reset_enable(); |
| 238 | bootdev_list_hunters(std); |
| 239 | ut_assert_nextline("Prio Used Uclass Hunter"); |
| 240 | ut_assert_nextlinen("----"); |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 241 | ut_assert_nextline(" 50 ethernet eth_bootdev"); |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame^] | 242 | ut_assert_nextline(" 40 ide ide_bootdev"); |
Simon Glass | df71f26 | 2023-01-17 10:47:38 -0700 | [diff] [blame] | 243 | ut_assert_nextline(" 10 mmc mmc_bootdev"); |
Simon Glass | 9985e9c | 2023-01-17 10:47:45 -0700 | [diff] [blame] | 244 | ut_assert_nextline(" 30 scsi scsi_bootdev"); |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 245 | ut_assert_nextline(" 40 usb usb_bootdev"); |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame^] | 246 | ut_assert_nextline("(total hunters: 5)"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 247 | ut_assert_console_end(); |
| 248 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 249 | ut_assertok(bootdev_hunt("usb1", false)); |
| 250 | ut_assert_nextline( |
| 251 | "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] | 252 | ut_assert_console_end(); |
| 253 | |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame^] | 254 | /* USB is fifth in the list, so bit 4 */ |
| 255 | ut_asserteq(BIT(4), std->hunters_used); |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 256 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 260 | |
| 261 | /* Check 'bootdev hunt' command */ |
| 262 | static int bootdev_test_cmd_hunt(struct unit_test_state *uts) |
| 263 | { |
| 264 | struct bootstd_priv *std; |
| 265 | |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 266 | test_set_skip_delays(true); |
| 267 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 268 | /* get access to the used hunters */ |
| 269 | ut_assertok(bootstd_get_priv(&std)); |
| 270 | |
| 271 | console_record_reset_enable(); |
| 272 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 273 | ut_assert_nextline("Prio Used Uclass Hunter"); |
| 274 | ut_assert_nextlinen("----"); |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame^] | 275 | ut_assert_skip_to_line("(total hunters: 5)"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 276 | ut_assert_console_end(); |
| 277 | |
| 278 | /* Scan all hunters */ |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 279 | sandbox_set_eth_enable(false); |
| 280 | |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 281 | ut_assertok(run_command("bootdev hunt", 0)); |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 282 | ut_assert_nextline("Hunting with: ethernet"); |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame^] | 283 | ut_assert_nextline("Hunting with: ide"); |
| 284 | ut_assert_nextline("Bus 0: not available "); |
Simon Glass | df71f26 | 2023-01-17 10:47:38 -0700 | [diff] [blame] | 285 | ut_assert_nextline("Hunting with: mmc"); |
Simon Glass | 9985e9c | 2023-01-17 10:47:45 -0700 | [diff] [blame] | 286 | ut_assert_nextline("Hunting with: scsi"); |
| 287 | ut_assert_nextline("scanning bus for devices..."); |
| 288 | ut_assert_skip_to_line("Hunting with: usb"); |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 289 | ut_assert_nextline( |
| 290 | "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 291 | ut_assert_console_end(); |
| 292 | |
| 293 | /* List available hunters */ |
| 294 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 295 | ut_assert_nextlinen("Prio"); |
| 296 | ut_assert_nextlinen("----"); |
Simon Glass | 5ea0a8d | 2023-01-17 10:47:40 -0700 | [diff] [blame] | 297 | ut_assert_nextline(" 50 * ethernet eth_bootdev"); |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame^] | 298 | ut_assert_nextline(" 40 * ide ide_bootdev"); |
Simon Glass | df71f26 | 2023-01-17 10:47:38 -0700 | [diff] [blame] | 299 | ut_assert_nextline(" 10 * mmc mmc_bootdev"); |
Simon Glass | 9985e9c | 2023-01-17 10:47:45 -0700 | [diff] [blame] | 300 | ut_assert_nextline(" 30 * scsi scsi_bootdev"); |
Simon Glass | f3cb89f | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 301 | ut_assert_nextline(" 40 * usb usb_bootdev"); |
Simon Glass | df71f26 | 2023-01-17 10:47:38 -0700 | [diff] [blame] | 302 | |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame^] | 303 | ut_assert_nextline("(total hunters: 5)"); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 304 | ut_assert_console_end(); |
| 305 | |
Simon Glass | be7c666 | 2023-01-17 10:47:46 -0700 | [diff] [blame^] | 306 | ut_asserteq(GENMASK(4, 0), std->hunters_used); |
Simon Glass | 5acb97a | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 311 | UT_TESTF_ETH_BOOTDEV); |
Simon Glass | 64cbc5c | 2023-01-17 10:47:42 -0700 | [diff] [blame] | 312 | |
| 313 | /* Check that only bootable partitions are processed */ |
| 314 | static int bootdev_test_bootable(struct unit_test_state *uts) |
| 315 | { |
| 316 | struct bootflow_iter iter; |
| 317 | struct bootflow bflow; |
| 318 | struct udevice *blk; |
| 319 | |
| 320 | memset(&iter, '\0', sizeof(iter)); |
| 321 | memset(&bflow, '\0', sizeof(bflow)); |
| 322 | iter.part = 0; |
| 323 | ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk)); |
| 324 | iter.dev = blk; |
| 325 | ut_assertok(device_find_next_child(&iter.dev)); |
| 326 | uclass_first_device(UCLASS_BOOTMETH, &bflow.method); |
| 327 | |
| 328 | /* |
| 329 | * initially we don't have any knowledge of which partitions are |
| 330 | * bootable, but mmc1 has two partitions, with the first one being |
| 331 | * bootable |
| 332 | */ |
| 333 | iter.part = 2; |
| 334 | ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); |
| 335 | ut_asserteq(0, iter.first_bootable); |
| 336 | |
| 337 | /* scan with part == 0 to get the partition info */ |
| 338 | iter.part = 0; |
| 339 | ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); |
| 340 | ut_asserteq(1, iter.first_bootable); |
| 341 | |
| 342 | /* now it will refuse to use non-bootable partitions */ |
| 343 | iter.part = 2; |
| 344 | ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |