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
16 void (*func)(struct putcinfo *info, char c);
20 /****************************************************************
22 ****************************************************************/
24 #define DEBUG_PORT PORT_SERIAL1
25 #define DEBUG_TIMEOUT 100000
30 if (!CONFIG_DEBUG_SERIAL)
32 // setup for serial logging: 8N1
33 u8 oldparam, newparam = 0x03;
34 oldparam = inb(DEBUG_PORT+SEROFF_LCR);
35 outb(newparam, DEBUG_PORT+SEROFF_LCR);
37 u8 oldier, newier = 0;
38 oldier = inb(DEBUG_PORT+SEROFF_IER);
39 outb(newier, DEBUG_PORT+SEROFF_IER);
41 if (oldparam != newparam || oldier != newier)
42 dprintf(1, "Changing serial settings was %x/%x now %x/%x\n"
43 , oldparam, oldier, newparam, newier);
46 // Write a character to the serial port.
50 if (!CONFIG_DEBUG_SERIAL)
52 int timeout = DEBUG_TIMEOUT;
53 while ((inb(DEBUG_PORT+SEROFF_LSR) & 0x60) != 0x60)
57 outb(c, DEBUG_PORT+SEROFF_DATA);
60 // Make sure all serial port writes have been completely sent.
64 if (!CONFIG_DEBUG_SERIAL)
66 int timeout = DEBUG_TIMEOUT;
67 while ((inb(DEBUG_PORT+SEROFF_LSR) & 0x40) != 0x40)
73 // Write a character to debug port(s).
75 putc_debug(struct putcinfo *action, char c)
77 if (! CONFIG_DEBUG_LEVEL)
79 if (! CONFIG_COREBOOT)
80 // Send character to debug port.
81 outb(c, PORT_BIOS_DEBUG);
88 static struct putcinfo debuginfo VAR16 = { putc_debug };
90 static struct putcinfo debuginfo = { putc_debug };
94 /****************************************************************
96 ****************************************************************/
98 // Show a character on the screen.
103 memset(&br, 0, sizeof(br));
107 call16_int(0x10, &br);
110 // Handle a character from a printf request.
112 putc_screen(struct putcinfo *action, char c)
114 if (CONFIG_SCREEN_AND_DEBUG)
115 putc_debug(action, c);
121 static struct putcinfo screeninfo = { putc_screen };
124 /****************************************************************
126 ****************************************************************/
128 // Output a character.
130 putc(struct putcinfo *action, char c)
133 // Only debugging output supported in 16bit mode.
134 putc_debug(action, c);
138 void (*func)(struct putcinfo *info, char c) = GET_GLOBAL(action->func);
144 puts(struct putcinfo *action, const char *s)
150 // Output a string that is in the CS segment.
152 puts_cs(struct putcinfo *action, const char *s)
156 char c = GET_GLOBAL(*vs);
163 // Output an unsigned integer.
165 putuint(struct putcinfo *action, u32 val)
168 char *d = &buf[sizeof(buf) - 1];
171 *d = (val % 10) + '0';
180 // Output a single digit hex character.
182 putsinglehex(struct putcinfo *action, u32 val)
187 val = 'a' + val - 10;
191 // Output an integer in hexadecimal.
193 puthex(struct putcinfo *action, u32 val, int width)
211 default: putsinglehex(action, (val >> 28) & 0xf);
212 case 7: putsinglehex(action, (val >> 24) & 0xf);
213 case 6: putsinglehex(action, (val >> 20) & 0xf);
214 case 5: putsinglehex(action, (val >> 16) & 0xf);
215 case 4: putsinglehex(action, (val >> 12) & 0xf);
216 case 3: putsinglehex(action, (val >> 8) & 0xf);
217 case 2: putsinglehex(action, (val >> 4) & 0xf);
218 case 1: putsinglehex(action, (val >> 0) & 0xf);
225 return ((u8)(c - '0')) < 10;
229 bvprintf(struct putcinfo *action, const char *fmt, va_list args)
233 char c = GET_GLOBAL(*(u8*)s);
243 c = GET_GLOBAL(*(u8*)n);
246 field_width = field_width * 10 + c - '0';
250 // Ignore long format indicator
252 c = GET_GLOBAL(*(u8*)n);
261 val = va_arg(args, s32);
266 putuint(action, val);
269 val = va_arg(args, s32);
270 putuint(action, val);
273 /* %p always has 0x prepended */
278 val = va_arg(args, s32);
279 puthex(action, val, field_width);
282 val = va_arg(args, int);
286 // Hack to support "%.s" - meaning string on stack.
287 if (GET_GLOBAL(*(u8*)(n+1)) != 's')
290 sarg = va_arg(args, const char *);
294 sarg = va_arg(args, const char *);
295 puts_cs(action, sarg);
306 panic(const char *fmt, ...)
308 if (CONFIG_DEBUG_LEVEL) {
311 bvprintf(&debuginfo, fmt, args);
313 debug_serial_flush();
316 // XXX - use PANIC PORT.
323 __dprintf(const char *fmt, ...)
327 bvprintf(&debuginfo, fmt, args);
329 debug_serial_flush();
333 printf(const char *fmt, ...)
338 bvprintf(&screeninfo, fmt, args);
340 if (CONFIG_SCREEN_AND_DEBUG)
341 debug_serial_flush();
345 /****************************************************************
347 ****************************************************************/
349 struct snprintfinfo {
350 struct putcinfo info;
355 putc_str(struct putcinfo *info, char c)
357 struct snprintfinfo *sinfo = container_of(info, struct snprintfinfo, info);
358 if (sinfo->str >= sinfo->end)
365 snprintf(char *str, size_t size, const char *fmt, ...)
370 struct snprintfinfo sinfo = { { putc_str }, str, str + size };
373 bvprintf(&sinfo.info, fmt, args);
375 char *end = sinfo.str;
376 if (end >= sinfo.end)
382 /****************************************************************
384 ****************************************************************/
387 hexdump(const void *d, int len)
391 if (count % 8 == 0) {
392 putc(&debuginfo, '\n');
393 puthex(&debuginfo, count*4, 8);
394 putc(&debuginfo, ':');
396 putc(&debuginfo, ' ');
398 puthex(&debuginfo, *(u32*)d, 8);
403 putc(&debuginfo, '\n');
404 debug_serial_flush();
408 dump_regs(struct bregs *regs)
411 dprintf(1, " NULL\n");
414 dprintf(1, " a=%08x b=%08x c=%08x d=%08x ds=%04x es=%04x ss=%04x\n"
415 , regs->eax, regs->ebx, regs->ecx, regs->edx
416 , regs->ds, regs->es, GET_SEG(SS));
417 dprintf(1, " si=%08x di=%08x bp=%08x sp=%08x cs=%04x ip=%04x f=%04x\n"
418 , regs->esi, regs->edi, regs->ebp, (u32)®s[1]
419 , regs->code.seg, regs->code.offset, regs->flags);
422 // Report entry to an Interrupt Service Routine (ISR).
424 __debug_isr(const char *fname)
426 puts_cs(&debuginfo, fname);
427 putc(&debuginfo, '\n');
428 debug_serial_flush();
431 // Function called on handler startup.
433 __debug_enter(struct bregs *regs, const char *fname)
435 dprintf(1, "enter %s:\n", fname);
439 // Send debugging output info.
441 __debug_stub(struct bregs *regs, int lineno, const char *fname)
443 dprintf(1, "stub %s:%d:\n", fname, lineno);
447 // Report on a handler returning a failure notification to the caller.
449 __set_fail(struct bregs *regs, int lineno, const char *fname)
451 dprintf(1, "fail %s:%d:\n", fname, lineno);
453 set_fail_silent(regs);
456 // Report on a handler returning a failure code to the caller. Note,
457 // the lineno and return code are encoded in the same parameter as gcc
458 // does a better job of scheduling function calls when there are 3 or
461 __set_code_fail(struct bregs *regs, u32 linecode, const char *fname)
464 u32 lineno = linecode >> 8;
465 dprintf(1, "fail %s:%d(%x):\n", fname, lineno, code);
467 set_code_fail_silent(regs, code);