]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePciCf8Lib/PciLib.c
Update the PciXXXReadBuffer() for better coding style.
[mirror_edk2.git] / MdePkg / Library / BasePciCf8Lib / PciLib.c
index 9421d8b99d3cb4ba942e8153491d08901e33265e..64bd1557fc5d86ca5c0ea47cdd90993c3175c1e7 100644 (file)
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-  Module Name:  PciLib.c\r
-\r
 **/\r
 \r
+\r
+#include <Base.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/PciCf8Lib.h>\r
+#include <Library/IoLib.h>\r
+#include <Library/DebugLib.h>\r
+\r
 //\r
 // Declare I/O Ports used to perform PCI Confguration Cycles\r
 //\r
@@ -1295,63 +1301,71 @@ PciCf8ReadBuffer (
   OUT     VOID                      *Buffer\r
   )\r
 {\r
-  UINTN                             EndAddress;\r
+  UINTN   ReturnValue;\r
 \r
   ASSERT_INVALID_PCI_ADDRESS (StartAddress, 0);\r
   ASSERT (((StartAddress & 0xFFF) + Size) <= 0x100);\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
     // Read a byte if StartAddress is byte aligned\r
     //\r
-    *(UINT8*)Buffer = PciCf8Read8 (StartAddress);\r
+    *(volatile UINT8 *)Buffer = PciCf8Read8 (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 = PciCf8Read16 (StartAddress);\r
+    WriteUnaligned16 ((UINT16 *)Buffer, (UINT16) PciCf8Read16 (StartAddress));\r
+\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 = PciCf8Read32 (StartAddress);\r
+    WriteUnaligned32 ((UINT32 *)Buffer, (UINT32) PciCf8Read32 (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 = PciCf8Read16 (StartAddress);\r
+    WriteUnaligned16 ((UINT16 *)Buffer, (UINT16) PciCf8Read16 (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 = PciCf8Read8 (StartAddress);\r
+    *(volatile UINT8 *)Buffer = PciCf8Read8 (StartAddress);\r
   }\r
 \r
-  return Size;\r
+  return ReturnValue;\r
 }\r
 \r
 /**\r
@@ -1387,7 +1401,7 @@ PciCf8WriteBuffer (
   IN      VOID                      *Buffer\r
   )\r
 {\r
-  UINTN                             EndAddress;\r
+  UINTN   ReturnValue;\r
 \r
   ASSERT_INVALID_PCI_ADDRESS (StartAddress, 0);\r
   ASSERT (((StartAddress & 0xFFF) + Size) <= 0x100);\r
@@ -1396,52 +1410,59 @@ PciCf8WriteBuffer (
     return 0;\r
   }\r
 \r
-  ASSERT (Buffer != 0);\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
+  if ((StartAddress & 1) != 0) {\r
     //\r
     // Write a byte if StartAddress is byte aligned\r
     //\r
     PciCf8Write8 (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
-    PciCf8Write16 (StartAddress, *(UINT16*)Buffer);\r
+    PciCf8Write16 (StartAddress, ReadUnaligned16 ((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
-    PciCf8Write32 (StartAddress, *(UINT32*)Buffer);\r
+    PciCf8Write32 (StartAddress, ReadUnaligned32 ((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
-    PciCf8Write16 (StartAddress, *(UINT16*)Buffer);\r
+    PciCf8Write16 (StartAddress, ReadUnaligned16 ((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
     PciCf8Write8 (StartAddress, *(UINT8*)Buffer);\r
   }\r
 \r
-  return Size;\r
+  return ReturnValue;\r
 }\r