blob: 518d99c4a27bbbc3a6bbc702b442f1c775f91633 [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/suites.h>
13#include <test/ut.h>
14#include "bootstd_common.h"
15
16/* Check 'bootmeth list' command */
17static int bootmeth_cmd_list(struct unit_test_state *uts)
18{
Simon Glassb255efc2022-04-24 23:31:24 -060019 ut_assertok(run_command("bootmeth list", 0));
20 ut_assert_nextline("Order Seq Name Description");
21 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060022 ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060023 ut_assert_nextline(" 1 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060024 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
25 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060026 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060027 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
28 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060029 ut_assert_console_end();
30
31 return 0;
32}
Simon Glassf0425022024-08-22 07:57:54 -060033BOOTSTD_TEST(bootmeth_cmd_list, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -060034
35/* Check 'bootmeth order' command */
36static int bootmeth_cmd_order(struct unit_test_state *uts)
37{
38 /* Select just one bootmethod */
Simon Glassb71d7f72023-05-10 16:34:46 -060039 ut_assertok(run_command("bootmeth order extlinux", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060040 ut_assert_console_end();
41 ut_assertnonnull(env_get("bootmeths"));
Simon Glassb71d7f72023-05-10 16:34:46 -060042 ut_asserteq_str("extlinux", env_get("bootmeths"));
Simon Glassb255efc2022-04-24 23:31:24 -060043
44 /* Only that one should be listed */
45 ut_assertok(run_command("bootmeth list", 0));
46 ut_assert_nextline("Order Seq Name Description");
47 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060048 ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060049 ut_assert_nextlinen("---");
50 ut_assert_nextline("(1 bootmeth)");
51 ut_assert_console_end();
52
53 /* Check the -a flag, efi should show as not in the order ("-") */
54 ut_assertok(run_command("bootmeth list -a", 0));
55 ut_assert_nextline("Order Seq Name Description");
56 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060057 ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060058 ut_assert_nextline(" - 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060059 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
60 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060061 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060062 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
63 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060064 ut_assert_console_end();
65
66 /* Check the -a flag with the reverse order */
Simon Glassb71d7f72023-05-10 16:34:46 -060067 ut_assertok(run_command("bootmeth order \"efi extlinux\"", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060068 ut_assert_console_end();
69 ut_assertok(run_command("bootmeth list -a", 0));
70 ut_assert_nextline("Order Seq Name Description");
71 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060072 ut_assert_nextline(" 1 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060073 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060074 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
75 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060076 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060077 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
78 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060079 ut_assert_console_end();
80
81 /* Now reset the order to empty, which should show all of them again */
82 ut_assertok(run_command("bootmeth order", 0));
83 ut_assert_console_end();
84 ut_assertnull(env_get("bootmeths"));
85 ut_assertok(run_command("bootmeth list", 0));
Simon Glass90b643d2022-07-30 15:52:36 -060086 ut_assert_skip_to_line(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
87 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060088
89 /* Try reverse order */
Simon Glassb71d7f72023-05-10 16:34:46 -060090 ut_assertok(run_command("bootmeth order \"efi extlinux\"", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060091 ut_assert_console_end();
92 ut_assertok(run_command("bootmeth list", 0));
93 ut_assert_nextline("Order Seq Name Description");
94 ut_assert_nextlinen("---");
95 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
Simon Glassb71d7f72023-05-10 16:34:46 -060096 ut_assert_nextline(" 1 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060097 ut_assert_nextlinen("---");
98 ut_assert_nextline("(2 bootmeths)");
99 ut_assertnonnull(env_get("bootmeths"));
Simon Glassb71d7f72023-05-10 16:34:46 -0600100 ut_asserteq_str("efi extlinux", env_get("bootmeths"));
Simon Glassb255efc2022-04-24 23:31:24 -0600101 ut_assert_console_end();
102
Simon Glassc7599442022-10-20 18:22:49 -0600103 return 0;
104}
Simon Glassf0425022024-08-22 07:57:54 -0600105BOOTSTD_TEST(bootmeth_cmd_order, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassc7599442022-10-20 18:22:49 -0600106
107/* Check 'bootmeth order' command with global bootmeths */
108static int bootmeth_cmd_order_glob(struct unit_test_state *uts)
109{
Simon Glass90b643d2022-07-30 15:52:36 -0600110 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc7599442022-10-20 18:22:49 -0600111 return -EAGAIN;
Simon Glass90b643d2022-07-30 15:52:36 -0600112
Simon Glassc8d37212022-07-30 15:52:34 -0600113 ut_assertok(run_command("bootmeth order \"efi firmware0\"", 0));
114 ut_assert_console_end();
115 ut_assertok(run_command("bootmeth list", 0));
116 ut_assert_nextline("Order Seq Name Description");
117 ut_assert_nextlinen("---");
118 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
119 ut_assert_nextline(" glob 2 firmware0 VBE simple");
120 ut_assert_nextlinen("---");
121 ut_assert_nextline("(2 bootmeths)");
122 ut_assertnonnull(env_get("bootmeths"));
123 ut_asserteq_str("efi firmware0", env_get("bootmeths"));
124 ut_assert_console_end();
125
Simon Glassb255efc2022-04-24 23:31:24 -0600126 return 0;
127}
Simon Glassf0425022024-08-22 07:57:54 -0600128BOOTSTD_TEST(bootmeth_cmd_order_glob, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassb255efc2022-04-24 23:31:24 -0600129
130/* Check 'bootmeths' env var */
131static int bootmeth_env(struct unit_test_state *uts)
132{
133 struct bootstd_priv *std;
134
135 ut_assertok(bootstd_get_priv(&std));
136
137 /* Select just one bootmethod */
Simon Glassb71d7f72023-05-10 16:34:46 -0600138 ut_assertok(env_set("bootmeths", "extlinux"));
Simon Glassb255efc2022-04-24 23:31:24 -0600139 ut_asserteq(1, std->bootmeth_count);
140
141 /* Select an invalid bootmethod */
142 ut_asserteq(1, run_command("setenv bootmeths fred", 0));
143 ut_assert_nextline("Unknown bootmeth 'fred'");
144 ut_assert_nextlinen("## Error inserting");
145 ut_assert_console_end();
146
Simon Glassb71d7f72023-05-10 16:34:46 -0600147 ut_assertok(env_set("bootmeths", "efi extlinux"));
Simon Glassb255efc2022-04-24 23:31:24 -0600148 ut_asserteq(2, std->bootmeth_count);
149 ut_assert_console_end();
150
151 return 0;
152}
Simon Glassf0425022024-08-22 07:57:54 -0600153BOOTSTD_TEST(bootmeth_env, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassf6d71a82022-07-30 15:52:19 -0600154
155/* Check the get_state_desc() method */
156static int bootmeth_state(struct unit_test_state *uts)
157{
158 struct udevice *dev;
159 char buf[50];
160
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200161 ut_assertok(uclass_first_device_err(UCLASS_BOOTMETH, &dev));
Simon Glassf6d71a82022-07-30 15:52:19 -0600162 ut_assertnonnull(dev);
163
164 ut_assertok(bootmeth_get_state_desc(dev, buf, sizeof(buf)));
165 ut_asserteq_str("OK", buf);
166
167 return 0;
168}
Simon Glass1a92f832024-08-22 07:57:48 -0600169BOOTSTD_TEST(bootmeth_state, UTF_DM | UTF_SCAN_FDT);