2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-09-15 08:06:11 +00:00
|
|
|
/*
|
|
|
|
* EFI efi_selftest
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <efi_selftest.h>
|
2020-05-10 17:39:56 +00:00
|
|
|
#include <net.h>
|
2017-09-15 08:06:11 +00:00
|
|
|
#include <vsprintf.h>
|
|
|
|
|
|
|
|
struct efi_simple_text_output_protocol *con_out;
|
2018-09-08 08:20:10 +00:00
|
|
|
struct efi_simple_text_input_protocol *con_in;
|
2017-09-15 08:06:11 +00:00
|
|
|
|
2017-10-05 14:36:06 +00:00
|
|
|
/*
|
|
|
|
* Print a MAC address to an u16 string
|
|
|
|
*
|
|
|
|
* @pointer: mac address
|
|
|
|
* @buf: pointer to buffer address
|
|
|
|
* on return position of terminating zero word
|
|
|
|
*/
|
|
|
|
static void mac(void *pointer, u16 **buf)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
u16 c;
|
|
|
|
u8 *p = (u8 *)pointer;
|
|
|
|
u8 byte;
|
|
|
|
u16 *pos = *buf;
|
|
|
|
|
|
|
|
for (i = 0; i < ARP_HLEN; ++i) {
|
|
|
|
if (i)
|
|
|
|
*pos++ = ':';
|
|
|
|
byte = p[i];
|
|
|
|
for (j = 4; j >= 0; j -= 4) {
|
|
|
|
c = (byte >> j) & 0x0f;
|
|
|
|
c += '0';
|
|
|
|
if (c > '9')
|
|
|
|
c += 'a' - '9' - 1;
|
|
|
|
*pos++ = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pos = 0;
|
|
|
|
*buf = pos;
|
|
|
|
}
|
|
|
|
|
2017-09-15 08:06:11 +00:00
|
|
|
/*
|
2020-10-03 11:12:03 +00:00
|
|
|
* printx() - print hexadecimal number to an u16 string
|
2017-09-15 08:06:11 +00:00
|
|
|
*
|
2021-01-17 06:30:26 +00:00
|
|
|
* @p: value to print
|
2020-10-03 11:12:03 +00:00
|
|
|
* @prec: minimum number of digits to print
|
|
|
|
* @buf: pointer to buffer address,
|
|
|
|
* on return position of terminating zero word
|
2017-09-15 08:06:11 +00:00
|
|
|
*/
|
2020-10-03 11:12:03 +00:00
|
|
|
static void printx(u64 p, int prec, u16 **buf)
|
2017-09-15 08:06:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u16 c;
|
|
|
|
u16 *pos = *buf;
|
|
|
|
|
2020-10-03 11:12:03 +00:00
|
|
|
for (i = 2 * sizeof(p) - 1; i >= 0; --i) {
|
|
|
|
c = (p >> (4 * i)) & 0x0f;
|
|
|
|
if (c || pos != *buf || !i || i < prec) {
|
|
|
|
c += '0';
|
|
|
|
if (c > '9')
|
|
|
|
c += 'a' - '9' - 1;
|
|
|
|
*pos++ = c;
|
|
|
|
}
|
2017-09-15 08:06:11 +00:00
|
|
|
}
|
|
|
|
*pos = 0;
|
|
|
|
*buf = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print an unsigned 32bit value as decimal number to an u16 string
|
|
|
|
*
|
2018-07-07 21:39:15 +00:00
|
|
|
* @value: value to be printed
|
|
|
|
* @prec: minimum number of digits to display
|
|
|
|
* @buf: pointer to buffer address
|
|
|
|
* on return position of terminating zero word
|
2017-09-15 08:06:11 +00:00
|
|
|
*/
|
2018-07-07 21:39:15 +00:00
|
|
|
static void uint2dec(u32 value, int prec, u16 **buf)
|
2017-09-15 08:06:11 +00:00
|
|
|
{
|
|
|
|
u16 *pos = *buf;
|
|
|
|
int i;
|
|
|
|
u16 c;
|
|
|
|
u64 f;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Increment by .5 and multiply with
|
|
|
|
* (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC
|
|
|
|
* to move the first digit to bit 60-63.
|
|
|
|
*/
|
|
|
|
f = 0x225C17D0;
|
|
|
|
f += (0x9B5A52DULL * value) >> 28;
|
|
|
|
f += 0x44B82FA0ULL * value;
|
|
|
|
|
|
|
|
for (i = 0; i < 10; ++i) {
|
|
|
|
/* Write current digit */
|
|
|
|
c = f >> 60;
|
2018-07-07 21:39:15 +00:00
|
|
|
if (c || pos != *buf || 10 - i <= prec)
|
2017-09-15 08:06:11 +00:00
|
|
|
*pos++ = c + '0';
|
|
|
|
/* Eliminate current digit */
|
|
|
|
f &= 0xfffffffffffffff;
|
|
|
|
/* Get next digit */
|
|
|
|
f *= 0xaULL;
|
|
|
|
}
|
|
|
|
if (pos == *buf)
|
|
|
|
*pos++ = '0';
|
|
|
|
*pos = 0;
|
|
|
|
*buf = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a signed 32bit value as decimal number to an u16 string
|
|
|
|
*
|
2018-07-07 21:39:15 +00:00
|
|
|
* @value: value to be printed
|
|
|
|
* @prec: minimum number of digits to display
|
|
|
|
* @buf: pointer to buffer address
|
2017-09-15 08:06:11 +00:00
|
|
|
* on return position of terminating zero word
|
|
|
|
*/
|
2018-07-07 21:39:15 +00:00
|
|
|
static void int2dec(s32 value, int prec, u16 **buf)
|
2017-09-15 08:06:11 +00:00
|
|
|
{
|
|
|
|
u32 u;
|
|
|
|
u16 *pos = *buf;
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
*pos++ = '-';
|
|
|
|
u = -value;
|
|
|
|
} else {
|
|
|
|
u = value;
|
|
|
|
}
|
2018-07-07 21:39:15 +00:00
|
|
|
uint2dec(u, prec, &pos);
|
2017-09-15 08:06:11 +00:00
|
|
|
*buf = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-01-11 07:15:54 +00:00
|
|
|
* Print a colored formatted string to the EFI console
|
2017-09-15 08:06:11 +00:00
|
|
|
*
|
2018-01-11 07:15:54 +00:00
|
|
|
* @color color, see constants in efi_api.h, use -1 for no color
|
|
|
|
* @fmt format string
|
|
|
|
* @... optional arguments
|
2017-09-15 08:06:11 +00:00
|
|
|
*/
|
2018-01-11 07:15:54 +00:00
|
|
|
void efi_st_printc(int color, const char *fmt, ...)
|
2017-09-15 08:06:11 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
u16 buf[160];
|
|
|
|
const char *c;
|
|
|
|
u16 *pos = buf;
|
|
|
|
const char *s;
|
2017-12-22 18:21:04 +00:00
|
|
|
u16 *u;
|
2018-07-07 21:39:15 +00:00
|
|
|
int prec;
|
2017-09-15 08:06:11 +00:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
2018-01-11 07:15:54 +00:00
|
|
|
if (color >= 0)
|
|
|
|
con_out->set_attribute(con_out, (unsigned long)color);
|
2017-09-15 08:06:11 +00:00
|
|
|
c = fmt;
|
|
|
|
for (; *c; ++c) {
|
|
|
|
switch (*c) {
|
|
|
|
case '\\':
|
|
|
|
++c;
|
|
|
|
switch (*c) {
|
|
|
|
case '\0':
|
|
|
|
--c;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
*pos++ = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
*pos++ = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
*pos++ = '\t';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*pos++ = *c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
++c;
|
2018-07-07 21:39:15 +00:00
|
|
|
/* Parse precision */
|
|
|
|
if (*c == '.') {
|
|
|
|
++c;
|
|
|
|
prec = *c - '0';
|
|
|
|
++c;
|
|
|
|
} else {
|
|
|
|
prec = 0;
|
|
|
|
}
|
2017-09-15 08:06:11 +00:00
|
|
|
switch (*c) {
|
|
|
|
case '\0':
|
|
|
|
--c;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2018-07-07 21:39:15 +00:00
|
|
|
int2dec(va_arg(args, s32), prec, &pos);
|
2017-09-15 08:06:11 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2017-10-05 14:36:06 +00:00
|
|
|
++c;
|
|
|
|
switch (*c) {
|
2017-10-18 16:13:13 +00:00
|
|
|
/* MAC address */
|
2017-10-05 14:36:06 +00:00
|
|
|
case 'm':
|
|
|
|
mac(va_arg(args, void*), &pos);
|
|
|
|
break;
|
2017-10-18 16:13:13 +00:00
|
|
|
|
|
|
|
/* u16 string */
|
|
|
|
case 's':
|
|
|
|
u = va_arg(args, u16*);
|
2017-12-22 18:21:04 +00:00
|
|
|
if (pos > buf) {
|
|
|
|
*pos = 0;
|
|
|
|
con_out->output_string(con_out,
|
|
|
|
buf);
|
|
|
|
}
|
|
|
|
con_out->output_string(con_out, u);
|
|
|
|
pos = buf;
|
2017-10-18 16:13:13 +00:00
|
|
|
break;
|
2017-10-05 14:36:06 +00:00
|
|
|
default:
|
|
|
|
--c;
|
2020-10-03 11:12:03 +00:00
|
|
|
printx((uintptr_t)va_arg(args, void *),
|
|
|
|
2 * sizeof(void *), &pos);
|
|
|
|
break;
|
2017-10-05 14:36:06 +00:00
|
|
|
}
|
2017-09-15 08:06:11 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
s = va_arg(args, const char *);
|
|
|
|
for (; *s; ++s)
|
|
|
|
*pos++ = *s;
|
|
|
|
break;
|
|
|
|
case 'u':
|
2018-07-07 21:39:15 +00:00
|
|
|
uint2dec(va_arg(args, u32), prec, &pos);
|
2017-09-15 08:06:11 +00:00
|
|
|
break;
|
2020-10-03 11:12:03 +00:00
|
|
|
case 'x':
|
|
|
|
printx((u64)va_arg(args, unsigned int),
|
|
|
|
prec, &pos);
|
|
|
|
break;
|
2017-09-15 08:06:11 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*pos++ = *c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
*pos = 0;
|
|
|
|
con_out->output_string(con_out, buf);
|
2018-01-11 07:15:54 +00:00
|
|
|
if (color >= 0)
|
|
|
|
con_out->set_attribute(con_out, EFI_LIGHTGRAY);
|
2017-09-15 08:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads an Unicode character from the input device.
|
|
|
|
*
|
|
|
|
* @return: Unicode character
|
|
|
|
*/
|
|
|
|
u16 efi_st_get_key(void)
|
|
|
|
{
|
|
|
|
struct efi_input_key input_key;
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
/* Wait for next key */
|
|
|
|
do {
|
|
|
|
ret = con_in->read_key_stroke(con_in, &input_key);
|
|
|
|
} while (ret == EFI_NOT_READY);
|
|
|
|
return input_key.unicode_char;
|
|
|
|
}
|