blob: 76ee851fe6a00f529b8ddc3ab7a05865273c6a3c [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);
Patrick Delaunay5d2451d2025-06-16 13:27:47 +020048 if (IS_ENABLED(CONFIG_EFI_PARTITION))
Jerome Forissier0664eec2025-04-16 09:48:20 +020049 ut_asserteq_str("EFI System Partition", str);
50 else
Heinrich Schuchardtd2e0aa32022-01-16 13:22:06 +010051 ut_asserteq_str("c12a7328-f81f-11d2-ba4b-00a0c93ec93b", str);
Heinrich Schuchardt7eaa37b2021-11-15 19:06:55 +010052 ret = snprintf(str, 4, "%pUL", guid);
53 ut_asserteq(0, str[3]);
54 ut_asserteq(36, ret);
Simon Glassd2423682021-05-08 06:59:58 -060055
56 return 0;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020057}
Simon Glass3394c3c2024-11-02 13:36:56 -060058COMMON_TEST(print_guid, 0);
Simon Glassd2423682021-05-08 06:59:58 -060059#endif
Heinrich Schuchardt6f035662019-04-29 08:08:43 +020060
Simon Glassd2423682021-05-08 06:59:58 -060061#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010062/* Test efi_loader specific printing */
Simon Glassd2423682021-05-08 06:59:58 -060063static int print_efi_ut(struct unit_test_state *uts)
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010064{
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010065 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);
Simon Glassd2423682021-05-08 06:59:58 -060086 ut_assertok(strcmp("_/SD(3)_", str));
Heinrich Schuchardt3b47f332018-01-26 06:30:30 +010087
88 /* NULL device path */
89 snprintf(str, sizeof(str), "_%pD_", NULL);
Simon Glassd2423682021-05-08 06:59:58 -060090 ut_assertok(strcmp("_<NULL>_", str));
91
92 return 0;
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010093}
Simon Glass3394c3c2024-11-02 13:36:56 -060094COMMON_TEST(print_efi_ut, 0);
Simon Glassd2423682021-05-08 06:59:58 -060095#endif
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010096
Simon Glassd2423682021-05-08 06:59:58 -060097static int print_printf(struct unit_test_state *uts)
Simon Glass75ee32f2017-06-15 21:37:51 -060098{
99 char big_str[400];
100 int big_str_len;
101 char str[10], *s;
102 int len;
103
Simon Glass75ee32f2017-06-15 21:37:51 -0600104 snprintf(str, sizeof(str), "testing");
Simon Glassd2423682021-05-08 06:59:58 -0600105 ut_assertok(strcmp("testing", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600106
107 snprintf(str, sizeof(str), "testing but too long");
Simon Glassd2423682021-05-08 06:59:58 -0600108 ut_assertok(strcmp("testing b", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600109
110 snprintf(str, 1, "testing none");
Simon Glassd2423682021-05-08 06:59:58 -0600111 ut_assertok(strcmp("", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600112
113 *str = 'x';
114 snprintf(str, 0, "testing none");
Simon Glassd2423682021-05-08 06:59:58 -0600115 ut_asserteq('x', *str);
Simon Glass75ee32f2017-06-15 21:37:51 -0600116
Heinrich Schuchardt3eb9d262024-11-03 21:46:42 +0100117 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 }
Rob Clark22ae0a72017-09-13 18:46:54 -0400121
Simon Glass75ee32f2017-06-15 21:37:51 -0600122 /* Test the banner function */
123 s = display_options_get_banner(true, str, sizeof(str));
Simon Glassd2423682021-05-08 06:59:58 -0600124 ut_asserteq_ptr(str, s);
125 ut_assertok(strcmp("\n\nU-Boo\n\n", s));
Simon Glass75ee32f2017-06-15 21:37:51 -0600126
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +0200127 /* Assert that we do not overwrite memory before the buffer */
128 str[0] = '`';
129 s = display_options_get_banner(true, str + 1, 1);
Simon Glassd2423682021-05-08 06:59:58 -0600130 ut_asserteq_ptr(str + 1, s);
131 ut_assertok(strcmp("`", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600132
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +0200133 str[0] = '~';
134 s = display_options_get_banner(true, str + 1, 2);
Simon Glassd2423682021-05-08 06:59:58 -0600135 ut_asserteq_ptr(str + 1, s);
136 ut_assertok(strcmp("~\n", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600137
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +0200138 /* The last two characters are set to \n\n for all buffer sizes > 2 */
Simon Glass75ee32f2017-06-15 21:37:51 -0600139 s = display_options_get_banner(false, str, sizeof(str));
Simon Glassd2423682021-05-08 06:59:58 -0600140 ut_asserteq_ptr(str, s);
141 ut_assertok(strcmp("U-Boot \n\n", s));
Simon Glass75ee32f2017-06-15 21:37:51 -0600142
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);
Simon Glassd2423682021-05-08 06:59:58 -0600147 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));
Simon Glass75ee32f2017-06-15 21:37:51 -0600150
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);
Simon Glassd2423682021-05-08 06:59:58 -0600155 ut_asserteq_ptr(big_str, s);
Simon Glass75ee32f2017-06-15 21:37:51 -0600156 len = strlen(version_string);
Simon Glassd2423682021-05-08 06:59:58 -0600157 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));
Simon Glass75ee32f2017-06-15 21:37:51 -0600161
Simon Glass75ee32f2017-06-15 21:37:51 -0600162 return 0;
163}
Simon Glass3394c3c2024-11-02 13:36:56 -0600164COMMON_TEST(print_printf, 0);
Simon Glass75ee32f2017-06-15 21:37:51 -0600165
Simon Glass4a249ba2021-05-08 06:59:59 -0600166static int print_display_buffer(struct unit_test_state *uts)
167{
168 u8 *buf;
169 int i;
170
Simon Glassa7dd66f2023-10-01 19:15:22 -0600171 /* This test requires writable memory at zero */
172 if (IS_ENABLED(CONFIG_X86))
173 return -EAGAIN;
174
Simon Glass4a249ba2021-05-08 06:59:59 -0600175 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 */
Simon Glass4a249ba2021-05-08 06:59:59 -0600181 print_buffer(0, buf, 1, 0x12, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600182 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 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600184 ut_assert_console_end();
185
186 /* line length */
Simon Glass4a249ba2021-05-08 06:59:59 -0600187 print_buffer(0, buf, 1, 0x12, 8);
Simon Glass85c8fc52021-05-08 07:00:00 -0600188 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 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600191 ut_assert_console_end();
192
193 /* long line */
Simon Glass4a249ba2021-05-08 06:59:59 -0600194 buf[0x41] = 0x41;
195 print_buffer(0, buf, 1, 0x42, 0x40);
Simon Glass85c8fc52021-05-08 07:00:00 -0600196 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");
Simon Glass4a249ba2021-05-08 06:59:59 -0600198 ut_assert_console_end();
199
200 /* address */
Simon Glass4a249ba2021-05-08 06:59:59 -0600201 print_buffer(0x12345678, buf, 1, 0x12, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600202 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 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600204 ut_assert_console_end();
205
206 /* 16-bit */
Simon Glass4a249ba2021-05-08 06:59:59 -0600207 print_buffer(0, buf, 2, 9, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600208 ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
209 ut_assert_nextline("00000010: 0010 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600210 ut_assert_console_end();
211
212 /* 32-bit */
Simon Glass4a249ba2021-05-08 06:59:59 -0600213 print_buffer(0, buf, 4, 5, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600214 ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
215 ut_assert_nextline("00000010: 00000010 ....");
Simon Glass4a249ba2021-05-08 06:59:59 -0600216 ut_assert_console_end();
217
218 /* 64-bit */
Simon Glass4a249ba2021-05-08 06:59:59 -0600219 print_buffer(0, buf, 8, 3, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600220 ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
221 ut_assert_nextline("00000010: 0000000000000010 ........");
Simon Glass4a249ba2021-05-08 06:59:59 -0600222 ut_assert_console_end();
223
224 /* ASCII */
Simon Glass4a249ba2021-05-08 06:59:59 -0600225 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);
Simon Glass85c8fc52021-05-08 07:00:00 -0600232 ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
Simon Glass4a249ba2021-05-08 06:59:59 -0600233 ut_assert_console_end();
234
235 unmap_sysmem(buf);
236
237 return 0;
238}
Simon Glass3394c3c2024-11-02 13:36:56 -0600239COMMON_TEST(print_display_buffer, UTF_CONSOLE);
Simon Glass4a249ba2021-05-08 06:59:59 -0600240
Simon Glass02b8f1a2021-05-08 07:00:05 -0600241static int print_hexdump_line(struct unit_test_state *uts)
242{
Simon Glass537664c2024-11-27 11:17:20 -0600243 u8 *linebuf;
Simon Glass02b8f1a2021-05-08 07:00:05 -0600244 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));
Simon Glass537664c2024-11-27 11:17:20 -0600256 ut_asserteq(0xff, linebuf[0]);
Simon Glass02b8f1a2021-05-08 07:00:05 -0600257 ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76));
Simon Glass537664c2024-11-27 11:17:20 -0600258 ut_asserteq('\0', linebuf[75]);
259 ut_asserteq(0xff, linebuf[76]);
Simon Glass02b8f1a2021-05-08 07:00:05 -0600260
261 unmap_sysmem(buf);
262
263 return 0;
264}
Simon Glass3394c3c2024-11-02 13:36:56 -0600265COMMON_TEST(print_hexdump_line, UTF_CONSOLE);
Simon Glass02b8f1a2021-05-08 07:00:05 -0600266
Simon Glass26507152021-05-08 07:00:02 -0600267static int print_do_hex_dump(struct unit_test_state *uts)
268{
269 u8 *buf;
270 int i;
271
Simon Glassa7dd66f2023-10-01 19:15:22 -0600272 /* This test requires writable memory at zero */
273 if (IS_ENABLED(CONFIG_X86))
274 return -EAGAIN;
275
Simon Glass26507152021-05-08 07:00:02 -0600276 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 */
Simon Glass26507152021-05-08 07:00:02 -0600282 print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200283 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);
Simon Glass26507152021-05-08 07:00:02 -0600287 ut_assert_console_end();
288
Simon Glassf1d06472021-05-08 07:00:03 -0600289 /* line length */
Simon Glassf1d06472021-05-08 07:00:03 -0600290 print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200291 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);
Simon Glassf1d06472021-05-08 07:00:03 -0600297 ut_assert_console_end();
298 unmap_sysmem(buf);
299
300 /* long line */
Simon Glassf1d06472021-05-08 07:00:03 -0600301 buf[0x41] = 0x41;
302 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200303 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);
Simon Glassf1d06472021-05-08 07:00:03 -0600307 ut_assert_console_end();
308
Simon Glass26507152021-05-08 07:00:02 -0600309 /* 16-bit */
Simon Glassf1d06472021-05-08 07:00:03 -0600310 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200311 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);
Simon Glass26507152021-05-08 07:00:02 -0600315 ut_assert_console_end();
316 unmap_sysmem(buf);
317
318 /* 32-bit */
Simon Glassf1d06472021-05-08 07:00:03 -0600319 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200320 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);
Simon Glass26507152021-05-08 07:00:02 -0600324 ut_assert_console_end();
325 unmap_sysmem(buf);
326
327 /* 64-bit */
Simon Glass26507152021-05-08 07:00:02 -0600328 print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200329 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);
Simon Glass26507152021-05-08 07:00:02 -0600333 ut_assert_console_end();
334 unmap_sysmem(buf);
335
336 /* ASCII */
Simon Glass26507152021-05-08 07:00:02 -0600337 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;
Simon Glassf1d06472021-05-08 07:00:03 -0600343 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200344 ut_assert_nextline("%0*lx: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....",
345 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
Simon Glass26507152021-05-08 07:00:02 -0600346 ut_assert_console_end();
347 unmap_sysmem(buf);
348
349 return 0;
350}
Simon Glass3394c3c2024-11-02 13:36:56 -0600351COMMON_TEST(print_do_hex_dump, UTF_CONSOLE);
Simon Glass26507152021-05-08 07:00:02 -0600352
Heinrich Schuchardt7eaa37b2021-11-15 19:06:55 +0100353static int snprint(struct unit_test_state *uts)
354{
355 char buf[10] = "xxxxxxxxx";
356 int ret;
357
Heinrich Schuchardt2df4f952022-01-29 16:33:16 +0100358 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);
Heinrich Schuchardt7eaa37b2021-11-15 19:06:55 +0100370 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}
Simon Glass3394c3c2024-11-02 13:36:56 -0600377COMMON_TEST(snprint, 0);