ReactOS  0.4.15-dev-1197-g8081ba9
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
 
#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)
 
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 29 of file kdcom.c.

◆ DEFAULT_DEBUG_BAUD_RATE

#define DEFAULT_DEBUG_BAUD_RATE   115200

Definition at line 28 of file kdcom.c.

◆ DEFAULT_DEBUG_COM1_IRQ

#define DEFAULT_DEBUG_COM1_IRQ   4

Definition at line 26 of file kdcom.c.

◆ DEFAULT_DEBUG_COM2_IRQ

#define DEFAULT_DEBUG_COM2_IRQ   3

Definition at line 27 of file kdcom.c.

◆ DEFAULT_DEBUG_PORT

#define DEFAULT_DEBUG_PORT   2 /* COM2 */

Definition at line 25 of file kdcom.c.

◆ MAX_COM_PORTS

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

Definition at line 48 of file kdcom.c.

Function Documentation

◆ KdD0Transition()

NTSTATUS NTAPI KdD0Transition ( VOID  )

Definition at line 99 of file kdcom.c.

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

◆ KdD3Transition()

NTSTATUS NTAPI KdD3Transition ( VOID  )

Definition at line 106 of file kdcom.c.

107 {
108  return STATUS_SUCCESS;
109 }
return STATUS_SUCCESS
Definition: btrfs.c:3014

◆ KdDebuggerInitialize0()

NTSTATUS NTAPI KdDebuggerInitialize0 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 158 of file kdcom.c.

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

Referenced by KdInitSystem().

◆ KdDebuggerInitialize1()

NTSTATUS NTAPI KdDebuggerInitialize1 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 283 of file kdcom.c.

284 {
285  return STATUS_SUCCESS;
286 }
return STATUS_SUCCESS
Definition: btrfs.c:3014

Referenced by Phase1InitializationDiscard().

◆ KdpPollBreakIn()

KDP_STATUS NTAPI KdpPollBreakIn ( VOID  )

Definition at line 343 of file kdcom.c.

344 {
345  KDP_STATUS KdStatus;
346  UCHAR Byte;
347 
348  KdStatus = KdpPollByte(&Byte);
349  if ((KdStatus == KDP_PACKET_RECEIVED) && (Byte == BREAKIN_PACKET_BYTE))
350  {
351  return KDP_PACKET_RECEIVED;
352  }
353  return KDP_PACKET_TIMEOUT;
354 }
KDP_STATUS
Definition: kddll.h:25
KDP_STATUS NTAPI KdpPollByte(OUT PUCHAR OutByte)
Definition: kdcom.c:299
#define BREAKIN_PACKET_BYTE
Definition: windbgkd.h:31
unsigned char UCHAR
Definition: xmlstorage.h:181
unsigned char Byte
Definition: zlib.h:37

Referenced by KdReceivePacket().

◆ KdpPollByte()

KDP_STATUS NTAPI KdpPollByte ( OUT PUCHAR  OutByte)

Definition at line 299 of file kdcom.c.

300 {
301  USHORT Status;
302 
303  /* Poll the byte */
304  Status = CpGetByte(&KdComPort, OutByte, FALSE, FALSE);
305  switch (Status)
306  {
307  case CP_GET_SUCCESS:
308  return KDP_PACKET_RECEIVED;
309 
310  case CP_GET_NODATA:
311  return KDP_PACKET_TIMEOUT;
312 
313  case CP_GET_ERROR:
314  default:
315  return KDP_PACKET_RESEND;
316  }
317 }
#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
#define FALSE
Definition: types.h:117
CPPORT KdComPort
Definition: kdcom.c:52
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 129 of file kdcom.c.

131 {
133 
134  KDDBGPRINT("KdpPortInitialize, Port = COM%ld\n", ComPortNumber);
135 
137  UlongToPtr(BaseArray[ComPortNumber]),
138  ComPortBaudRate);
139  if (!NT_SUCCESS(Status))
140  {
142  }
143  else
144  {
146  return STATUS_SUCCESS;
147  }
148 }
#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:52
#define UlongToPtr(u)
Definition: config.h:106
PUCHAR KdComPortInUse
Definition: usage.c:17
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Status
Definition: gdiplustypes.h:24
NTSTATUS NTAPI CpInitialize(IN PCPPORT Port, IN PUCHAR Address, IN ULONG BaudRate)
Definition: cport.c:88
PUCHAR Address
Definition: cportlib.h:29
static const ULONG BaseArray[]
Definition: hwide.c:41
return STATUS_SUCCESS
Definition: btrfs.c:3014

Referenced by KdDebuggerInitialize0().

◆ KdpReceiveByte()

KDP_STATUS NTAPI KdpReceiveByte ( OUT PUCHAR  OutByte)

Definition at line 321 of file kdcom.c.

322 {
323  USHORT Status;
324 
325  /* Get the byte */
326  Status = CpGetByte(&KdComPort, OutByte, TRUE, FALSE);
327  switch (Status)
328  {
329  case CP_GET_SUCCESS:
330  return KDP_PACKET_RECEIVED;
331 
332  case CP_GET_NODATA:
333  return KDP_PACKET_TIMEOUT;
334 
335  case CP_GET_ERROR:
336  default:
337  return KDP_PACKET_RESEND;
338  }
339 }
#define CP_GET_ERROR
Definition: cportlib.h:20
#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
#define FALSE
Definition: types.h:117
CPPORT KdComPort
Definition: kdcom.c:52
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 291 of file kdcom.c.

292 {
293  /* Send the byte */
295 }
CPPORT KdComPort
Definition: kdcom.c:52
VOID NTAPI CpPutByte(IN PCPPORT Port, IN UCHAR Byte)
Definition: cport.c:306
unsigned char Byte
Definition: zlib.h:37

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 121 of file kdcom.c.

122 {
123  /* Nothing to do on COM ports */
124  return STATUS_SUCCESS;
125 }
return STATUS_SUCCESS
Definition: btrfs.c:3014

◆ KdSave()

NTSTATUS NTAPI KdSave ( IN BOOLEAN  SleepTransition)

Definition at line 113 of file kdcom.c.

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

Variable Documentation

◆ KdComPort

CPPORT KdComPort

Definition at line 52 of file kdcom.c.

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

◆ KdComPortIrq

ULONG KdComPortIrq = 0

Definition at line 53 of file kdcom.c.

Referenced by KdDebuggerInitialize0().