1 // Raw screen writing and debug output code.
3 // Copyright (C) 2008-2013 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 "bregs.h" // struct bregs
11 #include "config.h" // CONFIG_*
12 #include "biosvar.h" // GET_GLOBAL
13 #include "hw/serialio.h" // serial_debug_putc
14 #include "malloc.h" // malloc_tmp
15 #include "output.h" // dprintf
16 #include "stacks.h" // call16_int
17 #include "string.h" // memset
18 #include "util.h" // ScreenAndDebug
21 void (*func)(struct putcinfo *info, char c);
25 /****************************************************************
27 ****************************************************************/
32 dprintf(1, "SeaBIOS (version %s)\n", VERSION);
35 // Write a character to debug port(s).
37 debug_putc(struct putcinfo *action, char c)
39 if (! CONFIG_DEBUG_LEVEL)
43 coreboot_debug_putc(c);
47 // Flush any pending output to debug port(s).
54 // In segmented mode just need a dummy variable (debug_putc is always
55 // used anyway), and in 32bit flat mode need a pointer to the 32bit
56 // instance of debug_putc().
58 static struct putcinfo debuginfo VAR16;
60 static struct putcinfo debuginfo VAR32SEG;
62 static struct putcinfo debuginfo = { debug_putc };
66 /****************************************************************
68 ****************************************************************/
70 // Show a character on the screen.
75 memset(&br, 0, sizeof(br));
80 call16_int(0x10, &br);
83 // Handle a character from a printf request.
85 screen_putc(struct putcinfo *action, char c)
88 debug_putc(&debuginfo, c);
94 static struct putcinfo screeninfo = { screen_putc };
97 /****************************************************************
99 ****************************************************************/
101 // Output a character.
103 putc(struct putcinfo *action, char c)
106 // Only debugging output supported in segmented mode.
107 debug_putc(action, c);
111 void (*func)(struct putcinfo *info, char c) = GET_GLOBAL(action->func);
117 puts(struct putcinfo *action, const char *s)
119 if (!MODESEGMENT && !s)
125 // Output a string that is in the CS segment.
127 puts_cs(struct putcinfo *action, const char *s)
131 char c = GET_GLOBAL(*vs);
138 // Output an unsigned integer.
140 putuint(struct putcinfo *action, u32 val)
143 char *d = &buf[sizeof(buf) - 1];
146 *d = (val % 10) + '0';
155 // Output a single digit hex character.
157 putsinglehex(struct putcinfo *action, u32 val)
162 val = 'a' + val - 10;
166 // Output an integer in hexadecimal with a specified width.
168 puthex(struct putcinfo *action, u32 val, int width)
171 default: putsinglehex(action, (val >> 28) & 0xf);
172 case 7: putsinglehex(action, (val >> 24) & 0xf);
173 case 6: putsinglehex(action, (val >> 20) & 0xf);
174 case 5: putsinglehex(action, (val >> 16) & 0xf);
175 case 4: putsinglehex(action, (val >> 12) & 0xf);
176 case 3: putsinglehex(action, (val >> 8) & 0xf);
177 case 2: putsinglehex(action, (val >> 4) & 0xf);
178 case 1: putsinglehex(action, (val >> 0) & 0xf);
182 // Output an integer in hexadecimal with a minimum width.
184 putprettyhex(struct putcinfo *action, u32 val, int width, char padchar)
192 putc(action, padchar);
193 puthex(action, val, count);
199 return ((u8)(c - '0')) < 10;
203 bvprintf(struct putcinfo *action, const char *fmt, va_list args)
207 char c = GET_GLOBAL(*(u8*)s);
219 c = GET_GLOBAL(*(u8*)n);
222 if (!field_width && (c == '0'))
225 field_width = field_width * 10 + c - '0';
229 // Ignore long format indicator
231 c = GET_GLOBAL(*(u8*)n);
236 c = GET_GLOBAL(*(u8*)n);
245 val = va_arg(args, s32);
252 putuint(action, val);
255 val = va_arg(args, s32);
258 putuint(action, val);
261 val = va_arg(args, s32);
264 puthex(action, val, 8);
267 val = va_arg(args, s32);
269 u32 upper = va_arg(args, s32);
271 putprettyhex(action, upper, field_width - 8, padchar);
272 puthex(action, val, 8);
276 putprettyhex(action, val, field_width, padchar);
279 val = va_arg(args, int);
283 // Hack to support "%.s" - meaning string on stack.
284 if (GET_GLOBAL(*(u8*)(n+1)) != 's')
287 sarg = va_arg(args, const char *);
291 sarg = va_arg(args, const char *);
292 puts_cs(action, sarg);
303 panic(const char *fmt, ...)
305 if (CONFIG_DEBUG_LEVEL) {
308 bvprintf(&debuginfo, fmt, args);
313 // XXX - use PANIC PORT.
320 __dprintf(const char *fmt, ...)
322 if (!MODESEGMENT && CONFIG_THREADS && CONFIG_DEBUG_LEVEL >= DEBUG_thread
323 && *fmt != '\\' && *fmt != '/') {
324 struct thread_info *cur = getCurThread();
325 if (cur != &MainThread) {
326 // Show "thread id" for this debug message.
327 debug_putc(&debuginfo, '|');
328 puthex(&debuginfo, (u32)cur, 8);
329 debug_putc(&debuginfo, '|');
330 debug_putc(&debuginfo, ' ');
336 bvprintf(&debuginfo, fmt, args);
342 printf(const char *fmt, ...)
347 bvprintf(&screeninfo, fmt, args);
354 /****************************************************************
356 ****************************************************************/
358 struct snprintfinfo {
359 struct putcinfo info;
364 putc_str(struct putcinfo *info, char c)
366 struct snprintfinfo *sinfo = container_of(info, struct snprintfinfo, info);
367 if (sinfo->str >= sinfo->end)
373 // Build a formatted string. Note, this function returns the actual
374 // number of bytes used (not including null) even in the overflow
377 snprintf(char *str, size_t size, const char *fmt, ...)
382 struct snprintfinfo sinfo = { { putc_str }, str, str + size };
385 bvprintf(&sinfo.info, fmt, args);
387 char *end = sinfo.str;
388 if (end >= sinfo.end)
394 // Build a formatted string - malloc'ing the memory.
396 znprintf(size_t size, const char *fmt, ...)
401 char *str = malloc_tmp(size);
406 struct snprintfinfo sinfo = { { putc_str }, str, str + size };
409 bvprintf(&sinfo.info, fmt, args);
411 char *end = sinfo.str;
412 if (end >= sinfo.end)
419 /****************************************************************
421 ****************************************************************/
424 hexdump(const void *d, int len)
428 if (count % 8 == 0) {
429 putc(&debuginfo, '\n');
430 puthex(&debuginfo, count*4, 8);
431 putc(&debuginfo, ':');
433 putc(&debuginfo, ' ');
435 puthex(&debuginfo, *(u32*)d, 8);
440 putc(&debuginfo, '\n');
445 dump_regs(struct bregs *regs)
448 dprintf(1, " NULL\n");
451 dprintf(1, " a=%08x b=%08x c=%08x d=%08x ds=%04x es=%04x ss=%04x\n"
452 , regs->eax, regs->ebx, regs->ecx, regs->edx
453 , regs->ds, regs->es, GET_SEG(SS));
454 dprintf(1, " si=%08x di=%08x bp=%08x sp=%08x cs=%04x ip=%04x f=%04x\n"
455 , regs->esi, regs->edi, regs->ebp, (u32)®s[1]
456 , regs->code.seg, regs->code.offset, regs->flags);
459 // Report entry to an Interrupt Service Routine (ISR).
461 __debug_isr(const char *fname)
463 puts_cs(&debuginfo, fname);
464 putc(&debuginfo, '\n');
468 // Function called on handler startup.
470 __debug_enter(struct bregs *regs, const char *fname)
472 dprintf(1, "enter %s:\n", fname);
476 // Send debugging output info.
478 __debug_stub(struct bregs *regs, int lineno, const char *fname)
480 dprintf(1, "stub %s:%d:\n", fname, lineno);
484 // Report on an invalid parameter.
486 __warn_invalid(struct bregs *regs, int lineno, const char *fname)
488 if (CONFIG_DEBUG_LEVEL >= DEBUG_invalid) {
489 dprintf(1, "invalid %s:%d:\n", fname, lineno);
494 // Report on an unimplemented feature.
496 __warn_unimplemented(struct bregs *regs, int lineno, const char *fname)
498 if (CONFIG_DEBUG_LEVEL >= DEBUG_unimplemented) {
499 dprintf(1, "unimplemented %s:%d:\n", fname, lineno);
504 // Report a detected internal inconsistency.
506 __warn_internalerror(int lineno, const char *fname)
508 dprintf(1, "WARNING - internal error detected at %s:%d!\n"
512 // Report on an allocation failure.
514 __warn_noalloc(int lineno, const char *fname)
516 dprintf(1, "WARNING - Unable to allocate resource at %s:%d!\n"
520 // Report on a timeout exceeded.
522 __warn_timeout(int lineno, const char *fname)
524 dprintf(1, "WARNING - Timeout at %s:%d!\n", fname, lineno);
527 // Report a handler reporting an invalid parameter to the caller.
529 __set_invalid(struct bregs *regs, int lineno, const char *fname)
531 __warn_invalid(regs, lineno, fname);
532 set_invalid_silent(regs);
535 // Report a call of an unimplemented function.
537 __set_unimplemented(struct bregs *regs, int lineno, const char *fname)
539 __warn_unimplemented(regs, lineno, fname);
540 set_invalid_silent(regs);
543 // Report a handler reporting an invalid parameter code to the
544 // caller. Note, the lineno and return code are encoded in the same
545 // parameter as gcc does a better job of scheduling function calls
546 // when there are 3 or less parameters.
548 __set_code_invalid(struct bregs *regs, u32 linecode, const char *fname)
551 u32 lineno = linecode >> 8;
552 __warn_invalid(regs, lineno, fname);
553 set_code_invalid_silent(regs, code);
556 // Report a call of an unimplemented function.
558 __set_code_unimplemented(struct bregs *regs, u32 linecode, const char *fname)
561 u32 lineno = linecode >> 8;
562 __warn_unimplemented(regs, lineno, fname);
563 set_code_invalid_silent(regs, code);