]> Git Repo - J-u-boot.git/blob - test/boot/bootflow.c
bootstd: test: Allow binding and using any mmc device
[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_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
512  *
513  * @uts: Unit test state
514  * @mmc_dev: MMC device to use, e.g. "mmc4"
515  * Returns 0 on success, -ve on failure
516  */
517 static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev)
518 {
519         const char *order[] = {"mmc2", "mmc1", mmc_dev, NULL};
520         struct udevice *dev, *bootstd;
521         struct bootstd_priv *std;
522         const char **old_order;
523         ofnode root, node;
524
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));
530
531         /* Enable the script bootmeth too */
532         ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
533         ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_script),
534                                 "bootmeth_script", 0, ofnode_null(), &dev));
535
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;
540
541         console_record_reset_enable();
542         ut_assertok(run_command("bootflow scan", 0));
543         ut_assert_console_end();
544
545         /* Restore the order used by the device tree */
546         std->bootdev_order = old_order;
547
548         return 0;
549 }
550
551 /**
552  * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
553  *
554  * @uts: Unit test state
555  * Returns 0 on success, -ve on failure
556  */
557 static int prep_mmc4_bootdev(struct unit_test_state *uts)
558 {
559         ut_assertok(prep_mmc_bootdev(uts, "mmc4"));
560
561         return 0;
562 }
563
564 /* Check 'bootflow menu' to select a bootflow */
565 static int bootflow_cmd_menu(struct unit_test_state *uts)
566 {
567         char prev[3];
568
569         ut_assertok(prep_mmc4_bootdev(uts));
570
571         /* Add keypresses to move to and select the second one in the list */
572         prev[0] = CTL_CH('n');
573         prev[1] = '\r';
574         prev[2] = '\0';
575         ut_asserteq(2, console_in_puts(prev));
576
577         ut_assertok(run_command("bootflow menu", 0));
578         ut_assert_nextline("Selected: Armbian");
579         ut_assert_console_end();
580
581         return 0;
582 }
583 BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
584
585 /* Check searching for a single bootdev using the hunters */
586 static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
587 {
588         struct bootstd_priv *std;
589
590         /* get access to the used hunters */
591         ut_assertok(bootstd_get_priv(&std));
592
593         ut_assertok(bootstd_test_drop_bootdev_order(uts));
594
595         console_record_reset_enable();
596         ut_assertok(run_command("bootflow scan -l mmc1", 0));
597         ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
598         ut_assert_skip_to_line("(1 bootflow, 1 valid)");
599         ut_assert_console_end();
600
601         /* check that the hunter was used */
602         ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
603
604         return 0;
605 }
606 BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
607
608 /* Check searching for a uclass label using the hunters */
609 static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
610 {
611         struct bootstd_priv *std;
612
613         /* get access to the used hunters */
614         ut_assertok(bootstd_get_priv(&std));
615
616         test_set_skip_delays(true);
617         test_set_eth_enable(false);
618         ut_assertok(bootstd_test_drop_bootdev_order(uts));
619
620         console_record_reset_enable();
621         ut_assertok(run_command("bootflow scan -l mmc", 0));
622
623         /* check that the hunter was used */
624         ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
625
626         /* check that we got the mmc1 bootflow */
627         ut_assert_nextline("Scanning for bootflows with label 'mmc'");
628         ut_assert_nextlinen("Seq");
629         ut_assert_nextlinen("---");
630         ut_assert_nextline("Hunting with: simple_bus");
631         ut_assert_nextline("Found 2 extension board(s).");
632         ut_assert_nextline("Hunting with: mmc");
633         ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
634         ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
635         ut_assert_nextline(
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();
640
641         return 0;
642 }
643 BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
644
645 /**
646  * check_font() - Check that the font size for an item matches expectations
647  *
648  * @uts: Unit test state
649  * @scn: Scene containing the text object
650  * @id: ID of the text object
651  * Returns 0 on success, -ve on failure
652  */
653 static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
654                       int font_size)
655 {
656         struct scene_obj_txt *txt;
657
658         txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
659         ut_assertnonnull(txt);
660
661         ut_asserteq(font_size, txt->font_size);
662
663         return 0;
664 }
665
666 /* Check themes work with a bootflow menu */
667 static int bootflow_menu_theme(struct unit_test_state *uts)
668 {
669         const int font_size = 30;
670         struct scene *scn;
671         struct expo *exp;
672         ofnode node;
673         int i;
674
675         ut_assertok(prep_mmc4_bootdev(uts));
676
677         ut_assertok(bootflow_menu_new(&exp));
678         node = ofnode_path("/bootstd/theme");
679         ut_assert(ofnode_valid(node));
680         ut_assertok(bootflow_menu_apply_theme(exp, node));
681
682         scn = expo_lookup_scene_id(exp, MAIN);
683         ut_assertnonnull(scn);
684
685         /*
686          * Check that the txt objects have the correct font size from the
687          * device tree node: bootstd/theme
688          *
689          * Check both menu items, since there are two bootflows
690          */
691         ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
692         ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
693         for (i = 0; i < 2; i++) {
694                 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
695                 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
696                 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
697         }
698
699         expo_destroy(exp);
700
701         return 0;
702 }
703 BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
704
705 /**
706  * check_arg() - Check both the normal case and the buffer-overflow case
707  *
708  * @uts: Unit-test state
709  * @expect_ret: Expected return value (i.e. buffer length)
710  * @expect_str: String expected to be returned
711  * @buf: Buffer to use
712  * @from: Original cmdline to update
713  * @arg: Argument to update (e.g. "console")
714  * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
715  * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
716  * it without any value ("initrd")
717  */
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)
721 {
722         /* check for writing outside the reported bounds */
723         buf[expect_ret] = '[';
724         ut_asserteq(expect_ret,
725                     cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
726         ut_asserteq_str(expect_str, buf);
727         ut_asserteq('[', buf[expect_ret]);
728
729         /* do the test again but with one less byte in the buffer */
730         ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
731                                             val, NULL));
732
733         return 0;
734 }
735
736 /* Test of bootflow_cmdline_set_arg() */
737 static int test_bootflow_cmdline_set(struct unit_test_state *uts)
738 {
739         char buf[50];
740         const int size = sizeof(buf);
741
742         /*
743          * note that buffer-overflow tests are immediately each test case, just
744          * top keep the code together
745          */
746
747         /* add an arg that doesn't already exist, starting from empty */
748         ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
749                                              NULL));
750
751         ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
752         ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
753         ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
754
755         /* add an arg that doesn't already exist, starting from non-empty */
756         ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
757                               BOOTFLOWCL_EMPTY));
758         ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
759                               ""));
760         ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
761                               "fred"));
762
763         /* update an arg at the start */
764         ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
765         ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
766                               BOOTFLOWCL_EMPTY));
767         ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
768         ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
769         ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
770                               "1234"));
771
772         /* update an arg at the end */
773         ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
774                               NULL));
775         ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
776                               BOOTFLOWCL_EMPTY));
777         ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
778                               ""));
779         ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
780                               "1"));
781         ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
782                               "arg", "1234"));
783
784         /* update an arg in the middle */
785         ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
786                               "mary=abc arg=123 john=2", "arg", NULL));
787         ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
788                               "mary=abc arg=123 john=2", "arg",
789                               BOOTFLOWCL_EMPTY));
790         ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
791                               "mary=abc arg=123 john=2", "arg", ""));
792         ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
793                               "mary=abc arg=123 john=2", "arg", "1"));
794         ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
795                               "mary=abc arg=123 john=2", "arg", "1234"));
796
797         /* handle existing args with quotes */
798         ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
799                               "mary=\"abc\" arg=123 john", "arg", NULL));
800
801         /* handle existing args with quoted spaces */
802         ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
803                               "mary=\"abc def\" arg=123 john", "arg", NULL));
804
805         ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
806                               buf, "mary=\"abc def\" arg=123 john", "def",
807                               "4"));
808
809         /* quote at the start */
810         ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
811                                             "mary=\"abc def\" arg=\"123 456\"",
812                                             "arg", "\"4 5 6", NULL));
813
814         /* quote at the end */
815         ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
816                                             "mary=\"abc def\" arg=\"123 456\"",
817                                             "arg", "4 5 6\"", NULL));
818
819         /* quote in the middle */
820         ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
821                                             "mary=\"abc def\" arg=\"123 456\"",
822                                             "arg", "\"4 \"5 6\"", NULL));
823
824         /* handle updating a quoted arg */
825         ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
826                               "mary=\"abc def\" arg=\"123 456\"", "arg",
827                               "4 5 6"));
828
829         /* changing a quoted arg to a non-quoted arg */
830         ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
831                               "mary=\"abc def\" arg=\"123 456\"", "arg",
832                               "789"));
833
834         /* changing a non-quoted arg to a quoted arg */
835         ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
836                               "mary=\"abc def\" arg=123", "arg", "456 789"));
837
838         /* handling of spaces */
839         ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
840         ut_assertok(check_arg(uts, 8, "arg=123", buf, "   ", "arg", "123"));
841         ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john  ", "arg",
842                               "123"));
843         ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john  arg=123  ",
844                               "arg", "123"));
845         ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
846                               " john  arg=123 mary ", "arg", "123"));
847
848         /* unchanged arg */
849         ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
850
851         /* arg which starts with the same name */
852         ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
853                               "mary=abc johnathon=2 john=1", "john", "3"));
854
855         return 0;
856 }
857 BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
858
859 /* Test of bootflow_cmdline_set_arg() */
860 static int bootflow_set_arg(struct unit_test_state *uts)
861 {
862         struct bootflow s_bflow, *bflow = &s_bflow;
863         ulong mem_start;
864
865         ut_assertok(env_set("bootargs", NULL));
866
867         mem_start = ut_check_delta(0);
868
869         /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
870         bflow->cmdline = NULL;
871         ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
872         ut_asserteq_str(bflow->cmdline, "fred=123");
873
874         ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
875         ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
876
877         ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
878         ut_asserteq_str(bflow->cmdline, "fred=123");
879         ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
880         ut_asserteq_ptr(bflow->cmdline, NULL);
881
882         ut_asserteq(0, ut_check_delta(mem_start));
883
884         ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
885         ut_asserteq_str("mary=here", env_get("bootargs"));
886         ut_assertok(env_set("bootargs", NULL));
887
888         return 0;
889 }
890 BOOTSTD_TEST(bootflow_set_arg, 0);
891
892 /* Test of bootflow_cmdline_get_arg() */
893 static int bootflow_cmdline_get(struct unit_test_state *uts)
894 {
895         int pos;
896
897         /* empty string */
898         ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
899
900         /* arg with empty value */
901         ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
902         ut_asserteq(5, pos);
903
904         /* arg with a value */
905         ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
906         ut_asserteq(5, pos);
907
908         /* arg with a value */
909         ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
910         ut_asserteq(12, pos);
911
912         /* arg with a value, after quoted arg */
913         ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
914         ut_asserteq(16, pos);
915
916         /* arg in the middle */
917         ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
918                                        &pos));
919         ut_asserteq(15, pos);
920
921         /* quoted arg */
922         ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
923                                        "fred", &pos));
924         ut_asserteq(17, pos);
925
926         /* args starting with the same prefix */
927         ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
928                                        &pos));
929         ut_asserteq(26, pos);
930
931         return 0;
932 }
933 BOOTSTD_TEST(bootflow_cmdline_get, 0);
934
935 static int bootflow_cmdline(struct unit_test_state *uts)
936 {
937         ut_assertok(run_command("bootflow scan mmc", 0));
938         ut_assertok(run_command("bootflow sel 0", 0));
939         console_record_reset_enable();
940
941         ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
942         ut_assert_nextline("Argument not found");
943         ut_assert_console_end();
944
945         ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
946         ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
947         ut_assert_nextline("123");
948
949         ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
950         ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
951         ut_assert_nextline("abc");
952
953         ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
954         ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
955         ut_assert_nextline("Argument not found");
956
957         ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
958         ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
959         ut_assert_nextline_empty();
960
961         ut_assert_console_end();
962
963         return 0;
964 }
965 BOOTSTD_TEST(bootflow_cmdline, 0);
This page took 0.084191 seconds and 4 git commands to generate.