blob: 0d4e966892efdf0ee87f7d4d3e2e29b9b1a753d1 [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
Jerome Forissier253d5982024-10-16 11:56:26 +0200112 *
113 * However with CONFIG_DSA_SANDBOX=n we have two fewer (dsa-test@0 and
114 * dsa-test@1).
Simon Glass484e4072023-01-17 10:48:14 -0700115 */
Jerome Forissier253d5982024-10-16 11:56:26 +0200116 if (CONFIG_IS_ENABLED(DSA_SANDBOX)) {
117 ut_assertok(run_command("bootflow scan -lH 9", 0));
118 ut_assert_nextline("Scanning for bootflows with label '9'");
119 } else {
120 ut_assertok(run_command("bootflow scan -lH 7", 0));
121 ut_assert_nextline("Scanning for bootflows with label '7'");
122 }
Simon Glass484e4072023-01-17 10:48:14 -0700123 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
124
Simon Glasse4cf1062023-01-17 10:48:13 -0700125 ut_assertok(run_command("bootflow scan -lH 0", 0));
Simon Glass484e4072023-01-17 10:48:14 -0700126 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassb255efc2022-04-24 23:31:24 -0600127 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
128 ut_assert_console_end();
129
130 return 0;
131}
Simon Glassf0425022024-08-22 07:57:54 -0600132BOOTSTD_TEST(bootflow_cmd_label, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
133 UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600134
135/* Check 'bootflow scan/list' commands using all bootdevs */
136static int bootflow_cmd_glob(struct unit_test_state *uts)
137{
138 ut_assertok(bootstd_test_drop_bootdev_order(uts));
139
Simon Glasse4cf1062023-01-17 10:48:13 -0700140 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600141 ut_assert_nextline("Scanning for bootflows in all bootdevs");
142 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
143 ut_assert_nextlinen("---");
144 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
145 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassb71d7f72023-05-10 16:34:46 -0600146 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -0600147 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
148 ut_assert_nextline("No more bootdevs");
149 ut_assert_nextlinen("---");
150 ut_assert_nextline("(1 bootflow, 1 valid)");
151 ut_assert_console_end();
152
153 ut_assertok(run_command("bootflow list", 0));
154 ut_assert_nextline("Showing all bootflows");
155 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
156 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -0600157 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -0600158 ut_assert_nextlinen("---");
159 ut_assert_nextline("(1 bootflow, 1 valid)");
160 ut_assert_console_end();
161
162 return 0;
163}
Simon Glassf0425022024-08-22 07:57:54 -0600164BOOTSTD_TEST(bootflow_cmd_glob, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600165
166/* Check 'bootflow scan -e' */
167static int bootflow_cmd_scan_e(struct unit_test_state *uts)
168{
169 ut_assertok(bootstd_test_drop_bootdev_order(uts));
170
Simon Glasse4cf1062023-01-17 10:48:13 -0700171 ut_assertok(run_command("bootflow scan -aleGH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600172 ut_assert_nextline("Scanning for bootflows in all bootdevs");
173 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
174 ut_assert_nextlinen("---");
175 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600176 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600177 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassd465ad52023-08-24 13:55:39 -0600178 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600179 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassb255efc2022-04-24 23:31:24 -0600180
181 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600182 ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600183 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassd465ad52023-08-24 13:55:39 -0600184 ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600185 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassb71d7f72023-05-10 16:34:46 -0600186 ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +0200187 ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 /EFI/BOOT/"
188 BOOTEFI_NAME);
Simon Glassb255efc2022-04-24 23:31:24 -0600189
190 ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600191 ut_assert_skip_to_line(
192 " 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600193 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassb255efc2022-04-24 23:31:24 -0600194 ut_assert_nextline("No more bootdevs");
195 ut_assert_nextlinen("---");
196 ut_assert_nextline("(64 bootflows, 1 valid)");
197 ut_assert_console_end();
198
199 ut_assertok(run_command("bootflow list", 0));
200 ut_assert_nextline("Showing all bootflows");
201 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
202 ut_assert_nextlinen("---");
Simon Glassd465ad52023-08-24 13:55:39 -0600203 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
204 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
205 ut_assert_skip_to_line(
206 " 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
207 ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glassb255efc2022-04-24 23:31:24 -0600208 ut_assert_nextlinen("---");
209 ut_assert_nextline("(64 bootflows, 1 valid)");
210 ut_assert_console_end();
211
212 return 0;
213}
Simon Glassf0425022024-08-22 07:57:54 -0600214BOOTSTD_TEST(bootflow_cmd_scan_e, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600215
216/* Check 'bootflow info' */
217static int bootflow_cmd_info(struct unit_test_state *uts)
218{
Simon Glassb255efc2022-04-24 23:31:24 -0600219 ut_assertok(run_command("bootdev select 1", 0));
220 ut_assert_console_end();
221 ut_assertok(run_command("bootflow scan", 0));
222 ut_assert_console_end();
223 ut_assertok(run_command("bootflow select 0", 0));
224 ut_assert_console_end();
225 ut_assertok(run_command("bootflow info", 0));
226 ut_assert_nextline("Name: mmc1.bootdev.part_1");
227 ut_assert_nextline("Device: mmc1.bootdev");
228 ut_assert_nextline("Block dev: mmc1.blk");
Simon Glassb71d7f72023-05-10 16:34:46 -0600229 ut_assert_nextline("Method: extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600230 ut_assert_nextline("State: ready");
231 ut_assert_nextline("Partition: 1");
232 ut_assert_nextline("Subdir: (none)");
233 ut_assert_nextline("Filename: /extlinux/extlinux.conf");
234 ut_assert_nextlinen("Buffer: ");
235 ut_assert_nextline("Size: 253 (595 bytes)");
Simon Glass72b7b192023-01-06 08:52:33 -0600236 ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
Simon Glass33927522023-07-12 09:04:34 -0600237 ut_assert_nextline("Cmdline: (none)");
Simon Glass612b9cc2023-01-06 08:52:34 -0600238 ut_assert_nextline("Logo: (none)");
Simon Glass7b8c6342023-01-17 10:47:56 -0700239 ut_assert_nextline("FDT: <NULL>");
Simon Glassb255efc2022-04-24 23:31:24 -0600240 ut_assert_nextline("Error: 0");
241 ut_assert_console_end();
242
243 ut_assertok(run_command("bootflow info -d", 0));
244 ut_assert_nextline("Name: mmc1.bootdev.part_1");
245 ut_assert_skip_to_line("Error: 0");
246 ut_assert_nextline("Contents:");
247 ut_assert_nextline("%s", "");
248 ut_assert_nextline("# extlinux.conf generated by appliance-creator");
249 ut_assert_skip_to_line(" initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
250 ut_assert_console_end();
251
252 return 0;
253}
Simon Glassf0425022024-08-22 07:57:54 -0600254BOOTSTD_TEST(bootflow_cmd_info, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600255
256/* Check 'bootflow scan -b' to boot the first available bootdev */
257static int bootflow_scan_boot(struct unit_test_state *uts)
258{
Simon Glass49ad1d82022-07-30 15:52:16 -0600259 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600260 ut_assertok(run_command("bootflow scan -b", 0));
261 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600262 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600263 ut_assert_nextline("Ignoring unknown command: ui");
264
265 /*
266 * We expect it to get through to boot although sandbox always returns
267 * -EFAULT as it cannot actually boot the kernel
268 */
269 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
270 ut_assert_nextline("Boot failed (err=-14)");
271 ut_assert_console_end();
272
273 return 0;
274}
Simon Glassf0425022024-08-22 07:57:54 -0600275BOOTSTD_TEST(bootflow_scan_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600276
277/* Check iterating through available bootflows */
278static int bootflow_iter(struct unit_test_state *uts)
279{
280 struct bootflow_iter iter;
281 struct bootflow bflow;
282
283 bootstd_clear_glob();
284
285 /* The first device is mmc2.bootdev which has no media */
286 ut_asserteq(-EPROTONOSUPPORT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700287 bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700288 BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600289 ut_asserteq(2, iter.num_methods);
290 ut_asserteq(0, iter.cur_method);
291 ut_asserteq(0, iter.part);
292 ut_asserteq(0, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600293 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600294 ut_asserteq(0, bflow.err);
295
296 /*
Simon Glassc8d37212022-07-30 15:52:34 -0600297 * This shows MEDIA even though there is none, since in
Simon Glassb255efc2022-04-24 23:31:24 -0600298 * bootdev_find_in_blk() we call part_get_info() which returns
299 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
300 * know.
301 */
302 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
303
304 ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
305 ut_asserteq(2, iter.num_methods);
306 ut_asserteq(1, iter.cur_method);
307 ut_asserteq(0, iter.part);
308 ut_asserteq(0, iter.max_part);
309 ut_asserteq_str("efi", iter.method->name);
310 ut_asserteq(0, bflow.err);
311 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
312 bootflow_free(&bflow);
313
314 /* The next device is mmc1.bootdev - at first we use the whole device */
315 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
316 ut_asserteq(2, iter.num_methods);
317 ut_asserteq(0, iter.cur_method);
318 ut_asserteq(0, iter.part);
319 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600320 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600321 ut_asserteq(0, bflow.err);
322 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
323 bootflow_free(&bflow);
324
325 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
326 ut_asserteq(2, iter.num_methods);
327 ut_asserteq(1, iter.cur_method);
328 ut_asserteq(0, iter.part);
329 ut_asserteq(0x1e, iter.max_part);
330 ut_asserteq_str("efi", iter.method->name);
331 ut_asserteq(0, bflow.err);
332 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
333 bootflow_free(&bflow);
334
335 /* Then more to partition 1 where we find something */
336 ut_assertok(bootflow_scan_next(&iter, &bflow));
337 ut_asserteq(2, iter.num_methods);
338 ut_asserteq(0, iter.cur_method);
339 ut_asserteq(1, iter.part);
340 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600341 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600342 ut_asserteq(0, bflow.err);
343 ut_asserteq(BOOTFLOWST_READY, bflow.state);
344 bootflow_free(&bflow);
345
346 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
347 ut_asserteq(2, iter.num_methods);
348 ut_asserteq(1, iter.cur_method);
349 ut_asserteq(1, iter.part);
350 ut_asserteq(0x1e, iter.max_part);
351 ut_asserteq_str("efi", iter.method->name);
352 ut_asserteq(0, bflow.err);
353 ut_asserteq(BOOTFLOWST_FS, bflow.state);
354 bootflow_free(&bflow);
355
Simon Glassf5e2df02023-01-17 10:47:41 -0700356 /* Then more to partition 2 which exists but is not bootable */
Simon Glass64cbc5c2023-01-17 10:47:42 -0700357 ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600358 ut_asserteq(2, iter.num_methods);
359 ut_asserteq(0, iter.cur_method);
360 ut_asserteq(2, iter.part);
361 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600362 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600363 ut_asserteq(0, bflow.err);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700364 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
Simon Glassb255efc2022-04-24 23:31:24 -0600365 bootflow_free(&bflow);
366
367 bootflow_iter_uninit(&iter);
368
369 ut_assert_console_end();
370
371 return 0;
372}
Simon Glass1a92f832024-08-22 07:57:48 -0600373BOOTSTD_TEST(bootflow_iter, UTF_DM | UTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600374
Simon Glass90b643d2022-07-30 15:52:36 -0600375#if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
Simon Glassb255efc2022-04-24 23:31:24 -0600376/* Check using the system bootdev */
377static int bootflow_system(struct unit_test_state *uts)
378{
Simon Glassb9ff6482023-01-17 10:47:23 -0700379 struct udevice *bootstd, *dev;
Simon Glassb255efc2022-04-24 23:31:24 -0600380
AKASHI Takahiroe3e542d2024-01-17 13:39:42 +0900381 if (!IS_ENABLED(CONFIG_EFI_BOOTMGR))
Simon Glassc7599442022-10-20 18:22:49 -0600382 return -EAGAIN;
Simon Glassb9ff6482023-01-17 10:47:23 -0700383 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200384 ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_3efi_mgr),
Simon Glassb9ff6482023-01-17 10:47:23 -0700385 "efi_mgr", 0, ofnode_null(), &dev));
386 ut_assertok(device_probe(dev));
Simon Glass161e1e32022-07-30 15:52:22 -0600387 sandbox_set_fake_efi_mgr_dev(dev, true);
Simon Glassb255efc2022-04-24 23:31:24 -0600388
Simon Glassb255efc2022-04-24 23:31:24 -0600389 /* We should get a single 'bootmgr' method right at the end */
390 bootstd_clear_glob();
Simon Glasse4cf1062023-01-17 10:48:13 -0700391 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glasscc15e142022-07-30 15:52:27 -0600392 ut_assert_skip_to_line(
Simon Glassd465ad52023-08-24 13:55:39 -0600393 " 0 efi_mgr ready (none) 0 <NULL> ");
Simon Glasscc15e142022-07-30 15:52:27 -0600394 ut_assert_skip_to_line("No more bootdevs");
Simon Glassb9ff6482023-01-17 10:47:23 -0700395 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
Simon Glassb255efc2022-04-24 23:31:24 -0600396 ut_assert_console_end();
397
398 return 0;
399}
Simon Glassf0425022024-08-22 07:57:54 -0600400BOOTSTD_TEST(bootflow_system, UTF_DM | UTF_SCAN_PDATA | UTF_SCAN_FDT |
401 UTF_CONSOLE);
Simon Glass90b643d2022-07-30 15:52:36 -0600402#endif
Simon Glassb255efc2022-04-24 23:31:24 -0600403
404/* Check disabling a bootmethod if it requests it */
405static int bootflow_iter_disable(struct unit_test_state *uts)
406{
407 struct udevice *bootstd, *dev;
408 struct bootflow_iter iter;
409 struct bootflow bflow;
410 int i;
411
412 /* Add the EFI bootmgr driver */
413 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
414 ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
415 &dev));
416
Simon Glassb255efc2022-04-24 23:31:24 -0600417 ut_assertok(bootstd_test_drop_bootdev_order(uts));
418
419 bootstd_clear_glob();
Simon Glass49ad1d82022-07-30 15:52:16 -0600420 ut_assertok(inject_response(uts));
Simon Glasse4cf1062023-01-17 10:48:13 -0700421 ut_assertok(run_command("bootflow scan -lbH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600422
423 /* Try to boot the bootmgr flow, which will fail */
424 console_record_reset_enable();
Simon Glass5d3d44f2023-01-17 10:48:16 -0700425 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600426 ut_asserteq(3, iter.num_methods);
427 ut_asserteq_str("sandbox", iter.method->name);
Simon Glass49ad1d82022-07-30 15:52:16 -0600428 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600429 ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
430
431 ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
432 ut_assert_console_end();
433
434 /* Check that the sandbox bootmeth has been removed */
435 ut_asserteq(2, iter.num_methods);
436 for (i = 0; i < iter.num_methods; i++)
437 ut_assert(strcmp("sandbox", iter.method_order[i]->name));
438
439 return 0;
440}
Simon Glassf0425022024-08-22 07:57:54 -0600441BOOTSTD_TEST(bootflow_iter_disable, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600442
Simon Glassc8d37212022-07-30 15:52:34 -0600443/* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
444static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
445{
Simon Glass90b643d2022-07-30 15:52:36 -0600446 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc7599442022-10-20 18:22:49 -0600447 return -EAGAIN;
Simon Glass90b643d2022-07-30 15:52:36 -0600448
Simon Glassc8d37212022-07-30 15:52:34 -0600449 ut_assertok(bootstd_test_drop_bootdev_order(uts));
450
451 /*
452 * Make sure that the -G flag makes the scan fail, since this is not
453 * supported when an ordering is provided
454 */
Simon Glassc8d37212022-07-30 15:52:34 -0600455 ut_assertok(bootmeth_set_order("efi firmware0"));
Simon Glasse4cf1062023-01-17 10:48:13 -0700456 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassc8d37212022-07-30 15:52:34 -0600457 ut_assert_nextline("Scanning for bootflows in all bootdevs");
458 ut_assert_nextline(
459 "Seq Method State Uclass Part Name Filename");
460 ut_assert_nextlinen("---");
461 ut_assert_nextlinen("---");
462 ut_assert_nextline("(0 bootflows, 0 valid)");
463 ut_assert_console_end();
464
Simon Glasse4cf1062023-01-17 10:48:13 -0700465 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassc8d37212022-07-30 15:52:34 -0600466 ut_assert_nextline("Scanning for bootflows in all bootdevs");
467 ut_assert_nextline(
468 "Seq Method State Uclass Part Name Filename");
469 ut_assert_nextlinen("---");
470 ut_assert_nextline("Scanning global bootmeth 'firmware0':");
471 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
472 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
473 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
474 ut_assert_nextline("No more bootdevs");
475 ut_assert_nextlinen("---");
476 ut_assert_nextline("(0 bootflows, 0 valid)");
477 ut_assert_console_end();
478
479 return 0;
480}
Simon Glassf0425022024-08-22 07:57:54 -0600481BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UTF_DM | UTF_SCAN_FDT |
482 UTF_CONSOLE);
Simon Glassc8d37212022-07-30 15:52:34 -0600483
Simon Glassb255efc2022-04-24 23:31:24 -0600484/* Check 'bootflow boot' to boot a selected bootflow */
485static int bootflow_cmd_boot(struct unit_test_state *uts)
486{
Simon Glassb255efc2022-04-24 23:31:24 -0600487 ut_assertok(run_command("bootdev select 1", 0));
488 ut_assert_console_end();
489 ut_assertok(run_command("bootflow scan", 0));
490 ut_assert_console_end();
491 ut_assertok(run_command("bootflow select 0", 0));
492 ut_assert_console_end();
Simon Glass49ad1d82022-07-30 15:52:16 -0600493
494 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600495 ut_asserteq(1, run_command("bootflow boot", 0));
496 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600497 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600498 ut_assert_nextline("Ignoring unknown command: ui");
499
500 /*
501 * We expect it to get through to boot although sandbox always returns
502 * -EFAULT as it cannot actually boot the kernel
503 */
504 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
505 ut_assert_nextline("Boot failed (err=-14)");
506 ut_assert_console_end();
507
508 return 0;
509}
Simon Glassf0425022024-08-22 07:57:54 -0600510BOOTSTD_TEST(bootflow_cmd_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600511
Simon Glassd92bcc42023-01-06 08:52:42 -0600512/**
Simon Glassbb76a5c2023-08-24 13:55:40 -0600513 * prep_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
Simon Glassd92bcc42023-01-06 08:52:42 -0600514 *
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600515 * After calling this function, set std->bootdev_order to *@old_orderp to
516 * restore normal operation of bootstd (i.e. with the original bootdev order)
517 *
Simon Glassd92bcc42023-01-06 08:52:42 -0600518 * @uts: Unit test state
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600519 * @mmc_dev: MMC device to use, e.g. "mmc4". Note that this must remain valid
520 * in the caller until
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200521 * @bind_cros: true to bind the ChromiumOS and Android bootmeths
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600522 * @old_orderp: Returns the original bootdev order, which must be restored
Simon Glassd92bcc42023-01-06 08:52:42 -0600523 * Returns 0 on success, -ve on failure
524 */
Simon Glassfff928c2023-08-24 13:55:41 -0600525static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200526 bool bind_cros_android, const char ***old_orderp)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600527{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600528 static const char *order[] = {"mmc2", "mmc1", NULL, NULL};
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600529 struct udevice *dev, *bootstd;
530 struct bootstd_priv *std;
531 const char **old_order;
Simon Glassbb76a5c2023-08-24 13:55:40 -0600532 ofnode root, node;
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600533
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600534 order[2] = mmc_dev;
535
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600536 /* Enable the mmc4 node since we need a second bootflow */
Simon Glassbb76a5c2023-08-24 13:55:40 -0600537 root = oftree_root(oftree_default());
538 node = ofnode_find_subnode(root, mmc_dev);
539 ut_assert(ofnode_valid(node));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600540 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
541
542 /* Enable the script bootmeth too */
543 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Simon Glass2f27e472023-08-19 16:49:35 -0600544 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_2script),
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600545 "bootmeth_script", 0, ofnode_null(), &dev));
546
Simon Glassfff928c2023-08-24 13:55:41 -0600547 /* Enable the cros bootmeth if needed */
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200548 if (IS_ENABLED(CONFIG_BOOTMETH_CROS) && bind_cros_android) {
Simon Glassfff928c2023-08-24 13:55:41 -0600549 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
550 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_cros),
551 "cros", 0, ofnode_null(), &dev));
552 }
553
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200554 /* Enable the android bootmeths if needed */
555 if (IS_ENABLED(CONFIG_BOOTMETH_ANDROID) && bind_cros_android) {
556 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
557 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_android),
558 "android", 0, ofnode_null(), &dev));
559 }
560
Simon Glassbb76a5c2023-08-24 13:55:40 -0600561 /* Change the order to include the device */
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600562 std = dev_get_priv(bootstd);
563 old_order = std->bootdev_order;
564 std->bootdev_order = order;
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600565 *old_orderp = old_order;
566
567 return 0;
568}
569
570/**
571 * scan_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
572 *
573 * @uts: Unit test state
574 * @mmc_dev: MMC device to use, e.g. "mmc4"
575 * @bind_cros: true to bind the ChromiumOS bootmeth
576 * Returns 0 on success, -ve on failure
577 */
578static int scan_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
579 bool bind_cros)
580{
581 struct bootstd_priv *std;
582 struct udevice *bootstd;
583 const char **old_order;
584
585 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, bind_cros, &old_order));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600586
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600587 ut_assertok(run_command("bootflow scan", 0));
588 ut_assert_console_end();
589
590 /* Restore the order used by the device tree */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600591 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
592 std = dev_get_priv(bootstd);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600593 std->bootdev_order = old_order;
594
Simon Glassd92bcc42023-01-06 08:52:42 -0600595 return 0;
596}
597
Simon Glassbb76a5c2023-08-24 13:55:40 -0600598/**
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200599 * scan_mmc_android_bootdev() - Set up an mmc bootdev so we can access other
600 * distros. Android bootflow might print "ANDROID:*" while scanning
601 *
602 * @uts: Unit test state
603 * @mmc_dev: MMC device to use, e.g. "mmc4"
604 * Returns 0 on success, -ve on failure
605 */
606static int scan_mmc_android_bootdev(struct unit_test_state *uts, const char *mmc_dev)
607{
608 struct bootstd_priv *std;
609 struct udevice *bootstd;
610 const char **old_order;
611
612 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, true, &old_order));
613
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200614 ut_assertok(run_command("bootflow scan", 0));
615 /* Android bootflow might print one or two 'ANDROID:*' logs */
616 ut_check_skipline(uts);
617 ut_check_skipline(uts);
618 ut_assert_console_end();
619
620 /* Restore the order used by the device tree */
621 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
622 std = dev_get_priv(bootstd);
623 std->bootdev_order = old_order;
624
625 return 0;
626}
627
628/**
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600629 * scan_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
Simon Glassbb76a5c2023-08-24 13:55:40 -0600630 *
631 * @uts: Unit test state
632 * Returns 0 on success, -ve on failure
633 */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600634static int scan_mmc4_bootdev(struct unit_test_state *uts)
Simon Glassbb76a5c2023-08-24 13:55:40 -0600635{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600636 ut_assertok(scan_mmc_bootdev(uts, "mmc4", false));
Simon Glassbb76a5c2023-08-24 13:55:40 -0600637
638 return 0;
639}
640
Simon Glassd92bcc42023-01-06 08:52:42 -0600641/* Check 'bootflow menu' to select a bootflow */
642static int bootflow_cmd_menu(struct unit_test_state *uts)
643{
Simon Glass6d5083b2023-10-01 19:14:38 -0600644 struct bootstd_priv *std;
Simon Glassd92bcc42023-01-06 08:52:42 -0600645 char prev[3];
646
Simon Glass6d5083b2023-10-01 19:14:38 -0600647 /* get access to the current bootflow */
648 ut_assertok(bootstd_get_priv(&std));
649
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600650 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600651
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600652 /* Add keypresses to move to and select the second one in the list */
653 prev[0] = CTL_CH('n');
654 prev[1] = '\r';
655 prev[2] = '\0';
656 ut_asserteq(2, console_in_puts(prev));
657
658 ut_assertok(run_command("bootflow menu", 0));
659 ut_assert_nextline("Selected: Armbian");
Simon Glass6d5083b2023-10-01 19:14:38 -0600660 ut_assertnonnull(std->cur_bootflow);
661 ut_assert_console_end();
662
663 /* Check not selecting anything */
664 prev[0] = '\e';
665 prev[1] = '\0';
666 ut_asserteq(1, console_in_puts(prev));
667
668 ut_asserteq(1, run_command("bootflow menu", 0));
669 ut_assertnull(std->cur_bootflow);
670 ut_assert_nextline("Nothing chosen");
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600671 ut_assert_console_end();
672
673 return 0;
674}
Simon Glassf0425022024-08-22 07:57:54 -0600675BOOTSTD_TEST(bootflow_cmd_menu, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassd92bcc42023-01-06 08:52:42 -0600676
Simon Glass9bf27862023-10-01 19:15:25 -0600677/* Check 'bootflow scan -m' to select a bootflow using a menu */
678static int bootflow_scan_menu(struct unit_test_state *uts)
679{
680 struct bootstd_priv *std;
681 const char **old_order, **new_order;
682 char prev[3];
683
684 /* get access to the current bootflow */
685 ut_assertok(bootstd_get_priv(&std));
686
687 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
688
689 /* Add keypresses to move to and select the second one in the list */
690 prev[0] = CTL_CH('n');
691 prev[1] = '\r';
692 prev[2] = '\0';
693 ut_asserteq(2, console_in_puts(prev));
694
695 ut_assertok(run_command("bootflow scan -lm", 0));
696 new_order = std->bootdev_order;
697 std->bootdev_order = old_order;
698
699 ut_assert_skip_to_line("No more bootdevs");
700 ut_assert_nextlinen("--");
701 ut_assert_nextline("(2 bootflows, 2 valid)");
702
703 ut_assert_nextline("Selected: Armbian");
704 ut_assertnonnull(std->cur_bootflow);
705 ut_assert_console_end();
706
707 /* Check not selecting anything */
708 prev[0] = '\e';
709 prev[1] = '\0';
710 ut_asserteq(1, console_in_puts(prev));
711
712 std->bootdev_order = new_order; /* Blue Monday */
713 ut_assertok(run_command("bootflow scan -lm", 0));
714 std->bootdev_order = old_order;
715
716 ut_assertnull(std->cur_bootflow);
717 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
718 ut_assert_nextline("Nothing chosen");
719 ut_assert_console_end();
720
721 return 0;
722}
Simon Glass11fcfa32024-08-22 07:57:50 -0600723BOOTSTD_TEST(bootflow_scan_menu, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass9bf27862023-10-01 19:15:25 -0600724
725/* Check 'bootflow scan -mb' to select and boot a bootflow using a menu */
726static int bootflow_scan_menu_boot(struct unit_test_state *uts)
727{
728 struct bootstd_priv *std;
729 const char **old_order;
730 char prev[3];
731
732 /* get access to the current bootflow */
733 ut_assertok(bootstd_get_priv(&std));
734
735 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
736
737 /* Add keypresses to move to and select the second one in the list */
738 prev[0] = CTL_CH('n');
739 prev[1] = '\r';
740 prev[2] = '\0';
741 ut_asserteq(2, console_in_puts(prev));
742
743 ut_assertok(run_command("bootflow scan -lmb", 0));
744 std->bootdev_order = old_order;
745
746 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
747
748 ut_assert_nextline("Selected: Armbian");
Francis Laniel5b64c452023-12-22 22:02:35 +0100749
750 if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
751 /*
752 * With old hush, despite booti failing to boot, i.e. returning
753 * CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as
754 * we are using bootmeth_script here, to return -EFAULT.
755 */
756 ut_assert_skip_to_line("Boot failed (err=-14)");
757 } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
758 /*
759 * While with modern one, run_command() propagates CMD_RET_FAILURE returned
760 * by booti, so we get 1 here.
761 */
762 ut_assert_skip_to_line("Boot failed (err=1)");
763 }
Simon Glass9bf27862023-10-01 19:15:25 -0600764 ut_assertnonnull(std->cur_bootflow);
765 ut_assert_console_end();
766
767 return 0;
768}
Simon Glass11fcfa32024-08-22 07:57:50 -0600769BOOTSTD_TEST(bootflow_scan_menu_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass9bf27862023-10-01 19:15:25 -0600770
Simon Glassba3d5372023-01-17 10:48:15 -0700771/* Check searching for a single bootdev using the hunters */
772static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
773{
774 struct bootstd_priv *std;
775
776 /* get access to the used hunters */
777 ut_assertok(bootstd_get_priv(&std));
778
779 ut_assertok(bootstd_test_drop_bootdev_order(uts));
780
Simon Glassba3d5372023-01-17 10:48:15 -0700781 ut_assertok(run_command("bootflow scan -l mmc1", 0));
782 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
783 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
784 ut_assert_console_end();
785
786 /* check that the hunter was used */
787 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
788
789 return 0;
790}
Simon Glassf0425022024-08-22 07:57:54 -0600791BOOTSTD_TEST(bootflow_cmd_hunt_single, UTF_DM | UTF_SCAN_FDT |
792 UTF_CONSOLE);
Simon Glassba3d5372023-01-17 10:48:15 -0700793
794/* Check searching for a uclass label using the hunters */
795static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
796{
797 struct bootstd_priv *std;
798
799 /* get access to the used hunters */
800 ut_assertok(bootstd_get_priv(&std));
801
802 test_set_skip_delays(true);
803 test_set_eth_enable(false);
804 ut_assertok(bootstd_test_drop_bootdev_order(uts));
805
Simon Glassba3d5372023-01-17 10:48:15 -0700806 ut_assertok(run_command("bootflow scan -l mmc", 0));
807
808 /* check that the hunter was used */
809 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
810
811 /* check that we got the mmc1 bootflow */
812 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
813 ut_assert_nextlinen("Seq");
814 ut_assert_nextlinen("---");
815 ut_assert_nextline("Hunting with: simple_bus");
816 ut_assert_nextline("Found 2 extension board(s).");
817 ut_assert_nextline("Hunting with: mmc");
818 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
819 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
820 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600821 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassba3d5372023-01-17 10:48:15 -0700822 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
823 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
824 ut_assert_console_end();
825
826 return 0;
827}
Simon Glassf0425022024-08-22 07:57:54 -0600828BOOTSTD_TEST(bootflow_cmd_hunt_label, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassba3d5372023-01-17 10:48:15 -0700829
Simon Glassd92bcc42023-01-06 08:52:42 -0600830/**
831 * check_font() - Check that the font size for an item matches expectations
832 *
833 * @uts: Unit test state
834 * @scn: Scene containing the text object
835 * @id: ID of the text object
836 * Returns 0 on success, -ve on failure
837 */
838static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
839 int font_size)
840{
841 struct scene_obj_txt *txt;
842
843 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
844 ut_assertnonnull(txt);
845
846 ut_asserteq(font_size, txt->font_size);
847
848 return 0;
849}
850
851/* Check themes work with a bootflow menu */
852static int bootflow_menu_theme(struct unit_test_state *uts)
853{
854 const int font_size = 30;
855 struct scene *scn;
856 struct expo *exp;
857 ofnode node;
858 int i;
859
Marek Vasutabb772a2024-10-20 17:43:12 +0200860 if (!CONFIG_IS_ENABLED(BOOTSTD_MENU))
861 return -EAGAIN;
862
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600863 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600864
865 ut_assertok(bootflow_menu_new(&exp));
866 node = ofnode_path("/bootstd/theme");
867 ut_assert(ofnode_valid(node));
868 ut_assertok(bootflow_menu_apply_theme(exp, node));
869
870 scn = expo_lookup_scene_id(exp, MAIN);
871 ut_assertnonnull(scn);
872
873 /*
874 * Check that the txt objects have the correct font size from the
875 * device tree node: bootstd/theme
876 *
877 * Check both menu items, since there are two bootflows
878 */
879 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
880 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
881 for (i = 0; i < 2; i++) {
882 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
883 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
884 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
885 }
886
887 expo_destroy(exp);
888
889 return 0;
890}
Simon Glassf0425022024-08-22 07:57:54 -0600891BOOTSTD_TEST(bootflow_menu_theme, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassa08adca2023-07-12 09:04:38 -0600892
893/**
894 * check_arg() - Check both the normal case and the buffer-overflow case
895 *
896 * @uts: Unit-test state
897 * @expect_ret: Expected return value (i.e. buffer length)
898 * @expect_str: String expected to be returned
899 * @buf: Buffer to use
900 * @from: Original cmdline to update
901 * @arg: Argument to update (e.g. "console")
902 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
903 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
904 * it without any value ("initrd")
905 */
906static int check_arg(struct unit_test_state *uts, int expect_ret,
907 const char *expect_str, char *buf, const char *from,
908 const char *arg, const char *val)
909{
910 /* check for writing outside the reported bounds */
911 buf[expect_ret] = '[';
912 ut_asserteq(expect_ret,
913 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
914 ut_asserteq_str(expect_str, buf);
915 ut_asserteq('[', buf[expect_ret]);
916
917 /* do the test again but with one less byte in the buffer */
918 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
919 val, NULL));
920
921 return 0;
922}
923
924/* Test of bootflow_cmdline_set_arg() */
925static int test_bootflow_cmdline_set(struct unit_test_state *uts)
926{
927 char buf[50];
928 const int size = sizeof(buf);
929
930 /*
931 * note that buffer-overflow tests are immediately each test case, just
932 * top keep the code together
933 */
934
935 /* add an arg that doesn't already exist, starting from empty */
936 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
937 NULL));
938
939 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
940 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
941 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
942
943 /* add an arg that doesn't already exist, starting from non-empty */
944 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
945 BOOTFLOWCL_EMPTY));
946 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
947 ""));
948 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
949 "fred"));
950
951 /* update an arg at the start */
952 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
953 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
954 BOOTFLOWCL_EMPTY));
955 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
956 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
957 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
958 "1234"));
959
960 /* update an arg at the end */
961 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
962 NULL));
963 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
964 BOOTFLOWCL_EMPTY));
965 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
966 ""));
967 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
968 "1"));
969 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
970 "arg", "1234"));
971
972 /* update an arg in the middle */
973 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
974 "mary=abc arg=123 john=2", "arg", NULL));
975 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
976 "mary=abc arg=123 john=2", "arg",
977 BOOTFLOWCL_EMPTY));
978 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
979 "mary=abc arg=123 john=2", "arg", ""));
980 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
981 "mary=abc arg=123 john=2", "arg", "1"));
982 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
983 "mary=abc arg=123 john=2", "arg", "1234"));
984
985 /* handle existing args with quotes */
986 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
987 "mary=\"abc\" arg=123 john", "arg", NULL));
988
989 /* handle existing args with quoted spaces */
990 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
991 "mary=\"abc def\" arg=123 john", "arg", NULL));
992
993 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
994 buf, "mary=\"abc def\" arg=123 john", "def",
995 "4"));
996
997 /* quote at the start */
998 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
999 "mary=\"abc def\" arg=\"123 456\"",
1000 "arg", "\"4 5 6", NULL));
1001
1002 /* quote at the end */
1003 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1004 "mary=\"abc def\" arg=\"123 456\"",
1005 "arg", "4 5 6\"", NULL));
1006
1007 /* quote in the middle */
1008 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1009 "mary=\"abc def\" arg=\"123 456\"",
1010 "arg", "\"4 \"5 6\"", NULL));
1011
1012 /* handle updating a quoted arg */
1013 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
1014 "mary=\"abc def\" arg=\"123 456\"", "arg",
1015 "4 5 6"));
1016
1017 /* changing a quoted arg to a non-quoted arg */
1018 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
1019 "mary=\"abc def\" arg=\"123 456\"", "arg",
1020 "789"));
1021
1022 /* changing a non-quoted arg to a quoted arg */
1023 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
1024 "mary=\"abc def\" arg=123", "arg", "456 789"));
1025
1026 /* handling of spaces */
1027 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1028 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1029 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
1030 "123"));
1031 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
1032 "arg", "123"));
1033 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
1034 " john arg=123 mary ", "arg", "123"));
1035
1036 /* unchanged arg */
1037 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
1038
1039 /* arg which starts with the same name */
1040 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
1041 "mary=abc johnathon=2 john=1", "john", "3"));
1042
1043 return 0;
1044}
1045BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
Simon Glass55a2da32023-07-12 09:04:39 -06001046
1047/* Test of bootflow_cmdline_set_arg() */
1048static int bootflow_set_arg(struct unit_test_state *uts)
1049{
1050 struct bootflow s_bflow, *bflow = &s_bflow;
1051 ulong mem_start;
1052
1053 ut_assertok(env_set("bootargs", NULL));
1054
1055 mem_start = ut_check_delta(0);
1056
1057 /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
1058 bflow->cmdline = NULL;
1059 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
1060 ut_asserteq_str(bflow->cmdline, "fred=123");
1061
1062 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
1063 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
1064
1065 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
1066 ut_asserteq_str(bflow->cmdline, "fred=123");
1067 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
1068 ut_asserteq_ptr(bflow->cmdline, NULL);
1069
1070 ut_asserteq(0, ut_check_delta(mem_start));
1071
1072 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
1073 ut_asserteq_str("mary=here", env_get("bootargs"));
1074 ut_assertok(env_set("bootargs", NULL));
1075
1076 return 0;
1077}
1078BOOTSTD_TEST(bootflow_set_arg, 0);
1079
1080/* Test of bootflow_cmdline_get_arg() */
1081static int bootflow_cmdline_get(struct unit_test_state *uts)
1082{
1083 int pos;
1084
1085 /* empty string */
1086 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
1087
1088 /* arg with empty value */
1089 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
1090 ut_asserteq(5, pos);
1091
1092 /* arg with a value */
1093 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
1094 ut_asserteq(5, pos);
1095
1096 /* arg with a value */
1097 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
1098 ut_asserteq(12, pos);
1099
1100 /* arg with a value, after quoted arg */
1101 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
1102 ut_asserteq(16, pos);
1103
1104 /* arg in the middle */
1105 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
1106 &pos));
1107 ut_asserteq(15, pos);
1108
1109 /* quoted arg */
1110 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
1111 "fred", &pos));
1112 ut_asserteq(17, pos);
1113
1114 /* args starting with the same prefix */
1115 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
1116 &pos));
1117 ut_asserteq(26, pos);
1118
1119 return 0;
1120}
1121BOOTSTD_TEST(bootflow_cmdline_get, 0);
1122
1123static int bootflow_cmdline(struct unit_test_state *uts)
1124{
1125 ut_assertok(run_command("bootflow scan mmc", 0));
1126 ut_assertok(run_command("bootflow sel 0", 0));
Simon Glass55a2da32023-07-12 09:04:39 -06001127
1128 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1129 ut_assert_nextline("Argument not found");
1130 ut_assert_console_end();
1131
1132 ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
1133 ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
1134 ut_assert_nextline("123");
1135
1136 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1137 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1138 ut_assert_nextline("abc");
1139
1140 ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
1141 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1142 ut_assert_nextline("Argument not found");
1143
1144 ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
1145 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1146 ut_assert_nextline_empty();
1147
Simon Glass7a164f82023-11-29 10:31:19 -07001148 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1149 ut_asserteq(0, run_command("bootflow cmdline set mary", 0));
Simon Glass55a2da32023-07-12 09:04:39 -06001150 ut_assert_console_end();
1151
1152 return 0;
1153}
Simon Glassf0425022024-08-22 07:57:54 -06001154BOOTSTD_TEST(bootflow_cmdline, UTF_CONSOLE);
Simon Glassfff928c2023-08-24 13:55:41 -06001155
Simon Glassa61057f2023-10-25 07:17:36 +13001156/* test a few special changes to a long command line */
1157static int bootflow_cmdline_special(struct unit_test_state *uts)
1158{
1159 char buf[500];
1160 int pos;
1161
1162 /*
1163 * check handling of an argument which has an embedded '=', as well as
1164 * handling of a argument which partially matches ("ro" and "root")
1165 */
1166 ut_asserteq(32, cmdline_set_arg(
1167 buf, sizeof(buf),
1168 "loglevel=7 root=PARTUUID=d68352e3 rootwait ro noinitrd",
1169 "root", NULL, &pos));
1170 ut_asserteq_str("loglevel=7 rootwait ro noinitrd", buf);
1171
1172 return 0;
1173}
1174BOOTSTD_TEST(bootflow_cmdline_special, 0);
1175
Simon Glassfff928c2023-08-24 13:55:41 -06001176/* Test ChromiumOS bootmeth */
1177static int bootflow_cros(struct unit_test_state *uts)
1178{
Simon Glass3ff8a9a2023-10-01 19:14:37 -06001179 ut_assertok(scan_mmc_bootdev(uts, "mmc5", true));
Simon Glassfff928c2023-08-24 13:55:41 -06001180 ut_assertok(run_command("bootflow list", 0));
1181
1182 ut_assert_nextlinen("Showing all");
1183 ut_assert_nextlinen("Seq");
1184 ut_assert_nextlinen("---");
1185 ut_assert_nextlinen(" 0 extlinux");
Simon Glassd7d3a972023-08-24 13:55:45 -06001186 ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 ");
1187 ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 ");
Simon Glassfff928c2023-08-24 13:55:41 -06001188 ut_assert_nextlinen("---");
Simon Glassd7d3a972023-08-24 13:55:45 -06001189 ut_assert_skip_to_line("(3 bootflows, 3 valid)");
Simon Glassfff928c2023-08-24 13:55:41 -06001190
1191 ut_assert_console_end();
1192
1193 return 0;
1194}
Simon Glassf0425022024-08-22 07:57:54 -06001195BOOTSTD_TEST(bootflow_cros, UTF_CONSOLE);
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +02001196
1197/* Test Android bootmeth */
1198static int bootflow_android(struct unit_test_state *uts)
1199{
1200 if (!IS_ENABLED(CONFIG_BOOTMETH_ANDROID))
1201 return -EAGAIN;
1202
1203 ut_assertok(scan_mmc_android_bootdev(uts, "mmc7"));
1204 ut_assertok(run_command("bootflow list", 0));
1205
1206 ut_assert_nextlinen("Showing all");
1207 ut_assert_nextlinen("Seq");
1208 ut_assert_nextlinen("---");
1209 ut_assert_nextlinen(" 0 extlinux");
1210 ut_assert_nextlinen(" 1 android ready mmc 0 mmc7.bootdev.whole ");
1211 ut_assert_nextlinen("---");
1212 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
1213
1214 ut_assert_console_end();
1215
1216 return 0;
1217}
Simon Glassf0425022024-08-22 07:57:54 -06001218BOOTSTD_TEST(bootflow_android, UTF_CONSOLE);