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)
10 #include <cportlib/cportlib.h>
14 /* Serial debug connection */
15 #define DEFAULT_DEBUG_PORT 2 /* COM2 */
16 #define DEFAULT_DEBUG_COM1_IRQ 4 /* COM1 IRQ */
17 #define DEFAULT_DEBUG_COM2_IRQ 3 /* COM2 IRQ */
18 #define DEFAULT_DEBUG_BAUD_RATE 115200 /* 115200 Baud */
20 #define DEFAULT_BAUD_RATE 19200
22 #if defined(_M_IX86) || defined(_M_AMD64)
23 const ULONG BaseArray
[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
25 const ULONG BaseArray
[] = {0, 0x800003F8};
26 #elif defined(_M_MIPS)
27 const ULONG BaseArray
[] = {0, 0x80006000, 0x80007000};
29 const ULONG BaseArray
[] = {0, 0xF1012000};
31 #error Unknown architecture
34 /* GLOBALS ********************************************************************/
36 CPPORT KdDebugComPort
;
37 ULONG KdDebugComPortIrq
= 0; // Not used at the moment.
40 /* FUNCTIONS ******************************************************************/
46 return STATUS_SUCCESS
;
53 return STATUS_SUCCESS
;
58 KdSave(IN BOOLEAN SleepTransition
)
60 /* Nothing to do on COM ports */
61 return STATUS_SUCCESS
;
66 KdRestore(IN BOOLEAN SleepTransition
)
68 /* Nothing to do on COM ports */
69 return STATUS_SUCCESS
;
74 KdpPortInitialize(IN ULONG ComPortNumber
,
75 IN ULONG ComPortBaudRate
)
79 KDDBGPRINT("KdpPortInitialize, Port = COM%ld\n", ComPortNumber
);
81 Status
= CpInitialize(&KdDebugComPort
,
82 UlongToPtr(BaseArray
[ComPortNumber
]),
84 if (!NT_SUCCESS(Status
))
86 return STATUS_INVALID_PARAMETER
;
90 KdComPortInUse
= KdDebugComPort
.Address
;
91 return STATUS_SUCCESS
;
95 /******************************************************************************
96 * \name KdDebuggerInitialize0
97 * \brief Phase 0 initialization.
98 * \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
103 KdDebuggerInitialize0(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL
)
105 ULONG ComPortNumber
= DEFAULT_DEBUG_PORT
;
106 ULONG ComPortBaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
108 PCHAR CommandLine
, PortString
, BaudString
, IrqString
;
111 /* Check if e have a LoaderBlock */
115 KdpDbgPrint
= LoaderBlock
->u
.I386
.CommonDataArea
;
116 KDDBGPRINT("KdDebuggerInitialize0\n");
118 /* Get the Command Line */
119 CommandLine
= LoaderBlock
->LoadOptions
;
122 _strupr(CommandLine
);
124 /* Get the port and baud rate */
125 PortString
= strstr(CommandLine
, "DEBUGPORT");
126 BaudString
= strstr(CommandLine
, "BAUDRATE");
127 IrqString
= strstr(CommandLine
, "IRQ");
129 /* Check if we got the /DEBUGPORT parameter */
132 /* Move past the actual string, to reach the port*/
133 PortString
+= strlen("DEBUGPORT");
135 /* Now get past any spaces and skip the equal sign */
136 while (*PortString
== ' ') PortString
++;
139 /* Do we have a serial port? */
140 if (strncmp(PortString
, "COM", 3) != 0)
142 return STATUS_INVALID_PARAMETER
;
145 /* Check for a valid Serial Port */
147 Value
= atol(PortString
);
148 if (Value
>= sizeof(BaseArray
) / sizeof(BaseArray
[0]))
150 return STATUS_INVALID_PARAMETER
;
153 /* Set the port to use */
154 ComPortNumber
= Value
;
157 /* Check if we got a baud rate */
160 /* Move past the actual string, to reach the rate */
161 BaudString
+= strlen("BAUDRATE");
163 /* Now get past any spaces */
164 while (*BaudString
== ' ') BaudString
++;
166 /* And make sure we have a rate */
169 /* Read and set it */
170 Value
= atol(BaudString
+ 1);
171 if (Value
) ComPortBaudRate
= Value
;
175 /* Check Serial Port Settings [IRQ] */
178 /* Move past the actual string, to reach the rate */
179 IrqString
+= strlen("IRQ");
181 /* Now get past any spaces */
182 while (*IrqString
== ' ') IrqString
++;
184 /* And make sure we have an IRQ */
187 /* Read and set it */
188 Value
= atol(IrqString
+ 1);
189 if (Value
) KdDebugComPortIrq
= Value
;
194 /* Initialize the port */
195 return KdpPortInitialize(ComPortNumber
, ComPortBaudRate
);
198 /******************************************************************************
199 * \name KdDebuggerInitialize1
200 * \brief Phase 1 initialization.
201 * \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
206 KdDebuggerInitialize1(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL
)
208 return STATUS_SUCCESS
;
214 KdpSendByte(IN UCHAR Byte
)
217 CpPutByte(&KdDebugComPort
, Byte
);
222 KdpPollByte(OUT PUCHAR OutByte
)
225 if (CpGetByte(&KdDebugComPort
, OutByte
, FALSE
) == CP_GET_SUCCESS
)
227 return KDP_PACKET_RECEIVED
;
231 return KDP_PACKET_TIMEOUT
;
237 KdpReceiveByte(OUT PUCHAR OutByte
)
240 if (CpGetByte(&KdDebugComPort
, OutByte
, TRUE
) == CP_GET_SUCCESS
)
242 return KDP_PACKET_RECEIVED
;
246 return KDP_PACKET_TIMEOUT
;
257 KdStatus
= KdpPollByte(&Byte
);
258 if ((KdStatus
== KDP_PACKET_RECEIVED
) && (Byte
== BREAKIN_PACKET_BYTE
))
260 return KDP_PACKET_RECEIVED
;
262 return KDP_PACKET_TIMEOUT
;