From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smarthost01a.sbp.mail.zen.net.uk (smarthost01a.sbp.mail.zen.net.uk [212.23.1.1]) by mx.groups.io with SMTP id smtpd.web12.28130.1647853822703868699 for ; Mon, 21 Mar 2022 02:10:23 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=permerror, err=parse error for token &{10 18 sdn.klaviyomail.com}: permanent DNS error (domain: starlabs.systems, ip: 212.23.1.1, mailfrom: sean@starlabs.systems) Received: from [51.148.147.4] (helo=sean-StarBook.lan) by smarthost01a.sbp.mail.zen.net.uk with esmtp (Exim 4.90_1) (envelope-from ) id 1nWE3Q-0007HO-Ej; Mon, 21 Mar 2022 09:10:20 +0000 From: "Sean Rhodes" To: devel@edk2.groups.io Cc: Patrick Rudolph , Guo Dong , Ray Ni , Maurice Ma , Benjamin You , Sean Rhodes Subject: [PATCH 4/5] UefiPayloadPkg: Add SmmStoreLib Date: Mon, 21 Mar 2022 09:10:12 +0000 Message-Id: <80d803b79934aad97484164026c00f6549e257ca.1647853813.git.sean@starlabs.systems> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 X-Originating-smarthost01a-IP: [51.148.147.4] Feedback-ID: 51.148.147.4 Content-Transfer-Encoding: quoted-printable From: Patrick Rudolph Implement all of the FVB protocol functions on top of the SmmStore as a library. The library consumes the introduced gEfiSmmStoreInfoHobGuid. The SMI handler uses a fixed communication buffer in reserved DRAM. To initiate a transaction you must write to the I/O APM_CNT port. Tests on Intel(R) Xeon(R) E-2288G CPU @ 3.70G showed that the SMI isn't triggered with a probability of 1:40 of all cases when called in a tight loop. The CPU continues running and the SMI is triggeres asynchronously a few clock cycles later. coreboot only handels synchronous APM request and does nothing on asynchronous APM triggers. As there's no livesign from SMM it's impossible to tell if the handler has run. Just wait a bit and try again to trigger a synchronous SMI. Tests confirmed that out of 5 million tries the SMI is now always handled. When a synchronous SMI happens with the correct write to the APM_CNT port, the ebx register is checked first that it doesn't point to SMRAM. If it doesn't it's used to read in the arguments that define a SmmStore transaction. The SMI handler will only operate on a predefined and memory mapped region = in the boot media. Cc: Guo Dong Cc: Ray Ni Cc: Maurice Ma Cc: Benjamin You Cc: Sean Rhodes Signed-off-by: Patrick Rudolph --- UefiPayloadPkg/Include/Library/SmmStoreLib.h | 120 +++++ UefiPayloadPkg/Library/SmmStoreLib/SmmStore.c | 473 ++++++++++++++++++ UefiPayloadPkg/Library/SmmStoreLib/SmmStore.h | 81 +++ .../Library/SmmStoreLib/SmmStoreLib.inf | 41 ++ .../Library/SmmStoreLib/X64/SmmStore.nasm | 48 ++ UefiPayloadPkg/UefiPayloadPkg.dsc | 1 + 6 files changed, 764 insertions(+) create mode 100644 UefiPayloadPkg/Include/Library/SmmStoreLib.h create mode 100644 UefiPayloadPkg/Library/SmmStoreLib/SmmStore.c create mode 100644 UefiPayloadPkg/Library/SmmStoreLib/SmmStore.h create mode 100644 UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf create mode 100644 UefiPayloadPkg/Library/SmmStoreLib/X64/SmmStore.nasm diff --git a/UefiPayloadPkg/Include/Library/SmmStoreLib.h b/UefiPayloadPkg/= Include/Library/SmmStoreLib.h new file mode 100644 index 0000000000..6ade375b1d --- /dev/null +++ b/UefiPayloadPkg/Include/Library/SmmStoreLib.h @@ -0,0 +1,120 @@ +/** @file SmmStoreLib.h=0D +=0D + Copyright (c) 2022, 9elements GmbH
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef SMM_STORE_LIB_H_=0D +#define SMM_STORE_LIB_H_=0D +=0D +#include =0D +#include =0D +#include =0D +=0D +#define SMMSTORE_COMBUF_SIZE 16=0D +=0D +/**=0D + Get the SmmStore block size=0D +=0D + @param BlockSize The pointer to store the block size in.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibGetBlockSize (=0D + OUT UINTN *BlockSize=0D + );=0D +=0D +/**=0D + Get the SmmStore number of blocks=0D +=0D + @param NumBlocks The pointer to store the number of blocks in.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibGetNumBlocks (=0D + OUT UINTN *NumBlocks=0D + );=0D +=0D +/**=0D + Get the SmmStore MMIO address=0D +=0D + @param MmioAddress The pointer to store the address in.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibGetMmioAddress (=0D + OUT EFI_PHYSICAL_ADDRESS *MmioAddress=0D + );=0D +=0D +/**=0D + Read from SmmStore=0D +=0D + @param[in] Lba The starting logical block index to read from.=0D + @param[in] Offset Offset into the block at which to begin reading.=0D + @param[in] NumBytes On input, indicates the requested read size. On=0D + output, indicates the actual number of bytes read=0D + @param[in] Buffer Pointer to the buffer to read into.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibRead (=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + );=0D +=0D +/**=0D + Write to SmmStore=0D +=0D + @param[in] Lba The starting logical block index to write to.=0D + @param[in] Offset Offset into the block at which to begin writing.=0D + @param[in] NumBytes On input, indicates the requested write size. On=0D + output, indicates the actual number of bytes written= =0D + @param[in] Buffer Pointer to the data to write.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibWrite (=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + );=0D +=0D +/**=0D + Erase a block using the SmmStore=0D +=0D + @param Lba The logical block index to erase.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibEraseBlock (=0D + IN EFI_LBA Lba=0D + );=0D +=0D +/**=0D + Initializes SmmStore support=0D +=0D + @retval EFI_WRITE_PROTECTED The SmmStore is not present.=0D + @retval EFI_UNSUPPORTED The SmmStoreInfo HOB wasn't found.=0D + @retval EFI_SUCCESS The SmmStore is supported.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibInitialize (=0D + VOID=0D + );=0D +=0D +/**=0D + Denitializes SmmStore support=0D +**/=0D +VOID=0D +EFIAPI=0D +SmmStoreLibDeinitialize (=0D + VOID=0D + );=0D +=0D +#endif /* SMM_STORE_LIB_H_ */=0D diff --git a/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.c b/UefiPayloadPkg= /Library/SmmStoreLib/SmmStore.c new file mode 100644 index 0000000000..25426a4d1a --- /dev/null +++ b/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.c @@ -0,0 +1,473 @@ +/** @file SmmStore.c=0D +=0D + Copyright (c) 2022, 9elements GmbH
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include "SmmStore.h"=0D +=0D +/*=0D + * A memory buffer to place arguments in.=0D + */=0D +STATIC SMM_STORE_COM_BUF *mArgComBuf;=0D +STATIC EFI_PHYSICAL_ADDRESS mArgComBufPhys;=0D +=0D +/*=0D + * Metadata provided by the first stage bootloader.=0D + */=0D +STATIC SMMSTORE_INFO *mSmmStoreInfo;=0D +=0D +STATIC EFI_EVENT mSmmStoreLibVirtualAddrChangeEvent;=0D +=0D +/**=0D + Calls into SMM to use the SMMSTOREv2 implementation for persistent stora= ge.=0D +=0D + @param Cmd The command to write into the APM port. This allows to en= ter the=0D + Smi special command handler.=0D + @param SubCmd The subcommand to execute in the Smi handler.=0D + @param Arg Optional argument to pass to the Smi handler. Typically a= pointer=0D + in 'flat' memory mode, which points to read only memory.= =0D +=0D + @retval EFI_NO_RESPONSE The SmmStore is not present or didn't resp= onse.=0D + @retval EFI_UNSUPPORTED The request isn't suppored.=0D + @retval EFI_DEVICE_ERROR An error occured while executing the reque= st.=0D + @retval EFI_SUCCESS The operation was executed successfully.=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +CallSmm (=0D + UINT8 Cmd,=0D + UINT8 SubCmd,=0D + UINTN Arg=0D + )=0D +{=0D + CONST UINTN Rax =3D ((SubCmd << 8) | Cmd);=0D + CONST UINTN Rbx =3D Arg;=0D + UINTN Result;=0D +=0D + Result =3D TriggerSmi (Rax, Rbx, 5);=0D + if (Result =3D=3D Rax) {=0D + return EFI_NO_RESPONSE;=0D + } else if (Result =3D=3D SMMSTORE_RET_SUCCESS) {=0D + return EFI_SUCCESS;=0D + } else if (Result =3D=3D SMMSTORE_RET_UNSUPPORTED) {=0D + return EFI_UNSUPPORTED;=0D + }=0D +=0D + return EFI_DEVICE_ERROR;=0D +}=0D +=0D +/**=0D + Get the SmmStore block size=0D +=0D + @param BlockSize The pointer to store the block size in.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibGetBlockSize (=0D + OUT UINTN *BlockSize=0D + )=0D +{=0D + if (mSmmStoreInfo =3D=3D NULL) {=0D + return EFI_NO_MEDIA;=0D + }=0D +=0D + if (BlockSize =3D=3D NULL) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + *BlockSize =3D mSmmStoreInfo->BlockSize;=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Get the SmmStore number of blocks=0D +=0D + @param NumBlocks The pointer to store the number of blocks in.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibGetNumBlocks (=0D + OUT UINTN *NumBlocks=0D + )=0D +{=0D + if (mSmmStoreInfo =3D=3D NULL) {=0D + return EFI_NO_MEDIA;=0D + }=0D +=0D + if (NumBlocks =3D=3D NULL) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + *NumBlocks =3D mSmmStoreInfo->NumBlocks;=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Get the SmmStore MMIO address=0D +=0D + @param MmioAddress The pointer to store the address in.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibGetMmioAddress (=0D + OUT EFI_PHYSICAL_ADDRESS *MmioAddress=0D + )=0D +{=0D + if (mSmmStoreInfo =3D=3D NULL) {=0D + return EFI_NO_MEDIA;=0D + }=0D +=0D + if (MmioAddress =3D=3D NULL) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + *MmioAddress =3D mSmmStoreInfo->MmioAddress;=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Read from SmmStore=0D +=0D + @param[in] Lba The starting logical block index to read from.=0D + @param[in] Offset Offset into the block at which to begin reading.=0D + @param[in] NumBytes On input, indicates the requested read size. On=0D + output, indicates the actual number of bytes read=0D + @param[in] Buffer Pointer to the buffer to read into.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibRead (=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + )=0D +{=0D + EFI_STATUS Status;=0D +=0D + if (mSmmStoreInfo =3D=3D NULL) {=0D + return EFI_NO_MEDIA;=0D + }=0D +=0D + if (Lba >=3D mSmmStoreInfo->NumBlocks) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + if (((*NumBytes + Offset) > mSmmStoreInfo->BlockSize) ||=0D + ((*NumBytes + Offset) > mSmmStoreInfo->ComBufferSize))=0D + {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + mArgComBuf->Read.BufSize =3D *NumBytes;=0D + mArgComBuf->Read.BufOffset =3D Offset;=0D + mArgComBuf->Read.BlockId =3D Lba;=0D +=0D + Status =3D CallSmm (mSmmStoreInfo->ApmCmd, SMMSTORE_CMD_RAW_READ, mArgCo= mBufPhys);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D + CopyMem (Buffer, (VOID *)(UINTN)(mSmmStoreInfo->ComBuffer + Offset), *Nu= mBytes);=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Write to SmmStore=0D +=0D + @param[in] Lba The starting logical block index to write to.=0D + @param[in] Offset Offset into the block at which to begin writing.=0D + @param[in] NumBytes On input, indicates the requested write size. On=0D + output, indicates the actual number of bytes written= =0D + @param[in] Buffer Pointer to the data to write.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibWrite (=0D + IN EFI_LBA Lba,=0D + IN UINTN Offset,=0D + IN UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + )=0D +{=0D + if (mSmmStoreInfo =3D=3D NULL) {=0D + return EFI_NO_MEDIA;=0D + }=0D +=0D + if (Lba >=3D mSmmStoreInfo->NumBlocks) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + if (((*NumBytes + Offset) > mSmmStoreInfo->BlockSize) ||=0D + ((*NumBytes + Offset) > mSmmStoreInfo->ComBufferSize))=0D + {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + mArgComBuf->Write.BufSize =3D *NumBytes;=0D + mArgComBuf->Write.BufOffset =3D Offset;=0D + mArgComBuf->Write.BlockId =3D Lba;=0D +=0D + CopyMem ((VOID *)(UINTN)(mSmmStoreInfo->ComBuffer + Offset), Buffer, *Nu= mBytes);=0D +=0D + return CallSmm (mSmmStoreInfo->ApmCmd, SMMSTORE_CMD_RAW_WRITE, mArgComBu= fPhys);=0D +}=0D +=0D +/**=0D + Erase a SmmStore block=0D +=0D + @param Lba The logical block index to erase.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibEraseBlock (=0D + IN EFI_LBA Lba=0D + )=0D +{=0D + if (mSmmStoreInfo =3D=3D NULL) {=0D + return EFI_NO_MEDIA;=0D + }=0D +=0D + if (Lba >=3D mSmmStoreInfo->NumBlocks) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + mArgComBuf->Clear.BlockId =3D Lba;=0D +=0D + return CallSmm (mSmmStoreInfo->ApmCmd, SMMSTORE_CMD_RAW_CLEAR, mArgComBu= fPhys);=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 +STATIC=0D +VOID=0D +EFIAPI=0D +SmmStoreLibVirtualNotifyEvent (=0D + IN EFI_EVENT Event,=0D + IN VOID *Context=0D + )=0D +{=0D + EfiConvertPointer (0x0, (VOID **)&mArgComBuf);=0D + if (mSmmStoreInfo !=3D NULL) {=0D + EfiConvertPointer (0x0, (VOID **)&mSmmStoreInfo->ComBuffer);=0D + EfiConvertPointer (0x0, (VOID **)&mSmmStoreInfo);=0D + }=0D +=0D + return;=0D +}=0D +=0D +/**=0D + Initializes SmmStore support=0D +=0D + @retval EFI_WRITE_PROTECTED The SmmStore is not present.=0D + @retval EFI_OUT_OF_RESOURCES Run out of memory.=0D + @retval EFI_SUCCESS The SmmStore is supported.=0D +=0D +**/=0D +EFI_STATUS=0D +SmmStoreLibInitialize (=0D + VOID=0D + )=0D +{=0D + EFI_STATUS Status;=0D + VOID *GuidHob;=0D + EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;=0D +=0D + //=0D + // Find the SmmStore information guid hob=0D + //=0D + GuidHob =3D GetFirstGuidHob (&gEfiSmmStoreInfoHobGuid);=0D + if (GuidHob =3D=3D NULL) {=0D + DEBUG ((DEBUG_WARN, "SmmStore not supported! Skipping driver init.\n")= );=0D + return EFI_UNSUPPORTED;=0D + }=0D +=0D + //=0D + // Place SmmStore information hob in a runtime buffer=0D + //=0D + mSmmStoreInfo =3D AllocateRuntimePool (GET_GUID_HOB_DATA_SIZE (GuidHob))= ;=0D + if (mSmmStoreInfo =3D=3D NULL) {=0D + return EFI_OUT_OF_RESOURCES;=0D + }=0D +=0D + CopyMem (mSmmStoreInfo, GET_GUID_HOB_DATA (GuidHob), GET_GUID_HOB_DATA_S= IZE (GuidHob));=0D +=0D + //=0D + // Validate input=0D + //=0D + if ((mSmmStoreInfo->MmioAddress =3D=3D 0) ||=0D + (mSmmStoreInfo->ComBuffer =3D=3D 0) ||=0D + (mSmmStoreInfo->BlockSize =3D=3D 0) ||=0D + (mSmmStoreInfo->NumBlocks =3D=3D 0))=0D + {=0D + DEBUG ((DEBUG_ERROR, "%a: Invalid data in SmmStore Info hob\n", __FUNC= TION__));=0D + FreePool (mSmmStoreInfo);=0D + mSmmStoreInfo =3D NULL;=0D + return EFI_WRITE_PROTECTED;=0D + }=0D +=0D + //=0D + // Allocate Communication Buffer for arguments to pass to SMM.=0D + // The argument com buffer is only read by SMM, but never written.=0D + // The FVB data send/retrieved will be placed in a separate bootloader=0D + // pre-allocated memory region, the ComBuffer.=0D + //=0D + if (mSmmStoreInfo->ComBuffer < BASE_4GB) {=0D + //=0D + // Assume that SMM handler is running in 32-bit mode when ComBuffer is= =0D + // is placed below BASE_4GB.=0D + //=0D + mArgComBufPhys =3D BASE_4GB - 1;=0D + } else {=0D + mArgComBufPhys =3D BASE_8EB - 1;=0D + }=0D +=0D + Status =3D gBS->AllocatePages (=0D + AllocateMaxAddress,=0D + EfiRuntimeServicesData,=0D + EFI_SIZE_TO_PAGES (sizeof (SMM_STORE_COM_BUF)),=0D + &mArgComBufPhys=0D + );=0D +=0D + if (EFI_ERROR (Status)) {=0D + FreePool (mSmmStoreInfo);=0D + mSmmStoreInfo =3D NULL;=0D + return EFI_OUT_OF_RESOURCES;=0D + }=0D +=0D + mArgComBuf =3D (VOID *)mArgComBufPhys;=0D +=0D + //=0D + // Register for the virtual address change event=0D + //=0D + Status =3D gBS->CreateEventEx (=0D + EVT_NOTIFY_SIGNAL,=0D + TPL_NOTIFY,=0D + SmmStoreLibVirtualNotifyEvent,=0D + NULL,=0D + &gEfiEventVirtualAddressChangeGuid,=0D + &mSmmStoreLibVirtualAddrChangeEvent=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + //=0D + // Finally mark the SMM communication buffer provided by CB or SBL as ru= ntime memory=0D + //=0D + Status =3D gDS->GetMemorySpaceDescriptor (mSmmStoreInfo->ComBuffer, &Gcd= Descriptor);=0D + if (EFI_ERROR (Status) || (GcdDescriptor.GcdMemoryType !=3D EfiGcdMemory= TypeReserved)) {=0D + DEBUG (=0D + (=0D + DEBUG_INFO,=0D + "%a: No memory space descriptor for com buffer found\n",=0D + __FUNCTION__=0D + )=0D + );=0D +=0D + //=0D + // Add a new entry if not covered by existing mapping=0D + //=0D + Status =3D gDS->AddMemorySpace (=0D + EfiGcdMemoryTypeReserved,=0D + mSmmStoreInfo->ComBuffer,=0D + mSmmStoreInfo->ComBufferSize,=0D + EFI_MEMORY_WB | EFI_MEMORY_RUNTIME=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D + }=0D +=0D + //=0D + // Mark as runtime service=0D + //=0D + Status =3D gDS->SetMemorySpaceAttributes (=0D + mSmmStoreInfo->ComBuffer,=0D + mSmmStoreInfo->ComBufferSize,=0D + EFI_MEMORY_RUNTIME=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + //=0D + // Mark the memory mapped store as MMIO memory=0D + //=0D + Status =3D gDS->GetMemorySpaceDescriptor (mSmmStoreInfo->MmioAddress, &G= cdDescriptor);=0D + if (EFI_ERROR (Status) || (GcdDescriptor.GcdMemoryType !=3D EfiGcdMemory= TypeMemoryMappedIo)) {=0D + DEBUG (=0D + (=0D + DEBUG_INFO,=0D + "%a: No memory space descriptor for com buffer found\n",=0D + __FUNCTION__=0D + )=0D + );=0D +=0D + //=0D + // Add a new entry if not covered by existing mapping=0D + //=0D + Status =3D gDS->AddMemorySpace (=0D + EfiGcdMemoryTypeMemoryMappedIo,=0D + mSmmStoreInfo->MmioAddress,=0D + mSmmStoreInfo->NumBlocks * mSmmStoreInfo->BlockSize,=0D + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D + }=0D +=0D + //=0D + // Mark as runtime service=0D + //=0D + Status =3D gDS->SetMemorySpaceAttributes (=0D + mSmmStoreInfo->MmioAddress,=0D + mSmmStoreInfo->NumBlocks * mSmmStoreInfo->BlockSize,=0D + EFI_MEMORY_RUNTIME=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Denitializes SmmStore support by freeing allocated memory and unregister= ing=0D + the virtual address change event.=0D +**/=0D +VOID=0D +EFIAPI=0D +SmmStoreLibDeinitialize (=0D + VOID=0D + )=0D +{=0D + if (mArgComBuf !=3D NULL) {=0D + gBS->FreePages (mArgComBufPhys, EFI_SIZE_TO_PAGES (sizeof (SMM_STORE_C= OM_BUF)));=0D + mArgComBuf =3D NULL;=0D + }=0D +=0D + if (mSmmStoreInfo !=3D NULL) {=0D + FreePool (mSmmStoreInfo);=0D + mSmmStoreInfo =3D NULL;=0D + }=0D +=0D + if (mSmmStoreLibVirtualAddrChangeEvent !=3D NULL) {=0D + gBS->CloseEvent (mSmmStoreLibVirtualAddrChangeEvent);=0D + mSmmStoreLibVirtualAddrChangeEvent =3D NULL;=0D + }=0D +}=0D diff --git a/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.h b/UefiPayloadPkg= /Library/SmmStoreLib/SmmStore.h new file mode 100644 index 0000000000..619b00fa30 --- /dev/null +++ b/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.h @@ -0,0 +1,81 @@ +/** @file SmmStore.h=0D +=0D + Copyright (c) 2022, 9elements GmbH
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef COREBOOT_SMMSTORE_H_=0D +#define COREBOOT_SMMSTORE_H_=0D +=0D +#define SMMSTORE_RET_SUCCESS 0=0D +#define SMMSTORE_RET_FAILURE 1=0D +#define SMMSTORE_RET_UNSUPPORTED 2=0D +=0D +/* Version 2 only */=0D +#define SMMSTORE_CMD_INIT 4=0D +#define SMMSTORE_CMD_RAW_READ 5=0D +#define SMMSTORE_CMD_RAW_WRITE 6=0D +#define SMMSTORE_CMD_RAW_CLEAR 7=0D +=0D +/*=0D + * This allows the payload to store raw data in the flash regions.=0D + * This can be used by a FaultTolerantWrite implementation, that uses at l= east=0D + * two regions in an A/B update scheme.=0D + */=0D +=0D +#pragma pack(1)=0D +=0D +/*=0D + * Reads a chunk of raw data with size BufSize from the block specified by= =0D + * block_id starting at BufOffset.=0D + * The read data is placed in buf.=0D + *=0D + * block_id must be less than num_blocks=0D + * BufOffset + BufSize must be less than block_size=0D + */=0D +typedef struct {=0D + UINT32 BufSize;=0D + UINT32 BufOffset;=0D + UINT32 BlockId;=0D +} SMM_STORE_PARAMS_WRITE;=0D +=0D +/*=0D + * Writes a chunk of raw data with size BufSize to the block specified by= =0D + * block_id starting at BufOffset.=0D + *=0D + * block_id must be less than num_blocks=0D + * BufOffset + BufSize must be less than block_size=0D + */=0D +typedef struct {=0D + UINT32 BufSize;=0D + UINT32 BufOffset;=0D + UINT32 BlockId;=0D +} SMM_STORE_PARAMS_READ;=0D +=0D +/*=0D + * Erases the specified block.=0D + *=0D + * block_id must be less than num_blocks=0D + */=0D +typedef struct {=0D + UINT32 BlockId;=0D +} SMM_STORE_PARAMS_CLEAR;=0D +=0D +typedef union {=0D + SMM_STORE_PARAMS_WRITE Write;=0D + SMM_STORE_PARAMS_READ Read;=0D + SMM_STORE_PARAMS_CLEAR Clear;=0D +} SMM_STORE_COM_BUF;=0D +#pragma pack(0)=0D +=0D +UINTN=0D +EFIAPI=0D +TriggerSmi (=0D + IN UINTN Cmd,=0D + IN UINTN Arg,=0D + IN UINTN Retry=0D + );=0D +=0D +#endif // COREBOOT_SMMSTORE_H_=0D diff --git a/UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf b/UefiPaylo= adPkg/Library/SmmStoreLib/SmmStoreLib.inf new file mode 100644 index 0000000000..13a520f358 --- /dev/null +++ b/UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf @@ -0,0 +1,41 @@ +## @file=0D +# SmmStore library for coreboot=0D +#=0D +# Copyright (c) 2022 9elements GmbH.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010005=0D + BASE_NAME =3D SmmStoreLib=0D + FILE_GUID =3D 40A2CBC6-CFB8-447b-A90E-298E88FD345E= =0D + MODULE_TYPE =3D DXE_DRIVER=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D SmmStoreLib=0D +=0D +[Sources]=0D + SmmStore.c=0D + SmmStore.h=0D +=0D +[Sources.X64]=0D + X64/SmmStore.nasm=0D +=0D +[LibraryClasses]=0D + BaseMemoryLib=0D + DebugLib=0D + DxeServicesTableLib=0D + HobLib=0D + MemoryAllocationLib=0D + UefiBootServicesTableLib=0D + UefiRuntimeLib=0D +=0D +[Guids]=0D + gEfiSmmStoreInfoHobGuid ## CONSUMES=0D + gEfiEventVirtualAddressChangeGuid ## CONSUMES=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D + UefiPayloadPkg/UefiPayloadPkg.dec=0D diff --git a/UefiPayloadPkg/Library/SmmStoreLib/X64/SmmStore.nasm b/UefiPay= loadPkg/Library/SmmStoreLib/X64/SmmStore.nasm new file mode 100644 index 0000000000..1a8b5b64c8 --- /dev/null +++ b/UefiPayloadPkg/Library/SmmStoreLib/X64/SmmStore.nasm @@ -0,0 +1,48 @@ +;-------------------------------------------------------------------------= ----- ;=0D +; Copyright (c) 2022, 9elements GmbH. All rights reserved.
=0D +; SPDX-License-Identifier: BSD-2-Clause-Patent=0D +;=0D +;-------------------------------------------------------------------------= ------=0D +=0D +%include "Nasm.inc"=0D +=0D +DEFAULT REL=0D +SECTION .text=0D +=0D +;UINTN=0D +;EFIAPI=0D +;TriggerSmi (=0D +; UINTN Cmd,=0D +; UINTN Arg,=0D +; UINTN Retry=0D +; )=0D +=0D +global ASM_PFX(TriggerSmi)=0D +ASM_PFX(TriggerSmi):=0D + push rbx=0D + mov rax, rcx ; Smi handler expect Cmd in RAX=0D + mov rbx, rdx ; Smi handler expect Argument in R= BX=0D +@Trigger:=0D + out 0b2h, al ; write to APM port to trigger SMI= =0D +=0D +; There might ba a delay between writing the Smi trigger register and=0D +; entering SMM, in which case the Smi handler will do nothing as only=0D +; synchronous Smis are handled. In addition when there's no Smi handler=0D +; or the SmmStore feature isn't compiled in, no register will be modified.= =0D +=0D +; As there's no livesign from SMM, just wait a bit for the handler to fire= ,=0D +; and then try again.=0D +=0D + cmp rax, rcx ; Check if rax was modified by SMM= =0D + jne @Return ; SMM modified rax, return now=0D + push rcx ; save rcx to stack=0D + mov rcx, 10000=0D + rep pause ; add a small delay=0D + pop rcx ; restore rcx=0D + cmp r8, 0=0D + je @Return=0D + dec r8=0D + jmp @Trigger=0D +@Return:=0D + pop rbx=0D + ret=0D diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayload= Pkg.dsc index 14a8d157a2..cf440fee58 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dsc +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc @@ -261,6 +261,7 @@ LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf=0D FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf= =0D AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLib= Null.inf=0D + SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf=0D !if $(VARIABLE_SUPPORT) =3D=3D "EMU"=0D TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurem= entLibNull.inf=0D !elseif $(VARIABLE_SUPPORT) =3D=3D "SPI"=0D --=20 2.32.0