2 * COPYRIGHT: GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/base/kddll/kdcom.c
5 * PURPOSE: COM port functions for the kernel debugger.
6 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@ewactos.org)
11 #include <cportlib/cportlib.h>
14 #include <ndk/halfuncs.h>
19 /* Serial debug connection */
20 #define DEFAULT_DEBUG_PORT 2 /* COM2 */
21 #define DEFAULT_DEBUG_COM1_IRQ 4 /* COM1 IRQ */
22 #define DEFAULT_DEBUG_COM2_IRQ 3 /* COM2 IRQ */
23 #define DEFAULT_DEBUG_BAUD_RATE 115200 /* 115200 Baud */
25 #define DEFAULT_BAUD_RATE 19200
27 #if defined(_M_IX86) || defined(_M_AMD64)
28 const ULONG BaseArray
[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
30 const ULONG BaseArray
[] = {0, 0x800003F8};
31 #elif defined(_M_MIPS)
32 const ULONG BaseArray
[] = {0, 0x80006000, 0x80007000};
34 const ULONG BaseArray
[] = {0, 0xF1012000};
36 #error Unknown architecture
39 /* GLOBALS ********************************************************************/
41 CPPORT KdDebugComPort
;
42 ULONG KdDebugComPortIrq
= 0; // Not used at the moment.
45 /* FUNCTIONS ******************************************************************/
51 return STATUS_SUCCESS
;
58 return STATUS_SUCCESS
;
63 KdSave(IN BOOLEAN SleepTransition
)
65 /* Nothing to do on COM ports */
66 return STATUS_SUCCESS
;
71 KdRestore(IN BOOLEAN SleepTransition
)
73 /* Nothing to do on COM ports */
74 return STATUS_SUCCESS
;
79 KdpPortInitialize(IN ULONG ComPortNumber
,
80 IN ULONG ComPortBaudRate
)
84 KDDBGPRINT("KdpPortInitialize, Port = COM%ld\n", ComPortNumber
);
86 Status
= CpInitialize(&KdDebugComPort
,
87 UlongToPtr(BaseArray
[ComPortNumber
]),
89 if (!NT_SUCCESS(Status
))
91 return STATUS_INVALID_PARAMETER
;
95 KdComPortInUse
= KdDebugComPort
.Address
;
96 return STATUS_SUCCESS
;
100 /******************************************************************************
101 * \name KdDebuggerInitialize0
102 * \brief Phase 0 initialization.
103 * \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
108 KdDebuggerInitialize0(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL
)
110 ULONG ComPortNumber
= DEFAULT_DEBUG_PORT
;
111 ULONG ComPortBaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
113 PCHAR CommandLine
, PortString
, BaudString
, IrqString
;
116 /* Check if e have a LoaderBlock */
120 KdpDbgPrint
= LoaderBlock
->u
.I386
.CommonDataArea
;
121 KDDBGPRINT("KdDebuggerInitialize0\n");
123 /* Get the Command Line */
124 CommandLine
= LoaderBlock
->LoadOptions
;
127 _strupr(CommandLine
);
129 /* Get the port and baud rate */
130 PortString
= strstr(CommandLine
, "DEBUGPORT");
131 BaudString
= strstr(CommandLine
, "BAUDRATE");
132 IrqString
= strstr(CommandLine
, "IRQ");
134 /* Check if we got the /DEBUGPORT parameter */
137 /* Move past the actual string, to reach the port*/
138 PortString
+= strlen("DEBUGPORT");
140 /* Now get past any spaces and skip the equal sign */
141 while (*PortString
== ' ') PortString
++;
144 /* Do we have a serial port? */
145 if (strncmp(PortString
, "COM", 3) != 0)
147 return STATUS_INVALID_PARAMETER
;
150 /* Check for a valid Serial Port */
152 Value
= atol(PortString
);
153 if (Value
>= sizeof(BaseArray
) / sizeof(BaseArray
[0]))
155 return STATUS_INVALID_PARAMETER
;
158 /* Set the port to use */
159 ComPortNumber
= Value
;
162 /* Check if we got a baud rate */
165 /* Move past the actual string, to reach the rate */
166 BaudString
+= strlen("BAUDRATE");
168 /* Now get past any spaces */
169 while (*BaudString
== ' ') BaudString
++;
171 /* And make sure we have a rate */
174 /* Read and set it */
175 Value
= atol(BaudString
+ 1);
176 if (Value
) ComPortBaudRate
= Value
;
180 /* Check Serial Port Settings [IRQ] */
183 /* Move past the actual string, to reach the rate */
184 IrqString
+= strlen("IRQ");
186 /* Now get past any spaces */
187 while (*IrqString
== ' ') IrqString
++;
189 /* And make sure we have an IRQ */
192 /* Read and set it */
193 Value
= atol(IrqString
+ 1);
194 if (Value
) KdDebugComPortIrq
= Value
;
199 /* Initialize the port */
200 return KdpPortInitialize(ComPortNumber
, ComPortBaudRate
);
203 /******************************************************************************
204 * \name KdDebuggerInitialize1
205 * \brief Phase 1 initialization.
206 * \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
211 KdDebuggerInitialize1(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL
)
213 return STATUS_SUCCESS
;
219 KdpSendByte(IN UCHAR Byte
)
222 CpPutByte(&KdDebugComPort
, Byte
);
227 KdpPollByte(OUT PUCHAR OutByte
)
230 if (CpGetByte(&KdDebugComPort
, OutByte
, FALSE
, FALSE
) == CP_GET_SUCCESS
)
232 return KDP_PACKET_RECEIVED
;
236 return KDP_PACKET_TIMEOUT
;
242 KdpReceiveByte(OUT PUCHAR OutByte
)
245 if (CpGetByte(&KdDebugComPort
, OutByte
, TRUE
, FALSE
) == CP_GET_SUCCESS
)
247 return KDP_PACKET_RECEIVED
;
251 return KDP_PACKET_TIMEOUT
;
262 KdStatus
= KdpPollByte(&Byte
);
263 if ((KdStatus
== KDP_PACKET_RECEIVED
) && (Byte
== BREAKIN_PACKET_BYTE
))
265 return KDP_PACKET_RECEIVED
;
267 return KDP_PACKET_TIMEOUT
;