1 // SPDX-License-Identifier: GPL-2.0+
3 * Test for bootdev functions. All start with 'bootdev'
5 * Copyright 2021 Google LLC
20 #include <dm/device-internal.h>
22 #include <test/suites.h>
24 #include "bootstd_common.h"
25 #include "../../boot/bootflow_internal.h"
26 #include "../../boot/scene_internal.h"
28 DECLARE_GLOBAL_DATA_PTR;
30 extern U_BOOT_DRIVER(bootmeth_script);
32 static int inject_response(struct unit_test_state *uts)
35 * The image being booted presents a menu of options:
37 * Fedora-Workstation-armhfp-31-1.9 Boot Options.
38 * 1: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
41 * Provide input for this, to avoid waiting two seconds for a timeout.
43 ut_asserteq(2, console_in_puts("1\n"));
48 /* Check 'bootflow scan/list' commands */
49 static int bootflow_cmd(struct unit_test_state *uts)
51 console_record_reset_enable();
52 ut_assertok(run_command("bootdev select 1", 0));
53 ut_assert_console_end();
54 ut_assertok(run_command("bootflow scan -lH", 0));
55 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("---");
58 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
59 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
60 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
61 ut_assert_nextline("No more bootdevs");
62 ut_assert_nextlinen("---");
63 ut_assert_nextline("(1 bootflow, 1 valid)");
64 ut_assert_console_end();
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("---");
70 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
71 ut_assert_nextlinen("---");
72 ut_assert_nextline("(1 bootflow, 1 valid)");
73 ut_assert_console_end();
77 BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
79 /* Check 'bootflow scan' with a label / seq */
80 static int bootflow_cmd_label(struct unit_test_state *uts)
82 test_set_eth_enable(false);
84 console_record_reset_enable();
85 ut_assertok(run_command("bootflow scan -lH mmc1", 0));
86 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
87 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
88 ut_assert_console_end();
90 ut_assertok(run_command("bootflow scan -lH 0", 0));
91 ut_assert_nextline("Scanning for bootflows with label '0'");
92 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
93 ut_assert_console_end();
96 * with ethernet enabled we have 8 devices ahead of the mmc ones:
98 * ut_assertok(run_command("bootdev list", 0));
99 * Seq Probed Status Uclass Name
100 * --- ------ ------ -------- ------------------
103 * 2 [ ] OK ethernet sbe5.bootdev
105 * 4 [ ] OK ethernet phy-test-eth.bootdev
106 * 5 [ ] OK ethernet dsa-test-eth.bootdev
109 * 8 [ ] OK mmc mmc2.bootdev
110 * 9 [ + ] OK mmc mmc1.bootdev
111 * a [ ] OK mmc mmc0.bootdev
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)");
117 ut_assertok(run_command("bootflow scan -lH 0", 0));
118 ut_assert_nextline("Scanning for bootflows with label '0'");
119 ut_assert_skip_to_line("(0 bootflows, 0 valid)");
120 ut_assert_console_end();
124 BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
125 UT_TESTF_ETH_BOOTDEV);
127 /* Check 'bootflow scan/list' commands using all bootdevs */
128 static int bootflow_cmd_glob(struct unit_test_state *uts)
130 ut_assertok(bootstd_test_drop_bootdev_order(uts));
132 console_record_reset_enable();
133 ut_assertok(run_command("bootflow scan -lGH", 0));
134 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':");
139 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
140 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();
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("---");
150 ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
151 ut_assert_nextlinen("---");
152 ut_assert_nextline("(1 bootflow, 1 valid)");
153 ut_assert_console_end();
157 BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
159 /* Check 'bootflow scan -e' */
160 static int bootflow_cmd_scan_e(struct unit_test_state *uts)
162 ut_assertok(bootstd_test_drop_bootdev_order(uts));
164 console_record_reset_enable();
165 ut_assertok(run_command("bootflow scan -aleGH", 0));
166 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':");
170 ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole ");
171 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
172 ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole ");
173 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
175 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
176 ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole ");
177 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
178 ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole ");
179 ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
180 ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
181 ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 efi/boot/bootsbox.efi");
183 ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
184 ut_assert_skip_to_line(
185 " 3f efi media mmc 0 mmc0.bootdev.whole ");
186 ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
187 ut_assert_nextline("No more bootdevs");
188 ut_assert_nextlinen("---");
189 ut_assert_nextline("(64 bootflows, 1 valid)");
190 ut_assert_console_end();
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("---");
196 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 ");
201 ut_assert_nextlinen("---");
202 ut_assert_nextline("(64 bootflows, 1 valid)");
203 ut_assert_console_end();
207 BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
209 /* Check 'bootflow info' */
210 static int bootflow_cmd_info(struct unit_test_state *uts)
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");
223 ut_assert_nextline("Method: extlinux");
224 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)");
230 ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
231 ut_assert_nextline("Cmdline: (none)");
232 ut_assert_nextline("Logo: (none)");
233 ut_assert_nextline("FDT: <NULL>");
234 ut_assert_nextline("Error: 0");
235 ut_assert_console_end();
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();
248 BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
250 /* Check 'bootflow scan -b' to boot the first available bootdev */
251 static int bootflow_scan_boot(struct unit_test_state *uts)
253 console_record_reset_enable();
254 ut_assertok(inject_response(uts));
255 ut_assertok(run_command("bootflow scan -b", 0));
257 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
258 ut_assert_nextline("Ignoring unknown command: ui");
261 * We expect it to get through to boot although sandbox always returns
262 * -EFAULT as it cannot actually boot the kernel
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();
270 BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
272 /* Check iterating through available bootflows */
273 static int bootflow_iter(struct unit_test_state *uts)
275 struct bootflow_iter iter;
276 struct bootflow bflow;
278 bootstd_clear_glob();
280 /* The first device is mmc2.bootdev which has no media */
281 ut_asserteq(-EPROTONOSUPPORT,
282 bootflow_scan_first(NULL, NULL, &iter,
283 BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
284 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);
288 ut_asserteq_str("extlinux", iter.method->name);
289 ut_asserteq(0, bflow.err);
292 * This shows MEDIA even though there is none, since in
293 * bootdev_find_in_blk() we call part_get_info() which returns
294 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
297 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
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);
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);
315 ut_asserteq_str("extlinux", iter.method->name);
316 ut_asserteq(0, bflow.err);
317 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
318 bootflow_free(&bflow);
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);
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);
336 ut_asserteq_str("extlinux", iter.method->name);
337 ut_asserteq(0, bflow.err);
338 ut_asserteq(BOOTFLOWST_READY, bflow.state);
339 bootflow_free(&bflow);
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);
351 /* Then more to partition 2 which exists but is not bootable */
352 ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
353 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);
357 ut_asserteq_str("extlinux", iter.method->name);
358 ut_asserteq(0, bflow.err);
359 ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
360 bootflow_free(&bflow);
362 bootflow_iter_uninit(&iter);
364 ut_assert_console_end();
368 BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
370 #if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
371 /* Check using the system bootdev */
372 static int bootflow_system(struct unit_test_state *uts)
374 struct udevice *bootstd, *dev;
376 if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
378 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));
382 sandbox_set_fake_efi_mgr_dev(dev, true);
384 /* We should get a single 'bootmgr' method right at the end */
385 bootstd_clear_glob();
386 console_record_reset_enable();
387 ut_assertok(run_command("bootflow scan -lH", 0));
388 ut_assert_skip_to_line(
389 " 0 efi_mgr ready (none) 0 <NULL> ");
390 ut_assert_skip_to_line("No more bootdevs");
391 ut_assert_skip_to_line("(2 bootflows, 2 valid)");
392 ut_assert_console_end();
396 BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_PDATA |
400 /* Check disabling a bootmethod if it requests it */
401 static int bootflow_iter_disable(struct unit_test_state *uts)
403 struct udevice *bootstd, *dev;
404 struct bootflow_iter iter;
405 struct bootflow bflow;
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",
413 ut_assertok(bootstd_test_drop_bootdev_order(uts));
415 bootstd_clear_glob();
416 console_record_reset_enable();
417 ut_assertok(inject_response(uts));
418 ut_assertok(run_command("bootflow scan -lbH", 0));
420 /* Try to boot the bootmgr flow, which will fail */
421 console_record_reset_enable();
422 ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
423 ut_asserteq(3, iter.num_methods);
424 ut_asserteq_str("sandbox", iter.method->name);
425 ut_assertok(inject_response(uts));
426 ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
428 ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
429 ut_assert_console_end();
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));
438 BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
440 /* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
441 static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
443 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
446 ut_assertok(bootstd_test_drop_bootdev_order(uts));
449 * Make sure that the -G flag makes the scan fail, since this is not
450 * supported when an ordering is provided
452 console_record_reset_enable();
453 ut_assertok(bootmeth_set_order("efi firmware0"));
454 ut_assertok(run_command("bootflow scan -lGH", 0));
455 ut_assert_nextline("Scanning for bootflows in all bootdevs");
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();
463 ut_assertok(run_command("bootflow scan -lH", 0));
464 ut_assert_nextline("Scanning for bootflows in all bootdevs");
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();
479 BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
481 /* Check 'bootflow boot' to boot a selected bootflow */
482 static int bootflow_cmd_boot(struct unit_test_state *uts)
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();
492 ut_assertok(inject_response(uts));
493 ut_asserteq(1, run_command("bootflow boot", 0));
495 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
496 ut_assert_nextline("Ignoring unknown command: ui");
499 * We expect it to get through to boot although sandbox always returns
500 * -EFAULT as it cannot actually boot the kernel
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();
508 BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
511 * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
513 * @uts: Unit test state
514 * Returns 0 on success, -ve on failure
516 static int prep_mmc4_bootdev(struct unit_test_state *uts)
518 static const char *order[] = {"mmc2", "mmc1", "mmc4", NULL};
519 struct udevice *dev, *bootstd;
520 struct bootstd_priv *std;
521 const char **old_order;
524 /* Enable the mmc4 node since we need a second bootflow */
525 node = ofnode_path("/mmc4");
526 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
528 /* Enable the script bootmeth too */
529 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
530 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_script),
531 "bootmeth_script", 0, ofnode_null(), &dev));
533 /* Change the order to include mmc4 */
534 std = dev_get_priv(bootstd);
535 old_order = std->bootdev_order;
536 std->bootdev_order = order;
538 console_record_reset_enable();
539 ut_assertok(run_command("bootflow scan", 0));
540 ut_assert_console_end();
542 /* Restore the order used by the device tree */
543 std->bootdev_order = old_order;
548 /* Check 'bootflow menu' to select a bootflow */
549 static int bootflow_cmd_menu(struct unit_test_state *uts)
553 ut_assertok(prep_mmc4_bootdev(uts));
555 /* Add keypresses to move to and select the second one in the list */
556 prev[0] = CTL_CH('n');
559 ut_asserteq(2, console_in_puts(prev));
561 ut_assertok(run_command("bootflow menu", 0));
562 ut_assert_nextline("Selected: Armbian");
563 ut_assert_console_end();
567 BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
569 /* Check searching for a single bootdev using the hunters */
570 static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
572 struct bootstd_priv *std;
574 /* get access to the used hunters */
575 ut_assertok(bootstd_get_priv(&std));
577 ut_assertok(bootstd_test_drop_bootdev_order(uts));
579 console_record_reset_enable();
580 ut_assertok(run_command("bootflow scan -l mmc1", 0));
581 ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
582 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
583 ut_assert_console_end();
585 /* check that the hunter was used */
586 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
590 BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
592 /* Check searching for a uclass label using the hunters */
593 static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
595 struct bootstd_priv *std;
597 /* get access to the used hunters */
598 ut_assertok(bootstd_get_priv(&std));
600 test_set_skip_delays(true);
601 test_set_eth_enable(false);
602 ut_assertok(bootstd_test_drop_bootdev_order(uts));
604 console_record_reset_enable();
605 ut_assertok(run_command("bootflow scan -l mmc", 0));
607 /* check that the hunter was used */
608 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
610 /* check that we got the mmc1 bootflow */
611 ut_assert_nextline("Scanning for bootflows with label 'mmc'");
612 ut_assert_nextlinen("Seq");
613 ut_assert_nextlinen("---");
614 ut_assert_nextline("Hunting with: simple_bus");
615 ut_assert_nextline("Found 2 extension board(s).");
616 ut_assert_nextline("Hunting with: mmc");
617 ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
618 ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
620 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
621 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
622 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
623 ut_assert_console_end();
627 BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
630 * check_font() - Check that the font size for an item matches expectations
632 * @uts: Unit test state
633 * @scn: Scene containing the text object
634 * @id: ID of the text object
635 * Returns 0 on success, -ve on failure
637 static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
640 struct scene_obj_txt *txt;
642 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
643 ut_assertnonnull(txt);
645 ut_asserteq(font_size, txt->font_size);
650 /* Check themes work with a bootflow menu */
651 static int bootflow_menu_theme(struct unit_test_state *uts)
653 const int font_size = 30;
659 ut_assertok(prep_mmc4_bootdev(uts));
661 ut_assertok(bootflow_menu_new(&exp));
662 node = ofnode_path("/bootstd/theme");
663 ut_assert(ofnode_valid(node));
664 ut_assertok(bootflow_menu_apply_theme(exp, node));
666 scn = expo_lookup_scene_id(exp, MAIN);
667 ut_assertnonnull(scn);
670 * Check that the txt objects have the correct font size from the
671 * device tree node: bootstd/theme
673 * Check both menu items, since there are two bootflows
675 ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
676 ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
677 for (i = 0; i < 2; i++) {
678 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
679 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
680 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
687 BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
690 * check_arg() - Check both the normal case and the buffer-overflow case
692 * @uts: Unit-test state
693 * @expect_ret: Expected return value (i.e. buffer length)
694 * @expect_str: String expected to be returned
695 * @buf: Buffer to use
696 * @from: Original cmdline to update
697 * @arg: Argument to update (e.g. "console")
698 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
699 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
700 * it without any value ("initrd")
702 static int check_arg(struct unit_test_state *uts, int expect_ret,
703 const char *expect_str, char *buf, const char *from,
704 const char *arg, const char *val)
706 /* check for writing outside the reported bounds */
707 buf[expect_ret] = '[';
708 ut_asserteq(expect_ret,
709 cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
710 ut_asserteq_str(expect_str, buf);
711 ut_asserteq('[', buf[expect_ret]);
713 /* do the test again but with one less byte in the buffer */
714 ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
720 /* Test of bootflow_cmdline_set_arg() */
721 static int test_bootflow_cmdline_set(struct unit_test_state *uts)
724 const int size = sizeof(buf);
727 * note that buffer-overflow tests are immediately each test case, just
728 * top keep the code together
731 /* add an arg that doesn't already exist, starting from empty */
732 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
735 ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
736 ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
737 ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
739 /* add an arg that doesn't already exist, starting from non-empty */
740 ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
742 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
744 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
747 /* update an arg at the start */
748 ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
749 ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
751 ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
752 ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
753 ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
756 /* update an arg at the end */
757 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
759 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
761 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
763 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
765 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
768 /* update an arg in the middle */
769 ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
770 "mary=abc arg=123 john=2", "arg", NULL));
771 ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
772 "mary=abc arg=123 john=2", "arg",
774 ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
775 "mary=abc arg=123 john=2", "arg", ""));
776 ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
777 "mary=abc arg=123 john=2", "arg", "1"));
778 ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
779 "mary=abc arg=123 john=2", "arg", "1234"));
781 /* handle existing args with quotes */
782 ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
783 "mary=\"abc\" arg=123 john", "arg", NULL));
785 /* handle existing args with quoted spaces */
786 ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
787 "mary=\"abc def\" arg=123 john", "arg", NULL));
789 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
790 buf, "mary=\"abc def\" arg=123 john", "def",
793 /* quote at the start */
794 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
795 "mary=\"abc def\" arg=\"123 456\"",
796 "arg", "\"4 5 6", NULL));
798 /* quote at the end */
799 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
800 "mary=\"abc def\" arg=\"123 456\"",
801 "arg", "4 5 6\"", NULL));
803 /* quote in the middle */
804 ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
805 "mary=\"abc def\" arg=\"123 456\"",
806 "arg", "\"4 \"5 6\"", NULL));
808 /* handle updating a quoted arg */
809 ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
810 "mary=\"abc def\" arg=\"123 456\"", "arg",
813 /* changing a quoted arg to a non-quoted arg */
814 ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
815 "mary=\"abc def\" arg=\"123 456\"", "arg",
818 /* changing a non-quoted arg to a quoted arg */
819 ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
820 "mary=\"abc def\" arg=123", "arg", "456 789"));
822 /* handling of spaces */
823 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
824 ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
825 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
827 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
829 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
830 " john arg=123 mary ", "arg", "123"));
833 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
835 /* arg which starts with the same name */
836 ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
837 "mary=abc johnathon=2 john=1", "john", "3"));
841 BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
843 /* Test of bootflow_cmdline_set_arg() */
844 static int bootflow_set_arg(struct unit_test_state *uts)
846 struct bootflow s_bflow, *bflow = &s_bflow;
849 ut_assertok(env_set("bootargs", NULL));
851 mem_start = ut_check_delta(0);
853 /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
854 bflow->cmdline = NULL;
855 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
856 ut_asserteq_str(bflow->cmdline, "fred=123");
858 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
859 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
861 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
862 ut_asserteq_str(bflow->cmdline, "fred=123");
863 ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
864 ut_asserteq_ptr(bflow->cmdline, NULL);
866 ut_asserteq(0, ut_check_delta(mem_start));
868 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
869 ut_asserteq_str("mary=here", env_get("bootargs"));
870 ut_assertok(env_set("bootargs", NULL));
874 BOOTSTD_TEST(bootflow_set_arg, 0);
876 /* Test of bootflow_cmdline_get_arg() */
877 static int bootflow_cmdline_get(struct unit_test_state *uts)
882 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
884 /* arg with empty value */
885 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
888 /* arg with a value */
889 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
892 /* arg with a value */
893 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
894 ut_asserteq(12, pos);
896 /* arg with a value, after quoted arg */
897 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
898 ut_asserteq(16, pos);
900 /* arg in the middle */
901 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
903 ut_asserteq(15, pos);
906 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
908 ut_asserteq(17, pos);
910 /* args starting with the same prefix */
911 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
913 ut_asserteq(26, pos);
917 BOOTSTD_TEST(bootflow_cmdline_get, 0);
919 static int bootflow_cmdline(struct unit_test_state *uts)
921 ut_assertok(run_command("bootflow scan mmc", 0));
922 ut_assertok(run_command("bootflow sel 0", 0));
923 console_record_reset_enable();
925 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
926 ut_assert_nextline("Argument not found");
927 ut_assert_console_end();
929 ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
930 ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
931 ut_assert_nextline("123");
933 ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
934 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
935 ut_assert_nextline("abc");
937 ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
938 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
939 ut_assert_nextline("Argument not found");
941 ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
942 ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
943 ut_assert_nextline_empty();
945 ut_assert_console_end();
949 BOOTSTD_TEST(bootflow_cmdline, 0);