2009-10-22 14:58:33 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: GPL, see COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* FILE: drivers/base/kddll/kdcom.c
|
|
|
|
* PURPOSE: COM port functions for the kernel debugger.
|
2014-12-23 17:48:16 +00:00
|
|
|
* PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
|
2009-10-22 14:58:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kddll.h"
|
2014-01-27 22:23:24 +00:00
|
|
|
|
2012-11-28 23:23:54 +00:00
|
|
|
#include <cportlib/cportlib.h>
|
2014-01-27 22:23:24 +00:00
|
|
|
#include <arc/arc.h>
|
2015-12-20 16:21:59 +00:00
|
|
|
#include <stdio.h>
|
2013-04-23 00:41:45 +00:00
|
|
|
#include <stdlib.h>
|
2014-01-27 22:23:24 +00:00
|
|
|
#include <ndk/halfuncs.h>
|
2013-04-21 23:56:15 +00:00
|
|
|
|
2012-11-28 23:23:54 +00:00
|
|
|
/* Serial debug connection */
|
2020-04-21 20:22:42 +00:00
|
|
|
#if defined(SARCH_PC98)
|
2009-10-22 14:58:33 +00:00
|
|
|
#define DEFAULT_DEBUG_PORT 2 /* COM2 */
|
2020-04-21 20:22:42 +00:00
|
|
|
#define DEFAULT_DEBUG_COM1_IRQ 4
|
|
|
|
#define DEFAULT_DEBUG_COM2_IRQ 5
|
|
|
|
#define DEFAULT_DEBUG_BAUD_RATE 9600
|
|
|
|
#define DEFAULT_BAUD_RATE 9600
|
|
|
|
#else
|
|
|
|
#define DEFAULT_DEBUG_PORT 2 /* COM2 */
|
|
|
|
#define DEFAULT_DEBUG_COM1_IRQ 4
|
|
|
|
#define DEFAULT_DEBUG_COM2_IRQ 3
|
|
|
|
#define DEFAULT_DEBUG_BAUD_RATE 115200
|
|
|
|
#define DEFAULT_BAUD_RATE 19200
|
|
|
|
#endif
|
2009-10-22 14:58:33 +00:00
|
|
|
|
|
|
|
#if defined(_M_IX86) || defined(_M_AMD64)
|
2020-03-06 18:50:31 +00:00
|
|
|
#if defined(SARCH_PC98)
|
|
|
|
const ULONG BaseArray[] = {0, 0x30, 0x238};
|
|
|
|
#else
|
2012-11-28 23:23:54 +00:00
|
|
|
const ULONG BaseArray[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
|
2020-03-06 18:50:31 +00:00
|
|
|
#endif
|
2009-10-22 14:58:33 +00:00
|
|
|
#elif defined(_M_PPC)
|
2012-11-28 23:23:54 +00:00
|
|
|
const ULONG BaseArray[] = {0, 0x800003F8};
|
2009-10-22 14:58:33 +00:00
|
|
|
#elif defined(_M_MIPS)
|
2012-11-28 23:23:54 +00:00
|
|
|
const ULONG BaseArray[] = {0, 0x80006000, 0x80007000};
|
2009-10-22 14:58:33 +00:00
|
|
|
#elif defined(_M_ARM)
|
2012-11-28 23:23:54 +00:00
|
|
|
const ULONG BaseArray[] = {0, 0xF1012000};
|
2009-10-22 14:58:33 +00:00
|
|
|
#else
|
|
|
|
#error Unknown architecture
|
|
|
|
#endif
|
|
|
|
|
2015-12-20 16:21:59 +00:00
|
|
|
#define MAX_COM_PORTS (sizeof(BaseArray) / sizeof(BaseArray[0]) - 1)
|
|
|
|
|
2009-10-22 14:58:33 +00:00
|
|
|
/* GLOBALS ********************************************************************/
|
|
|
|
|
2015-12-20 16:21:59 +00:00
|
|
|
CPPORT KdComPort;
|
|
|
|
ULONG KdComPortIrq = 0; // Not used at the moment.
|
|
|
|
#ifdef KDDEBUG
|
2012-11-28 23:23:54 +00:00
|
|
|
CPPORT KdDebugComPort;
|
2015-12-20 16:21:59 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* DEBUGGING ******************************************************************/
|
|
|
|
|
|
|
|
#ifdef KDDEBUG
|
|
|
|
ULONG KdpDbgPrint(const char *Format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int Length;
|
|
|
|
char* ptr;
|
|
|
|
CHAR Buffer[512];
|
|
|
|
|
|
|
|
va_start(ap, Format);
|
|
|
|
Length = _vsnprintf(Buffer, sizeof(Buffer), Format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* Check if we went past the buffer */
|
|
|
|
if (Length == -1)
|
|
|
|
{
|
|
|
|
/* Terminate it if we went over-board */
|
|
|
|
Buffer[sizeof(Buffer) - 1] = '\n';
|
|
|
|
|
|
|
|
/* Put maximum */
|
|
|
|
Length = sizeof(Buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = Buffer;
|
|
|
|
while (Length--)
|
|
|
|
{
|
|
|
|
if (*ptr == '\n')
|
|
|
|
CpPutByte(&KdDebugComPort, '\r');
|
|
|
|
|
|
|
|
CpPutByte(&KdDebugComPort, *ptr++);
|
|
|
|
}
|
2009-10-22 14:58:33 +00:00
|
|
|
|
2015-12-20 16:21:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2009-10-22 14:58:33 +00:00
|
|
|
|
2012-11-28 23:23:54 +00:00
|
|
|
/* FUNCTIONS ******************************************************************/
|
|
|
|
|
2013-04-22 00:28:16 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
KdD0Transition(VOID)
|
|
|
|
{
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
KdD3Transition(VOID)
|
|
|
|
{
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
KdSave(IN BOOLEAN SleepTransition)
|
|
|
|
{
|
|
|
|
/* Nothing to do on COM ports */
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
KdRestore(IN BOOLEAN SleepTransition)
|
|
|
|
{
|
|
|
|
/* Nothing to do on COM ports */
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-10-22 14:58:33 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2012-11-28 23:23:54 +00:00
|
|
|
KdpPortInitialize(IN ULONG ComPortNumber,
|
|
|
|
IN ULONG ComPortBaudRate)
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
2012-11-28 23:23:54 +00:00
|
|
|
NTSTATUS Status;
|
2009-10-22 14:58:33 +00:00
|
|
|
|
2011-09-11 08:56:38 +00:00
|
|
|
KDDBGPRINT("KdpPortInitialize, Port = COM%ld\n", ComPortNumber);
|
2009-10-22 14:58:33 +00:00
|
|
|
|
2015-12-20 16:21:59 +00:00
|
|
|
Status = CpInitialize(&KdComPort,
|
2012-11-28 23:23:54 +00:00
|
|
|
UlongToPtr(BaseArray[ComPortNumber]),
|
|
|
|
ComPortBaudRate);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2012-11-28 23:23:54 +00:00
|
|
|
else
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
2015-12-20 16:21:59 +00:00
|
|
|
KdComPortInUse = KdComPort.Address;
|
2012-11-28 23:23:54 +00:00
|
|
|
return STATUS_SUCCESS;
|
2009-10-22 14:58:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* \name KdDebuggerInitialize0
|
|
|
|
* \brief Phase 0 initialization.
|
|
|
|
* \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
|
|
|
|
* \return Status
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2012-11-28 23:23:54 +00:00
|
|
|
KdDebuggerInitialize0(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL)
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
2012-11-28 23:23:54 +00:00
|
|
|
ULONG ComPortNumber = DEFAULT_DEBUG_PORT;
|
|
|
|
ULONG ComPortBaudRate = DEFAULT_DEBUG_BAUD_RATE;
|
|
|
|
|
2009-10-22 14:58:33 +00:00
|
|
|
PCHAR CommandLine, PortString, BaudString, IrqString;
|
|
|
|
ULONG Value;
|
|
|
|
|
2015-12-20 16:21:59 +00:00
|
|
|
/* Check if we have a LoaderBlock */
|
2009-10-22 14:58:33 +00:00
|
|
|
if (LoaderBlock)
|
|
|
|
{
|
|
|
|
/* Get the Command Line */
|
|
|
|
CommandLine = LoaderBlock->LoadOptions;
|
|
|
|
|
|
|
|
/* Upcase it */
|
|
|
|
_strupr(CommandLine);
|
|
|
|
|
|
|
|
/* Get the port and baud rate */
|
|
|
|
PortString = strstr(CommandLine, "DEBUGPORT");
|
|
|
|
BaudString = strstr(CommandLine, "BAUDRATE");
|
2012-11-28 23:23:54 +00:00
|
|
|
IrqString = strstr(CommandLine, "IRQ");
|
2009-10-22 14:58:33 +00:00
|
|
|
|
|
|
|
/* Check if we got the /DEBUGPORT parameter */
|
|
|
|
if (PortString)
|
|
|
|
{
|
|
|
|
/* Move past the actual string, to reach the port*/
|
|
|
|
PortString += strlen("DEBUGPORT");
|
|
|
|
|
|
|
|
/* Now get past any spaces and skip the equal sign */
|
|
|
|
while (*PortString == ' ') PortString++;
|
|
|
|
PortString++;
|
|
|
|
|
|
|
|
/* Do we have a serial port? */
|
|
|
|
if (strncmp(PortString, "COM", 3) != 0)
|
|
|
|
{
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2012-11-28 23:23:54 +00:00
|
|
|
/* Check for a valid Serial Port */
|
2009-10-22 14:58:33 +00:00
|
|
|
PortString += 3;
|
|
|
|
Value = atol(PortString);
|
2012-11-28 23:23:54 +00:00
|
|
|
if (Value >= sizeof(BaseArray) / sizeof(BaseArray[0]))
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the port to use */
|
|
|
|
ComPortNumber = Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we got a baud rate */
|
|
|
|
if (BaudString)
|
|
|
|
{
|
|
|
|
/* Move past the actual string, to reach the rate */
|
|
|
|
BaudString += strlen("BAUDRATE");
|
|
|
|
|
|
|
|
/* Now get past any spaces */
|
|
|
|
while (*BaudString == ' ') BaudString++;
|
|
|
|
|
|
|
|
/* And make sure we have a rate */
|
|
|
|
if (*BaudString)
|
|
|
|
{
|
|
|
|
/* Read and set it */
|
|
|
|
Value = atol(BaudString + 1);
|
|
|
|
if (Value) ComPortBaudRate = Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check Serial Port Settings [IRQ] */
|
|
|
|
if (IrqString)
|
|
|
|
{
|
|
|
|
/* Move past the actual string, to reach the rate */
|
|
|
|
IrqString += strlen("IRQ");
|
|
|
|
|
|
|
|
/* Now get past any spaces */
|
|
|
|
while (*IrqString == ' ') IrqString++;
|
|
|
|
|
|
|
|
/* And make sure we have an IRQ */
|
|
|
|
if (*IrqString)
|
|
|
|
{
|
|
|
|
/* Read and set it */
|
|
|
|
Value = atol(IrqString + 1);
|
2015-12-20 16:21:59 +00:00
|
|
|
if (Value) KdComPortIrq = Value;
|
2009-10-22 14:58:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-20 16:21:59 +00:00
|
|
|
#ifdef KDDEBUG
|
|
|
|
/*
|
|
|
|
* Try to find a free COM port and use it as the KD debugging port.
|
|
|
|
* NOTE: Inspired by reactos/boot/freeldr/freeldr/comm/rs232.c, Rs232PortInitialize(...)
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Start enumerating COM ports from the last one to the first one,
|
|
|
|
* and break when we find a valid port.
|
|
|
|
* If we reach the first element of the list, the invalid COM port,
|
|
|
|
* then it means that no valid port was found.
|
|
|
|
*/
|
|
|
|
ULONG ComPort;
|
|
|
|
for (ComPort = MAX_COM_PORTS; ComPort > 0; ComPort--)
|
|
|
|
{
|
|
|
|
/* Check if the port exist; skip the KD port */
|
|
|
|
if ((ComPort != ComPortNumber) && CpDoesPortExist(UlongToPtr(BaseArray[ComPort])))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ComPort != 0)
|
|
|
|
CpInitialize(&KdDebugComPort, UlongToPtr(BaseArray[ComPort]), DEFAULT_BAUD_RATE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
KDDBGPRINT("KdDebuggerInitialize0\n");
|
|
|
|
|
2009-10-22 14:58:33 +00:00
|
|
|
/* Initialize the port */
|
2012-11-28 23:23:54 +00:00
|
|
|
return KdpPortInitialize(ComPortNumber, ComPortBaudRate);
|
2009-10-22 14:58:33 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 00:28:16 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* \name KdDebuggerInitialize1
|
|
|
|
* \brief Phase 1 initialization.
|
|
|
|
* \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
|
|
|
|
* \return Status
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
KdDebuggerInitialize1(IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL)
|
|
|
|
{
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-22 14:58:33 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2013-04-21 23:56:15 +00:00
|
|
|
KdpSendByte(IN UCHAR Byte)
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
|
|
|
/* Send the byte */
|
2015-12-20 16:21:59 +00:00
|
|
|
CpPutByte(&KdComPort, Byte);
|
2009-10-22 14:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KDP_STATUS
|
|
|
|
NTAPI
|
2013-04-21 23:56:15 +00:00
|
|
|
KdpPollByte(OUT PUCHAR OutByte)
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
2015-12-20 16:21:59 +00:00
|
|
|
USHORT Status;
|
|
|
|
|
2013-04-21 23:23:30 +00:00
|
|
|
/* Poll the byte */
|
2015-12-20 16:21:59 +00:00
|
|
|
Status = CpGetByte(&KdComPort, OutByte, FALSE, FALSE);
|
|
|
|
switch (Status)
|
2013-04-23 00:48:49 +00:00
|
|
|
{
|
2015-12-20 16:21:59 +00:00
|
|
|
case CP_GET_SUCCESS:
|
|
|
|
return KDP_PACKET_RECEIVED;
|
|
|
|
|
|
|
|
case CP_GET_NODATA:
|
|
|
|
return KDP_PACKET_TIMEOUT;
|
|
|
|
|
|
|
|
case CP_GET_ERROR:
|
|
|
|
default:
|
|
|
|
return KDP_PACKET_RESEND;
|
2013-04-23 00:48:49 +00:00
|
|
|
}
|
2009-10-22 14:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KDP_STATUS
|
|
|
|
NTAPI
|
2013-04-21 23:56:15 +00:00
|
|
|
KdpReceiveByte(OUT PUCHAR OutByte)
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
2015-12-20 16:21:59 +00:00
|
|
|
USHORT Status;
|
|
|
|
|
2013-04-21 23:23:30 +00:00
|
|
|
/* Get the byte */
|
2015-12-20 16:21:59 +00:00
|
|
|
Status = CpGetByte(&KdComPort, OutByte, TRUE, FALSE);
|
|
|
|
switch (Status)
|
2013-04-23 00:48:49 +00:00
|
|
|
{
|
2015-12-20 16:21:59 +00:00
|
|
|
case CP_GET_SUCCESS:
|
|
|
|
return KDP_PACKET_RECEIVED;
|
|
|
|
|
|
|
|
case CP_GET_NODATA:
|
|
|
|
return KDP_PACKET_TIMEOUT;
|
|
|
|
|
|
|
|
case CP_GET_ERROR:
|
|
|
|
default:
|
|
|
|
return KDP_PACKET_RESEND;
|
2013-04-23 00:48:49 +00:00
|
|
|
}
|
2009-10-22 14:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KDP_STATUS
|
|
|
|
NTAPI
|
2012-11-28 23:23:54 +00:00
|
|
|
KdpPollBreakIn(VOID)
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
2013-04-21 23:23:30 +00:00
|
|
|
KDP_STATUS KdStatus;
|
2009-10-22 14:58:33 +00:00
|
|
|
UCHAR Byte;
|
2013-04-21 23:23:30 +00:00
|
|
|
|
|
|
|
KdStatus = KdpPollByte(&Byte);
|
|
|
|
if ((KdStatus == KDP_PACKET_RECEIVED) && (Byte == BREAKIN_PACKET_BYTE))
|
2009-10-22 14:58:33 +00:00
|
|
|
{
|
2013-04-21 23:23:30 +00:00
|
|
|
return KDP_PACKET_RECEIVED;
|
2009-10-22 14:58:33 +00:00
|
|
|
}
|
|
|
|
return KDP_PACKET_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2012-11-28 23:23:54 +00:00
|
|
|
/* EOF */
|