blob: 92dc2b6e173864ea9fe5ce9c79306ba55ecfe38e [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
8#include <common.h>
9#include <bootm.h>
10#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
29 *buf = '\0';
30 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, true));
31 ut_asserteq_str("", buf);
32
33 strcpy(buf, "test");
34 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, true));
35 ut_asserteq_str("test", buf);
36
37 return 0;
38}
39BOOTM_TEST(bootm_test_nop, 0);
40
41/* Test cmdline processing when out of space */
42static int bootm_test_nospace(struct unit_test_state *uts)
43{
44 char buf[BUF_SIZE];
45
46 /* Zero buffer size */
47 *buf = '\0';
48 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, 0, true));
49
50 /* Buffer string not terminated */
51 memset(buf, 'a', BUF_SIZE);
52 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, BUF_SIZE, true));
53
54 /* Not enough space to copy string */
55 memset(buf, '\0', BUF_SIZE);
56 memset(buf, 'a', BUF_SIZE / 2);
57 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, BUF_SIZE, true));
58
59 /* Just enough space */
60 memset(buf, '\0', BUF_SIZE);
61 memset(buf, 'a', BUF_SIZE / 2 - 1);
62 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, true));
63
64 return 0;
65}
66BOOTM_TEST(bootm_test_nospace, 0);
67
68/* Test silent processing */
69static int bootm_test_silent(struct unit_test_state *uts)
70{
71 char buf[BUF_SIZE];
72
Simon Glass07a88862020-11-05 10:33:38 -070073 /* 'silent_linux' not set should do nothing */
74 env_set("silent_linux", NULL);
Simon Glassb4e1b6d2020-11-05 10:33:45 -070075 strcpy(buf, CONSOLE_STR);
76 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
77 ut_asserteq_str(CONSOLE_STR, buf);
Simon Glass07a88862020-11-05 10:33:38 -070078
79 ut_assertok(env_set("silent_linux", "no"));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070080 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
81 ut_asserteq_str(CONSOLE_STR, buf);
Simon Glass07a88862020-11-05 10:33:38 -070082
83 ut_assertok(env_set("silent_linux", "yes"));
Simon Glassb4e1b6d2020-11-05 10:33:45 -070084 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
85 ut_asserteq_str("console=", buf);
Simon Glass07a88862020-11-05 10:33:38 -070086
87 /* Empty buffer should still add the string */
Simon Glassb4e1b6d2020-11-05 10:33:45 -070088 *buf = '\0';
89 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
90 ut_asserteq_str("console=", buf);
91
92 /* Check nothing happens when do_silent is false */
93 *buf = '\0';
94 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, 0));
95 ut_asserteq_str("", buf);
96
97 /* Not enough space */
98 *buf = '\0';
99 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, 8, BOOTM_CL_SILENT));
100
101 /* Just enough space */
102 *buf = '\0';
103 ut_assertok(bootm_process_cmdline(buf, 9, BOOTM_CL_SILENT));
104
105 /* add at end */
106 strcpy(buf, "something");
107 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
108 ut_asserteq_str("something console=", buf);
109
110 /* change at start */
111 strcpy(buf, CONSOLE_STR " something");
112 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SILENT));
113 ut_asserteq_str("console= something", buf);
114
115 return 0;
116}
117BOOTM_TEST(bootm_test_silent, 0);
118
Simon Glass529e2082020-11-05 10:33:48 -0700119/* Test substitution processing */
120static int bootm_test_subst(struct unit_test_state *uts)
121{
122 char buf[BUF_SIZE];
123
124 /* try with an unset variable */
125 ut_assertok(env_set("var", NULL));
126 strcpy(buf, "some${var}thing");
127 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
128 ut_asserteq_str("something", buf);
129
130 /* Replace with shorter string */
131 ut_assertok(env_set("var", "bb"));
132 strcpy(buf, "some${var}thing");
133 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
134 ut_asserteq_str("somebbthing", buf);
135
136 /* Replace with same-length string */
137 ut_assertok(env_set("var", "abc"));
138 strcpy(buf, "some${var}thing");
139 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
140 ut_asserteq_str("someabcthing", buf);
141
142 /* Replace with longer string */
143 ut_assertok(env_set("var", "abcde"));
144 strcpy(buf, "some${var}thing");
145 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
146 ut_asserteq_str("someabcdething", buf);
147
148 /* Check it is case sensitive */
149 ut_assertok(env_set("VAR", NULL));
150 strcpy(buf, "some${VAR}thing");
151 ut_assertok(bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
152 ut_asserteq_str("something", buf);
153
154 /* Check too long - need 12 bytes for each string */
155 strcpy(buf, "some${var}thing");
156 ut_asserteq(-ENOSPC,
157 bootm_process_cmdline(buf, 12 * 2 - 1, BOOTM_CL_SUBST));
158
159 /* Check just enough space */
160 strcpy(buf, "some${var}thing");
161 ut_assertok(bootm_process_cmdline(buf, 16 * 2, BOOTM_CL_SUBST));
162 ut_asserteq_str("someabcdething", buf);
163
164 /*
165 * Check the substition string being too long. This results in a string
166 * of 12 (13 bytes). We need enough space for that plus the original
167 * "a${var}c" string of 9 bytes. So 12 + 9 = 21 bytes.
168 */
169 ut_assertok(env_set("var", "1234567890"));
170 strcpy(buf, "a${var}c");
171 ut_asserteq(-ENOSPC, bootm_process_cmdline(buf, 21, BOOTM_CL_SUBST));
172
173 strcpy(buf, "a${var}c");
174 ut_asserteq(0, bootm_process_cmdline(buf, 22, BOOTM_CL_SUBST));
175
176 /* Check multiple substitutions */
177 ut_assertok(env_set("var", "abc"));
178 strcpy(buf, "some${var}thing${bvar}else");
179 ut_asserteq(0, bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
180 ut_asserteq_str("someabcthingelse", buf);
181
182 /* Check multiple substitutions */
183 ut_assertok(env_set("bvar", "123"));
184 strcpy(buf, "some${var}thing${bvar}else");
185 ut_asserteq(0, bootm_process_cmdline(buf, BUF_SIZE, BOOTM_CL_SUBST));
186 ut_asserteq_str("someabcthing123else", buf);
187
188 return 0;
189}
190BOOTM_TEST(bootm_test_subst, 0);
191
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700192/* Test silent processing in the bootargs variable */
193static int bootm_test_silent_var(struct unit_test_state *uts)
194{
Simon Glass07a88862020-11-05 10:33:38 -0700195 env_set("bootargs", NULL);
Simon Glass529e2082020-11-05 10:33:48 -0700196 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_SUBST));
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700197 ut_assertnull(env_get("bootargs"));
198
Simon Glass529e2082020-11-05 10:33:48 -0700199 ut_assertok(env_set("bootargs", "some${var}thing"));
200 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_SUBST));
201 ut_asserteq_str("something", env_get("bootargs"));
202
203 return 0;
204}
205BOOTM_TEST(bootm_test_silent_var, 0);
206
207/* Test substitution processing in the bootargs variable */
208static int bootm_test_subst_var(struct unit_test_state *uts)
209{
210 env_set("bootargs", NULL);
Simon Glass63660dc2020-11-05 10:33:44 -0700211 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_SILENT));
Simon Glass07a88862020-11-05 10:33:38 -0700212 ut_asserteq_str("console=", env_get("bootargs"));
213
Simon Glass529e2082020-11-05 10:33:48 -0700214 ut_assertok(env_set("var", "abc"));
215 ut_assertok(env_set("bootargs", "some${var}thing"));
216 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_SILENT));
217 ut_asserteq_str("some${var}thing console=", env_get("bootargs"));
218
Simon Glass07a88862020-11-05 10:33:38 -0700219 return 0;
220}
Simon Glass529e2082020-11-05 10:33:48 -0700221BOOTM_TEST(bootm_test_subst_var, 0);
222
223/* Test substitution and silent console processing in the bootargs variable */
224static int bootm_test_subst_both(struct unit_test_state *uts)
225{
226 ut_assertok(env_set("silent_linux", "yes"));
227 env_set("bootargs", NULL);
228 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_ALL));
229 ut_asserteq_str("console=", env_get("bootargs"));
230
231 ut_assertok(env_set("bootargs", "some${var}thing " CONSOLE_STR));
232 ut_assertok(env_set("var", "1234567890"));
233 ut_assertok(bootm_process_cmdline_env(BOOTM_CL_ALL));
234 ut_asserteq_str("some1234567890thing console=", env_get("bootargs"));
235
236 return 0;
237}
238BOOTM_TEST(bootm_test_subst_both, 0);
Simon Glass07a88862020-11-05 10:33:38 -0700239
240int do_ut_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
241{
242 struct unit_test *tests = ll_entry_start(struct unit_test, bootm_test);
243 const int n_ents = ll_entry_count(struct unit_test, bootm_test);
244
245 return cmd_ut_category("bootm", "bootm_test_", tests, n_ents,
246 argc, argv);
247}