blob: ead71172e12fd6f3237db43da37c3f1e830f420e [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 <bootdev.h>
11#include <bootflow.h>
Simon Glass0917f772022-07-30 15:52:34 -060012#include <bootmeth.h>
Simon Glassfb1451b2022-04-24 23:31:24 -060013#include <bootstd.h>
Simon Glassd985f1d2023-01-06 08:52:41 -060014#include <cli.h>
Simon Glassfb1451b2022-04-24 23:31:24 -060015#include <dm.h>
Simon Glasse64c2952023-01-06 08:52:42 -060016#include <expo.h>
Simon Glass11361c52022-07-30 15:52:36 -060017#ifdef CONFIG_SANDBOX
Simon Glass2662b542022-07-30 15:52:22 -060018#include <asm/test.h>
Simon Glass11361c52022-07-30 15:52:36 -060019#endif
Simon Glassd985f1d2023-01-06 08:52:41 -060020#include <dm/device-internal.h>
Simon Glassfb1451b2022-04-24 23:31:24 -060021#include <dm/lists.h>
22#include <test/suites.h>
23#include <test/ut.h>
24#include "bootstd_common.h"
Simon Glasse64c2952023-01-06 08:52:42 -060025#include "../../boot/bootflow_internal.h"
26#include "../../boot/scene_internal.h"
Simon Glassfb1451b2022-04-24 23:31:24 -060027
Simon Glassd985f1d2023-01-06 08:52:41 -060028DECLARE_GLOBAL_DATA_PTR;
29
30extern U_BOOT_DRIVER(bootmeth_script);
31
Simon Glassf25f5752022-07-30 15:52:16 -060032static int inject_response(struct unit_test_state *uts)
33{
34 /*
35 * The image being booted presents a menu of options:
36 *
37 * Fedora-Workstation-armhfp-31-1.9 Boot Options.
38 * 1: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
39 * Enter choice:
40 *
41 * Provide input for this, to avoid waiting two seconds for a timeout.
42 */
43 ut_asserteq(2, console_in_puts("1\n"));
44
45 return 0;
46}
47
Simon Glassfb1451b2022-04-24 23:31:24 -060048/* Check 'bootflow scan/list' commands */
49static int bootflow_cmd(struct unit_test_state *uts)
50{
51 console_record_reset_enable();
52 ut_assertok(run_command("bootdev select 1", 0));
53 ut_assert_console_end();
Simon Glass18552d22023-01-17 10:48:13 -070054 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -060055 ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
56 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
57 ut_assert_nextlinen("---");
Simon Glass47aedc22023-01-17 10:48:14 -070058 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
59 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glass79f66352023-05-10 16:34:46 -060060 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass47aedc22023-01-17 10:48:14 -070061 ut_assert_nextline("No more bootdevs");
Simon Glassfb1451b2022-04-24 23:31:24 -060062 ut_assert_nextlinen("---");
63 ut_assert_nextline("(1 bootflow, 1 valid)");
64 ut_assert_console_end();
65
66 ut_assertok(run_command("bootflow list", 0));
67 ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'");
68 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
69 ut_assert_nextlinen("---");
Simon Glass79f66352023-05-10 16:34:46 -060070 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -060071 ut_assert_nextlinen("---");
72 ut_assert_nextline("(1 bootflow, 1 valid)");
73 ut_assert_console_end();
74
75 return 0;
76}
77BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
78
Simon Glass47aedc22023-01-17 10:48:14 -070079/* Check 'bootflow scan' with a label / seq */
Simon Glassfb1451b2022-04-24 23:31:24 -060080static int bootflow_cmd_label(struct unit_test_state *uts)
81{
Simon Glass47aedc22023-01-17 10:48:14 -070082 test_set_eth_enable(false);
83
Simon Glassfb1451b2022-04-24 23:31:24 -060084 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -070085 ut_assertok(run_command("bootflow scan -lH mmc1", 0));
Simon Glass47aedc22023-01-17 10:48:14 -070086 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
Simon Glassfb1451b2022-04-24 23:31:24 -060087 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
88 ut_assert_console_end();
89
Simon Glass47aedc22023-01-17 10:48:14 -070090 ut_assertok(run_command("bootflow scan -lH 0", 0));
91 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassfb1451b2022-04-24 23:31:24 -060092 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
93 ut_assert_console_end();
94
Simon Glass47aedc22023-01-17 10:48:14 -070095 /*
96 * with ethernet enabled we have 8 devices ahead of the mmc ones:
97 *
98 * ut_assertok(run_command("bootdev list", 0));
99 * Seq Probed Status Uclass Name
100 * --- ------ ------ -------- ------------------
101 * 0 [ + ] OK ethernet eth@10002000.bootdev
102 * 1 [ ] OK ethernet eth@10003000.bootdev
103 * 2 [ ] OK ethernet sbe5.bootdev
104 * 3 [ ] OK ethernet eth@10004000.bootdev
105 * 4 [ ] OK ethernet phy-test-eth.bootdev
106 * 5 [ ] OK ethernet dsa-test-eth.bootdev
107 * 6 [ ] OK ethernet dsa-test@0.bootdev
108 * 7 [ ] OK ethernet dsa-test@1.bootdev
109 * 8 [ ] OK mmc mmc2.bootdev
110 * 9 [ + ] OK mmc mmc1.bootdev
111 * a [ ] OK mmc mmc0.bootdev
112 */
113 ut_assertok(run_command("bootflow scan -lH 9", 0));
114 ut_assert_nextline("Scanning for bootflows with label '9'");
115 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
116
Simon Glass18552d22023-01-17 10:48:13 -0700117 ut_assertok(run_command("bootflow scan -lH 0", 0));
Simon Glass47aedc22023-01-17 10:48:14 -0700118 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassfb1451b2022-04-24 23:31:24 -0600119 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
120 ut_assert_console_end();
121
122 return 0;
123}
Simon Glass47aedc22023-01-17 10:48:14 -0700124BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
125 UT_TESTF_ETH_BOOTDEV);
Simon Glassfb1451b2022-04-24 23:31:24 -0600126
127/* Check 'bootflow scan/list' commands using all bootdevs */
128static int bootflow_cmd_glob(struct unit_test_state *uts)
129{
130 ut_assertok(bootstd_test_drop_bootdev_order(uts));
131
132 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -0700133 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -0600134 ut_assert_nextline("Scanning for bootflows in all bootdevs");
135 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
136 ut_assert_nextlinen("---");
137 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
138 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glass79f66352023-05-10 16:34:46 -0600139 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600140 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
141 ut_assert_nextline("No more bootdevs");
142 ut_assert_nextlinen("---");
143 ut_assert_nextline("(1 bootflow, 1 valid)");
144 ut_assert_console_end();
145
146 ut_assertok(run_command("bootflow list", 0));
147 ut_assert_nextline("Showing all bootflows");
148 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
149 ut_assert_nextlinen("---");
Simon Glass79f66352023-05-10 16:34:46 -0600150 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600151 ut_assert_nextlinen("---");
152 ut_assert_nextline("(1 bootflow, 1 valid)");
153 ut_assert_console_end();
154
155 return 0;
156}
157BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
158
159/* Check 'bootflow scan -e' */
160static int bootflow_cmd_scan_e(struct unit_test_state *uts)
161{
162 ut_assertok(bootstd_test_drop_bootdev_order(uts));
163
164 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -0700165 ut_assertok(run_command("bootflow scan -aleGH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -0600166 ut_assert_nextline("Scanning for bootflows in all bootdevs");
167 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
168 ut_assert_nextlinen("---");
169 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
Simon Glass79f66352023-05-10 16:34:46 -0600170 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole <NULL>");
Simon Glassc8894342023-05-10 16:34:26 -0600171 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassfb1451b2022-04-24 23:31:24 -0600172 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole <NULL>");
Simon Glassc8894342023-05-10 16:34:26 -0600173 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassfb1451b2022-04-24 23:31:24 -0600174
175 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glass79f66352023-05-10 16:34:46 -0600176 ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole <NULL>");
Simon Glassc8894342023-05-10 16:34:26 -0600177 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassfb1451b2022-04-24 23:31:24 -0600178 ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole <NULL>");
Simon Glassc8894342023-05-10 16:34:26 -0600179 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glass79f66352023-05-10 16:34:46 -0600180 ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600181 ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 efi/boot/bootsbox.efi");
182
183 ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
184 ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole <NULL>");
Simon Glassc8894342023-05-10 16:34:26 -0600185 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassfb1451b2022-04-24 23:31:24 -0600186 ut_assert_nextline("No more bootdevs");
187 ut_assert_nextlinen("---");
188 ut_assert_nextline("(64 bootflows, 1 valid)");
189 ut_assert_console_end();
190
191 ut_assertok(run_command("bootflow list", 0));
192 ut_assert_nextline("Showing all bootflows");
193 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
194 ut_assert_nextlinen("---");
Simon Glass79f66352023-05-10 16:34:46 -0600195 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole <NULL>");
Simon Glassfb1451b2022-04-24 23:31:24 -0600196 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole <NULL>");
Simon Glass79f66352023-05-10 16:34:46 -0600197 ut_assert_skip_to_line(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600198 ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole <NULL>");
199 ut_assert_nextlinen("---");
200 ut_assert_nextline("(64 bootflows, 1 valid)");
201 ut_assert_console_end();
202
203 return 0;
204}
205BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
206
207/* Check 'bootflow info' */
208static int bootflow_cmd_info(struct unit_test_state *uts)
209{
210 console_record_reset_enable();
211 ut_assertok(run_command("bootdev select 1", 0));
212 ut_assert_console_end();
213 ut_assertok(run_command("bootflow scan", 0));
214 ut_assert_console_end();
215 ut_assertok(run_command("bootflow select 0", 0));
216 ut_assert_console_end();
217 ut_assertok(run_command("bootflow info", 0));
218 ut_assert_nextline("Name: mmc1.bootdev.part_1");
219 ut_assert_nextline("Device: mmc1.bootdev");
220 ut_assert_nextline("Block dev: mmc1.blk");
Simon Glass79f66352023-05-10 16:34:46 -0600221 ut_assert_nextline("Method: extlinux");
Simon Glassfb1451b2022-04-24 23:31:24 -0600222 ut_assert_nextline("State: ready");
223 ut_assert_nextline("Partition: 1");
224 ut_assert_nextline("Subdir: (none)");
225 ut_assert_nextline("Filename: /extlinux/extlinux.conf");
226 ut_assert_nextlinen("Buffer: ");
227 ut_assert_nextline("Size: 253 (595 bytes)");
Simon Glass2175e762023-01-06 08:52:33 -0600228 ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
Simon Glassf4a91652023-07-12 09:04:34 -0600229 ut_assert_nextline("Cmdline: (none)");
Simon Glass24d8e1b2023-01-06 08:52:34 -0600230 ut_assert_nextline("Logo: (none)");
Simon Glass7638c852023-01-17 10:47:56 -0700231 ut_assert_nextline("FDT: <NULL>");
Simon Glassfb1451b2022-04-24 23:31:24 -0600232 ut_assert_nextline("Error: 0");
233 ut_assert_console_end();
234
235 ut_assertok(run_command("bootflow info -d", 0));
236 ut_assert_nextline("Name: mmc1.bootdev.part_1");
237 ut_assert_skip_to_line("Error: 0");
238 ut_assert_nextline("Contents:");
239 ut_assert_nextline("%s", "");
240 ut_assert_nextline("# extlinux.conf generated by appliance-creator");
241 ut_assert_skip_to_line(" initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
242 ut_assert_console_end();
243
244 return 0;
245}
246BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
247
248/* Check 'bootflow scan -b' to boot the first available bootdev */
249static int bootflow_scan_boot(struct unit_test_state *uts)
250{
251 console_record_reset_enable();
Simon Glassf25f5752022-07-30 15:52:16 -0600252 ut_assertok(inject_response(uts));
Simon Glassfb1451b2022-04-24 23:31:24 -0600253 ut_assertok(run_command("bootflow scan -b", 0));
254 ut_assert_nextline(
Simon Glass79f66352023-05-10 16:34:46 -0600255 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassfb1451b2022-04-24 23:31:24 -0600256 ut_assert_nextline("Ignoring unknown command: ui");
257
258 /*
259 * We expect it to get through to boot although sandbox always returns
260 * -EFAULT as it cannot actually boot the kernel
261 */
262 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
263 ut_assert_nextline("Boot failed (err=-14)");
264 ut_assert_console_end();
265
266 return 0;
267}
268BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
269
270/* Check iterating through available bootflows */
271static int bootflow_iter(struct unit_test_state *uts)
272{
273 struct bootflow_iter iter;
274 struct bootflow bflow;
275
276 bootstd_clear_glob();
277
278 /* The first device is mmc2.bootdev which has no media */
279 ut_asserteq(-EPROTONOSUPPORT,
Simon Glass4b7cb052023-01-17 10:48:16 -0700280 bootflow_scan_first(NULL, NULL, &iter,
Simon Glass4f806f32023-02-22 12:17:03 -0700281 BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600282 ut_asserteq(2, iter.num_methods);
283 ut_asserteq(0, iter.cur_method);
284 ut_asserteq(0, iter.part);
285 ut_asserteq(0, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600286 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600287 ut_asserteq(0, bflow.err);
288
289 /*
Simon Glass0917f772022-07-30 15:52:34 -0600290 * This shows MEDIA even though there is none, since in
Simon Glassfb1451b2022-04-24 23:31:24 -0600291 * bootdev_find_in_blk() we call part_get_info() which returns
292 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
293 * know.
294 */
295 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
296
297 ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
298 ut_asserteq(2, iter.num_methods);
299 ut_asserteq(1, iter.cur_method);
300 ut_asserteq(0, iter.part);
301 ut_asserteq(0, iter.max_part);
302 ut_asserteq_str("efi", iter.method->name);
303 ut_asserteq(0, bflow.err);
304 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
305 bootflow_free(&bflow);
306
307 /* The next device is mmc1.bootdev - at first we use the whole device */
308 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
309 ut_asserteq(2, iter.num_methods);
310 ut_asserteq(0, iter.cur_method);
311 ut_asserteq(0, iter.part);
312 ut_asserteq(0x1e, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600313 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600314 ut_asserteq(0, bflow.err);
315 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
316 bootflow_free(&bflow);
317
318 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
319 ut_asserteq(2, iter.num_methods);
320 ut_asserteq(1, iter.cur_method);
321 ut_asserteq(0, iter.part);
322 ut_asserteq(0x1e, iter.max_part);
323 ut_asserteq_str("efi", iter.method->name);
324 ut_asserteq(0, bflow.err);
325 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
326 bootflow_free(&bflow);
327
328 /* Then more to partition 1 where we find something */
329 ut_assertok(bootflow_scan_next(&iter, &bflow));
330 ut_asserteq(2, iter.num_methods);
331 ut_asserteq(0, iter.cur_method);
332 ut_asserteq(1, iter.part);
333 ut_asserteq(0x1e, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600334 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600335 ut_asserteq(0, bflow.err);
336 ut_asserteq(BOOTFLOWST_READY, bflow.state);
337 bootflow_free(&bflow);
338
339 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
340 ut_asserteq(2, iter.num_methods);
341 ut_asserteq(1, iter.cur_method);
342 ut_asserteq(1, iter.part);
343 ut_asserteq(0x1e, iter.max_part);
344 ut_asserteq_str("efi", iter.method->name);
345 ut_asserteq(0, bflow.err);
346 ut_asserteq(BOOTFLOWST_FS, bflow.state);
347 bootflow_free(&bflow);
348
Simon Glassdcffa442023-01-17 10:47:41 -0700349 /* Then more to partition 2 which exists but is not bootable */
Simon Glassf0e358f2023-01-17 10:47:42 -0700350 ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600351 ut_asserteq(2, iter.num_methods);
352 ut_asserteq(0, iter.cur_method);
353 ut_asserteq(2, iter.part);
354 ut_asserteq(0x1e, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600355 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600356 ut_asserteq(0, bflow.err);
Simon Glassf0e358f2023-01-17 10:47:42 -0700357 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
Simon Glassfb1451b2022-04-24 23:31:24 -0600358 bootflow_free(&bflow);
359
360 bootflow_iter_uninit(&iter);
361
362 ut_assert_console_end();
363
364 return 0;
365}
366BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
367
Simon Glass11361c52022-07-30 15:52:36 -0600368#if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
Simon Glassfb1451b2022-04-24 23:31:24 -0600369/* Check using the system bootdev */
370static int bootflow_system(struct unit_test_state *uts)
371{
Simon Glassecb274c2023-01-17 10:47:23 -0700372 struct udevice *bootstd, *dev;
Simon Glassfb1451b2022-04-24 23:31:24 -0600373
Simon Glassae0bf222022-10-11 09:47:20 -0600374 if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
Simon Glassc43635b2022-10-20 18:22:49 -0600375 return -EAGAIN;
Simon Glassecb274c2023-01-17 10:47:23 -0700376 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
377 ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_efi_mgr),
378 "efi_mgr", 0, ofnode_null(), &dev));
379 ut_assertok(device_probe(dev));
Simon Glass2662b542022-07-30 15:52:22 -0600380 sandbox_set_fake_efi_mgr_dev(dev, true);
Simon Glassfb1451b2022-04-24 23:31:24 -0600381
Simon Glassfb1451b2022-04-24 23:31:24 -0600382 /* We should get a single 'bootmgr' method right at the end */
383 bootstd_clear_glob();
384 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -0700385 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassc627cfc2022-07-30 15:52:27 -0600386 ut_assert_skip_to_line(
Simon Glassbd18b692022-07-30 15:52:28 -0600387 " 0 efi_mgr ready (none) 0 <NULL> <NULL>");
Simon Glassc627cfc2022-07-30 15:52:27 -0600388 ut_assert_skip_to_line("No more bootdevs");
Simon Glassecb274c2023-01-17 10:47:23 -0700389 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
Simon Glassfb1451b2022-04-24 23:31:24 -0600390 ut_assert_console_end();
391
392 return 0;
393}
Simon Glassbd18b692022-07-30 15:52:28 -0600394BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_PDATA |
395 UT_TESTF_SCAN_FDT);
Simon Glass11361c52022-07-30 15:52:36 -0600396#endif
Simon Glassfb1451b2022-04-24 23:31:24 -0600397
398/* Check disabling a bootmethod if it requests it */
399static int bootflow_iter_disable(struct unit_test_state *uts)
400{
401 struct udevice *bootstd, *dev;
402 struct bootflow_iter iter;
403 struct bootflow bflow;
404 int i;
405
406 /* Add the EFI bootmgr driver */
407 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
408 ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
409 &dev));
410
Simon Glassfb1451b2022-04-24 23:31:24 -0600411 ut_assertok(bootstd_test_drop_bootdev_order(uts));
412
413 bootstd_clear_glob();
Simon Glassf25f5752022-07-30 15:52:16 -0600414 console_record_reset_enable();
415 ut_assertok(inject_response(uts));
Simon Glass18552d22023-01-17 10:48:13 -0700416 ut_assertok(run_command("bootflow scan -lbH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -0600417
418 /* Try to boot the bootmgr flow, which will fail */
419 console_record_reset_enable();
Simon Glass4b7cb052023-01-17 10:48:16 -0700420 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600421 ut_asserteq(3, iter.num_methods);
422 ut_asserteq_str("sandbox", iter.method->name);
Simon Glassf25f5752022-07-30 15:52:16 -0600423 ut_assertok(inject_response(uts));
Simon Glassfb1451b2022-04-24 23:31:24 -0600424 ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
425
426 ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
427 ut_assert_console_end();
428
429 /* Check that the sandbox bootmeth has been removed */
430 ut_asserteq(2, iter.num_methods);
431 for (i = 0; i < iter.num_methods; i++)
432 ut_assert(strcmp("sandbox", iter.method_order[i]->name));
433
434 return 0;
435}
436BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
437
Simon Glass0917f772022-07-30 15:52:34 -0600438/* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
439static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
440{
Simon Glass11361c52022-07-30 15:52:36 -0600441 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc43635b2022-10-20 18:22:49 -0600442 return -EAGAIN;
Simon Glass11361c52022-07-30 15:52:36 -0600443
Simon Glass0917f772022-07-30 15:52:34 -0600444 ut_assertok(bootstd_test_drop_bootdev_order(uts));
445
446 /*
447 * Make sure that the -G flag makes the scan fail, since this is not
448 * supported when an ordering is provided
449 */
450 console_record_reset_enable();
451 ut_assertok(bootmeth_set_order("efi firmware0"));
Simon Glass18552d22023-01-17 10:48:13 -0700452 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glass0917f772022-07-30 15:52:34 -0600453 ut_assert_nextline("Scanning for bootflows in all bootdevs");
454 ut_assert_nextline(
455 "Seq Method State Uclass Part Name Filename");
456 ut_assert_nextlinen("---");
457 ut_assert_nextlinen("---");
458 ut_assert_nextline("(0 bootflows, 0 valid)");
459 ut_assert_console_end();
460
Simon Glass18552d22023-01-17 10:48:13 -0700461 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glass0917f772022-07-30 15:52:34 -0600462 ut_assert_nextline("Scanning for bootflows in all bootdevs");
463 ut_assert_nextline(
464 "Seq Method State Uclass Part Name Filename");
465 ut_assert_nextlinen("---");
466 ut_assert_nextline("Scanning global bootmeth 'firmware0':");
467 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
468 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
469 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
470 ut_assert_nextline("No more bootdevs");
471 ut_assert_nextlinen("---");
472 ut_assert_nextline("(0 bootflows, 0 valid)");
473 ut_assert_console_end();
474
475 return 0;
476}
477BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
478
Simon Glassfb1451b2022-04-24 23:31:24 -0600479/* Check 'bootflow boot' to boot a selected bootflow */
480static int bootflow_cmd_boot(struct unit_test_state *uts)
481{
482 console_record_reset_enable();
483 ut_assertok(run_command("bootdev select 1", 0));
484 ut_assert_console_end();
485 ut_assertok(run_command("bootflow scan", 0));
486 ut_assert_console_end();
487 ut_assertok(run_command("bootflow select 0", 0));
488 ut_assert_console_end();
Simon Glassf25f5752022-07-30 15:52:16 -0600489
490 ut_assertok(inject_response(uts));
Simon Glassfb1451b2022-04-24 23:31:24 -0600491 ut_asserteq(1, run_command("bootflow boot", 0));
492 ut_assert_nextline(
Simon Glass79f66352023-05-10 16:34:46 -0600493 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassfb1451b2022-04-24 23:31:24 -0600494 ut_assert_nextline("Ignoring unknown command: ui");
495
496 /*
497 * We expect it to get through to boot although sandbox always returns
498 * -EFAULT as it cannot actually boot the kernel
499 */
500 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
501 ut_assert_nextline("Boot failed (err=-14)");
502 ut_assert_console_end();
503
504 return 0;
505}
506BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassd985f1d2023-01-06 08:52:41 -0600507
Simon Glasse64c2952023-01-06 08:52:42 -0600508/**
509 * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
510 *
511 * @uts: Unit test state
512 * Returns 0 on success, -ve on failure
513 */
514static int prep_mmc4_bootdev(struct unit_test_state *uts)
Simon Glassd985f1d2023-01-06 08:52:41 -0600515{
516 static const char *order[] = {"mmc2", "mmc1", "mmc4", NULL};
517 struct udevice *dev, *bootstd;
518 struct bootstd_priv *std;
519 const char **old_order;
Simon Glassd985f1d2023-01-06 08:52:41 -0600520 ofnode node;
521
522 /* Enable the mmc4 node since we need a second bootflow */
523 node = ofnode_path("/mmc4");
524 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
525
526 /* Enable the script bootmeth too */
527 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
528 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_script),
529 "bootmeth_script", 0, ofnode_null(), &dev));
530
531 /* Change the order to include mmc4 */
532 std = dev_get_priv(bootstd);
533 old_order = std->bootdev_order;
534 std->bootdev_order = order;
535
536 console_record_reset_enable();
537 ut_assertok(run_command("bootflow scan", 0));
538 ut_assert_console_end();
539
540 /* Restore the order used by the device tree */
541 std->bootdev_order = old_order;
542
Simon Glasse64c2952023-01-06 08:52:42 -0600543 return 0;
544}
545
546/* Check 'bootflow menu' to select a bootflow */
547static int bootflow_cmd_menu(struct unit_test_state *uts)
548{
549 char prev[3];
550
551 ut_assertok(prep_mmc4_bootdev(uts));
552
Simon Glassd985f1d2023-01-06 08:52:41 -0600553 /* Add keypresses to move to and select the second one in the list */
554 prev[0] = CTL_CH('n');
555 prev[1] = '\r';
556 prev[2] = '\0';
557 ut_asserteq(2, console_in_puts(prev));
558
559 ut_assertok(run_command("bootflow menu", 0));
560 ut_assert_nextline("Selected: Armbian");
561 ut_assert_console_end();
562
563 return 0;
564}
565BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glasse64c2952023-01-06 08:52:42 -0600566
Simon Glass91943ff2023-01-17 10:48:15 -0700567/* Check searching for a single bootdev using the hunters */
568static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
569{
570 struct bootstd_priv *std;
571
572 /* get access to the used hunters */
573 ut_assertok(bootstd_get_priv(&std));
574
575 ut_assertok(bootstd_test_drop_bootdev_order(uts));
576
577 console_record_reset_enable();
578 ut_assertok(run_command("bootflow scan -l mmc1", 0));
579 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
580 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
581 ut_assert_console_end();
582
583 /* check that the hunter was used */
584 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
585
586 return 0;
587}
588BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
589
590/* Check searching for a uclass label using the hunters */
591static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
592{
593 struct bootstd_priv *std;
594
595 /* get access to the used hunters */
596 ut_assertok(bootstd_get_priv(&std));
597
598 test_set_skip_delays(true);
599 test_set_eth_enable(false);
600 ut_assertok(bootstd_test_drop_bootdev_order(uts));
601
602 console_record_reset_enable();
603 ut_assertok(run_command("bootflow scan -l mmc", 0));
604
605 /* check that the hunter was used */
606 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
607
608 /* check that we got the mmc1 bootflow */
609 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
610 ut_assert_nextlinen("Seq");
611 ut_assert_nextlinen("---");
612 ut_assert_nextline("Hunting with: simple_bus");
613 ut_assert_nextline("Found 2 extension board(s).");
614 ut_assert_nextline("Hunting with: mmc");
615 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
616 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
617 ut_assert_nextline(
Simon Glass79f66352023-05-10 16:34:46 -0600618 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass91943ff2023-01-17 10:48:15 -0700619 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
620 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
621 ut_assert_console_end();
622
623 return 0;
624}
625BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
626
Simon Glasse64c2952023-01-06 08:52:42 -0600627/**
628 * check_font() - Check that the font size for an item matches expectations
629 *
630 * @uts: Unit test state
631 * @scn: Scene containing the text object
632 * @id: ID of the text object
633 * Returns 0 on success, -ve on failure
634 */
635static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
636 int font_size)
637{
638 struct scene_obj_txt *txt;
639
640 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
641 ut_assertnonnull(txt);
642
643 ut_asserteq(font_size, txt->font_size);
644
645 return 0;
646}
647
648/* Check themes work with a bootflow menu */
649static int bootflow_menu_theme(struct unit_test_state *uts)
650{
651 const int font_size = 30;
652 struct scene *scn;
653 struct expo *exp;
654 ofnode node;
655 int i;
656
657 ut_assertok(prep_mmc4_bootdev(uts));
658
659 ut_assertok(bootflow_menu_new(&exp));
660 node = ofnode_path("/bootstd/theme");
661 ut_assert(ofnode_valid(node));
662 ut_assertok(bootflow_menu_apply_theme(exp, node));
663
664 scn = expo_lookup_scene_id(exp, MAIN);
665 ut_assertnonnull(scn);
666
667 /*
668 * Check that the txt objects have the correct font size from the
669 * device tree node: bootstd/theme
670 *
671 * Check both menu items, since there are two bootflows
672 */
673 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
674 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
675 for (i = 0; i < 2; i++) {
676 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
677 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
678 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
679 }
680
681 expo_destroy(exp);
682
683 return 0;
684}
685BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassd07861c2023-07-12 09:04:38 -0600686
687/**
688 * check_arg() - Check both the normal case and the buffer-overflow case
689 *
690 * @uts: Unit-test state
691 * @expect_ret: Expected return value (i.e. buffer length)
692 * @expect_str: String expected to be returned
693 * @buf: Buffer to use
694 * @from: Original cmdline to update
695 * @arg: Argument to update (e.g. "console")
696 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
697 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
698 * it without any value ("initrd")
699 */
700static int check_arg(struct unit_test_state *uts, int expect_ret,
701 const char *expect_str, char *buf, const char *from,
702 const char *arg, const char *val)
703{
704 /* check for writing outside the reported bounds */
705 buf[expect_ret] = '[';
706 ut_asserteq(expect_ret,
707 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
708 ut_asserteq_str(expect_str, buf);
709 ut_asserteq('[', buf[expect_ret]);
710
711 /* do the test again but with one less byte in the buffer */
712 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
713 val, NULL));
714
715 return 0;
716}
717
718/* Test of bootflow_cmdline_set_arg() */
719static int test_bootflow_cmdline_set(struct unit_test_state *uts)
720{
721 char buf[50];
722 const int size = sizeof(buf);
723
724 /*
725 * note that buffer-overflow tests are immediately each test case, just
726 * top keep the code together
727 */
728
729 /* add an arg that doesn't already exist, starting from empty */
730 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
731 NULL));
732
733 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
734 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
735 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
736
737 /* add an arg that doesn't already exist, starting from non-empty */
738 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
739 BOOTFLOWCL_EMPTY));
740 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
741 ""));
742 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
743 "fred"));
744
745 /* update an arg at the start */
746 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
747 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
748 BOOTFLOWCL_EMPTY));
749 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
750 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
751 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
752 "1234"));
753
754 /* update an arg at the end */
755 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
756 NULL));
757 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
758 BOOTFLOWCL_EMPTY));
759 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
760 ""));
761 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
762 "1"));
763 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
764 "arg", "1234"));
765
766 /* update an arg in the middle */
767 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
768 "mary=abc arg=123 john=2", "arg", NULL));
769 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
770 "mary=abc arg=123 john=2", "arg",
771 BOOTFLOWCL_EMPTY));
772 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
773 "mary=abc arg=123 john=2", "arg", ""));
774 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
775 "mary=abc arg=123 john=2", "arg", "1"));
776 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
777 "mary=abc arg=123 john=2", "arg", "1234"));
778
779 /* handle existing args with quotes */
780 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
781 "mary=\"abc\" arg=123 john", "arg", NULL));
782
783 /* handle existing args with quoted spaces */
784 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
785 "mary=\"abc def\" arg=123 john", "arg", NULL));
786
787 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
788 buf, "mary=\"abc def\" arg=123 john", "def",
789 "4"));
790
791 /* quote at the start */
792 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
793 "mary=\"abc def\" arg=\"123 456\"",
794 "arg", "\"4 5 6", NULL));
795
796 /* quote at the end */
797 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
798 "mary=\"abc def\" arg=\"123 456\"",
799 "arg", "4 5 6\"", NULL));
800
801 /* quote in the middle */
802 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
803 "mary=\"abc def\" arg=\"123 456\"",
804 "arg", "\"4 \"5 6\"", NULL));
805
806 /* handle updating a quoted arg */
807 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
808 "mary=\"abc def\" arg=\"123 456\"", "arg",
809 "4 5 6"));
810
811 /* changing a quoted arg to a non-quoted arg */
812 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
813 "mary=\"abc def\" arg=\"123 456\"", "arg",
814 "789"));
815
816 /* changing a non-quoted arg to a quoted arg */
817 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
818 "mary=\"abc def\" arg=123", "arg", "456 789"));
819
820 /* handling of spaces */
821 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
822 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
823 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
824 "123"));
825 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
826 "arg", "123"));
827 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
828 " john arg=123 mary ", "arg", "123"));
829
830 /* unchanged arg */
831 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
832
833 /* arg which starts with the same name */
834 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
835 "mary=abc johnathon=2 john=1", "john", "3"));
836
837 return 0;
838}
839BOOTSTD_TEST(test_bootflow_cmdline_set, 0);