#include <sys/types.h>
#include <sys/param.h>
#include <sys/machparam.h>
#include <sys/archsystm.h>
#include <sys/boot_console.h>
#include <sys/varargs.h>
#include "dboot_asm.h"
#include "dboot_printf.h"
#include "dboot_xboot.h"
#ifdef __xpv
#include <sys/hypervisor.h>
#endif
static void do_dboot_printf(char *fmt, va_list args);
static char digits[] = "0123456789abcdef";
void
dboot_panic(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
do_dboot_printf(fmt, args);
if (boot_console_type(NULL) == CONS_SCREEN_TEXT) {
dboot_printf("Press any key to reboot\n");
(void) bcons_getchar();
}
outb(0x64, 0xfe);
dboot_halt();
}
void
dboot_printf(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
do_dboot_printf(fmt, args);
}
static void
dboot_puts(char *s)
{
while (*s != 0) {
bcons_putchar(*s);
++s;
}
}
static void
dboot_putnum(uintmax_t x, boolean_t is_signed, uint8_t base)
{
char buffer[64];
int i;
if (is_signed && (intmax_t)x < 0) {
bcons_putchar('-');
x = -x;
}
for (i = -1; x != 0 && i <= 63; x /= base)
buffer[++i] = digits[x - ((x / base) * base)];
if (i < 0)
buffer[++i] = '0';
while (i >= 0)
bcons_putchar(buffer[i--]);
}
static void
do_dboot_printf(char *fmt, va_list args)
{
char *s;
uintmax_t x;
uint8_t base;
uint8_t size;
if (fmt == NULL) {
dboot_puts("dboot_printf(): 1st arg is NULL\n");
return;
}
for (; *fmt; ++fmt) {
if (*fmt != '%') {
bcons_putchar(*fmt);
continue;
}
size = 0;
again:
++fmt;
switch (*fmt) {
case '%':
bcons_putchar(*fmt);
break;
case 'c':
x = va_arg(args, int);
bcons_putchar(x);
break;
case 'j':
size = sizeof (uintmax_t);
goto again;
case 's':
s = va_arg(args, char *);
if (s == NULL)
dboot_puts("*NULL*");
else
dboot_puts(s);
break;
case 'p':
x = va_arg(args, ulong_t);
dboot_putnum(x, B_FALSE, 16);
break;
case 'l':
if (size == 0)
size = sizeof (long);
else if (size == sizeof (long))
size = sizeof (long long);
goto again;
case 'd':
if (size == 0)
x = va_arg(args, int);
else if (size == sizeof (long))
x = va_arg(args, long);
else
x = va_arg(args, long long);
dboot_putnum(x, B_TRUE, 10);
break;
case 'u':
base = 10;
goto unsigned_num;
case 'b':
base = 2;
goto unsigned_num;
case 'o':
base = 8;
goto unsigned_num;
case 'x':
base = 16;
unsigned_num:
if (size == 0)
x = va_arg(args, uint_t);
else if (size == sizeof (ulong_t))
x = va_arg(args, ulong_t);
else
x = va_arg(args, unsigned long long);
dboot_putnum(x, B_FALSE, base);
break;
case 'z':
size = sizeof (size_t);
goto again;
default:
dboot_puts("dboot_printf(): unknown % escape\n");
}
}
}