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