1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
8 #include <efi_selftest.h>
12 struct efi_simple_text_output_protocol *con_out;
13 struct efi_simple_text_input_protocol *con_in;
16 * Print a MAC address to an u16 string
18 * @pointer: mac address
19 * @buf: pointer to buffer address
20 * on return position of terminating zero word
22 static void mac(void *pointer, u16 **buf)
26 u8 *p = (u8 *)pointer;
30 for (i = 0; i < ARP_HLEN; ++i) {
34 for (j = 4; j >= 0; j -= 4) {
35 c = (byte >> j) & 0x0f;
47 * printx() - print hexadecimal number to an u16 string
50 * @prec: minimum number of digits to print
51 * @buf: pointer to buffer address,
52 * on return position of terminating zero word
54 static void printx(u64 p, int prec, u16 **buf)
60 for (i = 2 * sizeof(p) - 1; i >= 0; --i) {
61 c = (p >> (4 * i)) & 0x0f;
62 if (c || pos != *buf || !i || i < prec) {
74 * print_guid() - print GUID to an u16 string
77 * @buf: pointer to buffer address,
78 * on return position of terminating zero word
80 static void print_uuid(u8 *p, u16 **buf)
84 3, 2, 1, 0, '-', 5, 4, '-', 7, 6, '-',
85 8, 9, 10, 11, 12, 13, 14, 15 };
87 for (i = 0; i < sizeof(seq); ++i) {
91 printx(p[seq[i]], 2, buf);
96 * Print an unsigned 32bit value as decimal number to an u16 string
98 * @value: value to be printed
99 * @prec: minimum number of digits to display
100 * @buf: pointer to buffer address
101 * on return position of terminating zero word
103 static void uint2dec(u32 value, int prec, u16 **buf)
111 * Increment by .5 and multiply with
112 * (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC
113 * to move the first digit to bit 60-63.
116 f += (0x9B5A52DULL * value) >> 28;
117 f += 0x44B82FA0ULL * value;
119 for (i = 0; i < 10; ++i) {
120 /* Write current digit */
122 if (c || pos != *buf || 10 - i <= prec)
124 /* Eliminate current digit */
125 f &= 0xfffffffffffffff;
136 * Print a signed 32bit value as decimal number to an u16 string
138 * @value: value to be printed
139 * @prec: minimum number of digits to display
140 * @buf: pointer to buffer address
141 * on return position of terminating zero word
143 static void int2dec(s32 value, int prec, u16 **buf)
154 uint2dec(u, prec, &pos);
159 * Print a colored formatted string to the EFI console
161 * @color color, see constants in efi_api.h, use -1 for no color
163 * @... optional arguments
165 void efi_st_printc(int color, const char *fmt, ...)
178 con_out->set_attribute(con_out, (unsigned long)color);
203 /* Parse precision */
216 int2dec(va_arg(args, s32), prec, &pos);
223 mac(va_arg(args, void*), &pos);
228 u = va_arg(args, u16*);
231 con_out->output_string(con_out,
234 con_out->output_string(con_out, u);
238 print_uuid(va_arg(args, void*), &pos);
242 printx((uintptr_t)va_arg(args, void *),
243 2 * sizeof(void *), &pos);
248 s = va_arg(args, const char *);
253 uint2dec(va_arg(args, u32), prec, &pos);
256 printx((u64)va_arg(args, unsigned int),
269 con_out->output_string(con_out, buf);
271 con_out->set_attribute(con_out, EFI_LIGHTGRAY);
275 * Reads an Unicode character from the input device.
277 * Return: Unicode character
279 u16 efi_st_get_key(void)
281 struct efi_input_key input_key;
284 /* Wait for next key */
286 ret = con_in->read_key_stroke(con_in, &input_key);
287 } while (ret == EFI_NOT_READY);
288 return input_key.unicode_char;