blob: 9a364d0f267c0785c6ec4439c307966b359e95de [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
Simon Glassb255efc2022-04-24 23:31:24 -06009#include <bootstd.h>
10#include <dm.h>
11#include <bootdev.h>
12#include <bootflow.h>
13#include <mapmem.h>
14#include <os.h>
15#include <test/suites.h>
16#include <test/ut.h>
17#include "bootstd_common.h"
18
19/* Allow reseting the USB-started flag */
Linus Walleije3045d12023-01-23 23:07:21 +010020#if defined(CONFIG_USB_HOST) || defined(CONFIG_USB_GADGET)
Simon Glass2510aab2023-05-05 20:03:03 -060021extern bool usb_started;
Linus Walleije3045d12023-01-23 23:07:21 +010022#else
Simon Glass2510aab2023-05-05 20:03:03 -060023#include <usb.h>
Linus Walleije3045d12023-01-23 23:07:21 +010024#endif
Simon Glassb255efc2022-04-24 23:31:24 -060025
26/* Check 'bootdev list' command */
27static int bootdev_test_cmd_list(struct unit_test_state *uts)
28{
29 int probed;
30
31 console_record_reset_enable();
32 for (probed = 0; probed < 2; probed++) {
33 int probe_ch = probed ? '+' : ' ';
34
35 ut_assertok(run_command(probed ? "bootdev list -p" :
36 "bootdev list", 0));
37 ut_assert_nextline("Seq Probed Status Uclass Name");
38 ut_assert_nextlinen("---");
39 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 0, probe_ch, "OK",
40 "mmc", "mmc2.bootdev");
41 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 1, probe_ch, "OK",
42 "mmc", "mmc1.bootdev");
43 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 2, probe_ch, "OK",
44 "mmc", "mmc0.bootdev");
45 ut_assert_nextlinen("---");
46 ut_assert_nextline("(3 bootdevs)");
47 ut_assert_console_end();
48 }
49
50 return 0;
51}
Simon Glass1a92f832024-08-22 07:57:48 -060052BOOTSTD_TEST(bootdev_test_cmd_list, UTF_DM | UTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -060053
54/* Check 'bootdev select' and 'info' commands */
55static int bootdev_test_cmd_select(struct unit_test_state *uts)
56{
57 struct bootstd_priv *std;
58
59 /* get access to the CLI's cur_bootdev */
60 ut_assertok(bootstd_get_priv(&std));
61
62 console_record_reset_enable();
63 ut_asserteq(1, run_command("bootdev info", 0));
64 ut_assert_nextlinen("Please use");
65 ut_assert_console_end();
66
67 /* select by sequence */
68 ut_assertok(run_command("bootdev select 0", 0));
69 ut_assert_console_end();
70
71 ut_assertok(run_command("bootdev info", 0));
72 ut_assert_nextline("Name: mmc2.bootdev");
73 ut_assert_nextline("Sequence: 0");
74 ut_assert_nextline("Status: Probed");
75 ut_assert_nextline("Uclass: mmc");
76 ut_assert_nextline("Bootflows: 0 (0 valid)");
77 ut_assert_console_end();
78
79 /* select by bootdev name */
80 ut_assertok(run_command("bootdev select mmc1.bootdev", 0));
81 ut_assert_console_end();
82 ut_assertnonnull(std->cur_bootdev);
83 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
84
85 /* select by bootdev label*/
86 ut_assertok(run_command("bootdev select mmc1", 0));
87 ut_assert_console_end();
88 ut_assertnonnull(std->cur_bootdev);
89 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
90
91 /* deselect */
92 ut_assertok(run_command("bootdev select", 0));
93 ut_assert_console_end();
94 ut_assertnull(std->cur_bootdev);
95
96 ut_asserteq(1, run_command("bootdev info", 0));
97 ut_assert_nextlinen("Please use");
98 ut_assert_console_end();
99
100 return 0;
101}
Simon Glass1a92f832024-08-22 07:57:48 -0600102BOOTSTD_TEST(bootdev_test_cmd_select, UTF_DM | UTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600103
104/* Check bootdev labels */
105static int bootdev_test_labels(struct unit_test_state *uts)
106{
107 struct udevice *dev, *media;
Simon Glasse22fe922023-01-17 10:48:05 -0700108 int mflags = 0;
Simon Glassb255efc2022-04-24 23:31:24 -0600109
Simon Glasse22fe922023-01-17 10:48:05 -0700110 ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600111 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
Simon Glasse22fe922023-01-17 10:48:05 -0700112 ut_asserteq(0, mflags);
Simon Glassb255efc2022-04-24 23:31:24 -0600113 media = dev_get_parent(dev);
114 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
115 ut_asserteq_str("mmc2", media->name);
116
Simon Glasse22fe922023-01-17 10:48:05 -0700117 /* Check method flags */
118 ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700119 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
120 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700121 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700122 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
123 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700124
Simon Glassb255efc2022-04-24 23:31:24 -0600125 /* Check invalid uclass */
Simon Glass4108c6b2023-04-24 13:49:47 +1200126 ut_asserteq(-EPFNOSUPPORT,
127 bootdev_find_by_label("fred0", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600128
129 /* Check unknown sequence number */
Simon Glasse22fe922023-01-17 10:48:05 -0700130 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600131
132 return 0;
133}
Simon Glass1a92f832024-08-22 07:57:48 -0600134BOOTSTD_TEST(bootdev_test_labels, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600135
Simon Glassde567b12023-01-17 10:48:09 -0700136/* Check bootdev_find_by_any() */
137static int bootdev_test_any(struct unit_test_state *uts)
138{
139 struct udevice *dev, *media;
140 int mflags;
141
142 /*
143 * with ethernet enabled we have 8 devices ahead of the mmc ones:
144 *
145 * ut_assertok(run_command("bootdev list", 0));
146 * Seq Probed Status Uclass Name
147 * --- ------ ------ -------- ------------------
148 * 0 [ + ] OK ethernet eth@10002000.bootdev
149 * 1 [ ] OK ethernet eth@10003000.bootdev
150 * 2 [ ] OK ethernet sbe5.bootdev
151 * 3 [ ] OK ethernet eth@10004000.bootdev
152 * 4 [ ] OK ethernet phy-test-eth.bootdev
153 * 5 [ ] OK ethernet dsa-test-eth.bootdev
154 * 6 [ ] OK ethernet dsa-test@0.bootdev
155 * 7 [ ] OK ethernet dsa-test@1.bootdev
156 * 8 [ ] OK mmc mmc2.bootdev
157 * 9 [ + ] OK mmc mmc1.bootdev
158 * a [ ] OK mmc mmc0.bootdev
159 */
160 console_record_reset_enable();
161 ut_assertok(bootdev_find_by_any("8", &dev, &mflags));
162 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
163 ut_asserteq(BOOTFLOW_METHF_SINGLE_DEV, mflags);
164 media = dev_get_parent(dev);
165 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
166 ut_asserteq_str("mmc2", media->name);
167 ut_assert_console_end();
168
169 /* there should not be this many bootdevs */
170 ut_asserteq(-ENODEV, bootdev_find_by_any("50", &dev, &mflags));
171 ut_assert_nextline("Cannot find '50' (err=-19)");
172 ut_assert_console_end();
173
174 /* Check method flags */
175 ut_assertok(bootdev_find_by_any("pxe", &dev, &mflags));
176 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
177 mflags);
178
179 /* Check invalid uclass */
180 mflags = 123;
Simon Glass4108c6b2023-04-24 13:49:47 +1200181 ut_asserteq(-EPFNOSUPPORT, bootdev_find_by_any("fred0", &dev, &mflags));
182 ut_assert_nextline("Cannot find bootdev 'fred0' (err=-96)");
Simon Glassde567b12023-01-17 10:48:09 -0700183 ut_asserteq(123, mflags);
184 ut_assert_console_end();
185
186 return 0;
187}
Simon Glass1a92f832024-08-22 07:57:48 -0600188BOOTSTD_TEST(bootdev_test_any, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV);
Simon Glassde567b12023-01-17 10:48:09 -0700189
Simon Glassfd5a5192023-10-23 00:02:11 -0700190/*
191 * Check bootdev ordering with the bootdev-order property and boot_targets
192 * environment variable
193 */
Simon Glassb255efc2022-04-24 23:31:24 -0600194static int bootdev_test_order(struct unit_test_state *uts)
195{
196 struct bootflow_iter iter;
197 struct bootflow bflow;
198
Simon Glassfd5a5192023-10-23 00:02:11 -0700199 test_set_skip_delays(true);
200
201 /* Start up USB which gives us three additional bootdevs */
202 usb_started = false;
203 ut_assertok(run_command("usb start", 0));
204
Simon Glassb255efc2022-04-24 23:31:24 -0600205 /*
206 * First try the order set by the bootdev-order property
207 * Like all sandbox unit tests this relies on the devicetree setting up
208 * the required devices:
209 *
210 * mmc0 - nothing connected
211 * mmc1 - connected to mmc1.img file
212 * mmc2 - nothing connected
213 */
214 ut_assertok(env_set("boot_targets", NULL));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700215 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600216 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700217 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
218 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600219 bootflow_iter_uninit(&iter);
220
221 /* Use the environment variable to override it */
Simon Glassfd5a5192023-10-23 00:02:11 -0700222 ut_assertok(env_set("boot_targets", "mmc1 mmc2 usb"));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700223 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700224 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb51f01d2023-10-23 00:02:13 -0700225 ut_asserteq(5, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700226 ut_asserteq_str("mmc1.bootdev", iter.dev_used[0]->name);
227 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassfd5a5192023-10-23 00:02:11 -0700228 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
229 iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600230 bootflow_iter_uninit(&iter);
231
Simon Glass33795a92023-10-23 00:02:12 -0700232 /* Try a single uclass */
233 ut_assertok(env_set("boot_targets", NULL));
234 ut_assertok(bootflow_scan_first(NULL, "mmc", &iter, 0, &bflow));
235 ut_asserteq(2, iter.num_devs);
236
Simon Glassb51f01d2023-10-23 00:02:13 -0700237 /* Now scan past mmc1 and make sure that only mmc0 shows up */
238 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
239 ut_asserteq(3, iter.num_devs);
240 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
241 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
242 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
243 bootflow_iter_uninit(&iter);
244
245 /* Try a single uclass with boot_targets */
246 ut_assertok(env_set("boot_targets", "mmc"));
247 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
248 ut_asserteq(2, iter.num_devs);
249
250 /* Now scan past mmc1 and make sure that only mmc0 shows up */
Simon Glass33795a92023-10-23 00:02:12 -0700251 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
252 ut_asserteq(3, 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);
255 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
256 bootflow_iter_uninit(&iter);
257
Simon Glassb51f01d2023-10-23 00:02:13 -0700258 /* Try a single uclass with boot_targets */
259 ut_assertok(env_set("boot_targets", "mmc usb"));
260 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
261 ut_asserteq(2, iter.num_devs);
262
263 /* Now scan past mmc1 and make sure that the 3 USB devices show up */
264 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
265 ut_asserteq(6, iter.num_devs);
266 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
267 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
268 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
269 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
270 iter.dev_used[3]->name);
271 bootflow_iter_uninit(&iter);
272
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700273 return 0;
274}
Simon Glass1a92f832024-08-22 07:57:48 -0600275BOOTSTD_TEST(bootdev_test_order, UTF_DM | UTF_SCAN_FDT);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700276
277/* Check default bootdev ordering */
278static int bootdev_test_order_default(struct unit_test_state *uts)
279{
280 struct bootflow_iter iter;
281 struct bootflow bflow;
282
Simon Glassb255efc2022-04-24 23:31:24 -0600283 /*
284 * Now drop both orderings, to check the default (prioriy/sequence)
285 * ordering
286 */
287 ut_assertok(env_set("boot_targets", NULL));
288 ut_assertok(bootstd_test_drop_bootdev_order(uts));
289
Simon Glass5d3d44f2023-01-17 10:48:16 -0700290 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700291 ut_asserteq(2, iter.num_devs);
292 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
293 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600294
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700295 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600296 ut_asserteq(3, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700297 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600298 bootflow_iter_uninit(&iter);
299
300 return 0;
301}
Simon Glass1a92f832024-08-22 07:57:48 -0600302BOOTSTD_TEST(bootdev_test_order_default, UTF_DM | UTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600303
304/* Check bootdev ordering with the uclass priority */
305static int bootdev_test_prio(struct unit_test_state *uts)
306{
307 struct bootdev_uc_plat *ucp;
308 struct bootflow_iter iter;
309 struct bootflow bflow;
310 struct udevice *blk;
311
Simon Glassf3cb89f2023-01-17 10:47:37 -0700312 test_set_skip_delays(true);
313
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700314 /* disable ethernet since the hunter will run dhcp */
315 test_set_eth_enable(false);
316
Simon Glassb255efc2022-04-24 23:31:24 -0600317 /* Start up USB which gives us three additional bootdevs */
318 usb_started = false;
319 ut_assertok(run_command("usb start", 0));
320
321 ut_assertok(bootstd_test_drop_bootdev_order(uts));
322
323 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
324 console_record_reset_enable();
Simon Glass5d3d44f2023-01-17 10:48:16 -0700325 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700326 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600327 ut_asserteq(6, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700328 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600329 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700330 iter.dev_used[3]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600331
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700332 ut_assertok(bootdev_get_sibling_blk(iter.dev_used[3], &blk));
Simon Glassb255efc2022-04-24 23:31:24 -0600333 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
334
335 /* adjust the priority of the first USB bootdev to the highest */
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700336 ucp = dev_get_uclass_plat(iter.dev_used[3]);
337 ucp->prio = BOOTDEVP_1_PRE_SCAN;
Simon Glassb255efc2022-04-24 23:31:24 -0600338
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700339 /* try again but enable hunting, which brings in SCSI */
Simon Glassb255efc2022-04-24 23:31:24 -0600340 bootflow_iter_uninit(&iter);
Simon Glass99e68182023-02-22 12:17:03 -0700341 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWIF_HUNT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700342 &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700343 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
344 ut_asserteq(7, iter.num_devs);
Simon Glassb255efc2022-04-24 23:31:24 -0600345 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700346 iter.dev_used[0]->name);
347 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600348
349 return 0;
350}
Simon Glass1a92f832024-08-22 07:57:48 -0600351BOOTSTD_TEST(bootdev_test_prio, UTF_DM | UTF_SCAN_FDT);
Simon Glass5acb97a2023-01-17 10:47:33 -0700352
353/* Check listing hunters */
354static int bootdev_test_hunter(struct unit_test_state *uts)
355{
356 struct bootstd_priv *std;
357
Simon Glass4bb7c162023-05-05 20:03:04 -0600358 usb_started = false;
Simon Glassf3cb89f2023-01-17 10:47:37 -0700359 test_set_skip_delays(true);
360
Simon Glass5acb97a2023-01-17 10:47:33 -0700361 /* get access to the used hunters */
362 ut_assertok(bootstd_get_priv(&std));
363
364 console_record_reset_enable();
365 bootdev_list_hunters(std);
366 ut_assert_nextline("Prio Used Uclass Hunter");
367 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700368 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700369 ut_assert_nextline(" 1 simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700370 ut_assert_nextline(" 5 ide ide_bootdev");
371 ut_assert_nextline(" 2 mmc mmc_bootdev");
372 ut_assert_nextline(" 4 nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700373 ut_assert_nextline(" 4 qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700374 ut_assert_nextline(" 4 scsi scsi_bootdev");
375 ut_assert_nextline(" 4 spi_flash sf_bootdev");
376 ut_assert_nextline(" 5 usb usb_bootdev");
377 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700378 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700379 ut_assert_console_end();
380
Simon Glassf3cb89f2023-01-17 10:47:37 -0700381 ut_assertok(bootdev_hunt("usb1", false));
382 ut_assert_nextline(
383 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass1248d2b2023-01-17 10:47:34 -0700384 ut_assert_console_end();
385
Simon Glassdeffa692023-01-28 15:00:28 -0700386 /* USB is 7th in the list, so bit 8 */
387 ut_asserteq(BIT(8), std->hunters_used);
Simon Glassf3cb89f2023-01-17 10:47:37 -0700388
Simon Glass5acb97a2023-01-17 10:47:33 -0700389 return 0;
390}
Simon Glass1a92f832024-08-22 07:57:48 -0600391BOOTSTD_TEST(bootdev_test_hunter, UTF_DM | UTF_SCAN_FDT);
Simon Glass5acb97a2023-01-17 10:47:33 -0700392
393/* Check 'bootdev hunt' command */
394static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
395{
396 struct bootstd_priv *std;
397
Simon Glassf3cb89f2023-01-17 10:47:37 -0700398 test_set_skip_delays(true);
Simon Glass4bb7c162023-05-05 20:03:04 -0600399 usb_started = false;
Simon Glassf3cb89f2023-01-17 10:47:37 -0700400
Simon Glass5acb97a2023-01-17 10:47:33 -0700401 /* get access to the used hunters */
402 ut_assertok(bootstd_get_priv(&std));
403
404 console_record_reset_enable();
405 ut_assertok(run_command("bootdev hunt -l", 0));
406 ut_assert_nextline("Prio Used Uclass Hunter");
407 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700408 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700409 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700410 ut_assert_console_end();
411
Simon Glass7e1f6a42023-01-17 10:48:08 -0700412 /* Use the MMC hunter and see that it updates */
413 ut_assertok(run_command("bootdev hunt mmc", 0));
414 ut_assertok(run_command("bootdev hunt -l", 0));
415 ut_assert_skip_to_line(" 5 ide ide_bootdev");
416 ut_assert_nextline(" 2 * mmc mmc_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700417 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700418 ut_assert_console_end();
419
Simon Glass5acb97a2023-01-17 10:47:33 -0700420 /* Scan all hunters */
Simon Glassac06b26a2023-01-17 10:48:10 -0700421 test_set_eth_enable(false);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700422 test_set_skip_delays(true);
Simon Glass5acb97a2023-01-17 10:47:33 -0700423 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700424 ut_assert_nextline("Hunting with: ethernet");
Simon Glasse4cf1062023-01-17 10:48:13 -0700425
426 /* This is the extension feature which has no uclass at present */
427 ut_assert_nextline("Hunting with: simple_bus");
428 ut_assert_nextline("Found 2 extension board(s).");
Simon Glassbe7c6662023-01-17 10:47:46 -0700429 ut_assert_nextline("Hunting with: ide");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700430
431 /* mmc hunter has already been used so should not run again */
432
Simon Glass08382872023-01-17 10:47:47 -0700433 ut_assert_nextline("Hunting with: nvme");
Simon Glassdeffa692023-01-28 15:00:28 -0700434 ut_assert_nextline("Hunting with: qfw");
Simon Glass9985e9c2023-01-17 10:47:45 -0700435 ut_assert_nextline("Hunting with: scsi");
436 ut_assert_nextline("scanning bus for devices...");
Simon Glass215be682023-01-17 10:48:03 -0700437 ut_assert_skip_to_line("Hunting with: spi_flash");
438 ut_assert_nextline("Hunting with: usb");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700439 ut_assert_nextline(
440 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass215be682023-01-17 10:48:03 -0700441 ut_assert_nextline("Hunting with: virtio");
Simon Glass5acb97a2023-01-17 10:47:33 -0700442 ut_assert_console_end();
443
444 /* List available hunters */
445 ut_assertok(run_command("bootdev hunt -l", 0));
446 ut_assert_nextlinen("Prio");
447 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700448 ut_assert_nextline(" 6 * ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700449 ut_assert_nextline(" 1 * simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700450 ut_assert_nextline(" 5 * ide ide_bootdev");
451 ut_assert_nextline(" 2 * mmc mmc_bootdev");
452 ut_assert_nextline(" 4 * nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700453 ut_assert_nextline(" 4 * qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700454 ut_assert_nextline(" 4 * scsi scsi_bootdev");
455 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
456 ut_assert_nextline(" 5 * usb usb_bootdev");
457 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700458 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700459 ut_assert_console_end();
460
Simon Glasse4cf1062023-01-17 10:48:13 -0700461 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass5acb97a2023-01-17 10:47:33 -0700462
463 return 0;
464}
Simon Glass1a92f832024-08-22 07:57:48 -0600465BOOTSTD_TEST(bootdev_test_cmd_hunt, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700466
Simon Glass484e4072023-01-17 10:48:14 -0700467/* Check searching for bootdevs using the hunters */
468static int bootdev_test_hunt_scan(struct unit_test_state *uts)
469{
470 struct bootflow_iter iter;
471 struct bootstd_priv *std;
472 struct bootflow bflow;
473
474 /* get access to the used hunters */
475 ut_assertok(bootstd_get_priv(&std));
476
477 ut_assertok(bootstd_test_drop_bootdev_order(uts));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700478 ut_assertok(bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700479 BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT |
480 BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glass484e4072023-01-17 10:48:14 -0700481 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
482
483 return 0;
484}
Simon Glass1a92f832024-08-22 07:57:48 -0600485BOOTSTD_TEST(bootdev_test_hunt_scan, UTF_DM | UTF_SCAN_FDT);
Simon Glass484e4072023-01-17 10:48:14 -0700486
Simon Glass64cbc5c2023-01-17 10:47:42 -0700487/* Check that only bootable partitions are processed */
488static int bootdev_test_bootable(struct unit_test_state *uts)
489{
490 struct bootflow_iter iter;
491 struct bootflow bflow;
492 struct udevice *blk;
493
494 memset(&iter, '\0', sizeof(iter));
495 memset(&bflow, '\0', sizeof(bflow));
496 iter.part = 0;
497 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
498 iter.dev = blk;
499 ut_assertok(device_find_next_child(&iter.dev));
500 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
501
502 /*
503 * initially we don't have any knowledge of which partitions are
504 * bootable, but mmc1 has two partitions, with the first one being
505 * bootable
506 */
507 iter.part = 2;
508 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
509 ut_asserteq(0, iter.first_bootable);
510
511 /* scan with part == 0 to get the partition info */
512 iter.part = 0;
513 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
514 ut_asserteq(1, iter.first_bootable);
515
516 /* now it will refuse to use non-bootable partitions */
517 iter.part = 2;
518 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
519
520 return 0;
521}
Simon Glass1a92f832024-08-22 07:57:48 -0600522BOOTSTD_TEST(bootdev_test_bootable, UTF_DM | UTF_SCAN_FDT);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700523
524/* Check hunting for bootdev of a particular priority */
525static int bootdev_test_hunt_prio(struct unit_test_state *uts)
526{
Simon Glass4bb7c162023-05-05 20:03:04 -0600527 usb_started = false;
Simon Glass3e9f6be2023-01-17 10:48:07 -0700528 test_set_skip_delays(true);
529
530 console_record_reset_enable();
Simon Glass7e1f6a42023-01-17 10:48:08 -0700531 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700532 ut_assert_nextline("scanning bus for devices...");
533 ut_assert_skip_to_line(" Type: Hard Disk");
534 ut_assert_nextlinen(" Capacity:");
535 ut_assert_console_end();
536
537 /* now try a different priority, verbosely */
Simon Glass7e1f6a42023-01-17 10:48:08 -0700538 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700539 ut_assert_nextline("Hunting with: ide");
Simon Glass3e9f6be2023-01-17 10:48:07 -0700540 ut_assert_nextline("Hunting with: usb");
541 ut_assert_nextline(
542 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
543 ut_assert_console_end();
544
545 return 0;
546}
Simon Glass1a92f832024-08-22 07:57:48 -0600547BOOTSTD_TEST(bootdev_test_hunt_prio, UTF_DM | UTF_SCAN_FDT);
Simon Glassde567b12023-01-17 10:48:09 -0700548
549/* Check hunting for bootdevs with a particular label */
550static int bootdev_test_hunt_label(struct unit_test_state *uts)
551{
552 struct udevice *dev, *old;
553 struct bootstd_priv *std;
554 int mflags;
555
Simon Glass4bb7c162023-05-05 20:03:04 -0600556 usb_started = false;
557
Simon Glassde567b12023-01-17 10:48:09 -0700558 /* get access to the used hunters */
559 ut_assertok(bootstd_get_priv(&std));
560
561 /* scan an unknown uclass */
562 console_record_reset_enable();
563 old = (void *)&mflags; /* arbitrary pointer to check against dev */
564 dev = old;
565 mflags = 123;
Simon Glass4108c6b2023-04-24 13:49:47 +1200566 ut_asserteq(-EPFNOSUPPORT,
Simon Glassde567b12023-01-17 10:48:09 -0700567 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700568 ut_asserteq_ptr(old, dev);
569 ut_asserteq(123, mflags);
570 ut_assert_console_end();
571 ut_asserteq(0, std->hunters_used);
572
573 /* scan an invalid mmc controllers */
574 ut_asserteq(-ENOENT,
575 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
576 ut_asserteq_ptr(old, dev);
577 ut_asserteq(123, mflags);
Simon Glassde567b12023-01-17 10:48:09 -0700578 ut_assert_console_end();
579
580 ut_assertok(bootstd_test_check_mmc_hunter(uts));
581
582 /* scan for a particular mmc controller */
583 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
584 ut_assertnonnull(dev);
585 ut_asserteq_str("mmc1.bootdev", dev->name);
586 ut_asserteq(0, mflags);
587 ut_assert_console_end();
588
589 /* scan all of usb */
590 test_set_skip_delays(true);
591 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
592 ut_assertnonnull(dev);
593 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
594 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
595 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
596 ut_assert_console_end();
597
598 return 0;
599}
Simon Glass1a92f832024-08-22 07:57:48 -0600600BOOTSTD_TEST(bootdev_test_hunt_label, UTF_DM | UTF_SCAN_FDT);
Simon Glassac06b26a2023-01-17 10:48:10 -0700601
602/* Check iterating to the next label in a list */
603static int bootdev_test_next_label(struct unit_test_state *uts)
604{
605 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
606 struct bootflow_iter iter;
607 struct bootstd_priv *std;
608 struct bootflow bflow;
609 struct udevice *dev;
610 int mflags;
611
612 test_set_eth_enable(false);
613
614 /* get access to the used hunters */
615 ut_assertok(bootstd_get_priv(&std));
616
617 memset(&iter, '\0', sizeof(iter));
618 memset(&bflow, '\0', sizeof(bflow));
619 iter.part = 0;
620 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
621 iter.cur_label = -1;
622 iter.labels = labels;
623
624 dev = NULL;
625 mflags = 123;
626 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
627 console_record_reset_enable();
628 ut_assert_console_end();
629 ut_assertnonnull(dev);
630 ut_asserteq_str("mmc0.bootdev", dev->name);
631 ut_asserteq(0, mflags);
632
633 ut_assertok(bootstd_test_check_mmc_hunter(uts));
634
635 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
636 ut_assert_nextline("scanning bus for devices...");
637 ut_assert_skip_to_line(
638 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
639 ut_assert_console_end();
640 ut_assertnonnull(dev);
641 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
642 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
643
Simon Glassdeffa692023-01-28 15:00:28 -0700644 /* SCSI is 7th in the list, so bit 6 */
645 ut_asserteq(BIT(MMC_HUNTER) | BIT(6), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700646
647 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
648 ut_assert_console_end();
649 ut_assertnonnull(dev);
650 ut_asserteq_str("eth@10002000.bootdev", dev->name);
651 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
652 mflags);
653
654 /* dhcp: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700655 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700656
657 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
658 ut_assert_console_end();
659 ut_assertnonnull(dev);
660 ut_asserteq_str("eth@10002000.bootdev", dev->name);
661 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
662 mflags);
663
664 /* pxe: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700665 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700666
667 mflags = 123;
668 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
669 ut_asserteq(123, mflags);
670 ut_assert_console_end();
671
672 /* no change */
Simon Glassdeffa692023-01-28 15:00:28 -0700673 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700674
675 return 0;
676}
Simon Glass1a92f832024-08-22 07:57:48 -0600677BOOTSTD_TEST(bootdev_test_next_label, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
678 UTF_SF_BOOTDEV);
Simon Glass660a9952023-01-17 10:48:11 -0700679
Simon Glass660a9952023-01-17 10:48:11 -0700680/* Check iterating to the next prioirty in a list */
681static int bootdev_test_next_prio(struct unit_test_state *uts)
682{
683 struct bootflow_iter iter;
684 struct bootstd_priv *std;
685 struct bootflow bflow;
686 struct udevice *dev;
687 int ret;
688
Simon Glassba3d5372023-01-17 10:48:15 -0700689 test_set_eth_enable(false);
Simon Glass660a9952023-01-17 10:48:11 -0700690 test_set_skip_delays(true);
691
692 /* get access to the used hunters */
693 ut_assertok(bootstd_get_priv(&std));
694
695 memset(&iter, '\0', sizeof(iter));
696 memset(&bflow, '\0', sizeof(bflow));
697 iter.part = 0;
698 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
699 iter.cur_prio = 0;
Simon Glass99e68182023-02-22 12:17:03 -0700700 iter.flags = BOOTFLOWIF_SHOW;
Simon Glass660a9952023-01-17 10:48:11 -0700701
702 dev = NULL;
703 console_record_reset_enable();
704 ut_assertok(bootdev_next_prio(&iter, &dev));
705 ut_assertnonnull(dev);
706 ut_asserteq_str("mmc2.bootdev", dev->name);
707
708 /* hunt flag not set, so this should not use any hunters */
709 ut_asserteq(0, std->hunters_used);
710 ut_assert_console_end();
711
712 /* now try again with hunting enabled */
Simon Glass99e68182023-02-22 12:17:03 -0700713 iter.flags = BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT;
Simon Glass660a9952023-01-17 10:48:11 -0700714 iter.cur_prio = 0;
715 iter.part = 0;
716
717 ut_assertok(bootdev_next_prio(&iter, &dev));
718 ut_asserteq_str("mmc2.bootdev", dev->name);
Simon Glasse4cf1062023-01-17 10:48:13 -0700719 ut_assert_nextline("Hunting with: simple_bus");
720 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass660a9952023-01-17 10:48:11 -0700721 ut_assert_nextline("Hunting with: mmc");
722 ut_assert_console_end();
723
Simon Glasse4cf1062023-01-17 10:48:13 -0700724 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700725
726 ut_assertok(bootdev_next_prio(&iter, &dev));
727 ut_asserteq_str("mmc1.bootdev", dev->name);
728
729 ut_assertok(bootdev_next_prio(&iter, &dev));
730 ut_asserteq_str("mmc0.bootdev", dev->name);
731 ut_assert_console_end();
732
733 ut_assertok(bootdev_next_prio(&iter, &dev));
734 ut_asserteq_str("spi.bin@0.bootdev", dev->name);
735 ut_assert_skip_to_line("Hunting with: spi_flash");
736
737 /*
738 * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it
739 * starts looking at the devices, so we se virtio as well
740 */
741 ut_assert_nextline("Hunting with: virtio");
742 ut_assert_nextlinen("SF: Detected m25p16");
743
744 ut_assertok(bootdev_next_prio(&iter, &dev));
745 ut_asserteq_str("spi.bin@1.bootdev", dev->name);
746 ut_assert_nextlinen("SF: Detected m25p16");
747 ut_assert_console_end();
748
749 /* keep going until there are no more bootdevs */
750 do {
751 ret = bootdev_next_prio(&iter, &dev);
752 } while (!ret);
753 ut_asserteq(-ENODEV, ret);
754 ut_assertnull(dev);
Simon Glasse4cf1062023-01-17 10:48:13 -0700755 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700756
757 ut_assert_skip_to_line("Hunting with: ethernet");
758 ut_assert_console_end();
759
760 return 0;
761}
Simon Glass1a92f832024-08-22 07:57:48 -0600762BOOTSTD_TEST(bootdev_test_next_prio, UTF_DM | UTF_SCAN_FDT | UTF_SF_BOOTDEV);