blob: eb7f00af39ad991532166360dd57186e5652fc9e [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 Glass4f378de2024-11-07 14:31:50 -070016#include <efi_loader.h>
Simon Glassd92bcc42023-01-06 08:52:42 -060017#include <expo.h>
Simon Glassc6a52e72024-11-15 16:19:23 -070018#include <mapmem.h>
Simon Glass90b643d2022-07-30 15:52:36 -060019#ifdef CONFIG_SANDBOX
Simon Glass161e1e32022-07-30 15:52:22 -060020#include <asm/test.h>
Simon Glass90b643d2022-07-30 15:52:36 -060021#endif
Simon Glassd2bc33ed2023-01-06 08:52:41 -060022#include <dm/device-internal.h>
Simon Glassb255efc2022-04-24 23:31:24 -060023#include <dm/lists.h>
Simon Glassb255efc2022-04-24 23:31:24 -060024#include <test/ut.h>
25#include "bootstd_common.h"
Simon Glassd92bcc42023-01-06 08:52:42 -060026#include "../../boot/bootflow_internal.h"
27#include "../../boot/scene_internal.h"
Simon Glassb255efc2022-04-24 23:31:24 -060028
Simon Glassd2bc33ed2023-01-06 08:52:41 -060029DECLARE_GLOBAL_DATA_PTR;
30
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +020031extern U_BOOT_DRIVER(bootmeth_android);
Simon Glassfff928c2023-08-24 13:55:41 -060032extern U_BOOT_DRIVER(bootmeth_cros);
Simon Glass2f27e472023-08-19 16:49:35 -060033extern U_BOOT_DRIVER(bootmeth_2script);
Simon Glassd2bc33ed2023-01-06 08:52:41 -060034
Simon Glass4f378de2024-11-07 14:31:50 -070035/* Use this as the vendor for EFI to tell the app to exit boot services */
36static u16 __efi_runtime_data test_vendor[] = u"U-Boot testing";
37
Simon Glass49ad1d82022-07-30 15:52:16 -060038static int inject_response(struct unit_test_state *uts)
39{
40 /*
41 * The image being booted presents a menu of options:
42 *
43 * Fedora-Workstation-armhfp-31-1.9 Boot Options.
44 * 1: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
45 * Enter choice:
46 *
47 * Provide input for this, to avoid waiting two seconds for a timeout.
48 */
49 ut_asserteq(2, console_in_puts("1\n"));
50
51 return 0;
52}
53
Simon Glassb255efc2022-04-24 23:31:24 -060054/* Check 'bootflow scan/list' commands */
55static int bootflow_cmd(struct unit_test_state *uts)
56{
Simon Glassb255efc2022-04-24 23:31:24 -060057 ut_assertok(run_command("bootdev select 1", 0));
58 ut_assert_console_end();
Simon Glasse4cf1062023-01-17 10:48:13 -070059 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060060 ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
61 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
62 ut_assert_nextlinen("---");
Simon Glass484e4072023-01-17 10:48:14 -070063 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
64 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassb71d7f72023-05-10 16:34:46 -060065 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass484e4072023-01-17 10:48:14 -070066 ut_assert_nextline("No more bootdevs");
Simon Glassb255efc2022-04-24 23:31:24 -060067 ut_assert_nextlinen("---");
68 ut_assert_nextline("(1 bootflow, 1 valid)");
69 ut_assert_console_end();
70
71 ut_assertok(run_command("bootflow list", 0));
72 ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'");
73 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
74 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060075 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -060076 ut_assert_nextlinen("---");
77 ut_assert_nextline("(1 bootflow, 1 valid)");
78 ut_assert_console_end();
79
Simon Glassc6a52e72024-11-15 16:19:23 -070080 ut_assertok(run_command("bootstd images", 0));
81 ut_assert_nextlinen("Seq");
82 ut_assert_nextlinen("---");
83 ut_assert_nextlinen(" 0 mmc1.bootdev.part_1 extlinux_cfg");
84 ut_assert_nextlinen("---");
85 ut_assert_nextline("(1 image)");
86 ut_assert_console_end();
87
Simon Glassb255efc2022-04-24 23:31:24 -060088 return 0;
89}
Simon Glassf0425022024-08-22 07:57:54 -060090BOOTSTD_TEST(bootflow_cmd, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -060091
Simon Glass484e4072023-01-17 10:48:14 -070092/* Check 'bootflow scan' with a label / seq */
Simon Glassb255efc2022-04-24 23:31:24 -060093static int bootflow_cmd_label(struct unit_test_state *uts)
94{
Simon Glass484e4072023-01-17 10:48:14 -070095 test_set_eth_enable(false);
96
Simon Glasse4cf1062023-01-17 10:48:13 -070097 ut_assertok(run_command("bootflow scan -lH mmc1", 0));
Simon Glass484e4072023-01-17 10:48:14 -070098 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
Simon Glassb255efc2022-04-24 23:31:24 -060099 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
100 ut_assert_console_end();
101
Simon Glass484e4072023-01-17 10:48:14 -0700102 ut_assertok(run_command("bootflow scan -lH 0", 0));
103 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassb255efc2022-04-24 23:31:24 -0600104 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
105 ut_assert_console_end();
106
Simon Glass484e4072023-01-17 10:48:14 -0700107 /*
108 * with ethernet enabled we have 8 devices ahead of the mmc ones:
109 *
110 * ut_assertok(run_command("bootdev list", 0));
111 * Seq Probed Status Uclass Name
112 * --- ------ ------ -------- ------------------
113 * 0 [ + ] OK ethernet eth@10002000.bootdev
114 * 1 [ ] OK ethernet eth@10003000.bootdev
115 * 2 [ ] OK ethernet sbe5.bootdev
116 * 3 [ ] OK ethernet eth@10004000.bootdev
117 * 4 [ ] OK ethernet phy-test-eth.bootdev
118 * 5 [ ] OK ethernet dsa-test-eth.bootdev
119 * 6 [ ] OK ethernet dsa-test@0.bootdev
120 * 7 [ ] OK ethernet dsa-test@1.bootdev
121 * 8 [ ] OK mmc mmc2.bootdev
122 * 9 [ + ] OK mmc mmc1.bootdev
123 * a [ ] OK mmc mmc0.bootdev
Jerome Forissier253d5982024-10-16 11:56:26 +0200124 *
125 * However with CONFIG_DSA_SANDBOX=n we have two fewer (dsa-test@0 and
126 * dsa-test@1).
Simon Glass484e4072023-01-17 10:48:14 -0700127 */
Jerome Forissier253d5982024-10-16 11:56:26 +0200128 if (CONFIG_IS_ENABLED(DSA_SANDBOX)) {
129 ut_assertok(run_command("bootflow scan -lH 9", 0));
130 ut_assert_nextline("Scanning for bootflows with label '9'");
131 } else {
132 ut_assertok(run_command("bootflow scan -lH 7", 0));
133 ut_assert_nextline("Scanning for bootflows with label '7'");
134 }
Simon Glass484e4072023-01-17 10:48:14 -0700135 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
136
Simon Glasse4cf1062023-01-17 10:48:13 -0700137 ut_assertok(run_command("bootflow scan -lH 0", 0));
Simon Glass484e4072023-01-17 10:48:14 -0700138 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassb255efc2022-04-24 23:31:24 -0600139 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
140 ut_assert_console_end();
141
142 return 0;
143}
Simon Glassf0425022024-08-22 07:57:54 -0600144BOOTSTD_TEST(bootflow_cmd_label, UTF_DM | UTF_SCAN_FDT | UTF_ETH_BOOTDEV |
145 UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600146
147/* Check 'bootflow scan/list' commands using all bootdevs */
148static int bootflow_cmd_glob(struct unit_test_state *uts)
149{
150 ut_assertok(bootstd_test_drop_bootdev_order(uts));
151
Simon Glasse4cf1062023-01-17 10:48:13 -0700152 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600153 ut_assert_nextline("Scanning for bootflows in all bootdevs");
154 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
155 ut_assert_nextlinen("---");
156 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
157 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassb71d7f72023-05-10 16:34:46 -0600158 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -0600159 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
160 ut_assert_nextline("No more bootdevs");
161 ut_assert_nextlinen("---");
162 ut_assert_nextline("(1 bootflow, 1 valid)");
163 ut_assert_console_end();
164
165 ut_assertok(run_command("bootflow list", 0));
166 ut_assert_nextline("Showing all bootflows");
167 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
168 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -0600169 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -0600170 ut_assert_nextlinen("---");
171 ut_assert_nextline("(1 bootflow, 1 valid)");
172 ut_assert_console_end();
173
174 return 0;
175}
Simon Glassf0425022024-08-22 07:57:54 -0600176BOOTSTD_TEST(bootflow_cmd_glob, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600177
178/* Check 'bootflow scan -e' */
179static int bootflow_cmd_scan_e(struct unit_test_state *uts)
180{
181 ut_assertok(bootstd_test_drop_bootdev_order(uts));
182
Simon Glasse4cf1062023-01-17 10:48:13 -0700183 ut_assertok(run_command("bootflow scan -aleGH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600184 ut_assert_nextline("Scanning for bootflows in all bootdevs");
185 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
186 ut_assert_nextlinen("---");
187 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600188 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600189 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassd465ad52023-08-24 13:55:39 -0600190 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600191 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassb255efc2022-04-24 23:31:24 -0600192
193 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600194 ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600195 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassd465ad52023-08-24 13:55:39 -0600196 ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600197 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassb71d7f72023-05-10 16:34:46 -0600198 ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass3b1e60b2024-11-07 14:31:43 -0700199 ut_assert_nextline(
200 " 5 efi fs mmc 1 mmc1.bootdev.part_1 /EFI/BOOT/%s",
201 efi_get_basename());
Simon Glassb255efc2022-04-24 23:31:24 -0600202
203 ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600204 ut_assert_skip_to_line(
205 " 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600206 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassb255efc2022-04-24 23:31:24 -0600207 ut_assert_nextline("No more bootdevs");
208 ut_assert_nextlinen("---");
209 ut_assert_nextline("(64 bootflows, 1 valid)");
210 ut_assert_console_end();
211
212 ut_assertok(run_command("bootflow list", 0));
213 ut_assert_nextline("Showing all bootflows");
214 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
215 ut_assert_nextlinen("---");
Simon Glassd465ad52023-08-24 13:55:39 -0600216 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
217 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
218 ut_assert_skip_to_line(
219 " 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
220 ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glassb255efc2022-04-24 23:31:24 -0600221 ut_assert_nextlinen("---");
222 ut_assert_nextline("(64 bootflows, 1 valid)");
223 ut_assert_console_end();
224
225 return 0;
226}
Simon Glassf0425022024-08-22 07:57:54 -0600227BOOTSTD_TEST(bootflow_cmd_scan_e, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600228
229/* Check 'bootflow info' */
230static int bootflow_cmd_info(struct unit_test_state *uts)
231{
Simon Glassb255efc2022-04-24 23:31:24 -0600232 ut_assertok(run_command("bootdev select 1", 0));
233 ut_assert_console_end();
234 ut_assertok(run_command("bootflow scan", 0));
235 ut_assert_console_end();
236 ut_assertok(run_command("bootflow select 0", 0));
237 ut_assert_console_end();
238 ut_assertok(run_command("bootflow info", 0));
239 ut_assert_nextline("Name: mmc1.bootdev.part_1");
240 ut_assert_nextline("Device: mmc1.bootdev");
241 ut_assert_nextline("Block dev: mmc1.blk");
Simon Glassb71d7f72023-05-10 16:34:46 -0600242 ut_assert_nextline("Method: extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600243 ut_assert_nextline("State: ready");
244 ut_assert_nextline("Partition: 1");
245 ut_assert_nextline("Subdir: (none)");
246 ut_assert_nextline("Filename: /extlinux/extlinux.conf");
247 ut_assert_nextlinen("Buffer: ");
248 ut_assert_nextline("Size: 253 (595 bytes)");
Simon Glass72b7b192023-01-06 08:52:33 -0600249 ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
Simon Glass33927522023-07-12 09:04:34 -0600250 ut_assert_nextline("Cmdline: (none)");
Simon Glass612b9cc2023-01-06 08:52:34 -0600251 ut_assert_nextline("Logo: (none)");
Simon Glass7b8c6342023-01-17 10:47:56 -0700252 ut_assert_nextline("FDT: <NULL>");
Simon Glassb255efc2022-04-24 23:31:24 -0600253 ut_assert_nextline("Error: 0");
254 ut_assert_console_end();
255
256 ut_assertok(run_command("bootflow info -d", 0));
257 ut_assert_nextline("Name: mmc1.bootdev.part_1");
258 ut_assert_skip_to_line("Error: 0");
259 ut_assert_nextline("Contents:");
260 ut_assert_nextline("%s", "");
261 ut_assert_nextline("# extlinux.conf generated by appliance-creator");
262 ut_assert_skip_to_line(" initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
263 ut_assert_console_end();
264
265 return 0;
266}
Simon Glassf0425022024-08-22 07:57:54 -0600267BOOTSTD_TEST(bootflow_cmd_info, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600268
269/* Check 'bootflow scan -b' to boot the first available bootdev */
270static int bootflow_scan_boot(struct unit_test_state *uts)
271{
Simon Glass49ad1d82022-07-30 15:52:16 -0600272 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600273 ut_assertok(run_command("bootflow scan -b", 0));
274 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600275 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600276 ut_assert_nextline("Ignoring unknown command: ui");
277
278 /*
279 * We expect it to get through to boot although sandbox always returns
280 * -EFAULT as it cannot actually boot the kernel
281 */
282 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
283 ut_assert_nextline("Boot failed (err=-14)");
284 ut_assert_console_end();
285
286 return 0;
287}
Simon Glassf0425022024-08-22 07:57:54 -0600288BOOTSTD_TEST(bootflow_scan_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600289
290/* Check iterating through available bootflows */
291static int bootflow_iter(struct unit_test_state *uts)
292{
293 struct bootflow_iter iter;
294 struct bootflow bflow;
295
296 bootstd_clear_glob();
297
298 /* The first device is mmc2.bootdev which has no media */
299 ut_asserteq(-EPROTONOSUPPORT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700300 bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700301 BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600302 ut_asserteq(2, iter.num_methods);
303 ut_asserteq(0, iter.cur_method);
304 ut_asserteq(0, iter.part);
305 ut_asserteq(0, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600306 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600307 ut_asserteq(0, bflow.err);
308
309 /*
Simon Glassc8d37212022-07-30 15:52:34 -0600310 * This shows MEDIA even though there is none, since in
Simon Glassb255efc2022-04-24 23:31:24 -0600311 * bootdev_find_in_blk() we call part_get_info() which returns
312 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
313 * know.
314 */
315 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
316
317 ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
318 ut_asserteq(2, iter.num_methods);
319 ut_asserteq(1, iter.cur_method);
320 ut_asserteq(0, iter.part);
321 ut_asserteq(0, iter.max_part);
322 ut_asserteq_str("efi", iter.method->name);
323 ut_asserteq(0, bflow.err);
324 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
325 bootflow_free(&bflow);
326
327 /* The next device is mmc1.bootdev - at first we use the whole device */
328 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
329 ut_asserteq(2, iter.num_methods);
330 ut_asserteq(0, iter.cur_method);
331 ut_asserteq(0, iter.part);
332 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600333 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600334 ut_asserteq(0, bflow.err);
335 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
336 bootflow_free(&bflow);
337
338 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
339 ut_asserteq(2, iter.num_methods);
340 ut_asserteq(1, iter.cur_method);
341 ut_asserteq(0, iter.part);
342 ut_asserteq(0x1e, iter.max_part);
343 ut_asserteq_str("efi", iter.method->name);
344 ut_asserteq(0, bflow.err);
345 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
346 bootflow_free(&bflow);
347
348 /* Then more to partition 1 where we find something */
349 ut_assertok(bootflow_scan_next(&iter, &bflow));
350 ut_asserteq(2, iter.num_methods);
351 ut_asserteq(0, iter.cur_method);
352 ut_asserteq(1, iter.part);
353 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600354 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600355 ut_asserteq(0, bflow.err);
356 ut_asserteq(BOOTFLOWST_READY, bflow.state);
357 bootflow_free(&bflow);
358
359 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
360 ut_asserteq(2, iter.num_methods);
361 ut_asserteq(1, iter.cur_method);
362 ut_asserteq(1, iter.part);
363 ut_asserteq(0x1e, iter.max_part);
364 ut_asserteq_str("efi", iter.method->name);
365 ut_asserteq(0, bflow.err);
366 ut_asserteq(BOOTFLOWST_FS, bflow.state);
367 bootflow_free(&bflow);
368
Simon Glassf5e2df02023-01-17 10:47:41 -0700369 /* Then more to partition 2 which exists but is not bootable */
Simon Glass64cbc5c2023-01-17 10:47:42 -0700370 ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600371 ut_asserteq(2, iter.num_methods);
372 ut_asserteq(0, iter.cur_method);
373 ut_asserteq(2, iter.part);
374 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600375 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600376 ut_asserteq(0, bflow.err);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700377 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
Simon Glassb255efc2022-04-24 23:31:24 -0600378 bootflow_free(&bflow);
379
380 bootflow_iter_uninit(&iter);
381
382 ut_assert_console_end();
383
384 return 0;
385}
Simon Glass129ba732024-10-19 09:21:58 -0600386BOOTSTD_TEST(bootflow_iter, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600387
Simon Glass90b643d2022-07-30 15:52:36 -0600388#if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
Simon Glassb255efc2022-04-24 23:31:24 -0600389/* Check using the system bootdev */
390static int bootflow_system(struct unit_test_state *uts)
391{
Simon Glassb9ff6482023-01-17 10:47:23 -0700392 struct udevice *bootstd, *dev;
Simon Glassb255efc2022-04-24 23:31:24 -0600393
AKASHI Takahiroe3e542d2024-01-17 13:39:42 +0900394 if (!IS_ENABLED(CONFIG_EFI_BOOTMGR))
Simon Glassc7599442022-10-20 18:22:49 -0600395 return -EAGAIN;
Simon Glassb9ff6482023-01-17 10:47:23 -0700396 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200397 ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_3efi_mgr),
Simon Glassb9ff6482023-01-17 10:47:23 -0700398 "efi_mgr", 0, ofnode_null(), &dev));
399 ut_assertok(device_probe(dev));
Simon Glass161e1e32022-07-30 15:52:22 -0600400 sandbox_set_fake_efi_mgr_dev(dev, true);
Simon Glassb255efc2022-04-24 23:31:24 -0600401
Simon Glassb255efc2022-04-24 23:31:24 -0600402 /* We should get a single 'bootmgr' method right at the end */
403 bootstd_clear_glob();
Simon Glasse4cf1062023-01-17 10:48:13 -0700404 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glasscc15e142022-07-30 15:52:27 -0600405 ut_assert_skip_to_line(
Simon Glassd465ad52023-08-24 13:55:39 -0600406 " 0 efi_mgr ready (none) 0 <NULL> ");
Simon Glasscc15e142022-07-30 15:52:27 -0600407 ut_assert_skip_to_line("No more bootdevs");
Simon Glassb9ff6482023-01-17 10:47:23 -0700408 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
Simon Glassb255efc2022-04-24 23:31:24 -0600409 ut_assert_console_end();
410
411 return 0;
412}
Simon Glassf0425022024-08-22 07:57:54 -0600413BOOTSTD_TEST(bootflow_system, UTF_DM | UTF_SCAN_PDATA | UTF_SCAN_FDT |
414 UTF_CONSOLE);
Simon Glass90b643d2022-07-30 15:52:36 -0600415#endif
Simon Glassb255efc2022-04-24 23:31:24 -0600416
417/* Check disabling a bootmethod if it requests it */
418static int bootflow_iter_disable(struct unit_test_state *uts)
419{
420 struct udevice *bootstd, *dev;
421 struct bootflow_iter iter;
422 struct bootflow bflow;
423 int i;
424
425 /* Add the EFI bootmgr driver */
426 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
427 ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
428 &dev));
429
Simon Glassb255efc2022-04-24 23:31:24 -0600430 ut_assertok(bootstd_test_drop_bootdev_order(uts));
431
432 bootstd_clear_glob();
Simon Glass49ad1d82022-07-30 15:52:16 -0600433 ut_assertok(inject_response(uts));
Simon Glasse4cf1062023-01-17 10:48:13 -0700434 ut_assertok(run_command("bootflow scan -lbH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600435
436 /* Try to boot the bootmgr flow, which will fail */
437 console_record_reset_enable();
Simon Glass5d3d44f2023-01-17 10:48:16 -0700438 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600439 ut_asserteq(3, iter.num_methods);
440 ut_asserteq_str("sandbox", iter.method->name);
Simon Glass49ad1d82022-07-30 15:52:16 -0600441 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600442 ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
443
444 ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
445 ut_assert_console_end();
446
447 /* Check that the sandbox bootmeth has been removed */
448 ut_asserteq(2, iter.num_methods);
449 for (i = 0; i < iter.num_methods; i++)
450 ut_assert(strcmp("sandbox", iter.method_order[i]->name));
451
452 return 0;
453}
Simon Glassf0425022024-08-22 07:57:54 -0600454BOOTSTD_TEST(bootflow_iter_disable, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600455
Simon Glassc8d37212022-07-30 15:52:34 -0600456/* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
457static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
458{
Simon Glass90b643d2022-07-30 15:52:36 -0600459 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc7599442022-10-20 18:22:49 -0600460 return -EAGAIN;
Simon Glass90b643d2022-07-30 15:52:36 -0600461
Simon Glassc8d37212022-07-30 15:52:34 -0600462 ut_assertok(bootstd_test_drop_bootdev_order(uts));
463
464 /*
465 * Make sure that the -G flag makes the scan fail, since this is not
466 * supported when an ordering is provided
467 */
Simon Glassc8d37212022-07-30 15:52:34 -0600468 ut_assertok(bootmeth_set_order("efi firmware0"));
Simon Glasse4cf1062023-01-17 10:48:13 -0700469 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassc8d37212022-07-30 15:52:34 -0600470 ut_assert_nextline("Scanning for bootflows in all bootdevs");
471 ut_assert_nextline(
472 "Seq Method State Uclass Part Name Filename");
473 ut_assert_nextlinen("---");
474 ut_assert_nextlinen("---");
475 ut_assert_nextline("(0 bootflows, 0 valid)");
476 ut_assert_console_end();
477
Simon Glasse4cf1062023-01-17 10:48:13 -0700478 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassc8d37212022-07-30 15:52:34 -0600479 ut_assert_nextline("Scanning for bootflows in all bootdevs");
480 ut_assert_nextline(
481 "Seq Method State Uclass Part Name Filename");
482 ut_assert_nextlinen("---");
483 ut_assert_nextline("Scanning global bootmeth 'firmware0':");
484 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
485 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
486 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
487 ut_assert_nextline("No more bootdevs");
488 ut_assert_nextlinen("---");
489 ut_assert_nextline("(0 bootflows, 0 valid)");
490 ut_assert_console_end();
491
492 return 0;
493}
Simon Glassf0425022024-08-22 07:57:54 -0600494BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UTF_DM | UTF_SCAN_FDT |
495 UTF_CONSOLE);
Simon Glassc8d37212022-07-30 15:52:34 -0600496
Simon Glassb255efc2022-04-24 23:31:24 -0600497/* Check 'bootflow boot' to boot a selected bootflow */
498static int bootflow_cmd_boot(struct unit_test_state *uts)
499{
Simon Glassb255efc2022-04-24 23:31:24 -0600500 ut_assertok(run_command("bootdev select 1", 0));
501 ut_assert_console_end();
502 ut_assertok(run_command("bootflow scan", 0));
503 ut_assert_console_end();
504 ut_assertok(run_command("bootflow select 0", 0));
505 ut_assert_console_end();
Simon Glass49ad1d82022-07-30 15:52:16 -0600506
507 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600508 ut_asserteq(1, run_command("bootflow boot", 0));
509 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600510 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600511 ut_assert_nextline("Ignoring unknown command: ui");
512
513 /*
514 * We expect it to get through to boot although sandbox always returns
515 * -EFAULT as it cannot actually boot the kernel
516 */
517 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
518 ut_assert_nextline("Boot failed (err=-14)");
519 ut_assert_console_end();
520
521 return 0;
522}
Simon Glassf0425022024-08-22 07:57:54 -0600523BOOTSTD_TEST(bootflow_cmd_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600524
Simon Glassd92bcc42023-01-06 08:52:42 -0600525/**
Simon Glassbb76a5c2023-08-24 13:55:40 -0600526 * prep_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
Simon Glassd92bcc42023-01-06 08:52:42 -0600527 *
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600528 * After calling this function, set std->bootdev_order to *@old_orderp to
529 * restore normal operation of bootstd (i.e. with the original bootdev order)
530 *
Simon Glassd92bcc42023-01-06 08:52:42 -0600531 * @uts: Unit test state
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600532 * @mmc_dev: MMC device to use, e.g. "mmc4". Note that this must remain valid
533 * in the caller until
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200534 * @bind_cros: true to bind the ChromiumOS and Android bootmeths
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600535 * @old_orderp: Returns the original bootdev order, which must be restored
Simon Glassd92bcc42023-01-06 08:52:42 -0600536 * Returns 0 on success, -ve on failure
537 */
Simon Glassfff928c2023-08-24 13:55:41 -0600538static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200539 bool bind_cros_android, const char ***old_orderp)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600540{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600541 static const char *order[] = {"mmc2", "mmc1", NULL, NULL};
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600542 struct udevice *dev, *bootstd;
543 struct bootstd_priv *std;
544 const char **old_order;
Simon Glassbb76a5c2023-08-24 13:55:40 -0600545 ofnode root, node;
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600546
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600547 order[2] = mmc_dev;
548
Simon Glass64c63252024-11-07 14:31:49 -0700549 /* Enable the requested mmc node since we need a second bootflow */
Simon Glassbb76a5c2023-08-24 13:55:40 -0600550 root = oftree_root(oftree_default());
551 node = ofnode_find_subnode(root, mmc_dev);
552 ut_assert(ofnode_valid(node));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600553 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
554
555 /* Enable the script bootmeth too */
556 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Simon Glass2f27e472023-08-19 16:49:35 -0600557 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_2script),
Simon Glassa59d6d32024-10-19 09:21:50 -0600558 "script", 0, ofnode_null(), &dev));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600559
Simon Glassfff928c2023-08-24 13:55:41 -0600560 /* Enable the cros bootmeth if needed */
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200561 if (IS_ENABLED(CONFIG_BOOTMETH_CROS) && bind_cros_android) {
Simon Glassfff928c2023-08-24 13:55:41 -0600562 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
563 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_cros),
564 "cros", 0, ofnode_null(), &dev));
565 }
566
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200567 /* Enable the android bootmeths if needed */
568 if (IS_ENABLED(CONFIG_BOOTMETH_ANDROID) && bind_cros_android) {
569 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
570 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_android),
571 "android", 0, ofnode_null(), &dev));
572 }
573
Simon Glassbb76a5c2023-08-24 13:55:40 -0600574 /* Change the order to include the device */
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600575 std = dev_get_priv(bootstd);
576 old_order = std->bootdev_order;
577 std->bootdev_order = order;
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600578 *old_orderp = old_order;
579
580 return 0;
581}
582
583/**
584 * scan_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
585 *
586 * @uts: Unit test state
587 * @mmc_dev: MMC device to use, e.g. "mmc4"
588 * @bind_cros: true to bind the ChromiumOS bootmeth
589 * Returns 0 on success, -ve on failure
590 */
591static int scan_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
592 bool bind_cros)
593{
594 struct bootstd_priv *std;
595 struct udevice *bootstd;
596 const char **old_order;
597
598 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, bind_cros, &old_order));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600599
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600600 ut_assertok(run_command("bootflow scan", 0));
601 ut_assert_console_end();
602
603 /* Restore the order used by the device tree */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600604 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
605 std = dev_get_priv(bootstd);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600606 std->bootdev_order = old_order;
607
Simon Glassd92bcc42023-01-06 08:52:42 -0600608 return 0;
609}
610
Simon Glassbb76a5c2023-08-24 13:55:40 -0600611/**
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200612 * scan_mmc_android_bootdev() - Set up an mmc bootdev so we can access other
613 * distros. Android bootflow might print "ANDROID:*" while scanning
614 *
615 * @uts: Unit test state
616 * @mmc_dev: MMC device to use, e.g. "mmc4"
617 * Returns 0 on success, -ve on failure
618 */
619static int scan_mmc_android_bootdev(struct unit_test_state *uts, const char *mmc_dev)
620{
621 struct bootstd_priv *std;
622 struct udevice *bootstd;
623 const char **old_order;
624
625 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, true, &old_order));
626
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200627 ut_assertok(run_command("bootflow scan", 0));
628 /* Android bootflow might print one or two 'ANDROID:*' logs */
629 ut_check_skipline(uts);
630 ut_check_skipline(uts);
631 ut_assert_console_end();
632
633 /* Restore the order used by the device tree */
634 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
635 std = dev_get_priv(bootstd);
636 std->bootdev_order = old_order;
637
638 return 0;
639}
640
641/**
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600642 * scan_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
Simon Glassbb76a5c2023-08-24 13:55:40 -0600643 *
644 * @uts: Unit test state
645 * Returns 0 on success, -ve on failure
646 */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600647static int scan_mmc4_bootdev(struct unit_test_state *uts)
Simon Glassbb76a5c2023-08-24 13:55:40 -0600648{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600649 ut_assertok(scan_mmc_bootdev(uts, "mmc4", false));
Simon Glassbb76a5c2023-08-24 13:55:40 -0600650
651 return 0;
652}
653
Simon Glassd92bcc42023-01-06 08:52:42 -0600654/* Check 'bootflow menu' to select a bootflow */
655static int bootflow_cmd_menu(struct unit_test_state *uts)
656{
Simon Glass6d5083b2023-10-01 19:14:38 -0600657 struct bootstd_priv *std;
Simon Glassd92bcc42023-01-06 08:52:42 -0600658 char prev[3];
659
Simon Glass6d5083b2023-10-01 19:14:38 -0600660 /* get access to the current bootflow */
661 ut_assertok(bootstd_get_priv(&std));
662
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600663 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600664
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600665 /* Add keypresses to move to and select the second one in the list */
666 prev[0] = CTL_CH('n');
667 prev[1] = '\r';
668 prev[2] = '\0';
669 ut_asserteq(2, console_in_puts(prev));
670
671 ut_assertok(run_command("bootflow menu", 0));
672 ut_assert_nextline("Selected: Armbian");
Simon Glass6d5083b2023-10-01 19:14:38 -0600673 ut_assertnonnull(std->cur_bootflow);
674 ut_assert_console_end();
675
676 /* Check not selecting anything */
677 prev[0] = '\e';
678 prev[1] = '\0';
679 ut_asserteq(1, console_in_puts(prev));
680
681 ut_asserteq(1, run_command("bootflow menu", 0));
682 ut_assertnull(std->cur_bootflow);
683 ut_assert_nextline("Nothing chosen");
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600684 ut_assert_console_end();
685
686 return 0;
687}
Simon Glassf0425022024-08-22 07:57:54 -0600688BOOTSTD_TEST(bootflow_cmd_menu, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassd92bcc42023-01-06 08:52:42 -0600689
Simon Glass9bf27862023-10-01 19:15:25 -0600690/* Check 'bootflow scan -m' to select a bootflow using a menu */
691static int bootflow_scan_menu(struct unit_test_state *uts)
692{
693 struct bootstd_priv *std;
694 const char **old_order, **new_order;
695 char prev[3];
696
697 /* get access to the current bootflow */
698 ut_assertok(bootstd_get_priv(&std));
699
700 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
701
702 /* Add keypresses to move to and select the second one in the list */
703 prev[0] = CTL_CH('n');
704 prev[1] = '\r';
705 prev[2] = '\0';
706 ut_asserteq(2, console_in_puts(prev));
707
708 ut_assertok(run_command("bootflow scan -lm", 0));
709 new_order = std->bootdev_order;
710 std->bootdev_order = old_order;
711
712 ut_assert_skip_to_line("No more bootdevs");
713 ut_assert_nextlinen("--");
714 ut_assert_nextline("(2 bootflows, 2 valid)");
715
716 ut_assert_nextline("Selected: Armbian");
717 ut_assertnonnull(std->cur_bootflow);
718 ut_assert_console_end();
719
720 /* Check not selecting anything */
721 prev[0] = '\e';
722 prev[1] = '\0';
723 ut_asserteq(1, console_in_puts(prev));
724
725 std->bootdev_order = new_order; /* Blue Monday */
726 ut_assertok(run_command("bootflow scan -lm", 0));
727 std->bootdev_order = old_order;
728
729 ut_assertnull(std->cur_bootflow);
730 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
731 ut_assert_nextline("Nothing chosen");
732 ut_assert_console_end();
733
734 return 0;
735}
Simon Glass11fcfa32024-08-22 07:57:50 -0600736BOOTSTD_TEST(bootflow_scan_menu, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass9bf27862023-10-01 19:15:25 -0600737
738/* Check 'bootflow scan -mb' to select and boot a bootflow using a menu */
739static int bootflow_scan_menu_boot(struct unit_test_state *uts)
740{
741 struct bootstd_priv *std;
742 const char **old_order;
743 char prev[3];
744
745 /* get access to the current bootflow */
746 ut_assertok(bootstd_get_priv(&std));
747
748 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
749
750 /* Add keypresses to move to and select the second one in the list */
751 prev[0] = CTL_CH('n');
752 prev[1] = '\r';
753 prev[2] = '\0';
754 ut_asserteq(2, console_in_puts(prev));
755
756 ut_assertok(run_command("bootflow scan -lmb", 0));
757 std->bootdev_order = old_order;
758
759 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
760
761 ut_assert_nextline("Selected: Armbian");
Francis Laniel5b64c452023-12-22 22:02:35 +0100762
763 if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
764 /*
765 * With old hush, despite booti failing to boot, i.e. returning
766 * CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as
767 * we are using bootmeth_script here, to return -EFAULT.
768 */
769 ut_assert_skip_to_line("Boot failed (err=-14)");
770 } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
771 /*
772 * While with modern one, run_command() propagates CMD_RET_FAILURE returned
773 * by booti, so we get 1 here.
774 */
775 ut_assert_skip_to_line("Boot failed (err=1)");
776 }
Simon Glass9bf27862023-10-01 19:15:25 -0600777 ut_assertnonnull(std->cur_bootflow);
778 ut_assert_console_end();
779
780 return 0;
781}
Simon Glass11fcfa32024-08-22 07:57:50 -0600782BOOTSTD_TEST(bootflow_scan_menu_boot, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass9bf27862023-10-01 19:15:25 -0600783
Simon Glassba3d5372023-01-17 10:48:15 -0700784/* Check searching for a single bootdev using the hunters */
785static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
786{
787 struct bootstd_priv *std;
788
789 /* get access to the used hunters */
790 ut_assertok(bootstd_get_priv(&std));
791
792 ut_assertok(bootstd_test_drop_bootdev_order(uts));
793
Simon Glassba3d5372023-01-17 10:48:15 -0700794 ut_assertok(run_command("bootflow scan -l mmc1", 0));
795 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
796 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
797 ut_assert_console_end();
798
799 /* check that the hunter was used */
800 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
801
802 return 0;
803}
Simon Glassf0425022024-08-22 07:57:54 -0600804BOOTSTD_TEST(bootflow_cmd_hunt_single, UTF_DM | UTF_SCAN_FDT |
805 UTF_CONSOLE);
Simon Glassba3d5372023-01-17 10:48:15 -0700806
807/* Check searching for a uclass label using the hunters */
808static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
809{
810 struct bootstd_priv *std;
811
812 /* get access to the used hunters */
813 ut_assertok(bootstd_get_priv(&std));
814
815 test_set_skip_delays(true);
816 test_set_eth_enable(false);
817 ut_assertok(bootstd_test_drop_bootdev_order(uts));
818
Simon Glassba3d5372023-01-17 10:48:15 -0700819 ut_assertok(run_command("bootflow scan -l mmc", 0));
820
821 /* check that the hunter was used */
822 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
823
824 /* check that we got the mmc1 bootflow */
825 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
826 ut_assert_nextlinen("Seq");
827 ut_assert_nextlinen("---");
828 ut_assert_nextline("Hunting with: simple_bus");
829 ut_assert_nextline("Found 2 extension board(s).");
830 ut_assert_nextline("Hunting with: mmc");
831 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
832 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
833 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600834 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassba3d5372023-01-17 10:48:15 -0700835 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
836 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
837 ut_assert_console_end();
838
839 return 0;
840}
Simon Glassf0425022024-08-22 07:57:54 -0600841BOOTSTD_TEST(bootflow_cmd_hunt_label, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassba3d5372023-01-17 10:48:15 -0700842
Simon Glassd92bcc42023-01-06 08:52:42 -0600843/**
844 * check_font() - Check that the font size for an item matches expectations
845 *
846 * @uts: Unit test state
847 * @scn: Scene containing the text object
848 * @id: ID of the text object
849 * Returns 0 on success, -ve on failure
850 */
851static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
852 int font_size)
853{
854 struct scene_obj_txt *txt;
855
856 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
857 ut_assertnonnull(txt);
858
859 ut_asserteq(font_size, txt->font_size);
860
861 return 0;
862}
863
864/* Check themes work with a bootflow menu */
865static int bootflow_menu_theme(struct unit_test_state *uts)
866{
867 const int font_size = 30;
868 struct scene *scn;
869 struct expo *exp;
870 ofnode node;
871 int i;
872
Marek Vasutabb772a2024-10-20 17:43:12 +0200873 if (!CONFIG_IS_ENABLED(BOOTSTD_MENU))
874 return -EAGAIN;
875
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600876 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600877
878 ut_assertok(bootflow_menu_new(&exp));
879 node = ofnode_path("/bootstd/theme");
880 ut_assert(ofnode_valid(node));
881 ut_assertok(bootflow_menu_apply_theme(exp, node));
882
883 scn = expo_lookup_scene_id(exp, MAIN);
884 ut_assertnonnull(scn);
885
886 /*
887 * Check that the txt objects have the correct font size from the
888 * device tree node: bootstd/theme
889 *
890 * Check both menu items, since there are two bootflows
891 */
892 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
893 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
894 for (i = 0; i < 2; i++) {
895 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
896 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
897 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
898 }
899
900 expo_destroy(exp);
901
902 return 0;
903}
Simon Glassf0425022024-08-22 07:57:54 -0600904BOOTSTD_TEST(bootflow_menu_theme, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassa08adca2023-07-12 09:04:38 -0600905
906/**
907 * check_arg() - Check both the normal case and the buffer-overflow case
908 *
909 * @uts: Unit-test state
910 * @expect_ret: Expected return value (i.e. buffer length)
911 * @expect_str: String expected to be returned
912 * @buf: Buffer to use
913 * @from: Original cmdline to update
914 * @arg: Argument to update (e.g. "console")
915 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
916 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
917 * it without any value ("initrd")
918 */
919static int check_arg(struct unit_test_state *uts, int expect_ret,
920 const char *expect_str, char *buf, const char *from,
921 const char *arg, const char *val)
922{
923 /* check for writing outside the reported bounds */
924 buf[expect_ret] = '[';
925 ut_asserteq(expect_ret,
926 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
927 ut_asserteq_str(expect_str, buf);
928 ut_asserteq('[', buf[expect_ret]);
929
930 /* do the test again but with one less byte in the buffer */
931 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
932 val, NULL));
933
934 return 0;
935}
936
937/* Test of bootflow_cmdline_set_arg() */
938static int test_bootflow_cmdline_set(struct unit_test_state *uts)
939{
940 char buf[50];
941 const int size = sizeof(buf);
942
943 /*
944 * note that buffer-overflow tests are immediately each test case, just
945 * top keep the code together
946 */
947
948 /* add an arg that doesn't already exist, starting from empty */
949 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
950 NULL));
951
952 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
953 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
954 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
955
956 /* add an arg that doesn't already exist, starting from non-empty */
957 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
958 BOOTFLOWCL_EMPTY));
959 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
960 ""));
961 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
962 "fred"));
963
964 /* update an arg at the start */
965 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
966 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
967 BOOTFLOWCL_EMPTY));
968 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
969 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
970 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
971 "1234"));
972
973 /* update an arg at the end */
974 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
975 NULL));
976 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
977 BOOTFLOWCL_EMPTY));
978 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
979 ""));
980 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
981 "1"));
982 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
983 "arg", "1234"));
984
985 /* update an arg in the middle */
986 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
987 "mary=abc arg=123 john=2", "arg", NULL));
988 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
989 "mary=abc arg=123 john=2", "arg",
990 BOOTFLOWCL_EMPTY));
991 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
992 "mary=abc arg=123 john=2", "arg", ""));
993 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
994 "mary=abc arg=123 john=2", "arg", "1"));
995 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
996 "mary=abc arg=123 john=2", "arg", "1234"));
997
998 /* handle existing args with quotes */
999 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
1000 "mary=\"abc\" arg=123 john", "arg", NULL));
1001
1002 /* handle existing args with quoted spaces */
1003 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
1004 "mary=\"abc def\" arg=123 john", "arg", NULL));
1005
1006 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
1007 buf, "mary=\"abc def\" arg=123 john", "def",
1008 "4"));
1009
1010 /* quote at the start */
1011 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1012 "mary=\"abc def\" arg=\"123 456\"",
1013 "arg", "\"4 5 6", NULL));
1014
1015 /* quote at the end */
1016 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1017 "mary=\"abc def\" arg=\"123 456\"",
1018 "arg", "4 5 6\"", NULL));
1019
1020 /* quote in the middle */
1021 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1022 "mary=\"abc def\" arg=\"123 456\"",
1023 "arg", "\"4 \"5 6\"", NULL));
1024
1025 /* handle updating a quoted arg */
1026 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
1027 "mary=\"abc def\" arg=\"123 456\"", "arg",
1028 "4 5 6"));
1029
1030 /* changing a quoted arg to a non-quoted arg */
1031 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
1032 "mary=\"abc def\" arg=\"123 456\"", "arg",
1033 "789"));
1034
1035 /* changing a non-quoted arg to a quoted arg */
1036 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
1037 "mary=\"abc def\" arg=123", "arg", "456 789"));
1038
1039 /* handling of spaces */
1040 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1041 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1042 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
1043 "123"));
1044 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
1045 "arg", "123"));
1046 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
1047 " john arg=123 mary ", "arg", "123"));
1048
1049 /* unchanged arg */
1050 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
1051
1052 /* arg which starts with the same name */
1053 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
1054 "mary=abc johnathon=2 john=1", "john", "3"));
1055
1056 return 0;
1057}
1058BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
Simon Glass55a2da32023-07-12 09:04:39 -06001059
1060/* Test of bootflow_cmdline_set_arg() */
1061static int bootflow_set_arg(struct unit_test_state *uts)
1062{
1063 struct bootflow s_bflow, *bflow = &s_bflow;
1064 ulong mem_start;
1065
1066 ut_assertok(env_set("bootargs", NULL));
1067
1068 mem_start = ut_check_delta(0);
1069
1070 /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
1071 bflow->cmdline = NULL;
1072 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
1073 ut_asserteq_str(bflow->cmdline, "fred=123");
1074
1075 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
1076 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
1077
1078 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
1079 ut_asserteq_str(bflow->cmdline, "fred=123");
1080 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
1081 ut_asserteq_ptr(bflow->cmdline, NULL);
1082
1083 ut_asserteq(0, ut_check_delta(mem_start));
1084
1085 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
1086 ut_asserteq_str("mary=here", env_get("bootargs"));
1087 ut_assertok(env_set("bootargs", NULL));
1088
1089 return 0;
1090}
1091BOOTSTD_TEST(bootflow_set_arg, 0);
1092
1093/* Test of bootflow_cmdline_get_arg() */
1094static int bootflow_cmdline_get(struct unit_test_state *uts)
1095{
1096 int pos;
1097
1098 /* empty string */
1099 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
1100
1101 /* arg with empty value */
1102 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
1103 ut_asserteq(5, pos);
1104
1105 /* arg with a value */
1106 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
1107 ut_asserteq(5, pos);
1108
1109 /* arg with a value */
1110 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
1111 ut_asserteq(12, pos);
1112
1113 /* arg with a value, after quoted arg */
1114 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
1115 ut_asserteq(16, pos);
1116
1117 /* arg in the middle */
1118 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
1119 &pos));
1120 ut_asserteq(15, pos);
1121
1122 /* quoted arg */
1123 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
1124 "fred", &pos));
1125 ut_asserteq(17, pos);
1126
1127 /* args starting with the same prefix */
1128 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
1129 &pos));
1130 ut_asserteq(26, pos);
1131
1132 return 0;
1133}
1134BOOTSTD_TEST(bootflow_cmdline_get, 0);
1135
1136static int bootflow_cmdline(struct unit_test_state *uts)
1137{
1138 ut_assertok(run_command("bootflow scan mmc", 0));
1139 ut_assertok(run_command("bootflow sel 0", 0));
Simon Glass55a2da32023-07-12 09:04:39 -06001140
1141 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1142 ut_assert_nextline("Argument not found");
1143 ut_assert_console_end();
1144
1145 ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
1146 ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
1147 ut_assert_nextline("123");
1148
1149 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1150 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1151 ut_assert_nextline("abc");
1152
1153 ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
1154 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1155 ut_assert_nextline("Argument not found");
1156
1157 ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
1158 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1159 ut_assert_nextline_empty();
1160
Simon Glass7a164f82023-11-29 10:31:19 -07001161 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1162 ut_asserteq(0, run_command("bootflow cmdline set mary", 0));
Simon Glass55a2da32023-07-12 09:04:39 -06001163 ut_assert_console_end();
1164
1165 return 0;
1166}
Simon Glassf0425022024-08-22 07:57:54 -06001167BOOTSTD_TEST(bootflow_cmdline, UTF_CONSOLE);
Simon Glassfff928c2023-08-24 13:55:41 -06001168
Simon Glassa61057f2023-10-25 07:17:36 +13001169/* test a few special changes to a long command line */
1170static int bootflow_cmdline_special(struct unit_test_state *uts)
1171{
1172 char buf[500];
1173 int pos;
1174
1175 /*
1176 * check handling of an argument which has an embedded '=', as well as
1177 * handling of a argument which partially matches ("ro" and "root")
1178 */
1179 ut_asserteq(32, cmdline_set_arg(
1180 buf, sizeof(buf),
1181 "loglevel=7 root=PARTUUID=d68352e3 rootwait ro noinitrd",
1182 "root", NULL, &pos));
1183 ut_asserteq_str("loglevel=7 rootwait ro noinitrd", buf);
1184
1185 return 0;
1186}
1187BOOTSTD_TEST(bootflow_cmdline_special, 0);
1188
Simon Glassfff928c2023-08-24 13:55:41 -06001189/* Test ChromiumOS bootmeth */
1190static int bootflow_cros(struct unit_test_state *uts)
1191{
Simon Glass3ff8a9a2023-10-01 19:14:37 -06001192 ut_assertok(scan_mmc_bootdev(uts, "mmc5", true));
Simon Glassfff928c2023-08-24 13:55:41 -06001193 ut_assertok(run_command("bootflow list", 0));
1194
1195 ut_assert_nextlinen("Showing all");
1196 ut_assert_nextlinen("Seq");
1197 ut_assert_nextlinen("---");
1198 ut_assert_nextlinen(" 0 extlinux");
Simon Glassd7d3a972023-08-24 13:55:45 -06001199 ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 ");
1200 ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 ");
Simon Glassfff928c2023-08-24 13:55:41 -06001201 ut_assert_nextlinen("---");
Simon Glassd7d3a972023-08-24 13:55:45 -06001202 ut_assert_skip_to_line("(3 bootflows, 3 valid)");
Simon Glassfff928c2023-08-24 13:55:41 -06001203
Simon Glassc6a52e72024-11-15 16:19:23 -07001204 ut_assertok(run_command("bootstd images", 0));
1205 ut_assert_nextlinen("Seq");
1206 ut_assert_nextlinen("---");
1207 ut_assert_nextlinen(" 0 mmc1.bootdev.part_1 extlinux_cfg");
1208 ut_assert_nextlinen(" 1 mmc5.bootdev.part_2 x86_setup");
1209 ut_assert_nextlinen(" 1 mmc5.bootdev.part_2 cmdline");
1210 ut_assert_nextlinen(" 1 mmc5.bootdev.part_2 kernel - 4000 kernel");
1211 ut_assert_nextlinen(" 2 mmc5.bootdev.part_4 x86_setup");
1212 ut_assert_nextlinen(" 2 mmc5.bootdev.part_4 cmdline");
1213 ut_assert_nextlinen(" 2 mmc5.bootdev.part_4 kernel - 4000 kernel");
1214 ut_assert_nextlinen("---");
1215 ut_assert_nextline("(7 images)");
1216
Simon Glassfff928c2023-08-24 13:55:41 -06001217 ut_assert_console_end();
1218
1219 return 0;
1220}
Mattijs Korpershoek33342f02024-11-22 15:11:34 +01001221BOOTSTD_TEST(bootflow_cros, UTF_CONSOLE | UTF_DM | UTF_SCAN_FDT);
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +02001222
Guillaume La Roque368ad9e2024-11-26 09:06:13 +01001223/* Test Android bootmeth with boot image version 4 */
1224static int bootflow_android_image_v4(struct unit_test_state *uts)
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +02001225{
1226 if (!IS_ENABLED(CONFIG_BOOTMETH_ANDROID))
1227 return -EAGAIN;
1228
1229 ut_assertok(scan_mmc_android_bootdev(uts, "mmc7"));
1230 ut_assertok(run_command("bootflow list", 0));
1231
1232 ut_assert_nextlinen("Showing all");
1233 ut_assert_nextlinen("Seq");
1234 ut_assert_nextlinen("---");
1235 ut_assert_nextlinen(" 0 extlinux");
1236 ut_assert_nextlinen(" 1 android ready mmc 0 mmc7.bootdev.whole ");
1237 ut_assert_nextlinen("---");
1238 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
1239
1240 ut_assert_console_end();
1241
1242 return 0;
1243}
Guillaume La Roque368ad9e2024-11-26 09:06:13 +01001244BOOTSTD_TEST(bootflow_android_image_v4, UTF_CONSOLE | UTF_DM | UTF_SCAN_FDT);
1245
1246/* Test Android bootmeth with boot image version 2 */
1247static int bootflow_android_image_v2(struct unit_test_state *uts)
1248{
1249 if (!IS_ENABLED(CONFIG_BOOTMETH_ANDROID))
1250 return -EAGAIN;
1251
1252 ut_assertok(scan_mmc_android_bootdev(uts, "mmc8"));
1253 ut_assertok(run_command("bootflow list", 0));
1254
1255 ut_assert_nextlinen("Showing all");
1256 ut_assert_nextlinen("Seq");
1257 ut_assert_nextlinen("---");
1258 ut_assert_nextlinen(" 0 extlinux");
1259 ut_assert_nextlinen(" 1 android ready mmc 0 mmc8.bootdev.whole ");
1260 ut_assert_nextlinen("---");
1261 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
1262
1263 ut_assert_console_end();
1264
1265 return 0;
1266}
1267BOOTSTD_TEST(bootflow_android_image_v2, UTF_CONSOLE | UTF_DM | UTF_SCAN_FDT);
Simon Glass4f378de2024-11-07 14:31:50 -07001268
1269/* Test EFI bootmeth */
1270static int bootflow_efi(struct unit_test_state *uts)
1271{
1272 static const char *order[] = {"mmc1", "usb", NULL};
1273 struct bootstd_priv *std;
1274 struct udevice *bootstd;
1275 const char **old_order;
1276
1277 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
1278 std = dev_get_priv(bootstd);
1279 old_order = std->bootdev_order;
1280 std->bootdev_order = order;
1281
1282 /* disable ethernet since the hunter will run dhcp */
1283 test_set_eth_enable(false);
1284
1285 /* make USB scan without delays */
1286 test_set_skip_delays(true);
1287
1288 bootstd_reset_usb();
1289
1290 ut_assertok(run_command("bootflow scan", 0));
1291 ut_assert_skip_to_line(
1292 "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
1293
1294 ut_assertok(run_command("bootflow list", 0));
1295
1296 ut_assert_nextlinen("Showing all");
1297 ut_assert_nextlinen("Seq");
1298 ut_assert_nextlinen("---");
1299 ut_assert_nextlinen(" 0 extlinux");
1300 ut_assert_nextlinen(
1301 " 1 efi ready usb_mass_ 1 usb_mass_storage.lun0.boo /EFI/BOOT/BOOTSBOX.EFI");
1302 ut_assert_nextlinen("---");
1303 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
1304 ut_assert_console_end();
1305
1306 ut_assertok(run_command("bootflow select 1", 0));
1307 ut_assert_console_end();
1308
1309 systab.fw_vendor = test_vendor;
1310
1311 ut_asserteq(1, run_command("bootflow boot", 0));
1312 ut_assert_nextline(
1313 "** Booting bootflow 'usb_mass_storage.lun0.bootdev.part_1' with efi");
1314 if (IS_ENABLED(CONFIG_LOGF_FUNC))
1315 ut_assert_skip_to_line(" efi_run_image() Booting /\\EFI\\BOOT\\BOOTSBOX.EFI");
1316 else
1317 ut_assert_skip_to_line("Booting /\\EFI\\BOOT\\BOOTSBOX.EFI");
1318
1319 /* TODO: Why the \r ? */
1320 ut_assert_nextline("U-Boot test app for EFI_LOADER\r");
1321 ut_assert_nextline("Exiting test app");
1322 ut_assert_nextline("Boot failed (err=-14)");
1323
1324 ut_assert_console_end();
1325
1326 ut_assertok(bootstd_test_drop_bootdev_order(uts));
1327
1328 return 0;
1329}
1330BOOTSTD_TEST(bootflow_efi, UTF_CONSOLE);
Simon Glassc6a52e72024-11-15 16:19:23 -07001331
1332/* Check 'bootflow scan' provides a list of images */
1333static int bootstd_images(struct unit_test_state *uts)
1334{
1335 static const char *order[] = {"mmc2", "mmc1", "mmc4", "mmc5", NULL};
1336 const struct legacy_img_hdr *hdr;
1337 const struct bootflow_img *img;
1338 const struct bootflow *bflow;
1339 struct bootstd_priv *std;
1340 const char **old_order;
1341 struct udevice *dev;
1342 ofnode root, node;
1343 ulong data, len;
1344 char *ptr;
1345
1346 /* get access to the current bootflow */
1347 ut_assertok(bootstd_get_priv(&std));
1348
1349 ut_assertok(prep_mmc_bootdev(uts, "mmc4", true, &old_order));
1350
1351 /* bind mmc5 too, for cros */
1352 root = oftree_root(oftree_default());
1353 node = ofnode_find_subnode(root, "mmc5");
1354 ut_assert(ofnode_valid(node));
1355 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
1356
1357 std->bootdev_order = order;
1358 ut_assertok(run_command("bootflow scan", 0));
1359 ut_assert_console_end();
1360 std->bootdev_order = old_order;
1361
1362 ut_assertok(run_command("bootflow list", 0));
1363 ut_assert_skip_to_line("(4 bootflows, 4 valid)");
1364
1365 ut_assertok(run_command("bootstd images", 0));
1366 ut_assert_nextlinen("Seq");
1367 ut_assert_nextlinen("---");
1368 ut_assert_nextlinen(" 0 mmc1.bootdev.part_1 extlinux_cfg");
1369 ut_assert_nextlinen(" 1 mmc4.bootdev.part_1 script");
1370 ut_assert_nextlinen(" 1 mmc4.bootdev.part_1 logo");
1371 ut_assert_nextlinen(" 2 mmc5.bootdev.part_2 x86_setup");
1372 ut_assert_nextlinen(" 2 mmc5.bootdev.part_2 cmdline");
1373 ut_assert_nextlinen(" 2 mmc5.bootdev.part_2 kernel -");
1374 ut_assert_nextlinen(" 3 mmc5.bootdev.part_4 x86_setup");
1375 ut_assert_nextlinen(" 3 mmc5.bootdev.part_4 cmdline");
1376 ut_assert_nextlinen(" 3 mmc5.bootdev.part_4 kernel -");
1377 ut_assert_nextlinen("---");
1378 ut_assert_nextline("(9 images)");
1379
1380 /* check the first image */
1381 bflow = alist_get(&std->bootflows, 0, struct bootflow);
1382 img = alist_get(&bflow->images, 0, struct bootflow_img);
1383 ut_asserteq_strn("# extlinux.conf", map_sysmem(img->addr, 0));
1384
1385 /* check the second image */
1386 bflow = alist_get(&std->bootflows, 1, struct bootflow);
1387 img = alist_get(&bflow->images, 0, struct bootflow_img);
1388
1389 /* this is the length of the script in bytes */
1390 hdr = map_sysmem(img->addr, 0);
1391 image_multi_getimg(hdr, 0, &data, &len);
1392 ptr = (void *)data;
1393 ut_asserteq_strn("# DO NOT EDIT THIS FILE", ptr);
1394
1395 /* check the ChromiumOS images */
1396 bflow = alist_get(&std->bootflows, 2, struct bootflow);
1397 img = alist_get(&bflow->images, 1, struct bootflow_img);
1398 ptr = map_sysmem(img->addr, 0);
1399 ut_asserteq_strn("BOOT_IMAGE=/vmlinuz-5.15.0-121-generic root=", ptr);
1400
1401 /*
1402 * the x86 setup is not a real binary, so just check that it is empty,
1403 * so that if this changes in the future someone will notice and update
1404 * this test
1405 */
1406 img = alist_get(&bflow->images, 0, struct bootflow_img);
1407 ptr = map_sysmem(img->addr, 0);
1408 ut_asserteq(0, *(ulong *)ptr);
1409
1410 ut_assert_console_end();
1411
1412 return 0;
1413}
1414BOOTSTD_TEST(bootstd_images, UTF_CONSOLE);