]> Git Repo - J-u-boot.git/blob - test/boot/bootflow.c
Merge tag 'doc-next' of https://source.denx.de/u-boot/custodians/u-boot-efi into...
[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_cros);
31 extern U_BOOT_DRIVER(bootmeth_script);
32
33 static int inject_response(struct unit_test_state *uts)
34 {
35         /*
36          * The image being booted presents a menu of options:
37          *
38          * Fedora-Workstation-armhfp-31-1.9 Boot Options.
39          * 1:   Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
40          * Enter choice:
41          *
42          * Provide input for this, to avoid waiting two seconds for a timeout.
43          */
44         ut_asserteq(2, console_in_puts("1\n"));
45
46         return 0;
47 }
48
49 /* Check 'bootflow scan/list' commands */
50 static int bootflow_cmd(struct unit_test_state *uts)
51 {
52         console_record_reset_enable();
53         ut_assertok(run_command("bootdev select 1", 0));
54         ut_assert_console_end();
55         ut_assertok(run_command("bootflow scan -lH", 0));
56         ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
57         ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
58         ut_assert_nextlinen("---");
59         ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
60         ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
61         ut_assert_nextline("  0  extlinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
62         ut_assert_nextline("No more bootdevs");
63         ut_assert_nextlinen("---");
64         ut_assert_nextline("(1 bootflow, 1 valid)");
65         ut_assert_console_end();
66
67         ut_assertok(run_command("bootflow list", 0));
68         ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'");
69         ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
70         ut_assert_nextlinen("---");
71         ut_assert_nextline("  0  extlinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
72         ut_assert_nextlinen("---");
73         ut_assert_nextline("(1 bootflow, 1 valid)");
74         ut_assert_console_end();
75
76         return 0;
77 }
78 BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
79
80 /* Check 'bootflow scan' with a label / seq */
81 static int bootflow_cmd_label(struct unit_test_state *uts)
82 {
83         test_set_eth_enable(false);
84
85         console_record_reset_enable();
86         ut_assertok(run_command("bootflow scan -lH mmc1", 0));
87         ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
88         ut_assert_skip_to_line("(1 bootflow, 1 valid)");
89         ut_assert_console_end();
90
91         ut_assertok(run_command("bootflow scan -lH 0", 0));
92         ut_assert_nextline("Scanning for bootflows with label '0'");
93         ut_assert_skip_to_line("(0 bootflows, 0 valid)");
94         ut_assert_console_end();
95
96         /*
97          * with ethernet enabled we have 8 devices ahead of the mmc ones:
98          *
99          * ut_assertok(run_command("bootdev list", 0));
100          * Seq  Probed  Status  Uclass    Name
101          * ---  ------  ------  --------  ------------------
102          * 0   [ + ]      OK  ethernet  [email protected]
103          * 1   [   ]      OK  ethernet  [email protected]
104          * 2   [   ]      OK  ethernet  sbe5.bootdev
105          * 3   [   ]      OK  ethernet  [email protected]
106          * 4   [   ]      OK  ethernet  phy-test-eth.bootdev
107          * 5   [   ]      OK  ethernet  dsa-test-eth.bootdev
108          * 6   [   ]      OK  ethernet  [email protected]
109          * 7   [   ]      OK  ethernet  [email protected]
110          * 8   [   ]      OK  mmc       mmc2.bootdev
111          * 9   [ + ]      OK  mmc       mmc1.bootdev
112          * a   [   ]      OK  mmc       mmc0.bootdev
113          */
114         ut_assertok(run_command("bootflow scan -lH 9", 0));
115         ut_assert_nextline("Scanning for bootflows with label '9'");
116         ut_assert_skip_to_line("(1 bootflow, 1 valid)");
117
118         ut_assertok(run_command("bootflow scan -lH 0", 0));
119         ut_assert_nextline("Scanning for bootflows with label '0'");
120         ut_assert_skip_to_line("(0 bootflows, 0 valid)");
121         ut_assert_console_end();
122
123         return 0;
124 }
125 BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
126              UT_TESTF_ETH_BOOTDEV);
127
128 /* Check 'bootflow scan/list' commands using all bootdevs */
129 static int bootflow_cmd_glob(struct unit_test_state *uts)
130 {
131         ut_assertok(bootstd_test_drop_bootdev_order(uts));
132
133         console_record_reset_enable();
134         ut_assertok(run_command("bootflow scan -lGH", 0));
135         ut_assert_nextline("Scanning for bootflows in all bootdevs");
136         ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
137         ut_assert_nextlinen("---");
138         ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
139         ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
140         ut_assert_nextline("  0  extlinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
141         ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
142         ut_assert_nextline("No more bootdevs");
143         ut_assert_nextlinen("---");
144         ut_assert_nextline("(1 bootflow, 1 valid)");
145         ut_assert_console_end();
146
147         ut_assertok(run_command("bootflow list", 0));
148         ut_assert_nextline("Showing all bootflows");
149         ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
150         ut_assert_nextlinen("---");
151         ut_assert_nextline("  0  extlinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
152         ut_assert_nextlinen("---");
153         ut_assert_nextline("(1 bootflow, 1 valid)");
154         ut_assert_console_end();
155
156         return 0;
157 }
158 BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
159
160 /* Check 'bootflow scan -e' */
161 static int bootflow_cmd_scan_e(struct unit_test_state *uts)
162 {
163         ut_assertok(bootstd_test_drop_bootdev_order(uts));
164
165         console_record_reset_enable();
166         ut_assertok(run_command("bootflow scan -aleGH", 0));
167         ut_assert_nextline("Scanning for bootflows in all bootdevs");
168         ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
169         ut_assert_nextlinen("---");
170         ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
171         ut_assert_nextline("  0  extlinux     media   mmc          0  mmc2.bootdev.whole        ");
172         ut_assert_nextline("     ** No partition found, err=-93: Protocol not supported");
173         ut_assert_nextline("  1  efi          media   mmc          0  mmc2.bootdev.whole        ");
174         ut_assert_nextline("     ** No partition found, err=-93: Protocol not supported");
175
176         ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
177         ut_assert_nextline("  2  extlinux     media   mmc          0  mmc1.bootdev.whole        ");
178         ut_assert_nextline("     ** No partition found, err=-2: No such file or directory");
179         ut_assert_nextline("  3  efi          media   mmc          0  mmc1.bootdev.whole        ");
180         ut_assert_nextline("     ** No partition found, err=-2: No such file or directory");
181         ut_assert_nextline("  4  extlinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
182         ut_assert_nextline("  5  efi          fs      mmc          1  mmc1.bootdev.part_1       efi/boot/bootsbox.efi");
183
184         ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
185         ut_assert_skip_to_line(
186                 " 3f  efi          media   mmc          0  mmc0.bootdev.whole        ");
187         ut_assert_nextline("     ** No partition found, err=-93: Protocol not supported");
188         ut_assert_nextline("No more bootdevs");
189         ut_assert_nextlinen("---");
190         ut_assert_nextline("(64 bootflows, 1 valid)");
191         ut_assert_console_end();
192
193         ut_assertok(run_command("bootflow list", 0));
194         ut_assert_nextline("Showing all bootflows");
195         ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
196         ut_assert_nextlinen("---");
197         ut_assert_nextline("  0  extlinux     media   mmc          0  mmc2.bootdev.whole        ");
198         ut_assert_nextline("  1  efi          media   mmc          0  mmc2.bootdev.whole        ");
199         ut_assert_skip_to_line(
200                 "  4  extlinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
201         ut_assert_skip_to_line(" 3f  efi          media   mmc          0  mmc0.bootdev.whole        ");
202         ut_assert_nextlinen("---");
203         ut_assert_nextline("(64 bootflows, 1 valid)");
204         ut_assert_console_end();
205
206         return 0;
207 }
208 BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
209
210 /* Check 'bootflow info' */
211 static int bootflow_cmd_info(struct unit_test_state *uts)
212 {
213         console_record_reset_enable();
214         ut_assertok(run_command("bootdev select 1", 0));
215         ut_assert_console_end();
216         ut_assertok(run_command("bootflow scan", 0));
217         ut_assert_console_end();
218         ut_assertok(run_command("bootflow select 0", 0));
219         ut_assert_console_end();
220         ut_assertok(run_command("bootflow info", 0));
221         ut_assert_nextline("Name:      mmc1.bootdev.part_1");
222         ut_assert_nextline("Device:    mmc1.bootdev");
223         ut_assert_nextline("Block dev: mmc1.blk");
224         ut_assert_nextline("Method:    extlinux");
225         ut_assert_nextline("State:     ready");
226         ut_assert_nextline("Partition: 1");
227         ut_assert_nextline("Subdir:    (none)");
228         ut_assert_nextline("Filename:  /extlinux/extlinux.conf");
229         ut_assert_nextlinen("Buffer:    ");
230         ut_assert_nextline("Size:      253 (595 bytes)");
231         ut_assert_nextline("OS:        Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
232         ut_assert_nextline("Cmdline:   (none)");
233         ut_assert_nextline("Logo:      (none)");
234         ut_assert_nextline("FDT:       <NULL>");
235         ut_assert_nextline("Error:     0");
236         ut_assert_console_end();
237
238         ut_assertok(run_command("bootflow info -d", 0));
239         ut_assert_nextline("Name:      mmc1.bootdev.part_1");
240         ut_assert_skip_to_line("Error:     0");
241         ut_assert_nextline("Contents:");
242         ut_assert_nextline("%s", "");
243         ut_assert_nextline("# extlinux.conf generated by appliance-creator");
244         ut_assert_skip_to_line("        initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
245         ut_assert_console_end();
246
247         return 0;
248 }
249 BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
250
251 /* Check 'bootflow scan -b' to boot the first available bootdev */
252 static int bootflow_scan_boot(struct unit_test_state *uts)
253 {
254         console_record_reset_enable();
255         ut_assertok(inject_response(uts));
256         ut_assertok(run_command("bootflow scan -b", 0));
257         ut_assert_nextline(
258                 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
259         ut_assert_nextline("Ignoring unknown command: ui");
260
261         /*
262          * We expect it to get through to boot although sandbox always returns
263          * -EFAULT as it cannot actually boot the kernel
264          */
265         ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
266         ut_assert_nextline("Boot failed (err=-14)");
267         ut_assert_console_end();
268
269         return 0;
270 }
271 BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
272
273 /* Check iterating through available bootflows */
274 static int bootflow_iter(struct unit_test_state *uts)
275 {
276         struct bootflow_iter iter;
277         struct bootflow bflow;
278
279         bootstd_clear_glob();
280
281         /* The first device is mmc2.bootdev which has no media */
282         ut_asserteq(-EPROTONOSUPPORT,
283                     bootflow_scan_first(NULL, NULL, &iter,
284                                         BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
285         ut_asserteq(2, iter.num_methods);
286         ut_asserteq(0, iter.cur_method);
287         ut_asserteq(0, iter.part);
288         ut_asserteq(0, iter.max_part);
289         ut_asserteq_str("extlinux", iter.method->name);
290         ut_asserteq(0, bflow.err);
291
292         /*
293          * This shows MEDIA even though there is none, since in
294          * bootdev_find_in_blk() we call part_get_info() which returns
295          * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
296          * know.
297          */
298         ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
299
300         ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
301         ut_asserteq(2, iter.num_methods);
302         ut_asserteq(1, iter.cur_method);
303         ut_asserteq(0, iter.part);
304         ut_asserteq(0, iter.max_part);
305         ut_asserteq_str("efi", iter.method->name);
306         ut_asserteq(0, bflow.err);
307         ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
308         bootflow_free(&bflow);
309
310         /* The next device is mmc1.bootdev - at first we use the whole device */
311         ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
312         ut_asserteq(2, iter.num_methods);
313         ut_asserteq(0, iter.cur_method);
314         ut_asserteq(0, iter.part);
315         ut_asserteq(0x1e, iter.max_part);
316         ut_asserteq_str("extlinux", iter.method->name);
317         ut_asserteq(0, bflow.err);
318         ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
319         bootflow_free(&bflow);
320
321         ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
322         ut_asserteq(2, iter.num_methods);
323         ut_asserteq(1, iter.cur_method);
324         ut_asserteq(0, iter.part);
325         ut_asserteq(0x1e, iter.max_part);
326         ut_asserteq_str("efi", iter.method->name);
327         ut_asserteq(0, bflow.err);
328         ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
329         bootflow_free(&bflow);
330
331         /* Then more to partition 1 where we find something */
332         ut_assertok(bootflow_scan_next(&iter, &bflow));
333         ut_asserteq(2, iter.num_methods);
334         ut_asserteq(0, iter.cur_method);
335         ut_asserteq(1, iter.part);
336         ut_asserteq(0x1e, iter.max_part);
337         ut_asserteq_str("extlinux", iter.method->name);
338         ut_asserteq(0, bflow.err);
339         ut_asserteq(BOOTFLOWST_READY, bflow.state);
340         bootflow_free(&bflow);
341
342         ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
343         ut_asserteq(2, iter.num_methods);
344         ut_asserteq(1, iter.cur_method);
345         ut_asserteq(1, iter.part);
346         ut_asserteq(0x1e, iter.max_part);
347         ut_asserteq_str("efi", iter.method->name);
348         ut_asserteq(0, bflow.err);
349         ut_asserteq(BOOTFLOWST_FS, bflow.state);
350         bootflow_free(&bflow);
351
352         /* Then more to partition 2 which exists but is not bootable */
353         ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
354         ut_asserteq(2, iter.num_methods);
355         ut_asserteq(0, iter.cur_method);
356         ut_asserteq(2, iter.part);
357         ut_asserteq(0x1e, iter.max_part);
358         ut_asserteq_str("extlinux", iter.method->name);
359         ut_asserteq(0, bflow.err);
360         ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
361         bootflow_free(&bflow);
362
363         bootflow_iter_uninit(&iter);
364
365         ut_assert_console_end();
366
367         return 0;
368 }
369 BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
370
371 #if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
372 /* Check using the system bootdev */
373 static int bootflow_system(struct unit_test_state *uts)
374 {
375         struct udevice *bootstd, *dev;
376
377         if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
378                 return -EAGAIN;
379         ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
380         ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_efi_mgr),
381                                 "efi_mgr", 0, ofnode_null(), &dev));
382         ut_assertok(device_probe(dev));
383         sandbox_set_fake_efi_mgr_dev(dev, true);
384
385         /* We should get a single 'bootmgr' method right at the end */
386         bootstd_clear_glob();
387         console_record_reset_enable();
388         ut_assertok(run_command("bootflow scan -lH", 0));
389         ut_assert_skip_to_line(
390                 "  0  efi_mgr      ready   (none)       0  <NULL>                    ");
391         ut_assert_skip_to_line("No more bootdevs");
392         ut_assert_skip_to_line("(2 bootflows, 2 valid)");
393         ut_assert_console_end();
394
395         return 0;
396 }
397 BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_PDATA |
398              UT_TESTF_SCAN_FDT);
399 #endif
400
401 /* Check disabling a bootmethod if it requests it */
402 static int bootflow_iter_disable(struct unit_test_state *uts)
403 {
404         struct udevice *bootstd, *dev;
405         struct bootflow_iter iter;
406         struct bootflow bflow;
407         int i;
408
409         /* Add the EFI bootmgr driver */
410         ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
411         ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
412                                        &dev));
413
414         ut_assertok(bootstd_test_drop_bootdev_order(uts));
415
416         bootstd_clear_glob();
417         console_record_reset_enable();
418         ut_assertok(inject_response(uts));
419         ut_assertok(run_command("bootflow scan -lbH", 0));
420
421         /* Try to boot the bootmgr flow, which will fail */
422         console_record_reset_enable();
423         ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
424         ut_asserteq(3, iter.num_methods);
425         ut_asserteq_str("sandbox", iter.method->name);
426         ut_assertok(inject_response(uts));
427         ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
428
429         ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
430         ut_assert_console_end();
431
432         /* Check that the sandbox bootmeth has been removed */
433         ut_asserteq(2, iter.num_methods);
434         for (i = 0; i < iter.num_methods; i++)
435                 ut_assert(strcmp("sandbox", iter.method_order[i]->name));
436
437         return 0;
438 }
439 BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
440
441 /* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
442 static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
443 {
444         if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
445                 return -EAGAIN;
446
447         ut_assertok(bootstd_test_drop_bootdev_order(uts));
448
449         /*
450          * Make sure that the -G flag makes the scan fail, since this is not
451          * supported when an ordering is provided
452          */
453         console_record_reset_enable();
454         ut_assertok(bootmeth_set_order("efi firmware0"));
455         ut_assertok(run_command("bootflow scan -lGH", 0));
456         ut_assert_nextline("Scanning for bootflows in all bootdevs");
457         ut_assert_nextline(
458                 "Seq  Method       State   Uclass    Part  Name                      Filename");
459         ut_assert_nextlinen("---");
460         ut_assert_nextlinen("---");
461         ut_assert_nextline("(0 bootflows, 0 valid)");
462         ut_assert_console_end();
463
464         ut_assertok(run_command("bootflow scan -lH", 0));
465         ut_assert_nextline("Scanning for bootflows in all bootdevs");
466         ut_assert_nextline(
467                 "Seq  Method       State   Uclass    Part  Name                      Filename");
468         ut_assert_nextlinen("---");
469         ut_assert_nextline("Scanning global bootmeth 'firmware0':");
470         ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
471         ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
472         ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
473         ut_assert_nextline("No more bootdevs");
474         ut_assert_nextlinen("---");
475         ut_assert_nextline("(0 bootflows, 0 valid)");
476         ut_assert_console_end();
477
478         return 0;
479 }
480 BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
481
482 /* Check 'bootflow boot' to boot a selected bootflow */
483 static int bootflow_cmd_boot(struct unit_test_state *uts)
484 {
485         console_record_reset_enable();
486         ut_assertok(run_command("bootdev select 1", 0));
487         ut_assert_console_end();
488         ut_assertok(run_command("bootflow scan", 0));
489         ut_assert_console_end();
490         ut_assertok(run_command("bootflow select 0", 0));
491         ut_assert_console_end();
492
493         ut_assertok(inject_response(uts));
494         ut_asserteq(1, run_command("bootflow boot", 0));
495         ut_assert_nextline(
496                 "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
497         ut_assert_nextline("Ignoring unknown command: ui");
498
499         /*
500          * We expect it to get through to boot although sandbox always returns
501          * -EFAULT as it cannot actually boot the kernel
502          */
503         ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
504         ut_assert_nextline("Boot failed (err=-14)");
505         ut_assert_console_end();
506
507         return 0;
508 }
509 BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
510
511 /**
512  * prep_mmc_bootdev() - Set up an mmc bootdev so we can access other distros
513  *
514  * @uts: Unit test state
515  * @mmc_dev: MMC device to use, e.g. "mmc4"
516  * Returns 0 on success, -ve on failure
517  */
518 static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
519                             bool bind_cros)
520 {
521         const char *order[] = {"mmc2", "mmc1", mmc_dev, NULL};
522         struct udevice *dev, *bootstd;
523         struct bootstd_priv *std;
524         const char **old_order;
525         ofnode root, node;
526
527         /* Enable the mmc4 node since we need a second bootflow */
528         root = oftree_root(oftree_default());
529         node = ofnode_find_subnode(root, mmc_dev);
530         ut_assert(ofnode_valid(node));
531         ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
532
533         /* Enable the script bootmeth too */
534         ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
535         ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_script),
536                                 "bootmeth_script", 0, ofnode_null(), &dev));
537
538         /* Enable the cros bootmeth if needed */
539         if (bind_cros) {
540                 ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
541                 ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_cros),
542                                         "cros", 0, ofnode_null(), &dev));
543         }
544
545         /* Change the order to include the device */
546         std = dev_get_priv(bootstd);
547         old_order = std->bootdev_order;
548         std->bootdev_order = order;
549
550         console_record_reset_enable();
551         ut_assertok(run_command("bootflow scan", 0));
552         ut_assert_console_end();
553
554         /* Restore the order used by the device tree */
555         std->bootdev_order = old_order;
556
557         return 0;
558 }
559
560 /**
561  * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
562  *
563  * @uts: Unit test state
564  * Returns 0 on success, -ve on failure
565  */
566 static int prep_mmc4_bootdev(struct unit_test_state *uts)
567 {
568         ut_assertok(prep_mmc_bootdev(uts, "mmc4", false));
569
570         return 0;
571 }
572
573 /* Check 'bootflow menu' to select a bootflow */
574 static int bootflow_cmd_menu(struct unit_test_state *uts)
575 {
576         char prev[3];
577
578         ut_assertok(prep_mmc4_bootdev(uts));
579
580         /* Add keypresses to move to and select the second one in the list */
581         prev[0] = CTL_CH('n');
582         prev[1] = '\r';
583         prev[2] = '\0';
584         ut_asserteq(2, console_in_puts(prev));
585
586         ut_assertok(run_command("bootflow menu", 0));
587         ut_assert_nextline("Selected: Armbian");
588         ut_assert_console_end();
589
590         return 0;
591 }
592 BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
593
594 /* Check searching for a single bootdev using the hunters */
595 static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
596 {
597         struct bootstd_priv *std;
598
599         /* get access to the used hunters */
600         ut_assertok(bootstd_get_priv(&std));
601
602         ut_assertok(bootstd_test_drop_bootdev_order(uts));
603
604         console_record_reset_enable();
605         ut_assertok(run_command("bootflow scan -l mmc1", 0));
606         ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
607         ut_assert_skip_to_line("(1 bootflow, 1 valid)");
608         ut_assert_console_end();
609
610         /* check that the hunter was used */
611         ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
612
613         return 0;
614 }
615 BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
616
617 /* Check searching for a uclass label using the hunters */
618 static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
619 {
620         struct bootstd_priv *std;
621
622         /* get access to the used hunters */
623         ut_assertok(bootstd_get_priv(&std));
624
625         test_set_skip_delays(true);
626         test_set_eth_enable(false);
627         ut_assertok(bootstd_test_drop_bootdev_order(uts));
628
629         console_record_reset_enable();
630         ut_assertok(run_command("bootflow scan -l mmc", 0));
631
632         /* check that the hunter was used */
633         ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
634
635         /* check that we got the mmc1 bootflow */
636         ut_assert_nextline("Scanning for bootflows with label 'mmc'");
637         ut_assert_nextlinen("Seq");
638         ut_assert_nextlinen("---");
639         ut_assert_nextline("Hunting with: simple_bus");
640         ut_assert_nextline("Found 2 extension board(s).");
641         ut_assert_nextline("Hunting with: mmc");
642         ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
643         ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
644         ut_assert_nextline(
645                 "  0  extlinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
646         ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
647         ut_assert_skip_to_line("(1 bootflow, 1 valid)");
648         ut_assert_console_end();
649
650         return 0;
651 }
652 BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
653
654 /**
655  * check_font() - Check that the font size for an item matches expectations
656  *
657  * @uts: Unit test state
658  * @scn: Scene containing the text object
659  * @id: ID of the text object
660  * Returns 0 on success, -ve on failure
661  */
662 static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
663                       int font_size)
664 {
665         struct scene_obj_txt *txt;
666
667         txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
668         ut_assertnonnull(txt);
669
670         ut_asserteq(font_size, txt->font_size);
671
672         return 0;
673 }
674
675 /* Check themes work with a bootflow menu */
676 static int bootflow_menu_theme(struct unit_test_state *uts)
677 {
678         const int font_size = 30;
679         struct scene *scn;
680         struct expo *exp;
681         ofnode node;
682         int i;
683
684         ut_assertok(prep_mmc4_bootdev(uts));
685
686         ut_assertok(bootflow_menu_new(&exp));
687         node = ofnode_path("/bootstd/theme");
688         ut_assert(ofnode_valid(node));
689         ut_assertok(bootflow_menu_apply_theme(exp, node));
690
691         scn = expo_lookup_scene_id(exp, MAIN);
692         ut_assertnonnull(scn);
693
694         /*
695          * Check that the txt objects have the correct font size from the
696          * device tree node: bootstd/theme
697          *
698          * Check both menu items, since there are two bootflows
699          */
700         ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
701         ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
702         for (i = 0; i < 2; i++) {
703                 ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
704                 ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
705                 ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
706         }
707
708         expo_destroy(exp);
709
710         return 0;
711 }
712 BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
713
714 /**
715  * check_arg() - Check both the normal case and the buffer-overflow case
716  *
717  * @uts: Unit-test state
718  * @expect_ret: Expected return value (i.e. buffer length)
719  * @expect_str: String expected to be returned
720  * @buf: Buffer to use
721  * @from: Original cmdline to update
722  * @arg: Argument to update (e.g. "console")
723  * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
724  * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
725  * it without any value ("initrd")
726  */
727 static int check_arg(struct unit_test_state *uts, int expect_ret,
728                      const char *expect_str, char *buf, const char *from,
729                      const char *arg, const char *val)
730 {
731         /* check for writing outside the reported bounds */
732         buf[expect_ret] = '[';
733         ut_asserteq(expect_ret,
734                     cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
735         ut_asserteq_str(expect_str, buf);
736         ut_asserteq('[', buf[expect_ret]);
737
738         /* do the test again but with one less byte in the buffer */
739         ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
740                                             val, NULL));
741
742         return 0;
743 }
744
745 /* Test of bootflow_cmdline_set_arg() */
746 static int test_bootflow_cmdline_set(struct unit_test_state *uts)
747 {
748         char buf[50];
749         const int size = sizeof(buf);
750
751         /*
752          * note that buffer-overflow tests are immediately each test case, just
753          * top keep the code together
754          */
755
756         /* add an arg that doesn't already exist, starting from empty */
757         ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
758                                              NULL));
759
760         ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
761         ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
762         ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
763
764         /* add an arg that doesn't already exist, starting from non-empty */
765         ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
766                               BOOTFLOWCL_EMPTY));
767         ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
768                               ""));
769         ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
770                               "fred"));
771
772         /* update an arg at the start */
773         ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
774         ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
775                               BOOTFLOWCL_EMPTY));
776         ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
777         ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
778         ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
779                               "1234"));
780
781         /* update an arg at the end */
782         ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
783                               NULL));
784         ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
785                               BOOTFLOWCL_EMPTY));
786         ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
787                               ""));
788         ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
789                               "1"));
790         ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
791                               "arg", "1234"));
792
793         /* update an arg in the middle */
794         ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
795                               "mary=abc arg=123 john=2", "arg", NULL));
796         ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
797                               "mary=abc arg=123 john=2", "arg",
798                               BOOTFLOWCL_EMPTY));
799         ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
800                               "mary=abc arg=123 john=2", "arg", ""));
801         ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
802                               "mary=abc arg=123 john=2", "arg", "1"));
803         ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
804                               "mary=abc arg=123 john=2", "arg", "1234"));
805
806         /* handle existing args with quotes */
807         ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
808                               "mary=\"abc\" arg=123 john", "arg", NULL));
809
810         /* handle existing args with quoted spaces */
811         ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
812                               "mary=\"abc def\" arg=123 john", "arg", NULL));
813
814         ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
815                               buf, "mary=\"abc def\" arg=123 john", "def",
816                               "4"));
817
818         /* quote at the start */
819         ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
820                                             "mary=\"abc def\" arg=\"123 456\"",
821                                             "arg", "\"4 5 6", NULL));
822
823         /* quote at the end */
824         ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
825                                             "mary=\"abc def\" arg=\"123 456\"",
826                                             "arg", "4 5 6\"", NULL));
827
828         /* quote in the middle */
829         ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
830                                             "mary=\"abc def\" arg=\"123 456\"",
831                                             "arg", "\"4 \"5 6\"", NULL));
832
833         /* handle updating a quoted arg */
834         ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
835                               "mary=\"abc def\" arg=\"123 456\"", "arg",
836                               "4 5 6"));
837
838         /* changing a quoted arg to a non-quoted arg */
839         ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
840                               "mary=\"abc def\" arg=\"123 456\"", "arg",
841                               "789"));
842
843         /* changing a non-quoted arg to a quoted arg */
844         ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
845                               "mary=\"abc def\" arg=123", "arg", "456 789"));
846
847         /* handling of spaces */
848         ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
849         ut_assertok(check_arg(uts, 8, "arg=123", buf, "   ", "arg", "123"));
850         ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john  ", "arg",
851                               "123"));
852         ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john  arg=123  ",
853                               "arg", "123"));
854         ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
855                               " john  arg=123 mary ", "arg", "123"));
856
857         /* unchanged arg */
858         ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
859
860         /* arg which starts with the same name */
861         ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
862                               "mary=abc johnathon=2 john=1", "john", "3"));
863
864         return 0;
865 }
866 BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
867
868 /* Test of bootflow_cmdline_set_arg() */
869 static int bootflow_set_arg(struct unit_test_state *uts)
870 {
871         struct bootflow s_bflow, *bflow = &s_bflow;
872         ulong mem_start;
873
874         ut_assertok(env_set("bootargs", NULL));
875
876         mem_start = ut_check_delta(0);
877
878         /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
879         bflow->cmdline = NULL;
880         ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
881         ut_asserteq_str(bflow->cmdline, "fred=123");
882
883         ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
884         ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
885
886         ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
887         ut_asserteq_str(bflow->cmdline, "fred=123");
888         ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
889         ut_asserteq_ptr(bflow->cmdline, NULL);
890
891         ut_asserteq(0, ut_check_delta(mem_start));
892
893         ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
894         ut_asserteq_str("mary=here", env_get("bootargs"));
895         ut_assertok(env_set("bootargs", NULL));
896
897         return 0;
898 }
899 BOOTSTD_TEST(bootflow_set_arg, 0);
900
901 /* Test of bootflow_cmdline_get_arg() */
902 static int bootflow_cmdline_get(struct unit_test_state *uts)
903 {
904         int pos;
905
906         /* empty string */
907         ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
908
909         /* arg with empty value */
910         ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
911         ut_asserteq(5, pos);
912
913         /* arg with a value */
914         ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
915         ut_asserteq(5, pos);
916
917         /* arg with a value */
918         ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
919         ut_asserteq(12, pos);
920
921         /* arg with a value, after quoted arg */
922         ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
923         ut_asserteq(16, pos);
924
925         /* arg in the middle */
926         ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
927                                        &pos));
928         ut_asserteq(15, pos);
929
930         /* quoted arg */
931         ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
932                                        "fred", &pos));
933         ut_asserteq(17, pos);
934
935         /* args starting with the same prefix */
936         ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
937                                        &pos));
938         ut_asserteq(26, pos);
939
940         return 0;
941 }
942 BOOTSTD_TEST(bootflow_cmdline_get, 0);
943
944 static int bootflow_cmdline(struct unit_test_state *uts)
945 {
946         ut_assertok(run_command("bootflow scan mmc", 0));
947         ut_assertok(run_command("bootflow sel 0", 0));
948         console_record_reset_enable();
949
950         ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
951         ut_assert_nextline("Argument not found");
952         ut_assert_console_end();
953
954         ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
955         ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
956         ut_assert_nextline("123");
957
958         ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
959         ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
960         ut_assert_nextline("abc");
961
962         ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
963         ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
964         ut_assert_nextline("Argument not found");
965
966         ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
967         ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
968         ut_assert_nextline_empty();
969
970         ut_assert_console_end();
971
972         return 0;
973 }
974 BOOTSTD_TEST(bootflow_cmdline, 0);
975
976 /* Test ChromiumOS bootmeth */
977 static int bootflow_cros(struct unit_test_state *uts)
978 {
979         ut_assertok(prep_mmc_bootdev(uts, "mmc5", true));
980         ut_assertok(run_command("bootflow list", 0));
981
982         ut_assert_nextlinen("Showing all");
983         ut_assert_nextlinen("Seq");
984         ut_assert_nextlinen("---");
985         ut_assert_nextlinen("  0  extlinux");
986         ut_assert_nextlinen("  1  cros         ready   mmc          2  mmc5.bootdev.part_2       ");
987         ut_assert_nextlinen("  2  cros         ready   mmc          4  mmc5.bootdev.part_4       ");
988         ut_assert_nextlinen("---");
989         ut_assert_skip_to_line("(3 bootflows, 3 valid)");
990
991         ut_assert_console_end();
992
993         return 0;
994 }
995 BOOTSTD_TEST(bootflow_cros, 0);
This page took 0.084595 seconds and 4 git commands to generate.