blob: 8b46256fa48db72fb2aeabf3d5ac99b410a31735 [file] [log] [blame]
Simon Glassb255efc2022-04-24 23:31:24 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Test for bootdev functions. All start with 'bootdev'
4 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
Simon Glassb255efc2022-04-24 23:31:24 -06009#include <bootdev.h>
10#include <bootflow.h>
Simon Glassc8d37212022-07-30 15:52:34 -060011#include <bootmeth.h>
Simon Glassb255efc2022-04-24 23:31:24 -060012#include <bootstd.h>
Simon Glassd2bc33ed2023-01-06 08:52:41 -060013#include <cli.h>
Simon Glassb255efc2022-04-24 23:31:24 -060014#include <dm.h>
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +020015#include <efi_default_filename.h>
Simon Glassd92bcc42023-01-06 08:52:42 -060016#include <expo.h>
Simon Glass90b643d2022-07-30 15:52:36 -060017#ifdef CONFIG_SANDBOX
Simon Glass161e1e32022-07-30 15:52:22 -060018#include <asm/test.h>
Simon Glass90b643d2022-07-30 15:52:36 -060019#endif
Simon Glassd2bc33ed2023-01-06 08:52:41 -060020#include <dm/device-internal.h>
Simon Glassb255efc2022-04-24 23:31:24 -060021#include <dm/lists.h>
22#include <test/suites.h>
23#include <test/ut.h>
24#include "bootstd_common.h"
Simon Glassd92bcc42023-01-06 08:52:42 -060025#include "../../boot/bootflow_internal.h"
26#include "../../boot/scene_internal.h"
Simon Glassb255efc2022-04-24 23:31:24 -060027
Simon Glassd2bc33ed2023-01-06 08:52:41 -060028DECLARE_GLOBAL_DATA_PTR;
29
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +020030extern U_BOOT_DRIVER(bootmeth_android);
Simon Glassfff928c2023-08-24 13:55:41 -060031extern U_BOOT_DRIVER(bootmeth_cros);
Simon Glass2f27e472023-08-19 16:49:35 -060032extern U_BOOT_DRIVER(bootmeth_2script);
Simon Glassd2bc33ed2023-01-06 08:52:41 -060033
Simon Glass49ad1d82022-07-30 15:52:16 -060034static int inject_response(struct unit_test_state *uts)
35{
36 /*
37 * The image being booted presents a menu of options:
38 *
39 * Fedora-Workstation-armhfp-31-1.9 Boot Options.
40 * 1: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
41 * Enter choice:
42 *
43 * Provide input for this, to avoid waiting two seconds for a timeout.
44 */
45 ut_asserteq(2, console_in_puts("1\n"));
46
47 return 0;
48}
49
Simon Glassb255efc2022-04-24 23:31:24 -060050/* Check 'bootflow scan/list' commands */
51static int bootflow_cmd(struct unit_test_state *uts)
52{
53 console_record_reset_enable();
54 ut_assertok(run_command("bootdev select 1", 0));
55 ut_assert_console_end();
Simon Glasse4cf1062023-01-17 10:48:13 -070056 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060057 ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
58 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
59 ut_assert_nextlinen("---");
Simon Glass484e4072023-01-17 10:48:14 -070060 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
61 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassb71d7f72023-05-10 16:34:46 -060062 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass484e4072023-01-17 10:48:14 -070063 ut_assert_nextline("No more bootdevs");
Simon Glassb255efc2022-04-24 23:31:24 -060064 ut_assert_nextlinen("---");
65 ut_assert_nextline("(1 bootflow, 1 valid)");
66 ut_assert_console_end();
67
68 ut_assertok(run_command("bootflow list", 0));
69 ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'");
70 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
71 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060072 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -060073 ut_assert_nextlinen("---");
74 ut_assert_nextline("(1 bootflow, 1 valid)");
75 ut_assert_console_end();
76
77 return 0;
78}
79BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
80
Simon Glass484e4072023-01-17 10:48:14 -070081/* Check 'bootflow scan' with a label / seq */
Simon Glassb255efc2022-04-24 23:31:24 -060082static int bootflow_cmd_label(struct unit_test_state *uts)
83{
Simon Glass484e4072023-01-17 10:48:14 -070084 test_set_eth_enable(false);
85
Simon Glassb255efc2022-04-24 23:31:24 -060086 console_record_reset_enable();
Simon Glasse4cf1062023-01-17 10:48:13 -070087 ut_assertok(run_command("bootflow scan -lH mmc1", 0));
Simon Glass484e4072023-01-17 10:48:14 -070088 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
Simon Glassb255efc2022-04-24 23:31:24 -060089 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
90 ut_assert_console_end();
91
Simon Glass484e4072023-01-17 10:48:14 -070092 ut_assertok(run_command("bootflow scan -lH 0", 0));
93 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassb255efc2022-04-24 23:31:24 -060094 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
95 ut_assert_console_end();
96
Simon Glass484e4072023-01-17 10:48:14 -070097 /*
98 * with ethernet enabled we have 8 devices ahead of the mmc ones:
99 *
100 * ut_assertok(run_command("bootdev list", 0));
101 * Seq Probed Status Uclass Name
102 * --- ------ ------ -------- ------------------
103 * 0 [ + ] OK ethernet eth@10002000.bootdev
104 * 1 [ ] OK ethernet eth@10003000.bootdev
105 * 2 [ ] OK ethernet sbe5.bootdev
106 * 3 [ ] OK ethernet eth@10004000.bootdev
107 * 4 [ ] OK ethernet phy-test-eth.bootdev
108 * 5 [ ] OK ethernet dsa-test-eth.bootdev
109 * 6 [ ] OK ethernet dsa-test@0.bootdev
110 * 7 [ ] OK ethernet dsa-test@1.bootdev
111 * 8 [ ] OK mmc mmc2.bootdev
112 * 9 [ + ] OK mmc mmc1.bootdev
113 * a [ ] OK mmc mmc0.bootdev
114 */
115 ut_assertok(run_command("bootflow scan -lH 9", 0));
116 ut_assert_nextline("Scanning for bootflows with label '9'");
117 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
118
Simon Glasse4cf1062023-01-17 10:48:13 -0700119 ut_assertok(run_command("bootflow scan -lH 0", 0));
Simon Glass484e4072023-01-17 10:48:14 -0700120 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassb255efc2022-04-24 23:31:24 -0600121 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
122 ut_assert_console_end();
123
124 return 0;
125}
Simon Glass484e4072023-01-17 10:48:14 -0700126BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
127 UT_TESTF_ETH_BOOTDEV);
Simon Glassb255efc2022-04-24 23:31:24 -0600128
129/* Check 'bootflow scan/list' commands using all bootdevs */
130static int bootflow_cmd_glob(struct unit_test_state *uts)
131{
132 ut_assertok(bootstd_test_drop_bootdev_order(uts));
133
134 console_record_reset_enable();
Simon Glasse4cf1062023-01-17 10:48:13 -0700135 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600136 ut_assert_nextline("Scanning for bootflows in all bootdevs");
137 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
138 ut_assert_nextlinen("---");
139 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
140 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassb71d7f72023-05-10 16:34:46 -0600141 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -0600142 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
143 ut_assert_nextline("No more bootdevs");
144 ut_assert_nextlinen("---");
145 ut_assert_nextline("(1 bootflow, 1 valid)");
146 ut_assert_console_end();
147
148 ut_assertok(run_command("bootflow list", 0));
149 ut_assert_nextline("Showing all bootflows");
150 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
151 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -0600152 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassb255efc2022-04-24 23:31:24 -0600153 ut_assert_nextlinen("---");
154 ut_assert_nextline("(1 bootflow, 1 valid)");
155 ut_assert_console_end();
156
157 return 0;
158}
159BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
160
161/* Check 'bootflow scan -e' */
162static int bootflow_cmd_scan_e(struct unit_test_state *uts)
163{
164 ut_assertok(bootstd_test_drop_bootdev_order(uts));
165
166 console_record_reset_enable();
Simon Glasse4cf1062023-01-17 10:48:13 -0700167 ut_assertok(run_command("bootflow scan -aleGH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600168 ut_assert_nextline("Scanning for bootflows in all bootdevs");
169 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
170 ut_assert_nextlinen("---");
171 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600172 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600173 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassd465ad52023-08-24 13:55:39 -0600174 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600175 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassb255efc2022-04-24 23:31:24 -0600176
177 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600178 ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600179 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassd465ad52023-08-24 13:55:39 -0600180 ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600181 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassb71d7f72023-05-10 16:34:46 -0600182 ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +0200183 ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 /EFI/BOOT/"
184 BOOTEFI_NAME);
Simon Glassb255efc2022-04-24 23:31:24 -0600185
186 ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
Simon Glassd465ad52023-08-24 13:55:39 -0600187 ut_assert_skip_to_line(
188 " 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glass9482fdf2023-05-10 16:34:26 -0600189 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassb255efc2022-04-24 23:31:24 -0600190 ut_assert_nextline("No more bootdevs");
191 ut_assert_nextlinen("---");
192 ut_assert_nextline("(64 bootflows, 1 valid)");
193 ut_assert_console_end();
194
195 ut_assertok(run_command("bootflow list", 0));
196 ut_assert_nextline("Showing all bootflows");
197 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
198 ut_assert_nextlinen("---");
Simon Glassd465ad52023-08-24 13:55:39 -0600199 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
200 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
201 ut_assert_skip_to_line(
202 " 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
203 ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glassb255efc2022-04-24 23:31:24 -0600204 ut_assert_nextlinen("---");
205 ut_assert_nextline("(64 bootflows, 1 valid)");
206 ut_assert_console_end();
207
208 return 0;
209}
210BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
211
212/* Check 'bootflow info' */
213static int bootflow_cmd_info(struct unit_test_state *uts)
214{
215 console_record_reset_enable();
216 ut_assertok(run_command("bootdev select 1", 0));
217 ut_assert_console_end();
218 ut_assertok(run_command("bootflow scan", 0));
219 ut_assert_console_end();
220 ut_assertok(run_command("bootflow select 0", 0));
221 ut_assert_console_end();
222 ut_assertok(run_command("bootflow info", 0));
223 ut_assert_nextline("Name: mmc1.bootdev.part_1");
224 ut_assert_nextline("Device: mmc1.bootdev");
225 ut_assert_nextline("Block dev: mmc1.blk");
Simon Glassb71d7f72023-05-10 16:34:46 -0600226 ut_assert_nextline("Method: extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600227 ut_assert_nextline("State: ready");
228 ut_assert_nextline("Partition: 1");
229 ut_assert_nextline("Subdir: (none)");
230 ut_assert_nextline("Filename: /extlinux/extlinux.conf");
231 ut_assert_nextlinen("Buffer: ");
232 ut_assert_nextline("Size: 253 (595 bytes)");
Simon Glass72b7b192023-01-06 08:52:33 -0600233 ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
Simon Glass33927522023-07-12 09:04:34 -0600234 ut_assert_nextline("Cmdline: (none)");
Simon Glass612b9cc2023-01-06 08:52:34 -0600235 ut_assert_nextline("Logo: (none)");
Simon Glass7b8c6342023-01-17 10:47:56 -0700236 ut_assert_nextline("FDT: <NULL>");
Simon Glassb255efc2022-04-24 23:31:24 -0600237 ut_assert_nextline("Error: 0");
238 ut_assert_console_end();
239
240 ut_assertok(run_command("bootflow info -d", 0));
241 ut_assert_nextline("Name: mmc1.bootdev.part_1");
242 ut_assert_skip_to_line("Error: 0");
243 ut_assert_nextline("Contents:");
244 ut_assert_nextline("%s", "");
245 ut_assert_nextline("# extlinux.conf generated by appliance-creator");
246 ut_assert_skip_to_line(" initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
247 ut_assert_console_end();
248
249 return 0;
250}
251BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
252
253/* Check 'bootflow scan -b' to boot the first available bootdev */
254static int bootflow_scan_boot(struct unit_test_state *uts)
255{
256 console_record_reset_enable();
Simon Glass49ad1d82022-07-30 15:52:16 -0600257 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600258 ut_assertok(run_command("bootflow scan -b", 0));
259 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600260 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassb255efc2022-04-24 23:31:24 -0600261 ut_assert_nextline("Ignoring unknown command: ui");
262
263 /*
264 * We expect it to get through to boot although sandbox always returns
265 * -EFAULT as it cannot actually boot the kernel
266 */
267 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
268 ut_assert_nextline("Boot failed (err=-14)");
269 ut_assert_console_end();
270
271 return 0;
272}
273BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
274
275/* Check iterating through available bootflows */
276static int bootflow_iter(struct unit_test_state *uts)
277{
278 struct bootflow_iter iter;
279 struct bootflow bflow;
280
281 bootstd_clear_glob();
282
283 /* The first device is mmc2.bootdev which has no media */
284 ut_asserteq(-EPROTONOSUPPORT,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700285 bootflow_scan_first(NULL, NULL, &iter,
Simon Glass99e68182023-02-22 12:17:03 -0700286 BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600287 ut_asserteq(2, iter.num_methods);
288 ut_asserteq(0, iter.cur_method);
289 ut_asserteq(0, iter.part);
290 ut_asserteq(0, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600291 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600292 ut_asserteq(0, bflow.err);
293
294 /*
Simon Glassc8d37212022-07-30 15:52:34 -0600295 * This shows MEDIA even though there is none, since in
Simon Glassb255efc2022-04-24 23:31:24 -0600296 * bootdev_find_in_blk() we call part_get_info() which returns
297 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
298 * know.
299 */
300 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
301
302 ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
303 ut_asserteq(2, iter.num_methods);
304 ut_asserteq(1, iter.cur_method);
305 ut_asserteq(0, iter.part);
306 ut_asserteq(0, iter.max_part);
307 ut_asserteq_str("efi", iter.method->name);
308 ut_asserteq(0, bflow.err);
309 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
310 bootflow_free(&bflow);
311
312 /* The next device is mmc1.bootdev - at first we use the whole device */
313 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
314 ut_asserteq(2, iter.num_methods);
315 ut_asserteq(0, iter.cur_method);
316 ut_asserteq(0, iter.part);
317 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600318 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600319 ut_asserteq(0, bflow.err);
320 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
321 bootflow_free(&bflow);
322
323 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
324 ut_asserteq(2, iter.num_methods);
325 ut_asserteq(1, iter.cur_method);
326 ut_asserteq(0, iter.part);
327 ut_asserteq(0x1e, iter.max_part);
328 ut_asserteq_str("efi", iter.method->name);
329 ut_asserteq(0, bflow.err);
330 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
331 bootflow_free(&bflow);
332
333 /* Then more to partition 1 where we find something */
334 ut_assertok(bootflow_scan_next(&iter, &bflow));
335 ut_asserteq(2, iter.num_methods);
336 ut_asserteq(0, iter.cur_method);
337 ut_asserteq(1, iter.part);
338 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600339 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600340 ut_asserteq(0, bflow.err);
341 ut_asserteq(BOOTFLOWST_READY, bflow.state);
342 bootflow_free(&bflow);
343
344 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
345 ut_asserteq(2, iter.num_methods);
346 ut_asserteq(1, iter.cur_method);
347 ut_asserteq(1, iter.part);
348 ut_asserteq(0x1e, iter.max_part);
349 ut_asserteq_str("efi", iter.method->name);
350 ut_asserteq(0, bflow.err);
351 ut_asserteq(BOOTFLOWST_FS, bflow.state);
352 bootflow_free(&bflow);
353
Simon Glassf5e2df02023-01-17 10:47:41 -0700354 /* Then more to partition 2 which exists but is not bootable */
Simon Glass64cbc5c2023-01-17 10:47:42 -0700355 ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600356 ut_asserteq(2, iter.num_methods);
357 ut_asserteq(0, iter.cur_method);
358 ut_asserteq(2, iter.part);
359 ut_asserteq(0x1e, iter.max_part);
Simon Glassb71d7f72023-05-10 16:34:46 -0600360 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassb255efc2022-04-24 23:31:24 -0600361 ut_asserteq(0, bflow.err);
Simon Glass64cbc5c2023-01-17 10:47:42 -0700362 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
Simon Glassb255efc2022-04-24 23:31:24 -0600363 bootflow_free(&bflow);
364
365 bootflow_iter_uninit(&iter);
366
367 ut_assert_console_end();
368
369 return 0;
370}
371BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
372
Simon Glass90b643d2022-07-30 15:52:36 -0600373#if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
Simon Glassb255efc2022-04-24 23:31:24 -0600374/* Check using the system bootdev */
375static int bootflow_system(struct unit_test_state *uts)
376{
Simon Glassb9ff6482023-01-17 10:47:23 -0700377 struct udevice *bootstd, *dev;
Simon Glassb255efc2022-04-24 23:31:24 -0600378
AKASHI Takahiroe3e542d2024-01-17 13:39:42 +0900379 if (!IS_ENABLED(CONFIG_EFI_BOOTMGR))
Simon Glassc7599442022-10-20 18:22:49 -0600380 return -EAGAIN;
Simon Glassb9ff6482023-01-17 10:47:23 -0700381 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Heinrich Schuchardtdc1f0062024-04-03 20:34:15 +0200382 ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_3efi_mgr),
Simon Glassb9ff6482023-01-17 10:47:23 -0700383 "efi_mgr", 0, ofnode_null(), &dev));
384 ut_assertok(device_probe(dev));
Simon Glass161e1e32022-07-30 15:52:22 -0600385 sandbox_set_fake_efi_mgr_dev(dev, true);
Simon Glassb255efc2022-04-24 23:31:24 -0600386
Simon Glassb255efc2022-04-24 23:31:24 -0600387 /* We should get a single 'bootmgr' method right at the end */
388 bootstd_clear_glob();
389 console_record_reset_enable();
Simon Glasse4cf1062023-01-17 10:48:13 -0700390 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glasscc15e142022-07-30 15:52:27 -0600391 ut_assert_skip_to_line(
Simon Glassd465ad52023-08-24 13:55:39 -0600392 " 0 efi_mgr ready (none) 0 <NULL> ");
Simon Glasscc15e142022-07-30 15:52:27 -0600393 ut_assert_skip_to_line("No more bootdevs");
Simon Glassb9ff6482023-01-17 10:47:23 -0700394 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
Simon Glassb255efc2022-04-24 23:31:24 -0600395 ut_assert_console_end();
396
397 return 0;
398}
Simon Glass771a2442022-07-30 15:52:28 -0600399BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_PDATA |
400 UT_TESTF_SCAN_FDT);
Simon Glass90b643d2022-07-30 15:52:36 -0600401#endif
Simon Glassb255efc2022-04-24 23:31:24 -0600402
403/* Check disabling a bootmethod if it requests it */
404static int bootflow_iter_disable(struct unit_test_state *uts)
405{
406 struct udevice *bootstd, *dev;
407 struct bootflow_iter iter;
408 struct bootflow bflow;
409 int i;
410
411 /* Add the EFI bootmgr driver */
412 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
413 ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
414 &dev));
415
Simon Glassb255efc2022-04-24 23:31:24 -0600416 ut_assertok(bootstd_test_drop_bootdev_order(uts));
417
418 bootstd_clear_glob();
Simon Glass49ad1d82022-07-30 15:52:16 -0600419 console_record_reset_enable();
420 ut_assertok(inject_response(uts));
Simon Glasse4cf1062023-01-17 10:48:13 -0700421 ut_assertok(run_command("bootflow scan -lbH", 0));
Simon Glassb255efc2022-04-24 23:31:24 -0600422
423 /* Try to boot the bootmgr flow, which will fail */
424 console_record_reset_enable();
Simon Glass5d3d44f2023-01-17 10:48:16 -0700425 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassb255efc2022-04-24 23:31:24 -0600426 ut_asserteq(3, iter.num_methods);
427 ut_asserteq_str("sandbox", iter.method->name);
Simon Glass49ad1d82022-07-30 15:52:16 -0600428 ut_assertok(inject_response(uts));
Simon Glassb255efc2022-04-24 23:31:24 -0600429 ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
430
431 ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
432 ut_assert_console_end();
433
434 /* Check that the sandbox bootmeth has been removed */
435 ut_asserteq(2, iter.num_methods);
436 for (i = 0; i < iter.num_methods; i++)
437 ut_assert(strcmp("sandbox", iter.method_order[i]->name));
438
439 return 0;
440}
441BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
442
Simon Glassc8d37212022-07-30 15:52:34 -0600443/* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
444static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
445{
Simon Glass90b643d2022-07-30 15:52:36 -0600446 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc7599442022-10-20 18:22:49 -0600447 return -EAGAIN;
Simon Glass90b643d2022-07-30 15:52:36 -0600448
Simon Glassc8d37212022-07-30 15:52:34 -0600449 ut_assertok(bootstd_test_drop_bootdev_order(uts));
450
451 /*
452 * Make sure that the -G flag makes the scan fail, since this is not
453 * supported when an ordering is provided
454 */
455 console_record_reset_enable();
456 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}
482BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
483
Simon Glassb255efc2022-04-24 23:31:24 -0600484/* Check 'bootflow boot' to boot a selected bootflow */
485static int bootflow_cmd_boot(struct unit_test_state *uts)
486{
487 console_record_reset_enable();
488 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}
511BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
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 Glassd2bc33ed2023-01-06 08:52:41 -0600546 "bootmeth_script", 0, ofnode_null(), &dev));
547
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
588 console_record_reset_enable();
589 ut_assertok(run_command("bootflow scan", 0));
590 ut_assert_console_end();
591
592 /* Restore the order used by the device tree */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600593 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
594 std = dev_get_priv(bootstd);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600595 std->bootdev_order = old_order;
596
Simon Glassd92bcc42023-01-06 08:52:42 -0600597 return 0;
598}
599
Simon Glassbb76a5c2023-08-24 13:55:40 -0600600/**
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200601 * scan_mmc_android_bootdev() - Set up an mmc bootdev so we can access other
602 * distros. Android bootflow might print "ANDROID:*" while scanning
603 *
604 * @uts: Unit test state
605 * @mmc_dev: MMC device to use, e.g. "mmc4"
606 * Returns 0 on success, -ve on failure
607 */
608static int scan_mmc_android_bootdev(struct unit_test_state *uts, const char *mmc_dev)
609{
610 struct bootstd_priv *std;
611 struct udevice *bootstd;
612 const char **old_order;
613
614 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, true, &old_order));
615
616 console_record_reset_enable();
617 ut_assertok(run_command("bootflow scan", 0));
618 /* Android bootflow might print one or two 'ANDROID:*' logs */
619 ut_check_skipline(uts);
620 ut_check_skipline(uts);
621 ut_assert_console_end();
622
623 /* Restore the order used by the device tree */
624 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
625 std = dev_get_priv(bootstd);
626 std->bootdev_order = old_order;
627
628 return 0;
629}
630
631/**
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600632 * scan_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
Simon Glassbb76a5c2023-08-24 13:55:40 -0600633 *
634 * @uts: Unit test state
635 * Returns 0 on success, -ve on failure
636 */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600637static int scan_mmc4_bootdev(struct unit_test_state *uts)
Simon Glassbb76a5c2023-08-24 13:55:40 -0600638{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600639 ut_assertok(scan_mmc_bootdev(uts, "mmc4", false));
Simon Glassbb76a5c2023-08-24 13:55:40 -0600640
641 return 0;
642}
643
Simon Glassd92bcc42023-01-06 08:52:42 -0600644/* Check 'bootflow menu' to select a bootflow */
645static int bootflow_cmd_menu(struct unit_test_state *uts)
646{
Simon Glass6d5083b2023-10-01 19:14:38 -0600647 struct bootstd_priv *std;
Simon Glassd92bcc42023-01-06 08:52:42 -0600648 char prev[3];
649
Simon Glass6d5083b2023-10-01 19:14:38 -0600650 /* get access to the current bootflow */
651 ut_assertok(bootstd_get_priv(&std));
652
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600653 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600654
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600655 /* Add keypresses to move to and select the second one in the list */
656 prev[0] = CTL_CH('n');
657 prev[1] = '\r';
658 prev[2] = '\0';
659 ut_asserteq(2, console_in_puts(prev));
660
661 ut_assertok(run_command("bootflow menu", 0));
662 ut_assert_nextline("Selected: Armbian");
Simon Glass6d5083b2023-10-01 19:14:38 -0600663 ut_assertnonnull(std->cur_bootflow);
664 ut_assert_console_end();
665
666 /* Check not selecting anything */
667 prev[0] = '\e';
668 prev[1] = '\0';
669 ut_asserteq(1, console_in_puts(prev));
670
671 ut_asserteq(1, run_command("bootflow menu", 0));
672 ut_assertnull(std->cur_bootflow);
673 ut_assert_nextline("Nothing chosen");
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600674 ut_assert_console_end();
675
676 return 0;
677}
678BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassd92bcc42023-01-06 08:52:42 -0600679
Simon Glass9bf27862023-10-01 19:15:25 -0600680/* Check 'bootflow scan -m' to select a bootflow using a menu */
681static int bootflow_scan_menu(struct unit_test_state *uts)
682{
683 struct bootstd_priv *std;
684 const char **old_order, **new_order;
685 char prev[3];
686
687 /* get access to the current bootflow */
688 ut_assertok(bootstd_get_priv(&std));
689
690 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
691
692 /* Add keypresses to move to and select the second one in the list */
693 prev[0] = CTL_CH('n');
694 prev[1] = '\r';
695 prev[2] = '\0';
696 ut_asserteq(2, console_in_puts(prev));
697
698 ut_assertok(run_command("bootflow scan -lm", 0));
699 new_order = std->bootdev_order;
700 std->bootdev_order = old_order;
701
702 ut_assert_skip_to_line("No more bootdevs");
703 ut_assert_nextlinen("--");
704 ut_assert_nextline("(2 bootflows, 2 valid)");
705
706 ut_assert_nextline("Selected: Armbian");
707 ut_assertnonnull(std->cur_bootflow);
708 ut_assert_console_end();
709
710 /* Check not selecting anything */
711 prev[0] = '\e';
712 prev[1] = '\0';
713 ut_asserteq(1, console_in_puts(prev));
714
715 std->bootdev_order = new_order; /* Blue Monday */
716 ut_assertok(run_command("bootflow scan -lm", 0));
717 std->bootdev_order = old_order;
718
719 ut_assertnull(std->cur_bootflow);
720 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
721 ut_assert_nextline("Nothing chosen");
722 ut_assert_console_end();
723
724 return 0;
725}
726BOOTSTD_TEST(bootflow_scan_menu,
727 UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
728
729/* Check 'bootflow scan -mb' to select and boot a bootflow using a menu */
730static int bootflow_scan_menu_boot(struct unit_test_state *uts)
731{
732 struct bootstd_priv *std;
733 const char **old_order;
734 char prev[3];
735
736 /* get access to the current bootflow */
737 ut_assertok(bootstd_get_priv(&std));
738
739 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
740
741 /* Add keypresses to move to and select the second one in the list */
742 prev[0] = CTL_CH('n');
743 prev[1] = '\r';
744 prev[2] = '\0';
745 ut_asserteq(2, console_in_puts(prev));
746
747 ut_assertok(run_command("bootflow scan -lmb", 0));
748 std->bootdev_order = old_order;
749
750 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
751
752 ut_assert_nextline("Selected: Armbian");
Francis Laniel5b64c452023-12-22 22:02:35 +0100753
754 if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
755 /*
756 * With old hush, despite booti failing to boot, i.e. returning
757 * CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as
758 * we are using bootmeth_script here, to return -EFAULT.
759 */
760 ut_assert_skip_to_line("Boot failed (err=-14)");
761 } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
762 /*
763 * While with modern one, run_command() propagates CMD_RET_FAILURE returned
764 * by booti, so we get 1 here.
765 */
766 ut_assert_skip_to_line("Boot failed (err=1)");
767 }
Simon Glass9bf27862023-10-01 19:15:25 -0600768 ut_assertnonnull(std->cur_bootflow);
769 ut_assert_console_end();
770
771 return 0;
772}
773BOOTSTD_TEST(bootflow_scan_menu_boot,
774 UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
775
Simon Glassba3d5372023-01-17 10:48:15 -0700776/* Check searching for a single bootdev using the hunters */
777static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
778{
779 struct bootstd_priv *std;
780
781 /* get access to the used hunters */
782 ut_assertok(bootstd_get_priv(&std));
783
784 ut_assertok(bootstd_test_drop_bootdev_order(uts));
785
786 console_record_reset_enable();
787 ut_assertok(run_command("bootflow scan -l mmc1", 0));
788 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
789 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
790 ut_assert_console_end();
791
792 /* check that the hunter was used */
793 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
794
795 return 0;
796}
797BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
798
799/* Check searching for a uclass label using the hunters */
800static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
801{
802 struct bootstd_priv *std;
803
804 /* get access to the used hunters */
805 ut_assertok(bootstd_get_priv(&std));
806
807 test_set_skip_delays(true);
808 test_set_eth_enable(false);
809 ut_assertok(bootstd_test_drop_bootdev_order(uts));
810
811 console_record_reset_enable();
812 ut_assertok(run_command("bootflow scan -l mmc", 0));
813
814 /* check that the hunter was used */
815 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
816
817 /* check that we got the mmc1 bootflow */
818 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
819 ut_assert_nextlinen("Seq");
820 ut_assert_nextlinen("---");
821 ut_assert_nextline("Hunting with: simple_bus");
822 ut_assert_nextline("Found 2 extension board(s).");
823 ut_assert_nextline("Hunting with: mmc");
824 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
825 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
826 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600827 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassba3d5372023-01-17 10:48:15 -0700828 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
829 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
830 ut_assert_console_end();
831
832 return 0;
833}
834BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
835
Simon Glassd92bcc42023-01-06 08:52:42 -0600836/**
837 * check_font() - Check that the font size for an item matches expectations
838 *
839 * @uts: Unit test state
840 * @scn: Scene containing the text object
841 * @id: ID of the text object
842 * Returns 0 on success, -ve on failure
843 */
844static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
845 int font_size)
846{
847 struct scene_obj_txt *txt;
848
849 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
850 ut_assertnonnull(txt);
851
852 ut_asserteq(font_size, txt->font_size);
853
854 return 0;
855}
856
857/* Check themes work with a bootflow menu */
858static int bootflow_menu_theme(struct unit_test_state *uts)
859{
860 const int font_size = 30;
861 struct scene *scn;
862 struct expo *exp;
863 ofnode node;
864 int i;
865
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600866 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600867
868 ut_assertok(bootflow_menu_new(&exp));
869 node = ofnode_path("/bootstd/theme");
870 ut_assert(ofnode_valid(node));
871 ut_assertok(bootflow_menu_apply_theme(exp, node));
872
873 scn = expo_lookup_scene_id(exp, MAIN);
874 ut_assertnonnull(scn);
875
876 /*
877 * Check that the txt objects have the correct font size from the
878 * device tree node: bootstd/theme
879 *
880 * Check both menu items, since there are two bootflows
881 */
882 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
883 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
884 for (i = 0; i < 2; i++) {
885 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
886 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
887 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
888 }
889
890 expo_destroy(exp);
891
892 return 0;
893}
894BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassa08adca2023-07-12 09:04:38 -0600895
896/**
897 * check_arg() - Check both the normal case and the buffer-overflow case
898 *
899 * @uts: Unit-test state
900 * @expect_ret: Expected return value (i.e. buffer length)
901 * @expect_str: String expected to be returned
902 * @buf: Buffer to use
903 * @from: Original cmdline to update
904 * @arg: Argument to update (e.g. "console")
905 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
906 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
907 * it without any value ("initrd")
908 */
909static int check_arg(struct unit_test_state *uts, int expect_ret,
910 const char *expect_str, char *buf, const char *from,
911 const char *arg, const char *val)
912{
913 /* check for writing outside the reported bounds */
914 buf[expect_ret] = '[';
915 ut_asserteq(expect_ret,
916 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
917 ut_asserteq_str(expect_str, buf);
918 ut_asserteq('[', buf[expect_ret]);
919
920 /* do the test again but with one less byte in the buffer */
921 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
922 val, NULL));
923
924 return 0;
925}
926
927/* Test of bootflow_cmdline_set_arg() */
928static int test_bootflow_cmdline_set(struct unit_test_state *uts)
929{
930 char buf[50];
931 const int size = sizeof(buf);
932
933 /*
934 * note that buffer-overflow tests are immediately each test case, just
935 * top keep the code together
936 */
937
938 /* add an arg that doesn't already exist, starting from empty */
939 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
940 NULL));
941
942 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
943 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
944 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
945
946 /* add an arg that doesn't already exist, starting from non-empty */
947 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
948 BOOTFLOWCL_EMPTY));
949 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
950 ""));
951 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
952 "fred"));
953
954 /* update an arg at the start */
955 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
956 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
957 BOOTFLOWCL_EMPTY));
958 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
959 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
960 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
961 "1234"));
962
963 /* update an arg at the end */
964 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
965 NULL));
966 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
967 BOOTFLOWCL_EMPTY));
968 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
969 ""));
970 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
971 "1"));
972 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
973 "arg", "1234"));
974
975 /* update an arg in the middle */
976 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
977 "mary=abc arg=123 john=2", "arg", NULL));
978 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
979 "mary=abc arg=123 john=2", "arg",
980 BOOTFLOWCL_EMPTY));
981 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
982 "mary=abc arg=123 john=2", "arg", ""));
983 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
984 "mary=abc arg=123 john=2", "arg", "1"));
985 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
986 "mary=abc arg=123 john=2", "arg", "1234"));
987
988 /* handle existing args with quotes */
989 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
990 "mary=\"abc\" arg=123 john", "arg", NULL));
991
992 /* handle existing args with quoted spaces */
993 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
994 "mary=\"abc def\" arg=123 john", "arg", NULL));
995
996 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
997 buf, "mary=\"abc def\" arg=123 john", "def",
998 "4"));
999
1000 /* quote at the start */
1001 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1002 "mary=\"abc def\" arg=\"123 456\"",
1003 "arg", "\"4 5 6", NULL));
1004
1005 /* quote at the end */
1006 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1007 "mary=\"abc def\" arg=\"123 456\"",
1008 "arg", "4 5 6\"", NULL));
1009
1010 /* quote in the middle */
1011 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
1012 "mary=\"abc def\" arg=\"123 456\"",
1013 "arg", "\"4 \"5 6\"", NULL));
1014
1015 /* handle updating a quoted arg */
1016 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
1017 "mary=\"abc def\" arg=\"123 456\"", "arg",
1018 "4 5 6"));
1019
1020 /* changing a quoted arg to a non-quoted arg */
1021 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
1022 "mary=\"abc def\" arg=\"123 456\"", "arg",
1023 "789"));
1024
1025 /* changing a non-quoted arg to a quoted arg */
1026 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
1027 "mary=\"abc def\" arg=123", "arg", "456 789"));
1028
1029 /* handling of spaces */
1030 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1031 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
1032 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
1033 "123"));
1034 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
1035 "arg", "123"));
1036 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
1037 " john arg=123 mary ", "arg", "123"));
1038
1039 /* unchanged arg */
1040 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
1041
1042 /* arg which starts with the same name */
1043 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
1044 "mary=abc johnathon=2 john=1", "john", "3"));
1045
1046 return 0;
1047}
1048BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
Simon Glass55a2da32023-07-12 09:04:39 -06001049
1050/* Test of bootflow_cmdline_set_arg() */
1051static int bootflow_set_arg(struct unit_test_state *uts)
1052{
1053 struct bootflow s_bflow, *bflow = &s_bflow;
1054 ulong mem_start;
1055
1056 ut_assertok(env_set("bootargs", NULL));
1057
1058 mem_start = ut_check_delta(0);
1059
1060 /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
1061 bflow->cmdline = NULL;
1062 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
1063 ut_asserteq_str(bflow->cmdline, "fred=123");
1064
1065 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
1066 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
1067
1068 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
1069 ut_asserteq_str(bflow->cmdline, "fred=123");
1070 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
1071 ut_asserteq_ptr(bflow->cmdline, NULL);
1072
1073 ut_asserteq(0, ut_check_delta(mem_start));
1074
1075 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
1076 ut_asserteq_str("mary=here", env_get("bootargs"));
1077 ut_assertok(env_set("bootargs", NULL));
1078
1079 return 0;
1080}
1081BOOTSTD_TEST(bootflow_set_arg, 0);
1082
1083/* Test of bootflow_cmdline_get_arg() */
1084static int bootflow_cmdline_get(struct unit_test_state *uts)
1085{
1086 int pos;
1087
1088 /* empty string */
1089 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
1090
1091 /* arg with empty value */
1092 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
1093 ut_asserteq(5, pos);
1094
1095 /* arg with a value */
1096 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
1097 ut_asserteq(5, pos);
1098
1099 /* arg with a value */
1100 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
1101 ut_asserteq(12, pos);
1102
1103 /* arg with a value, after quoted arg */
1104 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
1105 ut_asserteq(16, pos);
1106
1107 /* arg in the middle */
1108 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
1109 &pos));
1110 ut_asserteq(15, pos);
1111
1112 /* quoted arg */
1113 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
1114 "fred", &pos));
1115 ut_asserteq(17, pos);
1116
1117 /* args starting with the same prefix */
1118 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
1119 &pos));
1120 ut_asserteq(26, pos);
1121
1122 return 0;
1123}
1124BOOTSTD_TEST(bootflow_cmdline_get, 0);
1125
1126static int bootflow_cmdline(struct unit_test_state *uts)
1127{
1128 ut_assertok(run_command("bootflow scan mmc", 0));
1129 ut_assertok(run_command("bootflow sel 0", 0));
1130 console_record_reset_enable();
1131
1132 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1133 ut_assert_nextline("Argument not found");
1134 ut_assert_console_end();
1135
1136 ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
1137 ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
1138 ut_assert_nextline("123");
1139
1140 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1141 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1142 ut_assert_nextline("abc");
1143
1144 ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
1145 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1146 ut_assert_nextline("Argument not found");
1147
1148 ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
1149 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1150 ut_assert_nextline_empty();
1151
Simon Glass7a164f82023-11-29 10:31:19 -07001152 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1153 ut_asserteq(0, run_command("bootflow cmdline set mary", 0));
1154 ut_assert_nextline_empty();
1155
Simon Glass55a2da32023-07-12 09:04:39 -06001156 ut_assert_console_end();
1157
1158 return 0;
1159}
1160BOOTSTD_TEST(bootflow_cmdline, 0);
Simon Glassfff928c2023-08-24 13:55:41 -06001161
Simon Glassa61057f2023-10-25 07:17:36 +13001162/* test a few special changes to a long command line */
1163static int bootflow_cmdline_special(struct unit_test_state *uts)
1164{
1165 char buf[500];
1166 int pos;
1167
1168 /*
1169 * check handling of an argument which has an embedded '=', as well as
1170 * handling of a argument which partially matches ("ro" and "root")
1171 */
1172 ut_asserteq(32, cmdline_set_arg(
1173 buf, sizeof(buf),
1174 "loglevel=7 root=PARTUUID=d68352e3 rootwait ro noinitrd",
1175 "root", NULL, &pos));
1176 ut_asserteq_str("loglevel=7 rootwait ro noinitrd", buf);
1177
1178 return 0;
1179}
1180BOOTSTD_TEST(bootflow_cmdline_special, 0);
1181
Simon Glassfff928c2023-08-24 13:55:41 -06001182/* Test ChromiumOS bootmeth */
1183static int bootflow_cros(struct unit_test_state *uts)
1184{
Simon Glass3ff8a9a2023-10-01 19:14:37 -06001185 ut_assertok(scan_mmc_bootdev(uts, "mmc5", true));
Simon Glassfff928c2023-08-24 13:55:41 -06001186 ut_assertok(run_command("bootflow list", 0));
1187
1188 ut_assert_nextlinen("Showing all");
1189 ut_assert_nextlinen("Seq");
1190 ut_assert_nextlinen("---");
1191 ut_assert_nextlinen(" 0 extlinux");
Simon Glassd7d3a972023-08-24 13:55:45 -06001192 ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 ");
1193 ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 ");
Simon Glassfff928c2023-08-24 13:55:41 -06001194 ut_assert_nextlinen("---");
Simon Glassd7d3a972023-08-24 13:55:45 -06001195 ut_assert_skip_to_line("(3 bootflows, 3 valid)");
Simon Glassfff928c2023-08-24 13:55:41 -06001196
1197 ut_assert_console_end();
1198
1199 return 0;
1200}
1201BOOTSTD_TEST(bootflow_cros, 0);
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +02001202
1203/* Test Android bootmeth */
1204static int bootflow_android(struct unit_test_state *uts)
1205{
1206 if (!IS_ENABLED(CONFIG_BOOTMETH_ANDROID))
1207 return -EAGAIN;
1208
1209 ut_assertok(scan_mmc_android_bootdev(uts, "mmc7"));
1210 ut_assertok(run_command("bootflow list", 0));
1211
1212 ut_assert_nextlinen("Showing all");
1213 ut_assert_nextlinen("Seq");
1214 ut_assert_nextlinen("---");
1215 ut_assert_nextlinen(" 0 extlinux");
1216 ut_assert_nextlinen(" 1 android ready mmc 0 mmc7.bootdev.whole ");
1217 ut_assert_nextlinen("---");
1218 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
1219
1220 ut_assert_console_end();
1221
1222 return 0;
1223}
1224BOOTSTD_TEST(bootflow_android, 0);