ReactOS  0.4.14-dev-337-gf981a68
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 35 of file kdcom.c.

Function Documentation

◆ KdD0Transition()

NTSTATUS NTAPI KdD0Transition ( VOID  )

Definition at line 86 of file kdcom.c.

87 {
88  return STATUS_SUCCESS;
89 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdD3Transition()

NTSTATUS NTAPI KdD3Transition ( VOID  )

Definition at line 93 of file kdcom.c.

94 {
95  return STATUS_SUCCESS;
96 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdDebuggerInitialize0()

NTSTATUS NTAPI KdDebuggerInitialize0 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 143 of file kdcom.c.

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

◆ KdDebuggerInitialize1()

NTSTATUS NTAPI KdDebuggerInitialize1 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 266 of file kdcom.c.

267 {
268  return STATUS_SUCCESS;
269 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdpPollBreakIn()

KDSTATUS NTAPI KdpPollBreakIn ( VOID  )

Definition at line 311 of file kdcom.c.

312 {
313  KDSTATUS KdStatus;
314  UCHAR Byte;
315 
316  KdStatus = KdpPollByte(&Byte);
317  if (KdStatus == KdPacketReceived)
318  {
319  if (Byte == 0x03)
320  {
321  KDDBGPRINT("BreakIn Polled.\n");
322  return KdPacketReceived;
323  }
324  else if (Byte == '$')
325  {
326  /* GDB tried to send a new packet. N-ack it. */
327  KdpSendByte('-');
328  }
329  }
330  return KdPacketTimedOut;
331 }
#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:288
#define KdPacketTimedOut
Definition: kddll.h:6
VOID NTAPI KdpSendByte(IN UCHAR Byte)
Definition: kdcom.c:280
unsigned char UCHAR
Definition: xmlstorage.h:181
ULONG KDSTATUS
Definition: kddll.h:4

◆ KdpPollByte()

KDSTATUS NTAPI KdpPollByte ( OUT PUCHAR  OutByte)

Definition at line 282 of file kdcom.c.

283 {
284  /* Poll the byte */
285  if (CpGetByte(&KdComPort, OutByte, FALSE, FALSE) == CP_GET_SUCCESS)
286  {
287  return KdPacketReceived;
288  }
289  else
290  {
291  return KdPacketTimedOut;
292  }
293 }
#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:41
#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 116 of file kdcom.c.

118 {
120 
122  UlongToPtr(BaseArray[ComPortNumber]),
123  ComPortBaudRate);
124  if (!NT_SUCCESS(Status))
125  {
127  }
128  else
129  {
131  return STATUS_SUCCESS;
132  }
133 }
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
CPPORT KdComPort
Definition: kdcom.c:41
#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
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdpReceiveByte()

KDSTATUS NTAPI KdpReceiveByte ( _Out_ PUCHAR  OutByte)

Definition at line 297 of file kdcom.c.

298 {
299  USHORT CpStatus = CpGetByte(&KdComPort, OutByte, TRUE, FALSE);
300  /* Get the byte */
301  if (CpStatus == CP_GET_SUCCESS)
302  {
303  return KdPacketReceived;
304  }
305 
306  return KdPacketTimedOut;
307 }
#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:41
#define KdPacketTimedOut
Definition: kddll.h:6
unsigned short USHORT
Definition: pedump.c:61
#define CP_GET_SUCCESS
Definition: cportlib.h:18

◆ KdpSendByte()

VOID NTAPI KdpSendByte ( _In_ UCHAR  Byte)

Definition at line 274 of file kdcom.c.

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

◆ KdRestore()

NTSTATUS NTAPI KdRestore ( IN BOOLEAN  SleepTransition)

Definition at line 108 of file kdcom.c.

109 {
110  /* Nothing to do on COM ports */
111  return STATUS_SUCCESS;
112 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdSave()

NTSTATUS NTAPI KdSave ( IN BOOLEAN  SleepTransition)

Definition at line 100 of file kdcom.c.

101 {
102  /* Nothing to do on COM ports */
103  return STATUS_SUCCESS;
104 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

Variable Documentation

◆ KdComPort

CPPORT KdComPort

Definition at line 39 of file kdcom.c.

◆ KdComPortIrq

ULONG KdComPortIrq = 0

Definition at line 40 of file kdcom.c.