1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * EFI efi_selftest
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 */
7
8 #include <efi_selftest.h>
9 #include <net.h>
10 #include <vsprintf.h>
11
12 struct efi_simple_text_output_protocol *con_out;
13 struct efi_simple_text_input_protocol *con_in;
14
15 /*
16 * Print a MAC address to an u16 string
17 *
18 * @pointer: mac address
19 * @buf: pointer to buffer address
20 * on return position of terminating zero word
21 */
mac(void * pointer,u16 ** buf)22 static void mac(void *pointer, u16 **buf)
23 {
24 int i, j;
25 u16 c;
26 u8 *p = (u8 *)pointer;
27 u8 byte;
28 u16 *pos = *buf;
29
30 for (i = 0; i < ARP_HLEN; ++i) {
31 if (i)
32 *pos++ = ':';
33 byte = p[i];
34 for (j = 4; j >= 0; j -= 4) {
35 c = (byte >> j) & 0x0f;
36 c += '0';
37 if (c > '9')
38 c += 'a' - '9' - 1;
39 *pos++ = c;
40 }
41 }
42 *pos = 0;
43 *buf = pos;
44 }
45
46 /*
47 * printx() - print hexadecimal number to an u16 string
48 *
49 * @p: value to print
50 * @prec: minimum number of digits to print
51 * @buf: pointer to buffer address,
52 * on return position of terminating zero word
53 */
printx(u64 p,int prec,u16 ** buf)54 static void printx(u64 p, int prec, u16 **buf)
55 {
56 int i;
57 u16 c;
58 u16 *pos = *buf;
59
60 for (i = 2 * sizeof(p) - 1; i >= 0; --i) {
61 c = (p >> (4 * i)) & 0x0f;
62 if (c || pos != *buf || !i || i < prec) {
63 c += '0';
64 if (c > '9')
65 c += 'a' - '9' - 1;
66 *pos++ = c;
67 }
68 }
69 *pos = 0;
70 *buf = pos;
71 }
72
73 /*
74 * Print an unsigned 32bit value as decimal number to an u16 string
75 *
76 * @value: value to be printed
77 * @prec: minimum number of digits to display
78 * @buf: pointer to buffer address
79 * on return position of terminating zero word
80 */
uint2dec(u32 value,int prec,u16 ** buf)81 static void uint2dec(u32 value, int prec, u16 **buf)
82 {
83 u16 *pos = *buf;
84 int i;
85 u16 c;
86 u64 f;
87
88 /*
89 * Increment by .5 and multiply with
90 * (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC
91 * to move the first digit to bit 60-63.
92 */
93 f = 0x225C17D0;
94 f += (0x9B5A52DULL * value) >> 28;
95 f += 0x44B82FA0ULL * value;
96
97 for (i = 0; i < 10; ++i) {
98 /* Write current digit */
99 c = f >> 60;
100 if (c || pos != *buf || 10 - i <= prec)
101 *pos++ = c + '0';
102 /* Eliminate current digit */
103 f &= 0xfffffffffffffff;
104 /* Get next digit */
105 f *= 0xaULL;
106 }
107 if (pos == *buf)
108 *pos++ = '0';
109 *pos = 0;
110 *buf = pos;
111 }
112
113 /*
114 * Print a signed 32bit value as decimal number to an u16 string
115 *
116 * @value: value to be printed
117 * @prec: minimum number of digits to display
118 * @buf: pointer to buffer address
119 * on return position of terminating zero word
120 */
int2dec(s32 value,int prec,u16 ** buf)121 static void int2dec(s32 value, int prec, u16 **buf)
122 {
123 u32 u;
124 u16 *pos = *buf;
125
126 if (value < 0) {
127 *pos++ = '-';
128 u = -value;
129 } else {
130 u = value;
131 }
132 uint2dec(u, prec, &pos);
133 *buf = pos;
134 }
135
136 /*
137 * Print a colored formatted string to the EFI console
138 *
139 * @color color, see constants in efi_api.h, use -1 for no color
140 * @fmt format string
141 * @... optional arguments
142 */
efi_st_printc(int color,const char * fmt,...)143 void efi_st_printc(int color, const char *fmt, ...)
144 {
145 va_list args;
146 u16 buf[160];
147 const char *c;
148 u16 *pos = buf;
149 const char *s;
150 u16 *u;
151 int prec;
152
153 va_start(args, fmt);
154
155 if (color >= 0)
156 con_out->set_attribute(con_out, (unsigned long)color);
157 c = fmt;
158 for (; *c; ++c) {
159 switch (*c) {
160 case '\\':
161 ++c;
162 switch (*c) {
163 case '\0':
164 --c;
165 break;
166 case 'n':
167 *pos++ = '\n';
168 break;
169 case 'r':
170 *pos++ = '\r';
171 break;
172 case 't':
173 *pos++ = '\t';
174 break;
175 default:
176 *pos++ = *c;
177 }
178 break;
179 case '%':
180 ++c;
181 /* Parse precision */
182 if (*c == '.') {
183 ++c;
184 prec = *c - '0';
185 ++c;
186 } else {
187 prec = 0;
188 }
189 switch (*c) {
190 case '\0':
191 --c;
192 break;
193 case 'd':
194 int2dec(va_arg(args, s32), prec, &pos);
195 break;
196 case 'p':
197 ++c;
198 switch (*c) {
199 /* MAC address */
200 case 'm':
201 mac(va_arg(args, void*), &pos);
202 break;
203
204 /* u16 string */
205 case 's':
206 u = va_arg(args, u16*);
207 if (pos > buf) {
208 *pos = 0;
209 con_out->output_string(con_out,
210 buf);
211 }
212 con_out->output_string(con_out, u);
213 pos = buf;
214 break;
215 default:
216 --c;
217 printx((uintptr_t)va_arg(args, void *),
218 2 * sizeof(void *), &pos);
219 break;
220 }
221 break;
222 case 's':
223 s = va_arg(args, const char *);
224 for (; *s; ++s)
225 *pos++ = *s;
226 break;
227 case 'u':
228 uint2dec(va_arg(args, u32), prec, &pos);
229 break;
230 case 'x':
231 printx((u64)va_arg(args, unsigned int),
232 prec, &pos);
233 break;
234 default:
235 break;
236 }
237 break;
238 default:
239 *pos++ = *c;
240 }
241 }
242 va_end(args);
243 *pos = 0;
244 con_out->output_string(con_out, buf);
245 if (color >= 0)
246 con_out->set_attribute(con_out, EFI_LIGHTGRAY);
247 }
248
249 /*
250 * Reads an Unicode character from the input device.
251 *
252 * @return: Unicode character
253 */
efi_st_get_key(void)254 u16 efi_st_get_key(void)
255 {
256 struct efi_input_key input_key;
257 efi_status_t ret;
258
259 /* Wait for next key */
260 do {
261 ret = con_in->read_key_stroke(con_in, &input_key);
262 } while (ret == EFI_NOT_READY);
263 return input_key.unicode_char;
264 }
265