xref: /openbmc/u-boot/test/print_ut.c (revision cd1cc31f)
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 #endif
48 }
49 
50 static int do_ut_print(cmd_tbl_t *cmdtp, int flag, int argc,
51 		       char *const argv[])
52 {
53 	char big_str[400];
54 	int big_str_len;
55 	char str[10], *s;
56 	int len;
57 
58 	printf("%s: Testing print\n", __func__);
59 
60 	snprintf(str, sizeof(str), "testing");
61 	assert(!strcmp("testing", str));
62 
63 	snprintf(str, sizeof(str), "testing but too long");
64 	assert(!strcmp("testing b", str));
65 
66 	snprintf(str, 1, "testing none");
67 	assert(!strcmp("", str));
68 
69 	*str = 'x';
70 	snprintf(str, 0, "testing none");
71 	assert(*str == 'x');
72 
73 	sprintf(big_str, "_%ls_", L"foo");
74 	assert(!strcmp("_foo_", big_str));
75 
76 	/* Test the banner function */
77 	s = display_options_get_banner(true, str, sizeof(str));
78 	assert(s == str);
79 	assert(!strcmp("\n\nU-Boo\n\n", s));
80 
81 	s = display_options_get_banner(true, str, 1);
82 	assert(s == str);
83 	assert(!strcmp("", s));
84 
85 	s = display_options_get_banner(true, str, 2);
86 	assert(s == str);
87 	assert(!strcmp("\n", s));
88 
89 	s = display_options_get_banner(false, str, sizeof(str));
90 	assert(s == str);
91 	assert(!strcmp("U-Boot \n\n", s));
92 
93 	/* Give it enough space for some of the version */
94 	big_str_len = strlen(version_string) - 5;
95 	s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
96 					    big_str_len);
97 	assert(s == big_str);
98 	assert(!strncmp(version_string, s, big_str_len - 3));
99 	assert(!strcmp("\n\n", s + big_str_len - 3));
100 
101 	/* Give it enough space for the version and some of the build tag */
102 	big_str_len = strlen(version_string) + 9 + 20;
103 	s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
104 					    big_str_len);
105 	assert(s == big_str);
106 	len = strlen(version_string);
107 	assert(!strncmp(version_string, s, len));
108 	assert(!strncmp(", Build: ", s + len, 9));
109 	assert(!strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
110 	assert(!strcmp("\n\n", s + big_str_len - 3));
111 
112 	/* Test efi_loader specific printing */
113 	efi_ut_print();
114 
115 	printf("%s: Everything went swimmingly\n", __func__);
116 	return 0;
117 }
118 
119 U_BOOT_CMD(
120 	ut_print,	1,	1,	do_ut_print,
121 	"Very basic test of printf(), etc.",
122 	""
123 );
124