ReactOS 0.4.15-dev-7934-g1dc8d80
kdcom.c File Reference
#include "kdgdb.h"
#include <cportlib/cportlib.h>
#include <arc/arc.h>
#include <stdlib.h>
#include <ndk/halfuncs.h>
Include dependency graph for kdcom.c:

Go to the source code of this file.

Macros

#define DEFAULT_DEBUG_PORT   2 /* COM2 */
 
#define DEFAULT_DEBUG_COM1_IRQ   4
 
#define DEFAULT_DEBUG_COM2_IRQ   3
 
#define DEFAULT_DEBUG_BAUD_RATE   115200
 
#define DEFAULT_BAUD_RATE   19200
 
#define MAX_COM_PORTS   (sizeof(BaseArray) / sizeof(BaseArray[0]) - 1)
 

Functions

NTSTATUS NTAPI KdD0Transition (VOID)
 
NTSTATUS NTAPI KdD3Transition (VOID)
 
NTSTATUS NTAPI KdSave (IN BOOLEAN SleepTransition)
 
NTSTATUS NTAPI KdRestore (IN BOOLEAN SleepTransition)
 
NTSTATUS NTAPI KdpPortInitialize (IN ULONG ComPortNumber, IN ULONG ComPortBaudRate)
 
NTSTATUS NTAPI KdDebuggerInitialize0 (IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL)
 
NTSTATUS NTAPI KdDebuggerInitialize1 (IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL)
 
VOID NTAPI KdpSendByte (_In_ UCHAR Byte)
 
KDSTATUS NTAPI KdpPollByte (OUT PUCHAR OutByte)
 
KDSTATUS NTAPI KdpReceiveByte (_Out_ PUCHAR OutByte)
 
KDSTATUS NTAPI KdpPollBreakIn (VOID)
 

Variables

CPPORT KdComPort
 
ULONG KdComPortIrq = 0
 

Macro Definition Documentation

◆ DEFAULT_BAUD_RATE

#define DEFAULT_BAUD_RATE   19200

Definition at line 27 of file kdcom.c.

◆ DEFAULT_DEBUG_BAUD_RATE

#define DEFAULT_DEBUG_BAUD_RATE   115200

Definition at line 26 of file kdcom.c.

◆ DEFAULT_DEBUG_COM1_IRQ

#define DEFAULT_DEBUG_COM1_IRQ   4

Definition at line 24 of file kdcom.c.

◆ DEFAULT_DEBUG_COM2_IRQ

#define DEFAULT_DEBUG_COM2_IRQ   3

Definition at line 25 of file kdcom.c.

◆ DEFAULT_DEBUG_PORT

#define DEFAULT_DEBUG_PORT   2 /* COM2 */

Definition at line 23 of file kdcom.c.

◆ MAX_COM_PORTS

#define MAX_COM_PORTS   (sizeof(BaseArray) / sizeof(BaseArray[0]) - 1)

Definition at line 46 of file kdcom.c.

Function Documentation

◆ KdD0Transition()

NTSTATUS NTAPI KdD0Transition ( VOID  )

Definition at line 97 of file kdcom.c.

98{
99 return STATUS_SUCCESS;
100}
#define STATUS_SUCCESS
Definition: shellext.h:65

◆ KdD3Transition()

NTSTATUS NTAPI KdD3Transition ( VOID  )

Definition at line 104 of file kdcom.c.

105{
106 return STATUS_SUCCESS;
107}

◆ KdDebuggerInitialize0()

NTSTATUS NTAPI KdDebuggerInitialize0 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 152 of file kdcom.c.

