]> git.proxmox.com Git - mirror_edk2.git/blobdiff - FmpDevicePkg/FmpDxe/FmpDxe.c
FmpDevicePkg DSC: Add NOOPT target to verify NOOPT target
[mirror_edk2.git] / FmpDevicePkg / FmpDxe / FmpDxe.c
index f0e8b0da823a342938739628dfce06544ac6a74a..57eac5ac147fc925b2c4ac98c4a3ba42d57627d0 100644 (file)
@@ -1,4 +1,4 @@
-/**  @file\r
+/** @file\r
   Produces a Firmware Management Protocol that supports updates to a firmware\r
   image stored in a firmware device with platform and firmware device specific\r
   information provided through PCDs and libraries.\r
@@ -89,6 +89,13 @@ UINT64  mImageId      = 0x1;
 CHAR16  *mVersionName = NULL;\r
 \r
 EFI_EVENT  mFmpDeviceLockEvent;\r
+//\r
+// Indicates if an attempt has been made to lock a\r
+// FLASH storage device by calling FmpDeviceLock().\r
+// A FLASH storage device may not support being locked,\r
+// so this variable is set to TRUE even if FmpDeviceLock()\r
+// returns an error.\r
+//\r
 BOOLEAN    mFmpDeviceLocked = FALSE;\r
 \r
 /**\r
@@ -222,6 +229,7 @@ GetLowestSupportedVersion (
   //\r
   // Check the FmpDeviceLib\r
   //\r
+  DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;\r
   Status = FmpDeviceGetLowestSupportedVersion (&DeviceLibLowestSupportedVersion);\r
   if (EFI_ERROR (Status)) {\r
     DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;\r
@@ -466,11 +474,11 @@ cleanup:
   This function allows a copy of the current firmware image to be created and saved.\r
   The saved copy could later been used, for example, in firmware image recovery or rollback.\r
 \r
-  @param[in]  This               A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
-  @param[in]  ImageIndex         A unique number identifying the firmware image(s) within the device.\r
+  @param[in]      This           A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
+  @param[in]      ImageIndex     A unique number identifying the firmware image(s) within the device.\r
                                  The number is between 1 and DescriptorCount.\r
-  @param[out] Image              Points to the buffer where the current image is copied to.\r
-  @param[out] ImageSize          On entry, points to the size of the buffer pointed to by Image, in bytes.\r
+  @param[in, out] Image          Points to the buffer where the current image is copied to.\r
+  @param[in, out] ImageSize      On entry, points to the size of the buffer pointed to by Image, in bytes.\r
                                  On return, points to the length of the image, in bytes.\r
 \r
   @retval EFI_SUCCESS            The device was successfully updated with the new image.\r
@@ -480,7 +488,7 @@ cleanup:
   @retval EFI_INVALID_PARAMETER  The Image was NULL.\r
   @retval EFI_NOT_FOUND          The current image is not copied to the buffer.\r
   @retval EFI_UNSUPPORTED        The operation is not supported.\r
-  @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.\r
+  @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -497,7 +505,16 @@ GetTheImage (
 \r
   Status = EFI_SUCCESS;\r
 \r
-  if ((ImageSize == NULL)) {\r
+  //\r
+  // Check to make sure index is 1 (only 1 image for this device)\r
+  //\r
+  if (ImageIndex != 1) {\r
+    DEBUG ((DEBUG_ERROR, "FmpDxe: GetImage() - Image Index Invalid.\n"));\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto cleanup;\r
+  }\r
+\r
+  if (ImageSize == NULL) {\r
     DEBUG ((DEBUG_ERROR, "FmpDxe: GetImage() - ImageSize Pointer Parameter is NULL.\n"));\r
     Status = EFI_INVALID_PARAMETER;\r
     goto cleanup;\r
@@ -523,16 +540,6 @@ GetTheImage (
     goto cleanup;\r
   }\r
 \r
-  //\r
-  // Check to make sure index is 1 (only 1 image for this device)\r
-  //\r
-  if (ImageIndex != 1) {\r
-    DEBUG ((DEBUG_ERROR, "FmpDxe: GetImage() - Image Index Invalid.\n"));\r
-    Status = EFI_INVALID_PARAMETER;\r
-    goto cleanup;\r
-  }\r
-\r
-\r
   Status = FmpDeviceGetImage (Image, ImageSize);\r
 cleanup:\r
 \r
@@ -626,9 +633,10 @@ GetAllHeaderSize (
                                  if available, additional information if the image is invalid.\r
 \r
   @retval EFI_SUCCESS            The image was successfully checked.\r
+  @retval EFI_ABORTED            The operation is aborted.\r
   @retval EFI_INVALID_PARAMETER  The Image was NULL.\r
   @retval EFI_UNSUPPORTED        The operation is not supported.\r
-  @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.\r
+  @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -638,7 +646,7 @@ CheckTheImage (
   IN  UINT8                             ImageIndex,\r
   IN  CONST VOID                        *Image,\r
   IN  UINTN                             ImageSize,\r
-  OUT UINT32                            *ImageUpdateable\r
+  OUT UINT32                            *ImageUpdatable\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -669,8 +677,8 @@ CheckTheImage (
     PopulateDescriptor();\r
   }\r
 \r
-  if (ImageUpdateable == NULL) {\r
-    DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - ImageUpdateable Pointer Parameter is NULL.\n"));\r
+  if (ImageUpdatable == NULL) {\r
+    DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - ImageUpdatable Pointer Parameter is NULL.\n"));\r
     Status = EFI_INVALID_PARAMETER;\r
     goto cleanup;\r
   }\r
@@ -678,14 +686,14 @@ CheckTheImage (
   //\r
   //Set to valid and then if any tests fail it will update this flag.\r
   //\r
-  *ImageUpdateable = IMAGE_UPDATABLE_VALID;\r
+  *ImageUpdatable = IMAGE_UPDATABLE_VALID;\r
 \r
   if (Image == NULL) {\r
     DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - Image Pointer Parameter is NULL.\n"));\r
     //\r
     // not sure if this is needed\r
     //\r
-    *ImageUpdateable = IMAGE_UPDATABLE_INVALID;\r
+    *ImageUpdatable = IMAGE_UPDATABLE_INVALID;\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -759,7 +767,7 @@ CheckTheImage (
   //\r
   if (ImageIndex != 1) {\r
     DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - Image Index Invalid.\n"));\r
-    *ImageUpdateable = IMAGE_UPDATABLE_INVALID_TYPE;\r
+    *ImageUpdatable = IMAGE_UPDATABLE_INVALID_TYPE;\r
     Status = EFI_SUCCESS;\r
     goto cleanup;\r
   }\r
@@ -777,7 +785,7 @@ CheckTheImage (
   Status = GetFmpPayloadHeaderVersion (FmpPayloadHeader, FmpPayloadSize, &Version);\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderVersion failed %r.\n", Status));\r
-    *ImageUpdateable = IMAGE_UPDATABLE_INVALID;\r
+    *ImageUpdatable = IMAGE_UPDATABLE_INVALID;\r
     Status = EFI_SUCCESS;\r
     goto cleanup;\r
   }\r
@@ -791,7 +799,7 @@ CheckTheImage (
       "FmpDxe: CheckTheImage() - Version Lower than lowest supported version. 0x%08X < 0x%08X\n",\r
       Version, mDesc.LowestSupportedImageVersion)\r
       );\r
-    *ImageUpdateable = IMAGE_UPDATABLE_INVALID_OLD;\r
+    *ImageUpdatable = IMAGE_UPDATABLE_INVALID_OLD;\r
     Status = EFI_SUCCESS;\r
     goto cleanup;\r
   }\r
@@ -802,7 +810,7 @@ CheckTheImage (
   Status = GetFmpPayloadHeaderSize (FmpPayloadHeader, FmpPayloadSize, &FmpHeaderSize);\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status));\r
-    *ImageUpdateable = IMAGE_UPDATABLE_INVALID;\r
+    *ImageUpdatable = IMAGE_UPDATABLE_INVALID;\r
     Status = EFI_SUCCESS;\r
     goto cleanup;\r
   }\r
@@ -822,7 +830,7 @@ CheckTheImage (
   //\r
   // FmpDeviceLib CheckImage function to do any specific checks\r
   //\r
-  Status = FmpDeviceCheckImage ((((UINT8 *)Image) + AllHeaderSize), RawSize, ImageUpdateable);\r
+  Status = FmpDeviceCheckImage ((((UINT8 *)Image) + AllHeaderSize), RawSize, ImageUpdatable);\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - FmpDeviceLib CheckImage failed. Status = %r\n", Status));\r
   }\r
@@ -874,7 +882,7 @@ cleanup:
   @retval EFI_ABORTED            The operation is aborted.\r
   @retval EFI_INVALID_PARAMETER  The Image was NULL.\r
   @retval EFI_UNSUPPORTED        The operation is not supported.\r
-  @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.\r
+  @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -898,6 +906,8 @@ SetTheImage (
   UINT32      AllHeaderSize;\r
   UINT32      IncommingFwVersion;\r
   UINT32      LastAttemptStatus;\r
+  UINT32      Version;\r
+  UINT32      LowestSupportedVersion;\r
 \r
   Status             = EFI_SUCCESS;\r
   Updateable         = 0;\r
@@ -918,7 +928,7 @@ SetTheImage (
   //\r
   if (mFmpDeviceLocked) {\r
     DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Device is already locked.  Can't update.\n"));\r
-    Status = EFI_ACCESS_DENIED;\r
+    Status = EFI_UNSUPPORTED;\r
     goto cleanup;\r
   }\r
 \r
@@ -1092,7 +1102,7 @@ SetTheImage (
   // Update the version stored in variable\r
   //\r
   if (!mRuntimeVersionSupported) {\r
-    UINT32 Version = DEFAULT_VERSION;\r
+    Version = DEFAULT_VERSION;\r
     GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &Version);\r
     SetVersionInVariable (Version);\r
   }\r
@@ -1101,21 +1111,13 @@ SetTheImage (
   // Update lowest supported variable\r
   //\r
   {\r
-    UINT32 Version = DEFAULT_LOWESTSUPPORTEDVERSION;\r
-    GetFmpPayloadHeaderLowestSupportedVersion (FmpHeader, FmpPayloadSize, &Version);\r
-    SetLowestSupportedVersionInVariable (Version);\r
+    LowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;\r
+    GetFmpPayloadHeaderLowestSupportedVersion (FmpHeader, FmpPayloadSize, &LowestSupportedVersion);\r
+    SetLowestSupportedVersionInVariable (LowestSupportedVersion);\r
   }\r
 \r
   LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;\r
 \r
-  //\r
-  // Set flag so the descriptor is repopulated\r
-  // This only applied to devices that do not require system reboot\r
-  //\r
-  if (!PcdGetBool (PcdFmpDeviceSystemResetRequired)) {\r
-    mDescriptorPopulated = FALSE;\r
-  }\r
-\r
 cleanup:\r
   mProgressFunc = NULL;\r
   mProgressSupported = FALSE;\r
@@ -1128,6 +1130,12 @@ cleanup:
     Progress (100);\r
   }\r
 \r
+  //\r
+  // Need repopulate after SetImage is called to\r
+  // update LastAttemptVersion and LastAttemptStatus.\r
+  //\r
+  mDescriptorPopulated = FALSE;\r
+\r
   return Status;\r
 }\r
 \r
@@ -1205,7 +1213,7 @@ GetPackageInfo (
   @retval EFI_INVALID_PARAMETER  The PackageVersionName length is longer than the value\r
                                  returned in PackageVersionNameMaxLen.\r
   @retval EFI_UNSUPPORTED        The operation is not supported.\r
-  @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.\r
+  @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1240,32 +1248,18 @@ FmpDxeLockEventNotify (
   EFI_STATUS  Status;\r
 \r
   if (!mFmpDeviceLocked) {\r
-    if (IsLockFmpDeviceAtLockEventGuidRequired ()) {\r
-      //\r
-      // Lock all UEFI Variables used by this module.\r
-      //\r
-      Status = LockAllFmpVariables ();\r
-      if (EFI_ERROR (Status)) {\r
-        DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to lock variables.  Status = %r.\n"));\r
+    //\r
+    // Lock the firmware device\r
+    //\r
+    Status = FmpDeviceLock();\r
+    if (EFI_ERROR (Status)) {\r
+      if (Status != EFI_UNSUPPORTED) {\r
+        DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLock() returned error.  Status = %r\n", Status));\r
       } else {\r
-        DEBUG ((DEBUG_INFO, "FmpDxe: All variables locked\n"));\r
-      }\r
-\r
-      //\r
-      // Lock the firmware device\r
-      //\r
-      Status = FmpDeviceLock();\r
-      if (EFI_ERROR (Status)) {\r
-        if (Status != EFI_UNSUPPORTED) {\r
-          DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLock() returned error.  Status = %r\n", Status));\r
-        } else {\r
-          DEBUG ((DEBUG_WARN, "FmpDxe: FmpDeviceLock() returned error.  Status = %r\n", Status));\r
-        }\r
+        DEBUG ((DEBUG_WARN, "FmpDxe: FmpDeviceLock() returned error.  Status = %r\n", Status));\r
       }\r
-      mFmpDeviceLocked = TRUE;\r
-    } else {\r
-      DEBUG ((DEBUG_VERBOSE, "FmpDxe: Not calling FmpDeviceLock() because mfg mode\n"));\r
     }\r
+    mFmpDeviceLocked = TRUE;\r
   }\r
 }\r
 \r
@@ -1409,6 +1403,45 @@ FmpDxeEntryPoint (
   //\r
   DetectTestKey ();\r
 \r
+  if (IsLockFmpDeviceAtLockEventGuidRequired ()) {\r
+    //\r
+    // Lock all UEFI Variables used by this module.\r
+    //\r
+    Status = LockAllFmpVariables ();\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to lock variables.  Status = %r.\n", Status));\r
+    } else {\r
+      DEBUG ((DEBUG_INFO, "FmpDxe: All variables locked\n"));\r
+    }\r
+\r
+    //\r
+    // Register notify function to lock the FMP device.\r
+    // The lock event GUID is retrieved from PcdFmpDeviceLockEventGuid.\r
+    // If PcdFmpDeviceLockEventGuid is not the size of an EFI_GUID, then\r
+    // gEfiEndOfDxeEventGroupGuid is used.\r
+    //\r
+    LockGuid = &gEfiEndOfDxeEventGroupGuid;\r
+    if (PcdGetSize (PcdFmpDeviceLockEventGuid) == sizeof (EFI_GUID)) {\r
+      LockGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceLockEventGuid);\r
+    }\r
+    DEBUG ((DEBUG_INFO, "FmpDxe: Lock GUID: %g\n", LockGuid));\r
+\r
+    Status = gBS->CreateEventEx (\r
+                    EVT_NOTIFY_SIGNAL,\r
+                    TPL_CALLBACK,\r
+                    FmpDxeLockEventNotify,\r
+                    NULL,\r
+                    LockGuid,\r
+                    &mFmpDeviceLockEvent\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to register notification.  Status = %r\n", Status));\r
+    }\r
+    ASSERT_EFI_ERROR (Status);\r
+  } else {\r
+    DEBUG ((DEBUG_VERBOSE, "FmpDxe: Not registering notification to call FmpDeviceLock() because mfg mode\n"));\r
+  }\r
+\r
   //\r
   // Register with library the install function so if the library uses\r
   // UEFI driver model/driver binding protocol it can install FMP on its device handle\r
@@ -1428,30 +1461,5 @@ FmpDxeEntryPoint (
       ));\r
   }\r
 \r
-  //\r
-  // Register notify function to lock the FMP device.\r
-  // The lock event GUID is retrieved from PcdFmpDeviceLockEventGuid.\r
-  // If PcdFmpDeviceLockEventGuid is not the size of an EFI_GUID, then\r
-  // gEfiEndOfDxeEventGroupGuid is used.\r
-  //\r
-  LockGuid = &gEfiEndOfDxeEventGroupGuid;\r
-  if (PcdGetSize (PcdFmpDeviceLockEventGuid) == sizeof (EFI_GUID)) {\r
-    LockGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceLockEventGuid);\r
-  }\r
-  DEBUG ((DEBUG_INFO, "FmpDxe: Lock GUID: %g\n", LockGuid));\r
-\r
-  Status = gBS->CreateEventEx (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_CALLBACK,\r
-                  FmpDxeLockEventNotify,\r
-                  NULL,\r
-                  LockGuid,\r
-                  &mFmpDeviceLockEvent\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to register for ready to boot.  Status = %r\n", Status));\r
-  }\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
   return Status;\r
 }\r