Fix some Rtl Prototype inconsistencies, more are in ntifs/winddk but i have fixed...
[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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21 #include <debug.h>
22 #include <rtl.h>
23 #include <comm.h>
24 #include <portio.h>
25 #include <machine.h>
26
27 #ifdef DEBUG
28
29 //#define DEBUG_ALL
30 //#define DEBUG_INIFILE
31 //#define DEBUG_REACTOS
32 //#define DEBUG_CUSTOM
33 #define DEBUG_NONE
34
35 #if defined (DEBUG_ALL)
36 U32 DebugPrintMask = DPRINT_WARNING | DPRINT_MEMORY | DPRINT_FILESYSTEM |
37 DPRINT_UI | DPRINT_DISK | DPRINT_CACHE | DPRINT_REACTOS |
38 DPRINT_LINUX;
39 #elif defined (DEBUG_INIFILE)
40 U32 DebugPrintMask = DPRINT_INIFILE;
41 #elif defined (DEBUG_REACTOS)
42 U32 DebugPrintMask = DPRINT_REACTOS | DPRINT_REGISTRY;
43 #elif defined (DEBUG_CUSTOM)
44 U32 DebugPrintMask = DPRINT_WARNING|DPRINT_FILESYSTEM|DPRINT_MEMORY|DPRINT_LINUX;
45 #else //#elif defined (DEBUG_NONE)
46 U32 DebugPrintMask = 0;
47 #endif
48
49 #define SCREEN 0
50 #define RS232 1
51 #define BOCHS 2
52
53 #define COM1 1
54 #define COM2 2
55 #define COM3 3
56 #define COM4 4
57
58 #define BOCHS_OUTPUT_PORT 0xe9
59
60 U32 DebugPort = RS232;
61 //U32 DebugPort = SCREEN;
62 //U32 DebugPort = BOCHS;
63 U32 ComPort = COM1;
64 //U32 BaudRate = 19200;
65 U32 BaudRate = 115200;
66
67 BOOL DebugStartOfLine = TRUE;
68
69 VOID DebugInit(VOID)
70 {
71 if (DebugPort == RS232)
72 {
73 Rs232PortInitialize(ComPort, BaudRate);
74 }
75 }
76
77 VOID DebugPrintChar(UCHAR Character)
78 {
79 if (Character == '\n')
80 {
81 DebugStartOfLine = TRUE;
82 }
83
84 if (DebugPort == RS232)
85 {
86 if (Character == '\n')
87 {
88 Rs232PortPutByte('\r');
89 }
90 Rs232PortPutByte(Character);
91 }
92 else if (DebugPort == BOCHS)
93 {
94 WRITE_PORT_UCHAR((PUCHAR)BOCHS_OUTPUT_PORT, Character);
95 }
96 else
97 {
98 MachConsPutChar(Character);
99 }
100 }
101
102 VOID DebugPrintHeader(U32 Mask)
103 {
104 /* No header */
105 if (Mask == 0)
106 return;
107
108 switch (Mask)
109 {
110 case DPRINT_WARNING:
111 DebugPrintChar('W');
112 DebugPrintChar('A');
113 DebugPrintChar('R');
114 DebugPrintChar('N');
115 DebugPrintChar('I');
116 DebugPrintChar('N');
117 DebugPrintChar('G');
118 DebugPrintChar(':');
119 DebugPrintChar(' ');
120 break;
121 case DPRINT_MEMORY:
122 DebugPrintChar('M');
123 DebugPrintChar('E');
124 DebugPrintChar('M');
125 DebugPrintChar('O');
126 DebugPrintChar('R');
127 DebugPrintChar('Y');
128 DebugPrintChar(':');
129 DebugPrintChar(' ');
130 break;
131 case DPRINT_FILESYSTEM:
132 DebugPrintChar('F');
133 DebugPrintChar('I');
134 DebugPrintChar('L');
135 DebugPrintChar('E');
136 DebugPrintChar('S');
137 DebugPrintChar('Y');
138 DebugPrintChar('S');
139 DebugPrintChar(':');
140 DebugPrintChar(' ');
141 break;
142 case DPRINT_INIFILE:
143 DebugPrintChar('I');
144 DebugPrintChar('N');
145 DebugPrintChar('I');
146 DebugPrintChar('F');
147 DebugPrintChar('I');
148 DebugPrintChar('L');
149 DebugPrintChar('E');
150 DebugPrintChar(':');
151 DebugPrintChar(' ');
152 break;
153 case DPRINT_UI:
154 DebugPrintChar('U');
155 DebugPrintChar('I');
156 DebugPrintChar(':');
157 DebugPrintChar(' ');
158 break;
159 case DPRINT_DISK:
160 DebugPrintChar('D');
161 DebugPrintChar('I');
162 DebugPrintChar('S');
163 DebugPrintChar('K');
164 DebugPrintChar(':');
165 DebugPrintChar(' ');
166 break;
167 case DPRINT_CACHE:
168 DebugPrintChar('C');
169 DebugPrintChar('A');
170 DebugPrintChar('C');
171 DebugPrintChar('H');
172 DebugPrintChar('E');
173 DebugPrintChar(':');
174 DebugPrintChar(' ');
175 break;
176 case DPRINT_REGISTRY:
177 DebugPrintChar('R');
178 DebugPrintChar('E');
179 DebugPrintChar('G');
180 DebugPrintChar('I');
181 DebugPrintChar('S');
182 DebugPrintChar('T');
183 DebugPrintChar('R');
184 DebugPrintChar('Y');
185 DebugPrintChar(':');
186 DebugPrintChar(' ');
187 break;
188 case DPRINT_REACTOS:
189 DebugPrintChar('R');
190 DebugPrintChar('E');
191 DebugPrintChar('A');
192 DebugPrintChar('C');
193 DebugPrintChar('T');
194 DebugPrintChar('O');
195 DebugPrintChar('S');
196 DebugPrintChar(':');
197 DebugPrintChar(' ');
198 break;
199 case DPRINT_LINUX:
200 DebugPrintChar('L');
201 DebugPrintChar('I');
202 DebugPrintChar('N');
203 DebugPrintChar('U');
204 DebugPrintChar('X');
205 DebugPrintChar(':');
206 DebugPrintChar(' ');
207 break;
208 case DPRINT_HWDETECT:
209 DebugPrintChar('H');
210 DebugPrintChar('W');
211 DebugPrintChar('D');
212 DebugPrintChar('E');
213 DebugPrintChar('T');
214 DebugPrintChar('E');
215 DebugPrintChar('C');
216 DebugPrintChar('T');
217 DebugPrintChar(':');
218 DebugPrintChar(' ');
219 break;
220 default:
221 DebugPrintChar('U');
222 DebugPrintChar('N');
223 DebugPrintChar('K');
224 DebugPrintChar('N');
225 DebugPrintChar('O');
226 DebugPrintChar('W');
227 DebugPrintChar('N');
228 DebugPrintChar(':');
229 DebugPrintChar(' ');
230 break;
231 }
232 }
233
234 static VOID DebugPrintV(char *format, int *dataptr)
235 {
236 char c, *ptr, str[16];
237 int ll;
238
239 ll = 0;
240 while ((c = *(format++)))
241 {
242 if (c != '%')
243 {
244 DebugPrintChar(c);
245 }
246 else
247 {
248 if (*format == 'I' && *(format+1) == '6' && *(format+2) == '4')
249 {
250 ll = 1;
251 format += 3;
252 }
253 else
254 {
255 ll = 0;
256 }
257 switch (c = *(format++))
258 {
259 case 'd': case 'u': case 'x':
260
261 if (ll)
262 {
263 *convert_i64_to_ascii(str, c, *((unsigned long long*) dataptr)) = 0;
264 dataptr += 2;
265 }
266 else
267 {
268 *convert_to_ascii(str, c, *((unsigned long *) dataptr++)) = 0;
269 }
270
271 ptr = str;
272
273 while (*ptr)
274 {
275 DebugPrintChar(*(ptr++));
276 }
277 break;
278
279 case 'c':
280
281 DebugPrintChar((*(dataptr++))&0xff);
282 break;
283
284 case 's':
285
286 ptr = (char *)(*(dataptr++));
287
288 while ((c = *(ptr++)))
289 {
290 DebugPrintChar(c);
291 }
292 break;
293 case '%':
294 DebugPrintChar(c);
295 break;
296 default:
297 DebugPrint(DPRINT_WARNING, "\nDebugPrint() invalid format specifier - %%%c\n", c);
298 break;
299 }
300 }
301 }
302
303
304 if (DebugPort == SCREEN)
305 {
306 //getch();
307 }
308
309 }
310
311 VOID DebugPrint(U32 Mask, char *format, ...)
312 {
313 int *dataptr = (int *) &format;
314
315 // Mask out unwanted debug messages
316 if (!(Mask & DebugPrintMask))
317 {
318 return;
319 }
320
321 // Print the header if we have started a new line
322 if (DebugStartOfLine)
323 {
324 DebugPrintHeader(Mask);
325 DebugStartOfLine = FALSE;
326 }
327
328 DebugPrintV(format, ++dataptr);
329 }
330
331 VOID DebugPrint1(char *format, ...)
332 {
333 int *dataptr = (int *) &format;
334
335 DebugPrintV(format, ++dataptr);
336 }
337
338 VOID DebugDumpBuffer(U32 Mask, PVOID Buffer, U32 Length)
339 {
340 PUCHAR BufPtr = (PUCHAR)Buffer;
341 U32 Idx;
342 U32 Idx2;
343
344 // Mask out unwanted debug messages
345 if (!(Mask & DebugPrintMask))
346 {
347 return;
348 }
349
350 DebugStartOfLine = FALSE; // We don't want line headers
351 DebugPrint(Mask, "Dumping buffer at 0x%x with length of %d bytes:\n", Buffer, Length);
352
353 for (Idx=0; Idx<Length; )
354 {
355 DebugStartOfLine = FALSE; // We don't want line headers
356
357 if (Idx < 0x0010)
358 {
359 DebugPrint(Mask, "000%x:\t", Idx);
360 }
361 else if (Idx < 0x0100)
362 {
363 DebugPrint(Mask, "00%x:\t", Idx);
364 }
365 else if (Idx < 0x1000)
366 {
367 DebugPrint(Mask, "0%x:\t", Idx);
368 }
369 else
370 {
371 DebugPrint(Mask, "%x:\t", Idx);
372 }
373
374 for (Idx2=0; Idx2<16; Idx2++,Idx++)
375 {
376 if (BufPtr[Idx] < 0x10)
377 {
378 DebugPrint(Mask, "0");
379 }
380 DebugPrint(Mask, "%x", BufPtr[Idx]);
381
382 if (Idx2 == 7)
383 {
384 DebugPrint(Mask, "-");
385 }
386 else
387 {
388 DebugPrint(Mask, " ");
389 }
390 }
391
392 Idx -= 16;
393 DebugPrint(Mask, " ");
394
395 for (Idx2=0; Idx2<16; Idx2++,Idx++)
396 {
397 if ((BufPtr[Idx] > 20) && (BufPtr[Idx] < 0x80))
398 {
399 DebugPrint(Mask, "%c", BufPtr[Idx]);
400 }
401 else
402 {
403 DebugPrint(Mask, ".");
404 }
405 }
406
407 DebugPrint(Mask, "\n");
408 }
409 }
410
411 #endif // defined DEBUG