Improve debugging output.
[seabios.git] / src / output.c
1 // Raw screen writing code.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU GPLv3 license.
6
7 #include <stdarg.h> // va_list
8
9 #include "farptr.h" // GET_VAR
10 #include "util.h" // printf
11 #include "bregs.h" // struct bregs
12 #include "config.h" // CONFIG_*
13
14 #define DEBUG_PORT 0x03f8
15 #define DEBUG_TIMEOUT 100000
16
17 void
18 debug_serial_setup()
19 {
20     if (!CONFIG_DEBUG_SERIAL)
21         return;
22     // setup for serial logging: 8N1
23     u8 oldparam, newparam = 0x03;
24     oldparam = inb(DEBUG_PORT+3);
25     outb(newparam, DEBUG_PORT+3);
26     // Disable irqs
27     u8 oldier, newier = 0;
28     oldier = inb(DEBUG_PORT+1);
29     outb(newier, DEBUG_PORT+1);
30
31     if (oldparam != newparam || oldier != newier)
32         dprintf(1, "Changing serial settings was %x/%x now %x/%x\n"
33                 , oldparam, oldier, newparam, newier);
34 }
35
36 static void
37 debug_serial(char c)
38 {
39     int timeout = DEBUG_TIMEOUT;
40     while ((inb(DEBUG_PORT+5) & 0x60) != 0x60)
41         if (!timeout--)
42             // Ran out of time.
43             return;
44     outb(c, DEBUG_PORT);
45 }
46
47 static void
48 screenc(u8 c)
49 {
50     struct bregs br;
51     memset(&br, 0, sizeof(br));
52     br.ah = 0x0e;
53     br.al = c;
54     call16_int(0x10, &br);
55 }
56
57 // Write a charcter to the framebuffer.
58 static void
59 putc(u16 action, char c)
60 {
61     if (CONFIG_DEBUG_LEVEL) {
62         if (! CONFIG_COREBOOT)
63             // Send character to debug port.
64             outb(c, PORT_BIOS_DEBUG);
65         if (CONFIG_DEBUG_SERIAL) {
66             // Send character to serial port.
67             if (c == '\n')
68                 debug_serial('\r');
69             debug_serial(c);
70         }
71     }
72
73     if (action) {
74         // Send character to video screen.
75         if (c == '\n')
76             screenc('\r');
77         screenc(c);
78     }
79 }
80
81 // Write a string to the framebuffer.
82 static void
83 puts(u16 action, const char *s)
84 {
85     for (; *s; s++)
86         putc(action, *s);
87 }
88
89 // Write a string to the framebuffer.
90 static void
91 puts_cs(u16 action, const char *s)
92 {
93     for (;; s++) {
94         char c = GET_VAR(CS, *(u8*)s);
95         if (!c)
96             break;
97         putc(action, c);
98     }
99 }
100
101 // Write an unsigned integer to the screen.
102 static void
103 putuint(u16 action, u32 val)
104 {
105     char buf[12];
106     char *d = &buf[sizeof(buf) - 1];
107     *d-- = '\0';
108     for (;;) {
109         *d = (val % 10) + '0';
110         val /= 10;
111         if (!val)
112             break;
113         d--;
114     }
115     puts(action, d);
116 }
117
118 // Write a single digit hex character to the screen.
119 static inline void
120 putsinglehex(u16 action, u32 val)
121 {
122     if (val <= 9)
123         val = '0' + val;
124     else
125         val = 'a' + val - 10;
126     putc(action, val);
127 }
128
129 // Write an integer in hexadecimal to the screen.
130 static void
131 puthex(u16 action, u32 val)
132 {
133     putsinglehex(action, (val >> 28) & 0xf);
134     putsinglehex(action, (val >> 24) & 0xf);
135     putsinglehex(action, (val >> 20) & 0xf);
136     putsinglehex(action, (val >> 16) & 0xf);
137     putsinglehex(action, (val >> 12) & 0xf);
138     putsinglehex(action, (val >> 8) & 0xf);
139     putsinglehex(action, (val >> 4) & 0xf);
140     putsinglehex(action, (val >> 0) & 0xf);
141 }
142
143 static inline int
144 isdigit(u8 c)
145 {
146     return c - '0' < 10;
147 }
148
149 static void
150 bvprintf(u16 action, const char *fmt, va_list args)
151 {
152     const char *s = fmt;
153     for (;; s++) {
154         char c = GET_VAR(CS, *(u8*)s);
155         if (!c)
156             break;
157         if (c != '%') {
158             putc(action, c);
159             continue;
160         }
161         const char *n = s+1;
162         for (;;) {
163             c = GET_VAR(CS, *(u8*)n);
164             if (!isdigit(c))
165                 break;
166             n++;
167         }
168         if (c == 'l') {
169             // Ignore long format indicator
170             n++;
171             c = GET_VAR(CS, *(u8*)n);
172         }
173         s32 val;
174         const char *sarg;
175         switch (c) {
176         case '%':
177             putc(action, '%');
178             break;
179         case 'd':
180             val = va_arg(args, s32);
181             if (val < 0) {
182                 putc(action, '-');
183                 val = -val;
184             }
185             putuint(action, val);
186             break;
187         case 'u':
188             val = va_arg(args, s32);
189             putuint(action, val);
190             break;
191         case 'p':
192         case 'x':
193             val = va_arg(args, s32);
194             puthex(action, val);
195             break;
196         case 'c':
197             val = va_arg(args, int);
198             putc(action, val);
199             break;
200         case '.':
201             // Hack to support "%.s" - meaning string on stack.
202             if (GET_VAR(CS, *(u8*)(n+1)) != 's')
203                 break;
204             n++;
205             sarg = va_arg(args, const char *);
206             puts(action, sarg);
207             break;
208         case 's':
209             sarg = va_arg(args, const char *);
210             puts_cs(action, sarg);
211             break;
212         default:
213             putc(action, '%');
214             n = s;
215         }
216         s = n;
217     }
218 }
219
220 void
221 BX_PANIC(const char *fmt, ...)
222 {
223     va_list args;
224     va_start(args, fmt);
225     bvprintf(0, fmt, args);
226     va_end(args);
227
228     // XXX - use PANIC PORT.
229     irq_disable();
230     for (;;)
231         hlt();
232 }
233
234 void
235 __dprintf(const char *fmt, ...)
236 {
237     va_list args;
238     va_start(args, fmt);
239     bvprintf(0, fmt, args);
240     va_end(args);
241 }
242
243 void
244 printf(const char *fmt, ...)
245 {
246     va_list args;
247     va_start(args, fmt);
248     bvprintf(1, fmt, args);
249     va_end(args);
250 }
251
252 static void
253 dump_regs(struct bregs *regs)
254 {
255     if (!regs) {
256         dprintf(1, "  NULL\n");
257         return;
258     }
259     dprintf(1, "  a=%x b=%x c=%x d=%x si=%x di=%x\n"
260             , regs->eax, regs->ebx, regs->ecx, regs->edx
261             , regs->esi, regs->edi);
262     dprintf(1, "  ds=%x es=%x ip=%x cs=%x f=%x r=%p\n"
263             , regs->ds, regs->es, regs->ip, regs->cs, regs->flags, regs);
264 }
265
266 void
267 __debug_isr(const char *fname)
268 {
269     puts_cs(0, fname);
270     putc(0, '\n');
271 }
272
273 // Function called on handler startup.
274 void
275 __debug_enter(const char *fname, struct bregs *regs)
276 {
277     dprintf(1, "enter %s:\n", fname);
278     dump_regs(regs);
279 }
280
281 void
282 __debug_stub(const char *fname, int lineno, struct bregs *regs)
283 {
284     dprintf(1, "stub %s:%d:\n", fname, lineno);
285     dump_regs(regs);
286 }
287
288 void
289 __set_fail(const char *fname, int lineno, struct bregs *regs)
290 {
291     dprintf(1, "fail %s:%d:\n", fname, lineno);
292     dump_regs(regs);
293     set_fail_silent(regs);
294 }
295
296 void
297 __set_code_fail(const char *fname, int lineno, struct bregs *regs, u8 code)
298 {
299     dprintf(1, "fail %s:%d(%x):\n", fname, lineno, code);
300     dump_regs(regs);
301     set_code_fail_silent(regs, code);
302 }