blob: 372bbab6b8db3b7b672e1153dbcde12fb83f9eaa [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>
Simon Glass3b1e60b2024-11-07 14:31:43 -070015#include <efi.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");
Simon Glass3b1e60b2024-11-07 14:31:43 -0700187 ut_assert_nextline(
188 " 5 efi fs mmc 1 mmc1.bootdev.part_1 /EFI/BOOT/%s",
189 efi_get_basename());
Simon Glassb255efc2022-04-24 23:31:24 -0600190
191 ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600192 ut_assert_skip_to_line(
193 " 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600194 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassb255efc2022-04-24 23:31:24 -0600195 ut_assert_nextline("No more bootdevs");
196 ut_assert_nextlinen("---");
197 ut_assert_nextline("(64 bootflows, 1 valid)");
198 ut_assert_console_end();
199
200 ut_assertok(run_command("bootflow list", 0));
201 ut_assert_nextline("Showing all bootflows");
202 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
203 ut_assert_nextlinen("---");
Simon Glassd465ad52023-08-24 13:55:39 -0600204 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
205 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
206 ut_assert_skip_to_line(
207 " 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
208 ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glassb255efc2022-04-24 23:31:24 -0600209 ut_assert_nextlinen("---");
210 ut_assert_nextline("(64 bootflows, 1 valid)");
211 ut_assert_console_end();
212
213 return 0;
214}
Simon Glassf0425022024-08-22 07:57:54 -0600215BOOTSTD_TEST(bootflow_cmd_scan_e, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600216
217/* Check 'bootflow info' */
218static int bootflow_cmd_info(struct unit_test_state *uts)
219{
Simon Glassb255efc2022-04-24 23:31:24 -0600220 ut_assertok(run_command("bootdev select 1", 0));
221 ut_assert_console_end();
222 ut_assertok(run_command("bootflow scan", 0));
223 ut_assert_console_end();
224 ut_assertok(run_command("bootflow select 0", 0));
225 ut_assert_console_end();
226 ut_assertok(run_command("bootflow info", 0));
227 ut_assert_nextline("Name: mmc1.bootdev.part_1");
228 ut_assert_nextline("Device: mmc1.bootdev");
229 ut_assert_nextline("Block dev: mmc1.blk");
Simon Glassb71d7f72023-05-10 16:34:46 -0600230 ut_assert_nextline("Method: extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600231 ut_assert_nextline("State: ready");
232 ut_assert_nextline("Partition: 1");
233 ut_assert_nextline("Subdir: (none)");
234 ut_assert_nextline("Filename: /extlinux/extlinux.conf");
235 ut_assert_nextlinen("Buffer: ");
236 ut_assert_nextline("Size: 253 (595 bytes)");
Simon Glass72b7b192023-01-06 08:52:33 -0600237 ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
Simon Glass33927522023-07-12 09:04:34 -0600238 ut_assert_nextline("Cmdline: (none)");
Simon Glass612b9cc2023-01-06 08:52:34 -0600239 ut_assert_nextline("Logo: (none)");
Simon Glass7b8c6342023-01-17 10:47:56 -0700240 ut_assert_nextline("FDT: <NULL>");
Simon Glassb255efc2022-04-24 23:31:24 -0600241 ut_assert_nextline("Error: 0");
242 ut_assert_console_end();
243
244 ut_assertok(run_command("bootflow info -d", 0));
245 ut_assert_nextline("Name: mmc1.bootdev.part_1");
246 ut_assert_skip_to_line("Error: 0");
247 ut_assert_nextline("Contents:");
248 ut_assert_nextline("%s", "");
249 ut_assert_nextline("# extlinux.conf generated by appliance-creator");
250 ut_assert_skip_to_line(" initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
251 ut_assert_console_end();
252
253 return 0;
254}
Simon Glassf0425022024-08-22 07:57:54 -0600255BOOTSTD_TEST(bootflow_cmd_info, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600256
257/* Check 'bootflow scan -b' to boot the first available bootdev */
258static int bootflow_scan_boot(struct unit_test_state *uts)
259{
Simon Glass49ad1d82022-07-30 15:52:16 -0600260 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600261 ut_assertok(run_command("bootflow scan -b", 0));
262 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600263 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600264 ut_assert_nextline("Ignoring unknown command: ui");
265
266 /*
267 * We expect it to get through to boot although sandbox always returns
268 * -EFAULT as it cannot actually boot the kernel
269 */
270 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
271 ut_assert_nextline("Boot failed (err=-14)");
272 ut_assert_console_end();
273
274 return 0;
275}
Simon Glassf0425022024-08-22 07:57:54 -0600276BOOTSTD_TEST(bootflow_scan_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600277
278/* Check iterating through available bootflows */
279static int bootflow_iter(struct unit_test_state *uts)
280{
281 struct bootflow_iter iter;
282 struct bootflow bflow;
283
284 bootstd_clear_glob();
285
286 /* The first device is mmc2.bootdev which has no media */
287 ut_asserteq(-EPROTONOSUPPORT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700288 bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700289 BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600290 ut_asserteq(2, iter.num_methods);
291 ut_asserteq(0, iter.cur_method);
292 ut_asserteq(0, iter.part);
293 ut_asserteq(0, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600294 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600295 ut_asserteq(0, bflow.err);
296
297 /*
Simon Glassc8d37212022-07-30 15:52:34 -0600298 * This shows MEDIA even though there is none, since in
Simon Glassb255efc2022-04-24 23:31:24 -0600299 * bootdev_find_in_blk() we call part_get_info() which returns
300 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
301 * know.
302 */
303 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
304
305 ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
306 ut_asserteq(2, iter.num_methods);
307 ut_asserteq(1, iter.cur_method);
308 ut_asserteq(0, iter.part);
309 ut_asserteq(0, iter.max_part);
310 ut_asserteq_str("efi", iter.method->name);
311 ut_asserteq(0, bflow.err);
312 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
313 bootflow_free(&bflow);
314
315 /* The next device is mmc1.bootdev - at first we use the whole device */
316 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
317 ut_asserteq(2, iter.num_methods);
318 ut_asserteq(0, iter.cur_method);
319 ut_asserteq(0, iter.part);
320 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600321 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600322 ut_asserteq(0, bflow.err);
323 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
324 bootflow_free(&bflow);
325
326 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
327 ut_asserteq(2, iter.num_methods);
328 ut_asserteq(1, iter.cur_method);
329 ut_asserteq(0, iter.part);
330 ut_asserteq(0x1e, iter.max_part);
331 ut_asserteq_str("efi", iter.method->name);
332 ut_asserteq(0, bflow.err);
333 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
334 bootflow_free(&bflow);
335
336 /* Then more to partition 1 where we find something */
337 ut_assertok(bootflow_scan_next(&iter, &bflow));
338 ut_asserteq(2, iter.num_methods);
339 ut_asserteq(0, iter.cur_method);
340 ut_asserteq(1, iter.part);
341 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600342 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600343 ut_asserteq(0, bflow.err);
344 ut_asserteq(BOOTFLOWST_READY, bflow.state);
345 bootflow_free(&bflow);
346
347 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
348 ut_asserteq(2, iter.num_methods);
349 ut_asserteq(1, iter.cur_method);
350 ut_asserteq(1, iter.part);
351 ut_asserteq(0x1e, iter.max_part);
352 ut_asserteq_str("efi", iter.method->name);
353 ut_asserteq(0, bflow.err);
354 ut_asserteq(BOOTFLOWST_FS, bflow.state);
355 bootflow_free(&bflow);
356
Simon Glassf5e2df02023-01-17 10:47:41 -0700357 /* Then more to partition 2 which exists but is not bootable */
Simon Glass64cbc5c2023-01-17 10:47:42 -0700358 ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600359 ut_asserteq(2, iter.num_methods);
360 ut_asserteq(0, iter.cur_method);
361 ut_asserteq(2, iter.part);
362 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600363 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600364 ut_asserteq(0, bflow.err);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700365 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
Simon Glassb255efc2022-04-24 23:31:24 -0600366 bootflow_free(&bflow);
367
368 bootflow_iter_uninit(&iter);
369
370 ut_assert_console_end();
371
372 return 0;
373}
Simon Glass129ba732024-10-19 09:21:58 -0600374BOOTSTD_TEST(bootflow_iter, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600375
Simon Glass90b643d2022-07-30 15:52:36 -0600376#if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
Simon Glassb255efc2022-04-24 23:31:24 -0600377/* Check using the system bootdev */
378static int bootflow_system(struct unit_test_state *uts)
379{
Simon Glassb9ff6482023-01-17 10:47:23 -0700380 struct udevice *bootstd, *dev;
Simon Glassb255efc2022-04-24 23:31:24 -0600381
AKASHI Takahiroe3e542d2024-01-17 13:39:42 +0900382 if (!IS_ENABLED(CONFIG_EFI_BOOTMGR))
Simon Glassc7599442022-10-20 18:22:49 -0600383 return -EAGAIN;
Simon Glassb9ff6482023-01-17 10:47:23 -0700384 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200385 ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_3efi_mgr),
Simon Glassb9ff6482023-01-17 10:47:23 -0700386 "efi_mgr", 0, ofnode_null(), &dev));
387 ut_assertok(device_probe(dev));
Simon Glass161e1e32022-07-30 15:52:22 -0600388 sandbox_set_fake_efi_mgr_dev(dev, true);
Simon Glassb255efc2022-04-24 23:31:24 -0600389
Simon Glassb255efc2022-04-24 23:31:24 -0600390 /* We should get a single 'bootmgr' method right at the end */
391 bootstd_clear_glob();
Simon Glasse4cf1062023-01-17 10:48:13 -0700392 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glasscc15e142022-07-30 15:52:27 -0600393 ut_assert_skip_to_line(
Simon Glassd465ad52023-08-24 13:55:39 -0600394 " 0 efi_mgr ready (none) 0 <NULL> ");
Simon Glasscc15e142022-07-30 15:52:27 -0600395 ut_assert_skip_to_line("No more bootdevs");
Simon Glassb9ff6482023-01-17 10:47:23 -0700396 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
Simon Glassb255efc2022-04-24 23:31:24 -0600397 ut_assert_console_end();
398
399 return 0;
400}
Simon Glassf0425022024-08-22 07:57:54 -0600401BOOTSTD_TEST(bootflow_system, UTF_DM | UTF_SCAN_PDATA | UTF_SCAN_FDT |
402 UTF_CONSOLE);
Simon Glass90b643d2022-07-30 15:52:36 -0600403#endif
Simon Glassb255efc2022-04-24 23:31:24 -0600404
405/* Check disabling a bootmethod if it requests it */
406static int bootflow_iter_disable(struct unit_test_state *uts)
407{
408 struct udevice *bootstd, *dev;
409 struct bootflow_iter iter;
410 struct bootflow bflow;
411 int i;
412
413 /* Add the EFI bootmgr driver */
414 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
415 ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
416 &dev));
417
Simon Glassb255efc2022-04-24 23:31:24 -0600418 ut_assertok(bootstd_test_drop_bootdev_order(uts));
419
420 bootstd_clear_glob();
Simon Glass49ad1d82022-07-30 15:52:16 -0600421 ut_assertok(inject_response(uts));
Simon Glasse4cf1062023-01-17 10:48:13 -0700422 ut_assertok(run_command("bootflow scan -lbH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600423
424 /* Try to boot the bootmgr flow, which will fail */
425 console_record_reset_enable();
Simon Glass5d3d44f2023-01-17 10:48:16 -0700426 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600427 ut_asserteq(3, iter.num_methods);
428 ut_asserteq_str("sandbox", iter.method->name);
Simon Glass49ad1d82022-07-30 15:52:16 -0600429 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600430 ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
431
432 ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
433 ut_assert_console_end();
434
435 /* Check that the sandbox bootmeth has been removed */
436 ut_asserteq(2, iter.num_methods);
437 for (i = 0; i < iter.num_methods; i++)
438 ut_assert(strcmp("sandbox", iter.method_order[i]->name));
439
440 return 0;
441}
Simon Glassf0425022024-08-22 07:57:54 -0600442BOOTSTD_TEST(bootflow_iter_disable, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600443
Simon Glassc8d37212022-07-30 15:52:34 -0600444/* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
445static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
446{
Simon Glass90b643d2022-07-30 15:52:36 -0600447 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc7599442022-10-20 18:22:49 -0600448 return -EAGAIN;
Simon Glass90b643d2022-07-30 15:52:36 -0600449
Simon Glassc8d37212022-07-30 15:52:34 -0600450 ut_assertok(bootstd_test_drop_bootdev_order(uts));
451
452 /*
453 * Make sure that the -G flag makes the scan fail, since this is not
454 * supported when an ordering is provided
455 */
Simon Glassc8d37212022-07-30 15:52:34 -0600456 ut_assertok(bootmeth_set_order("efi firmware0"));
Simon Glasse4cf1062023-01-17 10:48:13 -0700457 ut_assertok(run_command("bootflow scan -lGH", 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_nextlinen("---");
463 ut_assert_nextline("(0 bootflows, 0 valid)");
464 ut_assert_console_end();
465
Simon Glasse4cf1062023-01-17 10:48:13 -0700466 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassc8d37212022-07-30 15:52:34 -0600467 ut_assert_nextline("Scanning for bootflows in all bootdevs");
468 ut_assert_nextline(
469 "Seq Method State Uclass Part Name Filename");
470 ut_assert_nextlinen("---");
471 ut_assert_nextline("Scanning global bootmeth 'firmware0':");
472 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
473 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
474 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
475 ut_assert_nextline("No more bootdevs");
476 ut_assert_nextlinen("---");
477 ut_assert_nextline("(0 bootflows, 0 valid)");
478 ut_assert_console_end();
479
480 return 0;
481}
Simon Glassf0425022024-08-22 07:57:54 -0600482BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UTF_DM | UTF_SCAN_FDT |
483 UTF_CONSOLE);
Simon Glassc8d37212022-07-30 15:52:34 -0600484
Simon Glassb255efc2022-04-24 23:31:24 -0600485/* Check 'bootflow boot' to boot a selected bootflow */
486static int bootflow_cmd_boot(struct unit_test_state *uts)
487{
Simon Glassb255efc2022-04-24 23:31:24 -0600488 ut_assertok(run_command("bootdev select 1", 0));
489 ut_assert_console_end();
490 ut_assertok(run_command("bootflow scan", 0));
491 ut_assert_console_end();
492 ut_assertok(run_command("bootflow select 0", 0));
493 ut_assert_console_end();
Simon Glass49ad1d82022-07-30 15:52:16 -0600494
495 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600496 ut_asserteq(1, run_command("bootflow boot", 0));
497 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600498 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600499 ut_assert_nextline("Ignoring unknown command: ui");
500
501 /*
502 * We expect it to get through to boot although sandbox always returns
503 * -EFAULT as it cannot actually boot the kernel
504 */
505 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
506 ut_assert_nextline("Boot failed (err=-14)");
507 ut_assert_console_end();
508
509 return 0;
510}
Simon Glassf0425022024-08-22 07:57:54 -0600511BOOTSTD_TEST(bootflow_cmd_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600512
Simon Glassd92bcc42023-01-06 08:52:42 -0600513/**
Simon Glassbb76a5c2023-08-24 13:55:40 -0600514 * prep_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
Simon Glassd92bcc42023-01-06 08:52:42 -0600515 *
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600516 * After calling this function, set std->bootdev_order to *@old_orderp to
517 * restore normal operation of bootstd (i.e. with the original bootdev order)
518 *
Simon Glassd92bcc42023-01-06 08:52:42 -0600519 * @uts: Unit test state
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600520 * @mmc_dev: MMC device to use, e.g. "mmc4". Note that this must remain valid
521 * in the caller until
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200522 * @bind_cros: true to bind the ChromiumOS and Android bootmeths
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600523 * @old_orderp: Returns the original bootdev order, which must be restored
Simon Glassd92bcc42023-01-06 08:52:42 -0600524 * Returns 0 on success, -ve on failure
525 */
Simon Glassfff928c2023-08-24 13:55:41 -0600526static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200527 bool bind_cros_android, const char ***old_orderp)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600528{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600529 static const char *order[] = {"mmc2", "mmc1", NULL, NULL};
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600530 struct udevice *dev, *bootstd;
531 struct bootstd_priv *std;
532 const char **old_order;
Simon Glassbb76a5c2023-08-24 13:55:40 -0600533 ofnode root, node;
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600534
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600535 order[2] = mmc_dev;
536
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600537 /* Enable the mmc4 node since we need a second bootflow */
Simon Glassbb76a5c2023-08-24 13:55:40 -0600538 root = oftree_root(oftree_default());
539 node = ofnode_find_subnode(root, mmc_dev);
540 ut_assert(ofnode_valid(node));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600541 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
542
543 /* Enable the script bootmeth too */
544 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Simon Glass2f27e472023-08-19 16:49:35 -0600545 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_2script),
Simon Glassa59d6d32024-10-19 09:21:50 -0600546 "script", 0, ofnode_null(), &dev));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600547
Simon Glassfff928c2023-08-24 13:55:41 -0600548 /* Enable the cros bootmeth if needed */
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200549 if (IS_ENABLED(CONFIG_BOOTMETH_CROS) && bind_cros_android) {
Simon Glassfff928c2023-08-24 13:55:41 -0600550 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
551 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_cros),
552 "cros", 0, ofnode_null(), &dev));
553 }
554
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200555 /* Enable the android bootmeths if needed */
556 if (IS_ENABLED(CONFIG_BOOTMETH_ANDROID) && bind_cros_android) {
557 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
558 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_android),
559 "android", 0, ofnode_null(), &dev));
560 }
561
Simon Glassbb76a5c2023-08-24 13:55:40 -0600562 /* Change the order to include the device */
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600563 std = dev_get_priv(bootstd);
564 old_order = std->bootdev_order;
565 std->bootdev_order = order;
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600566 *old_orderp = old_order;
567
568 return 0;
569}
570
571/**
572 * scan_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
573 *
574 * @uts: Unit test state
575 * @mmc_dev: MMC device to use, e.g. "mmc4"
576 * @bind_cros: true to bind the ChromiumOS bootmeth
577 * Returns 0 on success, -ve on failure
578 */
579static int scan_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
580 bool bind_cros)
581{
582 struct bootstd_priv *std;
583 struct udevice *bootstd;
584 const char **old_order;
585
586 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, bind_cros, &old_order));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600587
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600588 ut_assertok(run_command("bootflow scan", 0));
589 ut_assert_console_end();
590
591 /* Restore the order used by the device tree */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600592 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
593 std = dev_get_priv(bootstd);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600594 std->bootdev_order = old_order;
595
Simon Glassd92bcc42023-01-06 08:52:42 -0600596 return 0;
597}
598
Simon Glassbb76a5c2023-08-24 13:55:40 -0600599/**
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200600 * scan_mmc_android_bootdev() - Set up an mmc bootdev so we can access other
601 * distros. Android bootflow might print "ANDROID:*" while scanning
602 *
603 * @uts: Unit test state
604 * @mmc_dev: MMC device to use, e.g. "mmc4"
605 * Returns 0 on success, -ve on failure
606 */
607static int scan_mmc_android_bootdev(struct unit_test_state *uts, const char *mmc_dev)
608{
609 struct bootstd_priv *std;
610 struct udevice *bootstd;
611 const char **old_order;
612
613 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, true, &old_order));
614
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200615 ut_assertok(run_command("bootflow scan", 0));
616 /* Android bootflow might print one or two 'ANDROID:*' logs */
617 ut_check_skipline(uts);
618 ut_check_skipline(uts);
619 ut_assert_console_end();
620
621 /* Restore the order used by the device tree */
622 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
623 std = dev_get_priv(bootstd);
624 std->bootdev_order = old_order;
625
626 return 0;
627}
628
629/**
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600630 * scan_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
Simon Glassbb76a5c2023-08-24 13:55:40 -0600631 *
632 * @uts: Unit test state
633 * Returns 0 on success, -ve on failure
634 */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600635static int scan_mmc4_bootdev(struct unit_test_state *uts)
Simon Glassbb76a5c2023-08-24 13:55:40 -0600636{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600637 ut_assertok(scan_mmc_bootdev(uts, "mmc4", false));
Simon Glassbb76a5c2023-08-24 13:55:40 -0600638
639 return 0;
640}
641
Simon Glassd92bcc42023-01-06 08:52:42 -0600642/* Check 'bootflow menu' to select a bootflow */
643static int bootflow_cmd_menu(struct unit_test_state *uts)
644{
Simon Glass6d5083b2023-10-01 19:14:38 -0600645 struct bootstd_priv *std;
Simon Glassd92bcc42023-01-06 08:52:42 -0600646 char prev[3];
647
Simon Glass6d5083b2023-10-01 19:14:38 -0600648 /* get access to the current bootflow */
649 ut_assertok(bootstd_get_priv(&std));
650
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600651 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600652
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600653 /* Add keypresses to move to and select the second one in the list */
654 prev[0] = CTL_CH('n');
655 prev[1] = '\r';
656 prev[2] = '\0';
657 ut_asserteq(2, console_in_puts(prev));
658
659 ut_assertok(run_command("bootflow menu", 0));
660 ut_assert_nextline("Selected: Armbian");
Simon Glass6d5083b2023-10-01 19:14:38 -0600661 ut_assertnonnull(std->cur_bootflow);
662 ut_assert_console_end();
663
664 /* Check not selecting anything */
665 prev[0] = '\e';
666 prev[1] = '\0';
667 ut_asserteq(1, console_in_puts(prev));
668
669 ut_asserteq(1, run_command("bootflow menu", 0));
670 ut_assertnull(std->cur_bootflow);
671 ut_assert_nextline("Nothing chosen");
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600672 ut_assert_console_end();
673
674 return 0;
675}
Simon Glassf0425022024-08-22 07:57:54 -0600676BOOTSTD_TEST(bootflow_cmd_menu, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassd92bcc42023-01-06 08:52:42 -0600677
Simon Glass9bf27862023-10-01 19:15:25 -0600678/* Check 'bootflow scan -m' to select a bootflow using a menu */
679static int bootflow_scan_menu(struct unit_test_state *uts)
680{
681 struct bootstd_priv *std;
682 const char **old_order, **new_order;
683 char prev[3];
684
685 /* get access to the current bootflow */
686 ut_assertok(bootstd_get_priv(&std));
687
688 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
689
690 /* Add keypresses to move to and select the second one in the list */
691 prev[0] = CTL_CH('n');
692 prev[1] = '\r';
693 prev[2] = '\0';
694 ut_asserteq(2, console_in_puts(prev));
695
696 ut_assertok(run_command("bootflow scan -lm", 0));
697 new_order = std->bootdev_order;
698 std->bootdev_order = old_order;
699
700 ut_assert_skip_to_line("No more bootdevs");
701 ut_assert_nextlinen("--");
702 ut_assert_nextline("(2 bootflows, 2 valid)");
703
704 ut_assert_nextline("Selected: Armbian");
705 ut_assertnonnull(std->cur_bootflow);
706 ut_assert_console_end();
707
708 /* Check not selecting anything */
709 prev[0] = '\e';
710 prev[1] = '\0';
711 ut_asserteq(1, console_in_puts(prev));
712
713 std->bootdev_order = new_order; /* Blue Monday */
714 ut_assertok(run_command("bootflow scan -lm", 0));
715 std->bootdev_order = old_order;
716
717 ut_assertnull(std->cur_bootflow);
718 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
719 ut_assert_nextline("Nothing chosen");
720 ut_assert_console_end();
721
722 return 0;
723}
Simon Glass11fcfa32024-08-22 07:57:50 -0600724BOOTSTD_TEST(bootflow_scan_menu, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass9bf27862023-10-01 19:15:25 -0600725
726/* Check 'bootflow scan -mb' to select and boot a bootflow using a menu */
727static int bootflow_scan_menu_boot(struct unit_test_state *uts)
728{
729 struct bootstd_priv *std;
730 const char **old_order;
731 char prev[3];
732
733 /* get access to the current bootflow */
734 ut_assertok(bootstd_get_priv(&std));
735
736 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
737
738 /* Add keypresses to move to and select the second one in the list */
739 prev[0] = CTL_CH('n');
740 prev[1] = '\r';
741 prev[2] = '\0';
742 ut_asserteq(2, console_in_puts(prev));
743
744 ut_assertok(run_command("bootflow scan -lmb", 0));
745 std->bootdev_order = old_order;
746
747 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
748
749 ut_assert_nextline("Selected: Armbian");
Francis Laniel5b64c452023-12-22 22:02:35 +0100750
751 if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
752 /*
753 * With old hush, despite booti failing to boot, i.e. returning
754 * CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as
755 * we are using bootmeth_script here, to return -EFAULT.
756 */
757 ut_assert_skip_to_line("Boot failed (err=-14)");
758 } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
759 /*
760 * While with modern one, run_command() propagates CMD_RET_FAILURE returned
761 * by booti, so we get 1 here.
762 */
763 ut_assert_skip_to_line("Boot failed (err=1)");
764 }
Simon Glass9bf27862023-10-01 19:15:25 -0600765 ut_assertnonnull(std->cur_bootflow);
766 ut_assert_console_end();
767
768 return 0;
769}
Simon Glass11fcfa32024-08-22 07:57:50 -0600770BOOTSTD_TEST(bootflow_scan_menu_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass9bf27862023-10-01 19:15:25 -0600771
Simon Glassba3d5372023-01-17 10:48:15 -0700772/* Check searching for a single bootdev using the hunters */
773static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
774{
775 struct bootstd_priv *std;
776
777 /* get access to the used hunters */
778 ut_assertok(bootstd_get_priv(&std));
779
780 ut_assertok(bootstd_test_drop_bootdev_order(uts));
781
Simon Glassba3d5372023-01-17 10:48:15 -0700782 ut_assertok(run_command("bootflow scan -l mmc1", 0));
783 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
784 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
785 ut_assert_console_end();
786
787 /* check that the hunter was used */
788 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
789
790 return 0;
791}
Simon Glassf0425022024-08-22 07:57:54 -0600792BOOTSTD_TEST(bootflow_cmd_hunt_single, UTF_DM | UTF_SCAN_FDT |
793 UTF_CONSOLE);
Simon Glassba3d5372023-01-17 10:48:15 -0700794
795/* Check searching for a uclass label using the hunters */
796static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
797{
798 struct bootstd_priv *std;
799
800 /* get access to the used hunters */
801 ut_assertok(bootstd_get_priv(&std));
802
803 test_set_skip_delays(true);
804 test_set_eth_enable(false);
805 ut_assertok(bootstd_test_drop_bootdev_order(uts));
806
Simon Glassba3d5372023-01-17 10:48:15 -0700807 ut_assertok(run_command("bootflow scan -l mmc", 0));
808
809 /* check that the hunter was used */
810 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
811
812 /* check that we got the mmc1 bootflow */
813 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
814 ut_assert_nextlinen("Seq");
815 ut_assert_nextlinen("---");
816 ut_assert_nextline("Hunting with: simple_bus");
817 ut_assert_nextline("Found 2 extension board(s).");
818 ut_assert_nextline("Hunting with: mmc");
819 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
820 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
821 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600822 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassba3d5372023-01-17 10:48:15 -0700823 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
824 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
825 ut_assert_console_end();
826
827 return 0;
828}
Simon Glassf0425022024-08-22 07:57:54 -0600829BOOTSTD_TEST(bootflow_cmd_hunt_label, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassba3d5372023-01-17 10:48:15 -0700830
Simon Glassd92bcc42023-01-06 08:52:42 -0600831/**
832 * check_font() - Check that the font size for an item matches expectations
833 *
834 * @uts: Unit test state
835 * @scn: Scene containing the text object
836 * @id: ID of the text object
837 * Returns 0 on success, -ve on failure
838 */
839static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
840 int font_size)
841{
842 struct scene_obj_txt *txt;
843
844 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
845 ut_assertnonnull(txt);
846
847 ut_asserteq(font_size, txt->font_size);
848
849 return 0;
850}
851
852/* Check themes work with a bootflow menu */
853static int bootflow_menu_theme(struct unit_test_state *uts)
854{
855 const int font_size = 30;
856 struct scene *scn;
857 struct expo *exp;
858 ofnode node;
859 int i;
860
Marek Vasutabb772a2024-10-20 17:43:12 +0200861 if (!CONFIG_IS_ENABLED(BOOTSTD_MENU))
862 return -EAGAIN;
863
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600864 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600865
866 ut_assertok(bootflow_menu_new(&exp));
867 node = ofnode_path("/bootstd/theme");
868 ut_assert(ofnode_valid(node));
869 ut_assertok(bootflow_menu_apply_theme(exp, node));
870
871 scn = expo_lookup_scene_id(exp, MAIN);
872 ut_assertnonnull(scn);
873
874 /*
875 * Check that the txt objects have the correct font size from the
876 * device tree node: bootstd/theme
877 *
878 * Check both menu items, since there are two bootflows
879 */
880 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
881 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
882 for (i = 0; i < 2; i++) {
883 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
884 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
885 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
886 }
887
888 expo_destroy(exp);
889
890 return 0;
891}
Simon Glassf0425022024-08-22 07:57:54 -0600892BOOTSTD_TEST(bootflow_menu_theme, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassa08adca2023-07-12 09:04:38 -0600893
894/**
895 * check_arg() - Check both the normal case and the buffer-overflow case
896 *
897 * @uts: Unit-test state
898 * @expect_ret: Expected return value (i.e. buffer length)
899 * @expect_str: String expected to be returned
900 * @buf: Buffer to use
901 * @from: Original cmdline to update
902 * @arg: Argument to update (e.g. "console")
903 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
904 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
905 * it without any value ("initrd")
906 */
907static int check_arg(struct unit_test_state *uts, int expect_ret,
908 const char *expect_str, char *buf, const char *from,
909 const char *arg, const char *val)
910{
911 /* check for writing outside the reported bounds */
912 buf[expect_ret] = '[';
913 ut_asserteq(expect_ret,
914 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
915 ut_asserteq_str(expect_str, buf);
916 ut_asserteq('[', buf[expect_ret]);
917
918 /* do the test again but with one less byte in the buffer */
919 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
920 val, NULL));
921
922 return 0;
923}
924
925/* Test of bootflow_cmdline_set_arg() */
926static int test_bootflow_cmdline_set(struct unit_test_state *uts)
927{
928 char buf[50];
929 const int size = sizeof(buf);
930
931 /*
932 * note that buffer-overflow tests are immediately each test case, just
933 * top keep the code together
934 */
935
936 /* add an arg that doesn't already exist, starting from empty */
937 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
938 NULL));
939
940 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
941 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
942 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
943
944 /* add an arg that doesn't already exist, starting from non-empty */
945 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
946 BOOTFLOWCL_EMPTY));
947 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
948 ""));
949 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
950 "fred"));
951
952 /* update an arg at the start */
953 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
954 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
955 BOOTFLOWCL_EMPTY));
956 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
957 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
958 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
959 "1234"));
960
961 /* update an arg at the end */
962 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
963 NULL));
964 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
965 BOOTFLOWCL_EMPTY));
966 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
967 ""));
968 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
969 "1"));
970 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
971 "arg", "1234"));
972
973 /* update an arg in the middle */
974 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
975 "mary=abc arg=123 john=2", "arg", NULL));
976 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
977 "mary=abc arg=123 john=2", "arg",
978 BOOTFLOWCL_EMPTY));
979 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
980 "mary=abc arg=123 john=2", "arg", ""));
981 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
982 "mary=abc arg=123 john=2", "arg", "1"));
983 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
984 "mary=abc arg=123 john=2", "arg", "1234"));
985
986 /* handle existing args with quotes */
987 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
988 "mary=\"abc\" arg=123 john", "arg", NULL));
989
990 /* handle existing args with quoted spaces */
991 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
992 "mary=\"abc def\" arg=123 john", "arg", NULL));
993
994 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
995 buf, "mary=\"abc def\" arg=123 john", "def",
996 "4"));
997
998 /* quote at the start */
999 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1000 "mary=\"abc def\" arg=\"123 456\"",
1001 "arg", "\"4 5 6", NULL));
1002
1003 /* quote at the end */
1004 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1005 "mary=\"abc def\" arg=\"123 456\"",
1006 "arg", "4 5 6\"", NULL));
1007
1008 /* quote in the middle */
1009 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1010 "mary=\"abc def\" arg=\"123 456\"",
1011 "arg", "\"4 \"5 6\"", NULL));
1012
1013 /* handle updating a quoted arg */
1014 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
1015 "mary=\"abc def\" arg=\"123 456\"", "arg",
1016 "4 5 6"));
1017
1018 /* changing a quoted arg to a non-quoted arg */
1019 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
1020 "mary=\"abc def\" arg=\"123 456\"", "arg",
1021 "789"));
1022
1023 /* changing a non-quoted arg to a quoted arg */
1024 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
1025 "mary=\"abc def\" arg=123", "arg", "456 789"));
1026
1027 /* handling of spaces */
1028 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1029 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1030 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
1031 "123"));
1032 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
1033 "arg", "123"));
1034 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
1035 " john arg=123 mary ", "arg", "123"));
1036
1037 /* unchanged arg */
1038 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
1039
1040 /* arg which starts with the same name */
1041 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
1042 "mary=abc johnathon=2 john=1", "john", "3"));
1043
1044 return 0;
1045}
1046BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
Simon Glass55a2da32023-07-12 09:04:39 -06001047
1048/* Test of bootflow_cmdline_set_arg() */
1049static int bootflow_set_arg(struct unit_test_state *uts)
1050{
1051 struct bootflow s_bflow, *bflow = &s_bflow;
1052 ulong mem_start;
1053
1054 ut_assertok(env_set("bootargs", NULL));
1055
1056 mem_start = ut_check_delta(0);
1057
1058 /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
1059 bflow->cmdline = NULL;
1060 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
1061 ut_asserteq_str(bflow->cmdline, "fred=123");
1062
1063 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
1064 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
1065
1066 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
1067 ut_asserteq_str(bflow->cmdline, "fred=123");
1068 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
1069 ut_asserteq_ptr(bflow->cmdline, NULL);
1070
1071 ut_asserteq(0, ut_check_delta(mem_start));
1072
1073 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
1074 ut_asserteq_str("mary=here", env_get("bootargs"));
1075 ut_assertok(env_set("bootargs", NULL));
1076
1077 return 0;
1078}
1079BOOTSTD_TEST(bootflow_set_arg, 0);
1080
1081/* Test of bootflow_cmdline_get_arg() */
1082static int bootflow_cmdline_get(struct unit_test_state *uts)
1083{
1084 int pos;
1085
1086 /* empty string */
1087 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
1088
1089 /* arg with empty value */
1090 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
1091 ut_asserteq(5, pos);
1092
1093 /* arg with a value */
1094 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
1095 ut_asserteq(5, pos);
1096
1097 /* arg with a value */
1098 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
1099 ut_asserteq(12, pos);
1100
1101 /* arg with a value, after quoted arg */
1102 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
1103 ut_asserteq(16, pos);
1104
1105 /* arg in the middle */
1106 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
1107 &pos));
1108 ut_asserteq(15, pos);
1109
1110 /* quoted arg */
1111 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
1112 "fred", &pos));
1113 ut_asserteq(17, pos);
1114
1115 /* args starting with the same prefix */
1116 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
1117 &pos));
1118 ut_asserteq(26, pos);
1119
1120 return 0;
1121}
1122BOOTSTD_TEST(bootflow_cmdline_get, 0);
1123
1124static int bootflow_cmdline(struct unit_test_state *uts)
1125{
1126 ut_assertok(run_command("bootflow scan mmc", 0));
1127 ut_assertok(run_command("bootflow sel 0", 0));
Simon Glass55a2da32023-07-12 09:04:39 -06001128
1129 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1130 ut_assert_nextline("Argument not found");
1131 ut_assert_console_end();
1132
1133 ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
1134 ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
1135 ut_assert_nextline("123");
1136
1137 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1138 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1139 ut_assert_nextline("abc");
1140
1141 ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
1142 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1143 ut_assert_nextline("Argument not found");
1144
1145 ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
1146 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1147 ut_assert_nextline_empty();
1148
Simon Glass7a164f82023-11-29 10:31:19 -07001149 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1150 ut_asserteq(0, run_command("bootflow cmdline set mary", 0));
Simon Glass55a2da32023-07-12 09:04:39 -06001151 ut_assert_console_end();
1152
1153 return 0;
1154}
Simon Glassf0425022024-08-22 07:57:54 -06001155BOOTSTD_TEST(bootflow_cmdline, UTF_CONSOLE);
Simon Glassfff928c2023-08-24 13:55:41 -06001156
Simon Glassa61057f2023-10-25 07:17:36 +13001157/* test a few special changes to a long command line */
1158static int bootflow_cmdline_special(struct unit_test_state *uts)
1159{
1160 char buf[500];
1161 int pos;
1162
1163 /*
1164 * check handling of an argument which has an embedded '=', as well as
1165 * handling of a argument which partially matches ("ro" and "root")
1166 */
1167 ut_asserteq(32, cmdline_set_arg(
1168 buf, sizeof(buf),
1169 "loglevel=7 root=PARTUUID=d68352e3 rootwait ro noinitrd",
1170 "root", NULL, &pos));
1171 ut_asserteq_str("loglevel=7 rootwait ro noinitrd", buf);
1172
1173 return 0;
1174}
1175BOOTSTD_TEST(bootflow_cmdline_special, 0);
1176
Simon Glassfff928c2023-08-24 13:55:41 -06001177/* Test ChromiumOS bootmeth */
1178static int bootflow_cros(struct unit_test_state *uts)
1179{
Simon Glass3ff8a9a2023-10-01 19:14:37 -06001180 ut_assertok(scan_mmc_bootdev(uts, "mmc5", true));
Simon Glassfff928c2023-08-24 13:55:41 -06001181 ut_assertok(run_command("bootflow list", 0));
1182
1183 ut_assert_nextlinen("Showing all");
1184 ut_assert_nextlinen("Seq");
1185 ut_assert_nextlinen("---");
1186 ut_assert_nextlinen(" 0 extlinux");
Simon Glassd7d3a972023-08-24 13:55:45 -06001187 ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 ");
1188 ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 ");
Simon Glassfff928c2023-08-24 13:55:41 -06001189 ut_assert_nextlinen("---");
Simon Glassd7d3a972023-08-24 13:55:45 -06001190 ut_assert_skip_to_line("(3 bootflows, 3 valid)");
Simon Glassfff928c2023-08-24 13:55:41 -06001191
1192 ut_assert_console_end();
1193
1194 return 0;
1195}
Simon Glassf0425022024-08-22 07:57:54 -06001196BOOTSTD_TEST(bootflow_cros, UTF_CONSOLE);
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +02001197
1198/* Test Android bootmeth */
1199static int bootflow_android(struct unit_test_state *uts)
1200{
1201 if (!IS_ENABLED(CONFIG_BOOTMETH_ANDROID))
1202 return -EAGAIN;
1203
1204 ut_assertok(scan_mmc_android_bootdev(uts, "mmc7"));
1205 ut_assertok(run_command("bootflow list", 0));
1206
1207 ut_assert_nextlinen("Showing all");
1208 ut_assert_nextlinen("Seq");
1209 ut_assert_nextlinen("---");
1210 ut_assert_nextlinen(" 0 extlinux");
1211 ut_assert_nextlinen(" 1 android ready mmc 0 mmc7.bootdev.whole ");
1212 ut_assert_nextlinen("---");
1213 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
1214
1215 ut_assert_console_end();
1216
1217 return 0;
1218}
Simon Glassf0425022024-08-22 07:57:54 -06001219BOOTSTD_TEST(bootflow_android, UTF_CONSOLE);