blob: 23ebc61ff52e18972b8eacfed38e8ff5c8699cf3 [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
Simon Glassb255efc2022-04-24 23:31:24 -060031 for (probed = 0; probed < 2; probed++) {
32 int probe_ch = probed ? '+' : ' ';
33
34 ut_assertok(run_command(probed ? "bootdev list -p" :
35 "bootdev list", 0));
36 ut_assert_nextline("Seq Probed Status Uclass Name");
37 ut_assert_nextlinen("---");
38 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 0, probe_ch, "OK",
39 "mmc", "mmc2.bootdev");
40 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 1, probe_ch, "OK",
41 "mmc", "mmc1.bootdev");
42 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 2, probe_ch, "OK",
43 "mmc", "mmc0.bootdev");
44 ut_assert_nextlinen("---");
45 ut_assert_nextline("(3 bootdevs)");
46 ut_assert_console_end();
47 }
48
49 return 0;
50}
Simon Glassf0425022024-08-22 07:57:54 -060051BOOTSTD_TEST(bootdev_test_cmd_list, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -060052
53/* Check 'bootdev select' and 'info' commands */
54static int bootdev_test_cmd_select(struct unit_test_state *uts)
55{
56 struct bootstd_priv *std;
57
58 /* get access to the CLI's cur_bootdev */
59 ut_assertok(bootstd_get_priv(&std));
60
Simon Glassb255efc2022-04-24 23:31:24 -060061 ut_asserteq(1, run_command("bootdev info", 0));
62 ut_assert_nextlinen("Please use");
63 ut_assert_console_end();
64
65 /* select by sequence */
66 ut_assertok(run_command("bootdev select 0", 0));
67 ut_assert_console_end();
68
69 ut_assertok(run_command("bootdev info", 0));
70 ut_assert_nextline("Name: mmc2.bootdev");
71 ut_assert_nextline("Sequence: 0");
72 ut_assert_nextline("Status: Probed");
73 ut_assert_nextline("Uclass: mmc");
74 ut_assert_nextline("Bootflows: 0 (0 valid)");
75 ut_assert_console_end();
76
77 /* select by bootdev name */
78 ut_assertok(run_command("bootdev select mmc1.bootdev", 0));
79 ut_assert_console_end();
80 ut_assertnonnull(std->cur_bootdev);
81 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
82
83 /* select by bootdev label*/
84 ut_assertok(run_command("bootdev select mmc1", 0));
85 ut_assert_console_end();
86 ut_assertnonnull(std->cur_bootdev);
87 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
88
89 /* deselect */
90 ut_assertok(run_command("bootdev select", 0));
91 ut_assert_console_end();
92 ut_assertnull(std->cur_bootdev);
93
94 ut_asserteq(1, run_command("bootdev info", 0));
95 ut_assert_nextlinen("Please use");
96 ut_assert_console_end();
97
98 return 0;
99}
Simon Glassf0425022024-08-22 07:57:54 -0600100BOOTSTD_TEST(bootdev_test_cmd_select, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600101
102/* Check bootdev labels */
103static int bootdev_test_labels(struct unit_test_state *uts)
104{
105 struct udevice *dev, *media;
Simon Glasse22fe922023-01-17 10:48:05 -0700106 int mflags = 0;
Simon Glassb255efc2022-04-24 23:31:24 -0600107
Simon Glasse22fe922023-01-17 10:48:05 -0700108 ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600109 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
Simon Glasse22fe922023-01-17 10:48:05 -0700110 ut_asserteq(0, mflags);
Simon Glassb255efc2022-04-24 23:31:24 -0600111 media = dev_get_parent(dev);
112 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
113 ut_asserteq_str("mmc2", media->name);
114
Simon Glasse22fe922023-01-17 10:48:05 -0700115 /* Check method flags */
116 ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700117 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
118 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700119 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700120 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
121 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700122
Simon Glassb255efc2022-04-24 23:31:24 -0600123 /* Check invalid uclass */
Simon Glass4108c6b2023-04-24 13:49:47 +1200124 ut_asserteq(-EPFNOSUPPORT,
125 bootdev_find_by_label("fred0", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600126
127 /* Check unknown sequence number */
Simon Glasse22fe922023-01-17 10:48:05 -0700128 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600129
130 return 0;
131}
Simon Glass1a92f832024-08-22 07:57:48 -0600132BOOTSTD_TEST(bootdev_test_labels, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600133
Simon Glassde567b12023-01-17 10:48:09 -0700134/* Check bootdev_find_by_any() */
135static int bootdev_test_any(struct unit_test_state *uts)
136{
137 struct udevice *dev, *media;
138 int mflags;
139
140 /*
141 * with ethernet enabled we have 8 devices ahead of the mmc ones:
142 *
143 * ut_assertok(run_command("bootdev list", 0));
144 * Seq Probed Status Uclass Name
145 * --- ------ ------ -------- ------------------
146 * 0 [ + ] OK ethernet eth@10002000.bootdev
147 * 1 [ ] OK ethernet eth@10003000.bootdev
148 * 2 [ ] OK ethernet sbe5.bootdev
149 * 3 [ ] OK ethernet eth@10004000.bootdev
150 * 4 [ ] OK ethernet phy-test-eth.bootdev
151 * 5 [ ] OK ethernet dsa-test-eth.bootdev
152 * 6 [ ] OK ethernet dsa-test@0.bootdev
153 * 7 [ ] OK ethernet dsa-test@1.bootdev
154 * 8 [ ] OK mmc mmc2.bootdev
155 * 9 [ + ] OK mmc mmc1.bootdev
156 * a [ ] OK mmc mmc0.bootdev
157 */
Simon Glassde567b12023-01-17 10:48:09 -0700158 ut_assertok(bootdev_find_by_any("8", &dev, &mflags));
159 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
160 ut_asserteq(BOOTFLOW_METHF_SINGLE_DEV, mflags);
161 media = dev_get_parent(dev);
162 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
163 ut_asserteq_str("mmc2", media->name);
164 ut_assert_console_end();
165
166 /* there should not be this many bootdevs */
167 ut_asserteq(-ENODEV, bootdev_find_by_any("50", &dev, &mflags));
168 ut_assert_nextline("Cannot find '50' (err=-19)");
169 ut_assert_console_end();
170
171 /* Check method flags */
172 ut_assertok(bootdev_find_by_any("pxe", &dev, &mflags));
173 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
174 mflags);
175
176 /* Check invalid uclass */
177 mflags = 123;
Simon Glass4108c6b2023-04-24 13:49:47 +1200178 ut_asserteq(-EPFNOSUPPORT, bootdev_find_by_any("fred0", &dev, &mflags));
179 ut_assert_nextline("Cannot find bootdev 'fred0' (err=-96)");
Simon Glassde567b12023-01-17 10:48:09 -0700180 ut_asserteq(123, mflags);
181 ut_assert_console_end();
182
183 return 0;
184}
Simon Glassf0425022024-08-22 07:57:54 -0600185BOOTSTD_TEST(bootdev_test_any, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
186 UTF_CONSOLE);
Simon Glassde567b12023-01-17 10:48:09 -0700187
Simon Glassfd5a5192023-10-23 00:02:11 -0700188/*
189 * Check bootdev ordering with the bootdev-order property and boot_targets
190 * environment variable
191 */
Simon Glassb255efc2022-04-24 23:31:24 -0600192static int bootdev_test_order(struct unit_test_state *uts)
193{
194 struct bootflow_iter iter;
195 struct bootflow bflow;
196
Simon Glassfd5a5192023-10-23 00:02:11 -0700197 test_set_skip_delays(true);
198
199 /* Start up USB which gives us three additional bootdevs */
200 usb_started = false;
201 ut_assertok(run_command("usb start", 0));
202
Simon Glassb255efc2022-04-24 23:31:24 -0600203 /*
204 * First try the order set by the bootdev-order property
205 * Like all sandbox unit tests this relies on the devicetree setting up
206 * the required devices:
207 *
208 * mmc0 - nothing connected
209 * mmc1 - connected to mmc1.img file
210 * mmc2 - nothing connected
211 */
212 ut_assertok(env_set("boot_targets", NULL));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700213 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600214 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700215 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
216 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600217 bootflow_iter_uninit(&iter);
218
219 /* Use the environment variable to override it */
Simon Glassfd5a5192023-10-23 00:02:11 -0700220 ut_assertok(env_set("boot_targets", "mmc1 mmc2 usb"));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700221 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700222 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb51f01d2023-10-23 00:02:13 -0700223 ut_asserteq(5, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700224 ut_asserteq_str("mmc1.bootdev", iter.dev_used[0]->name);
225 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassfd5a5192023-10-23 00:02:11 -0700226 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
227 iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600228 bootflow_iter_uninit(&iter);
229
Simon Glass33795a92023-10-23 00:02:12 -0700230 /* Try a single uclass */
231 ut_assertok(env_set("boot_targets", NULL));
232 ut_assertok(bootflow_scan_first(NULL, "mmc", &iter, 0, &bflow));
233 ut_asserteq(2, iter.num_devs);
234
Simon Glassb51f01d2023-10-23 00:02:13 -0700235 /* Now scan past mmc1 and make sure that only mmc0 shows up */
236 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
237 ut_asserteq(3, iter.num_devs);
238 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
239 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
240 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
241 bootflow_iter_uninit(&iter);
242
243 /* Try a single uclass with boot_targets */
244 ut_assertok(env_set("boot_targets", "mmc"));
245 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
246 ut_asserteq(2, iter.num_devs);
247
248 /* Now scan past mmc1 and make sure that only mmc0 shows up */
Simon Glass33795a92023-10-23 00:02:12 -0700249 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
250 ut_asserteq(3, iter.num_devs);
251 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
252 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
253 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
254 bootflow_iter_uninit(&iter);
255
Simon Glassb51f01d2023-10-23 00:02:13 -0700256 /* Try a single uclass with boot_targets */
257 ut_assertok(env_set("boot_targets", "mmc usb"));
258 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
259 ut_asserteq(2, iter.num_devs);
260
261 /* Now scan past mmc1 and make sure that the 3 USB devices show up */
262 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
263 ut_asserteq(6, iter.num_devs);
264 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
265 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
266 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
267 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
268 iter.dev_used[3]->name);
269 bootflow_iter_uninit(&iter);
270
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700271 return 0;
272}
Simon Glass1a92f832024-08-22 07:57:48 -0600273BOOTSTD_TEST(bootdev_test_order, UTF_DM | UTF_SCAN_FDT);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700274
275/* Check default bootdev ordering */
276static int bootdev_test_order_default(struct unit_test_state *uts)
277{
278 struct bootflow_iter iter;
279 struct bootflow bflow;
280
Simon Glassb255efc2022-04-24 23:31:24 -0600281 /*
282 * Now drop both orderings, to check the default (prioriy/sequence)
283 * ordering
284 */
285 ut_assertok(env_set("boot_targets", NULL));
286 ut_assertok(bootstd_test_drop_bootdev_order(uts));
287
Simon Glass5d3d44f2023-01-17 10:48:16 -0700288 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700289 ut_asserteq(2, iter.num_devs);
290 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
291 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600292
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700293 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600294 ut_asserteq(3, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700295 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600296 bootflow_iter_uninit(&iter);
297
298 return 0;
299}
Simon Glass1a92f832024-08-22 07:57:48 -0600300BOOTSTD_TEST(bootdev_test_order_default, UTF_DM | UTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600301
302/* Check bootdev ordering with the uclass priority */
303static int bootdev_test_prio(struct unit_test_state *uts)
304{
305 struct bootdev_uc_plat *ucp;
306 struct bootflow_iter iter;
307 struct bootflow bflow;
308 struct udevice *blk;
309
Simon Glassf3cb89f2023-01-17 10:47:37 -0700310 test_set_skip_delays(true);
311
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700312 /* disable ethernet since the hunter will run dhcp */
313 test_set_eth_enable(false);
314
Simon Glassb255efc2022-04-24 23:31:24 -0600315 /* Start up USB which gives us three additional bootdevs */
316 usb_started = false;
317 ut_assertok(run_command("usb start", 0));
318
319 ut_assertok(bootstd_test_drop_bootdev_order(uts));
320
321 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
Simon Glass5d3d44f2023-01-17 10:48:16 -0700322 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700323 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600324 ut_asserteq(6, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700325 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600326 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700327 iter.dev_used[3]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600328
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700329 ut_assertok(bootdev_get_sibling_blk(iter.dev_used[3], &blk));
Simon Glassb255efc2022-04-24 23:31:24 -0600330 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
331
332 /* adjust the priority of the first USB bootdev to the highest */
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700333 ucp = dev_get_uclass_plat(iter.dev_used[3]);
334 ucp->prio = BOOTDEVP_1_PRE_SCAN;
Simon Glassb255efc2022-04-24 23:31:24 -0600335
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700336 /* try again but enable hunting, which brings in SCSI */
Simon Glassb255efc2022-04-24 23:31:24 -0600337 bootflow_iter_uninit(&iter);
Simon Glass99e68182023-02-22 12:17:03 -0700338 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWIF_HUNT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700339 &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700340 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
341 ut_asserteq(7, iter.num_devs);
Simon Glassb255efc2022-04-24 23:31:24 -0600342 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700343 iter.dev_used[0]->name);
344 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600345
346 return 0;
347}
Simon Glass1a92f832024-08-22 07:57:48 -0600348BOOTSTD_TEST(bootdev_test_prio, UTF_DM | UTF_SCAN_FDT);
Simon Glass5acb97a2023-01-17 10:47:33 -0700349
350/* Check listing hunters */
351static int bootdev_test_hunter(struct unit_test_state *uts)
352{
353 struct bootstd_priv *std;
354
Simon Glass4bb7c162023-05-05 20:03:04 -0600355 usb_started = false;
Simon Glassf3cb89f2023-01-17 10:47:37 -0700356 test_set_skip_delays(true);
357
Simon Glass5acb97a2023-01-17 10:47:33 -0700358 /* get access to the used hunters */
359 ut_assertok(bootstd_get_priv(&std));
360
Simon Glass5acb97a2023-01-17 10:47:33 -0700361 bootdev_list_hunters(std);
362 ut_assert_nextline("Prio Used Uclass Hunter");
363 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700364 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700365 ut_assert_nextline(" 1 simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700366 ut_assert_nextline(" 5 ide ide_bootdev");
367 ut_assert_nextline(" 2 mmc mmc_bootdev");
368 ut_assert_nextline(" 4 nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700369 ut_assert_nextline(" 4 qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700370 ut_assert_nextline(" 4 scsi scsi_bootdev");
371 ut_assert_nextline(" 4 spi_flash sf_bootdev");
372 ut_assert_nextline(" 5 usb usb_bootdev");
373 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700374 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700375 ut_assert_console_end();
376
Simon Glassf3cb89f2023-01-17 10:47:37 -0700377 ut_assertok(bootdev_hunt("usb1", false));
378 ut_assert_nextline(
379 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass1248d2b2023-01-17 10:47:34 -0700380 ut_assert_console_end();
381
Simon Glassdeffa692023-01-28 15:00:28 -0700382 /* USB is 7th in the list, so bit 8 */
383 ut_asserteq(BIT(8), std->hunters_used);
Simon Glassf3cb89f2023-01-17 10:47:37 -0700384
Simon Glass5acb97a2023-01-17 10:47:33 -0700385 return 0;
386}
Simon Glassf0425022024-08-22 07:57:54 -0600387BOOTSTD_TEST(bootdev_test_hunter, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass5acb97a2023-01-17 10:47:33 -0700388
389/* Check 'bootdev hunt' command */
390static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
391{
392 struct bootstd_priv *std;
393
Simon Glassf3cb89f2023-01-17 10:47:37 -0700394 test_set_skip_delays(true);
Simon Glass4bb7c162023-05-05 20:03:04 -0600395 usb_started = false;
Simon Glassf3cb89f2023-01-17 10:47:37 -0700396
Simon Glass5acb97a2023-01-17 10:47:33 -0700397 /* get access to the used hunters */
398 ut_assertok(bootstd_get_priv(&std));
399
Simon Glass5acb97a2023-01-17 10:47:33 -0700400 ut_assertok(run_command("bootdev hunt -l", 0));
401 ut_assert_nextline("Prio Used Uclass Hunter");
402 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700403 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700404 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700405 ut_assert_console_end();
406
Simon Glass7e1f6a42023-01-17 10:48:08 -0700407 /* Use the MMC hunter and see that it updates */
408 ut_assertok(run_command("bootdev hunt mmc", 0));
409 ut_assertok(run_command("bootdev hunt -l", 0));
410 ut_assert_skip_to_line(" 5 ide ide_bootdev");
411 ut_assert_nextline(" 2 * mmc mmc_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700412 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700413 ut_assert_console_end();
414
Simon Glass5acb97a2023-01-17 10:47:33 -0700415 /* Scan all hunters */
Simon Glassac06b26a2023-01-17 10:48:10 -0700416 test_set_eth_enable(false);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700417 test_set_skip_delays(true);
Simon Glass5acb97a2023-01-17 10:47:33 -0700418 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700419 ut_assert_nextline("Hunting with: ethernet");
Simon Glasse4cf1062023-01-17 10:48:13 -0700420
421 /* This is the extension feature which has no uclass at present */
422 ut_assert_nextline("Hunting with: simple_bus");
423 ut_assert_nextline("Found 2 extension board(s).");
Simon Glassbe7c6662023-01-17 10:47:46 -0700424 ut_assert_nextline("Hunting with: ide");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700425
426 /* mmc hunter has already been used so should not run again */
427
Simon Glass08382872023-01-17 10:47:47 -0700428 ut_assert_nextline("Hunting with: nvme");
Simon Glassdeffa692023-01-28 15:00:28 -0700429 ut_assert_nextline("Hunting with: qfw");
Simon Glass9985e9c2023-01-17 10:47:45 -0700430 ut_assert_nextline("Hunting with: scsi");
431 ut_assert_nextline("scanning bus for devices...");
Simon Glass215be682023-01-17 10:48:03 -0700432 ut_assert_skip_to_line("Hunting with: spi_flash");
433 ut_assert_nextline("Hunting with: usb");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700434 ut_assert_nextline(
435 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass215be682023-01-17 10:48:03 -0700436 ut_assert_nextline("Hunting with: virtio");
Simon Glass5acb97a2023-01-17 10:47:33 -0700437 ut_assert_console_end();
438
439 /* List available hunters */
440 ut_assertok(run_command("bootdev hunt -l", 0));
441 ut_assert_nextlinen("Prio");
442 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700443 ut_assert_nextline(" 6 * ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700444 ut_assert_nextline(" 1 * simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700445 ut_assert_nextline(" 5 * ide ide_bootdev");
446 ut_assert_nextline(" 2 * mmc mmc_bootdev");
447 ut_assert_nextline(" 4 * nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700448 ut_assert_nextline(" 4 * qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700449 ut_assert_nextline(" 4 * scsi scsi_bootdev");
450 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
451 ut_assert_nextline(" 5 * usb usb_bootdev");
452 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700453 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700454 ut_assert_console_end();
455
Simon Glasse4cf1062023-01-17 10:48:13 -0700456 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass5acb97a2023-01-17 10:47:33 -0700457
458 return 0;
459}
Simon Glassf0425022024-08-22 07:57:54 -0600460BOOTSTD_TEST(bootdev_test_cmd_hunt, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
461 UTF_CONSOLE);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700462
Simon Glass484e4072023-01-17 10:48:14 -0700463/* Check searching for bootdevs using the hunters */
464static int bootdev_test_hunt_scan(struct unit_test_state *uts)
465{
466 struct bootflow_iter iter;
467 struct bootstd_priv *std;
468 struct bootflow bflow;
469
470 /* get access to the used hunters */
471 ut_assertok(bootstd_get_priv(&std));
472
473 ut_assertok(bootstd_test_drop_bootdev_order(uts));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700474 ut_assertok(bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700475 BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT |
476 BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glass484e4072023-01-17 10:48:14 -0700477 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
478
479 return 0;
480}
Simon Glass1a92f832024-08-22 07:57:48 -0600481BOOTSTD_TEST(bootdev_test_hunt_scan, UTF_DM | UTF_SCAN_FDT);
Simon Glass484e4072023-01-17 10:48:14 -0700482
Simon Glass64cbc5c2023-01-17 10:47:42 -0700483/* Check that only bootable partitions are processed */
484static int bootdev_test_bootable(struct unit_test_state *uts)
485{
486 struct bootflow_iter iter;
487 struct bootflow bflow;
488 struct udevice *blk;
489
490 memset(&iter, '\0', sizeof(iter));
491 memset(&bflow, '\0', sizeof(bflow));
492 iter.part = 0;
493 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
494 iter.dev = blk;
495 ut_assertok(device_find_next_child(&iter.dev));
496 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
497
498 /*
499 * initially we don't have any knowledge of which partitions are
500 * bootable, but mmc1 has two partitions, with the first one being
501 * bootable
502 */
503 iter.part = 2;
504 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
505 ut_asserteq(0, iter.first_bootable);
506
507 /* scan with part == 0 to get the partition info */
508 iter.part = 0;
509 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
510 ut_asserteq(1, iter.first_bootable);
511
512 /* now it will refuse to use non-bootable partitions */
513 iter.part = 2;
514 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
515
516 return 0;
517}
Simon Glass1a92f832024-08-22 07:57:48 -0600518BOOTSTD_TEST(bootdev_test_bootable, UTF_DM | UTF_SCAN_FDT);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700519
520/* Check hunting for bootdev of a particular priority */
521static int bootdev_test_hunt_prio(struct unit_test_state *uts)
522{
Simon Glass4bb7c162023-05-05 20:03:04 -0600523 usb_started = false;
Simon Glass3e9f6be2023-01-17 10:48:07 -0700524 test_set_skip_delays(true);
525
Simon Glass7e1f6a42023-01-17 10:48:08 -0700526 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700527 ut_assert_nextline("scanning bus for devices...");
528 ut_assert_skip_to_line(" Type: Hard Disk");
529 ut_assert_nextlinen(" Capacity:");
530 ut_assert_console_end();
531
532 /* now try a different priority, verbosely */
Simon Glass7e1f6a42023-01-17 10:48:08 -0700533 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700534 ut_assert_nextline("Hunting with: ide");
Simon Glass3e9f6be2023-01-17 10:48:07 -0700535 ut_assert_nextline("Hunting with: usb");
536 ut_assert_nextline(
537 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
538 ut_assert_console_end();
539
540 return 0;
541}
Simon Glassf0425022024-08-22 07:57:54 -0600542BOOTSTD_TEST(bootdev_test_hunt_prio, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassde567b12023-01-17 10:48:09 -0700543
544/* Check hunting for bootdevs with a particular label */
545static int bootdev_test_hunt_label(struct unit_test_state *uts)
546{
547 struct udevice *dev, *old;
548 struct bootstd_priv *std;
549 int mflags;
550
Simon Glass4bb7c162023-05-05 20:03:04 -0600551 usb_started = false;
552
Simon Glassde567b12023-01-17 10:48:09 -0700553 /* get access to the used hunters */
554 ut_assertok(bootstd_get_priv(&std));
555
556 /* scan an unknown uclass */
Simon Glassde567b12023-01-17 10:48:09 -0700557 old = (void *)&mflags; /* arbitrary pointer to check against dev */
558 dev = old;
559 mflags = 123;
Simon Glass4108c6b2023-04-24 13:49:47 +1200560 ut_asserteq(-EPFNOSUPPORT,
Simon Glassde567b12023-01-17 10:48:09 -0700561 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700562 ut_asserteq_ptr(old, dev);
563 ut_asserteq(123, mflags);
564 ut_assert_console_end();
565 ut_asserteq(0, std->hunters_used);
566
567 /* scan an invalid mmc controllers */
568 ut_asserteq(-ENOENT,
569 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
570 ut_asserteq_ptr(old, dev);
571 ut_asserteq(123, mflags);
Simon Glassde567b12023-01-17 10:48:09 -0700572 ut_assert_console_end();
573
574 ut_assertok(bootstd_test_check_mmc_hunter(uts));
575
576 /* scan for a particular mmc controller */
577 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
578 ut_assertnonnull(dev);
579 ut_asserteq_str("mmc1.bootdev", dev->name);
580 ut_asserteq(0, mflags);
581 ut_assert_console_end();
582
583 /* scan all of usb */
584 test_set_skip_delays(true);
585 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
586 ut_assertnonnull(dev);
587 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
588 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
589 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
590 ut_assert_console_end();
591
592 return 0;
593}
Simon Glassf0425022024-08-22 07:57:54 -0600594BOOTSTD_TEST(bootdev_test_hunt_label, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassac06b26a2023-01-17 10:48:10 -0700595
596/* Check iterating to the next label in a list */
597static int bootdev_test_next_label(struct unit_test_state *uts)
598{
599 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
600 struct bootflow_iter iter;
601 struct bootstd_priv *std;
602 struct bootflow bflow;
603 struct udevice *dev;
604 int mflags;
605
606 test_set_eth_enable(false);
607
608 /* get access to the used hunters */
609 ut_assertok(bootstd_get_priv(&std));
610
611 memset(&iter, '\0', sizeof(iter));
612 memset(&bflow, '\0', sizeof(bflow));
613 iter.part = 0;
614 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
615 iter.cur_label = -1;
616 iter.labels = labels;
617
618 dev = NULL;
619 mflags = 123;
620 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
Simon Glassac06b26a2023-01-17 10:48:10 -0700621 ut_assert_console_end();
622 ut_assertnonnull(dev);
623 ut_asserteq_str("mmc0.bootdev", dev->name);
624 ut_asserteq(0, mflags);
625
626 ut_assertok(bootstd_test_check_mmc_hunter(uts));
627
628 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
629 ut_assert_nextline("scanning bus for devices...");
630 ut_assert_skip_to_line(
631 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
632 ut_assert_console_end();
633 ut_assertnonnull(dev);
634 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
635 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
636
Simon Glassdeffa692023-01-28 15:00:28 -0700637 /* SCSI is 7th in the list, so bit 6 */
638 ut_asserteq(BIT(MMC_HUNTER) | BIT(6), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700639
640 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
641 ut_assert_console_end();
642 ut_assertnonnull(dev);
643 ut_asserteq_str("eth@10002000.bootdev", dev->name);
644 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
645 mflags);
646
647 /* dhcp: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700648 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700649
650 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
651 ut_assert_console_end();
652 ut_assertnonnull(dev);
653 ut_asserteq_str("eth@10002000.bootdev", dev->name);
654 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
655 mflags);
656
657 /* pxe: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700658 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700659
660 mflags = 123;
661 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
662 ut_asserteq(123, mflags);
663 ut_assert_console_end();
664
665 /* no change */
Simon Glassdeffa692023-01-28 15:00:28 -0700666 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700667
668 return 0;
669}
Simon Glass1a92f832024-08-22 07:57:48 -0600670BOOTSTD_TEST(bootdev_test_next_label, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
Simon Glassf0425022024-08-22 07:57:54 -0600671 UTF_SF_BOOTDEV | UTF_CONSOLE);
Simon Glass660a9952023-01-17 10:48:11 -0700672
Simon Glass660a9952023-01-17 10:48:11 -0700673/* Check iterating to the next prioirty in a list */
674static int bootdev_test_next_prio(struct unit_test_state *uts)
675{
676 struct bootflow_iter iter;
677 struct bootstd_priv *std;
678 struct bootflow bflow;
679 struct udevice *dev;
680 int ret;
681
Simon Glassba3d5372023-01-17 10:48:15 -0700682 test_set_eth_enable(false);
Simon Glass660a9952023-01-17 10:48:11 -0700683 test_set_skip_delays(true);
684
685 /* get access to the used hunters */
686 ut_assertok(bootstd_get_priv(&std));
687
688 memset(&iter, '\0', sizeof(iter));
689 memset(&bflow, '\0', sizeof(bflow));
690 iter.part = 0;
691 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
692 iter.cur_prio = 0;
Simon Glass99e68182023-02-22 12:17:03 -0700693 iter.flags = BOOTFLOWIF_SHOW;
Simon Glass660a9952023-01-17 10:48:11 -0700694
695 dev = NULL;
Simon Glass660a9952023-01-17 10:48:11 -0700696 ut_assertok(bootdev_next_prio(&iter, &dev));
697 ut_assertnonnull(dev);
698 ut_asserteq_str("mmc2.bootdev", dev->name);
699
700 /* hunt flag not set, so this should not use any hunters */
701 ut_asserteq(0, std->hunters_used);
702 ut_assert_console_end();
703
704 /* now try again with hunting enabled */
Simon Glass99e68182023-02-22 12:17:03 -0700705 iter.flags = BOOTFLOWIF_SHOW | BOOTFLOWIF_HUNT;
Simon Glass660a9952023-01-17 10:48:11 -0700706 iter.cur_prio = 0;
707 iter.part = 0;
708
709 ut_assertok(bootdev_next_prio(&iter, &dev));
710 ut_asserteq_str("mmc2.bootdev", dev->name);
Simon Glasse4cf1062023-01-17 10:48:13 -0700711 ut_assert_nextline("Hunting with: simple_bus");
712 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass660a9952023-01-17 10:48:11 -0700713 ut_assert_nextline("Hunting with: mmc");
714 ut_assert_console_end();
715
Simon Glasse4cf1062023-01-17 10:48:13 -0700716 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700717
718 ut_assertok(bootdev_next_prio(&iter, &dev));
719 ut_asserteq_str("mmc1.bootdev", dev->name);
720
721 ut_assertok(bootdev_next_prio(&iter, &dev));
722 ut_asserteq_str("mmc0.bootdev", dev->name);
723 ut_assert_console_end();
724
725 ut_assertok(bootdev_next_prio(&iter, &dev));
726 ut_asserteq_str("spi.bin@0.bootdev", dev->name);
727 ut_assert_skip_to_line("Hunting with: spi_flash");
728
729 /*
730 * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it
731 * starts looking at the devices, so we se virtio as well
732 */
733 ut_assert_nextline("Hunting with: virtio");
734 ut_assert_nextlinen("SF: Detected m25p16");
735
736 ut_assertok(bootdev_next_prio(&iter, &dev));
737 ut_asserteq_str("spi.bin@1.bootdev", dev->name);
738 ut_assert_nextlinen("SF: Detected m25p16");
739 ut_assert_console_end();
740
741 /* keep going until there are no more bootdevs */
742 do {
743 ret = bootdev_next_prio(&iter, &dev);
744 } while (!ret);
745 ut_asserteq(-ENODEV, ret);
746 ut_assertnull(dev);
Simon Glasse4cf1062023-01-17 10:48:13 -0700747 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700748
749 ut_assert_skip_to_line("Hunting with: ethernet");
750 ut_assert_console_end();
751
752 return 0;
753}
Simon Glassf0425022024-08-22 07:57:54 -0600754BOOTSTD_TEST(bootdev_test_next_prio, UTF_DM | UTF_SCAN_FDT | UTF_SF_BOOTDEV |
755 UTF_CONSOLE);