blob: f0f1d6010a164fb2d4e14aa89065ed7c44bdd7f2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse5a9d272017-06-15 21:37:51 -06002/*
3 * Copyright (c) 2012, The Chromium Authors
Simon Glasse5a9d272017-06-15 21:37:51 -06004 */
5
6#define DEBUG
7
8#include <common.h>
Heinrich Schuchardt7a9e6ee2018-08-31 21:31:24 +02009#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010010#include <efi_api.h>
11#endif
Simon Glasse5a9d272017-06-15 21:37:51 -060012#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
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010018/* Test efi_loader specific printing */
19static void efi_ut_print(void)
20{
Heinrich Schuchardt7a9e6ee2018-08-31 21:31:24 +020021#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010022 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));
Heinrich Schuchardt5f1ce1d2018-01-26 06:30:30 +010044
45 /* NULL device path */
46 snprintf(str, sizeof(str), "_%pD_", NULL);
47 assert(!strcmp("_<NULL>_", str));
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010048#endif
49}
50
Simon Glasse5a9d272017-06-15 21:37:51 -060051static 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
Rob Clark085391b2017-09-13 18:46:54 -040074 sprintf(big_str, "_%ls_", L"foo");
75 assert(!strcmp("_foo_", big_str));
76
Simon Glasse5a9d272017-06-15 21:37:51 -060077 /* 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
Heinrich Schuchardt256060e2018-01-10 18:06:08 +0100113 /* Test efi_loader specific printing */
114 efi_ut_print();
115
Simon Glasse5a9d272017-06-15 21:37:51 -0600116 printf("%s: Everything went swimmingly\n", __func__);
117 return 0;
118}
119
120U_BOOT_CMD(
121 ut_print, 1, 1, do_ut_print,
122 "Very basic test of printf(), etc.",
123 ""
124);