]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
Avoid change the return status from above code which will also be used later.
[mirror_edk2.git] / MdeModulePkg / Universal / CapsuleRuntimeDxe / CapsuleService.c
index 2a45bf37fe92d18778402989fd6c7ce611dccf69..61ee91951ba79f4833316d0d3f8652a679d3d567 100644 (file)
@@ -4,8 +4,8 @@
   It installs the Capsule Architectural Protocol defined in PI1.0a to signify \r
   the capsule runtime services are ready.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -19,6 +19,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include <Protocol/Capsule.h>\r
 #include <Guid/CapsuleVendor.h>\r
+#include <Guid/FmpCapsule.h>\r
 \r
 #include <Library/DebugLib.h>\r
 #include <Library/PcdLib.h>\r
@@ -27,6 +28,30 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/UefiRuntimeServicesTableLib.h>\r
 #include <Library/UefiRuntimeLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/PrintLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+//\r
+// Handle for the installation of Capsule Architecture Protocol.\r
+//\r
+EFI_HANDLE  mNewHandle = NULL;\r
+\r
+//\r
+// The times of calling UpdateCapsule ()\r
+//\r
+UINTN       mTimes      = 0;\r
+\r
+UINT32      mMaxSizePopulateCapsule     = 0;\r
+UINT32      mMaxSizeNonPopulateCapsule  = 0;\r
+\r
+/**\r
+  Create the variable to save the base address of page table and stack\r
+  for transferring into long mode in IA32 PEI.\r
+**/\r
+VOID\r
+SaveLongModeContext (\r
+  VOID\r
+  );\r
 \r
 /**\r
   Passes capsules to the firmware with both virtual and physical mapping. Depending on the intended\r
@@ -47,9 +72,16 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
                                 CAPSULE_FLAGS_PERSIT_ACROSS_RESET is not set, the\r
                                 capsule has been successfully processed by the firmware.\r
   @retval EFI_DEVICE_ERROR      The capsule update was started, but failed due to a device error.\r
-  @retval EFI_INVALID_PARAMETER CapsuleCount is Zero, or CapsuleImage is not valid.\r
-                                For across reset capsule image, ScatterGatherList is NULL.\r
+  @retval EFI_INVALID_PARAMETER CapsuleSize is NULL, or an incompatible set of flags were\r
+                                set in the capsule header.\r
+  @retval EFI_INVALID_PARAMETER CapsuleCount is Zero.\r
+  @retval EFI_INVALID_PARAMETER For across reset capsule image, ScatterGatherList is NULL.\r
   @retval EFI_UNSUPPORTED       CapsuleImage is not recognized by the firmware.\r
+  @retval EFI_OUT_OF_RESOURCES  When ExitBootServices() has been previously called this error indicates the capsule \r
+                                is compatible with this platform but is not capable of being submitted or processed \r
+                                in runtime. The caller may resubmit the capsule prior to ExitBootServices().\r
+  @retval EFI_OUT_OF_RESOURCES  When ExitBootServices() has not been previously called then this error indicates \r
+                                the capsule is compatible with this platform but there are insufficient resources to process.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -64,6 +96,9 @@ UpdateCapsule (
   EFI_STATUS                Status;\r
   EFI_CAPSULE_HEADER        *CapsuleHeader;\r
   BOOLEAN                   NeedReset;\r
+  BOOLEAN                   InitiateReset;\r
+  CHAR16                    CapsuleVarName[30];\r
+  CHAR16                    *TempVarName;  \r
   \r
   //\r
   // Capsule Count can't be less than one.\r
@@ -71,8 +106,11 @@ UpdateCapsule (
   if (CapsuleCount < 1) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  NeedReset     = FALSE;\r
-  CapsuleHeader = NULL;\r
+\r
+  NeedReset         = FALSE;\r
+  InitiateReset     = FALSE;\r
+  CapsuleHeader     = NULL;\r
+  CapsuleVarName[0] = 0;\r
 \r
   for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
     //\r
@@ -83,18 +121,36 @@ UpdateCapsule (
     if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
+    //\r
+    // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have\r
+    // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
+    //\r
+    if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
+    //\r
+    // Check FMP capsule flag \r
+    //\r
+    if (CompareGuid(&CapsuleHeader->CapsuleGuid, &gEfiFmpCapsuleGuid)\r
+     && (CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0 ) {\r
+       return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
     //\r
     // Check Capsule image without populate flag by firmware support capsule function  \r
     //\r
-    if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) && \r
-        (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {\r
-      return EFI_UNSUPPORTED;\r
+    if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
+      Status = SupportCapsuleImage (CapsuleHeader);\r
+      if (EFI_ERROR(Status)) {\r
+        return Status;\r
+      }\r
     }\r
   }\r
 \r
   //\r
-  // Walk through all capsules, record whether there is a capsule \r
-  // needs reset. And then process capsules which has no reset flag directly.\r
+  // Walk through all capsules, record whether there is a capsule needs reset\r
+  // or initiate reset. And then process capsules which has no reset flag directly.\r
   //\r
   for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {\r
     CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
@@ -104,7 +160,7 @@ UpdateCapsule (
     //\r
     if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {\r
       if (EfiAtRuntime ()) { \r
-        Status = EFI_UNSUPPORTED;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
       } else {\r
         Status = ProcessCapsuleImage(CapsuleHeader);\r
       }\r
@@ -113,6 +169,9 @@ UpdateCapsule (
       }\r
     } else {\r
       NeedReset = TRUE;\r
+      if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {\r
+        InitiateReset = TRUE;\r
+      }\r
     }\r
   }\r
   \r
@@ -139,19 +198,42 @@ UpdateCapsule (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  //\r
+  // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...\r
+  // if user calls UpdateCapsule multiple times.\r
+  //\r
+  StrCpy (CapsuleVarName, EFI_CAPSULE_VARIABLE_NAME);\r
+  TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r
+  if (mTimes > 0) {\r
+    UnicodeValueToString (TempVarName, 0, mTimes, 0);\r
+  }\r
+\r
   //\r
   // ScatterGatherList is only referenced if the capsules are defined to persist across\r
   // system reset. Set its value into NV storage to let pre-boot driver to pick it up \r
   // after coming through a system reset.\r
   //\r
-  Status = gRT->SetVariable (\r
-                 EFI_CAPSULE_VARIABLE_NAME,\r
-                 &gEfiCapsuleVendorGuid,\r
-                 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-                 sizeof (UINTN),\r
-                 (VOID *) &ScatterGatherList\r
-                 );\r
-\r
+  Status = EfiSetVariable (\r
+             CapsuleVarName,\r
+             &gEfiCapsuleVendorGuid,\r
+             EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+             sizeof (UINTN),\r
+             (VOID *) &ScatterGatherList\r
+             );\r
+  if (!EFI_ERROR (Status)) {\r
+     //\r
+     // Variable has been set successfully, increase variable index.\r
+     //\r
+     mTimes++;\r
+     if(InitiateReset) {\r
+       //\r
+       // Firmware that encounters a capsule which has the CAPSULE_FLAGS_INITIATE_RESET Flag set in its header\r
+       // will initiate a reset of the platform which is compatible with the passed-in capsule request and will \r
+       // not return back to the caller.\r
+       //\r
+       EfiResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
+     }\r
+  }\r
   return Status;\r
 }\r
 \r
@@ -183,8 +265,10 @@ QueryCapsuleCapabilities (
   OUT EFI_RESET_TYPE       *ResetType\r
   )\r
 {\r
+  EFI_STATUS                Status;\r
   UINTN                     ArrayNumber;\r
   EFI_CAPSULE_HEADER        *CapsuleHeader;\r
+  BOOLEAN                   NeedReset;\r
 \r
   //\r
   // Capsule Count can't be less than one.\r
@@ -194,13 +278,14 @@ QueryCapsuleCapabilities (
   }\r
   \r
   //\r
-  // Check whether input paramter is valid\r
+  // Check whether input parameter is valid\r
   //\r
   if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   CapsuleHeader = NULL;\r
+  NeedReset     = FALSE;\r
 \r
   for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
     CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
@@ -211,41 +296,59 @@ QueryCapsuleCapabilities (
     if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
+    //\r
+    // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have\r
+    // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
+    //\r
+    if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
+    //\r
+    // Check FMP capsule flag \r
+    //\r
+    if (CompareGuid(&CapsuleHeader->CapsuleGuid, &gEfiFmpCapsuleGuid)\r
+     && (CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0 ) {\r
+       return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
     //\r
     // Check Capsule image without populate flag is supported by firmware\r
     //\r
-    if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) && \r
-        (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {\r
-      return EFI_UNSUPPORTED;\r
+    if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
+      Status = SupportCapsuleImage (CapsuleHeader);\r
+      if (EFI_ERROR(Status)) {\r
+        return Status;\r
+      }\r
     }\r
   }\r
 \r
   //\r
-  // Assume that capsules have the same flags on reseting or not.\r
+  // Find out whether there is any capsule defined to persist across system reset. \r
   //\r
-  CapsuleHeader = CapsuleHeaderArray[0];\r
-  if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {\r
+  for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {\r
+    CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
+    if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {\r
+      NeedReset = TRUE;\r
+      break;\r
+    }\r
+  }\r
+\r
+  if (NeedReset) {\r
     //\r
     //Check if the platform supports update capsule across a system reset\r
     //\r
     if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {\r
       return EFI_UNSUPPORTED;\r
     }\r
-    *ResetType = EfiResetWarm;   \r
+    *ResetType = EfiResetWarm;\r
+    *MaxiumCapsuleSize = (UINT64) mMaxSizePopulateCapsule;\r
   } else {\r
     //\r
     // For non-reset capsule image.\r
     //\r
     *ResetType = EfiResetCold;\r
-  }\r
-  \r
-  //\r
-  // The support max capsule image size\r
-  //\r
-  if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {\r
-    *MaxiumCapsuleSize = PcdGet32(PcdMaxSizePopulateCapsule);\r
-  } else {\r
-    *MaxiumCapsuleSize = PcdGet32(PcdMaxSizeNonPopulateCapsule);\r
+    *MaxiumCapsuleSize = (UINT64) mMaxSizeNonPopulateCapsule;\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -270,8 +373,19 @@ CapsuleServiceInitialize (
   )\r
 {\r
   EFI_STATUS  Status;\r
-  EFI_HANDLE  NewHandle;\r
-  \r
+\r
+  mMaxSizePopulateCapsule = PcdGet32(PcdMaxSizePopulateCapsule);\r
+  mMaxSizeNonPopulateCapsule = PcdGet32(PcdMaxSizeNonPopulateCapsule);\r
+\r
+  //\r
+  // When PEI phase is IA32, DXE phase is X64, it is possible that capsule data are \r
+  // put above 4GB, so capsule PEI will transfer to long mode to get capsule data.\r
+  // The page table and stack is used to transfer processor mode from IA32 to long mode.\r
+  // Create the base address of page table and stack, and save them into variable.\r
+  // This is not needed when capsule with reset type is not supported.\r
+  //\r
+  SaveLongModeContext ();\r
+    \r
   //\r
   // Install capsule runtime services into UEFI runtime service tables.\r
   //\r
@@ -282,15 +396,13 @@ CapsuleServiceInitialize (
   // Install the Capsule Architectural Protocol on a new handle\r
   // to signify the capsule runtime services are ready.\r
   //\r
-  NewHandle = NULL;\r
-\r
   Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &NewHandle,\r
+                  &mNewHandle,\r
                   &gEfiCapsuleArchProtocolGuid,\r
                   NULL,\r
                   NULL\r
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r