[KDGDB]
[reactos.git] / reactos / drivers / base / kdgdb / gdb_receive.c
1 /*
2 * COPYRIGHT: GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/base/kddll/gdb_receive.c
5 * PURPOSE: Base functions for the kernel debugger.
6 */
7
8 #include "kdgdb.h"
9
10 /* GLOBALS ********************************************************************/
11 CHAR gdb_input[0x1000];
12
13 /* GLOBAL FUNCTIONS ***********************************************************/
14 char
15 hex_value(char ch)
16 {
17 if ((ch >= '0') && (ch <= '9'))
18 return (ch - '0');
19
20 if ((ch >= 'a') && (ch <= 'f'))
21 return (ch - 'a' + 10);
22
23 if ((ch >= 'A') && (ch <= 'F'))
24 return (ch - 'A' + 10);
25
26 return -1;
27 }
28
29 KDSTATUS
30 NTAPI
31 gdb_receive_packet(_Inout_ PKD_CONTEXT KdContext)
32 {
33 char* ByteBuffer = gdb_input;
34 UCHAR Byte;
35 KDSTATUS Status;
36 CHAR CheckSum = 0, ReceivedCheckSum;
37
38 do
39 {
40 Status = KdpReceiveByte(&Byte);
41 if (Status != KdPacketReceived)
42 return Status;
43 if (Byte == 0x03)
44 {
45 KdContext->KdpControlCPending = TRUE;
46 return KdPacketNeedsResend;
47 }
48 } while (Byte != '$');
49
50 while (TRUE)
51 {
52 /* Try to get a byte from the port */
53 Status = KdpReceiveByte(&Byte);
54 if (Status != KdPacketReceived)
55 return Status;
56
57 if (Byte == '#')
58 {
59 *ByteBuffer = '\0';
60 break;
61 }
62
63 *ByteBuffer++ = Byte;
64 CheckSum += (CHAR)Byte;
65 }
66
67 /* Get Check sum (two bytes) */
68 Status = KdpReceiveByte(&Byte);
69 if (Status != KdPacketReceived)
70 goto end;
71 ReceivedCheckSum = hex_value(Byte) << 4;;
72
73 Status = KdpReceiveByte(&Byte);
74 if (Status != KdPacketReceived)
75 goto end;
76 ReceivedCheckSum += hex_value(Byte);
77
78 end:
79 if (ReceivedCheckSum != CheckSum)
80 {
81 /* Do not acknowledge to GDB */
82 KdpSendByte('-');
83 return KdPacketNeedsResend;
84 }
85
86 /* Acknowledge */
87 KdpSendByte('+');
88
89 return KdPacketReceived;
90 }