xref: /reactos/ntoskrnl/kd/i386/kdserial.c (revision 7f26a396)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS kernel
4  * FILE:            ntoskrnl/kd/i386/kdbg.c
5  * PURPOSE:         Serial i/o functions for the kernel debugger.
6  * PROGRAMMER:      Alex Ionescu
7  *                  Herv� Poussineau
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15 
16 #if defined(SARCH_PC98)
17 #define DEFAULT_BAUD_RATE   9600
18 #else
19 #define DEFAULT_BAUD_RATE   19200
20 #endif
21 
22 #if defined(_M_IX86) || defined(_M_AMD64)
23 #if defined(SARCH_PC98)
24 const ULONG BaseArray[] = {0, 0x30, 0x238};
25 #else
26 const ULONG BaseArray[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
27 #endif
28 #elif defined(_M_PPC)
29 const ULONG BaseArray[] = {0, 0x800003F8};
30 #elif defined(_M_MIPS)
31 const ULONG BaseArray[] = {0, 0x80006000, 0x80007000};
32 #elif defined(_M_ARM)
33 const ULONG BaseArray[] = {0, 0xF1012000};
34 #else
35 #error Unknown architecture
36 #endif
37 
38 #define MAX_COM_PORTS   (sizeof(BaseArray) / sizeof(BaseArray[0]) - 1)
39 
40 /* STATIC VARIABLES ***********************************************************/
41 
42 // static CPPORT DefaultPort = {0, 0, 0};
43 
44 /* The COM port must only be initialized once! */
45 // static BOOLEAN PortInitialized = FALSE;
46 
47 /* REACTOS FUNCTIONS **********************************************************/
48 
49 BOOLEAN
50 NTAPI
51 KdPortInitializeEx(
52     IN PCPPORT PortInformation,
53     IN ULONG ComPortNumber)
54 {
55     NTSTATUS Status;
56 
57 #if 0 // Deactivated because never used in fact (was in KdPortInitialize but we use KdPortInitializeEx)
58     /*
59      * Find the port if needed
60      */
61 
62     if (!PortInitialized)
63     {
64         DefaultPort.BaudRate = PortInformation->BaudRate;
65 
66         if (ComPortNumber == 0)
67         {
68             /*
69              * Start enumerating COM ports from the last one to the first one,
70              * and break when we find a valid port.
71              * If we reach the first element of the list, the invalid COM port,
72              * then it means that no valid port was found.
73              */
74             for (ComPortNumber = MAX_COM_PORTS; ComPortNumber > 0; ComPortNumber--)
75             {
76                 if (CpDoesPortExist(UlongToPtr(BaseArray[ComPortNumber])))
77                 {
78                     PortInformation->Address = DefaultPort.Address = BaseArray[ComPortNumber];
79                     break;
80                 }
81             }
82             if (ComPortNumber == 0)
83             {
84                 HalDisplayString("\r\nKernel Debugger: No COM port found!\r\n\r\n");
85                 return FALSE;
86             }
87         }
88 
89         PortInitialized = TRUE;
90     }
91 #endif
92 
93     /*
94      * Initialize the port
95      */
96     Status = CpInitialize(PortInformation,
97                           (ComPortNumber == 0 ? PortInformation->Address
98                                               : UlongToPtr(BaseArray[ComPortNumber])),
99                           (PortInformation->BaudRate == 0 ? DEFAULT_BAUD_RATE
100                                                           : PortInformation->BaudRate));
101     if (!NT_SUCCESS(Status))
102     {
103         HalDisplayString("\r\nKernel Debugger: Serial port not found!\r\n\r\n");
104         return FALSE;
105     }
106     else
107     {
108 #ifndef NDEBUG
109         int Length;
110         CHAR Buffer[82];
111 
112         /* Print message to blue screen */
113         Length = snprintf(Buffer, sizeof(Buffer),
114                           "\r\nKernel Debugger: Serial port found: COM%ld (Port 0x%p) BaudRate %ld\r\n\r\n",
115                           ComPortNumber,
116                           PortInformation->Address,
117                           PortInformation->BaudRate);
118         if (Length == -1)
119         {
120             /* Terminate it if we went over-board */
121             Buffer[sizeof(Buffer) - 1] = ANSI_NULL;
122         }
123 
124         HalDisplayString(Buffer);
125 #endif /* NDEBUG */
126 
127 #if 0
128         /* Set global info */
129         KdComPortInUse = DefaultPort.Address;
130 #endif
131         return TRUE;
132     }
133 }
134 
135 BOOLEAN
136 NTAPI
137 KdPortGetByteEx(
138     IN PCPPORT PortInformation,
139     OUT PUCHAR ByteReceived)
140 {
141     return (CpGetByte(PortInformation, ByteReceived, FALSE, FALSE) == CP_GET_SUCCESS);
142 }
143 
144 VOID
145 NTAPI
146 KdPortPutByteEx(
147     IN PCPPORT PortInformation,
148     IN UCHAR ByteToSend)
149 {
150     CpPutByte(PortInformation, ByteToSend);
151 }
152 
153 /* EOF */
154