blob: 4fbb15b6d3c85deb922c654ff4ad2082705194b8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse5a9d272017-06-15 21:37:51 -06002/*
3 * Copyright (c) 2012, The Chromium Authors
Simon Glasse5a9d272017-06-15 21:37:51 -06004 */
5
Simon Glasse5a9d272017-06-15 21:37:51 -06006#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -06007#include <command.h>
Heinrich Schuchardt256060e2018-01-10 18:06:08 +01008#include <efi_api.h>
Simon Glasse5a9d272017-06-15 21:37:51 -06009#include <display_options.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glassc614ddf2021-05-08 06:59:59 -060011#include <mapmem.h>
Pali Rohárbdfb6d72021-08-02 15:18:31 +020012#include <version_string.h>
Simon Glass3bfb0f72021-10-14 12:48:06 -060013#include <vsprintf.h>
Simon Glassfbb99dc2021-05-08 06:59:58 -060014#include <test/suites.h>
15#include <test/test.h>
16#include <test/ut.h>
Simon Glasse5a9d272017-06-15 21:37:51 -060017
Simon Glassc614ddf2021-05-08 06:59:59 -060018#define BUF_SIZE 0x100
19
Simon Glasse5a9d272017-06-15 21:37:51 -060020#define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
21 "and a lot more text to come"
22
Simon Glassfbb99dc2021-05-08 06:59:58 -060023/* Declare a new print test */
24#define PRINT_TEST(_name, _flags) UNIT_TEST(_name, _flags, print_test)
25
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020026#if CONFIG_IS_ENABLED(LIB_UUID)
Simon Glassfbb99dc2021-05-08 06:59:58 -060027/* Test printing GUIDs */
28static int print_guid(struct unit_test_state *uts)
29{
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020030 unsigned char guid[16] = {
31 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
32 };
33 char str[40];
34
35 sprintf(str, "%pUb", guid);
Simon Glassfbb99dc2021-05-08 06:59:58 -060036 ut_assertok(strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str));
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020037 sprintf(str, "%pUB", guid);
Simon Glassfbb99dc2021-05-08 06:59:58 -060038 ut_assertok(strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str));
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020039 sprintf(str, "%pUl", guid);
Simon Glassfbb99dc2021-05-08 06:59:58 -060040 ut_assertok(strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str));
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020041 sprintf(str, "%pUL", guid);
Simon Glassfbb99dc2021-05-08 06:59:58 -060042 ut_assertok(strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str));
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020043
Simon Glassfbb99dc2021-05-08 06:59:58 -060044 return 0;
45}
46PRINT_TEST(print_guid, 0);
47#endif
48
Heinrich Schuchardt7a9e6ee2018-08-31 21:31:24 +020049#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Simon Glassfbb99dc2021-05-08 06:59:58 -060050/* Test efi_loader specific printing */
51static int print_efi_ut(struct unit_test_state *uts)
52{
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010053 char str[10];
54 u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
55 sizeof(struct efi_device_path)];
56 u8 *pos = buf;
57 struct efi_device_path *dp_end;
58 struct efi_device_path_sd_mmc_path *dp_sd =
59 (struct efi_device_path_sd_mmc_path *)pos;
60
61 /* Create a device path for an SD card */
62 dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
63 dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
64 dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
65 dp_sd->slot_number = 3;
66 pos += sizeof(struct efi_device_path_sd_mmc_path);
67 /* Append end node */
68 dp_end = (struct efi_device_path *)pos;
69 dp_end->type = DEVICE_PATH_TYPE_END;
70 dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
71 dp_end->length = sizeof(struct efi_device_path);
72
73 snprintf(str, sizeof(str), "_%pD_", buf);
Simon Glassfbb99dc2021-05-08 06:59:58 -060074 ut_assertok(strcmp("_/SD(3)_", str));
Heinrich Schuchardt5f1ce1d2018-01-26 06:30:30 +010075
76 /* NULL device path */
77 snprintf(str, sizeof(str), "_%pD_", NULL);
Simon Glassfbb99dc2021-05-08 06:59:58 -060078 ut_assertok(strcmp("_<NULL>_", str));
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010079
Simon Glassfbb99dc2021-05-08 06:59:58 -060080 return 0;
81}
82PRINT_TEST(print_efi_ut, 0);
83#endif
84
85static int print_printf(struct unit_test_state *uts)
Simon Glasse5a9d272017-06-15 21:37:51 -060086{
87 char big_str[400];
88 int big_str_len;
89 char str[10], *s;
90 int len;
91
Simon Glasse5a9d272017-06-15 21:37:51 -060092 snprintf(str, sizeof(str), "testing");
Simon Glassfbb99dc2021-05-08 06:59:58 -060093 ut_assertok(strcmp("testing", str));
Simon Glasse5a9d272017-06-15 21:37:51 -060094
95 snprintf(str, sizeof(str), "testing but too long");
Simon Glassfbb99dc2021-05-08 06:59:58 -060096 ut_assertok(strcmp("testing b", str));
Simon Glasse5a9d272017-06-15 21:37:51 -060097
98 snprintf(str, 1, "testing none");
Simon Glassfbb99dc2021-05-08 06:59:58 -060099 ut_assertok(strcmp("", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600100
101 *str = 'x';
102 snprintf(str, 0, "testing none");
Simon Glassfbb99dc2021-05-08 06:59:58 -0600103 ut_asserteq('x', *str);
Simon Glasse5a9d272017-06-15 21:37:51 -0600104
Rob Clark085391b2017-09-13 18:46:54 -0400105 sprintf(big_str, "_%ls_", L"foo");
Simon Glassfbb99dc2021-05-08 06:59:58 -0600106 ut_assertok(strcmp("_foo_", big_str));
Rob Clark085391b2017-09-13 18:46:54 -0400107
Simon Glasse5a9d272017-06-15 21:37:51 -0600108 /* Test the banner function */
109 s = display_options_get_banner(true, str, sizeof(str));
Simon Glassfbb99dc2021-05-08 06:59:58 -0600110 ut_asserteq_ptr(str, s);
111 ut_assertok(strcmp("\n\nU-Boo\n\n", s));
Simon Glasse5a9d272017-06-15 21:37:51 -0600112
Heinrich Schuchardt6c74e942019-04-26 18:39:00 +0200113 /* Assert that we do not overwrite memory before the buffer */
114 str[0] = '`';
115 s = display_options_get_banner(true, str + 1, 1);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600116 ut_asserteq_ptr(str + 1, s);
117 ut_assertok(strcmp("`", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600118
Heinrich Schuchardt6c74e942019-04-26 18:39:00 +0200119 str[0] = '~';
120 s = display_options_get_banner(true, str + 1, 2);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600121 ut_asserteq_ptr(str + 1, s);
122 ut_assertok(strcmp("~\n", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600123
Heinrich Schuchardt6c74e942019-04-26 18:39:00 +0200124 /* The last two characters are set to \n\n for all buffer sizes > 2 */
Simon Glasse5a9d272017-06-15 21:37:51 -0600125 s = display_options_get_banner(false, str, sizeof(str));
Simon Glassfbb99dc2021-05-08 06:59:58 -0600126 ut_asserteq_ptr(str, s);
127 ut_assertok(strcmp("U-Boot \n\n", s));
Simon Glasse5a9d272017-06-15 21:37:51 -0600128
129 /* Give it enough space for some of the version */
130 big_str_len = strlen(version_string) - 5;
131 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
132 big_str_len);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600133 ut_asserteq_ptr(big_str, s);
134 ut_assertok(strncmp(version_string, s, big_str_len - 3));
135 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
Simon Glasse5a9d272017-06-15 21:37:51 -0600136
137 /* Give it enough space for the version and some of the build tag */
138 big_str_len = strlen(version_string) + 9 + 20;
139 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
140 big_str_len);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600141 ut_asserteq_ptr(big_str, s);
Simon Glasse5a9d272017-06-15 21:37:51 -0600142 len = strlen(version_string);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600143 ut_assertok(strncmp(version_string, s, len));
144 ut_assertok(strncmp(", Build: ", s + len, 9));
145 ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
146 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
Simon Glasse5a9d272017-06-15 21:37:51 -0600147
Simon Glasse5a9d272017-06-15 21:37:51 -0600148 return 0;
149}
Simon Glassfbb99dc2021-05-08 06:59:58 -0600150PRINT_TEST(print_printf, 0);
Simon Glasse5a9d272017-06-15 21:37:51 -0600151
Simon Glassc614ddf2021-05-08 06:59:59 -0600152static int print_display_buffer(struct unit_test_state *uts)
153{
154 u8 *buf;
155 int i;
156
157 buf = map_sysmem(0, BUF_SIZE);
158 memset(buf, '\0', BUF_SIZE);
159 for (i = 0; i < 0x11; i++)
160 buf[i] = i * 0x11;
161
162 /* bytes */
163 console_record_reset();
164 print_buffer(0, buf, 1, 0x12, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600165 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
166 ut_assert_nextline("00000010: 10 00 ..");
Simon Glassc614ddf2021-05-08 06:59:59 -0600167 ut_assert_console_end();
168
169 /* line length */
170 console_record_reset();
171 print_buffer(0, buf, 1, 0x12, 8);
Simon Glassc7b16d82021-05-08 07:00:00 -0600172 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw");
173 ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........");
174 ut_assert_nextline("00000010: 10 00 ..");
Simon Glassc614ddf2021-05-08 06:59:59 -0600175 ut_assert_console_end();
176
177 /* long line */
178 console_record_reset();
179 buf[0x41] = 0x41;
180 print_buffer(0, buf, 1, 0x42, 0x40);
Simon Glassc7b16d82021-05-08 07:00:00 -0600181 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........................................................");
182 ut_assert_nextline("00000040: 00 41 .A");
Simon Glassc614ddf2021-05-08 06:59:59 -0600183 ut_assert_console_end();
184
185 /* address */
186 console_record_reset();
187 print_buffer(0x12345678, buf, 1, 0x12, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600188 ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
189 ut_assert_nextline("12345688: 10 00 ..");
Simon Glassc614ddf2021-05-08 06:59:59 -0600190 ut_assert_console_end();
191
192 /* 16-bit */
193 console_record_reset();
194 print_buffer(0, buf, 2, 9, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600195 ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
196 ut_assert_nextline("00000010: 0010 ..");
Simon Glassc614ddf2021-05-08 06:59:59 -0600197 ut_assert_console_end();
198
199 /* 32-bit */
200 console_record_reset();
201 print_buffer(0, buf, 4, 5, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600202 ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
203 ut_assert_nextline("00000010: 00000010 ....");
Simon Glassc614ddf2021-05-08 06:59:59 -0600204 ut_assert_console_end();
205
206 /* 64-bit */
207 console_record_reset();
208 print_buffer(0, buf, 8, 3, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600209 ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
210 ut_assert_nextline("00000010: 0000000000000010 ........");
Simon Glassc614ddf2021-05-08 06:59:59 -0600211 ut_assert_console_end();
212
213 /* ASCII */
214 console_record_reset();
215 buf[1] = 31;
216 buf[2] = 32;
217 buf[3] = 33;
218 for (i = 0; i < 4; i++)
219 buf[4 + i] = 126 + i;
220 buf[8] = 255;
221 print_buffer(0, buf, 1, 10, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600222 ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
Simon Glassc614ddf2021-05-08 06:59:59 -0600223 ut_assert_console_end();
224
225 unmap_sysmem(buf);
226
227 return 0;
228}
229PRINT_TEST(print_display_buffer, UT_TESTF_CONSOLE_REC);
230
Simon Glass0cceb992021-05-08 07:00:05 -0600231static int print_hexdump_line(struct unit_test_state *uts)
232{
233 char *linebuf;
234 u8 *buf;
235 int i;
236
237 buf = map_sysmem(0, BUF_SIZE);
238 memset(buf, '\0', BUF_SIZE);
239 for (i = 0; i < 0x11; i++)
240 buf[i] = i * 0x11;
241
242 /* Check buffer size calculations */
243 linebuf = map_sysmem(0x400, BUF_SIZE);
244 memset(linebuf, '\xff', BUF_SIZE);
245 ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75));
246 ut_asserteq(-1, linebuf[0]);
247 ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76));
248 ut_asserteq(0, linebuf[75]);
249 ut_asserteq(-1, linebuf[76]);
250
251 unmap_sysmem(buf);
252
253 return 0;
254}
255PRINT_TEST(print_hexdump_line, UT_TESTF_CONSOLE_REC);
256
Simon Glass19edf132021-05-08 07:00:02 -0600257static int print_do_hex_dump(struct unit_test_state *uts)
258{
259 u8 *buf;
260 int i;
261
262 buf = map_sysmem(0, BUF_SIZE);
263 memset(buf, '\0', BUF_SIZE);
264 for (i = 0; i < 0x11; i++)
265 buf[i] = i * 0x11;
266
267 /* bytes */
268 console_record_reset();
269 print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12);
270 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
271 ut_assert_nextline("00000010: 10 00 ..");
272 ut_assert_console_end();
273
Simon Glass5d6d2b82021-05-08 07:00:03 -0600274 /* line length */
275 console_record_reset();
276 print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true);
277 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw");
278 ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........");
279 ut_assert_nextline("00000010: 10 00 ..");
280 ut_assert_console_end();
281 unmap_sysmem(buf);
282
283 /* long line */
284 console_record_reset();
285 buf[0x41] = 0x41;
286 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true);
287 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........................................................");
288 ut_assert_nextline("00000040: 00 41 .A");
289 ut_assert_console_end();
290
Simon Glass19edf132021-05-08 07:00:02 -0600291 /* 16-bit */
292 console_record_reset();
Simon Glass5d6d2b82021-05-08 07:00:03 -0600293 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
Simon Glass19edf132021-05-08 07:00:02 -0600294 ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
295 ut_assert_nextline("00000010: 0010 ..");
296 ut_assert_console_end();
297 unmap_sysmem(buf);
298
299 /* 32-bit */
300 console_record_reset();
Simon Glass5d6d2b82021-05-08 07:00:03 -0600301 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
Simon Glass19edf132021-05-08 07:00:02 -0600302 ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
303 ut_assert_nextline("00000010: 00000010 ....");
304 ut_assert_console_end();
305 unmap_sysmem(buf);
306
307 /* 64-bit */
308 console_record_reset();
309 print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true);
310 ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
311 ut_assert_nextline("00000010: 0000000000000010 ........");
312 ut_assert_console_end();
313 unmap_sysmem(buf);
314
315 /* ASCII */
316 console_record_reset();
317 buf[1] = 31;
318 buf[2] = 32;
319 buf[3] = 33;
320 for (i = 0; i < 4; i++)
321 buf[4 + i] = 126 + i;
322 buf[8] = 255;
Simon Glass5d6d2b82021-05-08 07:00:03 -0600323 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
Simon Glass19edf132021-05-08 07:00:02 -0600324 ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
325 ut_assert_console_end();
326 unmap_sysmem(buf);
327
328 return 0;
329}
330PRINT_TEST(print_do_hex_dump, UT_TESTF_CONSOLE_REC);
331
Simon Glass3bfb0f72021-10-14 12:48:06 -0600332static int print_itoa(struct unit_test_state *uts)
333{
334 ut_asserteq_str("123", simple_itoa(123));
335 ut_asserteq_str("0", simple_itoa(0));
336 ut_asserteq_str("2147483647", simple_itoa(0x7fffffff));
337 ut_asserteq_str("4294967295", simple_itoa(0xffffffff));
338 if (sizeof(ulong) == 8) {
339 ut_asserteq_str("9223372036854775807",
340 simple_itoa((1UL << 63) - 1));
341 ut_asserteq_str("18446744073709551615", simple_itoa(-1));
342 }
343
344 return 0;
345}
346PRINT_TEST(print_itoa, 0);
347
Simon Glassfbb99dc2021-05-08 06:59:58 -0600348int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
349{
350 struct unit_test *tests = UNIT_TEST_SUITE_START(print_test);
351 const int n_ents = UNIT_TEST_SUITE_COUNT(print_test);
352
353 return cmd_ut_category("print", "print_", tests, n_ents, argc, argv);
354}