blob: dd647a26e63f4cb0e68ad71b28f79140b388efb3 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 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
Kevin O'Connor567e4e32008-04-05 11:37:51 -040010#include "util.h" // printf
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050011#include "biosvar.h" // struct bregs
12
Kevin O'Connor5c732402008-06-07 10:43:07 -040013#define DEBUG_PORT 0x03f8
14#define DEBUG_TIMEOUT 100000
15
Kevin O'Connor61d6b062008-06-21 12:15:10 -040016void
17debug_serial_setup()
18{
19 if (!CONFIG_DEBUG_SERIAL)
20 return;
21 // setup for serial logging: 8N1
22 u8 oldparam, newparam = 0x03;
23 oldparam = inb(DEBUG_PORT+3);
24 outb(newparam, DEBUG_PORT+3);
25 // Disable irqs
26 u8 oldier, newier = 0;
27 oldier = inb(DEBUG_PORT+1);
28 outb(newier, DEBUG_PORT+1);
29
30 if (oldparam != newparam || oldier != newier)
31 dprintf(1, "Changing serial settings was %x/%x now %x/%x\n"
32 , oldparam, oldier, newparam, newier);
33}
34
Kevin O'Connor5c732402008-06-07 10:43:07 -040035static void
36debug_serial(char c)
37{
38 int timeout = DEBUG_TIMEOUT;
39 while ((inb(DEBUG_PORT+5) & 0x60) != 0x60)
40 if (!timeout--)
41 // Ran out of time.
42 return;
43 outb(c, DEBUG_PORT);
44}
45
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050046static void
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050047screenc(u8 c)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050048{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050049 struct bregs br;
50 memset(&br, 0, sizeof(br));
51 br.ah = 0x0e;
52 br.al = c;
53 call16_int(0x10, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050054}
55
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050056// Write a charcter to the framebuffer.
57static void
58putc(u16 action, char c)
59{
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040060 if (CONFIG_DEBUG_LEVEL) {
61 if (! CONFIG_COREBOOT)
62 // Send character to debug port.
63 outb(c, PORT_BIOS_DEBUG);
64 if (CONFIG_DEBUG_SERIAL)
65 // Send character to serial port.
66 debug_serial(c);
67 }
Kevin O'Connor1812e202008-05-07 21:29:50 -040068
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050069 if (action) {
Kevin O'Connor1812e202008-05-07 21:29:50 -040070 // Send character to video screen.
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050071 if (c == '\n')
72 screenc('\r');
73 screenc(c);
74 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050075}
76
77// Write a string to the framebuffer.
78static void
79puts(u16 action, const char *s)
80{
81 for (; *s; s++)
82 putc(action, *s);
83}
84
85// Write a string to the framebuffer.
86static void
87puts_cs(u16 action, const char *s)
88{
89 for (;; s++) {
Kevin O'Connor41d28102008-03-30 23:22:39 -040090 char c = GET_VAR(CS, *(u8*)s);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050091 if (!c)
92 break;
93 putc(action, c);
94 }
95}
96
97// Write an unsigned integer to the screen.
98static void
99putuint(u16 action, u32 val)
100{
101 char buf[12];
102 char *d = &buf[sizeof(buf) - 1];
103 *d-- = '\0';
104 for (;;) {
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500105 *d = (val % 10) + '0';
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500106 val /= 10;
107 if (!val)
108 break;
109 d--;
110 }
111 puts(action, d);
112}
113
114// Write a single digit hex character to the screen.
115static inline void
116putsinglehex(u16 action, u32 val)
117{
118 if (val <= 9)
119 val = '0' + val;
120 else
121 val = 'a' + val - 10;
122 putc(action, val);
123}
124
125// Write an integer in hexadecimal to the screen.
126static void
127puthex(u16 action, u32 val)
128{
129 putsinglehex(action, (val >> 28) & 0xf);
130 putsinglehex(action, (val >> 24) & 0xf);
131 putsinglehex(action, (val >> 20) & 0xf);
132 putsinglehex(action, (val >> 16) & 0xf);
133 putsinglehex(action, (val >> 12) & 0xf);
134 putsinglehex(action, (val >> 8) & 0xf);
135 putsinglehex(action, (val >> 4) & 0xf);
136 putsinglehex(action, (val >> 0) & 0xf);
137}
138
Kevin O'Connor7d0f08a2008-03-04 22:27:55 -0500139static inline int
140isdigit(u8 c)
141{
142 return c - '0' < 10;
143}
144
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400145static void
146bvprintf(u16 action, const char *fmt, va_list args)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500147{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500148 const char *s = fmt;
149 for (;; s++) {
Kevin O'Connor41d28102008-03-30 23:22:39 -0400150 char c = GET_VAR(CS, *(u8*)s);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500151 if (!c)
152 break;
153 if (c != '%') {
154 putc(action, c);
155 continue;
156 }
157 const char *n = s+1;
Kevin O'Connor7d0f08a2008-03-04 22:27:55 -0500158 for (;;) {
Kevin O'Connor41d28102008-03-30 23:22:39 -0400159 c = GET_VAR(CS, *(u8*)n);
Kevin O'Connor7d0f08a2008-03-04 22:27:55 -0500160 if (!isdigit(c))
161 break;
162 n++;
163 }
Kevin O'Connora9096f42008-03-08 15:40:43 -0500164 if (c == 'l') {
165 // Ignore long format indicator
166 n++;
Kevin O'Connor41d28102008-03-30 23:22:39 -0400167 c = GET_VAR(CS, *(u8*)n);
Kevin O'Connora9096f42008-03-08 15:40:43 -0500168 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500169 s32 val;
170 const char *sarg;
171 switch (c) {
172 case '%':
173 putc(action, '%');
174 break;
175 case 'd':
176 val = va_arg(args, s32);
177 if (val < 0) {
178 putc(action, '-');
179 val = -val;
180 }
181 putuint(action, val);
182 break;
183 case 'u':
184 val = va_arg(args, s32);
185 putuint(action, val);
186 break;
Kevin O'Connorf06f03a2008-03-29 12:44:32 -0400187 case 'p':
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500188 case 'x':
189 val = va_arg(args, s32);
190 puthex(action, val);
191 break;
Kevin O'Connor12dade52008-03-01 13:35:17 -0500192 case 'c':
193 val = va_arg(args, int);
194 putc(action, val);
195 break;
Kevin O'Connore0113c92008-04-05 15:51:12 -0400196 case '.':
197 // Hack to support "%.s" - meaning string on stack.
198 if (GET_VAR(CS, *(u8*)(n+1)) != 's')
199 break;
200 n++;
201 sarg = va_arg(args, const char *);
202 puts(action, sarg);
203 break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500204 case 's':
205 sarg = va_arg(args, const char *);
206 puts_cs(action, sarg);
207 break;
208 default:
Kevin O'Connor4ce6a492008-02-29 00:21:27 -0500209 putc(action, '%');
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500210 n = s;
211 }
212 s = n;
213 }
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400214}
215
216void
217BX_PANIC(const char *fmt, ...)
218{
219 va_list args;
220 va_start(args, fmt);
221 bvprintf(0, fmt, args);
222 va_end(args);
223
224 // XXX - use PANIC PORT.
225 irq_disable();
226 for (;;)
227 hlt();
228}
229
230void
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400231__dprintf(const char *fmt, ...)
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400232{
233 va_list args;
234 va_start(args, fmt);
235 bvprintf(0, fmt, args);
236 va_end(args);
237}
238
239void
240printf(const char *fmt, ...)
241{
242 va_list args;
243 va_start(args, fmt);
244 bvprintf(1, fmt, args);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500245 va_end(args);
246}
247
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500248static void
249dump_regs(const char *fname, const char *type, struct bregs *regs)
250{
251 if (!regs) {
Kevin O'Connor5c732402008-06-07 10:43:07 -0400252 dprintf(1, "%s %s: NULL\n", type, fname);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500253 return;
254 }
Kevin O'Connor5c732402008-06-07 10:43:07 -0400255 dprintf(1, "%s %s: a=%x b=%x c=%x d=%x si=%x di=%x\n"
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500256 , type, fname, regs->eax, regs->ebx, regs->ecx, regs->edx
257 , regs->esi, regs->edi);
Kevin O'Connor5c732402008-06-07 10:43:07 -0400258 dprintf(1, " ds=%x es=%x ip=%x cs=%x f=%x r=%p\n"
Kevin O'Connorf06f03a2008-03-29 12:44:32 -0400259 , regs->ds, regs->es, regs->ip, regs->cs, regs->flags, regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500260}
261
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500262void
Kevin O'Connored128492008-03-11 11:14:59 -0400263__debug_isr(const char *fname)
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500264{
265 puts_cs(0, fname);
266 putc(0, '\n');
267}
268
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500269// Function called on handler startup.
270void
271__debug_enter(const char *fname, struct bregs *regs)
272{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500273 // XXX - implement run time suppression test
274 dump_regs(fname, "enter", regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500275}
276
277void
Kevin O'Connor6c781222008-03-09 12:19:23 -0400278__debug_fail(const char *fname, struct bregs *regs)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500279{
Kevin O'Connor6c781222008-03-09 12:19:23 -0400280 dump_regs(fname, "fail", regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500281}
Kevin O'Connor4ce6a492008-02-29 00:21:27 -0500282
283void
284__debug_stub(const char *fname, struct bregs *regs)
285{
286 dump_regs(fname, "stub", regs);
287}