lib: Add a function to convert a string to a hex value
[platform/kernel/u-boot.git] / test / print_ut.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2012, The Chromium Authors
4  */
5
6 #include <common.h>
7 #include <command.h>
8 #include <efi_api.h>
9 #include <display_options.h>
10 #include <log.h>
11 #include <mapmem.h>
12 #include <version_string.h>
13 #include <vsprintf.h>
14 #include <test/suites.h>
15 #include <test/test.h>
16 #include <test/ut.h>
17
18 #define BUF_SIZE        0x100
19
20 #define FAKE_BUILD_TAG  "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
21                         "and a lot more text to come"
22
23 /* Declare a new print test */
24 #define PRINT_TEST(_name, _flags)       UNIT_TEST(_name, _flags, print_test)
25
26 #if CONFIG_IS_ENABLED(LIB_UUID)
27 /* Test printing GUIDs */
28 static int print_guid(struct unit_test_state *uts)
29 {
30         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);
36         ut_assertok(strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str));
37         sprintf(str, "%pUB", guid);
38         ut_assertok(strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str));
39         sprintf(str, "%pUl", guid);
40         ut_assertok(strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str));
41         sprintf(str, "%pUL", guid);
42         ut_assertok(strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str));
43
44         return 0;
45 }
46 PRINT_TEST(print_guid, 0);
47 #endif
48
49 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
50 /* Test efi_loader specific printing */
51 static int print_efi_ut(struct unit_test_state *uts)
52 {
53         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);
74         ut_assertok(strcmp("_/SD(3)_", str));
75
76         /* NULL device path */
77         snprintf(str, sizeof(str), "_%pD_", NULL);
78         ut_assertok(strcmp("_<NULL>_", str));
79
80         return 0;
81 }
82 PRINT_TEST(print_efi_ut, 0);
83 #endif
84
85 static int print_printf(struct unit_test_state *uts)
86 {
87         char big_str[400];
88         int big_str_len;
89         char str[10], *s;
90         int len;
91
92         snprintf(str, sizeof(str), "testing");
93         ut_assertok(strcmp("testing", str));
94
95         snprintf(str, sizeof(str), "testing but too long");
96         ut_assertok(strcmp("testing b", str));
97
98         snprintf(str, 1, "testing none");
99         ut_assertok(strcmp("", str));
100
101         *str = 'x';
102         snprintf(str, 0, "testing none");
103         ut_asserteq('x', *str);
104
105         sprintf(big_str, "_%ls_", L"foo");
106         ut_assertok(strcmp("_foo_", big_str));
107
108         /* Test the banner function */
109         s = display_options_get_banner(true, str, sizeof(str));
110         ut_asserteq_ptr(str, s);
111         ut_assertok(strcmp("\n\nU-Boo\n\n", s));
112
113         /* Assert that we do not overwrite memory before the buffer */
114         str[0] = '`';
115         s = display_options_get_banner(true, str + 1, 1);
116         ut_asserteq_ptr(str + 1, s);
117         ut_assertok(strcmp("`", str));
118
119         str[0] = '~';
120         s = display_options_get_banner(true, str + 1, 2);
121         ut_asserteq_ptr(str + 1, s);
122         ut_assertok(strcmp("~\n", str));
123
124         /* The last two characters are set to \n\n for all buffer sizes > 2 */
125         s = display_options_get_banner(false, str, sizeof(str));
126         ut_asserteq_ptr(str, s);
127         ut_assertok(strcmp("U-Boot \n\n", s));
128
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);
133         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));
136
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);
141         ut_asserteq_ptr(big_str, s);
142         len = strlen(version_string);
143         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));
147
148         return 0;
149 }
150 PRINT_TEST(print_printf, 0);
151
152 static 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);
165         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                                            ..");
167         ut_assert_console_end();
168
169         /* line length */
170         console_record_reset();
171         print_buffer(0, buf, 1, 0x12, 8);
172         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                    ..");
175         ut_assert_console_end();
176
177         /* long line */
178         console_record_reset();
179         buf[0x41] = 0x41;
180         print_buffer(0, buf, 1, 0x42, 0x40);
181         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");
183         ut_assert_console_end();
184
185         /* address */
186         console_record_reset();
187         print_buffer(0x12345678, buf, 1, 0x12, 0);
188         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                                            ..");
190         ut_assert_console_end();
191
192         /* 16-bit */
193         console_record_reset();
194         print_buffer(0, buf, 2, 9, 0);
195         ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee  ..\"3DUfw........");
196         ut_assert_nextline("00000010: 0010                                     ..");
197         ut_assert_console_end();
198
199         /* 32-bit */
200         console_record_reset();
201         print_buffer(0, buf, 4, 5, 0);
202         ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc  ..\"3DUfw........");
203         ut_assert_nextline("00000010: 00000010                             ....");
204         ut_assert_console_end();
205
206         /* 64-bit */
207         console_record_reset();
208         print_buffer(0, buf, 8, 3, 0);
209         ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988  ..\"3DUfw........");
210         ut_assert_nextline("00000010: 0000000000000010                   ........");
211         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);
222         ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99                    .. !~.....");
223         ut_assert_console_end();
224
225         unmap_sysmem(buf);
226
227         return 0;
228 }
229 PRINT_TEST(print_display_buffer, UT_TESTF_CONSOLE_REC);
230
231 static 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 }
255 PRINT_TEST(print_hexdump_line, UT_TESTF_CONSOLE_REC);
256
257 static 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
274         /* 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
291         /* 16-bit */
292         console_record_reset();
293         print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
294         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();
301         print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
302         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;
323         print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
324         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 }
330 PRINT_TEST(print_do_hex_dump, UT_TESTF_CONSOLE_REC);
331
332 static 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
339         /* Use #ifdef here to avoid a compiler warning on 32-bit machines */
340 #ifdef CONFIG_PHYS_64BIT
341         if (sizeof(ulong) == 8) {
342                 ut_asserteq_str("9223372036854775807",
343                                 simple_itoa((1UL << 63) - 1));
344                 ut_asserteq_str("18446744073709551615", simple_itoa(-1));
345         }
346 #endif /* CONFIG_PHYS_64BIT */
347
348         return 0;
349 }
350 PRINT_TEST(print_itoa, 0);
351
352 static int print_xtoa(struct unit_test_state *uts)
353 {
354         ut_asserteq_str("7f", simple_xtoa(127));
355         ut_asserteq_str("00", simple_xtoa(0));
356         ut_asserteq_str("7fffffff", simple_xtoa(0x7fffffff));
357         ut_asserteq_str("ffffffff", simple_xtoa(0xffffffff));
358
359         /* Use #ifdef here to avoid a compiler warning on 32-bit machines */
360 #ifdef CONFIG_PHYS_64BIT
361         if (sizeof(ulong) == 8) {
362                 ut_asserteq_str("7fffffffffffffff",
363                                 simple_xtoa((1UL << 63) - 1));
364                 ut_asserteq_str("ffffffffffffffff", simple_xtoa(-1));
365         }
366 #endif /* CONFIG_PHYS_64BIT */
367
368         return 0;
369 }
370 PRINT_TEST(print_xtoa, 0);
371
372 int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
373 {
374         struct unit_test *tests = UNIT_TEST_SUITE_START(print_test);
375         const int n_ents = UNIT_TEST_SUITE_COUNT(print_test);
376
377         return cmd_ut_category("print", "print_", tests, n_ents, argc, argv);
378 }