]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePciExpressLib/PciLib.c
1. Fixed a logical error in PciReadBuffer() and PciWriteBuffer()
[mirror_edk2.git] / MdePkg / Library / BasePciExpressLib / PciLib.c
index acd31034835f90d56efc381afbb92889a3766c0f..771c18f811d106ac0bd47a1a4b79cac238fe81b2 100644 (file)
@@ -1192,18 +1192,21 @@ PciExpressReadBuffer (
   OUT     VOID                      *Buffer\r
   )\r
 {\r
-  UINTN                             EndAddress;\r
+  UINTN                             ReturnValue;\r
 \r
   ASSERT_INVALID_PCI_ADDRESS (StartAddress);\r
   ASSERT (((StartAddress & 0xFFF) + Size) <= 0x1000);\r
 \r
   if (Size == 0) {\r
-    return 0;\r
+    return Size;\r
   }\r
 \r
   ASSERT (Buffer != NULL);\r
 \r
-  EndAddress = StartAddress + Size;\r
+  //\r
+  // Save Size for return\r
+  //\r
+  ReturnValue = Size;\r
 \r
   if ((StartAddress & 1) != 0) {\r
     //\r
@@ -1211,44 +1214,48 @@ PciExpressReadBuffer (
     //\r
     *(UINT8*)Buffer = PciExpressRead8 (StartAddress);\r
     StartAddress += sizeof (UINT8);\r
+    Size -= sizeof (UINT8);\r
     Buffer = (UINT8*)Buffer + 1;\r
   }\r
 \r
-  if ((StartAddress < EndAddress) && ((StartAddress & 2) != 0)) {\r
+  if (Size >= sizeof (UINT16) && (StartAddress & 2) != 0) {\r
     //\r
     // Read a word if StartAddress is word aligned\r
     //\r
     *(UINT16*)Buffer = PciExpressRead16 (StartAddress);\r
     StartAddress += sizeof (UINT16);\r
+    Size -= sizeof (UINT16);\r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
-  while ((EndAddress - StartAddress) >= 4) {\r
+  while (Size >= sizeof (UINT32)) {\r
     //\r
     // Read as many double words as possible\r
     //\r
     *(UINT32*)Buffer = PciExpressRead32 (StartAddress);\r
     StartAddress += sizeof (UINT32);\r
+    Size -= sizeof (UINT32);\r
     Buffer = (UINT32*)Buffer + 1;\r
   }\r
 \r
-  if ((EndAddress & 2) != 0) {\r
+  if (Size >= sizeof (UINT16)) {\r
     //\r
     // Read the last remaining word if exist\r
     //\r
     *(UINT16*)Buffer = PciExpressRead16 (StartAddress);\r
     StartAddress += sizeof (UINT16);\r
+    Size -= sizeof (UINT16);\r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
-  if ((EndAddress & 1) != 0) {\r
+  if (Size >= sizeof (UINT8)) {\r
     //\r
     // Read the last remaining byte if exist\r
     //\r
     *(UINT8*)Buffer = PciExpressRead8 (StartAddress);\r
   }\r
 \r
-  return Size;\r
+  return ReturnValue;\r
 }\r
 \r
 /**\r
@@ -1283,7 +1290,7 @@ PciExpressWriteBuffer (
   IN      VOID                      *Buffer\r
   )\r
 {\r
-  UINTN                             EndAddress;\r
+  UINTN                             ReturnValue;\r
 \r
   ASSERT_INVALID_PCI_ADDRESS (StartAddress);\r
   ASSERT (((StartAddress & 0xFFF) + Size) <= 0x1000);\r
@@ -1294,7 +1301,10 @@ PciExpressWriteBuffer (
 \r
   ASSERT (Buffer != NULL);\r
 \r
-  EndAddress = StartAddress + Size;\r
+  //\r
+  // Save Size for return\r
+  //\r
+  ReturnValue = Size;\r
 \r
   if ((StartAddress & 1) != 0) {\r
     //\r
@@ -1302,42 +1312,46 @@ PciExpressWriteBuffer (
     //\r
     PciExpressWrite8 (StartAddress, *(UINT8*)Buffer);\r
     StartAddress += sizeof (UINT8);\r
+    Size -= sizeof (UINT8);\r
     Buffer = (UINT8*)Buffer + 1;\r
   }\r
 \r
-  if ((StartAddress < EndAddress) && ((StartAddress & 2) != 0)) {\r
+  if (Size >= sizeof (UINT16) && (StartAddress & 2) != 0) {\r
     //\r
     // Write a word if StartAddress is word aligned\r
     //\r
     PciExpressWrite16 (StartAddress, *(UINT16*)Buffer);\r
     StartAddress += sizeof (UINT16);\r
+    Size -= sizeof (UINT16);\r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
-  while ((EndAddress - StartAddress) >= 4) {\r
+  while (Size >= sizeof (UINT32)) {\r
     //\r
     // Write as many double words as possible\r
     //\r
     PciExpressWrite32 (StartAddress, *(UINT32*)Buffer);\r
     StartAddress += sizeof (UINT32);\r
+    Size -= sizeof (UINT32);\r
     Buffer = (UINT32*)Buffer + 1;\r
   }\r
 \r
-  if ((EndAddress & 2) != 0) {\r
+  if (Size >= sizeof (UINT16)) {\r
     //\r
     // Write the last remaining word if exist\r
     //\r
     PciExpressWrite16 (StartAddress, *(UINT16*)Buffer);\r
     StartAddress += sizeof (UINT16);\r
+    Size -= sizeof (UINT16);\r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
-  if ((EndAddress & 1) != 0) {\r
+  if (Size >= sizeof (UINT8)) {\r
     //\r
     // Write the last remaining byte if exist\r
     //\r
     PciExpressWrite8 (StartAddress, *(UINT8*)Buffer);\r
   }\r
 \r
-  return Size;\r
+  return ReturnValue;\r
 }\r