blob: 54a878c4bd5d00a8031083efaa13bdf562c17159 [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
30extern U_BOOT_DRIVER(bootmeth_script);
31
Simon Glassf25f5752022-07-30 15:52:16 -060032static int inject_response(struct unit_test_state *uts)
33{
34 /*
35 * The image being booted presents a menu of options:
36 *
37 * Fedora-Workstation-armhfp-31-1.9 Boot Options.
38 * 1: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
39 * Enter choice:
40 *
41 * Provide input for this, to avoid waiting two seconds for a timeout.
42 */
43 ut_asserteq(2, console_in_puts("1\n"));
44
45 return 0;
46}
47
Simon Glassfb1451b2022-04-24 23:31:24 -060048/* Check 'bootflow scan/list' commands */
49static int bootflow_cmd(struct unit_test_state *uts)
50{
51 console_record_reset_enable();
52 ut_assertok(run_command("bootdev select 1", 0));
53 ut_assert_console_end();
Simon Glass18552d22023-01-17 10:48:13 -070054 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -060055 ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
56 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
57 ut_assert_nextlinen("---");
Simon Glass47aedc22023-01-17 10:48:14 -070058 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
59 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glass79f66352023-05-10 16:34:46 -060060 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass47aedc22023-01-17 10:48:14 -070061 ut_assert_nextline("No more bootdevs");
Simon Glassfb1451b2022-04-24 23:31:24 -060062 ut_assert_nextlinen("---");
63 ut_assert_nextline("(1 bootflow, 1 valid)");
64 ut_assert_console_end();
65
66 ut_assertok(run_command("bootflow list", 0));
67 ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'");
68 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
69 ut_assert_nextlinen("---");
Simon Glass79f66352023-05-10 16:34:46 -060070 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -060071 ut_assert_nextlinen("---");
72 ut_assert_nextline("(1 bootflow, 1 valid)");
73 ut_assert_console_end();
74
75 return 0;
76}
77BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
78
Simon Glass47aedc22023-01-17 10:48:14 -070079/* Check 'bootflow scan' with a label / seq */
Simon Glassfb1451b2022-04-24 23:31:24 -060080static int bootflow_cmd_label(struct unit_test_state *uts)
81{
Simon Glass47aedc22023-01-17 10:48:14 -070082 test_set_eth_enable(false);
83
Simon Glassfb1451b2022-04-24 23:31:24 -060084 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -070085 ut_assertok(run_command("bootflow scan -lH mmc1", 0));
Simon Glass47aedc22023-01-17 10:48:14 -070086 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
Simon Glassfb1451b2022-04-24 23:31:24 -060087 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
88 ut_assert_console_end();
89
Simon Glass47aedc22023-01-17 10:48:14 -070090 ut_assertok(run_command("bootflow scan -lH 0", 0));
91 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassfb1451b2022-04-24 23:31:24 -060092 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
93 ut_assert_console_end();
94
Simon Glass47aedc22023-01-17 10:48:14 -070095 /*
96 * with ethernet enabled we have 8 devices ahead of the mmc ones:
97 *
98 * ut_assertok(run_command("bootdev list", 0));
99 * Seq Probed Status Uclass Name
100 * --- ------ ------ -------- ------------------
101 * 0 [ + ] OK ethernet eth@10002000.bootdev
102 * 1 [ ] OK ethernet eth@10003000.bootdev
103 * 2 [ ] OK ethernet sbe5.bootdev
104 * 3 [ ] OK ethernet eth@10004000.bootdev
105 * 4 [ ] OK ethernet phy-test-eth.bootdev
106 * 5 [ ] OK ethernet dsa-test-eth.bootdev
107 * 6 [ ] OK ethernet dsa-test@0.bootdev
108 * 7 [ ] OK ethernet dsa-test@1.bootdev
109 * 8 [ ] OK mmc mmc2.bootdev
110 * 9 [ + ] OK mmc mmc1.bootdev
111 * a [ ] OK mmc mmc0.bootdev
112 */
113 ut_assertok(run_command("bootflow scan -lH 9", 0));
114 ut_assert_nextline("Scanning for bootflows with label '9'");
115 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
116
Simon Glass18552d22023-01-17 10:48:13 -0700117 ut_assertok(run_command("bootflow scan -lH 0", 0));
Simon Glass47aedc22023-01-17 10:48:14 -0700118 ut_assert_nextline("Scanning for bootflows with label '0'");
Simon Glassfb1451b2022-04-24 23:31:24 -0600119 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
120 ut_assert_console_end();
121
122 return 0;
123}
Simon Glass47aedc22023-01-17 10:48:14 -0700124BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
125 UT_TESTF_ETH_BOOTDEV);
Simon Glassfb1451b2022-04-24 23:31:24 -0600126
127/* Check 'bootflow scan/list' commands using all bootdevs */
128static int bootflow_cmd_glob(struct unit_test_state *uts)
129{
130 ut_assertok(bootstd_test_drop_bootdev_order(uts));
131
132 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -0700133 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -0600134 ut_assert_nextline("Scanning for bootflows in all bootdevs");
135 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
136 ut_assert_nextlinen("---");
137 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
138 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glass79f66352023-05-10 16:34:46 -0600139 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600140 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
141 ut_assert_nextline("No more bootdevs");
142 ut_assert_nextlinen("---");
143 ut_assert_nextline("(1 bootflow, 1 valid)");
144 ut_assert_console_end();
145
146 ut_assertok(run_command("bootflow list", 0));
147 ut_assert_nextline("Showing all bootflows");
148 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
149 ut_assert_nextlinen("---");
Simon Glass79f66352023-05-10 16:34:46 -0600150 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600151 ut_assert_nextlinen("---");
152 ut_assert_nextline("(1 bootflow, 1 valid)");
153 ut_assert_console_end();
154
155 return 0;
156}
157BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
158
159/* Check 'bootflow scan -e' */
160static int bootflow_cmd_scan_e(struct unit_test_state *uts)
161{
162 ut_assertok(bootstd_test_drop_bootdev_order(uts));
163
164 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -0700165 ut_assertok(run_command("bootflow scan -aleGH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -0600166 ut_assert_nextline("Scanning for bootflows in all bootdevs");
167 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
168 ut_assert_nextlinen("---");
169 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
Simon Glassc3867e22023-08-24 13:55:39 -0600170 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600171 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassc3867e22023-08-24 13:55:39 -0600172 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600173 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassfb1451b2022-04-24 23:31:24 -0600174
175 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
Simon Glassc3867e22023-08-24 13:55:39 -0600176 ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600177 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glassc3867e22023-08-24 13:55:39 -0600178 ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600179 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
Simon Glass79f66352023-05-10 16:34:46 -0600180 ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glassfb1451b2022-04-24 23:31:24 -0600181 ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 efi/boot/bootsbox.efi");
182
183 ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
Simon Glassc3867e22023-08-24 13:55:39 -0600184 ut_assert_skip_to_line(
185 " 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glassc8894342023-05-10 16:34:26 -0600186 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
Simon Glassfb1451b2022-04-24 23:31:24 -0600187 ut_assert_nextline("No more bootdevs");
188 ut_assert_nextlinen("---");
189 ut_assert_nextline("(64 bootflows, 1 valid)");
190 ut_assert_console_end();
191
192 ut_assertok(run_command("bootflow list", 0));
193 ut_assert_nextline("Showing all bootflows");
194 ut_assert_nextline("Seq Method State Uclass Part Name Filename");
195 ut_assert_nextlinen("---");
Simon Glassc3867e22023-08-24 13:55:39 -0600196 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
197 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
198 ut_assert_skip_to_line(
199 " 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
200 ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole ");
Simon Glassfb1451b2022-04-24 23:31:24 -0600201 ut_assert_nextlinen("---");
202 ut_assert_nextline("(64 bootflows, 1 valid)");
203 ut_assert_console_end();
204
205 return 0;
206}
207BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
208
209/* Check 'bootflow info' */
210static int bootflow_cmd_info(struct unit_test_state *uts)
211{
212 console_record_reset_enable();
213 ut_assertok(run_command("bootdev select 1", 0));
214 ut_assert_console_end();
215 ut_assertok(run_command("bootflow scan", 0));
216 ut_assert_console_end();
217 ut_assertok(run_command("bootflow select 0", 0));
218 ut_assert_console_end();
219 ut_assertok(run_command("bootflow info", 0));
220 ut_assert_nextline("Name: mmc1.bootdev.part_1");
221 ut_assert_nextline("Device: mmc1.bootdev");
222 ut_assert_nextline("Block dev: mmc1.blk");
Simon Glass79f66352023-05-10 16:34:46 -0600223 ut_assert_nextline("Method: extlinux");
Simon Glassfb1451b2022-04-24 23:31:24 -0600224 ut_assert_nextline("State: ready");
225 ut_assert_nextline("Partition: 1");
226 ut_assert_nextline("Subdir: (none)");
227 ut_assert_nextline("Filename: /extlinux/extlinux.conf");
228 ut_assert_nextlinen("Buffer: ");
229 ut_assert_nextline("Size: 253 (595 bytes)");
Simon Glass2175e762023-01-06 08:52:33 -0600230 ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
Simon Glassf4a91652023-07-12 09:04:34 -0600231 ut_assert_nextline("Cmdline: (none)");
Simon Glass24d8e1b2023-01-06 08:52:34 -0600232 ut_assert_nextline("Logo: (none)");
Simon Glass7638c852023-01-17 10:47:56 -0700233 ut_assert_nextline("FDT: <NULL>");
Simon Glassfb1451b2022-04-24 23:31:24 -0600234 ut_assert_nextline("Error: 0");
235 ut_assert_console_end();
236
237 ut_assertok(run_command("bootflow info -d", 0));
238 ut_assert_nextline("Name: mmc1.bootdev.part_1");
239 ut_assert_skip_to_line("Error: 0");
240 ut_assert_nextline("Contents:");
241 ut_assert_nextline("%s", "");
242 ut_assert_nextline("# extlinux.conf generated by appliance-creator");
243 ut_assert_skip_to_line(" initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
244 ut_assert_console_end();
245
246 return 0;
247}
248BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
249
250/* Check 'bootflow scan -b' to boot the first available bootdev */
251static int bootflow_scan_boot(struct unit_test_state *uts)
252{
253 console_record_reset_enable();
Simon Glassf25f5752022-07-30 15:52:16 -0600254 ut_assertok(inject_response(uts));
Simon Glassfb1451b2022-04-24 23:31:24 -0600255 ut_assertok(run_command("bootflow scan -b", 0));
256 ut_assert_nextline(
Simon Glass79f66352023-05-10 16:34:46 -0600257 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassfb1451b2022-04-24 23:31:24 -0600258 ut_assert_nextline("Ignoring unknown command: ui");
259
260 /*
261 * We expect it to get through to boot although sandbox always returns
262 * -EFAULT as it cannot actually boot the kernel
263 */
264 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
265 ut_assert_nextline("Boot failed (err=-14)");
266 ut_assert_console_end();
267
268 return 0;
269}
270BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
271
272/* Check iterating through available bootflows */
273static int bootflow_iter(struct unit_test_state *uts)
274{
275 struct bootflow_iter iter;
276 struct bootflow bflow;
277
278 bootstd_clear_glob();
279
280 /* The first device is mmc2.bootdev which has no media */
281 ut_asserteq(-EPROTONOSUPPORT,
Simon Glass4b7cb052023-01-17 10:48:16 -0700282 bootflow_scan_first(NULL, NULL, &iter,
Simon Glass4f806f32023-02-22 12:17:03 -0700283 BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600284 ut_asserteq(2, iter.num_methods);
285 ut_asserteq(0, iter.cur_method);
286 ut_asserteq(0, iter.part);
287 ut_asserteq(0, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600288 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600289 ut_asserteq(0, bflow.err);
290
291 /*
Simon Glass0917f772022-07-30 15:52:34 -0600292 * This shows MEDIA even though there is none, since in
Simon Glassfb1451b2022-04-24 23:31:24 -0600293 * bootdev_find_in_blk() we call part_get_info() which returns
294 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
295 * know.
296 */
297 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
298
299 ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
300 ut_asserteq(2, iter.num_methods);
301 ut_asserteq(1, iter.cur_method);
302 ut_asserteq(0, iter.part);
303 ut_asserteq(0, iter.max_part);
304 ut_asserteq_str("efi", iter.method->name);
305 ut_asserteq(0, bflow.err);
306 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
307 bootflow_free(&bflow);
308
309 /* The next device is mmc1.bootdev - at first we use the whole device */
310 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
311 ut_asserteq(2, iter.num_methods);
312 ut_asserteq(0, iter.cur_method);
313 ut_asserteq(0, iter.part);
314 ut_asserteq(0x1e, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600315 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600316 ut_asserteq(0, bflow.err);
317 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
318 bootflow_free(&bflow);
319
320 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
321 ut_asserteq(2, iter.num_methods);
322 ut_asserteq(1, iter.cur_method);
323 ut_asserteq(0, iter.part);
324 ut_asserteq(0x1e, iter.max_part);
325 ut_asserteq_str("efi", iter.method->name);
326 ut_asserteq(0, bflow.err);
327 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
328 bootflow_free(&bflow);
329
330 /* Then more to partition 1 where we find something */
331 ut_assertok(bootflow_scan_next(&iter, &bflow));
332 ut_asserteq(2, iter.num_methods);
333 ut_asserteq(0, iter.cur_method);
334 ut_asserteq(1, iter.part);
335 ut_asserteq(0x1e, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600336 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600337 ut_asserteq(0, bflow.err);
338 ut_asserteq(BOOTFLOWST_READY, bflow.state);
339 bootflow_free(&bflow);
340
341 ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
342 ut_asserteq(2, iter.num_methods);
343 ut_asserteq(1, iter.cur_method);
344 ut_asserteq(1, iter.part);
345 ut_asserteq(0x1e, iter.max_part);
346 ut_asserteq_str("efi", iter.method->name);
347 ut_asserteq(0, bflow.err);
348 ut_asserteq(BOOTFLOWST_FS, bflow.state);
349 bootflow_free(&bflow);
350
Simon Glassdcffa442023-01-17 10:47:41 -0700351 /* Then more to partition 2 which exists but is not bootable */
Simon Glassf0e358f2023-01-17 10:47:42 -0700352 ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600353 ut_asserteq(2, iter.num_methods);
354 ut_asserteq(0, iter.cur_method);
355 ut_asserteq(2, iter.part);
356 ut_asserteq(0x1e, iter.max_part);
Simon Glass79f66352023-05-10 16:34:46 -0600357 ut_asserteq_str("extlinux", iter.method->name);
Simon Glassfb1451b2022-04-24 23:31:24 -0600358 ut_asserteq(0, bflow.err);
Simon Glassf0e358f2023-01-17 10:47:42 -0700359 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
Simon Glassfb1451b2022-04-24 23:31:24 -0600360 bootflow_free(&bflow);
361
362 bootflow_iter_uninit(&iter);
363
364 ut_assert_console_end();
365
366 return 0;
367}
368BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
369
Simon Glass11361c52022-07-30 15:52:36 -0600370#if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
Simon Glassfb1451b2022-04-24 23:31:24 -0600371/* Check using the system bootdev */
372static int bootflow_system(struct unit_test_state *uts)
373{
Simon Glassecb274c2023-01-17 10:47:23 -0700374 struct udevice *bootstd, *dev;
Simon Glassfb1451b2022-04-24 23:31:24 -0600375
Simon Glassae0bf222022-10-11 09:47:20 -0600376 if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
Simon Glassc43635b2022-10-20 18:22:49 -0600377 return -EAGAIN;
Simon Glassecb274c2023-01-17 10:47:23 -0700378 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
379 ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_efi_mgr),
380 "efi_mgr", 0, ofnode_null(), &dev));
381 ut_assertok(device_probe(dev));
Simon Glass2662b542022-07-30 15:52:22 -0600382 sandbox_set_fake_efi_mgr_dev(dev, true);
Simon Glassfb1451b2022-04-24 23:31:24 -0600383
Simon Glassfb1451b2022-04-24 23:31:24 -0600384 /* We should get a single 'bootmgr' method right at the end */
385 bootstd_clear_glob();
386 console_record_reset_enable();
Simon Glass18552d22023-01-17 10:48:13 -0700387 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glassc627cfc2022-07-30 15:52:27 -0600388 ut_assert_skip_to_line(
Simon Glassc3867e22023-08-24 13:55:39 -0600389 " 0 efi_mgr ready (none) 0 <NULL> ");
Simon Glassc627cfc2022-07-30 15:52:27 -0600390 ut_assert_skip_to_line("No more bootdevs");
Simon Glassecb274c2023-01-17 10:47:23 -0700391 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
Simon Glassfb1451b2022-04-24 23:31:24 -0600392 ut_assert_console_end();
393
394 return 0;
395}
Simon Glassbd18b692022-07-30 15:52:28 -0600396BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_PDATA |
397 UT_TESTF_SCAN_FDT);
Simon Glass11361c52022-07-30 15:52:36 -0600398#endif
Simon Glassfb1451b2022-04-24 23:31:24 -0600399
400/* Check disabling a bootmethod if it requests it */
401static int bootflow_iter_disable(struct unit_test_state *uts)
402{
403 struct udevice *bootstd, *dev;
404 struct bootflow_iter iter;
405 struct bootflow bflow;
406 int i;
407
408 /* Add the EFI bootmgr driver */
409 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
410 ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
411 &dev));
412
Simon Glassfb1451b2022-04-24 23:31:24 -0600413 ut_assertok(bootstd_test_drop_bootdev_order(uts));
414
415 bootstd_clear_glob();
Simon Glassf25f5752022-07-30 15:52:16 -0600416 console_record_reset_enable();
417 ut_assertok(inject_response(uts));
Simon Glass18552d22023-01-17 10:48:13 -0700418 ut_assertok(run_command("bootflow scan -lbH", 0));
Simon Glassfb1451b2022-04-24 23:31:24 -0600419
420 /* Try to boot the bootmgr flow, which will fail */
421 console_record_reset_enable();
Simon Glass4b7cb052023-01-17 10:48:16 -0700422 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
Simon Glassfb1451b2022-04-24 23:31:24 -0600423 ut_asserteq(3, iter.num_methods);
424 ut_asserteq_str("sandbox", iter.method->name);
Simon Glassf25f5752022-07-30 15:52:16 -0600425 ut_assertok(inject_response(uts));
Simon Glassfb1451b2022-04-24 23:31:24 -0600426 ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
427
428 ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
429 ut_assert_console_end();
430
431 /* Check that the sandbox bootmeth has been removed */
432 ut_asserteq(2, iter.num_methods);
433 for (i = 0; i < iter.num_methods; i++)
434 ut_assert(strcmp("sandbox", iter.method_order[i]->name));
435
436 return 0;
437}
438BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
439
Simon Glass0917f772022-07-30 15:52:34 -0600440/* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
441static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
442{
Simon Glass11361c52022-07-30 15:52:36 -0600443 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc43635b2022-10-20 18:22:49 -0600444 return -EAGAIN;
Simon Glass11361c52022-07-30 15:52:36 -0600445
Simon Glass0917f772022-07-30 15:52:34 -0600446 ut_assertok(bootstd_test_drop_bootdev_order(uts));
447
448 /*
449 * Make sure that the -G flag makes the scan fail, since this is not
450 * supported when an ordering is provided
451 */
452 console_record_reset_enable();
453 ut_assertok(bootmeth_set_order("efi firmware0"));
Simon Glass18552d22023-01-17 10:48:13 -0700454 ut_assertok(run_command("bootflow scan -lGH", 0));
Simon Glass0917f772022-07-30 15:52:34 -0600455 ut_assert_nextline("Scanning for bootflows in all bootdevs");
456 ut_assert_nextline(
457 "Seq Method State Uclass Part Name Filename");
458 ut_assert_nextlinen("---");
459 ut_assert_nextlinen("---");
460 ut_assert_nextline("(0 bootflows, 0 valid)");
461 ut_assert_console_end();
462
Simon Glass18552d22023-01-17 10:48:13 -0700463 ut_assertok(run_command("bootflow scan -lH", 0));
Simon Glass0917f772022-07-30 15:52:34 -0600464 ut_assert_nextline("Scanning for bootflows in all bootdevs");
465 ut_assert_nextline(
466 "Seq Method State Uclass Part Name Filename");
467 ut_assert_nextlinen("---");
468 ut_assert_nextline("Scanning global bootmeth 'firmware0':");
469 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
470 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
471 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
472 ut_assert_nextline("No more bootdevs");
473 ut_assert_nextlinen("---");
474 ut_assert_nextline("(0 bootflows, 0 valid)");
475 ut_assert_console_end();
476
477 return 0;
478}
479BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
480
Simon Glassfb1451b2022-04-24 23:31:24 -0600481/* Check 'bootflow boot' to boot a selected bootflow */
482static int bootflow_cmd_boot(struct unit_test_state *uts)
483{
484 console_record_reset_enable();
485 ut_assertok(run_command("bootdev select 1", 0));
486 ut_assert_console_end();
487 ut_assertok(run_command("bootflow scan", 0));
488 ut_assert_console_end();
489 ut_assertok(run_command("bootflow select 0", 0));
490 ut_assert_console_end();
Simon Glassf25f5752022-07-30 15:52:16 -0600491
492 ut_assertok(inject_response(uts));
Simon Glassfb1451b2022-04-24 23:31:24 -0600493 ut_asserteq(1, run_command("bootflow boot", 0));
494 ut_assert_nextline(
Simon Glass79f66352023-05-10 16:34:46 -0600495 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
Simon Glassfb1451b2022-04-24 23:31:24 -0600496 ut_assert_nextline("Ignoring unknown command: ui");
497
498 /*
499 * We expect it to get through to boot although sandbox always returns
500 * -EFAULT as it cannot actually boot the kernel
501 */
502 ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
503 ut_assert_nextline("Boot failed (err=-14)");
504 ut_assert_console_end();
505
506 return 0;
507}
508BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassd985f1d2023-01-06 08:52:41 -0600509
Simon Glasse64c2952023-01-06 08:52:42 -0600510/**
Simon Glass2b9adca2023-08-24 13:55:40 -0600511 * prep_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
Simon Glasse64c2952023-01-06 08:52:42 -0600512 *
513 * @uts: Unit test state
Simon Glass2b9adca2023-08-24 13:55:40 -0600514 * @mmc_dev: MMC device to use, e.g. "mmc4"
Simon Glasse64c2952023-01-06 08:52:42 -0600515 * Returns 0 on success, -ve on failure
516 */
Simon Glass2b9adca2023-08-24 13:55:40 -0600517static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev)
Simon Glassd985f1d2023-01-06 08:52:41 -0600518{
Simon Glass2b9adca2023-08-24 13:55:40 -0600519 const char *order[] = {"mmc2", "mmc1", mmc_dev, NULL};
Simon Glassd985f1d2023-01-06 08:52:41 -0600520 struct udevice *dev, *bootstd;
521 struct bootstd_priv *std;
522 const char **old_order;
Simon Glass2b9adca2023-08-24 13:55:40 -0600523 ofnode root, node;
Simon Glassd985f1d2023-01-06 08:52:41 -0600524
525 /* Enable the mmc4 node since we need a second bootflow */
Simon Glass2b9adca2023-08-24 13:55:40 -0600526 root = oftree_root(oftree_default());
527 node = ofnode_find_subnode(root, mmc_dev);
528 ut_assert(ofnode_valid(node));
Simon Glassd985f1d2023-01-06 08:52:41 -0600529 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
530
531 /* Enable the script bootmeth too */
532 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
533 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_script),
534 "bootmeth_script", 0, ofnode_null(), &dev));
535
Simon Glass2b9adca2023-08-24 13:55:40 -0600536 /* Change the order to include the device */
Simon Glassd985f1d2023-01-06 08:52:41 -0600537 std = dev_get_priv(bootstd);
538 old_order = std->bootdev_order;
539 std->bootdev_order = order;
540
541 console_record_reset_enable();
542 ut_assertok(run_command("bootflow scan", 0));
543 ut_assert_console_end();
544
545 /* Restore the order used by the device tree */
546 std->bootdev_order = old_order;
547
Simon Glasse64c2952023-01-06 08:52:42 -0600548 return 0;
549}
550
Simon Glass2b9adca2023-08-24 13:55:40 -0600551/**
552 * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
553 *
554 * @uts: Unit test state
555 * Returns 0 on success, -ve on failure
556 */
557static int prep_mmc4_bootdev(struct unit_test_state *uts)
558{
559 ut_assertok(prep_mmc_bootdev(uts, "mmc4"));
560
561 return 0;
562}
563
Simon Glasse64c2952023-01-06 08:52:42 -0600564/* Check 'bootflow menu' to select a bootflow */
565static int bootflow_cmd_menu(struct unit_test_state *uts)
566{
567 char prev[3];
568
569 ut_assertok(prep_mmc4_bootdev(uts));
570
Simon Glassd985f1d2023-01-06 08:52:41 -0600571 /* Add keypresses to move to and select the second one in the list */
572 prev[0] = CTL_CH('n');
573 prev[1] = '\r';
574 prev[2] = '\0';
575 ut_asserteq(2, console_in_puts(prev));
576
577 ut_assertok(run_command("bootflow menu", 0));
578 ut_assert_nextline("Selected: Armbian");
579 ut_assert_console_end();
580
581 return 0;
582}
583BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glasse64c2952023-01-06 08:52:42 -0600584
Simon Glass91943ff2023-01-17 10:48:15 -0700585/* Check searching for a single bootdev using the hunters */
586static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
587{
588 struct bootstd_priv *std;
589
590 /* get access to the used hunters */
591 ut_assertok(bootstd_get_priv(&std));
592
593 ut_assertok(bootstd_test_drop_bootdev_order(uts));
594
595 console_record_reset_enable();
596 ut_assertok(run_command("bootflow scan -l mmc1", 0));
597 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
598 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
599 ut_assert_console_end();
600
601 /* check that the hunter was used */
602 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
603
604 return 0;
605}
606BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
607
608/* Check searching for a uclass label using the hunters */
609static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
610{
611 struct bootstd_priv *std;
612
613 /* get access to the used hunters */
614 ut_assertok(bootstd_get_priv(&std));
615
616 test_set_skip_delays(true);
617 test_set_eth_enable(false);
618 ut_assertok(bootstd_test_drop_bootdev_order(uts));
619
620 console_record_reset_enable();
621 ut_assertok(run_command("bootflow scan -l mmc", 0));
622
623 /* check that the hunter was used */
624 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
625
626 /* check that we got the mmc1 bootflow */
627 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
628 ut_assert_nextlinen("Seq");
629 ut_assert_nextlinen("---");
630 ut_assert_nextline("Hunting with: simple_bus");
631 ut_assert_nextline("Found 2 extension board(s).");
632 ut_assert_nextline("Hunting with: mmc");
633 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
634 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
635 ut_assert_nextline(
Simon Glass79f66352023-05-10 16:34:46 -0600636 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
Simon Glass91943ff2023-01-17 10:48:15 -0700637 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
638 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
639 ut_assert_console_end();
640
641 return 0;
642}
643BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
644
Simon Glasse64c2952023-01-06 08:52:42 -0600645/**
646 * check_font() - Check that the font size for an item matches expectations
647 *
648 * @uts: Unit test state
649 * @scn: Scene containing the text object
650 * @id: ID of the text object
651 * Returns 0 on success, -ve on failure
652 */
653static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
654 int font_size)
655{
656 struct scene_obj_txt *txt;
657
658 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
659 ut_assertnonnull(txt);
660
661 ut_asserteq(font_size, txt->font_size);
662
663 return 0;
664}
665
666/* Check themes work with a bootflow menu */
667static int bootflow_menu_theme(struct unit_test_state *uts)
668{
669 const int font_size = 30;
670 struct scene *scn;
671 struct expo *exp;
672 ofnode node;
673 int i;
674
675 ut_assertok(prep_mmc4_bootdev(uts));
676
677 ut_assertok(bootflow_menu_new(&exp));
678 node = ofnode_path("/bootstd/theme");
679 ut_assert(ofnode_valid(node));
680 ut_assertok(bootflow_menu_apply_theme(exp, node));
681
682 scn = expo_lookup_scene_id(exp, MAIN);
683 ut_assertnonnull(scn);
684
685 /*
686 * Check that the txt objects have the correct font size from the
687 * device tree node: bootstd/theme
688 *
689 * Check both menu items, since there are two bootflows
690 */
691 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
692 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
693 for (i = 0; i < 2; i++) {
694 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
695 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
696 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
697 }
698
699 expo_destroy(exp);
700
701 return 0;
702}
703BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassd07861c2023-07-12 09:04:38 -0600704
705/**
706 * check_arg() - Check both the normal case and the buffer-overflow case
707 *
708 * @uts: Unit-test state
709 * @expect_ret: Expected return value (i.e. buffer length)
710 * @expect_str: String expected to be returned
711 * @buf: Buffer to use
712 * @from: Original cmdline to update
713 * @arg: Argument to update (e.g. "console")
714 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
715 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
716 * it without any value ("initrd")
717 */
718static int check_arg(struct unit_test_state *uts, int expect_ret,
719 const char *expect_str, char *buf, const char *from,
720 const char *arg, const char *val)
721{
722 /* check for writing outside the reported bounds */
723 buf[expect_ret] = '[';
724 ut_asserteq(expect_ret,
725 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
726 ut_asserteq_str(expect_str, buf);
727 ut_asserteq('[', buf[expect_ret]);
728
729 /* do the test again but with one less byte in the buffer */
730 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
731 val, NULL));
732
733 return 0;
734}
735
736/* Test of bootflow_cmdline_set_arg() */
737static int test_bootflow_cmdline_set(struct unit_test_state *uts)
738{
739 char buf[50];
740 const int size = sizeof(buf);
741
742 /*
743 * note that buffer-overflow tests are immediately each test case, just
744 * top keep the code together
745 */
746
747 /* add an arg that doesn't already exist, starting from empty */
748 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
749 NULL));
750
751 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
752 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
753 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
754
755 /* add an arg that doesn't already exist, starting from non-empty */
756 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
757 BOOTFLOWCL_EMPTY));
758 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
759 ""));
760 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
761 "fred"));
762
763 /* update an arg at the start */
764 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
765 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
766 BOOTFLOWCL_EMPTY));
767 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
768 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
769 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
770 "1234"));
771
772 /* update an arg at the end */
773 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
774 NULL));
775 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
776 BOOTFLOWCL_EMPTY));
777 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
778 ""));
779 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
780 "1"));
781 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
782 "arg", "1234"));
783
784 /* update an arg in the middle */
785 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
786 "mary=abc arg=123 john=2", "arg", NULL));
787 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
788 "mary=abc arg=123 john=2", "arg",
789 BOOTFLOWCL_EMPTY));
790 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
791 "mary=abc arg=123 john=2", "arg", ""));
792 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
793 "mary=abc arg=123 john=2", "arg", "1"));
794 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
795 "mary=abc arg=123 john=2", "arg", "1234"));
796
797 /* handle existing args with quotes */
798 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
799 "mary=\"abc\" arg=123 john", "arg", NULL));
800
801 /* handle existing args with quoted spaces */
802 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
803 "mary=\"abc def\" arg=123 john", "arg", NULL));
804
805 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
806 buf, "mary=\"abc def\" arg=123 john", "def",
807 "4"));
808
809 /* quote at the start */
810 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
811 "mary=\"abc def\" arg=\"123 456\"",
812 "arg", "\"4 5 6", NULL));
813
814 /* quote at the end */
815 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
816 "mary=\"abc def\" arg=\"123 456\"",
817 "arg", "4 5 6\"", NULL));
818
819 /* quote in the middle */
820 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
821 "mary=\"abc def\" arg=\"123 456\"",
822 "arg", "\"4 \"5 6\"", NULL));
823
824 /* handle updating a quoted arg */
825 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
826 "mary=\"abc def\" arg=\"123 456\"", "arg",
827 "4 5 6"));
828
829 /* changing a quoted arg to a non-quoted arg */
830 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
831 "mary=\"abc def\" arg=\"123 456\"", "arg",
832 "789"));
833
834 /* changing a non-quoted arg to a quoted arg */
835 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
836 "mary=\"abc def\" arg=123", "arg", "456 789"));
837
838 /* handling of spaces */
839 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
840 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
841 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
842 "123"));
843 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
844 "arg", "123"));
845 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
846 " john arg=123 mary ", "arg", "123"));
847
848 /* unchanged arg */
849 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
850
851 /* arg which starts with the same name */
852 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
853 "mary=abc johnathon=2 john=1", "john", "3"));
854
855 return 0;
856}
857BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
Simon Glass82c09382023-07-12 09:04:39 -0600858
859/* Test of bootflow_cmdline_set_arg() */
860static int bootflow_set_arg(struct unit_test_state *uts)
861{
862 struct bootflow s_bflow, *bflow = &s_bflow;
863 ulong mem_start;
864
865 ut_assertok(env_set("bootargs", NULL));
866
867 mem_start = ut_check_delta(0);
868
869 /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
870 bflow->cmdline = NULL;
871 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
872 ut_asserteq_str(bflow->cmdline, "fred=123");
873
874 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
875 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
876
877 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
878 ut_asserteq_str(bflow->cmdline, "fred=123");
879 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
880 ut_asserteq_ptr(bflow->cmdline, NULL);
881
882 ut_asserteq(0, ut_check_delta(mem_start));
883
884 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
885 ut_asserteq_str("mary=here", env_get("bootargs"));
886 ut_assertok(env_set("bootargs", NULL));
887
888 return 0;
889}
890BOOTSTD_TEST(bootflow_set_arg, 0);
891
892/* Test of bootflow_cmdline_get_arg() */
893static int bootflow_cmdline_get(struct unit_test_state *uts)
894{
895 int pos;
896
897 /* empty string */
898 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
899
900 /* arg with empty value */
901 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
902 ut_asserteq(5, pos);
903
904 /* arg with a value */
905 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
906 ut_asserteq(5, pos);
907
908 /* arg with a value */
909 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
910 ut_asserteq(12, pos);
911
912 /* arg with a value, after quoted arg */
913 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
914 ut_asserteq(16, pos);
915
916 /* arg in the middle */
917 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
918 &pos));
919 ut_asserteq(15, pos);
920
921 /* quoted arg */
922 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
923 "fred", &pos));
924 ut_asserteq(17, pos);
925
926 /* args starting with the same prefix */
927 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
928 &pos));
929 ut_asserteq(26, pos);
930
931 return 0;
932}
933BOOTSTD_TEST(bootflow_cmdline_get, 0);
934
935static int bootflow_cmdline(struct unit_test_state *uts)
936{
937 ut_assertok(run_command("bootflow scan mmc", 0));
938 ut_assertok(run_command("bootflow sel 0", 0));
939 console_record_reset_enable();
940
941 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
942 ut_assert_nextline("Argument not found");
943 ut_assert_console_end();
944
945 ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
946 ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
947 ut_assert_nextline("123");
948
949 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
950 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
951 ut_assert_nextline("abc");
952
953 ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
954 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
955 ut_assert_nextline("Argument not found");
956
957 ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
958 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
959 ut_assert_nextline_empty();
960
961 ut_assert_console_end();
962
963 return 0;
964}
965BOOTSTD_TEST(bootflow_cmdline, 0);