FmpDevicePkg: Add FmpDxe module

https://bugzilla.tianocore.org/show_bug.cgi?id=922

Based on content from the following branch:

https://github.com/Microsoft/MS_UEFI/tree/share/MsCapsuleSupport/MsCapsuleUpdatePkg

The FmpDxe directory contains 2 INF files.  FmpDxe.inf
is a DXE driver that is used in a platform to add a
Firmware Management Protocol for firmware device that
supports firmware updates.

FmpDxeLib.inf is a NULL library instance with the exact
same functionality as FmpDxe.inf, but allows the the
Firmware Management Protocol feature to be added to
an existing device driver.

The FmpDxe component is intended to be used "as is"
with no need for any device specific or platform specific
changes.

Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
This commit is contained in:
Kinney, Michael D 2018-04-04 10:31:06 -07:00
parent 403d4bcdec
commit b0bacc003a
8 changed files with 2480 additions and 0 deletions

View File

@ -0,0 +1,166 @@
/** @file
Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
#include <PiDxe.h>
#include <Library/DebugLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PcdLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseCryptLib.h>
/**
Check to see if any of the keys in PcdFmpDevicePkcs7CertBufferXdr matches
the test key. PcdFmpDeviceTestKeySha256Digest contains the SHA256 hash of
the test key. For each key in PcdFmpDevicePkcs7CertBufferXdr, compute the
SHA256 hash and compare it to PcdFmpDeviceTestKeySha256Digest. If the
SHA256 hash matches or there is then error computing the SHA256 hash, then
set PcdTestKeyUsed to TRUE. Skip this check if PcdTestKeyUsed is already
TRUE or PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE
bytes.
**/
VOID
DetectTestKey (
VOID
)
{
BOOLEAN TestKeyUsed;
UINTN PublicKeyDataLength;
UINT8 *PublicKeyDataXdr;
UINT8 *PublicKeyDataXdrEnd;
VOID *HashContext;
UINT8 Digest[SHA256_DIGEST_SIZE];
//
// If PcdFmpDeviceTestKeySha256Digest is not exacty SHA256_DIGEST_SIZE bytes,
// then skip the test key detection.
//
if (PcdGetSize (PcdFmpDeviceTestKeySha256Digest) != SHA256_DIGEST_SIZE) {
return;
}
//
// If PcdTestKeyUsed is already TRUE, then skip test key detection
//
TestKeyUsed = PcdGetBool (PcdTestKeyUsed);
if (TestKeyUsed) {
return;
}
//
// If PcdFmpDevicePkcs7CertBufferXdr is invalid, then skip test key detection
//
PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
if (PublicKeyDataXdr == NULL || PublicKeyDataXdr == PublicKeyDataXdrEnd) {
return;
}
//
// Allocate hash context buffer required for SHA 256
//
HashContext = AllocatePool (Sha256GetContextSize ());
if (HashContext == NULL) {
TestKeyUsed = TRUE;
}
//
// Loop through all keys in PcdFmpDevicePkcs7CertBufferXdr
//
while (!TestKeyUsed && PublicKeyDataXdr < PublicKeyDataXdrEnd) {
if (PublicKeyDataXdr + sizeof (UINT32) > PublicKeyDataXdrEnd) {
//
// Key data extends beyond end of PCD
//
break;
}
//
// Read key length stored in big endian format
//
PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
//
// Point to the start of the key data
//
PublicKeyDataXdr += sizeof (UINT32);
if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
//
// Key data extends beyond end of PCD
//
break;
}
//
// Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
// If error occurs computing SHA256, then assume test key is in use.
//
ZeroMem (Digest, SHA256_DIGEST_SIZE);
if (!Sha256Init (HashContext)) {
TestKeyUsed = TRUE;
break;
}
if (!Sha256Update (HashContext, PublicKeyDataXdr, PublicKeyDataLength)) {
TestKeyUsed = TRUE;
break;
}
if (!Sha256Final (HashContext, Digest)) {
TestKeyUsed = TRUE;
break;
}
//
// Check if SHA256 hash of public key matches SHA256 hash of test key
//
if (CompareMem (Digest, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest), SHA256_DIGEST_SIZE) == 0) {
TestKeyUsed = TRUE;
break;
}
//
// Point to start of next key
//
PublicKeyDataXdr += PublicKeyDataLength;
PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
}
//
// Free hash context buffer required for SHA 256
//
if (HashContext != NULL) {
FreePool (HashContext);
HashContext = NULL;
}
//
// If test key detected or an error occured checking for the test key, then
// set PcdTestKeyUsed to TRUE.
//
if (TestKeyUsed) {
DEBUG ((DEBUG_INFO, "FmpDxe: Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
PcdSetBoolS (PcdTestKeyUsed, TRUE);
} else {
DEBUG ((DEBUG_INFO, "FmpDxe: No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
}
}

1452
FmpDevicePkg/FmpDxe/FmpDxe.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,93 @@
## @file
# Produces a Firmware Management Protocol that supports updates to a firmware
# image stored in a firmware device with platform and firmware device specific
# information provided through PCDs and libraries.
#
# Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>
# Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##
[Defines]
INF_VERSION = 0x00010005
BASE_NAME = FmpDxe
MODULE_UNI_FILE = FmpDxe.uni
FILE_GUID = 78EF0A56-1CF0-4535-B5DA-F6FD2F405A11
MODULE_TYPE = DXE_DRIVER
VERSION_STRING = 1.0
ENTRY_POINT = FmpDxeEntryPoint
#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64 IPF ARM AARCH64
#
[Sources]
FmpDxe.c
DetectTestKey.c
VariableSupport.h
VariableSupport.c
[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
CryptoPkg/CryptoPkg.dec
FmpDevicePkg/FmpDevicePkg.dec
[LibraryClasses]
UefiDriverEntryPoint
DebugLib
BaseLib
BaseMemoryLib
UefiBootServicesTableLib
MemoryAllocationLib
UefiLib
BaseCryptLib
FmpAuthenticationLib
FmpDeviceLib
FmpPayloadHeaderLib
CapsuleUpdatePolicyLib
[Guids]
gEfiEndOfDxeEventGroupGuid
[Protocols]
gEdkiiVariableLockProtocolGuid ## CONSUMES
gEfiFirmwareManagementProtocolGuid ## PRODUCES
gEdkiiFirmwareManagementProgressProtocolGuid ## PRODUCES
[Pcd]
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceSystemResetRequired ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceImageIdName ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceBuildTimeLowestSupportedVersion ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceLockEventGuid ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceProgressWatchdogTimeInSeconds ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceProgressColor ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDevicePkcs7CertBufferXdr ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceTestKeySha256Digest ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdTestKeyUsed ## SOMETIMES_PRODUCES
[Depex]
gEfiVariableWriteArchProtocolGuid AND gEdkiiVariableLockProtocolGuid
[UserExtensions.TianoCore."ExtraFiles"]
FmpDxeExtra.uni

View File

@ -0,0 +1,20 @@
// /** @file
// Produces a Firmware Management Protocol that supports updates to a firmware
// image stored in a firmware device with platform and firmware device specific
// information provided through PCDs and libraries.
//
// Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
//
// This program and the accompanying materials
// are licensed and made available under the terms and conditions of the BSD License
// which accompanies this distribution. The full text of the license may be found at
// http://opensource.org/licenses/bsd-license.php
//
// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
//
// **/
#string STR_MODULE_ABSTRACT #language en-US "Produces a Firmware Management Protocol to support firmware updates"
#string STR_MODULE_DESCRIPTION #language en-US "Produces a Firmware Management Protocol that supports updates to a firmware image stored in a firmware device with platform and firmware device specific information provided through PCDs and libraries."

View File

@ -0,0 +1,18 @@
// /** @file
// FmpDxe Localized Strings and Content
//
// Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
//
// This program and the accompanying materials
// are licensed and made available under the terms and conditions of the BSD License
// which accompanies this distribution. The full text of the license may be found at
// http://opensource.org/licenses/bsd-license.php
//
// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
//
// **/
#string STR_PROPERTIES_MODULE_NAME
#language en-US
"Firmware Management Protocol DXE DXE Driver"

View File

@ -0,0 +1,90 @@
## @file
# Produces a Firmware Management Protocol that supports updates to a firmware
# image stored in a firmware device with platform and firmware device specific
# information provided through PCDs and libraries.
#
# Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>
# Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##
[Defines]
INF_VERSION = 0x00010005
BASE_NAME = FmpDxeLib
MODULE_UNI_FILE = FmpDxe.uni
FILE_GUID = 4B11717A-30B3-4122-8C69-8E0D5E141C32
MODULE_TYPE = DXE_DRIVER
VERSION_STRING = 1.0
LIBRARY_CLASS = NULL
CONSTRUCTOR = FmpDxeEntryPoint
#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64 IPF ARM AARCH64
#
[Sources]
FmpDxe.c
DetectTestKey.c
VariableSupport.h
VariableSupport.c
[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
CryptoPkg/CryptoPkg.dec
FmpDevicePkg/FmpDevicePkg.dec
[LibraryClasses]
DebugLib
BaseLib
BaseMemoryLib
UefiBootServicesTableLib
MemoryAllocationLib
UefiLib
BaseCryptLib
FmpAuthenticationLib
FmpDeviceLib
FmpPayloadHeaderLib
CapsuleUpdatePolicyLib
[Guids]
gEfiEndOfDxeEventGroupGuid
[Protocols]
gEdkiiVariableLockProtocolGuid ## CONSUMES
gEfiFirmwareManagementProtocolGuid ## PRODUCES
gEdkiiFirmwareManagementProgressProtocolGuid ## PRODUCES
[Pcd]
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceSystemResetRequired ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceImageIdName ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceBuildTimeLowestSupportedVersion ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceLockEventGuid ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceProgressWatchdogTimeInSeconds ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceProgressColor ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDevicePkcs7CertBufferXdr ## CONSUMES
gFmpDevicePkgTokenSpaceGuid.PcdFmpDeviceTestKeySha256Digest ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdTestKeyUsed ## SOMETIMES_PRODUCES
[Depex]
gEfiVariableWriteArchProtocolGuid AND gEdkiiVariableLockProtocolGuid

View File

@ -0,0 +1,461 @@
/** @file
UEFI variable support functions for Firmware Management Protocol based
firmware updates.
Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>
Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
#include <PiDxe.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Protocol/VariableLock.h>
#include "VariableSupport.h"
///
/// Array of UEFI variable names that are locked in LockAllFmpVariables().
///
const CHAR16 *mFmpVariableLockList[] = {
VARNAME_VERSION,
VARNAME_LSV,
VARNAME_LASTATTEMPTSTATUS,
VARNAME_LASTATTEMPTVERSION
};
/**
Returns the value used to fill in the Version field of the
EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
service of the Firmware Management Protocol. The value is read from a UEFI
variable. If the UEFI variables does not exist, then a default version value
is returned.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpVersion"
@return The version of the firmware image in the firmware device.
**/
UINT32
GetVersionFromVariable (
VOID
)
{
EFI_STATUS Status;
UINT32 *Value;
UINTN Size;
UINT32 Version;
Value = NULL;
Size = 0;
Version = DEFAULT_VERSION;
Status = GetVariable2 (VARNAME_VERSION, &gEfiCallerIdGuid, (VOID **)&Value, &Size);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed to get the Version from variable. Status = %r\n", Status));
return Version;
}
//
// No error from call
//
if (Size == sizeof (*Value)) {
//
// Successful read
//
Version = *Value;
} else {
//
// Return default since size was unknown
//
DEBUG ((DEBUG_ERROR, "Getting version Variable returned a size different than expected. Size = 0x%x\n", Size));
}
FreePool (Value);
return Version;
}
/**
Returns the value used to fill in the LowestSupportedVersion field of the
EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
service of the Firmware Management Protocol. The value is read from a UEFI
variable. If the UEFI variables does not exist, then a default lowest
supported version value is returned.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpLsv"
@return The lowest supported version of the firmware image in the firmware
device.
**/
UINT32
GetLowestSupportedVersionFromVariable (
VOID
)
{
EFI_STATUS Status;
UINT32 *Value;
UINTN Size;
UINT32 Version;
Value = NULL;
Size = 0;
Version = DEFAULT_LOWESTSUPPORTEDVERSION;
Status = GetVariable2 (VARNAME_LSV, &gEfiCallerIdGuid, (VOID **)&Value, &Size);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Warning: Failed to get the Lowest Supported Version from variable. Status = %r\n", Status));
return Version;
}
//
// No error from call
//
if (Size == sizeof (*Value)) {
//
// Successful read
//
Version = *Value;
} else {
//
// Return default since size was unknown
//
DEBUG ((DEBUG_ERROR, "Getting LSV Variable returned a size different than expected. Size = 0x%x\n", Size));
}
FreePool (Value);
return Version;
}
/**
Returns the value used to fill in the LastAttemptStatus field of the
EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
service of the Firmware Management Protocol. The value is read from a UEFI
variable. If the UEFI variables does not exist, then a default last attempt
status value is returned.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptStatus"
@return The last attempt status value for the most recent capsule update.
**/
UINT32
GetLastAttemptStatusFromVariable (
VOID
)
{
EFI_STATUS Status;
UINT32 *Value;
UINTN Size;
UINT32 LastAttemptStatus;
Value = NULL;
Size = 0;
LastAttemptStatus = DEFAULT_LASTATTEMPT;
Status = GetVariable2 (VARNAME_LASTATTEMPTSTATUS, &gEfiCallerIdGuid, (VOID **)&Value, &Size);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Warning: Failed to get the Last Attempt Status from variable. Status = %r\n", Status));
return LastAttemptStatus;
}
//
// No error from call
//
if (Size == sizeof (*Value)) {
//
// Successful read
//
LastAttemptStatus = *Value;
} else {
//
// Return default since size was unknown
//
DEBUG (
(DEBUG_ERROR,
"Getting Last Attempt Status Variable returned a size different than expected. Size = 0x%x\n",
Size)
);
}
FreePool (Value);
return LastAttemptStatus;
}
/**
Returns the value used to fill in the LastAttemptVersion field of the
EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
service of the Firmware Management Protocol. The value is read from a UEFI
variable. If the UEFI variables does not exist, then a default last attempt
version value is returned.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptVersion"
@return The last attempt version value for the most recent capsule update.
**/
UINT32
GetLastAttemptVersionFromVariable (
VOID
)
{
EFI_STATUS Status;
UINT32 *Value;
UINTN Size;
UINT32 Version;
Value = NULL;
Size = 0;
Version = DEFAULT_LASTATTEMPT;
Status = GetVariable2 (VARNAME_LASTATTEMPTVERSION, &gEfiCallerIdGuid, (VOID **)&Value, &Size);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Warning: Failed to get the Last Attempt Version from variable. Status = %r\n", Status));
return Version;
}
//
// No error from call
//
if (Size == sizeof (*Value)) {
//
// Successful read
//
Version = *Value;
} else {
//
// Return default since size was unknown
//
DEBUG (
(DEBUG_ERROR,
"Getting Last Attempt Version variable returned a size different than expected. Size = 0x%x\n",
Size)
);
}
FreePool (Value);
return Version;
}
/**
Saves the version current of the firmware image in the firmware device to a
UEFI variable.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpVersion"
@param[in] Version The version of the firmware image in the firmware device.
**/
VOID
SetVersionInVariable (
UINT32 Version
)
{
EFI_STATUS Status;
UINT32 Current;
Status = EFI_SUCCESS;
Current = GetVersionFromVariable();
if (Current != Version) {
Status = gRT->SetVariable (
VARNAME_VERSION,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
sizeof (Version),
&Version
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed to set the Version into a variable. Status = %r\n", Status));
}
} else {
DEBUG ((DEBUG_INFO, "Version variable doesn't need to update. Same value as before.\n"));
}
}
/**
Saves the lowest supported version current of the firmware image in the
firmware device to a UEFI variable.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpLsv"
@param[in] LowestSupported The lowest supported version of the firmware image
in the firmware device.
**/
VOID
SetLowestSupportedVersionInVariable (
UINT32 LowestSupportedVersion
)
{
EFI_STATUS Status;
UINT32 Current;
Status = EFI_SUCCESS;
Current = GetLowestSupportedVersionFromVariable();
if (LowestSupportedVersion > Current) {
Status = gRT->SetVariable (
VARNAME_LSV,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
sizeof (LowestSupportedVersion), &LowestSupportedVersion
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed to set the LSV into a variable. Status = %r\n", Status));
}
} else {
DEBUG ((DEBUG_INFO, "LSV variable doesn't need to update. Same value as before.\n"));
}
}
/**
Saves the last attempt status value of the most recent FMP capsule update to a
UEFI variable.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptStatus"
@param[in] LastAttemptStatus The last attempt status of the most recent FMP
capsule update.
**/
VOID
SetLastAttemptStatusInVariable (
UINT32 LastAttemptStatus
)
{
EFI_STATUS Status;
UINT32 Current;
Status = EFI_SUCCESS;
Current = GetLastAttemptStatusFromVariable();
if (Current != LastAttemptStatus) {
Status = gRT->SetVariable (
VARNAME_LASTATTEMPTSTATUS,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
sizeof (LastAttemptStatus),
&LastAttemptStatus
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed to set the LastAttemptStatus into a variable. Status = %r\n", Status));
}
} else {
DEBUG ((DEBUG_INFO, "LastAttemptStatus variable doesn't need to update. Same value as before.\n"));
}
}
/**
Saves the last attempt version value of the most recent FMP capsule update to
a UEFI variable.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptVersion"
@param[in] LastAttemptVersion The last attempt version value of the most
recent FMP capsule update.
**/
VOID
SetLastAttemptVersionInVariable (
UINT32 LastAttemptVersion
)
{
EFI_STATUS Status;
UINT32 Current;
Status = EFI_SUCCESS;
Current = GetLastAttemptVersionFromVariable();
if (Current != LastAttemptVersion) {
Status = gRT->SetVariable (
VARNAME_LASTATTEMPTVERSION,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
sizeof (LastAttemptVersion),
&LastAttemptVersion
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed to set the LastAttemptVersion into a variable. Status = %r\n", Status));
}
} else {
DEBUG ((DEBUG_INFO, "LastAttemptVersion variable doesn't need to update. Same value as before.\n"));
}
}
/**
Locks all the UEFI Variables used by this module.
@retval EFI_SUCCESS All UEFI variables are locked.
@retval EFI_UNSUPPORTED Variable Lock Protocol not found.
@retval Other One of the UEFI variables could not be locked.
**/
EFI_STATUS
LockAllFmpVariables (
VOID
)
{
EFI_STATUS Status;
EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;
EFI_STATUS ReturnStatus;
UINTN Index;
VariableLock = NULL;
Status = gBS->LocateProtocol (
&gEdkiiVariableLockProtocolGuid,
NULL,
(VOID **)&VariableLock
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to locate Variable Lock Protocol (%r).\n", Status));
return EFI_UNSUPPORTED;
}
ReturnStatus = EFI_SUCCESS;
for (Index = 0; Index < ARRAY_SIZE (mFmpVariableLockList); Index++) {
Status = VariableLock->RequestToLock (
VariableLock,
(CHAR16 *)mFmpVariableLockList[Index],
&gEfiCallerIdGuid
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to lock variable %g %s. Status = %r\n",
&gEfiCallerIdGuid,
mFmpVariableLockList[Index],
Status
));
if (!EFI_ERROR (ReturnStatus)) {
ReturnStatus = Status;
}
}
}
return ReturnStatus;
}

