ReactOS 0.4.16-dev-2528-g7139e57
kdcom.c File Reference
#include "kdgdb.h"
#include <arc/arc.h>
#include <stdlib.h>
#include <ndk/halfuncs.h>
#include <cportlib/cportlib.h>
#include <cportlib/uartinfo.h>
Include dependency graph for kdcom.c:

Go to the source code of this file.

Macros

#define CONST_STR_LEN(x)   (sizeof(x)/sizeof(x[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_ PUCHAR PortAddress, _In_ ULONG BaudRate)
 
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
 

Macro Definition Documentation

◆ CONST_STR_LEN

#define CONST_STR_LEN (   x)    (sizeof(x)/sizeof(x[0]) - 1)

Function Documentation

◆ KdD0Transition()

NTSTATUS NTAPI KdD0Transition ( VOID  )

Definition at line 65 of file kdcom.c.

66{
67 return STATUS_SUCCESS;
68}
#define STATUS_SUCCESS
Definition: shellext.h:65

◆ KdD3Transition()

NTSTATUS NTAPI KdD3Transition ( VOID  )

Definition at line 72 of file kdcom.c.

73{
74 return STATUS_SUCCESS;
75}

◆ KdDebuggerInitialize0()

NTSTATUS NTAPI KdDebuggerInitialize0 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 117 of file kdcom.c.

118{
119#define CONST_STR_LEN(x) (sizeof(x)/sizeof(x[0]) - 1)
120
121 ULONG ComPortNumber = DEFAULT_DEBUG_PORT;
122 ULONG ComPortBaudRate = DEFAULT_DEBUG_BAUD_RATE;
123 PUCHAR ComPortAddress = NULL;
124
125 PSTR CommandLine, PortString, BaudString;
126 ULONG Value;
127
128 /* Check if we have a LoaderBlock */
129 if (LoaderBlock)
130 {
131 /* Get the Command Line */
132 CommandLine = LoaderBlock->LoadOptions;
133
134 /* Upcase it */
135 _strupr(CommandLine);
136
137 /* Get the port and baud rate */
138 PortString = strstr(CommandLine, "DEBUGPORT");
139 BaudString = strstr(CommandLine, "BAUDRATE");
140
141 /* Check if we got the DEBUGPORT parameter */
142 if (PortString)
143 {
144 /* Move past the actual string and any spaces */
145 PortString += CONST_STR_LEN("DEBUGPORT");
146 while (*PortString == ' ') ++PortString;
147 /* Skip the equals sign */
148 if (*PortString) ++PortString;
149
150 /* Do we have a serial port? Recognize both
151 * 'DEBUGPORT=GDB' and 'DEBUGPORT=COM' syntaxes. */
152 if (_strnicmp(PortString, "GDB", CONST_STR_LEN("GDB")) != 0 &&
153 _strnicmp(PortString, "COM", CONST_STR_LEN("COM")) != 0)
154 {
156 }
157
158 /* Check for a valid serial port */
159 PortString += CONST_STR_LEN("COM"); // Same as len("GDB")
160 if (*PortString != ':')
161 {
162 Value = (ULONG)atol(PortString);
163 if (Value > MAX_COM_PORTS)
165 // if (Value > 0 && Value <= MAX_COM_PORTS)
166 /* Set the port to use */
167 ComPortNumber = Value;
168 }
169 else
170 {
171 /* Retrieve and set its address */
172 Value = strtoul(PortString + 1, NULL, 0);
173 if (Value)
174 {
175 ComPortNumber = 0;
176 ComPortAddress = UlongToPtr(Value);
177 }
178 }
179 }
180
181 /* Check if we got a baud rate */
182 if (BaudString)
183 {
184 /* Move past the actual string and any spaces */
185 BaudString += CONST_STR_LEN("BAUDRATE");
186 while (*BaudString == ' ') ++BaudString;
187
188 /* Make sure we have a rate */
189 if (*BaudString)
190 {
191 /* Read and set it */
192 Value = (ULONG)atol(BaudString + 1);
193 if (Value) ComPortBaudRate = Value;
194 }
195 }
196 }
197
198 if (!ComPortAddress)
199 ComPortAddress = UlongToPtr(BaseArray[ComPortNumber]);
200
201#ifdef KDDEBUG
202 /*
203 * Try to find a free COM port and use it as the KD debugging port.
204 * NOTE: Inspired by freeldr/comm/rs232.c, Rs232PortInitialize(...)
205 */
206 {
207 /*
208 * Enumerate COM ports from the last to the first one, and stop
209 * when we find a valid port. If we reach the first list element
210 * (the undefined COM port), no valid port was found.
211 */
213 ULONG ComPort;
214 for (ComPort = MAX_COM_PORTS; ComPort > 0; ComPort--)
215 {
216 /* Check if the port exist; skip the KD port */
217 Address = UlongToPtr(BaseArray[ComPort]);
218 if ((Address != ComPortAddress) && CpDoesPortExist(Address))
219 break;
220 }
221 if (ComPort != 0 && Address != NULL)
222 CpInitialize(&KdDebugComPort, Address, DEFAULT_BAUD_RATE);
223 }
224#endif
225
226 /* Initialize the port */
227 return KdpPortInitialize(ComPortAddress, ComPortBaudRate);
228}
NTSTATUS NTAPI CpInitialize(_Inout_ PCPPORT Port, _In_ PUCHAR Address, _In_ ULONG BaudRate)
Definition: cport.c:59
BOOLEAN NTAPI CpDoesPortExist(_In_ PUCHAR Address)
Definition: cport.c:33
#define NULL
Definition: types.h:112
#define _strnicmp(_String1, _String2, _MaxCount)
Definition: compat.h:23
_ACRTIMP __msvcrt_long __cdecl atol(const char *)
Definition: string.c:1782
_ACRTIMP __msvcrt_ulong __cdecl strtoul(const char *, char **, int)
Definition: string.c:1859
_ACRTIMP char *__cdecl strstr(const char *, const char *)
Definition: string.c:3415
#define UlongToPtr(u)
Definition: config.h:106
#define CONST_STR_LEN(x)
NTSTATUS NTAPI KdpPortInitialize(_In_ PUCHAR PortAddress, _In_ ULONG BaudRate)
Definition: kdcom.c:97
#define MAX_COM_PORTS
Definition: machpc.c:29
static WCHAR Address[46]
Definition: ping.c:68
_strupr
Definition: string.h:453
char * PSTR
Definition: typedefs.h:51
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
#define DEFAULT_DEBUG_PORT
Definition: uartinfo.h:16
#define DEFAULT_DEBUG_BAUD_RATE
Definition: uartinfo.h:17
#define DEFAULT_BAUD_RATE
Definition: uartinfo.h:24
#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 238 of file kdcom.c.

