From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by mx.groups.io with SMTP id smtpd.web10.1961.1687809601056151111 for ; Mon, 26 Jun 2023 13:00:01 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="signature has expired" header.i=@gmail.com header.s=20221208 header.b=Iwk80oYq; spf=pass (domain: gmail.com, ip: 209.85.210.181, mailfrom: kuqin12@gmail.com) Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-666e64e97e2so1951563b3a.1 for ; Mon, 26 Jun 2023 13:00:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687809600; x=1690401600; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7zPLDzREvxnRu8EN6ya91ULUr7kBvzCyfZmoWVETYGk=; b=Iwk80oYqxrdhfPSe4SBy/lkX/DuuxRc9GuTD3W6sYQ3C/VOq1lywkrUj8L3NLPXJRI 6RVtczdKKVc2i/dWbyu/guqCKcOQ0Gsgq+KJ0/ADtIAIy6AWXgh+Mw7ORNNLV5NfZHDy ENH23lTFHhqDoWkzJR2wPte37Tl7HOB0beXt3xQmFpxhQtjvO9pL71cZ+lhnrRGiAc2X MGyHOTamk8DeGYwyxY5+J8HoB5IaLmRRg72Jtfet97tUX/fYs4gcp4pAdIdpxzvjVU0b liA+ZmWZH/gDOxEPLchuaaIn9QxZEk0vySKJThjGPphXfLW6R+C/AF8d0LoPTkhKIgxU B8pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687809600; x=1690401600; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7zPLDzREvxnRu8EN6ya91ULUr7kBvzCyfZmoWVETYGk=; b=PwlXE+7n7KY5wR/+ezLIX2qfmCHMmu87IVvuFAN1SJtYQMIGxJv4MSbj/uDDgDGxwN PHRC9iqCTN26ThPYWUGqoC+9zRnkHJ3DTGmIRv8yLQ7SUd7bwPJqMStn6zRWptp7Qp/G NbtefQMyZO8+QGJrN8cWe4Uk6uX3pcAmNK+0Q5dh62GRhVrAPD0K/eO9uZ52h3UGVmuu WB1vKONIknpmBUxNHi6WBUEjU3lGANne6S8SVbwPiMHwq79pzYGqJU5jJV/7ABhqkIEv 4dckRRo3l7abKAckReuO1C8WiMfHLZIaMhnp1aO4Ai7jtP+G8P2KjvvRVHouk2a66vJl DWgQ== X-Gm-Message-State: AC+VfDwNTU6WumDG+s7zOOFUEZcUmSjRif2D+Xj9WpXfFmbR/v1Au9xA tYkHvAODDJMh1mv9ozKB50uZ5DWwYy4= X-Google-Smtp-Source: ACHHUZ6wNqQJ0plJEgIsLgxhBHLGuL/68vurZhwmFwSYf77fTwR5su5gkFQTPtdho+TBqVW4KkqZFg== X-Received: by 2002:a05:6a00:1995:b0:668:9f05:29db with SMTP id d21-20020a056a00199500b006689f0529dbmr15629690pfl.3.1687809600140; Mon, 26 Jun 2023 13:00:00 -0700 (PDT) Return-Path: Received: from MININT-0U7P5GU.redmond.corp.microsoft.com ([2001:4898:80e8:1:3ddb:b5a5:d301:15a6]) by smtp.gmail.com with ESMTPSA id j18-20020a62e912000000b00666add7f047sm4122755pfh.207.2023.06.26.12.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jun 2023 12:59:59 -0700 (PDT) From: "Kun Qin" To: devel@edk2.groups.io Cc: Hao A Wu , Liming Gao , Jian J Wang , Ronny Hansen , Shriram Masanamuthu Chinnathurai , Preshit Harlikar Subject: [PATCH v2 2/2] MdeModulePkg: Variable: Introduce MM based variable read service in PEI Date: Mon, 26 Jun 2023 12:59:52 -0700 Message-ID: <20230626195953.1807-3-kuqin12@gmail.com> X-Mailer: git-send-email 2.41.0.windows.1 In-Reply-To: <20230626195953.1807-1-kuqin12@gmail.com> References: <20230626195953.1807-1-kuqin12@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Kun Qin REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4464 This change introduced the Standalone MM based variable read capability in PEI phase for applicable platforms (such as ARM platforms). Similar to the x86 counterpart, MM communicate PPI is used to request variable information from Standalone MM environment. Cc: Hao A Wu Cc: Liming Gao Cc: Jian J Wang Co-authored-by: Ronny Hansen Co-authored-by: Shriram Masanamuthu Chinnathurai Co-authored-by: Preshit Harlikar Signed-off-by: Kun Qin --- Notes: v2: - Removed unused PCD [Liming] MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.c | 381 ++++= ++++++++++++++++ MdeModulePkg/MdeModulePkg.dsc | 1 + MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.h | 134 ++++= +++ MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.inf | 40 ++ 4 files changed, 556 insertions(+) diff --git a/MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.c = b/MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.c new file mode 100644 index 000000000000..4f937e22e89e --- /dev/null +++ b/MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.c @@ -0,0 +1,381 @@ +/** @file -- MmVariablePei.c=0D + Provides interface for reading Secure System Variables during PEI.=0D +=0D + Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
=0D + Copyright (c) Microsoft Corporation.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +**/=0D +=0D +#include "MmVariablePei.h"=0D +=0D +#define MM_VARIABLE_COMM_BUFFER_OFFSET (SMM_COMMUNICATE_HEADER_SIZE + SMM= _VARIABLE_COMMUNICATE_HEADER_SIZE)=0D +=0D +//=0D +// Module globals=0D +//=0D +EFI_PEI_READ_ONLY_VARIABLE2_PPI mPeiSecureVariableRead =3D {=0D + PeiMmGetVariable,=0D + PeiMmGetNextVariableName=0D +};=0D +=0D +EFI_PEI_PPI_DESCRIPTOR mPeiMmVariablePpi =3D {=0D + (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),=0D + &gEfiPeiReadOnlyVariable2PpiGuid,=0D + &mPeiSecureVariableRead=0D +};=0D +=0D +/**=0D + Entry point of PEI Secure Variable read driver=0D +=0D + @param FileHandle Handle of the file being invoked.=0D + Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextF= ile().=0D + @param PeiServices General purpose services available to every PEIM.=0D +=0D + @retval EFI_SUCCESS If the interface could be successfully installed=0D + @retval Others Returned from PeiServicesInstallPpi()=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +PeiMmVariableInitialize (=0D + IN EFI_PEI_FILE_HANDLE FileHandle,=0D + IN CONST EFI_PEI_SERVICES **PeiServices=0D + )=0D +{=0D + return PeiServicesInstallPpi (&mPeiMmVariablePpi);=0D +}=0D +=0D +/**=0D + Helper function to populate MM communicate header and variable communica= te header=0D + and then communicate to PEI.=0D +=0D + @param[in, out] CommunicateBuffer Size of the variable name.=0D + @param[in] CommunicateBufferSize The entire buffer size to be sen= t to MM.=0D + @param[in] Function The MM variable function value.= =0D +=0D + @retval EFI_INVALID_PARAMETER Invalid parameter.=0D + @retval EFI_SUCCESS Find the specified variable.=0D + @retval Others Errors returned by MM communicate or = variable service.=0D +=0D +**/=0D +EFI_STATUS=0D +PopulateHeaderAndCommunicate (=0D + IN OUT UINT8 *CommunicateBuffer,=0D + IN UINTN CommunicateBufferSize,=0D + IN UINTN Function=0D + )=0D +{=0D + EFI_STATUS Status;=0D + EFI_PEI_MM_COMMUNICATION_PPI *MmCommunicationPpi;=0D + EFI_MM_COMMUNICATE_HEADER *MmCommunicateHeader;=0D + SMM_VARIABLE_COMMUNICATE_HEADER *MmVarCommsHeader;=0D +=0D + // Minimal sanity check=0D + if ((CommunicateBuffer =3D=3D NULL) ||=0D + (CommunicateBufferSize < MM_VARIABLE_COMM_BUFFER_OFFSET))=0D + {=0D + Status =3D EFI_INVALID_PARAMETER;=0D + DEBUG ((DEBUG_ERROR, "%a: Invalid incoming parameters: %p and 0x%x\n",= __func__, CommunicateBuffer, CommunicateBufferSize));=0D + goto Exit;=0D + }=0D +=0D + if ((Function !=3D SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME) &&=0D + (Function !=3D SMM_VARIABLE_FUNCTION_GET_VARIABLE))=0D + {=0D + Status =3D EFI_INVALID_PARAMETER;=0D + DEBUG ((DEBUG_ERROR, "%a: Invalid function value: 0x%x\n", __func__, F= unction));=0D + goto Exit;=0D + }=0D +=0D + Status =3D PeiServicesLocatePpi (&gEfiPeiMmCommunicationPpiGuid, 0, NULL= , (VOID **)&MmCommunicationPpi);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "%a: Failed to locate PEI MM Communication PPI: %= r\n", __func__, Status));=0D + goto Exit;=0D + }=0D +=0D + // Zero the entire Communication Buffer Header=0D + MmCommunicateHeader =3D (EFI_MM_COMMUNICATE_HEADER *)CommunicateBuffer;= =0D +=0D + ZeroMem (MmCommunicateHeader, SMM_COMMUNICATE_HEADER_SIZE);=0D +=0D + // Use gEfiSmmVariableProtocolGuid to request the MM variable service in= Standalone MM=0D + CopyMem ((VOID *)&MmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProto= colGuid, sizeof (GUID));=0D +=0D + // Program the MM header size=0D + MmCommunicateHeader->MessageLength =3D CommunicateBufferSize - SMM_COMMU= NICATE_HEADER_SIZE;=0D +=0D + MmVarCommsHeader =3D (SMM_VARIABLE_COMMUNICATE_HEADER *)(CommunicateBuff= er + SMM_COMMUNICATE_HEADER_SIZE);=0D +=0D + // We are only supporting GetVariable and GetNextVariableName=0D + MmVarCommsHeader->Function =3D Function;=0D +=0D + // Send the MM request using MmCommunicationPei=0D + Status =3D MmCommunicationPpi->Communicate (MmCommunicationPpi, Communic= ateBuffer, &CommunicateBufferSize);=0D + if (EFI_ERROR (Status)) {=0D + // Received an error from MM interface.=0D + DEBUG ((DEBUG_ERROR, "%a - MM Interface Error: %r\n", __func__, Status= ));=0D + goto Exit;=0D + }=0D +=0D + // MM request was successfully handled by the framework.=0D + // Set status to the Variable Service Status Code=0D + Status =3D MmVarCommsHeader->ReturnStatus;=0D + if (EFI_ERROR (Status)) {=0D + // We received an error from Variable Service.=0D + // We cant do anymore so return Status=0D + if (Status !=3D EFI_BUFFER_TOO_SMALL) {=0D + DEBUG ((DEBUG_ERROR, "%a - Variable Service Error: %r\n", __func__, = Status));=0D + }=0D +=0D + goto Exit;=0D + }=0D +=0D +Exit:=0D + return Status;=0D +}=0D +=0D +/**=0D + This service retrieves a variable's value using its name and GUID.=0D +=0D + This function is using the Secure Variable Store. If the Data=0D + buffer is too small to hold the contents of the variable, the error=0D + EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buf= fer=0D + size to obtain the data.=0D +=0D + @param This A pointer to this instance of the EFI_PEI_= READ_ONLY_VARIABLE2_PPI.=0D + @param VariableName A pointer to a null-terminated string that= is the variable's name.=0D + @param VariableGuid A pointer to an EFI_GUID that is the varia= ble's GUID. The combination of=0D + VariableGuid and VariableName must be uniq= ue.=0D + @param Attributes If non-NULL, on return, points to the vari= able's attributes.=0D + @param DataSize On entry, points to the size in bytes of t= he Data buffer.=0D + On return, points to the size of the data = returned in Data.=0D + @param Data Points to the buffer which will hold the r= eturned variable value.=0D + May be NULL with a zero DataSize in order = to determine the size of the buffer needed.=0D +=0D + @retval EFI_SUCCESS The variable was read successfully.=0D + @retval EFI_NOT_FOUND The variable was not found.=0D + @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resultin= g data.=0D + DataSize is updated with the size required= for=0D + the specified variable.=0D + @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Da= ta is NULL.=0D + @retval EFI_DEVICE_ERROR The variable could not be retrieved becaus= e of a device error.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +PeiMmGetVariable (=0D + IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,=0D + IN CONST CHAR16 *VariableName,=0D + IN CONST EFI_GUID *VariableGuid,=0D + OUT UINT32 *Attributes, OPTIONAL=0D + IN OUT UINTN *DataSize,=0D + OUT VOID *Data OPTIONAL=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINTN MessageSize;=0D + SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *MmVarAccessHeader;=0D + UINT8 *MmCommunicateBuffer;=0D + UINTN RequiredPages;=0D +=0D + // Check input parameters=0D + if ((VariableName =3D=3D NULL) || (VariableGuid =3D=3D NULL) || (DataSiz= e =3D=3D NULL)) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + if (VariableName[0] =3D=3D 0) {=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + if ((*DataSize > 0) && (Data =3D=3D NULL)) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // Allocate required pages to send MM request=0D + MessageSize =3D MM_VARIABLE_COMM_BUFFER_OFFSET +=0D + OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)= +=0D + StrSize (VariableName) + *DataSize;=0D +=0D + RequiredPages =3D EFI_SIZE_TO_PAGES (MessageSize);=0D + MmCommunicateBuffer =3D (UINT8 *)AllocatePages (RequiredPages);=0D +=0D + if (MmCommunicateBuffer =3D=3D NULL) {=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + DEBUG ((DEBUG_ERROR, "%a: Failed to allocate memory: %r\n", __func__, = Status));=0D + return Status;=0D + }=0D +=0D + // Zero the entire Communication Buffer=0D + ZeroMem (MmCommunicateBuffer, (RequiredPages * EFI_PAGE_SIZE));=0D +=0D + //=0D + // Program all payload structure contents=0D + //=0D + MmVarAccessHeader =3D (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *)(MmCom= municateBuffer + MM_VARIABLE_COMM_BUFFER_OFFSET);=0D +=0D + // Variable GUID=0D + CopyMem ((VOID *)&MmVarAccessHeader->Guid, VariableGuid, sizeof (GUID));= =0D +=0D + // Program the max amount of data we accept.=0D + MmVarAccessHeader->DataSize =3D *DataSize;=0D +=0D + // Get size of the variable name=0D + MmVarAccessHeader->NameSize =3D StrSize (VariableName);=0D +=0D + // Populate incoming variable name=0D + CopyMem ((VOID *)&MmVarAccessHeader->Name, VariableName, MmVarAccessHead= er->NameSize);=0D +=0D + Status =3D PopulateHeaderAndCommunicate (MmCommunicateBuffer, MessageSiz= e, SMM_VARIABLE_FUNCTION_GET_VARIABLE);=0D + if (EFI_ERROR (Status)) {=0D + // We received an error from either communicate or Variable Service.=0D + if (Status !=3D EFI_BUFFER_TOO_SMALL) {=0D + DEBUG ((DEBUG_ERROR, "%a - Communite to MM for variable service erro= red: %r\n", __func__, Status));=0D + }=0D +=0D + goto Exit;=0D + }=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + // User provided buffer is too small=0D + if (*DataSize < MmVarAccessHeader->DataSize) {=0D + Status =3D EFI_BUFFER_TOO_SMALL;=0D + }=0D +=0D +Exit:=0D + // Check if we need to set Attributes=0D + if (Attributes !=3D NULL) {=0D + *Attributes =3D MmVarAccessHeader->Attributes;=0D + }=0D +=0D + *DataSize =3D MmVarAccessHeader->DataSize;=0D +=0D + if (Status =3D=3D EFI_SUCCESS) {=0D + CopyMem ((VOID *)Data, (UINT8 *)MmVarAccessHeader->Name + MmVarAccessH= eader->NameSize, *DataSize);=0D + }=0D +=0D + // Free the Communication Buffer=0D + if (MmCommunicateBuffer !=3D NULL) {=0D + FreePages (MmCommunicateBuffer, RequiredPages);=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +/**=0D + Return the next variable name and GUID.=0D +=0D + This function is called multiple times to retrieve the VariableName=0D + and VariableGuid of all variables currently available in the system.=0D + On each call, the previous results are passed into the interface,=0D + and, on return, the interface returns the data for the next=0D + interface. When the entire variable list has been returned,=0D + EFI_NOT_FOUND is returned.=0D +=0D + @param This A pointer to this instance of the EFI_PEI_READ= _ONLY_VARIABLE2_PPI.=0D +=0D + @param VariableNameSize On entry, points to the size of the buffer poi= nted to by VariableName.=0D + On return, the size of the variable name buffe= r.=0D + @param VariableName On entry, a pointer to a null-terminated strin= g that is the variable's name.=0D + On return, points to the next variable's null-= terminated name string.=0D +=0D + @param VariableGuid On entry, a pointer to an EFI_GUID that is the= variable's GUID.=0D + On return, a pointer to the next variable's GU= ID.=0D +=0D + @retval EFI_SUCCESS The variable was read successfully.=0D + @retval EFI_NOT_FOUND The variable could not be found.=0D + @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the = resulting=0D + data. VariableNameSize is updated with the= size=0D + required for the specified variable.=0D + @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or=0D + VariableNameSize is NULL.=0D + @retval EFI_DEVICE_ERROR The variable could not be retrieved becaus= e of a device error.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +PeiMmGetNextVariableName (=0D + IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,=0D + IN OUT UINTN *VariableNameSize,=0D + IN OUT CHAR16 *VariableName,=0D + IN OUT EFI_GUID *VariableGuid=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINTN MessageSize;=0D + UINT8 *MmCommunicateBuffer;=0D + SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *MmVarGetNextVarHeader;= =0D + UINTN RequiredPages;=0D +=0D + // Check input parameters=0D + if ((VariableName =3D=3D NULL) ||=0D + (VariableGuid =3D=3D NULL) ||=0D + (VariableNameSize =3D=3D NULL) ||=0D + (*VariableNameSize =3D=3D 0))=0D + {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // Allocate required pages to send MM request=0D + MessageSize =3D MM_VARIABLE_COMM_BUFFER_OFFSET +=0D + OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)= +=0D + StrSize (VariableName) + *VariableNameSize;=0D +=0D + RequiredPages =3D EFI_SIZE_TO_PAGES (MessageSize);=0D + MmCommunicateBuffer =3D (UINT8 *)AllocatePages (RequiredPages);=0D +=0D + if (MmCommunicateBuffer =3D=3D NULL) {=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + DEBUG ((DEBUG_ERROR, "%a: Failed to allocate memory: %r\n", __func__, = Status));=0D + return Status;=0D + }=0D +=0D + // Zero the entire Communication Buffer=0D + ZeroMem (MmCommunicateBuffer, (RequiredPages * EFI_PAGE_SIZE));=0D +=0D + //=0D + // Program all payload structure contents=0D + //=0D + MmVarGetNextVarHeader =3D (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NA= ME *)(MmCommunicateBuffer + MM_VARIABLE_COMM_BUFFER_OFFSET);=0D +=0D + // Variable GUID=0D + CopyMem ((VOID *)&MmVarGetNextVarHeader->Guid, VariableGuid, sizeof (GUI= D));=0D +=0D + // Program the maximal length of name we can accept.=0D + MmVarGetNextVarHeader->NameSize =3D *VariableNameSize;=0D +=0D + // Populate incoming variable name=0D + CopyMem ((VOID *)&MmVarGetNextVarHeader->Name, VariableName, MmVarGetNex= tVarHeader->NameSize);=0D +=0D + // Send the MM request using MmCommunicationPei=0D + Status =3D PopulateHeaderAndCommunicate (MmCommunicateBuffer, MessageSiz= e, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);=0D + if (EFI_ERROR (Status)) {=0D + // We received an error from either communicate or Variable Service.=0D + if (Status !=3D EFI_BUFFER_TOO_SMALL) {=0D + DEBUG ((DEBUG_ERROR, "%a - Communite to MM for variable service erro= red: %r\n", __func__, Status));=0D + }=0D +=0D + goto Exit;=0D + }=0D +=0D + Status =3D EFI_SUCCESS;=0D +=0D + // User provided buffer is too small=0D + if (*VariableNameSize < MmVarGetNextVarHeader->NameSize) {=0D + Status =3D EFI_BUFFER_TOO_SMALL;=0D + }=0D +=0D +Exit:=0D + // Update the name size to be returned=0D + *VariableNameSize =3D MmVarGetNextVarHeader->NameSize;=0D +=0D + if (Status =3D=3D EFI_SUCCESS) {=0D + CopyMem ((VOID *)VariableName, (UINT8 *)MmVarGetNextVarHeader->Name, *= VariableNameSize);=0D + CopyMem ((VOID *)VariableGuid, (UINT8 *)&(MmVarGetNextVarHeader->Guid)= , sizeof (EFI_GUID));=0D + }=0D +=0D + // Free the Communication Buffer=0D + if (MmCommunicateBuffer !=3D NULL) {=0D + FreePages (MmCommunicateBuffer, RequiredPages);=0D + }=0D +=0D + return Status;=0D +}=0D diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc index 5b1f50e9c084..1aedfe280ae1 100644 --- a/MdeModulePkg/MdeModulePkg.dsc +++ b/MdeModulePkg/MdeModulePkg.dsc @@ -400,6 +400,7 @@ [Components] MdeModulePkg/Application/VariableInfo/VariableInfo.inf=0D MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWritePei.inf=0D MdeModulePkg/Universal/Variable/Pei/VariablePei.inf=0D + MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.inf=0D MdeModulePkg/Universal/WatchdogTimerDxe/WatchdogTimer.inf=0D MdeModulePkg/Universal/TimestampDxe/TimestampDxe.inf=0D MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf=0D diff --git a/MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.h = b/MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.h new file mode 100644 index 000000000000..0feed8cd1cb6 --- /dev/null +++ b/MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.h @@ -0,0 +1,134 @@ +/** @file -- MmVariablePei.h=0D + Provides interface for reading Secure System Variables during PEI.=0D +=0D + Copyright (c) Microsoft Corporation.=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +**/=0D +=0D +#ifndef PEI_MM_VARIABLE_LIB_H_=0D +#define PEI_MM_VARIABLE_LIB_H_=0D +=0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include =0D +=0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +=0D +/**=0D + Entry point of PEI Secure Variable read driver=0D +=0D + @param FileHandle Handle of the file being invoked.=0D + Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextF= ile().=0D + @param PeiServices General purpose services available to every PEIM.=0D +=0D + @retval EFI_SUCCESS If the interface could be successfully installed=0D + @retval Others Returned from PeiServicesInstallPpi()=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +PeiMmVariableInitialize (=0D + IN EFI_PEI_FILE_HANDLE FileHandle,=0D + IN CONST EFI_PEI_SERVICES **PeiServices=0D + );=0D +=0D +/**=0D +=0D + This function enables the read of Secure Variables during PEI.=0D +=0D + This function is using the Secure Variable Store.If the Data=0D + buffer is too small to hold the contents of the variable, the error=0D + EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buf= fer=0D + size to obtain the data.=0D +=0D + The function performs the following:=0D +=0D + 1) Creates an MM request=0D + 2) Fills out the following data structures for the Secure Variable Servi= ce=0D + SMM_VARIABLE_COMMUNICATE_HEADER/SMM_VARIABLE_COMMUNICATE_ACCESS_VARI= ABLE=0D + 3) Adds the MM data structures to the MM request.=0D + 4) Sends the MM request to EL3 using MmCommunicationPeiLib.=0D + 5) The MM request is sent to S-EL0.=0D + 6) The MM request is then handled by the registered handler with GUID: g= EfiSmmVariableProtocolGuid=0D +=0D + @param This A pointer to this instance of the EFI_PEI_= READ_ONLY_VARIABLE2_PPI.=0D + @param VariableName A pointer to a null-terminated string that= is the variable's name.=0D + @param VariableGuid A pointer to an EFI_GUID that is the varia= ble's GUID. The combination of=0D + VariableGuid and VariableName must be uniq= ue.=0D + @param Attributes If non-NULL, on return, points to the vari= able's attributes.=0D + @param DataSize On entry, points to the size in bytes of t= he Data buffer.=0D + On return, points to the size of the data = returned in Data.=0D + @param Data Points to the buffer which will hold the r= eturned variable value.=0D + May be NULL with a zero DataSize in order = to determine the size of the buffer needed.=0D +=0D + @retval EFI_SUCCESS The variable was read successfully.=0D + @retval EFI_NOT_FOUND The variable was not found.=0D + @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resultin= g data.=0D + DataSize is updated with the size required= for=0D + the specified variable.=0D + @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Da= ta is NULL.=0D + @retval EFI_DEVICE_ERROR The variable could not be retrieved becaus= e of a device error.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +PeiMmGetVariable (=0D + IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,=0D + IN CONST CHAR16 *VariableName,=0D + IN CONST EFI_GUID *VariableGuid,=0D + OUT UINT32 *Attributes,=0D + IN OUT UINTN *DataSize,=0D + OUT VOID *Data OPTIONAL=0D + );=0D +=0D +/**=0D + Return the next variable name and GUID.=0D +=0D + This function is called multiple times to retrieve the VariableName=0D + and VariableGuid of all variables currently available in the system.=0D + On each call, the previous results are passed into the interface,=0D + and, on return, the interface returns the data for the next=0D + interface. When the entire variable list has been returned,=0D + EFI_NOT_FOUND is returned.=0D +=0D + @param This A pointer to this instance of the EFI_PEI_READ= _ONLY_VARIABLE2_PPI.=0D +=0D + @param VariableNameSize On entry, points to the size of the buffer poi= nted to by VariableName.=0D + On return, the size of the variable name buffe= r.=0D + @param VariableName On entry, a pointer to a null-terminated strin= g that is the variable's name.=0D + On return, points to the next variable's null-= terminated name string.=0D +=0D + @param VariableGuid On entry, a pointer to an EFI_GUID that is the= variable's GUID.=0D + On return, a pointer to the next variable's GU= ID.=0D +=0D + @retval EFI_SUCCESS The variable was read successfully.=0D + @retval EFI_NOT_FOUND The variable could not be found.=0D + @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the = resulting=0D + data. VariableNameSize is updated with the= size=0D + required for the specified variable.=0D + @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or=0D + VariableNameSize is NULL.=0D + @retval EFI_DEVICE_ERROR The variable could not be retrieved becaus= e of a device error.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +PeiMmGetNextVariableName (=0D + IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,=0D + IN OUT UINTN *VariableNameSize,=0D + IN OUT CHAR16 *VariableName,=0D + IN OUT EFI_GUID *VariableGuid=0D + );=0D +=0D +#endif /* PEI_MM_VARIABLE_LIB_H_ */=0D diff --git a/MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.in= f b/MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.inf new file mode 100644 index 000000000000..13e80523f960 --- /dev/null +++ b/MdeModulePkg/Universal/Variable/MmVariablePei/MmVariablePei.inf @@ -0,0 +1,40 @@ +## @file -- MmVariablePei.inf=0D +# Provides interface for reading Secure System Variables during PEI.=0D +#=0D +# Copyright (c) Microsoft Corporation.=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +##=0D +=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010005=0D + BASE_NAME =3D MmVariablePei=0D + FILE_GUID =3D CD660A87-454B-4346-A35C-3D89BF8ECFAF= =0D + MODULE_TYPE =3D PEIM=0D + VERSION_STRING =3D 1.0=0D + ENTRY_POINT =3D PeiMmVariableInitialize=0D +=0D +[Sources]=0D + MmVariablePei.c=0D + MmVariablePei.h=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D +=0D +[LibraryClasses]=0D + PcdLib=0D + PeiServicesLib=0D + PeimEntryPoint=0D + MemoryAllocationLib=0D + HobLib=0D +=0D +[Protocols]=0D + gEfiSmmVariableProtocolGuid ## CONSUMES=0D +=0D +[Ppis]=0D + gEfiPeiReadOnlyVariable2PpiGuid ## PRODUCES=0D + gEfiPeiMmCommunicationPpiGuid ## CONSUMES=0D +=0D +[Depex]=0D + gEfiPeiMmCommunicationPpiGuid=0D --=20 2.41.0.windows.1