blob: e1eb8ccd9a7d113b6752ceea475c3e71e6e2e797 [file] [log] [blame]
Simon Glassb255efc2022-04-24 23:31:24 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Test for bootdev functions. All start with 'bootdev'
4 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <common.h>
10#include <bootstd.h>
11#include <dm.h>
12#include <bootdev.h>
13#include <bootflow.h>
14#include <mapmem.h>
15#include <os.h>
16#include <test/suites.h>
17#include <test/ut.h>
18#include "bootstd_common.h"
19
20/* Allow reseting the USB-started flag */
Linus Walleije3045d12023-01-23 23:07:21 +010021#if defined(CONFIG_USB_HOST) || defined(CONFIG_USB_GADGET)
Simon Glassb255efc2022-04-24 23:31:24 -060022extern char usb_started;
Linus Walleije3045d12023-01-23 23:07:21 +010023#else
24char usb_started;
25#endif
Simon Glassb255efc2022-04-24 23:31:24 -060026
27/* Check 'bootdev list' command */
28static int bootdev_test_cmd_list(struct unit_test_state *uts)
29{
30 int probed;
31
32 console_record_reset_enable();
33 for (probed = 0; probed < 2; probed++) {
34 int probe_ch = probed ? '+' : ' ';
35
36 ut_assertok(run_command(probed ? "bootdev list -p" :
37 "bootdev list", 0));
38 ut_assert_nextline("Seq Probed Status Uclass Name");
39 ut_assert_nextlinen("---");
40 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 0, probe_ch, "OK",
41 "mmc", "mmc2.bootdev");
42 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 1, probe_ch, "OK",
43 "mmc", "mmc1.bootdev");
44 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 2, probe_ch, "OK",
45 "mmc", "mmc0.bootdev");
46 ut_assert_nextlinen("---");
47 ut_assert_nextline("(3 bootdevs)");
48 ut_assert_console_end();
49 }
50
51 return 0;
52}
53BOOTSTD_TEST(bootdev_test_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
54
55/* Check 'bootdev select' and 'info' commands */
56static int bootdev_test_cmd_select(struct unit_test_state *uts)
57{
58 struct bootstd_priv *std;
59
60 /* get access to the CLI's cur_bootdev */
61 ut_assertok(bootstd_get_priv(&std));
62
63 console_record_reset_enable();
64 ut_asserteq(1, run_command("bootdev info", 0));
65 ut_assert_nextlinen("Please use");
66 ut_assert_console_end();
67
68 /* select by sequence */
69 ut_assertok(run_command("bootdev select 0", 0));
70 ut_assert_console_end();
71
72 ut_assertok(run_command("bootdev info", 0));
73 ut_assert_nextline("Name: mmc2.bootdev");
74 ut_assert_nextline("Sequence: 0");
75 ut_assert_nextline("Status: Probed");
76 ut_assert_nextline("Uclass: mmc");
77 ut_assert_nextline("Bootflows: 0 (0 valid)");
78 ut_assert_console_end();
79
80 /* select by bootdev name */
81 ut_assertok(run_command("bootdev select mmc1.bootdev", 0));
82 ut_assert_console_end();
83 ut_assertnonnull(std->cur_bootdev);
84 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
85
86 /* select by bootdev label*/
87 ut_assertok(run_command("bootdev select mmc1", 0));
88 ut_assert_console_end();
89 ut_assertnonnull(std->cur_bootdev);
90 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
91
92 /* deselect */
93 ut_assertok(run_command("bootdev select", 0));
94 ut_assert_console_end();
95 ut_assertnull(std->cur_bootdev);
96
97 ut_asserteq(1, run_command("bootdev info", 0));
98 ut_assert_nextlinen("Please use");
99 ut_assert_console_end();
100
101 return 0;
102}
103BOOTSTD_TEST(bootdev_test_cmd_select, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
104
105/* Check bootdev labels */
106static int bootdev_test_labels(struct unit_test_state *uts)
107{
108 struct udevice *dev, *media;
Simon Glasse22fe922023-01-17 10:48:05 -0700109 int mflags = 0;
Simon Glassb255efc2022-04-24 23:31:24 -0600110
Simon Glasse22fe922023-01-17 10:48:05 -0700111 ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600112 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
Simon Glasse22fe922023-01-17 10:48:05 -0700113 ut_asserteq(0, mflags);
Simon Glassb255efc2022-04-24 23:31:24 -0600114 media = dev_get_parent(dev);
115 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
116 ut_asserteq_str("mmc2", media->name);
117
Simon Glasse22fe922023-01-17 10:48:05 -0700118 /* Check method flags */
119 ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700120 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
121 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700122 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700123 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
124 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700125
Simon Glassb255efc2022-04-24 23:31:24 -0600126 /* Check invalid uclass */
Simon Glasse22fe922023-01-17 10:48:05 -0700127 ut_asserteq(-EINVAL, 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 Glasse22fe922023-01-17 10:48:05 -0700134BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
135 UT_TESTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600136
Simon Glassde567b12023-01-17 10:48:09 -0700137/* Check bootdev_find_by_any() */
138static int bootdev_test_any(struct unit_test_state *uts)
139{
140 struct udevice *dev, *media;
141 int mflags;
142
143 /*
144 * with ethernet enabled we have 8 devices ahead of the mmc ones:
145 *
146 * ut_assertok(run_command("bootdev list", 0));
147 * Seq Probed Status Uclass Name
148 * --- ------ ------ -------- ------------------
149 * 0 [ + ] OK ethernet eth@10002000.bootdev
150 * 1 [ ] OK ethernet eth@10003000.bootdev
151 * 2 [ ] OK ethernet sbe5.bootdev
152 * 3 [ ] OK ethernet eth@10004000.bootdev
153 * 4 [ ] OK ethernet phy-test-eth.bootdev
154 * 5 [ ] OK ethernet dsa-test-eth.bootdev
155 * 6 [ ] OK ethernet dsa-test@0.bootdev
156 * 7 [ ] OK ethernet dsa-test@1.bootdev
157 * 8 [ ] OK mmc mmc2.bootdev
158 * 9 [ + ] OK mmc mmc1.bootdev
159 * a [ ] OK mmc mmc0.bootdev
160 */
161 console_record_reset_enable();
162 ut_assertok(bootdev_find_by_any("8", &dev, &mflags));
163 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
164 ut_asserteq(BOOTFLOW_METHF_SINGLE_DEV, mflags);
165 media = dev_get_parent(dev);
166 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
167 ut_asserteq_str("mmc2", media->name);
168 ut_assert_console_end();
169
170 /* there should not be this many bootdevs */
171 ut_asserteq(-ENODEV, bootdev_find_by_any("50", &dev, &mflags));
172 ut_assert_nextline("Cannot find '50' (err=-19)");
173 ut_assert_console_end();
174
175 /* Check method flags */
176 ut_assertok(bootdev_find_by_any("pxe", &dev, &mflags));
177 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
178 mflags);
179
180 /* Check invalid uclass */
181 mflags = 123;
182 ut_asserteq(-EINVAL, bootdev_find_by_any("fred0", &dev, &mflags));
183 ut_assert_nextline("Unknown uclass 'fred0' in label");
184 ut_assert_nextline("Cannot find bootdev 'fred0' (err=-22)");
185 ut_asserteq(123, mflags);
186 ut_assert_console_end();
187
188 return 0;
189}
190BOOTSTD_TEST(bootdev_test_any, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
191 UT_TESTF_ETH_BOOTDEV);
192
Simon Glassb255efc2022-04-24 23:31:24 -0600193/* Check bootdev ordering with the bootdev-order property */
194static int bootdev_test_order(struct unit_test_state *uts)
195{
196 struct bootflow_iter iter;
197 struct bootflow bflow;
198
199 /*
200 * First try the order set by the bootdev-order property
201 * Like all sandbox unit tests this relies on the devicetree setting up
202 * the required devices:
203 *
204 * mmc0 - nothing connected
205 * mmc1 - connected to mmc1.img file
206 * mmc2 - nothing connected
207 */
208 ut_assertok(env_set("boot_targets", NULL));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700209 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600210 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700211 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
212 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600213 bootflow_iter_uninit(&iter);
214
215 /* Use the environment variable to override it */
216 ut_assertok(env_set("boot_targets", "mmc1 mmc2"));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700217 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700218 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600219 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700220 ut_asserteq_str("mmc1.bootdev", iter.dev_used[0]->name);
221 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600222 bootflow_iter_uninit(&iter);
223
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700224 return 0;
225}
226BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
227
228/* Check default bootdev ordering */
229static int bootdev_test_order_default(struct unit_test_state *uts)
230{
231 struct bootflow_iter iter;
232 struct bootflow bflow;
233
Simon Glassb255efc2022-04-24 23:31:24 -0600234 /*
235 * Now drop both orderings, to check the default (prioriy/sequence)
236 * ordering
237 */
238 ut_assertok(env_set("boot_targets", NULL));
239 ut_assertok(bootstd_test_drop_bootdev_order(uts));
240
Simon Glass5d3d44f2023-01-17 10:48:16 -0700241 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700242 ut_asserteq(2, iter.num_devs);
243 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
244 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600245
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700246 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600247 ut_asserteq(3, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700248 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600249 bootflow_iter_uninit(&iter);
250
251 return 0;
252}
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700253BOOTSTD_TEST(bootdev_test_order_default, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600254
255/* Check bootdev ordering with the uclass priority */
256static int bootdev_test_prio(struct unit_test_state *uts)
257{
258 struct bootdev_uc_plat *ucp;
259 struct bootflow_iter iter;
260 struct bootflow bflow;
261 struct udevice *blk;
262
Simon Glassf3cb89f2023-01-17 10:47:37 -0700263 test_set_skip_delays(true);
264
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700265 /* disable ethernet since the hunter will run dhcp */
266 test_set_eth_enable(false);
267
Simon Glassb255efc2022-04-24 23:31:24 -0600268 /* Start up USB which gives us three additional bootdevs */
269 usb_started = false;
270 ut_assertok(run_command("usb start", 0));
271
272 ut_assertok(bootstd_test_drop_bootdev_order(uts));
273
274 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
275 console_record_reset_enable();
Simon Glass5d3d44f2023-01-17 10:48:16 -0700276 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700277 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600278 ut_asserteq(6, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700279 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600280 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700281 iter.dev_used[3]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600282
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700283 ut_assertok(bootdev_get_sibling_blk(iter.dev_used[3], &blk));
Simon Glassb255efc2022-04-24 23:31:24 -0600284 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
285
286 /* adjust the priority of the first USB bootdev to the highest */
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700287 ucp = dev_get_uclass_plat(iter.dev_used[3]);
288 ucp->prio = BOOTDEVP_1_PRE_SCAN;
Simon Glassb255efc2022-04-24 23:31:24 -0600289
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700290 /* try again but enable hunting, which brings in SCSI */
Simon Glassb255efc2022-04-24 23:31:24 -0600291 bootflow_iter_uninit(&iter);
Simon Glass5d3d44f2023-01-17 10:48:16 -0700292 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWF_HUNT,
293 &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700294 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
295 ut_asserteq(7, iter.num_devs);
Simon Glassb255efc2022-04-24 23:31:24 -0600296 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700297 iter.dev_used[0]->name);
298 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600299
300 return 0;
301}
302BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass5acb97a2023-01-17 10:47:33 -0700303
304/* Check listing hunters */
305static int bootdev_test_hunter(struct unit_test_state *uts)
306{
307 struct bootstd_priv *std;
308
Simon Glassf3cb89f2023-01-17 10:47:37 -0700309 test_set_skip_delays(true);
310
Simon Glass5acb97a2023-01-17 10:47:33 -0700311 /* get access to the used hunters */
312 ut_assertok(bootstd_get_priv(&std));
313
314 console_record_reset_enable();
315 bootdev_list_hunters(std);
316 ut_assert_nextline("Prio Used Uclass Hunter");
317 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700318 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700319 ut_assert_nextline(" 1 simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700320 ut_assert_nextline(" 5 ide ide_bootdev");
321 ut_assert_nextline(" 2 mmc mmc_bootdev");
322 ut_assert_nextline(" 4 nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700323 ut_assert_nextline(" 4 qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700324 ut_assert_nextline(" 4 scsi scsi_bootdev");
325 ut_assert_nextline(" 4 spi_flash sf_bootdev");
326 ut_assert_nextline(" 5 usb usb_bootdev");
327 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700328 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700329 ut_assert_console_end();
330
Simon Glassf3cb89f2023-01-17 10:47:37 -0700331 ut_assertok(bootdev_hunt("usb1", false));
332 ut_assert_nextline(
333 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass1248d2b2023-01-17 10:47:34 -0700334 ut_assert_console_end();
335
Simon Glassdeffa692023-01-28 15:00:28 -0700336 /* USB is 7th in the list, so bit 8 */
337 ut_asserteq(BIT(8), std->hunters_used);
Simon Glassf3cb89f2023-01-17 10:47:37 -0700338
Simon Glass5acb97a2023-01-17 10:47:33 -0700339 return 0;
340}
341BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
342
343/* Check 'bootdev hunt' command */
344static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
345{
346 struct bootstd_priv *std;
347
Simon Glassf3cb89f2023-01-17 10:47:37 -0700348 test_set_skip_delays(true);
349
Simon Glass5acb97a2023-01-17 10:47:33 -0700350 /* get access to the used hunters */
351 ut_assertok(bootstd_get_priv(&std));
352
353 console_record_reset_enable();
354 ut_assertok(run_command("bootdev hunt -l", 0));
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 Glassdeffa692023-01-28 15:00:28 -0700358 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700359 ut_assert_console_end();
360
Simon Glass7e1f6a42023-01-17 10:48:08 -0700361 /* Use the MMC hunter and see that it updates */
362 ut_assertok(run_command("bootdev hunt mmc", 0));
363 ut_assertok(run_command("bootdev hunt -l", 0));
364 ut_assert_skip_to_line(" 5 ide ide_bootdev");
365 ut_assert_nextline(" 2 * mmc mmc_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700366 ut_assert_skip_to_line("(total hunters: 10)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700367 ut_assert_console_end();
368
Simon Glass5acb97a2023-01-17 10:47:33 -0700369 /* Scan all hunters */
Simon Glassac06b26a2023-01-17 10:48:10 -0700370 test_set_eth_enable(false);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700371 test_set_skip_delays(true);
Simon Glass5acb97a2023-01-17 10:47:33 -0700372 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700373 ut_assert_nextline("Hunting with: ethernet");
Simon Glasse4cf1062023-01-17 10:48:13 -0700374
375 /* This is the extension feature which has no uclass at present */
376 ut_assert_nextline("Hunting with: simple_bus");
377 ut_assert_nextline("Found 2 extension board(s).");
Simon Glassbe7c6662023-01-17 10:47:46 -0700378 ut_assert_nextline("Hunting with: ide");
379 ut_assert_nextline("Bus 0: not available ");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700380
381 /* mmc hunter has already been used so should not run again */
382
Simon Glass08382872023-01-17 10:47:47 -0700383 ut_assert_nextline("Hunting with: nvme");
Simon Glassdeffa692023-01-28 15:00:28 -0700384 ut_assert_nextline("Hunting with: qfw");
Simon Glass9985e9c2023-01-17 10:47:45 -0700385 ut_assert_nextline("Hunting with: scsi");
386 ut_assert_nextline("scanning bus for devices...");
Simon Glass215be682023-01-17 10:48:03 -0700387 ut_assert_skip_to_line("Hunting with: spi_flash");
388 ut_assert_nextline("Hunting with: usb");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700389 ut_assert_nextline(
390 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass215be682023-01-17 10:48:03 -0700391 ut_assert_nextline("Hunting with: virtio");
Simon Glass5acb97a2023-01-17 10:47:33 -0700392 ut_assert_console_end();
393
394 /* List available hunters */
395 ut_assertok(run_command("bootdev hunt -l", 0));
396 ut_assert_nextlinen("Prio");
397 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700398 ut_assert_nextline(" 6 * ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700399 ut_assert_nextline(" 1 * simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700400 ut_assert_nextline(" 5 * ide ide_bootdev");
401 ut_assert_nextline(" 2 * mmc mmc_bootdev");
402 ut_assert_nextline(" 4 * nvme nvme_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700403 ut_assert_nextline(" 4 * qfw qfw_bootdev");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700404 ut_assert_nextline(" 4 * scsi scsi_bootdev");
405 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
406 ut_assert_nextline(" 5 * usb usb_bootdev");
407 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glassdeffa692023-01-28 15:00:28 -0700408 ut_assert_nextline("(total hunters: 10)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700409 ut_assert_console_end();
410
Simon Glasse4cf1062023-01-17 10:48:13 -0700411 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass5acb97a2023-01-17 10:47:33 -0700412
413 return 0;
414}
415BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
416 UT_TESTF_ETH_BOOTDEV);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700417
Simon Glass484e4072023-01-17 10:48:14 -0700418/* Check searching for bootdevs using the hunters */
419static int bootdev_test_hunt_scan(struct unit_test_state *uts)
420{
421 struct bootflow_iter iter;
422 struct bootstd_priv *std;
423 struct bootflow bflow;
424
425 /* get access to the used hunters */
426 ut_assertok(bootstd_get_priv(&std));
427
428 ut_assertok(bootstd_test_drop_bootdev_order(uts));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700429 ut_assertok(bootflow_scan_first(NULL, NULL, &iter,
430 BOOTFLOWF_SHOW | BOOTFLOWF_HUNT |
Simon Glass484e4072023-01-17 10:48:14 -0700431 BOOTFLOWF_SKIP_GLOBAL, &bflow));
432 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
433
434 return 0;
435}
436BOOTSTD_TEST(bootdev_test_hunt_scan, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
437
Simon Glass64cbc5c2023-01-17 10:47:42 -0700438/* Check that only bootable partitions are processed */
439static int bootdev_test_bootable(struct unit_test_state *uts)
440{
441 struct bootflow_iter iter;
442 struct bootflow bflow;
443 struct udevice *blk;
444
445 memset(&iter, '\0', sizeof(iter));
446 memset(&bflow, '\0', sizeof(bflow));
447 iter.part = 0;
448 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
449 iter.dev = blk;
450 ut_assertok(device_find_next_child(&iter.dev));
451 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
452
453 /*
454 * initially we don't have any knowledge of which partitions are
455 * bootable, but mmc1 has two partitions, with the first one being
456 * bootable
457 */
458 iter.part = 2;
459 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
460 ut_asserteq(0, iter.first_bootable);
461
462 /* scan with part == 0 to get the partition info */
463 iter.part = 0;
464 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
465 ut_asserteq(1, iter.first_bootable);
466
467 /* now it will refuse to use non-bootable partitions */
468 iter.part = 2;
469 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
470
471 return 0;
472}
473BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700474
475/* Check hunting for bootdev of a particular priority */
476static int bootdev_test_hunt_prio(struct unit_test_state *uts)
477{
478 test_set_skip_delays(true);
479
480 console_record_reset_enable();
Simon Glass7e1f6a42023-01-17 10:48:08 -0700481 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700482 ut_assert_nextline("scanning bus for devices...");
483 ut_assert_skip_to_line(" Type: Hard Disk");
484 ut_assert_nextlinen(" Capacity:");
485 ut_assert_console_end();
486
487 /* now try a different priority, verbosely */
Simon Glass7e1f6a42023-01-17 10:48:08 -0700488 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700489 ut_assert_nextline("Hunting with: ide");
490 ut_assert_nextline("Bus 0: not available ");
491 ut_assert_nextline("Hunting with: usb");
492 ut_assert_nextline(
493 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
494 ut_assert_console_end();
495
496 return 0;
497}
498BOOTSTD_TEST(bootdev_test_hunt_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassde567b12023-01-17 10:48:09 -0700499
500/* Check hunting for bootdevs with a particular label */
501static int bootdev_test_hunt_label(struct unit_test_state *uts)
502{
503 struct udevice *dev, *old;
504 struct bootstd_priv *std;
505 int mflags;
506
507 /* get access to the used hunters */
508 ut_assertok(bootstd_get_priv(&std));
509
510 /* scan an unknown uclass */
511 console_record_reset_enable();
512 old = (void *)&mflags; /* arbitrary pointer to check against dev */
513 dev = old;
514 mflags = 123;
515 ut_asserteq(-EINVAL,
516 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
517 ut_assert_nextline("Unknown uclass 'fred' in label");
518 ut_asserteq_ptr(old, dev);
519 ut_asserteq(123, mflags);
520 ut_assert_console_end();
521 ut_asserteq(0, std->hunters_used);
522
523 /* scan an invalid mmc controllers */
524 ut_asserteq(-ENOENT,
525 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
526 ut_asserteq_ptr(old, dev);
527 ut_asserteq(123, mflags);
528 ut_assert_nextline("Unknown seq 4 for label 'mmc4'");
529 ut_assert_console_end();
530
531 ut_assertok(bootstd_test_check_mmc_hunter(uts));
532
533 /* scan for a particular mmc controller */
534 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
535 ut_assertnonnull(dev);
536 ut_asserteq_str("mmc1.bootdev", dev->name);
537 ut_asserteq(0, mflags);
538 ut_assert_console_end();
539
540 /* scan all of usb */
541 test_set_skip_delays(true);
542 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
543 ut_assertnonnull(dev);
544 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
545 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
546 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
547 ut_assert_console_end();
548
549 return 0;
550}
551BOOTSTD_TEST(bootdev_test_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassac06b26a2023-01-17 10:48:10 -0700552
553/* Check iterating to the next label in a list */
554static int bootdev_test_next_label(struct unit_test_state *uts)
555{
556 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
557 struct bootflow_iter iter;
558 struct bootstd_priv *std;
559 struct bootflow bflow;
560 struct udevice *dev;
561 int mflags;
562
563 test_set_eth_enable(false);
564
565 /* get access to the used hunters */
566 ut_assertok(bootstd_get_priv(&std));
567
568 memset(&iter, '\0', sizeof(iter));
569 memset(&bflow, '\0', sizeof(bflow));
570 iter.part = 0;
571 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
572 iter.cur_label = -1;
573 iter.labels = labels;
574
575 dev = NULL;
576 mflags = 123;
577 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
578 console_record_reset_enable();
579 ut_assert_console_end();
580 ut_assertnonnull(dev);
581 ut_asserteq_str("mmc0.bootdev", dev->name);
582 ut_asserteq(0, mflags);
583
584 ut_assertok(bootstd_test_check_mmc_hunter(uts));
585
586 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
587 ut_assert_nextline("scanning bus for devices...");
588 ut_assert_skip_to_line(
589 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
590 ut_assert_console_end();
591 ut_assertnonnull(dev);
592 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
593 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
594
Simon Glassdeffa692023-01-28 15:00:28 -0700595 /* SCSI is 7th in the list, so bit 6 */
596 ut_asserteq(BIT(MMC_HUNTER) | BIT(6), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700597
598 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
599 ut_assert_console_end();
600 ut_assertnonnull(dev);
601 ut_asserteq_str("eth@10002000.bootdev", dev->name);
602 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
603 mflags);
604
605 /* dhcp: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700606 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700607
608 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
609 ut_assert_console_end();
610 ut_assertnonnull(dev);
611 ut_asserteq_str("eth@10002000.bootdev", dev->name);
612 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
613 mflags);
614
615 /* pxe: Ethernet is first so bit 0 */
Simon Glassdeffa692023-01-28 15:00:28 -0700616 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700617
618 mflags = 123;
619 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
620 ut_asserteq(123, mflags);
621 ut_assert_console_end();
622
623 /* no change */
Simon Glassdeffa692023-01-28 15:00:28 -0700624 ut_asserteq(BIT(MMC_HUNTER) | BIT(6) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700625
626 return 0;
627}
628BOOTSTD_TEST(bootdev_test_next_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
629 UT_TESTF_ETH_BOOTDEV | UT_TESTF_SF_BOOTDEV);
Simon Glass660a9952023-01-17 10:48:11 -0700630
631
632/* Check iterating to the next prioirty in a list */
633static int bootdev_test_next_prio(struct unit_test_state *uts)
634{
635 struct bootflow_iter iter;
636 struct bootstd_priv *std;
637 struct bootflow bflow;
638 struct udevice *dev;
639 int ret;
640
Simon Glassba3d5372023-01-17 10:48:15 -0700641 test_set_eth_enable(false);
Simon Glass660a9952023-01-17 10:48:11 -0700642 test_set_skip_delays(true);
643
644 /* get access to the used hunters */
645 ut_assertok(bootstd_get_priv(&std));
646
647 memset(&iter, '\0', sizeof(iter));
648 memset(&bflow, '\0', sizeof(bflow));
649 iter.part = 0;
650 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
651 iter.cur_prio = 0;
652 iter.flags = BOOTFLOWF_SHOW;
653
654 dev = NULL;
655 console_record_reset_enable();
656 ut_assertok(bootdev_next_prio(&iter, &dev));
657 ut_assertnonnull(dev);
658 ut_asserteq_str("mmc2.bootdev", dev->name);
659
660 /* hunt flag not set, so this should not use any hunters */
661 ut_asserteq(0, std->hunters_used);
662 ut_assert_console_end();
663
664 /* now try again with hunting enabled */
665 iter.flags = BOOTFLOWF_SHOW | BOOTFLOWF_HUNT;
666 iter.cur_prio = 0;
667 iter.part = 0;
668
669 ut_assertok(bootdev_next_prio(&iter, &dev));
670 ut_asserteq_str("mmc2.bootdev", dev->name);
Simon Glasse4cf1062023-01-17 10:48:13 -0700671 ut_assert_nextline("Hunting with: simple_bus");
672 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass660a9952023-01-17 10:48:11 -0700673 ut_assert_nextline("Hunting with: mmc");
674 ut_assert_console_end();
675
Simon Glasse4cf1062023-01-17 10:48:13 -0700676 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700677
678 ut_assertok(bootdev_next_prio(&iter, &dev));
679 ut_asserteq_str("mmc1.bootdev", dev->name);
680
681 ut_assertok(bootdev_next_prio(&iter, &dev));
682 ut_asserteq_str("mmc0.bootdev", dev->name);
683 ut_assert_console_end();
684
685 ut_assertok(bootdev_next_prio(&iter, &dev));
686 ut_asserteq_str("spi.bin@0.bootdev", dev->name);
687 ut_assert_skip_to_line("Hunting with: spi_flash");
688
689 /*
690 * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it
691 * starts looking at the devices, so we se virtio as well
692 */
693 ut_assert_nextline("Hunting with: virtio");
694 ut_assert_nextlinen("SF: Detected m25p16");
695
696 ut_assertok(bootdev_next_prio(&iter, &dev));
697 ut_asserteq_str("spi.bin@1.bootdev", dev->name);
698 ut_assert_nextlinen("SF: Detected m25p16");
699 ut_assert_console_end();
700
701 /* keep going until there are no more bootdevs */
702 do {
703 ret = bootdev_next_prio(&iter, &dev);
704 } while (!ret);
705 ut_asserteq(-ENODEV, ret);
706 ut_assertnull(dev);
Simon Glasse4cf1062023-01-17 10:48:13 -0700707 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700708
709 ut_assert_skip_to_line("Hunting with: ethernet");
710 ut_assert_console_end();
711
712 return 0;
713}
714BOOTSTD_TEST(bootdev_test_next_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
715 UT_TESTF_SF_BOOTDEV);