From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:400c:c0c::244; helo=mail-wr0-x244.google.com; envelope-from=leif.lindholm@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wr0-x244.google.com (mail-wr0-x244.google.com [IPv6:2a00:1450:400c:c0c::244]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id AA80720352611 for ; Thu, 26 Oct 2017 14:19:16 -0700 (PDT) Received: by mail-wr0-x244.google.com with SMTP id l1so4475278wrc.3 for ; Thu, 26 Oct 2017 14:23:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=9i5UW/q4f1ichBPrTKepNjrsw92eOH069lAd/JBgFaM=; b=kAebtjjdZpaHFXcDJGCbiL4PRNMfwl9PMdx1Qol6LxMHRRbyIr9epxtAWt/g/Os8Ty qkhlhnk96EjJAdMKuGgZFPV4mCWu4VRZWEDNH7ei6DggQAFYAAgIY1op+7CtzRNejOO6 R5R2spiufJv1G+fua7RgLkCpqBHg4aaGf8FOo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=9i5UW/q4f1ichBPrTKepNjrsw92eOH069lAd/JBgFaM=; b=llqrEIQeQUjxMBdCRrp5vYIJnmrlfC6dzUo1+LcoFTvtZSH0irIpmh2kl/IBd7o4tt hyWi+emOB0ET2S2hAaZEP1iz3sE0sIQZcpsNxsMgf3GyG3JL9D5AKcDMT9akxt3rE9RM tzKEtC8PWGTQIkmm9v8WO3lav05E1uyUA6swmul+dBm9m7NuQM8r2Hti0KnGf+wG4jcB lAA55Mp6EqV4z1TBlU0NiqHPogQ1lyZoUNNvRRdMjEDyKfcyjs1bADCRVKhIUv3bseIv yNtSqnNYInIueMDiMnohGdLJqCXZ79uirLpzX8NgRtesFhOTzBI52xA6CJydzPTLNw2E +rrw== X-Gm-Message-State: AMCzsaUfOeHH8MvJE/VUXn6uBuNgwTUWZt8nisEpNaT51Q//qDj8b+KI ZylO7gZMqa67sU0wutGaN18YOA== X-Google-Smtp-Source: ABhQp+QW9joSVJuafV4gIpQZ9NJyLZch716m2FOUYiGcE/DNtbA0JLT4U/By53S8sA7oVP+WVfWj0g== X-Received: by 10.223.185.33 with SMTP id k30mr6505371wrf.40.1509052981493; Thu, 26 Oct 2017 14:23:01 -0700 (PDT) Received: from bivouac.eciton.net (bivouac.eciton.net. [2a00:1098:0:86:1000:23:0:2]) by smtp.gmail.com with ESMTPSA id d40sm332168wma.48.2017.10.26.14.23.00 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 26 Oct 2017 14:23:00 -0700 (PDT) Date: Thu, 26 Oct 2017 22:22:59 +0100 From: Leif Lindholm To: Ard Biesheuvel Cc: edk2-devel@lists.01.org, daniel.thompson@linaro.org, masami.hiramatsu@linaro.org Message-ID: <20171026212259.kkxy6y2rocbkq3rp@bivouac.eciton.net> References: <20171025175947.22798-1-ard.biesheuvel@linaro.org> <20171025175947.22798-16-ard.biesheuvel@linaro.org> MIME-Version: 1.0 In-Reply-To: <20171025175947.22798-16-ard.biesheuvel@linaro.org> User-Agent: NeoMutt/20170113 (1.7.2) Subject: Re: [PATCH edk2-platforms v2 15/23] Silicon/SynQuacer: implement PlatformFlashAccessLib X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Oct 2017 21:19:17 -0000 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Oct 25, 2017 at 06:59:39PM +0100, Ard Biesheuvel wrote: > 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 > --- > Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.c | 250 ++++++++++++++++++++ > Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.inf | 38 +++ > 2 files changed, 288 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..cb4e1eece436 > --- /dev/null > +++ b/Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.c > @@ -0,0 +1,250 @@ > +/** @file > + Platform flash device access library NULL instance. !NULL! > + > + Copyright (c) 2016, Intel Corporation. All rights reserved.
Linaro? If you fold those in: Reviewed-by: Leif Lindholm > + 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 > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > + > +/** > + 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.
> +# 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 >