ReactOS  0.4.14-dev-1314-gacf135d
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 /* COM1 IRQ */
 
#define DEFAULT_DEBUG_COM2_IRQ   3 /* COM2 IRQ */
 
#define DEFAULT_DEBUG_BAUD_RATE   115200 /* 115200 Baud */
 
#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 21 of file kdcom.c.

◆ DEFAULT_DEBUG_BAUD_RATE

#define DEFAULT_DEBUG_BAUD_RATE   115200 /* 115200 Baud */

Definition at line 19 of file kdcom.c.

◆ DEFAULT_DEBUG_COM1_IRQ

#define DEFAULT_DEBUG_COM1_IRQ   4 /* COM1 IRQ */

Definition at line 17 of file kdcom.c.

◆ DEFAULT_DEBUG_COM2_IRQ

#define DEFAULT_DEBUG_COM2_IRQ   3 /* COM2 IRQ */

Definition at line 18 of file kdcom.c.

◆ DEFAULT_DEBUG_PORT

#define DEFAULT_DEBUG_PORT   2 /* COM2 */

Definition at line 16 of file kdcom.c.

◆ MAX_COM_PORTS

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

Definition at line 39 of file kdcom.c.

Function Documentation

◆ KdD0Transition()

NTSTATUS NTAPI KdD0Transition ( VOID  )

Definition at line 90 of file kdcom.c.

91 {
92  return STATUS_SUCCESS;
93 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdD3Transition()

NTSTATUS NTAPI KdD3Transition ( VOID  )

Definition at line 97 of file kdcom.c.

98 {
99  return STATUS_SUCCESS;
100 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdDebuggerInitialize0()

NTSTATUS NTAPI KdDebuggerInitialize0 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 145 of file kdcom.c.

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

◆ KdDebuggerInitialize1()

NTSTATUS NTAPI KdDebuggerInitialize1 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 268 of file kdcom.c.

269 {
270  return STATUS_SUCCESS;
271 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdpPollBreakIn()

KDSTATUS NTAPI KdpPollBreakIn ( VOID  )

Definition at line 319 of file kdcom.c.

320 {
321  KDSTATUS KdStatus;
322  UCHAR Byte;
323 
324  KdStatus = KdpPollByte(&Byte);
325  if (KdStatus == KdPacketReceived)
326  {
327  if (Byte == 0x03)
328  {
329  KDDBGPRINT("BreakIn Polled.\n");
330  return KdPacketReceived;
331  }
332  else if (Byte == '$')
333  {
334  /* GDB tried to send a new packet. N-ack it. */
335  KdpSendByte('-');
336  }
337  }
338  return KdPacketTimedOut;
339 }
#define KdPacketReceived
Definition: kddll.h:5
#define KDDBGPRINT(...)
Definition: kddll.h:19
unsigned char Byte
Definition: zconf.h:391
KDP_STATUS NTAPI KdpPollByte(OUT PUCHAR OutByte)
Definition: kdcom.c:292
#define KdPacketTimedOut
Definition: kddll.h:6
VOID NTAPI KdpSendByte(IN UCHAR Byte)
Definition: kdcom.c:284
unsigned char UCHAR
Definition: xmlstorage.h:181
ULONG KDSTATUS
Definition: kddll.h:4

◆ KdpPollByte()

KDSTATUS NTAPI KdpPollByte ( OUT PUCHAR  OutByte)

Definition at line 284 of file kdcom.c.

285 {
286  /* Poll the byte */
287  if (CpGetByte(&KdComPort, OutByte, FALSE, FALSE) == CP_GET_SUCCESS)
288  {
289  return KdPacketReceived;
290  }
291  else
292  {
293  return KdPacketTimedOut;
294  }
295 }
#define KdPacketReceived
Definition: kddll.h:5
USHORT NTAPI CpGetByte(IN PCPPORT Port, OUT PUCHAR Byte, IN BOOLEAN Wait, IN BOOLEAN Poll)
Definition: cport.c:256
CPPORT KdComPort
Definition: kdcom.c:45
#define KdPacketTimedOut
Definition: kddll.h:6
#define CP_GET_SUCCESS
Definition: cportlib.h:18

◆ KdpPortInitialize()

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

Definition at line 120 of file kdcom.c.

122 {
124 
126  UlongToPtr(BaseArray[ComPortNumber]),
127  ComPortBaudRate);
128  if (!NT_SUCCESS(Status))
129  {
131  }
132 
134  return STATUS_SUCCESS;
135 }
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
CPPORT KdComPort
Definition: kdcom.c:45
#define UlongToPtr(u)
Definition: config.h:106
PUCHAR KdComPortInUse
Definition: usage.c:17
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS NTAPI CpInitialize(IN PCPPORT Port, IN PUCHAR Address, IN ULONG BaudRate)
Definition: cport.c:88
PUCHAR Address
Definition: cportlib.h:29
Status
Definition: gdiplustypes.h:24
static const ULONG BaseArray[]
Definition: hwide.c:41
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdpReceiveByte()

KDSTATUS NTAPI KdpReceiveByte ( _Out_ PUCHAR  OutByte)

Definition at line 299 of file kdcom.c.

300 {
301  USHORT CpStatus;
302 
303  do
304  {
305  CpStatus = CpGetByte(&KdComPort, OutByte, TRUE, FALSE);
306  } while (CpStatus == CP_GET_NODATA);
307 
308  /* Get the byte */
309  if (CpStatus == CP_GET_SUCCESS)
310  {
311  return KdPacketReceived;
312  }
313 
314  return KdPacketTimedOut;
315 }
#define KdPacketReceived
Definition: kddll.h:5
#define TRUE
Definition: types.h:120
USHORT NTAPI CpGetByte(IN PCPPORT Port, OUT PUCHAR Byte, IN BOOLEAN Wait, IN BOOLEAN Poll)
Definition: cport.c:256
CPPORT KdComPort
Definition: kdcom.c:45
#define KdPacketTimedOut
Definition: kddll.h:6
unsigned short USHORT
Definition: pedump.c:61
#define CP_GET_SUCCESS
Definition: cportlib.h:18
#define CP_GET_NODATA
Definition: cportlib.h:19

◆ KdpSendByte()

VOID NTAPI KdpSendByte ( _In_ UCHAR  Byte)

Definition at line 276 of file kdcom.c.

277 {
278  /* Send the byte */
280 }
unsigned char Byte
Definition: zconf.h:391
CPPORT KdComPort
Definition: kdcom.c:45
VOID NTAPI CpPutByte(IN PCPPORT Port, IN UCHAR Byte)
Definition: cport.c:306

◆ KdRestore()

NTSTATUS NTAPI KdRestore ( IN BOOLEAN  SleepTransition)

Definition at line 112 of file kdcom.c.

113 {
114  /* Nothing to do on COM ports */
115  return STATUS_SUCCESS;
116 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdSave()

NTSTATUS NTAPI KdSave ( IN BOOLEAN  SleepTransition)

Definition at line 104 of file kdcom.c.

105 {
106  /* Nothing to do on COM ports */
107  return STATUS_SUCCESS;
108 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

Variable Documentation

◆ KdComPort

CPPORT KdComPort

Definition at line 43 of file kdcom.c.

◆ KdComPortIrq

ULONG KdComPortIrq = 0

Definition at line 44 of file kdcom.c.