xref: /openbmc/u-boot/test/print_ut.c (revision 3ebd892f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2012, The Chromium Authors
4  */
5 
6 #define DEBUG
7 
8 #include <common.h>
9 #if defined(CONFIG_EFI_LOADER) && \
10 	!defined(CONFIG_SPL_BUILD) && !defined(API_BUILD)
11 #include <efi_api.h>
12 #endif
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 
19 /* Test efi_loader specific printing */
20 static void efi_ut_print(void)
21 {
22 #if defined(CONFIG_EFI_LOADER) && \
23     !defined(CONFIG_SPL_BUILD) && !defined(API_BUILD)
24 	char str[10];
25 	u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
26 	       sizeof(struct efi_device_path)];
27 	u8 *pos = buf;
28 	struct efi_device_path *dp_end;
29 	struct efi_device_path_sd_mmc_path *dp_sd =
30 			(struct efi_device_path_sd_mmc_path *)pos;
31 
32 	/* Create a device path for an SD card */
33 	dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
34 	dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
35 	dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
36 	dp_sd->slot_number = 3;
37 	pos += sizeof(struct efi_device_path_sd_mmc_path);
38 	/* Append end node */
39 	dp_end = (struct efi_device_path *)pos;
40 	dp_end->type = DEVICE_PATH_TYPE_END;
41 	dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
42 	dp_end->length = sizeof(struct efi_device_path);
43 
44 	snprintf(str, sizeof(str), "_%pD_", buf);
45 	assert(!strcmp("_/SD(3)_", str));
46 
47 	/* NULL device path */
48 	snprintf(str, sizeof(str), "_%pD_", NULL);
49 	assert(!strcmp("_<NULL>_", str));
50 #endif
51 }
52 
53 static int do_ut_print(cmd_tbl_t *cmdtp, int flag, int argc,
54 		       char *const argv[])
55 {
56 	char big_str[400];
57 	int big_str_len;
58 	char str[10], *s;
59 	int len;
60 
61 	printf("%s: Testing print\n", __func__);
62 
63 	snprintf(str, sizeof(str), "testing");
64 	assert(!strcmp("testing", str));
65 
66 	snprintf(str, sizeof(str), "testing but too long");
67 	assert(!strcmp("testing b", str));
68 
69 	snprintf(str, 1, "testing none");
70 	assert(!strcmp("", str));
71 
72 	*str = 'x';
73 	snprintf(str, 0, "testing none");
74 	assert(*str == 'x');
75 
76 	sprintf(big_str, "_%ls_", L"foo");
77 	assert(!strcmp("_foo_", big_str));
78 
79 	/* Test the banner function */
80 	s = display_options_get_banner(true, str, sizeof(str));
81 	assert(s == str);
82 	assert(!strcmp("\n\nU-Boo\n\n", s));
83 
84 	s = display_options_get_banner(true, str, 1);
85 	assert(s == str);
86 	assert(!strcmp("", s));
87 
88 	s = display_options_get_banner(true, str, 2);
89 	assert(s == str);
90 	assert(!strcmp("\n", s));
91 
92 	s = display_options_get_banner(false, str, sizeof(str));
93 	assert(s == str);
94 	assert(!strcmp("U-Boot \n\n", s));
95 
96 	/* Give it enough space for some of the version */
97 	big_str_len = strlen(version_string) - 5;
98 	s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
99 					    big_str_len);
100 	assert(s == big_str);
101 	assert(!strncmp(version_string, s, big_str_len - 3));
102 	assert(!strcmp("\n\n", s + big_str_len - 3));
103 
104 	/* Give it enough space for the version and some of the build tag */
105 	big_str_len = strlen(version_string) + 9 + 20;
106 	s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
107 					    big_str_len);
108 	assert(s == big_str);
109 	len = strlen(version_string);
110 	assert(!strncmp(version_string, s, len));
111 	assert(!strncmp(", Build: ", s + len, 9));
112 	assert(!strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
113 	assert(!strcmp("\n\n", s + big_str_len - 3));
114 
115 	/* Test efi_loader specific printing */
116 	efi_ut_print();
117 
118 	printf("%s: Everything went swimmingly\n", __func__);
119 	return 0;
120 }
121 
122 U_BOOT_CMD(
123 	ut_print,	1,	1,	do_ut_print,
124 	"Very basic test of printf(), etc.",
125 	""
126 );
127