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 | |
| 6 | #define DEBUG |
| 7 | |
| 8 | #include <common.h> |
Heinrich Schuchardt | 7a9e6ee | 2018-08-31 21:31:24 +0200 | [diff] [blame] | 9 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 10 | #include <efi_api.h> |
| 11 | #endif |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 12 | #include <display_options.h> |
| 13 | #include <version.h> |
| 14 | |
| 15 | #define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \ |
| 16 | "and a lot more text to come" |
| 17 | |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 18 | /* Test printing GUIDs */ |
| 19 | static void guid_ut_print(void) |
| 20 | { |
| 21 | #if CONFIG_IS_ENABLED(LIB_UUID) |
| 22 | unsigned char guid[16] = { |
| 23 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 |
| 24 | }; |
| 25 | char str[40]; |
| 26 | |
| 27 | sprintf(str, "%pUb", guid); |
| 28 | assert(!strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str)); |
| 29 | sprintf(str, "%pUB", guid); |
| 30 | assert(!strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str)); |
| 31 | sprintf(str, "%pUl", guid); |
| 32 | assert(!strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str)); |
| 33 | sprintf(str, "%pUL", guid); |
| 34 | assert(!strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str)); |
| 35 | #endif |
| 36 | } |
| 37 | |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 38 | /* Test efi_loader specific printing */ |
| 39 | static void efi_ut_print(void) |
| 40 | { |
Heinrich Schuchardt | 7a9e6ee | 2018-08-31 21:31:24 +0200 | [diff] [blame] | 41 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 42 | char str[10]; |
| 43 | u8 buf[sizeof(struct efi_device_path_sd_mmc_path) + |
| 44 | sizeof(struct efi_device_path)]; |
| 45 | u8 *pos = buf; |
| 46 | struct efi_device_path *dp_end; |
| 47 | struct efi_device_path_sd_mmc_path *dp_sd = |
| 48 | (struct efi_device_path_sd_mmc_path *)pos; |
| 49 | |
| 50 | /* Create a device path for an SD card */ |
| 51 | dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 52 | dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD; |
| 53 | dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path); |
| 54 | dp_sd->slot_number = 3; |
| 55 | pos += sizeof(struct efi_device_path_sd_mmc_path); |
| 56 | /* Append end node */ |
| 57 | dp_end = (struct efi_device_path *)pos; |
| 58 | dp_end->type = DEVICE_PATH_TYPE_END; |
| 59 | dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 60 | dp_end->length = sizeof(struct efi_device_path); |
| 61 | |
| 62 | snprintf(str, sizeof(str), "_%pD_", buf); |
| 63 | assert(!strcmp("_/SD(3)_", str)); |
Heinrich Schuchardt | 5f1ce1d | 2018-01-26 06:30:30 +0100 | [diff] [blame] | 64 | |
| 65 | /* NULL device path */ |
| 66 | snprintf(str, sizeof(str), "_%pD_", NULL); |
| 67 | assert(!strcmp("_<NULL>_", str)); |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 68 | #endif |
| 69 | } |
| 70 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 71 | static int do_ut_print(cmd_tbl_t *cmdtp, int flag, int argc, |
| 72 | char *const argv[]) |
| 73 | { |
| 74 | char big_str[400]; |
| 75 | int big_str_len; |
| 76 | char str[10], *s; |
| 77 | int len; |
| 78 | |
| 79 | printf("%s: Testing print\n", __func__); |
| 80 | |
| 81 | snprintf(str, sizeof(str), "testing"); |
| 82 | assert(!strcmp("testing", str)); |
| 83 | |
| 84 | snprintf(str, sizeof(str), "testing but too long"); |
| 85 | assert(!strcmp("testing b", str)); |
| 86 | |
| 87 | snprintf(str, 1, "testing none"); |
| 88 | assert(!strcmp("", str)); |
| 89 | |
| 90 | *str = 'x'; |
| 91 | snprintf(str, 0, "testing none"); |
| 92 | assert(*str == 'x'); |
| 93 | |
Rob Clark | 085391b | 2017-09-13 18:46:54 -0400 | [diff] [blame] | 94 | sprintf(big_str, "_%ls_", L"foo"); |
| 95 | assert(!strcmp("_foo_", big_str)); |
| 96 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 97 | /* Test the banner function */ |
| 98 | s = display_options_get_banner(true, str, sizeof(str)); |
| 99 | assert(s == str); |
| 100 | assert(!strcmp("\n\nU-Boo\n\n", s)); |
| 101 | |
Heinrich Schuchardt | 6c74e94 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 102 | /* Assert that we do not overwrite memory before the buffer */ |
| 103 | str[0] = '`'; |
| 104 | s = display_options_get_banner(true, str + 1, 1); |
| 105 | assert(s == str + 1); |
| 106 | assert(!strcmp("`", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 107 | |
Heinrich Schuchardt | 6c74e94 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 108 | str[0] = '~'; |
| 109 | s = display_options_get_banner(true, str + 1, 2); |
| 110 | assert(s == str + 1); |
| 111 | assert(!strcmp("~\n", str)); |
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 | /* 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] | 114 | s = display_options_get_banner(false, str, sizeof(str)); |
| 115 | assert(s == str); |
| 116 | assert(!strcmp("U-Boot \n\n", s)); |
| 117 | |
| 118 | /* Give it enough space for some of the version */ |
| 119 | big_str_len = strlen(version_string) - 5; |
| 120 | s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str, |
| 121 | big_str_len); |
| 122 | assert(s == big_str); |
| 123 | assert(!strncmp(version_string, s, big_str_len - 3)); |
| 124 | assert(!strcmp("\n\n", s + big_str_len - 3)); |
| 125 | |
| 126 | /* Give it enough space for the version and some of the build tag */ |
| 127 | big_str_len = strlen(version_string) + 9 + 20; |
| 128 | s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str, |
| 129 | big_str_len); |
| 130 | assert(s == big_str); |
| 131 | len = strlen(version_string); |
| 132 | assert(!strncmp(version_string, s, len)); |
| 133 | assert(!strncmp(", Build: ", s + len, 9)); |
| 134 | assert(!strncmp(FAKE_BUILD_TAG, s + 9 + len, 12)); |
| 135 | assert(!strcmp("\n\n", s + big_str_len - 3)); |
| 136 | |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 137 | /* Test efi_loader specific printing */ |
| 138 | efi_ut_print(); |
| 139 | |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 140 | /* Test printing GUIDs */ |
| 141 | guid_ut_print(); |
| 142 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 143 | printf("%s: Everything went swimmingly\n", __func__); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | U_BOOT_CMD( |
| 148 | ut_print, 1, 1, do_ut_print, |
| 149 | "Very basic test of printf(), etc.", |
| 150 | "" |
| 151 | ); |