blob: fb46db832e58e5692f4af32f659a0bed658b98e7 [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 Schuchardt218d3892018-01-10 18:06:08 +01009#if defined(CONFIG_EFI_LOADER) && \
10 !defined(CONFIG_SPL_BUILD) && !defined(API_BUILD)
11#include <efi_api.h>
12#endif
Simon Glass75ee32f2017-06-15 21:37:51 -060013#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
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010019/* Test efi_loader specific printing */
20static 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));
Heinrich Schuchardt3b47f332018-01-26 06:30:30 +010046
47 /* NULL device path */
48 snprintf(str, sizeof(str), "_%pD_", NULL);
49 assert(!strcmp("_<NULL>_", str));
Heinrich Schuchardt218d3892018-01-10 18:06:08 +010050#endif
51}
52
Simon Glass75ee32f2017-06-15 21:37:51 -060053static 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
Rob Clark22ae0a72017-09-13 18:46:54 -040076 sprintf(big_str, "_%ls_", L"foo");
77 assert(!strcmp("_foo_", big_str));
78
Simon Glass75ee32f2017-06-15 21:37:51 -060079 /* 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
Heinrich Schuchardt218d3892018-01-10 18:06:08 +0100115 /* Test efi_loader specific printing */
116 efi_ut_print();
117
Simon Glass75ee32f2017-06-15 21:37:51 -0600118 printf("%s: Everything went swimmingly\n", __func__);
119 return 0;
120}
121
122U_BOOT_CMD(
123 ut_print, 1, 1, do_ut_print,
124 "Very basic test of printf(), etc.",
125 ""
126);