blob: 6ad63afe90a2c8cdc84871f083f6a3a223babb50 [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
Simon Glassb255efc2022-04-24 23:31:24 -06009#include <bootdev.h>
10#include <bootflow.h>
Simon Glassc8d37212022-07-30 15:52:34 -060011#include <bootmeth.h>
Simon Glassb255efc2022-04-24 23:31:24 -060012#include <bootstd.h>
Simon Glassd2bc33ed2023-01-06 08:52:41 -060013#include <cli.h>
Simon Glassb255efc2022-04-24 23:31:24 -060014#include <dm.h>
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +020015#include <efi_default_filename.h>
Simon Glassd92bcc42023-01-06 08:52:42 -060016#include <expo.h>
Simon Glass90b643d2022-07-30 15:52:36 -060017#ifdef CONFIG_SANDBOX
Simon Glass161e1e32022-07-30 15:52:22 -060018#include <asm/test.h>
Simon Glass90b643d2022-07-30 15:52:36 -060019#endif
Simon Glassd2bc33ed2023-01-06 08:52:41 -060020#include <dm/device-internal.h>
Simon Glassb255efc2022-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 Glassd92bcc42023-01-06 08:52:42 -060025#include "../../boot/bootflow_internal.h"
26#include "../../boot/scene_internal.h"
Simon Glassb255efc2022-04-24 23:31:24 -060027
Simon Glassd2bc33ed2023-01-06 08:52:41 -060028DECLARE_GLOBAL_DATA_PTR;
29
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +020030extern U_BOOT_DRIVER(bootmeth_android);
Simon Glassfff928c2023-08-24 13:55:41 -060031extern U_BOOT_DRIVER(bootmeth_cros);
Simon Glass2f27e472023-08-19 16:49:35 -060032extern U_BOOT_DRIVER(bootmeth_2script);
Simon Glassd2bc33ed2023-01-06 08:52:41 -060033
Simon Glass49ad1d82022-07-30 15:52:16 -060034static int inject_response(struct unit_test_state *uts)
35{
36 /*
37 * The image being booted presents a menu of options:
38 *
39 * Fedora-Workstation-armhfp-31-1.9 Boot Options.
40 * 1: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
41 * Enter choice:
42 *
43 * Provide input for this, to avoid waiting two seconds for a timeout.
44 */
45 ut_asserteq(2, console_in_puts("1\n"));
46
47 return 0;
48}
49
Simon Glassb255efc2022-04-24 23:31:24 -060050/* Check 'bootflow scan/list' commands */
51static int bootflow_cmd(struct unit_test_state *uts)
52{
Simon Glassb255efc2022-04-24 23:31:24 -060053 ut_assertok(run_command("bootdev select 1", 0));
54 ut_assert_console_end();
Simon Glasse4cf1062023-01-17 10:48:13 -070055 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060056 ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
57 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
58 ut_assert_nextlinen("---");
Simon Glass484e4072023-01-17 10:48:14 -070059 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
60 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassb71d7f72023-05-10 16:34:46 -060061 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass484e4072023-01-17 10:48:14 -070062 ut_assert_nextline("No more bootdevs");
Simon Glassb255efc2022-04-24 23:31:24 -060063 ut_assert_nextlinen("---");
64 ut_assert_nextline("(1 bootflow, 1 valid)");
65 ut_assert_console_end();
66
67 ut_assertok(run_command("bootflow list", 0));
68 ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'");
69 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
70 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060071 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -060072 ut_assert_nextlinen("---");
73 ut_assert_nextline("(1 bootflow, 1 valid)");
74 ut_assert_console_end();
75
76 return 0;
77}
Simon Glassf0425022024-08-22 07:57:54 -060078BOOTSTD_TEST(bootflow_cmd, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -060079
Simon Glass484e4072023-01-17 10:48:14 -070080/* Check 'bootflow scan' with a label / seq */
Simon Glassb255efc2022-04-24 23:31:24 -060081static int bootflow_cmd_label(struct unit_test_state *uts)
82{
Simon Glass484e4072023-01-17 10:48:14 -070083 test_set_eth_enable(false);
84
Simon Glasse4cf1062023-01-17 10:48:13 -070085 ut_assertok(run_command("bootflow scan -lH mmc1", 0));
Simon Glass484e4072023-01-17 10:48:14 -070086 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
Simon Glassb255efc2022-04-24 23:31:24 -060087 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
88 ut_assert_console_end();
89
Simon Glass484e4072023-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 Glassb255efc2022-04-24 23:31:24 -060092 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
93 ut_assert_console_end();
94
Simon Glass484e4072023-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 Glasse4cf1062023-01-17 10:48:13 -0700117 ut_assertok(run_command("bootflow scan -lH 0", 0));
Simon Glass484e4072023-01-17 10:48:14 -0700118 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassb255efc2022-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 Glassf0425022024-08-22 07:57:54 -0600124BOOTSTD_TEST(bootflow_cmd_label, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
125 UTF_CONSOLE);
Simon Glassb255efc2022-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
Simon Glasse4cf1062023-01-17 10:48:13 -0700132 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600133 ut_assert_nextline("Scanning for bootflows in all bootdevs");
134 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
135 ut_assert_nextlinen("---");
136 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
137 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassb71d7f72023-05-10 16:34:46 -0600138 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -0600139 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
140 ut_assert_nextline("No more bootdevs");
141 ut_assert_nextlinen("---");
142 ut_assert_nextline("(1 bootflow, 1 valid)");
143 ut_assert_console_end();
144
145 ut_assertok(run_command("bootflow list", 0));
146 ut_assert_nextline("Showing all bootflows");
147 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
148 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -0600149 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -0600150 ut_assert_nextlinen("---");
151 ut_assert_nextline("(1 bootflow, 1 valid)");
152 ut_assert_console_end();
153
154 return 0;
155}
Simon Glassf0425022024-08-22 07:57:54 -0600156BOOTSTD_TEST(bootflow_cmd_glob, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600157
158/* Check 'bootflow scan -e' */
159static int bootflow_cmd_scan_e(struct unit_test_state *uts)
160{
161 ut_assertok(bootstd_test_drop_bootdev_order(uts));
162
Simon Glasse4cf1062023-01-17 10:48:13 -0700163 ut_assertok(run_command("bootflow scan -aleGH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600164 ut_assert_nextline("Scanning for bootflows in all bootdevs");
165 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
166 ut_assert_nextlinen("---");
167 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600168 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600169 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassd465ad52023-08-24 13:55:39 -0600170 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600171 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassb255efc2022-04-24 23:31:24 -0600172
173 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600174 ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600175 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassd465ad52023-08-24 13:55:39 -0600176 ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600177 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassb71d7f72023-05-10 16:34:46 -0600178 ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +0200179 ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 /EFI/BOOT/"
180 BOOTEFI_NAME);
Simon Glassb255efc2022-04-24 23:31:24 -0600181
182 ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600183 ut_assert_skip_to_line(
184 " 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600185 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassb255efc2022-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 Glassd465ad52023-08-24 13:55:39 -0600195 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
196 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
197 ut_assert_skip_to_line(
198 " 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
199 ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glassb255efc2022-04-24 23:31:24 -0600200 ut_assert_nextlinen("---");
201 ut_assert_nextline("(64 bootflows, 1 valid)");
202 ut_assert_console_end();
203
204 return 0;
205}
Simon Glassf0425022024-08-22 07:57:54 -0600206BOOTSTD_TEST(bootflow_cmd_scan_e, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600207
208/* Check 'bootflow info' */
209static int bootflow_cmd_info(struct unit_test_state *uts)
210{
Simon Glassb255efc2022-04-24 23:31:24 -0600211 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 Glassb71d7f72023-05-10 16:34:46 -0600221 ut_assert_nextline("Method: extlinux");
Simon Glassb255efc2022-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 Glass72b7b192023-01-06 08:52:33 -0600228 ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
Simon Glass33927522023-07-12 09:04:34 -0600229 ut_assert_nextline("Cmdline: (none)");
Simon Glass612b9cc2023-01-06 08:52:34 -0600230 ut_assert_nextline("Logo: (none)");
Simon Glass7b8c6342023-01-17 10:47:56 -0700231 ut_assert_nextline("FDT: <NULL>");
Simon Glassb255efc2022-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}
Simon Glassf0425022024-08-22 07:57:54 -0600246BOOTSTD_TEST(bootflow_cmd_info, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600247
248/* Check 'bootflow scan -b' to boot the first available bootdev */
249static int bootflow_scan_boot(struct unit_test_state *uts)
250{
Simon Glass49ad1d82022-07-30 15:52:16 -0600251 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600252 ut_assertok(run_command("bootflow scan -b", 0));
253 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600254 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600255 ut_assert_nextline("Ignoring unknown command: ui");
256
257 /*
258 * We expect it to get through to boot although sandbox always returns
259 * -EFAULT as it cannot actually boot the kernel
260 */
261 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
262 ut_assert_nextline("Boot failed (err=-14)");
263 ut_assert_console_end();
264
265 return 0;
266}
Simon Glassf0425022024-08-22 07:57:54 -0600267BOOTSTD_TEST(bootflow_scan_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600268
269/* Check iterating through available bootflows */
270static int bootflow_iter(struct unit_test_state *uts)
271{
272 struct bootflow_iter iter;
273 struct bootflow bflow;
274
275 bootstd_clear_glob();
276
277 /* The first device is mmc2.bootdev which has no media */
278 ut_asserteq(-EPROTONOSUPPORT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700279 bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700280 BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600281 ut_asserteq(2, iter.num_methods);
282 ut_asserteq(0, iter.cur_method);
283 ut_asserteq(0, iter.part);
284 ut_asserteq(0, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600285 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600286 ut_asserteq(0, bflow.err);
287
288 /*
Simon Glassc8d37212022-07-30 15:52:34 -0600289 * This shows MEDIA even though there is none, since in
Simon Glassb255efc2022-04-24 23:31:24 -0600290 * bootdev_find_in_blk() we call part_get_info() which returns
291 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
292 * know.
293 */
294 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
295
296 ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
297 ut_asserteq(2, iter.num_methods);
298 ut_asserteq(1, iter.cur_method);
299 ut_asserteq(0, iter.part);
300 ut_asserteq(0, iter.max_part);
301 ut_asserteq_str("efi", iter.method->name);
302 ut_asserteq(0, bflow.err);
303 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
304 bootflow_free(&bflow);
305
306 /* The next device is mmc1.bootdev - at first we use the whole device */
307 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
308 ut_asserteq(2, iter.num_methods);
309 ut_asserteq(0, iter.cur_method);
310 ut_asserteq(0, iter.part);
311 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600312 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600313 ut_asserteq(0, bflow.err);
314 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
315 bootflow_free(&bflow);
316
317 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
318 ut_asserteq(2, iter.num_methods);
319 ut_asserteq(1, iter.cur_method);
320 ut_asserteq(0, iter.part);
321 ut_asserteq(0x1e, iter.max_part);
322 ut_asserteq_str("efi", iter.method->name);
323 ut_asserteq(0, bflow.err);
324 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
325 bootflow_free(&bflow);
326
327 /* Then more to partition 1 where we find something */
328 ut_assertok(bootflow_scan_next(&iter, &bflow));
329 ut_asserteq(2, iter.num_methods);
330 ut_asserteq(0, iter.cur_method);
331 ut_asserteq(1, iter.part);
332 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600333 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600334 ut_asserteq(0, bflow.err);
335 ut_asserteq(BOOTFLOWST_READY, bflow.state);
336 bootflow_free(&bflow);
337
338 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
339 ut_asserteq(2, iter.num_methods);
340 ut_asserteq(1, iter.cur_method);
341 ut_asserteq(1, iter.part);
342 ut_asserteq(0x1e, iter.max_part);
343 ut_asserteq_str("efi", iter.method->name);
344 ut_asserteq(0, bflow.err);
345 ut_asserteq(BOOTFLOWST_FS, bflow.state);
346 bootflow_free(&bflow);
347
Simon Glassf5e2df02023-01-17 10:47:41 -0700348 /* Then more to partition 2 which exists but is not bootable */
Simon Glass64cbc5c2023-01-17 10:47:42 -0700349 ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600350 ut_asserteq(2, iter.num_methods);
351 ut_asserteq(0, iter.cur_method);
352 ut_asserteq(2, iter.part);
353 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600354 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600355 ut_asserteq(0, bflow.err);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700356 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
Simon Glassb255efc2022-04-24 23:31:24 -0600357 bootflow_free(&bflow);
358
359 bootflow_iter_uninit(&iter);
360
361 ut_assert_console_end();
362
363 return 0;
364}
Simon Glass1a92f832024-08-22 07:57:48 -0600365BOOTSTD_TEST(bootflow_iter, UTF_DM | UTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600366
Simon Glass90b643d2022-07-30 15:52:36 -0600367#if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
Simon Glassb255efc2022-04-24 23:31:24 -0600368/* Check using the system bootdev */
369static int bootflow_system(struct unit_test_state *uts)
370{
Simon Glassb9ff6482023-01-17 10:47:23 -0700371 struct udevice *bootstd, *dev;
Simon Glassb255efc2022-04-24 23:31:24 -0600372
AKASHI Takahiroe3e542d2024-01-17 13:39:42 +0900373 if (!IS_ENABLED(CONFIG_EFI_BOOTMGR))
Simon Glassc7599442022-10-20 18:22:49 -0600374 return -EAGAIN;
Simon Glassb9ff6482023-01-17 10:47:23 -0700375 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200376 ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_3efi_mgr),
Simon Glassb9ff6482023-01-17 10:47:23 -0700377 "efi_mgr", 0, ofnode_null(), &dev));
378 ut_assertok(device_probe(dev));
Simon Glass161e1e32022-07-30 15:52:22 -0600379 sandbox_set_fake_efi_mgr_dev(dev, true);
Simon Glassb255efc2022-04-24 23:31:24 -0600380
Simon Glassb255efc2022-04-24 23:31:24 -0600381 /* We should get a single 'bootmgr' method right at the end */
382 bootstd_clear_glob();
Simon Glasse4cf1062023-01-17 10:48:13 -0700383 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glasscc15e142022-07-30 15:52:27 -0600384 ut_assert_skip_to_line(
Simon Glassd465ad52023-08-24 13:55:39 -0600385 " 0 efi_mgr ready (none) 0 <NULL> ");
Simon Glasscc15e142022-07-30 15:52:27 -0600386 ut_assert_skip_to_line("No more bootdevs");
Simon Glassb9ff6482023-01-17 10:47:23 -0700387 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
Simon Glassb255efc2022-04-24 23:31:24 -0600388 ut_assert_console_end();
389
390 return 0;
391}
Simon Glassf0425022024-08-22 07:57:54 -0600392BOOTSTD_TEST(bootflow_system, UTF_DM | UTF_SCAN_PDATA | UTF_SCAN_FDT |
393 UTF_CONSOLE);
Simon Glass90b643d2022-07-30 15:52:36 -0600394#endif
Simon Glassb255efc2022-04-24 23:31:24 -0600395
396/* Check disabling a bootmethod if it requests it */
397static int bootflow_iter_disable(struct unit_test_state *uts)
398{
399 struct udevice *bootstd, *dev;
400 struct bootflow_iter iter;
401 struct bootflow bflow;
402 int i;
403
404 /* Add the EFI bootmgr driver */
405 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
406 ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
407 &dev));
408
Simon Glassb255efc2022-04-24 23:31:24 -0600409 ut_assertok(bootstd_test_drop_bootdev_order(uts));
410
411 bootstd_clear_glob();
Simon Glass49ad1d82022-07-30 15:52:16 -0600412 ut_assertok(inject_response(uts));
Simon Glasse4cf1062023-01-17 10:48:13 -0700413 ut_assertok(run_command("bootflow scan -lbH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600414
415 /* Try to boot the bootmgr flow, which will fail */
416 console_record_reset_enable();
Simon Glass5d3d44f2023-01-17 10:48:16 -0700417 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600418 ut_asserteq(3, iter.num_methods);
419 ut_asserteq_str("sandbox", iter.method->name);
Simon Glass49ad1d82022-07-30 15:52:16 -0600420 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600421 ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
422
423 ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
424 ut_assert_console_end();
425
426 /* Check that the sandbox bootmeth has been removed */
427 ut_asserteq(2, iter.num_methods);
428 for (i = 0; i < iter.num_methods; i++)
429 ut_assert(strcmp("sandbox", iter.method_order[i]->name));
430
431 return 0;
432}
Simon Glassf0425022024-08-22 07:57:54 -0600433BOOTSTD_TEST(bootflow_iter_disable, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600434
Simon Glassc8d37212022-07-30 15:52:34 -0600435/* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
436static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
437{
Simon Glass90b643d2022-07-30 15:52:36 -0600438 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc7599442022-10-20 18:22:49 -0600439 return -EAGAIN;
Simon Glass90b643d2022-07-30 15:52:36 -0600440
Simon Glassc8d37212022-07-30 15:52:34 -0600441 ut_assertok(bootstd_test_drop_bootdev_order(uts));
442
443 /*
444 * Make sure that the -G flag makes the scan fail, since this is not
445 * supported when an ordering is provided
446 */
Simon Glassc8d37212022-07-30 15:52:34 -0600447 ut_assertok(bootmeth_set_order("efi firmware0"));
Simon Glasse4cf1062023-01-17 10:48:13 -0700448 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassc8d37212022-07-30 15:52:34 -0600449 ut_assert_nextline("Scanning for bootflows in all bootdevs");
450 ut_assert_nextline(
451 "Seq Method State Uclass Part Name Filename");
452 ut_assert_nextlinen("---");
453 ut_assert_nextlinen("---");
454 ut_assert_nextline("(0 bootflows, 0 valid)");
455 ut_assert_console_end();
456
Simon Glasse4cf1062023-01-17 10:48:13 -0700457 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassc8d37212022-07-30 15:52:34 -0600458 ut_assert_nextline("Scanning for bootflows in all bootdevs");
459 ut_assert_nextline(
460 "Seq Method State Uclass Part Name Filename");
461 ut_assert_nextlinen("---");
462 ut_assert_nextline("Scanning global bootmeth 'firmware0':");
463 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
464 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
465 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
466 ut_assert_nextline("No more bootdevs");
467 ut_assert_nextlinen("---");
468 ut_assert_nextline("(0 bootflows, 0 valid)");
469 ut_assert_console_end();
470
471 return 0;
472}
Simon Glassf0425022024-08-22 07:57:54 -0600473BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UTF_DM | UTF_SCAN_FDT |
474 UTF_CONSOLE);
Simon Glassc8d37212022-07-30 15:52:34 -0600475
Simon Glassb255efc2022-04-24 23:31:24 -0600476/* Check 'bootflow boot' to boot a selected bootflow */
477static int bootflow_cmd_boot(struct unit_test_state *uts)
478{
Simon Glassb255efc2022-04-24 23:31:24 -0600479 ut_assertok(run_command("bootdev select 1", 0));
480 ut_assert_console_end();
481 ut_assertok(run_command("bootflow scan", 0));
482 ut_assert_console_end();
483 ut_assertok(run_command("bootflow select 0", 0));
484 ut_assert_console_end();
Simon Glass49ad1d82022-07-30 15:52:16 -0600485
486 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600487 ut_asserteq(1, run_command("bootflow boot", 0));
488 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600489 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600490 ut_assert_nextline("Ignoring unknown command: ui");
491
492 /*
493 * We expect it to get through to boot although sandbox always returns
494 * -EFAULT as it cannot actually boot the kernel
495 */
496 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
497 ut_assert_nextline("Boot failed (err=-14)");
498 ut_assert_console_end();
499
500 return 0;
501}
Simon Glassf0425022024-08-22 07:57:54 -0600502BOOTSTD_TEST(bootflow_cmd_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600503
Simon Glassd92bcc42023-01-06 08:52:42 -0600504/**
Simon Glassbb76a5c2023-08-24 13:55:40 -0600505 * prep_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
Simon Glassd92bcc42023-01-06 08:52:42 -0600506 *
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600507 * After calling this function, set std->bootdev_order to *@old_orderp to
508 * restore normal operation of bootstd (i.e. with the original bootdev order)
509 *
Simon Glassd92bcc42023-01-06 08:52:42 -0600510 * @uts: Unit test state
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600511 * @mmc_dev: MMC device to use, e.g. "mmc4". Note that this must remain valid
512 * in the caller until
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200513 * @bind_cros: true to bind the ChromiumOS and Android bootmeths
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600514 * @old_orderp: Returns the original bootdev order, which must be restored
Simon Glassd92bcc42023-01-06 08:52:42 -0600515 * Returns 0 on success, -ve on failure
516 */
Simon Glassfff928c2023-08-24 13:55:41 -0600517static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200518 bool bind_cros_android, const char ***old_orderp)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600519{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600520 static const char *order[] = {"mmc2", "mmc1", NULL, NULL};
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600521 struct udevice *dev, *bootstd;
522 struct bootstd_priv *std;
523 const char **old_order;
Simon Glassbb76a5c2023-08-24 13:55:40 -0600524 ofnode root, node;
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600525
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600526 order[2] = mmc_dev;
527
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600528 /* Enable the mmc4 node since we need a second bootflow */
Simon Glassbb76a5c2023-08-24 13:55:40 -0600529 root = oftree_root(oftree_default());
530 node = ofnode_find_subnode(root, mmc_dev);
531 ut_assert(ofnode_valid(node));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600532 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
533
534 /* Enable the script bootmeth too */
535 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Simon Glass2f27e472023-08-19 16:49:35 -0600536 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_2script),
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600537 "bootmeth_script", 0, ofnode_null(), &dev));
538
Simon Glassfff928c2023-08-24 13:55:41 -0600539 /* Enable the cros bootmeth if needed */
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200540 if (IS_ENABLED(CONFIG_BOOTMETH_CROS) && bind_cros_android) {
Simon Glassfff928c2023-08-24 13:55:41 -0600541 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
542 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_cros),
543 "cros", 0, ofnode_null(), &dev));
544 }
545
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200546 /* Enable the android bootmeths if needed */
547 if (IS_ENABLED(CONFIG_BOOTMETH_ANDROID) && bind_cros_android) {
548 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
549 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_android),
550 "android", 0, ofnode_null(), &dev));
551 }
552
Simon Glassbb76a5c2023-08-24 13:55:40 -0600553 /* Change the order to include the device */
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600554 std = dev_get_priv(bootstd);
555 old_order = std->bootdev_order;
556 std->bootdev_order = order;
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600557 *old_orderp = old_order;
558
559 return 0;
560}
561
562/**
563 * scan_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
564 *
565 * @uts: Unit test state
566 * @mmc_dev: MMC device to use, e.g. "mmc4"
567 * @bind_cros: true to bind the ChromiumOS bootmeth
568 * Returns 0 on success, -ve on failure
569 */
570static int scan_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
571 bool bind_cros)
572{
573 struct bootstd_priv *std;
574 struct udevice *bootstd;
575 const char **old_order;
576
577 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, bind_cros, &old_order));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600578
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600579 ut_assertok(run_command("bootflow scan", 0));
580 ut_assert_console_end();
581
582 /* Restore the order used by the device tree */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600583 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
584 std = dev_get_priv(bootstd);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600585 std->bootdev_order = old_order;
586
Simon Glassd92bcc42023-01-06 08:52:42 -0600587 return 0;
588}
589
Simon Glassbb76a5c2023-08-24 13:55:40 -0600590/**
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200591 * scan_mmc_android_bootdev() - Set up an mmc bootdev so we can access other
592 * distros. Android bootflow might print "ANDROID:*" while scanning
593 *
594 * @uts: Unit test state
595 * @mmc_dev: MMC device to use, e.g. "mmc4"
596 * Returns 0 on success, -ve on failure
597 */
598static int scan_mmc_android_bootdev(struct unit_test_state *uts, const char *mmc_dev)
599{
600 struct bootstd_priv *std;
601 struct udevice *bootstd;
602 const char **old_order;
603
604 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, true, &old_order));
605
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200606 ut_assertok(run_command("bootflow scan", 0));
607 /* Android bootflow might print one or two 'ANDROID:*' logs */
608 ut_check_skipline(uts);
609 ut_check_skipline(uts);
610 ut_assert_console_end();
611
612 /* Restore the order used by the device tree */
613 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
614 std = dev_get_priv(bootstd);
615 std->bootdev_order = old_order;
616
617 return 0;
618}
619
620/**
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600621 * scan_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
Simon Glassbb76a5c2023-08-24 13:55:40 -0600622 *
623 * @uts: Unit test state
624 * Returns 0 on success, -ve on failure
625 */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600626static int scan_mmc4_bootdev(struct unit_test_state *uts)
Simon Glassbb76a5c2023-08-24 13:55:40 -0600627{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600628 ut_assertok(scan_mmc_bootdev(uts, "mmc4", false));
Simon Glassbb76a5c2023-08-24 13:55:40 -0600629
630 return 0;
631}
632
Simon Glassd92bcc42023-01-06 08:52:42 -0600633/* Check 'bootflow menu' to select a bootflow */
634static int bootflow_cmd_menu(struct unit_test_state *uts)
635{
Simon Glass6d5083b2023-10-01 19:14:38 -0600636 struct bootstd_priv *std;
Simon Glassd92bcc42023-01-06 08:52:42 -0600637 char prev[3];
638
Simon Glass6d5083b2023-10-01 19:14:38 -0600639 /* get access to the current bootflow */
640 ut_assertok(bootstd_get_priv(&std));
641
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600642 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600643
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600644 /* Add keypresses to move to and select the second one in the list */
645 prev[0] = CTL_CH('n');
646 prev[1] = '\r';
647 prev[2] = '\0';
648 ut_asserteq(2, console_in_puts(prev));
649
650 ut_assertok(run_command("bootflow menu", 0));
651 ut_assert_nextline("Selected: Armbian");
Simon Glass6d5083b2023-10-01 19:14:38 -0600652 ut_assertnonnull(std->cur_bootflow);
653 ut_assert_console_end();
654
655 /* Check not selecting anything */
656 prev[0] = '\e';
657 prev[1] = '\0';
658 ut_asserteq(1, console_in_puts(prev));
659
660 ut_asserteq(1, run_command("bootflow menu", 0));
661 ut_assertnull(std->cur_bootflow);
662 ut_assert_nextline("Nothing chosen");
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600663 ut_assert_console_end();
664
665 return 0;
666}
Simon Glassf0425022024-08-22 07:57:54 -0600667BOOTSTD_TEST(bootflow_cmd_menu, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassd92bcc42023-01-06 08:52:42 -0600668
Simon Glass9bf27862023-10-01 19:15:25 -0600669/* Check 'bootflow scan -m' to select a bootflow using a menu */
670static int bootflow_scan_menu(struct unit_test_state *uts)
671{
672 struct bootstd_priv *std;
673 const char **old_order, **new_order;
674 char prev[3];
675
676 /* get access to the current bootflow */
677 ut_assertok(bootstd_get_priv(&std));
678
679 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
680
681 /* Add keypresses to move to and select the second one in the list */
682 prev[0] = CTL_CH('n');
683 prev[1] = '\r';
684 prev[2] = '\0';
685 ut_asserteq(2, console_in_puts(prev));
686
687 ut_assertok(run_command("bootflow scan -lm", 0));
688 new_order = std->bootdev_order;
689 std->bootdev_order = old_order;
690
691 ut_assert_skip_to_line("No more bootdevs");
692 ut_assert_nextlinen("--");
693 ut_assert_nextline("(2 bootflows, 2 valid)");
694
695 ut_assert_nextline("Selected: Armbian");
696 ut_assertnonnull(std->cur_bootflow);
697 ut_assert_console_end();
698
699 /* Check not selecting anything */
700 prev[0] = '\e';
701 prev[1] = '\0';
702 ut_asserteq(1, console_in_puts(prev));
703
704 std->bootdev_order = new_order; /* Blue Monday */
705 ut_assertok(run_command("bootflow scan -lm", 0));
706 std->bootdev_order = old_order;
707
708 ut_assertnull(std->cur_bootflow);
709 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
710 ut_assert_nextline("Nothing chosen");
711 ut_assert_console_end();
712
713 return 0;
714}
Simon Glass11fcfa32024-08-22 07:57:50 -0600715BOOTSTD_TEST(bootflow_scan_menu, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass9bf27862023-10-01 19:15:25 -0600716
717/* Check 'bootflow scan -mb' to select and boot a bootflow using a menu */
718static int bootflow_scan_menu_boot(struct unit_test_state *uts)
719{
720 struct bootstd_priv *std;
721 const char **old_order;
722 char prev[3];
723
724 /* get access to the current bootflow */
725 ut_assertok(bootstd_get_priv(&std));
726
727 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
728
729 /* Add keypresses to move to and select the second one in the list */
730 prev[0] = CTL_CH('n');
731 prev[1] = '\r';
732 prev[2] = '\0';
733 ut_asserteq(2, console_in_puts(prev));
734
735 ut_assertok(run_command("bootflow scan -lmb", 0));
736 std->bootdev_order = old_order;
737
738 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
739
740 ut_assert_nextline("Selected: Armbian");
Francis Laniel5b64c452023-12-22 22:02:35 +0100741
742 if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
743 /*
744 * With old hush, despite booti failing to boot, i.e. returning
745 * CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as
746 * we are using bootmeth_script here, to return -EFAULT.
747 */
748 ut_assert_skip_to_line("Boot failed (err=-14)");
749 } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
750 /*
751 * While with modern one, run_command() propagates CMD_RET_FAILURE returned
752 * by booti, so we get 1 here.
753 */
754 ut_assert_skip_to_line("Boot failed (err=1)");
755 }
Simon Glass9bf27862023-10-01 19:15:25 -0600756 ut_assertnonnull(std->cur_bootflow);
757 ut_assert_console_end();
758
759 return 0;
760}
Simon Glass11fcfa32024-08-22 07:57:50 -0600761BOOTSTD_TEST(bootflow_scan_menu_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass9bf27862023-10-01 19:15:25 -0600762
Simon Glassba3d5372023-01-17 10:48:15 -0700763/* Check searching for a single bootdev using the hunters */
764static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
765{
766 struct bootstd_priv *std;
767
768 /* get access to the used hunters */
769 ut_assertok(bootstd_get_priv(&std));
770
771 ut_assertok(bootstd_test_drop_bootdev_order(uts));
772
Simon Glassba3d5372023-01-17 10:48:15 -0700773 ut_assertok(run_command("bootflow scan -l mmc1", 0));
774 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
775 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
776 ut_assert_console_end();
777
778 /* check that the hunter was used */
779 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
780
781 return 0;
782}
Simon Glassf0425022024-08-22 07:57:54 -0600783BOOTSTD_TEST(bootflow_cmd_hunt_single, UTF_DM | UTF_SCAN_FDT |
784 UTF_CONSOLE);
Simon Glassba3d5372023-01-17 10:48:15 -0700785
786/* Check searching for a uclass label using the hunters */
787static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
788{
789 struct bootstd_priv *std;
790
791 /* get access to the used hunters */
792 ut_assertok(bootstd_get_priv(&std));
793
794 test_set_skip_delays(true);
795 test_set_eth_enable(false);
796 ut_assertok(bootstd_test_drop_bootdev_order(uts));
797
Simon Glassba3d5372023-01-17 10:48:15 -0700798 ut_assertok(run_command("bootflow scan -l mmc", 0));
799
800 /* check that the hunter was used */
801 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
802
803 /* check that we got the mmc1 bootflow */
804 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
805 ut_assert_nextlinen("Seq");
806 ut_assert_nextlinen("---");
807 ut_assert_nextline("Hunting with: simple_bus");
808 ut_assert_nextline("Found 2 extension board(s).");
809 ut_assert_nextline("Hunting with: mmc");
810 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
811 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
812 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600813 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassba3d5372023-01-17 10:48:15 -0700814 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
815 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
816 ut_assert_console_end();
817
818 return 0;
819}
Simon Glassf0425022024-08-22 07:57:54 -0600820BOOTSTD_TEST(bootflow_cmd_hunt_label, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassba3d5372023-01-17 10:48:15 -0700821
Simon Glassd92bcc42023-01-06 08:52:42 -0600822/**
823 * check_font() - Check that the font size for an item matches expectations
824 *
825 * @uts: Unit test state
826 * @scn: Scene containing the text object
827 * @id: ID of the text object
828 * Returns 0 on success, -ve on failure
829 */
830static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
831 int font_size)
832{
833 struct scene_obj_txt *txt;
834
835 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
836 ut_assertnonnull(txt);
837
838 ut_asserteq(font_size, txt->font_size);
839
840 return 0;
841}
842
843/* Check themes work with a bootflow menu */
844static int bootflow_menu_theme(struct unit_test_state *uts)
845{
846 const int font_size = 30;
847 struct scene *scn;
848 struct expo *exp;
849 ofnode node;
850 int i;
851
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600852 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600853
854 ut_assertok(bootflow_menu_new(&exp));
855 node = ofnode_path("/bootstd/theme");
856 ut_assert(ofnode_valid(node));
857 ut_assertok(bootflow_menu_apply_theme(exp, node));
858
859 scn = expo_lookup_scene_id(exp, MAIN);
860 ut_assertnonnull(scn);
861
862 /*
863 * Check that the txt objects have the correct font size from the
864 * device tree node: bootstd/theme
865 *
866 * Check both menu items, since there are two bootflows
867 */
868 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
869 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
870 for (i = 0; i < 2; i++) {
871 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
872 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
873 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
874 }
875
876 expo_destroy(exp);
877
878 return 0;
879}
Simon Glassf0425022024-08-22 07:57:54 -0600880BOOTSTD_TEST(bootflow_menu_theme, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassa08adca2023-07-12 09:04:38 -0600881
882/**
883 * check_arg() - Check both the normal case and the buffer-overflow case
884 *
885 * @uts: Unit-test state
886 * @expect_ret: Expected return value (i.e. buffer length)
887 * @expect_str: String expected to be returned
888 * @buf: Buffer to use
889 * @from: Original cmdline to update
890 * @arg: Argument to update (e.g. "console")
891 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
892 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
893 * it without any value ("initrd")
894 */
895static int check_arg(struct unit_test_state *uts, int expect_ret,
896 const char *expect_str, char *buf, const char *from,
897 const char *arg, const char *val)
898{
899 /* check for writing outside the reported bounds */
900 buf[expect_ret] = '[';
901 ut_asserteq(expect_ret,
902 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
903 ut_asserteq_str(expect_str, buf);
904 ut_asserteq('[', buf[expect_ret]);
905
906 /* do the test again but with one less byte in the buffer */
907 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
908 val, NULL));
909
910 return 0;
911}
912
913/* Test of bootflow_cmdline_set_arg() */
914static int test_bootflow_cmdline_set(struct unit_test_state *uts)
915{
916 char buf[50];
917 const int size = sizeof(buf);
918
919 /*
920 * note that buffer-overflow tests are immediately each test case, just
921 * top keep the code together
922 */
923
924 /* add an arg that doesn't already exist, starting from empty */
925 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
926 NULL));
927
928 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
929 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
930 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
931
932 /* add an arg that doesn't already exist, starting from non-empty */
933 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
934 BOOTFLOWCL_EMPTY));
935 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
936 ""));
937 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
938 "fred"));
939
940 /* update an arg at the start */
941 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
942 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
943 BOOTFLOWCL_EMPTY));
944 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
945 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
946 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
947 "1234"));
948
949 /* update an arg at the end */
950 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
951 NULL));
952 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
953 BOOTFLOWCL_EMPTY));
954 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
955 ""));
956 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
957 "1"));
958 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
959 "arg", "1234"));
960
961 /* update an arg in the middle */
962 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
963 "mary=abc arg=123 john=2", "arg", NULL));
964 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
965 "mary=abc arg=123 john=2", "arg",
966 BOOTFLOWCL_EMPTY));
967 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
968 "mary=abc arg=123 john=2", "arg", ""));
969 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
970 "mary=abc arg=123 john=2", "arg", "1"));
971 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
972 "mary=abc arg=123 john=2", "arg", "1234"));
973
974 /* handle existing args with quotes */
975 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
976 "mary=\"abc\" arg=123 john", "arg", NULL));
977
978 /* handle existing args with quoted spaces */
979 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
980 "mary=\"abc def\" arg=123 john", "arg", NULL));
981
982 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
983 buf, "mary=\"abc def\" arg=123 john", "def",
984 "4"));
985
986 /* quote at the start */
987 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
988 "mary=\"abc def\" arg=\"123 456\"",
989 "arg", "\"4 5 6", NULL));
990
991 /* quote at the end */
992 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
993 "mary=\"abc def\" arg=\"123 456\"",
994 "arg", "4 5 6\"", NULL));
995
996 /* quote in the middle */
997 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
998 "mary=\"abc def\" arg=\"123 456\"",
999 "arg", "\"4 \"5 6\"", NULL));
1000
1001 /* handle updating a quoted arg */
1002 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
1003 "mary=\"abc def\" arg=\"123 456\"", "arg",
1004 "4 5 6"));
1005
1006 /* changing a quoted arg to a non-quoted arg */
1007 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
1008 "mary=\"abc def\" arg=\"123 456\"", "arg",
1009 "789"));
1010
1011 /* changing a non-quoted arg to a quoted arg */
1012 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
1013 "mary=\"abc def\" arg=123", "arg", "456 789"));
1014
1015 /* handling of spaces */
1016 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1017 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1018 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
1019 "123"));
1020 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
1021 "arg", "123"));
1022 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
1023 " john arg=123 mary ", "arg", "123"));
1024
1025 /* unchanged arg */
1026 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
1027
1028 /* arg which starts with the same name */
1029 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
1030 "mary=abc johnathon=2 john=1", "john", "3"));
1031
1032 return 0;
1033}
1034BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
Simon Glass55a2da32023-07-12 09:04:39 -06001035
1036/* Test of bootflow_cmdline_set_arg() */
1037static int bootflow_set_arg(struct unit_test_state *uts)
1038{
1039 struct bootflow s_bflow, *bflow = &s_bflow;
1040 ulong mem_start;
1041
1042 ut_assertok(env_set("bootargs", NULL));
1043
1044 mem_start = ut_check_delta(0);
1045
1046 /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
1047 bflow->cmdline = NULL;
1048 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
1049 ut_asserteq_str(bflow->cmdline, "fred=123");
1050
1051 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
1052 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
1053
1054 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
1055 ut_asserteq_str(bflow->cmdline, "fred=123");
1056 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
1057 ut_asserteq_ptr(bflow->cmdline, NULL);
1058
1059 ut_asserteq(0, ut_check_delta(mem_start));
1060
1061 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
1062 ut_asserteq_str("mary=here", env_get("bootargs"));
1063 ut_assertok(env_set("bootargs", NULL));
1064
1065 return 0;
1066}
1067BOOTSTD_TEST(bootflow_set_arg, 0);
1068
1069/* Test of bootflow_cmdline_get_arg() */
1070static int bootflow_cmdline_get(struct unit_test_state *uts)
1071{
1072 int pos;
1073
1074 /* empty string */
1075 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
1076
1077 /* arg with empty value */
1078 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
1079 ut_asserteq(5, pos);
1080
1081 /* arg with a value */
1082 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
1083 ut_asserteq(5, pos);
1084
1085 /* arg with a value */
1086 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
1087 ut_asserteq(12, pos);
1088
1089 /* arg with a value, after quoted arg */
1090 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
1091 ut_asserteq(16, pos);
1092
1093 /* arg in the middle */
1094 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
1095 &pos));
1096 ut_asserteq(15, pos);
1097
1098 /* quoted arg */
1099 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
1100 "fred", &pos));
1101 ut_asserteq(17, pos);
1102
1103 /* args starting with the same prefix */
1104 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
1105 &pos));
1106 ut_asserteq(26, pos);
1107
1108 return 0;
1109}
1110BOOTSTD_TEST(bootflow_cmdline_get, 0);
1111
1112static int bootflow_cmdline(struct unit_test_state *uts)
1113{
1114 ut_assertok(run_command("bootflow scan mmc", 0));
1115 ut_assertok(run_command("bootflow sel 0", 0));
Simon Glass55a2da32023-07-12 09:04:39 -06001116
1117 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1118 ut_assert_nextline("Argument not found");
1119 ut_assert_console_end();
1120
1121 ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
1122 ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
1123 ut_assert_nextline("123");
1124
1125 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1126 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1127 ut_assert_nextline("abc");
1128
1129 ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
1130 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1131 ut_assert_nextline("Argument not found");
1132
1133 ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
1134 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1135 ut_assert_nextline_empty();
1136
Simon Glass7a164f82023-11-29 10:31:19 -07001137 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1138 ut_asserteq(0, run_command("bootflow cmdline set mary", 0));
Simon Glass55a2da32023-07-12 09:04:39 -06001139 ut_assert_console_end();
1140
1141 return 0;
1142}
Simon Glassf0425022024-08-22 07:57:54 -06001143BOOTSTD_TEST(bootflow_cmdline, UTF_CONSOLE);
Simon Glassfff928c2023-08-24 13:55:41 -06001144
Simon Glassa61057f2023-10-25 07:17:36 +13001145/* test a few special changes to a long command line */
1146static int bootflow_cmdline_special(struct unit_test_state *uts)
1147{
1148 char buf[500];
1149 int pos;
1150
1151 /*
1152 * check handling of an argument which has an embedded '=', as well as
1153 * handling of a argument which partially matches ("ro" and "root")
1154 */
1155 ut_asserteq(32, cmdline_set_arg(
1156 buf, sizeof(buf),
1157 "loglevel=7 root=PARTUUID=d68352e3 rootwait ro noinitrd",
1158 "root", NULL, &pos));
1159 ut_asserteq_str("loglevel=7 rootwait ro noinitrd", buf);
1160
1161 return 0;
1162}
1163BOOTSTD_TEST(bootflow_cmdline_special, 0);
1164
Simon Glassfff928c2023-08-24 13:55:41 -06001165/* Test ChromiumOS bootmeth */
1166static int bootflow_cros(struct unit_test_state *uts)
1167{
Simon Glass3ff8a9a2023-10-01 19:14:37 -06001168 ut_assertok(scan_mmc_bootdev(uts, "mmc5", true));
Simon Glassfff928c2023-08-24 13:55:41 -06001169 ut_assertok(run_command("bootflow list", 0));
1170
1171 ut_assert_nextlinen("Showing all");
1172 ut_assert_nextlinen("Seq");
1173 ut_assert_nextlinen("---");
1174 ut_assert_nextlinen(" 0 extlinux");
Simon Glassd7d3a972023-08-24 13:55:45 -06001175 ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 ");
1176 ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 ");
Simon Glassfff928c2023-08-24 13:55:41 -06001177 ut_assert_nextlinen("---");
Simon Glassd7d3a972023-08-24 13:55:45 -06001178 ut_assert_skip_to_line("(3 bootflows, 3 valid)");
Simon Glassfff928c2023-08-24 13:55:41 -06001179
1180 ut_assert_console_end();
1181
1182 return 0;
1183}
Simon Glassf0425022024-08-22 07:57:54 -06001184BOOTSTD_TEST(bootflow_cros, UTF_CONSOLE);
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +02001185
1186/* Test Android bootmeth */
1187static int bootflow_android(struct unit_test_state *uts)
1188{
1189 if (!IS_ENABLED(CONFIG_BOOTMETH_ANDROID))
1190 return -EAGAIN;
1191
1192 ut_assertok(scan_mmc_android_bootdev(uts, "mmc7"));
1193 ut_assertok(run_command("bootflow list", 0));
1194
1195 ut_assert_nextlinen("Showing all");
1196 ut_assert_nextlinen("Seq");
1197 ut_assert_nextlinen("---");
1198 ut_assert_nextlinen(" 0 extlinux");
1199 ut_assert_nextlinen(" 1 android ready mmc 0 mmc7.bootdev.whole ");
1200 ut_assert_nextlinen("---");
1201 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
1202
1203 ut_assert_console_end();
1204
1205 return 0;
1206}
Simon Glassf0425022024-08-22 07:57:54 -06001207BOOTSTD_TEST(bootflow_android, UTF_CONSOLE);