📄 efibis_persist.c
字号:
Status = EFI_NOT_FOUND;
}
gBS->FreePool(FragmentName);
if (EFI_SUCCESS == Status)
{
gBS->FreePool(BISPersistentStorage);
}
// abort the cycling when the last one is reached
if (FragmentIndex == 0)
break;
}
} // end of while
}
if (EFI_SUCCESS == Status)
{
// Verify data is the expected length
Status = ValidateLength(TotalBISPersistentStorageLen);
}
return Status;
} // End of Persistentstorage_Read_Fn
///////////////////////////////////////////////////////////////////////////////
// Function: Persistentstorage_Write_Fn
//
// Purpose: Update the certificate data in persistent storage.
//
// Parameters: This -
// Buffer - buffer of the data to write
// Reserved -
//
// Function Returns: EFI_SUCCESS if succefully wrote, otherwise an error.
//
// Note: Use 0 for BytesToWrite to clear the data
//
///////////////////////////////////////////////////////////////////////////////
EFI_STATUS
Persistentstorage_Write_Fn(
IN EFI_BIS_PERSISTENCE_INTERFACE *This,
IN UINT8* Buffer,
IN VOID *Reserved
)
{
EFI_STATUS Status= EFI_SUCCESS;
CHAR16 *FragmentName;
UINT32 FragmentIndex;
UINT32 NumOfBytesToWrite;
UINT32 BufferIndex=0;
BOOLEAN KeepWriting = TRUE;
FragmentIndex = 0;
FragmentName = 0;
// Check input parameters
Status = EFI_INVALID_PARAMETER; // assume failure till proven otherwise
if (This)
{
if (Buffer)
{
if (Reserved == NULL)
{
Status = EFI_SUCCESS;
}
}
}
if (EFI_SUCCESS == Status)
{
// clear out the variables (delete them)
while (EFI_SUCCESS == Status)
{
Status = EnumerateFragmentNames(&FragmentIndex,
&FragmentName);
if (EFI_SUCCESS == Status)
{
Status = gRT->SetVariable (
FragmentName, // VariableName
&BISPersistProto, // VendorGuid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS, // Attributes
0, // DataSize
Buffer); // Data
gBS->FreePool(FragmentName);
// abort the cycling when the last one is reached
if (FragmentIndex == 0)
break;
}
} // end of while
// clear the number of fragments
Status = gRT->SetVariable (
VarBISPersistentStorageFragments, // VariableName
&BISPersistProto, // VendorGuid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS, // Attributes
0, // DataSize
Buffer); // Data
} // End of if (EFI_SUCCESS == Status)
FragmentIndex = 0;
NumOfBytesToWrite = BISPersistentStorageSize;
// write out the packets
while (KeepWriting)
{
// Get the variable name ready
FragmentName = x_malloc ((UINT32)(BisStrSize (VarBISPersistentStorage) + sizeof(UINT32)));
if (FragmentName)
{
EfiCopyMem(FragmentName, // Dest
VarBISPersistentStorage, // Src
BisStrSize (VarBISPersistentStorage)); // len
FragmentName[BisStrLen (VarBISPersistentStorage)]=
(CHAR16)(FragmentIndex + '0');
if (NumOfBytesToWrite > PERSIST_MAX_EFI_VAR_SIZE)
{
Status = gRT->SetVariable (
FragmentName, // VariableName
&BISPersistProto, // VendorGuid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS, // Attributes
PERSIST_MAX_EFI_VAR_SIZE, // DataSize
Buffer+ BufferIndex); // Data
NumOfBytesToWrite = NumOfBytesToWrite - PERSIST_MAX_EFI_VAR_SIZE;
BufferIndex+=PERSIST_MAX_EFI_VAR_SIZE;
FragmentIndex++;
}
else
{
Status = gRT->SetVariable (
FragmentName, // VariableName
&BISPersistProto, // VendorGuid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS, // Attributes
NumOfBytesToWrite, // DataSize
Buffer+ BufferIndex); // Data
KeepWriting = FALSE;
}
} // End of if (FragmentName)
else
{
// Failed to allocate pool
DEBUG((EFI_D_ERROR,
"PERSIST: Memory allocation failed\n"));
Status = EFI_OUT_OF_RESOURCES;
}
if (Status != EFI_SUCCESS)
{
DEBUG((EFI_D_ERROR,
"PERSIST: Variable was not set. rc=%r\n", Status));
KeepWriting = FALSE;
}
gBS->FreePool(FragmentName);
} // End of while (KeepWriting)
if (EFI_SUCCESS == Status)
{
// Updates the number of fragments variable
Status = gRT->SetVariable (
VarBISPersistentStorageFragments, // VariableName
&BISPersistProto, // VendorGuid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS, // Attributes
sizeof(UINT32), // DataSize
&FragmentIndex); // Data
if (Status != EFI_SUCCESS)
{
DEBUG((EFI_D_ERROR,
"PERSIST: Variable was not set. rc=%r\n", Status));
}
}
return Status;
} // End of Persistentstorage_Write_Fn
///////////////////////////////////////////////////////////////////////////////
// Function: Persistentstorage_Getlength_Fn
//
// Purpose: Return the size of the data in persistent storage
//
// Parameters: This -
// Length - Length of data stored in persistent storage
// Reserved -
//
// Function Returns: Size of data stored in persistent storage
//
// Note:
///////////////////////////////////////////////////////////////////////////////
EFI_STATUS
Persistentstorage_Getlength_Fn(
IN EFI_BIS_PERSISTENCE_INTERFACE *This,
OUT UINT32 *Length,
IN VOID *Reserved
)
{
EFI_STATUS Status= EFI_SUCCESS;
UINT32 BISPersistentStorageLen;
UINT32 TotalBISPersistentStorageLen = 0;
UINT8 *BISPersistentStorage;
CHAR16 *FragmentName;
UINT32 FragmentIndex = 0;
FragmentName = NULL;
// Check input parameters
Status = EFI_INVALID_PARAMETER; // assume failure till proven otherwise
if (This)
{
if (Length)
{
if (Reserved == NULL)
{
Status = EFI_SUCCESS;
}
}
}
if (EFI_SUCCESS == Status)
{
while (EFI_SUCCESS == Status)
{
Status = EnumerateFragmentNames(&FragmentIndex,
&FragmentName);
if (EFI_SUCCESS == Status)
{
BISPersistentStorage = BisLibGetVariableAndSize (
FragmentName, // Name
&BISPersistProto, // VendorGUID
(UINTN*)&BISPersistentStorageLen); // var length
if (BISPersistentStorage)
{
// get the total space so far
TotalBISPersistentStorageLen+= BISPersistentStorageLen;
gBS->FreePool(BISPersistentStorage);
}
else
{
// Error in retrieving data from persistent storage
DEBUG((EFI_D_ERROR,
"PERSIST: Variable was not found.\n"));
Status = EFI_NOT_FOUND;
}
gBS->FreePool(FragmentName);
// abort the cycling when the last one is reached
if (FragmentIndex == 0)
break;
}
} // end of while
} // End of if (EFI_SUCCESS == Status)
if (EFI_SUCCESS == Status)
{
*Length = TotalBISPersistentStorageLen;
}
// Verify data is the expected length
Status = ValidateLength(TotalBISPersistentStorageLen);
return Status;
} // End of Persistentstorage_Getlength_Fn()
///////////////////////////////////////////////////////////////////////////////
// Function: EFIBIS_InitPersistModule
//
// Purpose: Initiallize the Persististent Module
//
// Parameters: ImageHandle -
// SystemTable -
//
// Function Returns: - EFI_SUCCESS - OK
// EFI_OUT_OF_RESOURCES - malloc failure
// BIS_PERSIST_MEM_SIZE_MISMATCH - persistent
// memory size was not expected size
//
// Note:
///////////////////////////////////////////////////////////////////////////////
EFI_STATUS
EFIBIS_InitPersistModule(
EFI_BIS_PERSISTENCE_INTERFACE **persistInterface
)
{
EFI_STATUS Status= EFI_SUCCESS;
EFI_BIS_PERSISTENCE_INTERFACE *persistI_F;
UINT8 *Buffer = 0;
EFI_HANDLE tempHandle;
tempHandle = NULL;
persistI_F = x_malloc(sizeof(EFI_BIS_PERSISTENCE_INTERFACE));
if ( persistI_F == NULL)
{
DEBUG((EFI_D_ERROR,
"PERSIST: Memory allocation failed\n"));
Status= EFI_OUT_OF_RESOURCES;
}
//Fill out the interface structure
if ( Status == EFI_SUCCESS)
{
persistI_F->Read= Persistentstorage_Read_Fn;
persistI_F->Write= Persistentstorage_Write_Fn;
persistI_F->GetLength= Persistentstorage_Getlength_Fn;
//no real instance data defined
persistI_F->InstanceData= (VOID*)persistI_F;
}
if (EFI_SUCCESS == Status)
{
// check if data is present
Buffer = x_malloc(BISPersistentStorageSize);
if (Buffer)
{
Status = Persistentstorage_Read_Fn(persistI_F,
Buffer,
0);
gBS->FreePool(Buffer);
Buffer = 0;
}
if (EFI_NOT_FOUND == Status)
{
// Variable is not found, allocate it and return
Buffer = x_malloc(BISPersistentStorageSize);
if (Buffer)
{
Status = Persistentstorage_Write_Fn(persistI_F,
Buffer,
0);
if (EFI_SUCCESS == Status)
{
gBS->FreePool(Buffer);
Buffer = 0;
}
}
else
{
// Failed to allocate pool
DEBUG((EFI_D_ERROR,
"PERSIST: Memory allocation failed\n"));
Status = EFI_OUT_OF_RESOURCES;
}
} // End of if variable is not found
} // End of if (EFI_SUCCESS == Status)
//free mem if error
if ( Status != EFI_SUCCESS)
{
gBS->FreePool( persistI_F );
}
if ( Status == EFI_SUCCESS)
{
*persistInterface= persistI_F;
}
if ( Status != EFI_SUCCESS)
{
DEBUG((EFI_D_ERROR, "EFIBIS_InitPersistModule result %r\n", Status));
}
if(Buffer)
{
gBS->FreePool(Buffer);
}
return Status;
} // End of EFIBIS_InitPersistModule
//eof
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -