Added new 'bootcd' target.
[reactos.git] / freeldr / freeldr / debug.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 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
25 #ifdef DEBUG
26
27 //#define DEBUG_ULTRA
28 //#define DEBUG_INIFILE
29 //#define DEBUG_REACTOS
30 //#define DEBUG_CUSTOM
31 #define DEBUG_NONE
32
33 #if defined (DEBUG_ULTRA)
34 U32 DebugPrintMask = DPRINT_WARNING | DPRINT_MEMORY | DPRINT_FILESYSTEM |
35 DPRINT_UI | DPRINT_DISK | DPRINT_CACHE | DPRINT_REACTOS |
36 DPRINT_LINUX;
37 #elif defined (DEBUG_INIFILE)
38 U32 DebugPrintMask = DPRINT_INIFILE;
39 #elif defined (DEBUG_REACTOS)
40 U32 DebugPrintMask = DPRINT_REACTOS | DPRINT_REGISTRY;
41 #elif defined (DEBUG_CUSTOM)
42 U32 DebugPrintMask = DPRINT_WARNING|DPRINT_FILESYSTEM;
43 #else //#elif defined (DEBUG_NONE)
44 U32 DebugPrintMask = 0;
45 #endif
46
47 #define SCREEN 0
48 #define RS232 1
49 #define BOCHS 2
50
51 #define COM1 1
52 #define COM2 2
53 #define COM3 3
54 #define COM4 4
55
56 #define BOCHS_OUTPUT_PORT 0xe9
57
58 //U32 DebugPort = RS232;
59 U32 DebugPort = SCREEN;
60 //U32 DebugPort = BOCHS;
61 U32 ComPort = COM1;
62 //U32 BaudRate = 19200;
63 U32 BaudRate = 115200;
64
65 BOOL 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 else if (DebugPort == BOCHS)
91 {
92 WRITE_PORT_UCHAR((PUCHAR)BOCHS_OUTPUT_PORT, Character);
93 }
94 else
95 {
96 putchar(Character);
97 }
98 }
99
100 VOID DebugPrintHeader(U32 Mask)
101 {
102 /* No header */
103 if (Mask == 0)
104 return;
105
106 switch (Mask)
107 {
108 case DPRINT_WARNING:
109 DebugPrintChar('W');
110 DebugPrintChar('A');
111 DebugPrintChar('R');
112 DebugPrintChar('N');
113 DebugPrintChar('I');
114 DebugPrintChar('N');
115 DebugPrintChar('G');
116 DebugPrintChar(':');
117 DebugPrintChar(' ');
118 break;
119 case DPRINT_MEMORY:
120 DebugPrintChar('M');
121 DebugPrintChar('E');
122 DebugPrintChar('M');
123 DebugPrintChar('O');
124 DebugPrintChar('R');
125 DebugPrintChar('Y');
126 DebugPrintChar(':');
127 DebugPrintChar(' ');
128 break;
129 case DPRINT_FILESYSTEM:
130 DebugPrintChar('F');
131 DebugPrintChar('I');
132 DebugPrintChar('L');
133 DebugPrintChar('E');
134 DebugPrintChar('S');
135 DebugPrintChar('Y');
136 DebugPrintChar('S');
137 DebugPrintChar(':');
138 DebugPrintChar(' ');
139 break;
140 case DPRINT_INIFILE:
141 DebugPrintChar('I');
142 DebugPrintChar('N');
143 DebugPrintChar('I');
144 DebugPrintChar('F');
145 DebugPrintChar('I');
146 DebugPrintChar('L');
147 DebugPrintChar('E');
148 DebugPrintChar(':');
149 DebugPrintChar(' ');
150 break;
151 case DPRINT_UI:
152 DebugPrintChar('U');
153 DebugPrintChar('I');
154 DebugPrintChar(':');
155 DebugPrintChar(' ');
156 break;
157 case DPRINT_DISK:
158 DebugPrintChar('D');
159 DebugPrintChar('I');
160 DebugPrintChar('S');
161 DebugPrintChar('K');
162 DebugPrintChar(':');
163 DebugPrintChar(' ');
164 break;
165 case DPRINT_CACHE:
166 DebugPrintChar('C');
167 DebugPrintChar('A');
168 DebugPrintChar('C');
169 DebugPrintChar('H');
170 DebugPrintChar('E');
171 DebugPrintChar(':');
172 DebugPrintChar(' ');
173 break;
174 case DPRINT_REGISTRY:
175 DebugPrintChar('R');
176 DebugPrintChar('E');
177 DebugPrintChar('G');
178 DebugPrintChar('I');
179 DebugPrintChar('S');
180 DebugPrintChar('T');
181 DebugPrintChar('R');
182 DebugPrintChar('Y');
183 DebugPrintChar(':');
184 DebugPrintChar(' ');
185 break;
186 case DPRINT_REACTOS:
187 DebugPrintChar('R');
188 DebugPrintChar('E');
189 DebugPrintChar('A');
190 DebugPrintChar('C');
191 DebugPrintChar('T');
192 DebugPrintChar('O');
193 DebugPrintChar('S');
194 DebugPrintChar(':');
195 DebugPrintChar(' ');
196 break;
197 case DPRINT_LINUX:
198 DebugPrintChar('L');
199 DebugPrintChar('I');
200 DebugPrintChar('N');
201 DebugPrintChar('U');
202 DebugPrintChar('X');
203 DebugPrintChar(':');
204 DebugPrintChar(' ');
205 break;
206 default:
207 DebugPrintChar('U');
208 DebugPrintChar('N');
209 DebugPrintChar('K');
210 DebugPrintChar('N');
211 DebugPrintChar('O');
212 DebugPrintChar('W');
213 DebugPrintChar('N');
214 DebugPrintChar(':');
215 DebugPrintChar(' ');
216 break;
217 }
218 }
219
220 VOID DebugPrint(U32 Mask, char *format, ...)
221 {
222 int *dataptr = (int *) &format;
223 char c, *ptr, str[16];
224
225 // Mask out unwanted debug messages
226 if (!(Mask & DebugPrintMask))
227 {
228 return;
229 }
230
231 // Print the header if we have started a new line
232 if (DebugStartOfLine)
233 {
234 DebugPrintHeader(Mask);
235 DebugStartOfLine = FALSE;
236 }
237
238 dataptr++;
239
240 while ((c = *(format++)))
241 {
242 if (c != '%')
243 {
244 DebugPrintChar(c);
245 }
246 else
247 {
248 switch (c = *(format++))
249 {
250 case 'd': case 'u': case 'x':
251
252 *convert_to_ascii(str, c, *((unsigned long *) dataptr++)) = 0;
253
254 ptr = str;
255
256 while (*ptr)
257 {
258 DebugPrintChar(*(ptr++));
259 }
260 break;
261
262 case 'c':
263
264 DebugPrintChar((*(dataptr++))&0xff);
265 break;
266
267 case 's':
268
269 ptr = (char *)(*(dataptr++));
270
271 while ((c = *(ptr++)))
272 {
273 DebugPrintChar(c);
274 }
275 break;
276 case '%':
277 DebugPrintChar(c);
278 break;
279 default:
280 DebugPrint(Mask, "\nDebugPrint() invalid format specifier - %%%c\n", c);
281 break;
282 }
283 }
284 }
285
286
287 if (DebugPort == SCREEN)
288 {
289 //getch();
290 }
291
292 }
293
294 VOID DebugDumpBuffer(U32 Mask, PVOID Buffer, U32 Length)
295 {
296 PUCHAR BufPtr = (PUCHAR)Buffer;
297 U32 Idx;
298 U32 Idx2;
299
300 // Mask out unwanted debug messages
301 if (!(Mask & DebugPrintMask))
302 {
303 return;
304 }
305
306 DebugStartOfLine = FALSE; // We don't want line headers
307 DebugPrint(Mask, "Dumping buffer at 0x%x with length of %d bytes:\n", Buffer, Length);
308
309 for (Idx=0; Idx<Length; )
310 {
311 DebugStartOfLine = FALSE; // We don't want line headers
312
313 if (Idx < 0x0010)
314 {
315 DebugPrint(Mask, "000%x:\t", Idx);
316 }
317 else if (Idx < 0x0100)
318 {
319 DebugPrint(Mask, "00%x:\t", Idx);
320 }
321 else if (Idx < 0x1000)
322 {
323 DebugPrint(Mask, "0%x:\t", Idx);
324 }
325 else
326 {
327 DebugPrint(Mask, "%x:\t", Idx);
328 }
329
330 for (Idx2=0; Idx2<16; Idx2++,Idx++)
331 {
332 if (BufPtr[Idx] < 0x10)
333 {
334 DebugPrint(Mask, "0");
335 }
336 DebugPrint(Mask, "%x", BufPtr[Idx]);
337
338 if (Idx2 == 7)
339 {
340 DebugPrint(Mask, "-");
341 }
342 else
343 {
344 DebugPrint(Mask, " ");
345 }
346 }
347
348 Idx -= 16;
349 DebugPrint(Mask, " ");
350
351 for (Idx2=0; Idx2<16; Idx2++,Idx++)
352 {
353 if ((BufPtr[Idx] > 20) && (BufPtr[Idx] < 0x80))
354 {
355 DebugPrint(Mask, "%c", BufPtr[Idx]);
356 }
357 else
358 {
359 DebugPrint(Mask, ".");
360 }
361 }
362
363 DebugPrint(Mask, "\n");
364 }
365 }
366
367 #endif // defined DEBUG