blob: 1c2a79fb10849d19681d50a299fe2f14ed23cb07 [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;
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
193 /* Start up USB which gives us three additional bootdevs */
194 usb_started = false;
195 ut_assertok(run_command("usb start", 0));
196
197 ut_assertok(bootstd_test_drop_bootdev_order(uts));
198
199 /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
200 console_record_reset_enable();
201 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
202 ut_asserteq(6, iter.num_devs);
203 ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
204 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
205 iter.dev_order[3]->name);
206
207 ut_assertok(bootdev_get_sibling_blk(iter.dev_order[3], &blk));
208 ut_asserteq_str("usb_mass_storage.lun0", blk->name);
209
210 /* adjust the priority of the first USB bootdev to the highest */
211 ucp = dev_get_uclass_plat(iter.dev_order[3]);
212 ucp->prio = 1;
213
214 bootflow_iter_uninit(&iter);
215 ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
216 ut_asserteq(6, iter.num_devs);
217 ut_asserteq_str("usb_mass_storage.lun0.bootdev",
218 iter.dev_order[0]->name);
219 ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name);
220
221 return 0;
222}
223BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);