153{
154 ULONG ComPortNumber = DEFAULT_DEBUG_PORT;
155 ULONG ComPortBaudRate = DEFAULT_DEBUG_BAUD_RATE;
156
157 PCHAR CommandLine, PortString, BaudString, IrqString;
158 ULONG Value;
159
160 /* Check if we have a LoaderBlock */
161 if (LoaderBlock)
162 {
163 /* Get the Command Line */
164 CommandLine = LoaderBlock->LoadOptions;
165
166 /* Upcase it */
167 _strupr(CommandLine);
168
169 /* Get the port and baud rate */
170 PortString = strstr(CommandLine, "DEBUGPORT");
171 BaudString = strstr(CommandLine, "BAUDRATE");
172 IrqString = strstr(CommandLine, "IRQ");
173
174 /* Check if we got the /DEBUGPORT parameter */
175 if (PortString)
176 {
177 /* Move past the actual string, to reach the port*/
178 PortString += strlen("DEBUGPORT");
179
180 /* Now get past any spaces and skip the equal sign */
181 while (*PortString == ' ') PortString++;
182 PortString++;
183
184 /* Do we have a serial port? */
185 if (strncmp(PortString, "COM", 3) != 0)
186 {
188 }
189
190 /* Check for a valid Serial Port */
191 PortString += 3;
192 Value = atol(PortString);
193 if (Value >= sizeof(BaseArray) / sizeof(BaseArray[0]))
194 {
196 }
197
198 /* Set the port to use */
199 ComPortNumber = Value;
200 }
201
202 /* Check if we got a baud rate */
203 if (BaudString)
204 {
205 /* Move past the actual string, to reach the rate */
206 BaudString += strlen("BAUDRATE");
207
208 /* Now get past any spaces */
209 while (*BaudString == ' ') BaudString++;
210
211 /* And make sure we have a rate */
212 if (*BaudString)
213 {
214 /* Read and set it */
215 Value = atol(BaudString + 1);
216 if (Value) ComPortBaudRate = Value;
217 }
218 }
219
220 /* Check Serial Port Settings [IRQ] */
221 if (IrqString)
222 {
223 /* Move past the actual string, to reach the rate */
224 IrqString += strlen("IRQ");
225
226 /* Now get past any spaces */
227 while (*IrqString == ' ') IrqString++;
228
229 /* And make sure we have an IRQ */
230 if (*IrqString)
231 {
232 /* Read and set it */
233 Value = atol(IrqString + 1);
234 if (Value) KdComPortIrq = Value;
235 }
236 }
237 }
238
239#ifdef KDDEBUG
240 /*
241 * Try to find a free COM port and use it as the KD debugging port.
242 * NOTE: Inspired by reactos/boot/freeldr/freeldr/comm/rs232.c, Rs232PortInitialize(...)
243 */
244 {
245 /*
246 * Start enumerating COM ports from the last one to the first one,
247 * and break when we find a valid port.
248 * If we reach the first element of the list, the invalid COM port,
249 * then it means that no valid port was found.
250 */
251 ULONG ComPort;
252 for (ComPort = MAX_COM_PORTS; ComPort > 0; ComPort--)
253 {
254 /* Check if the port exist; skip the KD port */
255 if ((ComPort != ComPortNumber) && CpDoesPortExist(UlongToPtr(BaseArray[ComPort])))
256 break;
257 }
258 if (ComPort != 0)
259 CpInitialize(&KdDebugComPort, UlongToPtr(BaseArray[ComPort]), DEFAULT_BAUD_RATE);
260 }
261#endif
262
263 /* Initialize the port */
264 return KdpPortInitialize(ComPortNumber, ComPortBaudRate);
265}
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
NTSTATUS NTAPI CpInitialize(IN PCPPORT Port, IN PUCHAR Address, IN ULONG BaudRate)
Definition: cport.c:85
BOOLEAN NTAPI CpDoesPortExist(IN PUCHAR Address)
Definition: cport.c:224
#define UlongToPtr(u)
Definition: config.h:106
static const ULONG BaseArray[]
Definition: hwide.c:41
_Check_return_ long __cdecl atol(_In_z_ const char *_Str)
#define DEFAULT_DEBUG_PORT
Definition: kdcom.c:25
#define DEFAULT_DEBUG_BAUD_RATE
Definition: kdcom.c:28
NTSTATUS NTAPI KdpPortInitialize(IN ULONG ComPortNumber, IN ULONG ComPortBaudRate)
Definition: kdcom.c:129
#define DEFAULT_BAUD_RATE
Definition: kdcom.c:29
#define MAX_COM_PORTS
Definition: kdcom.c:48
ULONG KdComPortIrq
Definition: kdcom.c:53
_CRTIMP char *__cdecl _strupr(_Inout_z_ char *_String)
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413

◆ KdDebuggerInitialize1()

NTSTATUS NTAPI KdDebuggerInitialize1 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 275 of file kdcom.c.

276{
277 return STATUS_SUCCESS;
278}

◆ KdpPollBreakIn()

KDSTATUS NTAPI KdpPollBreakIn ( VOID  )

Definition at line 326 of file kdcom.c.

