blob: fb62731339631ca368289196773e7fd6c30a8220 [file] [log] [blame]
Simon Glassb255efc2022-04-24 23:31:24 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
Simon Glassda192022022-07-30 15:52:17 -06003 * Test for bootdev functions. All start with 'bootmeth'
Simon Glassb255efc2022-04-24 23:31:24 -06004 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <common.h>
Simon Glassf6d71a82022-07-30 15:52:19 -060010#include <bootmeth.h>
Simon Glassb255efc2022-04-24 23:31:24 -060011#include <bootstd.h>
Simon Glassf6d71a82022-07-30 15:52:19 -060012#include <dm.h>
Simon Glassb255efc2022-04-24 23:31:24 -060013#include <test/suites.h>
14#include <test/ut.h>
15#include "bootstd_common.h"
16
17/* Check 'bootmeth list' command */
18static int bootmeth_cmd_list(struct unit_test_state *uts)
19{
20 console_record_reset_enable();
21 ut_assertok(run_command("bootmeth list", 0));
22 ut_assert_nextline("Order Seq Name Description");
23 ut_assert_nextlinen("---");
24 ut_assert_nextline(" 0 0 syslinux Syslinux boot from a block device");
25 ut_assert_nextline(" 1 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060026 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
27 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060028 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060029 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
30 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060031 ut_assert_console_end();
32
33 return 0;
34}
35BOOTSTD_TEST(bootmeth_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
36
37/* Check 'bootmeth order' command */
38static int bootmeth_cmd_order(struct unit_test_state *uts)
39{
40 /* Select just one bootmethod */
41 console_record_reset_enable();
42 ut_assertok(run_command("bootmeth order syslinux", 0));
43 ut_assert_console_end();
44 ut_assertnonnull(env_get("bootmeths"));
45 ut_asserteq_str("syslinux", env_get("bootmeths"));
46
47 /* Only that one should be listed */
48 ut_assertok(run_command("bootmeth list", 0));
49 ut_assert_nextline("Order Seq Name Description");
50 ut_assert_nextlinen("---");
51 ut_assert_nextline(" 0 0 syslinux Syslinux boot from a block device");
52 ut_assert_nextlinen("---");
53 ut_assert_nextline("(1 bootmeth)");
54 ut_assert_console_end();
55
56 /* Check the -a flag, efi should show as not in the order ("-") */
57 ut_assertok(run_command("bootmeth list -a", 0));
58 ut_assert_nextline("Order Seq Name Description");
59 ut_assert_nextlinen("---");
60 ut_assert_nextline(" 0 0 syslinux Syslinux boot from a block device");
61 ut_assert_nextline(" - 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060062 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
63 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060064 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060065 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
66 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060067 ut_assert_console_end();
68
69 /* Check the -a flag with the reverse order */
70 ut_assertok(run_command("bootmeth order \"efi syslinux\"", 0));
71 ut_assert_console_end();
72 ut_assertok(run_command("bootmeth list -a", 0));
73 ut_assert_nextline("Order Seq Name Description");
74 ut_assert_nextlinen("---");
75 ut_assert_nextline(" 1 0 syslinux Syslinux boot from a block device");
76 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060077 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
78 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060079 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060080 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
81 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060082 ut_assert_console_end();
83
84 /* Now reset the order to empty, which should show all of them again */
85 ut_assertok(run_command("bootmeth order", 0));
86 ut_assert_console_end();
87 ut_assertnull(env_get("bootmeths"));
88 ut_assertok(run_command("bootmeth list", 0));
Simon Glass90b643d2022-07-30 15:52:36 -060089 ut_assert_skip_to_line(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
90 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060091
92 /* Try reverse order */
93 ut_assertok(run_command("bootmeth order \"efi syslinux\"", 0));
94 ut_assert_console_end();
95 ut_assertok(run_command("bootmeth list", 0));
96 ut_assert_nextline("Order Seq Name Description");
97 ut_assert_nextlinen("---");
98 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
99 ut_assert_nextline(" 1 0 syslinux Syslinux boot from a block device");
100 ut_assert_nextlinen("---");
101 ut_assert_nextline("(2 bootmeths)");
102 ut_assertnonnull(env_get("bootmeths"));
103 ut_asserteq_str("efi syslinux", env_get("bootmeths"));
104 ut_assert_console_end();
105
Simon Glassc8d37212022-07-30 15:52:34 -0600106 /* Try with global bootmeths */
Simon Glass90b643d2022-07-30 15:52:36 -0600107 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
108 return 0;
109
Simon Glassc8d37212022-07-30 15:52:34 -0600110 ut_assertok(run_command("bootmeth order \"efi firmware0\"", 0));
111 ut_assert_console_end();
112 ut_assertok(run_command("bootmeth list", 0));
113 ut_assert_nextline("Order Seq Name Description");
114 ut_assert_nextlinen("---");
115 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
116 ut_assert_nextline(" glob 2 firmware0 VBE simple");
117 ut_assert_nextlinen("---");
118 ut_assert_nextline("(2 bootmeths)");
119 ut_assertnonnull(env_get("bootmeths"));
120 ut_asserteq_str("efi firmware0", env_get("bootmeths"));
121 ut_assert_console_end();
122
Simon Glassb255efc2022-04-24 23:31:24 -0600123 return 0;
124}
125BOOTSTD_TEST(bootmeth_cmd_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
126
127/* Check 'bootmeths' env var */
128static int bootmeth_env(struct unit_test_state *uts)
129{
130 struct bootstd_priv *std;
131
132 ut_assertok(bootstd_get_priv(&std));
133
134 /* Select just one bootmethod */
135 console_record_reset_enable();
136 ut_assertok(env_set("bootmeths", "syslinux"));
137 ut_asserteq(1, std->bootmeth_count);
138
139 /* Select an invalid bootmethod */
140 ut_asserteq(1, run_command("setenv bootmeths fred", 0));
141 ut_assert_nextline("Unknown bootmeth 'fred'");
142 ut_assert_nextlinen("## Error inserting");
143 ut_assert_console_end();
144
145 ut_assertok(env_set("bootmeths", "efi syslinux"));
146 ut_asserteq(2, std->bootmeth_count);
147 ut_assert_console_end();
148
149 return 0;
150}
151BOOTSTD_TEST(bootmeth_env, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassf6d71a82022-07-30 15:52:19 -0600152
153/* Check the get_state_desc() method */
154static int bootmeth_state(struct unit_test_state *uts)
155{
156 struct udevice *dev;
157 char buf[50];
158
159 ut_assertok(uclass_first_device(UCLASS_BOOTMETH, &dev));
160 ut_assertnonnull(dev);
161
162 ut_assertok(bootmeth_get_state_desc(dev, buf, sizeof(buf)));
163 ut_asserteq_str("OK", buf);
164
165 return 0;
166}
167BOOTSTD_TEST(bootmeth_state, UT_TESTF_DM | UT_TESTF_SCAN_FDT);