blob: f9653633a68d9884207d4614030d7bafc859f678 [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));
116 ut_asserteq(BOOTFLOW_METHF_PXE_ONLY, mflags);
117 ut_assertok(bootdev_find_by_label("dhcp", &dev, &mflags));
118 ut_asserteq(BOOTFLOW_METHF_DHCP_ONLY, mflags);
119
Simon Glassb255efc2022-04-24 23:31:24 -0600120 /* Check invalid uclass */
Simon Glasse22fe922023-01-17 10:48:05 -0700121 ut_asserteq(-EINVAL, bootdev_find_by_label("fred0", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600122
123 /* Check unknown sequence number */
Simon Glasse22fe922023-01-17 10:48:05 -0700124 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
Simon Glassb255efc2022-04-24 23:31:24 -0600125
126 return 0;
127}
Simon Glasse22fe922023-01-17 10:48:05 -0700128BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
129 UT_TESTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600130
131/* Check bootdev ordering with the bootdev-order property */
132static int bootdev_test_order(struct unit_test_state *uts)
133{
134 struct bootflow_iter iter;
135 struct bootflow bflow;
136
137 /*
138 * First try the order set by the bootdev-order property
139 * Like all sandbox unit tests this relies on the devicetree setting up
140 * the required devices:
141 *
142 * mmc0 - nothing connected
143 * mmc1 - connected to mmc1.img file
144 * mmc2 - nothing connected
145 */
146 ut_assertok(env_set("boot_targets", NULL));
147 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
148 ut_asserteq(2, iter.num_devs);
149 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
150 ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name);
151 bootflow_iter_uninit(&iter);
152
153 /* Use the environment variable to override it */
154 ut_assertok(env_set("boot_targets", "mmc1 mmc2"));
155 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
156 ut_asserteq(2, iter.num_devs);
157 ut_asserteq_str("mmc1.bootdev", iter.dev_order[0]->name);
158 ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name);
159 bootflow_iter_uninit(&iter);
160
161 /*
162 * Now drop both orderings, to check the default (prioriy/sequence)
163 * ordering
164 */
165 ut_assertok(env_set("boot_targets", NULL));
166 ut_assertok(bootstd_test_drop_bootdev_order(uts));
167
168 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
169 ut_asserteq(3, iter.num_devs);
170 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
171 ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name);
172 ut_asserteq_str("mmc0.bootdev", iter.dev_order[2]->name);
173
174 /*
175 * Check that adding aliases for the bootdevs works. We just fake it by
176 * setting the sequence numbers directly.
177 */
178 iter.dev_order[0]->seq_ = 0;
179 iter.dev_order[1]->seq_ = 3;
180 iter.dev_order[2]->seq_ = 2;
181 bootflow_iter_uninit(&iter);
182
183 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
184 ut_asserteq(3, iter.num_devs);
185 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
186 ut_asserteq_str("mmc0.bootdev", iter.dev_order[1]->name);
187 ut_asserteq_str("mmc1.bootdev", iter.dev_order[2]->name);
188 bootflow_iter_uninit(&iter);
189
190 return 0;
191}
192BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
193
194/* Check bootdev ordering with the uclass priority */
195static int bootdev_test_prio(struct unit_test_state *uts)
196{
197 struct bootdev_uc_plat *ucp;
198 struct bootflow_iter iter;
199 struct bootflow bflow;
200 struct udevice *blk;
201
Simon Glassf3cb89f2023-01-17 10:47:37 -0700202 test_set_skip_delays(true);
203
Simon Glassb255efc2022-04-24 23:31:24 -0600204 /* Start up USB which gives us three additional bootdevs */
205 usb_started = false;
206 ut_assertok(run_command("usb start", 0));
207
208 ut_assertok(bootstd_test_drop_bootdev_order(uts));
209
210 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
211 console_record_reset_enable();
212 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
213 ut_asserteq(6, iter.num_devs);
214 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
215 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
216 iter.dev_order[3]->name);
217
218 ut_assertok(bootdev_get_sibling_blk(iter.dev_order[3], &blk));
219 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
220
221 /* adjust the priority of the first USB bootdev to the highest */
222 ucp = dev_get_uclass_plat(iter.dev_order[3]);
223 ucp->prio = 1;
224
225 bootflow_iter_uninit(&iter);
226 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
227 ut_asserteq(6, iter.num_devs);
228 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
229 iter.dev_order[0]->name);
230 ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name);
231
232 return 0;
233}
234BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass5acb97a2023-01-17 10:47:33 -0700235
236/* Check listing hunters */
237static int bootdev_test_hunter(struct unit_test_state *uts)
238{
239 struct bootstd_priv *std;
240
Simon Glassf3cb89f2023-01-17 10:47:37 -0700241 test_set_skip_delays(true);
242
Simon Glass5acb97a2023-01-17 10:47:33 -0700243 /* get access to the used hunters */
244 ut_assertok(bootstd_get_priv(&std));
245
246 console_record_reset_enable();
247 bootdev_list_hunters(std);
248 ut_assert_nextline("Prio Used Uclass Hunter");
249 ut_assert_nextlinen("----");
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700250 ut_assert_nextline(" 50 ethernet eth_bootdev");
Simon Glassbe7c6662023-01-17 10:47:46 -0700251 ut_assert_nextline(" 40 ide ide_bootdev");
Simon Glassdf71f262023-01-17 10:47:38 -0700252 ut_assert_nextline(" 10 mmc mmc_bootdev");
Simon Glass08382872023-01-17 10:47:47 -0700253 ut_assert_nextline(" 30 nvme nvme_bootdev");
Simon Glass9985e9c2023-01-17 10:47:45 -0700254 ut_assert_nextline(" 30 scsi scsi_bootdev");
Simon Glass215be682023-01-17 10:48:03 -0700255 ut_assert_nextline(" 30 spi_flash sf_bootdev");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700256 ut_assert_nextline(" 40 usb usb_bootdev");
Simon Glassa4bf8f62023-01-17 10:47:52 -0700257 ut_assert_nextline(" 30 virtio virtio_bootdev");
Simon Glass215be682023-01-17 10:48:03 -0700258 ut_assert_nextline("(total hunters: 8)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700259 ut_assert_console_end();
260
Simon Glassf3cb89f2023-01-17 10:47:37 -0700261 ut_assertok(bootdev_hunt("usb1", false));
262 ut_assert_nextline(
263 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass1248d2b2023-01-17 10:47:34 -0700264 ut_assert_console_end();
265
Simon Glass215be682023-01-17 10:48:03 -0700266 /* USB is fifth in the list, so bit 6 */
267 ut_asserteq(BIT(6), std->hunters_used);
Simon Glassf3cb89f2023-01-17 10:47:37 -0700268
Simon Glass5acb97a2023-01-17 10:47:33 -0700269 return 0;
270}
271BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
272
273/* Check 'bootdev hunt' command */
274static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
275{
276 struct bootstd_priv *std;
277
Simon Glassf3cb89f2023-01-17 10:47:37 -0700278 test_set_skip_delays(true);
279
Simon Glass5acb97a2023-01-17 10:47:33 -0700280 /* get access to the used hunters */
281 ut_assertok(bootstd_get_priv(&std));
282
283 console_record_reset_enable();
284 ut_assertok(run_command("bootdev hunt -l", 0));
285 ut_assert_nextline("Prio Used Uclass Hunter");
286 ut_assert_nextlinen("----");
Simon Glass215be682023-01-17 10:48:03 -0700287 ut_assert_skip_to_line("(total hunters: 8)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700288 ut_assert_console_end();
289
290 /* Scan all hunters */
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700291 sandbox_set_eth_enable(false);
292
Simon Glass5acb97a2023-01-17 10:47:33 -0700293 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700294 ut_assert_nextline("Hunting with: ethernet");
Simon Glassbe7c6662023-01-17 10:47:46 -0700295 ut_assert_nextline("Hunting with: ide");
296 ut_assert_nextline("Bus 0: not available ");
Simon Glassdf71f262023-01-17 10:47:38 -0700297 ut_assert_nextline("Hunting with: mmc");
Simon Glass08382872023-01-17 10:47:47 -0700298 ut_assert_nextline("Hunting with: nvme");
Simon Glass9985e9c2023-01-17 10:47:45 -0700299 ut_assert_nextline("Hunting with: scsi");
300 ut_assert_nextline("scanning bus for devices...");
Simon Glass215be682023-01-17 10:48:03 -0700301 ut_assert_skip_to_line("Hunting with: spi_flash");
302 ut_assert_nextline("Hunting with: usb");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700303 ut_assert_nextline(
304 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glass215be682023-01-17 10:48:03 -0700305 ut_assert_nextline("Hunting with: virtio");
Simon Glass5acb97a2023-01-17 10:47:33 -0700306 ut_assert_console_end();
307
308 /* List available hunters */
309 ut_assertok(run_command("bootdev hunt -l", 0));
310 ut_assert_nextlinen("Prio");
311 ut_assert_nextlinen("----");
Simon Glass5ea0a8d2023-01-17 10:47:40 -0700312 ut_assert_nextline(" 50 * ethernet eth_bootdev");
Simon Glassbe7c6662023-01-17 10:47:46 -0700313 ut_assert_nextline(" 40 * ide ide_bootdev");
Simon Glassdf71f262023-01-17 10:47:38 -0700314 ut_assert_nextline(" 10 * mmc mmc_bootdev");
Simon Glass08382872023-01-17 10:47:47 -0700315 ut_assert_nextline(" 30 * nvme nvme_bootdev");
Simon Glass9985e9c2023-01-17 10:47:45 -0700316 ut_assert_nextline(" 30 * scsi scsi_bootdev");
Simon Glass215be682023-01-17 10:48:03 -0700317 ut_assert_nextline(" 30 * spi_flash sf_bootdev");
Simon Glassf3cb89f2023-01-17 10:47:37 -0700318 ut_assert_nextline(" 40 * usb usb_bootdev");
Simon Glassa4bf8f62023-01-17 10:47:52 -0700319 ut_assert_nextline(" 30 * virtio virtio_bootdev");
Simon Glass215be682023-01-17 10:48:03 -0700320 ut_assert_nextline("(total hunters: 8)");
Simon Glass5acb97a2023-01-17 10:47:33 -0700321 ut_assert_console_end();
322
Simon Glass215be682023-01-17 10:48:03 -0700323 ut_asserteq(GENMASK(7, 0), std->hunters_used);
Simon Glass5acb97a2023-01-17 10:47:33 -0700324
325 return 0;
326}
327BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
328 UT_TESTF_ETH_BOOTDEV);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700329
330/* Check that only bootable partitions are processed */
331static int bootdev_test_bootable(struct unit_test_state *uts)
332{
333 struct bootflow_iter iter;
334 struct bootflow bflow;
335 struct udevice *blk;
336
337 memset(&iter, '\0', sizeof(iter));
338 memset(&bflow, '\0', sizeof(bflow));
339 iter.part = 0;
340 ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk));
341 iter.dev = blk;
342 ut_assertok(device_find_next_child(&iter.dev));
343 uclass_first_device(UCLASS_BOOTMETH, &bflow.method);
344
345 /*
346 * initially we don't have any knowledge of which partitions are
347 * bootable, but mmc1 has two partitions, with the first one being
348 * bootable
349 */
350 iter.part = 2;
351 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
352 ut_asserteq(0, iter.first_bootable);
353
354 /* scan with part == 0 to get the partition info */
355 iter.part = 0;
356 ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
357 ut_asserteq(1, iter.first_bootable);
358
359 /* now it will refuse to use non-bootable partitions */
360 iter.part = 2;
361 ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow));
362
363 return 0;
364}
365BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700366
367/* Check hunting for bootdev of a particular priority */
368static int bootdev_test_hunt_prio(struct unit_test_state *uts)
369{
370 test_set_skip_delays(true);
371
372 console_record_reset_enable();
373 ut_assertok(bootdev_hunt_prio(BOOTDEVP_2_SCAN_FAST, false));
374 ut_assert_nextline("scanning bus for devices...");
375 ut_assert_skip_to_line(" Type: Hard Disk");
376 ut_assert_nextlinen(" Capacity:");
377 ut_assert_console_end();
378
379 /* now try a different priority, verbosely */
380 ut_assertok(bootdev_hunt_prio(BOOTDEVP_3_SCAN_SLOW, true));
381 ut_assert_nextline("Hunting with: ide");
382 ut_assert_nextline("Bus 0: not available ");
383 ut_assert_nextline("Hunting with: usb");
384 ut_assert_nextline(
385 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
386 ut_assert_console_end();
387
388 return 0;
389}
390BOOTSTD_TEST(bootdev_test_hunt_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);