X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Foutput.c;h=bdde7ccaa4fbac2c5ec12700c152c0de9fc58203;hb=refs%2Fheads%2Fcoreboot;hp=da585b4f35b18f9266cb17ea93db4bf4831b0d15;hpb=f8e800dea4aad59c83254bddd8e9ccfcd3b45774;p=seabios.git diff --git a/src/output.c b/src/output.c index da585b4..bdde7cc 100644 --- a/src/output.c +++ b/src/output.c @@ -1,6 +1,6 @@ // Raw screen writing and debug output code. // -// Copyright (C) 2008 Kevin O'Connor +// Copyright (C) 2008,2009 Kevin O'Connor // // This file may be distributed under the terms of the GNU LGPLv3 license. @@ -12,22 +12,30 @@ #include "config.h" // CONFIG_* #include "biosvar.h" // GET_GLOBAL -#define DEBUG_PORT PORT_SERIAL1 +struct putcinfo { + void (*func)(struct putcinfo *info, char c); +}; + + +/**************************************************************** + * Debug output + ****************************************************************/ + #define DEBUG_TIMEOUT 100000 void -debug_serial_setup() +debug_serial_setup(void) { if (!CONFIG_DEBUG_SERIAL) return; // setup for serial logging: 8N1 u8 oldparam, newparam = 0x03; - oldparam = inb(DEBUG_PORT+SEROFF_LCR); - outb(newparam, DEBUG_PORT+SEROFF_LCR); + oldparam = inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_LCR); + outb(newparam, CONFIG_DEBUG_SERIAL_PORT+SEROFF_LCR); // Disable irqs u8 oldier, newier = 0; - oldier = inb(DEBUG_PORT+SEROFF_IER); - outb(newier, DEBUG_PORT+SEROFF_IER); + oldier = inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_IER); + outb(newier, CONFIG_DEBUG_SERIAL_PORT+SEROFF_IER); if (oldparam != newparam || oldier != newier) dprintf(1, "Changing serial settings was %x/%x now %x/%x\n" @@ -41,33 +49,60 @@ debug_serial(char c) if (!CONFIG_DEBUG_SERIAL) return; int timeout = DEBUG_TIMEOUT; - while ((inb(DEBUG_PORT+SEROFF_LSR) & 0x60) != 0x60) + while ((inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_LSR) & 0x20) != 0x20) if (!timeout--) // Ran out of time. return; - outb(c, DEBUG_PORT+SEROFF_DATA); + outb(c, CONFIG_DEBUG_SERIAL_PORT+SEROFF_DATA); } // Make sure all serial port writes have been completely sent. static void -debug_serial_flush() +debug_serial_flush(void) { if (!CONFIG_DEBUG_SERIAL) return; int timeout = DEBUG_TIMEOUT; - while ((inb(DEBUG_PORT+SEROFF_LSR) & 0x40) != 0x40) + while ((inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_LSR) & 0x60) != 0x60) if (!timeout--) // Ran out of time. return; } -// Show a character on the screen. +// Write a character to debug port(s). static void -screenc(u8 c) +putc_debug(struct putcinfo *action, char c) { - if (MODE16) - // printf is only used in 32bit code. + if (! CONFIG_DEBUG_LEVEL) return; + if (CONFIG_DEBUG_IO) + // Send character to debug port. + outb(c, CONFIG_DEBUG_IO_PORT); + if (c == '\n') + debug_serial('\r'); + debug_serial(c); +} + +// In segmented mode just need a dummy variable (putc_debug is always +// used anyway), and in 32bit flat mode need a pointer to the 32bit +// instance of putc_debug(). +#if MODE16 +static struct putcinfo debuginfo VAR16; +#elif MODESEGMENT +static struct putcinfo debuginfo VAR32SEG; +#else +static struct putcinfo debuginfo = { putc_debug }; +#endif + + +/**************************************************************** + * Screen writing + ****************************************************************/ + +// Show a character on the screen. +static void +screenc(char c) +{ struct bregs br; memset(&br, 0, sizeof(br)); br.flags = F_IF; @@ -76,42 +111,55 @@ screenc(u8 c) call16_int(0x10, &br); } +// Handle a character from a printf request. +static void +putc_screen(struct putcinfo *action, char c) +{ + if (ScreenAndDebug) + putc_debug(&debuginfo, c); + if (c == '\n') + screenc('\r'); + screenc(c); +} + +static struct putcinfo screeninfo = { putc_screen }; + + +/**************************************************************** + * Xprintf code + ****************************************************************/ + // Output a character. static void -putc(u16 action, char c) -{ - if (CONFIG_DEBUG_LEVEL && (CONFIG_SCREEN_AND_DEBUG || !action)) { - if (! CONFIG_COREBOOT) - // Send character to debug port. - outb(c, PORT_BIOS_DEBUG); - // Send character to serial port. - if (c == '\n') - debug_serial('\r'); - debug_serial(c); +putc(struct putcinfo *action, char c) +{ + if (MODESEGMENT) { + // Only debugging output supported in segmented mode. + putc_debug(action, c); + return; } - if (action) { - // Send character to video screen. - if (c == '\n') - screenc('\r'); - screenc(c); - } + void (*func)(struct putcinfo *info, char c) = GET_GLOBAL(action->func); + func(action, c); } // Ouptut a string. static void -puts(u16 action, const char *s) +puts(struct putcinfo *action, const char *s) { + if (!MODESEGMENT && !s) + s = "(NULL)"; for (; *s; s++) putc(action, *s); } // Output a string that is in the CS segment. static void -puts_cs(u16 action, const char *s) +puts_cs(struct putcinfo *action, const char *s) { - for (;; s++) { - char c = GET_GLOBAL(*(u8*)s); + char *vs = (char*)s; + for (;; vs++) { + char c = GET_GLOBAL(*vs); if (!c) break; putc(action, c); @@ -120,7 +168,7 @@ puts_cs(u16 action, const char *s) // Output an unsigned integer. static void -putuint(u16 action, u32 val) +putuint(struct putcinfo *action, u32 val) { char buf[12]; char *d = &buf[sizeof(buf) - 1]; @@ -137,7 +185,7 @@ putuint(u16 action, u32 val) // Output a single digit hex character. static inline void -putsinglehex(u16 action, u32 val) +putsinglehex(struct putcinfo *action, u32 val) { if (val <= 9) val = '0' + val; @@ -148,21 +196,24 @@ putsinglehex(u16 action, u32 val) // Output an integer in hexadecimal. static void -puthex(u16 action, u32 val, int width) +puthex(struct putcinfo *action, u32 val, int width, int spacepad) { if (!width) { u32 tmp = val; width = 1; - if (tmp > 0xffff) { - width += 4; - tmp >>= 16; - } - if (tmp > 0xff) { - width += 2; - tmp >>= 8; + while (tmp >>= 4) + width++; + } else if (spacepad) { + u32 tmp = val; + u32 count = 1; + while (tmp >>= 4) + count++; + if (width > count) { + count = width - count; + width -= count; + while (count--) + putc(action, ' '); } - if (tmp > 0xf) - width += 1; } switch (width) { @@ -184,7 +235,7 @@ isdigit(u8 c) } static void -bvprintf(u16 action, const char *fmt, va_list args) +bvprintf(struct putcinfo *action, const char *fmt, va_list args) { const char *s = fmt; for (;; s++) { @@ -197,11 +248,15 @@ bvprintf(u16 action, const char *fmt, va_list args) } const char *n = s+1; int field_width = 0; + int spacepad = 1; for (;;) { c = GET_GLOBAL(*(u8*)n); if (!isdigit(c)) break; - field_width = field_width * 10 + c - '0'; + if (!field_width && (c == '0')) + spacepad = 0; + else + field_width = field_width * 10 + c - '0'; n++; } if (c == 'l') { @@ -232,9 +287,10 @@ bvprintf(u16 action, const char *fmt, va_list args) putc(action, '0'); putc(action, 'x'); field_width = 8; + spacepad = 0; case 'x': val = va_arg(args, s32); - puthex(action, val, field_width); + puthex(action, val, field_width, spacepad); break; case 'c': val = va_arg(args, int); @@ -258,7 +314,6 @@ bvprintf(u16 action, const char *fmt, va_list args) } s = n; } - debug_serial_flush(); } void @@ -267,8 +322,9 @@ panic(const char *fmt, ...) if (CONFIG_DEBUG_LEVEL) { va_list args; va_start(args, fmt); - bvprintf(0, fmt, args); + bvprintf(&debuginfo, fmt, args); va_end(args); + debug_serial_flush(); } // XXX - use PANIC PORT. @@ -280,39 +336,125 @@ panic(const char *fmt, ...) void __dprintf(const char *fmt, ...) { + if (!MODESEGMENT && CONFIG_THREADS && CONFIG_DEBUG_LEVEL >= DEBUG_thread + && *fmt != '\\' && *fmt != '/') { + struct thread_info *cur = getCurThread(); + if (cur != &MainThread) { + // Show "thread id" for this debug message. + putc_debug(&debuginfo, '|'); + puthex(&debuginfo, (u32)cur, 8, 0); + putc_debug(&debuginfo, '|'); + putc_debug(&debuginfo, ' '); + } + } + va_list args; va_start(args, fmt); - bvprintf(0, fmt, args); + bvprintf(&debuginfo, fmt, args); va_end(args); + debug_serial_flush(); } void printf(const char *fmt, ...) { + ASSERT32FLAT(); + va_list args; + va_start(args, fmt); + bvprintf(&screeninfo, fmt, args); + va_end(args); + if (ScreenAndDebug) + debug_serial_flush(); +} + + +/**************************************************************** + * snprintf + ****************************************************************/ + +struct snprintfinfo { + struct putcinfo info; + char *str, *end; +}; + +static void +putc_str(struct putcinfo *info, char c) +{ + struct snprintfinfo *sinfo = container_of(info, struct snprintfinfo, info); + if (sinfo->str >= sinfo->end) + return; + *sinfo->str = c; + sinfo->str++; +} + +// Build a formatted string. Note, this function returns the actual +// number of bytes used (not including null) even in the overflow +// case. +int +snprintf(char *str, size_t size, const char *fmt, ...) +{ + ASSERT32FLAT(); + if (!size) + return 0; + struct snprintfinfo sinfo = { { putc_str }, str, str + size }; va_list args; va_start(args, fmt); - bvprintf(1, fmt, args); + bvprintf(&sinfo.info, fmt, args); va_end(args); + char *end = sinfo.str; + if (end >= sinfo.end) + end = sinfo.end - 1; + *end = '\0'; + return end - str; } +// Build a formatted string - malloc'ing the memory. +char * +znprintf(size_t size, const char *fmt, ...) +{ + ASSERT32FLAT(); + if (!size) + return NULL; + char *str = malloc_tmp(size); + if (!str) { + warn_noalloc(); + return NULL; + } + struct snprintfinfo sinfo = { { putc_str }, str, str + size }; + va_list args; + va_start(args, fmt); + bvprintf(&sinfo.info, fmt, args); + va_end(args); + char *end = sinfo.str; + if (end >= sinfo.end) + end = sinfo.end - 1; + *end = '\0'; + return str; +} + + +/**************************************************************** + * Misc helpers + ****************************************************************/ + void -hexdump(void *d, int len) +hexdump(const void *d, int len) { int count=0; - while (len) { + while (len > 0) { if (count % 8 == 0) { - putc(0, '\n'); - puthex(0, count*4, 8); - putc(0, ':'); + putc(&debuginfo, '\n'); + puthex(&debuginfo, count*4, 8, 0); + putc(&debuginfo, ':'); } else { - putc(0, ' '); + putc(&debuginfo, ' '); } - puthex(0, *(u32*)d, 8); + puthex(&debuginfo, *(u32*)d, 8, 0); count++; len-=4; d+=4; } - putc(0, '\n'); + putc(&debuginfo, '\n'); debug_serial_flush(); } @@ -335,8 +477,8 @@ dump_regs(struct bregs *regs) void __debug_isr(const char *fname) { - puts_cs(0, fname); - putc(0, '\n'); + puts_cs(&debuginfo, fname); + putc(&debuginfo, '\n'); debug_serial_flush(); } @@ -356,25 +498,84 @@ __debug_stub(struct bregs *regs, int lineno, const char *fname) dump_regs(regs); } -// Report on a handler returning a failure notification to the caller. +// Report on an invalid parameter. void -__set_fail(struct bregs *regs, int lineno, const char *fname) +__warn_invalid(struct bregs *regs, int lineno, const char *fname) { - dprintf(1, "fail %s:%d:\n", fname, lineno); - dump_regs(regs); - set_fail_silent(regs); + if (CONFIG_DEBUG_LEVEL >= DEBUG_invalid) { + dprintf(1, "invalid %s:%d:\n", fname, lineno); + dump_regs(regs); + } +} + +// Report on an unimplemented feature. +void +__warn_unimplemented(struct bregs *regs, int lineno, const char *fname) +{ + if (CONFIG_DEBUG_LEVEL >= DEBUG_unimplemented) { + dprintf(1, "unimplemented %s:%d:\n", fname, lineno); + dump_regs(regs); + } +} + +// Report a detected internal inconsistency. +void +__warn_internalerror(int lineno, const char *fname) +{ + dprintf(1, "WARNING - internal error detected at %s:%d!\n" + , fname, lineno); +} + +// Report on an allocation failure. +void +__warn_noalloc(int lineno, const char *fname) +{ + dprintf(1, "WARNING - Unable to allocate resource at %s:%d!\n" + , fname, lineno); +} + +// Report on a timeout exceeded. +void +__warn_timeout(int lineno, const char *fname) +{ + dprintf(1, "WARNING - Timeout at %s:%d!\n", fname, lineno); +} + +// Report a handler reporting an invalid parameter to the caller. +void +__set_invalid(struct bregs *regs, int lineno, const char *fname) +{ + __warn_invalid(regs, lineno, fname); + set_invalid_silent(regs); } -// Report on a handler returning a failure code to the caller. Note, -// the lineno and return code are encoded in the same parameter as gcc -// does a better job of scheduling function calls when there are 3 or -// less parameters. +// Report a call of an unimplemented function. void -__set_code_fail(struct bregs *regs, u32 linecode, const char *fname) +__set_unimplemented(struct bregs *regs, int lineno, const char *fname) +{ + __warn_unimplemented(regs, lineno, fname); + set_invalid_silent(regs); +} + +// Report a handler reporting an invalid parameter code to the +// caller. Note, the lineno and return code are encoded in the same +// parameter as gcc does a better job of scheduling function calls +// when there are 3 or less parameters. +void +__set_code_invalid(struct bregs *regs, u32 linecode, const char *fname) { u8 code = linecode; u32 lineno = linecode >> 8; - dprintf(1, "fail %s:%d(%x):\n", fname, lineno, code); - dump_regs(regs); - set_code_fail_silent(regs, code); + __warn_invalid(regs, lineno, fname); + set_code_invalid_silent(regs, code); +} + +// Report a call of an unimplemented function. +void +__set_code_unimplemented(struct bregs *regs, u32 linecode, const char *fname) +{ + u8 code = linecode; + u32 lineno = linecode >> 8; + __warn_unimplemented(regs, lineno, fname); + set_code_invalid_silent(regs, code); }