]> Git Repo - J-u-boot.git/blob - test/boot/bootflow.c
bootflow: Show an empty filename when there is none
[J-u-boot.git] / test / boot / bootflow.c
1 // 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 <[email protected]>
7  */
8
9 #include <common.h>
10 #include <bootdev.h>
11 #include <bootflow.h>
12 #include <bootmeth.h>
13 #include <bootstd.h>
14 #include <cli.h>
15 #include <dm.h>
16 #include <expo.h>
17 #ifdef CONFIG_SANDBOX
18 #include <asm/test.h>
19 #endif
20 #include <dm/device-internal.h>
21 #include <dm/lists.h>
22 #include <test/suites.h>
23 #include <test/ut.h>
24 #include "bootstd_common.h"
25 #include "../../boot/bootflow_internal.h"
26 #include "../../boot/scene_internal.h"
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 extern U_BOOT_DRIVER(bootmeth_script);
31
32 static 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
48 /* Check 'bootflow scan/list' commands */
49 static 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();
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();
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("---");
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();
74
75         return 0;
76 }
77 BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
78
79 /* Check 'bootflow scan' with a label / seq */
80 static int bootflow_cmd_label(struct unit_test_state *uts)
81 {
82         test_set_eth_enable(false);
83
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();
89
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();
94
95         /*
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  [email protected]
102          * 1   [   ]      OK  ethernet  [email protected]
103          * 2   [   ]      OK  ethernet  sbe5.bootdev
104          * 3   [   ]      OK  ethernet  [email protected]
105          * 4   [   ]      OK  ethernet  phy-test-eth.bootdev
106          * 5   [   ]      OK  ethernet  dsa-test-eth.bootdev
107          * 6   [   ]      OK  ethernet  [email protected]
108          * 7   [   ]      OK  ethernet  [email protected]
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
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();
121
122         return 0;
123 }
124 BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
125              UT_TESTF_ETH_BOOTDEV);
126
127 /* Check 'bootflow scan/list' commands using all bootdevs */
128 static 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();
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();
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("---");
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();
154
155         return 0;
156 }
157 BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
158
159 /* Check 'bootflow scan -e' */
160 static 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();
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");
174
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");
182
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();
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("---");
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();
204
205         return 0;
206 }
207 BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
208
209 /* Check 'bootflow info' */
210 static 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");
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();
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 }
248 BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
249
250 /* Check 'bootflow scan -b' to boot the first available bootdev */
251 static int bootflow_scan_boot(struct unit_test_state *uts)
252 {
253         console_record_reset_enable();
254         ut_assertok(inject_response(uts));
255         ut_assertok(run_command("bootflow scan -b", 0));
256         ut_assert_nextline(
257                 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
258         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 }
270 BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
271
272 /* Check iterating through available bootflows */
273 static 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,
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);
290
291         /*
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
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);
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);
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);
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);
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
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);
361
362         bootflow_iter_uninit(&iter);
363
364         ut_assert_console_end();
365
366         return 0;
367 }
368 BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
369
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)
373 {
374         struct udevice *bootstd, *dev;
375
376         if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
377                 return -EAGAIN;
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);
383
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();
393
394         return 0;
395 }
396 BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_PDATA |
397              UT_TESTF_SCAN_FDT);
398 #endif
399
400 /* Check disabling a bootmethod if it requests it */
401 static 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
413         ut_assertok(bootstd_test_drop_bootdev_order(uts));
414
415         bootstd_clear_glob();
416         console_record_reset_enable();
417         ut_assertok(inject_response(uts));
418         ut_assertok(run_command("bootflow scan -lbH", 0));
419
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));
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 }
438 BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
439
440 /* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
441 static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
442 {
443         if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
444                 return -EAGAIN;
445
446         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"));
454         ut_assertok(run_command("bootflow scan -lGH", 0));
455         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
463         ut_assertok(run_command("bootflow scan -lH", 0));
464         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 }
479 BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
480
481 /* Check 'bootflow boot' to boot a selected bootflow */
482 static 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();
491
492         ut_assertok(inject_response(uts));
493         ut_asserteq(1, run_command("bootflow boot", 0));
494         ut_assert_nextline(
495                 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
496         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 }
508 BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
509
510 /**
511  * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
512  *
513  * @uts: Unit test state
514  * Returns 0 on success, -ve on failure
515  */
516 static int prep_mmc4_bootdev(struct unit_test_state *uts)
517 {
518         static const char *order[] = {"mmc2", "mmc1", "mmc4", NULL};
519         struct udevice *dev, *bootstd;
520         struct bootstd_priv *std;
521         const char **old_order;
522         ofnode node;
523
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));
527
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));
532
533         /* Change the order to include mmc4 */
534         std = dev_get_priv(bootstd);
535         old_order = std->bootdev_order;
536         std->bootdev_order = order;
537
538         console_record_reset_enable();
539         ut_assertok(run_command("bootflow scan", 0));
540         ut_assert_console_end();
541
542         /* Restore the order used by the device tree */
543         std->bootdev_order = old_order;
544
545         return 0;
546 }
547
548 /* Check 'bootflow menu' to select a bootflow */
549 static int bootflow_cmd_menu(struct unit_test_state *uts)
550 {
551         char prev[3];
552
553         ut_assertok(prep_mmc4_bootdev(uts));
554
555         /* Add keypresses to move to and select the second one in the list */
556         prev[0] = CTL_CH('n');
557         prev[1] = '\r';
558         prev[2] = '\0';
559         ut_asserteq(2, console_in_puts(prev));
560
561         ut_assertok(run_command("bootflow menu", 0));
562         ut_assert_nextline("Selected: Armbian");
563         ut_assert_console_end();
564
565         return 0;
566 }
567 BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
568
569 /* Check searching for a single bootdev using the hunters */
570 static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
571 {
572         struct bootstd_priv *std;
573
574         /* get access to the used hunters */
575         ut_assertok(bootstd_get_priv(&std));
576
577         ut_assertok(bootstd_test_drop_bootdev_order(uts));
578
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();
584
585         /* check that the hunter was used */
586         ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
587
588         return 0;
589 }
590 BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
591
592 /* Check searching for a uclass label using the hunters */
593 static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
594 {
595         struct bootstd_priv *std;
596
597         /* get access to the used hunters */
598         ut_assertok(bootstd_get_priv(&std));
599
600         test_set_skip_delays(true);
601         test_set_eth_enable(false);
602         ut_assertok(bootstd_test_drop_bootdev_order(uts));
603
604         console_record_reset_enable();
605         ut_assertok(run_command("bootflow scan -l mmc", 0));
606
607         /* check that the hunter was used */
608         ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
609
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':");
619         ut_assert_nextline(
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();
624
625         return 0;
626 }
627 BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
628
629 /**
630  * check_font() - Check that the font size for an item matches expectations
631  *
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
636  */
637 static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
638                       int font_size)
639 {
640         struct scene_obj_txt *txt;
641
642         txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
643         ut_assertnonnull(txt);
644
645         ut_asserteq(font_size, txt->font_size);
646
647         return 0;
648 }
649
650 /* Check themes work with a bootflow menu */
651 static int bootflow_menu_theme(struct unit_test_state *uts)
652 {
653         const int font_size = 30;
654         struct scene *scn;
655         struct expo *exp;
656         ofnode node;
657         int i;
658
659         ut_assertok(prep_mmc4_bootdev(uts));
660
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));
665
666         scn = expo_lookup_scene_id(exp, MAIN);
667         ut_assertnonnull(scn);
668
669         /*
670          * Check that the txt objects have the correct font size from the
671          * device tree node: bootstd/theme
672          *
673          * Check both menu items, since there are two bootflows
674          */
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));
681         }
682
683         expo_destroy(exp);
684
685         return 0;
686 }
687 BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
688
689 /**
690  * check_arg() - Check both the normal case and the buffer-overflow case
691  *
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")
701  */
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)
705 {
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]);
712
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,
715                                             val, NULL));
716
717         return 0;
718 }
719
720 /* Test of bootflow_cmdline_set_arg() */
721 static int test_bootflow_cmdline_set(struct unit_test_state *uts)
722 {
723         char buf[50];
724         const int size = sizeof(buf);
725
726         /*
727          * note that buffer-overflow tests are immediately each test case, just
728          * top keep the code together
729          */
730
731         /* add an arg that doesn't already exist, starting from empty */
732         ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
733                                              NULL));
734
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"));
738
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",
741                               BOOTFLOWCL_EMPTY));
742         ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
743                               ""));
744         ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
745                               "fred"));
746
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",
750                               BOOTFLOWCL_EMPTY));
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",
754                               "1234"));
755
756         /* update an arg at the end */
757         ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
758                               NULL));
759         ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
760                               BOOTFLOWCL_EMPTY));
761         ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
762                               ""));
763         ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
764                               "1"));
765         ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
766                               "arg", "1234"));
767
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",
773                               BOOTFLOWCL_EMPTY));
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"));
780
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));
784
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));
788
789         ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
790                               buf, "mary=\"abc def\" arg=123 john", "def",
791                               "4"));
792
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));
797
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));
802
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));
807
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",
811                               "4 5 6"));
812
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",
816                               "789"));
817
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"));
821
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",
826                               "123"));
827         ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john  arg=123  ",
828                               "arg", "123"));
829         ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
830                               " john  arg=123 mary ", "arg", "123"));
831
832         /* unchanged arg */
833         ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
834
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"));
838
839         return 0;
840 }
841 BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
842
843 /* Test of bootflow_cmdline_set_arg() */
844 static int bootflow_set_arg(struct unit_test_state *uts)
845 {
846         struct bootflow s_bflow, *bflow = &s_bflow;
847         ulong mem_start;
848
849         ut_assertok(env_set("bootargs", NULL));
850
851         mem_start = ut_check_delta(0);
852
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");
857
858         ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
859         ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
860
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);
865
866         ut_asserteq(0, ut_check_delta(mem_start));
867
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));
871
872         return 0;
873 }
874 BOOTSTD_TEST(bootflow_set_arg, 0);
875
876 /* Test of bootflow_cmdline_get_arg() */
877 static int bootflow_cmdline_get(struct unit_test_state *uts)
878 {
879         int pos;
880
881         /* empty string */
882         ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
883
884         /* arg with empty value */
885         ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
886         ut_asserteq(5, pos);
887
888         /* arg with a value */
889         ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
890         ut_asserteq(5, pos);
891
892         /* arg with a value */
893         ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
894         ut_asserteq(12, pos);
895
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);
899
900         /* arg in the middle */
901         ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
902                                        &pos));
903         ut_asserteq(15, pos);
904
905         /* quoted arg */
906         ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
907                                        "fred", &pos));
908         ut_asserteq(17, pos);
909
910         /* args starting with the same prefix */
911         ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
912                                        &pos));
913         ut_asserteq(26, pos);
914
915         return 0;
916 }
917 BOOTSTD_TEST(bootflow_cmdline_get, 0);
918
919 static int bootflow_cmdline(struct unit_test_state *uts)
920 {
921         ut_assertok(run_command("bootflow scan mmc", 0));
922         ut_assertok(run_command("bootflow sel 0", 0));
923         console_record_reset_enable();
924
925         ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
926         ut_assert_nextline("Argument not found");
927         ut_assert_console_end();
928
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");
932
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");
936
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");
940
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();
944
945         ut_assert_console_end();
946
947         return 0;
948 }
949 BOOTSTD_TEST(bootflow_cmdline, 0);
This page took 0.082014 seconds and 4 git commands to generate.