2 Functions in this file will mainly focus on looking through the capsule
3 for the image to be programmed, and the flash area that is going to be
6 Copyright (c) 2002 - 2014, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions
10 of the BSD License which accompanies this distribution. The
11 full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "UpdateDriver.h"
21 EFI_HII_HANDLE gHiiHandle
;
24 Update the whole FV, or certain files in the FV.
26 @param ConfigData Pointer to the config data on updating file.
27 @param ImageBuffer Image buffer to be updated.
28 @param ImageSize Image size.
29 @param FileType FFS file type.
30 @param FileAttributes FFS file attribute.
32 @retval EFI_NOT_FOUND The matched FVB protocol is not found.
33 @retval EFI_SUCCESS The image buffer is updated into FV.
37 PerformUpdateOnFirmwareVolume (
38 IN UPDATE_CONFIG_DATA
*ConfigData
,
39 IN UINT8
*ImageBuffer
,
41 IN EFI_FV_FILETYPE FileType
,
42 IN EFI_FV_FILE_ATTRIBUTES FileAttributes
47 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
50 EFI_HANDLE
*HandleBuffer
;
51 EFI_PHYSICAL_ADDRESS BaseAddress
;
52 EFI_FVB_ATTRIBUTES_2 Attributes
;
55 // Locate all Fvb protocol
58 Status
= gBS
->LocateHandleBuffer (
60 &gEfiFirmwareVolumeBlockProtocolGuid
,
65 if ((EFI_ERROR (Status
)) || (NumOfHandles
== 0) || (HandleBuffer
== NULL
)) {
66 if (HandleBuffer
!= NULL
) {
67 FreePool (HandleBuffer
);
73 // Check the FVB protocol one by one
77 for (Index
= 0; Index
< NumOfHandles
; Index
++) {
78 Status
= gBS
->HandleProtocol (
80 &gEfiFirmwareVolumeBlockProtocolGuid
,
81 (VOID
**) &FvbProtocol
83 if (EFI_ERROR (Status
)) {
88 // Ensure this FVB protocol supported Write operation.
90 Status
= FvbProtocol
->GetAttributes (FvbProtocol
, &Attributes
);
91 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
95 Status
= FvbProtocol
->GetPhysicalAddress (
99 if (EFI_ERROR (Status
)) {
102 if (BaseAddress
== ConfigData
->BaseAddress
) {
109 if (HandleBuffer
!= NULL
) {
110 FreePool (HandleBuffer
);
113 return EFI_NOT_FOUND
;
117 // Now we have got the corresponding FVB protocol. Use the FVB protocol
118 // to update the whole FV, or certain files in the FV.
120 if (ConfigData
->UpdateType
== UpdateWholeFV
) {
121 if (FileType
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
122 Status
= EFI_INVALID_PARAMETER
;
124 Status
= PerformUpdateOnWholeFv (
132 } else if (ConfigData
->UpdateType
== UpdateFvFile
) {
133 Status
= PerformUpdateOnFvFile (
144 if (HandleBuffer
!= NULL
) {
145 FreePool (HandleBuffer
);
153 Update the file directly into flash area.
155 @param ConfigData Pointer to the config data on updating file.
156 @param ImageBuffer Image buffer to be updated.
157 @param ImageSize Image size.
159 @retval EFI_SUCCESS The file is updated into flash area.
160 @retval EFI_NOT_FOUND The FVB protocol for the updated flash area is not found.
164 PerformUpdateOnFlashArea (
165 IN UPDATE_CONFIG_DATA
*ConfigData
,
166 IN UINT8
*ImageBuffer
,
172 EFI_PHYSICAL_ADDRESS FlashAddress
;
175 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
178 EFI_HANDLE
*HandleBuffer
;
179 EFI_PHYSICAL_ADDRESS BaseAddress
;
180 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
181 EFI_HANDLE FvbHandle
;
184 EFI_FVB_ATTRIBUTES_2 Attributes
;
186 SizeLeft
= ImageSize
;
187 PtrImage
= ImageBuffer
;
188 FlashAddress
= ConfigData
->BaseAddress
;
189 Status
= EFI_SUCCESS
;
195 TmpStr
= HiiGetString (gHiiHandle
, STRING_TOKEN(UPDATE_FLASH_RANGE
), NULL
);
196 if (TmpStr
!= NULL
) {
197 Print (TmpStr
, FlashAddress
, ((UINT64
)SizeLeft
+ FlashAddress
));
202 // Locate all Fvb protocol
204 Status
= gBS
->LocateHandleBuffer (
206 &gEfiFirmwareVolumeBlockProtocolGuid
,
211 if ((EFI_ERROR (Status
)) || (NumOfHandles
== 0) || (HandleBuffer
== NULL
)) {
212 if (HandleBuffer
!= NULL
) {
213 FreePool (HandleBuffer
);
215 return EFI_NOT_FOUND
;
218 while (SizeLeft
> 0) {
220 // First get the FVB protocols. If the flash area is a FV, or sub FV,
221 // we can directly locate all the FVB protocol. Otherwise we should use
222 // implementation specific method to get the alternate FVB protocol
228 // Check the FVB protocol one by one
230 for (Index
= 0; Index
< NumOfHandles
; Index
++) {
231 Status
= gBS
->HandleProtocol (
233 &gEfiFirmwareVolumeBlockProtocolGuid
,
234 (VOID
**) &FvbProtocol
236 if (EFI_ERROR (Status
)) {
241 // Ensure this FVB protocol supported Write operation.
243 Status
= FvbProtocol
->GetAttributes (FvbProtocol
, &Attributes
);
244 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
248 Status
= FvbProtocol
->GetPhysicalAddress (
252 if (EFI_ERROR (Status
)) {
255 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)BaseAddress
;
258 // This sub area entry falls in the range of the FV
260 if ((FlashAddress
>= BaseAddress
) && (FlashAddress
< (BaseAddress
+ FwVolHeader
->FvLength
))) {
267 if (HandleBuffer
!= NULL
) {
268 FreePool (HandleBuffer
);
271 return EFI_NOT_FOUND
;
274 FvbHandle
= HandleBuffer
[Index
];
278 // If the flash area is boot required, the update must be fault tolerant
280 if (ConfigData
->FaultTolerant
) {
282 // Finally we are here. We have got the corresponding FVB protocol. Now
283 // we need to convert the physical address to LBA and offset and call
284 // FTW write. Also check if the flash range is larger than the FV.
286 Status
= FaultTolerantUpdateOnPartFv (
297 // Finally we are here. We have got the corresponding FVB protocol. Now
298 // we need to convert the physical address to LBA and offset and call
299 // FVB write. Also check if the flash range is larger than the FV.
301 Status
= NonFaultTolerantUpdateOnPartFv (
311 if (EFI_ERROR (Status
)) {
316 // As part of the FV has been replaced, the FV driver shall re-parse
317 // the firmware volume. So re-install FVB protocol here
319 Status
= gBS
->ReinstallProtocolInterface (
321 &gEfiFirmwareVolumeBlockProtocolGuid
,
326 if (EFI_ERROR (Status
)) {
331 // Check if we are done with the update
333 SizeLeft
= SizeLeft
- SizeUpdated
;
334 FlashAddress
= FlashAddress
+ SizeUpdated
;
335 PtrImage
= PtrImage
+ SizeUpdated
;
338 if (HandleBuffer
!= NULL
) {
339 FreePool (HandleBuffer
);
347 Find the updated file, and program it into the flash area based on the config data.
349 @param FwVolProtocol Pointer to FV protocol that contains the updated file.
350 @param ConfigData Pointer to the Config Data on updating file.
352 @retval EFI_INVALID_PARAMETER The update operation is not valid.
353 @retval EFI_NOT_FOUND The updated file is not found.
354 @retval EFI_SUCCESS The file is updated into the flash area.
359 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVolProtocol
,
360 IN UPDATE_CONFIG_DATA
*ConfigData
365 UINTN FileBufferSize
;
366 EFI_FV_FILETYPE FileType
;
367 EFI_FV_FILE_ATTRIBUTES Attrib
;
368 EFI_SECTION_TYPE SectionType
;
369 UINT32 AuthenticationStatus
;
371 BOOLEAN StartToUpdate
;
373 Status
= EFI_SUCCESS
;
376 Status
= FwVolProtocol
->ReadFile (
378 &(ConfigData
->FileGuid
),
379 (VOID
**) &FileBuffer
,
383 &AuthenticationStatus
385 if (EFI_ERROR (Status
)) {
389 StartToUpdate
= FALSE
;
392 // Check if the update image is the one we require
393 // and then perform the update
395 switch (ConfigData
->UpdateType
) {
400 // For UpdateWholeFv, the update file shall be a firmware volume
403 if (FileType
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
404 DEBUG ((EFI_D_UPDATE
, "UpdateDriver: Data file should be of TYPE EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\n"));
405 Status
= EFI_INVALID_PARAMETER
;
407 if (FileBuffer
!= NULL
) {
408 FreePool (FileBuffer
);
410 SectionType
= EFI_SECTION_FIRMWARE_VOLUME_IMAGE
;
413 Status
= FwVolProtocol
->ReadSection (
415 &(ConfigData
->FileGuid
),
418 (VOID
**) &FileBuffer
,
420 &AuthenticationStatus
422 if (!EFI_ERROR (Status
)) {
424 // Execute the update. For UpdateWholeFv, the update
425 // will always execute on a whole FV
427 StartToUpdate
= TRUE
;
428 Status
= PerformUpdateOnFirmwareVolume (
437 DEBUG ((EFI_D_UPDATE
, "UpdateDriver: Data file should be sectioned with TYPE EFI_SECTION_FIRMWARE_VOLUME_IMAGE\n"));
445 // For UpdateFvRange, the update file shall be a raw file
446 // which does not contain any sections. The contents of the file
447 // will be directly programmed.
449 if (FileType
!= EFI_FV_FILETYPE_RAW
) {
450 DEBUG ((EFI_D_UPDATE
, "UpdateDriver: Data file should of TYPE EFI_FV_FILETYPE_RAW\n"));
451 Status
= EFI_INVALID_PARAMETER
;
454 // For UpdateFvRange, the update may be performed on a sub area
455 // of a certain FV, or a flash area that is not FV, or part of FV.
456 // The update may also go across more than one FVs.
458 StartToUpdate
= TRUE
;
459 Status
= PerformUpdateOnFlashArea (
470 // No check will be done the the file got. The contents of the file
471 // will be directly programmed.
472 // Though UpdateFvFile will only update a single file, but the update
473 // will always execute on a FV
475 StartToUpdate
= TRUE
;
476 Status
= PerformUpdateOnFirmwareVolume (
486 Status
= EFI_INVALID_PARAMETER
;
490 if (EFI_ERROR (Status
)) {
491 TmpStr
= HiiGetString (gHiiHandle
, STRING_TOKEN(UPDATE_DRIVER_ABORTED
), NULL
);
493 TmpStr
= HiiGetString (gHiiHandle
, STRING_TOKEN(UPDATE_DRIVER_DONE
), NULL
);
495 if (TmpStr
!= NULL
) {
501 if (FileBuffer
!= NULL
) {
502 FreePool(FileBuffer
);
510 Process the input firmware volume by using DXE service ProcessFirmwareVolume.
512 @param DataBuffer Point to the FV image to be processed.
513 @param BufferSize Size of the FV image buffer.
514 @param FwVolProtocol Point to the installed FV protocol for the input FV image.
516 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
517 @retval EFI_VOLUME_CORRUPTED FV image is corrupted.
518 @retval EFI_SUCCESS FV image is processed and FV protocol is installed.
525 EFI_FIRMWARE_VOLUME2_PROTOCOL
**FwVolProtocol
528 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
529 EFI_HANDLE FwVolHandle
;
531 UINT8
*ProcessedDataBuffer
;
534 ProcessedDataBuffer
= NULL
;
535 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) DataBuffer
;
536 if (FwVolHeader
->FvLength
!= BufferSize
) {
537 return EFI_VOLUME_CORRUPTED
;
540 FvAlignment
= 1 << ((FwVolHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16);
542 // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value.
544 if (FvAlignment
< 8) {
548 // Check FvImage Align is required.
550 if (((UINTN
) FwVolHeader
% FvAlignment
) == 0) {
551 ProcessedDataBuffer
= DataBuffer
;
554 // Allocate new aligned buffer to store DataBuffer.
556 ProcessedDataBuffer
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize
), (UINTN
) FvAlignment
);
557 if (ProcessedDataBuffer
== NULL
) {
558 return EFI_OUT_OF_RESOURCES
;
560 CopyMem (ProcessedDataBuffer
, DataBuffer
, BufferSize
);
563 // Process the firmware volume
565 gDS
->ProcessFirmwareVolume (
572 // Get the FwVol protocol
574 Status
= gBS
->HandleProtocol (
576 &gEfiFirmwareVolume2ProtocolGuid
,
577 (VOID
**) FwVolProtocol
584 Find the image in the same FV and program it in a target Firmware Volume device.
585 After update image, it will reset system and no return.
587 @param ImageHandle A handle for the image that is initializing this driver
588 @param SystemTable A pointer to the EFI system table
590 @retval EFI_ABORTED System reset failed.
591 @retval EFI_NOT_FOUND The updated image is not found in the same FV.
596 InitializeUpdateDriver (
597 IN EFI_HANDLE ImageHandle
,
598 IN EFI_SYSTEM_TABLE
*SystemTable
602 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImageProtocol
;
603 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVolProtocol
;
604 EFI_FIRMWARE_VOLUME2_PROTOCOL
*DataFwVolProtocol
;
605 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*FwVolFilePathNode
;
606 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*AlignedDevPathNode
;
607 EFI_DEVICE_PATH_PROTOCOL
*FilePathNode
;
608 EFI_SECTION_TYPE SectionType
;
610 UINTN FileBufferSize
;
611 EFI_FV_FILETYPE FileType
;
612 EFI_FV_FILE_ATTRIBUTES Attrib
;
613 UINT32 AuthenticationStatus
;
614 UPDATE_CONFIG_DATA
*ConfigData
;
615 UPDATE_CONFIG_DATA
*UpdateConfigData
;
623 if (gST
->ConOut
!= NULL
) {
624 gST
->ConOut
->ClearScreen (gST
->ConOut
);
625 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_YELLOW
| EFI_BRIGHT
);
626 gST
->ConOut
->EnableCursor (gST
->ConOut
, FALSE
);
629 gHiiHandle
= HiiAddPackages (
632 UpdateDriverDxeStrings
,
635 ASSERT (gHiiHandle
!= NULL
);
638 // In order to look for the update data file and programmed image file
639 // from the same volume which this driver is dispatched from, we need
640 // to get the device path of this driver image. It is done by first
641 // locate the LoadedImageProtocol and then get its device path
643 Status
= gBS
->OpenProtocol (
645 &gEfiLoadedImageProtocolGuid
,
646 (VOID
**)&LoadedImageProtocol
,
649 EFI_OPEN_PROTOCOL_GET_PROTOCOL
651 if (EFI_ERROR (Status
)) {
655 // Get the firmware volume protocol where this file resides
657 Status
= gBS
->HandleProtocol (
658 LoadedImageProtocol
->DeviceHandle
,
659 &gEfiFirmwareVolume2ProtocolGuid
,
660 (VOID
**) &FwVolProtocol
662 if (EFI_ERROR (Status
)) {
663 return EFI_NOT_FOUND
;
667 // Shall do some extra check to see if it is really contained in the FV?
668 // Should be able to find the section of this driver in the the FV.
670 FilePathNode
= LoadedImageProtocol
->FilePath
;
671 FwVolFilePathNode
= NULL
;
672 while (!IsDevicePathEnd (FilePathNode
)) {
673 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*)FilePathNode
)!= NULL
) {
674 FwVolFilePathNode
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) FilePathNode
;
677 FilePathNode
= NextDevicePathNode (FilePathNode
);
680 if (FwVolFilePathNode
!= NULL
) {
681 AlignedDevPathNode
= AllocateCopyPool (DevicePathNodeLength (FwVolFilePathNode
), FwVolFilePathNode
);
683 SectionType
= EFI_SECTION_PE32
;
686 Status
= FwVolProtocol
->ReadSection (
688 &(AlignedDevPathNode
->FvFileName
),
691 (VOID
**) &FileBuffer
,
693 &AuthenticationStatus
695 if (EFI_ERROR (Status
)) {
696 FreePool (AlignedDevPathNode
);
700 if (FileBuffer
!= NULL
) {
701 FreePool(FileBuffer
);
706 // Check the NameGuid of the udpate driver so that it can be
707 // used as the CallerId in fault tolerant write protocol
709 if (!CompareGuid (&gEfiCallerIdGuid
, &(AlignedDevPathNode
->FvFileName
))) {
710 FreePool (AlignedDevPathNode
);
711 return EFI_NOT_FOUND
;
713 FreePool (AlignedDevPathNode
);
715 return EFI_NOT_FOUND
;
719 // Now try to find the script file. The script file is usually
720 // a raw data file which does not contain any sections.
724 Status
= FwVolProtocol
->ReadFile (
726 &gEfiConfigFileNameGuid
,
727 (VOID
**) &FileBuffer
,
731 &AuthenticationStatus
733 if (EFI_ERROR (Status
)) {
736 if (FileType
!= EFI_FV_FILETYPE_RAW
) {
737 return EFI_NOT_FOUND
;
741 // Parse the configuration file.
745 Status
= ParseUpdateDataFile (
751 if (FileBuffer
!= NULL
) {
752 FreePool (FileBuffer
);
755 if (EFI_ERROR (Status
)) {
758 ASSERT (ConfigData
!= NULL
);
761 // Now find the update image. The update image should be put in a FV, and then
762 // encapsulated as a raw FFS file. This is to prevent the update image from
763 // being dispatched. So the raw data we get here should be an FV. We need to
764 // process this FV and read the files that is going to be updated.
768 Status
= FwVolProtocol
->ReadFile (
770 &gEfiUpdateDataFileGuid
,
771 (VOID
**) &FileBuffer
,
775 &AuthenticationStatus
777 if (EFI_ERROR (Status
)) {
780 if (FileType
!= EFI_FV_FILETYPE_RAW
) {
781 return EFI_NOT_FOUND
;
785 // FileBuffer should be an FV. Process the FV
787 DataFwVolProtocol
= NULL
;
788 Status
= ProcessUpdateImage (
793 if (EFI_ERROR (Status
)) {
794 FreePool (FileBuffer
);
801 TmpStr
= HiiGetString (gHiiHandle
, STRING_TOKEN(UPDATE_PROCESS_DATA
), NULL
);
802 if (TmpStr
!= NULL
) {
808 // Execute the update
811 UpdateConfigData
= ConfigData
;
812 while (Index
< NumOfUpdates
) {
813 Status
= PerformUpdate (
818 // Shall updates be serialized so that if an update is not successfully completed,
819 // the remaining updates won't be performed.
821 if (EFI_ERROR (Status
)) {
829 if (EFI_ERROR (Status
)) {
830 if (ConfigData
!= NULL
) {
831 FreePool(ConfigData
);
840 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
843 // Hopefully it won't be reached