Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, The Chromium Authors |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 7 | #include <command.h> |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 8 | #include <efi_api.h> |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 9 | #include <display_options.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 11 | #include <mapmem.h> |
Pali Rohár | bdfb6d7 | 2021-08-02 15:18:31 +0200 | [diff] [blame] | 12 | #include <version_string.h> |
Simon Glass | 3bfb0f7 | 2021-10-14 12:48:06 -0600 | [diff] [blame] | 13 | #include <vsprintf.h> |
Simon Glass | fbb99dc | 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 | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 17 | |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 18 | #define BUF_SIZE 0x100 |
| 19 | |
Simon Glass | e5a9d27 | 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 | fbb99dc | 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 | |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 26 | #if CONFIG_IS_ENABLED(LIB_UUID) |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 27 | /* Test printing GUIDs */ |
| 28 | static int print_guid(struct unit_test_state *uts) |
| 29 | { |
Heinrich Schuchardt | 3bad256 | 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 | }; |
| 33 | char str[40]; |
| 34 | |
| 35 | sprintf(str, "%pUb", guid); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 36 | ut_assertok(strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str)); |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 37 | sprintf(str, "%pUB", guid); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 38 | ut_assertok(strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str)); |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 39 | sprintf(str, "%pUl", guid); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 40 | ut_assertok(strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str)); |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 41 | sprintf(str, "%pUL", guid); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 42 | ut_assertok(strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str)); |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 43 | |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | PRINT_TEST(print_guid, 0); |
| 47 | #endif |
| 48 | |
Heinrich Schuchardt | 7a9e6ee | 2018-08-31 21:31:24 +0200 | [diff] [blame] | 49 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 50 | /* Test efi_loader specific printing */ |
| 51 | static int print_efi_ut(struct unit_test_state *uts) |
| 52 | { |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 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); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 74 | ut_assertok(strcmp("_/SD(3)_", str)); |
Heinrich Schuchardt | 5f1ce1d | 2018-01-26 06:30:30 +0100 | [diff] [blame] | 75 | |
| 76 | /* NULL device path */ |
| 77 | snprintf(str, sizeof(str), "_%pD_", NULL); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 78 | ut_assertok(strcmp("_<NULL>_", str)); |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 79 | |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | PRINT_TEST(print_efi_ut, 0); |
| 83 | #endif |
| 84 | |
| 85 | static int print_printf(struct unit_test_state *uts) |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 86 | { |
| 87 | char big_str[400]; |
| 88 | int big_str_len; |
| 89 | char str[10], *s; |
| 90 | int len; |
| 91 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 92 | snprintf(str, sizeof(str), "testing"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 93 | ut_assertok(strcmp("testing", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 94 | |
| 95 | snprintf(str, sizeof(str), "testing but too long"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 96 | ut_assertok(strcmp("testing b", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 97 | |
| 98 | snprintf(str, 1, "testing none"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 99 | ut_assertok(strcmp("", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 100 | |
| 101 | *str = 'x'; |
| 102 | snprintf(str, 0, "testing none"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 103 | ut_asserteq('x', *str); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 104 | |
Rob Clark | 085391b | 2017-09-13 18:46:54 -0400 | [diff] [blame] | 105 | sprintf(big_str, "_%ls_", L"foo"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 106 | ut_assertok(strcmp("_foo_", big_str)); |
Rob Clark | 085391b | 2017-09-13 18:46:54 -0400 | [diff] [blame] | 107 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 108 | /* Test the banner function */ |
| 109 | s = display_options_get_banner(true, str, sizeof(str)); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 110 | ut_asserteq_ptr(str, s); |
| 111 | ut_assertok(strcmp("\n\nU-Boo\n\n", s)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 112 | |
Heinrich Schuchardt | 6c74e94 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 113 | /* Assert that we do not overwrite memory before the buffer */ |
| 114 | str[0] = '`'; |
| 115 | s = display_options_get_banner(true, str + 1, 1); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 116 | ut_asserteq_ptr(str + 1, s); |
| 117 | ut_assertok(strcmp("`", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 118 | |
Heinrich Schuchardt | 6c74e94 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 119 | str[0] = '~'; |
| 120 | s = display_options_get_banner(true, str + 1, 2); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 121 | ut_asserteq_ptr(str + 1, s); |
| 122 | ut_assertok(strcmp("~\n", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 123 | |
Heinrich Schuchardt | 6c74e94 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 124 | /* The last two characters are set to \n\n for all buffer sizes > 2 */ |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 125 | s = display_options_get_banner(false, str, sizeof(str)); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 126 | ut_asserteq_ptr(str, s); |
| 127 | ut_assertok(strcmp("U-Boot \n\n", s)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 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); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 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)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 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); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 141 | ut_asserteq_ptr(big_str, s); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 142 | len = strlen(version_string); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 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)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 147 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 148 | return 0; |
| 149 | } |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 150 | PRINT_TEST(print_printf, 0); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 151 | |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 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); |
Simon Glass | c7b16d8 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 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 .."); |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 167 | ut_assert_console_end(); |
| 168 | |
| 169 | /* line length */ |
| 170 | console_record_reset(); |
| 171 | print_buffer(0, buf, 1, 0x12, 8); |
Simon Glass | c7b16d8 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 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 .."); |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 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); |
Simon Glass | c7b16d8 | 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 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"); |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 183 | ut_assert_console_end(); |
| 184 | |
| 185 | /* address */ |
| 186 | console_record_reset(); |
| 187 | print_buffer(0x12345678, buf, 1, 0x12, 0); |
Simon Glass | c7b16d8 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 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 .."); |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 190 | ut_assert_console_end(); |
| 191 | |
| 192 | /* 16-bit */ |
| 193 | console_record_reset(); |
| 194 | print_buffer(0, buf, 2, 9, 0); |
Simon Glass | c7b16d8 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 195 | ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........"); |
| 196 | ut_assert_nextline("00000010: 0010 .."); |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 197 | ut_assert_console_end(); |
| 198 | |
| 199 | /* 32-bit */ |
| 200 | console_record_reset(); |
| 201 | print_buffer(0, buf, 4, 5, 0); |
Simon Glass | c7b16d8 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 202 | ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........"); |
| 203 | ut_assert_nextline("00000010: 00000010 ...."); |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 204 | ut_assert_console_end(); |
| 205 | |
| 206 | /* 64-bit */ |
| 207 | console_record_reset(); |
| 208 | print_buffer(0, buf, 8, 3, 0); |
Simon Glass | c7b16d8 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 209 | ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........"); |
| 210 | ut_assert_nextline("00000010: 0000000000000010 ........"); |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 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); |
Simon Glass | c7b16d8 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 222 | ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~....."); |
Simon Glass | c614ddf | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 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 | |
Simon Glass | 0cceb99 | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 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 | |
Simon Glass | 19edf13 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 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 | |
Simon Glass | 5d6d2b8 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 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 | |
Simon Glass | 19edf13 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 291 | /* 16-bit */ |
| 292 | console_record_reset(); |
Simon Glass | 5d6d2b8 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 293 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true); |
Simon Glass | 19edf13 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 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(); |
Simon Glass | 5d6d2b8 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 301 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true); |
Simon Glass | 19edf13 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 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; |
Simon Glass | 5d6d2b8 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 323 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true); |
Simon Glass | 19edf13 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 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 | |
Simon Glass | 3bfb0f7 | 2021-10-14 12:48:06 -0600 | [diff] [blame] | 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)); |
Simon Glass | 4a255ea | 2021-10-14 12:48:07 -0600 | [diff] [blame^] | 338 | |
| 339 | /* Use #ifdef here to avoid a compiler warning on 32-bit machines */ |
| 340 | #ifdef CONFIG_PHYS_64BIT |
Simon Glass | 3bfb0f7 | 2021-10-14 12:48:06 -0600 | [diff] [blame] | 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 | } |
Simon Glass | 4a255ea | 2021-10-14 12:48:07 -0600 | [diff] [blame^] | 346 | #endif /* CONFIG_PHYS_64BIT */ |
Simon Glass | 3bfb0f7 | 2021-10-14 12:48:06 -0600 | [diff] [blame] | 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | PRINT_TEST(print_itoa, 0); |
| 351 | |
Simon Glass | 4a255ea | 2021-10-14 12:48:07 -0600 | [diff] [blame^] | 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 | |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 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 | } |