Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, The Chromium Authors |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 6 | #include <command.h> |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 7 | #include <efi_api.h> |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 8 | #include <display_options.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 10 | #include <mapmem.h> |
Pali Rohár | ba87ddf | 2021-08-02 15:18:31 +0200 | [diff] [blame] | 11 | #include <version_string.h> |
Simon Glass | 978739d | 2021-10-14 12:48:06 -0600 | [diff] [blame] | 12 | #include <vsprintf.h> |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 13 | #include <test/suites.h> |
| 14 | #include <test/test.h> |
| 15 | #include <test/ut.h> |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 16 | |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 17 | #define BUF_SIZE 0x100 |
| 18 | |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 19 | #define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \ |
| 20 | "and a lot more text to come" |
| 21 | |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 22 | /* Declare a new print test */ |
| 23 | #define PRINT_TEST(_name, _flags) UNIT_TEST(_name, _flags, print_test) |
| 24 | |
| 25 | #if CONFIG_IS_ENABLED(LIB_UUID) |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 26 | /* Test printing GUIDs */ |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 27 | static int print_guid(struct unit_test_state *uts) |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 28 | { |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 29 | unsigned char guid[16] = { |
| 30 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 |
| 31 | }; |
Heinrich Schuchardt | d2e0aa3 | 2022-01-16 13:22:06 +0100 | [diff] [blame] | 32 | unsigned char guid_esp[16] = { |
| 33 | 0x28, 0x73, 0x2a, 0xc1, 0x1f, 0xf8, 0xd2, 0x11, |
| 34 | 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B |
| 35 | }; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 36 | char str[40]; |
Heinrich Schuchardt | 7eaa37b | 2021-11-15 19:06:55 +0100 | [diff] [blame] | 37 | int ret; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 38 | |
| 39 | sprintf(str, "%pUb", guid); |
Heinrich Schuchardt | d2e0aa3 | 2022-01-16 13:22:06 +0100 | [diff] [blame] | 40 | ut_asserteq_str("01020304-0506-0708-090a-0b0c0d0e0f10", str); |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 41 | sprintf(str, "%pUB", guid); |
Heinrich Schuchardt | d2e0aa3 | 2022-01-16 13:22:06 +0100 | [diff] [blame] | 42 | ut_asserteq_str("01020304-0506-0708-090A-0B0C0D0E0F10", str); |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 43 | sprintf(str, "%pUl", guid); |
Heinrich Schuchardt | d2e0aa3 | 2022-01-16 13:22:06 +0100 | [diff] [blame] | 44 | ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str); |
| 45 | sprintf(str, "%pUs", guid); |
| 46 | ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str); |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 47 | sprintf(str, "%pUL", guid); |
Heinrich Schuchardt | d2e0aa3 | 2022-01-16 13:22:06 +0100 | [diff] [blame] | 48 | ut_asserteq_str("04030201-0605-0807-090A-0B0C0D0E0F10", str); |
| 49 | sprintf(str, "%pUs", guid_esp); |
| 50 | if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) { /* brace needed */ |
| 51 | ut_asserteq_str("system", str); |
| 52 | } else { |
| 53 | ut_asserteq_str("c12a7328-f81f-11d2-ba4b-00a0c93ec93b", str); |
| 54 | } |
Heinrich Schuchardt | 7eaa37b | 2021-11-15 19:06:55 +0100 | [diff] [blame] | 55 | ret = snprintf(str, 4, "%pUL", guid); |
| 56 | ut_asserteq(0, str[3]); |
| 57 | ut_asserteq(36, ret); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 58 | |
| 59 | return 0; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 60 | } |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 61 | PRINT_TEST(print_guid, 0); |
| 62 | #endif |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 63 | |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 64 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 65 | /* Test efi_loader specific printing */ |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 66 | static int print_efi_ut(struct unit_test_state *uts) |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 67 | { |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 68 | char str[10]; |
| 69 | u8 buf[sizeof(struct efi_device_path_sd_mmc_path) + |
| 70 | sizeof(struct efi_device_path)]; |
| 71 | u8 *pos = buf; |
| 72 | struct efi_device_path *dp_end; |
| 73 | struct efi_device_path_sd_mmc_path *dp_sd = |
| 74 | (struct efi_device_path_sd_mmc_path *)pos; |
| 75 | |
| 76 | /* Create a device path for an SD card */ |
| 77 | dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 78 | dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD; |
| 79 | dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path); |
| 80 | dp_sd->slot_number = 3; |
| 81 | pos += sizeof(struct efi_device_path_sd_mmc_path); |
| 82 | /* Append end node */ |
| 83 | dp_end = (struct efi_device_path *)pos; |
| 84 | dp_end->type = DEVICE_PATH_TYPE_END; |
| 85 | dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 86 | dp_end->length = sizeof(struct efi_device_path); |
| 87 | |
| 88 | snprintf(str, sizeof(str), "_%pD_", buf); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 89 | ut_assertok(strcmp("_/SD(3)_", str)); |
Heinrich Schuchardt | 3b47f33 | 2018-01-26 06:30:30 +0100 | [diff] [blame] | 90 | |
| 91 | /* NULL device path */ |
| 92 | snprintf(str, sizeof(str), "_%pD_", NULL); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 93 | ut_assertok(strcmp("_<NULL>_", str)); |
| 94 | |
| 95 | return 0; |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 96 | } |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 97 | PRINT_TEST(print_efi_ut, 0); |
| 98 | #endif |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 99 | |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 100 | static int print_printf(struct unit_test_state *uts) |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 101 | { |
| 102 | char big_str[400]; |
| 103 | int big_str_len; |
| 104 | char str[10], *s; |
| 105 | int len; |
| 106 | |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 107 | snprintf(str, sizeof(str), "testing"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 108 | ut_assertok(strcmp("testing", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 109 | |
| 110 | snprintf(str, sizeof(str), "testing but too long"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 111 | ut_assertok(strcmp("testing b", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 112 | |
| 113 | snprintf(str, 1, "testing none"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 114 | ut_assertok(strcmp("", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 115 | |
| 116 | *str = 'x'; |
| 117 | snprintf(str, 0, "testing none"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 118 | ut_asserteq('x', *str); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 119 | |
Simon Glass | 276af33 | 2022-01-23 12:55:14 -0700 | [diff] [blame] | 120 | sprintf(big_str, "_%ls_", u"foo"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 121 | ut_assertok(strcmp("_foo_", big_str)); |
Rob Clark | 22ae0a7 | 2017-09-13 18:46:54 -0400 | [diff] [blame] | 122 | |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 123 | /* Test the banner function */ |
| 124 | s = display_options_get_banner(true, str, sizeof(str)); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 125 | ut_asserteq_ptr(str, s); |
| 126 | ut_assertok(strcmp("\n\nU-Boo\n\n", s)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 127 | |
Heinrich Schuchardt | b144ab1 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 128 | /* Assert that we do not overwrite memory before the buffer */ |
| 129 | str[0] = '`'; |
| 130 | s = display_options_get_banner(true, str + 1, 1); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 131 | ut_asserteq_ptr(str + 1, s); |
| 132 | ut_assertok(strcmp("`", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 133 | |
Heinrich Schuchardt | b144ab1 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 134 | str[0] = '~'; |
| 135 | s = display_options_get_banner(true, str + 1, 2); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 136 | ut_asserteq_ptr(str + 1, s); |
| 137 | ut_assertok(strcmp("~\n", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 138 | |
Heinrich Schuchardt | b144ab1 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 139 | /* The last two characters are set to \n\n for all buffer sizes > 2 */ |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 140 | s = display_options_get_banner(false, str, sizeof(str)); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 141 | ut_asserteq_ptr(str, s); |
| 142 | ut_assertok(strcmp("U-Boot \n\n", s)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 143 | |
| 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 Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 148 | 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 Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 151 | |
| 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 Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 156 | ut_asserteq_ptr(big_str, s); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 157 | len = strlen(version_string); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 158 | 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 Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 162 | |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 163 | return 0; |
| 164 | } |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 165 | PRINT_TEST(print_printf, 0); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 166 | |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 167 | static int print_display_buffer(struct unit_test_state *uts) |
| 168 | { |
| 169 | u8 *buf; |
| 170 | int i; |
| 171 | |
Simon Glass | a7dd66f | 2023-10-01 19:15:22 -0600 | [diff] [blame] | 172 | /* This test requires writable memory at zero */ |
| 173 | if (IS_ENABLED(CONFIG_X86)) |
| 174 | return -EAGAIN; |
| 175 | |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 176 | 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 */ |
| 182 | console_record_reset(); |
| 183 | print_buffer(0, buf, 1, 0x12, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 184 | 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 Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 186 | ut_assert_console_end(); |
| 187 | |
| 188 | /* line length */ |
| 189 | console_record_reset(); |
| 190 | print_buffer(0, buf, 1, 0x12, 8); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 191 | ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw"); |
| 192 | ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........"); |
| 193 | ut_assert_nextline("00000010: 10 00 .."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 194 | ut_assert_console_end(); |
| 195 | |
| 196 | /* long line */ |
| 197 | console_record_reset(); |
| 198 | buf[0x41] = 0x41; |
| 199 | print_buffer(0, buf, 1, 0x42, 0x40); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 200 | 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........................................................"); |
| 201 | ut_assert_nextline("00000040: 00 41 .A"); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 202 | ut_assert_console_end(); |
| 203 | |
| 204 | /* address */ |
| 205 | console_record_reset(); |
| 206 | print_buffer(0x12345678, buf, 1, 0x12, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 207 | ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........"); |
| 208 | ut_assert_nextline("12345688: 10 00 .."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 209 | ut_assert_console_end(); |
| 210 | |
| 211 | /* 16-bit */ |
| 212 | console_record_reset(); |
| 213 | print_buffer(0, buf, 2, 9, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 214 | ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........"); |
| 215 | ut_assert_nextline("00000010: 0010 .."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 216 | ut_assert_console_end(); |
| 217 | |
| 218 | /* 32-bit */ |
| 219 | console_record_reset(); |
| 220 | print_buffer(0, buf, 4, 5, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 221 | ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........"); |
| 222 | ut_assert_nextline("00000010: 00000010 ...."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 223 | ut_assert_console_end(); |
| 224 | |
| 225 | /* 64-bit */ |
| 226 | console_record_reset(); |
| 227 | print_buffer(0, buf, 8, 3, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 228 | ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........"); |
| 229 | ut_assert_nextline("00000010: 0000000000000010 ........"); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 230 | ut_assert_console_end(); |
| 231 | |
| 232 | /* ASCII */ |
| 233 | console_record_reset(); |
| 234 | buf[1] = 31; |
| 235 | buf[2] = 32; |
| 236 | buf[3] = 33; |
| 237 | for (i = 0; i < 4; i++) |
| 238 | buf[4 + i] = 126 + i; |
| 239 | buf[8] = 255; |
| 240 | print_buffer(0, buf, 1, 10, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 241 | ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~....."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 242 | ut_assert_console_end(); |
| 243 | |
| 244 | unmap_sysmem(buf); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | PRINT_TEST(print_display_buffer, UT_TESTF_CONSOLE_REC); |
| 249 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 250 | static int print_hexdump_line(struct unit_test_state *uts) |
| 251 | { |
| 252 | char *linebuf; |
| 253 | u8 *buf; |
| 254 | int i; |
| 255 | |
| 256 | buf = map_sysmem(0, BUF_SIZE); |
| 257 | memset(buf, '\0', BUF_SIZE); |
| 258 | for (i = 0; i < 0x11; i++) |
| 259 | buf[i] = i * 0x11; |
| 260 | |
| 261 | /* Check buffer size calculations */ |
| 262 | linebuf = map_sysmem(0x400, BUF_SIZE); |
| 263 | memset(linebuf, '\xff', BUF_SIZE); |
| 264 | ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75)); |
| 265 | ut_asserteq(-1, linebuf[0]); |
| 266 | ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76)); |
| 267 | ut_asserteq(0, linebuf[75]); |
| 268 | ut_asserteq(-1, linebuf[76]); |
| 269 | |
| 270 | unmap_sysmem(buf); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | PRINT_TEST(print_hexdump_line, UT_TESTF_CONSOLE_REC); |
| 275 | |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 276 | static int print_do_hex_dump(struct unit_test_state *uts) |
| 277 | { |
| 278 | u8 *buf; |
| 279 | int i; |
| 280 | |
Simon Glass | a7dd66f | 2023-10-01 19:15:22 -0600 | [diff] [blame] | 281 | /* This test requires writable memory at zero */ |
| 282 | if (IS_ENABLED(CONFIG_X86)) |
| 283 | return -EAGAIN; |
| 284 | |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 285 | buf = map_sysmem(0, BUF_SIZE); |
| 286 | memset(buf, '\0', BUF_SIZE); |
| 287 | for (i = 0; i < 0x11; i++) |
| 288 | buf[i] = i * 0x11; |
| 289 | |
| 290 | /* bytes */ |
| 291 | console_record_reset(); |
| 292 | print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12); |
Marek Vasut | 6bb14de | 2023-08-25 10:19:40 +0200 | [diff] [blame] | 293 | ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........", |
| 294 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); |
| 295 | ut_assert_nextline("%0*lx: 10 00 ..", |
| 296 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 297 | ut_assert_console_end(); |
| 298 | |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 299 | /* line length */ |
| 300 | console_record_reset(); |
| 301 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true); |
Marek Vasut | 6bb14de | 2023-08-25 10:19:40 +0200 | [diff] [blame] | 302 | ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 ..\"3DUfw", |
| 303 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); |
| 304 | ut_assert_nextline("%0*lx: 88 99 aa bb cc dd ee ff ........", |
| 305 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x8UL); |
| 306 | ut_assert_nextline("%0*lx: 10 00 ..", |
| 307 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 308 | ut_assert_console_end(); |
| 309 | unmap_sysmem(buf); |
| 310 | |
| 311 | /* long line */ |
| 312 | console_record_reset(); |
| 313 | buf[0x41] = 0x41; |
| 314 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true); |
Marek Vasut | 6bb14de | 2023-08-25 10:19:40 +0200 | [diff] [blame] | 315 | 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........................................................", |
| 316 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); |
| 317 | ut_assert_nextline("%0*lx: 00 41 .A", |
| 318 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x40UL); |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 319 | ut_assert_console_end(); |
| 320 | |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 321 | /* 16-bit */ |
| 322 | console_record_reset(); |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 323 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true); |
Marek Vasut | 6bb14de | 2023-08-25 10:19:40 +0200 | [diff] [blame] | 324 | ut_assert_nextline("%0*lx: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........", |
| 325 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); |
| 326 | ut_assert_nextline("%0*lx: 0010 ..", |
| 327 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 328 | ut_assert_console_end(); |
| 329 | unmap_sysmem(buf); |
| 330 | |
| 331 | /* 32-bit */ |
| 332 | console_record_reset(); |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 333 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true); |
Marek Vasut | 6bb14de | 2023-08-25 10:19:40 +0200 | [diff] [blame] | 334 | ut_assert_nextline("%0*lx: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........", |
| 335 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); |
| 336 | ut_assert_nextline("%0*lx: 00000010 ....", |
| 337 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 338 | ut_assert_console_end(); |
| 339 | unmap_sysmem(buf); |
| 340 | |
| 341 | /* 64-bit */ |
| 342 | console_record_reset(); |
| 343 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true); |
Marek Vasut | 6bb14de | 2023-08-25 10:19:40 +0200 | [diff] [blame] | 344 | ut_assert_nextline("%0*lx: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........", |
| 345 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); |
| 346 | ut_assert_nextline("%0*lx: 0000000000000010 ........", |
| 347 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 348 | ut_assert_console_end(); |
| 349 | unmap_sysmem(buf); |
| 350 | |
| 351 | /* ASCII */ |
| 352 | console_record_reset(); |
| 353 | buf[1] = 31; |
| 354 | buf[2] = 32; |
| 355 | buf[3] = 33; |
| 356 | for (i = 0; i < 4; i++) |
| 357 | buf[4 + i] = 126 + i; |
| 358 | buf[8] = 255; |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 359 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true); |
Marek Vasut | 6bb14de | 2023-08-25 10:19:40 +0200 | [diff] [blame] | 360 | ut_assert_nextline("%0*lx: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....", |
| 361 | IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 362 | ut_assert_console_end(); |
| 363 | unmap_sysmem(buf); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | PRINT_TEST(print_do_hex_dump, UT_TESTF_CONSOLE_REC); |
| 368 | |
Heinrich Schuchardt | 7eaa37b | 2021-11-15 19:06:55 +0100 | [diff] [blame] | 369 | static int snprint(struct unit_test_state *uts) |
| 370 | { |
| 371 | char buf[10] = "xxxxxxxxx"; |
| 372 | int ret; |
| 373 | |
Heinrich Schuchardt | 2df4f95 | 2022-01-29 16:33:16 +0100 | [diff] [blame] | 374 | ret = snprintf(buf, 5, "%d", 12345678); |
| 375 | ut_asserteq_str("1234", buf); |
| 376 | ut_asserteq(8, ret); |
| 377 | ret = snprintf(buf, 5, "0x%x", 0x1234); |
| 378 | ut_asserteq_str("0x12", buf); |
| 379 | ut_asserteq(6, ret); |
| 380 | ret = snprintf(buf, 5, "0x%08x", 0x1234); |
| 381 | ut_asserteq_str("0x00", buf); |
| 382 | ut_asserteq(10, ret); |
| 383 | ret = snprintf(buf, 3, "%s", "abc"); |
| 384 | ut_asserteq_str("ab", buf); |
| 385 | ut_asserteq(3, ret); |
Heinrich Schuchardt | 7eaa37b | 2021-11-15 19:06:55 +0100 | [diff] [blame] | 386 | ret = snprintf(buf, 4, "%s:%s", "abc", "def"); |
| 387 | ut_asserteq(0, buf[3]); |
| 388 | ut_asserteq(7, ret); |
| 389 | ret = snprintf(buf, 4, "%s:%d", "abc", 9999); |
| 390 | ut_asserteq(8, ret); |
| 391 | return 0; |
| 392 | } |
| 393 | PRINT_TEST(snprint, 0); |
| 394 | |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 395 | int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 396 | { |
| 397 | struct unit_test *tests = UNIT_TEST_SUITE_START(print_test); |
| 398 | const int n_ents = UNIT_TEST_SUITE_COUNT(print_test); |
| 399 | |
| 400 | return cmd_ut_category("print", "print_", tests, n_ents, argc, argv); |
| 401 | } |