From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) by mx.groups.io with SMTP id smtpd.web09.2266.1615660309024409662 for ; Sat, 13 Mar 2021 10:31:49 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@linaro.org header.s=google header.b=hlsWjtUF; spf=pass (domain: linaro.org, ip: 209.85.221.48, mailfrom: ilias.apalodimas@linaro.org) Received: by mail-wr1-f48.google.com with SMTP id y16so6570727wrw.3 for ; Sat, 13 Mar 2021 10:31:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IlcZWoZuOrE0vpgyr2ORIdY1N32Te7aLl+TSdD+rWxo=; b=hlsWjtUFQdOrxtY7cjaD4Z7Nzcj0XFEpI2dKoKSLGXOJp2HhxTBzeR6kBalTybJQ15 JEW7EDztWKhZeaav6o2TFFeOY7mN7DDlNGtbDR9TDl15ZAc7YkGIFtn+769GPg9Q9YuK aAzSJHg8nPu/NYw1goz00CQJ1kTLEf/QMEjuRAQIj5g+Tr3i4SzeNYuB7neyum99pfPl 5A0WkbLs5WU5dVoSRseU4dYRGYTRMxXHPwyE36h34GqTcQWmB0Vrpt05kYNrM2fVHunX 36tRWfJWjbwTTU4dkRQNqyZxYGH/bTpVBfdxLQ3xbBllzuDbNF1nqe7Kphx0MLaG3xpV b2tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IlcZWoZuOrE0vpgyr2ORIdY1N32Te7aLl+TSdD+rWxo=; b=hjCRrXxuwowFSz2uJl454zNoAwDvv31DQFTopqdcjCYaQxdMfbKz4vHpSSMvBp1Nxk LaC3H0/QAQpAM6upazfJ9uC3KObLX92HpH5tjqkK6bLFQhaU5MLh77seQCg+CCzWle8m chBPRB+KOMDX6/hx+KC0UFmqNJHFjDej+d7qJipfD0weXy/I3aYr9+uDoOzieWOYgAEc uPN+0xu9a0wKH0GNA6twSiaNlyNKukfYcTW42rSgERmqneKzY8anzYhggGUj5gzD9TGP czQKzrebcCBksDMSEh8aonErZXp5vlenR/jbcnzjdNft2Y5oYZNNHIbG6nq2yc+cctFV KG5A== X-Gm-Message-State: AOAM533rrDxBq+IWxXSVsYEX/PY997qq011CpN0EFNi+/Pnw+N3e0QS7 ZGaFhOT8X00H0Pgmbc74TvPlA1EFfqyL/csh X-Google-Smtp-Source: ABdhPJyaHHzJEVhY9qihq5M3mWN/cwBpc9tMttihj941FCNTjodTbuinJm+tskykAJLYo0mia0OiOw== X-Received: by 2002:adf:c3c8:: with SMTP id d8mr20013938wrg.167.1615660307292; Sat, 13 Mar 2021 10:31:47 -0800 (PST) Return-Path: Received: from apalos.home ([2a02:587:4647:e6c6:2e56:dcff:fe9a:8f06]) by smtp.gmail.com with ESMTPSA id s18sm14744164wrr.27.2021.03.13.10.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Mar 2021 10:31:46 -0800 (PST) From: "Ilias Apalodimas" To: devel@edk2.groups.io, sami.mujawar@arm.com, pierre.gondois@arm.com Cc: ardb+tianocore@kernel.org, sughosh.ganu@linaro.org, leif@nuviainc.com, Ilias Apalodimas Subject: [PATCH 1/3 v7] Drivers/OpTeeRpmb: Add an OP-TEE backed RPMB driver Date: Sat, 13 Mar 2021 20:31:37 +0200 Message-Id: <20210313183139.3256104-2-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210313183139.3256104-1-ilias.apalodimas@linaro.org> References: <20210313183139.3256104-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable A following patch is adding support for building StMM in order to run it from OP-TEE. OP-TEE in combination with a NS-world supplicant can use the RPMB partition of an eMMC to store EFI variables. The supplicant functionality is currently available in U-Boot only but can be ported into EDK2. Assuming similar functionality is added in EDK2, this will allow any hardware with an RPMB partition to store EFI variables securely. So let's add a driver that enables access of the RPMB partition through OP-TEE. Since the upper layers expect a byte addressable interface, the driver allocates memory and patches the PCDs, while syncing the memory/hardware on read/write callbacks. Signed-off-by: Ilias Apalodimas --- Drivers/OpTee/OpteeRpmbPkg/FixupPcd.c | 81 ++ Drivers/OpTee/OpteeRpmbPkg/FixupPcd.inf | 43 + Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFv.inf | 58 ++ Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFvb.c | 861 +++++++++++++++++++++ Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFvb.h | 51 ++ 5 files changed, 1094 insertions(+) create mode 100644 Drivers/OpTee/OpteeRpmbPkg/FixupPcd.c create mode 100644 Drivers/OpTee/OpteeRpmbPkg/FixupPcd.inf create mode 100644 Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFv.inf create mode 100644 Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFvb.c create mode 100644 Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFvb.h diff --git a/Drivers/OpTee/OpteeRpmbPkg/FixupPcd.c b/Drivers/OpTee/OpteeRpm= bPkg/FixupPcd.c new file mode 100644 index 000000000000..56402fd1283f --- /dev/null +++ b/Drivers/OpTee/OpteeRpmbPkg/FixupPcd.c @@ -0,0 +1,81 @@ +/** @file=0D +=0D + Update the patched PCDs to their correct value=0D +=0D + Copyright (c) 2020, Linaro Ltd. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +/**=0D + * Patch the relevant PCDs of the RPMB driver with the correct address of = the=0D + * allocated memory=0D + *=0D +**/=0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +=0D +#include "OpTeeRpmbFvb.h"=0D +=0D +/**=0D + Fixup the Pcd values for variable storage=0D +=0D + Since the upper layers of EDK2 expect a memory mapped interface and we c= an't=0D + offer that from an RPMB, the driver allocates memory on init and passes = that=0D + on the upper layers. Since the memory is dynamically allocated and we ca= n't set the=0D + PCD in StMM context, we need to patch it correctly on each access=0D +=0D + @retval EFI_SUCCESS Protocol was found and PCDs patched up=0D + @retval EFI_INVALID_PARAMETER Invalid parameter=0D + @retval EFI_NOT_FOUND Protocol interface not found=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +FixPcdMemory (=0D + VOID=0D + )=0D +{=0D + EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;=0D + MEM_INSTANCE *Instance;=0D + EFI_STATUS Status;=0D +=0D + //=0D + // Locate SmmFirmwareVolumeBlockProtocol=0D + //=0D + Status =3D gMmst->MmLocateProtocol (=0D + &gEfiSmmFirmwareVolumeBlockProtocolGuid,=0D + NULL,=0D + (VOID **) &FvbProtocol=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (FvbProtocol);=0D +=0D + // Patch PCDs with the correct values=0D + PatchPcdSet64 (PcdFlashNvStorageVariableBase64, Instance->MemBaseAddress= );=0D + PatchPcdSet64 (=0D + PcdFlashNvStorageFtwWorkingBase64,=0D + Instance->MemBaseAddress + PcdGet32 (PcdFlashNvStorageVariableSize)=0D + );=0D + PatchPcdSet64 (=0D + PcdFlashNvStorageFtwSpareBase64,=0D + Instance->MemBaseAddress +=0D + PcdGet32 (PcdFlashNvStorageVariableSize) +=0D + PcdGet32 (PcdFlashNvStorageFtwWorkingSize)=0D + );=0D +=0D + DEBUG ((DEBUG_INFO, "%a: Fixup PcdFlashNvStorageVariableBase64: 0x%lx\n"= ,=0D + __FUNCTION__, PcdGet64 (PcdFlashNvStorageVariableBase64)));=0D + DEBUG ((DEBUG_INFO, "%a: Fixup PcdFlashNvStorageFtwWorkingBase64: 0x%lx\= n",=0D + __FUNCTION__, PcdGet64 (PcdFlashNvStorageFtwWorkingBase64)));=0D + DEBUG ((DEBUG_INFO, "%a: Fixup PcdFlashNvStorageFtwSpareBase64: 0x%lx\n"= ,=0D + __FUNCTION__, PcdGet64 (PcdFlashNvStorageFtwSpareBase64)));=0D +=0D + return Status;=0D +}=0D diff --git a/Drivers/OpTee/OpteeRpmbPkg/FixupPcd.inf b/Drivers/OpTee/OpteeR= pmbPkg/FixupPcd.inf new file mode 100644 index 000000000000..5146257993ef --- /dev/null +++ b/Drivers/OpTee/OpteeRpmbPkg/FixupPcd.inf @@ -0,0 +1,43 @@ +## @file=0D +# Instance of Base Memory Library without assembly.=0D +#=0D +# Copyright (c) 2020, Linaro Ltd. All rights reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x0001001A=0D + BASE_NAME =3D FixupPcd=0D + FILE_GUID =3D a827c337-a9c6-301b-aeb7-acbc95d8da22= =0D + MODULE_TYPE =3D BASE=0D + VERSION_STRING =3D 0.1=0D + LIBRARY_CLASS =3D RpmbPcdFixup|MM_STANDALONE=0D + CONSTRUCTOR =3D FixPcdMemory=0D +=0D +[Sources]=0D + FixupPcd.c=0D + OpTeeRpmbFvb.h=0D +=0D +[Packages]=0D + MdeModulePkg/MdeModulePkg.dec=0D + MdePkg/MdePkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + DebugLib=0D + MmServicesTableLib=0D + PcdLib=0D +=0D +[Pcd]=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize=0D +=0D +[Protocols]=0D + gEfiSmmFirmwareVolumeBlockProtocolGuid ## CONSUMES=0D diff --git a/Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFv.inf b/Drivers/OpTee/Opt= eeRpmbPkg/OpTeeRpmbFv.inf new file mode 100644 index 000000000000..c3580859ffde --- /dev/null +++ b/Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFv.inf @@ -0,0 +1,58 @@ +## @file=0D +#=0D +# Component description file for OpTeeRpmbFv module=0D +#=0D +# Copyright (c) 2020, Linaro Ltd. All rights reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x0001001A=0D + BASE_NAME =3D OpTeeRpmbFv=0D + FILE_GUID =3D 4803FC20-E583-3BCD-8C60-141E85C9A2CF= =0D + MODULE_TYPE =3D MM_STANDALONE=0D + VERSION_STRING =3D 0.1=0D + PI_SPECIFICATION_VERSION =3D 0x00010032=0D + ENTRY_POINT =3D OpTeeRpmbFvbInit=0D +=0D +[Sources]=0D + OpTeeRpmbFvb.c=0D + OpTeeRpmbFvb.h=0D +=0D +[Packages]=0D + ArmPkg/ArmPkg.dec=0D + ArmPlatformPkg/ArmPlatformPkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D + MdePkg/MdePkg.dec=0D + StandaloneMmPkg/StandaloneMmPkg.dec=0D +=0D +[LibraryClasses]=0D + ArmSvcLib=0D + BaseLib=0D + BaseMemoryLib=0D + DebugLib=0D + MemoryAllocationLib=0D + MmServicesTableLib=0D + PcdLib=0D + StandaloneMmDriverEntryPoint=0D +=0D +[Guids]=0D + gEfiAuthenticatedVariableGuid=0D + gEfiSystemNvDataFvGuid=0D + gEfiVariableGuid=0D +=0D +[Pcd]=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize=0D +=0D +[Protocols]=0D + gEfiSmmFirmwareVolumeBlockProtocolGuid ## PRODUCES=0D +=0D +[Depex]=0D + TRUE=0D diff --git a/Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFvb.c b/Drivers/OpTee/Opte= eRpmbPkg/OpTeeRpmbFvb.c new file mode 100644 index 000000000000..80d5b21ff12a --- /dev/null +++ b/Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFvb.c @@ -0,0 +1,861 @@ +/** @file=0D +=0D + FV block I/O protocol driver for RPMB eMMC accessed via OP-TEE=0D +=0D + Copyright (c) 2020, Linaro Ltd. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include "OpTeeRpmbFvb.h"=0D +=0D +// These are what OP-TEE expects in ./core/arch/arm/kernel/stmm_sp.c=0D +// Since the FFA autodiscovery mechanism is not yet implemented we are=0D +// hardcoding the ID values for the two operations OP-TEE currently suppor= ts=0D +//=0D +// mMemMgrId is used to set the page permissions after relocating the exec= utable=0D +// mStorageId is used to access the RPMB partition via OP-TEE=0D +// In both cases the return value is located in x3. Once the autodiscovery= mechanism=0D +// is in place, we'll have to account for an error value in x2 as well, ha= ndling=0D +// the autodiscovery failed scenario=0D +STATIC CONST UINT16 mMemMgrId =3D 3U;=0D +STATIC CONST UINT16 mStorageId =3D 4U;=0D +=0D +STATIC MEM_INSTANCE mInstance;=0D +=0D +/**=0D + Sends an SVC call to OP-TEE for reading/writing an RPMB partition=0D +=0D + @param[in] SvcAct SVC ID for read/write=0D + @param[in] Addr Base address of the buffer. When reading contents= will be=0D + copied to that buffer after reading them from the= device.=0D + When writing, the buffer holds the contents we wa= nt to=0D + write cwtoin the device=0D + @param[in] NumBytes Number of bytes to read/write=0D + @param[in] Offset Offset into the RPMB file=0D +=0D + @retval EFI_SUCCESS read/write ok=0D + @retval EFI_UNSUPPORTED SVC to op-tee not supported=0D + @retval EFI_INVALID_PARAMETER SVC to op-tee had an invalid param=0D + @retval EFI_ACCESS_DENIED SVC to op-tee was denied=0D + @retval EFI_OUT_OF_RESOURCES op-tee out of memory=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +ReadWriteRpmb (=0D + IN UINTN SvcAct,=0D + IN UINTN Addr,=0D + IN UINTN NumBytes,=0D + IN UINTN Offset=0D + )=0D +{=0D + ARM_SVC_ARGS SvcArgs;=0D + EFI_STATUS Status;=0D +=0D + ZeroMem (&SvcArgs, sizeof (SvcArgs));=0D +=0D + SvcArgs.Arg0 =3D ARM_SVC_ID_FFA_MSG_SEND_DIRECT_REQ_AARCH64;=0D + SvcArgs.Arg1 =3D mStorageId;=0D + SvcArgs.Arg2 =3D 0;=0D + SvcArgs.Arg3 =3D SvcAct;=0D + SvcArgs.Arg4 =3D Addr;=0D + SvcArgs.Arg5 =3D NumBytes;=0D + SvcArgs.Arg6 =3D Offset;=0D +=0D + ArmCallSvc (&SvcArgs);=0D + if (SvcArgs.Arg3) {=0D + DEBUG ((DEBUG_ERROR, "%a: Svc Call 0x%08x Addr: 0x%08x len: 0x%x Offse= t: 0x%x failed with 0x%x\n",=0D + __func__, SvcAct, Addr, NumBytes, Offset, SvcArgs.Arg3));=0D + }=0D +=0D + switch (SvcArgs.Arg3) {=0D + case ARM_SVC_SPM_RET_SUCCESS:=0D + Status =3D EFI_SUCCESS;=0D + break;=0D +=0D + case ARM_SVC_SPM_RET_NOT_SUPPORTED:=0D + Status =3D EFI_UNSUPPORTED;=0D + break;=0D +=0D + case ARM_SVC_SPM_RET_INVALID_PARAMS:=0D + Status =3D EFI_INVALID_PARAMETER;=0D + break;=0D +=0D + case ARM_SVC_SPM_RET_DENIED:=0D + Status =3D EFI_ACCESS_DENIED;=0D + break;=0D +=0D + case ARM_SVC_SPM_RET_NO_MEMORY:=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + break;=0D +=0D + default:=0D + Status =3D EFI_ACCESS_DENIED;=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +/**=0D + The GetAttributes() function retrieves the attributes and=0D + current settings of the block.=0D +=0D + @param[in] This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL= =0D + instance.=0D + @param[out] Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the=0D + attributes and current settings are=0D + returned. Type EFI_FVB_ATTRIBUTES_2 is defined=0D + in EFI_FIRMWARE_VOLUME_HEADER.=0D +=0D + @retval EFI_SUCCESS The firmware volume attributes were=0D + returned.=0D +=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +OpTeeRpmbFvbGetAttributes (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D + OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D + )=0D +{=0D + *Attributes =3D EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled=0D + EFI_FVB2_READ_STATUS | // Reads are currently enabl= ed=0D + EFI_FVB2_WRITE_STATUS | // Writes are currently enab= led=0D + EFI_FVB2_WRITE_ENABLED_CAP | // Writes may be 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 ta= ke this value (i.e. '1')=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + The SetAttributes() function sets configurable firmware volume=0D + attributes and returns the new settings of the firmware volume.=0D +=0D + @param[in] This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL= instance.=0D + @param[in] Attributes On input, Attributes is a pointer to=0D + EFI_FVB_ATTRIBUTES_2 that contains the=0D + desired firmware volume settings. On=0D + successful return, it contains the new=0D + settings of the firmware volume. Type=0D + EFI_FVB_ATTRIBUTES_2 is defined in=0D + EFI_FIRMWARE_VOLUME_HEADER.=0D +=0D + @retval EFI_UNSUPPORTED Set attributes not supported=0D +=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +OpTeeRpmbFvbSetAttributes (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D + IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D + )=0D +{=0D + DEBUG ((DEBUG_ERROR, "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[in] This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PR= OTOCOL instance.=0D + @param[out] 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 retur= ned.=0D + @retval EFI_UNSUPPORTED The firmware volume is not memory mapped.= =0D +=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +OpTeeRpmbFvbGetPhysicalAddress (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D + OUT EFI_PHYSICAL_ADDRESS *Address=0D + )=0D +{=0D + MEM_INSTANCE *Instance;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D + *Address =3D Instance->MemBaseAddress;=0D +=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[in] This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_= PROTOCOL instance.=0D + @param[in] Lba Indicates the block for which to return = the size.=0D + @param[out] BlockSize Pointer to a caller-allocated UINTN in w= hich=0D + the size of the block is returned.=0D + @param[out] 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 + @retval EFI_SUCCESS The firmware volume base address was ret= urned.=0D + @retval EFI_INVALID_PARAMETER The requested LBA is out of range.=0D +=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +OpTeeRpmbFvbGetBlockSize (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D + IN EFI_LBA Lba,=0D + OUT UINTN *BlockSize,=0D + OUT UINTN *NumberOfBlocks=0D + )=0D +{=0D + MEM_INSTANCE *Instance;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D + if (Lba > Instance->NBlocks) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + *NumberOfBlocks =3D Instance->NBlocks - (UINTN)Lba;=0D + *BlockSize =3D Instance->BlockSize;=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Reads the specified number of bytes into a buffer from the specified blo= ck.=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[in] This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROT= OCOL instance.=0D + @param[in] Lba The starting logical block index=0D + @param[in] Offset Offset into the block at which to begin read= ing.=0D + @param[in,out] NumBytes Pointer to a UINTN. At entry, *NumBytes=0D + contains the total size of the buffer. At=0D + exit, *NumBytes contains the total number of= =0D + bytes read.=0D + @param[in,out] Buffer Pointer to a caller-allocated buffer that wi= ll=0D + be used to hold the data that is read.=0D +=0D + @retval EFI_SUCCESS The firmware volume was read successfully,=0D + and contents are in Buffer.=0D + @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA=0D + boundary. On output, NumBytes=0D + contains the total number of bytes=0D + returned in Buffer.=0D + @retval EFI_ACCESS_DENIED The firmware volume is in the=0D + ReadDisabled state.=0D + @retval EFI_DEVICE_ERROR The block device is not=0D + functioning correctly and could=0D + not be read.=0D +=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +OpTeeRpmbFvbRead (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK_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 Status;=0D + MEM_INSTANCE *Instance;=0D + VOID *Base;=0D +=0D + Status =3D EFI_SUCCESS;=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D + if (!Instance->Initialized) {=0D + Status =3D Instance->Initialize (Instance);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D + }=0D +=0D + Base =3D (VOID *)Instance->MemBaseAddress + (Lba * Instance->BlockSize) = + Offset;=0D + // We could read the data from the RPMB instead of memory=0D + // The 2 copies should already be identical=0D + // Copy from memory image=0D + CopyMem (Buffer, Base, *NumBytes);=0D +=0D + return Status;=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[in] This Indicates the EFI_FIRMWARE_VOLUME_BLO= CK_PROTOCOL=0D + instance.=0D + @param[in] Lba The starting logical block index to w= rite to.=0D + @param[in] Offset Offset into the block at which to beg= in writing.=0D + @param[in,out] NumBytes The pointer to a UINTN. At entry, *Nu= mBytes=0D + contains the total size of the buffer= . At=0D + exit, *NumBytes contains the total nu= mber of=0D + bytes actually written.=0D + @param[in] Buffer The pointer to a caller-allocated buf= fer=0D + that contains the source for the writ= e.=0D +=0D + @retval EFI_SUCCESS The firmware volume was written=0D + successfully.=0D + @retval EFI_BAD_BUFFER_SIZE The write was attempted across an=0D + LBA boundary. On output, NumBytes=0D + contains the total number of bytes=0D + actually written.=0D + @retval EFI_ACCESS_DENIED The firmware volume is in the=0D + WriteDisabled state.=0D + @retval EFI_DEVICE_ERROR The block device is malfunctioning=0D + and could not be written.=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +OpTeeRpmbFvbWrite (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN OUT UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + )=0D +{=0D + MEM_INSTANCE *Instance;=0D + EFI_STATUS Status;=0D + VOID *Base;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D + if (!Instance->Initialized) {=0D + Status =3D Instance->Initialize (Instance);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D + }=0D + Base =3D (VOID *)Instance->MemBaseAddress + Lba * Instance->BlockSize + = Offset;=0D + Status =3D ReadWriteRpmb (=0D + SP_SVC_RPMB_WRITE,=0D + (UINTN)Buffer,=0D + *NumBytes,=0D + (Lba * Instance->BlockSize) + Offset=0D + );=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D + // Update the memory copy=0D + CopyMem (Base, Buffer, *NumBytes);=0D +=0D + return Status;=0D +}=0D +=0D +/**=0D + Erases and initializes 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[in] This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PR= OTOCOL=0D + instance.=0D + @param[in] ... The variable argument list is a list of tu= ples.=0D + Each tuple describes a range of LBAs to er= ase=0D + and consists of the following:=0D + - An EFI_LBA that indicates the starting L= BA=0D + - A UINTN that indicates the number of blo= cks to=0D + erase.=0D +=0D + The list is terminated with an=0D + EFI_LBA_LIST_TERMINATOR. For example, the= =0D + following indicates that two ranges of blo= cks=0D + (5-7 and 10-11) are to be erased: EraseBlo= cks=0D + (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATO= R);=0D +=0D + @retval EFI_SUCCESS The erase request successfully=0D + completed.=0D + @retval EFI_ACCESS_DENIED The firmware volume is in the=0D + WriteDisabled state.=0D + @retval EFI_DEVICE_ERROR The block device is not functioning=0D + correctly and could not be written.=0D + The firmware device may have been=0D + partially erased.=0D + @retval EFI_INVALID_PARAMETER One or more of the LBAs listed=0D + in the variable argument list do=0D + not exist in the firmware volume.=0D +=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +OpTeeRpmbFvbErase (=0D + IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D + ...=0D + )=0D +{=0D + MEM_INSTANCE *Instance;=0D + UINTN NumBytes;=0D + UINTN NumLba;=0D + EFI_LBA Start;=0D + VOID *Base;=0D + VOID *Buf;=0D + VA_LIST Args;=0D + EFI_STATUS Status;=0D +=0D + Instance =3D INSTANCE_FROM_FVB_THIS (This);=0D +=0D + VA_START (Args, This);=0D + for (Start =3D VA_ARG (Args, EFI_LBA);=0D + Start !=3D EFI_LBA_LIST_TERMINATOR;=0D + Start =3D VA_ARG (Args, EFI_LBA)) {=0D + NumLba =3D VA_ARG (Args, UINTN);=0D + if (NumLba =3D=3D 0 || Start + NumLba > Instance->NBlocks) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D + NumBytes =3D NumLba * Instance->BlockSize;=0D + Base =3D (VOID *)Instance->MemBaseAddress + Start * Instance->BlockSiz= e;=0D + Buf =3D AllocatePool (NumLba * Instance->BlockSize);=0D + if (Buf =3D=3D NULL) {=0D + return EFI_DEVICE_ERROR;=0D + }=0D + SetMem64 (Buf, NumLba * Instance->BlockSize, ~0UL);=0D + // Write the device=0D + Status =3D ReadWriteRpmb (=0D + SP_SVC_RPMB_WRITE,=0D + (UINTN)Buf,=0D + NumBytes,=0D + Start * Instance->BlockSize=0D + );=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D + // Update the in memory copy=0D + SetMem64 (Base, NumLba * Instance->BlockSize, ~0UL);=0D + FreePool (Buf);=0D + }=0D +=0D + VA_END (Args);=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Since we use a memory backed storage we need to restore the RPMB content= s=0D + into memory before we register the Fvb protocol.=0D +=0D + @param Instance Address to copy flash contents to=0D +**/=0D +STATIC=0D +VOID=0D +ReadEntireFlash (=0D + IN MEM_INSTANCE *Instance=0D + )=0D +{=0D + UINTN ReadAddr;=0D + UINTN StorageFtwWorkingSize;=0D + UINTN StorageVariableSize;=0D + UINTN StorageFtwSpareSize;=0D +=0D + StorageFtwWorkingSize =3D PcdGet32(PcdFlashNvStorageFtwWorkingSize);=0D + StorageVariableSize =3D PcdGet32(PcdFlashNvStorageVariableSize);=0D + StorageFtwSpareSize =3D PcdGet32(PcdFlashNvStorageFtwSpareSize);=0D +=0D + ReadAddr =3D Instance->MemBaseAddress;=0D + // There's no need to check if the read failed here. The upper EDK2 laye= rs=0D + // will initialize the flash correctly if the in-memory copy is wrong=0D + ReadWriteRpmb (=0D + SP_SVC_RPMB_READ,=0D + ReadAddr,=0D + StorageVariableSize + StorageFtwWorkingSize + StorageFtwSpareSize,=0D + 0=0D + );=0D +}=0D +=0D +/**=0D + Validate the firmware volume header.=0D +=0D + @param[in] FwVolHeader Pointer to the firmware volume= =0D + header for the RPMB=0D +=0D + @retval EFI_SUCCESS The firmware volume header is correct=0D + @retval EFI_NOT_FOUND No header present=0D + @retval EFI_VOLUME_CORRUPTED The firmware volume header CRC was invalid= =0D + or either one of GUID's, Signature and hea= der=0D + length was invalid=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +EFIAPI=0D +ValidateFvHeader (=0D + IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader=0D + )=0D +{=0D + UINT16 Checksum;=0D + VARIABLE_STORE_HEADER *VariableStoreHeader;=0D + UINTN VariableStoreLength;=0D + UINTN FvLength;=0D +=0D + FvLength =3D PcdGet32(PcdFlashNvStorageVariableSize) +=0D + PcdGet32(PcdFlashNvStorageFtwWorkingSize) +=0D + PcdGet32(PcdFlashNvStorageFtwSpareSize);=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 + DEBUG ((DEBUG_INFO, "%a: No Firmware Volume header present\n",=0D + __FUNCTION__));=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + // Check the Firmware Volume Guid=0D + if (!CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid)= ) {=0D + DEBUG ((DEBUG_INFO, "%a: Firmware Volume Guid non-compatible\n",=0D + __FUNCTION__));=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + // Verify the header checksum=0D + Checksum =3D CalculateSum16((UINT16*)FwVolHeader, FwVolHeader->HeaderLen= gth);=0D + if (Checksum !=3D 0) {=0D + DEBUG ((DEBUG_INFO, "%a: FV checksum is invalid (Checksum:0x%X)\n",=0D + __FUNCTION__, Checksum));=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + VariableStoreHeader =3D (VARIABLE_STORE_HEADER*)((UINTN)FwVolHeader +=0D + FwVolHeader->HeaderLength);=0D +=0D + // Check the Variable Store Guid=0D + if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&= =0D + !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVar= iableGuid)) {=0D + DEBUG ((DEBUG_INFO, "%a: Variable Store Guid non-compatible\n", __FUNC= TION__));=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + VariableStoreLength =3D PcdGet32 (PcdFlashNvStorageVariableSize) -=0D + FwVolHeader->HeaderLength;=0D + if (VariableStoreHeader->Size !=3D VariableStoreLength) {=0D + DEBUG ((DEBUG_INFO, "%a: Variable Store Length does not match\n",=0D + __FUNCTION__));=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Initialize Fvb and variable storage headers for the RPMB storage.=0D +=0D + @param[in,out] Instance MEM_INSTANCE pointer describing the device = and=0D + the Firmware Block Protocol=0D +=0D + @retval EFI_SUCCESS read/write ok=0D + @retval EFI_UNSUPPORTED SVC to op-tee not supported=0D + @retval EFI_INVALID_PARAMETER SVC to op-tee had an invalid par= am=0D + @retval EFI_ACCESS_DENIED SVC to op-tee was denied=0D + @retval EFI_OUT_OF_RESOURCES op-tee out of memory=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +InitializeFvAndVariableStoreHeaders (=0D + IN OUT MEM_INSTANCE *Instance=0D + )=0D +{=0D + EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader;=0D + VARIABLE_STORE_HEADER *VariableStoreHeader;=0D + EFI_STATUS Status;=0D + UINTN HeadersLength;=0D + VOID* Headers;=0D +=0D + HeadersLength =3D sizeof (EFI_FIRMWARE_VOLUME_HEADER) +=0D + sizeof (EFI_FV_BLOCK_MAP_ENTRY) +=0D + sizeof (VARIABLE_STORE_HEADER);=0D + Headers =3D AllocateZeroPool (HeadersLength);=0D + if (Headers =3D=3D NULL) {=0D + return EFI_OUT_OF_RESOURCES;=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 PcdGet32(PcdFlashNvStorageVariableSiz= e) +=0D + PcdGet32(PcdFlashNvStorageFtwWorkingSiz= e) +=0D + PcdGet32(PcdFlashNvStorageFtwSpareSize)= ;=0D + FirmwareVolumeHeader->Signature =3D EFI_FVH_SIGNATURE;=0D + FirmwareVolumeHeader->Attributes =3D EFI_FVB2_READ_ENABLED_CAP |=0D + EFI_FVB2_READ_STATUS |=0D + EFI_FVB2_STICKY_WRITE |=0D + EFI_FVB2_MEMORY_MAPPED |=0D + EFI_FVB2_ERASE_POLARITY |=0D + EFI_FVB2_WRITE_STATUS |=0D + EFI_FVB2_WRITE_ENABLED_CAP;=0D +=0D + FirmwareVolumeHeader->HeaderLength =3D sizeof (EFI_FIRMWARE_VOLUME_HEADE= R) +=0D + sizeof (EFI_FV_BLOCK_MAP_ENTRY);=0D + FirmwareVolumeHeader->Revision =3D EFI_FVH_REVISION;=0D + FirmwareVolumeHeader->BlockMap[0].NumBlocks =3D Instance->NBlocks;=0D + FirmwareVolumeHeader->BlockMap[0].Length =3D Instance->BlockSize;=0D + FirmwareVolumeHeader->BlockMap[1].NumBlocks =3D 0;=0D + FirmwareVolumeHeader->BlockMap[1].Length =3D 0;=0D + FirmwareVolumeHeader->Checksum =3D CalculateCheckSum16 (=0D + (UINT16*)FirmwareVolumeHeader,=0D + FirmwareVolumeHeader->HeaderLength=0D + );=0D +=0D + //=0D + // VARIABLE_STORE_HEADER=0D + //=0D + VariableStoreHeader =3D (VARIABLE_STORE_HEADER*)((UINTN)Headers +=0D + FirmwareVolumeHeader->HeaderLength);=0D + CopyGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGui= d);=0D + VariableStoreHeader->Size =3D PcdGet32(PcdFlashNvStorageVariableSize) -= =0D + FirmwareVolumeHeader->HeaderLength;=0D + VariableStoreHeader->Format =3D VARIABLE_STORE_FORMATTED;=0D + VariableStoreHeader->State =3D VARIABLE_STORE_HEALTHY;=0D +=0D + Status =3D ReadWriteRpmb (SP_SVC_RPMB_WRITE, (UINTN)Headers, HeadersLeng= th, 0);=0D + if (EFI_ERROR (Status)) {=0D + goto Exit;=0D + }=0D + // Install the combined header in memory=0D + CopyMem ((VOID*)Instance->MemBaseAddress, Headers, HeadersLength);=0D +=0D +Exit:=0D + FreePool (Headers);=0D + return Status;=0D +}=0D +=0D +/**=0D + Initialize the firmware block protocol for the specified memory.=0D +=0D + @param[in,out] Instance MEM_INSTANCE pointer describing= the device and the=0D + Firmware Block Protocol=0D +=0D + @retval EFI_SUCCESS Initialized successfully or alr= eady initialized=0D + @retval EFI_UNSUPPORTED SVC to op-tee not supported=0D + @retval EFI_INVALID_PARAMETER SVC to op-tee had an invalid pa= ram=0D + @retval EFI_ACCESS_DENIED SVC to op-tee was denied=0D + @retval EFI_OUT_OF_RESOURCES op-tee out of memory=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +EFIAPI=0D +FvbInitialize (=0D + IN OUT MEM_INSTANCE *Instance=0D + )=0D +{=0D + EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;=0D + EFI_STATUS Status;=0D +=0D + if (Instance->Initialized) {=0D + return EFI_SUCCESS;=0D + }=0D +=0D + // FirmwareVolumeHeader->FvLength is declared to have the Variable area= =0D + // AND the FTW working area AND the FTW Spare contiguous.=0D + ASSERT (=0D + (PcdGet64 (PcdFlashNvStorageVariableBase64) +=0D + PcdGet32 (PcdFlashNvStorageVariableSize)) =3D=3D=0D + PcdGet64 (PcdFlashNvStorageFtwWorkingBase64)=0D + );=0D + ASSERT (=0D + (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) +=0D + PcdGet32 (PcdFlashNvStorageFtwWorkingSize)) =3D=3D=0D + PcdGet64 (PcdFlashNvStorageFtwSpareBase64));=0D +=0D + // Check if the size of the area is at least one block size=0D + ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) / Instance->BlockSize >= 0);=0D + ASSERT (PcdGet32 (PcdFlashNvStorageFtwWorkingSize) / Instance->BlockSize= > 0);=0D + ASSERT (PcdGet32 (PcdFlashNvStorageFtwSpareSize) / Instance->BlockSize >= 0);=0D +=0D + // Ensure the Variable areas are aligned on block size boundaries=0D + ASSERT ((PcdGet64 (PcdFlashNvStorageVariableBase64) % Instance->BlockSiz= e) =3D=3D 0);=0D + ASSERT ((PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) % Instance->BlockS= ize) =3D=3D 0);=0D + ASSERT ((PcdGet64 (PcdFlashNvStorageFtwSpareBase64) % Instance->BlockSiz= e) =3D=3D 0);=0D +=0D + // Read the file from disk and copy it to memory=0D + ReadEntireFlash (Instance);=0D +=0D + FwVolHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *)Instance->MemBaseAddress;= =0D + Status =3D ValidateFvHeader (FwVolHeader);=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 +=0D + // Reset memory=0D + SetMem64 ((VOID *)Instance->MemBaseAddress, Instance->NBlocks * Instan= ce->BlockSize, ~0UL);=0D + DEBUG ((DEBUG_INFO, "%a: Erasing Flash.\n", __FUNCTION__));=0D + Status =3D ReadWriteRpmb (=0D + SP_SVC_RPMB_WRITE,=0D + Instance->MemBaseAddress,=0D + PcdGet32(PcdFlashNvStorageVariableSize) +=0D + PcdGet32(PcdFlashNvStorageFtwWorkingSize) +=0D + PcdGet32(PcdFlashNvStorageFtwSpareSize),=0D + 0=0D + );=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D + // Install all appropriate headers=0D + DEBUG ((DEBUG_INFO, "%a: Installing a correct one for this volume.\n",= =0D + __FUNCTION__));=0D + Status =3D InitializeFvAndVariableStoreHeaders (Instance);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D + } else {=0D + DEBUG ((DEBUG_INFO, "%a: Found valid FVB Header.\n", __FUNCTION__));=0D + }=0D + Instance->Initialized =3D TRUE;=0D +=0D + return Status;=0D +}=0D +=0D +/**=0D + Since the RPMB is not byte addressable we need to allocate memory=0D + and sync that on reads/writes. Initialize the memory and install the=0D + Fvb protocol.=0D +=0D + @param[in] ImageHandle The EFI image handle=0D + @param[in] SystemTable MM system table=0D +=0D + @retval EFI_SUCCESS Protocol installed=0D + @retval EFI_INVALID_PARAMETER Invalid Pcd values specified=0D + @retval EFI_OUT_OF_RESOURCES Can't allocate necessary memory=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +OpTeeRpmbFvbInit (=0D + IN EFI_HANDLE ImageHandle,=0D + IN EFI_MM_SYSTEM_TABLE *SystemTable=0D + )=0D +{=0D + EFI_STATUS Status;=0D + VOID *Addr;=0D + UINTN FvLength;=0D + UINTN NBlocks;=0D +=0D + FvLength =3D PcdGet32(PcdFlashNvStorageVariableSize) +=0D + PcdGet32(PcdFlashNvStorageFtwWorkingSize) +=0D + PcdGet32(PcdFlashNvStorageFtwSpareSize);=0D +=0D + NBlocks =3D EFI_SIZE_TO_PAGES (ALIGN_VARIABLE (FvLength));=0D + Addr =3D AllocatePages (NBlocks);=0D + if (Addr =3D=3D NULL) {=0D + ASSERT (0);=0D + return EFI_OUT_OF_RESOURCES;=0D + }=0D +=0D + ZeroMem (&mInstance, sizeof (mInstance));=0D +=0D + mInstance.FvbProtocol.GetPhysicalAddress =3D OpTeeRpmbFvbGetPhysicalAddr= ess;=0D + mInstance.FvbProtocol.GetAttributes =3D OpTeeRpmbFvbGetAttributes;= =0D + mInstance.FvbProtocol.SetAttributes =3D OpTeeRpmbFvbSetAttributes;= =0D + mInstance.FvbProtocol.GetBlockSize =3D OpTeeRpmbFvbGetBlockSize;=0D + mInstance.FvbProtocol.EraseBlocks =3D OpTeeRpmbFvbErase;=0D + mInstance.FvbProtocol.Write =3D OpTeeRpmbFvbWrite;=0D + mInstance.FvbProtocol.Read =3D OpTeeRpmbFvbRead;=0D +=0D + mInstance.MemBaseAddress =3D (EFI_PHYSICAL_ADDRESS)Addr;=0D + mInstance.Signature =3D FLASH_SIGNATURE;=0D + mInstance.Initialize =3D FvbInitialize;=0D + mInstance.BlockSize =3D EFI_PAGE_SIZE;=0D + mInstance.NBlocks =3D NBlocks;=0D +=0D + // Update the defined PCDs related to Variable Storage=0D + PatchPcdSet64 (PcdFlashNvStorageVariableBase64, mInstance.MemBaseAddress= );=0D + PatchPcdSet64 (=0D + PcdFlashNvStorageFtwWorkingBase64,=0D + mInstance.MemBaseAddress + PcdGet32 (PcdFlashNvStorageVariableSize)=0D + );=0D + PatchPcdSet64 (=0D + PcdFlashNvStorageFtwSpareBase64,=0D + mInstance.MemBaseAddress +=0D + PcdGet32 (PcdFlashNvStorageVariableSize) +=0D + PcdGet32 (PcdFlashNvStorageFtwWorkingSize)=0D + );=0D +=0D + Status =3D gMmst->MmInstallProtocolInterface (=0D + &mInstance.Handle,=0D + &gEfiSmmFirmwareVolumeBlockProtocolGuid,=0D + EFI_NATIVE_INTERFACE,=0D + &mInstance.FvbProtocol=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + DEBUG ((DEBUG_INFO, "%a: Register OP-TEE RPMB Fvb\n", __FUNCTION__));=0D + DEBUG ((DEBUG_INFO, "%a: Using NV store FV in-memory copy at 0x%lx\n",=0D + __FUNCTION__, PatchPcdGet64 (PcdFlashNvStorageVariableBase64)));=0D +=0D + return Status;=0D +}=0D diff --git a/Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFvb.h b/Drivers/OpTee/Opte= eRpmbPkg/OpTeeRpmbFvb.h new file mode 100644 index 000000000000..8a8e67160758 --- /dev/null +++ b/Drivers/OpTee/OpteeRpmbPkg/OpTeeRpmbFvb.h @@ -0,0 +1,51 @@ +/** @file=0D +=0D + Copyright (c) 2020, Linaro Ltd. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef OPTEE_RPMB_FVB_H_=0D +#define OPTEE_RPMB_FVB_H_=0D +=0D +/**=0D + Those are not currently defined in any spec, it's an internal=0D + contract between OP-TEE and EDK2.=0D + For more details check core/arch/arm/include/kernel/stmm_sp.h in OP-TEE=0D +**/=0D +#define SP_SVC_RPMB_READ 0xC4000066=0D +#define SP_SVC_RPMB_WRITE 0xC4000067=0D +=0D +#define FLASH_SIGNATURE SIGNATURE_32('r', 'p', 'm', 'b')=0D +#define INSTANCE_FROM_FVB_THIS(a) CR(a, MEM_INSTANCE, FvbProtocol, \=0D + FLASH_SIGNATURE)=0D +=0D +typedef struct _MEM_INSTANCE MEM_INSTANCE;=0D +typedef EFI_STATUS (*MEM_INITIALIZE) (MEM_INSTANCE* Instance);=0D +=0D +/**=0D + This struct is used by the RPMB driver. Since the upper EDK2 layers=0D + expect byte addressable memory, we allocate a memory area of certain=0D + size and sync it to the hardware on reads/writes.=0D +**/=0D +struct _MEM_INSTANCE=0D +{=0D + /// Internal signature used to discover the instance=0D + UINT32 Signature;=0D + /// Function used to initialize the instance=0D + MEM_INITIALIZE Initialize;=0D + /// Set to true if initialized=0D + BOOLEAN Initialized;=0D + /// FVB protocol of the instance=0D + EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL FvbProtocol;=0D + /// Handle to install=0D + EFI_HANDLE Handle;=0D + /// Physical address of the beggining of the allocated memory=0D + EFI_PHYSICAL_ADDRESS MemBaseAddress;=0D + /// Blocksize=0D + UINT16 BlockSize;=0D + /// Number of allocated blocks=0D + UINT16 NBlocks;=0D +};=0D +=0D +#endif=0D --=20 2.30.1