blob: e3711b1080912473c6c4750a1d78ac02a7778b0c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass75ee32f2017-06-15 21:37:51 -06002/*
3 * Copyright (c) 2012, The Chromium Authors
Simon Glass75ee32f2017-06-15 21:37:51 -06004 */
5
Simon Glassed38aef2020-05-10 11:40:03 -06006#include <command.h>
Heinrich Schuchardt218d3892018-01-10 18:06:08 +01007#include <efi_api.h>
Simon Glass75ee32f2017-06-15 21:37:51 -06008#include <display_options.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass4a249ba2021-05-08 06:59:59 -060010#include <mapmem.h>
Pali Rohárba87ddf2021-08-02 15:18:31 +020011#include <version_string.h>
Raymond Mao24da8312024-05-16 14:11:52 -070012#include <stdio.h>
Simon Glass978739d2021-10-14 12:48:06 -060013#include <vsprintf.h>
Simon Glass3394c3c2024-11-02 13:36:56 -060014#include <test/common.h>
Simon Glassd2423682021-05-08 06:59:58 -060015#include <test/test.h>
16#include <test/ut.h>
Simon Glass75ee32f2017-06-15 21:37:51 -060017
Simon Glass4a249ba2021-05-08 06:59:59 -060018#define BUF_SIZE 0x100
19
Simon Glass75ee32f2017-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 Glassd2423682021-05-08 06:59:58 -060023#if CONFIG_IS_ENABLED(LIB_UUID)
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020024/* Test printing GUIDs */
Simon Glassd2423682021-05-08 06:59:58 -060025static int print_guid(struct unit_test_state *uts)
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020026{
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020027 unsigned char guid[16] = {
28 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
29 };
Heinrich Schuchardtd2e0aa32022-01-16 13:22:06 +010030 unsigned char guid_esp[16] = {
31 0x28, 0x73, 0x2a, 0xc1, 0x1f, 0xf8, 0xd2, 0x11,
32 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B
33 };
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020034 char str[40];
Heinrich Schuchardt7eaa37b2021-11-15 19:06:55 +010035 int ret;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020036
37 sprintf(str, "%pUb", guid);
Heinrich Schuchardtd2e0aa32022-01-16 13:22:06 +010038 ut_asserteq_str("01020304-0506-0708-090a-0b0c0d0e0f10", str);
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020039 sprintf(str, "%pUB", guid);
Heinrich Schuchardtd2e0aa32022-01-16 13:22:06 +010040 ut_asserteq_str("01020304-0506-0708-090A-0B0C0D0E0F10", str);
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020041 sprintf(str, "%pUl", guid);
Heinrich Schuchardtd2e0aa32022-01-16 13:22:06 +010042 ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str);
43 sprintf(str, "%pUs", guid);
44 ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str);
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020045 sprintf(str, "%pUL", guid);
Heinrich Schuchardtd2e0aa32022-01-16 13:22:06 +010046 ut_asserteq_str("04030201-0605-0807-090A-0B0C0D0E0F10", str);
47 sprintf(str, "%pUs", guid_esp);
48 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) { /* brace needed */
49 ut_asserteq_str("system", str);
50 } else {
51 ut_asserteq_str("c12a7328-f81f-11d2-ba4b-00a0c93ec93b", str);
52 }
Heinrich Schuchardt7eaa37b2021-11-15 19:06:55 +010053 ret = snprintf(str, 4, "%pUL", guid);
54 ut_asserteq(0, str[3]);
55 ut_asserteq(36, ret);
Simon Glassd2423682021-05-08 06:59:58 -060056
57 return 0;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020058}
Simon Glass3394c3c2024-11-02 13:36:56 -060059COMMON_TEST(print_guid, 0);
Simon Glassd2423682021-05-08 06:59:58 -060060#endif
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020061
Simon Glassd2423682021-05-08 06:59:58 -060062#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010063/* Test efi_loader specific printing */
Simon Glassd2423682021-05-08 06:59:58 -060064static int print_efi_ut(struct unit_test_state *uts)
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010065{
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010066 char str[10];
67 u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
68 sizeof(struct efi_device_path)];
69 u8 *pos = buf;
70 struct efi_device_path *dp_end;
71 struct efi_device_path_sd_mmc_path *dp_sd =
72 (struct efi_device_path_sd_mmc_path *)pos;
73
74 /* Create a device path for an SD card */
75 dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
76 dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
77 dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
78 dp_sd->slot_number = 3;
79 pos += sizeof(struct efi_device_path_sd_mmc_path);
80 /* Append end node */
81 dp_end = (struct efi_device_path *)pos;
82 dp_end->type = DEVICE_PATH_TYPE_END;
83 dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
84 dp_end->length = sizeof(struct efi_device_path);
85
86 snprintf(str, sizeof(str), "_%pD_", buf);
Simon Glassd2423682021-05-08 06:59:58 -060087 ut_assertok(strcmp("_/SD(3)_", str));
Heinrich Schuchardt3b47f332018-01-26 06:30:30 +010088
89 /* NULL device path */
90 snprintf(str, sizeof(str), "_%pD_", NULL);
Simon Glassd2423682021-05-08 06:59:58 -060091 ut_assertok(strcmp("_<NULL>_", str));
92
93 return 0;
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010094}
Simon Glass3394c3c2024-11-02 13:36:56 -060095COMMON_TEST(print_efi_ut, 0);
Simon Glassd2423682021-05-08 06:59:58 -060096#endif
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010097
Simon Glassd2423682021-05-08 06:59:58 -060098static int print_printf(struct unit_test_state *uts)
Simon Glass75ee32f2017-06-15 21:37:51 -060099{
100 char big_str[400];
101 int big_str_len;
102 char str[10], *s;
103 int len;
104
Simon Glass75ee32f2017-06-15 21:37:51 -0600105 snprintf(str, sizeof(str), "testing");
Simon Glassd2423682021-05-08 06:59:58 -0600106 ut_assertok(strcmp("testing", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600107
108 snprintf(str, sizeof(str), "testing but too long");
Simon Glassd2423682021-05-08 06:59:58 -0600109 ut_assertok(strcmp("testing b", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600110
111 snprintf(str, 1, "testing none");
Simon Glassd2423682021-05-08 06:59:58 -0600112 ut_assertok(strcmp("", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600113
114 *str = 'x';
115 snprintf(str, 0, "testing none");
Simon Glassd2423682021-05-08 06:59:58 -0600116 ut_asserteq('x', *str);
Simon Glass75ee32f2017-06-15 21:37:51 -0600117
Heinrich Schuchardt3eb9d262024-11-03 21:46:42 +0100118 if (CONFIG_IS_ENABLED(EFI_LOADER) || IS_ENABLED(CONFIG_EFI_APP)) {
119 sprintf(big_str, "_%ls_", u"foo");
120 ut_assertok(strcmp("_foo_", big_str));
121 }
Rob Clark22ae0a72017-09-13 18:46:54 -0400122
Simon Glass75ee32f2017-06-15 21:37:51 -0600123 /* Test the banner function */
124 s = display_options_get_banner(true, str, sizeof(str));
Simon Glassd2423682021-05-08 06:59:58 -0600125 ut_asserteq_ptr(str, s);
126 ut_assertok(strcmp("\n\nU-Boo\n\n", s));
Simon Glass75ee32f2017-06-15 21:37:51 -0600127
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +0200128 /* Assert that we do not overwrite memory before the buffer */
129 str[0] = '`';
130 s = display_options_get_banner(true, str + 1, 1);
Simon Glassd2423682021-05-08 06:59:58 -0600131 ut_asserteq_ptr(str + 1, s);
132 ut_assertok(strcmp("`", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600133
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +0200134 str[0] = '~';
135 s = display_options_get_banner(true, str + 1, 2);
Simon Glassd2423682021-05-08 06:59:58 -0600136 ut_asserteq_ptr(str + 1, s);
137 ut_assertok(strcmp("~\n", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600138
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +0200139 /* The last two characters are set to \n\n for all buffer sizes > 2 */
Simon Glass75ee32f2017-06-15 21:37:51 -0600140 s = display_options_get_banner(false, str, sizeof(str));
Simon Glassd2423682021-05-08 06:59:58 -0600141 ut_asserteq_ptr(str, s);
142 ut_assertok(strcmp("U-Boot \n\n", s));
Simon Glass75ee32f2017-06-15 21:37:51 -0600143
144 /* Give it enough space for some of the version */
145 big_str_len = strlen(version_string) - 5;
146 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
147 big_str_len);
Simon Glassd2423682021-05-08 06:59:58 -0600148 ut_asserteq_ptr(big_str, s);
149 ut_assertok(strncmp(version_string, s, big_str_len - 3));
150 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
Simon Glass75ee32f2017-06-15 21:37:51 -0600151
152 /* Give it enough space for the version and some of the build tag */
153 big_str_len = strlen(version_string) + 9 + 20;
154 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
155 big_str_len);
Simon Glassd2423682021-05-08 06:59:58 -0600156 ut_asserteq_ptr(big_str, s);
Simon Glass75ee32f2017-06-15 21:37:51 -0600157 len = strlen(version_string);
Simon Glassd2423682021-05-08 06:59:58 -0600158 ut_assertok(strncmp(version_string, s, len));
159 ut_assertok(strncmp(", Build: ", s + len, 9));
160 ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
161 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
Simon Glass75ee32f2017-06-15 21:37:51 -0600162
Simon Glass75ee32f2017-06-15 21:37:51 -0600163 return 0;
164}
Simon Glass3394c3c2024-11-02 13:36:56 -0600165COMMON_TEST(print_printf, 0);
Simon Glass75ee32f2017-06-15 21:37:51 -0600166
Simon Glass4a249ba2021-05-08 06:59:59 -0600167static int print_display_buffer(struct unit_test_state *uts)
168{
169 u8 *buf;
170 int i;
171
Simon Glassa7dd66f2023-10-01 19:15:22 -0600172 /* This test requires writable memory at zero */
173 if (IS_ENABLED(CONFIG_X86))
174 return -EAGAIN;
175
Simon Glass4a249ba2021-05-08 06:59:59 -0600176 buf = map_sysmem(0, BUF_SIZE);
177 memset(buf, '\0', BUF_SIZE);
178 for (i = 0; i < 0x11; i++)
179 buf[i] = i * 0x11;
180
181 /* bytes */
Simon Glass4a249ba2021-05-08 06:59:59 -0600182 print_buffer(0, buf, 1, 0x12, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600183 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
184 ut_assert_nextline("00000010: 10 00 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600185 ut_assert_console_end();
186
187 /* line length */
Simon Glass4a249ba2021-05-08 06:59:59 -0600188 print_buffer(0, buf, 1, 0x12, 8);
Simon Glass85c8fc52021-05-08 07:00:00 -0600189 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw");
190 ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........");
191 ut_assert_nextline("00000010: 10 00 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600192 ut_assert_console_end();
193
194 /* long line */
Simon Glass4a249ba2021-05-08 06:59:59 -0600195 buf[0x41] = 0x41;
196 print_buffer(0, buf, 1, 0x42, 0x40);
Simon Glass85c8fc52021-05-08 07:00:00 -0600197 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........................................................");
198 ut_assert_nextline("00000040: 00 41 .A");
Simon Glass4a249ba2021-05-08 06:59:59 -0600199 ut_assert_console_end();
200
201 /* address */
Simon Glass4a249ba2021-05-08 06:59:59 -0600202 print_buffer(0x12345678, buf, 1, 0x12, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600203 ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
204 ut_assert_nextline("12345688: 10 00 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600205 ut_assert_console_end();
206
207 /* 16-bit */
Simon Glass4a249ba2021-05-08 06:59:59 -0600208 print_buffer(0, buf, 2, 9, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600209 ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
210 ut_assert_nextline("00000010: 0010 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600211 ut_assert_console_end();
212
213 /* 32-bit */
Simon Glass4a249ba2021-05-08 06:59:59 -0600214 print_buffer(0, buf, 4, 5, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600215 ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
216 ut_assert_nextline("00000010: 00000010 ....");
Simon Glass4a249ba2021-05-08 06:59:59 -0600217 ut_assert_console_end();
218
219 /* 64-bit */
Simon Glass4a249ba2021-05-08 06:59:59 -0600220 print_buffer(0, buf, 8, 3, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600221 ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
222 ut_assert_nextline("00000010: 0000000000000010 ........");
Simon Glass4a249ba2021-05-08 06:59:59 -0600223 ut_assert_console_end();
224
225 /* ASCII */
Simon Glass4a249ba2021-05-08 06:59:59 -0600226 buf[1] = 31;
227 buf[2] = 32;
228 buf[3] = 33;
229 for (i = 0; i < 4; i++)
230 buf[4 + i] = 126 + i;
231 buf[8] = 255;
232 print_buffer(0, buf, 1, 10, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600233 ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
Simon Glass4a249ba2021-05-08 06:59:59 -0600234 ut_assert_console_end();
235
236 unmap_sysmem(buf);
237
238 return 0;
239}
Simon Glass3394c3c2024-11-02 13:36:56 -0600240COMMON_TEST(print_display_buffer, UTF_CONSOLE);
Simon Glass4a249ba2021-05-08 06:59:59 -0600241
Simon Glass02b8f1a2021-05-08 07:00:05 -0600242static int print_hexdump_line(struct unit_test_state *uts)
243{
Simon Glass537664c2024-11-27 11:17:20 -0600244 u8 *linebuf;
Simon Glass02b8f1a2021-05-08 07:00:05 -0600245 u8 *buf;
246 int i;
247
248 buf = map_sysmem(0, BUF_SIZE);
249 memset(buf, '\0', BUF_SIZE);
250 for (i = 0; i < 0x11; i++)
251 buf[i] = i * 0x11;
252
253 /* Check buffer size calculations */
254 linebuf = map_sysmem(0x400, BUF_SIZE);
255 memset(linebuf, '\xff', BUF_SIZE);
256 ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75));
Simon Glass537664c2024-11-27 11:17:20 -0600257 ut_asserteq(0xff, linebuf[0]);
Simon Glass02b8f1a2021-05-08 07:00:05 -0600258 ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76));
Simon Glass537664c2024-11-27 11:17:20 -0600259 ut_asserteq('\0', linebuf[75]);
260 ut_asserteq(0xff, linebuf[76]);
Simon Glass02b8f1a2021-05-08 07:00:05 -0600261
262 unmap_sysmem(buf);
263
264 return 0;
265}
Simon Glass3394c3c2024-11-02 13:36:56 -0600266COMMON_TEST(print_hexdump_line, UTF_CONSOLE);
Simon Glass02b8f1a2021-05-08 07:00:05 -0600267
Simon Glass26507152021-05-08 07:00:02 -0600268static int print_do_hex_dump(struct unit_test_state *uts)
269{
270 u8 *buf;
271 int i;
272
Simon Glassa7dd66f2023-10-01 19:15:22 -0600273 /* This test requires writable memory at zero */
274 if (IS_ENABLED(CONFIG_X86))
275 return -EAGAIN;
276
Simon Glass26507152021-05-08 07:00:02 -0600277 buf = map_sysmem(0, BUF_SIZE);
278 memset(buf, '\0', BUF_SIZE);
279 for (i = 0; i < 0x11; i++)
280 buf[i] = i * 0x11;
281
282 /* bytes */
Simon Glass26507152021-05-08 07:00:02 -0600283 print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200284 ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........",
285 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
286 ut_assert_nextline("%0*lx: 10 00 ..",
287 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass26507152021-05-08 07:00:02 -0600288 ut_assert_console_end();
289
Simon Glassf1d06472021-05-08 07:00:03 -0600290 /* line length */
Simon Glassf1d06472021-05-08 07:00:03 -0600291 print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200292 ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 ..\"3DUfw",
293 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
294 ut_assert_nextline("%0*lx: 88 99 aa bb cc dd ee ff ........",
295 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x8UL);
296 ut_assert_nextline("%0*lx: 10 00 ..",
297 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glassf1d06472021-05-08 07:00:03 -0600298 ut_assert_console_end();
299 unmap_sysmem(buf);
300
301 /* long line */
Simon Glassf1d06472021-05-08 07:00:03 -0600302 buf[0x41] = 0x41;
303 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200304 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........................................................",
305 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
306 ut_assert_nextline("%0*lx: 00 41 .A",
307 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x40UL);
Simon Glassf1d06472021-05-08 07:00:03 -0600308 ut_assert_console_end();
309
Simon Glass26507152021-05-08 07:00:02 -0600310 /* 16-bit */
Simon Glassf1d06472021-05-08 07:00:03 -0600311 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200312 ut_assert_nextline("%0*lx: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........",
313 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
314 ut_assert_nextline("%0*lx: 0010 ..",
315 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass26507152021-05-08 07:00:02 -0600316 ut_assert_console_end();
317 unmap_sysmem(buf);
318
319 /* 32-bit */
Simon Glassf1d06472021-05-08 07:00:03 -0600320 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200321 ut_assert_nextline("%0*lx: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........",
322 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
323 ut_assert_nextline("%0*lx: 00000010 ....",
324 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass26507152021-05-08 07:00:02 -0600325 ut_assert_console_end();
326 unmap_sysmem(buf);
327
328 /* 64-bit */
Simon Glass26507152021-05-08 07:00:02 -0600329 print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200330 ut_assert_nextline("%0*lx: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........",
331 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
332 ut_assert_nextline("%0*lx: 0000000000000010 ........",
333 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass26507152021-05-08 07:00:02 -0600334 ut_assert_console_end();
335 unmap_sysmem(buf);
336
337 /* ASCII */
Simon Glass26507152021-05-08 07:00:02 -0600338 buf[1] = 31;
339 buf[2] = 32;
340 buf[3] = 33;
341 for (i = 0; i < 4; i++)
342 buf[4 + i] = 126 + i;
343 buf[8] = 255;
Simon Glassf1d06472021-05-08 07:00:03 -0600344 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200345 ut_assert_nextline("%0*lx: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....",
346 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
Simon Glass26507152021-05-08 07:00:02 -0600347 ut_assert_console_end();
348 unmap_sysmem(buf);
349
350 return 0;
351}
Simon Glass3394c3c2024-11-02 13:36:56 -0600352COMMON_TEST(print_do_hex_dump, UTF_CONSOLE);
Simon Glass26507152021-05-08 07:00:02 -0600353
Heinrich Schuchardt7eaa37b2021-11-15 19:06:55 +0100354static int snprint(struct unit_test_state *uts)
355{
356 char buf[10] = "xxxxxxxxx";
357 int ret;
358
Heinrich Schuchardt2df4f952022-01-29 16:33:16 +0100359 ret = snprintf(buf, 5, "%d", 12345678);
360 ut_asserteq_str("1234", buf);
361 ut_asserteq(8, ret);
362 ret = snprintf(buf, 5, "0x%x", 0x1234);
363 ut_asserteq_str("0x12", buf);
364 ut_asserteq(6, ret);
365 ret = snprintf(buf, 5, "0x%08x", 0x1234);
366 ut_asserteq_str("0x00", buf);
367 ut_asserteq(10, ret);
368 ret = snprintf(buf, 3, "%s", "abc");
369 ut_asserteq_str("ab", buf);
370 ut_asserteq(3, ret);
Heinrich Schuchardt7eaa37b2021-11-15 19:06:55 +0100371 ret = snprintf(buf, 4, "%s:%s", "abc", "def");
372 ut_asserteq(0, buf[3]);
373 ut_asserteq(7, ret);
374 ret = snprintf(buf, 4, "%s:%d", "abc", 9999);
375 ut_asserteq(8, ret);
376 return 0;
377}
Simon Glass3394c3c2024-11-02 13:36:56 -0600378COMMON_TEST(snprint, 0);