blob: 0bc548dca831cc09e0de630a7ded77e282a48d1c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass75ee32f2017-06-15 21:37:51 -06002/*
3 * Copyright (c) 2012, The Chromium Authors
Simon Glass75ee32f2017-06-15 21:37:51 -06004 */
5
6#define DEBUG
7
8#include <common.h>
Heinrich Schuchardteed3f952018-08-31 21:31:24 +02009#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010010#include <efi_api.h>
11#endif
Simon Glass75ee32f2017-06-15 21:37:51 -060012#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 Schuchardt218d3892018-01-10 18:06:08 +010018/* Test efi_loader specific printing */
19static void efi_ut_print(void)
20{
Heinrich Schuchardteed3f952018-08-31 21:31:24 +020021#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010022 char str[10];
23 u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
24 sizeof(struct efi_device_path)];
25 u8 *pos = buf;
26 struct efi_device_path *dp_end;
27 struct efi_device_path_sd_mmc_path *dp_sd =
28 (struct efi_device_path_sd_mmc_path *)pos;
29
30 /* Create a device path for an SD card */
31 dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
32 dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
33 dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
34 dp_sd->slot_number = 3;
35 pos += sizeof(struct efi_device_path_sd_mmc_path);
36 /* Append end node */
37 dp_end = (struct efi_device_path *)pos;
38 dp_end->type = DEVICE_PATH_TYPE_END;
39 dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
40 dp_end->length = sizeof(struct efi_device_path);
41
42 snprintf(str, sizeof(str), "_%pD_", buf);
43 assert(!strcmp("_/SD(3)_", str));
Heinrich Schuchardt3b47f332018-01-26 06:30:30 +010044
45 /* NULL device path */
46 snprintf(str, sizeof(str), "_%pD_", NULL);
47 assert(!strcmp("_<NULL>_", str));
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010048#endif
49}
50
Simon Glass75ee32f2017-06-15 21:37:51 -060051static int do_ut_print(cmd_tbl_t *cmdtp, int flag, int argc,
52 char *const argv[])
53{
54 char big_str[400];
55 int big_str_len;
56 char str[10], *s;
57 int len;
58
59 printf("%s: Testing print\n", __func__);
60
61 snprintf(str, sizeof(str), "testing");
62 assert(!strcmp("testing", str));
63
64 snprintf(str, sizeof(str), "testing but too long");
65 assert(!strcmp("testing b", str));
66
67 snprintf(str, 1, "testing none");
68 assert(!strcmp("", str));
69
70 *str = 'x';
71 snprintf(str, 0, "testing none");
72 assert(*str == 'x');
73
Rob Clark22ae0a72017-09-13 18:46:54 -040074 sprintf(big_str, "_%ls_", L"foo");
75 assert(!strcmp("_foo_", big_str));
76
Simon Glass75ee32f2017-06-15 21:37:51 -060077 /* Test the banner function */
78 s = display_options_get_banner(true, str, sizeof(str));
79 assert(s == str);
80 assert(!strcmp("\n\nU-Boo\n\n", s));
81
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +020082 /* Assert that we do not overwrite memory before the buffer */
83 str[0] = '`';
84 s = display_options_get_banner(true, str + 1, 1);
85 assert(s == str + 1);
86 assert(!strcmp("`", str));
Simon Glass75ee32f2017-06-15 21:37:51 -060087
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +020088 str[0] = '~';
89 s = display_options_get_banner(true, str + 1, 2);
90 assert(s == str + 1);
91 assert(!strcmp("~\n", str));
Simon Glass75ee32f2017-06-15 21:37:51 -060092
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +020093 /* The last two characters are set to \n\n for all buffer sizes > 2 */
Simon Glass75ee32f2017-06-15 21:37:51 -060094 s = display_options_get_banner(false, str, sizeof(str));
95 assert(s == str);
96 assert(!strcmp("U-Boot \n\n", s));
97
98 /* Give it enough space for some of the version */
99 big_str_len = strlen(version_string) - 5;
100 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
101 big_str_len);
102 assert(s == big_str);
103 assert(!strncmp(version_string, s, big_str_len - 3));
104 assert(!strcmp("\n\n", s + big_str_len - 3));
105
106 /* Give it enough space for the version and some of the build tag */
107 big_str_len = strlen(version_string) + 9 + 20;
108 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
109 big_str_len);
110 assert(s == big_str);
111 len = strlen(version_string);
112 assert(!strncmp(version_string, s, len));
113 assert(!strncmp(", Build: ", s + len, 9));
114 assert(!strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
115 assert(!strcmp("\n\n", s + big_str_len - 3));
116
Heinrich Schuchardt218d3892018-01-10 18:06:08 +0100117 /* Test efi_loader specific printing */
118 efi_ut_print();
119
Simon Glass75ee32f2017-06-15 21:37:51 -0600120 printf("%s: Everything went swimmingly\n", __func__);
121 return 0;
122}
123
124U_BOOT_CMD(
125 ut_print, 1, 1, do_ut_print,
126 "Very basic test of printf(), etc.",
127 ""
128);