239{
240 return STATUS_SUCCESS;
241}

◆ KdpPollBreakIn()

KDSTATUS NTAPI KdpPollBreakIn ( VOID  )

Definition at line 289 of file kdcom.c.

290{
291 KDSTATUS KdStatus;
292 UCHAR Byte;
293
294 KdStatus = KdpPollByte(&Byte);
295 if (KdStatus == KdPacketReceived)
296 {
297 if (Byte == 0x03)
298 {
299 KDDBGPRINT("BreakIn Polled.\n");
300 return KdPacketReceived;
301 }
302 else if (Byte == '$')
303 {
304 /* GDB tried to send a new packet. N-ack it. */
305 KdpSendByte('-');
306 }
307 }
308 return KdPacketTimedOut;
309}
unsigned char Byte
Definition: zlib.h:37
#define KDDBGPRINT(...)
Definition: kddll.h:19
KDP_STATUS NTAPI KdpPollByte(OUT PUCHAR OutByte)
Definition: kdcom.c:252
VOID NTAPI KdpSendByte(IN UCHAR Byte)
Definition: kdcom.c:246
#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 254 of file kdcom.c.

255{
256 /* Poll the byte */
257 if (CpGetByte(&KdComPort, OutByte, FALSE, FALSE) == CP_GET_SUCCESS)
258 {
259 return KdPacketReceived;
260 }
261 else
262 {
263 return KdPacketTimedOut;
264 }
265}
USHORT NTAPI CpGetByte(_Inout_ PCPPORT Port, _Out_ PUCHAR Byte, _In_ BOOLEAN Wait, _In_ BOOLEAN Poll)
Definition: cport.c:82
#define CP_GET_SUCCESS
Definition: cportlib.h:17
#define FALSE
Definition: types.h:117
CPPORT KdComPort
Definition: kdcom.c:20

◆ KdpPortInitialize()

NTSTATUS NTAPI KdpPortInitialize ( _In_ PUCHAR  PortAddress,
_In_ ULONG  BaudRate 
)

Definition at line 95 of file kdcom.c.

98{
100
101 Status = CpInitialize(&KdComPort, PortAddress, BaudRate);
102 if (!NT_SUCCESS(Status))
104
106 return STATUS_SUCCESS;
107}
LONG NTSTATUS
Definition: precomp.h:26
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
Status
Definition: gdiplustypes.h:25
PUCHAR KdComPortInUse
Definition: usage.c:17
PUCHAR Address
Definition: cportlib.h:28

◆ KdpReceiveByte()

KDSTATUS NTAPI KdpReceiveByte ( OUT PUCHAR  OutByte)

Definition at line 269 of file kdcom.c.

270{
271 USHORT CpStatus;
272
273 do
274 {
275 CpStatus = CpGetByte(&KdComPort, OutByte, TRUE, FALSE);
276 } while (CpStatus == CP_GET_NODATA);
277
278 /* Get the byte */
279 if (CpStatus == CP_GET_SUCCESS)
280 {
281 return KdPacketReceived;
282 }
283
284 return KdPacketTimedOut;
285}
#define CP_GET_NODATA
Definition: cportlib.h:18
#define TRUE
Definition: types.h:120
unsigned short USHORT
Definition: pedump.c:61

◆ KdpSendByte()

VOID NTAPI KdpSendByte ( IN UCHAR  Byte)

Definition at line 246 of file kdcom.c.

247{
248 /* Send the byte */
250}
VOID NTAPI CpPutByte(_Inout_ PCPPORT Port, _In_ UCHAR Byte)
Definition: cport.c:93

◆ KdRestore()

NTSTATUS NTAPI KdRestore ( IN BOOLEAN  SleepTransition)

Definition at line 87 of file kdcom.c.

88{
89 /* Nothing to do on COM ports */
90 return STATUS_SUCCESS;
91}

◆ KdSave()

NTSTATUS NTAPI KdSave ( IN BOOLEAN  SleepTransition)

Definition at line 79 of file kdcom.c.

80{
81 /* Nothing to do on COM ports */
82 return STATUS_SUCCESS;
83}

Variable Documentation

◆ KdComPort

CPPORT KdComPort

Definition at line 19 of file kdcom.c.