blob: 113b789ea7936babc6e8d2a449afcd01fa225a3c [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{
19 console_record_reset_enable();
20 ut_assertok(run_command("bootmeth list", 0));
21 ut_assert_nextline("Order Seq Name Description");
22 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060023 ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060024 ut_assert_nextline(" 1 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060025 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
26 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060027 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060028 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
29 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060030 ut_assert_console_end();
31
32 return 0;
33}
34BOOTSTD_TEST(bootmeth_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
35
36/* Check 'bootmeth order' command */
37static int bootmeth_cmd_order(struct unit_test_state *uts)
38{
39 /* Select just one bootmethod */
40 console_record_reset_enable();
Simon Glassb71d7f72023-05-10 16:34:46 -060041 ut_assertok(run_command("bootmeth order extlinux", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060042 ut_assert_console_end();
43 ut_assertnonnull(env_get("bootmeths"));
Simon Glassb71d7f72023-05-10 16:34:46 -060044 ut_asserteq_str("extlinux", env_get("bootmeths"));
Simon Glassb255efc2022-04-24 23:31:24 -060045
46 /* Only that one should be listed */
47 ut_assertok(run_command("bootmeth list", 0));
48 ut_assert_nextline("Order Seq Name Description");
49 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060050 ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060051 ut_assert_nextlinen("---");
52 ut_assert_nextline("(1 bootmeth)");
53 ut_assert_console_end();
54
55 /* Check the -a flag, efi should show as not in the order ("-") */
56 ut_assertok(run_command("bootmeth list -a", 0));
57 ut_assert_nextline("Order Seq Name Description");
58 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060059 ut_assert_nextline(" 0 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060060 ut_assert_nextline(" - 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060061 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
62 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060063 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060064 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
65 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060066 ut_assert_console_end();
67
68 /* Check the -a flag with the reverse order */
Simon Glassb71d7f72023-05-10 16:34:46 -060069 ut_assertok(run_command("bootmeth order \"efi extlinux\"", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060070 ut_assert_console_end();
71 ut_assertok(run_command("bootmeth list -a", 0));
72 ut_assert_nextline("Order Seq Name Description");
73 ut_assert_nextlinen("---");
Simon Glassb71d7f72023-05-10 16:34:46 -060074 ut_assert_nextline(" 1 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060075 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
Simon Glass90b643d2022-07-30 15:52:36 -060076 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
77 ut_assert_nextline(" glob 2 firmware0 VBE simple");
Simon Glassb255efc2022-04-24 23:31:24 -060078 ut_assert_nextlinen("---");
Simon Glass90b643d2022-07-30 15:52:36 -060079 ut_assert_nextline(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
80 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060081 ut_assert_console_end();
82
83 /* Now reset the order to empty, which should show all of them again */
84 ut_assertok(run_command("bootmeth order", 0));
85 ut_assert_console_end();
86 ut_assertnull(env_get("bootmeths"));
87 ut_assertok(run_command("bootmeth list", 0));
Simon Glass90b643d2022-07-30 15:52:36 -060088 ut_assert_skip_to_line(IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) ?
89 "(3 bootmeths)" : "(2 bootmeths)");
Simon Glassb255efc2022-04-24 23:31:24 -060090
91 /* Try reverse order */
Simon Glassb71d7f72023-05-10 16:34:46 -060092 ut_assertok(run_command("bootmeth order \"efi extlinux\"", 0));
Simon Glassb255efc2022-04-24 23:31:24 -060093 ut_assert_console_end();
94 ut_assertok(run_command("bootmeth list", 0));
95 ut_assert_nextline("Order Seq Name Description");
96 ut_assert_nextlinen("---");
97 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
Simon Glassb71d7f72023-05-10 16:34:46 -060098 ut_assert_nextline(" 1 0 extlinux Extlinux boot from a block device");
Simon Glassb255efc2022-04-24 23:31:24 -060099 ut_assert_nextlinen("---");
100 ut_assert_nextline("(2 bootmeths)");
101 ut_assertnonnull(env_get("bootmeths"));
Simon Glassb71d7f72023-05-10 16:34:46 -0600102 ut_asserteq_str("efi extlinux", env_get("bootmeths"));
Simon Glassb255efc2022-04-24 23:31:24 -0600103 ut_assert_console_end();
104
Simon Glassc7599442022-10-20 18:22:49 -0600105 return 0;
106}
107BOOTSTD_TEST(bootmeth_cmd_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
108
109/* Check 'bootmeth order' command with global bootmeths */
110static int bootmeth_cmd_order_glob(struct unit_test_state *uts)
111{
Simon Glass90b643d2022-07-30 15:52:36 -0600112 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
Simon Glassc7599442022-10-20 18:22:49 -0600113 return -EAGAIN;
Simon Glass90b643d2022-07-30 15:52:36 -0600114
Simon Glassc7599442022-10-20 18:22:49 -0600115 console_record_reset_enable();
Simon Glassc8d37212022-07-30 15:52:34 -0600116 ut_assertok(run_command("bootmeth order \"efi firmware0\"", 0));
117 ut_assert_console_end();
118 ut_assertok(run_command("bootmeth list", 0));
119 ut_assert_nextline("Order Seq Name Description");
120 ut_assert_nextlinen("---");
121 ut_assert_nextline(" 0 1 efi EFI boot from an .efi file");
122 ut_assert_nextline(" glob 2 firmware0 VBE simple");
123 ut_assert_nextlinen("---");
124 ut_assert_nextline("(2 bootmeths)");
125 ut_assertnonnull(env_get("bootmeths"));
126 ut_asserteq_str("efi firmware0", env_get("bootmeths"));
127 ut_assert_console_end();
128
Simon Glassb255efc2022-04-24 23:31:24 -0600129 return 0;
130}
Simon Glassc7599442022-10-20 18:22:49 -0600131BOOTSTD_TEST(bootmeth_cmd_order_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassb255efc2022-04-24 23:31:24 -0600132
133/* Check 'bootmeths' env var */
134static int bootmeth_env(struct unit_test_state *uts)
135{
136 struct bootstd_priv *std;
137
138 ut_assertok(bootstd_get_priv(&std));
139
140 /* Select just one bootmethod */
141 console_record_reset_enable();
Simon Glassb71d7f72023-05-10 16:34:46 -0600142 ut_assertok(env_set("bootmeths", "extlinux"));
Simon Glassb255efc2022-04-24 23:31:24 -0600143 ut_asserteq(1, std->bootmeth_count);
144
145 /* Select an invalid bootmethod */
146 ut_asserteq(1, run_command("setenv bootmeths fred", 0));
147 ut_assert_nextline("Unknown bootmeth 'fred'");
148 ut_assert_nextlinen("## Error inserting");
149 ut_assert_console_end();
150
Simon Glassb71d7f72023-05-10 16:34:46 -0600151 ut_assertok(env_set("bootmeths", "efi extlinux"));
Simon Glassb255efc2022-04-24 23:31:24 -0600152 ut_asserteq(2, std->bootmeth_count);
153 ut_assert_console_end();
154
155 return 0;
156}
157BOOTSTD_TEST(bootmeth_env, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
Simon Glassf6d71a82022-07-30 15:52:19 -0600158
159/* Check the get_state_desc() method */
160static int bootmeth_state(struct unit_test_state *uts)
161{
162 struct udevice *dev;
163 char buf[50];
164
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200165 ut_assertok(uclass_first_device_err(UCLASS_BOOTMETH, &dev));
Simon Glassf6d71a82022-07-30 15:52:19 -0600166 ut_assertnonnull(dev);
167
168 ut_assertok(bootmeth_get_state_desc(dev, buf, sizeof(buf)));
169 ut_asserteq_str("OK", buf);
170
171 return 0;
172}
173BOOTSTD_TEST(bootmeth_state, UT_TESTF_DM | UT_TESTF_SCAN_FDT);