blob: c5f14a7a1323257328bccb2bccc06ff421383f57 [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 Glass9fea3a72023-05-05 20:03:03 -060022extern bool usb_started;
Linus Walleij0c1413f2023-01-23 23:07:21 +010023#else
Simon Glass9fea3a72023-05-05 20:03:03 -060024#include <usb.h>
Linus Walleij0c1413f2023-01-23 23:07:21 +010025#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 Glasse824d0d2023-09-23 14:50:15 -0600224 /* Make sure it scans a bootdevs in each target */
225 ut_assertok(env_set("boot_targets", "mmc spi"));
226 ut_asserteq(0, bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
227 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
228 ut_asserteq(3, iter.num_devs);
229 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
230 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
231 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
232 bootflow_iter_uninit(&iter);
233
Simon Glassa950f282023-01-17 10:48:17 -0700234 return 0;
235}
236BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
237
238/* Check default bootdev ordering */
239static int bootdev_test_order_default(struct unit_test_state *uts)
240{
241 struct bootflow_iter iter;
242 struct bootflow bflow;
243
Simon Glassfb1451b2022-04-24 23:31:24 -0600244 /*
245 * Now drop both orderings, to check the default (prioriy/sequence)
246 * ordering
247 */
248 ut_assertok(env_set("boot_targets", NULL));
249 ut_assertok(bootstd_test_drop_bootdev_order(uts));
250
Simon Glass4b7cb052023-01-17 10:48:16 -0700251 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassa950f282023-01-17 10:48:17 -0700252 ut_asserteq(2, iter.num_devs);
253 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
254 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600255
Simon Glassa950f282023-01-17 10:48:17 -0700256 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600257 ut_asserteq(3, iter.num_devs);
Simon Glassa950f282023-01-17 10:48:17 -0700258 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600259 bootflow_iter_uninit(&iter);
260
261 return 0;
262}
Simon Glassa950f282023-01-17 10:48:17 -0700263BOOTSTD_TEST(bootdev_test_order_default, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassfb1451b2022-04-24 23:31:24 -0600264
265/* Check bootdev ordering with the uclass priority */
266static int bootdev_test_prio(struct unit_test_state *uts)
267{
268 struct bootdev_uc_plat *ucp;
269 struct bootflow_iter iter;
270 struct bootflow bflow;
271 struct udevice *blk;
272
Simon Glass04fb2b62023-01-17 10:47:37 -0700273 test_set_skip_delays(true);
274
Simon Glassa950f282023-01-17 10:48:17 -0700275 /* disable ethernet since the hunter will run dhcp */
276 test_set_eth_enable(false);
277
Simon Glassfb1451b2022-04-24 23:31:24 -0600278 /* Start up USB which gives us three additional bootdevs */
279 usb_started = false;
280 ut_assertok(run_command("usb start", 0));
281
282 ut_assertok(bootstd_test_drop_bootdev_order(uts));
283
284 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
285 console_record_reset_enable();
Simon Glass4b7cb052023-01-17 10:48:16 -0700286 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassa950f282023-01-17 10:48:17 -0700287 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600288 ut_asserteq(6, iter.num_devs);
Simon Glassa950f282023-01-17 10:48:17 -0700289 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600290 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glassa950f282023-01-17 10:48:17 -0700291 iter.dev_used[3]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600292
Simon Glassa950f282023-01-17 10:48:17 -0700293 ut_assertok(bootdev_get_sibling_blk(iter.dev_used[3], &blk));
Simon Glassfb1451b2022-04-24 23:31:24 -0600294 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
295
296 /* adjust the priority of the first USB bootdev to the highest */
Simon Glassa950f282023-01-17 10:48:17 -0700297 ucp = dev_get_uclass_plat(iter.dev_used[3]);
298 ucp->prio = BOOTDEVP_1_PRE_SCAN;
Simon Glassfb1451b2022-04-24 23:31:24 -0600299
Simon Glassa950f282023-01-17 10:48:17 -0700300 /* try again but enable hunting, which brings in SCSI */
Simon Glassfb1451b2022-04-24 23:31:24 -0600301 bootflow_iter_uninit(&iter);
Simon Glass4f806f32023-02-22 12:17:03 -0700302 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWIF_HUNT,
Simon Glass4b7cb052023-01-17 10:48:16 -0700303 &bflow));
Simon Glassa950f282023-01-17 10:48:17 -0700304 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
305 ut_asserteq(7, iter.num_devs);
Simon Glassfb1451b2022-04-24 23:31:24 -0600306 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glassa950f282023-01-17 10:48:17 -0700307 iter.dev_used[0]->name);
308 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600309
310 return 0;
311}
312BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassbd90b092023-01-17 10:47:33 -0700313
314/* Check listing hunters */
315static int bootdev_test_hunter(struct unit_test_state *uts)
316{
317 struct bootstd_priv *std;
318
Simon Glass8c29b732023-05-05 20:03:04 -0600319 usb_started = false;
Simon Glass04fb2b62023-01-17 10:47:37 -0700320 test_set_skip_delays(true);
321
Simon Glassbd90b092023-01-17 10:47:33 -0700322 /* get access to the used hunters */
323 ut_assertok(bootstd_get_priv(&std));
324
325 console_record_reset_enable();
326 bootdev_list_hunters(std);
327 ut_assert_nextline("Prio Used Uclass Hunter");
328 ut_assert_nextlinen("----");
Simon Glasseacc2612023-01-17 10:48:08 -0700329 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glass18552d22023-01-17 10:48:13 -0700330 ut_assert_nextline(" 1 simple_bus (none)");
Simon Glasseacc2612023-01-17 10:48:08 -0700331 ut_assert_nextline(" 5 ide ide_bootdev");
332 ut_assert_nextline(" 2 mmc mmc_bootdev");
333 ut_assert_nextline(" 4 nvme nvme_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700334 ut_assert_nextline(" 4 qfw qfw_bootdev");
Simon Glasseacc2612023-01-17 10:48:08 -0700335 ut_assert_nextline(" 4 scsi scsi_bootdev");
336 ut_assert_nextline(" 4 spi_flash sf_bootdev");
337 ut_assert_nextline(" 5 usb usb_bootdev");
338 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700339 ut_assert_nextline("(total hunters: 10)");
Simon Glassbd90b092023-01-17 10:47:33 -0700340 ut_assert_console_end();
341
Simon Glass04fb2b62023-01-17 10:47:37 -0700342 ut_assertok(bootdev_hunt("usb1", false));
343 ut_assert_nextline(
344 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glassc7b63d52023-01-17 10:47:34 -0700345 ut_assert_console_end();
346
Simon Glass662cfa02023-01-28 15:00:28 -0700347 /* USB is 7th in the list, so bit 8 */
348 ut_asserteq(BIT(8), std->hunters_used);
Simon Glass04fb2b62023-01-17 10:47:37 -0700349
Simon Glassbd90b092023-01-17 10:47:33 -0700350 return 0;
351}
352BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
353
354/* Check 'bootdev hunt' command */
355static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
356{
357 struct bootstd_priv *std;
358
Simon Glass04fb2b62023-01-17 10:47:37 -0700359 test_set_skip_delays(true);
Simon Glass8c29b732023-05-05 20:03:04 -0600360 usb_started = false;
Simon Glass04fb2b62023-01-17 10:47:37 -0700361
Simon Glassbd90b092023-01-17 10:47:33 -0700362 /* get access to the used hunters */
363 ut_assertok(bootstd_get_priv(&std));
364
365 console_record_reset_enable();
366 ut_assertok(run_command("bootdev hunt -l", 0));
367 ut_assert_nextline("Prio Used Uclass Hunter");
368 ut_assert_nextlinen("----");
Simon Glasseacc2612023-01-17 10:48:08 -0700369 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700370 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glasseacc2612023-01-17 10:48:08 -0700371 ut_assert_console_end();
372
373 /* Use the MMC hunter and see that it updates */
374 ut_assertok(run_command("bootdev hunt mmc", 0));
375 ut_assertok(run_command("bootdev hunt -l", 0));
376 ut_assert_skip_to_line(" 5 ide ide_bootdev");
377 ut_assert_nextline(" 2 * mmc mmc_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700378 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glassbd90b092023-01-17 10:47:33 -0700379 ut_assert_console_end();
380
381 /* Scan all hunters */
Simon Glasse4b69482023-01-17 10:48:10 -0700382 test_set_eth_enable(false);
Simon Glasseacc2612023-01-17 10:48:08 -0700383 test_set_skip_delays(true);
Simon Glassbd90b092023-01-17 10:47:33 -0700384 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass4146c822023-01-17 10:47:40 -0700385 ut_assert_nextline("Hunting with: ethernet");
Simon Glass18552d22023-01-17 10:48:13 -0700386
387 /* This is the extension feature which has no uclass at present */
388 ut_assert_nextline("Hunting with: simple_bus");
389 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass0d77f8f2023-01-17 10:47:46 -0700390 ut_assert_nextline("Hunting with: ide");
Simon Glasseacc2612023-01-17 10:48:08 -0700391
392 /* mmc hunter has already been used so should not run again */
393
Simon Glass758c7062023-01-17 10:47:47 -0700394 ut_assert_nextline("Hunting with: nvme");
Simon Glass662cfa02023-01-28 15:00:28 -0700395 ut_assert_nextline("Hunting with: qfw");
Simon Glass8f090b62023-01-17 10:47:45 -0700396 ut_assert_nextline("Hunting with: scsi");
397 ut_assert_nextline("scanning bus for devices...");
Simon Glass0c1f4a92023-01-17 10:48:03 -0700398 ut_assert_skip_to_line("Hunting with: spi_flash");
399 ut_assert_nextline("Hunting with: usb");
Simon Glass04fb2b62023-01-17 10:47:37 -0700400 ut_assert_nextline(
401 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass0c1f4a92023-01-17 10:48:03 -0700402 ut_assert_nextline("Hunting with: virtio");
Simon Glassbd90b092023-01-17 10:47:33 -0700403 ut_assert_console_end();
404
405 /* List available hunters */
406 ut_assertok(run_command("bootdev hunt -l", 0));
407 ut_assert_nextlinen("Prio");
408 ut_assert_nextlinen("----");
Simon Glasseacc2612023-01-17 10:48:08 -0700409 ut_assert_nextline(" 6 * ethernet eth_bootdev");
Simon Glass18552d22023-01-17 10:48:13 -0700410 ut_assert_nextline(" 1 * simple_bus (none)");
Simon Glasseacc2612023-01-17 10:48:08 -0700411 ut_assert_nextline(" 5 * ide ide_bootdev");
412 ut_assert_nextline(" 2 * mmc mmc_bootdev");
413 ut_assert_nextline(" 4 * nvme nvme_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700414 ut_assert_nextline(" 4 * qfw qfw_bootdev");
Simon Glasseacc2612023-01-17 10:48:08 -0700415 ut_assert_nextline(" 4 * scsi scsi_bootdev");
416 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
417 ut_assert_nextline(" 5 * usb usb_bootdev");
418 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glass662cfa02023-01-28 15:00:28 -0700419 ut_assert_nextline("(total hunters: 10)");
Simon Glassbd90b092023-01-17 10:47:33 -0700420 ut_assert_console_end();
421
Simon Glass18552d22023-01-17 10:48:13 -0700422 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glassbd90b092023-01-17 10:47:33 -0700423
424 return 0;
425}
426BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
427 UT_TESTF_ETH_BOOTDEV);
Simon Glassf0e358f2023-01-17 10:47:42 -0700428
Simon Glass47aedc22023-01-17 10:48:14 -0700429/* Check searching for bootdevs using the hunters */
430static int bootdev_test_hunt_scan(struct unit_test_state *uts)
431{
432 struct bootflow_iter iter;
433 struct bootstd_priv *std;
434 struct bootflow bflow;
435
436 /* get access to the used hunters */
437 ut_assertok(bootstd_get_priv(&std));
438
439 ut_assertok(bootstd_test_drop_bootdev_order(uts));
Simon Glass4b7cb052023-01-17 10:48:16 -0700440 ut_assertok(bootflow_scan_first(NULL, NULL, &iter,
Simon Glass4f806f32023-02-22 12:17:03 -0700441 BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT |
442 BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glass47aedc22023-01-17 10:48:14 -0700443 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
444
445 return 0;
446}
447BOOTSTD_TEST(bootdev_test_hunt_scan, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
448
Simon Glassf0e358f2023-01-17 10:47:42 -0700449/* Check that only bootable partitions are processed */
450static int bootdev_test_bootable(struct unit_test_state *uts)
451{
452 struct bootflow_iter iter;
453 struct bootflow bflow;
454 struct udevice *blk;
455
456 memset(&iter, '\0', sizeof(iter));
457 memset(&bflow, '\0', sizeof(bflow));
458 iter.part = 0;
459 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
460 iter.dev = blk;
461 ut_assertok(device_find_next_child(&iter.dev));
462 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
463
464 /*
465 * initially we don't have any knowledge of which partitions are
466 * bootable, but mmc1 has two partitions, with the first one being
467 * bootable
468 */
469 iter.part = 2;
470 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
471 ut_asserteq(0, iter.first_bootable);
472
473 /* scan with part == 0 to get the partition info */
474 iter.part = 0;
475 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
476 ut_asserteq(1, iter.first_bootable);
477
478 /* now it will refuse to use non-bootable partitions */
479 iter.part = 2;
480 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
481
482 return 0;
483}
484BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass79a7d4a2023-01-17 10:48:07 -0700485
486/* Check hunting for bootdev of a particular priority */
487static int bootdev_test_hunt_prio(struct unit_test_state *uts)
488{
Simon Glass8c29b732023-05-05 20:03:04 -0600489 usb_started = false;
Simon Glass79a7d4a2023-01-17 10:48:07 -0700490 test_set_skip_delays(true);
491
492 console_record_reset_enable();
Simon Glasseacc2612023-01-17 10:48:08 -0700493 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass79a7d4a2023-01-17 10:48:07 -0700494 ut_assert_nextline("scanning bus for devices...");
495 ut_assert_skip_to_line(" Type: Hard Disk");
496 ut_assert_nextlinen(" Capacity:");
497 ut_assert_console_end();
498
499 /* now try a different priority, verbosely */
Simon Glasseacc2612023-01-17 10:48:08 -0700500 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass79a7d4a2023-01-17 10:48:07 -0700501 ut_assert_nextline("Hunting with: ide");
Simon Glass79a7d4a2023-01-17 10:48:07 -0700502 ut_assert_nextline("Hunting with: usb");
503 ut_assert_nextline(
504 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
505 ut_assert_console_end();
506
507 return 0;
508}
509BOOTSTD_TEST(bootdev_test_hunt_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass66e3dce2023-01-17 10:48:09 -0700510
511/* Check hunting for bootdevs with a particular label */
512static int bootdev_test_hunt_label(struct unit_test_state *uts)
513{
514 struct udevice *dev, *old;
515 struct bootstd_priv *std;
516 int mflags;
517
Simon Glass8c29b732023-05-05 20:03:04 -0600518 usb_started = false;
519
Simon Glass66e3dce2023-01-17 10:48:09 -0700520 /* get access to the used hunters */
521 ut_assertok(bootstd_get_priv(&std));
522
523 /* scan an unknown uclass */
524 console_record_reset_enable();
525 old = (void *)&mflags; /* arbitrary pointer to check against dev */
526 dev = old;
527 mflags = 123;
Simon Glass1736b4a2023-04-24 13:49:47 +1200528 ut_asserteq(-EPFNOSUPPORT,
Simon Glass66e3dce2023-01-17 10:48:09 -0700529 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
Simon Glass66e3dce2023-01-17 10:48:09 -0700530 ut_asserteq_ptr(old, dev);
531 ut_asserteq(123, mflags);
532 ut_assert_console_end();
533 ut_asserteq(0, std->hunters_used);
534
535 /* scan an invalid mmc controllers */
536 ut_asserteq(-ENOENT,
537 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
538 ut_asserteq_ptr(old, dev);
539 ut_asserteq(123, mflags);
Simon Glass66e3dce2023-01-17 10:48:09 -0700540 ut_assert_console_end();
541
542 ut_assertok(bootstd_test_check_mmc_hunter(uts));
543
544 /* scan for a particular mmc controller */
545 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
546 ut_assertnonnull(dev);
547 ut_asserteq_str("mmc1.bootdev", dev->name);
548 ut_asserteq(0, mflags);
549 ut_assert_console_end();
550
551 /* scan all of usb */
552 test_set_skip_delays(true);
553 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
554 ut_assertnonnull(dev);
555 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
556 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
557 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
558 ut_assert_console_end();
559
560 return 0;
561}
562BOOTSTD_TEST(bootdev_test_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glasse4b69482023-01-17 10:48:10 -0700563
564/* Check iterating to the next label in a list */
565static int bootdev_test_next_label(struct unit_test_state *uts)
566{
567 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
568 struct bootflow_iter iter;
569 struct bootstd_priv *std;
570 struct bootflow bflow;
571 struct udevice *dev;
572 int mflags;
573
574 test_set_eth_enable(false);
575
576 /* get access to the used hunters */
577 ut_assertok(bootstd_get_priv(&std));
578
579 memset(&iter, '\0', sizeof(iter));
580 memset(&bflow, '\0', sizeof(bflow));
581 iter.part = 0;
582 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
583 iter.cur_label = -1;
584 iter.labels = labels;
585
586 dev = NULL;
587 mflags = 123;
588 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
589 console_record_reset_enable();
590 ut_assert_console_end();
591 ut_assertnonnull(dev);
592 ut_asserteq_str("mmc0.bootdev", dev->name);
593 ut_asserteq(0, mflags);
594
595 ut_assertok(bootstd_test_check_mmc_hunter(uts));
596
597 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
598 ut_assert_nextline("scanning bus for devices...");
599 ut_assert_skip_to_line(
600 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
601 ut_assert_console_end();
602 ut_assertnonnull(dev);
603 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
604 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
605
Simon Glass662cfa02023-01-28 15:00:28 -0700606 /* SCSI is 7th in the list, so bit 6 */
607 ut_asserteq(BIT(MMC_HUNTER) | BIT(6), std->hunters_used);
Simon Glasse4b69482023-01-17 10:48:10 -0700608
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_DHCP_ONLY,
614 mflags);
615
616 /* dhcp: Ethernet is first so bit 0 */
Simon Glass662cfa02023-01-28 15:00:28 -0700617 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glasse4b69482023-01-17 10:48:10 -0700618
619 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
620 ut_assert_console_end();
621 ut_assertnonnull(dev);
622 ut_asserteq_str("eth@10002000.bootdev", dev->name);
623 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
624 mflags);
625
626 /* pxe: Ethernet is first so bit 0 */
Simon Glass662cfa02023-01-28 15:00:28 -0700627 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glasse4b69482023-01-17 10:48:10 -0700628
629 mflags = 123;
630 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
631 ut_asserteq(123, mflags);
632 ut_assert_console_end();
633
634 /* no change */
Simon Glass662cfa02023-01-28 15:00:28 -0700635 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glasse4b69482023-01-17 10:48:10 -0700636
637 return 0;
638}
639BOOTSTD_TEST(bootdev_test_next_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
640 UT_TESTF_ETH_BOOTDEV | UT_TESTF_SF_BOOTDEV);
Simon Glass43e89a32023-01-17 10:48:11 -0700641
642
643/* Check iterating to the next prioirty in a list */
644static int bootdev_test_next_prio(struct unit_test_state *uts)
645{
646 struct bootflow_iter iter;
647 struct bootstd_priv *std;
648 struct bootflow bflow;
649 struct udevice *dev;
650 int ret;
651
Simon Glass91943ff2023-01-17 10:48:15 -0700652 test_set_eth_enable(false);
Simon Glass43e89a32023-01-17 10:48:11 -0700653 test_set_skip_delays(true);
654
655 /* get access to the used hunters */
656 ut_assertok(bootstd_get_priv(&std));
657
658 memset(&iter, '\0', sizeof(iter));
659 memset(&bflow, '\0', sizeof(bflow));
660 iter.part = 0;
661 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
662 iter.cur_prio = 0;
Simon Glass4f806f32023-02-22 12:17:03 -0700663 iter.flags = BOOTFLOWIF_SHOW;
Simon Glass43e89a32023-01-17 10:48:11 -0700664
665 dev = NULL;
666 console_record_reset_enable();
667 ut_assertok(bootdev_next_prio(&iter, &dev));
668 ut_assertnonnull(dev);
669 ut_asserteq_str("mmc2.bootdev", dev->name);
670
671 /* hunt flag not set, so this should not use any hunters */
672 ut_asserteq(0, std->hunters_used);
673 ut_assert_console_end();
674
675 /* now try again with hunting enabled */
Simon Glass4f806f32023-02-22 12:17:03 -0700676 iter.flags = BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT;
Simon Glass43e89a32023-01-17 10:48:11 -0700677 iter.cur_prio = 0;
678 iter.part = 0;
679
680 ut_assertok(bootdev_next_prio(&iter, &dev));
681 ut_asserteq_str("mmc2.bootdev", dev->name);
Simon Glass18552d22023-01-17 10:48:13 -0700682 ut_assert_nextline("Hunting with: simple_bus");
683 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass43e89a32023-01-17 10:48:11 -0700684 ut_assert_nextline("Hunting with: mmc");
685 ut_assert_console_end();
686
Simon Glass18552d22023-01-17 10:48:13 -0700687 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
Simon Glass43e89a32023-01-17 10:48:11 -0700688
689 ut_assertok(bootdev_next_prio(&iter, &dev));
690 ut_asserteq_str("mmc1.bootdev", dev->name);
691
692 ut_assertok(bootdev_next_prio(&iter, &dev));
693 ut_asserteq_str("mmc0.bootdev", dev->name);
694 ut_assert_console_end();
695
696 ut_assertok(bootdev_next_prio(&iter, &dev));
697 ut_asserteq_str("spi.bin@0.bootdev", dev->name);
698 ut_assert_skip_to_line("Hunting with: spi_flash");
699
700 /*
701 * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it
702 * starts looking at the devices, so we se virtio as well
703 */
704 ut_assert_nextline("Hunting with: virtio");
705 ut_assert_nextlinen("SF: Detected m25p16");
706
707 ut_assertok(bootdev_next_prio(&iter, &dev));
708 ut_asserteq_str("spi.bin@1.bootdev", dev->name);
709 ut_assert_nextlinen("SF: Detected m25p16");
710 ut_assert_console_end();
711
712 /* keep going until there are no more bootdevs */
713 do {
714 ret = bootdev_next_prio(&iter, &dev);
715 } while (!ret);
716 ut_asserteq(-ENODEV, ret);
717 ut_assertnull(dev);
Simon Glass18552d22023-01-17 10:48:13 -0700718 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass43e89a32023-01-17 10:48:11 -0700719
720 ut_assert_skip_to_line("Hunting with: ethernet");
721 ut_assert_console_end();
722
723 return 0;
724}
725BOOTSTD_TEST(bootdev_test_next_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
726 UT_TESTF_SF_BOOTDEV);