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_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
513 * @uts: Unit test state
514 * @mmc_dev: MMC device to use, e.g. "mmc4"
515 * Returns 0 on success, -ve on failure
517 static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev)
519 const char *order[] = {"mmc2", "mmc1", mmc_dev, NULL};
520 struct udevice *dev, *bootstd;
521 struct bootstd_priv *std;
522 const char **old_order;
525 /* Enable the mmc4 node since we need a second bootflow */
526 root = oftree_root(oftree_default());
527 node = ofnode_find_subnode(root, mmc_dev);
528 ut_assert(ofnode_valid(node));
529 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
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));
536 /* Change the order to include the device */
537 std = dev_get_priv(bootstd);
538 old_order = std->bootdev_order;
539 std->bootdev_order = order;
541 console_record_reset_enable();
542 ut_assertok(run_command("bootflow scan", 0));
543 ut_assert_console_end();
545 /* Restore the order used by the device tree */
546 std->bootdev_order = old_order;
552 * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
554 * @uts: Unit test state
555 * Returns 0 on success, -ve on failure
557 static int prep_mmc4_bootdev(struct unit_test_state *uts)
559 ut_assertok(prep_mmc_bootdev(uts, "mmc4"));
564 /* Check 'bootflow menu' to select a bootflow */
565 static int bootflow_cmd_menu(struct unit_test_state *uts)
569 ut_assertok(prep_mmc4_bootdev(uts));
571 /* Add keypresses to move to and select the second one in the list */
572 prev[0] = CTL_CH('n');
575 ut_asserteq(2, console_in_puts(prev));
577 ut_assertok(run_command("bootflow menu", 0));
578 ut_assert_nextline("Selected: Armbian");
579 ut_assert_console_end();
583 BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
585 /* Check searching for a single bootdev using the hunters */
586 static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
588 struct bootstd_priv *std;
590 /* get access to the used hunters */
591 ut_assertok(bootstd_get_priv(&std));
593 ut_assertok(bootstd_test_drop_bootdev_order(uts));
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();
601 /* check that the hunter was used */
602 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
606 BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
608 /* Check searching for a uclass label using the hunters */
609 static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
611 struct bootstd_priv *std;
613 /* get access to the used hunters */
614 ut_assertok(bootstd_get_priv(&std));
616 test_set_skip_delays(true);
617 test_set_eth_enable(false);
618 ut_assertok(bootstd_test_drop_bootdev_order(uts));
620 console_record_reset_enable();
621 ut_assertok(run_command("bootflow scan -l mmc", 0));
623 /* check that the hunter was used */
624 ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
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':");
636 " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
637 ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
638 ut_assert_skip_to_line("(1 bootflow, 1 valid)");
639 ut_assert_console_end();
643 BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
646 * check_font() - Check that the font size for an item matches expectations
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
653 static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
656 struct scene_obj_txt *txt;
658 txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
659 ut_assertnonnull(txt);
661 ut_asserteq(font_size, txt->font_size);
666 /* Check themes work with a bootflow menu */
667 static int bootflow_menu_theme(struct unit_test_state *uts)
669 const int font_size = 30;
675 ut_assertok(prep_mmc4_bootdev(uts));
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));
682 scn = expo_lookup_scene_id(exp, MAIN);
683 ut_assertnonnull(scn);
686 * Check that the txt objects have the correct font size from the
687 * device tree node: bootstd/theme
689 * Check both menu items, since there are two bootflows
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));
703 BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
706 * check_arg() - Check both the normal case and the buffer-overflow case
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")
718 static 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)
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]);
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,
736 /* Test of bootflow_cmdline_set_arg() */
737 static int test_bootflow_cmdline_set(struct unit_test_state *uts)
740 const int size = sizeof(buf);
743 * note that buffer-overflow tests are immediately each test case, just
744 * top keep the code together
747 /* add an arg that doesn't already exist, starting from empty */
748 ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
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"));
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",
758 ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
760 ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
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",
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",
772 /* update an arg at the end */
773 ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
775 ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
777 ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
779 ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
781 ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
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",
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"));
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));
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));
805 ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
806 buf, "mary=\"abc def\" arg=123 john", "def",
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));
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));
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));
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",
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",
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"));
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",
843 ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
845 ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
846 " john arg=123 mary ", "arg", "123"));
849 ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
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"));
857 BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
859 /* Test of bootflow_cmdline_set_arg() */
860 static int bootflow_set_arg(struct unit_test_state *uts)
862 struct bootflow s_bflow, *bflow = &s_bflow;
865 ut_assertok(env_set("bootargs", NULL));
867 mem_start = ut_check_delta(0);
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");
874 ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
875 ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
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);
882 ut_asserteq(0, ut_check_delta(mem_start));
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));
890 BOOTSTD_TEST(bootflow_set_arg, 0);
892 /* Test of bootflow_cmdline_get_arg() */
893 static int bootflow_cmdline_get(struct unit_test_state *uts)
898 ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
900 /* arg with empty value */
901 ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
904 /* arg with a value */
905 ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
908 /* arg with a value */
909 ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
910 ut_asserteq(12, pos);
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);
916 /* arg in the middle */
917 ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
919 ut_asserteq(15, pos);
922 ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
924 ut_asserteq(17, pos);
926 /* args starting with the same prefix */
927 ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
929 ut_asserteq(26, pos);
933 BOOTSTD_TEST(bootflow_cmdline_get, 0);
935 static int bootflow_cmdline(struct unit_test_state *uts)
937 ut_assertok(run_command("bootflow scan mmc", 0));
938 ut_assertok(run_command("bootflow sel 0", 0));
939 console_record_reset_enable();
941 ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
942 ut_assert_nextline("Argument not found");
943 ut_assert_console_end();
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");
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");
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");
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();
961 ut_assert_console_end();
965 BOOTSTD_TEST(bootflow_cmdline, 0);