blob: 963ed913db320187c598f79c5a9b8ad0733b04d2 [file] [log] [blame]
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +02001/*
2 * EFI efi_selftest
3 *
4 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <efi_selftest.h>
10#include <vsprintf.h>
11
12struct efi_simple_text_output_protocol *con_out;
13struct efi_simple_input_interface *con_in;
14
15/*
Heinrich Schuchardt1b6332592017-10-05 16:36:06 +020016 * 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 */
22static 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/*
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020047 * Print a pointer to an u16 string
48 *
49 * @pointer: pointer
50 * @buf: pointer to buffer address
51 * on return position of terminating zero word
52 */
53static void pointer(void *pointer, u16 **buf)
54{
55 int i;
56 u16 c;
57 uintptr_t p = (uintptr_t)pointer;
58 u16 *pos = *buf;
59
60 for (i = 8 * sizeof(p) - 4; i >= 0; i -= 4) {
61 c = (p >> i) & 0x0f;
62 c += '0';
63 if (c > '9')
64 c += 'a' - '9' - 1;
65 *pos++ = c;
66 }
67 *pos = 0;
68 *buf = pos;
69}
70
71/*
72 * Print an unsigned 32bit value as decimal number to an u16 string
73 *
74 * @value: value to be printed
75 * @buf: pointer to buffer address
76 * on return position of terminating zero word
77 */
78static void uint2dec(u32 value, u16 **buf)
79{
80 u16 *pos = *buf;
81 int i;
82 u16 c;
83 u64 f;
84
85 /*
86 * Increment by .5 and multiply with
87 * (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC
88 * to move the first digit to bit 60-63.
89 */
90 f = 0x225C17D0;
91 f += (0x9B5A52DULL * value) >> 28;
92 f += 0x44B82FA0ULL * value;
93
94 for (i = 0; i < 10; ++i) {
95 /* Write current digit */
96 c = f >> 60;
97 if (c || pos != *buf)
98 *pos++ = c + '0';
99 /* Eliminate current digit */
100 f &= 0xfffffffffffffff;
101 /* Get next digit */
102 f *= 0xaULL;
103 }
104 if (pos == *buf)
105 *pos++ = '0';
106 *pos = 0;
107 *buf = pos;
108}
109
110/*
111 * Print a signed 32bit value as decimal number to an u16 string
112 *
113 * @value: value to be printed
114 * @buf: pointer to buffer address
115 * on return position of terminating zero word
116 */
117static void int2dec(s32 value, u16 **buf)
118{
119 u32 u;
120 u16 *pos = *buf;
121
122 if (value < 0) {
123 *pos++ = '-';
124 u = -value;
125 } else {
126 u = value;
127 }
128 uint2dec(u, &pos);
129 *buf = pos;
130}
131
132/*
133 * Print a formatted string to the EFI console
134 *
135 * @fmt: format string
136 * @...: optional arguments
137 */
138void efi_st_printf(const char *fmt, ...)
139{
140 va_list args;
141 u16 buf[160];
142 const char *c;
143 u16 *pos = buf;
144 const char *s;
Heinrich Schuchardte0abeac2017-12-22 19:21:04 +0100145 u16 *u;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200146
147 va_start(args, fmt);
148
149 c = fmt;
150 for (; *c; ++c) {
151 switch (*c) {
152 case '\\':
153 ++c;
154 switch (*c) {
155 case '\0':
156 --c;
157 break;
158 case 'n':
159 *pos++ = '\n';
160 break;
161 case 'r':
162 *pos++ = '\r';
163 break;
164 case 't':
165 *pos++ = '\t';
166 break;
167 default:
168 *pos++ = *c;
169 }
170 break;
171 case '%':
172 ++c;
173 switch (*c) {
174 case '\0':
175 --c;
176 break;
177 case 'd':
178 int2dec(va_arg(args, s32), &pos);
179 break;
180 case 'p':
Heinrich Schuchardt1b6332592017-10-05 16:36:06 +0200181 ++c;
182 switch (*c) {
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200183 /* MAC address */
Heinrich Schuchardt1b6332592017-10-05 16:36:06 +0200184 case 'm':
185 mac(va_arg(args, void*), &pos);
186 break;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200187
188 /* u16 string */
189 case 's':
190 u = va_arg(args, u16*);
Heinrich Schuchardte0abeac2017-12-22 19:21:04 +0100191 if (pos > buf) {
192 *pos = 0;
193 con_out->output_string(con_out,
194 buf);
195 }
196 con_out->output_string(con_out, u);
197 pos = buf;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200198 break;
Heinrich Schuchardt1b6332592017-10-05 16:36:06 +0200199 default:
200 --c;
201 pointer(va_arg(args, void*), &pos);
202 }
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200203 break;
204 case 's':
205 s = va_arg(args, const char *);
206 for (; *s; ++s)
207 *pos++ = *s;
208 break;
209 case 'u':
210 uint2dec(va_arg(args, u32), &pos);
211 break;
212 default:
213 break;
214 }
215 break;
216 default:
217 *pos++ = *c;
218 }
219 }
220 va_end(args);
221 *pos = 0;
222 con_out->output_string(con_out, buf);
223}
224
225/*
226 * Reads an Unicode character from the input device.
227 *
228 * @return: Unicode character
229 */
230u16 efi_st_get_key(void)
231{
232 struct efi_input_key input_key;
233 efi_status_t ret;
234
235 /* Wait for next key */
236 do {
237 ret = con_in->read_key_stroke(con_in, &input_key);
238 } while (ret == EFI_NOT_READY);
239 return input_key.unicode_char;
240}