public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ard.biesheuvel@linaro.org>
To: edk2-devel@lists.01.org, leif.lindholm@linaro.org
Cc: graeme.gregory@linaro.org, daniel.thompson@linaro.org,
	masami.hiramatsu@linaro.org, methavanitpong.pipat@socionext.com,
	Ard Biesheuvel <ard.biesheuvel@linaro.org>
Subject: [PATCH edk2-platforms v3 17/27] Silicon/SynQuacer: implement PlatformFlashAccessLib
Date: Tue, 31 Oct 2017 10:52:08 +0000	[thread overview]
Message-ID: <20171031105218.30208-18-ard.biesheuvel@linaro.org> (raw)
In-Reply-To: <20171031105218.30208-1-ard.biesheuvel@linaro.org>

In order to support capsule update, implement PlatformFlashAccessLib that
exposes write access to the UEFI NOR partition.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.c   | 251 ++++++++++++++++++++
 Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.inf |  38 +++
 2 files changed, 289 insertions(+)

diff --git a/Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.c b/Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.c
new file mode 100644
index 000000000000..5a67f8c00995
--- /dev/null
+++ b/Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.c
@@ -0,0 +1,251 @@
+/** @file
+  Platform flash device access library for Socionext SynQuacer
+
+  Copyright (c) 2016, Linaro, Ltd. All rights reserved.<BR>
+
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <PiDxe.h>
+
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/DxeServicesTableLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/PlatformFlashAccessLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+
+#include <Protocol/FirmwareVolumeBlock.h>
+
+/**
+  Gets firmware volume block handle by given address.
+
+  This function gets firmware volume block handle whose
+  address range contains the parameter Address.
+
+  @param[in]  Address    Address which should be contained
+                         by returned FVB handle.
+  @param[out] FvbHandle  Pointer to FVB handle for output.
+
+  @retval EFI_SUCCESS    FVB handle successfully returned.
+  @retval EFI_NOT_FOUND  Failed to find FVB handle by address.
+
+**/
+STATIC
+EFI_STATUS
+GetFvbByAddress (
+  IN  EFI_PHYSICAL_ADDRESS                Address,
+  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **OutFvb,
+  OUT EFI_PHYSICAL_ADDRESS                *FvbBaseAddress
+  )
+{
+  EFI_STATUS                          Status;
+  EFI_HANDLE                          *HandleBuffer;
+  UINTN                               HandleCount;
+  UINTN                               Index;
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;
+  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;
+  EFI_FVB_ATTRIBUTES_2                Attributes;
+
+  //
+  // Locate all handles with Firmware Volume Block protocol
+  //
+  Status = gBS->LocateHandleBuffer (
+                  ByProtocol,
+                  &gEfiFirmwareVolumeBlockProtocolGuid,
+                  NULL,
+                  &HandleCount,
+                  &HandleBuffer
+                  );
+  if (EFI_ERROR (Status)) {
+    return EFI_NOT_FOUND;
+  }
+  //
+  // Traverse all the handles, searching for the one containing parameter Address
+  //
+  for (Index = 0; Index < HandleCount; Index += 1) {
+    Status = gBS->HandleProtocol (
+                    HandleBuffer[Index],
+                    &gEfiFirmwareVolumeBlockProtocolGuid,
+                    (VOID **) &Fvb
+                    );
+    if (EFI_ERROR (Status)) {
+      Status = EFI_NOT_FOUND;
+      break;
+    }
+    //
+    // Checks if the address range of this handle contains parameter Address
+    //
+    Status = Fvb->GetPhysicalAddress (Fvb, FvbBaseAddress);
+    if (EFI_ERROR (Status)) {
+      continue;
+    }
+
+    //
+    // Check if this FVB is writable: DXE core produces FVB protocols for
+    // firmware volumes as well.
+    //
+    Status = Fvb->GetAttributes (Fvb, &Attributes);
+    if (EFI_ERROR (Status) || !(Attributes & EFI_FVB2_WRITE_STATUS)) {
+      DEBUG ((DEBUG_INFO,
+        "%a: ignoring read-only FVB protocol implementation\n",
+        __FUNCTION__));
+      continue;
+    }
+
+    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) *FvbBaseAddress);
+    if ((Address >= *FvbBaseAddress) && (Address <= (*FvbBaseAddress + FwVolHeader->FvLength))) {
+      *OutFvb  = Fvb;
+      Status   = EFI_SUCCESS;
+      break;
+    }
+
+    Status = EFI_NOT_FOUND;
+  }
+
+  FreePool (HandleBuffer);
+  return Status;
+}
+
+/**
+  Perform flash write operation.
+
+  @param[in] FirmwareType      The type of firmware.
+  @param[in] FlashAddress      The address of flash device to be accessed.
+  @param[in] FlashAddressType  The type of flash device address.
+  @param[in] Buffer            The pointer to the data buffer.
+  @param[in] Length            The length of data buffer in bytes.
+
+  @retval EFI_SUCCESS           The operation returns successfully.
+  @retval EFI_WRITE_PROTECTED   The flash device is read only.
+  @retval EFI_UNSUPPORTED       The flash device access is unsupported.
+  @retval EFI_INVALID_PARAMETER The input parameter is not valid.
+**/
+EFI_STATUS
+EFIAPI
+PerformFlashWrite (
+  IN PLATFORM_FIRMWARE_TYPE       FirmwareType,
+  IN EFI_PHYSICAL_ADDRESS         FlashAddress,
+  IN FLASH_ADDRESS_TYPE           FlashAddressType,
+  IN VOID                         *Buffer,
+  IN UINTN                        Length
+  )
+{
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;
+  EFI_STATUS                          Status;
+  UINTN                               BlockSize;
+  UINTN                               NumberOfBlocks;
+  EFI_LBA                             Lba;
+  EFI_PHYSICAL_ADDRESS                FvbBaseAddress;
+  UINTN                               NumBytes;
+
+  if (FlashAddressType != FlashAddressTypeAbsoluteAddress) {
+    DEBUG ((DEBUG_ERROR, "%a: only FlashAddressTypeAbsoluteAddress supported\n",
+      __FUNCTION__));
+
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (FirmwareType != PlatformFirmwareTypeSystemFirmware) {
+    DEBUG ((DEBUG_ERROR,
+      "%a: only PlatformFirmwareTypeSystemFirmware supported\n",
+      __FUNCTION__));
+
+    return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Locate the gEfiFirmwareVolumeBlockProtocolGuid implementation
+  // that covers the system firmware
+  //
+  Fvb = NULL;
+  Status = GetFvbByAddress (FlashAddress, &Fvb, &FvbBaseAddress);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR,
+      "%a: failed to locate FVB handle for address 0x%llx - %r\n",
+      __FUNCTION__, FlashAddress, Status));
+    return Status;
+  }
+
+  Status = Fvb->GetBlockSize(Fvb, 0, &BlockSize, &NumberOfBlocks);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "%a: failed to get FVB blocksize - %r\n",
+      __FUNCTION__, Status));
+    return Status;
+  }
+
+  if ((Length % BlockSize) != 0) {
+    DEBUG ((DEBUG_ERROR,
+      "%a: Length 0x%lx is not a multiple of the blocksize 0x%lx\n",
+      __FUNCTION__, Length, BlockSize));
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Lba = (FlashAddress - FvbBaseAddress) / BlockSize;
+  if (Lba > NumberOfBlocks - 1) {
+    DEBUG ((DEBUG_ERROR,
+      "%a: flash device with non-uniform blocksize not supported\n",
+      __FUNCTION__));
+    return EFI_UNSUPPORTED;
+  }
+
+  //
+  // Remap the region as device rather than uncached.
+  //
+  Status = gDS->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo,
+                  FlashAddress, Length, EFI_MEMORY_UC);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_WARN, "%a: gDS->AddMemorySpace () failed - %r\n",
+      __FUNCTION__, Status));
+  }
+
+  Status = gDS->SetMemorySpaceAttributes (FlashAddress, Length, EFI_MEMORY_UC);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "%a: gDS->SetMemorySpaceAttributes () failed - %r\n",
+      __FUNCTION__, Status));
+    return Status;
+  }
+
+  //
+  // Erase the region
+  //
+  DEBUG ((DEBUG_INFO, "%a: erasing 0x%llx bytes at address %llx (LBA 0x%lx)\n",
+    __FUNCTION__, Length, FlashAddress, Lba));
+
+  Status = Fvb->EraseBlocks (Fvb, Lba, Length / BlockSize,
+                  EFI_LBA_LIST_TERMINATOR);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "%a: Fvb->EraseBlocks () failed - %r\n",
+      __FUNCTION__, Status));
+    return Status;
+  }
+
+  while (Length > 0) {
+    //
+    // Write the new data
+    //
+    DEBUG ((DEBUG_INFO, "%a: writing 0x%llx bytes at LBA 0x%lx\n",
+      __FUNCTION__, BlockSize, Lba));
+
+    NumBytes = BlockSize;
+    Status = Fvb->Write (Fvb, Lba, 0, &NumBytes, Buffer);
+    if (EFI_ERROR (Status)) {
+      DEBUG ((DEBUG_ERROR,
+        "%a: write of LBA 0x%lx failed - %r (NumBytes == 0x%lx)\n",
+        __FUNCTION__, Lba, Status, NumBytes));
+    }
+
+    Buffer += BlockSize;
+    Length -= BlockSize;
+    Lba++;
+  }
+
+  return EFI_SUCCESS;
+}
diff --git a/Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.inf b/Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.inf
new file mode 100644
index 000000000000..33f4795ddcf9
--- /dev/null
+++ b/Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.inf
@@ -0,0 +1,38 @@
+## @file
+#  Platform flash device access library.
+#
+#  Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR>
+#  This program and the accompanying materials
+#  are licensed and made available under the terms and conditions of the BSD License
+#  which accompanies this distribution.  The full text of the license may be found at
+#  http://opensource.org/licenses/bsd-license.php
+#
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010019
+  BASE_NAME                      = SynQuacerPlatformFlashAccessLib
+  FILE_GUID                      = b4e7f39f-ab67-4318-b1ad-c1e211304f0b
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = PlatformFlashAccessLib|DXE_DRIVER
+
+[Sources]
+  SynQuacerPlatformFlashAccessLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  SignedCapsulePkg/SignedCapsulePkg.dec
+
+[Protocols]
+  gEfiFirmwareVolumeBlockProtocolGuid
+
+[LibraryClasses]
+  BaseMemoryLib
+  DebugLib
+  DxeServicesTableLib
+  UefiBootServicesTableLib
+
-- 
2.11.0



  parent reply	other threads:[~2017-10-31 10:49 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-31 10:51 [PATCH edk2-platforms v3 00/27] add support for Socionext Synquacer Ard Biesheuvel
2017-10-31 10:51 ` [PATCH edk2-platforms v3 01/27] Silicon/SynQuacer: add package with platform headers Ard Biesheuvel
2017-10-31 10:51 ` [PATCH edk2-platforms v3 02/27] Silicon/Socionext: add driver for NETSEC network controller Ard Biesheuvel
2017-11-01  4:41   ` Leif Lindholm
2017-10-31 10:51 ` [PATCH edk2-platforms v3 03/27] Silicon/Socionext: add PlatformPeilib implementation for SynQuacer Ard Biesheuvel
2017-11-01  4:44   ` Leif Lindholm
2017-10-31 10:51 ` [PATCH edk2-platforms v3 04/27] Silicon/SynQuacer: implement a platform DXE driver Ard Biesheuvel
2017-11-01  4:46   ` Leif Lindholm
2017-10-31 10:51 ` [PATCH edk2-platforms v3 05/27] Silicon/SynQuacer: add MemoryInitPeiLib implementation Ard Biesheuvel
2017-11-01  4:49   ` Leif Lindholm
2017-10-31 10:51 ` [PATCH edk2-platforms v3 06/27] Platform: add support for Socionext SynQuacer eval board Ard Biesheuvel
2017-11-01  4:53   ` Leif Lindholm
2017-10-31 10:51 ` [PATCH edk2-platforms v3 07/27] Silicon/SynQuacer: implement PciSegmentLib to support dual RCs Ard Biesheuvel
2017-10-31 10:51 ` [PATCH edk2-platforms v3 08/27] Silicon/SynQuacer: implement PciHostBridgeLib support Ard Biesheuvel
2017-11-01  4:57   ` Leif Lindholm
2017-10-31 10:52 ` [PATCH edk2-platforms v3 09/27] Silicon/SynQuacer: implement EFI_CPU_IO2_PROTOCOL Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 10/27] Platform/SynQuacerEvalBoard: add PCI support Ard Biesheuvel
2017-11-01  4:59   ` Leif Lindholm
2017-10-31 10:52 ` [PATCH edk2-platforms v3 11/27] Platform/SynQuacerEvalBoard: add NETSEC driver Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 12/27] Silicon/SynQuacer: add ACPI support Ard Biesheuvel
2017-11-01 10:15   ` graeme.gregory
2017-11-01 16:36     ` Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 13/27] Silicon/SynQuacer: add device tree support for eval board Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 14/27] Silicon/SynQuacer: add NorFlashPlatformLib implementation Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 15/27] Silicon/Socionext: add driver for SPI NOR flash Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 16/27] Platform/SynQuacer: incorporate NOR flash and variable drivers Ard Biesheuvel
2017-10-31 10:52 ` Ard Biesheuvel [this message]
2017-11-01  5:06   ` [PATCH edk2-platforms v3 17/27] Silicon/SynQuacer: implement PlatformFlashAccessLib Leif Lindholm
2017-10-31 10:52 ` [PATCH edk2-platforms v3 18/27] SynQuacer/SynQuacerMemoryInitPeiLib: add capsule support Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 19/27] Socionext/SynQuacerEvalBoard: wire up basic " Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 20/27] Socionext/SynQuacerEvalBoard: switch to execute in place Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 21/27] Platform/SynQuacerEvalBoard: add signed capsule update support Ard Biesheuvel
2017-11-01  5:10   ` Leif Lindholm
2017-10-31 10:52 ` [PATCH edk2-platforms v3 22/27] Silicon/SynQuacer/AcpiTables: hide PCI domain #0 Ard Biesheuvel
2017-11-01  5:12   ` Leif Lindholm
2017-10-31 10:52 ` [PATCH edk2-platforms v3 23/27] Silicon/SynQuacerPciHostBridgeLib: add workaround to support 32-bit only cards Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 24/27] Platform/Socionext: add support for Socionext Developer Box rev 0.1 Ard Biesheuvel
2017-11-01  5:18   ` Leif Lindholm
2017-11-01  8:31     ` Ard Biesheuvel
2017-11-01 10:01       ` Leif Lindholm
2017-11-01 14:34         ` Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 25/27] Platform/DeveloperBox: add ConsolePrefDxe driver Ard Biesheuvel
2017-10-31 10:52 ` [PATCH edk2-platforms v3 26/27] Silicon/SynQuacer: add description of GPIO block to device tree Ard Biesheuvel
2017-11-01  5:20   ` Leif Lindholm
2017-10-31 10:52 ` [PATCH edk2-platforms v3 27/27] Silicon/SynQuacer: add description of EXIU to the " Ard Biesheuvel
2017-11-01  5:21   ` Leif Lindholm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171031105218.30208-18-ard.biesheuvel@linaro.org \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox