ReactOS  r76032
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

#define DEFAULT_BAUD_RATE   19200

Definition at line 23 of file kdcom.c.

Referenced by KdDebuggerInitialize0().

#define DEFAULT_DEBUG_BAUD_RATE   115200 /* 115200 Baud */

Definition at line 21 of file kdcom.c.

Referenced by KdDebuggerInitialize0().

#define DEFAULT_DEBUG_COM1_IRQ   4 /* COM1 IRQ */

Definition at line 19 of file kdcom.c.

#define DEFAULT_DEBUG_COM2_IRQ   3 /* COM2 IRQ */

Definition at line 20 of file kdcom.c.

#define DEFAULT_DEBUG_PORT   2 /* COM2 */

Definition at line 18 of file kdcom.c.

Referenced by KdDebuggerInitialize0().

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

Definition at line 37 of file kdcom.c.

Referenced by KdDebuggerInitialize0().

Function Documentation

NTSTATUS NTAPI KdD0Transition ( VOID  )

Definition at line 88 of file kdcom.c.

Referenced by KdPowerTransition().

89 {
90  return STATUS_SUCCESS;
91 }
return STATUS_SUCCESS
Definition: btrfs.c:2664
NTSTATUS NTAPI KdD3Transition ( VOID  )

Definition at line 95 of file kdcom.c.

Referenced by KdPowerTransition().

96 {
97  return STATUS_SUCCESS;
98 }
return STATUS_SUCCESS
Definition: btrfs.c:2664
NTSTATUS NTAPI KdDebuggerInitialize0 ( IN PLOADER_PARAMETER_BLOCK LoaderBlock  OPTIONAL)

Definition at line 147 of file kdcom.c.

Referenced by KdInitSystem().

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

Definition at line 272 of file kdcom.c.

Referenced by Phase1InitializationDiscard().

273 {
274  return STATUS_SUCCESS;
275 }
return STATUS_SUCCESS
Definition: btrfs.c:2664
KDP_STATUS NTAPI KdpPollBreakIn ( VOID  )

Definition at line 332 of file kdcom.c.

Referenced by KdReceivePacket().

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

Definition at line 288 of file kdcom.c.

Referenced by KdpPollBreakIn().

289 {
290  USHORT Status;
291 
292  /* Poll the byte */
293  Status = CpGetByte(&KdComPort, OutByte, FALSE, FALSE);
294  switch (Status)
295  {
296  case CP_GET_SUCCESS:
297  return KDP_PACKET_RECEIVED;
298 
299  case CP_GET_NODATA:
300  return KDP_PACKET_TIMEOUT;
301 
302  case CP_GET_ERROR:
303  default:
304  return KDP_PACKET_RESEND;
305  }
306 }
#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:41
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
NTSTATUS NTAPI KdpPortInitialize ( IN ULONG  ComPortNumber,
IN ULONG  ComPortBaudRate 
)

Definition at line 118 of file kdcom.c.

Referenced by KdDebuggerInitialize0().

120 {
122 
123  KDDBGPRINT("KdpPortInitialize, Port = COM%ld\n", ComPortNumber);
124 
125  Status = CpInitialize(&KdComPort,
126  UlongToPtr(BaseArray[ComPortNumber]),
127  ComPortBaudRate);
128  if (!NT_SUCCESS(Status))
129  {
131  }
132  else
133  {
135  return STATUS_SUCCESS;
136  }
137 }
#define KDDBGPRINT(...)
Definition: kddll.h:19
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
return STATUS_SUCCESS
Definition: btrfs.c:2664
CPPORT KdComPort
Definition: kdcom.c:41
#define UlongToPtr(ul)
Definition: basetsd.h:97
PUCHAR KdComPortInUse
Definition: usage.c:17
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
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
LONG NTSTATUS
Definition: DriverTester.h:11
KDP_STATUS NTAPI KdpReceiveByte ( OUT PUCHAR  OutByte)

Definition at line 310 of file kdcom.c.

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

311 {
312  USHORT Status;
313 
314  /* Get the byte */
315  Status = CpGetByte(&KdComPort, OutByte, TRUE, FALSE);
316  switch (Status)
317  {
318  case CP_GET_SUCCESS:
319  return KDP_PACKET_RECEIVED;
320 
321  case CP_GET_NODATA:
322  return KDP_PACKET_TIMEOUT;
323 
324  case CP_GET_ERROR:
325  default:
326  return KDP_PACKET_RESEND;
327  }
328 }
#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
#define FALSE
Definition: types.h:117
CPPORT KdComPort
Definition: kdcom.c:41
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
VOID NTAPI KdpSendByte ( IN UCHAR  Byte)

Definition at line 280 of file kdcom.c.

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

281 {
282  /* Send the byte */
284 }
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
NTSTATUS NTAPI KdRestore ( IN BOOLEAN  SleepTransition)

Definition at line 110 of file kdcom.c.

Referenced by KdExitDebugger(), and KdpSwitchProcessor().

111 {
112  /* Nothing to do on COM ports */
113  return STATUS_SUCCESS;
114 }
return STATUS_SUCCESS
Definition: btrfs.c:2664
NTSTATUS NTAPI KdSave ( IN BOOLEAN  SleepTransition)

Definition at line 102 of file kdcom.c.

Referenced by KdEnterDebugger(), and KdpSwitchProcessor().

103 {
104  /* Nothing to do on COM ports */
105  return STATUS_SUCCESS;
106 }
return STATUS_SUCCESS
Definition: btrfs.c:2664

Variable Documentation

CPPORT KdComPort

Definition at line 41 of file kdcom.c.

ULONG KdComPortIrq = 0

Definition at line 42 of file kdcom.c.

Referenced by KdDebuggerInitialize0().