[FREELDR]
[reactos.git] / reactos / boot / freeldr / freeldr / debug.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <freeldr.h>
21
22 #include <debug.h>
23
24 #if DBG && !defined(_M_ARM)
25
26 //#define DEBUG_ALL
27 //#define DEBUG_INIFILE
28 //#define DEBUG_REACTOS
29 #define DEBUG_CUSTOM
30 #define DEBUG_NONE
31
32 #if defined (DEBUG_ALL)
33 ULONG DebugPrintMask = DPRINT_WARNING | DPRINT_MEMORY | DPRINT_FILESYSTEM |
34 DPRINT_UI | DPRINT_DISK | DPRINT_CACHE | DPRINT_REACTOS |
35 DPRINT_LINUX | DPRINT_HWDETECT | DPRINT_PELOADER | DPRINT_WINDOWS;
36 #elif defined (DEBUG_INIFILE)
37 ULONG DebugPrintMask = DPRINT_INIFILE;
38 #elif defined (DEBUG_REACTOS)
39 ULONG DebugPrintMask = DPRINT_REACTOS | DPRINT_REGISTRY;
40 #elif defined (DEBUG_CUSTOM)
41 ULONG DebugPrintMask = DPRINT_WARNING | DPRINT_MEMORY;
42 #else //#elif defined (DEBUG_NONE)
43 ULONG DebugPrintMask = 0;
44 #endif
45
46 #define SCREEN 1
47 #define RS232 2
48 #define BOCHS 4
49
50 #define COM1 1
51 #define COM2 2
52 #define COM3 3
53 #define COM4 4
54
55 #define BOCHS_OUTPUT_PORT 0xe9
56
57 ULONG DebugPort = RS232;
58 //ULONG DebugPort = SCREEN;
59 //ULONG DebugPort = BOCHS;
60 //ULONG DebugPort = SCREEN|BOCHS;
61 ULONG ComPort = COM1;
62 //ULONG BaudRate = 19200;
63 ULONG BaudRate = 115200;
64
65 BOOLEAN DebugStartOfLine = TRUE;
66
67 VOID DebugInit(VOID)
68 {
69 if (DebugPort & RS232)
70 {
71 Rs232PortInitialize(ComPort, BaudRate);
72 }
73 }
74
75 VOID DebugPrintChar(UCHAR Character)
76 {
77 if (Character == '\n')
78 {
79 DebugStartOfLine = TRUE;
80 }
81
82 if (DebugPort & RS232)
83 {
84 if (Character == '\n')
85 {
86 Rs232PortPutByte('\r');
87 }
88 Rs232PortPutByte(Character);
89 }
90 if (DebugPort & BOCHS)
91 {
92 WRITE_PORT_UCHAR((PUCHAR)BOCHS_OUTPUT_PORT, Character);
93 }
94 if (DebugPort & SCREEN)
95 {
96 MachConsPutChar(Character);
97 }
98 }
99
100 ULONG
101 DbgPrint(const char *Format, ...)
102 {
103 int i;
104 int Length;
105 va_list ap;
106 CHAR Buffer[512];
107
108 va_start(ap, Format);
109 Length = _vsnprintf(Buffer, sizeof(Buffer), Format, ap);
110 va_end(ap);
111
112 /* Check if we went past the buffer */
113 if (Length == -1)
114 {
115 /* Terminate it if we went over-board */
116 Buffer[sizeof(Buffer) - 1] = '\n';
117
118 /* Put maximum */
119 Length = sizeof(Buffer);
120 }
121
122 for (i = 0; i < Length; i++)
123 {
124 DebugPrintChar(Buffer[i]);
125 }
126
127 return 0;
128 }
129
130 VOID
131 DbgPrint2(ULONG Mask, ULONG Level, const char *File, ULONG Line, char *Format, ...)
132 {
133 va_list ap;
134 char Buffer[2096];
135 char *ptr = Buffer;
136
137 // Mask out unwanted debug messages
138 if (Level >= WARN_LEVEL && !(Mask & DebugPrintMask))
139 {
140 return;
141 }
142
143 // Print the header if we have started a new line
144 if (DebugStartOfLine)
145 {
146 DbgPrint("(%s:%lu) ", File, Line);
147
148 switch (Level)
149 {
150 case ERR_LEVEL:
151 DbgPrint("err: ");
152 break;
153 case FIXME_LEVEL:
154 DbgPrint("fixme: ");
155 break;
156 case WARN_LEVEL:
157 DbgPrint("warn: ");
158 break;
159 case TRACE_LEVEL:
160 DbgPrint("trace: ");
161 break;
162 }
163
164 DebugStartOfLine = FALSE;
165 }
166
167 va_start(ap, Format);
168 vsprintf(Buffer, Format, ap);
169 va_end(ap);
170
171 while (*ptr)
172 {
173 DebugPrintChar(*ptr++);
174 }
175 }
176
177 VOID
178 DebugDumpBuffer(ULONG Mask, PVOID Buffer, ULONG Length)
179 {
180 PUCHAR BufPtr = (PUCHAR)Buffer;
181 ULONG Idx;
182 ULONG Idx2;
183
184 // Mask out unwanted debug messages
185 if (!(Mask & DebugPrintMask))
186 {
187 return;
188 }
189
190 DebugStartOfLine = FALSE; // We don't want line headers
191 DbgPrint("Dumping buffer at 0x%x with length of %d bytes:\n", Buffer, Length);
192
193 for (Idx=0; Idx<Length; )
194 {
195 DebugStartOfLine = FALSE; // We don't want line headers
196
197 if (Idx < 0x0010)
198 {
199 DbgPrint("000%x:\t", Idx);
200 }
201 else if (Idx < 0x0100)
202 {
203 DbgPrint("00%x:\t", Idx);
204 }
205 else if (Idx < 0x1000)
206 {
207 DbgPrint("0%x:\t", Idx);
208 }
209 else
210 {
211 DbgPrint("%x:\t", Idx);
212 }
213
214 for (Idx2=0; Idx2<16; Idx2++,Idx++)
215 {
216 if (BufPtr[Idx] < 0x10)
217 {
218 DbgPrint("0");
219 }
220 DbgPrint("%x", BufPtr[Idx]);
221
222 if (Idx2 == 7)
223 {
224 DbgPrint("-");
225 }
226 else
227 {
228 DbgPrint(" ");
229 }
230 }
231
232 Idx -= 16;
233 DbgPrint(" ");
234
235 for (Idx2=0; Idx2<16; Idx2++,Idx++)
236 {
237 if ((BufPtr[Idx] > 20) && (BufPtr[Idx] < 0x80))
238 {
239 DbgPrint("%c", BufPtr[Idx]);
240 }
241 else
242 {
243 DbgPrint(".");
244 }
245 }
246
247 DbgPrint("\n");
248 }
249 }
250
251 #else
252
253 ULONG
254 DbgPrint(PCCH Format, ...)
255 {
256 return 0;
257 }
258
259 #endif // DBG
260
261 ULONG
262 MsgBoxPrint(const char *Format, ...)
263 {
264 va_list ap;
265 CHAR Buffer[512];
266 ULONG Length;
267
268 va_start(ap, Format);
269
270 /* Construct a string */
271 Length = _vsnprintf(Buffer, 512, Format, ap);
272
273 /* Check if we went past the buffer */
274 if (Length == MAXULONG)
275 {
276 /* Terminate it if we went over-board */
277 Buffer[sizeof(Buffer) - 1] = '\n';
278
279 /* Put maximum */
280 Length = sizeof(Buffer);
281 }
282
283 /* Show it as a message box */
284 UiMessageBox(Buffer);
285
286 /* Cleanup and exit */
287 va_end(ap);
288 return 0;
289 }
290
291 //DECLSPEC_NORETURN
292 NTKERNELAPI
293 VOID
294 NTAPI
295 KeBugCheckEx(
296 IN ULONG BugCheckCode,
297 IN ULONG_PTR BugCheckParameter1,
298 IN ULONG_PTR BugCheckParameter2,
299 IN ULONG_PTR BugCheckParameter3,
300 IN ULONG_PTR BugCheckParameter4)
301 {
302 char Buffer[70];
303 sprintf(Buffer, "*** STOP: 0x%08lX (0x%08lX, 0x%08lX, 0x%08lX, 0x%08lX)",
304 BugCheckCode, BugCheckParameter1, BugCheckParameter2,
305 BugCheckParameter3, BugCheckParameter4);
306 UiMessageBoxCritical(Buffer);
307 assert(FALSE);
308 for (;;);
309 }