blob: 577f259fb3718386c1212293d9833a76a4d17fd8 [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
Simon Glassf6d71a82022-07-30 15:52:19 -06009#include <bootmeth.h>
Simon Glassb255efc2022-04-24 23:31:24 -060010#include <bootstd.h>
Simon Glassf6d71a82022-07-30 15:52:19 -060011#include <dm.h>
Simon Glassb255efc2022-04-24 23:31:24 -060012#include <test/ut.h>
13#include "bootstd_common.h"
14
15/* Check 'bootmeth list' command */
16static int bootmeth_cmd_list(struct unit_test_state *uts)
17{
Simon Glassb255efc2022-04-24 23:31:24 -060018 ut_assertok(run_command("bootmeth list", 0));
19 ut_assert_nextline("Order Seq Name Description");
20 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060021 ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060022 ut_assert_nextline(" 1 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060023 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
24 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060025 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060026 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
27 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060028 ut_assert_console_end();
29
30 return 0;
31}
Simon Glassf0425022024-08-22 07:57:54 -060032BOOTSTD_TEST(bootmeth_cmd_list, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -060033
34/* Check 'bootmeth order' command */
35static int bootmeth_cmd_order(struct unit_test_state *uts)
36{
37 /* Select just one bootmethod */
Simon Glassb71d7f72023-05-10 16:34:46 -060038 ut_assertok(run_command("bootmeth order extlinux", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060039 ut_assert_console_end();
40 ut_assertnonnull(env_get("bootmeths"));
Simon Glassb71d7f72023-05-10 16:34:46 -060041 ut_asserteq_str("extlinux", env_get("bootmeths"));
Simon Glassb255efc2022-04-24 23:31:24 -060042
43 /* Only that one should be listed */
44 ut_assertok(run_command("bootmeth list", 0));
45 ut_assert_nextline("Order Seq Name Description");
46 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060047 ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060048 ut_assert_nextlinen("---");
49 ut_assert_nextline("(1 bootmeth)");
50 ut_assert_console_end();
51
52 /* Check the -a flag, efi should show as not in the order ("-") */
53 ut_assertok(run_command("bootmeth list -a", 0));
54 ut_assert_nextline("Order Seq Name Description");
55 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060056 ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060057 ut_assert_nextline(" - 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060058 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
59 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060060 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060061 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
62 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060063 ut_assert_console_end();
64
65 /* Check the -a flag with the reverse order */
Simon Glassb71d7f72023-05-10 16:34:46 -060066 ut_assertok(run_command("bootmeth order \"efi extlinux\"", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060067 ut_assert_console_end();
68 ut_assertok(run_command("bootmeth list -a", 0));
69 ut_assert_nextline("Order Seq Name Description");
70 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060071 ut_assert_nextline(" 1 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060072 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060073 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
74 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060075 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060076 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
77 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060078 ut_assert_console_end();
79
80 /* Now reset the order to empty, which should show all of them again */
81 ut_assertok(run_command("bootmeth order", 0));
82 ut_assert_console_end();
83 ut_assertnull(env_get("bootmeths"));
84 ut_assertok(run_command("bootmeth list", 0));
Simon Glass90b643d2022-07-30 15:52:36 -060085 ut_assert_skip_to_line(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
86 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060087
88 /* Try reverse order */
Simon Glassb71d7f72023-05-10 16:34:46 -060089 ut_assertok(run_command("bootmeth order \"efi extlinux\"", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060090 ut_assert_console_end();
91 ut_assertok(run_command("bootmeth list", 0));
92 ut_assert_nextline("Order Seq Name Description");
93 ut_assert_nextlinen("---");
94 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
Simon Glassb71d7f72023-05-10 16:34:46 -060095 ut_assert_nextline(" 1 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060096 ut_assert_nextlinen("---");
97 ut_assert_nextline("(2 bootmeths)");
98 ut_assertnonnull(env_get("bootmeths"));
Simon Glassb71d7f72023-05-10 16:34:46 -060099 ut_asserteq_str("efi extlinux", env_get("bootmeths"));
Simon Glassb255efc2022-04-24 23:31:24 -0600100 ut_assert_console_end();
101
Simon Glassc7599442022-10-20 18:22:49 -0600102 return 0;
103}
Simon Glassf0425022024-08-22 07:57:54 -0600104BOOTSTD_TEST(bootmeth_cmd_order, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassc7599442022-10-20 18:22:49 -0600105
106/* Check 'bootmeth order' command with global bootmeths */
107static int bootmeth_cmd_order_glob(struct unit_test_state *uts)
108{
Simon Glass90b643d2022-07-30 15:52:36 -0600109 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc7599442022-10-20 18:22:49 -0600110 return -EAGAIN;
Simon Glass90b643d2022-07-30 15:52:36 -0600111
Simon Glassc8d37212022-07-30 15:52:34 -0600112 ut_assertok(run_command("bootmeth order \"efi firmware0\"", 0));
113 ut_assert_console_end();
114 ut_assertok(run_command("bootmeth list", 0));
115 ut_assert_nextline("Order Seq Name Description");
116 ut_assert_nextlinen("---");
117 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
118 ut_assert_nextline(" glob 2 firmware0 VBE simple");
119 ut_assert_nextlinen("---");
120 ut_assert_nextline("(2 bootmeths)");
121 ut_assertnonnull(env_get("bootmeths"));
122 ut_asserteq_str("efi firmware0", env_get("bootmeths"));
123 ut_assert_console_end();
124
Simon Glassb255efc2022-04-24 23:31:24 -0600125 return 0;
126}
Simon Glassf0425022024-08-22 07:57:54 -0600127BOOTSTD_TEST(bootmeth_cmd_order_glob, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600128
Martyn Welch081f3af2024-10-09 14:15:41 +0100129/* Check 'bootmeth set' command */
130static int bootmeth_cmd_set(struct unit_test_state *uts)
131{
132 /* Check we can enable extlinux fallback */
133 console_record_reset_enable();
134 ut_assertok(run_command("bootmeth set extlinux fallback 1", 0));
135 ut_assert_console_end();
136
137 /* Check we can disable extlinux fallback */
138 console_record_reset_enable();
139 ut_assertok(run_command("bootmeth set extlinux fallback 0", 0));
140 ut_assert_console_end();
141
142 /* Check extlinux fallback unexpected value */
143 console_record_reset_enable();
144 ut_asserteq(1, run_command("bootmeth set extlinux fallback fred", 0));
145 ut_assert_nextline("Unexpected value 'fred'");
146 ut_assert_nextline("Failed (err=-22)");
147 ut_assert_console_end();
148
149 /* Check that we need to provide right number of parameters */
150 ut_asserteq(1, run_command("bootmeth set extlinux fallback", 0));
151 ut_assert_nextline("Required parameters not provided");
152 ut_assert_console_end();
153
154 /* Check that we need to provide a valid bootmethod */
155 ut_asserteq(1, run_command("bootmeth set fred fallback 0", 0));
156 ut_assert_nextline("Unknown bootmeth 'fred'");
157 ut_assert_nextline("Failed (err=-19)");
158 ut_assert_console_end();
159
160 /* Check that we need to provide a valid property */
161 ut_asserteq(1, run_command("bootmeth set extlinux fred 0", 0));
162 ut_assert_nextline("Invalid option");
163 ut_assert_nextline("Failed (err=-22)");
164 ut_assert_console_end();
165
166 /* Check that we need to provide a bootmeth that supports properties */
167 ut_asserteq(1, run_command("bootmeth set efi fallback 0", 0));
168 ut_assert_nextline("set_property not found");
169 ut_assert_nextline("Failed (err=-19)");
170 ut_assert_console_end();
171
172 return 0;
173}
174BOOTSTD_TEST(bootmeth_cmd_set, UTF_DM | UTF_SCAN_FDT);
175
Simon Glassb255efc2022-04-24 23:31:24 -0600176/* Check 'bootmeths' env var */
177static int bootmeth_env(struct unit_test_state *uts)
178{
179 struct bootstd_priv *std;
180
181 ut_assertok(bootstd_get_priv(&std));
182
183 /* Select just one bootmethod */
Simon Glassb71d7f72023-05-10 16:34:46 -0600184 ut_assertok(env_set("bootmeths", "extlinux"));
Simon Glassb255efc2022-04-24 23:31:24 -0600185 ut_asserteq(1, std->bootmeth_count);
186
187 /* Select an invalid bootmethod */
188 ut_asserteq(1, run_command("setenv bootmeths fred", 0));
189 ut_assert_nextline("Unknown bootmeth 'fred'");
190 ut_assert_nextlinen("## Error inserting");
191 ut_assert_console_end();
192
Simon Glassb71d7f72023-05-10 16:34:46 -0600193 ut_assertok(env_set("bootmeths", "efi extlinux"));
Simon Glassb255efc2022-04-24 23:31:24 -0600194 ut_asserteq(2, std->bootmeth_count);
195 ut_assert_console_end();
196
197 return 0;
198}
Simon Glassf0425022024-08-22 07:57:54 -0600199BOOTSTD_TEST(bootmeth_env, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassf6d71a82022-07-30 15:52:19 -0600200
201/* Check the get_state_desc() method */
202static int bootmeth_state(struct unit_test_state *uts)
203{
204 struct udevice *dev;
205 char buf[50];
206
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200207 ut_assertok(uclass_first_device_err(UCLASS_BOOTMETH, &dev));
Simon Glassf6d71a82022-07-30 15:52:19 -0600208 ut_assertnonnull(dev);
209
210 ut_assertok(bootmeth_get_state_desc(dev, buf, sizeof(buf)));
211 ut_asserteq_str("OK", buf);
212
213 return 0;
214}
Simon Glass1a92f832024-08-22 07:57:48 -0600215BOOTSTD_TEST(bootmeth_state, UTF_DM | UTF_SCAN_FDT);