blob: f5e607b21a3c2f1d2a4d93383af6c7ed7bcb46c7 [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 Glass09140112020-05-10 11:40:03 -06006#include <command.h>
Heinrich Schuchardt256060e2018-01-10 18:06:08 +01007#include <efi_api.h>
Simon Glasse5a9d272017-06-15 21:37:51 -06008#include <display_options.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glassc614ddf2021-05-08 06:59:59 -060010#include <mapmem.h>
Pali Rohárbdfb6d72021-08-02 15:18:31 +020011#include <version_string.h>
Raymond Maocb73fe92024-05-16 14:11:52 -070012#include <stdio.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 };
Heinrich Schuchardt04641c12022-01-16 13:22:06 +010033 unsigned char guid_esp[16] = {
34 0x28, 0x73, 0x2a, 0xc1, 0x1f, 0xf8, 0xd2, 0x11,
35 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B
36 };
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020037 char str[40];
Heinrich Schuchardt0c9e8bf2021-11-15 19:06:55 +010038 int ret;
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020039
40 sprintf(str, "%pUb", guid);
Heinrich Schuchardt04641c12022-01-16 13:22:06 +010041 ut_asserteq_str("01020304-0506-0708-090a-0b0c0d0e0f10", str);
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020042 sprintf(str, "%pUB", guid);
Heinrich Schuchardt04641c12022-01-16 13:22:06 +010043 ut_asserteq_str("01020304-0506-0708-090A-0B0C0D0E0F10", str);
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020044 sprintf(str, "%pUl", guid);
Heinrich Schuchardt04641c12022-01-16 13:22:06 +010045 ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str);
46 sprintf(str, "%pUs", guid);
47 ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str);
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020048 sprintf(str, "%pUL", guid);
Heinrich Schuchardt04641c12022-01-16 13:22:06 +010049 ut_asserteq_str("04030201-0605-0807-090A-0B0C0D0E0F10", str);
50 sprintf(str, "%pUs", guid_esp);
51 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) { /* brace needed */
52 ut_asserteq_str("system", str);
53 } else {
54 ut_asserteq_str("c12a7328-f81f-11d2-ba4b-00a0c93ec93b", str);
55 }
Heinrich Schuchardt0c9e8bf2021-11-15 19:06:55 +010056 ret = snprintf(str, 4, "%pUL", guid);
57 ut_asserteq(0, str[3]);
58 ut_asserteq(36, ret);
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020059
Simon Glassfbb99dc2021-05-08 06:59:58 -060060 return 0;
61}
62PRINT_TEST(print_guid, 0);
63#endif
64
Heinrich Schuchardt7a9e6ee2018-08-31 21:31:24 +020065#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Simon Glassfbb99dc2021-05-08 06:59:58 -060066/* Test efi_loader specific printing */
67static int print_efi_ut(struct unit_test_state *uts)
68{
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010069 char str[10];
70 u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
71 sizeof(struct efi_device_path)];
72 u8 *pos = buf;
73 struct efi_device_path *dp_end;
74 struct efi_device_path_sd_mmc_path *dp_sd =
75 (struct efi_device_path_sd_mmc_path *)pos;
76
77 /* Create a device path for an SD card */
78 dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
79 dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
80 dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
81 dp_sd->slot_number = 3;
82 pos += sizeof(struct efi_device_path_sd_mmc_path);
83 /* Append end node */
84 dp_end = (struct efi_device_path *)pos;
85 dp_end->type = DEVICE_PATH_TYPE_END;
86 dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
87 dp_end->length = sizeof(struct efi_device_path);
88
89 snprintf(str, sizeof(str), "_%pD_", buf);
Simon Glassfbb99dc2021-05-08 06:59:58 -060090 ut_assertok(strcmp("_/SD(3)_", str));
Heinrich Schuchardt5f1ce1d2018-01-26 06:30:30 +010091
92 /* NULL device path */
93 snprintf(str, sizeof(str), "_%pD_", NULL);
Simon Glassfbb99dc2021-05-08 06:59:58 -060094 ut_assertok(strcmp("_<NULL>_", str));
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010095
Simon Glassfbb99dc2021-05-08 06:59:58 -060096 return 0;
97}
98PRINT_TEST(print_efi_ut, 0);
99#endif
100
101static int print_printf(struct unit_test_state *uts)
Simon Glasse5a9d272017-06-15 21:37:51 -0600102{
103 char big_str[400];
104 int big_str_len;
105 char str[10], *s;
106 int len;
107
Simon Glasse5a9d272017-06-15 21:37:51 -0600108 snprintf(str, sizeof(str), "testing");
Simon Glassfbb99dc2021-05-08 06:59:58 -0600109 ut_assertok(strcmp("testing", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600110
111 snprintf(str, sizeof(str), "testing but too long");
Simon Glassfbb99dc2021-05-08 06:59:58 -0600112 ut_assertok(strcmp("testing b", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600113
114 snprintf(str, 1, "testing none");
Simon Glassfbb99dc2021-05-08 06:59:58 -0600115 ut_assertok(strcmp("", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600116
117 *str = 'x';
118 snprintf(str, 0, "testing none");
Simon Glassfbb99dc2021-05-08 06:59:58 -0600119 ut_asserteq('x', *str);
Simon Glasse5a9d272017-06-15 21:37:51 -0600120
Simon Glass5b9a5b22022-01-23 12:55:14 -0700121 sprintf(big_str, "_%ls_", u"foo");
Simon Glassfbb99dc2021-05-08 06:59:58 -0600122 ut_assertok(strcmp("_foo_", big_str));
Rob Clark085391b2017-09-13 18:46:54 -0400123
Simon Glasse5a9d272017-06-15 21:37:51 -0600124 /* Test the banner function */
125 s = display_options_get_banner(true, str, sizeof(str));
Simon Glassfbb99dc2021-05-08 06:59:58 -0600126 ut_asserteq_ptr(str, s);
127 ut_assertok(strcmp("\n\nU-Boo\n\n", s));
Simon Glasse5a9d272017-06-15 21:37:51 -0600128
Heinrich Schuchardt6c74e942019-04-26 18:39:00 +0200129 /* Assert that we do not overwrite memory before the buffer */
130 str[0] = '`';
131 s = display_options_get_banner(true, str + 1, 1);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600132 ut_asserteq_ptr(str + 1, s);
133 ut_assertok(strcmp("`", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600134
Heinrich Schuchardt6c74e942019-04-26 18:39:00 +0200135 str[0] = '~';
136 s = display_options_get_banner(true, str + 1, 2);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600137 ut_asserteq_ptr(str + 1, s);
138 ut_assertok(strcmp("~\n", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600139
Heinrich Schuchardt6c74e942019-04-26 18:39:00 +0200140 /* The last two characters are set to \n\n for all buffer sizes > 2 */
Simon Glasse5a9d272017-06-15 21:37:51 -0600141 s = display_options_get_banner(false, str, sizeof(str));
Simon Glassfbb99dc2021-05-08 06:59:58 -0600142 ut_asserteq_ptr(str, s);
143 ut_assertok(strcmp("U-Boot \n\n", s));
Simon Glasse5a9d272017-06-15 21:37:51 -0600144
145 /* Give it enough space for some of the version */
146 big_str_len = strlen(version_string) - 5;
147 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
148 big_str_len);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600149 ut_asserteq_ptr(big_str, s);
150 ut_assertok(strncmp(version_string, s, big_str_len - 3));
151 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
Simon Glasse5a9d272017-06-15 21:37:51 -0600152
153 /* Give it enough space for the version and some of the build tag */
154 big_str_len = strlen(version_string) + 9 + 20;
155 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
156 big_str_len);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600157 ut_asserteq_ptr(big_str, s);
Simon Glasse5a9d272017-06-15 21:37:51 -0600158 len = strlen(version_string);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600159 ut_assertok(strncmp(version_string, s, len));
160 ut_assertok(strncmp(", Build: ", s + len, 9));
161 ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
162 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
Simon Glasse5a9d272017-06-15 21:37:51 -0600163
Simon Glasse5a9d272017-06-15 21:37:51 -0600164 return 0;
165}
Simon Glassfbb99dc2021-05-08 06:59:58 -0600166PRINT_TEST(print_printf, 0);
Simon Glasse5a9d272017-06-15 21:37:51 -0600167
Simon Glassc614ddf2021-05-08 06:59:59 -0600168static int print_display_buffer(struct unit_test_state *uts)
169{
170 u8 *buf;
171 int i;
172
Simon Glassc0e708e2023-10-01 19:15:22 -0600173 /* This test requires writable memory at zero */
174 if (IS_ENABLED(CONFIG_X86))
175 return -EAGAIN;
176
Simon Glassc614ddf2021-05-08 06:59:59 -0600177 buf = map_sysmem(0, BUF_SIZE);
178 memset(buf, '\0', BUF_SIZE);
179 for (i = 0; i < 0x11; i++)
180 buf[i] = i * 0x11;
181
182 /* bytes */
Simon Glassc614ddf2021-05-08 06:59:59 -0600183 print_buffer(0, buf, 1, 0x12, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600184 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
185 ut_assert_nextline("00000010: 10 00 ..");
Simon Glassc614ddf2021-05-08 06:59:59 -0600186 ut_assert_console_end();
187
188 /* line length */
Simon Glassc614ddf2021-05-08 06:59:59 -0600189 print_buffer(0, buf, 1, 0x12, 8);
Simon Glassc7b16d82021-05-08 07:00:00 -0600190 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw");
191 ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........");
192 ut_assert_nextline("00000010: 10 00 ..");
Simon Glassc614ddf2021-05-08 06:59:59 -0600193 ut_assert_console_end();
194
195 /* long line */
Simon Glassc614ddf2021-05-08 06:59:59 -0600196 buf[0x41] = 0x41;
197 print_buffer(0, buf, 1, 0x42, 0x40);
Simon Glassc7b16d82021-05-08 07:00:00 -0600198 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........................................................");
199 ut_assert_nextline("00000040: 00 41 .A");
Simon Glassc614ddf2021-05-08 06:59:59 -0600200 ut_assert_console_end();
201
202 /* address */
Simon Glassc614ddf2021-05-08 06:59:59 -0600203 print_buffer(0x12345678, buf, 1, 0x12, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600204 ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
205 ut_assert_nextline("12345688: 10 00 ..");
Simon Glassc614ddf2021-05-08 06:59:59 -0600206 ut_assert_console_end();
207
208 /* 16-bit */
Simon Glassc614ddf2021-05-08 06:59:59 -0600209 print_buffer(0, buf, 2, 9, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600210 ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
211 ut_assert_nextline("00000010: 0010 ..");
Simon Glassc614ddf2021-05-08 06:59:59 -0600212 ut_assert_console_end();
213
214 /* 32-bit */
Simon Glassc614ddf2021-05-08 06:59:59 -0600215 print_buffer(0, buf, 4, 5, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600216 ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
217 ut_assert_nextline("00000010: 00000010 ....");
Simon Glassc614ddf2021-05-08 06:59:59 -0600218 ut_assert_console_end();
219
220 /* 64-bit */
Simon Glassc614ddf2021-05-08 06:59:59 -0600221 print_buffer(0, buf, 8, 3, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600222 ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
223 ut_assert_nextline("00000010: 0000000000000010 ........");
Simon Glassc614ddf2021-05-08 06:59:59 -0600224 ut_assert_console_end();
225
226 /* ASCII */
Simon Glassc614ddf2021-05-08 06:59:59 -0600227 buf[1] = 31;
228 buf[2] = 32;
229 buf[3] = 33;
230 for (i = 0; i < 4; i++)
231 buf[4 + i] = 126 + i;
232 buf[8] = 255;
233 print_buffer(0, buf, 1, 10, 0);
Simon Glassc7b16d82021-05-08 07:00:00 -0600234 ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
Simon Glassc614ddf2021-05-08 06:59:59 -0600235 ut_assert_console_end();
236
237 unmap_sysmem(buf);
238
239 return 0;
240}
Simon Glass9b997622024-08-22 07:57:50 -0600241PRINT_TEST(print_display_buffer, UTF_CONSOLE);
Simon Glassc614ddf2021-05-08 06:59:59 -0600242
Simon Glass0cceb992021-05-08 07:00:05 -0600243static int print_hexdump_line(struct unit_test_state *uts)
244{
245 char *linebuf;
246 u8 *buf;
247 int i;
248
249 buf = map_sysmem(0, BUF_SIZE);
250 memset(buf, '\0', BUF_SIZE);
251 for (i = 0; i < 0x11; i++)
252 buf[i] = i * 0x11;
253
254 /* Check buffer size calculations */
255 linebuf = map_sysmem(0x400, BUF_SIZE);
256 memset(linebuf, '\xff', BUF_SIZE);
257 ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75));
258 ut_asserteq(-1, linebuf[0]);
259 ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76));
260 ut_asserteq(0, linebuf[75]);
261 ut_asserteq(-1, linebuf[76]);
262
263 unmap_sysmem(buf);
264
265 return 0;
266}
Simon Glass9b997622024-08-22 07:57:50 -0600267PRINT_TEST(print_hexdump_line, UTF_CONSOLE);
Simon Glass0cceb992021-05-08 07:00:05 -0600268
Simon Glass19edf132021-05-08 07:00:02 -0600269static int print_do_hex_dump(struct unit_test_state *uts)
270{
271 u8 *buf;
272 int i;
273
Simon Glassc0e708e2023-10-01 19:15:22 -0600274 /* This test requires writable memory at zero */
275 if (IS_ENABLED(CONFIG_X86))
276 return -EAGAIN;
277
Simon Glass19edf132021-05-08 07:00:02 -0600278 buf = map_sysmem(0, BUF_SIZE);
279 memset(buf, '\0', BUF_SIZE);
280 for (i = 0; i < 0x11; i++)
281 buf[i] = i * 0x11;
282
283 /* bytes */
Simon Glass19edf132021-05-08 07:00:02 -0600284 print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12);
Marek Vasut8b3694a2023-08-25 10:19:40 +0200285 ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........",
286 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
287 ut_assert_nextline("%0*lx: 10 00 ..",
288 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass19edf132021-05-08 07:00:02 -0600289 ut_assert_console_end();
290
Simon Glass5d6d2b82021-05-08 07:00:03 -0600291 /* line length */
Simon Glass5d6d2b82021-05-08 07:00:03 -0600292 print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true);
Marek Vasut8b3694a2023-08-25 10:19:40 +0200293 ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 ..\"3DUfw",
294 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
295 ut_assert_nextline("%0*lx: 88 99 aa bb cc dd ee ff ........",
296 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x8UL);
297 ut_assert_nextline("%0*lx: 10 00 ..",
298 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass5d6d2b82021-05-08 07:00:03 -0600299 ut_assert_console_end();
300 unmap_sysmem(buf);
301
302 /* long line */
Simon Glass5d6d2b82021-05-08 07:00:03 -0600303 buf[0x41] = 0x41;
304 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true);
Marek Vasut8b3694a2023-08-25 10:19:40 +0200305 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........................................................",
306 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
307 ut_assert_nextline("%0*lx: 00 41 .A",
308 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x40UL);
Simon Glass5d6d2b82021-05-08 07:00:03 -0600309 ut_assert_console_end();
310
Simon Glass19edf132021-05-08 07:00:02 -0600311 /* 16-bit */
Simon Glass5d6d2b82021-05-08 07:00:03 -0600312 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
Marek Vasut8b3694a2023-08-25 10:19:40 +0200313 ut_assert_nextline("%0*lx: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........",
314 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
315 ut_assert_nextline("%0*lx: 0010 ..",
316 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass19edf132021-05-08 07:00:02 -0600317 ut_assert_console_end();
318 unmap_sysmem(buf);
319
320 /* 32-bit */
Simon Glass5d6d2b82021-05-08 07:00:03 -0600321 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
Marek Vasut8b3694a2023-08-25 10:19:40 +0200322 ut_assert_nextline("%0*lx: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........",
323 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
324 ut_assert_nextline("%0*lx: 00000010 ....",
325 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass19edf132021-05-08 07:00:02 -0600326 ut_assert_console_end();
327 unmap_sysmem(buf);
328
329 /* 64-bit */
Simon Glass19edf132021-05-08 07:00:02 -0600330 print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true);
Marek Vasut8b3694a2023-08-25 10:19:40 +0200331 ut_assert_nextline("%0*lx: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........",
332 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
333 ut_assert_nextline("%0*lx: 0000000000000010 ........",
334 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass19edf132021-05-08 07:00:02 -0600335 ut_assert_console_end();
336 unmap_sysmem(buf);
337
338 /* ASCII */
Simon Glass19edf132021-05-08 07:00:02 -0600339 buf[1] = 31;
340 buf[2] = 32;
341 buf[3] = 33;
342 for (i = 0; i < 4; i++)
343 buf[4 + i] = 126 + i;
344 buf[8] = 255;
Simon Glass5d6d2b82021-05-08 07:00:03 -0600345 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
Marek Vasut8b3694a2023-08-25 10:19:40 +0200346 ut_assert_nextline("%0*lx: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....",
347 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
Simon Glass19edf132021-05-08 07:00:02 -0600348 ut_assert_console_end();
349 unmap_sysmem(buf);
350
351 return 0;
352}
Simon Glass9b997622024-08-22 07:57:50 -0600353PRINT_TEST(print_do_hex_dump, UTF_CONSOLE);
Simon Glass19edf132021-05-08 07:00:02 -0600354
Heinrich Schuchardt0c9e8bf2021-11-15 19:06:55 +0100355static int snprint(struct unit_test_state *uts)
356{
357 char buf[10] = "xxxxxxxxx";
358 int ret;
359
Heinrich Schuchardt73cde902022-01-29 16:33:16 +0100360 ret = snprintf(buf, 5, "%d", 12345678);
361 ut_asserteq_str("1234", buf);
362 ut_asserteq(8, ret);
363 ret = snprintf(buf, 5, "0x%x", 0x1234);
364 ut_asserteq_str("0x12", buf);
365 ut_asserteq(6, ret);
366 ret = snprintf(buf, 5, "0x%08x", 0x1234);
367 ut_asserteq_str("0x00", buf);
368 ut_asserteq(10, ret);
369 ret = snprintf(buf, 3, "%s", "abc");
370 ut_asserteq_str("ab", buf);
371 ut_asserteq(3, ret);
Heinrich Schuchardt0c9e8bf2021-11-15 19:06:55 +0100372 ret = snprintf(buf, 4, "%s:%s", "abc", "def");
373 ut_asserteq(0, buf[3]);
374 ut_asserteq(7, ret);
375 ret = snprintf(buf, 4, "%s:%d", "abc", 9999);
376 ut_asserteq(8, ret);
377 return 0;
378}
379PRINT_TEST(snprint, 0);
380
Simon Glassfbb99dc2021-05-08 06:59:58 -0600381int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
382{
383 struct unit_test *tests = UNIT_TEST_SUITE_START(print_test);
384 const int n_ents = UNIT_TEST_SUITE_COUNT(print_test);
385
386 return cmd_ut_category("print", "print_", tests, n_ents, argc, argv);
387}