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