]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmPlatformPkg/NorFlashDxe: use strictly aligned CopyMem()
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Wed, 7 Sep 2016 10:32:42 +0000 (11:32 +0100)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Fri, 9 Sep 2016 12:48:49 +0000 (13:48 +0100)
The UEFI spec stipulates that unaligned accesses should be enabled
on CPUs that support them, which means all of them, given that we
no longer support pre-v7 ARM cores, and the AARCH64 bindings mandate
support for unaligned accesses unconditionally.

This means that one should not assume that CopyMem () is safe to call
on regions that may be mapped using device attributes, which is the
case for the NOR flash. Since we have no control over the mappings when
running under the OS, and given that write accesses require device
mappings, we should not call CopyMem () in the read path either, but
use our own implementation that is guaranteed to take alignment into
account.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Leif Lindholm <leif.lindholm@linaro.org>
ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashDxe.c

index 3abbe5cb32bc2c7f44af3f014e6cb4515e022ff6..ca61ac5e1983d3cdad2e2f1ae59c3b1d6abe814f 100644 (file)
@@ -744,6 +744,65 @@ NorFlashWriteBlocks (
   return Status;\r
 }\r
 \r
+#define BOTH_ALIGNED(a, b, align) ((((UINTN)(a) | (UINTN)(b)) & ((align) - 1)) == 0)\r
+\r
+/**\r
+  Copy Length bytes from Source to Destination, using aligned accesses only.\r
+  Note that this implementation uses memcpy() semantics rather then memmove()\r
+  semantics, i.e., SourceBuffer and DestinationBuffer should not overlap.\r
+\r
+  @param  DestinationBuffer The target of the copy request.\r
+  @param  SourceBuffer      The place to copy from.\r
+  @param  Length            The number of bytes to copy.\r
+\r
+  @return Destination\r
+\r
+**/\r
+STATIC\r
+VOID *\r
+AlignedCopyMem (\r
+  OUT     VOID                      *DestinationBuffer,\r
+  IN      CONST VOID                *SourceBuffer,\r
+  IN      UINTN                     Length\r
+  )\r
+{\r
+  UINT8             *Destination8;\r
+  CONST UINT8       *Source8;\r
+  UINT32            *Destination32;\r
+  CONST UINT32      *Source32;\r
+  UINT64            *Destination64;\r
+  CONST UINT64      *Source64;\r
+\r
+  if (BOTH_ALIGNED(DestinationBuffer, SourceBuffer, 8) && Length >= 8) {\r
+    Destination64 = DestinationBuffer;\r
+    Source64 = SourceBuffer;\r
+    while (Length >= 8) {\r
+      *Destination64++ = *Source64++;\r
+      Length -= 8;\r
+    }\r
+\r
+    Destination8 = (UINT8 *)Destination64;\r
+    Source8 = (CONST UINT8 *)Source64;\r
+  } else if (BOTH_ALIGNED(DestinationBuffer, SourceBuffer, 4) && Length >= 4) {\r
+    Destination32 = DestinationBuffer;\r
+    Source32 = SourceBuffer;\r
+    while (Length >= 4) {\r
+      *Destination32++ = *Source32++;\r
+      Length -= 4;\r
+    }\r
+\r
+    Destination8 = (UINT8 *)Destination32;\r
+    Source8 = (CONST UINT8 *)Source32;\r
+  } else {\r
+    Destination8 = DestinationBuffer;\r
+    Source8 = SourceBuffer;\r
+  }\r
+  while (Length-- != 0) {\r
+    *Destination8++ = *Source8++;\r
+  }\r
+  return DestinationBuffer;\r
+}\r
+\r
 EFI_STATUS\r
 NorFlashReadBlocks (\r
   IN NOR_FLASH_INSTANCE   *Instance,\r
@@ -791,7 +850,7 @@ NorFlashReadBlocks (
   SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
 \r
   // Readout the data\r
-  CopyMem(Buffer, (UINTN *)StartAddress, BufferSizeInBytes);\r
+  AlignedCopyMem (Buffer, (VOID *)StartAddress, BufferSizeInBytes);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -832,7 +891,7 @@ NorFlashRead (
   SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
 \r
   // Readout the data\r
-  CopyMem (Buffer, (UINTN *)(StartAddress + Offset), BufferSizeInBytes);\r
+  AlignedCopyMem (Buffer, (VOID *)StartAddress + Offset, BufferSizeInBytes);\r
 \r
   return EFI_SUCCESS;\r
 }\r