blob: 8cf3f30e0f75ca44754ad2ad7098c69dbcd82707 [file] [log] [blame]
Simon Glassfb1451b2022-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 */
Linus Walleij0c1413f2023-01-23 23:07:21 +010021#if defined(CONFIG_USB_HOST) || defined(CONFIG_USB_GADGET)
Simon Glassfb1451b2022-04-24 23:31:24 -060022extern char usb_started;
Linus Walleij0c1413f2023-01-23 23:07:21 +010023#else
24char usb_started;
25#endif
Simon Glassfb1451b2022-04-24 23:31:24 -060026
27/* Check 'bootdev list' command */
28static 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}
53BOOTSTD_TEST(bootdev_test_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
54
55/* Check 'bootdev select' and 'info' commands */
56static 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}
103BOOTSTD_TEST(bootdev_test_cmd_select, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
104
105/* Check bootdev labels */
106static int bootdev_test_labels(struct unit_test_state *uts)
107{
108 struct udevice *dev, *media;
Simon Glassd9f48572023-01-17 10:48:05 -0700109 int mflags = 0;
Simon Glassfb1451b2022-04-24 23:31:24 -0600110
Simon Glassd9f48572023-01-17 10:48:05 -0700111 ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags));
Simon Glassfb1451b2022-04-24 23:31:24 -0600112 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
Simon Glassd9f48572023-01-17 10:48:05 -0700113 ut_asserteq(0, mflags);
Simon Glassfb1451b2022-04-24 23:31:24 -0600114 media = dev_get_parent(dev);
115 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
116 ut_asserteq_str("mmc2", media->name);
117
Simon Glassd9f48572023-01-17 10:48:05 -0700118 /* Check method flags */
119 ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags));
Simon Glass66e3dce2023-01-17 10:48:09 -0700120 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
121 mflags);
Simon Glassd9f48572023-01-17 10:48:05 -0700122 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
Simon Glass66e3dce2023-01-17 10:48:09 -0700123 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
124 mflags);
Simon Glassd9f48572023-01-17 10:48:05 -0700125
Simon Glassfb1451b2022-04-24 23:31:24 -0600126 /* Check invalid uclass */
Simon Glass1736b4a2023-04-24 13:49:47 +1200127 ut_asserteq(-EPFNOSUPPORT,
128 bootdev_find_by_label("fred0", &dev, &mflags));
Simon Glassfb1451b2022-04-24 23:31:24 -0600129
130 /* Check unknown sequence number */
Simon Glassd9f48572023-01-17 10:48:05 -0700131 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
Simon Glassfb1451b2022-04-24 23:31:24 -0600132
133 return 0;
134}
Simon Glassd9f48572023-01-17 10:48:05 -0700135BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
136 UT_TESTF_ETH_BOOTDEV);
Simon Glassfb1451b2022-04-24 23:31:24 -0600137
Simon Glass66e3dce2023-01-17 10:48:09 -0700138/* Check bootdev_find_by_any() */
139static 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;
Simon Glass1736b4a2023-04-24 13:49:47 +1200183 ut_asserteq(-EPFNOSUPPORT, bootdev_find_by_any("fred0", &dev, &mflags));
184 ut_assert_nextline("Cannot find bootdev 'fred0' (err=-96)");
Simon Glass66e3dce2023-01-17 10:48:09 -0700185 ut_asserteq(123, mflags);
186 ut_assert_console_end();
187
188 return 0;
189}
190BOOTSTD_TEST(bootdev_test_any, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
191 UT_TESTF_ETH_BOOTDEV);
192
Simon Glassfb1451b2022-04-24 23:31:24 -0600193/* Check bootdev ordering with the bootdev-order property */
194static 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));
Simon Glass4b7cb052023-01-17 10:48:16 -0700209 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600210 ut_asserteq(2, iter.num_devs);
Simon Glassa950f282023-01-17 10:48:17 -0700211 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
212 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600213 bootflow_iter_uninit(&iter);
214
215 /* Use the environment variable to override it */
216 ut_assertok(env_set("boot_targets", "mmc1 mmc2"));
Simon Glass4b7cb052023-01-17 10:48:16 -0700217 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassa950f282023-01-17 10:48:17 -0700218 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600219 ut_asserteq(2, iter.num_devs);
Simon Glassa950f282023-01-17 10:48:17 -0700220 ut_asserteq_str("mmc1.bootdev", iter.dev_used[0]->name);
221 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600222 bootflow_iter_uninit(&iter);
223
Simon Glassa950f282023-01-17 10:48:17 -0700224 return 0;
225}
226BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
227
228/* Check default bootdev ordering */
229static int bootdev_test_order_default(struct unit_test_state *uts)
230{
231 struct bootflow_iter iter;
232 struct bootflow bflow;
233
Simon Glassfb1451b2022-04-24 23:31:24 -0600234 /*
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
Simon Glass4b7cb052023-01-17 10:48:16 -0700241 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassa950f282023-01-17 10:48:17 -0700242 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);
Simon Glassfb1451b2022-04-24 23:31:24 -0600245
Simon Glassa950f282023-01-17 10:48:17 -0700246 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600247 ut_asserteq(3, iter.num_devs);
Simon Glassa950f282023-01-17 10:48:17 -0700248 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600249 bootflow_iter_uninit(&iter);
250
251 return 0;
252}
Simon Glassa950f282023-01-17 10:48:17 -0700253BOOTSTD_TEST(bootdev_test_order_default, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassfb1451b2022-04-24 23:31:24 -0600254
255/* Check bootdev ordering with the uclass priority */
256static 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
Simon Glass04fb2b62023-01-17 10:47:37 -0700263 test_set_skip_delays(true);
264
Simon Glassa950f282023-01-17 10:48:17 -0700265 /* disable ethernet since the hunter will run dhcp */
266 test_set_eth_enable(false);
267
Simon Glassfb1451b2022-04-24 23:31:24 -0600268 /* 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();
Simon Glass4b7cb052023-01-17 10:48:16 -0700276 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassa950f282023-01-17 10:48:17 -0700277 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600278 ut_asserteq(6, iter.num_devs);
Simon Glassa950f282023-01-17 10:48:17 -0700279 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600280 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glassa950f282023-01-17 10:48:17 -0700281 iter.dev_used[3]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600282
Simon Glassa950f282023-01-17 10:48:17 -0700283 ut_assertok(bootdev_get_sibling_blk(iter.dev_used[3], &blk));
Simon Glassfb1451b2022-04-24 23:31:24 -0600284 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
285
286 /* adjust the priority of the first USB bootdev to the highest */
Simon Glassa950f282023-01-17 10:48:17 -0700287 ucp = dev_get_uclass_plat(iter.dev_used[3]);
288 ucp->prio = BOOTDEVP_1_PRE_SCAN;
Simon Glassfb1451b2022-04-24 23:31:24 -0600289
Simon Glassa950f282023-01-17 10:48:17 -0700290 /* try again but enable hunting, which brings in SCSI */
Simon Glassfb1451b2022-04-24 23:31:24 -0600291 bootflow_iter_uninit(&iter);
Simon Glass4f806f32023-02-22 12:17:03 -0700292 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWIF_HUNT,
Simon Glass4b7cb052023-01-17 10:48:16 -0700293 &bflow));
Simon Glassa950f282023-01-17 10:48:17 -0700294 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
295 ut_asserteq(7, iter.num_devs);
Simon Glassfb1451b2022-04-24 23:31:24 -0600296 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glassa950f282023-01-17 10:48:17 -0700297 iter.dev_used[0]->name);
298 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600299
300 return 0;
301}
302BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassbd90b092023-01-17 10:47:33 -0700303
304/* Check listing hunters */
305static int bootdev_test_hunter(struct unit_test_state *uts)
306{
307 struct bootstd_priv *std;
308
Simon Glass04fb2b62023-01-17 10:47:37 -0700309 test_set_skip_delays(true);
310
Simon Glassbd90b092023-01-17 10:47:33 -0700311 /* get access to the used hunters */
312 ut_assertok(bootstd_get_priv(&std));
313
314 console_record_reset_enable();
315 bootdev_list_hunters(std);
316 ut_assert_nextline("Prio Used Uclass Hunter");
317 ut_assert_nextlinen("----");
Simon Glasseacc2612023-01-17 10:48:08 -0700318 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glass18552d22023-01-17 10:48:13 -0700319 ut_assert_nextline(" 1 simple_bus (none)");
Simon Glasseacc2612023-01-17 10:48:08 -0700320 ut_assert_nextline(" 5 ide ide_bootdev");
321 ut_assert_nextline(" 2 mmc mmc_bootdev");
322 ut_assert_nextline(" 4 nvme nvme_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700323 ut_assert_nextline(" 4 qfw qfw_bootdev");
Simon Glasseacc2612023-01-17 10:48:08 -0700324 ut_assert_nextline(" 4 scsi scsi_bootdev");
325 ut_assert_nextline(" 4 spi_flash sf_bootdev");
326 ut_assert_nextline(" 5 usb usb_bootdev");
327 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700328 ut_assert_nextline("(total hunters: 10)");
Simon Glassbd90b092023-01-17 10:47:33 -0700329 ut_assert_console_end();
330
Simon Glass04fb2b62023-01-17 10:47:37 -0700331 ut_assertok(bootdev_hunt("usb1", false));
332 ut_assert_nextline(
333 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glassc7b63d52023-01-17 10:47:34 -0700334 ut_assert_console_end();
335
Simon Glass662cfa02023-01-28 15:00:28 -0700336 /* USB is 7th in the list, so bit 8 */
337 ut_asserteq(BIT(8), std->hunters_used);
Simon Glass04fb2b62023-01-17 10:47:37 -0700338
Simon Glassbd90b092023-01-17 10:47:33 -0700339 return 0;
340}
341BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
342
343/* Check 'bootdev hunt' command */
344static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
345{
346 struct bootstd_priv *std;
347
Simon Glass04fb2b62023-01-17 10:47:37 -0700348 test_set_skip_delays(true);
349
Simon Glassbd90b092023-01-17 10:47:33 -0700350 /* get access to the used hunters */
351 ut_assertok(bootstd_get_priv(&std));
352
353 console_record_reset_enable();
354 ut_assertok(run_command("bootdev hunt -l", 0));
355 ut_assert_nextline("Prio Used Uclass Hunter");
356 ut_assert_nextlinen("----");
Simon Glasseacc2612023-01-17 10:48:08 -0700357 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700358 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glasseacc2612023-01-17 10:48:08 -0700359 ut_assert_console_end();
360
361 /* Use the MMC hunter and see that it updates */
362 ut_assertok(run_command("bootdev hunt mmc", 0));
363 ut_assertok(run_command("bootdev hunt -l", 0));
364 ut_assert_skip_to_line(" 5 ide ide_bootdev");
365 ut_assert_nextline(" 2 * mmc mmc_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700366 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glassbd90b092023-01-17 10:47:33 -0700367 ut_assert_console_end();
368
369 /* Scan all hunters */
Simon Glasse4b69482023-01-17 10:48:10 -0700370 test_set_eth_enable(false);
Simon Glasseacc2612023-01-17 10:48:08 -0700371 test_set_skip_delays(true);
Simon Glassbd90b092023-01-17 10:47:33 -0700372 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass4146c822023-01-17 10:47:40 -0700373 ut_assert_nextline("Hunting with: ethernet");
Simon Glass18552d22023-01-17 10:48:13 -0700374
375 /* This is the extension feature which has no uclass at present */
376 ut_assert_nextline("Hunting with: simple_bus");
377 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass0d77f8f2023-01-17 10:47:46 -0700378 ut_assert_nextline("Hunting with: ide");
Simon Glasseacc2612023-01-17 10:48:08 -0700379
380 /* mmc hunter has already been used so should not run again */
381
Simon Glass758c7062023-01-17 10:47:47 -0700382 ut_assert_nextline("Hunting with: nvme");
Simon Glass662cfa02023-01-28 15:00:28 -0700383 ut_assert_nextline("Hunting with: qfw");
Simon Glass8f090b62023-01-17 10:47:45 -0700384 ut_assert_nextline("Hunting with: scsi");
385 ut_assert_nextline("scanning bus for devices...");
Simon Glass0c1f4a92023-01-17 10:48:03 -0700386 ut_assert_skip_to_line("Hunting with: spi_flash");
387 ut_assert_nextline("Hunting with: usb");
Simon Glass04fb2b62023-01-17 10:47:37 -0700388 ut_assert_nextline(
389 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass0c1f4a92023-01-17 10:48:03 -0700390 ut_assert_nextline("Hunting with: virtio");
Simon Glassbd90b092023-01-17 10:47:33 -0700391 ut_assert_console_end();
392
393 /* List available hunters */
394 ut_assertok(run_command("bootdev hunt -l", 0));
395 ut_assert_nextlinen("Prio");
396 ut_assert_nextlinen("----");
Simon Glasseacc2612023-01-17 10:48:08 -0700397 ut_assert_nextline(" 6 * ethernet eth_bootdev");
Simon Glass18552d22023-01-17 10:48:13 -0700398 ut_assert_nextline(" 1 * simple_bus (none)");
Simon Glasseacc2612023-01-17 10:48:08 -0700399 ut_assert_nextline(" 5 * ide ide_bootdev");
400 ut_assert_nextline(" 2 * mmc mmc_bootdev");
401 ut_assert_nextline(" 4 * nvme nvme_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700402 ut_assert_nextline(" 4 * qfw qfw_bootdev");
Simon Glasseacc2612023-01-17 10:48:08 -0700403 ut_assert_nextline(" 4 * scsi scsi_bootdev");
404 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
405 ut_assert_nextline(" 5 * usb usb_bootdev");
406 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700407 ut_assert_nextline("(total hunters: 10)");
Simon Glassbd90b092023-01-17 10:47:33 -0700408 ut_assert_console_end();
409
Simon Glass18552d22023-01-17 10:48:13 -0700410 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glassbd90b092023-01-17 10:47:33 -0700411
412 return 0;
413}
414BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
415 UT_TESTF_ETH_BOOTDEV);
Simon Glassf0e358f2023-01-17 10:47:42 -0700416
Simon Glass47aedc22023-01-17 10:48:14 -0700417/* Check searching for bootdevs using the hunters */
418static int bootdev_test_hunt_scan(struct unit_test_state *uts)
419{
420 struct bootflow_iter iter;
421 struct bootstd_priv *std;
422 struct bootflow bflow;
423
424 /* get access to the used hunters */
425 ut_assertok(bootstd_get_priv(&std));
426
427 ut_assertok(bootstd_test_drop_bootdev_order(uts));
Simon Glass4b7cb052023-01-17 10:48:16 -0700428 ut_assertok(bootflow_scan_first(NULL, NULL, &iter,
Simon Glass4f806f32023-02-22 12:17:03 -0700429 BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT |
430 BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glass47aedc22023-01-17 10:48:14 -0700431 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
432
433 return 0;
434}
435BOOTSTD_TEST(bootdev_test_hunt_scan, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
436
Simon Glassf0e358f2023-01-17 10:47:42 -0700437/* Check that only bootable partitions are processed */
438static int bootdev_test_bootable(struct unit_test_state *uts)
439{
440 struct bootflow_iter iter;
441 struct bootflow bflow;
442 struct udevice *blk;
443
444 memset(&iter, '\0', sizeof(iter));
445 memset(&bflow, '\0', sizeof(bflow));
446 iter.part = 0;
447 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
448 iter.dev = blk;
449 ut_assertok(device_find_next_child(&iter.dev));
450 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
451
452 /*
453 * initially we don't have any knowledge of which partitions are
454 * bootable, but mmc1 has two partitions, with the first one being
455 * bootable
456 */
457 iter.part = 2;
458 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
459 ut_asserteq(0, iter.first_bootable);
460
461 /* scan with part == 0 to get the partition info */
462 iter.part = 0;
463 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
464 ut_asserteq(1, iter.first_bootable);
465
466 /* now it will refuse to use non-bootable partitions */
467 iter.part = 2;
468 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
469
470 return 0;
471}
472BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass79a7d4a2023-01-17 10:48:07 -0700473
474/* Check hunting for bootdev of a particular priority */
475static int bootdev_test_hunt_prio(struct unit_test_state *uts)
476{
477 test_set_skip_delays(true);
478
479 console_record_reset_enable();
Simon Glasseacc2612023-01-17 10:48:08 -0700480 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass79a7d4a2023-01-17 10:48:07 -0700481 ut_assert_nextline("scanning bus for devices...");
482 ut_assert_skip_to_line(" Type: Hard Disk");
483 ut_assert_nextlinen(" Capacity:");
484 ut_assert_console_end();
485
486 /* now try a different priority, verbosely */
Simon Glasseacc2612023-01-17 10:48:08 -0700487 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass79a7d4a2023-01-17 10:48:07 -0700488 ut_assert_nextline("Hunting with: ide");
Simon Glass79a7d4a2023-01-17 10:48:07 -0700489 ut_assert_nextline("Hunting with: usb");
490 ut_assert_nextline(
491 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
492 ut_assert_console_end();
493
494 return 0;
495}
496BOOTSTD_TEST(bootdev_test_hunt_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass66e3dce2023-01-17 10:48:09 -0700497
498/* Check hunting for bootdevs with a particular label */
499static int bootdev_test_hunt_label(struct unit_test_state *uts)
500{
501 struct udevice *dev, *old;
502 struct bootstd_priv *std;
503 int mflags;
504
505 /* get access to the used hunters */
506 ut_assertok(bootstd_get_priv(&std));
507
508 /* scan an unknown uclass */
509 console_record_reset_enable();
510 old = (void *)&mflags; /* arbitrary pointer to check against dev */
511 dev = old;
512 mflags = 123;
Simon Glass1736b4a2023-04-24 13:49:47 +1200513 ut_asserteq(-EPFNOSUPPORT,
Simon Glass66e3dce2023-01-17 10:48:09 -0700514 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
Simon Glass66e3dce2023-01-17 10:48:09 -0700515 ut_asserteq_ptr(old, dev);
516 ut_asserteq(123, mflags);
517 ut_assert_console_end();
518 ut_asserteq(0, std->hunters_used);
519
520 /* scan an invalid mmc controllers */
521 ut_asserteq(-ENOENT,
522 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
523 ut_asserteq_ptr(old, dev);
524 ut_asserteq(123, mflags);
Simon Glass66e3dce2023-01-17 10:48:09 -0700525 ut_assert_console_end();
526
527 ut_assertok(bootstd_test_check_mmc_hunter(uts));
528
529 /* scan for a particular mmc controller */
530 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
531 ut_assertnonnull(dev);
532 ut_asserteq_str("mmc1.bootdev", dev->name);
533 ut_asserteq(0, mflags);
534 ut_assert_console_end();
535
536 /* scan all of usb */
537 test_set_skip_delays(true);
538 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
539 ut_assertnonnull(dev);
540 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
541 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
542 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
543 ut_assert_console_end();
544
545 return 0;
546}
547BOOTSTD_TEST(bootdev_test_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glasse4b69482023-01-17 10:48:10 -0700548
549/* Check iterating to the next label in a list */
550static int bootdev_test_next_label(struct unit_test_state *uts)
551{
552 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
553 struct bootflow_iter iter;
554 struct bootstd_priv *std;
555 struct bootflow bflow;
556 struct udevice *dev;
557 int mflags;
558
559 test_set_eth_enable(false);
560
561 /* get access to the used hunters */
562 ut_assertok(bootstd_get_priv(&std));
563
564 memset(&iter, '\0', sizeof(iter));
565 memset(&bflow, '\0', sizeof(bflow));
566 iter.part = 0;
567 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
568 iter.cur_label = -1;
569 iter.labels = labels;
570
571 dev = NULL;
572 mflags = 123;
573 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
574 console_record_reset_enable();
575 ut_assert_console_end();
576 ut_assertnonnull(dev);
577 ut_asserteq_str("mmc0.bootdev", dev->name);
578 ut_asserteq(0, mflags);
579
580 ut_assertok(bootstd_test_check_mmc_hunter(uts));
581
582 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
583 ut_assert_nextline("scanning bus for devices...");
584 ut_assert_skip_to_line(
585 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
586 ut_assert_console_end();
587 ut_assertnonnull(dev);
588 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
589 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
590
Simon Glass662cfa02023-01-28 15:00:28 -0700591 /* SCSI is 7th in the list, so bit 6 */
592 ut_asserteq(BIT(MMC_HUNTER) | BIT(6), std->hunters_used);
Simon Glasse4b69482023-01-17 10:48:10 -0700593
594 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
595 ut_assert_console_end();
596 ut_assertnonnull(dev);
597 ut_asserteq_str("eth@10002000.bootdev", dev->name);
598 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
599 mflags);
600
601 /* dhcp: Ethernet is first so bit 0 */
Simon Glass662cfa02023-01-28 15:00:28 -0700602 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glasse4b69482023-01-17 10:48:10 -0700603
604 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
605 ut_assert_console_end();
606 ut_assertnonnull(dev);
607 ut_asserteq_str("eth@10002000.bootdev", dev->name);
608 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
609 mflags);
610
611 /* pxe: Ethernet is first so bit 0 */
Simon Glass662cfa02023-01-28 15:00:28 -0700612 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glasse4b69482023-01-17 10:48:10 -0700613
614 mflags = 123;
615 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
616 ut_asserteq(123, mflags);
617 ut_assert_console_end();
618
619 /* no change */
Simon Glass662cfa02023-01-28 15:00:28 -0700620 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glasse4b69482023-01-17 10:48:10 -0700621
622 return 0;
623}
624BOOTSTD_TEST(bootdev_test_next_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
625 UT_TESTF_ETH_BOOTDEV | UT_TESTF_SF_BOOTDEV);
Simon Glass43e89a32023-01-17 10:48:11 -0700626
627
628/* Check iterating to the next prioirty in a list */
629static int bootdev_test_next_prio(struct unit_test_state *uts)
630{
631 struct bootflow_iter iter;
632 struct bootstd_priv *std;
633 struct bootflow bflow;
634 struct udevice *dev;
635 int ret;
636
Simon Glass91943ff2023-01-17 10:48:15 -0700637 test_set_eth_enable(false);
Simon Glass43e89a32023-01-17 10:48:11 -0700638 test_set_skip_delays(true);
639
640 /* get access to the used hunters */
641 ut_assertok(bootstd_get_priv(&std));
642
643 memset(&iter, '\0', sizeof(iter));
644 memset(&bflow, '\0', sizeof(bflow));
645 iter.part = 0;
646 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
647 iter.cur_prio = 0;
Simon Glass4f806f32023-02-22 12:17:03 -0700648 iter.flags = BOOTFLOWIF_SHOW;
Simon Glass43e89a32023-01-17 10:48:11 -0700649
650 dev = NULL;
651 console_record_reset_enable();
652 ut_assertok(bootdev_next_prio(&iter, &dev));
653 ut_assertnonnull(dev);
654 ut_asserteq_str("mmc2.bootdev", dev->name);
655
656 /* hunt flag not set, so this should not use any hunters */
657 ut_asserteq(0, std->hunters_used);
658 ut_assert_console_end();
659
660 /* now try again with hunting enabled */
Simon Glass4f806f32023-02-22 12:17:03 -0700661 iter.flags = BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT;
Simon Glass43e89a32023-01-17 10:48:11 -0700662 iter.cur_prio = 0;
663 iter.part = 0;
664
665 ut_assertok(bootdev_next_prio(&iter, &dev));
666 ut_asserteq_str("mmc2.bootdev", dev->name);
Simon Glass18552d22023-01-17 10:48:13 -0700667 ut_assert_nextline("Hunting with: simple_bus");
668 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass43e89a32023-01-17 10:48:11 -0700669 ut_assert_nextline("Hunting with: mmc");
670 ut_assert_console_end();
671
Simon Glass18552d22023-01-17 10:48:13 -0700672 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
Simon Glass43e89a32023-01-17 10:48:11 -0700673
674 ut_assertok(bootdev_next_prio(&iter, &dev));
675 ut_asserteq_str("mmc1.bootdev", dev->name);
676
677 ut_assertok(bootdev_next_prio(&iter, &dev));
678 ut_asserteq_str("mmc0.bootdev", dev->name);
679 ut_assert_console_end();
680
681 ut_assertok(bootdev_next_prio(&iter, &dev));
682 ut_asserteq_str("spi.bin@0.bootdev", dev->name);
683 ut_assert_skip_to_line("Hunting with: spi_flash");
684
685 /*
686 * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it
687 * starts looking at the devices, so we se virtio as well
688 */
689 ut_assert_nextline("Hunting with: virtio");
690 ut_assert_nextlinen("SF: Detected m25p16");
691
692 ut_assertok(bootdev_next_prio(&iter, &dev));
693 ut_asserteq_str("spi.bin@1.bootdev", dev->name);
694 ut_assert_nextlinen("SF: Detected m25p16");
695 ut_assert_console_end();
696
697 /* keep going until there are no more bootdevs */
698 do {
699 ret = bootdev_next_prio(&iter, &dev);
700 } while (!ret);
701 ut_asserteq(-ENODEV, ret);
702 ut_assertnull(dev);
Simon Glass18552d22023-01-17 10:48:13 -0700703 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass43e89a32023-01-17 10:48:11 -0700704
705 ut_assert_skip_to_line("Hunting with: ethernet");
706 ut_assert_console_end();
707
708 return 0;
709}
710BOOTSTD_TEST(bootdev_test_next_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
711 UT_TESTF_SF_BOOTDEV);