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