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