blob: 608aef15265b46826ec0dd6d086e0c9bc3bf4c2e [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
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 */
21extern char usb_started;
22
23/* Check 'bootdev list' command */
24static 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}
49BOOTSTD_TEST(bootdev_test_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
50
51/* Check 'bootdev select' and 'info' commands */
52static 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}
99BOOTSTD_TEST(bootdev_test_cmd_select, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
100
101/* Check bootdev labels */
102static int bootdev_test_labels(struct unit_test_state *uts)
103{
104 struct udevice *dev, *media;
Simon Glasse22fe922023-01-17 10:48:05 -0700105 int mflags = 0;
Simon Glassb255efc2022-04-24 23:31:24 -0600106
Simon Glasse22fe922023-01-17 10:48:05 -0700107 ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600108 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
Simon Glasse22fe922023-01-17 10:48:05 -0700109 ut_asserteq(0, mflags);
Simon Glassb255efc2022-04-24 23:31:24 -0600110 media = dev_get_parent(dev);
111 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
112 ut_asserteq_str("mmc2", media->name);
113
Simon Glasse22fe922023-01-17 10:48:05 -0700114 /* Check method flags */
115 ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700116 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
117 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700118 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700119 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
120 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700121
Simon Glassb255efc2022-04-24 23:31:24 -0600122 /* Check invalid uclass */
Simon Glasse22fe922023-01-17 10:48:05 -0700123 ut_asserteq(-EINVAL, bootdev_find_by_label("fred0", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600124
125 /* Check unknown sequence number */
Simon Glasse22fe922023-01-17 10:48:05 -0700126 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600127
128 return 0;
129}
Simon Glasse22fe922023-01-17 10:48:05 -0700130BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
131 UT_TESTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600132
Simon Glassde567b12023-01-17 10:48:09 -0700133/* Check bootdev_find_by_any() */
134static int bootdev_test_any(struct unit_test_state *uts)
135{
136 struct udevice *dev, *media;
137 int mflags;
138
139 /*
140 * with ethernet enabled we have 8 devices ahead of the mmc ones:
141 *
142 * ut_assertok(run_command("bootdev list", 0));
143 * Seq Probed Status Uclass Name
144 * --- ------ ------ -------- ------------------
145 * 0 [ + ] OK ethernet eth@10002000.bootdev
146 * 1 [ ] OK ethernet eth@10003000.bootdev
147 * 2 [ ] OK ethernet sbe5.bootdev
148 * 3 [ ] OK ethernet eth@10004000.bootdev
149 * 4 [ ] OK ethernet phy-test-eth.bootdev
150 * 5 [ ] OK ethernet dsa-test-eth.bootdev
151 * 6 [ ] OK ethernet dsa-test@0.bootdev
152 * 7 [ ] OK ethernet dsa-test@1.bootdev
153 * 8 [ ] OK mmc mmc2.bootdev
154 * 9 [ + ] OK mmc mmc1.bootdev
155 * a [ ] OK mmc mmc0.bootdev
156 */
157 console_record_reset_enable();
158 ut_assertok(bootdev_find_by_any("8", &dev, &mflags));
159 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
160 ut_asserteq(BOOTFLOW_METHF_SINGLE_DEV, mflags);
161 media = dev_get_parent(dev);
162 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
163 ut_asserteq_str("mmc2", media->name);
164 ut_assert_console_end();
165
166 /* there should not be this many bootdevs */
167 ut_asserteq(-ENODEV, bootdev_find_by_any("50", &dev, &mflags));
168 ut_assert_nextline("Cannot find '50' (err=-19)");
169 ut_assert_console_end();
170
171 /* Check method flags */
172 ut_assertok(bootdev_find_by_any("pxe", &dev, &mflags));
173 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
174 mflags);
175
176 /* Check invalid uclass */
177 mflags = 123;
178 ut_asserteq(-EINVAL, bootdev_find_by_any("fred0", &dev, &mflags));
179 ut_assert_nextline("Unknown uclass 'fred0' in label");
180 ut_assert_nextline("Cannot find bootdev 'fred0' (err=-22)");
181 ut_asserteq(123, mflags);
182 ut_assert_console_end();
183
184 return 0;
185}
186BOOTSTD_TEST(bootdev_test_any, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
187 UT_TESTF_ETH_BOOTDEV);
188
Simon Glassb255efc2022-04-24 23:31:24 -0600189/* Check bootdev ordering with the bootdev-order property */
190static int bootdev_test_order(struct unit_test_state *uts)
191{
192 struct bootflow_iter iter;
193 struct bootflow bflow;
194
195 /*
196 * First try the order set by the bootdev-order property
197 * Like all sandbox unit tests this relies on the devicetree setting up
198 * the required devices:
199 *
200 * mmc0 - nothing connected
201 * mmc1 - connected to mmc1.img file
202 * mmc2 - nothing connected
203 */
204 ut_assertok(env_set("boot_targets", NULL));
205 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
206 ut_asserteq(2, iter.num_devs);
207 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
208 ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name);
209 bootflow_iter_uninit(&iter);
210
211 /* Use the environment variable to override it */
212 ut_assertok(env_set("boot_targets", "mmc1 mmc2"));
213 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
214 ut_asserteq(2, iter.num_devs);
215 ut_asserteq_str("mmc1.bootdev", iter.dev_order[0]->name);
216 ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name);
217 bootflow_iter_uninit(&iter);
218
219 /*
220 * Now drop both orderings, to check the default (prioriy/sequence)
221 * ordering
222 */
223 ut_assertok(env_set("boot_targets", NULL));
224 ut_assertok(bootstd_test_drop_bootdev_order(uts));
225
226 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
227 ut_asserteq(3, iter.num_devs);
228 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
229 ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name);
230 ut_asserteq_str("mmc0.bootdev", iter.dev_order[2]->name);
231
232 /*
233 * Check that adding aliases for the bootdevs works. We just fake it by
234 * setting the sequence numbers directly.
235 */
236 iter.dev_order[0]->seq_ = 0;
237 iter.dev_order[1]->seq_ = 3;
238 iter.dev_order[2]->seq_ = 2;
239 bootflow_iter_uninit(&iter);
240
241 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
242 ut_asserteq(3, iter.num_devs);
243 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
244 ut_asserteq_str("mmc0.bootdev", iter.dev_order[1]->name);
245 ut_asserteq_str("mmc1.bootdev", iter.dev_order[2]->name);
246 bootflow_iter_uninit(&iter);
247
248 return 0;
249}
250BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
251
252/* Check bootdev ordering with the uclass priority */
253static int bootdev_test_prio(struct unit_test_state *uts)
254{
255 struct bootdev_uc_plat *ucp;
256 struct bootflow_iter iter;
257 struct bootflow bflow;
258 struct udevice *blk;
259
Simon Glassf3cb89f2023-01-17 10:47:37 -0700260 test_set_skip_delays(true);
261
Simon Glassb255efc2022-04-24 23:31:24 -0600262 /* Start up USB which gives us three additional bootdevs */
263 usb_started = false;
264 ut_assertok(run_command("usb start", 0));
265
266 ut_assertok(bootstd_test_drop_bootdev_order(uts));
267
268 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
269 console_record_reset_enable();
270 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
271 ut_asserteq(6, iter.num_devs);
272 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
273 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
274 iter.dev_order[3]->name);
275
276 ut_assertok(bootdev_get_sibling_blk(iter.dev_order[3], &blk));
277 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
278
279 /* adjust the priority of the first USB bootdev to the highest */
280 ucp = dev_get_uclass_plat(iter.dev_order[3]);
281 ucp->prio = 1;
282
283 bootflow_iter_uninit(&iter);
284 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
285 ut_asserteq(6, iter.num_devs);
286 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
287 iter.dev_order[0]->name);
288 ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name);
289
290 return 0;
291}
292BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass5acb97a2023-01-17 10:47:33 -0700293
294/* Check listing hunters */
295static int bootdev_test_hunter(struct unit_test_state *uts)
296{
297 struct bootstd_priv *std;
298
Simon Glassf3cb89f2023-01-17 10:47:37 -0700299 test_set_skip_delays(true);
300
Simon Glass5acb97a2023-01-17 10:47:33 -0700301 /* get access to the used hunters */
302 ut_assertok(bootstd_get_priv(&std));
303
304 console_record_reset_enable();
305 bootdev_list_hunters(std);
306 ut_assert_nextline("Prio Used Uclass Hunter");
307 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700308 ut_assert_nextline(" 6 ethernet eth_bootdev");
309 ut_assert_nextline(" 5 ide ide_bootdev");
310 ut_assert_nextline(" 2 mmc mmc_bootdev");
311 ut_assert_nextline(" 4 nvme nvme_bootdev");
312 ut_assert_nextline(" 4 scsi scsi_bootdev");
313 ut_assert_nextline(" 4 spi_flash sf_bootdev");
314 ut_assert_nextline(" 5 usb usb_bootdev");
315 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glass215be682023-01-17 10:48:03 -0700316 ut_assert_nextline("(total hunters: 8)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700317 ut_assert_console_end();
318
Simon Glassf3cb89f2023-01-17 10:47:37 -0700319 ut_assertok(bootdev_hunt("usb1", false));
320 ut_assert_nextline(
321 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass1248d2b2023-01-17 10:47:34 -0700322 ut_assert_console_end();
323
Simon Glass215be682023-01-17 10:48:03 -0700324 /* USB is fifth in the list, so bit 6 */
325 ut_asserteq(BIT(6), std->hunters_used);
Simon Glassf3cb89f2023-01-17 10:47:37 -0700326
Simon Glass5acb97a2023-01-17 10:47:33 -0700327 return 0;
328}
329BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
330
331/* Check 'bootdev hunt' command */
332static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
333{
334 struct bootstd_priv *std;
335
Simon Glassf3cb89f2023-01-17 10:47:37 -0700336 test_set_skip_delays(true);
337
Simon Glass5acb97a2023-01-17 10:47:33 -0700338 /* get access to the used hunters */
339 ut_assertok(bootstd_get_priv(&std));
340
341 console_record_reset_enable();
342 ut_assertok(run_command("bootdev hunt -l", 0));
343 ut_assert_nextline("Prio Used Uclass Hunter");
344 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700345 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glass215be682023-01-17 10:48:03 -0700346 ut_assert_skip_to_line("(total hunters: 8)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700347 ut_assert_console_end();
348
Simon Glass7e1f6a42023-01-17 10:48:08 -0700349 /* Use the MMC hunter and see that it updates */
350 ut_assertok(run_command("bootdev hunt mmc", 0));
351 ut_assertok(run_command("bootdev hunt -l", 0));
352 ut_assert_skip_to_line(" 5 ide ide_bootdev");
353 ut_assert_nextline(" 2 * mmc mmc_bootdev");
354 ut_assert_skip_to_line("(total hunters: 8)");
355 ut_assert_console_end();
356
Simon Glass5acb97a2023-01-17 10:47:33 -0700357 /* Scan all hunters */
Simon Glassac06b26a2023-01-17 10:48:10 -0700358 test_set_eth_enable(false);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700359 test_set_skip_delays(true);
Simon Glass5acb97a2023-01-17 10:47:33 -0700360 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700361 ut_assert_nextline("Hunting with: ethernet");
Simon Glassbe7c6662023-01-17 10:47:46 -0700362 ut_assert_nextline("Hunting with: ide");
363 ut_assert_nextline("Bus 0: not available ");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700364
365 /* mmc hunter has already been used so should not run again */
366
Simon Glass08382872023-01-17 10:47:47 -0700367 ut_assert_nextline("Hunting with: nvme");
Simon Glass9985e9c2023-01-17 10:47:45 -0700368 ut_assert_nextline("Hunting with: scsi");
369 ut_assert_nextline("scanning bus for devices...");
Simon Glass215be682023-01-17 10:48:03 -0700370 ut_assert_skip_to_line("Hunting with: spi_flash");
371 ut_assert_nextline("Hunting with: usb");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700372 ut_assert_nextline(
373 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass215be682023-01-17 10:48:03 -0700374 ut_assert_nextline("Hunting with: virtio");
Simon Glass5acb97a2023-01-17 10:47:33 -0700375 ut_assert_console_end();
376
377 /* List available hunters */
378 ut_assertok(run_command("bootdev hunt -l", 0));
379 ut_assert_nextlinen("Prio");
380 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700381 ut_assert_nextline(" 6 * ethernet eth_bootdev");
382 ut_assert_nextline(" 5 * ide ide_bootdev");
383 ut_assert_nextline(" 2 * mmc mmc_bootdev");
384 ut_assert_nextline(" 4 * nvme nvme_bootdev");
385 ut_assert_nextline(" 4 * scsi scsi_bootdev");
386 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
387 ut_assert_nextline(" 5 * usb usb_bootdev");
388 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glass215be682023-01-17 10:48:03 -0700389 ut_assert_nextline("(total hunters: 8)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700390 ut_assert_console_end();
391
Simon Glass215be682023-01-17 10:48:03 -0700392 ut_asserteq(GENMASK(7, 0), std->hunters_used);
Simon Glass5acb97a2023-01-17 10:47:33 -0700393
394 return 0;
395}
396BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
397 UT_TESTF_ETH_BOOTDEV);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700398
399/* Check that only bootable partitions are processed */
400static int bootdev_test_bootable(struct unit_test_state *uts)
401{
402 struct bootflow_iter iter;
403 struct bootflow bflow;
404 struct udevice *blk;
405
406 memset(&iter, '\0', sizeof(iter));
407 memset(&bflow, '\0', sizeof(bflow));
408 iter.part = 0;
409 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
410 iter.dev = blk;
411 ut_assertok(device_find_next_child(&iter.dev));
412 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
413
414 /*
415 * initially we don't have any knowledge of which partitions are
416 * bootable, but mmc1 has two partitions, with the first one being
417 * bootable
418 */
419 iter.part = 2;
420 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
421 ut_asserteq(0, iter.first_bootable);
422
423 /* scan with part == 0 to get the partition info */
424 iter.part = 0;
425 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
426 ut_asserteq(1, iter.first_bootable);
427
428 /* now it will refuse to use non-bootable partitions */
429 iter.part = 2;
430 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
431
432 return 0;
433}
434BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700435
436/* Check hunting for bootdev of a particular priority */
437static int bootdev_test_hunt_prio(struct unit_test_state *uts)
438{
439 test_set_skip_delays(true);
440
441 console_record_reset_enable();
Simon Glass7e1f6a42023-01-17 10:48:08 -0700442 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700443 ut_assert_nextline("scanning bus for devices...");
444 ut_assert_skip_to_line(" Type: Hard Disk");
445 ut_assert_nextlinen(" Capacity:");
446 ut_assert_console_end();
447
448 /* now try a different priority, verbosely */
Simon Glass7e1f6a42023-01-17 10:48:08 -0700449 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700450 ut_assert_nextline("Hunting with: ide");
451 ut_assert_nextline("Bus 0: not available ");
452 ut_assert_nextline("Hunting with: usb");
453 ut_assert_nextline(
454 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
455 ut_assert_console_end();
456
457 return 0;
458}
459BOOTSTD_TEST(bootdev_test_hunt_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassde567b12023-01-17 10:48:09 -0700460
461/* Check hunting for bootdevs with a particular label */
462static int bootdev_test_hunt_label(struct unit_test_state *uts)
463{
464 struct udevice *dev, *old;
465 struct bootstd_priv *std;
466 int mflags;
467
468 /* get access to the used hunters */
469 ut_assertok(bootstd_get_priv(&std));
470
471 /* scan an unknown uclass */
472 console_record_reset_enable();
473 old = (void *)&mflags; /* arbitrary pointer to check against dev */
474 dev = old;
475 mflags = 123;
476 ut_asserteq(-EINVAL,
477 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
478 ut_assert_nextline("Unknown uclass 'fred' in label");
479 ut_asserteq_ptr(old, dev);
480 ut_asserteq(123, mflags);
481 ut_assert_console_end();
482 ut_asserteq(0, std->hunters_used);
483
484 /* scan an invalid mmc controllers */
485 ut_asserteq(-ENOENT,
486 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
487 ut_asserteq_ptr(old, dev);
488 ut_asserteq(123, mflags);
489 ut_assert_nextline("Unknown seq 4 for label 'mmc4'");
490 ut_assert_console_end();
491
492 ut_assertok(bootstd_test_check_mmc_hunter(uts));
493
494 /* scan for a particular mmc controller */
495 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
496 ut_assertnonnull(dev);
497 ut_asserteq_str("mmc1.bootdev", dev->name);
498 ut_asserteq(0, mflags);
499 ut_assert_console_end();
500
501 /* scan all of usb */
502 test_set_skip_delays(true);
503 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
504 ut_assertnonnull(dev);
505 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
506 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
507 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
508 ut_assert_console_end();
509
510 return 0;
511}
512BOOTSTD_TEST(bootdev_test_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassac06b26a2023-01-17 10:48:10 -0700513
514/* Check iterating to the next label in a list */
515static int bootdev_test_next_label(struct unit_test_state *uts)
516{
517 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
518 struct bootflow_iter iter;
519 struct bootstd_priv *std;
520 struct bootflow bflow;
521 struct udevice *dev;
522 int mflags;
523
524 test_set_eth_enable(false);
525
526 /* get access to the used hunters */
527 ut_assertok(bootstd_get_priv(&std));
528
529 memset(&iter, '\0', sizeof(iter));
530 memset(&bflow, '\0', sizeof(bflow));
531 iter.part = 0;
532 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
533 iter.cur_label = -1;
534 iter.labels = labels;
535
536 dev = NULL;
537 mflags = 123;
538 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
539 console_record_reset_enable();
540 ut_assert_console_end();
541 ut_assertnonnull(dev);
542 ut_asserteq_str("mmc0.bootdev", dev->name);
543 ut_asserteq(0, mflags);
544
545 ut_assertok(bootstd_test_check_mmc_hunter(uts));
546
547 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
548 ut_assert_nextline("scanning bus for devices...");
549 ut_assert_skip_to_line(
550 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
551 ut_assert_console_end();
552 ut_assertnonnull(dev);
553 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
554 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
555
556 /* SCSI is fifth in the list, so bit 4 */
557 ut_asserteq(BIT(2) | BIT(4), std->hunters_used);
558
559 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
560 ut_assert_console_end();
561 ut_assertnonnull(dev);
562 ut_asserteq_str("eth@10002000.bootdev", dev->name);
563 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
564 mflags);
565
566 /* dhcp: Ethernet is first so bit 0 */
567 ut_asserteq(BIT(2) | BIT(4) | BIT(0), std->hunters_used);
568
569 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
570 ut_assert_console_end();
571 ut_assertnonnull(dev);
572 ut_asserteq_str("eth@10002000.bootdev", dev->name);
573 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
574 mflags);
575
576 /* pxe: Ethernet is first so bit 0 */
577 ut_asserteq(BIT(2) | BIT(4) | BIT(0), std->hunters_used);
578
579 mflags = 123;
580 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
581 ut_asserteq(123, mflags);
582 ut_assert_console_end();
583
584 /* no change */
585 ut_asserteq(BIT(2) | BIT(4) | BIT(0), std->hunters_used);
586
587 return 0;
588}
589BOOTSTD_TEST(bootdev_test_next_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
590 UT_TESTF_ETH_BOOTDEV | UT_TESTF_SF_BOOTDEV);