From patchwork Fri Nov 10 14:21:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 118550 Delivered-To: patch@linaro.org Received: by 10.80.225.132 with SMTP id k4csp2258946edl; Fri, 10 Nov 2017 06:23:08 -0800 (PST) X-Google-Smtp-Source: AGs4zMYw9ypbI7eiFa2lQVU47pmqk1veiU7ulmoHO+jrsHhRdjL94jBSaajezd/3ogwxeTEr3lLd X-Received: by 10.99.110.197 with SMTP id j188mr528018pgc.34.1510323788269; Fri, 10 Nov 2017 06:23:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510323788; cv=none; d=google.com; s=arc-20160816; b=wqTMybiFp++NwhPhfOgRXoMSWnt/+3Hvbxw2ZqPcDudHFu1Q1M2oA8FhlZbnczQAf1 ilwDIO0VS+Z1zF6FsNJgvBSk1N8lzX+2R6/n7E/PyDfGZWGJlmZNJ2aGIk7nGLR/w1el DB4OxXC9PrGN0V+UFm0z3a6qD/7ikLexYCbE8vpSvt8bEPq3PoKukcvDtWqrlN4AHNt1 oWgmpz6aWrXm3cKZtQRQXxFUcqUXgylekMxv5eRwp8Dn2nekj+V/SmkMVahCnvXp2vS2 pNPf6DxrQak67DP+P7PhVxz7zD+1AEWGiZ6qtow3booO6Ha51tq6X55q8IpNzhdYrjjE 6h+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=iO9PcgEvCsOD5SHT9ob2Ms2gyh/pQGaQ9nrqX/apqeM=; b=xXoW0FE/rvZrcYmU7A7pdxo8j30/nkC+2QtSHeIWZWRB1kNpZqSieZP/mcijNZc1dq MXmjHVOz65X5TJd5nkRALMBnTukobF1Pl6YPilHKX5bT7y3lHWGmoHl6ZvLNuDvBMxM9 xkI6SepoERo4vuyA8WM3/3EgYe6pB5MaBxR6ASA6LVhSLJCOUTAvLty1kJ8V9fjcI5zL nGzkGbkHmpevpCRV/U7KcvX4ayrxWKt3UY7Mqqhkf65Q6pCVKKn7cMaOSSVHaVgJJidX VvHWCmkWx7XBmLif5bTc8cpy3cs9NW5JJ3/4hDsqN9W6GFmokY3fbs70Iha9mtpMhB9v ezAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=G4NLn6/P; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [198.145.21.10]) by mx.google.com with ESMTPS id w12si9009130pgp.482.2017.11.10.06.23.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Nov 2017 06:23:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) client-ip=198.145.21.10; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=G4NLn6/P; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id EC92A20355206; Fri, 10 Nov 2017 06:18:31 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:400c:c09::243; helo=mail-wm0-x243.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wm0-x243.google.com (mail-wm0-x243.google.com [IPv6:2a00:1450:400c:c09::243]) (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 8090B2035D0F1 for ; Fri, 10 Nov 2017 06:18:29 -0800 (PST) Received: by mail-wm0-x243.google.com with SMTP id r68so3084690wmr.1 for ; Fri, 10 Nov 2017 06:22:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=F+2516Ub3p/EZc47xRz2y9cdDIyvuoDj3fQ3P0BsbzM=; b=G4NLn6/P5jKjoriZ8c/bLAa+9pMNfoAenVvFIlRcJNSW6ECfS7IQw7o/LlQiCtMKif SKUAc82UVI7AOGxA88ymAbxuSCbKyrq5QJYF7WSQY/3MF5UZlj6J5ajkjojb7XtJd+MC SxyF49AP819aSMBGNOySTmJrR8SPAb1NzM+3A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=F+2516Ub3p/EZc47xRz2y9cdDIyvuoDj3fQ3P0BsbzM=; b=bLVOs1fAD9hhoAs9wnMnwOf93kGduyPYX47SUW4xT97G747TFJBT+F12U1pE5Etq8+ zNP14jInrQ4CP9SgKXzyJFtEv+0IYim/r5GvuNoDJVlSgIzDbzszLQWqqxZoqTwWA7BY LbOeHfSkzTRnAVsdBchecOUtrVMygMyXkQtm+HgreQfD53YXJwMUsXxw1QF9pN6eXLtr L9tL3fub8AyNObTJuYEp3gCYrep7yRfmEEmg8uOWC6jd31Pi0tg1919EFBITCC3LreyF bCpDi8ysFrlg0kqNBEsrGJIAHrDGEDWJX923p04D147fZzAWYo6lMY5AzNGCV9V4wlHB j4ww== X-Gm-Message-State: AJaThX6Qkf/iCyoTIOsBfYlAWGywLHDF17mB0wJGAm4JEOKhaJ47KknB ZkruHFXlwb9OIZ0Fvlk2blBIJ/dWpIA= X-Received: by 10.28.131.200 with SMTP id f191mr385783wmd.39.1510323750868; Fri, 10 Nov 2017 06:22:30 -0800 (PST) Received: from localhost.localdomain ([160.167.170.128]) by smtp.gmail.com with ESMTPSA id e131sm1036477wmg.15.2017.11.10.06.22.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Nov 2017 06:22:30 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org, leif.lindholm@linaro.org, daniel.thompson@linaro.org Date: Fri, 10 Nov 2017 14:21:09 +0000 Message-Id: <20171110142127.12018-17-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171110142127.12018-1-ard.biesheuvel@linaro.org> References: <20171110142127.12018-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH edk2-platforms v4 16/34] 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: , Cc: masahisa.kojima@linaro.org, masami.hiramatsu@linaro.org, Ard Biesheuvel MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" 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 Reviewed-by: Leif Lindholm --- Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.c | 251 ++++++++++++++++++++ Silicon/Socionext/SynQuacer/Library/SynQuacerPlatformFlashAccessLib/SynQuacerPlatformFlashAccessLib.inf | 38 +++ 2 files changed, 289 insertions(+) -- 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel 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.
+ + 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 +