blob: 1bf5929c39683dcc90668dff85b49ab100bb4b67 [file] [log] [blame]
Simon Glassb255efc2022-04-24 23:31:24 -06001// 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
Simon Glassb255efc2022-04-24 23:31:24 -06009#include <bootstd.h>
10#include <dm.h>
11#include <bootdev.h>
12#include <bootflow.h>
13#include <mapmem.h>
14#include <os.h>
15#include <test/suites.h>
16#include <test/ut.h>
17#include "bootstd_common.h"
18
19/* Allow reseting the USB-started flag */
Linus Walleije3045d12023-01-23 23:07:21 +010020#if defined(CONFIG_USB_HOST) || defined(CONFIG_USB_GADGET)
Simon Glass2510aab2023-05-05 20:03:03 -060021extern bool usb_started;
Linus Walleije3045d12023-01-23 23:07:21 +010022#else
Simon Glass2510aab2023-05-05 20:03:03 -060023#include <usb.h>
Linus Walleije3045d12023-01-23 23:07:21 +010024#endif
Simon Glassb255efc2022-04-24 23:31:24 -060025
26/* Check 'bootdev list' command */
27static int bootdev_test_cmd_list(struct unit_test_state *uts)
28{
29 int probed;
30
31 console_record_reset_enable();
32 for (probed = 0; probed < 2; probed++) {
33 int probe_ch = probed ? '+' : ' ';
34
35 ut_assertok(run_command(probed ? "bootdev list -p" :
36 "bootdev list", 0));
37 ut_assert_nextline("Seq Probed Status Uclass Name");
38 ut_assert_nextlinen("---");
39 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 0, probe_ch, "OK",
40 "mmc", "mmc2.bootdev");
41 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 1, probe_ch, "OK",
42 "mmc", "mmc1.bootdev");
43 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 2, probe_ch, "OK",
44 "mmc", "mmc0.bootdev");
45 ut_assert_nextlinen("---");
46 ut_assert_nextline("(3 bootdevs)");
47 ut_assert_console_end();
48 }
49
50 return 0;
51}
52BOOTSTD_TEST(bootdev_test_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
53
54/* Check 'bootdev select' and 'info' commands */
55static int bootdev_test_cmd_select(struct unit_test_state *uts)
56{
57 struct bootstd_priv *std;
58
59 /* get access to the CLI's cur_bootdev */
60 ut_assertok(bootstd_get_priv(&std));
61
62 console_record_reset_enable();
63 ut_asserteq(1, run_command("bootdev info", 0));
64 ut_assert_nextlinen("Please use");
65 ut_assert_console_end();
66
67 /* select by sequence */
68 ut_assertok(run_command("bootdev select 0", 0));
69 ut_assert_console_end();
70
71 ut_assertok(run_command("bootdev info", 0));
72 ut_assert_nextline("Name: mmc2.bootdev");
73 ut_assert_nextline("Sequence: 0");
74 ut_assert_nextline("Status: Probed");
75 ut_assert_nextline("Uclass: mmc");
76 ut_assert_nextline("Bootflows: 0 (0 valid)");
77 ut_assert_console_end();
78
79 /* select by bootdev name */
80 ut_assertok(run_command("bootdev select mmc1.bootdev", 0));
81 ut_assert_console_end();
82 ut_assertnonnull(std->cur_bootdev);
83 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
84
85 /* select by bootdev label*/
86 ut_assertok(run_command("bootdev select mmc1", 0));
87 ut_assert_console_end();
88 ut_assertnonnull(std->cur_bootdev);
89 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
90
91 /* deselect */
92 ut_assertok(run_command("bootdev select", 0));
93 ut_assert_console_end();
94 ut_assertnull(std->cur_bootdev);
95
96 ut_asserteq(1, run_command("bootdev info", 0));
97 ut_assert_nextlinen("Please use");
98 ut_assert_console_end();
99
100 return 0;
101}
102BOOTSTD_TEST(bootdev_test_cmd_select, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
103
104/* Check bootdev labels */
105static int bootdev_test_labels(struct unit_test_state *uts)
106{
107 struct udevice *dev, *media;
Simon Glasse22fe922023-01-17 10:48:05 -0700108 int mflags = 0;
Simon Glassb255efc2022-04-24 23:31:24 -0600109
Simon Glasse22fe922023-01-17 10:48:05 -0700110 ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600111 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
Simon Glasse22fe922023-01-17 10:48:05 -0700112 ut_asserteq(0, mflags);
Simon Glassb255efc2022-04-24 23:31:24 -0600113 media = dev_get_parent(dev);
114 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
115 ut_asserteq_str("mmc2", media->name);
116
Simon Glasse22fe922023-01-17 10:48:05 -0700117 /* Check method flags */
118 ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700119 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
120 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700121 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700122 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
123 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700124
Simon Glassb255efc2022-04-24 23:31:24 -0600125 /* Check invalid uclass */
Simon Glass4108c6b2023-04-24 13:49:47 +1200126 ut_asserteq(-EPFNOSUPPORT,
127 bootdev_find_by_label("fred0", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600128
129 /* Check unknown sequence number */
Simon Glasse22fe922023-01-17 10:48:05 -0700130 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600131
132 return 0;
133}
Simon Glasse22fe922023-01-17 10:48:05 -0700134BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
135 UT_TESTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600136
Simon Glassde567b12023-01-17 10:48:09 -0700137/* Check bootdev_find_by_any() */
138static int bootdev_test_any(struct unit_test_state *uts)
139{
140 struct udevice *dev, *media;
141 int mflags;
142
143 /*
144 * with ethernet enabled we have 8 devices ahead of the mmc ones:
145 *
146 * ut_assertok(run_command("bootdev list", 0));
147 * Seq Probed Status Uclass Name
148 * --- ------ ------ -------- ------------------
149 * 0 [ + ] OK ethernet eth@10002000.bootdev
150 * 1 [ ] OK ethernet eth@10003000.bootdev
151 * 2 [ ] OK ethernet sbe5.bootdev
152 * 3 [ ] OK ethernet eth@10004000.bootdev
153 * 4 [ ] OK ethernet phy-test-eth.bootdev
154 * 5 [ ] OK ethernet dsa-test-eth.bootdev
155 * 6 [ ] OK ethernet dsa-test@0.bootdev
156 * 7 [ ] OK ethernet dsa-test@1.bootdev
157 * 8 [ ] OK mmc mmc2.bootdev
158 * 9 [ + ] OK mmc mmc1.bootdev
159 * a [ ] OK mmc mmc0.bootdev
160 */
161 console_record_reset_enable();
162 ut_assertok(bootdev_find_by_any("8", &dev, &mflags));
163 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
164 ut_asserteq(BOOTFLOW_METHF_SINGLE_DEV, mflags);
165 media = dev_get_parent(dev);
166 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
167 ut_asserteq_str("mmc2", media->name);
168 ut_assert_console_end();
169
170 /* there should not be this many bootdevs */
171 ut_asserteq(-ENODEV, bootdev_find_by_any("50", &dev, &mflags));
172 ut_assert_nextline("Cannot find '50' (err=-19)");
173 ut_assert_console_end();
174
175 /* Check method flags */
176 ut_assertok(bootdev_find_by_any("pxe", &dev, &mflags));
177 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
178 mflags);
179
180 /* Check invalid uclass */
181 mflags = 123;
Simon Glass4108c6b2023-04-24 13:49:47 +1200182 ut_asserteq(-EPFNOSUPPORT, bootdev_find_by_any("fred0", &dev, &mflags));
183 ut_assert_nextline("Cannot find bootdev 'fred0' (err=-96)");
Simon Glassde567b12023-01-17 10:48:09 -0700184 ut_asserteq(123, mflags);
185 ut_assert_console_end();
186
187 return 0;
188}
189BOOTSTD_TEST(bootdev_test_any, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
190 UT_TESTF_ETH_BOOTDEV);
191
Simon Glassfd5a5192023-10-23 00:02:11 -0700192/*
193 * Check bootdev ordering with the bootdev-order property and boot_targets
194 * environment variable
195 */
Simon Glassb255efc2022-04-24 23:31:24 -0600196static int bootdev_test_order(struct unit_test_state *uts)
197{
198 struct bootflow_iter iter;
199 struct bootflow bflow;
200
Simon Glassfd5a5192023-10-23 00:02:11 -0700201 test_set_skip_delays(true);
202
203 /* Start up USB which gives us three additional bootdevs */
204 usb_started = false;
205 ut_assertok(run_command("usb start", 0));
206
Simon Glassb255efc2022-04-24 23:31:24 -0600207 /*
208 * First try the order set by the bootdev-order property
209 * Like all sandbox unit tests this relies on the devicetree setting up
210 * the required devices:
211 *
212 * mmc0 - nothing connected
213 * mmc1 - connected to mmc1.img file
214 * mmc2 - nothing connected
215 */
216 ut_assertok(env_set("boot_targets", NULL));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700217 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600218 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700219 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
220 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600221 bootflow_iter_uninit(&iter);
222
223 /* Use the environment variable to override it */
Simon Glassfd5a5192023-10-23 00:02:11 -0700224 ut_assertok(env_set("boot_targets", "mmc1 mmc2 usb"));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700225 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700226 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb51f01d2023-10-23 00:02:13 -0700227 ut_asserteq(5, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700228 ut_asserteq_str("mmc1.bootdev", iter.dev_used[0]->name);
229 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassfd5a5192023-10-23 00:02:11 -0700230 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
231 iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600232 bootflow_iter_uninit(&iter);
233
Simon Glass33795a92023-10-23 00:02:12 -0700234 /* Try a single uclass */
235 ut_assertok(env_set("boot_targets", NULL));
236 ut_assertok(bootflow_scan_first(NULL, "mmc", &iter, 0, &bflow));
237 ut_asserteq(2, iter.num_devs);
238
Simon Glassb51f01d2023-10-23 00:02:13 -0700239 /* Now scan past mmc1 and make sure that only mmc0 shows up */
240 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
241 ut_asserteq(3, iter.num_devs);
242 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
243 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
244 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
245 bootflow_iter_uninit(&iter);
246
247 /* Try a single uclass with boot_targets */
248 ut_assertok(env_set("boot_targets", "mmc"));
249 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
250 ut_asserteq(2, iter.num_devs);
251
252 /* Now scan past mmc1 and make sure that only mmc0 shows up */
Simon Glass33795a92023-10-23 00:02:12 -0700253 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
254 ut_asserteq(3, iter.num_devs);
255 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
256 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
257 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
258 bootflow_iter_uninit(&iter);
259
Simon Glassb51f01d2023-10-23 00:02:13 -0700260 /* Try a single uclass with boot_targets */
261 ut_assertok(env_set("boot_targets", "mmc usb"));
262 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
263 ut_asserteq(2, iter.num_devs);
264
265 /* Now scan past mmc1 and make sure that the 3 USB devices show up */
266 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
267 ut_asserteq(6, iter.num_devs);
268 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
269 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
270 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
271 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
272 iter.dev_used[3]->name);
273 bootflow_iter_uninit(&iter);
274
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700275 return 0;
276}
277BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
278
279/* Check default bootdev ordering */
280static int bootdev_test_order_default(struct unit_test_state *uts)
281{
282 struct bootflow_iter iter;
283 struct bootflow bflow;
284
Simon Glassb255efc2022-04-24 23:31:24 -0600285 /*
286 * Now drop both orderings, to check the default (prioriy/sequence)
287 * ordering
288 */
289 ut_assertok(env_set("boot_targets", NULL));
290 ut_assertok(bootstd_test_drop_bootdev_order(uts));
291
Simon Glass5d3d44f2023-01-17 10:48:16 -0700292 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700293 ut_asserteq(2, iter.num_devs);
294 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
295 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600296
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700297 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600298 ut_asserteq(3, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700299 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600300 bootflow_iter_uninit(&iter);
301
302 return 0;
303}
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700304BOOTSTD_TEST(bootdev_test_order_default, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600305
306/* Check bootdev ordering with the uclass priority */
307static int bootdev_test_prio(struct unit_test_state *uts)
308{
309 struct bootdev_uc_plat *ucp;
310 struct bootflow_iter iter;
311 struct bootflow bflow;
312 struct udevice *blk;
313
Simon Glassf3cb89f2023-01-17 10:47:37 -0700314 test_set_skip_delays(true);
315
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700316 /* disable ethernet since the hunter will run dhcp */
317 test_set_eth_enable(false);
318
Simon Glassb255efc2022-04-24 23:31:24 -0600319 /* Start up USB which gives us three additional bootdevs */
320 usb_started = false;
321 ut_assertok(run_command("usb start", 0));
322
323 ut_assertok(bootstd_test_drop_bootdev_order(uts));
324
325 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
326 console_record_reset_enable();
Simon Glass5d3d44f2023-01-17 10:48:16 -0700327 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700328 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600329 ut_asserteq(6, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700330 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600331 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700332 iter.dev_used[3]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600333
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700334 ut_assertok(bootdev_get_sibling_blk(iter.dev_used[3], &blk));
Simon Glassb255efc2022-04-24 23:31:24 -0600335 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
336
337 /* adjust the priority of the first USB bootdev to the highest */
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700338 ucp = dev_get_uclass_plat(iter.dev_used[3]);
339 ucp->prio = BOOTDEVP_1_PRE_SCAN;
Simon Glassb255efc2022-04-24 23:31:24 -0600340
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700341 /* try again but enable hunting, which brings in SCSI */
Simon Glassb255efc2022-04-24 23:31:24 -0600342 bootflow_iter_uninit(&iter);
Simon Glass99e68182023-02-22 12:17:03 -0700343 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWIF_HUNT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700344 &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700345 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
346 ut_asserteq(7, iter.num_devs);
Simon Glassb255efc2022-04-24 23:31:24 -0600347 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700348 iter.dev_used[0]->name);
349 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600350
351 return 0;
352}
353BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass5acb97a2023-01-17 10:47:33 -0700354
355/* Check listing hunters */
356static int bootdev_test_hunter(struct unit_test_state *uts)
357{
358 struct bootstd_priv *std;
359
Simon Glass4bb7c162023-05-05 20:03:04 -0600360 usb_started = false;
Simon Glassf3cb89f2023-01-17 10:47:37 -0700361 test_set_skip_delays(true);
362
Simon Glass5acb97a2023-01-17 10:47:33 -0700363 /* get access to the used hunters */
364 ut_assertok(bootstd_get_priv(&std));
365
366 console_record_reset_enable();
367 bootdev_list_hunters(std);
368 ut_assert_nextline("Prio Used Uclass Hunter");
369 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700370 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700371 ut_assert_nextline(" 1 simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700372 ut_assert_nextline(" 5 ide ide_bootdev");
373 ut_assert_nextline(" 2 mmc mmc_bootdev");
374 ut_assert_nextline(" 4 nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700375 ut_assert_nextline(" 4 qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700376 ut_assert_nextline(" 4 scsi scsi_bootdev");
377 ut_assert_nextline(" 4 spi_flash sf_bootdev");
378 ut_assert_nextline(" 5 usb usb_bootdev");
379 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700380 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700381 ut_assert_console_end();
382
Simon Glassf3cb89f2023-01-17 10:47:37 -0700383 ut_assertok(bootdev_hunt("usb1", false));
384 ut_assert_nextline(
385 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass1248d2b2023-01-17 10:47:34 -0700386 ut_assert_console_end();
387
Simon Glassdeffa692023-01-28 15:00:28 -0700388 /* USB is 7th in the list, so bit 8 */
389 ut_asserteq(BIT(8), std->hunters_used);
Simon Glassf3cb89f2023-01-17 10:47:37 -0700390
Simon Glass5acb97a2023-01-17 10:47:33 -0700391 return 0;
392}
393BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
394
395/* Check 'bootdev hunt' command */
396static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
397{
398 struct bootstd_priv *std;
399
Simon Glassf3cb89f2023-01-17 10:47:37 -0700400 test_set_skip_delays(true);
Simon Glass4bb7c162023-05-05 20:03:04 -0600401 usb_started = false;
Simon Glassf3cb89f2023-01-17 10:47:37 -0700402
Simon Glass5acb97a2023-01-17 10:47:33 -0700403 /* get access to the used hunters */
404 ut_assertok(bootstd_get_priv(&std));
405
406 console_record_reset_enable();
407 ut_assertok(run_command("bootdev hunt -l", 0));
408 ut_assert_nextline("Prio Used Uclass Hunter");
409 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700410 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700411 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700412 ut_assert_console_end();
413
Simon Glass7e1f6a42023-01-17 10:48:08 -0700414 /* Use the MMC hunter and see that it updates */
415 ut_assertok(run_command("bootdev hunt mmc", 0));
416 ut_assertok(run_command("bootdev hunt -l", 0));
417 ut_assert_skip_to_line(" 5 ide ide_bootdev");
418 ut_assert_nextline(" 2 * mmc mmc_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700419 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700420 ut_assert_console_end();
421
Simon Glass5acb97a2023-01-17 10:47:33 -0700422 /* Scan all hunters */
Simon Glassac06b26a2023-01-17 10:48:10 -0700423 test_set_eth_enable(false);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700424 test_set_skip_delays(true);
Simon Glass5acb97a2023-01-17 10:47:33 -0700425 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700426 ut_assert_nextline("Hunting with: ethernet");
Simon Glasse4cf1062023-01-17 10:48:13 -0700427
428 /* This is the extension feature which has no uclass at present */
429 ut_assert_nextline("Hunting with: simple_bus");
430 ut_assert_nextline("Found 2 extension board(s).");
Simon Glassbe7c6662023-01-17 10:47:46 -0700431 ut_assert_nextline("Hunting with: ide");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700432
433 /* mmc hunter has already been used so should not run again */
434
Simon Glass08382872023-01-17 10:47:47 -0700435 ut_assert_nextline("Hunting with: nvme");
Simon Glassdeffa692023-01-28 15:00:28 -0700436 ut_assert_nextline("Hunting with: qfw");
Simon Glass9985e9c2023-01-17 10:47:45 -0700437 ut_assert_nextline("Hunting with: scsi");
438 ut_assert_nextline("scanning bus for devices...");
Simon Glass215be682023-01-17 10:48:03 -0700439 ut_assert_skip_to_line("Hunting with: spi_flash");
440 ut_assert_nextline("Hunting with: usb");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700441 ut_assert_nextline(
442 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass215be682023-01-17 10:48:03 -0700443 ut_assert_nextline("Hunting with: virtio");
Simon Glass5acb97a2023-01-17 10:47:33 -0700444 ut_assert_console_end();
445
446 /* List available hunters */
447 ut_assertok(run_command("bootdev hunt -l", 0));
448 ut_assert_nextlinen("Prio");
449 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700450 ut_assert_nextline(" 6 * ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700451 ut_assert_nextline(" 1 * simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700452 ut_assert_nextline(" 5 * ide ide_bootdev");
453 ut_assert_nextline(" 2 * mmc mmc_bootdev");
454 ut_assert_nextline(" 4 * nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700455 ut_assert_nextline(" 4 * qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700456 ut_assert_nextline(" 4 * scsi scsi_bootdev");
457 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
458 ut_assert_nextline(" 5 * usb usb_bootdev");
459 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700460 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700461 ut_assert_console_end();
462
Simon Glasse4cf1062023-01-17 10:48:13 -0700463 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass5acb97a2023-01-17 10:47:33 -0700464
465 return 0;
466}
467BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
468 UT_TESTF_ETH_BOOTDEV);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700469
Simon Glass484e4072023-01-17 10:48:14 -0700470/* Check searching for bootdevs using the hunters */
471static int bootdev_test_hunt_scan(struct unit_test_state *uts)
472{
473 struct bootflow_iter iter;
474 struct bootstd_priv *std;
475 struct bootflow bflow;
476
477 /* get access to the used hunters */
478 ut_assertok(bootstd_get_priv(&std));
479
480 ut_assertok(bootstd_test_drop_bootdev_order(uts));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700481 ut_assertok(bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700482 BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT |
483 BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glass484e4072023-01-17 10:48:14 -0700484 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
485
486 return 0;
487}
488BOOTSTD_TEST(bootdev_test_hunt_scan, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
489
Simon Glass64cbc5c2023-01-17 10:47:42 -0700490/* Check that only bootable partitions are processed */
491static int bootdev_test_bootable(struct unit_test_state *uts)
492{
493 struct bootflow_iter iter;
494 struct bootflow bflow;
495 struct udevice *blk;
496
497 memset(&iter, '\0', sizeof(iter));
498 memset(&bflow, '\0', sizeof(bflow));
499 iter.part = 0;
500 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
501 iter.dev = blk;
502 ut_assertok(device_find_next_child(&iter.dev));
503 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
504
505 /*
506 * initially we don't have any knowledge of which partitions are
507 * bootable, but mmc1 has two partitions, with the first one being
508 * bootable
509 */
510 iter.part = 2;
511 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
512 ut_asserteq(0, iter.first_bootable);
513
514 /* scan with part == 0 to get the partition info */
515 iter.part = 0;
516 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
517 ut_asserteq(1, iter.first_bootable);
518
519 /* now it will refuse to use non-bootable partitions */
520 iter.part = 2;
521 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
522
523 return 0;
524}
525BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700526
527/* Check hunting for bootdev of a particular priority */
528static int bootdev_test_hunt_prio(struct unit_test_state *uts)
529{
Simon Glass4bb7c162023-05-05 20:03:04 -0600530 usb_started = false;
Simon Glass3e9f6be2023-01-17 10:48:07 -0700531 test_set_skip_delays(true);
532
533 console_record_reset_enable();
Simon Glass7e1f6a42023-01-17 10:48:08 -0700534 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700535 ut_assert_nextline("scanning bus for devices...");
536 ut_assert_skip_to_line(" Type: Hard Disk");
537 ut_assert_nextlinen(" Capacity:");
538 ut_assert_console_end();
539
540 /* now try a different priority, verbosely */
Simon Glass7e1f6a42023-01-17 10:48:08 -0700541 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700542 ut_assert_nextline("Hunting with: ide");
Simon Glass3e9f6be2023-01-17 10:48:07 -0700543 ut_assert_nextline("Hunting with: usb");
544 ut_assert_nextline(
545 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
546 ut_assert_console_end();
547
548 return 0;
549}
550BOOTSTD_TEST(bootdev_test_hunt_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassde567b12023-01-17 10:48:09 -0700551
552/* Check hunting for bootdevs with a particular label */
553static int bootdev_test_hunt_label(struct unit_test_state *uts)
554{
555 struct udevice *dev, *old;
556 struct bootstd_priv *std;
557 int mflags;
558
Simon Glass4bb7c162023-05-05 20:03:04 -0600559 usb_started = false;
560
Simon Glassde567b12023-01-17 10:48:09 -0700561 /* get access to the used hunters */
562 ut_assertok(bootstd_get_priv(&std));
563
564 /* scan an unknown uclass */
565 console_record_reset_enable();
566 old = (void *)&mflags; /* arbitrary pointer to check against dev */
567 dev = old;
568 mflags = 123;
Simon Glass4108c6b2023-04-24 13:49:47 +1200569 ut_asserteq(-EPFNOSUPPORT,
Simon Glassde567b12023-01-17 10:48:09 -0700570 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700571 ut_asserteq_ptr(old, dev);
572 ut_asserteq(123, mflags);
573 ut_assert_console_end();
574 ut_asserteq(0, std->hunters_used);
575
576 /* scan an invalid mmc controllers */
577 ut_asserteq(-ENOENT,
578 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
579 ut_asserteq_ptr(old, dev);
580 ut_asserteq(123, mflags);
Simon Glassde567b12023-01-17 10:48:09 -0700581 ut_assert_console_end();
582
583 ut_assertok(bootstd_test_check_mmc_hunter(uts));
584
585 /* scan for a particular mmc controller */
586 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
587 ut_assertnonnull(dev);
588 ut_asserteq_str("mmc1.bootdev", dev->name);
589 ut_asserteq(0, mflags);
590 ut_assert_console_end();
591
592 /* scan all of usb */
593 test_set_skip_delays(true);
594 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
595 ut_assertnonnull(dev);
596 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
597 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
598 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
599 ut_assert_console_end();
600
601 return 0;
602}
603BOOTSTD_TEST(bootdev_test_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassac06b26a2023-01-17 10:48:10 -0700604
605/* Check iterating to the next label in a list */
606static int bootdev_test_next_label(struct unit_test_state *uts)
607{
608 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
609 struct bootflow_iter iter;
610 struct bootstd_priv *std;
611 struct bootflow bflow;
612 struct udevice *dev;
613 int mflags;
614
615 test_set_eth_enable(false);
616
617 /* get access to the used hunters */
618 ut_assertok(bootstd_get_priv(&std));
619
620 memset(&iter, '\0', sizeof(iter));
621 memset(&bflow, '\0', sizeof(bflow));
622 iter.part = 0;
623 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
624 iter.cur_label = -1;
625 iter.labels = labels;
626
627 dev = NULL;
628 mflags = 123;
629 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
630 console_record_reset_enable();
631 ut_assert_console_end();
632 ut_assertnonnull(dev);
633 ut_asserteq_str("mmc0.bootdev", dev->name);
634 ut_asserteq(0, mflags);
635
636 ut_assertok(bootstd_test_check_mmc_hunter(uts));
637
638 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
639 ut_assert_nextline("scanning bus for devices...");
640 ut_assert_skip_to_line(
641 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
642 ut_assert_console_end();
643 ut_assertnonnull(dev);
644 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
645 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
646
Simon Glassdeffa692023-01-28 15:00:28 -0700647 /* SCSI is 7th in the list, so bit 6 */
648 ut_asserteq(BIT(MMC_HUNTER) | BIT(6), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700649
650 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
651 ut_assert_console_end();
652 ut_assertnonnull(dev);
653 ut_asserteq_str("eth@10002000.bootdev", dev->name);
654 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
655 mflags);
656
657 /* dhcp: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700658 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700659
660 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
661 ut_assert_console_end();
662 ut_assertnonnull(dev);
663 ut_asserteq_str("eth@10002000.bootdev", dev->name);
664 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
665 mflags);
666
667 /* pxe: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700668 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700669
670 mflags = 123;
671 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
672 ut_asserteq(123, mflags);
673 ut_assert_console_end();
674
675 /* no change */
Simon Glassdeffa692023-01-28 15:00:28 -0700676 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700677
678 return 0;
679}
680BOOTSTD_TEST(bootdev_test_next_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
681 UT_TESTF_ETH_BOOTDEV | UT_TESTF_SF_BOOTDEV);
Simon Glass660a9952023-01-17 10:48:11 -0700682
Simon Glass660a9952023-01-17 10:48:11 -0700683/* Check iterating to the next prioirty in a list */
684static int bootdev_test_next_prio(struct unit_test_state *uts)
685{
686 struct bootflow_iter iter;
687 struct bootstd_priv *std;
688 struct bootflow bflow;
689 struct udevice *dev;
690 int ret;
691
Simon Glassba3d5372023-01-17 10:48:15 -0700692 test_set_eth_enable(false);
Simon Glass660a9952023-01-17 10:48:11 -0700693 test_set_skip_delays(true);
694
695 /* get access to the used hunters */
696 ut_assertok(bootstd_get_priv(&std));
697
698 memset(&iter, '\0', sizeof(iter));
699 memset(&bflow, '\0', sizeof(bflow));
700 iter.part = 0;
701 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
702 iter.cur_prio = 0;
Simon Glass99e68182023-02-22 12:17:03 -0700703 iter.flags = BOOTFLOWIF_SHOW;
Simon Glass660a9952023-01-17 10:48:11 -0700704
705 dev = NULL;
706 console_record_reset_enable();
707 ut_assertok(bootdev_next_prio(&iter, &dev));
708 ut_assertnonnull(dev);
709 ut_asserteq_str("mmc2.bootdev", dev->name);
710
711 /* hunt flag not set, so this should not use any hunters */
712 ut_asserteq(0, std->hunters_used);
713 ut_assert_console_end();
714
715 /* now try again with hunting enabled */
Simon Glass99e68182023-02-22 12:17:03 -0700716 iter.flags = BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT;
Simon Glass660a9952023-01-17 10:48:11 -0700717 iter.cur_prio = 0;
718 iter.part = 0;
719
720 ut_assertok(bootdev_next_prio(&iter, &dev));
721 ut_asserteq_str("mmc2.bootdev", dev->name);
Simon Glasse4cf1062023-01-17 10:48:13 -0700722 ut_assert_nextline("Hunting with: simple_bus");
723 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass660a9952023-01-17 10:48:11 -0700724 ut_assert_nextline("Hunting with: mmc");
725 ut_assert_console_end();
726
Simon Glasse4cf1062023-01-17 10:48:13 -0700727 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700728
729 ut_assertok(bootdev_next_prio(&iter, &dev));
730 ut_asserteq_str("mmc1.bootdev", dev->name);
731
732 ut_assertok(bootdev_next_prio(&iter, &dev));
733 ut_asserteq_str("mmc0.bootdev", dev->name);
734 ut_assert_console_end();
735
736 ut_assertok(bootdev_next_prio(&iter, &dev));
737 ut_asserteq_str("spi.bin@0.bootdev", dev->name);
738 ut_assert_skip_to_line("Hunting with: spi_flash");
739
740 /*
741 * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it
742 * starts looking at the devices, so we se virtio as well
743 */
744 ut_assert_nextline("Hunting with: virtio");
745 ut_assert_nextlinen("SF: Detected m25p16");
746
747 ut_assertok(bootdev_next_prio(&iter, &dev));
748 ut_asserteq_str("spi.bin@1.bootdev", dev->name);
749 ut_assert_nextlinen("SF: Detected m25p16");
750 ut_assert_console_end();
751
752 /* keep going until there are no more bootdevs */
753 do {
754 ret = bootdev_next_prio(&iter, &dev);
755 } while (!ret);
756 ut_asserteq(-ENODEV, ret);
757 ut_assertnull(dev);
Simon Glasse4cf1062023-01-17 10:48:13 -0700758 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700759
760 ut_assert_skip_to_line("Hunting with: ethernet");
761 ut_assert_console_end();
762
763 return 0;
764}
765BOOTSTD_TEST(bootdev_test_next_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
766 UT_TESTF_SF_BOOTDEV);