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 an unsigned 32bit value as decimal number to an u16 string
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
81 static void uint2dec(u32 value, int prec, u16 **buf)
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.
94 f += (0x9B5A52DULL * value) >> 28;
95 f += 0x44B82FA0ULL * value;
97 for (i = 0; i < 10; ++i) {
98 /* Write current digit */
100 if (c || pos != *buf || 10 - i <= prec)
102 /* Eliminate current digit */
103 f &= 0xfffffffffffffff;
114 * Print a signed 32bit value as decimal number to an u16 string
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
121 static void int2dec(s32 value, int prec, u16 **buf)
132 uint2dec(u, prec, &pos);
137 * Print a colored formatted string to the EFI console
139 * @color color, see constants in efi_api.h, use -1 for no color
141 * @... optional arguments
143 void efi_st_printc(int color, const char *fmt, ...)
156 con_out->set_attribute(con_out, (unsigned long)color);
181 /* Parse precision */
194 int2dec(va_arg(args, s32), prec, &pos);
201 mac(va_arg(args, void*), &pos);
206 u = va_arg(args, u16*);
209 con_out->output_string(con_out,
212 con_out->output_string(con_out, u);
217 printx((uintptr_t)va_arg(args, void *),
218 2 * sizeof(void *), &pos);
223 s = va_arg(args, const char *);
228 uint2dec(va_arg(args, u32), prec, &pos);
231 printx((u64)va_arg(args, unsigned int),
244 con_out->output_string(con_out, buf);
246 con_out->set_attribute(con_out, EFI_LIGHTGRAY);
250 * Reads an Unicode character from the input device.
252 * @return: Unicode character
254 u16 efi_st_get_key(void)
256 struct efi_input_key input_key;
259 /* Wait for next key */
261 ret = con_in->read_key_stroke(con_in, &input_key);
262 } while (ret == EFI_NOT_READY);
263 return input_key.unicode_char;