Simon Glass | fb1451b | 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 | 04fb2b6 | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 193 | test_set_skip_delays(true); |
| 194 | |
Simon Glass | fb1451b | 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 | bd90b09 | 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 | 04fb2b6 | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 232 | test_set_skip_delays(true); |
| 233 | |
Simon Glass | bd90b09 | 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 | 4146c82 | 2023-01-17 10:47:40 -0700 | [diff] [blame^] | 241 | ut_assert_nextline(" 50 ethernet eth_bootdev"); |
Simon Glass | 843160f | 2023-01-17 10:47:38 -0700 | [diff] [blame] | 242 | ut_assert_nextline(" 10 mmc mmc_bootdev"); |
Simon Glass | 04fb2b6 | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 243 | ut_assert_nextline(" 40 usb usb_bootdev"); |
Simon Glass | 4146c82 | 2023-01-17 10:47:40 -0700 | [diff] [blame^] | 244 | ut_assert_nextline("(total hunters: 3)"); |
Simon Glass | bd90b09 | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 245 | ut_assert_console_end(); |
| 246 | |
Simon Glass | 04fb2b6 | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 247 | ut_assertok(bootdev_hunt("usb1", false)); |
| 248 | ut_assert_nextline( |
| 249 | "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); |
Simon Glass | c7b63d5 | 2023-01-17 10:47:34 -0700 | [diff] [blame] | 250 | ut_assert_console_end(); |
| 251 | |
Simon Glass | 4146c82 | 2023-01-17 10:47:40 -0700 | [diff] [blame^] | 252 | /* USB is third in the list, so bit 2 */ |
| 253 | ut_asserteq(BIT(2), std->hunters_used); |
Simon Glass | 04fb2b6 | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 254 | |
Simon Glass | bd90b09 | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 258 | |
| 259 | /* Check 'bootdev hunt' command */ |
| 260 | static int bootdev_test_cmd_hunt(struct unit_test_state *uts) |
| 261 | { |
| 262 | struct bootstd_priv *std; |
| 263 | |
Simon Glass | 04fb2b6 | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 264 | test_set_skip_delays(true); |
| 265 | |
Simon Glass | bd90b09 | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 266 | /* get access to the used hunters */ |
| 267 | ut_assertok(bootstd_get_priv(&std)); |
| 268 | |
| 269 | console_record_reset_enable(); |
| 270 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 271 | ut_assert_nextline("Prio Used Uclass Hunter"); |
| 272 | ut_assert_nextlinen("----"); |
Simon Glass | 4146c82 | 2023-01-17 10:47:40 -0700 | [diff] [blame^] | 273 | ut_assert_skip_to_line("(total hunters: 3)"); |
Simon Glass | bd90b09 | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 274 | ut_assert_console_end(); |
| 275 | |
| 276 | /* Scan all hunters */ |
Simon Glass | 4146c82 | 2023-01-17 10:47:40 -0700 | [diff] [blame^] | 277 | sandbox_set_eth_enable(false); |
| 278 | |
Simon Glass | bd90b09 | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 279 | ut_assertok(run_command("bootdev hunt", 0)); |
Simon Glass | 4146c82 | 2023-01-17 10:47:40 -0700 | [diff] [blame^] | 280 | ut_assert_nextline("Hunting with: ethernet"); |
Simon Glass | 843160f | 2023-01-17 10:47:38 -0700 | [diff] [blame] | 281 | ut_assert_nextline("Hunting with: mmc"); |
Simon Glass | 04fb2b6 | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 282 | ut_assert_nextline("Hunting with: usb"); |
| 283 | ut_assert_nextline( |
| 284 | "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); |
Simon Glass | bd90b09 | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 285 | ut_assert_console_end(); |
| 286 | |
| 287 | /* List available hunters */ |
| 288 | ut_assertok(run_command("bootdev hunt -l", 0)); |
| 289 | ut_assert_nextlinen("Prio"); |
| 290 | ut_assert_nextlinen("----"); |
Simon Glass | 4146c82 | 2023-01-17 10:47:40 -0700 | [diff] [blame^] | 291 | ut_assert_nextline(" 50 * ethernet eth_bootdev"); |
Simon Glass | 843160f | 2023-01-17 10:47:38 -0700 | [diff] [blame] | 292 | ut_assert_nextline(" 10 * mmc mmc_bootdev"); |
Simon Glass | 04fb2b6 | 2023-01-17 10:47:37 -0700 | [diff] [blame] | 293 | ut_assert_nextline(" 40 * usb usb_bootdev"); |
Simon Glass | 843160f | 2023-01-17 10:47:38 -0700 | [diff] [blame] | 294 | |
Simon Glass | 4146c82 | 2023-01-17 10:47:40 -0700 | [diff] [blame^] | 295 | ut_assert_nextline("(total hunters: 3)"); |
Simon Glass | bd90b09 | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 296 | ut_assert_console_end(); |
| 297 | |
Simon Glass | 4146c82 | 2023-01-17 10:47:40 -0700 | [diff] [blame^] | 298 | ut_asserteq(GENMASK(2, 0), std->hunters_used); |
Simon Glass | bd90b09 | 2023-01-17 10:47:33 -0700 | [diff] [blame] | 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT | |
| 303 | UT_TESTF_ETH_BOOTDEV); |