blob: 52b83f149cb97c34887eae995950707c8c4d9ac8 [file] [log] [blame]
Simon Glass07a88862020-11-05 10:33:38 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for bootm routines
4 *
5 * Copyright 2020 Google LLC
6 */
7
Simon Glass07a88862020-11-05 10:33:38 -07008#include <bootm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glass07a88862020-11-05 10:33:38 -070010#include <test/suites.h>
11#include <test/test.h>
12#include <test/ut.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
16#define BOOTM_TEST(_name, _flags) UNIT_TEST(_name, _flags, bootm_test)
17
Simon Glassb4e1b6d2020-11-05 10:33:45 -070018enum {
19 BUF_SIZE = 1024,
20};
21
Simon Glass07a88862020-11-05 10:33:38 -070022#define CONSOLE_STR "console=/dev/ttyS0"
23
Simon Glassb4e1b6d2020-11-05 10:33:45 -070024/* Test cmdline processing where nothing happens */
25static int bootm_test_nop(struct unit_test_state *uts)
Simon Glass07a88862020-11-05 10:33:38 -070026{
Simon Glassb4e1b6d2020-11-05 10:33:45 -070027 char buf[BUF_SIZE];
28
Andrew Goodbodyb4af83a2024-11-01 13:02:54 +000029 /* This tests relies on GD_FLG_SILENT not being set */
30 gd->flags &= ~GD_FLG_SILENT;
31
Simon Glassb4e1b6d2020-11-05 10:33:45 -070032 *buf = '\0';
Andrew Goodbodycbe01ae2024-11-01 13:02:53 +000033 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_ALL));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070034 ut_asserteq_str("", buf);
35
36 strcpy(buf, "test");
Andrew Goodbodycbe01ae2024-11-01 13:02:53 +000037 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_ALL));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070038 ut_asserteq_str("test", buf);
39
40 return 0;
41}
42BOOTM_TEST(bootm_test_nop, 0);
43
44/* Test cmdline processing when out of space */
45static int bootm_test_nospace(struct unit_test_state *uts)
46{
47 char buf[BUF_SIZE];
48
Andrew Goodbodyb4af83a2024-11-01 13:02:54 +000049 /* This tests relies on GD_FLG_SILENT not being set */
50 gd->flags &= ~GD_FLG_SILENT;
51
Simon Glassb4e1b6d2020-11-05 10:33:45 -070052 /* Zero buffer size */
53 *buf = '\0';
Andrew Goodbodycbe01ae2024-11-01 13:02:53 +000054 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, 0, BOOTM_CL_ALL));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070055
56 /* Buffer string not terminated */
57 memset(buf, 'a', BUF_SIZE);
Andrew Goodbodycbe01ae2024-11-01 13:02:53 +000058 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_ALL));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070059
60 /* Not enough space to copy string */
61 memset(buf, '\0', BUF_SIZE);
62 memset(buf, 'a', BUF_SIZE / 2);
Andrew Goodbodycbe01ae2024-11-01 13:02:53 +000063 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_ALL));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070064
65 /* Just enough space */
66 memset(buf, '\0', BUF_SIZE);
67 memset(buf, 'a', BUF_SIZE / 2 - 1);
Andrew Goodbodycbe01ae2024-11-01 13:02:53 +000068 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_ALL));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070069
70 return 0;
71}
72BOOTM_TEST(bootm_test_nospace, 0);
73
74/* Test silent processing */
75static int bootm_test_silent(struct unit_test_state *uts)
76{
77 char buf[BUF_SIZE];
78
Andrew Goodbodyb4af83a2024-11-01 13:02:54 +000079 /* This tests relies on GD_FLG_SILENT not being set */
80 gd->flags &= ~GD_FLG_SILENT;
81
Simon Glass07a88862020-11-05 10:33:38 -070082 /* 'silent_linux' not set should do nothing */
83 env_set("silent_linux", NULL);
Simon Glassb4e1b6d2020-11-05 10:33:45 -070084 strcpy(buf, CONSOLE_STR);
85 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
86 ut_asserteq_str(CONSOLE_STR, buf);
Simon Glass07a88862020-11-05 10:33:38 -070087
88 ut_assertok(env_set("silent_linux", "no"));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070089 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
90 ut_asserteq_str(CONSOLE_STR, buf);
Simon Glass07a88862020-11-05 10:33:38 -070091
92 ut_assertok(env_set("silent_linux", "yes"));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070093 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
Sean Anderson15bdcd42022-05-19 18:26:05 -040094 ut_asserteq_str("console=ttynull", buf);
Simon Glass07a88862020-11-05 10:33:38 -070095
96 /* Empty buffer should still add the string */
Simon Glassb4e1b6d2020-11-05 10:33:45 -070097 *buf = '\0';
98 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
Sean Anderson15bdcd42022-05-19 18:26:05 -040099 ut_asserteq_str("console=ttynull", buf);
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700100
101 /* Check nothing happens when do_silent is false */
102 *buf = '\0';
103 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, 0));
104 ut_asserteq_str("", buf);
105
106 /* Not enough space */
107 *buf = '\0';
Sean Anderson15bdcd42022-05-19 18:26:05 -0400108 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, 15, BOOTM_CL_SILENT));
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700109
110 /* Just enough space */
111 *buf = '\0';
Sean Anderson15bdcd42022-05-19 18:26:05 -0400112 ut_assertok(bootm_process_cmdline(buf, 16, BOOTM_CL_SILENT));
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700113
114 /* add at end */
115 strcpy(buf, "something");
116 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
Sean Anderson15bdcd42022-05-19 18:26:05 -0400117 ut_asserteq_str("something console=ttynull", buf);
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700118
119 /* change at start */
120 strcpy(buf, CONSOLE_STR " something");
121 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
Sean Anderson15bdcd42022-05-19 18:26:05 -0400122 ut_asserteq_str("console=ttynull something", buf);
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700123
124 return 0;
125}
126BOOTM_TEST(bootm_test_silent, 0);
127
Simon Glass529e2082020-11-05 10:33:48 -0700128/* Test substitution processing */
129static int bootm_test_subst(struct unit_test_state *uts)
130{
131 char buf[BUF_SIZE];
132
133 /* try with an unset variable */
134 ut_assertok(env_set("var", NULL));
135 strcpy(buf, "some${var}thing");
136 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
137 ut_asserteq_str("something", buf);
138
139 /* Replace with shorter string */
140 ut_assertok(env_set("var", "bb"));
141 strcpy(buf, "some${var}thing");
142 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
143 ut_asserteq_str("somebbthing", buf);
144
145 /* Replace with same-length string */
146 ut_assertok(env_set("var", "abc"));
147 strcpy(buf, "some${var}thing");
148 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
149 ut_asserteq_str("someabcthing", buf);
150
151 /* Replace with longer string */
152 ut_assertok(env_set("var", "abcde"));
153 strcpy(buf, "some${var}thing");
154 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
155 ut_asserteq_str("someabcdething", buf);
156
157 /* Check it is case sensitive */
158 ut_assertok(env_set("VAR", NULL));
159 strcpy(buf, "some${VAR}thing");
160 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
161 ut_asserteq_str("something", buf);
162
163 /* Check too long - need 12 bytes for each string */
164 strcpy(buf, "some${var}thing");
165 ut_asserteq(-ENOSPC,
166 bootm_process_cmdline(buf, 12 * 2 - 1, BOOTM_CL_SUBST));
167
168 /* Check just enough space */
169 strcpy(buf, "some${var}thing");
170 ut_assertok(bootm_process_cmdline(buf, 16 * 2, BOOTM_CL_SUBST));
171 ut_asserteq_str("someabcdething", buf);
172
173 /*
174 * Check the substition string being too long. This results in a string
175 * of 12 (13 bytes). We need enough space for that plus the original
176 * "a${var}c" string of 9 bytes. So 12 + 9 = 21 bytes.
177 */
178 ut_assertok(env_set("var", "1234567890"));
179 strcpy(buf, "a${var}c");
180 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, 21, BOOTM_CL_SUBST));
181
182 strcpy(buf, "a${var}c");
183 ut_asserteq(0, bootm_process_cmdline(buf, 22, BOOTM_CL_SUBST));
184
185 /* Check multiple substitutions */
186 ut_assertok(env_set("var", "abc"));
187 strcpy(buf, "some${var}thing${bvar}else");
188 ut_asserteq(0, bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
189 ut_asserteq_str("someabcthingelse", buf);
190
191 /* Check multiple substitutions */
192 ut_assertok(env_set("bvar", "123"));
193 strcpy(buf, "some${var}thing${bvar}else");
194 ut_asserteq(0, bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
195 ut_asserteq_str("someabcthing123else", buf);
196
197 return 0;
198}
199BOOTM_TEST(bootm_test_subst, 0);
200
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700201/* Test silent processing in the bootargs variable */
202static int bootm_test_silent_var(struct unit_test_state *uts)
203{
Simon Glass07a88862020-11-05 10:33:38 -0700204 env_set("bootargs", NULL);
Simon Glass529e2082020-11-05 10:33:48 -0700205 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_SUBST));
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700206 ut_assertnull(env_get("bootargs"));
207
Simon Glass529e2082020-11-05 10:33:48 -0700208 ut_assertok(env_set("bootargs", "some${var}thing"));
209 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_SUBST));
210 ut_asserteq_str("something", env_get("bootargs"));
211
212 return 0;
213}
214BOOTM_TEST(bootm_test_silent_var, 0);
215
216/* Test substitution processing in the bootargs variable */
217static int bootm_test_subst_var(struct unit_test_state *uts)
218{
Simon Glass5e666ed2022-08-06 17:51:46 -0600219 ut_assertok(env_set("silent_linux", "yes"));
220 ut_assertok(env_set("bootargs", NULL));
Simon Glass63660dc2020-11-05 10:33:44 -0700221 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_SILENT));
Sean Anderson15bdcd42022-05-19 18:26:05 -0400222 ut_asserteq_str("console=ttynull", env_get("bootargs"));
Simon Glass07a88862020-11-05 10:33:38 -0700223
Simon Glass529e2082020-11-05 10:33:48 -0700224 ut_assertok(env_set("var", "abc"));
225 ut_assertok(env_set("bootargs", "some${var}thing"));
226 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_SILENT));
Sean Anderson15bdcd42022-05-19 18:26:05 -0400227 ut_asserteq_str("some${var}thing console=ttynull", env_get("bootargs"));
Simon Glass529e2082020-11-05 10:33:48 -0700228
Simon Glass07a88862020-11-05 10:33:38 -0700229 return 0;
230}
Simon Glass529e2082020-11-05 10:33:48 -0700231BOOTM_TEST(bootm_test_subst_var, 0);
232
233/* Test substitution and silent console processing in the bootargs variable */
234static int bootm_test_subst_both(struct unit_test_state *uts)
235{
236 ut_assertok(env_set("silent_linux", "yes"));
237 env_set("bootargs", NULL);
238 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_ALL));
Sean Anderson15bdcd42022-05-19 18:26:05 -0400239 ut_asserteq_str("console=ttynull", env_get("bootargs"));
Simon Glass529e2082020-11-05 10:33:48 -0700240
241 ut_assertok(env_set("bootargs", "some${var}thing " CONSOLE_STR));
242 ut_assertok(env_set("var", "1234567890"));
243 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_ALL));
Sean Anderson15bdcd42022-05-19 18:26:05 -0400244 ut_asserteq_str("some1234567890thing console=ttynull", env_get("bootargs"));
Simon Glass529e2082020-11-05 10:33:48 -0700245
246 return 0;
247}
248BOOTM_TEST(bootm_test_subst_both, 0);
Simon Glass07a88862020-11-05 10:33:38 -0700249
250int do_ut_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
251{
Simon Glassb50211f2021-03-07 17:35:10 -0700252 struct unit_test *tests = UNIT_TEST_SUITE_START(bootm_test);
253 const int n_ents = UNIT_TEST_SUITE_COUNT(bootm_test);
Simon Glass07a88862020-11-05 10:33:38 -0700254
255 return cmd_ut_category("bootm", "bootm_test_", tests, n_ents,
256 argc, argv);
257}