blob: c635d06ec25d4bf274099f153648d34f5a845436 [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
Simon Glassb255efc2022-04-24 23:31:24 -060019/* Check 'bootdev list' command */
20static int bootdev_test_cmd_list(struct unit_test_state *uts)
21{
22 int probed;
23
Simon Glassb255efc2022-04-24 23:31:24 -060024 for (probed = 0; probed < 2; probed++) {
25 int probe_ch = probed ? '+' : ' ';
26
27 ut_assertok(run_command(probed ? "bootdev list -p" :
28 "bootdev list", 0));
29 ut_assert_nextline("Seq Probed Status Uclass Name");
30 ut_assert_nextlinen("---");
31 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 0, probe_ch, "OK",
32 "mmc", "mmc2.bootdev");
33 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 1, probe_ch, "OK",
34 "mmc", "mmc1.bootdev");
35 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 2, probe_ch, "OK",
36 "mmc", "mmc0.bootdev");
37 ut_assert_nextlinen("---");
38 ut_assert_nextline("(3 bootdevs)");
39 ut_assert_console_end();
40 }
41
42 return 0;
43}
Simon Glassf0425022024-08-22 07:57:54 -060044BOOTSTD_TEST(bootdev_test_cmd_list, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -060045
46/* Check 'bootdev select' and 'info' commands */
47static int bootdev_test_cmd_select(struct unit_test_state *uts)
48{
49 struct bootstd_priv *std;
50
51 /* get access to the CLI's cur_bootdev */
52 ut_assertok(bootstd_get_priv(&std));
53
Simon Glassb255efc2022-04-24 23:31:24 -060054 ut_asserteq(1, run_command("bootdev info", 0));
55 ut_assert_nextlinen("Please use");
56 ut_assert_console_end();
57
58 /* select by sequence */
59 ut_assertok(run_command("bootdev select 0", 0));
60 ut_assert_console_end();
61
62 ut_assertok(run_command("bootdev info", 0));
63 ut_assert_nextline("Name: mmc2.bootdev");
64 ut_assert_nextline("Sequence: 0");
65 ut_assert_nextline("Status: Probed");
66 ut_assert_nextline("Uclass: mmc");
67 ut_assert_nextline("Bootflows: 0 (0 valid)");
68 ut_assert_console_end();
69
70 /* select by bootdev name */
71 ut_assertok(run_command("bootdev select mmc1.bootdev", 0));
72 ut_assert_console_end();
73 ut_assertnonnull(std->cur_bootdev);
74 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
75
76 /* select by bootdev label*/
77 ut_assertok(run_command("bootdev select mmc1", 0));
78 ut_assert_console_end();
79 ut_assertnonnull(std->cur_bootdev);
80 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
81
82 /* deselect */
83 ut_assertok(run_command("bootdev select", 0));
84 ut_assert_console_end();
85 ut_assertnull(std->cur_bootdev);
86
87 ut_asserteq(1, run_command("bootdev info", 0));
88 ut_assert_nextlinen("Please use");
89 ut_assert_console_end();
90
91 return 0;
92}
Simon Glassf0425022024-08-22 07:57:54 -060093BOOTSTD_TEST(bootdev_test_cmd_select, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -060094
95/* Check bootdev labels */
96static int bootdev_test_labels(struct unit_test_state *uts)
97{
98 struct udevice *dev, *media;
Simon Glasse22fe922023-01-17 10:48:05 -070099 int mflags = 0;
Simon Glassb255efc2022-04-24 23:31:24 -0600100
Simon Glasse22fe922023-01-17 10:48:05 -0700101 ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600102 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
Simon Glasse22fe922023-01-17 10:48:05 -0700103 ut_asserteq(0, mflags);
Simon Glassb255efc2022-04-24 23:31:24 -0600104 media = dev_get_parent(dev);
105 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
106 ut_asserteq_str("mmc2", media->name);
107
Simon Glasse22fe922023-01-17 10:48:05 -0700108 /* Check method flags */
109 ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700110 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
111 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700112 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700113 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
114 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700115
Simon Glassb255efc2022-04-24 23:31:24 -0600116 /* Check invalid uclass */
Simon Glass4108c6b2023-04-24 13:49:47 +1200117 ut_asserteq(-EPFNOSUPPORT,
118 bootdev_find_by_label("fred0", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600119
120 /* Check unknown sequence number */
Simon Glasse22fe922023-01-17 10:48:05 -0700121 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600122
123 return 0;
124}
Simon Glass1a92f832024-08-22 07:57:48 -0600125BOOTSTD_TEST(bootdev_test_labels, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600126
Simon Glassde567b12023-01-17 10:48:09 -0700127/* Check bootdev_find_by_any() */
128static int bootdev_test_any(struct unit_test_state *uts)
129{
130 struct udevice *dev, *media;
131 int mflags;
132
133 /*
134 * with ethernet enabled we have 8 devices ahead of the mmc ones:
135 *
136 * ut_assertok(run_command("bootdev list", 0));
137 * Seq Probed Status Uclass Name
138 * --- ------ ------ -------- ------------------
139 * 0 [ + ] OK ethernet eth@10002000.bootdev
140 * 1 [ ] OK ethernet eth@10003000.bootdev
141 * 2 [ ] OK ethernet sbe5.bootdev
142 * 3 [ ] OK ethernet eth@10004000.bootdev
143 * 4 [ ] OK ethernet phy-test-eth.bootdev
144 * 5 [ ] OK ethernet dsa-test-eth.bootdev
145 * 6 [ ] OK ethernet dsa-test@0.bootdev
146 * 7 [ ] OK ethernet dsa-test@1.bootdev
147 * 8 [ ] OK mmc mmc2.bootdev
148 * 9 [ + ] OK mmc mmc1.bootdev
149 * a [ ] OK mmc mmc0.bootdev
150 */
Simon Glassde567b12023-01-17 10:48:09 -0700151 ut_assertok(bootdev_find_by_any("8", &dev, &mflags));
152 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
153 ut_asserteq(BOOTFLOW_METHF_SINGLE_DEV, mflags);
154 media = dev_get_parent(dev);
155 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
156 ut_asserteq_str("mmc2", media->name);
157 ut_assert_console_end();
158
159 /* there should not be this many bootdevs */
160 ut_asserteq(-ENODEV, bootdev_find_by_any("50", &dev, &mflags));
161 ut_assert_nextline("Cannot find '50' (err=-19)");
162 ut_assert_console_end();
163
164 /* Check method flags */
165 ut_assertok(bootdev_find_by_any("pxe", &dev, &mflags));
166 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
167 mflags);
168
169 /* Check invalid uclass */
170 mflags = 123;
Simon Glass4108c6b2023-04-24 13:49:47 +1200171 ut_asserteq(-EPFNOSUPPORT, bootdev_find_by_any("fred0", &dev, &mflags));
172 ut_assert_nextline("Cannot find bootdev 'fred0' (err=-96)");
Simon Glassde567b12023-01-17 10:48:09 -0700173 ut_asserteq(123, mflags);
174 ut_assert_console_end();
175
176 return 0;
177}
Simon Glassf0425022024-08-22 07:57:54 -0600178BOOTSTD_TEST(bootdev_test_any, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
179 UTF_CONSOLE);
Simon Glassde567b12023-01-17 10:48:09 -0700180
Simon Glassfd5a5192023-10-23 00:02:11 -0700181/*
182 * Check bootdev ordering with the bootdev-order property and boot_targets
183 * environment variable
184 */
Simon Glassb255efc2022-04-24 23:31:24 -0600185static int bootdev_test_order(struct unit_test_state *uts)
186{
187 struct bootflow_iter iter;
188 struct bootflow bflow;
189
Simon Glassfd5a5192023-10-23 00:02:11 -0700190 test_set_skip_delays(true);
191
192 /* Start up USB which gives us three additional bootdevs */
Simon Glass22c93da2024-09-01 16:26:16 -0600193 bootstd_reset_usb();
Simon Glassfd5a5192023-10-23 00:02:11 -0700194 ut_assertok(run_command("usb start", 0));
195
Simon Glassb255efc2022-04-24 23:31:24 -0600196 /*
197 * First try the order set by the bootdev-order property
198 * Like all sandbox unit tests this relies on the devicetree setting up
199 * the required devices:
200 *
201 * mmc0 - nothing connected
202 * mmc1 - connected to mmc1.img file
203 * mmc2 - nothing connected
204 */
205 ut_assertok(env_set("boot_targets", NULL));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700206 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600207 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700208 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
209 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600210 bootflow_iter_uninit(&iter);
211
212 /* Use the environment variable to override it */
Simon Glassfd5a5192023-10-23 00:02:11 -0700213 ut_assertok(env_set("boot_targets", "mmc1 mmc2 usb"));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700214 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700215 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb51f01d2023-10-23 00:02:13 -0700216 ut_asserteq(5, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700217 ut_asserteq_str("mmc1.bootdev", iter.dev_used[0]->name);
218 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassfd5a5192023-10-23 00:02:11 -0700219 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
220 iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600221 bootflow_iter_uninit(&iter);
222
Simon Glass33795a92023-10-23 00:02:12 -0700223 /* Try a single uclass */
224 ut_assertok(env_set("boot_targets", NULL));
225 ut_assertok(bootflow_scan_first(NULL, "mmc", &iter, 0, &bflow));
226 ut_asserteq(2, iter.num_devs);
227
Simon Glassb51f01d2023-10-23 00:02:13 -0700228 /* Now scan past mmc1 and make sure that only mmc0 shows up */
229 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
230 ut_asserteq(3, iter.num_devs);
231 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
232 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
233 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
234 bootflow_iter_uninit(&iter);
235
236 /* Try a single uclass with boot_targets */
237 ut_assertok(env_set("boot_targets", "mmc"));
238 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
239 ut_asserteq(2, iter.num_devs);
240
241 /* Now scan past mmc1 and make sure that only mmc0 shows up */
Simon Glass33795a92023-10-23 00:02:12 -0700242 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
243 ut_asserteq(3, iter.num_devs);
244 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
245 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
246 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
247 bootflow_iter_uninit(&iter);
248
Simon Glassb51f01d2023-10-23 00:02:13 -0700249 /* Try a single uclass with boot_targets */
250 ut_assertok(env_set("boot_targets", "mmc usb"));
251 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
252 ut_asserteq(2, iter.num_devs);
253
254 /* Now scan past mmc1 and make sure that the 3 USB devices show up */
255 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
256 ut_asserteq(6, iter.num_devs);
257 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
258 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
259 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
260 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
261 iter.dev_used[3]->name);
262 bootflow_iter_uninit(&iter);
263
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700264 return 0;
265}
Simon Glass1a92f832024-08-22 07:57:48 -0600266BOOTSTD_TEST(bootdev_test_order, UTF_DM | UTF_SCAN_FDT);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700267
268/* Check default bootdev ordering */
269static int bootdev_test_order_default(struct unit_test_state *uts)
270{
271 struct bootflow_iter iter;
272 struct bootflow bflow;
273
Simon Glassb255efc2022-04-24 23:31:24 -0600274 /*
275 * Now drop both orderings, to check the default (prioriy/sequence)
276 * ordering
277 */
278 ut_assertok(env_set("boot_targets", NULL));
279 ut_assertok(bootstd_test_drop_bootdev_order(uts));
280
Simon Glass5d3d44f2023-01-17 10:48:16 -0700281 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700282 ut_asserteq(2, iter.num_devs);
283 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
284 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600285
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700286 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600287 ut_asserteq(3, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700288 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600289 bootflow_iter_uninit(&iter);
290
291 return 0;
292}
Simon Glass1a92f832024-08-22 07:57:48 -0600293BOOTSTD_TEST(bootdev_test_order_default, UTF_DM | UTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600294
295/* Check bootdev ordering with the uclass priority */
296static int bootdev_test_prio(struct unit_test_state *uts)
297{
298 struct bootdev_uc_plat *ucp;
299 struct bootflow_iter iter;
300 struct bootflow bflow;
301 struct udevice *blk;
302
Simon Glassf3cb89f2023-01-17 10:47:37 -0700303 test_set_skip_delays(true);
304
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700305 /* disable ethernet since the hunter will run dhcp */
306 test_set_eth_enable(false);
307
Simon Glassb255efc2022-04-24 23:31:24 -0600308 /* Start up USB which gives us three additional bootdevs */
Simon Glass22c93da2024-09-01 16:26:16 -0600309 bootstd_reset_usb();
Simon Glassb255efc2022-04-24 23:31:24 -0600310 ut_assertok(run_command("usb start", 0));
311
312 ut_assertok(bootstd_test_drop_bootdev_order(uts));
313
314 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
Simon Glass5d3d44f2023-01-17 10:48:16 -0700315 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700316 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600317 ut_asserteq(6, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700318 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600319 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700320 iter.dev_used[3]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600321
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700322 ut_assertok(bootdev_get_sibling_blk(iter.dev_used[3], &blk));
Simon Glassb255efc2022-04-24 23:31:24 -0600323 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
324
325 /* adjust the priority of the first USB bootdev to the highest */
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700326 ucp = dev_get_uclass_plat(iter.dev_used[3]);
327 ucp->prio = BOOTDEVP_1_PRE_SCAN;
Simon Glassb255efc2022-04-24 23:31:24 -0600328
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700329 /* try again but enable hunting, which brings in SCSI */
Simon Glassb255efc2022-04-24 23:31:24 -0600330 bootflow_iter_uninit(&iter);
Simon Glass99e68182023-02-22 12:17:03 -0700331 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWIF_HUNT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700332 &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700333 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
334 ut_asserteq(7, iter.num_devs);
Simon Glassb255efc2022-04-24 23:31:24 -0600335 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700336 iter.dev_used[0]->name);
337 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600338
339 return 0;
340}
Simon Glass1a92f832024-08-22 07:57:48 -0600341BOOTSTD_TEST(bootdev_test_prio, UTF_DM | UTF_SCAN_FDT);
Simon Glass5acb97a2023-01-17 10:47:33 -0700342
343/* Check listing hunters */
344static int bootdev_test_hunter(struct unit_test_state *uts)
345{
346 struct bootstd_priv *std;
347
Simon Glass22c93da2024-09-01 16:26:16 -0600348 bootstd_reset_usb();
Simon Glassf3cb89f2023-01-17 10:47:37 -0700349 test_set_skip_delays(true);
350
Simon Glass5acb97a2023-01-17 10:47:33 -0700351 /* get access to the used hunters */
352 ut_assertok(bootstd_get_priv(&std));
353
Simon Glass5acb97a2023-01-17 10:47:33 -0700354 bootdev_list_hunters(std);
355 ut_assert_nextline("Prio Used Uclass Hunter");
356 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700357 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700358 ut_assert_nextline(" 1 simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700359 ut_assert_nextline(" 5 ide ide_bootdev");
360 ut_assert_nextline(" 2 mmc mmc_bootdev");
361 ut_assert_nextline(" 4 nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700362 ut_assert_nextline(" 4 qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700363 ut_assert_nextline(" 4 scsi scsi_bootdev");
364 ut_assert_nextline(" 4 spi_flash sf_bootdev");
365 ut_assert_nextline(" 5 usb usb_bootdev");
366 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700367 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700368 ut_assert_console_end();
369
Simon Glassf3cb89f2023-01-17 10:47:37 -0700370 ut_assertok(bootdev_hunt("usb1", false));
371 ut_assert_nextline(
372 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass1248d2b2023-01-17 10:47:34 -0700373 ut_assert_console_end();
374
Simon Glassdeffa692023-01-28 15:00:28 -0700375 /* USB is 7th in the list, so bit 8 */
376 ut_asserteq(BIT(8), std->hunters_used);
Simon Glassf3cb89f2023-01-17 10:47:37 -0700377
Simon Glass5acb97a2023-01-17 10:47:33 -0700378 return 0;
379}
Simon Glassf0425022024-08-22 07:57:54 -0600380BOOTSTD_TEST(bootdev_test_hunter, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass5acb97a2023-01-17 10:47:33 -0700381
382/* Check 'bootdev hunt' command */
383static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
384{
385 struct bootstd_priv *std;
386
Simon Glassf3cb89f2023-01-17 10:47:37 -0700387 test_set_skip_delays(true);
Simon Glass22c93da2024-09-01 16:26:16 -0600388 bootstd_reset_usb();
Simon Glassf3cb89f2023-01-17 10:47:37 -0700389
Simon Glass5acb97a2023-01-17 10:47:33 -0700390 /* get access to the used hunters */
391 ut_assertok(bootstd_get_priv(&std));
392
Simon Glass5acb97a2023-01-17 10:47:33 -0700393 ut_assertok(run_command("bootdev hunt -l", 0));
394 ut_assert_nextline("Prio Used Uclass Hunter");
395 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700396 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700397 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700398 ut_assert_console_end();
399
Simon Glass7e1f6a42023-01-17 10:48:08 -0700400 /* Use the MMC hunter and see that it updates */
401 ut_assertok(run_command("bootdev hunt mmc", 0));
402 ut_assertok(run_command("bootdev hunt -l", 0));
403 ut_assert_skip_to_line(" 5 ide ide_bootdev");
404 ut_assert_nextline(" 2 * mmc mmc_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700405 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700406 ut_assert_console_end();
407
Simon Glass5acb97a2023-01-17 10:47:33 -0700408 /* Scan all hunters */
Simon Glassac06b26a2023-01-17 10:48:10 -0700409 test_set_eth_enable(false);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700410 test_set_skip_delays(true);
Simon Glass5acb97a2023-01-17 10:47:33 -0700411 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700412 ut_assert_nextline("Hunting with: ethernet");
Simon Glasse4cf1062023-01-17 10:48:13 -0700413
414 /* This is the extension feature which has no uclass at present */
415 ut_assert_nextline("Hunting with: simple_bus");
416 ut_assert_nextline("Found 2 extension board(s).");
Simon Glassbe7c6662023-01-17 10:47:46 -0700417 ut_assert_nextline("Hunting with: ide");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700418
419 /* mmc hunter has already been used so should not run again */
420
Simon Glass08382872023-01-17 10:47:47 -0700421 ut_assert_nextline("Hunting with: nvme");
Simon Glassdeffa692023-01-28 15:00:28 -0700422 ut_assert_nextline("Hunting with: qfw");
Simon Glass9985e9c2023-01-17 10:47:45 -0700423 ut_assert_nextline("Hunting with: scsi");
424 ut_assert_nextline("scanning bus for devices...");
Simon Glass215be682023-01-17 10:48:03 -0700425 ut_assert_skip_to_line("Hunting with: spi_flash");
426 ut_assert_nextline("Hunting with: usb");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700427 ut_assert_nextline(
428 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass215be682023-01-17 10:48:03 -0700429 ut_assert_nextline("Hunting with: virtio");
Simon Glass5acb97a2023-01-17 10:47:33 -0700430 ut_assert_console_end();
431
432 /* List available hunters */
433 ut_assertok(run_command("bootdev hunt -l", 0));
434 ut_assert_nextlinen("Prio");
435 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700436 ut_assert_nextline(" 6 * ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700437 ut_assert_nextline(" 1 * simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700438 ut_assert_nextline(" 5 * ide ide_bootdev");
439 ut_assert_nextline(" 2 * mmc mmc_bootdev");
440 ut_assert_nextline(" 4 * nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700441 ut_assert_nextline(" 4 * qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700442 ut_assert_nextline(" 4 * scsi scsi_bootdev");
443 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
444 ut_assert_nextline(" 5 * usb usb_bootdev");
445 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700446 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700447 ut_assert_console_end();
448
Simon Glasse4cf1062023-01-17 10:48:13 -0700449 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass5acb97a2023-01-17 10:47:33 -0700450
451 return 0;
452}
Simon Glassf0425022024-08-22 07:57:54 -0600453BOOTSTD_TEST(bootdev_test_cmd_hunt, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
454 UTF_CONSOLE);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700455
Simon Glass484e4072023-01-17 10:48:14 -0700456/* Check searching for bootdevs using the hunters */
457static int bootdev_test_hunt_scan(struct unit_test_state *uts)
458{
459 struct bootflow_iter iter;
460 struct bootstd_priv *std;
461 struct bootflow bflow;
462
463 /* get access to the used hunters */
464 ut_assertok(bootstd_get_priv(&std));
465
466 ut_assertok(bootstd_test_drop_bootdev_order(uts));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700467 ut_assertok(bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700468 BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT |
469 BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glass484e4072023-01-17 10:48:14 -0700470 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
471
472 return 0;
473}
Simon Glass1a92f832024-08-22 07:57:48 -0600474BOOTSTD_TEST(bootdev_test_hunt_scan, UTF_DM | UTF_SCAN_FDT);
Simon Glass484e4072023-01-17 10:48:14 -0700475
Simon Glass64cbc5c2023-01-17 10:47:42 -0700476/* Check that only bootable partitions are processed */
477static int bootdev_test_bootable(struct unit_test_state *uts)
478{
479 struct bootflow_iter iter;
480 struct bootflow bflow;
481 struct udevice *blk;
482
483 memset(&iter, '\0', sizeof(iter));
484 memset(&bflow, '\0', sizeof(bflow));
485 iter.part = 0;
486 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
487 iter.dev = blk;
488 ut_assertok(device_find_next_child(&iter.dev));
489 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
490
491 /*
492 * initially we don't have any knowledge of which partitions are
493 * bootable, but mmc1 has two partitions, with the first one being
494 * bootable
495 */
496 iter.part = 2;
497 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
498 ut_asserteq(0, iter.first_bootable);
499
500 /* scan with part == 0 to get the partition info */
501 iter.part = 0;
502 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
503 ut_asserteq(1, iter.first_bootable);
504
505 /* now it will refuse to use non-bootable partitions */
506 iter.part = 2;
507 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
508
509 return 0;
510}
Simon Glass1a92f832024-08-22 07:57:48 -0600511BOOTSTD_TEST(bootdev_test_bootable, UTF_DM | UTF_SCAN_FDT);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700512
513/* Check hunting for bootdev of a particular priority */
514static int bootdev_test_hunt_prio(struct unit_test_state *uts)
515{
Simon Glass22c93da2024-09-01 16:26:16 -0600516 bootstd_reset_usb();
Simon Glass3e9f6be2023-01-17 10:48:07 -0700517 test_set_skip_delays(true);
518
Simon Glass7e1f6a42023-01-17 10:48:08 -0700519 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700520 ut_assert_nextline("scanning bus for devices...");
521 ut_assert_skip_to_line(" Type: Hard Disk");
522 ut_assert_nextlinen(" Capacity:");
523 ut_assert_console_end();
524
525 /* now try a different priority, verbosely */
Simon Glass7e1f6a42023-01-17 10:48:08 -0700526 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700527 ut_assert_nextline("Hunting with: ide");
Simon Glass3e9f6be2023-01-17 10:48:07 -0700528 ut_assert_nextline("Hunting with: usb");
529 ut_assert_nextline(
530 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
531 ut_assert_console_end();
532
533 return 0;
534}
Simon Glassf0425022024-08-22 07:57:54 -0600535BOOTSTD_TEST(bootdev_test_hunt_prio, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassde567b12023-01-17 10:48:09 -0700536
537/* Check hunting for bootdevs with a particular label */
538static int bootdev_test_hunt_label(struct unit_test_state *uts)
539{
540 struct udevice *dev, *old;
541 struct bootstd_priv *std;
542 int mflags;
543
Simon Glass22c93da2024-09-01 16:26:16 -0600544 bootstd_reset_usb();
Simon Glass4bb7c162023-05-05 20:03:04 -0600545
Simon Glassde567b12023-01-17 10:48:09 -0700546 /* get access to the used hunters */
547 ut_assertok(bootstd_get_priv(&std));
548
549 /* scan an unknown uclass */
Simon Glassde567b12023-01-17 10:48:09 -0700550 old = (void *)&mflags; /* arbitrary pointer to check against dev */
551 dev = old;
552 mflags = 123;
Simon Glass4108c6b2023-04-24 13:49:47 +1200553 ut_asserteq(-EPFNOSUPPORT,
Simon Glassde567b12023-01-17 10:48:09 -0700554 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700555 ut_asserteq_ptr(old, dev);
556 ut_asserteq(123, mflags);
557 ut_assert_console_end();
558 ut_asserteq(0, std->hunters_used);
559
560 /* scan an invalid mmc controllers */
561 ut_asserteq(-ENOENT,
562 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
563 ut_asserteq_ptr(old, dev);
564 ut_asserteq(123, mflags);
Simon Glassde567b12023-01-17 10:48:09 -0700565 ut_assert_console_end();
566
567 ut_assertok(bootstd_test_check_mmc_hunter(uts));
568
569 /* scan for a particular mmc controller */
570 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
571 ut_assertnonnull(dev);
572 ut_asserteq_str("mmc1.bootdev", dev->name);
573 ut_asserteq(0, mflags);
574 ut_assert_console_end();
575
576 /* scan all of usb */
577 test_set_skip_delays(true);
578 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
579 ut_assertnonnull(dev);
580 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
581 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
582 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
583 ut_assert_console_end();
584
585 return 0;
586}
Simon Glassf0425022024-08-22 07:57:54 -0600587BOOTSTD_TEST(bootdev_test_hunt_label, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassac06b26a2023-01-17 10:48:10 -0700588
589/* Check iterating to the next label in a list */
590static int bootdev_test_next_label(struct unit_test_state *uts)
591{
592 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
593 struct bootflow_iter iter;
594 struct bootstd_priv *std;
595 struct bootflow bflow;
596 struct udevice *dev;
597 int mflags;
598
599 test_set_eth_enable(false);
600
601 /* get access to the used hunters */
602 ut_assertok(bootstd_get_priv(&std));
603
604 memset(&iter, '\0', sizeof(iter));
605 memset(&bflow, '\0', sizeof(bflow));
606 iter.part = 0;
607 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
608 iter.cur_label = -1;
609 iter.labels = labels;
610
611 dev = NULL;
612 mflags = 123;
613 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
Simon Glassac06b26a2023-01-17 10:48:10 -0700614 ut_assert_console_end();
615 ut_assertnonnull(dev);
616 ut_asserteq_str("mmc0.bootdev", dev->name);
617 ut_asserteq(0, mflags);
618
619 ut_assertok(bootstd_test_check_mmc_hunter(uts));
620
621 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
622 ut_assert_nextline("scanning bus for devices...");
623 ut_assert_skip_to_line(
624 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
625 ut_assert_console_end();
626 ut_assertnonnull(dev);
627 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
628 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
629
Simon Glassdeffa692023-01-28 15:00:28 -0700630 /* SCSI is 7th in the list, so bit 6 */
631 ut_asserteq(BIT(MMC_HUNTER) | BIT(6), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700632
633 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
634 ut_assert_console_end();
635 ut_assertnonnull(dev);
636 ut_asserteq_str("eth@10002000.bootdev", dev->name);
637 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
638 mflags);
639
640 /* dhcp: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700641 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700642
643 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
644 ut_assert_console_end();
645 ut_assertnonnull(dev);
646 ut_asserteq_str("eth@10002000.bootdev", dev->name);
647 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
648 mflags);
649
650 /* pxe: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700651 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700652
653 mflags = 123;
654 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
655 ut_asserteq(123, mflags);
656 ut_assert_console_end();
657
658 /* no change */
Simon Glassdeffa692023-01-28 15:00:28 -0700659 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700660
661 return 0;
662}
Simon Glass1a92f832024-08-22 07:57:48 -0600663BOOTSTD_TEST(bootdev_test_next_label, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
Simon Glassf0425022024-08-22 07:57:54 -0600664 UTF_SF_BOOTDEV | UTF_CONSOLE);
Simon Glass660a9952023-01-17 10:48:11 -0700665
Simon Glass660a9952023-01-17 10:48:11 -0700666/* Check iterating to the next prioirty in a list */
667static int bootdev_test_next_prio(struct unit_test_state *uts)
668{
669 struct bootflow_iter iter;
670 struct bootstd_priv *std;
671 struct bootflow bflow;
672 struct udevice *dev;
673 int ret;
674
Simon Glassba3d5372023-01-17 10:48:15 -0700675 test_set_eth_enable(false);
Simon Glass660a9952023-01-17 10:48:11 -0700676 test_set_skip_delays(true);
677
678 /* get access to the used hunters */
679 ut_assertok(bootstd_get_priv(&std));
680
681 memset(&iter, '\0', sizeof(iter));
682 memset(&bflow, '\0', sizeof(bflow));
683 iter.part = 0;
684 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
685 iter.cur_prio = 0;
Simon Glass99e68182023-02-22 12:17:03 -0700686 iter.flags = BOOTFLOWIF_SHOW;
Simon Glass660a9952023-01-17 10:48:11 -0700687
688 dev = NULL;
Simon Glass660a9952023-01-17 10:48:11 -0700689 ut_assertok(bootdev_next_prio(&iter, &dev));
690 ut_assertnonnull(dev);
691 ut_asserteq_str("mmc2.bootdev", dev->name);
692
693 /* hunt flag not set, so this should not use any hunters */
694 ut_asserteq(0, std->hunters_used);
695 ut_assert_console_end();
696
697 /* now try again with hunting enabled */
Simon Glass99e68182023-02-22 12:17:03 -0700698 iter.flags = BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT;
Simon Glass660a9952023-01-17 10:48:11 -0700699 iter.cur_prio = 0;
700 iter.part = 0;
701
702 ut_assertok(bootdev_next_prio(&iter, &dev));
703 ut_asserteq_str("mmc2.bootdev", dev->name);
Simon Glasse4cf1062023-01-17 10:48:13 -0700704 ut_assert_nextline("Hunting with: simple_bus");
705 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass660a9952023-01-17 10:48:11 -0700706 ut_assert_nextline("Hunting with: mmc");
707 ut_assert_console_end();
708
Simon Glasse4cf1062023-01-17 10:48:13 -0700709 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700710
711 ut_assertok(bootdev_next_prio(&iter, &dev));
712 ut_asserteq_str("mmc1.bootdev", dev->name);
713
714 ut_assertok(bootdev_next_prio(&iter, &dev));
715 ut_asserteq_str("mmc0.bootdev", dev->name);
716 ut_assert_console_end();
717
718 ut_assertok(bootdev_next_prio(&iter, &dev));
719 ut_asserteq_str("spi.bin@0.bootdev", dev->name);
720 ut_assert_skip_to_line("Hunting with: spi_flash");
721
722 /*
723 * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it
724 * starts looking at the devices, so we se virtio as well
725 */
726 ut_assert_nextline("Hunting with: virtio");
727 ut_assert_nextlinen("SF: Detected m25p16");
728
729 ut_assertok(bootdev_next_prio(&iter, &dev));
730 ut_asserteq_str("spi.bin@1.bootdev", dev->name);
731 ut_assert_nextlinen("SF: Detected m25p16");
732 ut_assert_console_end();
733
734 /* keep going until there are no more bootdevs */
735 do {
736 ret = bootdev_next_prio(&iter, &dev);
737 } while (!ret);
738 ut_asserteq(-ENODEV, ret);
739 ut_assertnull(dev);
Simon Glasse4cf1062023-01-17 10:48:13 -0700740 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700741
742 ut_assert_skip_to_line("Hunting with: ethernet");
743 ut_assert_console_end();
744
745 return 0;
746}
Simon Glassf0425022024-08-22 07:57:54 -0600747BOOTSTD_TEST(bootdev_test_next_prio, UTF_DM | UTF_SCAN_FDT | UTF_SF_BOOTDEV |
748 UTF_CONSOLE);