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