Sync kdcom from amd64 branch
authorTimo Kreuzer <timo.kreuzer@reactos.org>
Tue, 27 Oct 2009 23:38:56 +0000 (23:38 +0000)
committerTimo Kreuzer <timo.kreuzer@reactos.org>
Tue, 27 Oct 2009 23:38:56 +0000 (23:38 +0000)
svn path=/trunk/; revision=43801

reactos/drivers/base/kddll/kdcom.c
reactos/drivers/base/kddll/kddll.c
reactos/drivers/base/kddll/kddll.rbuild
reactos/drivers/base/kddll/kdserial.c

index 409b88e..22d806c 100644 (file)
@@ -216,6 +216,11 @@ KdpSendByte(IN BYTE Byte)
     /* Wait for the port to be ready */
     while ((READ_PORT_UCHAR(ComPortBase + COM_LSR) & LSR_TBE) == 0);
 
+    /* This is needed due to subtle timing issues */
+    READ_PORT_UCHAR(ComPortBase + COM_MSR);
+    while ((READ_PORT_UCHAR(ComPortBase + COM_LSR) & LSR_TBE) == 0);
+    READ_PORT_UCHAR(ComPortBase + COM_MSR);
+
     /* Send the byte */
     WRITE_PORT_UCHAR(ComPortBase + COM_DAT, Byte);
 }
