prevent buffer overflow, LoadString accepts the size of the buffer in TCHARs, not...
[reactos.git] / reactos / ntoskrnl / kd / kdinit.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/kd/kdinit.c
5 * PURPOSE: Kernel Debugger Initializtion
6 *
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
8 */
9
10 #include <ntoskrnl.h>
11 #define NDEBUG
12 #include <internal/debug.h>
13
14 /* VARIABLES ***************************************************************/
15
16 KD_PORT_INFORMATION PortInfo = {DEFAULT_DEBUG_PORT, DEFAULT_DEBUG_BAUD_RATE, 0};
17 ULONG KdpPortIrq;
18 KDP_DEBUG_MODE KdpDebugMode;
19 LIST_ENTRY KdProviders;
20 PKDP_INIT_ROUTINE WrapperInitRoutine;
21 KD_DISPATCH_TABLE DispatchTable[KdMax];
22 KD_DISPATCH_TABLE WrapperTable;
23
24 PKDP_INIT_ROUTINE InitRoutines[KdMax] = {KdpScreenInit,
25 KdpSerialInit,
26 KdpInitDebugLog};
27
28 /* PRIVATE FUNCTIONS *********************************************************/
29
30 PCHAR
31 STDCALL
32 KdpGetWrapperDebugMode(PCHAR Currentp2,
33 PLOADER_PARAMETER_BLOCK LoaderBlock)
34 {
35 PCHAR p2 = Currentp2;
36
37 #ifdef DBG
38 /* Check for BOCHS Debugging */
39 if (!_strnicmp(p2, "BOCHS", 5))
40 {
41 /* Enable It */
42 p2 += 5;
43 KdpDebugMode.Bochs = TRUE;
44 WrapperInitRoutine = KdpBochsInit;
45 }
46
47 /* Check for GDB Debugging */
48 if (!_strnicmp(p2, "GDB", 3))
49 {
50 /* Enable it */
51 p2 += 3;
52 KdpDebugMode.Gdb = TRUE;
53
54 /* Enable Debugging */
55 KdDebuggerEnabled = TRUE;
56 WrapperInitRoutine = KdpGdbStubInit;
57 }
58
59 /* Check for PICE Debugging */
60 else if (!_strnicmp(p2, "PICE", 4))
61 {
62 /* Enable it */
63 p2 += 4;
64 KdpDebugMode.Pice = TRUE;
65
66 /* Enable Debugging */
67 KdDebuggerEnabled = TRUE;
68 }
69 #endif
70
71 #ifdef KDBG
72 /* Get the KDBG Settings and enable it */
73 KdDebuggerEnabled = TRUE;
74 KdpDebugMode.Gdb = TRUE;
75 KdbpGetCommandLineSettings((PCHAR)LoaderBlock->CommandLine);
76 #endif
77 return p2;
78 }
79
80 PCHAR
81 STDCALL
82 KdpGetDebugMode(PCHAR Currentp2)
83 {
84 PCHAR p2 = Currentp2;
85 ULONG Value;
86
87 /* Check for Screen Debugging */
88 if (!_strnicmp(p2, "SCREEN", 6))
89 {
90 /* Enable It */
91 p2 += 6;
92 KdpDebugMode.Screen = TRUE;
93 }
94 /* Check for Serial Debugging */
95 else if (!_strnicmp(p2, "COM", 3))
96 {
97 /* Gheck for a valid Serial Port */
98 p2 += 3;
99 Value = (ULONG)atol(p2);
100 if (Value > 0 && Value < 5)
101 {
102 /* Valid port found, enable Serial Debugging */
103 KdpDebugMode.Serial = TRUE;
104
105 /* Set the port to use */
106 SerialPortInfo.ComPort = Value;
107 KdpPort = Value;
108 }
109 }
110 /* Check for Debug Log Debugging */
111 else if (!_strnicmp(p2, "FILE", 4))
112 {
113 /* Enable It */
114 p2 += 4;
115 KdpDebugMode.File = TRUE;
116 }
117
118 return p2;
119 }
120
121 VOID
122 STDCALL
123 KdpCallInitRoutine(ULONG BootPhase)
124 {
125 PLIST_ENTRY CurrentEntry;
126 PKD_DISPATCH_TABLE CurrentTable;
127
128 /* Call the registered handlers */
129 CurrentEntry = KdProviders.Flink;
130 while (CurrentEntry != &KdProviders)
131 {
132 /* Get the current table */
133 CurrentTable = CONTAINING_RECORD(CurrentEntry,
134 KD_DISPATCH_TABLE,
135 KdProvidersList);
136
137 /* Call it */
138 CurrentTable->KdpInitRoutine(CurrentTable, BootPhase);
139
140 /* Next Table */
141 CurrentEntry = CurrentEntry->Flink;
142 }
143
144 /* Call the Wrapper Init Routine */
145 if (WrapperInitRoutine)
146 WrapperTable.KdpInitRoutine(&WrapperTable, BootPhase);
147 }
148
149 VOID
150 INIT_FUNCTION
151 KdInitSystem(ULONG BootPhase,
152 PLOADER_PARAMETER_BLOCK LoaderBlock)
153 {
154 ULONG Value;
155 ULONG i;
156 PCHAR p1, p2;
157
158 /* Set Default Port Options */
159 if (BootPhase == 0)
160 {
161 /* Initialize the Provider List */
162 InitializeListHead(&KdProviders);
163
164 /* Parse the Command Line */
165 p1 = (PCHAR)LoaderBlock->CommandLine;
166 while (p1 && (p2 = strchr(p1, '/')))
167 {
168 /* Move past the slash */
169 p2++;
170
171 /* Identify the Debug Type being Used */
172 if (!_strnicmp(p2, "DEBUGPORT=", 10))
173 {
174 p2 += 10;
175 p2 = KdpGetDebugMode(p2);
176 p2 = KdpGetWrapperDebugMode(p2, LoaderBlock);
177 }
178 /* Check for Kernel Debugging Enable */
179 else if (!_strnicmp(p2, "DEBUG", 5))
180 {
181 /* Enable it on the Serial Port */
182 p2 += 5;
183 KdDebuggerEnabled = TRUE;
184 KdpDebugMode.Serial = TRUE;
185 }
186 /* Check for Kernel Debugging Bypass */
187 else if (!_strnicmp(p2, "NODEBUG", 7))
188 {
189 /* Disable Debugging */
190 p2 += 7;
191 KdDebuggerEnabled = FALSE;
192 }
193 /* Check for Kernel Debugging Bypass unless STOP Error */
194 else if (!_strnicmp(p2, "CRASHDEBUG", 10))
195 {
196 /* Disable Debugging */
197 p2 += 10;
198 KdDebuggerEnabled = FALSE;
199 }
200 /* Check Serial Port Settings [Baud Rate] */
201 else if (!_strnicmp(p2, "BAUDRATE=", 9))
202 {
203 /* Get the Baud Rate */
204 p2 += 9;
205 Value = (ULONG)atol(p2);
206
207 /* Check if it's valid and Set it */
208 if (0 < Value) PortInfo.BaudRate = SerialPortInfo.BaudRate = Value;
209 }
210 /* Check Serial Port Settings [IRQ] */
211 else if (!_strnicmp(p2, "IRQ=", 4))
212 {
213 /* Get the IRQ */
214 p2 += 3;
215 Value = (ULONG)atol(p2);
216
217 /* Check if it's valid and set it */
218 if (0 < Value) KdpPortIrq = Value;
219 }
220
221 /* Move to next */
222 p1 = p2;
223 }
224
225 /* Call Providers at Phase 0 */
226 for (i = 0; i < KdMax; i++)
227 {
228 InitRoutines[i](&DispatchTable[i], 0);
229 }
230
231 /* Call Wrapper at Phase 0 */
232 if (WrapperInitRoutine) WrapperInitRoutine(&WrapperTable, 0);
233
234 return;
235 }
236
237 /* Call the Initialization Routines of the Registered Providers */
238 KdpCallInitRoutine(BootPhase);
239 }
240
241 /* EOF */