blob: 32a31c4460993bc8afff81c74c793c6d21d13090 [file] [log] [blame]
Simon Glassfb1451b2022-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;
105
106 ut_assertok(bootdev_find_by_label("mmc2", &dev));
107 ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
108 media = dev_get_parent(dev);
109 ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
110 ut_asserteq_str("mmc2", media->name);
111
112 /* Check invalid uclass */
113 ut_asserteq(-EINVAL, bootdev_find_by_label("fred0", &dev));
114
115 /* Check unknown sequence number */
116 ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev));
117
118 return 0;
119}
120BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
121
122/* Check bootdev ordering with the bootdev-order property */
123static int bootdev_test_order(struct unit_test_state *uts)
124{
125 struct bootflow_iter iter;
126 struct bootflow bflow;
127
128 /*
129 * First try the order set by the bootdev-order property
130 * Like all sandbox unit tests this relies on the devicetree setting up
131 * the required devices:
132 *
133 * mmc0 - nothing connected
134 * mmc1 - connected to mmc1.img file
135 * mmc2 - nothing connected
136 */
137 ut_assertok(env_set("boot_targets", NULL));
138 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
139 ut_asserteq(2, iter.num_devs);
140 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
141 ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name);
142 bootflow_iter_uninit(&iter);
143
144 /* Use the environment variable to override it */
145 ut_assertok(env_set("boot_targets", "mmc1 mmc2"));
146 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
147 ut_asserteq(2, iter.num_devs);
148 ut_asserteq_str("mmc1.bootdev", iter.dev_order[0]->name);
149 ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name);
150 bootflow_iter_uninit(&iter);
151
152 /*
153 * Now drop both orderings, to check the default (prioriy/sequence)
154 * ordering
155 */
156 ut_assertok(env_set("boot_targets", NULL));
157 ut_assertok(bootstd_test_drop_bootdev_order(uts));
158
159 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
160 ut_asserteq(3, iter.num_devs);
161 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
162 ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name);
163 ut_asserteq_str("mmc0.bootdev", iter.dev_order[2]->name);
164
165 /*
166 * Check that adding aliases for the bootdevs works. We just fake it by
167 * setting the sequence numbers directly.
168 */
169 iter.dev_order[0]->seq_ = 0;
170 iter.dev_order[1]->seq_ = 3;
171 iter.dev_order[2]->seq_ = 2;
172 bootflow_iter_uninit(&iter);
173
174 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
175 ut_asserteq(3, iter.num_devs);
176 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
177 ut_asserteq_str("mmc0.bootdev", iter.dev_order[1]->name);
178 ut_asserteq_str("mmc1.bootdev", iter.dev_order[2]->name);
179 bootflow_iter_uninit(&iter);
180
181 return 0;
182}
183BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
184
185/* Check bootdev ordering with the uclass priority */
186static int bootdev_test_prio(struct unit_test_state *uts)
187{
188 struct bootdev_uc_plat *ucp;
189 struct bootflow_iter iter;
190 struct bootflow bflow;
191 struct udevice *blk;
192
Simon Glass04fb2b62023-01-17 10:47:37 -0700193 test_set_skip_delays(true);
194
Simon Glassfb1451b2022-04-24 23:31:24 -0600195 /* Start up USB which gives us three additional bootdevs */
196 usb_started = false;
197 ut_assertok(run_command("usb start", 0));
198
199 ut_assertok(bootstd_test_drop_bootdev_order(uts));
200
201 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
202 console_record_reset_enable();
203 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
204 ut_asserteq(6, iter.num_devs);
205 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
206 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
207 iter.dev_order[3]->name);
208
209 ut_assertok(bootdev_get_sibling_blk(iter.dev_order[3], &blk));
210 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
211
212 /* adjust the priority of the first USB bootdev to the highest */
213 ucp = dev_get_uclass_plat(iter.dev_order[3]);
214 ucp->prio = 1;
215
216 bootflow_iter_uninit(&iter);
217 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
218 ut_asserteq(6, iter.num_devs);
219 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
220 iter.dev_order[0]->name);
221 ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name);
222
223 return 0;
224}
225BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassbd90b092023-01-17 10:47:33 -0700226
227/* Check listing hunters */
228static int bootdev_test_hunter(struct unit_test_state *uts)
229{
230 struct bootstd_priv *std;
231
Simon Glass04fb2b62023-01-17 10:47:37 -0700232 test_set_skip_delays(true);
233
Simon Glassbd90b092023-01-17 10:47:33 -0700234 /* get access to the used hunters */
235 ut_assertok(bootstd_get_priv(&std));
236
237 console_record_reset_enable();
238 bootdev_list_hunters(std);
239 ut_assert_nextline("Prio Used Uclass Hunter");
240 ut_assert_nextlinen("----");
Simon Glass4146c822023-01-17 10:47:40 -0700241 ut_assert_nextline(" 50 ethernet eth_bootdev");
Simon Glass843160f2023-01-17 10:47:38 -0700242 ut_assert_nextline(" 10 mmc mmc_bootdev");
Simon Glass04fb2b62023-01-17 10:47:37 -0700243 ut_assert_nextline(" 40 usb usb_bootdev");
Simon Glass4146c822023-01-17 10:47:40 -0700244 ut_assert_nextline("(total hunters: 3)");
Simon Glassbd90b092023-01-17 10:47:33 -0700245 ut_assert_console_end();
246
Simon Glass04fb2b62023-01-17 10:47:37 -0700247 ut_assertok(bootdev_hunt("usb1", false));
248 ut_assert_nextline(
249 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glassc7b63d52023-01-17 10:47:34 -0700250 ut_assert_console_end();
251
Simon Glass4146c822023-01-17 10:47:40 -0700252 /* USB is third in the list, so bit 2 */
253 ut_asserteq(BIT(2), std->hunters_used);
Simon Glass04fb2b62023-01-17 10:47:37 -0700254
Simon Glassbd90b092023-01-17 10:47:33 -0700255 return 0;
256}
257BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
258
259/* Check 'bootdev hunt' command */
260static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
261{
262 struct bootstd_priv *std;
263
Simon Glass04fb2b62023-01-17 10:47:37 -0700264 test_set_skip_delays(true);
265
Simon Glassbd90b092023-01-17 10:47:33 -0700266 /* get access to the used hunters */
267 ut_assertok(bootstd_get_priv(&std));
268
269 console_record_reset_enable();
270 ut_assertok(run_command("bootdev hunt -l", 0));
271 ut_assert_nextline("Prio Used Uclass Hunter");
272 ut_assert_nextlinen("----");
Simon Glass4146c822023-01-17 10:47:40 -0700273 ut_assert_skip_to_line("(total hunters: 3)");
Simon Glassbd90b092023-01-17 10:47:33 -0700274 ut_assert_console_end();
275
276 /* Scan all hunters */
Simon Glass4146c822023-01-17 10:47:40 -0700277 sandbox_set_eth_enable(false);
278
Simon Glassbd90b092023-01-17 10:47:33 -0700279 ut_assertok(run_command("bootdev hunt", 0));
Simon Glass4146c822023-01-17 10:47:40 -0700280 ut_assert_nextline("Hunting with: ethernet");
Simon Glass843160f2023-01-17 10:47:38 -0700281 ut_assert_nextline("Hunting with: mmc");
Simon Glass04fb2b62023-01-17 10:47:37 -0700282 ut_assert_nextline("Hunting with: usb");
283 ut_assert_nextline(
284 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
Simon Glassbd90b092023-01-17 10:47:33 -0700285 ut_assert_console_end();
286
287 /* List available hunters */
288 ut_assertok(run_command("bootdev hunt -l", 0));
289 ut_assert_nextlinen("Prio");
290 ut_assert_nextlinen("----");
Simon Glass4146c822023-01-17 10:47:40 -0700291 ut_assert_nextline(" 50 * ethernet eth_bootdev");
Simon Glass843160f2023-01-17 10:47:38 -0700292 ut_assert_nextline(" 10 * mmc mmc_bootdev");
Simon Glass04fb2b62023-01-17 10:47:37 -0700293 ut_assert_nextline(" 40 * usb usb_bootdev");
Simon Glass843160f2023-01-17 10:47:38 -0700294
Simon Glass4146c822023-01-17 10:47:40 -0700295 ut_assert_nextline("(total hunters: 3)");
Simon Glassbd90b092023-01-17 10:47:33 -0700296 ut_assert_console_end();
297
Simon Glass4146c822023-01-17 10:47:40 -0700298 ut_asserteq(GENMASK(2, 0), std->hunters_used);
Simon Glassbd90b092023-01-17 10:47:33 -0700299
300 return 0;
301}
302BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
303 UT_TESTF_ETH_BOOTDEV);