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