327{
328 KDSTATUS KdStatus;
329 UCHAR Byte;
330
331 KdStatus = KdpPollByte(&Byte);
332 if (KdStatus == KdPacketReceived)
333 {
334 if (Byte == 0x03)
335 {
336 KDDBGPRINT("BreakIn Polled.\n");
337 return KdPacketReceived;
338 }
339 else if (Byte == '$')
340 {
341 /* GDB tried to send a new packet. N-ack it. */
342 KdpSendByte('-');
343 }
344 }
345 return KdPacketTimedOut;
346}
unsigned char Byte
Definition: zlib.h:37
#define KDDBGPRINT(...)
Definition: kddll.h:19
KDP_STATUS NTAPI KdpPollByte(OUT PUCHAR OutByte)
Definition: kdcom.c:299
VOID NTAPI KdpSendByte(_In_ UCHAR Byte)
Definition: kdcom.c:283
#define KdPacketReceived
Definition: kddll.h:5
ULONG KDSTATUS
Definition: kddll.h:4
#define KdPacketTimedOut
Definition: kddll.h:6
unsigned char UCHAR
Definition: xmlstorage.h:181

◆ KdpPollByte()

KDSTATUS NTAPI KdpPollByte ( OUT PUCHAR  OutByte)

Definition at line 291 of file kdcom.c.

292{
293 /* Poll the byte */
294 if (CpGetByte(&KdComPort, OutByte, FALSE, FALSE) == CP_GET_SUCCESS)
295 {
296 return KdPacketReceived;
297 }
298 else
299 {
300 return KdPacketTimedOut;
301 }
302}
#define CP_GET_SUCCESS
Definition: cportlib.h:18
USHORT NTAPI CpGetByte(IN PCPPORT Port, OUT PUCHAR Byte, IN BOOLEAN Wait, IN BOOLEAN Poll)
Definition: cport.c:253
#define FALSE
Definition: types.h:117
CPPORT KdComPort
Definition: kdcom.c:52

◆ KdpPortInitialize()

NTSTATUS NTAPI KdpPortInitialize ( IN ULONG  ComPortNumber,
IN ULONG  ComPortBaudRate 
)

Definition at line 127 of file kdcom.c.

129{
131
133 UlongToPtr(BaseArray[ComPortNumber]),
134 ComPortBaudRate);
135 if (!NT_SUCCESS(Status))
136 {
138 }
139
141 return STATUS_SUCCESS;
142}
LONG NTSTATUS
Definition: precomp.h:26
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Status
Definition: gdiplustypes.h:25
PUCHAR KdComPortInUse
Definition: usage.c:17
PUCHAR Address
Definition: cportlib.h:29

◆ KdpReceiveByte()

KDSTATUS NTAPI KdpReceiveByte ( _Out_ PUCHAR  OutByte)

Definition at line 306 of file kdcom.c.

307{
308 USHORT CpStatus;
309
310 do
311 {
312 CpStatus = CpGetByte(&KdComPort, OutByte, TRUE, FALSE);
313 } while (CpStatus == CP_GET_NODATA);
314
315 /* Get the byte */
316 if (CpStatus == CP_GET_SUCCESS)
317 {
318 return KdPacketReceived;
319 }
320
321 return KdPacketTimedOut;
322}
#define CP_GET_NODATA
Definition: cportlib.h:19
#define TRUE
Definition: types.h:120
unsigned short USHORT
Definition: pedump.c:61

◆ KdpSendByte()

VOID NTAPI KdpSendByte ( _In_ UCHAR  Byte)

Definition at line 283 of file kdcom.c.

284{
285 /* Send the byte */
287}
VOID NTAPI CpPutByte(IN PCPPORT Port, IN UCHAR Byte)
Definition: cport.c:303

◆ KdRestore()

NTSTATUS NTAPI KdRestore ( IN BOOLEAN  SleepTransition)

Definition at line 119 of file kdcom.c.

120{
121 /* Nothing to do on COM ports */
122 return STATUS_SUCCESS;
123}

◆ KdSave()

NTSTATUS NTAPI KdSave ( IN BOOLEAN  SleepTransition)

Definition at line 111 of file kdcom.c.

112{
113 /* Nothing to do on COM ports */
114 return STATUS_SUCCESS;
115}

Variable Documentation

◆ KdComPort

CPPORT KdComPort

Definition at line 50 of file kdcom.c.

◆ KdComPortIrq

ULONG KdComPortIrq = 0

Definition at line 51 of file kdcom.c.