blob: 674d4c05f83fd7ae5ab72d29e829ef079830d61d [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
9#include <common.h>
10#include <bootdev.h>
11#include <bootflow.h>
Simon Glassc8d37212022-07-30 15:52:34 -060012#include <bootmeth.h>
Simon Glassb255efc2022-04-24 23:31:24 -060013#include <bootstd.h>
Simon Glassd2bc33ed2023-01-06 08:52:41 -060014#include <cli.h>
Simon Glassb255efc2022-04-24 23:31:24 -060015#include <dm.h>
Heinrich Schuchardt4226d9d2024-04-03 23:39:48 +020016#include <efi_default_filename.h>
Simon Glassd92bcc42023-01-06 08:52:42 -060017#include <expo.h>
Simon Glass90b643d2022-07-30 15:52:36 -060018#ifdef CONFIG_SANDBOX
Simon Glass161e1e32022-07-30 15:52:22 -060019#include <asm/test.h>
Simon Glass90b643d2022-07-30 15:52:36 -060020#endif
Simon Glassd2bc33ed2023-01-06 08:52:41 -060021#include <dm/device-internal.h>
Simon Glassb255efc2022-04-24 23:31:24 -060022#include <dm/lists.h>
23#include <test/suites.h>
24#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
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
522 * @bind_cros: true to bind the ChromiumOS bootmeth
523 * @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,
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600527 bool bind_cros, 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 */
Dan Carpenter57671082024-02-21 09:26:09 +0300549 if (IS_ENABLED(CONFIG_BOOTMETH_CROS) && bind_cros) {
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
Simon Glassbb76a5c2023-08-24 13:55:40 -0600555 /* Change the order to include the device */
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600556 std = dev_get_priv(bootstd);
557 old_order = std->bootdev_order;
558 std->bootdev_order = order;
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600559 *old_orderp = old_order;
560
561 return 0;
562}
563
564/**
565 * scan_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
566 *
567 * @uts: Unit test state
568 * @mmc_dev: MMC device to use, e.g. "mmc4"
569 * @bind_cros: true to bind the ChromiumOS bootmeth
570 * Returns 0 on success, -ve on failure
571 */
572static int scan_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
573 bool bind_cros)
574{
575 struct bootstd_priv *std;
576 struct udevice *bootstd;
577 const char **old_order;
578
579 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, bind_cros, &old_order));
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600580
581 console_record_reset_enable();
582 ut_assertok(run_command("bootflow scan", 0));
583 ut_assert_console_end();
584
585 /* Restore the order used by the device tree */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600586 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
587 std = dev_get_priv(bootstd);
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600588 std->bootdev_order = old_order;
589
Simon Glassd92bcc42023-01-06 08:52:42 -0600590 return 0;
591}
592
Simon Glassbb76a5c2023-08-24 13:55:40 -0600593/**
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600594 * scan_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
Simon Glassbb76a5c2023-08-24 13:55:40 -0600595 *
596 * @uts: Unit test state
597 * Returns 0 on success, -ve on failure
598 */
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600599static int scan_mmc4_bootdev(struct unit_test_state *uts)
Simon Glassbb76a5c2023-08-24 13:55:40 -0600600{
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600601 ut_assertok(scan_mmc_bootdev(uts, "mmc4", false));
Simon Glassbb76a5c2023-08-24 13:55:40 -0600602
603 return 0;
604}
605
Simon Glassd92bcc42023-01-06 08:52:42 -0600606/* Check 'bootflow menu' to select a bootflow */
607static int bootflow_cmd_menu(struct unit_test_state *uts)
608{
Simon Glass6d5083b2023-10-01 19:14:38 -0600609 struct bootstd_priv *std;
Simon Glassd92bcc42023-01-06 08:52:42 -0600610 char prev[3];
611
Simon Glass6d5083b2023-10-01 19:14:38 -0600612 /* get access to the current bootflow */
613 ut_assertok(bootstd_get_priv(&std));
614
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600615 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600616
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600617 /* Add keypresses to move to and select the second one in the list */
618 prev[0] = CTL_CH('n');
619 prev[1] = '\r';
620 prev[2] = '\0';
621 ut_asserteq(2, console_in_puts(prev));
622
623 ut_assertok(run_command("bootflow menu", 0));
624 ut_assert_nextline("Selected: Armbian");
Simon Glass6d5083b2023-10-01 19:14:38 -0600625 ut_assertnonnull(std->cur_bootflow);
626 ut_assert_console_end();
627
628 /* Check not selecting anything */
629 prev[0] = '\e';
630 prev[1] = '\0';
631 ut_asserteq(1, console_in_puts(prev));
632
633 ut_asserteq(1, run_command("bootflow menu", 0));
634 ut_assertnull(std->cur_bootflow);
635 ut_assert_nextline("Nothing chosen");
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600636 ut_assert_console_end();
637
638 return 0;
639}
640BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassd92bcc42023-01-06 08:52:42 -0600641
Simon Glass9bf27862023-10-01 19:15:25 -0600642/* Check 'bootflow scan -m' to select a bootflow using a menu */
643static int bootflow_scan_menu(struct unit_test_state *uts)
644{
645 struct bootstd_priv *std;
646 const char **old_order, **new_order;
647 char prev[3];
648
649 /* get access to the current bootflow */
650 ut_assertok(bootstd_get_priv(&std));
651
652 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
653
654 /* Add keypresses to move to and select the second one in the list */
655 prev[0] = CTL_CH('n');
656 prev[1] = '\r';
657 prev[2] = '\0';
658 ut_asserteq(2, console_in_puts(prev));
659
660 ut_assertok(run_command("bootflow scan -lm", 0));
661 new_order = std->bootdev_order;
662 std->bootdev_order = old_order;
663
664 ut_assert_skip_to_line("No more bootdevs");
665 ut_assert_nextlinen("--");
666 ut_assert_nextline("(2 bootflows, 2 valid)");
667
668 ut_assert_nextline("Selected: Armbian");
669 ut_assertnonnull(std->cur_bootflow);
670 ut_assert_console_end();
671
672 /* Check not selecting anything */
673 prev[0] = '\e';
674 prev[1] = '\0';
675 ut_asserteq(1, console_in_puts(prev));
676
677 std->bootdev_order = new_order; /* Blue Monday */
678 ut_assertok(run_command("bootflow scan -lm", 0));
679 std->bootdev_order = old_order;
680
681 ut_assertnull(std->cur_bootflow);
682 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
683 ut_assert_nextline("Nothing chosen");
684 ut_assert_console_end();
685
686 return 0;
687}
688BOOTSTD_TEST(bootflow_scan_menu,
689 UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
690
691/* Check 'bootflow scan -mb' to select and boot a bootflow using a menu */
692static int bootflow_scan_menu_boot(struct unit_test_state *uts)
693{
694 struct bootstd_priv *std;
695 const char **old_order;
696 char prev[3];
697
698 /* get access to the current bootflow */
699 ut_assertok(bootstd_get_priv(&std));
700
701 ut_assertok(prep_mmc_bootdev(uts, "mmc4", false, &old_order));
702
703 /* Add keypresses to move to and select the second one in the list */
704 prev[0] = CTL_CH('n');
705 prev[1] = '\r';
706 prev[2] = '\0';
707 ut_asserteq(2, console_in_puts(prev));
708
709 ut_assertok(run_command("bootflow scan -lmb", 0));
710 std->bootdev_order = old_order;
711
712 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
713
714 ut_assert_nextline("Selected: Armbian");
Francis Laniel5b64c452023-12-22 22:02:35 +0100715
716 if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
717 /*
718 * With old hush, despite booti failing to boot, i.e. returning
719 * CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as
720 * we are using bootmeth_script here, to return -EFAULT.
721 */
722 ut_assert_skip_to_line("Boot failed (err=-14)");
723 } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
724 /*
725 * While with modern one, run_command() propagates CMD_RET_FAILURE returned
726 * by booti, so we get 1 here.
727 */
728 ut_assert_skip_to_line("Boot failed (err=1)");
729 }
Simon Glass9bf27862023-10-01 19:15:25 -0600730 ut_assertnonnull(std->cur_bootflow);
731 ut_assert_console_end();
732
733 return 0;
734}
735BOOTSTD_TEST(bootflow_scan_menu_boot,
736 UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
737
Simon Glassba3d5372023-01-17 10:48:15 -0700738/* Check searching for a single bootdev using the hunters */
739static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
740{
741 struct bootstd_priv *std;
742
743 /* get access to the used hunters */
744 ut_assertok(bootstd_get_priv(&std));
745
746 ut_assertok(bootstd_test_drop_bootdev_order(uts));
747
748 console_record_reset_enable();
749 ut_assertok(run_command("bootflow scan -l mmc1", 0));
750 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
751 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
752 ut_assert_console_end();
753
754 /* check that the hunter was used */
755 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
756
757 return 0;
758}
759BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
760
761/* Check searching for a uclass label using the hunters */
762static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
763{
764 struct bootstd_priv *std;
765
766 /* get access to the used hunters */
767 ut_assertok(bootstd_get_priv(&std));
768
769 test_set_skip_delays(true);
770 test_set_eth_enable(false);
771 ut_assertok(bootstd_test_drop_bootdev_order(uts));
772
773 console_record_reset_enable();
774 ut_assertok(run_command("bootflow scan -l mmc", 0));
775
776 /* check that the hunter was used */
777 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
778
779 /* check that we got the mmc1 bootflow */
780 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
781 ut_assert_nextlinen("Seq");
782 ut_assert_nextlinen("---");
783 ut_assert_nextline("Hunting with: simple_bus");
784 ut_assert_nextline("Found 2 extension board(s).");
785 ut_assert_nextline("Hunting with: mmc");
786 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
787 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
788 ut_assert_nextline(
Simon Glassb71d7f72023-05-10 16:34:46 -0600789 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassba3d5372023-01-17 10:48:15 -0700790 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
791 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
792 ut_assert_console_end();
793
794 return 0;
795}
796BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
797
Simon Glassd92bcc42023-01-06 08:52:42 -0600798/**
799 * check_font() - Check that the font size for an item matches expectations
800 *
801 * @uts: Unit test state
802 * @scn: Scene containing the text object
803 * @id: ID of the text object
804 * Returns 0 on success, -ve on failure
805 */
806static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
807 int font_size)
808{
809 struct scene_obj_txt *txt;
810
811 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
812 ut_assertnonnull(txt);
813
814 ut_asserteq(font_size, txt->font_size);
815
816 return 0;
817}
818
819/* Check themes work with a bootflow menu */
820static int bootflow_menu_theme(struct unit_test_state *uts)
821{
822 const int font_size = 30;
823 struct scene *scn;
824 struct expo *exp;
825 ofnode node;
826 int i;
827
Simon Glass3ff8a9a2023-10-01 19:14:37 -0600828 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glassd92bcc42023-01-06 08:52:42 -0600829
830 ut_assertok(bootflow_menu_new(&exp));
831 node = ofnode_path("/bootstd/theme");
832 ut_assert(ofnode_valid(node));
833 ut_assertok(bootflow_menu_apply_theme(exp, node));
834
835 scn = expo_lookup_scene_id(exp, MAIN);
836 ut_assertnonnull(scn);
837
838 /*
839 * Check that the txt objects have the correct font size from the
840 * device tree node: bootstd/theme
841 *
842 * Check both menu items, since there are two bootflows
843 */
844 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
845 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
846 for (i = 0; i < 2; i++) {
847 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
848 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
849 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
850 }
851
852 expo_destroy(exp);
853
854 return 0;
855}
856BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassa08adca2023-07-12 09:04:38 -0600857
858/**
859 * check_arg() - Check both the normal case and the buffer-overflow case
860 *
861 * @uts: Unit-test state
862 * @expect_ret: Expected return value (i.e. buffer length)
863 * @expect_str: String expected to be returned
864 * @buf: Buffer to use
865 * @from: Original cmdline to update
866 * @arg: Argument to update (e.g. "console")
867 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
868 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
869 * it without any value ("initrd")
870 */
871static int check_arg(struct unit_test_state *uts, int expect_ret,
872 const char *expect_str, char *buf, const char *from,
873 const char *arg, const char *val)
874{
875 /* check for writing outside the reported bounds */
876 buf[expect_ret] = '[';
877 ut_asserteq(expect_ret,
878 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
879 ut_asserteq_str(expect_str, buf);
880 ut_asserteq('[', buf[expect_ret]);
881
882 /* do the test again but with one less byte in the buffer */
883 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
884 val, NULL));
885
886 return 0;
887}
888
889/* Test of bootflow_cmdline_set_arg() */
890static int test_bootflow_cmdline_set(struct unit_test_state *uts)
891{
892 char buf[50];
893 const int size = sizeof(buf);
894
895 /*
896 * note that buffer-overflow tests are immediately each test case, just
897 * top keep the code together
898 */
899
900 /* add an arg that doesn't already exist, starting from empty */
901 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
902 NULL));
903
904 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
905 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
906 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
907
908 /* add an arg that doesn't already exist, starting from non-empty */
909 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
910 BOOTFLOWCL_EMPTY));
911 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
912 ""));
913 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
914 "fred"));
915
916 /* update an arg at the start */
917 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
918 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
919 BOOTFLOWCL_EMPTY));
920 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
921 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
922 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
923 "1234"));
924
925 /* update an arg at the end */
926 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
927 NULL));
928 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
929 BOOTFLOWCL_EMPTY));
930 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
931 ""));
932 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
933 "1"));
934 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
935 "arg", "1234"));
936
937 /* update an arg in the middle */
938 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
939 "mary=abc arg=123 john=2", "arg", NULL));
940 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
941 "mary=abc arg=123 john=2", "arg",
942 BOOTFLOWCL_EMPTY));
943 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
944 "mary=abc arg=123 john=2", "arg", ""));
945 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
946 "mary=abc arg=123 john=2", "arg", "1"));
947 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
948 "mary=abc arg=123 john=2", "arg", "1234"));
949
950 /* handle existing args with quotes */
951 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
952 "mary=\"abc\" arg=123 john", "arg", NULL));
953
954 /* handle existing args with quoted spaces */
955 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
956 "mary=\"abc def\" arg=123 john", "arg", NULL));
957
958 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
959 buf, "mary=\"abc def\" arg=123 john", "def",
960 "4"));
961
962 /* quote at the start */
963 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
964 "mary=\"abc def\" arg=\"123 456\"",
965 "arg", "\"4 5 6", NULL));
966
967 /* quote at the end */
968 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
969 "mary=\"abc def\" arg=\"123 456\"",
970 "arg", "4 5 6\"", NULL));
971
972 /* quote in the middle */
973 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
974 "mary=\"abc def\" arg=\"123 456\"",
975 "arg", "\"4 \"5 6\"", NULL));
976
977 /* handle updating a quoted arg */
978 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
979 "mary=\"abc def\" arg=\"123 456\"", "arg",
980 "4 5 6"));
981
982 /* changing a quoted arg to a non-quoted arg */
983 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
984 "mary=\"abc def\" arg=\"123 456\"", "arg",
985 "789"));
986
987 /* changing a non-quoted arg to a quoted arg */
988 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
989 "mary=\"abc def\" arg=123", "arg", "456 789"));
990
991 /* handling of spaces */
992 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
993 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
994 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
995 "123"));
996 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
997 "arg", "123"));
998 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
999 " john arg=123 mary ", "arg", "123"));
1000
1001 /* unchanged arg */
1002 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
1003
1004 /* arg which starts with the same name */
1005 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
1006 "mary=abc johnathon=2 john=1", "john", "3"));
1007
1008 return 0;
1009}
1010BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
Simon Glass55a2da32023-07-12 09:04:39 -06001011
1012/* Test of bootflow_cmdline_set_arg() */
1013static int bootflow_set_arg(struct unit_test_state *uts)
1014{
1015 struct bootflow s_bflow, *bflow = &s_bflow;
1016 ulong mem_start;
1017
1018 ut_assertok(env_set("bootargs", NULL));
1019
1020 mem_start = ut_check_delta(0);
1021
1022 /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
1023 bflow->cmdline = NULL;
1024 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
1025 ut_asserteq_str(bflow->cmdline, "fred=123");
1026
1027 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
1028 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
1029
1030 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
1031 ut_asserteq_str(bflow->cmdline, "fred=123");
1032 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
1033 ut_asserteq_ptr(bflow->cmdline, NULL);
1034
1035 ut_asserteq(0, ut_check_delta(mem_start));
1036
1037 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
1038 ut_asserteq_str("mary=here", env_get("bootargs"));
1039 ut_assertok(env_set("bootargs", NULL));
1040
1041 return 0;
1042}
1043BOOTSTD_TEST(bootflow_set_arg, 0);
1044
1045/* Test of bootflow_cmdline_get_arg() */
1046static int bootflow_cmdline_get(struct unit_test_state *uts)
1047{
1048 int pos;
1049
1050 /* empty string */
1051 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
1052
1053 /* arg with empty value */
1054 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
1055 ut_asserteq(5, pos);
1056
1057 /* arg with a value */
1058 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
1059 ut_asserteq(5, pos);
1060
1061 /* arg with a value */
1062 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
1063 ut_asserteq(12, pos);
1064
1065 /* arg with a value, after quoted arg */
1066 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
1067 ut_asserteq(16, pos);
1068
1069 /* arg in the middle */
1070 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
1071 &pos));
1072 ut_asserteq(15, pos);
1073
1074 /* quoted arg */
1075 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
1076 "fred", &pos));
1077 ut_asserteq(17, pos);
1078
1079 /* args starting with the same prefix */
1080 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
1081 &pos));
1082 ut_asserteq(26, pos);
1083
1084 return 0;
1085}
1086BOOTSTD_TEST(bootflow_cmdline_get, 0);
1087
1088static int bootflow_cmdline(struct unit_test_state *uts)
1089{
1090 ut_assertok(run_command("bootflow scan mmc", 0));
1091 ut_assertok(run_command("bootflow sel 0", 0));
1092 console_record_reset_enable();
1093
1094 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1095 ut_assert_nextline("Argument not found");
1096 ut_assert_console_end();
1097
1098 ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
1099 ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
1100 ut_assert_nextline("123");
1101
1102 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1103 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1104 ut_assert_nextline("abc");
1105
1106 ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
1107 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
1108 ut_assert_nextline("Argument not found");
1109
1110 ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
1111 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
1112 ut_assert_nextline_empty();
1113
Simon Glass7a164f82023-11-29 10:31:19 -07001114 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
1115 ut_asserteq(0, run_command("bootflow cmdline set mary", 0));
1116 ut_assert_nextline_empty();
1117
Simon Glass55a2da32023-07-12 09:04:39 -06001118 ut_assert_console_end();
1119
1120 return 0;
1121}
1122BOOTSTD_TEST(bootflow_cmdline, 0);
Simon Glassfff928c2023-08-24 13:55:41 -06001123
Simon Glassa61057f2023-10-25 07:17:36 +13001124/* test a few special changes to a long command line */
1125static int bootflow_cmdline_special(struct unit_test_state *uts)
1126{
1127 char buf[500];
1128 int pos;
1129
1130 /*
1131 * check handling of an argument which has an embedded '=', as well as
1132 * handling of a argument which partially matches ("ro" and "root")
1133 */
1134 ut_asserteq(32, cmdline_set_arg(
1135 buf, sizeof(buf),
1136 "loglevel=7 root=PARTUUID=d68352e3 rootwait ro noinitrd",
1137 "root", NULL, &pos));
1138 ut_asserteq_str("loglevel=7 rootwait ro noinitrd", buf);
1139
1140 return 0;
1141}
1142BOOTSTD_TEST(bootflow_cmdline_special, 0);
1143
Simon Glassfff928c2023-08-24 13:55:41 -06001144/* Test ChromiumOS bootmeth */
1145static int bootflow_cros(struct unit_test_state *uts)
1146{
Simon Glass3ff8a9a2023-10-01 19:14:37 -06001147 ut_assertok(scan_mmc_bootdev(uts, "mmc5", true));
Simon Glassfff928c2023-08-24 13:55:41 -06001148 ut_assertok(run_command("bootflow list", 0));
1149
1150 ut_assert_nextlinen("Showing all");
1151 ut_assert_nextlinen("Seq");
1152 ut_assert_nextlinen("---");
1153 ut_assert_nextlinen(" 0 extlinux");
Simon Glassd7d3a972023-08-24 13:55:45 -06001154 ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 ");
1155 ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 ");
Simon Glassfff928c2023-08-24 13:55:41 -06001156 ut_assert_nextlinen("---");
Simon Glassd7d3a972023-08-24 13:55:45 -06001157 ut_assert_skip_to_line("(3 bootflows, 3 valid)");
Simon Glassfff928c2023-08-24 13:55:41 -06001158
1159 ut_assert_console_end();
1160
1161 return 0;
1162}
1163BOOTSTD_TEST(bootflow_cros, 0);