@@ -224,6 +229,8 @@ KDP_STATUS
 NTAPI
 KdpPollByte(OUT PBYTE OutByte)
 {
+    READ_PORT_UCHAR(ComPortBase + COM_MSR); // Timing
+
     /* Check if data is available */
     if ((READ_PORT_UCHAR(ComPortBase + COM_LSR) & LSR_DR))
     {
index b86c905..43aca1d 100644 (file)
@@ -13,6 +13,7 @@
 
 PFNDBGPRNT KdpDbgPrint = NULL;
 ULONG CurrentPacketId = INITIAL_PACKET_ID | SYNC_PACKET_ID;
+ULONG RemotePacketId = 0;
 
 
 /* PRIVATE FUNCTIONS **********************************************************/
@@ -148,24 +149,31 @@ KdReceivePacket(
         KdStatus = KdpReceiveBuffer(&Packet.PacketType, sizeof(USHORT));
         if (KdStatus != KDP_PACKET_RECEIVED)
         {
-            /* Didn't receive a PacketType or PacketType is bad. Start over. */
-            continue;
+            /* Didn't receive a PacketType. */
+            return KdStatus;
+        }
+
+        /* Check if we got a resend packet */
+        if (Packet.PacketLeader == CONTROL_PACKET_LEADER &&
+            Packet.PacketType == PACKET_TYPE_KD_RESEND)
+        {
+            return KDP_PACKET_RESEND;
         }
 
         /* Step 3 - Read ByteCount */
         KdStatus = KdpReceiveBuffer(&Packet.ByteCount, sizeof(USHORT));
-        if (KdStatus != KDP_PACKET_RECEIVED || Packet.ByteCount > PACKET_MAX_SIZE)
+        if (KdStatus != KDP_PACKET_RECEIVED)
         {
-            /* Didn't receive ByteCount or it's too big. Start over. */
-            continue;
+            /* Didn't receive ByteCount. */
+            return KdStatus;
         }
 
         /* Step 4 - Read PacketId */
         KdStatus = KdpReceiveBuffer(&Packet.PacketId, sizeof(ULONG));
         if (KdStatus != KDP_PACKET_RECEIVED)
         {
-            /* Didn't receive PacketId. Start over. */
-            continue;
+            /* Didn't receive PacketId. */
+            return KdStatus;
         }
 
 /*
@@ -180,8 +188,8 @@ KdReceivePacket(
         KdStatus = KdpReceiveBuffer(&Packet.Checksum, sizeof(ULONG));
         if (KdStatus != KDP_PACKET_RECEIVED)
         {
-            /* Didn't receive Checksum. Start over. */
-            continue;
+            /* Didn't receive Checksum. */
+            return KdStatus;
         }
 
         /* Step 6 - Handle control packets */
@@ -205,6 +213,7 @@ KdReceivePacket(
                     KDDBGPRINT("KdReceivePacket - got a reset packet\n");
                     KdpSendControlPacket(PACKET_TYPE_KD_RESET, 0);
                     CurrentPacketId = INITIAL_PACKET_ID;
+                    RemotePacketId = INITIAL_PACKET_ID;
                     /* Fall through */
 
                 case PACKET_TYPE_KD_RESEND:
@@ -227,43 +236,18 @@ KdReceivePacket(
             return KDP_PACKET_RECEIVED;
         }
 
-        /* Did we get the right packet type? */
-        if (PacketType != Packet.PacketType)
-        {
-            /* We received something different, start over */
-            KDDBGPRINT("KdReceivePacket - wrong PacketType\n");
-            KdpSendControlPacket(PACKET_TYPE_KD_RESEND, 0);
-            continue;
-        }
-
         /* Get size of the message header */
-        switch (Packet.PacketType)
-        {
-            case PACKET_TYPE_KD_STATE_CHANGE64:
-                MessageHeader->Length = sizeof(DBGKD_WAIT_STATE_CHANGE64);
-                break;
-
-            case PACKET_TYPE_KD_STATE_MANIPULATE:
-                MessageHeader->Length = sizeof(DBGKD_MANIPULATE_STATE64);
-                break;
-
-            case PACKET_TYPE_KD_DEBUG_IO:
-                MessageHeader->Length = sizeof(DBGKD_DEBUG_IO);
-                break;
-
-            default:
-                KDDBGPRINT("KdReceivePacket - unknown PacketType\n");
-                return KDP_PACKET_RESEND;
-        }
-
-        //KDDBGPRINT("KdReceivePacket - got normal PacketType\n");
+        MessageHeader->Length = MessageHeader->MaximumLength;
 
-        /* Packet smaller than expected? */
-        if (MessageHeader->Length > Packet.ByteCount)
+        /* Packet smaller than expected or too big? */
+        if (Packet.ByteCount < MessageHeader->Length ||
+            Packet.ByteCount > PACKET_MAX_SIZE)
         {
             KDDBGPRINT("KdReceivePacket - too few data (%d) for type %d\n",
                           Packet.ByteCount, MessageHeader->Length);
             MessageHeader->Length = Packet.ByteCount;
+            KdpSendControlPacket(PACKET_TYPE_KD_RESEND, 0);
+            continue;
         }
 
         //KDDBGPRINT("KdReceivePacket - got normal PacketType, Buffer = %p\n", MessageHeader->Buffer);
@@ -316,20 +300,20 @@ KdReceivePacket(
             }
         }
 
-        /* Compare checksum */
-        if (Packet.Checksum != Checksum)
+        /* We must receive a PACKET_TRAILING_BYTE now */
+        KdStatus = KdpReceiveBuffer(&Byte, sizeof(UCHAR));
+        if (KdStatus != KDP_PACKET_RECEIVED || Byte != PACKET_TRAILING_BYTE)
         {
-            KDDBGPRINT("KdReceivePacket - wrong cheksum, got %x, calculated %x\n",
-                          Packet.Checksum, Checksum);
+            KDDBGPRINT("KdReceivePacket - wrong trailing byte (0x%x), status 0x%x\n", Byte, KdStatus);
             KdpSendControlPacket(PACKET_TYPE_KD_RESEND, 0);
             continue;
         }
 
-        /* We must receive a PACKET_TRAILING_BYTE now */
-        KdStatus = KdpReceiveBuffer(&Byte, sizeof(UCHAR));
-        if (KdStatus != KDP_PACKET_RECEIVED || Byte != PACKET_TRAILING_BYTE)
+        /* Compare checksum */
+        if (Packet.Checksum != Checksum)
         {
-            KDDBGPRINT("KdReceivePacket - wrong trailing byte (0x%x), status 0x%x\n", Byte, KdStatus);
+            KDDBGPRINT("KdReceivePacket - wrong cheksum, got %x, calculated %x\n",
+                          Packet.Checksum, Checksum);
             KdpSendControlPacket(PACKET_TYPE_KD_RESEND, 0);
             continue;
         }
@@ -337,9 +321,24 @@ KdReceivePacket(
         /* Acknowledge the received packet */
         KdpSendControlPacket(PACKET_TYPE_KD_ACKNOWLEDGE, Packet.PacketId);
 
-        //KDDBGPRINT("KdReceivePacket - all ok\n");
+        /* Check if the received PacketId is ok */
+        if (Packet.PacketId != RemotePacketId)
+        {
+            /* Continue with next packet */
+            continue;
+        }
+
+        /* Did we get the right packet type? */
+        if (PacketType == Packet.PacketType)
+        {
+            /* Yes, return success */
+            //KDDBGPRINT("KdReceivePacket - all ok\n");
+            RemotePacketId ^= 1;
+            return KDP_PACKET_RECEIVED;
+        }
 
-        return KDP_PACKET_RECEIVED;
+        /* We received something different, ignore it. */
+        KDDBGPRINT("KdReceivePacket - wrong PacketType\n");
     }
 
     return KDP_PACKET_RECEIVED;
@@ -352,10 +351,11 @@ KdSendPacket(
     IN ULONG PacketType,
     IN PSTRING MessageHeader,
     IN PSTRING MessageData,
-    IN OUT PKD_CONTEXT Context)
+    IN OUT PKD_CONTEXT KdContext)
 {
     KD_PACKET Packet;
     KDP_STATUS KdStatus;
+    ULONG Retries;
 
     /* Initialize a KD_PACKET */
     Packet.PacketLeader = PACKET_LEADER;
@@ -372,7 +372,9 @@ KdSendPacket(
                                                 MessageData->Length);
     }
 
-    for (;;)
+    Retries = KdContext->KdpDefaultRetries;
+
+    do
     {
         /* Set the packet id */
         Packet.PacketId = CurrentPacketId;
@@ -394,10 +396,10 @@ KdSendPacket(
 
         /* Wait for acknowledge */
         KdStatus = KdReceivePacket(PACKET_TYPE_KD_ACKNOWLEDGE,
-                                  NULL,
-                                  NULL,
-                                  0,
-                                  NULL);
+                                   NULL,
+                                   NULL,
+                                   0,
+                                   KdContext);
 
         /* Did we succeed? */
         if (KdStatus == KDP_PACKET_RECEIVED)
@@ -413,9 +415,14 @@ KdSendPacket(
             return;
         }
 
+        if (KdStatus == KDP_PACKET_TIMEOUT)
+        {
+            Retries--;
+        }
+
         /* Packet timed out, send it again */
+        KDDBGPRINT("KdSendPacket got KdStatus 0x%x\n", KdStatus);
     }
-
-    return;
+    while (Retries > 0);
 }
 
index 4e243be..3fca485 100644 (file)
@@ -10,6 +10,7 @@
 
 <module name="kdserial" type="staticlibrary">
        <include base="kdserial">.</include>
+       <library>ntoskrnl</library>
        <file>kdserial.c</file>
 </module>
 
index 6964ab9..14a3c4e 100644 (file)
@@ -124,7 +124,7 @@ KdpReceivePacketLeader(
             /* Check for breakin byte */
             if (Byte == BREAKIN_PACKET_BYTE)
             {
-                KdpDbgPrint("BREAKIN_PACKET_BYTE\n");
+                KDDBGPRINT("BREAKIN_PACKET_BYTE\n");
                 Index = 0;
                 Buffer[0] = Byte;
                 continue;
@@ -138,7 +138,7 @@ KdpReceivePacketLeader(
     while (Index < 4);
 
     /* Enable the debugger */
-//    KdDebuggerNotPresent = FALSE;
+    KdDebuggerNotPresent = FALSE;
     SharedUserData->KdDebuggerEnabled |= 0x00000002;
 
     /* Return the received packet leader */