1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2012, The Chromium Authors
4  */
5 
6 #include <command.h>
7 #include <efi_api.h>
8 #include <display_options.h>
9 #include <log.h>
10 #include <mapmem.h>
11 #include <version_string.h>
12 #include <stdio.h>
13 #include <vsprintf.h>
14 #include <test/common.h>
15 #include <test/test.h>
16 #include <test/ut.h>
17 
18 #define BUF_SIZE	0x100
19 
20 #define FAKE_BUILD_TAG	"jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
21 			"and a lot more text to come"
22 
23 #if CONFIG_IS_ENABLED(LIB_UUID)
24 /* Test printing GUIDs */
print_guid(struct unit_test_state * uts)25 static int print_guid(struct unit_test_state *uts)
26 {
27 	unsigned char guid[16] = {
28 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
29 	};
30 	unsigned char guid_esp[16] = {
31 		0x28, 0x73, 0x2a, 0xc1, 0x1f, 0xf8, 0xd2, 0x11,
32 		0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B
33 	};
34 	char str[40];
35 	int ret;
36 
37 	sprintf(str, "%pUb", guid);
38 	ut_asserteq_str("01020304-0506-0708-090a-0b0c0d0e0f10", str);
39 	sprintf(str, "%pUB", guid);
40 	ut_asserteq_str("01020304-0506-0708-090A-0B0C0D0E0F10", str);
41 	sprintf(str, "%pUl", guid);
42 	ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str);
43 	sprintf(str, "%pUs", guid);
44 	ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str);
45 	sprintf(str, "%pUL", guid);
46 	ut_asserteq_str("04030201-0605-0807-090A-0B0C0D0E0F10", str);
47 	sprintf(str, "%pUs", guid_esp);
48 	if (IS_ENABLED(CONFIG_EFI_PARTITION))
49 		ut_asserteq_str("EFI System Partition", str);
50 	else
51 		ut_asserteq_str("c12a7328-f81f-11d2-ba4b-00a0c93ec93b", str);
52 	ret = snprintf(str, 4, "%pUL", guid);
53 	ut_asserteq(0, str[3]);
54 	ut_asserteq(36, ret);
55 
56 	return 0;
57 }
58 COMMON_TEST(print_guid, 0);
59 #endif
60 
61 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
62 /* Test efi_loader specific printing */
print_efi_ut(struct unit_test_state * uts)63 static int print_efi_ut(struct unit_test_state *uts)
64 {
65 	char str[10];
66 	u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
67 	       sizeof(struct efi_device_path)];
68 	u8 *pos = buf;
69 	struct efi_device_path *dp_end;
70 	struct efi_device_path_sd_mmc_path *dp_sd =
71 			(struct efi_device_path_sd_mmc_path *)pos;
72 
73 	/* Create a device path for an SD card */
74 	dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
75 	dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
76 	dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
77 	dp_sd->slot_number = 3;
78 	pos += sizeof(struct efi_device_path_sd_mmc_path);
79 	/* Append end node */
80 	dp_end = (struct efi_device_path *)pos;
81 	dp_end->type = DEVICE_PATH_TYPE_END;
82 	dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
83 	dp_end->length = sizeof(struct efi_device_path);
84 
85 	snprintf(str, sizeof(str), "_%pD_", buf);
86 	ut_assertok(strcmp("_/SD(3)_", str));
87 
88 	/* NULL device path */
89 	snprintf(str, sizeof(str), "_%pD_", NULL);
90 	ut_assertok(strcmp("_<NULL>_", str));
91 
92 	return 0;
93 }
94 COMMON_TEST(print_efi_ut, 0);
95 #endif
96 
print_printf(struct unit_test_state * uts)97 static int print_printf(struct unit_test_state *uts)
98 {
99 	char big_str[400];
100 	int big_str_len;
101 	char str[10], *s;
102 	int len;
103 
104 	snprintf(str, sizeof(str), "testing");
105 	ut_assertok(strcmp("testing", str));
106 
107 	snprintf(str, sizeof(str), "testing but too long");
108 	ut_assertok(strcmp("testing b", str));
109 
110 	snprintf(str, 1, "testing none");
111 	ut_assertok(strcmp("", str));
112 
113 	*str = 'x';
114 	snprintf(str, 0, "testing none");
115 	ut_asserteq('x', *str);
116 
117 	if (CONFIG_IS_ENABLED(EFI_LOADER) || IS_ENABLED(CONFIG_EFI_APP)) {
118 		sprintf(big_str, "_%ls_", u"foo");
119 		ut_assertok(strcmp("_foo_", big_str));
120 	}
121 
122 	/* Test the banner function */
123 	s = display_options_get_banner(true, str, sizeof(str));
124 	ut_asserteq_ptr(str, s);
125 	ut_assertok(strcmp("\n\nU-Boo\n\n", s));
126 
127 	/* Assert that we do not overwrite memory before the buffer */
128 	str[0] = '`';
129 	s = display_options_get_banner(true, str + 1, 1);
130 	ut_asserteq_ptr(str + 1, s);
131 	ut_assertok(strcmp("`", str));
132 
133 	str[0] = '~';
134 	s = display_options_get_banner(true, str + 1, 2);
135 	ut_asserteq_ptr(str + 1, s);
136 	ut_assertok(strcmp("~\n", str));
137 
138 	/* The last two characters are set to \n\n for all buffer sizes > 2 */
139 	s = display_options_get_banner(false, str, sizeof(str));
140 	ut_asserteq_ptr(str, s);
141 	ut_assertok(strcmp("U-Boot \n\n", s));
142 
143 	/* Give it enough space for some of the version */
144 	big_str_len = strlen(version_string) - 5;
145 	s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
146 					    big_str_len);
147 	ut_asserteq_ptr(big_str, s);
148 	ut_assertok(strncmp(version_string, s, big_str_len - 3));
149 	ut_assertok(strcmp("\n\n", s + big_str_len - 3));
150 
151 	/* Give it enough space for the version and some of the build tag */
152 	big_str_len = strlen(version_string) + 9 + 20;
153 	s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
154 					    big_str_len);
155 	ut_asserteq_ptr(big_str, s);
156 	len = strlen(version_string);
157 	ut_assertok(strncmp(version_string, s, len));
158 	ut_assertok(strncmp(", Build: ", s + len, 9));
159 	ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
160 	ut_assertok(strcmp("\n\n", s + big_str_len - 3));
161 
162 	return 0;
163 }
164 COMMON_TEST(print_printf, 0);
165 
print_display_buffer(struct unit_test_state * uts)166 static int print_display_buffer(struct unit_test_state *uts)
167 {
168 	u8 *buf;
169 	int i;
170 
171 	/* This test requires writable memory at zero */
172 	if (IS_ENABLED(CONFIG_X86))
173 		return -EAGAIN;
174 
175 	buf = map_sysmem(0, BUF_SIZE);
176 	memset(buf, '\0', BUF_SIZE);
177 	for (i = 0; i < 0x11; i++)
178 		buf[i] = i * 0x11;
179 
180 	/* bytes */
181 	print_buffer(0, buf, 1, 0x12, 0);
182 	ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff  ..\"3DUfw........");
183 	ut_assert_nextline("00000010: 10 00                                            ..");
184 	ut_assert_console_end();
185 
186 	/* line length */
187 	print_buffer(0, buf, 1, 0x12, 8);
188 	ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77  ..\"3DUfw");
189 	ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff  ........");
190 	ut_assert_nextline("00000010: 10 00                    ..");
191 	ut_assert_console_end();
192 
193 	/* long line */
194 	buf[0x41] = 0x41;
195 	print_buffer(0, buf, 1, 0x42, 0x40);
196 	ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ..\"3DUfw........................................................");
197 	ut_assert_nextline("00000040: 00 41                                                                                                                                                                                            .A");
198 	ut_assert_console_end();
199 
200 	/* address */
201 	print_buffer(0x12345678, buf, 1, 0x12, 0);
202 	ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff  ..\"3DUfw........");
203 	ut_assert_nextline("12345688: 10 00                                            ..");
204 	ut_assert_console_end();
205 
206 	/* 16-bit */
207 	print_buffer(0, buf, 2, 9, 0);
208 	ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee  ..\"3DUfw........");
209 	ut_assert_nextline("00000010: 0010                                     ..");
210 	ut_assert_console_end();
211 
212 	/* 32-bit */
213 	print_buffer(0, buf, 4, 5, 0);
214 	ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc  ..\"3DUfw........");
215 	ut_assert_nextline("00000010: 00000010                             ....");
216 	ut_assert_console_end();
217 
218 	/* 64-bit */
219 	print_buffer(0, buf, 8, 3, 0);
220 	ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988  ..\"3DUfw........");
221 	ut_assert_nextline("00000010: 0000000000000010                   ........");
222 	ut_assert_console_end();
223 
224 	/* ASCII */
225 	buf[1] = 31;
226 	buf[2] = 32;
227 	buf[3] = 33;
228 	for (i = 0; i < 4; i++)
229 		buf[4 + i] = 126 + i;
230 	buf[8] = 255;
231 	print_buffer(0, buf, 1, 10, 0);
232 	ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99                    .. !~.....");
233 	ut_assert_console_end();
234 
235 	unmap_sysmem(buf);
236 
237 	return 0;
238 }
239 COMMON_TEST(print_display_buffer, UTF_CONSOLE);
240 
print_hexdump_line(struct unit_test_state * uts)241 static int print_hexdump_line(struct unit_test_state *uts)
242 {
243 	u8 *linebuf;
244 	u8 *buf;
245 	int i;
246 
247 	buf = map_sysmem(0, BUF_SIZE);
248 	memset(buf, '\0', BUF_SIZE);
249 	for (i = 0; i < 0x11; i++)
250 		buf[i] = i * 0x11;
251 
252 	/* Check buffer size calculations */
253 	linebuf = map_sysmem(0x400, BUF_SIZE);
254 	memset(linebuf, '\xff', BUF_SIZE);
255 	ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75));
256 	ut_asserteq(0xff, linebuf[0]);
257 	ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76));
258 	ut_asserteq('\0', linebuf[75]);
259 	ut_asserteq(0xff, linebuf[76]);
260 
261 	unmap_sysmem(buf);
262 
263 	return 0;
264 }
265 COMMON_TEST(print_hexdump_line, UTF_CONSOLE);
266 
print_do_hex_dump(struct unit_test_state * uts)267 static int print_do_hex_dump(struct unit_test_state *uts)
268 {
269 	u8 *buf;
270 	int i;
271 
272 	/* This test requires writable memory at zero */
273 	if (IS_ENABLED(CONFIG_X86))
274 		return -EAGAIN;
275 
276 	buf = map_sysmem(0, BUF_SIZE);
277 	memset(buf, '\0', BUF_SIZE);
278 	for (i = 0; i < 0x11; i++)
279 		buf[i] = i * 0x11;
280 
281 	/* bytes */
282 	print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12);
283 	ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff  ..\"3DUfw........",
284 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
285 	ut_assert_nextline("%0*lx: 10 00                                            ..",
286 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
287 	ut_assert_console_end();
288 
289 	/* line length */
290 	print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true);
291 	ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77  ..\"3DUfw",
292 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
293 	ut_assert_nextline("%0*lx: 88 99 aa bb cc dd ee ff  ........",
294 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x8UL);
295 	ut_assert_nextline("%0*lx: 10 00                    ..",
296 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
297 	ut_assert_console_end();
298 	unmap_sysmem(buf);
299 
300 	/* long line */
301 	buf[0x41] = 0x41;
302 	print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true);
303 	ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ..\"3DUfw........................................................",
304 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
305 	ut_assert_nextline("%0*lx: 00 41                                                                                                                                                                                            .A",
306 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x40UL);
307 	ut_assert_console_end();
308 
309 	/* 16-bit */
310 	print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
311 	ut_assert_nextline("%0*lx: 1100 3322 5544 7766 9988 bbaa ddcc ffee  ..\"3DUfw........",
312 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
313 	ut_assert_nextline("%0*lx: 0010                                     ..",
314 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
315 	ut_assert_console_end();
316 	unmap_sysmem(buf);
317 
318 	/* 32-bit */
319 	print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
320 	ut_assert_nextline("%0*lx: 33221100 77665544 bbaa9988 ffeeddcc  ..\"3DUfw........",
321 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
322 	ut_assert_nextline("%0*lx: 00000010                             ....",
323 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
324 	ut_assert_console_end();
325 	unmap_sysmem(buf);
326 
327 	/* 64-bit */
328 	print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true);
329 	ut_assert_nextline("%0*lx: 7766554433221100 ffeeddccbbaa9988  ..\"3DUfw........",
330 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
331 	ut_assert_nextline("%0*lx: 0000000000000010                   ........",
332 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
333 	ut_assert_console_end();
334 	unmap_sysmem(buf);
335 
336 	/* ASCII */
337 	buf[1] = 31;
338 	buf[2] = 32;
339 	buf[3] = 33;
340 	for (i = 0; i < 4; i++)
341 		buf[4 + i] = 126 + i;
342 	buf[8] = 255;
343 	print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
344 	ut_assert_nextline("%0*lx: 00 1f 20 21 7e 7f 80 81 ff 99                    .. !~.....",
345 			   IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
346 	ut_assert_console_end();
347 	unmap_sysmem(buf);
348 
349 	return 0;
350 }
351 COMMON_TEST(print_do_hex_dump, UTF_CONSOLE);
352 
snprint(struct unit_test_state * uts)353 static int snprint(struct unit_test_state *uts)
354 {
355 	char buf[10] = "xxxxxxxxx";
356 	int ret;
357 
358 	ret = snprintf(buf, 5, "%d", 12345678);
359 	ut_asserteq_str("1234", buf);
360 	ut_asserteq(8, ret);
361 	ret = snprintf(buf, 5, "0x%x", 0x1234);
362 	ut_asserteq_str("0x12", buf);
363 	ut_asserteq(6, ret);
364 	ret = snprintf(buf, 5, "0x%08x", 0x1234);
365 	ut_asserteq_str("0x00", buf);
366 	ut_asserteq(10, ret);
367 	ret = snprintf(buf, 3, "%s", "abc");
368 	ut_asserteq_str("ab", buf);
369 	ut_asserteq(3, ret);
370 	ret = snprintf(buf, 4, "%s:%s", "abc", "def");
371 	ut_asserteq(0, buf[3]);
372 	ut_asserteq(7, ret);
373 	ret = snprintf(buf, 4, "%s:%d", "abc", 9999);
374 	ut_asserteq(8, ret);
375 	return 0;
376 }
377 COMMON_TEST(snprint, 0);
378