blob: 6b29213416db931b71a5a55fb590692a40704a24 [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 */
Linus Walleije3045d12023-01-23 23:07:21 +010021#if defined(CONFIG_USB_HOST) || defined(CONFIG_USB_GADGET)
Simon Glass2510aab2023-05-05 20:03:03 -060022extern bool usb_started;
Linus Walleije3045d12023-01-23 23:07:21 +010023#else
Simon Glass2510aab2023-05-05 20:03:03 -060024#include <usb.h>
Linus Walleije3045d12023-01-23 23:07:21 +010025#endif
Simon Glassb255efc2022-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 Glasse22fe922023-01-17 10:48:05 -0700109 int mflags = 0;
Simon Glassb255efc2022-04-24 23:31:24 -0600110
Simon Glasse22fe922023-01-17 10:48:05 -0700111 ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600112 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
Simon Glasse22fe922023-01-17 10:48:05 -0700113 ut_asserteq(0, mflags);
Simon Glassb255efc2022-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 Glasse22fe922023-01-17 10:48:05 -0700118 /* Check method flags */
119 ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700120 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
121 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700122 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700123 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
124 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700125
Simon Glassb255efc2022-04-24 23:31:24 -0600126 /* Check invalid uclass */
Simon Glass4108c6b2023-04-24 13:49:47 +1200127 ut_asserteq(-EPFNOSUPPORT,
128 bootdev_find_by_label("fred0", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600129
130 /* Check unknown sequence number */
Simon Glasse22fe922023-01-17 10:48:05 -0700131 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600132
133 return 0;
134}
Simon Glasse22fe922023-01-17 10:48:05 -0700135BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
136 UT_TESTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600137
Simon Glassde567b12023-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 Glass4108c6b2023-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 Glassde567b12023-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 Glassb255efc2022-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 Glass5d3d44f2023-01-17 10:48:16 -0700209 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600210 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-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 Glassb255efc2022-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 Glass5d3d44f2023-01-17 10:48:16 -0700217 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700218 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600219 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-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 Glassb255efc2022-04-24 23:31:24 -0600222 bootflow_iter_uninit(&iter);
223
Simon Glasscb2a5cd2023-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 Glassb255efc2022-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 Glass5d3d44f2023-01-17 10:48:16 -0700241 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-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 Glassb255efc2022-04-24 23:31:24 -0600245
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700246 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600247 ut_asserteq(3, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700248 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600249 bootflow_iter_uninit(&iter);
250
251 return 0;
252}
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700253BOOTSTD_TEST(bootdev_test_order_default, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassb255efc2022-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 Glassf3cb89f2023-01-17 10:47:37 -0700263 test_set_skip_delays(true);
264
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700265 /* disable ethernet since the hunter will run dhcp */
266 test_set_eth_enable(false);
267
Simon Glassb255efc2022-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 Glass5d3d44f2023-01-17 10:48:16 -0700276 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700277 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600278 ut_asserteq(6, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700279 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600280 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700281 iter.dev_used[3]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600282
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700283 ut_assertok(bootdev_get_sibling_blk(iter.dev_used[3], &blk));
Simon Glassb255efc2022-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 Glasscb2a5cd2023-01-17 10:48:17 -0700287 ucp = dev_get_uclass_plat(iter.dev_used[3]);
288 ucp->prio = BOOTDEVP_1_PRE_SCAN;
Simon Glassb255efc2022-04-24 23:31:24 -0600289
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700290 /* try again but enable hunting, which brings in SCSI */
Simon Glassb255efc2022-04-24 23:31:24 -0600291 bootflow_iter_uninit(&iter);
Simon Glass99e68182023-02-22 12:17:03 -0700292 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWIF_HUNT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700293 &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700294 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
295 ut_asserteq(7, iter.num_devs);
Simon Glassb255efc2022-04-24 23:31:24 -0600296 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700297 iter.dev_used[0]->name);
298 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600299
300 return 0;
301}
302BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass5acb97a2023-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 Glass4bb7c162023-05-05 20:03:04 -0600309 usb_started = false;
Simon Glassf3cb89f2023-01-17 10:47:37 -0700310 test_set_skip_delays(true);
311
Simon Glass5acb97a2023-01-17 10:47:33 -0700312 /* get access to the used hunters */
313 ut_assertok(bootstd_get_priv(&std));
314
315 console_record_reset_enable();
316 bootdev_list_hunters(std);
317 ut_assert_nextline("Prio Used Uclass Hunter");
318 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700319 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700320 ut_assert_nextline(" 1 simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700321 ut_assert_nextline(" 5 ide ide_bootdev");
322 ut_assert_nextline(" 2 mmc mmc_bootdev");
323 ut_assert_nextline(" 4 nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700324 ut_assert_nextline(" 4 qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700325 ut_assert_nextline(" 4 scsi scsi_bootdev");
326 ut_assert_nextline(" 4 spi_flash sf_bootdev");
327 ut_assert_nextline(" 5 usb usb_bootdev");
328 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700329 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700330 ut_assert_console_end();
331
Simon Glassf3cb89f2023-01-17 10:47:37 -0700332 ut_assertok(bootdev_hunt("usb1", false));
333 ut_assert_nextline(
334 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass1248d2b2023-01-17 10:47:34 -0700335 ut_assert_console_end();
336
Simon Glassdeffa692023-01-28 15:00:28 -0700337 /* USB is 7th in the list, so bit 8 */
338 ut_asserteq(BIT(8), std->hunters_used);
Simon Glassf3cb89f2023-01-17 10:47:37 -0700339
Simon Glass5acb97a2023-01-17 10:47:33 -0700340 return 0;
341}
342BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
343
344/* Check 'bootdev hunt' command */
345static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
346{
347 struct bootstd_priv *std;
348
Simon Glassf3cb89f2023-01-17 10:47:37 -0700349 test_set_skip_delays(true);
Simon Glass4bb7c162023-05-05 20:03:04 -0600350 usb_started = false;
Simon Glassf3cb89f2023-01-17 10:47:37 -0700351
Simon Glass5acb97a2023-01-17 10:47:33 -0700352 /* get access to the used hunters */
353 ut_assertok(bootstd_get_priv(&std));
354
355 console_record_reset_enable();
356 ut_assertok(run_command("bootdev hunt -l", 0));
357 ut_assert_nextline("Prio Used Uclass Hunter");
358 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700359 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700360 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700361 ut_assert_console_end();
362
Simon Glass7e1f6a42023-01-17 10:48:08 -0700363 /* Use the MMC hunter and see that it updates */
364 ut_assertok(run_command("bootdev hunt mmc", 0));
365 ut_assertok(run_command("bootdev hunt -l", 0));
366 ut_assert_skip_to_line(" 5 ide ide_bootdev");
367 ut_assert_nextline(" 2 * mmc mmc_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700368 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700369 ut_assert_console_end();
370
Simon Glass5acb97a2023-01-17 10:47:33 -0700371 /* Scan all hunters */
Simon Glassac06b26a2023-01-17 10:48:10 -0700372 test_set_eth_enable(false);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700373 test_set_skip_delays(true);
Simon Glass5acb97a2023-01-17 10:47:33 -0700374 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700375 ut_assert_nextline("Hunting with: ethernet");
Simon Glasse4cf1062023-01-17 10:48:13 -0700376
377 /* This is the extension feature which has no uclass at present */
378 ut_assert_nextline("Hunting with: simple_bus");
379 ut_assert_nextline("Found 2 extension board(s).");
Simon Glassbe7c6662023-01-17 10:47:46 -0700380 ut_assert_nextline("Hunting with: ide");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700381
382 /* mmc hunter has already been used so should not run again */
383
Simon Glass08382872023-01-17 10:47:47 -0700384 ut_assert_nextline("Hunting with: nvme");
Simon Glassdeffa692023-01-28 15:00:28 -0700385 ut_assert_nextline("Hunting with: qfw");
Simon Glass9985e9c2023-01-17 10:47:45 -0700386 ut_assert_nextline("Hunting with: scsi");
387 ut_assert_nextline("scanning bus for devices...");
Simon Glass215be682023-01-17 10:48:03 -0700388 ut_assert_skip_to_line("Hunting with: spi_flash");
389 ut_assert_nextline("Hunting with: usb");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700390 ut_assert_nextline(
391 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass215be682023-01-17 10:48:03 -0700392 ut_assert_nextline("Hunting with: virtio");
Simon Glass5acb97a2023-01-17 10:47:33 -0700393 ut_assert_console_end();
394
395 /* List available hunters */
396 ut_assertok(run_command("bootdev hunt -l", 0));
397 ut_assert_nextlinen("Prio");
398 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700399 ut_assert_nextline(" 6 * ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700400 ut_assert_nextline(" 1 * simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700401 ut_assert_nextline(" 5 * ide ide_bootdev");
402 ut_assert_nextline(" 2 * mmc mmc_bootdev");
403 ut_assert_nextline(" 4 * nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700404 ut_assert_nextline(" 4 * qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700405 ut_assert_nextline(" 4 * scsi scsi_bootdev");
406 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
407 ut_assert_nextline(" 5 * usb usb_bootdev");
408 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700409 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700410 ut_assert_console_end();
411
Simon Glasse4cf1062023-01-17 10:48:13 -0700412 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass5acb97a2023-01-17 10:47:33 -0700413
414 return 0;
415}
416BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
417 UT_TESTF_ETH_BOOTDEV);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700418
Simon Glass484e4072023-01-17 10:48:14 -0700419/* Check searching for bootdevs using the hunters */
420static int bootdev_test_hunt_scan(struct unit_test_state *uts)
421{
422 struct bootflow_iter iter;
423 struct bootstd_priv *std;
424 struct bootflow bflow;
425
426 /* get access to the used hunters */
427 ut_assertok(bootstd_get_priv(&std));
428
429 ut_assertok(bootstd_test_drop_bootdev_order(uts));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700430 ut_assertok(bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700431 BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT |
432 BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glass484e4072023-01-17 10:48:14 -0700433 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
434
435 return 0;
436}
437BOOTSTD_TEST(bootdev_test_hunt_scan, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
438
Simon Glass64cbc5c2023-01-17 10:47:42 -0700439/* Check that only bootable partitions are processed */
440static int bootdev_test_bootable(struct unit_test_state *uts)
441{
442 struct bootflow_iter iter;
443 struct bootflow bflow;
444 struct udevice *blk;
445
446 memset(&iter, '\0', sizeof(iter));
447 memset(&bflow, '\0', sizeof(bflow));
448 iter.part = 0;
449 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
450 iter.dev = blk;
451 ut_assertok(device_find_next_child(&iter.dev));
452 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
453
454 /*
455 * initially we don't have any knowledge of which partitions are
456 * bootable, but mmc1 has two partitions, with the first one being
457 * bootable
458 */
459 iter.part = 2;
460 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
461 ut_asserteq(0, iter.first_bootable);
462
463 /* scan with part == 0 to get the partition info */
464 iter.part = 0;
465 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
466 ut_asserteq(1, iter.first_bootable);
467
468 /* now it will refuse to use non-bootable partitions */
469 iter.part = 2;
470 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
471
472 return 0;
473}
474BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700475
476/* Check hunting for bootdev of a particular priority */
477static int bootdev_test_hunt_prio(struct unit_test_state *uts)
478{
Simon Glass4bb7c162023-05-05 20:03:04 -0600479 usb_started = false;
Simon Glass3e9f6be2023-01-17 10:48:07 -0700480 test_set_skip_delays(true);
481
482 console_record_reset_enable();
Simon Glass7e1f6a42023-01-17 10:48:08 -0700483 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700484 ut_assert_nextline("scanning bus for devices...");
485 ut_assert_skip_to_line(" Type: Hard Disk");
486 ut_assert_nextlinen(" Capacity:");
487 ut_assert_console_end();
488
489 /* now try a different priority, verbosely */
Simon Glass7e1f6a42023-01-17 10:48:08 -0700490 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700491 ut_assert_nextline("Hunting with: ide");
Simon Glass3e9f6be2023-01-17 10:48:07 -0700492 ut_assert_nextline("Hunting with: usb");
493 ut_assert_nextline(
494 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
495 ut_assert_console_end();
496
497 return 0;
498}
499BOOTSTD_TEST(bootdev_test_hunt_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassde567b12023-01-17 10:48:09 -0700500
501/* Check hunting for bootdevs with a particular label */
502static int bootdev_test_hunt_label(struct unit_test_state *uts)
503{
504 struct udevice *dev, *old;
505 struct bootstd_priv *std;
506 int mflags;
507
Simon Glass4bb7c162023-05-05 20:03:04 -0600508 usb_started = false;
509
Simon Glassde567b12023-01-17 10:48:09 -0700510 /* get access to the used hunters */
511 ut_assertok(bootstd_get_priv(&std));
512
513 /* scan an unknown uclass */
514 console_record_reset_enable();
515 old = (void *)&mflags; /* arbitrary pointer to check against dev */
516 dev = old;
517 mflags = 123;
Simon Glass4108c6b2023-04-24 13:49:47 +1200518 ut_asserteq(-EPFNOSUPPORT,
Simon Glassde567b12023-01-17 10:48:09 -0700519 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700520 ut_asserteq_ptr(old, dev);
521 ut_asserteq(123, mflags);
522 ut_assert_console_end();
523 ut_asserteq(0, std->hunters_used);
524
525 /* scan an invalid mmc controllers */
526 ut_asserteq(-ENOENT,
527 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
528 ut_asserteq_ptr(old, dev);
529 ut_asserteq(123, mflags);
Simon Glassde567b12023-01-17 10:48:09 -0700530 ut_assert_console_end();
531
532 ut_assertok(bootstd_test_check_mmc_hunter(uts));
533
534 /* scan for a particular mmc controller */
535 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
536 ut_assertnonnull(dev);
537 ut_asserteq_str("mmc1.bootdev", dev->name);
538 ut_asserteq(0, mflags);
539 ut_assert_console_end();
540
541 /* scan all of usb */
542 test_set_skip_delays(true);
543 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
544 ut_assertnonnull(dev);
545 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
546 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
547 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
548 ut_assert_console_end();
549
550 return 0;
551}
552BOOTSTD_TEST(bootdev_test_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassac06b26a2023-01-17 10:48:10 -0700553
554/* Check iterating to the next label in a list */
555static int bootdev_test_next_label(struct unit_test_state *uts)
556{
557 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
558 struct bootflow_iter iter;
559 struct bootstd_priv *std;
560 struct bootflow bflow;
561 struct udevice *dev;
562 int mflags;
563
564 test_set_eth_enable(false);
565
566 /* get access to the used hunters */
567 ut_assertok(bootstd_get_priv(&std));
568
569 memset(&iter, '\0', sizeof(iter));
570 memset(&bflow, '\0', sizeof(bflow));
571 iter.part = 0;
572 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
573 iter.cur_label = -1;
574 iter.labels = labels;
575
576 dev = NULL;
577 mflags = 123;
578 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
579 console_record_reset_enable();
580 ut_assert_console_end();
581 ut_assertnonnull(dev);
582 ut_asserteq_str("mmc0.bootdev", dev->name);
583 ut_asserteq(0, mflags);
584
585 ut_assertok(bootstd_test_check_mmc_hunter(uts));
586
587 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
588 ut_assert_nextline("scanning bus for devices...");
589 ut_assert_skip_to_line(
590 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
591 ut_assert_console_end();
592 ut_assertnonnull(dev);
593 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
594 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
595
Simon Glassdeffa692023-01-28 15:00:28 -0700596 /* SCSI is 7th in the list, so bit 6 */
597 ut_asserteq(BIT(MMC_HUNTER) | BIT(6), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700598
599 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
600 ut_assert_console_end();
601 ut_assertnonnull(dev);
602 ut_asserteq_str("eth@10002000.bootdev", dev->name);
603 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
604 mflags);
605
606 /* dhcp: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700607 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-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_PXE_ONLY,
614 mflags);
615
616 /* pxe: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700617 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700618
619 mflags = 123;
620 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
621 ut_asserteq(123, mflags);
622 ut_assert_console_end();
623
624 /* no change */
Simon Glassdeffa692023-01-28 15:00:28 -0700625 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700626
627 return 0;
628}
629BOOTSTD_TEST(bootdev_test_next_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
630 UT_TESTF_ETH_BOOTDEV | UT_TESTF_SF_BOOTDEV);
Simon Glass660a9952023-01-17 10:48:11 -0700631
632
633/* Check iterating to the next prioirty in a list */
634static int bootdev_test_next_prio(struct unit_test_state *uts)
635{
636 struct bootflow_iter iter;
637 struct bootstd_priv *std;
638 struct bootflow bflow;
639 struct udevice *dev;
640 int ret;
641
Simon Glassba3d5372023-01-17 10:48:15 -0700642 test_set_eth_enable(false);
Simon Glass660a9952023-01-17 10:48:11 -0700643 test_set_skip_delays(true);
644
645 /* get access to the used hunters */
646 ut_assertok(bootstd_get_priv(&std));
647
648 memset(&iter, '\0', sizeof(iter));
649 memset(&bflow, '\0', sizeof(bflow));
650 iter.part = 0;
651 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
652 iter.cur_prio = 0;
Simon Glass99e68182023-02-22 12:17:03 -0700653 iter.flags = BOOTFLOWIF_SHOW;
Simon Glass660a9952023-01-17 10:48:11 -0700654
655 dev = NULL;
656 console_record_reset_enable();
657 ut_assertok(bootdev_next_prio(&iter, &dev));
658 ut_assertnonnull(dev);
659 ut_asserteq_str("mmc2.bootdev", dev->name);
660
661 /* hunt flag not set, so this should not use any hunters */
662 ut_asserteq(0, std->hunters_used);
663 ut_assert_console_end();
664
665 /* now try again with hunting enabled */
Simon Glass99e68182023-02-22 12:17:03 -0700666 iter.flags = BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT;
Simon Glass660a9952023-01-17 10:48:11 -0700667 iter.cur_prio = 0;
668 iter.part = 0;
669
670 ut_assertok(bootdev_next_prio(&iter, &dev));
671 ut_asserteq_str("mmc2.bootdev", dev->name);
Simon Glasse4cf1062023-01-17 10:48:13 -0700672 ut_assert_nextline("Hunting with: simple_bus");
673 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass660a9952023-01-17 10:48:11 -0700674 ut_assert_nextline("Hunting with: mmc");
675 ut_assert_console_end();
676
Simon Glasse4cf1062023-01-17 10:48:13 -0700677 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700678
679 ut_assertok(bootdev_next_prio(&iter, &dev));
680 ut_asserteq_str("mmc1.bootdev", dev->name);
681
682 ut_assertok(bootdev_next_prio(&iter, &dev));
683 ut_asserteq_str("mmc0.bootdev", dev->name);
684 ut_assert_console_end();
685
686 ut_assertok(bootdev_next_prio(&iter, &dev));
687 ut_asserteq_str("spi.bin@0.bootdev", dev->name);
688 ut_assert_skip_to_line("Hunting with: spi_flash");
689
690 /*
691 * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it
692 * starts looking at the devices, so we se virtio as well
693 */
694 ut_assert_nextline("Hunting with: virtio");
695 ut_assert_nextlinen("SF: Detected m25p16");
696
697 ut_assertok(bootdev_next_prio(&iter, &dev));
698 ut_asserteq_str("spi.bin@1.bootdev", dev->name);
699 ut_assert_nextlinen("SF: Detected m25p16");
700 ut_assert_console_end();
701
702 /* keep going until there are no more bootdevs */
703 do {
704 ret = bootdev_next_prio(&iter, &dev);
705 } while (!ret);
706 ut_asserteq(-ENODEV, ret);
707 ut_assertnull(dev);
Simon Glasse4cf1062023-01-17 10:48:13 -0700708 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700709
710 ut_assert_skip_to_line("Hunting with: ethernet");
711 ut_assert_console_end();
712
713 return 0;
714}
715BOOTSTD_TEST(bootdev_test_next_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
716 UT_TESTF_SF_BOOTDEV);