/** Dump FMP image data. @param[in] ImageTypeId The ImageTypeId of the FMP image. It is used to identify the FMP protocol. @param[in] ImageIndex The ImageIndex of the FMP image. It is the input parameter for FMP->GetImage(). @param[in] ImageName The file name to hold the output FMP image. **/ VOID DumpFmpImage ( IN EFI_GUID *ImageTypeId, IN UINTN ImageIndex, IN CHAR16 *ImageName ) { EFI_STATUS Status; EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp; VOID *Image; UINTN ImageSize; Fmp = FindFmpFromImageTypeId (ImageTypeId); if (Fmp == NULL) { Print(L"No FMP include ImageTypeId %g\n", ImageTypeId); return ; } if (ImageIndex > 0xFF) { Print(L"ImageIndex 0x%x too big\n", ImageIndex); return ; } Image = Fmp; ImageSize = 0; Status = Fmp->GetImage (Fmp, (UINT8)ImageIndex, Image, &ImageSize); if (Status != EFI_BUFFER_TOO_SMALL) { Print(L"Fmp->GetImage - %r\n", Status); return ; } Image = AllocatePool (ImageSize); if (Image == NULL) { Print(L"Allocate FmpImage 0x%x - %r\n", ImageSize, EFI_OUT_OF_RESOURCES); return ; } Status = Fmp->GetImage (Fmp, (UINT8)ImageIndex, Image, &ImageSize); if (EFI_ERROR(Status)) { Print(L"Fmp->GetImage - %r\n", Status); return ; } Status = WriteFileFromBuffer(ImageName, ImageSize, Image); Print(L"CapsuleApp: Dump %g ImageIndex (0x%x) to %s %r\n", ImageTypeId, ImageIndex, ImageName, Status); FreePool (Image); return ; }
/** Function to create ESRT based on FMP Instances. Create ESRT table, get the descriptors from FMP Instance and create ESRT entries (ESRE). @return Pointer to the ESRT created. **/ EFI_SYSTEM_RESOURCE_TABLE * CreateFmpBasedEsrt ( VOID ) { EFI_STATUS Status; EFI_SYSTEM_RESOURCE_TABLE *Table; UINTN NoProtocols; VOID **Buffer; UINTN Index; EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp; UINTN DescriptorSize; EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf; EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBufOrg; UINT8 FmpImageInfoCount; UINT32 FmpImageInfoDescriptorVer; UINTN ImageInfoSize; UINT32 PackageVersion; CHAR16 *PackageVersionName; Status = EFI_SUCCESS; Table = NULL; NoProtocols = 0; Buffer = NULL; PackageVersionName = NULL; FmpImageInfoBuf = NULL; FmpImageInfoBufOrg = NULL; Fmp = NULL; Status = EfiLocateProtocolBuffer ( &gEfiFirmwareManagementProtocolGuid, &NoProtocols, &Buffer ); if (EFI_ERROR(Status) || (Buffer == NULL)) { return NULL; } // // Allocate Memory for table // Table = AllocateZeroPool ( (GROWTH_STEP * sizeof (EFI_SYSTEM_RESOURCE_ENTRY)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE) ); if (Table == NULL) { DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to allocate memory for ESRT.\n")); gBS->FreePool (Buffer); return NULL; } Table->FwResourceCount = 0; Table->FwResourceCountMax = GROWTH_STEP; Table->FwResourceVersion = EFI_SYSTEM_RESOURCE_TABLE_FIRMWARE_RESOURCE_VERSION; for (Index = 0; Index < NoProtocols; Index++) { Fmp = (EFI_FIRMWARE_MANAGEMENT_PROTOCOL *) Buffer[Index]; ImageInfoSize = 0; Status = Fmp->GetImageInfo ( Fmp, // FMP Pointer &ImageInfoSize, // Buffer Size (in this case 0) NULL, // NULL so we can get size &FmpImageInfoDescriptorVer, // DescriptorVersion &FmpImageInfoCount, // DescriptorCount &DescriptorSize, // DescriptorSize &PackageVersion, // PackageVersion &PackageVersionName // PackageVersionName ); if (Status != EFI_BUFFER_TOO_SMALL) { DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Unexpected Failure in GetImageInfo. Status = %r\n", Status)); continue; } FmpImageInfoBuf = AllocateZeroPool (ImageInfoSize); if (FmpImageInfoBuf == NULL) { DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to get memory for descriptors.\n")); continue; } FmpImageInfoBufOrg = FmpImageInfoBuf; PackageVersionName = NULL; Status = Fmp->GetImageInfo ( Fmp, &ImageInfoSize, // ImageInfoSize FmpImageInfoBuf, // ImageInfo &FmpImageInfoDescriptorVer, // DescriptorVersion &FmpImageInfoCount, // DescriptorCount &DescriptorSize, // DescriptorSize &PackageVersion, // PackageVersion &PackageVersionName // PackageVersionName ); if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failure in GetImageInfo. Status = %r\n", Status)); FreePool (FmpImageInfoBufOrg); FmpImageInfoBufOrg = NULL; continue; } // // Check each descriptor and read from the one specified // while (FmpImageInfoCount > 0) { // // If the descriptor has the IN USE bit set, create ESRT entry otherwise ignore. // if ((FmpImageInfoBuf->AttributesSetting & FmpImageInfoBuf->AttributesSupported & IMAGE_ATTRIBUTE_IN_USE) == IMAGE_ATTRIBUTE_IN_USE) { // // Create ESRT entry // CreateEsrtEntry (&Table, FmpImageInfoBuf, FmpImageInfoDescriptorVer); } FmpImageInfoCount--; // // Increment the buffer pointer ahead by the size of the descriptor // FmpImageInfoBuf = (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)(((UINT8 *)FmpImageInfoBuf) + DescriptorSize); } if (PackageVersionName != NULL) { FreePool (PackageVersionName); PackageVersionName = NULL; } FreePool (FmpImageInfoBufOrg); FmpImageInfoBufOrg = NULL; } gBS->FreePool (Buffer); return Table; }
/** Dump FMP protocol info. **/ VOID DumpFmpData ( VOID ) { EFI_STATUS Status; EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp; EFI_HANDLE *HandleBuffer; UINTN NumberOfHandles; UINTN Index; EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf; UINTN ImageInfoSize; UINT32 FmpImageInfoDescriptorVer; UINT8 FmpImageInfoCount; UINTN DescriptorSize; UINT32 PackageVersion; CHAR16 *PackageVersionName; UINT32 PackageVersionNameMaxLen; UINT64 AttributesSupported; UINT64 AttributesSetting; Print(L"############\n"); Print(L"# FMP DATA #\n"); Print(L"############\n"); Status = gBS->LocateHandleBuffer ( ByProtocol, &gEfiFirmwareManagementProtocolGuid, NULL, &NumberOfHandles, &HandleBuffer ); if (EFI_ERROR(Status)) { Print(L"FMP protocol - %r\n", EFI_NOT_FOUND); return; } for (Index = 0; Index < NumberOfHandles; Index++) { Status = gBS->HandleProtocol( HandleBuffer[Index], &gEfiFirmwareManagementProtocolGuid, (VOID **)&Fmp ); if (EFI_ERROR(Status)) { continue; } ImageInfoSize = 0; Status = Fmp->GetImageInfo ( Fmp, &ImageInfoSize, NULL, NULL, NULL, NULL, NULL, NULL ); if (Status != EFI_BUFFER_TOO_SMALL) { continue; } FmpImageInfoBuf = NULL; FmpImageInfoBuf = AllocateZeroPool (ImageInfoSize); if (FmpImageInfoBuf == NULL) { Status = EFI_OUT_OF_RESOURCES; goto EXIT; } PackageVersionName = NULL; Status = Fmp->GetImageInfo ( Fmp, &ImageInfoSize, // ImageInfoSize FmpImageInfoBuf, // ImageInfo &FmpImageInfoDescriptorVer, // DescriptorVersion &FmpImageInfoCount, // DescriptorCount &DescriptorSize, // DescriptorSize &PackageVersion, // PackageVersion &PackageVersionName // PackageVersionName ); // // If FMP GetInformation interface failed, skip this resource // if (EFI_ERROR(Status)) { Print(L"FMP (%d) ImageInfo - %r\n", Index, Status); FreePool(FmpImageInfoBuf); continue; } Print(L"FMP (%d) ImageInfo:\n", Index); DumpFmpImageInfo( ImageInfoSize, // ImageInfoSize FmpImageInfoBuf, // ImageInfo FmpImageInfoDescriptorVer, // DescriptorVersion FmpImageInfoCount, // DescriptorCount DescriptorSize, // DescriptorSize PackageVersion, // PackageVersion PackageVersionName // PackageVersionName ); if (PackageVersionName != NULL) { FreePool(PackageVersionName); } FreePool(FmpImageInfoBuf); // // Get package info // PackageVersionName = NULL; Status = Fmp->GetPackageInfo ( Fmp, &PackageVersion, // PackageVersion &PackageVersionName, // PackageVersionName &PackageVersionNameMaxLen, // PackageVersionNameMaxLen &AttributesSupported, // AttributesSupported &AttributesSetting // AttributesSetting ); if (EFI_ERROR(Status)) { Print(L"FMP (%d) PackageInfo - %r\n", Index, Status); } else { Print(L"FMP (%d) ImageInfo:\n", Index); DumpFmpPackageInfo( PackageVersion, // PackageVersion PackageVersionName, // PackageVersionName PackageVersionNameMaxLen, // PackageVersionNameMaxLen AttributesSupported, // AttributesSupported AttributesSetting // AttributesSetting ); if (PackageVersionName != NULL) { FreePool(PackageVersionName); } } } Print(L"\n"); EXIT: FreePool(HandleBuffer); }
/** Updates the firmware image of the device. This function updates the hardware with the new firmware image. This function returns EFI_UNSUPPORTED if the firmware image is not updatable. If the firmware image is updatable, the function should perform the following minimal validations before proceeding to do the firmware image update. - Validate the image authentication if image has attribute IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns EFI_SECURITY_VIOLATION if the validation fails. - Validate the image is a supported image for this device. The function returns EFI_ABORTED if the image is unsupported. The function can optionally provide more detailed information on why the image is not a supported image. - Validate the data from VendorCode if not null. Image validation must be performed before VendorCode data validation. VendorCode data is ignored or considered invalid if image validation failed. The function returns EFI_ABORTED if the data is invalid. VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if the caller did not specify the policy or use the default policy. As an example, vendor can implement a policy to allow an option to force a firmware image update when the abort reason is due to the new firmware image version is older than the current firmware image version or bad image checksum. Sensitive operations such as those wiping the entire firmware image and render the device to be non-functional should be encoded in the image itself rather than passed with the VendorCode. AbortReason enables vendor to have the option to provide a more detailed description of the abort reason to the caller. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device. The number is between 1 and DescriptorCount. @param[in] Image Points to the new image. @param[in] ImageSize Size of the new image in bytes. @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy. Null indicates the caller did not specify the policy or use the default policy. @param[in] Progress A function used by the driver to report the progress of the firmware update. @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more details for the aborted operation. The buffer is allocated by this function with AllocatePool(), and it is the caller's responsibility to free it with a call to FreePool(). @retval EFI_SUCCESS The device was successfully updated with the new image. @retval EFI_ABORTED The operation is aborted. @retval EFI_INVALID_PARAMETER The Image was NULL. @retval EFI_UNSUPPORTED The operation is not supported. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure. **/ EFI_STATUS EFIAPI FmpSetImage ( IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This, IN UINT8 ImageIndex, IN CONST VOID *Image, IN UINTN ImageSize, IN CONST VOID *VendorCode, IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress, OUT CHAR16 **AbortReason ) { SYSTEM_FMP_PRIVATE_DATA *SystemFmpPrivate; EFI_FIRMWARE_MANAGEMENT_PROTOCOL *SystemFmp; EFI_STATUS Status; EFI_STATUS VarStatus; if (Image == NULL || ImageSize == 0 || AbortReason == NULL) { return EFI_INVALID_PARAMETER; } SystemFmpPrivate = SYSTEM_FMP_PRIVATE_DATA_FROM_FMP(This); *AbortReason = NULL; if (ImageIndex == 0 || ImageIndex > SystemFmpPrivate->DescriptorCount) { return EFI_INVALID_PARAMETER; } // // Process FV // Status = DispatchSystemFmpImages((VOID *)Image, ImageSize, &SystemFmpPrivate->LastAttempt.LastAttemptVersion, &SystemFmpPrivate->LastAttempt.LastAttemptStatus); DEBUG((DEBUG_INFO, "(Agent)SetImage - LastAttemp Version - 0x%x, State - 0x%x\n", SystemFmpPrivate->LastAttempt.LastAttemptVersion, SystemFmpPrivate->LastAttempt.LastAttemptStatus)); if (EFI_ERROR(Status)) { VarStatus = gRT->SetVariable( SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME, &gSystemFmpLastAttemptVariableGuid, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, sizeof(SystemFmpPrivate->LastAttempt), &SystemFmpPrivate->LastAttempt ); DEBUG((DEBUG_INFO, "(Agent)SetLastAttemp - %r\n", VarStatus)); return Status; } // // Pass Thru to System FMP Protocol on same handle as FMP Protocol // Status = gBS->HandleProtocol( SystemFmpPrivate->Handle, &gSystemFmpProtocolGuid, (VOID **)&SystemFmp ); if (EFI_ERROR(Status)) { DEBUG((DEBUG_INFO, "(Agent)SetImage - SystemFmpProtocol - %r\n", Status)); SystemFmpPrivate->LastAttempt.LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT; VarStatus = gRT->SetVariable( SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME, &gSystemFmpLastAttemptVariableGuid, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, sizeof(SystemFmpPrivate->LastAttempt), &SystemFmpPrivate->LastAttempt ); DEBUG((DEBUG_INFO, "(Agent)SetLastAttemp - %r\n", VarStatus)); return Status; } return SystemFmp->SetImage(SystemFmp, ImageIndex, Image, ImageSize, VendorCode, Progress, AbortReason); }
/** return the FMP whoes ImageInfo includes the ImageTypeId. @param[in] ImageTypeId A unique GUID identifying the firmware image type. @return The FMP whoes ImageInfo includes the ImageTypeId **/ EFI_FIRMWARE_MANAGEMENT_PROTOCOL * FindFmpFromImageTypeId ( IN EFI_GUID *ImageTypeId ) { EFI_STATUS Status; EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp; EFI_FIRMWARE_MANAGEMENT_PROTOCOL *TargetFmp; EFI_HANDLE *HandleBuffer; UINTN NumberOfHandles; UINTN Index; EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf; UINTN ImageInfoSize; UINT32 FmpImageInfoDescriptorVer; UINT8 FmpImageInfoCount; UINTN DescriptorSize; UINT32 PackageVersion; CHAR16 *PackageVersionName; Status = gBS->LocateHandleBuffer ( ByProtocol, &gEfiFirmwareManagementProtocolGuid, NULL, &NumberOfHandles, &HandleBuffer ); if (EFI_ERROR(Status)) { Print(L"FMP protocol - %r\n", EFI_NOT_FOUND); return NULL; } TargetFmp = NULL; for (Index = 0; Index < NumberOfHandles; Index++) { Status = gBS->HandleProtocol( HandleBuffer[Index], &gEfiFirmwareManagementProtocolGuid, (VOID **)&Fmp ); if (EFI_ERROR(Status)) { continue; } ImageInfoSize = 0; Status = Fmp->GetImageInfo ( Fmp, &ImageInfoSize, NULL, NULL, NULL, NULL, NULL, NULL ); if (Status != EFI_BUFFER_TOO_SMALL) { continue; } FmpImageInfoBuf = NULL; FmpImageInfoBuf = AllocateZeroPool (ImageInfoSize); if (FmpImageInfoBuf == NULL) { FreePool(HandleBuffer); Print(L"Out of resource\n"); return NULL; } PackageVersionName = NULL; Status = Fmp->GetImageInfo ( Fmp, &ImageInfoSize, // ImageInfoSize FmpImageInfoBuf, // ImageInfo &FmpImageInfoDescriptorVer, // DescriptorVersion &FmpImageInfoCount, // DescriptorCount &DescriptorSize, // DescriptorSize &PackageVersion, // PackageVersion &PackageVersionName // PackageVersionName ); // // If FMP GetInformation interface failed, skip this resource // if (EFI_ERROR(Status)) { FreePool(FmpImageInfoBuf); continue; } if (PackageVersionName != NULL) { FreePool(PackageVersionName); } if (IsThisFmpImageInfo (FmpImageInfoBuf, FmpImageInfoCount, DescriptorSize, ImageTypeId)) { TargetFmp = Fmp; } FreePool(FmpImageInfoBuf); if (TargetFmp != NULL) { break; } } FreePool(HandleBuffer); return TargetFmp; }