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 * Print a pointer to an u16 string
50 * @buf: pointer to buffer address
51 * on return position of terminating zero word
53 static void pointer(void *pointer, u16 **buf)
57 uintptr_t p = (uintptr_t)pointer;
60 for (i = 8 * sizeof(p) - 4; i >= 0; i -= 4) {
72 * Print an unsigned 32bit value as decimal number to an u16 string
74 * @value: value to be printed
75 * @prec: minimum number of digits to display
76 * @buf: pointer to buffer address
77 * on return position of terminating zero word
79 static void uint2dec(u32 value, int prec, u16 **buf)
87 * Increment by .5 and multiply with
88 * (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC
89 * to move the first digit to bit 60-63.
92 f += (0x9B5A52DULL * value) >> 28;
93 f += 0x44B82FA0ULL * value;
95 for (i = 0; i < 10; ++i) {
96 /* Write current digit */
98 if (c || pos != *buf || 10 - i <= prec)
100 /* Eliminate current digit */
101 f &= 0xfffffffffffffff;
112 * Print a signed 32bit value as decimal number to an u16 string
114 * @value: value to be printed
115 * @prec: minimum number of digits to display
116 * @buf: pointer to buffer address
117 * on return position of terminating zero word
119 static void int2dec(s32 value, int prec, u16 **buf)
130 uint2dec(u, prec, &pos);
135 * Print a colored formatted string to the EFI console
137 * @color color, see constants in efi_api.h, use -1 for no color
139 * @... optional arguments
141 void efi_st_printc(int color, const char *fmt, ...)
154 con_out->set_attribute(con_out, (unsigned long)color);
179 /* Parse precision */
192 int2dec(va_arg(args, s32), prec, &pos);
199 mac(va_arg(args, void*), &pos);
204 u = va_arg(args, u16*);
207 con_out->output_string(con_out,
210 con_out->output_string(con_out, u);
215 pointer(va_arg(args, void*), &pos);
219 s = va_arg(args, const char *);
224 uint2dec(va_arg(args, u32), prec, &pos);
236 con_out->output_string(con_out, buf);
238 con_out->set_attribute(con_out, EFI_LIGHTGRAY);
242 * Reads an Unicode character from the input device.
244 * @return: Unicode character
246 u16 efi_st_get_key(void)
248 struct efi_input_key input_key;
251 /* Wait for next key */
253 ret = con_in->read_key_stroke(con_in, &input_key);
254 } while (ret == EFI_NOT_READY);
255 return input_key.unicode_char;