1 // Raw screen writing and debug output code.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include <stdarg.h> // va_list
9 #include "farptr.h" // GET_VAR
10 #include "util.h" // printf
11 #include "bregs.h" // struct bregs
12 #include "config.h" // CONFIG_*
13 #include "biosvar.h" // GET_GLOBAL
15 #define DEBUG_PORT 0x03f8
16 #define DEBUG_TIMEOUT 100000
21 if (!CONFIG_DEBUG_SERIAL)
23 // setup for serial logging: 8N1
24 u8 oldparam, newparam = 0x03;
25 oldparam = inb(DEBUG_PORT+3);
26 outb(newparam, DEBUG_PORT+3);
28 u8 oldier, newier = 0;
29 oldier = inb(DEBUG_PORT+1);
30 outb(newier, DEBUG_PORT+1);
32 if (oldparam != newparam || oldier != newier)
33 dprintf(1, "Changing serial settings was %x/%x now %x/%x\n"
34 , oldparam, oldier, newparam, newier);
37 // Write a character to the serial port.
41 int timeout = DEBUG_TIMEOUT;
42 while ((inb(DEBUG_PORT+5) & 0x60) != 0x60)
49 // Show a character on the screen.
54 // printf is only used in 32bit code.
57 memset(&br, 0, sizeof(br));
60 call16_int(0x10, &br);
63 // Output a character.
65 putc(u16 action, char c)
67 if (CONFIG_DEBUG_LEVEL) {
68 if (! CONFIG_COREBOOT)
69 // Send character to debug port.
70 outb(c, PORT_BIOS_DEBUG);
71 if (CONFIG_DEBUG_SERIAL) {
72 // Send character to serial port.
80 // Send character to video screen.
89 puts(u16 action, const char *s)
95 // Output a string that is in the CS segment.
97 puts_cs(u16 action, const char *s)
100 char c = GET_GLOBAL(*(u8*)s);
107 // Output an unsigned integer.
109 putuint(u16 action, u32 val)
112 char *d = &buf[sizeof(buf) - 1];
115 *d = (val % 10) + '0';
124 // Output a single digit hex character.
126 putsinglehex(u16 action, u32 val)
131 val = 'a' + val - 10;
135 // Output an integer in hexadecimal.
137 puthex(u16 action, u32 val)
139 putsinglehex(action, (val >> 28) & 0xf);
140 putsinglehex(action, (val >> 24) & 0xf);
141 putsinglehex(action, (val >> 20) & 0xf);
142 putsinglehex(action, (val >> 16) & 0xf);
143 putsinglehex(action, (val >> 12) & 0xf);
144 putsinglehex(action, (val >> 8) & 0xf);
145 putsinglehex(action, (val >> 4) & 0xf);
146 putsinglehex(action, (val >> 0) & 0xf);
152 return ((u8)(c - '0')) < 10;
156 bvprintf(u16 action, const char *fmt, va_list args)
160 char c = GET_GLOBAL(*(u8*)s);
169 c = GET_GLOBAL(*(u8*)n);
175 // Ignore long format indicator
177 c = GET_GLOBAL(*(u8*)n);
186 val = va_arg(args, s32);
191 putuint(action, val);
194 val = va_arg(args, s32);
195 putuint(action, val);
199 val = va_arg(args, s32);
203 val = va_arg(args, int);
207 // Hack to support "%.s" - meaning string on stack.
208 if (GET_GLOBAL(*(u8*)(n+1)) != 's')
211 sarg = va_arg(args, const char *);
215 sarg = va_arg(args, const char *);
216 puts_cs(action, sarg);
227 BX_PANIC(const char *fmt, ...)
229 if (CONFIG_DEBUG_LEVEL) {
232 bvprintf(0, fmt, args);
236 // XXX - use PANIC PORT.
243 __dprintf(const char *fmt, ...)
247 bvprintf(0, fmt, args);
252 printf(const char *fmt, ...)
256 bvprintf(1, fmt, args);
261 dump_regs(struct bregs *regs)
264 dprintf(1, " NULL\n");
267 dprintf(1, " a=%x b=%x c=%x d=%x si=%x di=%x\n"
268 , regs->eax, regs->ebx, regs->ecx, regs->edx
269 , regs->esi, regs->edi);
270 dprintf(1, " ds=%x es=%x ip=%x cs=%x f=%x r=%p\n"
271 , regs->ds, regs->es, regs->ip, regs->cs, regs->flags, regs);
274 // Report entry to an Interrupt Service Routine (ISR).
276 __debug_isr(const char *fname)
282 // Function called on handler startup.
284 __debug_enter(struct bregs *regs, const char *fname)
286 dprintf(1, "enter %s:\n", fname);
290 // Send debugging output info.
292 __debug_stub(struct bregs *regs, int lineno, const char *fname)
294 dprintf(1, "stub %s:%d:\n", fname, lineno);
298 // Report on a handler returning a failure notification to the caller.
300 __set_fail(struct bregs *regs, int lineno, const char *fname)
302 dprintf(1, "fail %s:%d:\n", fname, lineno);
304 set_fail_silent(regs);
307 // Report on a handler returning a failure code to the caller. Note,
308 // the lineno and return code are encoded in the same parameter as gcc
309 // does a better job of scheduling function calls when there are 3 or
312 __set_code_fail(struct bregs *regs, u32 linecode, const char *fname)
315 u32 lineno = linecode >> 8;
316 dprintf(1, "fail %s:%d(%x):\n", fname, lineno, code);
318 set_code_fail_silent(regs, code);