+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation \r
+All rights reserved. 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
+ \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+Module Name:\r
+\r
+ CapsuleService.c\r
+\r
+Abstract:\r
+\r
+ Capsule Runtime Service.\r
+\r
+--*/\r
+\r
+#include "CapsuleService.h"\r
+\r
+\r
+STATIC EFI_GUID mEfiCapsuleHeaderGuid = EFI_CAPSULE_GUID;\r
+\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+UpdateCapsule (\r
+ IN UEFI_CAPSULE_HEADER **CapsuleHeaderArray,\r
+ IN UINTN CapsuleCount,\r
+ IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ This code finds whether the capsules need reset to update, if not, update immediately.\r
+\r
+Arguments:\r
+\r
+ CapsuleHeaderArray A array of pointers to capsule headers passed in\r
+ CapsuleCount The number of capsule\r
+ ScatterGatherList Physical address of datablock list points to capsule\r
+ \r
+Returns:\r
+\r
+ EFI STATUS\r
+ EFI_SUCCESS Valid capsule was passed.If CAPSULE_FLAG_PERSIT_ACROSS_RESET is\r
+ not set, the capsule has been successfully processed by the firmware.\r
+ If it set, the ScattlerGatherList is successfully to be set.\r
+ EFI_INVALID_PARAMETER CapsuleCount is less than 1,CapsuleGuid is not supported.\r
+ EFI_DEVICE_ERROR Failed to SetVariable or AllocatePool or ProcessFirmwareVolume. \r
+ \r
+--*/\r
+{\r
+ UINTN CapsuleSize;\r
+ UINTN ArrayNumber;\r
+ VOID *BufferPtr;\r
+ EFI_STATUS Status;\r
+ EFI_HANDLE FvHandle;\r
+ UEFI_CAPSULE_HEADER *CapsuleHeader;\r
+\r
+ if (CapsuleCount < 1) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ BufferPtr = NULL;\r
+ CapsuleHeader = NULL;\r
+\r
+ //\r
+ //Compare GUIDs with EFI_CAPSULE_GUID, if capsule header contains CAPSULE_FLAGS_PERSIST_ACROSS_RESET\r
+ //and CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flags,whatever the GUID is ,the service supports.\r
+ //\r
+ for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
+ CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
+ if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) && !(CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET)) {\r
+ return EFI_INVALID_PARAMETER; \r
+ }\r
+ if (!CompareGuid (&CapsuleHeader->CapsuleGuid, &mEfiCapsuleHeaderGuid)) {\r
+ if (!(CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) {\r
+ return EFI_UNSUPPORTED;\r
+ } \r
+ } \r
+ }\r
+\r
+ //\r
+ //Assume that capsules have the same flags on reseting or not. \r
+ //\r
+ CapsuleHeader = CapsuleHeaderArray[0];\r
+\r
+ if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {\r
+ //\r
+ //Check if the platform supports update capsule across a system reset\r
+ //\r
+ if (!FeaturePcdGet(PcdSupportUpdateCapsuleRest)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ \r
+ if (ScatterGatherList == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ } else {\r
+ Status = EfiSetVariable (\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
+ if (Status != EFI_SUCCESS) { \r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+ }\r
+ return EFI_SUCCESS;\r
+ }\r
+ \r
+ //\r
+ //The rest occurs in the condition of non-reset mode\r
+ //\r
+ if (EfiAtRuntime ()) { \r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ //Here should be in the boot-time\r
+ //\r
+ for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {\r
+ CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
+ CapsuleSize = CapsuleHeader->CapsuleImageSize - CapsuleHeader->HeaderSize;\r
+ Status = gBS->AllocatePool (EfiBootServicesData, CapsuleSize, &BufferPtr);\r
+ if (Status != EFI_SUCCESS) {\r
+ goto Done;\r
+ }\r
+ gBS->CopyMem (BufferPtr, (UINT8*)CapsuleHeader+ CapsuleHeader->HeaderSize, CapsuleSize);\r
+\r
+ //\r
+ //Call DXE service ProcessFirmwareVolume to process immediatelly \r
+ //\r
+ Status = gDS->ProcessFirmwareVolume (BufferPtr, CapsuleSize, &FvHandle);\r
+ if (Status != EFI_SUCCESS) {\r
+ gBS->FreePool (BufferPtr);\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+ gDS->Dispatch ();\r
+ gBS->FreePool (BufferPtr);\r
+ }\r
+ return EFI_SUCCESS;\r
+\r
+Done:\r
+ if (BufferPtr != NULL) {\r
+ gBS->FreePool (BufferPtr);\r
+ } \r
+ return EFI_DEVICE_ERROR;\r
+}\r
+\r
+\r
+ \r
+EFI_STATUS\r
+EFIAPI\r
+QueryCapsuleCapabilities (\r
+ IN UEFI_CAPSULE_HEADER **CapsuleHeaderArray,\r
+ IN UINTN CapsuleCount,\r
+ OUT UINT64 *MaxiumCapsuleSize,\r
+ OUT EFI_RESET_TYPE *ResetType\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ This code is to query about capsule capability.\r
+\r
+Arguments:\r
+\r
+ CapsuleHeaderArray A array of pointers to capsule headers passed in\r
+ CapsuleCount The number of capsule\r
+ MaxiumCapsuleSize Max capsule size is supported\r
+ ResetType Reset type the capsule indicates, if reset is not needed,return EfiResetCold.\r
+ If reset is needed, return EfiResetWarm.\r
+\r
+Returns:\r
+\r
+ EFI STATUS\r
+ EFI_SUCCESS Valid answer returned\r
+ EFI_INVALID_PARAMETER MaxiumCapsuleSize is NULL,ResetType is NULL.CapsuleCount is less than 1,CapsuleGuid is not supported.\r
+ EFI_UNSUPPORTED The capsule type is not supported.\r
+\r
+--*/\r
+{\r
+ UINTN ArrayNumber;\r
+ UEFI_CAPSULE_HEADER *CapsuleHeader;\r
+\r
+ if (CapsuleCount < 1) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ } \r
+\r
+ CapsuleHeader = NULL;\r
+ \r
+ //\r
+ //Compare GUIDs with EFI_CAPSULE_GUID, if capsule header contains CAPSULE_FLAGS_PERSIST_ACROSS_RESET\r
+ //and CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flags,whatever the GUID is ,the service supports.\r
+ //\r
+ for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
+ CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
+ if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) && !(CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET)) {\r
+ return EFI_INVALID_PARAMETER; \r
+ }\r
+ if (!CompareGuid (&CapsuleHeader->CapsuleGuid, &mEfiCapsuleHeaderGuid)) {\r
+ if (!(CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ } \r
+ }\r
+\r
+ //\r
+ //Assume that capsules have the same flags on reseting or not. \r
+ //\r
+ CapsuleHeader = CapsuleHeaderArray[0]; \r
+ if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {\r
+ //\r
+ //Check if the platform supports update capsule across a system reset\r
+ //\r
+ if (!FeaturePcdGet(PcdSupportUpdateCapsuleRest)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ *ResetType = EfiResetWarm;\r
+ *MaxiumCapsuleSize = FixedPcdGet32(PcdMaxSizePopulateCapsule); \r
+ } else {\r
+ *ResetType = EfiResetCold;\r
+ *MaxiumCapsuleSize = FixedPcdGet32(PcdMaxSizeNonPopulateCapsule);\r
+ } \r
+ return EFI_SUCCESS;\r
+} \r
+\r