blob: f1eb9072d97a6c1b2995d1298fd3b2542df1c23f [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
Simon Glass276af332022-01-23 12:55:14 -0700118 sprintf(big_str, "_%ls_", u"foo");
Simon Glassd2423682021-05-08 06:59:58 -0600119 ut_assertok(strcmp("_foo_", big_str));
Rob Clark22ae0a72017-09-13 18:46:54 -0400120
Simon Glass75ee32f2017-06-15 21:37:51 -0600121 /* Test the banner function */
122 s = display_options_get_banner(true, str, sizeof(str));
Simon Glassd2423682021-05-08 06:59:58 -0600123 ut_asserteq_ptr(str, s);
124 ut_assertok(strcmp("\n\nU-Boo\n\n", s));
Simon Glass75ee32f2017-06-15 21:37:51 -0600125
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +0200126 /* Assert that we do not overwrite memory before the buffer */
127 str[0] = '`';
128 s = display_options_get_banner(true, str + 1, 1);
Simon Glassd2423682021-05-08 06:59:58 -0600129 ut_asserteq_ptr(str + 1, s);
130 ut_assertok(strcmp("`", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600131
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +0200132 str[0] = '~';
133 s = display_options_get_banner(true, str + 1, 2);
Simon Glassd2423682021-05-08 06:59:58 -0600134 ut_asserteq_ptr(str + 1, s);
135 ut_assertok(strcmp("~\n", str));
Simon Glass75ee32f2017-06-15 21:37:51 -0600136
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +0200137 /* The last two characters are set to \n\n for all buffer sizes > 2 */
Simon Glass75ee32f2017-06-15 21:37:51 -0600138 s = display_options_get_banner(false, str, sizeof(str));
Simon Glassd2423682021-05-08 06:59:58 -0600139 ut_asserteq_ptr(str, s);
140 ut_assertok(strcmp("U-Boot \n\n", s));
Simon Glass75ee32f2017-06-15 21:37:51 -0600141
142 /* Give it enough space for some of the version */
143 big_str_len = strlen(version_string) - 5;
144 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
145 big_str_len);
Simon Glassd2423682021-05-08 06:59:58 -0600146 ut_asserteq_ptr(big_str, s);
147 ut_assertok(strncmp(version_string, s, big_str_len - 3));
148 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
Simon Glass75ee32f2017-06-15 21:37:51 -0600149
150 /* Give it enough space for the version and some of the build tag */
151 big_str_len = strlen(version_string) + 9 + 20;
152 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
153 big_str_len);
Simon Glassd2423682021-05-08 06:59:58 -0600154 ut_asserteq_ptr(big_str, s);
Simon Glass75ee32f2017-06-15 21:37:51 -0600155 len = strlen(version_string);
Simon Glassd2423682021-05-08 06:59:58 -0600156 ut_assertok(strncmp(version_string, s, len));
157 ut_assertok(strncmp(", Build: ", s + len, 9));
158 ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
159 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
Simon Glass75ee32f2017-06-15 21:37:51 -0600160
Simon Glass75ee32f2017-06-15 21:37:51 -0600161 return 0;
162}
Simon Glass3394c3c2024-11-02 13:36:56 -0600163COMMON_TEST(print_printf, 0);
Simon Glass75ee32f2017-06-15 21:37:51 -0600164
Simon Glass4a249ba2021-05-08 06:59:59 -0600165static int print_display_buffer(struct unit_test_state *uts)
166{
167 u8 *buf;
168 int i;
169
Simon Glassa7dd66f2023-10-01 19:15:22 -0600170 /* This test requires writable memory at zero */
171 if (IS_ENABLED(CONFIG_X86))
172 return -EAGAIN;
173
Simon Glass4a249ba2021-05-08 06:59:59 -0600174 buf = map_sysmem(0, BUF_SIZE);
175 memset(buf, '\0', BUF_SIZE);
176 for (i = 0; i < 0x11; i++)
177 buf[i] = i * 0x11;
178
179 /* bytes */
Simon Glass4a249ba2021-05-08 06:59:59 -0600180 print_buffer(0, buf, 1, 0x12, 0);
Simon Glass85c8fc52021-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 ..\"3DUfw........");
182 ut_assert_nextline("00000010: 10 00 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600183 ut_assert_console_end();
184
185 /* line length */
Simon Glass4a249ba2021-05-08 06:59:59 -0600186 print_buffer(0, buf, 1, 0x12, 8);
Simon Glass85c8fc52021-05-08 07:00:00 -0600187 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw");
188 ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........");
189 ut_assert_nextline("00000010: 10 00 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600190 ut_assert_console_end();
191
192 /* long line */
Simon Glass4a249ba2021-05-08 06:59:59 -0600193 buf[0x41] = 0x41;
194 print_buffer(0, buf, 1, 0x42, 0x40);
Simon Glass85c8fc52021-05-08 07:00:00 -0600195 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........................................................");
196 ut_assert_nextline("00000040: 00 41 .A");
Simon Glass4a249ba2021-05-08 06:59:59 -0600197 ut_assert_console_end();
198
199 /* address */
Simon Glass4a249ba2021-05-08 06:59:59 -0600200 print_buffer(0x12345678, buf, 1, 0x12, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600201 ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
202 ut_assert_nextline("12345688: 10 00 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600203 ut_assert_console_end();
204
205 /* 16-bit */
Simon Glass4a249ba2021-05-08 06:59:59 -0600206 print_buffer(0, buf, 2, 9, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600207 ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
208 ut_assert_nextline("00000010: 0010 ..");
Simon Glass4a249ba2021-05-08 06:59:59 -0600209 ut_assert_console_end();
210
211 /* 32-bit */
Simon Glass4a249ba2021-05-08 06:59:59 -0600212 print_buffer(0, buf, 4, 5, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600213 ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
214 ut_assert_nextline("00000010: 00000010 ....");
Simon Glass4a249ba2021-05-08 06:59:59 -0600215 ut_assert_console_end();
216
217 /* 64-bit */
Simon Glass4a249ba2021-05-08 06:59:59 -0600218 print_buffer(0, buf, 8, 3, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600219 ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
220 ut_assert_nextline("00000010: 0000000000000010 ........");
Simon Glass4a249ba2021-05-08 06:59:59 -0600221 ut_assert_console_end();
222
223 /* ASCII */
Simon Glass4a249ba2021-05-08 06:59:59 -0600224 buf[1] = 31;
225 buf[2] = 32;
226 buf[3] = 33;
227 for (i = 0; i < 4; i++)
228 buf[4 + i] = 126 + i;
229 buf[8] = 255;
230 print_buffer(0, buf, 1, 10, 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600231 ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
Simon Glass4a249ba2021-05-08 06:59:59 -0600232 ut_assert_console_end();
233
234 unmap_sysmem(buf);
235
236 return 0;
237}
Simon Glass3394c3c2024-11-02 13:36:56 -0600238COMMON_TEST(print_display_buffer, UTF_CONSOLE);
Simon Glass4a249ba2021-05-08 06:59:59 -0600239
Simon Glass02b8f1a2021-05-08 07:00:05 -0600240static int print_hexdump_line(struct unit_test_state *uts)
241{
242 char *linebuf;
243 u8 *buf;
244 int i;
245
246 buf = map_sysmem(0, BUF_SIZE);
247 memset(buf, '\0', BUF_SIZE);
248 for (i = 0; i < 0x11; i++)
249 buf[i] = i * 0x11;
250
251 /* Check buffer size calculations */
252 linebuf = map_sysmem(0x400, BUF_SIZE);
253 memset(linebuf, '\xff', BUF_SIZE);
254 ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75));
255 ut_asserteq(-1, linebuf[0]);
256 ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76));
257 ut_asserteq(0, linebuf[75]);
258 ut_asserteq(-1, linebuf[76]);
259
260 unmap_sysmem(buf);
261
262 return 0;
263}
Simon Glass3394c3c2024-11-02 13:36:56 -0600264COMMON_TEST(print_hexdump_line, UTF_CONSOLE);
Simon Glass02b8f1a2021-05-08 07:00:05 -0600265
Simon Glass26507152021-05-08 07:00:02 -0600266static int print_do_hex_dump(struct unit_test_state *uts)
267{
268 u8 *buf;
269 int i;
270
Simon Glassa7dd66f2023-10-01 19:15:22 -0600271 /* This test requires writable memory at zero */
272 if (IS_ENABLED(CONFIG_X86))
273 return -EAGAIN;
274
Simon Glass26507152021-05-08 07:00:02 -0600275 buf = map_sysmem(0, BUF_SIZE);
276 memset(buf, '\0', BUF_SIZE);
277 for (i = 0; i < 0x11; i++)
278 buf[i] = i * 0x11;
279
280 /* bytes */
Simon Glass26507152021-05-08 07:00:02 -0600281 print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200282 ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........",
283 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
284 ut_assert_nextline("%0*lx: 10 00 ..",
285 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass26507152021-05-08 07:00:02 -0600286 ut_assert_console_end();
287
Simon Glassf1d06472021-05-08 07:00:03 -0600288 /* line length */
Simon Glassf1d06472021-05-08 07:00:03 -0600289 print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200290 ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 ..\"3DUfw",
291 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
292 ut_assert_nextline("%0*lx: 88 99 aa bb cc dd ee ff ........",
293 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x8UL);
294 ut_assert_nextline("%0*lx: 10 00 ..",
295 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glassf1d06472021-05-08 07:00:03 -0600296 ut_assert_console_end();
297 unmap_sysmem(buf);
298
299 /* long line */
Simon Glassf1d06472021-05-08 07:00:03 -0600300 buf[0x41] = 0x41;
301 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200302 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........................................................",
303 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
304 ut_assert_nextline("%0*lx: 00 41 .A",
305 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x40UL);
Simon Glassf1d06472021-05-08 07:00:03 -0600306 ut_assert_console_end();
307
Simon Glass26507152021-05-08 07:00:02 -0600308 /* 16-bit */
Simon Glassf1d06472021-05-08 07:00:03 -0600309 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200310 ut_assert_nextline("%0*lx: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........",
311 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
312 ut_assert_nextline("%0*lx: 0010 ..",
313 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass26507152021-05-08 07:00:02 -0600314 ut_assert_console_end();
315 unmap_sysmem(buf);
316
317 /* 32-bit */
Simon Glassf1d06472021-05-08 07:00:03 -0600318 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200319 ut_assert_nextline("%0*lx: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........",
320 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
321 ut_assert_nextline("%0*lx: 00000010 ....",
322 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass26507152021-05-08 07:00:02 -0600323 ut_assert_console_end();
324 unmap_sysmem(buf);
325
326 /* 64-bit */
Simon Glass26507152021-05-08 07:00:02 -0600327 print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200328 ut_assert_nextline("%0*lx: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........",
329 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
330 ut_assert_nextline("%0*lx: 0000000000000010 ........",
331 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL);
Simon Glass26507152021-05-08 07:00:02 -0600332 ut_assert_console_end();
333 unmap_sysmem(buf);
334
335 /* ASCII */
Simon Glass26507152021-05-08 07:00:02 -0600336 buf[1] = 31;
337 buf[2] = 32;
338 buf[3] = 33;
339 for (i = 0; i < 4; i++)
340 buf[4 + i] = 126 + i;
341 buf[8] = 255;
Simon Glassf1d06472021-05-08 07:00:03 -0600342 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
Marek Vasut6bb14de2023-08-25 10:19:40 +0200343 ut_assert_nextline("%0*lx: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....",
344 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL);
Simon Glass26507152021-05-08 07:00:02 -0600345 ut_assert_console_end();
346 unmap_sysmem(buf);
347
348 return 0;
349}
Simon Glass3394c3c2024-11-02 13:36:56 -0600350COMMON_TEST(print_do_hex_dump, UTF_CONSOLE);
Simon Glass26507152021-05-08 07:00:02 -0600351
Heinrich Schuchardt7eaa37b2021-11-15 19:06:55 +0100352static int snprint(struct unit_test_state *uts)
353{
354 char buf[10] = "xxxxxxxxx";
355 int ret;
356
Heinrich Schuchardt2df4f952022-01-29 16:33:16 +0100357 ret = snprintf(buf, 5, "%d", 12345678);
358 ut_asserteq_str("1234", buf);
359 ut_asserteq(8, ret);
360 ret = snprintf(buf, 5, "0x%x", 0x1234);
361 ut_asserteq_str("0x12", buf);
362 ut_asserteq(6, ret);
363 ret = snprintf(buf, 5, "0x%08x", 0x1234);
364 ut_asserteq_str("0x00", buf);
365 ut_asserteq(10, ret);
366 ret = snprintf(buf, 3, "%s", "abc");
367 ut_asserteq_str("ab", buf);
368 ut_asserteq(3, ret);
Heinrich Schuchardt7eaa37b2021-11-15 19:06:55 +0100369 ret = snprintf(buf, 4, "%s:%s", "abc", "def");
370 ut_asserteq(0, buf[3]);
371 ut_asserteq(7, ret);
372 ret = snprintf(buf, 4, "%s:%d", "abc", 9999);
373 ut_asserteq(8, ret);
374 return 0;
375}
Simon Glass3394c3c2024-11-02 13:36:56 -0600376COMMON_TEST(snprint, 0);