1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/** @file -- MmVariablePei.c
Provides interface for reading Secure System Variables during PEI.
Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
Copyright (c) Microsoft Corporation.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "MmVariablePei.h"
#define MM_VARIABLE_COMM_BUFFER_OFFSET (SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE)
//
// Module globals
//
EFI_PEI_READ_ONLY_VARIABLE2_PPI mPeiSecureVariableRead = {
PeiMmGetVariable,
PeiMmGetNextVariableName
};
EFI_PEI_PPI_DESCRIPTOR mPeiMmVariablePpi = {
(EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
&gEfiPeiReadOnlyVariable2PpiGuid,
&mPeiSecureVariableRead
};
/**
Entry point of PEI Secure Variable read driver
@param FileHandle Handle of the file being invoked.
Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
@param PeiServices General purpose services available to every PEIM.
@retval EFI_SUCCESS If the interface could be successfully installed
@retval Others Returned from PeiServicesInstallPpi()
**/
EFI_STATUS
EFIAPI
PeiMmVariableInitialize (
IN EFI_PEI_FILE_HANDLE FileHandle,
IN CONST EFI_PEI_SERVICES **PeiServices
)
{
return PeiServicesInstallPpi (&mPeiMmVariablePpi);
}
/**
Helper function to populate MM communicate header and variable communicate header
and then communicate to PEI.
@param[in, out] CommunicateBuffer Size of the variable name.
@param[in] CommunicateBufferSize The entire buffer size to be sent to MM.
@param[in] Function The MM variable function value.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval EFI_SUCCESS Find the specified variable.
@retval Others Errors returned by MM communicate or variable service.
**/
EFI_STATUS
PopulateHeaderAndCommunicate (
IN OUT UINT8 *CommunicateBuffer,
IN UINTN CommunicateBufferSize,
IN UINTN Function
)
{
EFI_STATUS Status;
EFI_PEI_MM_COMMUNICATION_PPI *MmCommunicationPpi;
EFI_PEI_MM_COMMUNICATION3_PPI *MmCommunicationPpiV3;
EFI_MM_COMMUNICATE_HEADER *MmCommunicateHeader;
EFI_MM_COMMUNICATE_HEADER_V3 *MmCommunicateHeaderV3;
SMM_VARIABLE_COMMUNICATE_HEADER *MmVarCommsHeader;
// Minimal sanity check
if ((CommunicateBuffer == NULL) ||
(CommunicateBufferSize < MM_VARIABLE_COMM_BUFFER_OFFSET))
{
Status = EFI_INVALID_PARAMETER;
DEBUG ((DEBUG_ERROR, "%a: Invalid incoming parameters: %p and 0x%x\n", __func__, CommunicateBuffer, CommunicateBufferSize));
goto Exit;
}
if ((Function != SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME) &&
(Function != SMM_VARIABLE_FUNCTION_GET_VARIABLE))
{
Status = EFI_INVALID_PARAMETER;
DEBUG ((DEBUG_ERROR, "%a: Invalid function value: 0x%x\n", __func__, Function));
goto Exit;
}
MmCommunicationPpiV3 = NULL;
MmCommunicationPpi = NULL;
Status = PeiServicesLocatePpi (&gEfiPeiMmCommunication3PpiGuid, 0, NULL, (VOID **)&MmCommunicationPpiV3);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "%a: Unable to locate PEI MM Communication3 PPI: %r\n", __func__, Status));
// Try to locate the older version of the PPI
Status = PeiServicesLocatePpi (&gEfiPeiMmCommunicationPpiGuid, 0, NULL, (VOID **)&MmCommunicationPpi);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "%a: Failed to locate PEI MM Communication PPI: %r\n", __func__, Status));
goto Exit;
}
}
if (MmCommunicationPpiV3 != NULL) {
// Zero the entire Communication Buffer Header
MmCommunicateHeaderV3 = (EFI_MM_COMMUNICATE_HEADER_V3 *)CommunicateBuffer;
ZeroMem (MmCommunicateHeaderV3, SMM_COMMUNICATE_HEADER_SIZE_V3);
// Use gEfiSmmVariableProtocolGuid to request the MM variable service in Standalone MM
CopyMem ((VOID *)&MmCommunicateHeaderV3->HeaderGuid, &gEfiMmCommunicateHeaderV3Guid, sizeof (GUID));
MmCommunicateHeaderV3->BufferSize = CommunicateBufferSize;
CopyMem ((VOID *)&MmCommunicateHeaderV3->MessageGuid, &gEfiSmmVariableProtocolGuid, sizeof (GUID));
// Program the MM header size
MmCommunicateHeaderV3->MessageSize = CommunicateBufferSize - SMM_COMMUNICATE_HEADER_SIZE_V3;
MmVarCommsHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)(MmCommunicateHeaderV3->MessageData);
// We are only supporting GetVariable and GetNextVariableName
MmVarCommsHeader->Function = Function;
// Send the MM request using MmCommunicationPei
Status = MmCommunicationPpiV3->Communicate (MmCommunicationPpiV3, CommunicateBuffer);
} else if (MmCommunicationPpi != NULL) {
// Zero the entire Communication Buffer Header
MmCommunicateHeader = (EFI_MM_COMMUNICATE_HEADER *)CommunicateBuffer;
ZeroMem (MmCommunicateHeader, SMM_COMMUNICATE_HEADER_SIZE);
// Use gEfiSmmVariableProtocolGuid to request the MM variable service in Standalone MM
CopyMem ((VOID *)&MmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid, sizeof (GUID));
// Program the MM header size
MmCommunicateHeader->MessageLength = CommunicateBufferSize - SMM_COMMUNICATE_HEADER_SIZE;
MmVarCommsHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)(CommunicateBuffer + SMM_COMMUNICATE_HEADER_SIZE);
// We are only supporting GetVariable and GetNextVariableName
MmVarCommsHeader->Function = Function;
// Send the MM request using MmCommunicationPei
Status = MmCommunicationPpi->Communicate (MmCommunicationPpi, CommunicateBuffer, &CommunicateBufferSize);
} else {
// We should never reach here, but just in case
Status = EFI_UNSUPPORTED;
DEBUG ((DEBUG_ERROR, "%a: No MM Communication PPI found!\n", __func__));
}
if (EFI_ERROR (Status)) {
// Received an error from MM interface.
DEBUG ((DEBUG_ERROR, "%a - MM Interface Error: %r\n", __func__, Status));
goto Exit;
}
// MM request was successfully handled by the framework.
// Set status to the Variable Service Status Code
Status = MmVarCommsHeader->ReturnStatus;
if (EFI_ERROR (Status)) {
// We received an error from Variable Service.
// We cant do anymore so return Status
if (Status != EFI_BUFFER_TOO_SMALL) {
DEBUG ((DEBUG_ERROR, "%a - Variable Service Error: %r\n", __func__, Status));
}
goto Exit;
}
Exit:
return Status;
}
/**
This service retrieves a variable's value using its name and GUID.
This function is using the Secure Variable Store. If the Data
buffer is too small to hold the contents of the variable, the error
EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer
size to obtain the data.
@param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
@param VariableName A pointer to a null-terminated string that is the variable's name.
@param VariableGuid A pointer to an EFI_GUID that is the variable's GUID. The combination of
VariableGuid and VariableName must be unique.
@param Attributes If non-NULL, on return, points to the variable's attributes.
@param DataSize On entry, points to the size in bytes of the Data buffer.
On return, points to the size of the data returned in Data.
@param Data Points to the buffer which will hold the returned variable value.
May be NULL with a zero DataSize in order to determine the size of the buffer needed.
@retval EFI_SUCCESS The variable was read successfully.
@retval EFI_NOT_FOUND The variable was not found.
@retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the resulting data.
DataSize is updated with the size required for
the specified variable.
@retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.
@retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
**/
EFI_STATUS
EFIAPI
PeiMmGetVariable (
IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
IN CONST CHAR16 *VariableName,
IN CONST EFI_GUID *VariableGuid,
OUT UINT32 *Attributes, OPTIONAL
IN OUT UINTN *DataSize,
OUT VOID *Data OPTIONAL
)
{
EFI_STATUS Status;
UINTN MessageSize;
SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *MmVarAccessHeader;
UINT8 *MmCommunicateBuffer;
UINTN RequiredPages;
// Check input parameters
if ((VariableName == NULL) || (VariableGuid == NULL) || (DataSize == NULL)) {
return EFI_INVALID_PARAMETER;
}
if (VariableName[0] == 0) {
return EFI_NOT_FOUND;
}
if ((*DataSize > 0) && (Data == NULL)) {
return EFI_INVALID_PARAMETER;
}
// Allocate required pages to send MM request
MessageSize = MM_VARIABLE_COMM_BUFFER_OFFSET +
OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) +
StrSize (VariableName) + *DataSize;
RequiredPages = EFI_SIZE_TO_PAGES (MessageSize);
MmCommunicateBuffer = (UINT8 *)AllocatePages (RequiredPages);
if (MmCommunicateBuffer == NULL) {
Status = EFI_OUT_OF_RESOURCES;
DEBUG ((DEBUG_ERROR, "%a: Failed to allocate memory: %r\n", __func__, Status));
return Status;
}
// Zero the entire Communication Buffer
ZeroMem (MmCommunicateBuffer, (RequiredPages * EFI_PAGE_SIZE));
//
// Program all payload structure contents
//
MmVarAccessHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *)(MmCommunicateBuffer + MM_VARIABLE_COMM_BUFFER_OFFSET);
// Variable GUID
CopyMem ((VOID *)&MmVarAccessHeader->Guid, VariableGuid, sizeof (GUID));
// Program the max amount of data we accept.
MmVarAccessHeader->DataSize = *DataSize;
// Get size of the variable name
MmVarAccessHeader->NameSize = StrSize (VariableName);
// Populate incoming variable name
CopyMem ((VOID *)&MmVarAccessHeader->Name, VariableName, MmVarAccessHeader->NameSize);
Status = PopulateHeaderAndCommunicate (MmCommunicateBuffer, MessageSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
if (EFI_ERROR (Status)) {
// We received an error from either communicate or Variable Service.
if (Status != EFI_BUFFER_TOO_SMALL) {
DEBUG ((DEBUG_ERROR, "%a - Communite to MM for variable service errored: %r\n", __func__, Status));
}
goto Exit;
}
Status = EFI_SUCCESS;
// User provided buffer is too small
if (*DataSize < MmVarAccessHeader->DataSize) {
Status = EFI_BUFFER_TOO_SMALL;
}
Exit:
// Check if we need to set Attributes
if (Attributes != NULL) {
*Attributes = MmVarAccessHeader->Attributes;
}
*DataSize = MmVarAccessHeader->DataSize;
if (Status == EFI_SUCCESS) {
CopyMem ((VOID *)Data, (UINT8 *)MmVarAccessHeader->Name + MmVarAccessHeader->NameSize, *DataSize);
}
// Free the Communication Buffer
if (MmCommunicateBuffer != NULL) {
FreePages (MmCommunicateBuffer, RequiredPages);
}
return Status;
}
/**
Return the next variable name and GUID.
This function is called multiple times to retrieve the VariableName
and VariableGuid of all variables currently available in the system.
On each call, the previous results are passed into the interface,
and, on return, the interface returns the data for the next
interface. When the entire variable list has been returned,
EFI_NOT_FOUND is returned.
@param This A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.
@param VariableNameSize On entry, points to the size of the buffer pointed to by VariableName.
On return, the size of the variable name buffer.
@param VariableName On entry, a pointer to a null-terminated string that is the variable's name.
On return, points to the next variable's null-terminated name string.
@param VariableGuid On entry, a pointer to an EFI_GUID that is the variable's GUID.
On return, a pointer to the next variable's GUID.
@retval EFI_SUCCESS The variable was read successfully.
@retval EFI_NOT_FOUND The variable could not be found.
@retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the resulting
data. VariableNameSize is updated with the size
required for the specified variable.
@retval EFI_INVALID_PARAMETER VariableName, VariableGuid or
VariableNameSize is NULL.
@retval EFI_DEVICE_ERROR The variable could not be retrieved because of a device error.
**/
EFI_STATUS
EFIAPI
PeiMmGetNextVariableName (
IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
IN OUT UINTN *VariableNameSize,
IN OUT CHAR16 *VariableName,
IN OUT EFI_GUID *VariableGuid
)
{
EFI_STATUS Status;
UINTN MessageSize;
UINT8 *MmCommunicateBuffer;
SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *MmVarGetNextVarHeader;
UINTN RequiredPages;
// Check input parameters
if ((VariableName == NULL) ||
(VariableGuid == NULL) ||
(VariableNameSize == NULL) ||
(*VariableNameSize == 0))
{
return EFI_INVALID_PARAMETER;
}
// Allocate required pages to send MM request
MessageSize = MM_VARIABLE_COMM_BUFFER_OFFSET +
OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) +
StrSize (VariableName) + *VariableNameSize;
RequiredPages = EFI_SIZE_TO_PAGES (MessageSize);
MmCommunicateBuffer = (UINT8 *)AllocatePages (RequiredPages);
if (MmCommunicateBuffer == NULL) {
Status = EFI_OUT_OF_RESOURCES;
DEBUG ((DEBUG_ERROR, "%a: Failed to allocate memory: %r\n", __func__, Status));
return Status;
}
// Zero the entire Communication Buffer
ZeroMem (MmCommunicateBuffer, (RequiredPages * EFI_PAGE_SIZE));
//
// Program all payload structure contents
//
MmVarGetNextVarHeader = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *)(MmCommunicateBuffer + MM_VARIABLE_COMM_BUFFER_OFFSET);
// Variable GUID
CopyMem ((VOID *)&MmVarGetNextVarHeader->Guid, VariableGuid, sizeof (GUID));
// Program the maximal length of name we can accept.
MmVarGetNextVarHeader->NameSize = *VariableNameSize;
// Populate incoming variable name
CopyMem ((VOID *)&MmVarGetNextVarHeader->Name, VariableName, MmVarGetNextVarHeader->NameSize);
// Send the MM request using MmCommunicationPei
Status = PopulateHeaderAndCommunicate (MmCommunicateBuffer, MessageSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);
if (EFI_ERROR (Status)) {
// We received an error from either communicate or Variable Service.
if (Status != EFI_BUFFER_TOO_SMALL) {
DEBUG ((DEBUG_ERROR, "%a - Communite to MM for variable service errored: %r\n", __func__, Status));
}
goto Exit;
}
Status = EFI_SUCCESS;
// User provided buffer is too small
if (*VariableNameSize < MmVarGetNextVarHeader->NameSize) {
Status = EFI_BUFFER_TOO_SMALL;
}
Exit:
// Update the name size to be returned
*VariableNameSize = MmVarGetNextVarHeader->NameSize;
if (Status == EFI_SUCCESS) {
CopyMem ((VOID *)VariableName, (UINT8 *)MmVarGetNextVarHeader->Name, *VariableNameSize);
CopyMem ((VOID *)VariableGuid, (UINT8 *)&(MmVarGetNextVarHeader->Guid), sizeof (EFI_GUID));
}
// Free the Communication Buffer
if (MmCommunicateBuffer != NULL) {
FreePages (MmCommunicateBuffer, RequiredPages);
}
return Status;
}
|