--- /dev/null
+/** @file\r
+ Produce FMP instance for Microcode.\r
+\r
+ Copyright (c) 2016, 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
+\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
+**/\r
+\r
+#include "MicrocodeUpdate.h"\r
+\r
+//\r
+// MicrocodeFmp driver private data\r
+//\r
+MICROCODE_FMP_PRIVATE_DATA *mMicrocodeFmpPrivate = NULL;\r
+\r
+EFI_FIRMWARE_MANAGEMENT_PROTOCOL mFirmwareManagementProtocol = {\r
+ FmpGetImageInfo,\r
+ FmpGetImage,\r
+ FmpSetImage,\r
+ FmpCheckImage,\r
+ FmpGetPackageInfo,\r
+ FmpSetPackageInfo\r
+};\r
+\r
+/**\r
+ Initialize Microcode Descriptor.\r
+\r
+ @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
+\r
+ @return EFI_SUCCESS Microcode Descriptor is initialized.\r
+**/\r
+EFI_STATUS\r
+InitializeMicrocodeDescriptor (\r
+ IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
+ );\r
+\r
+/**\r
+ Returns information about the current firmware image(s) of the device.\r
+\r
+ 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, out] ImageInfoSize A pointer to the size, in bytes, of the ImageInfo buffer.\r
+ On input, this is the size of the buffer allocated by the caller.\r
+ On output, it is the size of the buffer returned by the firmware\r
+ if the buffer was large enough, or the size of the buffer needed\r
+ to contain the image(s) information if the buffer was too small.\r
+ @param[in, out] ImageInfo A pointer to the buffer in which firmware places the current image(s)\r
+ information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.\r
+ @param[out] DescriptorVersion A pointer to the location in which firmware returns the version number\r
+ associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
+ @param[out] DescriptorCount A pointer to the location in which firmware returns the number of\r
+ descriptors or firmware images within this device.\r
+ @param[out] DescriptorSize A pointer to the location in which firmware returns the size, in bytes,\r
+ of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
+ @param[out] PackageVersion A version number that represents all the firmware images in the device.\r
+ The format is vendor specific and new version must have a greater value\r
+ than the old version. If PackageVersion is not supported, the value is\r
+ 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison\r
+ is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates\r
+ that package version update is in progress.\r
+ @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing the\r
+ package version name. The buffer is allocated by this function with\r
+ AllocatePool(), and it is the caller's responsibility to free it with a call\r
+ to FreePool().\r
+\r
+ @retval EFI_SUCCESS The device was successfully updated with the new image.\r
+ @retval EFI_BUFFER_TOO_SMALL The ImageInfo buffer was too small. The current buffer size\r
+ needed to hold the image(s) information is returned in ImageInfoSize.\r
+ @retval EFI_INVALID_PARAMETER ImageInfoSize is NULL.\r
+ @retval EFI_DEVICE_ERROR Valid information could not be returned. Possible corrupted image.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpGetImageInfo (\r
+ IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
+ IN OUT UINTN *ImageInfoSize,\r
+ IN OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageInfo,\r
+ OUT UINT32 *DescriptorVersion,\r
+ OUT UINT8 *DescriptorCount,\r
+ OUT UINTN *DescriptorSize,\r
+ OUT UINT32 *PackageVersion,\r
+ OUT CHAR16 **PackageVersionName\r
+ )\r
+{\r
+ MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate;\r
+ UINTN Index;\r
+\r
+ MicrocodeFmpPrivate = MICROCODE_FMP_PRIVATE_DATA_FROM_FMP(This);\r
+\r
+ if(ImageInfoSize == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (*ImageInfoSize < sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * MicrocodeFmpPrivate->DescriptorCount) {\r
+ *ImageInfoSize = sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * MicrocodeFmpPrivate->DescriptorCount;\r
+ return EFI_BUFFER_TOO_SMALL;\r
+ }\r
+\r
+ if (ImageInfo == NULL ||\r
+ DescriptorVersion == NULL ||\r
+ DescriptorCount == NULL ||\r
+ DescriptorSize == NULL ||\r
+ PackageVersion == NULL ||\r
+ PackageVersionName == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ *ImageInfoSize = sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * MicrocodeFmpPrivate->DescriptorCount;\r
+ *DescriptorSize = sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR);\r
+ *DescriptorCount = MicrocodeFmpPrivate->DescriptorCount;\r
+ *DescriptorVersion = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;\r
+\r
+ //\r
+ // supports 1 ImageInfo descriptor\r
+ //\r
+ CopyMem(&ImageInfo[0], MicrocodeFmpPrivate->ImageDescriptor, sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * MicrocodeFmpPrivate->DescriptorCount);\r
+ for (Index = 0; Index < MicrocodeFmpPrivate->DescriptorCount; Index++) {\r
+ if ((ImageInfo[Index].AttributesSetting & IMAGE_ATTRIBUTE_IN_USE) != 0) {\r
+ ImageInfo[Index].LastAttemptVersion = MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion;\r
+ ImageInfo[Index].LastAttemptStatus = MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus;\r
+ }\r
+ }\r
+\r
+ //\r
+ // package version\r
+ //\r
+ *PackageVersion = MicrocodeFmpPrivate->PackageVersion;\r
+ if (MicrocodeFmpPrivate->PackageVersionName != NULL) {\r
+ *PackageVersionName = AllocateCopyPool(StrSize(MicrocodeFmpPrivate->PackageVersionName), MicrocodeFmpPrivate->PackageVersionName);\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Retrieves a copy of the current firmware image of the device.\r
+\r
+ 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
+ The number is between 1 and DescriptorCount.\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
+ @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the\r
+ image. The current buffer size needed to hold the image is returned\r
+ in ImageSize.\r
+ @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
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpGetImage (\r
+ IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
+ IN UINT8 ImageIndex,\r
+ IN OUT VOID *Image,\r
+ IN OUT UINTN *ImageSize\r
+ )\r
+{\r
+ MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate;\r
+ MICROCODE_INFO *MicrocodeInfo;\r
+\r
+ if (Image == NULL || ImageSize == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ MicrocodeFmpPrivate = MICROCODE_FMP_PRIVATE_DATA_FROM_FMP(This);\r
+\r
+ if (ImageIndex == 0 || ImageIndex > MicrocodeFmpPrivate->DescriptorCount || ImageSize == NULL || Image == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ MicrocodeInfo = &MicrocodeFmpPrivate->MicrocodeInfo[ImageIndex - 1];\r
+\r
+ if (*ImageSize < MicrocodeInfo->TotalSize) {\r
+ *ImageSize = MicrocodeInfo->TotalSize;\r
+ return EFI_BUFFER_TOO_SMALL;\r
+ }\r
+\r
+ *ImageSize = MicrocodeInfo->TotalSize;\r
+ CopyMem (Image, MicrocodeInfo->MicrocodeEntryPoint, MicrocodeInfo->TotalSize);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Updates the firmware image of the device.\r
+\r
+ This function updates the hardware with the new firmware image.\r
+ This function returns EFI_UNSUPPORTED if the firmware image is not updatable.\r
+ If the firmware image is updatable, the function should perform the following minimal validations\r
+ before proceeding to do the firmware image update.\r
+ - Validate the image authentication if image has attribute\r
+ IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns\r
+ EFI_SECURITY_VIOLATION if the validation fails.\r
+ - Validate the image is a supported image for this device. The function returns EFI_ABORTED if\r
+ the image is unsupported. The function can optionally provide more detailed information on\r
+ why the image is not a supported image.\r
+ - Validate the data from VendorCode if not null. Image validation must be performed before\r
+ VendorCode data validation. VendorCode data is ignored or considered invalid if image\r
+ validation failed. The function returns EFI_ABORTED if the data is invalid.\r
+\r
+ VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if\r
+ the caller did not specify the policy or use the default policy. As an example, vendor can implement\r
+ a policy to allow an option to force a firmware image update when the abort reason is due to the new\r
+ firmware image version is older than the current firmware image version or bad image checksum.\r
+ Sensitive operations such as those wiping the entire firmware image and render the device to be\r
+ non-functional should be encoded in the image itself rather than passed with the VendorCode.\r
+ AbortReason enables vendor to have the option to provide a more detailed description of the abort\r
+ reason to the caller.\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
+ The number is between 1 and DescriptorCount.\r
+ @param[in] Image Points to the new image.\r
+ @param[in] ImageSize Size of the new image in bytes.\r
+ @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.\r
+ Null indicates the caller did not specify the policy or use the default policy.\r
+ @param[in] Progress A function used by the driver to report the progress of the firmware update.\r
+ @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more\r
+ details for the aborted operation. The buffer is allocated by this function\r
+ with AllocatePool(), and it is the caller's responsibility to free it with a\r
+ call to FreePool().\r
+\r
+ @retval EFI_SUCCESS The device was successfully updated with the new image.\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
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpSetImage (\r
+ IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
+ IN UINT8 ImageIndex,\r
+ IN CONST VOID *Image,\r
+ IN UINTN ImageSize,\r
+ IN CONST VOID *VendorCode,\r
+ IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,\r
+ OUT CHAR16 **AbortReason\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_STATUS VarStatus;\r
+ MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate;\r
+\r
+ if (Image == NULL || AbortReason == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ MicrocodeFmpPrivate = MICROCODE_FMP_PRIVATE_DATA_FROM_FMP(This);\r
+ *AbortReason = NULL;\r
+\r
+ if (ImageIndex == 0 || ImageIndex > MicrocodeFmpPrivate->DescriptorCount || Image == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Status = MicrocodeWrite(MicrocodeFmpPrivate, (VOID *)Image, ImageSize, &MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion, &MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus, AbortReason);\r
+ DEBUG((DEBUG_INFO, "SetImage - LastAttemp Version - 0x%x, State - 0x%x\n", MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion, MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus));\r
+ VarStatus = gRT->SetVariable(\r
+ MICROCODE_FMP_LAST_ATTEMPT_VARIABLE_NAME,\r
+ &gEfiCallerIdGuid,\r
+ EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+ sizeof(MicrocodeFmpPrivate->LastAttempt),\r
+ &MicrocodeFmpPrivate->LastAttempt\r
+ );\r
+ DEBUG((DEBUG_INFO, "SetLastAttemp - %r\n", VarStatus));\r
+\r
+ if (!EFI_ERROR(Status)) {\r
+ InitializeMicrocodeDescriptor(MicrocodeFmpPrivate);\r
+ DumpPrivateInfo (MicrocodeFmpPrivate);\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Checks if the firmware image is valid for the device.\r
+\r
+ This function allows firmware update application to validate the firmware image without\r
+ invoking the SetImage() first.\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
+ The number is between 1 and DescriptorCount.\r
+ @param[in] Image Points to the new image.\r
+ @param[in] ImageSize Size of the new image in bytes.\r
+ @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,\r
+ if available, additional information if the image is invalid.\r
+\r
+ @retval EFI_SUCCESS The image was successfully checked.\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
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpCheckImage (\r
+ IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
+ IN UINT8 ImageIndex,\r
+ IN CONST VOID *Image,\r
+ IN UINTN ImageSize,\r
+ OUT UINT32 *ImageUpdatable\r
+ )\r
+{\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+ Returns information about the firmware package.\r
+\r
+ This function returns package information.\r
+\r
+ @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
+ @param[out] PackageVersion A version number that represents all the firmware images in the device.\r
+ The format is vendor specific and new version must have a greater value\r
+ than the old version. If PackageVersion is not supported, the value is\r
+ 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version\r
+ comparison is to be performed using PackageVersionName. A value of\r
+ 0xFFFFFFFD indicates that package version update is in progress.\r
+ @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing\r
+ the package version name. The buffer is allocated by this function with\r
+ AllocatePool(), and it is the caller's responsibility to free it with a\r
+ call to FreePool().\r
+ @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of\r
+ package version name. A value of 0 indicates the device does not support\r
+ update of package version name. Length is the number of Unicode characters,\r
+ including the terminating null character.\r
+ @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute\r
+ Definitions' for possible returned values of this parameter. A value of 1\r
+ indicates the attribute is supported and the current setting value is\r
+ indicated in AttributesSetting. A value of 0 indicates the attribute is not\r
+ supported and the current setting value in AttributesSetting is meaningless.\r
+ @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned\r
+ values of this parameter\r
+\r
+ @retval EFI_SUCCESS The package information was successfully returned.\r
+ @retval EFI_UNSUPPORTED The operation is not supported.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpGetPackageInfo (\r
+ IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
+ OUT UINT32 *PackageVersion,\r
+ OUT CHAR16 **PackageVersionName,\r
+ OUT UINT32 *PackageVersionNameMaxLen,\r
+ OUT UINT64 *AttributesSupported,\r
+ OUT UINT64 *AttributesSetting\r
+ )\r
+{\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+ Updates information about the firmware package.\r
+\r
+ This function updates package information.\r
+ This function returns EFI_UNSUPPORTED if the package information is not updatable.\r
+ VendorCode enables vendor to implement vendor-specific package information update policy.\r
+ Null if the caller did not specify this policy or use the default policy.\r
+\r
+ @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
+ @param[in] Image Points to the authentication image.\r
+ Null if authentication is not required.\r
+ @param[in] ImageSize Size of the authentication image in bytes.\r
+ 0 if authentication is not required.\r
+ @param[in] VendorCode This enables vendor to implement vendor-specific firmware\r
+ image update policy.\r
+ Null indicates the caller did not specify this policy or use\r
+ the default policy.\r
+ @param[in] PackageVersion The new package version.\r
+ @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing\r
+ the package version name.\r
+ The string length is equal to or less than the value returned in\r
+ PackageVersionNameMaxLen.\r
+\r
+ @retval EFI_SUCCESS The device was successfully updated with the new package\r
+ information.\r
+ @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
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpSetPackageInfo (\r
+ IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
+ IN CONST VOID *Image,\r
+ IN UINTN ImageSize,\r
+ IN CONST VOID *VendorCode,\r
+ IN UINT32 PackageVersion,\r
+ IN CONST CHAR16 *PackageVersionName\r
+ )\r
+{\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+ Initialize Processor Microcode Index.\r
+\r
+ @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
+**/\r
+VOID\r
+InitializedProcessorMicrocodeIndex (\r
+ IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
+ )\r
+{\r
+ UINTN CpuIndex;\r
+ UINTN MicrocodeIndex;\r
+ UINTN TargetCpuIndex;\r
+ UINT32 AttemptStatus;\r
+ EFI_STATUS Status;\r
+\r
+ for (CpuIndex = 0; CpuIndex < MicrocodeFmpPrivate->ProcessorCount; CpuIndex++) {\r
+ if (MicrocodeFmpPrivate->ProcessorInfo[CpuIndex].MicrocodeIndex != (UINTN)-1) {\r
+ continue;\r
+ }\r
+ for (MicrocodeIndex = 0; MicrocodeIndex < MicrocodeFmpPrivate->DescriptorCount; MicrocodeIndex++) {\r
+ if (!MicrocodeFmpPrivate->MicrocodeInfo[MicrocodeIndex].InUse) {\r
+ continue;\r
+ }\r
+ TargetCpuIndex = CpuIndex;\r
+ Status = VerifyMicrocode(\r
+ MicrocodeFmpPrivate,\r
+ MicrocodeFmpPrivate->MicrocodeInfo[MicrocodeIndex].MicrocodeEntryPoint,\r
+ MicrocodeFmpPrivate->MicrocodeInfo[MicrocodeIndex].TotalSize,\r
+ FALSE,\r
+ &AttemptStatus,\r
+ NULL,\r
+ &TargetCpuIndex\r
+ );\r
+ if (!EFI_ERROR(Status)) {\r
+ MicrocodeFmpPrivate->ProcessorInfo[CpuIndex].MicrocodeIndex = MicrocodeIndex;\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+/**\r
+ Initialize Microcode Descriptor.\r
+\r
+ @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
+\r
+ @return EFI_SUCCESS Microcode Descriptor is initialized.\r
+**/\r
+EFI_STATUS\r
+InitializeMicrocodeDescriptor (\r
+ IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
+ )\r
+{\r
+ UINT8 CurrentMicrocodeCount;\r
+\r
+ CurrentMicrocodeCount = (UINT8)GetMicrocodeInfo (MicrocodeFmpPrivate, 0, NULL, NULL);\r
+\r
+ if (CurrentMicrocodeCount > MicrocodeFmpPrivate->DescriptorCount) {\r
+ if (MicrocodeFmpPrivate->ImageDescriptor != NULL) {\r
+ FreePool(MicrocodeFmpPrivate->ImageDescriptor);\r
+ MicrocodeFmpPrivate->ImageDescriptor = NULL;\r
+ }\r
+ if (MicrocodeFmpPrivate->MicrocodeInfo != NULL) {\r
+ FreePool(MicrocodeFmpPrivate->MicrocodeInfo);\r
+ MicrocodeFmpPrivate->MicrocodeInfo = NULL;\r
+ }\r
+ } else {\r
+ ZeroMem(MicrocodeFmpPrivate->ImageDescriptor, MicrocodeFmpPrivate->DescriptorCount * sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR));\r
+ ZeroMem(MicrocodeFmpPrivate->MicrocodeInfo, MicrocodeFmpPrivate->DescriptorCount * sizeof(MICROCODE_INFO));\r
+ }\r
+\r
+ MicrocodeFmpPrivate->DescriptorCount = CurrentMicrocodeCount;\r
+\r
+ if (MicrocodeFmpPrivate->ImageDescriptor == NULL) {\r
+ MicrocodeFmpPrivate->ImageDescriptor = AllocateZeroPool(MicrocodeFmpPrivate->DescriptorCount * sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR));\r
+ if (MicrocodeFmpPrivate->ImageDescriptor == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ }\r
+ if (MicrocodeFmpPrivate->MicrocodeInfo == NULL) {\r
+ MicrocodeFmpPrivate->MicrocodeInfo = AllocateZeroPool(MicrocodeFmpPrivate->DescriptorCount * sizeof(MICROCODE_INFO));\r
+ if (MicrocodeFmpPrivate->MicrocodeInfo == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ }\r
+\r
+ CurrentMicrocodeCount = (UINT8)GetMicrocodeInfo (MicrocodeFmpPrivate, MicrocodeFmpPrivate->DescriptorCount, MicrocodeFmpPrivate->ImageDescriptor, MicrocodeFmpPrivate->MicrocodeInfo);\r
+ ASSERT(CurrentMicrocodeCount == MicrocodeFmpPrivate->DescriptorCount);\r
+\r
+ InitializedProcessorMicrocodeIndex (MicrocodeFmpPrivate);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Initialize MicrocodeFmpDriver multiprocessor information.\r
+\r
+ @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
+\r
+ @return EFI_SUCCESS private data is initialized.\r
+**/\r
+EFI_STATUS\r
+InitializeProcessorInfo (\r
+ IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_MP_SERVICES_PROTOCOL *MpService;\r
+ UINTN NumberOfProcessors;\r
+ UINTN NumberOfEnabledProcessors;\r
+ UINTN Index;\r
+ UINTN BspIndex;\r
+\r
+ Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&MpService);\r
+ ASSERT_EFI_ERROR(Status);\r
+\r
+ MicrocodeFmpPrivate->MpService = MpService;\r
+ MicrocodeFmpPrivate->ProcessorCount = 0;\r
+ MicrocodeFmpPrivate->ProcessorInfo = NULL;\r
+\r
+ Status = MpService->GetNumberOfProcessors (MpService, &NumberOfProcessors, &NumberOfEnabledProcessors);\r
+ ASSERT_EFI_ERROR(Status);\r
+ MicrocodeFmpPrivate->ProcessorCount = NumberOfProcessors;\r
+\r
+ Status = MpService->WhoAmI (MpService, &BspIndex);\r
+ ASSERT_EFI_ERROR(Status);\r
+ MicrocodeFmpPrivate->BspIndex = BspIndex;\r
+\r
+ MicrocodeFmpPrivate->ProcessorInfo = AllocateZeroPool (sizeof(PROCESSOR_INFO) * MicrocodeFmpPrivate->ProcessorCount);\r
+ if (MicrocodeFmpPrivate->ProcessorInfo == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ for (Index = 0; Index < NumberOfProcessors; Index++) {\r
+ MicrocodeFmpPrivate->ProcessorInfo[Index].CpuIndex = Index;\r
+ MicrocodeFmpPrivate->ProcessorInfo[Index].MicrocodeIndex = (UINTN)-1;\r
+ if (Index == BspIndex) {\r
+ CollectProcessorInfo (&MicrocodeFmpPrivate->ProcessorInfo[Index]);\r
+ } else {\r
+ Status = MpService->StartupThisAP (\r
+ MpService,\r
+ CollectProcessorInfo,\r
+ Index,\r
+ NULL,\r
+ 0,\r
+ &MicrocodeFmpPrivate->ProcessorInfo[Index],\r
+ NULL\r
+ );\r
+ ASSERT_EFI_ERROR(Status);\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Dump private information.\r
+\r
+ @param[in] MicrocodeFmpPrivate private data structure.\r
+**/\r
+VOID\r
+DumpPrivateInfo (\r
+ IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
+ )\r
+{\r
+ UINTN Index;\r
+ PROCESSOR_INFO *ProcessorInfo;\r
+ MICROCODE_INFO *MicrocodeInfo;\r
+ EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageDescriptor;\r
+\r
+ DEBUG ((DEBUG_INFO, "ProcessorInfo:\n"));\r
+ DEBUG ((DEBUG_INFO, " ProcessorCount - 0x%x\n", MicrocodeFmpPrivate->ProcessorCount));\r
+ DEBUG ((DEBUG_INFO, " BspIndex - 0x%x\n", MicrocodeFmpPrivate->BspIndex));\r
+\r
+ ProcessorInfo = MicrocodeFmpPrivate->ProcessorInfo;\r
+ for (Index = 0; Index < MicrocodeFmpPrivate->ProcessorCount; Index++) {\r
+ DEBUG ((\r
+ DEBUG_INFO,\r
+ " ProcessorInfo[0x%x] - 0x%08x, 0x%02x, 0x%08x, (0x%x)\n",\r
+ ProcessorInfo[Index].CpuIndex,\r
+ ProcessorInfo[Index].ProcessorSignature,\r
+ ProcessorInfo[Index].PlatformId,\r
+ ProcessorInfo[Index].MicrocodeRevision,\r
+ ProcessorInfo[Index].MicrocodeIndex\r
+ ));\r
+ }\r
+\r
+ DEBUG ((DEBUG_INFO, "MicrocodeInfo:\n"));\r
+ MicrocodeInfo = MicrocodeFmpPrivate->MicrocodeInfo;\r
+ DEBUG ((DEBUG_INFO, " MicrocodeRegion - 0x%x - 0x%x\n", MicrocodeFmpPrivate->MicrocodePatchAddress, MicrocodeFmpPrivate->MicrocodePatchRegionSize));\r
+ DEBUG ((DEBUG_INFO, " MicrocodeCount - 0x%x\n", MicrocodeFmpPrivate->DescriptorCount));\r
+ for (Index = 0; Index < MicrocodeFmpPrivate->DescriptorCount; Index++) {\r
+ DEBUG ((\r
+ DEBUG_INFO,\r
+ " MicrocodeInfo[0x%x] - 0x%08x, 0x%08x, (0x%x)\n",\r
+ Index,\r
+ MicrocodeInfo[Index].MicrocodeEntryPoint,\r
+ MicrocodeInfo[Index].TotalSize,\r
+ MicrocodeInfo[Index].InUse\r
+ ));\r
+ }\r
+\r
+ ImageDescriptor = MicrocodeFmpPrivate->ImageDescriptor;\r
+ DEBUG ((DEBUG_VERBOSE, "ImageDescriptor:\n"));\r
+ for (Index = 0; Index < MicrocodeFmpPrivate->DescriptorCount; Index++) {\r
+ DEBUG((DEBUG_VERBOSE, " ImageDescriptor (%d)\n", Index));\r
+ DEBUG((DEBUG_VERBOSE, " ImageIndex - 0x%x\n", ImageDescriptor[Index].ImageIndex));\r
+ DEBUG((DEBUG_VERBOSE, " ImageTypeId - %g\n", &ImageDescriptor[Index].ImageTypeId));\r
+ DEBUG((DEBUG_VERBOSE, " ImageId - 0x%lx\n", ImageDescriptor[Index].ImageId));\r
+ DEBUG((DEBUG_VERBOSE, " ImageIdName - %s\n", ImageDescriptor[Index].ImageIdName));\r
+ DEBUG((DEBUG_VERBOSE, " Version - 0x%x\n", ImageDescriptor[Index].Version));\r
+ DEBUG((DEBUG_VERBOSE, " VersionName - %s\n", ImageDescriptor[Index].VersionName));\r
+ DEBUG((DEBUG_VERBOSE, " Size - 0x%x\n", ImageDescriptor[Index].Size));\r
+ DEBUG((DEBUG_VERBOSE, " AttributesSupported - 0x%lx\n", ImageDescriptor[Index].AttributesSupported));\r
+ DEBUG((DEBUG_VERBOSE, " AttributesSetting - 0x%lx\n", ImageDescriptor[Index].AttributesSetting));\r
+ DEBUG((DEBUG_VERBOSE, " Compatibilities - 0x%lx\n", ImageDescriptor[Index].Compatibilities));\r
+ DEBUG((DEBUG_VERBOSE, " LowestSupportedImageVersion - 0x%x\n", ImageDescriptor[Index].LowestSupportedImageVersion));\r
+ DEBUG((DEBUG_VERBOSE, " LastAttemptVersion - 0x%x\n", ImageDescriptor[Index].LastAttemptVersion));\r
+ DEBUG((DEBUG_VERBOSE, " LastAttemptStatus - 0x%x\n", ImageDescriptor[Index].LastAttemptStatus));\r
+ DEBUG((DEBUG_VERBOSE, " HardwareInstance - 0x%lx\n", ImageDescriptor[Index].HardwareInstance));\r
+ }\r
+}\r
+\r
+/**\r
+ Initialize MicrocodeFmpDriver private data structure.\r
+\r
+ @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
+\r
+ @return EFI_SUCCESS private data is initialized.\r
+**/\r
+EFI_STATUS\r
+InitializePrivateData (\r
+ IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_STATUS VarStatus;\r
+ UINTN VarSize;\r
+ BOOLEAN Result;\r
+\r
+ MicrocodeFmpPrivate->Signature = MICROCODE_FMP_PRIVATE_DATA_SIGNATURE;\r
+ MicrocodeFmpPrivate->Handle = NULL;\r
+ CopyMem(&MicrocodeFmpPrivate->Fmp, &mFirmwareManagementProtocol, sizeof(EFI_FIRMWARE_MANAGEMENT_PROTOCOL));\r
+\r
+ MicrocodeFmpPrivate->PackageVersion = 0x1;\r
+ MicrocodeFmpPrivate->PackageVersionName = L"Microcode";\r
+\r
+ MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion = 0x0;\r
+ MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus = 0x0;\r
+ VarSize = sizeof(MicrocodeFmpPrivate->LastAttempt);\r
+ VarStatus = gRT->GetVariable(\r
+ MICROCODE_FMP_LAST_ATTEMPT_VARIABLE_NAME,\r
+ &gEfiCallerIdGuid,\r
+ NULL,\r
+ &VarSize,\r
+ &MicrocodeFmpPrivate->LastAttempt\r
+ );\r
+ DEBUG((DEBUG_INFO, "GetLastAttemp - %r\n", VarStatus));\r
+ DEBUG((DEBUG_INFO, "GetLastAttemp Version - 0x%x, State - 0x%x\n", MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion, MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus));\r
+\r
+ Result = GetMicrocodeRegion(&MicrocodeFmpPrivate->MicrocodePatchAddress, &MicrocodeFmpPrivate->MicrocodePatchRegionSize);\r
+ if (!Result) {\r
+ DEBUG((DEBUG_ERROR, "Fail to get Microcode Region\n"));\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ Status = InitializeProcessorInfo (MicrocodeFmpPrivate);\r
+ if (EFI_ERROR(Status)) {\r
+ DEBUG((DEBUG_ERROR, "InitializeProcessorInfo - %r\n", Status));\r
+ return Status;\r
+ }\r
+\r
+ Status = InitializeMicrocodeDescriptor(MicrocodeFmpPrivate);\r
+ if (EFI_ERROR(Status)) {\r
+ DEBUG((DEBUG_ERROR, "InitializeMicrocodeDescriptor - %r\n", Status));\r
+ return Status;\r
+ }\r
+\r
+ DumpPrivateInfo (MicrocodeFmpPrivate);\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Microcode FMP module entrypoint\r
+\r
+ @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
+ @param[in] SystemTable A pointer to the EFI System Table.\r
+\r
+ @return EFI_SUCCESS Microcode FMP module is initialized.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MicrocodeFmpMain (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Initialize MicrocodeFmpPrivateData\r
+ //\r
+ mMicrocodeFmpPrivate = AllocateZeroPool (sizeof(MICROCODE_FMP_PRIVATE_DATA));\r
+ if (mMicrocodeFmpPrivate == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ Status = InitializePrivateData(mMicrocodeFmpPrivate);\r
+ if (EFI_ERROR(Status)) {\r
+ FreePool(mMicrocodeFmpPrivate);\r
+ mMicrocodeFmpPrivate = NULL;\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Install FMP protocol.\r
+ //\r
+ Status = gBS->InstallProtocolInterface (\r
+ &mMicrocodeFmpPrivate->Handle,\r
+ &gEfiFirmwareManagementProtocolGuid,\r
+ EFI_NATIVE_INTERFACE,\r
+ &mMicrocodeFmpPrivate->Fmp\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ FreePool(mMicrocodeFmpPrivate);\r
+ mMicrocodeFmpPrivate = NULL;\r
+ return Status;\r
+ }\r
+\r
+ return Status;\r
+}\r