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>
16 /* Serial debug connection */
17 #define DEFAULT_DEBUG_PORT 2 /* COM2 */
18 #define DEFAULT_DEBUG_COM1_IRQ 4 /* COM1 IRQ */
19 #define DEFAULT_DEBUG_COM2_IRQ 3 /* COM2 IRQ */
20 #define DEFAULT_DEBUG_BAUD_RATE 115200 /* 115200 Baud */
22 #define DEFAULT_BAUD_RATE 19200
24 #if defined(_M_IX86) || defined(_M_AMD64)
25 const ULONG BaseArray
[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
27 const ULONG BaseArray
[] = {0, 0x800003F8};
28 #elif defined(_M_MIPS)
29 const ULONG BaseArray
[] = {0, 0x80006000, 0x80007000};
31 const ULONG BaseArray
[] = {0, 0xF1012000};
33 #error Unknown architecture
36 /* GLOBALS ********************************************************************/
38 CPPORT KdDebugComPort
;
39 ULONG KdDebugComPortIrq
= 0; // Not used at the moment.
42 /* FUNCTIONS ******************************************************************/
48 return STATUS_SUCCESS
;
55 return STATUS_SUCCESS
;
60 KdSave(IN BOOLEAN SleepTransition
)
62 /* Nothing to do on COM ports */
63 return STATUS_SUCCESS
;
68 KdRestore(IN BOOLEAN SleepTransition
)
70 /* Nothing to do on COM ports */
71 return STATUS_SUCCESS
;
76 KdpPortInitialize(IN ULONG ComPortNumber
,
77 IN ULONG ComPortBaudRate
)
81 KDDBGPRINT("KdpPortInitialize, Port = COM%ld\n", ComPortNumber
);
83 Status
= CpInitialize(&KdDebugComPort
,
84 UlongToPtr(BaseArray
[ComPortNumber
]),
86 if (!NT_SUCCESS(Status
))
88 return STATUS_INVALID_PARAMETER
;
92 KdComPortInUse
= KdDebugComPort
.Address
;
93 return STATUS_SUCCESS
;
97 /******************************************************************************
98 * \name KdDebuggerInitialize0
99 * \brief Phase 0 initialization.
100 * \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
105 KdDebuggerInitialize0(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL
)
107 ULONG ComPortNumber
= DEFAULT_DEBUG_PORT
;
108 ULONG ComPortBaudRate
= DEFAULT_DEBUG_BAUD_RATE
;
110 PCHAR CommandLine
, PortString
, BaudString
, IrqString
;
113 /* Check if e have a LoaderBlock */
117 KdpDbgPrint
= LoaderBlock
->u
.I386
.CommonDataArea
;
118 KDDBGPRINT("KdDebuggerInitialize0\n");
120 /* Get the Command Line */
121 CommandLine
= LoaderBlock
->LoadOptions
;
124 _strupr(CommandLine
);
126 /* Get the port and baud rate */
127 PortString
= strstr(CommandLine
, "DEBUGPORT");
128 BaudString
= strstr(CommandLine
, "BAUDRATE");
129 IrqString
= strstr(CommandLine
, "IRQ");
131 /* Check if we got the /DEBUGPORT parameter */
134 /* Move past the actual string, to reach the port*/
135 PortString
+= strlen("DEBUGPORT");
137 /* Now get past any spaces and skip the equal sign */
138 while (*PortString
== ' ') PortString
++;
141 /* Do we have a serial port? */
142 if (strncmp(PortString
, "COM", 3) != 0)
144 return STATUS_INVALID_PARAMETER
;
147 /* Check for a valid Serial Port */
149 Value
= atol(PortString
);
150 if (Value
>= sizeof(BaseArray
) / sizeof(BaseArray
[0]))
152 return STATUS_INVALID_PARAMETER
;
155 /* Set the port to use */
156 ComPortNumber
= Value
;
159 /* Check if we got a baud rate */
162 /* Move past the actual string, to reach the rate */
163 BaudString
+= strlen("BAUDRATE");
165 /* Now get past any spaces */
166 while (*BaudString
== ' ') BaudString
++;
168 /* And make sure we have a rate */
171 /* Read and set it */
172 Value
= atol(BaudString
+ 1);
173 if (Value
) ComPortBaudRate
= Value
;
177 /* Check Serial Port Settings [IRQ] */
180 /* Move past the actual string, to reach the rate */
181 IrqString
+= strlen("IRQ");
183 /* Now get past any spaces */
184 while (*IrqString
== ' ') IrqString
++;
186 /* And make sure we have an IRQ */
189 /* Read and set it */
190 Value
= atol(IrqString
+ 1);
191 if (Value
) KdDebugComPortIrq
= Value
;
196 /* Initialize the port */
197 return KdpPortInitialize(ComPortNumber
, ComPortBaudRate
);
200 /******************************************************************************
201 * \name KdDebuggerInitialize1
202 * \brief Phase 1 initialization.
203 * \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
208 KdDebuggerInitialize1(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL
)
210 return STATUS_SUCCESS
;
216 KdpSendByte(IN UCHAR Byte
)
219 CpPutByte(&KdDebugComPort
, Byte
);
224 KdpPollByte(OUT PUCHAR OutByte
)
227 if (CpGetByte(&KdDebugComPort
, OutByte
, FALSE
, FALSE
) == CP_GET_SUCCESS
)
229 return KDP_PACKET_RECEIVED
;
233 return KDP_PACKET_TIMEOUT
;
239 KdpReceiveByte(OUT PUCHAR OutByte
)
242 if (CpGetByte(&KdDebugComPort
, OutByte
, TRUE
, FALSE
) == CP_GET_SUCCESS
)
244 return KDP_PACKET_RECEIVED
;
248 return KDP_PACKET_TIMEOUT
;
259 KdStatus
= KdpPollByte(&Byte
);
260 if ((KdStatus
== KDP_PACKET_RECEIVED
) && (Byte
== BREAKIN_PACKET_BYTE
))
262 return KDP_PACKET_RECEIVED
;
264 return KDP_PACKET_TIMEOUT
;