2 * COPYRIGHT: GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/base/kdgdb/kdcom.c
5 * PURPOSE: COM port functions for the kernel debugger.
10 #include <cportlib/cportlib.h>
13 #include <ndk/halfuncs.h>
15 /* Serial debug connection */
16 #define DEFAULT_DEBUG_PORT 2 /* COM2 */
17 #define DEFAULT_DEBUG_COM1_IRQ 4 /* COM1 IRQ */
18 #define DEFAULT_DEBUG_COM2_IRQ 3 /* COM2 IRQ */
19 #define DEFAULT_DEBUG_BAUD_RATE 115200 /* 115200 Baud */
21 #define DEFAULT_BAUD_RATE 19200
23 #if defined(_M_IX86) || defined(_M_AMD64)
24 const ULONG BaseArray
[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
26 const ULONG BaseArray
[] = {0, 0x800003F8};
27 #elif defined(_M_MIPS)
28 const ULONG BaseArray
[] = {0, 0x80006000, 0x80007000};
30 const ULONG BaseArray
[] = {0, 0xF1012000};
32 #error Unknown architecture
35 /* GLOBALS ********************************************************************/
37 CPPORT KdDebugComPort
;
38 ULONG KdDebugComPortIrq
= 0; // Not used at the moment.
41 /* FUNCTIONS ******************************************************************/
47 return STATUS_SUCCESS
;
54 return STATUS_SUCCESS
;
59 KdSave(IN BOOLEAN SleepTransition
)
61 /* Nothing to do on COM ports */
62 return STATUS_SUCCESS
;
67 KdRestore(IN BOOLEAN SleepTransition
)
69 /* Nothing to do on COM ports */
70 return STATUS_SUCCESS
;
75 KdpPortInitialize(IN ULONG ComPortNumber
,
76 IN ULONG ComPortBaudRate
)
80 Status
= CpInitialize(&KdDebugComPort
,
81 UlongToPtr(BaseArray
[ComPortNumber
]),
83 if (!NT_SUCCESS(Status
))
85 return STATUS_INVALID_PARAMETER
;
89 KdComPortInUse
= KdDebugComPort
.Address
;
90 return STATUS_SUCCESS
;
94 /******************************************************************************
95 * \name KdDebuggerInitialize0
96 * \brief Phase 0 initialization.
97 * \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
102 KdDebuggerInitialize0(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL
)
104 ULONG ComPortNumber
= DEFAULT_DEBUG_PORT
;
105 ULONG ComPortBaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
107 PCHAR CommandLine
, PortString
, BaudString
, IrqString
;
110 /* Check if e have a LoaderBlock */
114 /* Get the Command Line */
115 CommandLine
= LoaderBlock
->LoadOptions
;
118 _strupr(CommandLine
);
120 /* Get the port and baud rate */
121 PortString
= strstr(CommandLine
, "DEBUGPORT");
122 BaudString
= strstr(CommandLine
, "BAUDRATE");
123 IrqString
= strstr(CommandLine
, "IRQ");
125 /* Check if we got the /DEBUGPORT parameter */
128 /* Move past the actual string, to reach the port*/
129 PortString
+= strlen("DEBUGPORT");
131 /* Now get past any spaces and skip the equal sign */
132 while (*PortString
== ' ') PortString
++;
135 /* Do we have a serial port? */
136 if (strncmp(PortString
, "COM", 3) != 0)
138 return STATUS_INVALID_PARAMETER
;
141 /* Check for a valid Serial Port */
143 Value
= atol(PortString
);
144 if (Value
>= sizeof(BaseArray
) / sizeof(BaseArray
[0]))
146 return STATUS_INVALID_PARAMETER
;
149 /* Set the port to use */
150 ComPortNumber
= Value
;
153 /* Check if we got a baud rate */
156 /* Move past the actual string, to reach the rate */
157 BaudString
+= strlen("BAUDRATE");
159 /* Now get past any spaces */
160 while (*BaudString
== ' ') BaudString
++;
162 /* And make sure we have a rate */
165 /* Read and set it */
166 Value
= atol(BaudString
+ 1);
167 if (Value
) ComPortBaudRate
= Value
;
171 /* Check Serial Port Settings [IRQ] */
174 /* Move past the actual string, to reach the rate */
175 IrqString
+= strlen("IRQ");
177 /* Now get past any spaces */
178 while (*IrqString
== ' ') IrqString
++;
180 /* And make sure we have an IRQ */
183 /* Read and set it */
184 Value
= atol(IrqString
+ 1);
185 if (Value
) KdDebugComPortIrq
= Value
;
190 /* Initialize the port */
191 return KdpPortInitialize(ComPortNumber
, ComPortBaudRate
);
194 /******************************************************************************
195 * \name KdDebuggerInitialize1
196 * \brief Phase 1 initialization.
197 * \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
202 KdDebuggerInitialize1(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL
)
204 return STATUS_SUCCESS
;
210 KdpSendByte(_In_ UCHAR Byte
)
213 CpPutByte(&KdDebugComPort
, Byte
);
218 KdpPollByte(OUT PUCHAR OutByte
)
221 if (CpGetByte(&KdDebugComPort
, OutByte
, FALSE
, FALSE
) == CP_GET_SUCCESS
)
223 return KdPacketReceived
;
227 return KdPacketTimedOut
;
233 KdpReceiveByte(_Out_ PUCHAR OutByte
)
236 if (CpGetByte(&KdDebugComPort
, OutByte
, TRUE
, FALSE
) == CP_GET_SUCCESS
)
238 return KdPacketReceived
;
242 return KdPacketTimedOut
;
253 KdStatus
= KdpPollByte(&Byte
);
254 if (KdStatus
== KdPacketReceived
)
258 return KdPacketReceived
;
260 else if (Byte
== '$')
262 /* GDB tried to send a new packet. N-ack it. */
266 return KdPacketTimedOut
;