blob: 102b2b5613561aee84ed8033951bda1acc684c4e [file] [log] [blame]
Simon Glassfb1451b2022-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 Glass0917f772022-07-30 15:52:34 -060012#include <bootmeth.h>
Simon Glassfb1451b2022-04-24 23:31:24 -060013#include <bootstd.h>
Simon Glassd985f1d2023-01-06 08:52:41 -060014#include <cli.h>
Simon Glassfb1451b2022-04-24 23:31:24 -060015#include <dm.h>
Simon Glasse64c2952023-01-06 08:52:42 -060016#include <expo.h>
Simon Glass11361c52022-07-30 15:52:36 -060017#ifdef CONFIG_SANDBOX
Simon Glass2662b542022-07-30 15:52:22 -060018#include <asm/test.h>
Simon Glass11361c52022-07-30 15:52:36 -060019#endif
Simon Glassd985f1d2023-01-06 08:52:41 -060020#include <dm/device-internal.h>
Simon Glassfb1451b2022-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 Glasse64c2952023-01-06 08:52:42 -060025#include "../../boot/bootflow_internal.h"
26#include "../../boot/scene_internal.h"
Simon Glassfb1451b2022-04-24 23:31:24 -060027
Simon Glassd985f1d2023-01-06 08:52:41 -060028DECLARE_GLOBAL_DATA_PTR;
29
Simon Glassd08db022023-08-24 13:55:41 -060030extern U_BOOT_DRIVER(bootmeth_cros);
Simon Glass5986d462023-08-19 16:49:35 -060031extern U_BOOT_DRIVER(bootmeth_2script);
Simon Glassd985f1d2023-01-06 08:52:41 -060032
Simon Glassf25f5752022-07-30 15:52:16 -060033static int inject_response(struct unit_test_state *uts)
34{
35 /*
36 * The image being booted presents a menu of options:
37 *
38 * Fedora-Workstation-armhfp-31-1.9 Boot Options.
39 * 1: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
40 * Enter choice:
41 *
42 * Provide input for this, to avoid waiting two seconds for a timeout.
43 */
44 ut_asserteq(2, console_in_puts("1\n"));
45
46 return 0;
47}
48
Simon Glassfb1451b2022-04-24 23:31:24 -060049/* Check 'bootflow scan/list' commands */
50static int bootflow_cmd(struct unit_test_state *uts)
51{
52 console_record_reset_enable();
53 ut_assertok(run_command("bootdev select 1", 0));
54 ut_assert_console_end();
Simon Glass18552d22023-01-17 10:48:13 -070055 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -060056 ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
57 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
58 ut_assert_nextlinen("---");
Simon Glass47aedc22023-01-17 10:48:14 -070059 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
60 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glass79f66352023-05-10 16:34:46 -060061 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass47aedc22023-01-17 10:48:14 -070062 ut_assert_nextline("No more bootdevs");
Simon Glassfb1451b2022-04-24 23:31:24 -060063 ut_assert_nextlinen("---");
64 ut_assert_nextline("(1 bootflow, 1 valid)");
65 ut_assert_console_end();
66
67 ut_assertok(run_command("bootflow list", 0));
68 ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'");
69 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
70 ut_assert_nextlinen("---");
Simon Glass79f66352023-05-10 16:34:46 -060071 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -060072 ut_assert_nextlinen("---");
73 ut_assert_nextline("(1 bootflow, 1 valid)");
74 ut_assert_console_end();
75
76 return 0;
77}
78BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
79
Simon Glass47aedc22023-01-17 10:48:14 -070080/* Check 'bootflow scan' with a label / seq */
Simon Glassfb1451b2022-04-24 23:31:24 -060081static int bootflow_cmd_label(struct unit_test_state *uts)
82{
Simon Glass47aedc22023-01-17 10:48:14 -070083 test_set_eth_enable(false);
84
Simon Glassfb1451b2022-04-24 23:31:24 -060085 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -070086 ut_assertok(run_command("bootflow scan -lH mmc1", 0));
Simon Glass47aedc22023-01-17 10:48:14 -070087 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
Simon Glassfb1451b2022-04-24 23:31:24 -060088 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
89 ut_assert_console_end();
90
Simon Glass47aedc22023-01-17 10:48:14 -070091 ut_assertok(run_command("bootflow scan -lH 0", 0));
92 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassfb1451b2022-04-24 23:31:24 -060093 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
94 ut_assert_console_end();
95
Simon Glass47aedc22023-01-17 10:48:14 -070096 /*
97 * with ethernet enabled we have 8 devices ahead of the mmc ones:
98 *
99 * ut_assertok(run_command("bootdev list", 0));
100 * Seq Probed Status Uclass Name
101 * --- ------ ------ -------- ------------------
102 * 0 [ + ] OK ethernet eth@10002000.bootdev
103 * 1 [ ] OK ethernet eth@10003000.bootdev
104 * 2 [ ] OK ethernet sbe5.bootdev
105 * 3 [ ] OK ethernet eth@10004000.bootdev
106 * 4 [ ] OK ethernet phy-test-eth.bootdev
107 * 5 [ ] OK ethernet dsa-test-eth.bootdev
108 * 6 [ ] OK ethernet dsa-test@0.bootdev
109 * 7 [ ] OK ethernet dsa-test@1.bootdev
110 * 8 [ ] OK mmc mmc2.bootdev
111 * 9 [ + ] OK mmc mmc1.bootdev
112 * a [ ] OK mmc mmc0.bootdev
113 */
114 ut_assertok(run_command("bootflow scan -lH 9", 0));
115 ut_assert_nextline("Scanning for bootflows with label '9'");
116 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
117
Simon Glass18552d22023-01-17 10:48:13 -0700118 ut_assertok(run_command("bootflow scan -lH 0", 0));
Simon Glass47aedc22023-01-17 10:48:14 -0700119 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassfb1451b2022-04-24 23:31:24 -0600120 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
121 ut_assert_console_end();
122
123 return 0;
124}
Simon Glass47aedc22023-01-17 10:48:14 -0700125BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
126 UT_TESTF_ETH_BOOTDEV);
Simon Glassfb1451b2022-04-24 23:31:24 -0600127
128/* Check 'bootflow scan/list' commands using all bootdevs */
129static int bootflow_cmd_glob(struct unit_test_state *uts)
130{
131 ut_assertok(bootstd_test_drop_bootdev_order(uts));
132
133 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -0700134 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -0600135 ut_assert_nextline("Scanning for bootflows in all bootdevs");
136 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
137 ut_assert_nextlinen("---");
138 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
139 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glass79f66352023-05-10 16:34:46 -0600140 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600141 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
142 ut_assert_nextline("No more bootdevs");
143 ut_assert_nextlinen("---");
144 ut_assert_nextline("(1 bootflow, 1 valid)");
145 ut_assert_console_end();
146
147 ut_assertok(run_command("bootflow list", 0));
148 ut_assert_nextline("Showing all bootflows");
149 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
150 ut_assert_nextlinen("---");
Simon Glass79f66352023-05-10 16:34:46 -0600151 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600152 ut_assert_nextlinen("---");
153 ut_assert_nextline("(1 bootflow, 1 valid)");
154 ut_assert_console_end();
155
156 return 0;
157}
158BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
159
160/* Check 'bootflow scan -e' */
161static int bootflow_cmd_scan_e(struct unit_test_state *uts)
162{
163 ut_assertok(bootstd_test_drop_bootdev_order(uts));
164
165 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -0700166 ut_assertok(run_command("bootflow scan -aleGH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -0600167 ut_assert_nextline("Scanning for bootflows in all bootdevs");
168 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
169 ut_assert_nextlinen("---");
170 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
Simon Glassc3867e22023-08-24 13:55:39 -0600171 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600172 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassc3867e22023-08-24 13:55:39 -0600173 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600174 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassfb1451b2022-04-24 23:31:24 -0600175
176 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassc3867e22023-08-24 13:55:39 -0600177 ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600178 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassc3867e22023-08-24 13:55:39 -0600179 ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600180 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glass79f66352023-05-10 16:34:46 -0600181 ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600182 ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 efi/boot/bootsbox.efi");
183
184 ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
Simon Glassc3867e22023-08-24 13:55:39 -0600185 ut_assert_skip_to_line(
186 " 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600187 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassfb1451b2022-04-24 23:31:24 -0600188 ut_assert_nextline("No more bootdevs");
189 ut_assert_nextlinen("---");
190 ut_assert_nextline("(64 bootflows, 1 valid)");
191 ut_assert_console_end();
192
193 ut_assertok(run_command("bootflow list", 0));
194 ut_assert_nextline("Showing all bootflows");
195 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
196 ut_assert_nextlinen("---");
Simon Glassc3867e22023-08-24 13:55:39 -0600197 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
198 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
199 ut_assert_skip_to_line(
200 " 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
201 ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glassfb1451b2022-04-24 23:31:24 -0600202 ut_assert_nextlinen("---");
203 ut_assert_nextline("(64 bootflows, 1 valid)");
204 ut_assert_console_end();
205
206 return 0;
207}
208BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
209
210/* Check 'bootflow info' */
211static int bootflow_cmd_info(struct unit_test_state *uts)
212{
213 console_record_reset_enable();
214 ut_assertok(run_command("bootdev select 1", 0));
215 ut_assert_console_end();
216 ut_assertok(run_command("bootflow scan", 0));
217 ut_assert_console_end();
218 ut_assertok(run_command("bootflow select 0", 0));
219 ut_assert_console_end();
220 ut_assertok(run_command("bootflow info", 0));
221 ut_assert_nextline("Name: mmc1.bootdev.part_1");
222 ut_assert_nextline("Device: mmc1.bootdev");
223 ut_assert_nextline("Block dev: mmc1.blk");
Simon Glass79f66352023-05-10 16:34:46 -0600224 ut_assert_nextline("Method: extlinux");
Simon Glassfb1451b2022-04-24 23:31:24 -0600225 ut_assert_nextline("State: ready");
226 ut_assert_nextline("Partition: 1");
227 ut_assert_nextline("Subdir: (none)");
228 ut_assert_nextline("Filename: /extlinux/extlinux.conf");
229 ut_assert_nextlinen("Buffer: ");
230 ut_assert_nextline("Size: 253 (595 bytes)");
Simon Glass2175e762023-01-06 08:52:33 -0600231 ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
Simon Glassf4a91652023-07-12 09:04:34 -0600232 ut_assert_nextline("Cmdline: (none)");
Simon Glass24d8e1b2023-01-06 08:52:34 -0600233 ut_assert_nextline("Logo: (none)");
Simon Glass7638c852023-01-17 10:47:56 -0700234 ut_assert_nextline("FDT: <NULL>");
Simon Glassfb1451b2022-04-24 23:31:24 -0600235 ut_assert_nextline("Error: 0");
236 ut_assert_console_end();
237
238 ut_assertok(run_command("bootflow info -d", 0));
239 ut_assert_nextline("Name: mmc1.bootdev.part_1");
240 ut_assert_skip_to_line("Error: 0");
241 ut_assert_nextline("Contents:");
242 ut_assert_nextline("%s", "");
243 ut_assert_nextline("# extlinux.conf generated by appliance-creator");
244 ut_assert_skip_to_line(" initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
245 ut_assert_console_end();
246
247 return 0;
248}
249BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
250
251/* Check 'bootflow scan -b' to boot the first available bootdev */
252static int bootflow_scan_boot(struct unit_test_state *uts)
253{
254 console_record_reset_enable();
Simon Glassf25f5752022-07-30 15:52:16 -0600255 ut_assertok(inject_response(uts));
Simon Glassfb1451b2022-04-24 23:31:24 -0600256 ut_assertok(run_command("bootflow scan -b", 0));
257 ut_assert_nextline(
Simon Glass79f66352023-05-10 16:34:46 -0600258 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassfb1451b2022-04-24 23:31:24 -0600259 ut_assert_nextline("Ignoring unknown command: ui");
260
261 /*
262 * We expect it to get through to boot although sandbox always returns
263 * -EFAULT as it cannot actually boot the kernel
264 */
265 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
266 ut_assert_nextline("Boot failed (err=-14)");
267 ut_assert_console_end();
268
269 return 0;
270}
271BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
272
273/* Check iterating through available bootflows */
274static int bootflow_iter(struct unit_test_state *uts)
275{
276 struct bootflow_iter iter;
277 struct bootflow bflow;
278
279 bootstd_clear_glob();
280
281 /* The first device is mmc2.bootdev which has no media */
282 ut_asserteq(-EPROTONOSUPPORT,
Simon Glass4b7cb052023-01-17 10:48:16 -0700283 bootflow_scan_first(NULL, NULL, &iter,
Simon Glass4f806f32023-02-22 12:17:03 -0700284 BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600285 ut_asserteq(2, iter.num_methods);
286 ut_asserteq(0, iter.cur_method);
287 ut_asserteq(0, iter.part);
288 ut_asserteq(0, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600289 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600290 ut_asserteq(0, bflow.err);
291
292 /*
Simon Glass0917f772022-07-30 15:52:34 -0600293 * This shows MEDIA even though there is none, since in
Simon Glassfb1451b2022-04-24 23:31:24 -0600294 * bootdev_find_in_blk() we call part_get_info() which returns
295 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
296 * know.
297 */
298 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
299
300 ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
301 ut_asserteq(2, iter.num_methods);
302 ut_asserteq(1, iter.cur_method);
303 ut_asserteq(0, iter.part);
304 ut_asserteq(0, iter.max_part);
305 ut_asserteq_str("efi", iter.method->name);
306 ut_asserteq(0, bflow.err);
307 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
308 bootflow_free(&bflow);
309
310 /* The next device is mmc1.bootdev - at first we use the whole device */
311 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
312 ut_asserteq(2, iter.num_methods);
313 ut_asserteq(0, iter.cur_method);
314 ut_asserteq(0, iter.part);
315 ut_asserteq(0x1e, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600316 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600317 ut_asserteq(0, bflow.err);
318 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
319 bootflow_free(&bflow);
320
321 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
322 ut_asserteq(2, iter.num_methods);
323 ut_asserteq(1, iter.cur_method);
324 ut_asserteq(0, iter.part);
325 ut_asserteq(0x1e, iter.max_part);
326 ut_asserteq_str("efi", iter.method->name);
327 ut_asserteq(0, bflow.err);
328 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
329 bootflow_free(&bflow);
330
331 /* Then more to partition 1 where we find something */
332 ut_assertok(bootflow_scan_next(&iter, &bflow));
333 ut_asserteq(2, iter.num_methods);
334 ut_asserteq(0, iter.cur_method);
335 ut_asserteq(1, iter.part);
336 ut_asserteq(0x1e, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600337 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600338 ut_asserteq(0, bflow.err);
339 ut_asserteq(BOOTFLOWST_READY, bflow.state);
340 bootflow_free(&bflow);
341
342 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
343 ut_asserteq(2, iter.num_methods);
344 ut_asserteq(1, iter.cur_method);
345 ut_asserteq(1, iter.part);
346 ut_asserteq(0x1e, iter.max_part);
347 ut_asserteq_str("efi", iter.method->name);
348 ut_asserteq(0, bflow.err);
349 ut_asserteq(BOOTFLOWST_FS, bflow.state);
350 bootflow_free(&bflow);
351
Simon Glassdcffa442023-01-17 10:47:41 -0700352 /* Then more to partition 2 which exists but is not bootable */
Simon Glassf0e358f2023-01-17 10:47:42 -0700353 ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600354 ut_asserteq(2, iter.num_methods);
355 ut_asserteq(0, iter.cur_method);
356 ut_asserteq(2, iter.part);
357 ut_asserteq(0x1e, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600358 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600359 ut_asserteq(0, bflow.err);
Simon Glassf0e358f2023-01-17 10:47:42 -0700360 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
Simon Glassfb1451b2022-04-24 23:31:24 -0600361 bootflow_free(&bflow);
362
363 bootflow_iter_uninit(&iter);
364
365 ut_assert_console_end();
366
367 return 0;
368}
369BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
370
Simon Glass11361c52022-07-30 15:52:36 -0600371#if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
Simon Glassfb1451b2022-04-24 23:31:24 -0600372/* Check using the system bootdev */
373static int bootflow_system(struct unit_test_state *uts)
374{
Simon Glassecb274c2023-01-17 10:47:23 -0700375 struct udevice *bootstd, *dev;
Simon Glassfb1451b2022-04-24 23:31:24 -0600376
Simon Glassae0bf222022-10-11 09:47:20 -0600377 if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
Simon Glassc43635b2022-10-20 18:22:49 -0600378 return -EAGAIN;
Simon Glassecb274c2023-01-17 10:47:23 -0700379 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
380 ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_efi_mgr),
381 "efi_mgr", 0, ofnode_null(), &dev));
382 ut_assertok(device_probe(dev));
Simon Glass2662b542022-07-30 15:52:22 -0600383 sandbox_set_fake_efi_mgr_dev(dev, true);
Simon Glassfb1451b2022-04-24 23:31:24 -0600384
Simon Glassfb1451b2022-04-24 23:31:24 -0600385 /* We should get a single 'bootmgr' method right at the end */
386 bootstd_clear_glob();
387 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -0700388 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassc627cfc2022-07-30 15:52:27 -0600389 ut_assert_skip_to_line(
Simon Glassc3867e22023-08-24 13:55:39 -0600390 " 0 efi_mgr ready (none) 0 <NULL> ");
Simon Glassc627cfc2022-07-30 15:52:27 -0600391 ut_assert_skip_to_line("No more bootdevs");
Simon Glassecb274c2023-01-17 10:47:23 -0700392 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
Simon Glassfb1451b2022-04-24 23:31:24 -0600393 ut_assert_console_end();
394
395 return 0;
396}
Simon Glassbd18b692022-07-30 15:52:28 -0600397BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_PDATA |
398 UT_TESTF_SCAN_FDT);
Simon Glass11361c52022-07-30 15:52:36 -0600399#endif
Simon Glassfb1451b2022-04-24 23:31:24 -0600400
401/* Check disabling a bootmethod if it requests it */
402static int bootflow_iter_disable(struct unit_test_state *uts)
403{
404 struct udevice *bootstd, *dev;
405 struct bootflow_iter iter;
406 struct bootflow bflow;
407 int i;
408
409 /* Add the EFI bootmgr driver */
410 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
411 ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
412 &dev));
413
Simon Glassfb1451b2022-04-24 23:31:24 -0600414 ut_assertok(bootstd_test_drop_bootdev_order(uts));
415
416 bootstd_clear_glob();
Simon Glassf25f5752022-07-30 15:52:16 -0600417 console_record_reset_enable();
418 ut_assertok(inject_response(uts));
Simon Glass18552d22023-01-17 10:48:13 -0700419 ut_assertok(run_command("bootflow scan -lbH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -0600420
421 /* Try to boot the bootmgr flow, which will fail */
422 console_record_reset_enable();
Simon Glass4b7cb052023-01-17 10:48:16 -0700423 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600424 ut_asserteq(3, iter.num_methods);
425 ut_asserteq_str("sandbox", iter.method->name);
Simon Glassf25f5752022-07-30 15:52:16 -0600426 ut_assertok(inject_response(uts));
Simon Glassfb1451b2022-04-24 23:31:24 -0600427 ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
428
429 ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
430 ut_assert_console_end();
431
432 /* Check that the sandbox bootmeth has been removed */
433 ut_asserteq(2, iter.num_methods);
434 for (i = 0; i < iter.num_methods; i++)
435 ut_assert(strcmp("sandbox", iter.method_order[i]->name));
436
437 return 0;
438}
439BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
440
Simon Glass0917f772022-07-30 15:52:34 -0600441/* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
442static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
443{
Simon Glass11361c52022-07-30 15:52:36 -0600444 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc43635b2022-10-20 18:22:49 -0600445 return -EAGAIN;
Simon Glass11361c52022-07-30 15:52:36 -0600446
Simon Glass0917f772022-07-30 15:52:34 -0600447 ut_assertok(bootstd_test_drop_bootdev_order(uts));
448
449 /*
450 * Make sure that the -G flag makes the scan fail, since this is not
451 * supported when an ordering is provided
452 */
453 console_record_reset_enable();
454 ut_assertok(bootmeth_set_order("efi firmware0"));
Simon Glass18552d22023-01-17 10:48:13 -0700455 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glass0917f772022-07-30 15:52:34 -0600456 ut_assert_nextline("Scanning for bootflows in all bootdevs");
457 ut_assert_nextline(
458 "Seq Method State Uclass Part Name Filename");
459 ut_assert_nextlinen("---");
460 ut_assert_nextlinen("---");
461 ut_assert_nextline("(0 bootflows, 0 valid)");
462 ut_assert_console_end();
463
Simon Glass18552d22023-01-17 10:48:13 -0700464 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glass0917f772022-07-30 15:52:34 -0600465 ut_assert_nextline("Scanning for bootflows in all bootdevs");
466 ut_assert_nextline(
467 "Seq Method State Uclass Part Name Filename");
468 ut_assert_nextlinen("---");
469 ut_assert_nextline("Scanning global bootmeth 'firmware0':");
470 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
471 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
472 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
473 ut_assert_nextline("No more bootdevs");
474 ut_assert_nextlinen("---");
475 ut_assert_nextline("(0 bootflows, 0 valid)");
476 ut_assert_console_end();
477
478 return 0;
479}
480BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
481
Simon Glassfb1451b2022-04-24 23:31:24 -0600482/* Check 'bootflow boot' to boot a selected bootflow */
483static int bootflow_cmd_boot(struct unit_test_state *uts)
484{
485 console_record_reset_enable();
486 ut_assertok(run_command("bootdev select 1", 0));
487 ut_assert_console_end();
488 ut_assertok(run_command("bootflow scan", 0));
489 ut_assert_console_end();
490 ut_assertok(run_command("bootflow select 0", 0));
491 ut_assert_console_end();
Simon Glassf25f5752022-07-30 15:52:16 -0600492
493 ut_assertok(inject_response(uts));
Simon Glassfb1451b2022-04-24 23:31:24 -0600494 ut_asserteq(1, run_command("bootflow boot", 0));
495 ut_assert_nextline(
Simon Glass79f66352023-05-10 16:34:46 -0600496 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassfb1451b2022-04-24 23:31:24 -0600497 ut_assert_nextline("Ignoring unknown command: ui");
498
499 /*
500 * We expect it to get through to boot although sandbox always returns
501 * -EFAULT as it cannot actually boot the kernel
502 */
503 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
504 ut_assert_nextline("Boot failed (err=-14)");
505 ut_assert_console_end();
506
507 return 0;
508}
509BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassd985f1d2023-01-06 08:52:41 -0600510
Simon Glasse64c2952023-01-06 08:52:42 -0600511/**
Simon Glass2b9adca2023-08-24 13:55:40 -0600512 * prep_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
Simon Glasse64c2952023-01-06 08:52:42 -0600513 *
Simon Glassbaea7ec2023-10-01 19:14:37 -0600514 * After calling this function, set std->bootdev_order to *@old_orderp to
515 * restore normal operation of bootstd (i.e. with the original bootdev order)
516 *
Simon Glasse64c2952023-01-06 08:52:42 -0600517 * @uts: Unit test state
Simon Glassbaea7ec2023-10-01 19:14:37 -0600518 * @mmc_dev: MMC device to use, e.g. "mmc4". Note that this must remain valid
519 * in the caller until
520 * @bind_cros: true to bind the ChromiumOS bootmeth
521 * @old_orderp: Returns the original bootdev order, which must be restored
Simon Glasse64c2952023-01-06 08:52:42 -0600522 * Returns 0 on success, -ve on failure
523 */
Simon Glassd08db022023-08-24 13:55:41 -0600524static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
Simon Glassbaea7ec2023-10-01 19:14:37 -0600525 bool bind_cros, const char ***old_orderp)
Simon Glassd985f1d2023-01-06 08:52:41 -0600526{
Simon Glassbaea7ec2023-10-01 19:14:37 -0600527 static const char *order[] = {"mmc2", "mmc1", NULL, NULL};
Simon Glassd985f1d2023-01-06 08:52:41 -0600528 struct udevice *dev, *bootstd;
529 struct bootstd_priv *std;
530 const char **old_order;
Simon Glass2b9adca2023-08-24 13:55:40 -0600531 ofnode root, node;
Simon Glassd985f1d2023-01-06 08:52:41 -0600532
Simon Glassbaea7ec2023-10-01 19:14:37 -0600533 order[2] = mmc_dev;
534
Simon Glassd985f1d2023-01-06 08:52:41 -0600535 /* Enable the mmc4 node since we need a second bootflow */
Simon Glass2b9adca2023-08-24 13:55:40 -0600536 root = oftree_root(oftree_default());
537 node = ofnode_find_subnode(root, mmc_dev);
538 ut_assert(ofnode_valid(node));
Simon Glassd985f1d2023-01-06 08:52:41 -0600539 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
540
541 /* Enable the script bootmeth too */
542 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
Simon Glass5986d462023-08-19 16:49:35 -0600543 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_2script),
Simon Glassd985f1d2023-01-06 08:52:41 -0600544 "bootmeth_script", 0, ofnode_null(), &dev));
545
Simon Glassd08db022023-08-24 13:55:41 -0600546 /* Enable the cros bootmeth if needed */
547 if (bind_cros) {
548 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
549 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_cros),
550 "cros", 0, ofnode_null(), &dev));
551 }
552
Simon Glass2b9adca2023-08-24 13:55:40 -0600553 /* Change the order to include the device */
Simon Glassd985f1d2023-01-06 08:52:41 -0600554 std = dev_get_priv(bootstd);
555 old_order = std->bootdev_order;
556 std->bootdev_order = order;
Simon Glassbaea7ec2023-10-01 19:14:37 -0600557 *old_orderp = old_order;
558
559 return 0;
560}
561
562/**
563 * scan_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
564 *
565 * @uts: Unit test state
566 * @mmc_dev: MMC device to use, e.g. "mmc4"
567 * @bind_cros: true to bind the ChromiumOS bootmeth
568 * Returns 0 on success, -ve on failure
569 */
570static int scan_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
571 bool bind_cros)
572{
573 struct bootstd_priv *std;
574 struct udevice *bootstd;
575 const char **old_order;
576
577 ut_assertok(prep_mmc_bootdev(uts, mmc_dev, bind_cros, &old_order));
Simon Glassd985f1d2023-01-06 08:52:41 -0600578
579 console_record_reset_enable();
580 ut_assertok(run_command("bootflow scan", 0));
581 ut_assert_console_end();
582
583 /* Restore the order used by the device tree */
Simon Glassbaea7ec2023-10-01 19:14:37 -0600584 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
585 std = dev_get_priv(bootstd);
Simon Glassd985f1d2023-01-06 08:52:41 -0600586 std->bootdev_order = old_order;
587
Simon Glasse64c2952023-01-06 08:52:42 -0600588 return 0;
589}
590
Simon Glass2b9adca2023-08-24 13:55:40 -0600591/**
Simon Glassbaea7ec2023-10-01 19:14:37 -0600592 * scan_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
Simon Glass2b9adca2023-08-24 13:55:40 -0600593 *
594 * @uts: Unit test state
595 * Returns 0 on success, -ve on failure
596 */
Simon Glassbaea7ec2023-10-01 19:14:37 -0600597static int scan_mmc4_bootdev(struct unit_test_state *uts)
Simon Glass2b9adca2023-08-24 13:55:40 -0600598{
Simon Glassbaea7ec2023-10-01 19:14:37 -0600599 ut_assertok(scan_mmc_bootdev(uts, "mmc4", false));
Simon Glass2b9adca2023-08-24 13:55:40 -0600600
601 return 0;
602}
603
Simon Glasse64c2952023-01-06 08:52:42 -0600604/* Check 'bootflow menu' to select a bootflow */
605static int bootflow_cmd_menu(struct unit_test_state *uts)
606{
607 char prev[3];
608
Simon Glassbaea7ec2023-10-01 19:14:37 -0600609 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glasse64c2952023-01-06 08:52:42 -0600610
Simon Glassd985f1d2023-01-06 08:52:41 -0600611 /* Add keypresses to move to and select the second one in the list */
612 prev[0] = CTL_CH('n');
613 prev[1] = '\r';
614 prev[2] = '\0';
615 ut_asserteq(2, console_in_puts(prev));
616
617 ut_assertok(run_command("bootflow menu", 0));
618 ut_assert_nextline("Selected: Armbian");
619 ut_assert_console_end();
620
621 return 0;
622}
623BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glasse64c2952023-01-06 08:52:42 -0600624
Simon Glass91943ff2023-01-17 10:48:15 -0700625/* Check searching for a single bootdev using the hunters */
626static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
627{
628 struct bootstd_priv *std;
629
630 /* get access to the used hunters */
631 ut_assertok(bootstd_get_priv(&std));
632
633 ut_assertok(bootstd_test_drop_bootdev_order(uts));
634
635 console_record_reset_enable();
636 ut_assertok(run_command("bootflow scan -l mmc1", 0));
637 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
638 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
639 ut_assert_console_end();
640
641 /* check that the hunter was used */
642 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
643
644 return 0;
645}
646BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
647
648/* Check searching for a uclass label using the hunters */
649static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
650{
651 struct bootstd_priv *std;
652
653 /* get access to the used hunters */
654 ut_assertok(bootstd_get_priv(&std));
655
656 test_set_skip_delays(true);
657 test_set_eth_enable(false);
658 ut_assertok(bootstd_test_drop_bootdev_order(uts));
659
660 console_record_reset_enable();
661 ut_assertok(run_command("bootflow scan -l mmc", 0));
662
663 /* check that the hunter was used */
664 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
665
666 /* check that we got the mmc1 bootflow */
667 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
668 ut_assert_nextlinen("Seq");
669 ut_assert_nextlinen("---");
670 ut_assert_nextline("Hunting with: simple_bus");
671 ut_assert_nextline("Found 2 extension board(s).");
672 ut_assert_nextline("Hunting with: mmc");
673 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
674 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
675 ut_assert_nextline(
Simon Glass79f66352023-05-10 16:34:46 -0600676 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass91943ff2023-01-17 10:48:15 -0700677 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
678 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
679 ut_assert_console_end();
680
681 return 0;
682}
683BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
684
Simon Glasse64c2952023-01-06 08:52:42 -0600685/**
686 * check_font() - Check that the font size for an item matches expectations
687 *
688 * @uts: Unit test state
689 * @scn: Scene containing the text object
690 * @id: ID of the text object
691 * Returns 0 on success, -ve on failure
692 */
693static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
694 int font_size)
695{
696 struct scene_obj_txt *txt;
697
698 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
699 ut_assertnonnull(txt);
700
701 ut_asserteq(font_size, txt->font_size);
702
703 return 0;
704}
705
706/* Check themes work with a bootflow menu */
707static int bootflow_menu_theme(struct unit_test_state *uts)
708{
709 const int font_size = 30;
710 struct scene *scn;
711 struct expo *exp;
712 ofnode node;
713 int i;
714
Simon Glassbaea7ec2023-10-01 19:14:37 -0600715 ut_assertok(scan_mmc4_bootdev(uts));
Simon Glasse64c2952023-01-06 08:52:42 -0600716
717 ut_assertok(bootflow_menu_new(&exp));
718 node = ofnode_path("/bootstd/theme");
719 ut_assert(ofnode_valid(node));
720 ut_assertok(bootflow_menu_apply_theme(exp, node));
721
722 scn = expo_lookup_scene_id(exp, MAIN);
723 ut_assertnonnull(scn);
724
725 /*
726 * Check that the txt objects have the correct font size from the
727 * device tree node: bootstd/theme
728 *
729 * Check both menu items, since there are two bootflows
730 */
731 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
732 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
733 for (i = 0; i < 2; i++) {
734 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
735 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
736 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
737 }
738
739 expo_destroy(exp);
740
741 return 0;
742}
743BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassd07861c2023-07-12 09:04:38 -0600744
745/**
746 * check_arg() - Check both the normal case and the buffer-overflow case
747 *
748 * @uts: Unit-test state
749 * @expect_ret: Expected return value (i.e. buffer length)
750 * @expect_str: String expected to be returned
751 * @buf: Buffer to use
752 * @from: Original cmdline to update
753 * @arg: Argument to update (e.g. "console")
754 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
755 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
756 * it without any value ("initrd")
757 */
758static int check_arg(struct unit_test_state *uts, int expect_ret,
759 const char *expect_str, char *buf, const char *from,
760 const char *arg, const char *val)
761{
762 /* check for writing outside the reported bounds */
763 buf[expect_ret] = '[';
764 ut_asserteq(expect_ret,
765 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
766 ut_asserteq_str(expect_str, buf);
767 ut_asserteq('[', buf[expect_ret]);
768
769 /* do the test again but with one less byte in the buffer */
770 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
771 val, NULL));
772
773 return 0;
774}
775
776/* Test of bootflow_cmdline_set_arg() */
777static int test_bootflow_cmdline_set(struct unit_test_state *uts)
778{
779 char buf[50];
780 const int size = sizeof(buf);
781
782 /*
783 * note that buffer-overflow tests are immediately each test case, just
784 * top keep the code together
785 */
786
787 /* add an arg that doesn't already exist, starting from empty */
788 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
789 NULL));
790
791 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
792 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
793 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
794
795 /* add an arg that doesn't already exist, starting from non-empty */
796 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
797 BOOTFLOWCL_EMPTY));
798 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
799 ""));
800 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
801 "fred"));
802
803 /* update an arg at the start */
804 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
805 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
806 BOOTFLOWCL_EMPTY));
807 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
808 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
809 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
810 "1234"));
811
812 /* update an arg at the end */
813 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
814 NULL));
815 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
816 BOOTFLOWCL_EMPTY));
817 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
818 ""));
819 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
820 "1"));
821 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
822 "arg", "1234"));
823
824 /* update an arg in the middle */
825 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
826 "mary=abc arg=123 john=2", "arg", NULL));
827 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
828 "mary=abc arg=123 john=2", "arg",
829 BOOTFLOWCL_EMPTY));
830 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
831 "mary=abc arg=123 john=2", "arg", ""));
832 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
833 "mary=abc arg=123 john=2", "arg", "1"));
834 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
835 "mary=abc arg=123 john=2", "arg", "1234"));
836
837 /* handle existing args with quotes */
838 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
839 "mary=\"abc\" arg=123 john", "arg", NULL));
840
841 /* handle existing args with quoted spaces */
842 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
843 "mary=\"abc def\" arg=123 john", "arg", NULL));
844
845 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
846 buf, "mary=\"abc def\" arg=123 john", "def",
847 "4"));
848
849 /* quote at the start */
850 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
851 "mary=\"abc def\" arg=\"123 456\"",
852 "arg", "\"4 5 6", NULL));
853
854 /* quote at the end */
855 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
856 "mary=\"abc def\" arg=\"123 456\"",
857 "arg", "4 5 6\"", NULL));
858
859 /* quote in the middle */
860 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
861 "mary=\"abc def\" arg=\"123 456\"",
862 "arg", "\"4 \"5 6\"", NULL));
863
864 /* handle updating a quoted arg */
865 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
866 "mary=\"abc def\" arg=\"123 456\"", "arg",
867 "4 5 6"));
868
869 /* changing a quoted arg to a non-quoted arg */
870 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
871 "mary=\"abc def\" arg=\"123 456\"", "arg",
872 "789"));
873
874 /* changing a non-quoted arg to a quoted arg */
875 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
876 "mary=\"abc def\" arg=123", "arg", "456 789"));
877
878 /* handling of spaces */
879 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
880 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
881 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
882 "123"));
883 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
884 "arg", "123"));
885 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
886 " john arg=123 mary ", "arg", "123"));
887
888 /* unchanged arg */
889 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
890
891 /* arg which starts with the same name */
892 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
893 "mary=abc johnathon=2 john=1", "john", "3"));
894
895 return 0;
896}
897BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
Simon Glass82c09382023-07-12 09:04:39 -0600898
899/* Test of bootflow_cmdline_set_arg() */
900static int bootflow_set_arg(struct unit_test_state *uts)
901{
902 struct bootflow s_bflow, *bflow = &s_bflow;
903 ulong mem_start;
904
905 ut_assertok(env_set("bootargs", NULL));
906
907 mem_start = ut_check_delta(0);
908
909 /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
910 bflow->cmdline = NULL;
911 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
912 ut_asserteq_str(bflow->cmdline, "fred=123");
913
914 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
915 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
916
917 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
918 ut_asserteq_str(bflow->cmdline, "fred=123");
919 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
920 ut_asserteq_ptr(bflow->cmdline, NULL);
921
922 ut_asserteq(0, ut_check_delta(mem_start));
923
924 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
925 ut_asserteq_str("mary=here", env_get("bootargs"));
926 ut_assertok(env_set("bootargs", NULL));
927
928 return 0;
929}
930BOOTSTD_TEST(bootflow_set_arg, 0);
931
932/* Test of bootflow_cmdline_get_arg() */
933static int bootflow_cmdline_get(struct unit_test_state *uts)
934{
935 int pos;
936
937 /* empty string */
938 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
939
940 /* arg with empty value */
941 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
942 ut_asserteq(5, pos);
943
944 /* arg with a value */
945 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
946 ut_asserteq(5, pos);
947
948 /* arg with a value */
949 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
950 ut_asserteq(12, pos);
951
952 /* arg with a value, after quoted arg */
953 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
954 ut_asserteq(16, pos);
955
956 /* arg in the middle */
957 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
958 &pos));
959 ut_asserteq(15, pos);
960
961 /* quoted arg */
962 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
963 "fred", &pos));
964 ut_asserteq(17, pos);
965
966 /* args starting with the same prefix */
967 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
968 &pos));
969 ut_asserteq(26, pos);
970
971 return 0;
972}
973BOOTSTD_TEST(bootflow_cmdline_get, 0);
974
975static int bootflow_cmdline(struct unit_test_state *uts)
976{
977 ut_assertok(run_command("bootflow scan mmc", 0));
978 ut_assertok(run_command("bootflow sel 0", 0));
979 console_record_reset_enable();
980
981 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
982 ut_assert_nextline("Argument not found");
983 ut_assert_console_end();
984
985 ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
986 ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
987 ut_assert_nextline("123");
988
989 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
990 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
991 ut_assert_nextline("abc");
992
993 ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
994 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
995 ut_assert_nextline("Argument not found");
996
997 ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
998 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
999 ut_assert_nextline_empty();
1000
1001 ut_assert_console_end();
1002
1003 return 0;
1004}
1005BOOTSTD_TEST(bootflow_cmdline, 0);
Simon Glassd08db022023-08-24 13:55:41 -06001006
Simon Glass19248dc2023-10-25 07:17:36 +13001007/* test a few special changes to a long command line */
1008static int bootflow_cmdline_special(struct unit_test_state *uts)
1009{
1010 char buf[500];
1011 int pos;
1012
1013 /*
1014 * check handling of an argument which has an embedded '=', as well as
1015 * handling of a argument which partially matches ("ro" and "root")
1016 */
1017 ut_asserteq(32, cmdline_set_arg(
1018 buf, sizeof(buf),
1019 "loglevel=7 root=PARTUUID=d68352e3 rootwait ro noinitrd",
1020 "root", NULL, &pos));
1021 ut_asserteq_str("loglevel=7 rootwait ro noinitrd", buf);
1022
1023 return 0;
1024}
1025BOOTSTD_TEST(bootflow_cmdline_special, 0);
1026
Simon Glassd08db022023-08-24 13:55:41 -06001027/* Test ChromiumOS bootmeth */
1028static int bootflow_cros(struct unit_test_state *uts)
1029{
Simon Glassbaea7ec2023-10-01 19:14:37 -06001030 ut_assertok(scan_mmc_bootdev(uts, "mmc5", true));
Simon Glassd08db022023-08-24 13:55:41 -06001031 ut_assertok(run_command("bootflow list", 0));
1032
1033 ut_assert_nextlinen("Showing all");
1034 ut_assert_nextlinen("Seq");
1035 ut_assert_nextlinen("---");
1036 ut_assert_nextlinen(" 0 extlinux");
Simon Glass71f634b2023-08-24 13:55:45 -06001037 ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 ");
1038 ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 ");
Simon Glassd08db022023-08-24 13:55:41 -06001039 ut_assert_nextlinen("---");
Simon Glass71f634b2023-08-24 13:55:45 -06001040 ut_assert_skip_to_line("(3 bootflows, 3 valid)");
Simon Glassd08db022023-08-24 13:55:41 -06001041
1042 ut_assert_console_end();
1043
1044 return 0;
1045}
1046BOOTSTD_TEST(bootflow_cros, 0);