From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web10.623.1666630898601599335 for ; Mon, 24 Oct 2022 10:01:38 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=It6VOtLr; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C2286614E7; Mon, 24 Oct 2022 17:01:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BBD0DC433B5; Mon, 24 Oct 2022 17:01:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666630897; bh=n8HUkBzeSmtkPPWfpTbvjnK+SsHOkc9HRsGow86/P7Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=It6VOtLrGZASnJBCR1hNBli/NLr7sNYpvcCJWEweyUEZUc/g8qW+fjRW1aDR1VRXb 1khp/k2CxAax0FxzJwQsgqiAN2ARatV9M+T2USxsmOtqJ3t191+ncUvquz6825C6vZ +HOj/9MZ1g1Pt4S8wsfHbcqCPbryKdi4hVc9Y/12WafIyKZ5sA+2VT3uLNw2afWjcY MOSXowZTEL+4PjvGwYDyuO/UiApwx48lN+u2HsdNQHKlJUmM4ZrrSBL5zWlUo+d8DU mrxnIhbhZIlgp/ZgNFJhtRe5e8tjge30Lh4h1s9m52bhqCBSbqJVo17lm+Rrdo0M1N h6fZ1/7qj6g2w== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Gerd Hoffmann , Sunil V L , Sami Mujawar , Leif Lindholm Subject: [PATCH 02/11] OvmfPkg/VirtNorFlashDxe: clone ArmPlatformPkg's NOR flash driver Date: Mon, 24 Oct 2022 19:01:13 +0200 Message-Id: <20221024170122.594577-3-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221024170122.594577-1-ardb@kernel.org> References: <20221024170122.594577-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable QEMU's mach-virt is loosely based on ARM Versatile Express, and inherits its NOR flash driver, which is now being used on other QEMU emulated architectures as well. In order to permit ourselves the freedom to optimize this driver for use under KVM emulation, let's clone it into OvmfPkg, so we have a version we can hack without the risk of regressing bare metal platforms. The cloned version is mostly identical to the original, but it depends on the newly added VirtNorFlashPlatformLib library class instead of the original one from ArmPlatformPkg. Beyond that, only cosmetic changes related to #include order etc were made. Signed-off-by: Ard Biesheuvel --- OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c | 991 ++++++++++++++++++++ OvmfPkg/VirtNorFlashDxe/VirtNorFlash.h | 422 +++++++++ OvmfPkg/VirtNorFlashDxe/VirtNorFlashBlockIoDxe.c | 123 +++ OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.c | 506 ++++++++++ OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.inf | 72 ++ OvmfPkg/VirtNorFlashDxe/VirtNorFlashFvb.c | 777 +++++++++++++++ 6 files changed, 2891 insertions(+) diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c b/OvmfPkg/VirtNorFlashD= xe/VirtNorFlash.c new file mode 100644 index 000000000000..12fa720dad84 --- /dev/null +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c @@ -0,0 +1,991 @@ +/** @file NorFlash.c=0D +=0D + Copyright (c) 2011 - 2020, Arm Limited. All rights reserved.
=0D + Copyright (c) 2020, Linaro, Ltd. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D +#include "VirtNorFlash.h"=0D +=0D +//=0D +// Global variable declarations=0D +//=0D +extern NOR_FLASH_INSTANCE **mNorFlashInstances;=0D +extern UINT32 mNorFlashDeviceCount;=0D +=0D +UINT32=0D +NorFlashReadStatusRegister (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN SR_Address=0D + )=0D +{=0D + // Prepare to read the status register=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_STATUS_RE= GISTER);=0D + return MmioRead32 (Instance->DeviceBaseAddress);=0D +}=0D +=0D +STATIC=0D +BOOLEAN=0D +NorFlashBlockIsLocked (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + )=0D +{=0D + UINT32 LockStatus;=0D +=0D + // Send command for reading device id=0D + SEND_NOR_COMMAND (BlockAddress, 2, P30_CMD_READ_DEVICE_ID);=0D +=0D + // Read block lock status=0D + LockStatus =3D MmioRead32 (CREATE_NOR_ADDRESS (BlockAddress, 2));=0D +=0D + // Decode block lock status=0D + LockStatus =3D FOLD_32BIT_INTO_16BIT (LockStatus);=0D +=0D + if ((LockStatus & 0x2) !=3D 0) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashBlockIsLocked: WARNING: Block LOCKED DOW= N\n"));=0D + }=0D +=0D + return ((LockStatus & 0x1) !=3D 0);=0D +}=0D +=0D +STATIC=0D +EFI_STATUS=0D +NorFlashUnlockSingleBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + )=0D +{=0D + UINT32 LockStatus;=0D +=0D + // Raise the Task Priority Level to TPL_NOTIFY to serialise all its oper= ations=0D + // and to protect shared data structures.=0D +=0D + if (FeaturePcdGet (PcdNorFlashCheckBlockLocked) =3D=3D TRUE) {=0D + do {=0D + // Request a lock setup=0D + SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_LOCK_BLOCK_SETUP);=0D +=0D + // Request an unlock=0D + SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_UNLOCK_BLOCK);=0D +=0D + // Send command for reading device id=0D + SEND_NOR_COMMAND (BlockAddress, 2, P30_CMD_READ_DEVICE_ID);=0D +=0D + // Read block lock status=0D + LockStatus =3D MmioRead32 (CREATE_NOR_ADDRESS (BlockAddress, 2));=0D +=0D + // Decode block lock status=0D + LockStatus =3D FOLD_32BIT_INTO_16BIT (LockStatus);=0D + } while ((LockStatus & 0x1) =3D=3D 1);=0D + } else {=0D + // Request a lock setup=0D + SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_LOCK_BLOCK_SETUP);=0D +=0D + // Request an unlock=0D + SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_UNLOCK_BLOCK);=0D +=0D + // Wait until the status register gives us the all clear=0D + do {=0D + LockStatus =3D NorFlashReadStatusRegister (Instance, BlockAddress);= =0D + } while ((LockStatus & P30_SR_BIT_WRITE) !=3D P30_SR_BIT_WRITE);=0D + }=0D +=0D + // Put device back into Read Array mode=0D + SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_READ_ARRAY);=0D +=0D + DEBUG ((DEBUG_BLKIO, "UnlockSingleBlock: BlockAddress=3D0x%08x\n", Block= Address));=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +EFI_STATUS=0D +NorFlashUnlockSingleBlockIfNecessary (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + )=0D +{=0D + EFI_STATUS Status;=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + if (NorFlashBlockIsLocked (Instance, BlockAddress)) {=0D + Status =3D NorFlashUnlockSingleBlock (Instance, BlockAddress);=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +/**=0D + * The following function presumes that the block has already been unlocke= d.=0D + **/=0D +EFI_STATUS=0D +NorFlashEraseSingleBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINT32 StatusRegister;=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + // Request a block erase and then confirm it=0D + SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_BLOCK_ERASE_SETUP);=0D + SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_BLOCK_ERASE_CONFIRM);=0D +=0D + // Wait until the status register gives us the all clear=0D + do {=0D + StatusRegister =3D NorFlashReadStatusRegister (Instance, BlockAddress)= ;=0D + } while ((StatusRegister & P30_SR_BIT_WRITE) !=3D P30_SR_BIT_WRITE);=0D +=0D + if (StatusRegister & P30_SR_BIT_VPP) {=0D + DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=3D0x%08x: VPP Rang= e Error\n", BlockAddress));=0D + Status =3D EFI_DEVICE_ERROR;=0D + }=0D +=0D + if ((StatusRegister & (P30_SR_BIT_ERASE | P30_SR_BIT_PROGRAM)) =3D=3D (P= 30_SR_BIT_ERASE | P30_SR_BIT_PROGRAM)) {=0D + DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=3D0x%08x: Command = Sequence Error\n", BlockAddress));=0D + Status =3D EFI_DEVICE_ERROR;=0D + }=0D +=0D + if (StatusRegister & P30_SR_BIT_ERASE) {=0D + DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=3D0x%08x: Block Er= ase Error StatusRegister:0x%X\n", BlockAddress, StatusRegister));=0D + Status =3D EFI_DEVICE_ERROR;=0D + }=0D +=0D + if (StatusRegister & P30_SR_BIT_BLOCK_LOCKED) {=0D + // The debug level message has been reduced because a device lock migh= t happen. In this case we just retry it ...=0D + DEBUG ((DEBUG_INFO, "EraseSingleBlock(BlockAddress=3D0x%08x: Block Loc= ked Error\n", BlockAddress));=0D + Status =3D EFI_WRITE_PROTECTED;=0D + }=0D +=0D + if (EFI_ERROR (Status)) {=0D + // Clear the Status Register=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_CLEAR_STATUS= _REGISTER);=0D + }=0D +=0D + // Put device back into Read Array mode=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);=0D +=0D + return Status;=0D +}=0D +=0D +EFI_STATUS=0D +NorFlashWriteSingleWord (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN WordAddress,=0D + IN UINT32 WriteData=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINT32 StatusRegister;=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + // Request a write single word command=0D + SEND_NOR_COMMAND (WordAddress, 0, P30_CMD_WORD_PROGRAM_SETUP);=0D +=0D + // Store the word into NOR Flash;=0D + MmioWrite32 (WordAddress, WriteData);=0D +=0D + // Wait for the write to complete and then check for any errors; i.e. ch= eck the Status Register=0D + do {=0D + // Prepare to read the status register=0D + StatusRegister =3D NorFlashReadStatusRegister (Instance, WordAddress);= =0D + // The chip is busy while the WRITE bit is not asserted=0D + } while ((StatusRegister & P30_SR_BIT_WRITE) !=3D P30_SR_BIT_WRITE);=0D +=0D + // Perform a full status check:=0D + // Mask the relevant bits of Status Register.=0D + // Everything should be zero, if not, we have a problem=0D +=0D + if (StatusRegister & P30_SR_BIT_VPP) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteSingleWord(WordAddress:0x%X): VPP R= ange Error\n", WordAddress));=0D + Status =3D EFI_DEVICE_ERROR;=0D + }=0D +=0D + if (StatusRegister & P30_SR_BIT_PROGRAM) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteSingleWord(WordAddress:0x%X): Progr= am Error\n", WordAddress));=0D + Status =3D EFI_DEVICE_ERROR;=0D + }=0D +=0D + if (StatusRegister & P30_SR_BIT_BLOCK_LOCKED) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteSingleWord(WordAddress:0x%X): Devic= e Protect Error\n", WordAddress));=0D + Status =3D EFI_DEVICE_ERROR;=0D + }=0D +=0D + if (!EFI_ERROR (Status)) {=0D + // Clear the Status Register=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_CLEAR_STATUS= _REGISTER);=0D + }=0D +=0D + // Put device back into Read Array mode=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);=0D +=0D + return Status;=0D +}=0D +=0D +/*=0D + * Writes data to the NOR Flash using the Buffered Programming method.=0D + *=0D + * The maximum size of the on-chip buffer is 32-words, because of hardware= restrictions.=0D + * Therefore this function will only handle buffers up to 32 words or 128 = bytes.=0D + * To deal with larger buffers, call this function again.=0D + *=0D + * This function presumes that both the TargetAddress and the TargetAddres= s+BufferSize=0D + * exist entirely within the NOR Flash. Therefore these conditions will no= t be checked here.=0D + *=0D + * In buffered programming, if the target address not at the beginning of = a 32-bit word boundary,=0D + * then programming time is doubled and power consumption is increased.=0D + * Therefore, it is a requirement to align buffer writes to 32-bit word bo= undaries.=0D + * i.e. the last 4 bits of the target start address must be zero: 0x......= 00=0D + */=0D +EFI_STATUS=0D +NorFlashWriteBuffer (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN TargetAddress,=0D + IN UINTN BufferSizeInBytes,=0D + IN UINT32 *Buffer=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINTN BufferSizeInWords;=0D + UINTN Count;=0D + volatile UINT32 *Data;=0D + UINTN WaitForBuffer;=0D + BOOLEAN BufferAvailable;=0D + UINT32 StatusRegister;=0D +=0D + WaitForBuffer =3D MAX_BUFFERED_PROG_ITERATIONS;=0D + BufferAvailable =3D FALSE;=0D +=0D + // Check that the target address does not cross a 32-word boundary.=0D + if ((TargetAddress & BOUNDARY_OF_32_WORDS) !=3D 0) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // Check there are some data to program=0D + if (BufferSizeInBytes =3D=3D 0) {=0D + return EFI_BUFFER_TOO_SMALL;=0D + }=0D +=0D + // Check that the buffer size does not exceed the maximum hardware buffe= r size on chip.=0D + if (BufferSizeInBytes > P30_MAX_BUFFER_SIZE_IN_BYTES) {=0D + return EFI_BAD_BUFFER_SIZE;=0D + }=0D +=0D + // Check that the buffer size is a multiple of 32-bit words=0D + if ((BufferSizeInBytes % 4) !=3D 0) {=0D + return EFI_BAD_BUFFER_SIZE;=0D + }=0D +=0D + // Pre-programming conditions checked, now start the algorithm.=0D +=0D + // Prepare the data destination address=0D + Data =3D (UINT32 *)TargetAddress;=0D +=0D + // Check the availability of the buffer=0D + do {=0D + // Issue the Buffered Program Setup command=0D + SEND_NOR_COMMAND (TargetAddress, 0, P30_CMD_BUFFERED_PROGRAM_SETUP);=0D +=0D + // Read back the status register bit#7 from the same address=0D + if (((*Data) & P30_SR_BIT_WRITE) =3D=3D P30_SR_BIT_WRITE) {=0D + BufferAvailable =3D TRUE;=0D + }=0D +=0D + // Update the loop counter=0D + WaitForBuffer--;=0D + } while ((WaitForBuffer > 0) && (BufferAvailable =3D=3D FALSE));=0D +=0D + // The buffer was not available for writing=0D + if (WaitForBuffer =3D=3D 0) {=0D + Status =3D EFI_DEVICE_ERROR;=0D + goto EXIT;=0D + }=0D +=0D + // From now on we work in 32-bit words=0D + BufferSizeInWords =3D BufferSizeInBytes / (UINTN)4;=0D +=0D + // Write the word count, which is (buffer_size_in_words - 1),=0D + // because word count 0 means one word.=0D + SEND_NOR_COMMAND (TargetAddress, 0, (BufferSizeInWords - 1));=0D +=0D + // Write the data to the NOR Flash, advancing each address by 4 bytes=0D + for (Count =3D 0; Count < BufferSizeInWords; Count++, Data++, Buffer++) = {=0D + MmioWrite32 ((UINTN)Data, *Buffer);=0D + }=0D +=0D + // Issue the Buffered Program Confirm command, to start the programming = operation=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_BUFFERED_PROGR= AM_CONFIRM);=0D +=0D + // Wait for the write to complete and then check for any errors; i.e. ch= eck the Status Register=0D + do {=0D + StatusRegister =3D NorFlashReadStatusRegister (Instance, TargetAddress= );=0D + // The chip is busy while the WRITE bit is not asserted=0D + } while ((StatusRegister & P30_SR_BIT_WRITE) !=3D P30_SR_BIT_WRITE);=0D +=0D + // Perform a full status check:=0D + // Mask the relevant bits of Status Register.=0D + // Everything should be zero, if not, we have a problem=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + if (StatusRegister & P30_SR_BIT_VPP) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteBuffer(TargetAddress:0x%X): VPP Ran= ge Error\n", TargetAddress));=0D + Status =3D EFI_DEVICE_ERROR;=0D + }=0D +=0D + if (StatusRegister & P30_SR_BIT_PROGRAM) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteBuffer(TargetAddress:0x%X): Program= Error\n", TargetAddress));=0D + Status =3D EFI_DEVICE_ERROR;=0D + }=0D +=0D + if (StatusRegister & P30_SR_BIT_BLOCK_LOCKED) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteBuffer(TargetAddress:0x%X): Device = Protect Error\n", TargetAddress));=0D + Status =3D EFI_DEVICE_ERROR;=0D + }=0D +=0D + if (!EFI_ERROR (Status)) {=0D + // Clear the Status Register=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_CLEAR_STATUS= _REGISTER);=0D + }=0D +=0D +EXIT:=0D + // Put device back into Read Array mode=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);=0D +=0D + return Status;=0D +}=0D +=0D +EFI_STATUS=0D +NorFlashWriteBlocks (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINTN BufferSizeInBytes,=0D + IN VOID *Buffer=0D + )=0D +{=0D + UINT32 *pWriteBuffer;=0D + EFI_STATUS Status;=0D + EFI_LBA CurrentBlock;=0D + UINT32 BlockSizeInWords;=0D + UINT32 NumBlocks;=0D + UINT32 BlockCount;=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + // The buffer must be valid=0D + if (Buffer =3D=3D NULL) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + if (Instance->Media.ReadOnly =3D=3D TRUE) {=0D + return EFI_WRITE_PROTECTED;=0D + }=0D +=0D + // We must have some bytes to read=0D + DEBUG ((DEBUG_BLKIO, "NorFlashWriteBlocks: BufferSizeInBytes=3D0x%x\n", = BufferSizeInBytes));=0D + if (BufferSizeInBytes =3D=3D 0) {=0D + return EFI_BAD_BUFFER_SIZE;=0D + }=0D +=0D + // The size of the buffer must be a multiple of the block size=0D + DEBUG ((DEBUG_BLKIO, "NorFlashWriteBlocks: BlockSize in bytes =3D0x%x\n"= , Instance->Media.BlockSize));=0D + if ((BufferSizeInBytes % Instance->Media.BlockSize) !=3D 0) {=0D + return EFI_BAD_BUFFER_SIZE;=0D + }=0D +=0D + // All blocks must be within the device=0D + NumBlocks =3D ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize;=0D +=0D + DEBUG ((DEBUG_BLKIO, "NorFlashWriteBlocks: NumBlocks=3D%d, LastBlock=3D%= ld, Lba=3D%ld.\n", NumBlocks, Instance->Media.LastBlock, Lba));=0D +=0D + if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteBlocks: ERROR - Write will exceed l= ast block.\n"));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + BlockSizeInWords =3D Instance->Media.BlockSize / 4;=0D +=0D + // Because the target *Buffer is a pointer to VOID, we must put all the = data into a pointer=0D + // to a proper data type, so use *ReadBuffer=0D + pWriteBuffer =3D (UINT32 *)Buffer;=0D +=0D + CurrentBlock =3D Lba;=0D + for (BlockCount =3D 0; BlockCount < NumBlocks; BlockCount++, CurrentBloc= k++, pWriteBuffer =3D pWriteBuffer + BlockSizeInWords) {=0D + DEBUG ((DEBUG_BLKIO, "NorFlashWriteBlocks: Writing block #%d\n", (UINT= N)CurrentBlock));=0D +=0D + Status =3D NorFlashWriteFullBlock (Instance, CurrentBlock, pWriteBuffe= r, BlockSizeInWords);=0D +=0D + if (EFI_ERROR (Status)) {=0D + break;=0D + }=0D + }=0D +=0D + DEBUG ((DEBUG_BLKIO, "NorFlashWriteBlocks: Exit Status =3D \"%r\".\n", S= tatus));=0D + return Status;=0D +}=0D +=0D +#define BOTH_ALIGNED(a, b, align) ((((UINTN)(a) | (UINTN)(b)) & ((align) = - 1)) =3D=3D 0)=0D +=0D +/**=0D + Copy Length bytes from Source to Destination, using aligned accesses onl= y.=0D + Note that this implementation uses memcpy() semantics rather then memmov= e()=0D + semantics, i.e., SourceBuffer and DestinationBuffer should not overlap.= =0D +=0D + @param DestinationBuffer The target of the copy request.=0D + @param SourceBuffer The place to copy from.=0D + @param Length The number of bytes to copy.=0D +=0D + @return Destination=0D +=0D +**/=0D +STATIC=0D +VOID *=0D +AlignedCopyMem (=0D + OUT VOID *DestinationBuffer,=0D + IN CONST VOID *SourceBuffer,=0D + IN UINTN Length=0D + )=0D +{=0D + UINT8 *Destination8;=0D + CONST UINT8 *Source8;=0D + UINT32 *Destination32;=0D + CONST UINT32 *Source32;=0D + UINT64 *Destination64;=0D + CONST UINT64 *Source64;=0D +=0D + if (BOTH_ALIGNED (DestinationBuffer, SourceBuffer, 8) && (Length >=3D 8)= ) {=0D + Destination64 =3D DestinationBuffer;=0D + Source64 =3D SourceBuffer;=0D + while (Length >=3D 8) {=0D + *Destination64++ =3D *Source64++;=0D + Length -=3D 8;=0D + }=0D +=0D + Destination8 =3D (UINT8 *)Destination64;=0D + Source8 =3D (CONST UINT8 *)Source64;=0D + } else if (BOTH_ALIGNED (DestinationBuffer, SourceBuffer, 4) && (Length = >=3D 4)) {=0D + Destination32 =3D DestinationBuffer;=0D + Source32 =3D SourceBuffer;=0D + while (Length >=3D 4) {=0D + *Destination32++ =3D *Source32++;=0D + Length -=3D 4;=0D + }=0D +=0D + Destination8 =3D (UINT8 *)Destination32;=0D + Source8 =3D (CONST UINT8 *)Source32;=0D + } else {=0D + Destination8 =3D DestinationBuffer;=0D + Source8 =3D SourceBuffer;=0D + }=0D +=0D + while (Length-- !=3D 0) {=0D + *Destination8++ =3D *Source8++;=0D + }=0D +=0D + return DestinationBuffer;=0D +}=0D +=0D +EFI_STATUS=0D +NorFlashReadBlocks (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINTN BufferSizeInBytes,=0D + OUT VOID *Buffer=0D + )=0D +{=0D + UINT32 NumBlocks;=0D + UINTN StartAddress;=0D +=0D + DEBUG ((=0D + DEBUG_BLKIO,=0D + "NorFlashReadBlocks: BufferSize=3D0x%xB BlockSize=3D0x%xB LastBlock=3D= %ld, Lba=3D%ld.\n",=0D + BufferSizeInBytes,=0D + Instance->Media.BlockSize,=0D + Instance->Media.LastBlock,=0D + Lba=0D + ));=0D +=0D + // The buffer must be valid=0D + if (Buffer =3D=3D NULL) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // Return if we have not any byte to read=0D + if (BufferSizeInBytes =3D=3D 0) {=0D + return EFI_SUCCESS;=0D + }=0D +=0D + // The size of the buffer must be a multiple of the block size=0D + if ((BufferSizeInBytes % Instance->Media.BlockSize) !=3D 0) {=0D + return EFI_BAD_BUFFER_SIZE;=0D + }=0D +=0D + // All blocks must be within the device=0D + NumBlocks =3D ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize;=0D +=0D + if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashReadBlocks: ERROR - Read will exceed las= t block\n"));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // Get the address to start reading from=0D + StartAddress =3D GET_NOR_BLOCK_ADDRESS (=0D + Instance->RegionBaseAddress,=0D + Lba,=0D + Instance->Media.BlockSize=0D + );=0D +=0D + // Put the device into Read Array mode=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);=0D +=0D + // Readout the data=0D + AlignedCopyMem (Buffer, (VOID *)StartAddress, BufferSizeInBytes);=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +EFI_STATUS=0D +NorFlashRead (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN UINTN BufferSizeInBytes,=0D + OUT VOID *Buffer=0D + )=0D +{=0D + UINTN StartAddress;=0D +=0D + // The buffer must be valid=0D + if (Buffer =3D=3D NULL) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // Return if we have not any byte to read=0D + if (BufferSizeInBytes =3D=3D 0) {=0D + return EFI_SUCCESS;=0D + }=0D +=0D + if (((Lba * Instance->Media.BlockSize) + Offset + BufferSizeInBytes) > I= nstance->Size) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashRead: ERROR - Read will exceed device si= ze.\n"));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // Get the address to start reading from=0D + StartAddress =3D GET_NOR_BLOCK_ADDRESS (=0D + Instance->RegionBaseAddress,=0D + Lba,=0D + Instance->Media.BlockSize=0D + );=0D +=0D + // Put the device into Read Array mode=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);=0D +=0D + // Readout the data=0D + AlignedCopyMem (Buffer, (VOID *)(StartAddress + Offset), BufferSizeInByt= es);=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/*=0D + Write a full or portion of a block. It must not span block boundaries; t= hat is,=0D + Offset + *NumBytes <=3D Instance->Media.BlockSize.=0D +*/=0D +EFI_STATUS=0D +NorFlashWriteSingleBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN OUT UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + )=0D +{=0D + EFI_STATUS TempStatus;=0D + UINT32 Tmp;=0D + UINT32 TmpBuf;=0D + UINT32 WordToWrite;=0D + UINT32 Mask;=0D + BOOLEAN DoErase;=0D + UINTN BytesToWrite;=0D + UINTN CurOffset;=0D + UINTN WordAddr;=0D + UINTN BlockSize;=0D + UINTN BlockAddress;=0D + UINTN PrevBlockAddress;=0D +=0D + PrevBlockAddress =3D 0;=0D +=0D + DEBUG ((DEBUG_BLKIO, "NorFlashWriteSingleBlock(Parameters: Lba=3D%ld, Of= fset=3D0x%x, *NumBytes=3D0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes,= Buffer));=0D +=0D + // Detect WriteDisabled state=0D + if (Instance->Media.ReadOnly =3D=3D TRUE) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteSingleBlock: ERROR - Can not write:= Device is in WriteDisabled state.\n"));=0D + // It is in WriteDisabled state, return an error right away=0D + return EFI_ACCESS_DENIED;=0D + }=0D +=0D + // Cache the block size to avoid de-referencing pointers all the time=0D + BlockSize =3D Instance->Media.BlockSize;=0D +=0D + // The write must not span block boundaries.=0D + // We need to check each variable individually because adding two large = values together overflows.=0D + if ((Offset >=3D BlockSize) ||=0D + (*NumBytes > BlockSize) ||=0D + ((Offset + *NumBytes) > BlockSize))=0D + {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER= _SIZE: (Offset=3D0x%x + NumBytes=3D0x%x) > BlockSize=3D0x%x\n", Offset, *Nu= mBytes, BlockSize));=0D + return EFI_BAD_BUFFER_SIZE;=0D + }=0D +=0D + // We must have some bytes to write=0D + if (*NumBytes =3D=3D 0) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER= _SIZE: (Offset=3D0x%x + NumBytes=3D0x%x) > BlockSize=3D0x%x\n", Offset, *Nu= mBytes, BlockSize));=0D + return EFI_BAD_BUFFER_SIZE;=0D + }=0D +=0D + // Pick 128bytes as a good start for word operations as opposed to erasi= ng the=0D + // block and writing the data regardless if an erase is really needed.=0D + // It looks like most individual NV variable writes are smaller than 128= bytes.=0D + if (*NumBytes <=3D 128) {=0D + // Check to see if we need to erase before programming the data into N= OR.=0D + // If the destination bits are only changing from 1s to 0s we can just= write.=0D + // After a block is erased all bits in the block is set to 1.=0D + // If any byte requires us to erase we just give up and rewrite all of= it.=0D + DoErase =3D FALSE;=0D + BytesToWrite =3D *NumBytes;=0D + CurOffset =3D Offset;=0D +=0D + while (BytesToWrite > 0) {=0D + // Read full word from NOR, splice as required. A word is the smalle= st=0D + // unit we can write.=0D + TempStatus =3D NorFlashRead (Instance, Lba, CurOffset & ~(0x3), size= of (Tmp), &Tmp);=0D + if (EFI_ERROR (TempStatus)) {=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D + // Physical address of word in NOR to write.=0D + WordAddr =3D (CurOffset & ~(0x3)) + GET_NOR_BLOCK_ADDRESS (=0D + Instance->RegionBaseAddress,=0D + Lba,=0D + BlockSize=0D + );=0D + // The word of data that is to be written.=0D + TmpBuf =3D *((UINT32 *)(Buffer + (*NumBytes - BytesToWrite)));=0D +=0D + // First do word aligned chunks.=0D + if ((CurOffset & 0x3) =3D=3D 0) {=0D + if (BytesToWrite >=3D 4) {=0D + // Is the destination still in 'erased' state?=0D + if (~Tmp !=3D 0) {=0D + // Check to see if we are only changing bits to zero.=0D + if ((Tmp ^ TmpBuf) & TmpBuf) {=0D + DoErase =3D TRUE;=0D + break;=0D + }=0D + }=0D +=0D + // Write this word to NOR=0D + WordToWrite =3D TmpBuf;=0D + CurOffset +=3D sizeof (TmpBuf);=0D + BytesToWrite -=3D sizeof (TmpBuf);=0D + } else {=0D + // BytesToWrite < 4. Do small writes and left-overs=0D + Mask =3D ~((~0) << (BytesToWrite * 8));=0D + // Mask out the bytes we want.=0D + TmpBuf &=3D Mask;=0D + // Is the destination still in 'erased' state?=0D + if ((Tmp & Mask) !=3D Mask) {=0D + // Check to see if we are only changing bits to zero.=0D + if ((Tmp ^ TmpBuf) & TmpBuf) {=0D + DoErase =3D TRUE;=0D + break;=0D + }=0D + }=0D +=0D + // Merge old and new data. Write merged word to NOR=0D + WordToWrite =3D (Tmp & ~Mask) | TmpBuf;=0D + CurOffset +=3D BytesToWrite;=0D + BytesToWrite =3D 0;=0D + }=0D + } else {=0D + // Do multiple words, but starting unaligned.=0D + if (BytesToWrite > (4 - (CurOffset & 0x3))) {=0D + Mask =3D ((~0) << ((CurOffset & 0x3) * 8));=0D + // Mask out the bytes we want.=0D + TmpBuf &=3D Mask;=0D + // Is the destination still in 'erased' state?=0D + if ((Tmp & Mask) !=3D Mask) {=0D + // Check to see if we are only changing bits to zero.=0D + if ((Tmp ^ TmpBuf) & TmpBuf) {=0D + DoErase =3D TRUE;=0D + break;=0D + }=0D + }=0D +=0D + // Merge old and new data. Write merged word to NOR=0D + WordToWrite =3D (Tmp & ~Mask) | TmpBuf;=0D + BytesToWrite -=3D (4 - (CurOffset & 0x3));=0D + CurOffset +=3D (4 - (CurOffset & 0x3));=0D + } else {=0D + // Unaligned and fits in one word.=0D + Mask =3D (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) *= 8);=0D + // Mask out the bytes we want.=0D + TmpBuf =3D (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask;=0D + // Is the destination still in 'erased' state?=0D + if ((Tmp & Mask) !=3D Mask) {=0D + // Check to see if we are only changing bits to zero.=0D + if ((Tmp ^ TmpBuf) & TmpBuf) {=0D + DoErase =3D TRUE;=0D + break;=0D + }=0D + }=0D +=0D + // Merge old and new data. Write merged word to NOR=0D + WordToWrite =3D (Tmp & ~Mask) | TmpBuf;=0D + CurOffset +=3D BytesToWrite;=0D + BytesToWrite =3D 0;=0D + }=0D + }=0D +=0D + //=0D + // Write the word to NOR.=0D + //=0D +=0D + BlockAddress =3D GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress,= Lba, BlockSize);=0D + if (BlockAddress !=3D PrevBlockAddress) {=0D + TempStatus =3D NorFlashUnlockSingleBlockIfNecessary (Instance, Blo= ckAddress);=0D + if (EFI_ERROR (TempStatus)) {=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D + PrevBlockAddress =3D BlockAddress;=0D + }=0D +=0D + TempStatus =3D NorFlashWriteSingleWord (Instance, WordAddr, WordToWr= ite);=0D + if (EFI_ERROR (TempStatus)) {=0D + return EFI_DEVICE_ERROR;=0D + }=0D + }=0D +=0D + // Exit if we got here and could write all the data. Otherwise do the= =0D + // Erase-Write cycle.=0D + if (!DoErase) {=0D + return EFI_SUCCESS;=0D + }=0D + }=0D +=0D + // Check we did get some memory. Buffer is BlockSize.=0D + if (Instance->ShadowBuffer =3D=3D NULL) {=0D + DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D + // Read NOR Flash data into shadow buffer=0D + TempStatus =3D NorFlashReadBlocks (Instance, Lba, BlockSize, Instance->S= hadowBuffer);=0D + if (EFI_ERROR (TempStatus)) {=0D + // Return one of the pre-approved error statuses=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D + // Put the data at the appropriate location inside the buffer area=0D + CopyMem ((VOID *)((UINTN)Instance->ShadowBuffer + Offset), Buffer, *NumB= ytes);=0D +=0D + // Write the modified buffer back to the NorFlash=0D + TempStatus =3D NorFlashWriteBlocks (Instance, Lba, BlockSize, Instance->= ShadowBuffer);=0D + if (EFI_ERROR (TempStatus)) {=0D + // Return one of the pre-approved error statuses=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/*=0D + Although DiskIoDxe will automatically install the DiskIO protocol whenev= er=0D + we install the BlockIO protocol, its implementation is sub-optimal as it= reads=0D + and writes entire blocks using the BlockIO protocol. In fact we can acce= ss=0D + NOR flash with a finer granularity than that, so we can improve performa= nce=0D + by directly producing the DiskIO protocol.=0D +*/=0D +=0D +/**=0D + Read BufferSize bytes from Offset into Buffer.=0D +=0D + @param This Protocol instance pointer.=0D + @param MediaId Id of the media, changes every time the me= dia is replaced.=0D + @param Offset The starting byte offset to read from=0D + @param BufferSize Size of Buffer=0D + @param Buffer Buffer containing read data=0D +=0D + @retval EFI_SUCCESS The data was read correctly from the devic= e.=0D + @retval EFI_DEVICE_ERROR The device reported an error while perform= ing the read.=0D + @retval EFI_NO_MEDIA There is no media in the device.=0D + @retval EFI_MEDIA_CHANGED The MediaId does not match the current dev= ice.=0D + @retval EFI_INVALID_PARAMETER The read request contains device addresses= that are not=0D + valid for the device.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashDiskIoReadDisk (=0D + IN EFI_DISK_IO_PROTOCOL *This,=0D + IN UINT32 MediaId,=0D + IN UINT64 DiskOffset,=0D + IN UINTN BufferSize,=0D + OUT VOID *Buffer=0D + )=0D +{=0D + NOR_FLASH_INSTANCE *Instance;=0D + UINT32 BlockSize;=0D + UINT32 BlockOffset;=0D + EFI_LBA Lba;=0D +=0D + Instance =3D INSTANCE_FROM_DISKIO_THIS (This);=0D +=0D + if (MediaId !=3D Instance->Media.MediaId) {=0D + return EFI_MEDIA_CHANGED;=0D + }=0D +=0D + BlockSize =3D Instance->Media.BlockSize;=0D + Lba =3D (EFI_LBA)DivU64x32Remainder (DiskOffset, BlockSize, &Block= Offset);=0D +=0D + return NorFlashRead (Instance, Lba, BlockOffset, BufferSize, Buffer);=0D +}=0D +=0D +/**=0D + Writes a specified number of bytes to a device.=0D +=0D + @param This Indicates a pointer to the calling context.=0D + @param MediaId ID of the medium to be written.=0D + @param Offset The starting byte offset on the logical block I/O dev= ice to write.=0D + @param BufferSize The size in bytes of Buffer. The number of bytes to w= rite to the device.=0D + @param Buffer A pointer to the buffer containing the data to be wri= tten.=0D +=0D + @retval EFI_SUCCESS The data was written correctly to the devi= ce.=0D + @retval EFI_WRITE_PROTECTED The device can not be written to.=0D + @retval EFI_DEVICE_ERROR The device reported an error while perform= ing the write.=0D + @retval EFI_NO_MEDIA There is no media in the device.=0D + @retval EFI_MEDIA_CHANGED The MediaId does not match the current dev= ice.=0D + @retval EFI_INVALID_PARAMETER The write request contains device addresse= s that are not=0D + valid for the device.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashDiskIoWriteDisk (=0D + IN EFI_DISK_IO_PROTOCOL *This,=0D + IN UINT32 MediaId,=0D + IN UINT64 DiskOffset,=0D + IN UINTN BufferSize,=0D + IN VOID *Buffer=0D + )=0D +{=0D + NOR_FLASH_INSTANCE *Instance;=0D + UINT32 BlockSize;=0D + UINT32 BlockOffset;=0D + EFI_LBA Lba;=0D + UINTN RemainingBytes;=0D + UINTN WriteSize;=0D + EFI_STATUS Status;=0D +=0D + Instance =3D INSTANCE_FROM_DISKIO_THIS (This);=0D +=0D + if (MediaId !=3D Instance->Media.MediaId) {=0D + return EFI_MEDIA_CHANGED;=0D + }=0D +=0D + BlockSize =3D Instance->Media.BlockSize;=0D + Lba =3D (EFI_LBA)DivU64x32Remainder (DiskOffset, BlockSize, &Block= Offset);=0D +=0D + RemainingBytes =3D BufferSize;=0D +=0D + // Write either all the remaining bytes, or the number of bytes that bri= ng=0D + // us up to a block boundary, whichever is less.=0D + // (DiskOffset | (BlockSize - 1)) + 1) rounds DiskOffset up to the next= =0D + // block boundary (even if it is already on one).=0D + WriteSize =3D MIN (RemainingBytes, ((DiskOffset | (BlockSize - 1)) + 1) = - DiskOffset);=0D +=0D + do {=0D + if (WriteSize =3D=3D BlockSize) {=0D + // Write a full block=0D + Status =3D NorFlashWriteFullBlock (Instance, Lba, Buffer, BlockSize = / sizeof (UINT32));=0D + } else {=0D + // Write a partial block=0D + Status =3D NorFlashWriteSingleBlock (Instance, Lba, BlockOffset, &Wr= iteSize, Buffer);=0D + }=0D +=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D + // Now continue writing either all the remaining bytes or single block= s.=0D + RemainingBytes -=3D WriteSize;=0D + Buffer =3D (UINT8 *)Buffer + WriteSize;=0D + Lba++;=0D + BlockOffset =3D 0;=0D + WriteSize =3D MIN (RemainingBytes, BlockSize);=0D + } while (RemainingBytes);=0D +=0D + return Status;=0D +}=0D +=0D +EFI_STATUS=0D +NorFlashReset (=0D + IN NOR_FLASH_INSTANCE *Instance=0D + )=0D +{=0D + // As there is no specific RESET to perform, ensure that the devices is = in the default Read Array mode=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Fixup internal data so that EFI can be call in virtual mode.=0D + Call the passed in Child Notify event and convert any pointers in=0D + lib to virtual mode.=0D +=0D + @param[in] Event The Event that is being processed=0D + @param[in] Context Event Context=0D +**/=0D +VOID=0D +EFIAPI=0D +NorFlashVirtualNotifyEvent (=0D + IN EFI_EVENT Event,=0D + IN VOID *Context=0D + )=0D +{=0D + UINTN Index;=0D +=0D + for (Index =3D 0; Index < mNorFlashDeviceCount; Index++) {=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->DeviceBas= eAddress);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->RegionBas= eAddress);=0D +=0D + // Convert BlockIo protocol=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->BlockIoPr= otocol.FlushBlocks);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->BlockIoPr= otocol.ReadBlocks);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->BlockIoPr= otocol.Reset);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->BlockIoPr= otocol.WriteBlocks);=0D +=0D + // Convert Fvb=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtoc= ol.EraseBlocks);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtoc= ol.GetAttributes);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtoc= ol.GetBlockSize);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtoc= ol.GetPhysicalAddress);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtoc= ol.Read);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtoc= ol.SetAttributes);=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtoc= ol.Write);=0D +=0D + if (mNorFlashInstances[Index]->ShadowBuffer !=3D NULL) {=0D + EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->ShadowB= uffer);=0D + }=0D + }=0D +=0D + return;=0D +}=0D diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.h b/OvmfPkg/VirtNorFlashD= xe/VirtNorFlash.h new file mode 100644 index 000000000000..c1e5f2ca3131 --- /dev/null +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.h @@ -0,0 +1,422 @@ +/** @file NorFlash.h=0D +=0D + Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef __NOR_FLASH_H__=0D +#define __NOR_FLASH_H__=0D +=0D +#include =0D +#include =0D +=0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#define NOR_FLASH_ERASE_RETRY 10=0D +=0D +// Device access macros=0D +// These are necessary because we use 2 x 16bit parts to make up 32bit dat= a=0D +=0D +#define HIGH_16_BITS 0xFFFF0000=0D +#define LOW_16_BITS 0x0000FFFF=0D +#define LOW_8_BITS 0x000000FF=0D +=0D +#define FOLD_32BIT_INTO_16BIT(value) ( ( value >> 16 ) | ( value & LOW_16= _BITS ) )=0D +=0D +#define GET_LOW_BYTE(value) ( value & LOW_8_BITS )=0D +#define GET_HIGH_BYTE(value) ( GET_LOW_BYTE( value >> 16 ) )=0D +=0D +// Each command must be sent simultaneously to both chips,=0D +// i.e. at the lower 16 bits AND at the higher 16 bits=0D +#define CREATE_NOR_ADDRESS(BaseAddr, OffsetAddr) ((BaseAddr) + ((Off= setAddr) << 2))=0D +#define CREATE_DUAL_CMD(Cmd) ( ( Cmd << 16) | ( = Cmd & LOW_16_BITS) )=0D +#define SEND_NOR_COMMAND(BaseAddr, Offset, Cmd) MmioWrite32 (CREATE= _NOR_ADDRESS(BaseAddr,Offset), CREATE_DUAL_CMD(Cmd))=0D +#define GET_NOR_BLOCK_ADDRESS(BaseAddr, Lba, LbaSize) ( BaseAddr + (UINTN= )((Lba) * LbaSize) )=0D +=0D +// Status Register Bits=0D +#define P30_SR_BIT_WRITE (BIT7 << 16 | BIT7)=0D +#define P30_SR_BIT_ERASE_SUSPEND (BIT6 << 16 | BIT6)=0D +#define P30_SR_BIT_ERASE (BIT5 << 16 | BIT5)=0D +#define P30_SR_BIT_PROGRAM (BIT4 << 16 | BIT4)=0D +#define P30_SR_BIT_VPP (BIT3 << 16 | BIT3)=0D +#define P30_SR_BIT_PROGRAM_SUSPEND (BIT2 << 16 | BIT2)=0D +#define P30_SR_BIT_BLOCK_LOCKED (BIT1 << 16 | BIT1)=0D +#define P30_SR_BIT_BEFP (BIT0 << 16 | BIT0)=0D +=0D +// Device Commands for Intel StrataFlash(R) Embedded Memory (P30) Family=0D +=0D +// On chip buffer size for buffered programming operations=0D +// There are 2 chips, each chip can buffer up to 32 (16-bit)words, and eac= h word is 2 bytes.=0D +// Therefore the total size of the buffer is 2 x 32 x 2 =3D 128 bytes=0D +#define P30_MAX_BUFFER_SIZE_IN_BYTES ((UINTN)128)=0D +#define P30_MAX_BUFFER_SIZE_IN_WORDS (P30_MAX_BUFFER_SIZE_IN_BYTES/((UINT= N)4))=0D +#define MAX_BUFFERED_PROG_ITERATIONS 10000000=0D +#define BOUNDARY_OF_32_WORDS 0x7F=0D +=0D +// CFI Addresses=0D +#define P30_CFI_ADDR_QUERY_UNIQUE_QRY 0x10=0D +#define P30_CFI_ADDR_VENDOR_ID 0x13=0D +=0D +// CFI Data=0D +#define CFI_QRY 0x00595251=0D +=0D +// READ Commands=0D +#define P30_CMD_READ_DEVICE_ID 0x0090=0D +#define P30_CMD_READ_STATUS_REGISTER 0x0070=0D +#define P30_CMD_CLEAR_STATUS_REGISTER 0x0050=0D +#define P30_CMD_READ_ARRAY 0x00FF=0D +#define P30_CMD_READ_CFI_QUERY 0x0098=0D +=0D +// WRITE Commands=0D +#define P30_CMD_WORD_PROGRAM_SETUP 0x0040=0D +#define P30_CMD_ALTERNATE_WORD_PROGRAM_SETUP 0x0010=0D +#define P30_CMD_BUFFERED_PROGRAM_SETUP 0x00E8=0D +#define P30_CMD_BUFFERED_PROGRAM_CONFIRM 0x00D0=0D +#define P30_CMD_BEFP_SETUP 0x0080=0D +#define P30_CMD_BEFP_CONFIRM 0x00D0=0D +=0D +// ERASE Commands=0D +#define P30_CMD_BLOCK_ERASE_SETUP 0x0020=0D +#define P30_CMD_BLOCK_ERASE_CONFIRM 0x00D0=0D +=0D +// SUSPEND Commands=0D +#define P30_CMD_PROGRAM_OR_ERASE_SUSPEND 0x00B0=0D +#define P30_CMD_SUSPEND_RESUME 0x00D0=0D +=0D +// BLOCK LOCKING / UNLOCKING Commands=0D +#define P30_CMD_LOCK_BLOCK_SETUP 0x0060=0D +#define P30_CMD_LOCK_BLOCK 0x0001=0D +#define P30_CMD_UNLOCK_BLOCK 0x00D0=0D +#define P30_CMD_LOCK_DOWN_BLOCK 0x002F=0D +=0D +// PROTECTION Commands=0D +#define P30_CMD_PROGRAM_PROTECTION_REGISTER_SETUP 0x00C0=0D +=0D +// CONFIGURATION Commands=0D +#define P30_CMD_READ_CONFIGURATION_REGISTER_SETUP 0x0060=0D +#define P30_CMD_READ_CONFIGURATION_REGISTER 0x0003=0D +=0D +#define NOR_FLASH_SIGNATURE SIGNATURE_32('n', 'o', 'r', '0')=0D +#define INSTANCE_FROM_FVB_THIS(a) CR(a, NOR_FLASH_INSTANCE, FvbProtoco= l, NOR_FLASH_SIGNATURE)=0D +#define INSTANCE_FROM_BLKIO_THIS(a) CR(a, NOR_FLASH_INSTANCE, BlockIoPro= tocol, NOR_FLASH_SIGNATURE)=0D +#define INSTANCE_FROM_DISKIO_THIS(a) CR(a, NOR_FLASH_INSTANCE, DiskIoProt= ocol, NOR_FLASH_SIGNATURE)=0D +=0D +typedef struct _NOR_FLASH_INSTANCE NOR_FLASH_INSTANCE;=0D +=0D +#pragma pack (1)=0D +typedef struct {=0D + VENDOR_DEVICE_PATH Vendor;=0D + UINT8 Index;=0D + EFI_DEVICE_PATH_PROTOCOL End;=0D +} NOR_FLASH_DEVICE_PATH;=0D +#pragma pack ()=0D +=0D +struct _NOR_FLASH_INSTANCE {=0D + UINT32 Signature;=0D + EFI_HANDLE Handle;=0D +=0D + UINTN DeviceBaseAddress;=0D + UINTN RegionBaseAddress;=0D + UINTN Size;=0D + EFI_LBA StartLba;=0D +=0D + EFI_BLOCK_IO_PROTOCOL BlockIoProtocol;=0D + EFI_BLOCK_IO_MEDIA Media;=0D + EFI_DISK_IO_PROTOCOL DiskIoProtocol;=0D +=0D + EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL FvbProtocol;=0D + VOID *ShadowBuffer;=0D +=0D + NOR_FLASH_DEVICE_PATH DevicePath;=0D +};=0D +=0D +EFI_STATUS=0D +NorFlashReadCfiData (=0D + IN UINTN DeviceBaseAddress,=0D + IN UINTN CFI_Offset,=0D + IN UINT32 NumberOfBytes,=0D + OUT UINT32 *Data=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashWriteBuffer (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN TargetAddress,=0D + IN UINTN BufferSizeInBytes,=0D + IN UINT32 *Buffer=0D + );=0D +=0D +//=0D +// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.Reset=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashBlockIoReset (=0D + IN EFI_BLOCK_IO_PROTOCOL *This,=0D + IN BOOLEAN ExtendedVerification=0D + );=0D +=0D +//=0D +// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.ReadBlocks=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashBlockIoReadBlocks (=0D + IN EFI_BLOCK_IO_PROTOCOL *This,=0D + IN UINT32 MediaId,=0D + IN EFI_LBA Lba,=0D + IN UINTN BufferSizeInBytes,=0D + OUT VOID *Buffer=0D + );=0D +=0D +//=0D +// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.WriteBlocks=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashBlockIoWriteBlocks (=0D + IN EFI_BLOCK_IO_PROTOCOL *This,=0D + IN UINT32 MediaId,=0D + IN EFI_LBA Lba,=0D + IN UINTN BufferSizeInBytes,=0D + IN VOID *Buffer=0D + );=0D +=0D +//=0D +// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.FlushBlocks=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashBlockIoFlushBlocks (=0D + IN EFI_BLOCK_IO_PROTOCOL *This=0D + );=0D +=0D +//=0D +// DiskIO Protocol function EFI_DISK_IO_PROTOCOL.ReadDisk=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashDiskIoReadDisk (=0D + IN EFI_DISK_IO_PROTOCOL *This,=0D + IN UINT32 MediaId,=0D + IN UINT64 Offset,=0D + IN UINTN BufferSize,=0D + OUT VOID *Buffer=0D + );=0D +=0D +//=0D +// DiskIO Protocol function EFI_DISK_IO_PROTOCOL.WriteDisk=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashDiskIoWriteDisk (=0D + IN EFI_DISK_IO_PROTOCOL *This,=0D + IN UINT32 MediaId,=0D + IN UINT64 Offset,=0D + IN UINTN BufferSize,=0D + IN VOID *Buffer=0D + );=0D +=0D +//=0D +// NorFlashFvbDxe.c=0D +//=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +FvbGetAttributes (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +FvbSetAttributes (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +FvbGetPhysicalAddress (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + OUT EFI_PHYSICAL_ADDRESS *Address=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +FvbGetBlockSize (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + IN EFI_LBA Lba,=0D + OUT UINTN *BlockSize,=0D + OUT UINTN *NumberOfBlocks=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +FvbRead (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN OUT UINTN *NumBytes,=0D + IN OUT UINT8 *Buffer=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +FvbWrite (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN OUT UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +FvbEraseBlocks (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + ...=0D + );=0D +=0D +EFI_STATUS=0D +ValidateFvHeader (=0D + IN NOR_FLASH_INSTANCE *Instance=0D + );=0D +=0D +EFI_STATUS=0D +InitializeFvAndVariableStoreHeaders (=0D + IN NOR_FLASH_INSTANCE *Instance=0D + );=0D +=0D +VOID=0D +EFIAPI=0D +FvbVirtualNotifyEvent (=0D + IN EFI_EVENT Event,=0D + IN VOID *Context=0D + );=0D +=0D +//=0D +// NorFlashDxe.c=0D +//=0D +=0D +EFI_STATUS=0D +NorFlashWriteFullBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINT32 *DataBuffer,=0D + IN UINT32 BlockSizeInWords=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashUnlockAndEraseSingleBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashCreateInstance (=0D + IN UINTN NorFlashDeviceBase,=0D + IN UINTN NorFlashRegionBase,=0D + IN UINTN NorFlashSize,=0D + IN UINT32 Index,=0D + IN UINT32 BlockSize,=0D + IN BOOLEAN SupportFvb,=0D + OUT NOR_FLASH_INSTANCE **NorFlashInstance=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashFvbInitialize (=0D + IN NOR_FLASH_INSTANCE *Instance=0D + );=0D +=0D +//=0D +// NorFlash.c=0D +//=0D +EFI_STATUS=0D +NorFlashWriteSingleBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN OUT UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashWriteBlocks (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINTN BufferSizeInBytes,=0D + IN VOID *Buffer=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashReadBlocks (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINTN BufferSizeInBytes,=0D + OUT VOID *Buffer=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashRead (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN UINTN BufferSizeInBytes,=0D + OUT VOID *Buffer=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashWrite (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN OUT UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashReset (=0D + IN NOR_FLASH_INSTANCE *Instance=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashEraseSingleBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashUnlockSingleBlockIfNecessary (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + );=0D +=0D +EFI_STATUS=0D +NorFlashWriteSingleWord (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN WordAddress,=0D + IN UINT32 WriteData=0D + );=0D +=0D +VOID=0D +EFIAPI=0D +NorFlashVirtualNotifyEvent (=0D + IN EFI_EVENT Event,=0D + IN VOID *Context=0D + );=0D +=0D +#endif /* __NOR_FLASH_H__ */=0D diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlashBlockIoDxe.c b/OvmfPkg/Vir= tNorFlashDxe/VirtNorFlashBlockIoDxe.c new file mode 100644 index 000000000000..ecf152e35539 --- /dev/null +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlashBlockIoDxe.c @@ -0,0 +1,123 @@ +/** @file NorFlashBlockIoDxe.c=0D +=0D + Copyright (c) 2011-2013, ARM Ltd. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +#include =0D +=0D +#include "VirtNorFlash.h"=0D +=0D +//=0D +// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.Reset=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashBlockIoReset (=0D + IN EFI_BLOCK_IO_PROTOCOL *This,=0D + IN BOOLEAN ExtendedVerification=0D + )=0D +{=0D + NOR_FLASH_INSTANCE *Instance;=0D +=0D + Instance =3D INSTANCE_FROM_BLKIO_THIS (This);=0D +=0D + DEBUG ((DEBUG_BLKIO, "NorFlashBlockIoReset(MediaId=3D0x%x)\n", This->Med= ia->MediaId));=0D +=0D + return NorFlashReset (Instance);=0D +}=0D +=0D +//=0D +// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.ReadBlocks=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashBlockIoReadBlocks (=0D + IN EFI_BLOCK_IO_PROTOCOL *This,=0D + IN UINT32 MediaId,=0D + IN EFI_LBA Lba,=0D + IN UINTN BufferSizeInBytes,=0D + OUT VOID *Buffer=0D + )=0D +{=0D + NOR_FLASH_INSTANCE *Instance;=0D + EFI_STATUS Status;=0D + EFI_BLOCK_IO_MEDIA *Media;=0D +=0D + if (This =3D=3D NULL) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + Instance =3D INSTANCE_FROM_BLKIO_THIS (This);=0D + Media =3D This->Media;=0D +=0D + DEBUG ((DEBUG_BLKIO, "NorFlashBlockIoReadBlocks(MediaId=3D0x%x, Lba=3D%l= d, BufferSize=3D0x%x bytes (%d kB), BufferPtr @ 0x%08x)\n", MediaId, Lba, B= ufferSizeInBytes, BufferSizeInBytes, Buffer));=0D +=0D + if (!Media) {=0D + Status =3D EFI_INVALID_PARAMETER;=0D + } else if (!Media->MediaPresent) {=0D + Status =3D EFI_NO_MEDIA;=0D + } else if (Media->MediaId !=3D MediaId) {=0D + Status =3D EFI_MEDIA_CHANGED;=0D + } else if ((Media->IoAlign > 2) && (((UINTN)Buffer & (Media->IoAlign - 1= )) !=3D 0)) {=0D + Status =3D EFI_INVALID_PARAMETER;=0D + } else {=0D + Status =3D NorFlashReadBlocks (Instance, Lba, BufferSizeInBytes, Buffe= r);=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +//=0D +// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.WriteBlocks=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashBlockIoWriteBlocks (=0D + IN EFI_BLOCK_IO_PROTOCOL *This,=0D + IN UINT32 MediaId,=0D + IN EFI_LBA Lba,=0D + IN UINTN BufferSizeInBytes,=0D + IN VOID *Buffer=0D + )=0D +{=0D + NOR_FLASH_INSTANCE *Instance;=0D + EFI_STATUS Status;=0D +=0D + Instance =3D INSTANCE_FROM_BLKIO_THIS (This);=0D +=0D + DEBUG ((DEBUG_BLKIO, "NorFlashBlockIoWriteBlocks(MediaId=3D0x%x, Lba=3D%= ld, BufferSize=3D0x%x bytes, BufferPtr @ 0x%08x)\n", MediaId, Lba, BufferSi= zeInBytes, Buffer));=0D +=0D + if ( !This->Media->MediaPresent ) {=0D + Status =3D EFI_NO_MEDIA;=0D + } else if ( This->Media->MediaId !=3D MediaId ) {=0D + Status =3D EFI_MEDIA_CHANGED;=0D + } else if ( This->Media->ReadOnly ) {=0D + Status =3D EFI_WRITE_PROTECTED;=0D + } else {=0D + Status =3D NorFlashWriteBlocks (Instance, Lba, BufferSizeInBytes, Buff= er);=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +//=0D +// BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.FlushBlocks=0D +//=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashBlockIoFlushBlocks (=0D + IN EFI_BLOCK_IO_PROTOCOL *This=0D + )=0D +{=0D + // No Flush required for the NOR Flash driver=0D + // because cache operations are not permitted.=0D +=0D + DEBUG ((DEBUG_BLKIO, "NorFlashBlockIoFlushBlocks: Function NOT IMPLEMENT= ED (not required).\n"));=0D +=0D + // Nothing to do so just return without error=0D + return EFI_SUCCESS;=0D +}=0D diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.c b/OvmfPkg/VirtNorFla= shDxe/VirtNorFlashDxe.c new file mode 100644 index 000000000000..819425545e46 --- /dev/null +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.c @@ -0,0 +1,506 @@ +/** @file NorFlashDxe.c=0D +=0D + Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include "VirtNorFlash.h"=0D +=0D +STATIC EFI_EVENT mNorFlashVirtualAddrChangeEvent;=0D +=0D +//=0D +// Global variable declarations=0D +//=0D +NOR_FLASH_INSTANCE **mNorFlashInstances;=0D +UINT32 mNorFlashDeviceCount;=0D +UINTN mFlashNvStorageVariableBase;=0D +EFI_EVENT mFvbVirtualAddrChangeEvent;=0D +=0D +NOR_FLASH_INSTANCE mNorFlashInstanceTemplate =3D {=0D + NOR_FLASH_SIGNATURE, // Signature=0D + NULL, // Handle ... NEED TO BE FILLED=0D +=0D + 0, // DeviceBaseAddress ... NEED TO BE FILLED=0D + 0, // RegionBaseAddress ... NEED TO BE FILLED=0D + 0, // Size ... NEED TO BE FILLED=0D + 0, // StartLba=0D +=0D + {=0D + EFI_BLOCK_IO_PROTOCOL_REVISION2, // Revision=0D + NULL, // Media ... NEED TO BE FILLED=0D + NorFlashBlockIoReset, // Reset;=0D + NorFlashBlockIoReadBlocks, // ReadBlocks=0D + NorFlashBlockIoWriteBlocks, // WriteBlocks=0D + NorFlashBlockIoFlushBlocks // FlushBlocks=0D + }, // BlockIoProtocol=0D +=0D + {=0D + 0, // MediaId ... NEED TO BE FILLED=0D + FALSE, // RemovableMedia=0D + TRUE, // MediaPresent=0D + FALSE, // LogicalPartition=0D + FALSE, // ReadOnly=0D + FALSE, // WriteCaching;=0D + 0, // BlockSize ... NEED TO BE FILLED=0D + 4, // IoAlign=0D + 0, // LastBlock ... NEED TO BE FILLED=0D + 0, // LowestAlignedLba=0D + 1, // LogicalBlocksPerPhysicalBlock=0D + }, // Media;=0D +=0D + {=0D + EFI_DISK_IO_PROTOCOL_REVISION, // Revision=0D + NorFlashDiskIoReadDisk, // ReadDisk=0D + NorFlashDiskIoWriteDisk // WriteDisk=0D + },=0D +=0D + {=0D + FvbGetAttributes, // GetAttributes=0D + FvbSetAttributes, // SetAttributes=0D + FvbGetPhysicalAddress, // GetPhysicalAddress=0D + FvbGetBlockSize, // GetBlockSize=0D + FvbRead, // Read=0D + FvbWrite, // Write=0D + FvbEraseBlocks, // EraseBlocks=0D + NULL, // ParentHandle=0D + }, // FvbProtoccol;=0D + NULL, // ShadowBuffer=0D + {=0D + {=0D + {=0D + HARDWARE_DEVICE_PATH,=0D + HW_VENDOR_DP,=0D + {=0D + (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End)),=0D + (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End) >> 8)=0D + }=0D + },=0D + { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0,= 0x0, 0x0, 0x0, 0x0 }=0D + }, // GU= ID ... NEED TO BE FILLED=0D + },=0D + 0, // Index=0D + {=0D + END_DEVICE_PATH_TYPE,=0D + END_ENTIRE_DEVICE_PATH_SUBTYPE,=0D + { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }=0D + }=0D + } // DevicePath=0D +};=0D +=0D +EFI_STATUS=0D +NorFlashCreateInstance (=0D + IN UINTN NorFlashDeviceBase,=0D + IN UINTN NorFlashRegionBase,=0D + IN UINTN NorFlashSize,=0D + IN UINT32 Index,=0D + IN UINT32 BlockSize,=0D + IN BOOLEAN SupportFvb,=0D + OUT NOR_FLASH_INSTANCE **NorFlashInstance=0D + )=0D +{=0D + EFI_STATUS Status;=0D + NOR_FLASH_INSTANCE *Instance;=0D +=0D + ASSERT (NorFlashInstance !=3D NULL);=0D +=0D + Instance =3D AllocateRuntimeCopyPool (sizeof (NOR_FLASH_INSTANCE), &mNor= FlashInstanceTemplate);=0D + if (Instance =3D=3D NULL) {=0D + return EFI_OUT_OF_RESOURCES;=0D + }=0D +=0D + Instance->DeviceBaseAddress =3D NorFlashDeviceBase;=0D + Instance->RegionBaseAddress =3D NorFlashRegionBase;=0D + Instance->Size =3D NorFlashSize;=0D +=0D + Instance->BlockIoProtocol.Media =3D &Instance->Media;=0D + Instance->Media.MediaId =3D Index;=0D + Instance->Media.BlockSize =3D BlockSize;=0D + Instance->Media.LastBlock =3D (NorFlashSize / BlockSize)-1;=0D +=0D + CopyGuid (&Instance->DevicePath.Vendor.Guid, &gEfiCallerIdGuid);=0D + Instance->DevicePath.Index =3D (UINT8)Index;=0D +=0D + Instance->ShadowBuffer =3D AllocateRuntimePool (BlockSize);=0D + if (Instance->ShadowBuffer =3D=3D NULL) {=0D + return EFI_OUT_OF_RESOURCES;=0D + }=0D +=0D + if (SupportFvb) {=0D + NorFlashFvbInitialize (Instance);=0D +=0D + Status =3D gBS->InstallMultipleProtocolInterfaces (=0D + &Instance->Handle,=0D + &gEfiDevicePathProtocolGuid,=0D + &Instance->DevicePath,=0D + &gEfiBlockIoProtocolGuid,=0D + &Instance->BlockIoProtocol,=0D + &gEfiFirmwareVolumeBlockProtocolGuid,=0D + &Instance->FvbProtocol,=0D + NULL=0D + );=0D + if (EFI_ERROR (Status)) {=0D + FreePool (Instance);=0D + return Status;=0D + }=0D + } else {=0D + Status =3D gBS->InstallMultipleProtocolInterfaces (=0D + &Instance->Handle,=0D + &gEfiDevicePathProtocolGuid,=0D + &Instance->DevicePath,=0D + &gEfiBlockIoProtocolGuid,=0D + &Instance->BlockIoProtocol,=0D + &gEfiDiskIoProtocolGuid,=0D + &Instance->DiskIoProtocol,=0D + NULL=0D + );=0D + if (EFI_ERROR (Status)) {=0D + FreePool (Instance);=0D + return Status;=0D + }=0D + }=0D +=0D + *NorFlashInstance =3D Instance;=0D + return Status;=0D +}=0D +=0D +/**=0D + * This function unlock and erase an entire NOR Flash block.=0D + **/=0D +EFI_STATUS=0D +NorFlashUnlockAndEraseSingleBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN UINTN BlockAddress=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINTN Index;=0D + EFI_TPL OriginalTPL;=0D +=0D + if (!EfiAtRuntime ()) {=0D + // Raise TPL to TPL_HIGH to stop anyone from interrupting us.=0D + OriginalTPL =3D gBS->RaiseTPL (TPL_HIGH_LEVEL);=0D + } else {=0D + // This initialization is only to prevent the compiler to complain abo= ut the=0D + // use of uninitialized variables=0D + OriginalTPL =3D TPL_HIGH_LEVEL;=0D + }=0D +=0D + Index =3D 0;=0D + // The block erase might fail a first time (SW bug ?). Retry it ...=0D + do {=0D + // Unlock the block if we have to=0D + Status =3D NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddres= s);=0D + if (EFI_ERROR (Status)) {=0D + break;=0D + }=0D +=0D + Status =3D NorFlashEraseSingleBlock (Instance, BlockAddress);=0D + Index++;=0D + } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status =3D=3D EFI_WRITE_PRO= TECTED));=0D +=0D + if (Index =3D=3D NOR_FLASH_ERASE_RETRY) {=0D + DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=3D0x%08x: Block Lo= cked Error (try to erase %d times)\n", BlockAddress, Index));=0D + }=0D +=0D + if (!EfiAtRuntime ()) {=0D + // Interruptions can resume.=0D + gBS->RestoreTPL (OriginalTPL);=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +EFI_STATUS=0D +NorFlashWriteFullBlock (=0D + IN NOR_FLASH_INSTANCE *Instance,=0D + IN EFI_LBA Lba,=0D + IN UINT32 *DataBuffer,=0D + IN UINT32 BlockSizeInWords=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINTN WordAddress;=0D + UINT32 WordIndex;=0D + UINTN BufferIndex;=0D + UINTN BlockAddress;=0D + UINTN BuffersInBlock;=0D + UINTN RemainingWords;=0D + EFI_TPL OriginalTPL;=0D + UINTN Cnt;=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + // Get the physical address of the block=0D + BlockAddress =3D GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba= , BlockSizeInWords * 4);=0D +=0D + // Start writing from the first address at the start of the block=0D + WordAddress =3D BlockAddress;=0D +=0D + if (!EfiAtRuntime ()) {=0D + // Raise TPL to TPL_HIGH to stop anyone from interrupting us.=0D + OriginalTPL =3D gBS->RaiseTPL (TPL_HIGH_LEVEL);=0D + } else {=0D + // This initialization is only to prevent the compiler to complain abo= ut the=0D + // use of uninitialized variables=0D + OriginalTPL =3D TPL_HIGH_LEVEL;=0D + }=0D +=0D + Status =3D NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and E= rase the single block at 0x%X\n", BlockAddress));=0D + goto EXIT;=0D + }=0D +=0D + // To speed up the programming operation, NOR Flash is programmed using = the Buffered Programming method.=0D +=0D + // Check that the address starts at a 32-word boundary, i.e. last 7 bits= must be zero=0D + if ((WordAddress & BOUNDARY_OF_32_WORDS) =3D=3D 0x00) {=0D + // First, break the entire block into buffer-sized chunks.=0D + BuffersInBlock =3D (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE= _IN_BYTES;=0D +=0D + // Then feed each buffer chunk to the NOR Flash=0D + // If a buffer does not contain any data, don't write it.=0D + for (BufferIndex =3D 0;=0D + BufferIndex < BuffersInBlock;=0D + BufferIndex++, WordAddress +=3D P30_MAX_BUFFER_SIZE_IN_BYTES, Dat= aBuffer +=3D P30_MAX_BUFFER_SIZE_IN_WORDS=0D + )=0D + {=0D + // Check the buffer to see if it contains any data (not set all 1s).= =0D + for (Cnt =3D 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) {=0D + if (~DataBuffer[Cnt] !=3D 0 ) {=0D + // Some data found, write the buffer.=0D + Status =3D NorFlashWriteBuffer (=0D + Instance,=0D + WordAddress,=0D + P30_MAX_BUFFER_SIZE_IN_BYTES,=0D + DataBuffer=0D + );=0D + if (EFI_ERROR (Status)) {=0D + goto EXIT;=0D + }=0D +=0D + break;=0D + }=0D + }=0D + }=0D +=0D + // Finally, finish off any remaining words that are less than the maxi= mum size of the buffer=0D + RemainingWords =3D BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS;=0D +=0D + if (RemainingWords !=3D 0) {=0D + Status =3D NorFlashWriteBuffer (Instance, WordAddress, (RemainingWor= ds * 4), DataBuffer);=0D + if (EFI_ERROR (Status)) {=0D + goto EXIT;=0D + }=0D + }=0D + } else {=0D + // For now, use the single word programming algorithm=0D + // It is unlikely that the NOR Flash will exist in an address which fa= lls within a 32 word boundary range,=0D + // i.e. which ends in the range 0x......01 - 0x......7F.=0D + for (WordIndex =3D 0; WordIndex < BlockSizeInWords; WordIndex++, DataB= uffer++, WordAddress =3D WordAddress + 4) {=0D + Status =3D NorFlashWriteSingleWord (Instance, WordAddress, *DataBuff= er);=0D + if (EFI_ERROR (Status)) {=0D + goto EXIT;=0D + }=0D + }=0D + }=0D +=0D +EXIT:=0D + if (!EfiAtRuntime ()) {=0D + // Interruptions can resume.=0D + gBS->RestoreTPL (OriginalTPL);=0D + }=0D +=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed = at address 0x%08x. Exit Status =3D \"%r\".\n", WordAddress, Status));=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashInitialise (=0D + IN EFI_HANDLE ImageHandle,=0D + IN EFI_SYSTEM_TABLE *SystemTable=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINT32 Index;=0D + VIRT_NOR_FLASH_DESCRIPTION *NorFlashDevices;=0D + BOOLEAN ContainVariableStorage;=0D +=0D + Status =3D VirtNorFlashPlatformInitialization ();=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to initialize Nor Flash= devices\n"));=0D + return Status;=0D + }=0D +=0D + Status =3D VirtNorFlashPlatformGetDevices (&NorFlashDevices, &mNorFlashD= eviceCount);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to get Nor Flash device= s\n"));=0D + return Status;=0D + }=0D +=0D + mNorFlashInstances =3D AllocateRuntimePool (sizeof (NOR_FLASH_INSTANCE *= ) * mNorFlashDeviceCount);=0D +=0D + for (Index =3D 0; Index < mNorFlashDeviceCount; Index++) {=0D + // Check if this NOR Flash device contain the variable storage region= =0D +=0D + if (PcdGet64 (PcdFlashNvStorageVariableBase64) !=3D 0) {=0D + ContainVariableStorage =3D=0D + (NorFlashDevices[Index].RegionBaseAddress <=3D PcdGet64 (PcdFlashN= vStorageVariableBase64)) &&=0D + (PcdGet64 (PcdFlashNvStorageVariableBase64) + PcdGet32 (PcdFlashNv= StorageVariableSize) <=3D=0D + NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index]= .Size);=0D + } else {=0D + ContainVariableStorage =3D=0D + (NorFlashDevices[Index].RegionBaseAddress <=3D PcdGet32 (PcdFlashN= vStorageVariableBase)) &&=0D + (PcdGet32 (PcdFlashNvStorageVariableBase) + PcdGet32 (PcdFlashNvSt= orageVariableSize) <=3D=0D + NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index]= .Size);=0D + }=0D +=0D + Status =3D NorFlashCreateInstance (=0D + NorFlashDevices[Index].DeviceBaseAddress,=0D + NorFlashDevices[Index].RegionBaseAddress,=0D + NorFlashDevices[Index].Size,=0D + Index,=0D + NorFlashDevices[Index].BlockSize,=0D + ContainVariableStorage,=0D + &mNorFlashInstances[Index]=0D + );=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to create instance fo= r NorFlash[%d]\n", Index));=0D + }=0D + }=0D +=0D + //=0D + // Register for the virtual address change event=0D + //=0D + Status =3D gBS->CreateEventEx (=0D + EVT_NOTIFY_SIGNAL,=0D + TPL_NOTIFY,=0D + NorFlashVirtualNotifyEvent,=0D + NULL,=0D + &gEfiEventVirtualAddressChangeGuid,=0D + &mNorFlashVirtualAddrChangeEvent=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + return Status;=0D +}=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +NorFlashFvbInitialize (=0D + IN NOR_FLASH_INSTANCE *Instance=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINT32 FvbNumLba;=0D + EFI_BOOT_MODE BootMode;=0D + UINTN RuntimeMmioRegionSize;=0D +=0D + DEBUG ((DEBUG_BLKIO, "NorFlashFvbInitialize\n"));=0D + ASSERT ((Instance !=3D NULL));=0D +=0D + //=0D + // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME=0D + //=0D +=0D + // Note: all the NOR Flash region needs to be reserved into the UEFI Run= time memory;=0D + // even if we only use the small block region at the top of the NO= R Flash.=0D + // The reason is when the NOR Flash memory is set into program mod= e, the command=0D + // is written as the base of the flash region (ie: Instance->Devic= eBaseAddress)=0D + RuntimeMmioRegionSize =3D (Instance->RegionBaseAddress - Instance->Devic= eBaseAddress) + Instance->Size;=0D +=0D + Status =3D gDS->AddMemorySpace (=0D + EfiGcdMemoryTypeMemoryMappedIo,=0D + Instance->DeviceBaseAddress,=0D + RuntimeMmioRegionSize,=0D + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + Status =3D gDS->SetMemorySpaceAttributes (=0D + Instance->DeviceBaseAddress,=0D + RuntimeMmioRegionSize,=0D + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + mFlashNvStorageVariableBase =3D (PcdGet64 (PcdFlashNvStorageVariableBase= 64) !=3D 0) ?=0D + PcdGet64 (PcdFlashNvStorageVariableBase64)= : PcdGet32 (PcdFlashNvStorageVariableBase);=0D +=0D + // Set the index of the first LBA for the FVB=0D + Instance->StartLba =3D (mFlashNvStorageVariableBase - Instance->RegionBa= seAddress) / Instance->Media.BlockSize;=0D +=0D + BootMode =3D GetBootModeHob ();=0D + if (BootMode =3D=3D BOOT_WITH_DEFAULT_SETTINGS) {=0D + Status =3D EFI_INVALID_PARAMETER;=0D + } else {=0D + // Determine if there is a valid header at the beginning of the NorFla= sh=0D + Status =3D ValidateFvHeader (Instance);=0D + }=0D +=0D + // Install the Default FVB header if required=0D + if (EFI_ERROR (Status)) {=0D + // There is no valid header, so time to install one.=0D + DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__= ));=0D + DEBUG ((=0D + DEBUG_INFO,=0D + "%a: Installing a correct one for this volume.\n",=0D + __FUNCTION__=0D + ));=0D +=0D + // Erase all the NorFlash that is reserved for variable storage=0D + FvbNumLba =3D (PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (Pc= dFlashNvStorageFtwWorkingSize) + PcdGet32 (PcdFlashNvStorageFtwSpareSize)) = / Instance->Media.BlockSize;=0D +=0D + Status =3D FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumL= ba, EFI_LBA_LIST_TERMINATOR);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D + // Install all appropriate headers=0D + Status =3D InitializeFvAndVariableStoreHeaders (Instance);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D + }=0D +=0D + //=0D + // The driver implementing the variable read service can now be dispatch= ed;=0D + // the varstore headers are in place.=0D + //=0D + Status =3D gBS->InstallProtocolInterface (=0D + &gImageHandle,=0D + &gEdkiiNvVarStoreFormattedGuid,=0D + EFI_NATIVE_INTERFACE,=0D + NULL=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + //=0D + // Register for the virtual address change event=0D + //=0D + Status =3D gBS->CreateEventEx (=0D + EVT_NOTIFY_SIGNAL,=0D + TPL_NOTIFY,=0D + FvbVirtualNotifyEvent,=0D + NULL,=0D + &gEfiEventVirtualAddressChangeGuid,=0D + &mFvbVirtualAddrChangeEvent=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + return Status;=0D +}=0D diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.inf b/OvmfPkg/VirtNorF= lashDxe/VirtNorFlashDxe.inf new file mode 100644 index 000000000000..1bf50e482391 --- /dev/null +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.inf @@ -0,0 +1,72 @@ +#/** @file=0D +#=0D +# Component description file for NorFlashDxe module=0D +#=0D +# Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +#**/=0D +=0D +[Defines]=0D + INF_VERSION =3D 1.29=0D + BASE_NAME =3D VirtNorFlashDxe=0D + FILE_GUID =3D e452cabd-5fe1-4d97-8161-e80ed6a409a8= =0D + MODULE_TYPE =3D DXE_RUNTIME_DRIVER=0D + VERSION_STRING =3D 1.0=0D + ENTRY_POINT =3D NorFlashInitialise=0D +=0D +[Sources.common]=0D + VirtNorFlash.c=0D + VirtNorFlash.h=0D + VirtNorFlashBlockIoDxe.c=0D + VirtNorFlashDxe.c=0D + VirtNorFlashFvb.c=0D +=0D +[Packages]=0D + ArmPlatformPkg/ArmPlatformPkg.dec=0D + EmbeddedPkg/EmbeddedPkg.dec=0D + MdePkg/MdePkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D + OvmfPkg/OvmfPkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + DebugLib=0D + DxeServicesTableLib=0D + HobLib=0D + IoLib=0D + UefiBootServicesTableLib=0D + UefiDriverEntryPoint=0D + UefiLib=0D + UefiRuntimeLib=0D + VirtNorFlashPlatformLib=0D +=0D +[Guids]=0D + gEdkiiNvVarStoreFormattedGuid ## PRODUCES ## PROTOCOL=0D + gEfiAuthenticatedVariableGuid=0D + gEfiEventVirtualAddressChangeGuid=0D + gEfiSystemNvDataFvGuid=0D + gEfiVariableGuid=0D +=0D +[Protocols]=0D + gEfiBlockIoProtocolGuid=0D + gEfiDevicePathProtocolGuid=0D + gEfiDiskIoProtocolGuid=0D + gEfiFirmwareVolumeBlockProtocolGuid=0D +=0D +[Pcd.common]=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize=0D +=0D + gArmPlatformTokenSpaceGuid.PcdNorFlashCheckBlockLocked=0D +=0D +[Depex]=0D + gEfiCpuArchProtocolGuid=0D diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlashFvb.c b/OvmfPkg/VirtNorFla= shDxe/VirtNorFlashFvb.c new file mode 100644 index 000000000000..c824e0a0fbad --- /dev/null +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlashFvb.c @@ -0,0 +1,777 @@ +/*++ @file NorFlashFvbDxe.c=0D +=0D + Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D + --*/=0D +=0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +=0D +#include "VirtNorFlash.h"=0D +=0D +extern UINTN mFlashNvStorageVariableBase;=0D +///=0D +/// The Firmware Volume Block Protocol is the low-level interface=0D +/// to a firmware volume. File-level access to a firmware volume=0D +/// should not be done using the Firmware Volume Block Protocol.=0D +/// Normal access to a firmware volume must use the Firmware=0D +/// Volume Protocol. Typically, only the file system driver that=0D +/// produces the Firmware Volume Protocol will bind to the=0D +/// Firmware Volume Block Protocol.=0D +///=0D +=0D +/**=0D + Initialises the FV Header and Variable Store Header=0D + to support variable operations.=0D +=0D + @param[in] Ptr - Location to initialise the headers=0D +=0D +**/=0D +EFI_STATUS=0D +InitializeFvAndVariableStoreHeaders (=0D + IN NOR_FLASH_INSTANCE *Instance=0D + )=0D +{=0D + EFI_STATUS Status;=0D + VOID *Headers;=0D + UINTN HeadersLength;=0D + EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader;=0D + VARIABLE_STORE_HEADER *VariableStoreHeader;=0D + UINT32 NvStorageFtwSpareSize;=0D + UINT32 NvStorageFtwWorkingSize;=0D + UINT32 NvStorageVariableSize;=0D + UINT64 NvStorageFtwSpareBase;=0D + UINT64 NvStorageFtwWorkingBase;=0D + UINT64 NvStorageVariableBase;=0D +=0D + HeadersLength =3D sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_B= LOCK_MAP_ENTRY) + sizeof (VARIABLE_STORE_HEADER);=0D + Headers =3D AllocateZeroPool (HeadersLength);=0D +=0D + NvStorageFtwWorkingSize =3D PcdGet32 (PcdFlashNvStorageFtwWorkingSize);= =0D + NvStorageFtwSpareSize =3D PcdGet32 (PcdFlashNvStorageFtwSpareSize);=0D + NvStorageVariableSize =3D PcdGet32 (PcdFlashNvStorageVariableSize);=0D +=0D + NvStorageFtwSpareBase =3D (PcdGet64 (PcdFlashNvStorageFtwSpareBase64) != =3D 0) ?=0D + PcdGet64 (PcdFlashNvStorageFtwSpareBase64) : Pcd= Get32 (PcdFlashNvStorageFtwSpareBase);=0D + NvStorageFtwWorkingBase =3D (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64= ) !=3D 0) ?=0D + PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) := PcdGet32 (PcdFlashNvStorageFtwWorkingBase);=0D + NvStorageVariableBase =3D (PcdGet64 (PcdFlashNvStorageVariableBase64) != =3D 0) ?=0D + PcdGet64 (PcdFlashNvStorageVariableBase64) : Pcd= Get32 (PcdFlashNvStorageVariableBase);=0D +=0D + // FirmwareVolumeHeader->FvLength is declared to have the Variable area = AND the FTW working area AND the FTW Spare contiguous.=0D + if ((NvStorageVariableBase + NvStorageVariableSize) !=3D NvStorageFtwWor= kingBase) {=0D + DEBUG ((=0D + DEBUG_ERROR,=0D + "%a: NvStorageFtwWorkingBase is not contiguous with NvStorageVariabl= eBase region\n",=0D + __FUNCTION__=0D + ));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + if ((NvStorageFtwWorkingBase + NvStorageFtwWorkingSize) !=3D NvStorageFt= wSpareBase) {=0D + DEBUG ((=0D + DEBUG_ERROR,=0D + "%a: NvStorageFtwSpareBase is not contiguous with NvStorageFtwWorkin= gBase region\n",=0D + __FUNCTION__=0D + ));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // Check if the size of the area is at least one block size=0D + if ((NvStorageVariableSize <=3D 0) || (NvStorageVariableSize / Instance-= >Media.BlockSize <=3D 0)) {=0D + DEBUG ((=0D + DEBUG_ERROR,=0D + "%a: NvStorageVariableSize is 0x%x, should be atleast one block size= \n",=0D + __FUNCTION__,=0D + NvStorageVariableSize=0D + ));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + if ((NvStorageFtwWorkingSize <=3D 0) || (NvStorageFtwWorkingSize / Insta= nce->Media.BlockSize <=3D 0)) {=0D + DEBUG ((=0D + DEBUG_ERROR,=0D + "%a: NvStorageFtwWorkingSize is 0x%x, should be atleast one block si= ze\n",=0D + __FUNCTION__,=0D + NvStorageFtwWorkingSize=0D + ));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + if ((NvStorageFtwSpareSize <=3D 0) || (NvStorageFtwSpareSize / Instance-= >Media.BlockSize <=3D 0)) {=0D + DEBUG ((=0D + DEBUG_ERROR,=0D + "%a: NvStorageFtwSpareSize is 0x%x, should be atleast one block size= \n",=0D + __FUNCTION__,=0D + NvStorageFtwSpareSize=0D + ));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // Ensure the Variable area Base Addresses are aligned on a block size b= oundaries=0D + if ((NvStorageVariableBase % Instance->Media.BlockSize !=3D 0) ||=0D + (NvStorageFtwWorkingBase % Instance->Media.BlockSize !=3D 0) ||=0D + (NvStorageFtwSpareBase % Instance->Media.BlockSize !=3D 0))=0D + {=0D + DEBUG ((DEBUG_ERROR, "%a: NvStorage Base addresses must be aligned to = block size boundaries", __FUNCTION__));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + //=0D + // EFI_FIRMWARE_VOLUME_HEADER=0D + //=0D + FirmwareVolumeHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *)Headers;=0D + CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid= );=0D + FirmwareVolumeHeader->FvLength =3D=0D + PcdGet32 (PcdFlashNvStorageVariableSize) +=0D + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +=0D + PcdGet32 (PcdFlashNvStorageFtwSpareSize);=0D + FirmwareVolumeHeader->Signature =3D EFI_FVH_SIGNATURE;=0D + FirmwareVolumeHeader->Attributes =3D (EFI_FVB_ATTRIBUTES_2)(=0D + EFI_FVB2_READ_= ENABLED_CAP | // Reads may be enabled=0D + EFI_FVB2_READ_= STATUS | // Reads are currently enabled=0D + EFI_FVB2_STICK= Y_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERA= SE_POLARITY=0D + EFI_FVB2_MEMOR= Y_MAPPED | // It is memory mapped=0D + EFI_FVB2_ERASE= _POLARITY | // After erasure all bits take this value (i.e. '1')=0D + EFI_FVB2_WRITE= _STATUS | // Writes are currently enabled=0D + EFI_FVB2_WRITE= _ENABLED_CAP // Writes may be enabled=0D + );=0D + FirmwareVolumeHeader->HeaderLength =3D sizeof (EFI_FIRMWARE_VOL= UME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY);=0D + FirmwareVolumeHeader->Revision =3D EFI_FVH_REVISION;=0D + FirmwareVolumeHeader->BlockMap[0].NumBlocks =3D Instance->Media.LastBloc= k + 1;=0D + FirmwareVolumeHeader->BlockMap[0].Length =3D Instance->Media.BlockSiz= e;=0D + FirmwareVolumeHeader->BlockMap[1].NumBlocks =3D 0;=0D + FirmwareVolumeHeader->BlockMap[1].Length =3D 0;=0D + FirmwareVolumeHeader->Checksum =3D CalculateCheckSum16 ((UI= NT16 *)FirmwareVolumeHeader, FirmwareVolumeHeader->HeaderLength);=0D +=0D + //=0D + // VARIABLE_STORE_HEADER=0D + //=0D + VariableStoreHeader =3D (VARIABLE_STORE_HEADER *)((UINTN)Headers + Firmw= areVolumeHeader->HeaderLength);=0D + CopyGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGui= d);=0D + VariableStoreHeader->Size =3D PcdGet32 (PcdFlashNvStorageVariableSize)= - FirmwareVolumeHeader->HeaderLength;=0D + VariableStoreHeader->Format =3D VARIABLE_STORE_FORMATTED;=0D + VariableStoreHeader->State =3D VARIABLE_STORE_HEALTHY;=0D +=0D + // Install the combined super-header in the NorFlash=0D + Status =3D FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Heade= rs);=0D +=0D + FreePool (Headers);=0D + return Status;=0D +}=0D +=0D +/**=0D + Check the integrity of firmware volume header.=0D +=0D + @param[in] FwVolHeader - A pointer to a firmware volume header=0D +=0D + @retval EFI_SUCCESS - The firmware volume is consistent=0D + @retval EFI_NOT_FOUND - The firmware volume has been corrupted.=0D +=0D +**/=0D +EFI_STATUS=0D +ValidateFvHeader (=0D + IN NOR_FLASH_INSTANCE *Instance=0D + )=0D +{=0D + UINT16 Checksum;=0D + EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;=0D + VARIABLE_STORE_HEADER *VariableStoreHeader;=0D + UINTN VariableStoreLength;=0D + UINTN FvLength;=0D +=0D + FwVolHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *)Instance->RegionBaseAddres= s;=0D +=0D + FvLength =3D PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFla= shNvStorageFtwWorkingSize) +=0D + PcdGet32 (PcdFlashNvStorageFtwSpareSize);=0D +=0D + //=0D + // Verify the header revision, header signature, length=0D + // Length of FvBlock cannot be 2**64-1=0D + // HeaderLength cannot be an odd number=0D + //=0D + if ( (FwVolHeader->Revision !=3D EFI_FVH_REVISION)=0D + || (FwVolHeader->Signature !=3D EFI_FVH_SIGNATURE)=0D + || (FwVolHeader->FvLength !=3D FvLength)=0D + )=0D + {=0D + DEBUG ((=0D + DEBUG_INFO,=0D + "%a: No Firmware Volume header present\n",=0D + __FUNCTION__=0D + ));=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + // Check the Firmware Volume Guid=0D + if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid)= =3D=3D FALSE ) {=0D + DEBUG ((=0D + DEBUG_INFO,=0D + "%a: Firmware Volume Guid non-compatible\n",=0D + __FUNCTION__=0D + ));=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + // Verify the header checksum=0D + Checksum =3D CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderL= ength);=0D + if (Checksum !=3D 0) {=0D + DEBUG ((=0D + DEBUG_INFO,=0D + "%a: FV checksum is invalid (Checksum:0x%X)\n",=0D + __FUNCTION__,=0D + Checksum=0D + ));=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + VariableStoreHeader =3D (VARIABLE_STORE_HEADER *)((UINTN)FwVolHeader + F= wVolHeader->HeaderLength);=0D +=0D + // Check the Variable Store Guid=0D + if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&= =0D + !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVar= iableGuid))=0D + {=0D + DEBUG ((=0D + DEBUG_INFO,=0D + "%a: Variable Store Guid non-compatible\n",=0D + __FUNCTION__=0D + ));=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + VariableStoreLength =3D PcdGet32 (PcdFlashNvStorageVariableSize) - FwVol= Header->HeaderLength;=0D + if (VariableStoreHeader->Size !=3D VariableStoreLength) {=0D + DEBUG ((=0D + DEBUG_INFO,=0D + "%a: Variable Store Length does not match\n",=0D + __FUNCTION__=0D + ));=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + The GetAttributes() function retrieves the attributes and=0D + current settings of the block.=0D +=0D + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL ins= tance.=0D +=0D + @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attribut= es and=0D + current settings are returned.=0D + Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_= VOLUME_HEADER.=0D +=0D + @retval EFI_SUCCESS The firmware volume attributes were returned.=0D +=0D + **/=0D +EFI_STATUS=0D +EFIAPI=0D +FvbGetAttributes (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D + )=0D +{=0D + EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes;=0D + NOR_FLASH_INSTANCE *Instance;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D +=0D + FlashFvbAttributes =3D (EFI_FVB_ATTRIBUTES_2)(=0D +=0D + EFI_FVB2_READ_ENABLED_CAP | = // Reads may be enabled=0D + EFI_FVB2_READ_STATUS | = // Reads are currently enabled=0D + EFI_FVB2_STICKY_WRITE | = // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY=0D + EFI_FVB2_MEMORY_MAPPED | = // It is memory mapped=0D + EFI_FVB2_ERASE_POLARITY = // After erasure all bits take this value (i.e. '1')=0D +=0D + );=0D +=0D + // Check if it is write protected=0D + if (Instance->Media.ReadOnly !=3D TRUE) {=0D + FlashFvbAttributes =3D FlashFvbAttributes |=0D + EFI_FVB2_WRITE_STATUS | // Writes are curren= tly enabled=0D + EFI_FVB2_WRITE_ENABLED_CAP; // Writes may be ena= bled=0D + }=0D +=0D + *Attributes =3D FlashFvbAttributes;=0D +=0D + DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + The SetAttributes() function sets configurable firmware volume attributes= =0D + and returns the new settings of the firmware volume.=0D +=0D +=0D + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_= PROTOCOL instance.=0D +=0D + @param Attributes On input, Attributes is a pointer to EFI_= FVB_ATTRIBUTES_2=0D + that contains the desired firmware volume= settings.=0D + On successful return, it contains the new= settings of=0D + the firmware volume.=0D + Type EFI_FVB_ATTRIBUTES_2 is defined in E= FI_FIRMWARE_VOLUME_HEADER.=0D +=0D + @retval EFI_SUCCESS The firmware volume attributes were retur= ned.=0D +=0D + @retval EFI_INVALID_PARAMETER The attributes requested are in conflict = with the capabilities=0D + as declared in the firmware volume header= .=0D +=0D + **/=0D +EFI_STATUS=0D +EFIAPI=0D +FvbSetAttributes (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D + )=0D +{=0D + DEBUG ((DEBUG_BLKIO, "FvbSetAttributes(0x%X) is not supported\n", *Attri= butes));=0D + return EFI_UNSUPPORTED;=0D +}=0D +=0D +/**=0D + The GetPhysicalAddress() function retrieves the base address of=0D + a memory-mapped firmware volume. This function should be called=0D + only for memory-mapped firmware volumes.=0D +=0D + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOC= OL instance.=0D +=0D + @param Address Pointer to a caller-allocated=0D + EFI_PHYSICAL_ADDRESS that, on successful=0D + return from GetPhysicalAddress(), contains the= =0D + base address of the firmware volume.=0D +=0D + @retval EFI_SUCCESS The firmware volume base address was returned.= =0D +=0D + @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.=0D +=0D + **/=0D +EFI_STATUS=0D +EFIAPI=0D +FvbGetPhysicalAddress (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + OUT EFI_PHYSICAL_ADDRESS *Address=0D + )=0D +{=0D + NOR_FLASH_INSTANCE *Instance;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D +=0D + DEBUG ((DEBUG_BLKIO, "FvbGetPhysicalAddress(BaseAddress=3D0x%08x)\n", In= stance->RegionBaseAddress));=0D +=0D + ASSERT (Address !=3D NULL);=0D +=0D + *Address =3D mFlashNvStorageVariableBase;=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + The GetBlockSize() function retrieves the size of the requested=0D + block. It also returns the number of additional blocks with=0D + the identical size. The GetBlockSize() function is used to=0D + retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).=0D +=0D +=0D + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_= PROTOCOL instance.=0D +=0D + @param Lba Indicates the block for which to return t= he size.=0D +=0D + @param BlockSize Pointer to a caller-allocated UINTN in wh= ich=0D + the size of the block is returned.=0D +=0D + @param NumberOfBlocks Pointer to a caller-allocated UINTN in=0D + which the number of consecutive blocks,=0D + starting with Lba, is returned. All=0D + blocks in this range have a size of=0D + BlockSize.=0D +=0D +=0D + @retval EFI_SUCCESS The firmware volume base address was retu= rned.=0D +=0D + @retval EFI_INVALID_PARAMETER The requested LBA is out of range.=0D +=0D + **/=0D +EFI_STATUS=0D +EFIAPI=0D +FvbGetBlockSize (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + IN EFI_LBA Lba,=0D + OUT UINTN *BlockSize,=0D + OUT UINTN *NumberOfBlocks=0D + )=0D +{=0D + EFI_STATUS Status;=0D + NOR_FLASH_INSTANCE *Instance;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D +=0D + DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=3D%ld, BlockSize=3D0x%x, LastB= lock=3D%ld)\n", Lba, Instance->Media.BlockSize, Instance->Media.LastBlock))= ;=0D +=0D + if (Lba > Instance->Media.LastBlock) {=0D + DEBUG ((DEBUG_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is be= yond the last Lba (%ld).\n", Lba, Instance->Media.LastBlock));=0D + Status =3D EFI_INVALID_PARAMETER;=0D + } else {=0D + // This is easy because in this platform each NorFlash device has equa= l sized blocks.=0D + *BlockSize =3D (UINTN)Instance->Media.BlockSize;=0D + *NumberOfBlocks =3D (UINTN)(Instance->Media.LastBlock - Lba + 1);=0D +=0D + DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=3D0x%x, *NumberOfBlo= cks=3D0x%x.\n", *BlockSize, *NumberOfBlocks));=0D +=0D + Status =3D EFI_SUCCESS;=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +/**=0D + Reads the specified number of bytes into a buffer from the specified bloc= k.=0D +=0D + The Read() function reads the requested number of bytes from the=0D + requested block and stores them in the provided buffer.=0D + Implementations should be mindful that the firmware volume=0D + might be in the ReadDisabled state. If it is in this state,=0D + the Read() function must return the status code=0D + EFI_ACCESS_DENIED without modifying the contents of the=0D + buffer. The Read() function must also prevent spanning block=0D + boundaries. If a read is requested that would span a block=0D + boundary, the read must read up to the boundary but not=0D + beyond. The output parameter NumBytes must be set to correctly=0D + indicate the number of bytes actually read. The caller must be=0D + aware that a read may be partially completed.=0D +=0D + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROT= OCOL instance.=0D +=0D + @param Lba The starting logical block index from which t= o read.=0D +=0D + @param Offset Offset into the block at which to begin readi= ng.=0D +=0D + @param NumBytes Pointer to a UINTN.=0D + At entry, *NumBytes contains the total size o= f the buffer.=0D + At exit, *NumBytes contains the total number = of bytes read.=0D +=0D + @param Buffer Pointer to a caller-allocated buffer that wil= l be used=0D + to hold the data that is read.=0D +=0D + @retval EFI_SUCCESS The firmware volume was read successfully, a= nd contents are=0D + in Buffer.=0D +=0D + @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.=0D + On output, NumBytes contains the total number= of bytes=0D + returned in Buffer.=0D +=0D + @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled st= ate.=0D +=0D + @retval EFI_DEVICE_ERROR The block device is not functioning correctly= and could not be read.=0D +=0D + **/=0D +EFI_STATUS=0D +EFIAPI=0D +FvbRead (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN OUT UINTN *NumBytes,=0D + IN OUT UINT8 *Buffer=0D + )=0D +{=0D + EFI_STATUS TempStatus;=0D + UINTN BlockSize;=0D + NOR_FLASH_INSTANCE *Instance;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D +=0D + DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=3D%ld, Offset=3D0x%x, *Num= Bytes=3D0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBy= tes, Buffer));=0D +=0D + TempStatus =3D EFI_SUCCESS;=0D +=0D + // Cache the block size to avoid de-referencing pointers all the time=0D + BlockSize =3D Instance->Media.BlockSize;=0D +=0D + DEBUG ((DEBUG_BLKIO, "FvbRead: Check if (Offset=3D0x%x + NumBytes=3D0x%x= ) <=3D BlockSize=3D0x%x\n", Offset, *NumBytes, BlockSize));=0D +=0D + // The read must not span block boundaries.=0D + // We need to check each variable individually because adding two large = values together overflows.=0D + if ((Offset >=3D BlockSize) ||=0D + (*NumBytes > BlockSize) ||=0D + ((Offset + *NumBytes) > BlockSize))=0D + {=0D + DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=3D= 0x%x + NumBytes=3D0x%x) > BlockSize=3D0x%x\n", Offset, *NumBytes, BlockSize= ));=0D + return EFI_BAD_BUFFER_SIZE;=0D + }=0D +=0D + // We must have some bytes to read=0D + if (*NumBytes =3D=3D 0) {=0D + return EFI_BAD_BUFFER_SIZE;=0D + }=0D +=0D + // Decide if we are doing full block reads or not.=0D + if (*NumBytes % BlockSize !=3D 0) {=0D + TempStatus =3D NorFlashRead (Instance, Instance->StartLba + Lba, Offse= t, *NumBytes, Buffer);=0D + if (EFI_ERROR (TempStatus)) {=0D + return EFI_DEVICE_ERROR;=0D + }=0D + } else {=0D + // Read NOR Flash data into shadow buffer=0D + TempStatus =3D NorFlashReadBlocks (Instance, Instance->StartLba + Lba,= BlockSize, Buffer);=0D + if (EFI_ERROR (TempStatus)) {=0D + // Return one of the pre-approved error statuses=0D + return EFI_DEVICE_ERROR;=0D + }=0D + }=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Writes the specified number of bytes from the input buffer to the block.= =0D +=0D + The Write() function writes the specified number of bytes from=0D + the provided buffer to the specified block and offset. If the=0D + firmware volume is sticky write, the caller must ensure that=0D + all the bits of the specified range to write are in the=0D + EFI_FVB_ERASE_POLARITY state before calling the Write()=0D + function, or else the result will be unpredictable. This=0D + unpredictability arises because, for a sticky-write firmware=0D + volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY=0D + state but cannot flip it back again. Before calling the=0D + Write() function, it is recommended for the caller to first call=0D + the EraseBlocks() function to erase the specified block to=0D + write. A block erase cycle will transition bits from the=0D + (NOT)EFI_FVB_ERASE_POLARITY state back to the=0D + EFI_FVB_ERASE_POLARITY state. Implementations should be=0D + mindful that the firmware volume might be in the WriteDisabled=0D + state. If it is in this state, the Write() function must=0D + return the status code EFI_ACCESS_DENIED without modifying the=0D + contents of the firmware volume. The Write() function must=0D + also prevent spanning block boundaries. If a write is=0D + requested that spans a block boundary, the write must store up=0D + to the boundary but not beyond. The output parameter NumBytes=0D + must be set to correctly indicate the number of bytes actually=0D + written. The caller must be aware that a write may be=0D + partially completed. All writes, partial or otherwise, must be=0D + fully flushed to the hardware before the Write() service=0D + returns.=0D +=0D + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROT= OCOL instance.=0D +=0D + @param Lba The starting logical block index to write to.= =0D +=0D + @param Offset Offset into the block at which to begin writi= ng.=0D +=0D + @param NumBytes The pointer to a UINTN.=0D + At entry, *NumBytes contains the total size o= f the buffer.=0D + At exit, *NumBytes contains the total number = of bytes actually written.=0D +=0D + @param Buffer The pointer to a caller-allocated buffer that= contains the source for the write.=0D +=0D + @retval EFI_SUCCESS The firmware volume was written successfully.= =0D +=0D + @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundar= y.=0D + On output, NumBytes contains the total number= of bytes=0D + actually written.=0D +=0D + @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled s= tate.=0D +=0D + @retval EFI_DEVICE_ERROR The block device is malfunctioning and could = not be written.=0D +=0D +=0D + **/=0D +EFI_STATUS=0D +EFIAPI=0D +FvbWrite (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN OUT UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + )=0D +{=0D + NOR_FLASH_INSTANCE *Instance;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D +=0D + return NorFlashWriteSingleBlock (Instance, Instance->StartLba + Lba, Off= set, NumBytes, Buffer);=0D +}=0D +=0D +/**=0D + Erases and initialises a firmware volume block.=0D +=0D + The EraseBlocks() function erases one or more blocks as denoted=0D + by the variable argument list. The entire parameter list of=0D + blocks must be verified before erasing any blocks. If a block is=0D + requested that does not exist within the associated firmware=0D + volume (it has a larger index than the last block of the=0D + firmware volume), the EraseBlocks() function must return the=0D + status code EFI_INVALID_PARAMETER without modifying the contents=0D + of the firmware volume. Implementations should be mindful that=0D + the firmware volume might be in the WriteDisabled state. If it=0D + is in this state, the EraseBlocks() function must return the=0D + status code EFI_ACCESS_DENIED without modifying the contents of=0D + the firmware volume. All calls to EraseBlocks() must be fully=0D + flushed to the hardware before the EraseBlocks() service=0D + returns.=0D +=0D + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_= PROTOCOL=0D + instance.=0D +=0D + @param ... The variable argument list is a list of t= uples.=0D + Each tuple describes a range of LBAs to e= rase=0D + and consists of the following:=0D + - An EFI_LBA that indicates the starting = LBA=0D + - A UINTN that indicates the number of bl= ocks to erase.=0D +=0D + The list is terminated with an EFI_LBA_LI= ST_TERMINATOR.=0D + For example, the following indicates that= two ranges of blocks=0D + (5-7 and 10-11) are to be erased:=0D + EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_L= IST_TERMINATOR);=0D +=0D + @retval EFI_SUCCESS The erase request successfully completed.= =0D +=0D + @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabl= ed state.=0D +=0D + @retval EFI_DEVICE_ERROR The block device is not functioning corre= ctly and could not be written.=0D + The firmware device may have been partial= ly erased.=0D +=0D + @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the var= iable argument list do=0D + not exist in the firmware volume.=0D +=0D + **/=0D +EFI_STATUS=0D +EFIAPI=0D +FvbEraseBlocks (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,=0D + ...=0D + )=0D +{=0D + EFI_STATUS Status;=0D + VA_LIST Args;=0D + UINTN BlockAddress; // Physical address of Lba to erase=0D + EFI_LBA StartingLba; // Lba from which we start erasing=0D + UINTN NumOfLba; // Number of Lba blocks to erase=0D + NOR_FLASH_INSTANCE *Instance;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D +=0D + DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks()\n"));=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + // Detect WriteDisabled state=0D + if (Instance->Media.ReadOnly =3D=3D TRUE) {=0D + // Firmware volume is in WriteDisabled state=0D + DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Device is in WriteDisabl= ed state.\n"));=0D + return EFI_ACCESS_DENIED;=0D + }=0D +=0D + // Before erasing, check the entire list of parameters to ensure all spe= cified blocks are valid=0D +=0D + VA_START (Args, This);=0D + do {=0D + // Get the Lba from which we start erasing=0D + StartingLba =3D VA_ARG (Args, EFI_LBA);=0D +=0D + // Have we reached the end of the list?=0D + if (StartingLba =3D=3D EFI_LBA_LIST_TERMINATOR) {=0D + // Exit the while loop=0D + break;=0D + }=0D +=0D + // How many Lba blocks are we requested to erase?=0D + NumOfLba =3D VA_ARG (Args, UINTN);=0D +=0D + // All blocks must be within range=0D + DEBUG ((=0D + DEBUG_BLKIO,=0D + "FvbEraseBlocks: Check if: ( StartingLba=3D%ld + NumOfLba=3D%Lu - 1 = ) > LastBlock=3D%ld.\n",=0D + Instance->StartLba + StartingLba,=0D + (UINT64)NumOfLba,=0D + Instance->Media.LastBlock=0D + ));=0D + if ((NumOfLba =3D=3D 0) || ((Instance->StartLba + StartingLba + NumOfL= ba - 1) > Instance->Media.LastBlock)) {=0D + VA_END (Args);=0D + DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past th= e last Lba.\n"));=0D + Status =3D EFI_INVALID_PARAMETER;=0D + goto EXIT;=0D + }=0D + } while (TRUE);=0D +=0D + VA_END (Args);=0D +=0D + //=0D + // To get here, all must be ok, so start erasing=0D + //=0D + VA_START (Args, This);=0D + do {=0D + // Get the Lba from which we start erasing=0D + StartingLba =3D VA_ARG (Args, EFI_LBA);=0D +=0D + // Have we reached the end of the list?=0D + if (StartingLba =3D=3D EFI_LBA_LIST_TERMINATOR) {=0D + // Exit the while loop=0D + break;=0D + }=0D +=0D + // How many Lba blocks are we requested to erase?=0D + NumOfLba =3D VA_ARG (Args, UINTN);=0D +=0D + // Go through each one and erase it=0D + while (NumOfLba > 0) {=0D + // Get the physical address of Lba to erase=0D + BlockAddress =3D GET_NOR_BLOCK_ADDRESS (=0D + Instance->RegionBaseAddress,=0D + Instance->StartLba + StartingLba,=0D + Instance->Media.BlockSize=0D + );=0D +=0D + // Erase it=0D + DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=3D%ld @ 0x%08x.\n"= , Instance->StartLba + StartingLba, BlockAddress));=0D + Status =3D NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress= );=0D + if (EFI_ERROR (Status)) {=0D + VA_END (Args);=0D + Status =3D EFI_DEVICE_ERROR;=0D + goto EXIT;=0D + }=0D +=0D + // Move to the next Lba=0D + StartingLba++;=0D + NumOfLba--;=0D + }=0D + } while (TRUE);=0D +=0D + VA_END (Args);=0D +=0D +EXIT:=0D + return Status;=0D +}=0D +=0D +/**=0D + Fixup internal data so that EFI can be call in virtual mode.=0D + Call the passed in Child Notify event and convert any pointers in=0D + lib to virtual mode.=0D +=0D + @param[in] Event The Event that is being processed=0D + @param[in] Context Event Context=0D +**/=0D +VOID=0D +EFIAPI=0D +FvbVirtualNotifyEvent (=0D + IN EFI_EVENT Event,=0D + IN VOID *Context=0D + )=0D +{=0D + EfiConvertPointer (0x0, (VOID **)&mFlashNvStorageVariableBase);=0D + return;=0D +}=0D --=20 2.35.1