]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashFvbDxe.c
ArmPlatformPkg/Drivers/NorFlashDxe: Directly implement DiskIO protocol
[mirror_edk2.git] / ArmPlatformPkg / Drivers / NorFlashDxe / NorFlashFvbDxe.c
index b9230c1ee007c8f147bbb86b565e1b5b8a40b65e..bf420853b0a173d96cbb37a163f11ae2fac2eec2 100644 (file)
@@ -20,6 +20,7 @@
 #include <Library/UefiLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
+#include <Library/DxeServicesTableLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 \r
 #include <Guid/VariableFormat.h>\r
@@ -27,6 +28,8 @@
 \r
 #include "NorFlashDxe.h"\r
 \r
+STATIC EFI_EVENT mFvbVirtualAddrChangeEvent;\r
+STATIC UINTN     mFlashNvStorageVariableBase;\r
 \r
 ///\r
 /// The Firmware Volume Block Protocol is the low-level interface\r
@@ -301,7 +304,7 @@ FvbGetPhysicalAddress (
 \r
   ASSERT(Address != NULL);\r
 \r
-  *Address = PcdGet32 (PcdFlashNvStorageVariableBase);\r
+  *Address = mFlashNvStorageVariableBase;\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -414,7 +417,6 @@ FvbRead (
   IN OUT    UINT8                                 *Buffer\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
   EFI_STATUS    TempStatus;\r
   UINTN         BlockSize;\r
   NOR_FLASH_INSTANCE *Instance;\r
@@ -427,8 +429,7 @@ FvbRead (
     Instance->Initialize(Instance);\r
   }\r
 \r
-  Status = EFI_SUCCESS;\r
-  TempStatus = Status;\r
+  TempStatus = EFI_SUCCESS;\r
 \r
   // Cache the block size to avoid de-referencing pointers all the time\r
   BlockSize = Instance->Media.BlockSize;\r
@@ -449,25 +450,21 @@ FvbRead (
     return EFI_BAD_BUFFER_SIZE;\r
   }\r
 \r
-  // Check we did get some memory\r
-  if (Instance->FvbBuffer == NULL) {\r
-    DEBUG ((EFI_D_ERROR, "FvbRead: ERROR - Buffer not ready\n"));\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\r
-  // Read NOR Flash data into shadow buffer\r
-  TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Instance->FvbBuffer);\r
-  if (EFI_ERROR (TempStatus)) {\r
-    // Return one of the pre-approved error statuses\r
-    return EFI_DEVICE_ERROR;\r
+  // Decide if we are doing full block reads or not.\r
+  if (*NumBytes % BlockSize != 0) {\r
+    TempStatus = NorFlashRead (Instance, Instance->StartLba + Lba, Offset, *NumBytes, Buffer);\r
+    if (EFI_ERROR (TempStatus)) {\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+  } else {\r
+    // Read NOR Flash data into shadow buffer\r
+    TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Buffer);\r
+    if (EFI_ERROR (TempStatus)) {\r
+      // Return one of the pre-approved error statuses\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
   }\r
-\r
-  // Put the data at the appropriate location inside the buffer area\r
-  DEBUG ((DEBUG_BLKIO, "FvbRead: CopyMem( Dst=0x%08x, Src=0x%08x, Size=0x%x ).\n", Buffer, (UINTN)Instance->FvbBuffer + Offset, *NumBytes));\r
-\r
-  CopyMem (Buffer, (VOID*)((UINTN)Instance->FvbBuffer + Offset), *NumBytes);\r
-\r
-  return Status;\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -534,71 +531,11 @@ FvbWrite (
   IN        UINT8                                 *Buffer\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
-  EFI_STATUS  TempStatus;\r
-  UINTN       BlockSize;\r
   NOR_FLASH_INSTANCE *Instance;\r
 \r
-  Instance = INSTANCE_FROM_FVB_THIS(This);\r
-\r
-  if (!Instance->Initialized && Instance->Initialize) {\r
-    Instance->Initialize(Instance);\r
-  }\r
-\r
-  DEBUG ((DEBUG_BLKIO, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBytes, Buffer));\r
-\r
-  Status = EFI_SUCCESS;\r
-  TempStatus = Status;\r
-\r
-  // Detect WriteDisabled state\r
-  if (Instance->Media.ReadOnly == TRUE) {\r
-    DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - Can not write: Device is in WriteDisabled state.\n"));\r
-    // It is in WriteDisabled state, return an error right away\r
-    return EFI_ACCESS_DENIED;\r
-  }\r
-\r
-  // Cache the block size to avoid de-referencing pointers all the time\r
-  BlockSize = Instance->Media.BlockSize;\r
-\r
-  // The write must not span block boundaries.\r
-  // We need to check each variable individually because adding two large values together overflows.\r
-  if ( ( Offset               >= BlockSize ) ||\r
-       ( *NumBytes            >  BlockSize ) ||\r
-       ( (Offset + *NumBytes) >  BlockSize )    ) {\r
-    DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r
-    return EFI_BAD_BUFFER_SIZE;\r
-  }\r
+  Instance = INSTANCE_FROM_FVB_THIS (This);\r
 \r
-  // We must have some bytes to write\r
-  if (*NumBytes == 0) {\r
-    DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r
-    return EFI_BAD_BUFFER_SIZE;\r
-  }\r
-\r
-  // Check we did get some memory\r
-  if (Instance->FvbBuffer == NULL) {\r
-    DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\r
-  // Read NOR Flash data into shadow buffer\r
-  TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Instance->FvbBuffer);\r
-  if (EFI_ERROR (TempStatus)) {\r
-    // Return one of the pre-approved error statuses\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\r
-  // Put the data at the appropriate location inside the buffer area\r
-  CopyMem ((VOID*)((UINTN)Instance->FvbBuffer + Offset), Buffer, *NumBytes);\r
-\r
-  // Write the modified buffer back to the NorFlash\r
-  TempStatus = NorFlashWriteBlocks (Instance, Instance->StartLba + Lba, BlockSize, Instance->FvbBuffer);\r
-  if (EFI_ERROR (TempStatus)) {\r
-    // Return one of the pre-approved error statuses\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\r
-  return Status;\r
+  return NorFlashWriteSingleBlock (Instance, Lba, Offset, NumBytes, Buffer);\r
 }\r
 \r
 /**\r
@@ -745,6 +682,25 @@ EXIT:
   return Status;\r
 }\r
 \r
+/**\r
+  Fixup internal data so that EFI can be call in virtual mode.\r
+  Call the passed in Child Notify event and convert any pointers in\r
+  lib to virtual mode.\r
+\r
+  @param[in]    Event   The Event that is being processed\r
+  @param[in]    Context Event Context\r
+**/\r
+VOID\r
+EFIAPI\r
+FvbVirtualNotifyEvent (\r
+  IN EFI_EVENT        Event,\r
+  IN VOID             *Context\r
+  )\r
+{\r
+  EfiConvertPointer (0x0, (VOID**)&mFlashNvStorageVariableBase);\r
+  return;\r
+}\r
+\r
 EFI_STATUS\r
 EFIAPI\r
 NorFlashFvbInitialize (\r
@@ -754,10 +710,12 @@ NorFlashFvbInitialize (
   EFI_STATUS  Status;\r
   UINT32      FvbNumLba;\r
   EFI_BOOT_MODE BootMode;\r
+  UINTN       RuntimeMmioRegionSize;\r
 \r
   DEBUG((DEBUG_BLKIO,"NorFlashFvbInitialize\n"));\r
 \r
   Instance->Initialized = TRUE;\r
+  mFlashNvStorageVariableBase = FixedPcdGet32 (PcdFlashNvStorageVariableBase);\r
 \r
   // Set the index of the first LBA for the FVB\r
   Instance->StartLba = (PcdGet32 (PcdFlashNvStorageVariableBase) - Instance->RegionBaseAddress) / Instance->Media.BlockSize;\r
@@ -789,5 +747,41 @@ NorFlashFvbInitialize (
       return Status;\r
     }\r
   }\r
+\r
+  //\r
+  // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME\r
+  //\r
+\r
+  // Note: all the NOR Flash region needs to be reserved into the UEFI Runtime memory;\r
+  //       even if we only use the small block region at the top of the NOR Flash.\r
+  //       The reason is when the NOR Flash memory is set into program mode, the command\r
+  //       is written as the base of the flash region (ie: Instance->DeviceBaseAddress)\r
+  RuntimeMmioRegionSize = (Instance->RegionBaseAddress - Instance->DeviceBaseAddress) + Instance->Size;\r
+\r
+  Status = gDS->AddMemorySpace (\r
+      EfiGcdMemoryTypeMemoryMappedIo,\r
+      Instance->DeviceBaseAddress, RuntimeMmioRegionSize,\r
+      EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r
+      );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = gDS->SetMemorySpaceAttributes (\r
+      Instance->DeviceBaseAddress, RuntimeMmioRegionSize,\r
+      EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Register for the virtual address change event\r
+  //\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  FvbVirtualNotifyEvent,\r
+                  NULL,\r
+                  &gEfiEventVirtualAddressChangeGuid,\r
+                  &mFvbVirtualAddrChangeEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   return Status;\r
 }\r