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