NorFlashDxe must use aligned MMIO accesses to
read data from flash as this is device memory.

The AlignedCopyMem() was used to copy the flash
data which prevented unaligned access to device
memory. However, the compiler could optimize the
code to generate pre/post indexed or LDP
operations. This is a problem for guest/virtual
firmware as the hypervisor code cannot get the
syndrome information for the trapped accesses.

Similarly, the GUIDS FwVolHeader->FileSystemGuid
and VariableStoreHeader->Signature in
ValidateFvHeader() are compared using
CompareGuid(). These GUIDs point to flash data
(which is device memory) and therefore need
aligned MMIO accesses.

To address the above issues, BaseMemoryLibMmio
library has been introduced to perform aligned
MMIO accesses.

This patch removes the usage of AlignedCopyMem()
and replaces it with CopyMem() with an expectation
that the BaseMemoryLibMmio will be linked
with NorFlashDxe.

Signed-off-by: Sami Mujawar <sami.muja...@arm.com>
---
 ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashDxe.c | 65 +-------------------
 1 file changed, 3 insertions(+), 62 deletions(-)

diff --git a/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashDxe.c 
b/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashDxe.c
index 
d9e196cbf18c32fe5306cc3c0674a7b5798a9191..f9890de8244d37e0e860fd183bb216ff7d1e7035
 100644
--- a/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashDxe.c
+++ b/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashDxe.c
@@ -1,6 +1,6 @@
 /** @file  NorFlashDxe.c
 
-  Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
+  Copyright (c) 2011 - 2020, ARM Ltd. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -735,65 +735,6 @@ NorFlashWriteBlocks (
   return Status;
 }
 
-#define BOTH_ALIGNED(a, b, align) ((((UINTN)(a) | (UINTN)(b)) & ((align) - 1)) 
== 0)
-
-/**
-  Copy Length bytes from Source to Destination, using aligned accesses only.
-  Note that this implementation uses memcpy() semantics rather then memmove()
-  semantics, i.e., SourceBuffer and DestinationBuffer should not overlap.
-
-  @param  DestinationBuffer The target of the copy request.
-  @param  SourceBuffer      The place to copy from.
-  @param  Length            The number of bytes to copy.
-
-  @return Destination
-
-**/
-STATIC
-VOID *
-AlignedCopyMem (
-  OUT     VOID                      *DestinationBuffer,
-  IN      CONST VOID                *SourceBuffer,
-  IN      UINTN                     Length
-  )
-{
-  UINT8             *Destination8;
-  CONST UINT8       *Source8;
-  UINT32            *Destination32;
-  CONST UINT32      *Source32;
-  UINT64            *Destination64;
-  CONST UINT64      *Source64;
-
-  if (BOTH_ALIGNED(DestinationBuffer, SourceBuffer, 8) && Length >= 8) {
-    Destination64 = DestinationBuffer;
-    Source64 = SourceBuffer;
-    while (Length >= 8) {
-      *Destination64++ = *Source64++;
-      Length -= 8;
-    }
-
-    Destination8 = (UINT8 *)Destination64;
-    Source8 = (CONST UINT8 *)Source64;
-  } else if (BOTH_ALIGNED(DestinationBuffer, SourceBuffer, 4) && Length >= 4) {
-    Destination32 = DestinationBuffer;
-    Source32 = SourceBuffer;
-    while (Length >= 4) {
-      *Destination32++ = *Source32++;
-      Length -= 4;
-    }
-
-    Destination8 = (UINT8 *)Destination32;
-    Source8 = (CONST UINT8 *)Source32;
-  } else {
-    Destination8 = DestinationBuffer;
-    Source8 = SourceBuffer;
-  }
-  while (Length-- != 0) {
-    *Destination8++ = *Source8++;
-  }
-  return DestinationBuffer;
-}
-
 EFI_STATUS
 NorFlashReadBlocks (
   IN NOR_FLASH_INSTANCE   *Instance,
@@ -841,7 +782,7 @@ NorFlashReadBlocks (
   SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);
 
   // Readout the data
-  AlignedCopyMem (Buffer, (VOID *)StartAddress, BufferSizeInBytes);
+  CopyMem (Buffer, (VOID *)StartAddress, BufferSizeInBytes);
 
   return EFI_SUCCESS;
 }
@@ -882,7 +823,7 @@ NorFlashRead (
   SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);
 
   // Readout the data
-  AlignedCopyMem (Buffer, (VOID *)(StartAddress + Offset), BufferSizeInBytes);
+  CopyMem (Buffer, (VOID *)(StartAddress + Offset), BufferSizeInBytes);
 
   return EFI_SUCCESS;
 }
-- 
'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#59505): https://edk2.groups.io/g/devel/message/59505
Mute This Topic: https://groups.io/mt/74200900/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to