Hi liming:
    I checked the code, and found the root cause of the issue.
    As the following code in edk2,   in my code,  after the  first time to read the variable in PEI phase,  it cached the variable store info  IndexTable into  a Hob in a special non-volatile memory,  but after the code running in dxe phase, the variable was changed.  And after a warm reboot, maybe call s3 sleep is more exactly, when read the variable, it  first get the IndexTable from the Hob but not build the IndexTable again, so it read the deleted variable if hasn't the condition check.  This is really a special case.

      GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid);
        if (GuidHob != NULL) {
          StoreInfo->IndexTable = GET_GUID_HOB_DATA (GuidHob);
        } else {
          //
          // If it's the first time to access variable region in flash, create a guid hob to record
          // VAR_ADDED type variable info.
          // Note that as the resource of PEI phase is limited, only store the limited number of
          // VAR_ADDED type variables to reduce access time.
          //
          StoreInfo->IndexTable = (VARIABLE_INDEX_TABLE *) BuildGuidHob (&gEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));
          StoreInfo->IndexTable->Length      = 0;
          StoreInfo->IndexTable->StartPtr    = GetStartPointer (VariableStoreHeader);
          StoreInfo->IndexTable->EndPtr      = GetEndPointer   (VariableStoreHeader);
          StoreInfo->IndexTable->GoneThrough = 0;
        }


     

On Mon, Oct 10, 2022 at 09:39 AM, gaoliming wrote:

Jiading:

  Please check why NV variable data is required to be changed in PEI phase. This will be helpful for this issue.

 

Thanks

Liming

发件人: Jiading Zhang <jdzhang@kunluntech.com.cn>
发送时间: 20221010 8:35
收件人: gaoliming <gaoliming@byosoft.com.cn>; devel@edk2.groups.io
主题: Re: [edk2-devel] 回复: [edk2-devel] [PATCH] MdeModulePkg VariablePei: Add Variable state check when find variable in IndexTable.

 

Hi liming:
   Yes, NV Variable Data is not changed in PEI phase in normal case. This issue was found when we did a special coding, and when found variable in the IndexTable, it found the variable before the last changed if  didn't add the following condition:
  "if ((VariableHeader->State == VAR_ADDED) || (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)))"

Maybe our specail coding had some defect
and caused this issue.

On Fri, Sep 30, 2022 at 10:46 AM, gaoliming wrote:

Jiading:

 Hob Variable Store Info IndexTable is NULL. So, this logic doesnt work for HOB variable store. NV Variable Store Info has IndexTable. When its IndexTable is initialized, its IndexTable will only record the variable with VAR_ADDED attribute. Because NV Variable Data is not changed in PEI phase, this check is not required by NV variable.

 

Thanks

Liming

发件人: devel@edk2.groups.io <devel@edk2.groups.io> 代表 Jiading Zhang
发送时间: 2022928 11:05
收件人: devel@edk2.groups.io
主题: [edk2-devel] [PATCH] MdeModulePkg VariablePei: Add Variable state check when find variable in IndexTable.

 

When read a variable in PEI, it will find it first in the HOB, then find in variable store. When find in variable store, it will check the variable state, but find in HOB, it doesn't check the state, so if the variable was changed, it will find the obsolete variable in the HOB.

 

Signed-off-by: jdzhang <jdzhang@kunluntech.com.cn>

---

 MdeModulePkg/Universal/Variable/Pei/Variable.c | 12 +++++++-----

 1 file changed, 7 insertions(+), 5 deletions(-)

 

diff --git a/MdeModulePkg/Universal/Variable/Pei/Variable.c b/MdeModulePkg/Universal/Variable/Pei/Variable.c

index 26a4c73b45..dffbd8cdb1 100644

--- a/MdeModulePkg/Universal/Variable/Pei/Variable.c

+++ b/MdeModulePkg/Universal/Variable/Pei/Variable.c

@@ -866,11 +866,13 @@ FindVariableEx (

       Offset  += IndexTable->Index[Index];

       MaxIndex = (VARIABLE_HEADER *)((UINT8 *)IndexTable->StartPtr + Offset);

       GetVariableHeader (StoreInfo, MaxIndex, &VariableHeader);

-      if (CompareWithValidVariable (StoreInfo, MaxIndex, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {

-        if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {

-          InDeletedVariable = PtrTrack->CurrPtr;

-        } else {

-          return EFI_SUCCESS;

+      if ((VariableHeader->State == VAR_ADDED) || (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED))) {

+        if (CompareWithValidVariable (StoreInfo, MaxIndex, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {

+          if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {

+            InDeletedVariable = PtrTrack->CurrPtr;

+          } else {

+            return EFI_SUCCESS;

+          }

         }

       }

     }

-- 

2.20.1.windows.1