blob: ef5215bbcec314c0fc7ecca0de830fe481b24802 [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 */
21extern char usb_started;
22
23/* Check 'bootdev list' command */
24static int bootdev_test_cmd_list(struct unit_test_state *uts)
25{
26 int probed;
27
28 console_record_reset_enable();
29 for (probed = 0; probed < 2; probed++) {
30 int probe_ch = probed ? '+' : ' ';
31
32 ut_assertok(run_command(probed ? "bootdev list -p" :
33 "bootdev list", 0));
34 ut_assert_nextline("Seq Probed Status Uclass Name");
35 ut_assert_nextlinen("---");
36 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 0, probe_ch, "OK",
37 "mmc", "mmc2.bootdev");
38 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 1, probe_ch, "OK",
39 "mmc", "mmc1.bootdev");
40 ut_assert_nextline("%3x [ %c ] %6s %-8s %s", 2, probe_ch, "OK",
41 "mmc", "mmc0.bootdev");
42 ut_assert_nextlinen("---");
43 ut_assert_nextline("(3 bootdevs)");
44 ut_assert_console_end();
45 }
46
47 return 0;
48}
49BOOTSTD_TEST(bootdev_test_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
50
51/* Check 'bootdev select' and 'info' commands */
52static int bootdev_test_cmd_select(struct unit_test_state *uts)
53{
54 struct bootstd_priv *std;
55
56 /* get access to the CLI's cur_bootdev */
57 ut_assertok(bootstd_get_priv(&std));
58
59 console_record_reset_enable();
60 ut_asserteq(1, run_command("bootdev info", 0));
61 ut_assert_nextlinen("Please use");
62 ut_assert_console_end();
63
64 /* select by sequence */
65 ut_assertok(run_command("bootdev select 0", 0));
66 ut_assert_console_end();
67
68 ut_assertok(run_command("bootdev info", 0));
69 ut_assert_nextline("Name: mmc2.bootdev");
70 ut_assert_nextline("Sequence: 0");
71 ut_assert_nextline("Status: Probed");
72 ut_assert_nextline("Uclass: mmc");
73 ut_assert_nextline("Bootflows: 0 (0 valid)");
74 ut_assert_console_end();
75
76 /* select by bootdev name */
77 ut_assertok(run_command("bootdev select mmc1.bootdev", 0));
78 ut_assert_console_end();
79 ut_assertnonnull(std->cur_bootdev);
80 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
81
82 /* select by bootdev label*/
83 ut_assertok(run_command("bootdev select mmc1", 0));
84 ut_assert_console_end();
85 ut_assertnonnull(std->cur_bootdev);
86 ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
87
88 /* deselect */
89 ut_assertok(run_command("bootdev select", 0));
90 ut_assert_console_end();
91 ut_assertnull(std->cur_bootdev);
92
93 ut_asserteq(1, run_command("bootdev info", 0));
94 ut_assert_nextlinen("Please use");
95 ut_assert_console_end();
96
97 return 0;
98}
99BOOTSTD_TEST(bootdev_test_cmd_select, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
100
101/* Check bootdev labels */
102static int bootdev_test_labels(struct unit_test_state *uts)
103{
104 struct udevice *dev, *media;
Simon Glasse22fe922023-01-17 10:48:05 -0700105 int mflags = 0;
Simon Glassb255efc2022-04-24 23:31:24 -0600106
Simon Glasse22fe922023-01-17 10:48:05 -0700107 ut_assertok(bootdev_find_by_label("mmc2", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600108 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
Simon Glasse22fe922023-01-17 10:48:05 -0700109 ut_asserteq(0, mflags);
Simon Glassb255efc2022-04-24 23:31:24 -0600110 media = dev_get_parent(dev);
111 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
112 ut_asserteq_str("mmc2", media->name);
113
Simon Glasse22fe922023-01-17 10:48:05 -0700114 /* Check method flags */
115 ut_assertok(bootdev_find_by_label("pxe", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700116 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
117 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700118 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
Simon Glassde567b12023-01-17 10:48:09 -0700119 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
120 mflags);
Simon Glasse22fe922023-01-17 10:48:05 -0700121
Simon Glassb255efc2022-04-24 23:31:24 -0600122 /* Check invalid uclass */
Simon Glasse22fe922023-01-17 10:48:05 -0700123 ut_asserteq(-EINVAL, bootdev_find_by_label("fred0", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600124
125 /* Check unknown sequence number */
Simon Glasse22fe922023-01-17 10:48:05 -0700126 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600127
128 return 0;
129}
Simon Glasse22fe922023-01-17 10:48:05 -0700130BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
131 UT_TESTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600132
Simon Glassde567b12023-01-17 10:48:09 -0700133/* Check bootdev_find_by_any() */
134static int bootdev_test_any(struct unit_test_state *uts)
135{
136 struct udevice *dev, *media;
137 int mflags;
138
139 /*
140 * with ethernet enabled we have 8 devices ahead of the mmc ones:
141 *
142 * ut_assertok(run_command("bootdev list", 0));
143 * Seq Probed Status Uclass Name
144 * --- ------ ------ -------- ------------------
145 * 0 [ + ] OK ethernet eth@10002000.bootdev
146 * 1 [ ] OK ethernet eth@10003000.bootdev
147 * 2 [ ] OK ethernet sbe5.bootdev
148 * 3 [ ] OK ethernet eth@10004000.bootdev
149 * 4 [ ] OK ethernet phy-test-eth.bootdev
150 * 5 [ ] OK ethernet dsa-test-eth.bootdev
151 * 6 [ ] OK ethernet dsa-test@0.bootdev
152 * 7 [ ] OK ethernet dsa-test@1.bootdev
153 * 8 [ ] OK mmc mmc2.bootdev
154 * 9 [ + ] OK mmc mmc1.bootdev
155 * a [ ] OK mmc mmc0.bootdev
156 */
157 console_record_reset_enable();
158 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;
178 ut_asserteq(-EINVAL, bootdev_find_by_any("fred0", &dev, &mflags));
179 ut_assert_nextline("Unknown uclass 'fred0' in label");
180 ut_assert_nextline("Cannot find bootdev 'fred0' (err=-22)");
181 ut_asserteq(123, mflags);
182 ut_assert_console_end();
183
184 return 0;
185}
186BOOTSTD_TEST(bootdev_test_any, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
187 UT_TESTF_ETH_BOOTDEV);
188
Simon Glassb255efc2022-04-24 23:31:24 -0600189/* Check bootdev ordering with the bootdev-order property */
190static int bootdev_test_order(struct unit_test_state *uts)
191{
192 struct bootflow_iter iter;
193 struct bootflow bflow;
194
195 /*
196 * First try the order set by the bootdev-order property
197 * Like all sandbox unit tests this relies on the devicetree setting up
198 * the required devices:
199 *
200 * mmc0 - nothing connected
201 * mmc1 - connected to mmc1.img file
202 * mmc2 - nothing connected
203 */
204 ut_assertok(env_set("boot_targets", NULL));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700205 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600206 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700207 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
208 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600209 bootflow_iter_uninit(&iter);
210
211 /* Use the environment variable to override it */
212 ut_assertok(env_set("boot_targets", "mmc1 mmc2"));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700213 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700214 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600215 ut_asserteq(2, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700216 ut_asserteq_str("mmc1.bootdev", iter.dev_used[0]->name);
217 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600218 bootflow_iter_uninit(&iter);
219
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700220 return 0;
221}
222BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
223
224/* Check default bootdev ordering */
225static int bootdev_test_order_default(struct unit_test_state *uts)
226{
227 struct bootflow_iter iter;
228 struct bootflow bflow;
229
Simon Glassb255efc2022-04-24 23:31:24 -0600230 /*
231 * Now drop both orderings, to check the default (prioriy/sequence)
232 * ordering
233 */
234 ut_assertok(env_set("boot_targets", NULL));
235 ut_assertok(bootstd_test_drop_bootdev_order(uts));
236
Simon Glass5d3d44f2023-01-17 10:48:16 -0700237 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700238 ut_asserteq(2, iter.num_devs);
239 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
240 ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600241
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700242 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600243 ut_asserteq(3, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700244 ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600245 bootflow_iter_uninit(&iter);
246
247 return 0;
248}
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700249BOOTSTD_TEST(bootdev_test_order_default, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600250
251/* Check bootdev ordering with the uclass priority */
252static int bootdev_test_prio(struct unit_test_state *uts)
253{
254 struct bootdev_uc_plat *ucp;
255 struct bootflow_iter iter;
256 struct bootflow bflow;
257 struct udevice *blk;
258
Simon Glassf3cb89f2023-01-17 10:47:37 -0700259 test_set_skip_delays(true);
260
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700261 /* disable ethernet since the hunter will run dhcp */
262 test_set_eth_enable(false);
263
Simon Glassb255efc2022-04-24 23:31:24 -0600264 /* Start up USB which gives us three additional bootdevs */
265 usb_started = false;
266 ut_assertok(run_command("usb start", 0));
267
268 ut_assertok(bootstd_test_drop_bootdev_order(uts));
269
270 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
271 console_record_reset_enable();
Simon Glass5d3d44f2023-01-17 10:48:16 -0700272 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700273 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600274 ut_asserteq(6, iter.num_devs);
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700275 ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600276 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700277 iter.dev_used[3]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600278
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700279 ut_assertok(bootdev_get_sibling_blk(iter.dev_used[3], &blk));
Simon Glassb255efc2022-04-24 23:31:24 -0600280 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
281
282 /* adjust the priority of the first USB bootdev to the highest */
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700283 ucp = dev_get_uclass_plat(iter.dev_used[3]);
284 ucp->prio = BOOTDEVP_1_PRE_SCAN;
Simon Glassb255efc2022-04-24 23:31:24 -0600285
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700286 /* try again but enable hunting, which brings in SCSI */
Simon Glassb255efc2022-04-24 23:31:24 -0600287 bootflow_iter_uninit(&iter);
Simon Glass5d3d44f2023-01-17 10:48:16 -0700288 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWF_HUNT,
289 &bflow));
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700290 ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
291 ut_asserteq(7, iter.num_devs);
Simon Glassb255efc2022-04-24 23:31:24 -0600292 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700293 iter.dev_used[0]->name);
294 ut_asserteq_str("mmc2.bootdev", iter.dev_used[1]->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600295
296 return 0;
297}
298BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass5acb97a2023-01-17 10:47:33 -0700299
300/* Check listing hunters */
301static int bootdev_test_hunter(struct unit_test_state *uts)
302{
303 struct bootstd_priv *std;
304
Simon Glassf3cb89f2023-01-17 10:47:37 -0700305 test_set_skip_delays(true);
306
Simon Glass5acb97a2023-01-17 10:47:33 -0700307 /* get access to the used hunters */
308 ut_assertok(bootstd_get_priv(&std));
309
310 console_record_reset_enable();
311 bootdev_list_hunters(std);
312 ut_assert_nextline("Prio Used Uclass Hunter");
313 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700314 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700315 ut_assert_nextline(" 1 simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700316 ut_assert_nextline(" 5 ide ide_bootdev");
317 ut_assert_nextline(" 2 mmc mmc_bootdev");
318 ut_assert_nextline(" 4 nvme nvme_bootdev");
319 ut_assert_nextline(" 4 scsi scsi_bootdev");
320 ut_assert_nextline(" 4 spi_flash sf_bootdev");
321 ut_assert_nextline(" 5 usb usb_bootdev");
322 ut_assert_nextline(" 4 virtio virtio_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700323 ut_assert_nextline("(total hunters: 9)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700324 ut_assert_console_end();
325
Simon Glassf3cb89f2023-01-17 10:47:37 -0700326 ut_assertok(bootdev_hunt("usb1", false));
327 ut_assert_nextline(
328 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass1248d2b2023-01-17 10:47:34 -0700329 ut_assert_console_end();
330
Simon Glasse4cf1062023-01-17 10:48:13 -0700331 /* USB is sixth in the list, so bit 7 */
332 ut_asserteq(BIT(7), std->hunters_used);
Simon Glassf3cb89f2023-01-17 10:47:37 -0700333
Simon Glass5acb97a2023-01-17 10:47:33 -0700334 return 0;
335}
336BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
337
338/* Check 'bootdev hunt' command */
339static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
340{
341 struct bootstd_priv *std;
342
Simon Glassf3cb89f2023-01-17 10:47:37 -0700343 test_set_skip_delays(true);
344
Simon Glass5acb97a2023-01-17 10:47:33 -0700345 /* get access to the used hunters */
346 ut_assertok(bootstd_get_priv(&std));
347
348 console_record_reset_enable();
349 ut_assertok(run_command("bootdev hunt -l", 0));
350 ut_assert_nextline("Prio Used Uclass Hunter");
351 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700352 ut_assert_nextline(" 6 ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700353 ut_assert_skip_to_line("(total hunters: 9)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700354 ut_assert_console_end();
355
Simon Glass7e1f6a42023-01-17 10:48:08 -0700356 /* Use the MMC hunter and see that it updates */
357 ut_assertok(run_command("bootdev hunt mmc", 0));
358 ut_assertok(run_command("bootdev hunt -l", 0));
359 ut_assert_skip_to_line(" 5 ide ide_bootdev");
360 ut_assert_nextline(" 2 * mmc mmc_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700361 ut_assert_skip_to_line("(total hunters: 9)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700362 ut_assert_console_end();
363
Simon Glass5acb97a2023-01-17 10:47:33 -0700364 /* Scan all hunters */
Simon Glassac06b26a2023-01-17 10:48:10 -0700365 test_set_eth_enable(false);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700366 test_set_skip_delays(true);
Simon Glass5acb97a2023-01-17 10:47:33 -0700367 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700368 ut_assert_nextline("Hunting with: ethernet");
Simon Glasse4cf1062023-01-17 10:48:13 -0700369
370 /* This is the extension feature which has no uclass at present */
371 ut_assert_nextline("Hunting with: simple_bus");
372 ut_assert_nextline("Found 2 extension board(s).");
Simon Glassbe7c6662023-01-17 10:47:46 -0700373 ut_assert_nextline("Hunting with: ide");
374 ut_assert_nextline("Bus 0: not available ");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700375
376 /* mmc hunter has already been used so should not run again */
377
Simon Glass08382872023-01-17 10:47:47 -0700378 ut_assert_nextline("Hunting with: nvme");
Simon Glass9985e9c2023-01-17 10:47:45 -0700379 ut_assert_nextline("Hunting with: scsi");
380 ut_assert_nextline("scanning bus for devices...");
Simon Glass215be682023-01-17 10:48:03 -0700381 ut_assert_skip_to_line("Hunting with: spi_flash");
382 ut_assert_nextline("Hunting with: usb");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700383 ut_assert_nextline(
384 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass215be682023-01-17 10:48:03 -0700385 ut_assert_nextline("Hunting with: virtio");
Simon Glass5acb97a2023-01-17 10:47:33 -0700386 ut_assert_console_end();
387
388 /* List available hunters */
389 ut_assertok(run_command("bootdev hunt -l", 0));
390 ut_assert_nextlinen("Prio");
391 ut_assert_nextlinen("----");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700392 ut_assert_nextline(" 6 * ethernet eth_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700393 ut_assert_nextline(" 1 * simple_bus (none)");
Simon Glass7e1f6a42023-01-17 10:48:08 -0700394 ut_assert_nextline(" 5 * ide ide_bootdev");
395 ut_assert_nextline(" 2 * mmc mmc_bootdev");
396 ut_assert_nextline(" 4 * nvme nvme_bootdev");
397 ut_assert_nextline(" 4 * scsi scsi_bootdev");
398 ut_assert_nextline(" 4 * spi_flash sf_bootdev");
399 ut_assert_nextline(" 5 * usb usb_bootdev");
400 ut_assert_nextline(" 4 * virtio virtio_bootdev");
Simon Glasse4cf1062023-01-17 10:48:13 -0700401 ut_assert_nextline("(total hunters: 9)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700402 ut_assert_console_end();
403
Simon Glasse4cf1062023-01-17 10:48:13 -0700404 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass5acb97a2023-01-17 10:47:33 -0700405
406 return 0;
407}
408BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
409 UT_TESTF_ETH_BOOTDEV);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700410
Simon Glass484e4072023-01-17 10:48:14 -0700411/* Check searching for bootdevs using the hunters */
412static int bootdev_test_hunt_scan(struct unit_test_state *uts)
413{
414 struct bootflow_iter iter;
415 struct bootstd_priv *std;
416 struct bootflow bflow;
417
418 /* get access to the used hunters */
419 ut_assertok(bootstd_get_priv(&std));
420
421 ut_assertok(bootstd_test_drop_bootdev_order(uts));
Simon Glass5d3d44f2023-01-17 10:48:16 -0700422 ut_assertok(bootflow_scan_first(NULL, NULL, &iter,
423 BOOTFLOWF_SHOW | BOOTFLOWF_HUNT |
Simon Glass484e4072023-01-17 10:48:14 -0700424 BOOTFLOWF_SKIP_GLOBAL, &bflow));
425 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
426
427 return 0;
428}
429BOOTSTD_TEST(bootdev_test_hunt_scan, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
430
Simon Glass64cbc5c2023-01-17 10:47:42 -0700431/* Check that only bootable partitions are processed */
432static int bootdev_test_bootable(struct unit_test_state *uts)
433{
434 struct bootflow_iter iter;
435 struct bootflow bflow;
436 struct udevice *blk;
437
438 memset(&iter, '\0', sizeof(iter));
439 memset(&bflow, '\0', sizeof(bflow));
440 iter.part = 0;
441 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
442 iter.dev = blk;
443 ut_assertok(device_find_next_child(&iter.dev));
444 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
445
446 /*
447 * initially we don't have any knowledge of which partitions are
448 * bootable, but mmc1 has two partitions, with the first one being
449 * bootable
450 */
451 iter.part = 2;
452 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
453 ut_asserteq(0, iter.first_bootable);
454
455 /* scan with part == 0 to get the partition info */
456 iter.part = 0;
457 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
458 ut_asserteq(1, iter.first_bootable);
459
460 /* now it will refuse to use non-bootable partitions */
461 iter.part = 2;
462 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
463
464 return 0;
465}
466BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700467
468/* Check hunting for bootdev of a particular priority */
469static int bootdev_test_hunt_prio(struct unit_test_state *uts)
470{
471 test_set_skip_delays(true);
472
473 console_record_reset_enable();
Simon Glass7e1f6a42023-01-17 10:48:08 -0700474 ut_assertok(bootdev_hunt_prio(BOOTDEVP_4_SCAN_FAST, false));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700475 ut_assert_nextline("scanning bus for devices...");
476 ut_assert_skip_to_line(" Type: Hard Disk");
477 ut_assert_nextlinen(" Capacity:");
478 ut_assert_console_end();
479
480 /* now try a different priority, verbosely */
Simon Glass7e1f6a42023-01-17 10:48:08 -0700481 ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
Simon Glass3e9f6be2023-01-17 10:48:07 -0700482 ut_assert_nextline("Hunting with: ide");
483 ut_assert_nextline("Bus 0: not available ");
484 ut_assert_nextline("Hunting with: usb");
485 ut_assert_nextline(
486 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
487 ut_assert_console_end();
488
489 return 0;
490}
491BOOTSTD_TEST(bootdev_test_hunt_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassde567b12023-01-17 10:48:09 -0700492
493/* Check hunting for bootdevs with a particular label */
494static int bootdev_test_hunt_label(struct unit_test_state *uts)
495{
496 struct udevice *dev, *old;
497 struct bootstd_priv *std;
498 int mflags;
499
500 /* get access to the used hunters */
501 ut_assertok(bootstd_get_priv(&std));
502
503 /* scan an unknown uclass */
504 console_record_reset_enable();
505 old = (void *)&mflags; /* arbitrary pointer to check against dev */
506 dev = old;
507 mflags = 123;
508 ut_asserteq(-EINVAL,
509 bootdev_hunt_and_find_by_label("fred", &dev, &mflags));
510 ut_assert_nextline("Unknown uclass 'fred' in label");
511 ut_asserteq_ptr(old, dev);
512 ut_asserteq(123, mflags);
513 ut_assert_console_end();
514 ut_asserteq(0, std->hunters_used);
515
516 /* scan an invalid mmc controllers */
517 ut_asserteq(-ENOENT,
518 bootdev_hunt_and_find_by_label("mmc4", &dev, &mflags));
519 ut_asserteq_ptr(old, dev);
520 ut_asserteq(123, mflags);
521 ut_assert_nextline("Unknown seq 4 for label 'mmc4'");
522 ut_assert_console_end();
523
524 ut_assertok(bootstd_test_check_mmc_hunter(uts));
525
526 /* scan for a particular mmc controller */
527 ut_assertok(bootdev_hunt_and_find_by_label("mmc1", &dev, &mflags));
528 ut_assertnonnull(dev);
529 ut_asserteq_str("mmc1.bootdev", dev->name);
530 ut_asserteq(0, mflags);
531 ut_assert_console_end();
532
533 /* scan all of usb */
534 test_set_skip_delays(true);
535 ut_assertok(bootdev_hunt_and_find_by_label("usb", &dev, &mflags));
536 ut_assertnonnull(dev);
537 ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
538 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
539 ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
540 ut_assert_console_end();
541
542 return 0;
543}
544BOOTSTD_TEST(bootdev_test_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassac06b26a2023-01-17 10:48:10 -0700545
546/* Check iterating to the next label in a list */
547static int bootdev_test_next_label(struct unit_test_state *uts)
548{
549 const char *const labels[] = {"mmc0", "scsi", "dhcp", "pxe", NULL};
550 struct bootflow_iter iter;
551 struct bootstd_priv *std;
552 struct bootflow bflow;
553 struct udevice *dev;
554 int mflags;
555
556 test_set_eth_enable(false);
557
558 /* get access to the used hunters */
559 ut_assertok(bootstd_get_priv(&std));
560
561 memset(&iter, '\0', sizeof(iter));
562 memset(&bflow, '\0', sizeof(bflow));
563 iter.part = 0;
564 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
565 iter.cur_label = -1;
566 iter.labels = labels;
567
568 dev = NULL;
569 mflags = 123;
570 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
571 console_record_reset_enable();
572 ut_assert_console_end();
573 ut_assertnonnull(dev);
574 ut_asserteq_str("mmc0.bootdev", dev->name);
575 ut_asserteq(0, mflags);
576
577 ut_assertok(bootstd_test_check_mmc_hunter(uts));
578
579 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
580 ut_assert_nextline("scanning bus for devices...");
581 ut_assert_skip_to_line(
582 " Capacity: 1.9 MB = 0.0 GB (4095 x 512)");
583 ut_assert_console_end();
584 ut_assertnonnull(dev);
585 ut_asserteq_str("scsi.id0lun0.bootdev", dev->name);
586 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
587
Simon Glasse4cf1062023-01-17 10:48:13 -0700588 /* SCSI is sixth in the list, so bit 5 */
589 ut_asserteq(BIT(MMC_HUNTER) | BIT(5), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700590
591 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
592 ut_assert_console_end();
593 ut_assertnonnull(dev);
594 ut_asserteq_str("eth@10002000.bootdev", dev->name);
595 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_DHCP_ONLY,
596 mflags);
597
598 /* dhcp: Ethernet is first so bit 0 */
Simon Glasse4cf1062023-01-17 10:48:13 -0700599 ut_asserteq(BIT(MMC_HUNTER) | BIT(5) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700600
601 ut_assertok(bootdev_next_label(&iter, &dev, &mflags));
602 ut_assert_console_end();
603 ut_assertnonnull(dev);
604 ut_asserteq_str("eth@10002000.bootdev", dev->name);
605 ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS | BOOTFLOW_METHF_PXE_ONLY,
606 mflags);
607
608 /* pxe: Ethernet is first so bit 0 */
Simon Glasse4cf1062023-01-17 10:48:13 -0700609 ut_asserteq(BIT(MMC_HUNTER) | BIT(5) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700610
611 mflags = 123;
612 ut_asserteq(-ENODEV, bootdev_next_label(&iter, &dev, &mflags));
613 ut_asserteq(123, mflags);
614 ut_assert_console_end();
615
616 /* no change */
Simon Glasse4cf1062023-01-17 10:48:13 -0700617 ut_asserteq(BIT(MMC_HUNTER) | BIT(5) | BIT(0), std->hunters_used);
Simon Glassac06b26a2023-01-17 10:48:10 -0700618
619 return 0;
620}
621BOOTSTD_TEST(bootdev_test_next_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
622 UT_TESTF_ETH_BOOTDEV | UT_TESTF_SF_BOOTDEV);
Simon Glass660a9952023-01-17 10:48:11 -0700623
624
625/* Check iterating to the next prioirty in a list */
626static int bootdev_test_next_prio(struct unit_test_state *uts)
627{
628 struct bootflow_iter iter;
629 struct bootstd_priv *std;
630 struct bootflow bflow;
631 struct udevice *dev;
632 int ret;
633
Simon Glassba3d5372023-01-17 10:48:15 -0700634 test_set_eth_enable(false);
Simon Glass660a9952023-01-17 10:48:11 -0700635 test_set_skip_delays(true);
636
637 /* get access to the used hunters */
638 ut_assertok(bootstd_get_priv(&std));
639
640 memset(&iter, '\0', sizeof(iter));
641 memset(&bflow, '\0', sizeof(bflow));
642 iter.part = 0;
643 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
644 iter.cur_prio = 0;
645 iter.flags = BOOTFLOWF_SHOW;
646
647 dev = NULL;
648 console_record_reset_enable();
649 ut_assertok(bootdev_next_prio(&iter, &dev));
650 ut_assertnonnull(dev);
651 ut_asserteq_str("mmc2.bootdev", dev->name);
652
653 /* hunt flag not set, so this should not use any hunters */
654 ut_asserteq(0, std->hunters_used);
655 ut_assert_console_end();
656
657 /* now try again with hunting enabled */
658 iter.flags = BOOTFLOWF_SHOW | BOOTFLOWF_HUNT;
659 iter.cur_prio = 0;
660 iter.part = 0;
661
662 ut_assertok(bootdev_next_prio(&iter, &dev));
663 ut_asserteq_str("mmc2.bootdev", dev->name);
Simon Glasse4cf1062023-01-17 10:48:13 -0700664 ut_assert_nextline("Hunting with: simple_bus");
665 ut_assert_nextline("Found 2 extension board(s).");
Simon Glass660a9952023-01-17 10:48:11 -0700666 ut_assert_nextline("Hunting with: mmc");
667 ut_assert_console_end();
668
Simon Glasse4cf1062023-01-17 10:48:13 -0700669 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700670
671 ut_assertok(bootdev_next_prio(&iter, &dev));
672 ut_asserteq_str("mmc1.bootdev", dev->name);
673
674 ut_assertok(bootdev_next_prio(&iter, &dev));
675 ut_asserteq_str("mmc0.bootdev", dev->name);
676 ut_assert_console_end();
677
678 ut_assertok(bootdev_next_prio(&iter, &dev));
679 ut_asserteq_str("spi.bin@0.bootdev", dev->name);
680 ut_assert_skip_to_line("Hunting with: spi_flash");
681
682 /*
683 * this scans all bootdevs of priority BOOTDEVP_4_SCAN_FAST before it
684 * starts looking at the devices, so we se virtio as well
685 */
686 ut_assert_nextline("Hunting with: virtio");
687 ut_assert_nextlinen("SF: Detected m25p16");
688
689 ut_assertok(bootdev_next_prio(&iter, &dev));
690 ut_asserteq_str("spi.bin@1.bootdev", dev->name);
691 ut_assert_nextlinen("SF: Detected m25p16");
692 ut_assert_console_end();
693
694 /* keep going until there are no more bootdevs */
695 do {
696 ret = bootdev_next_prio(&iter, &dev);
697 } while (!ret);
698 ut_asserteq(-ENODEV, ret);
699 ut_assertnull(dev);
Simon Glasse4cf1062023-01-17 10:48:13 -0700700 ut_asserteq(GENMASK(MAX_HUNTER, 0), std->hunters_used);
Simon Glass660a9952023-01-17 10:48:11 -0700701
702 ut_assert_skip_to_line("Hunting with: ethernet");
703 ut_assert_console_end();
704
705 return 0;
706}
707BOOTSTD_TEST(bootdev_test_next_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
708 UT_TESTF_SF_BOOTDEV);