ReactOS  0.4.14-dev-1332-g6db3d88
kdcom.c File Reference
#include "kddll.h"
#include <cportlib/cportlib.h>
#include <arc/arc.h>
#include <stdio.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)
 
KDP_STATUS NTAPI KdpPollByte (OUT PUCHAR OutByte)
 
KDP_STATUS NTAPI KdpReceiveByte (OUT PUCHAR OutByte)
 
KDP_STATUS NTAPI KdpPollBreakIn (VOID)
 

Variables

CPPORT KdComPort
 
ULONG KdComPortIrq = 0
 

Macro Definition Documentation

◆ DEFAULT_BAUD_RATE

#define DEFAULT_BAUD_RATE   19200

Definition at line 23 of file kdcom.c.

◆ DEFAULT_DEBUG_BAUD_RATE

#define DEFAULT_DEBUG_BAUD_RATE   115200 /* 115200 Baud */

Definition at line 21 of file kdcom.c.

◆ DEFAULT_DEBUG_COM1_IRQ

#define DEFAULT_DEBUG_COM1_IRQ   4 /* COM1 IRQ */

Definition at line 19 of file kdcom.c.

◆ DEFAULT_DEBUG_COM2_IRQ

#define DEFAULT_DEBUG_COM2_IRQ   3 /* COM2 IRQ */

Definition at line 20 of file kdcom.c.

◆ DEFAULT_DEBUG_PORT

#define DEFAULT_DEBUG_PORT   2 /* COM2 */

Definition at line 18 of file kdcom.c.

◆ MAX_COM_PORTS

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

Definition at line 41 of file kdcom.c.

Function Documentation

◆ KdD0Transition()

NTSTATUS NTAPI KdD0Transition ( VOID  )

Definition at line 92 of file kdcom.c.

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

◆ KdD3Transition()

NTSTATUS NTAPI KdD3Transition ( VOID  )

Definition at line 99 of file kdcom.c.

100 {
101  return STATUS_SUCCESS;
102 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ KdDebuggerInitialize0()

NTSTATUS NTAPI KdDebuggerInitialize0 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 151 of file kdcom.c.

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

Referenced by KdInitSystem().

◆ KdDebuggerInitialize1()

NTSTATUS NTAPI KdDebuggerInitialize1 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 276 of file kdcom.c.

277 {
278  return STATUS_SUCCESS;
279 }
return STATUS_SUCCESS
Definition: btrfs.c:2938

Referenced by Phase1InitializationDiscard().

◆ KdpPollBreakIn()

KDP_STATUS NTAPI KdpPollBreakIn ( VOID  )

Definition at line 336 of file kdcom.c.

337 {
338  KDP_STATUS KdStatus;
339  UCHAR Byte;
340 
341  KdStatus = KdpPollByte(&Byte);
342  if ((KdStatus == KDP_PACKET_RECEIVED) && (Byte == BREAKIN_PACKET_BYTE))
343  {
344  return KDP_PACKET_RECEIVED;
345  }
346  return KDP_PACKET_TIMEOUT;
347 }
unsigned char Byte
Definition: zconf.h:391
KDP_STATUS
Definition: kddll.h:25
KDP_STATUS NTAPI KdpPollByte(OUT PUCHAR OutByte)
Definition: kdcom.c:292
#define BREAKIN_PACKET_BYTE
Definition: windbgkd.h:31
unsigned char UCHAR
Definition: xmlstorage.h:181

Referenced by KdReceivePacket().

◆ KdpPollByte()

KDP_STATUS NTAPI KdpPollByte ( OUT PUCHAR  OutByte)

Definition at line 292 of file kdcom.c.

293 {
294  USHORT Status;
295 
296  /* Poll the byte */
297  Status = CpGetByte(&KdComPort, OutByte, FALSE, FALSE);
298  switch (Status)
299  {
300  case CP_GET_SUCCESS:
301  return KDP_PACKET_RECEIVED;
302 
303  case CP_GET_NODATA:
304  return KDP_PACKET_TIMEOUT;
305 
306  case CP_GET_ERROR:
307  default:
308  return KDP_PACKET_RESEND;
309  }
310 }
#define CP_GET_ERROR
Definition: cportlib.h:20
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
Status
Definition: gdiplustypes.h:24
unsigned short USHORT
Definition: pedump.c:61
#define CP_GET_SUCCESS
Definition: cportlib.h:18
#define CP_GET_NODATA
Definition: cportlib.h:19

Referenced by gdb_receive_packet(), and KdpPollBreakIn().

◆ KdpPortInitialize()

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

Definition at line 122 of file kdcom.c.

124 {
126 
127  KDDBGPRINT("KdpPortInitialize, Port = COM%ld\n", ComPortNumber);
128 
130  UlongToPtr(BaseArray[ComPortNumber]),
131  ComPortBaudRate);
132  if (!NT_SUCCESS(Status))
133  {
135  }
136  else
137  {
139  return STATUS_SUCCESS;
140  }
141 }
#define KDDBGPRINT(...)
Definition: kddll.h:19
#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

Referenced by KdDebuggerInitialize0().

◆ KdpReceiveByte()

KDP_STATUS NTAPI KdpReceiveByte ( OUT PUCHAR  OutByte)

Definition at line 314 of file kdcom.c.

315 {
316  USHORT Status;
317 
318  /* Get the byte */
319  Status = CpGetByte(&KdComPort, OutByte, TRUE, FALSE);
320  switch (Status)
321  {
322  case CP_GET_SUCCESS:
323  return KDP_PACKET_RECEIVED;
324 
325  case CP_GET_NODATA:
326  return KDP_PACKET_TIMEOUT;
327 
328  case CP_GET_ERROR:
329  default:
330  return KDP_PACKET_RESEND;
331  }
332 }
#define TRUE
Definition: types.h:120
#define CP_GET_ERROR
Definition: cportlib.h:20
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
Status
Definition: gdiplustypes.h:24
unsigned short USHORT
Definition: pedump.c:61
#define CP_GET_SUCCESS
Definition: cportlib.h:18
#define CP_GET_NODATA
Definition: cportlib.h:19

Referenced by finish_gdb_packet(), gdb_receive_packet(), KdpReceiveBuffer(), and KdpReceivePacketLeader().

◆ KdpSendByte()

VOID NTAPI KdpSendByte ( IN UCHAR  Byte)

Definition at line 284 of file kdcom.c.

285 {
286  /* Send the byte */
288 }
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

Referenced by finish_gdb_packet(), gdb_receive_packet(), KdpPollBreakIn(), KdpSendBuffer(), KdSendPacket(), send_gdb_partial_binary(), send_gdb_partial_packet(), and start_gdb_packet().

◆ KdRestore()

NTSTATUS NTAPI KdRestore ( IN BOOLEAN  SleepTransition)

Definition at line 114 of file kdcom.c.

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

◆ KdSave()

NTSTATUS NTAPI KdSave ( IN BOOLEAN  SleepTransition)

Definition at line 106 of file kdcom.c.

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

Variable Documentation

◆ KdComPort

CPPORT KdComPort

Definition at line 45 of file kdcom.c.

Referenced by KdpPollByte(), KdpPortInitialize(), KdpReceiveByte(), and KdpSendByte().

◆ KdComPortIrq

ULONG KdComPortIrq = 0

Definition at line 46 of file kdcom.c.

Referenced by KdDebuggerInitialize0().