View File

@ -0,0 +1,180 @@
/** @file
UEFI variable support functions for Firmware Management Protocol based
firmware updates.
Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>
Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
#ifndef __VARIABLE_SUPPORT_H__
#define __VARIABLE_SUPPORT_H__
#define DEFAULT_VERSION 0x1
#define DEFAULT_LOWESTSUPPORTEDVERSION 0x0
#define DEFAULT_LASTATTEMPT 0x0
#define VARNAME_VERSION L"FmpVersion"
#define VARNAME_LSV L"FmpLsv"
#define VARNAME_LASTATTEMPTSTATUS L"LastAttemptStatus"
#define VARNAME_LASTATTEMPTVERSION L"LastAttemptVersion"
/**
Returns the value used to fill in the Version field of the
EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
service of the Firmware Management Protocol. The value is read from a UEFI
variable. If the UEFI variables does not exist, then a default version value
is returned.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpVersion"
@return The version of the firmware image in the firmware device.
**/
UINT32
GetVersionFromVariable (
VOID
);
/**
Returns the value used to fill in the LowestSupportedVersion field of the
EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
service of the Firmware Management Protocol. The value is read from a UEFI
variable. If the UEFI variables does not exist, then a default lowest
supported version value is returned.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpLsv"
@return The lowest supported version of the firmware image in the firmware
device.
**/
UINT32
GetLowestSupportedVersionFromVariable (
VOID
);
/**
Returns the value used to fill in the LastAttemptStatus field of the
EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
service of the Firmware Management Protocol. The value is read from a UEFI
variable. If the UEFI variables does not exist, then a default last attempt
status value is returned.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptStatus"
@return The last attempt status value for the most recent capsule update.
**/
UINT32
GetLastAttemptStatusFromVariable (
VOID
);
/**
Returns the value used to fill in the LastAttemptVersion field of the
EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the GetImageInfo()
service of the Firmware Management Protocol. The value is read from a UEFI
variable. If the UEFI variables does not exist, then a default last attempt
version value is returned.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptVersion"
@return The last attempt version value for the most recent capsule update.
**/
UINT32
GetLastAttemptVersionFromVariable (
VOID
);
/**
Saves the version current of the firmware image in the firmware device to a
UEFI variable.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpVersion"
@param[in] Version The version of the firmware image in the firmware device.
**/
VOID
SetVersionInVariable (
UINT32 Version
);
/**
Saves the lowest supported version current of the firmware image in the
firmware device to a UEFI variable.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"FmpLsv"
@param[in] LowestSupported The lowest supported version of the firmware image
in the firmware device.
**/
VOID
SetLowestSupportedVersionInVariable (
UINT32 LowestSupportedVersion
);
/**
Saves the last attempt status value of the most recent FMP capsule update to a
UEFI variable.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptStatus"
@param[in] LastAttemptStatus The last attempt status of the most recent FMP
capsule update.
**/
VOID
SetLastAttemptStatusInVariable (
UINT32 LastAttemptStatus
);
/**
Saves the last attempt version value of the most recent FMP capsule update to
a UEFI variable.
UEFI Variable accessed: GUID = gEfiCallerIdGuid, Name = L"LastAttemptVersion"
@param[in] LastAttemptVersion The last attempt version value of the most
recent FMP capsule update.
**/
VOID
SetLastAttemptVersionInVariable (
UINT32 LastAttemptVersion
);
/**
Locks all the UEFI Variables that use gEfiCallerIdGuid of the currently
executing module.
**/
EFI_STATUS
LockAllFmpVariables (
VOID
);
#endif