xref: /openbmc/u-boot/test/print_ut.c (revision 59e43c32)
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 CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
10 #include <efi_api.h>
11 #endif
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 
18 /* Test efi_loader specific printing */
19 static void efi_ut_print(void)
20 {
21 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
22 	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));
44 
45 	/* NULL device path */
46 	snprintf(str, sizeof(str), "_%pD_", NULL);
47 	assert(!strcmp("_<NULL>_", str));
48 #endif
49 }
50 
51 static 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 
74 	sprintf(big_str, "_%ls_", L"foo");
75 	assert(!strcmp("_foo_", big_str));
76 
77 	/* 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 
82 	s = display_options_get_banner(true, str, 1);
83 	assert(s == str);
84 	assert(!strcmp("", s));
85 
86 	s = display_options_get_banner(true, str, 2);
87 	assert(s == str);
88 	assert(!strcmp("\n", s));
89 
90 	s = display_options_get_banner(false, str, sizeof(str));
91 	assert(s == str);
92 	assert(!strcmp("U-Boot \n\n", s));
93 
94 	/* Give it enough space for some of the version */
95 	big_str_len = strlen(version_string) - 5;
96 	s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
97 					    big_str_len);
98 	assert(s == big_str);
99 	assert(!strncmp(version_string, s, big_str_len - 3));
100 	assert(!strcmp("\n\n", s + big_str_len - 3));
101 
102 	/* Give it enough space for the version and some of the build tag */
103 	big_str_len = strlen(version_string) + 9 + 20;
104 	s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
105 					    big_str_len);
106 	assert(s == big_str);
107 	len = strlen(version_string);
108 	assert(!strncmp(version_string, s, len));
109 	assert(!strncmp(", Build: ", s + len, 9));
110 	assert(!strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
111 	assert(!strcmp("\n\n", s + big_str_len - 3));
112 
113 	/* Test efi_loader specific printing */
114 	efi_ut_print();
115 
116 	printf("%s: Everything went swimmingly\n", __func__);
117 	return 0;
118 }
119 
120 U_BOOT_CMD(
121 	ut_print,	1,	1,	do_ut_print,
122 	"Very basic test of printf(), etc.",
123 	""
124 );
125