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);
87 static struct putcinfo debuginfo = { putc_debug };
90 /****************************************************************
92 ****************************************************************/
94 // Show a character on the screen.
99 memset(&br, 0, sizeof(br));
103 call16_int(0x10, &br);
106 // Handle a character from a printf request.
108 putc_screen(struct putcinfo *action, char c)
110 if (CONFIG_SCREEN_AND_DEBUG)
111 putc_debug(action, c);
117 static struct putcinfo screeninfo = { putc_screen };
120 /****************************************************************
122 ****************************************************************/
124 // Output a character.
126 putc(struct putcinfo *action, char c)
129 // Only debugging output supported in 16bit mode.
130 putc_debug(action, c);
134 void (*func)(struct putcinfo *info, char c) = GET_GLOBAL(action->func);
140 puts(struct putcinfo *action, const char *s)
146 // Output a string that is in the CS segment.
148 puts_cs(struct putcinfo *action, const char *s)
152 char c = GET_GLOBAL(*vs);
159 // Output an unsigned integer.
161 putuint(struct putcinfo *action, u32 val)
164 char *d = &buf[sizeof(buf) - 1];
167 *d = (val % 10) + '0';
176 // Output a single digit hex character.
178 putsinglehex(struct putcinfo *action, u32 val)
183 val = 'a' + val - 10;
187 // Output an integer in hexadecimal.
189 puthex(struct putcinfo *action, u32 val, int width)
207 default: putsinglehex(action, (val >> 28) & 0xf);
208 case 7: putsinglehex(action, (val >> 24) & 0xf);
209 case 6: putsinglehex(action, (val >> 20) & 0xf);
210 case 5: putsinglehex(action, (val >> 16) & 0xf);
211 case 4: putsinglehex(action, (val >> 12) & 0xf);
212 case 3: putsinglehex(action, (val >> 8) & 0xf);
213 case 2: putsinglehex(action, (val >> 4) & 0xf);
214 case 1: putsinglehex(action, (val >> 0) & 0xf);
221 return ((u8)(c - '0')) < 10;
225 bvprintf(struct putcinfo *action, const char *fmt, va_list args)
229 char c = GET_GLOBAL(*(u8*)s);
239 c = GET_GLOBAL(*(u8*)n);
242 field_width = field_width * 10 + c - '0';
246 // Ignore long format indicator
248 c = GET_GLOBAL(*(u8*)n);
257 val = va_arg(args, s32);
262 putuint(action, val);
265 val = va_arg(args, s32);
266 putuint(action, val);
269 /* %p always has 0x prepended */
274 val = va_arg(args, s32);
275 puthex(action, val, field_width);
278 val = va_arg(args, int);
282 // Hack to support "%.s" - meaning string on stack.
283 if (GET_GLOBAL(*(u8*)(n+1)) != 's')
286 sarg = va_arg(args, const char *);
290 sarg = va_arg(args, const char *);
291 puts_cs(action, sarg);
302 panic(const char *fmt, ...)
304 if (CONFIG_DEBUG_LEVEL) {
307 bvprintf(&debuginfo, fmt, args);
309 debug_serial_flush();
312 // XXX - use PANIC PORT.
319 __dprintf(const char *fmt, ...)
323 bvprintf(&debuginfo, fmt, args);
325 debug_serial_flush();
329 printf(const char *fmt, ...)
334 bvprintf(&screeninfo, fmt, args);
336 if (CONFIG_SCREEN_AND_DEBUG)
337 debug_serial_flush();
341 /****************************************************************
343 ****************************************************************/
345 struct snprintfinfo {
346 struct putcinfo info;
351 putc_str(struct putcinfo *info, char c)
353 struct snprintfinfo *sinfo = container_of(info, struct snprintfinfo, info);
354 if (sinfo->str >= sinfo->end)
361 snprintf(char *str, size_t size, const char *fmt, ...)
366 struct snprintfinfo sinfo = { { putc_str }, str, str + size };
369 bvprintf(&sinfo.info, fmt, args);
371 char *end = sinfo.str;
372 if (end >= sinfo.end)
378 /****************************************************************
380 ****************************************************************/
383 hexdump(const void *d, int len)
387 if (count % 8 == 0) {
388 putc(&debuginfo, '\n');
389 puthex(&debuginfo, count*4, 8);
390 putc(&debuginfo, ':');
392 putc(&debuginfo, ' ');
394 puthex(&debuginfo, *(u32*)d, 8);
399 putc(&debuginfo, '\n');
400 debug_serial_flush();
404 dump_regs(struct bregs *regs)
407 dprintf(1, " NULL\n");
410 dprintf(1, " a=%08x b=%08x c=%08x d=%08x ds=%04x es=%04x ss=%04x\n"
411 , regs->eax, regs->ebx, regs->ecx, regs->edx
412 , regs->ds, regs->es, GET_SEG(SS));
413 dprintf(1, " si=%08x di=%08x bp=%08x sp=%08x cs=%04x ip=%04x f=%04x\n"
414 , regs->esi, regs->edi, regs->ebp, (u32)®s[1]
415 , regs->code.seg, regs->code.offset, regs->flags);
418 // Report entry to an Interrupt Service Routine (ISR).
420 __debug_isr(const char *fname)
422 puts_cs(&debuginfo, fname);
423 putc(&debuginfo, '\n');
424 debug_serial_flush();
427 // Function called on handler startup.
429 __debug_enter(struct bregs *regs, const char *fname)
431 dprintf(1, "enter %s:\n", fname);
435 // Send debugging output info.
437 __debug_stub(struct bregs *regs, int lineno, const char *fname)
439 dprintf(1, "stub %s:%d:\n", fname, lineno);
443 // Report on a handler returning a failure notification to the caller.
445 __set_fail(struct bregs *regs, int lineno, const char *fname)
447 dprintf(1, "fail %s:%d:\n", fname, lineno);
449 set_fail_silent(regs);
452 // Report on a handler returning a failure code to the caller. Note,
453 // the lineno and return code are encoded in the same parameter as gcc
454 // does a better job of scheduling function calls when there are 3 or
457 __set_code_fail(struct bregs *regs, u32 linecode, const char *fname)
460 u32 lineno = linecode >> 8;
461 dprintf(1, "fail %s:%d(%x):\n", fname, lineno, code);
463 set_code_fail_silent(regs, code);