2 A shell application that triggers capsule update process.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/BaseLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/UefiRuntimeServicesTableLib.h>
22 #include <Library/UefiLib.h>
23 #include <Library/PrintLib.h>
24 #include <Protocol/LoadedImage.h>
25 #include <Protocol/SimpleFileSystem.h>
26 #include <Protocol/GraphicsOutput.h>
27 #include <Guid/FileInfo.h>
29 #include <Guid/GlobalVariable.h>
30 #include <Guid/CapsuleReport.h>
31 #include <Guid/SystemResourceTable.h>
32 #include <Guid/FmpCapsule.h>
33 #include <IndustryStandard/WindowsUxCapsule.h>
35 #define CAPSULE_HEADER_SIZE 0x20
37 #define NESTED_CAPSULE_HEADER_SIZE SIZE_4KB
38 #define SYSTEM_FIRMWARE_FLAG 0x50000
39 #define DEVICE_FIRMWARE_FLAG 0x78010
41 #define MAJOR_VERSION 1
42 #define MINOR_VERSION 0
44 #define MAX_CAPSULE_NUM 10
50 // Define how many block descriptors we want to test with.
52 UINTN NumberOfDescriptors
= 1;
53 UINTN CapsuleFirstIndex
;
54 UINTN CapsuleLastIndex
;
57 Dump capsule information
59 @param[in] CapsuleName The name of the capsule image.
61 @retval EFI_SUCCESS The capsule information is dumped.
62 @retval EFI_UNSUPPORTED Input parameter is not valid.
66 IN CHAR16
*CapsuleName
70 Dump capsule status variable.
72 @retval EFI_SUCCESS The capsule status variable is dumped.
73 @retval EFI_UNSUPPORTED Input parameter is not valid.
76 DmpCapsuleStatusVariable (
81 Dump FMP protocol info.
99 @param[in] FileName The file to be read.
100 @param[out] BufferSize The file buffer size
101 @param[out] Buffer The file buffer
103 @retval EFI_SUCCESS Read file successfully
104 @retval EFI_NOT_FOUND File not found
109 OUT UINTN
*BufferSize
,
116 @param[in] FileName The file to be written.
117 @param[in] BufferSize The file buffer size
118 @param[in] Buffer The file buffer
120 @retval EFI_SUCCESS Write file successfully
123 WriteFileFromBuffer (
131 This function parse application ARG.
143 @retval EFI_SUCCESS The capsule header is appended.
144 @retval EFI_UNSUPPORTED Input parameter is not valid.
145 @retval EFI_OUT_OF_RESOURCES No enough resource to create UX capsule.
152 CHAR16
*OutputCapsuleName
;
156 UINT8
*FullCapsuleBuffer
;
157 UINTN FullCapsuleBufferSize
;
158 EFI_DISPLAY_CAPSULE
*DisplayCapsule
;
160 EFI_GRAPHICS_OUTPUT_PROTOCOL
*Gop
;
162 Status
= gBS
->LocateProtocol(&gEfiGraphicsOutputProtocolGuid
, NULL
, (VOID
**)&Gop
);
163 if (EFI_ERROR(Status
)) {
164 Print(L
"CapsuleApp: NO GOP is found.\n");
165 return EFI_UNSUPPORTED
;
167 Print(L
"Current GOP: Mode - %d, ", Gop
->Mode
->Mode
);
168 Print(L
"HorizontalResolution - %d, ", Gop
->Mode
->Info
->HorizontalResolution
);
169 Print(L
"VerticalResolution - %d\n", Gop
->Mode
->Info
->VerticalResolution
);
170 // HorizontalResolution >= BMP_IMAGE_HEADER.PixelWidth
171 // VerticalResolution >= BMP_IMAGE_HEADER.PixelHeight
174 Print(L
"CapsuleApp: Invalid Parameter.\n");
175 return EFI_UNSUPPORTED
;
178 if (StrCmp(Argv
[3], L
"-O") != 0) {
179 Print(L
"CapsuleApp: NO output capsule name.\n");
180 return EFI_UNSUPPORTED
;
182 OutputCapsuleName
= Argv
[4];
186 FullCapsuleBuffer
= NULL
;
189 Status
= ReadFileToBuffer(BmpName
, &FileSize
, &BmpBuffer
);
190 if (EFI_ERROR(Status
)) {
191 Print(L
"CapsuleApp: BMP image (%s) is not found.\n", BmpName
);
195 FullCapsuleBufferSize
= sizeof(EFI_DISPLAY_CAPSULE
) + FileSize
;
196 FullCapsuleBuffer
= AllocatePool(FullCapsuleBufferSize
);
197 if (FullCapsuleBuffer
== NULL
) {
198 Print(L
"CapsuleApp: Capsule Buffer size (0x%x) too big.\n", FullCapsuleBufferSize
);
199 Status
= EFI_OUT_OF_RESOURCES
;
203 DisplayCapsule
= (EFI_DISPLAY_CAPSULE
*)FullCapsuleBuffer
;
204 CopyGuid(&DisplayCapsule
->CapsuleHeader
.CapsuleGuid
, &gWindowsUxCapsuleGuid
);
205 DisplayCapsule
->CapsuleHeader
.HeaderSize
= sizeof(DisplayCapsule
->CapsuleHeader
);
206 DisplayCapsule
->CapsuleHeader
.Flags
= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
207 DisplayCapsule
->CapsuleHeader
.CapsuleImageSize
= (UINT32
)FullCapsuleBufferSize
;
209 DisplayCapsule
->ImagePayload
.Version
= 1;
210 DisplayCapsule
->ImagePayload
.Checksum
= 0;
211 DisplayCapsule
->ImagePayload
.ImageType
= 0; // BMP
212 DisplayCapsule
->ImagePayload
.Reserved
= 0;
213 DisplayCapsule
->ImagePayload
.Mode
= Gop
->Mode
->Mode
;
214 DisplayCapsule
->ImagePayload
.OffsetX
= 0;
215 DisplayCapsule
->ImagePayload
.OffsetY
= 0;
217 CopyMem((DisplayCapsule
+ 1), BmpBuffer
, FileSize
);
219 DisplayCapsule
->ImagePayload
.Checksum
= CalculateCheckSum8(FullCapsuleBuffer
, FullCapsuleBufferSize
);
221 Status
= WriteFileFromBuffer(OutputCapsuleName
, FullCapsuleBufferSize
, FullCapsuleBuffer
);
222 Print(L
"CapsuleApp: Write %s %r\n", OutputCapsuleName
, Status
);
225 if (BmpBuffer
!= NULL
) {
229 if (FullCapsuleBuffer
!= NULL
) {
230 FreePool(FullCapsuleBuffer
);
237 Get ImageTypeId in the FMP capsule header.
239 @param[in] CapsuleHeader The FMP capsule image header.
244 GetCapsuleImageTypeId (
245 IN EFI_CAPSULE_HEADER
*CapsuleHeader
248 EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER
*FmpCapsuleHeader
;
249 UINT64
*ItemOffsetList
;
250 EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER
*ImageHeader
;
252 FmpCapsuleHeader
= (EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER
*)((UINT8
*)CapsuleHeader
+ CapsuleHeader
->HeaderSize
);
253 ItemOffsetList
= (UINT64
*)(FmpCapsuleHeader
+ 1);
254 if (FmpCapsuleHeader
->PayloadItemCount
== 0) {
257 ImageHeader
= (EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER
*)((UINT8
*)FmpCapsuleHeader
+ ItemOffsetList
[FmpCapsuleHeader
->EmbeddedDriverCount
]);
258 return &ImageHeader
->UpdateImageTypeId
;
262 Get ESRT FwType according to ImageTypeId
264 @param[in] ImageTypeId ImageTypeId of an FMP capsule.
270 IN EFI_GUID
*ImageTypeId
274 EFI_SYSTEM_RESOURCE_TABLE
*Esrt
;
275 EFI_SYSTEM_RESOURCE_ENTRY
*EsrtEntry
;
281 Status
= EfiGetSystemConfigurationTable(&gEfiSystemResourceTableGuid
, (VOID
**)&Esrt
);
282 if (!EFI_ERROR(Status
)) {
283 ASSERT(Esrt
!= NULL
);
284 EsrtEntry
= (VOID
*)(Esrt
+ 1);
285 for (Index
= 0; Index
< Esrt
->FwResourceCount
; Index
++, EsrtEntry
++) {
286 if (CompareGuid(&EsrtEntry
->FwClass
, ImageTypeId
)) {
287 return EsrtEntry
->FwType
;
292 return ESRT_FW_TYPE_UNKNOWN
;
296 Append a capsule header on top of current image.
297 This function follows Windows UEFI Firmware Update Platform document.
299 @retval EFI_SUCCESS The capsule header is appended.
300 @retval EFI_UNSUPPORTED Input parameter is not valid.
301 @retval EFI_OUT_OF_RESOURCES No enough resource to append capsule header.
308 CHAR16
*OutputCapsuleName
;
312 UINT8
*FullCapsuleBuffer
;
313 UINTN FullCapsuleBufferSize
;
314 EFI_CAPSULE_HEADER
*NestedCapsuleHeader
;
315 EFI_GUID
*ImageTypeId
;
320 Print(L
"CapsuleApp: Invalid Parameter.\n");
321 return EFI_UNSUPPORTED
;
324 if (StrCmp(Argv
[3], L
"-O") != 0) {
325 Print(L
"CapsuleApp: NO output capsule name.\n");
326 return EFI_UNSUPPORTED
;
328 OutputCapsuleName
= Argv
[4];
330 CapsuleBuffer
= NULL
;
332 FullCapsuleBuffer
= NULL
;
334 CapsuleName
= Argv
[2];
335 Status
= ReadFileToBuffer(CapsuleName
, &FileSize
, &CapsuleBuffer
);
336 if (EFI_ERROR(Status
)) {
337 Print(L
"CapsuleApp: Capsule image (%s) is not found.\n", CapsuleName
);
341 ImageTypeId
= GetCapsuleImageTypeId(CapsuleBuffer
);
342 if (ImageTypeId
== NULL
) {
343 Print(L
"CapsuleApp: Capsule ImageTypeId is not found.\n");
346 FwType
= GetEsrtFwType(ImageTypeId
);
347 if ((FwType
!= ESRT_FW_TYPE_SYSTEMFIRMWARE
) && (FwType
!= ESRT_FW_TYPE_DEVICEFIRMWARE
)) {
348 Print(L
"CapsuleApp: Capsule FwType is invalid.\n");
352 FullCapsuleBufferSize
= NESTED_CAPSULE_HEADER_SIZE
+ FileSize
;
353 FullCapsuleBuffer
= AllocatePool(FullCapsuleBufferSize
);
354 if (FullCapsuleBuffer
== NULL
) {
355 Print(L
"CapsuleApp: Capsule Buffer size (0x%x) too big.\n", FullCapsuleBufferSize
);
356 Status
= EFI_OUT_OF_RESOURCES
;
360 NestedCapsuleHeader
= (EFI_CAPSULE_HEADER
*)FullCapsuleBuffer
;
361 ZeroMem(NestedCapsuleHeader
, NESTED_CAPSULE_HEADER_SIZE
);
362 CopyGuid(&NestedCapsuleHeader
->CapsuleGuid
, ImageTypeId
);
363 NestedCapsuleHeader
->HeaderSize
= NESTED_CAPSULE_HEADER_SIZE
;
364 NestedCapsuleHeader
->Flags
= (FwType
== ESRT_FW_TYPE_DEVICEFIRMWARE
) ? SYSTEM_FIRMWARE_FLAG
: DEVICE_FIRMWARE_FLAG
;
365 NestedCapsuleHeader
->CapsuleImageSize
= (UINT32
)FullCapsuleBufferSize
;
367 CopyMem((UINT8
*)NestedCapsuleHeader
+ NestedCapsuleHeader
->HeaderSize
, CapsuleBuffer
, FileSize
);
369 Status
= WriteFileFromBuffer(OutputCapsuleName
, FullCapsuleBufferSize
, FullCapsuleBuffer
);
370 Print(L
"CapsuleApp: Write %s %r\n", OutputCapsuleName
, Status
);
373 if (CapsuleBuffer
!= NULL
) {
374 FreePool(CapsuleBuffer
);
377 if (FullCapsuleBuffer
!= NULL
) {
378 FreePool(FullCapsuleBuffer
);
386 Clear capsule status variable.
388 @retval EFI_SUCCESS The capsule status variable is cleared.
391 ClearCapsuleStatusVariable (
397 CHAR16 CapsuleVarName
[20];
400 StrCpyS (CapsuleVarName
, sizeof(CapsuleVarName
)/sizeof(CapsuleVarName
[0]), L
"Capsule");
401 TempVarName
= CapsuleVarName
+ StrLen (CapsuleVarName
);
405 UnicodeSPrint (TempVarName
, 5 * sizeof(CHAR16
), L
"%04x", Index
);
407 Status
= gRT
->SetVariable (
409 &gEfiCapsuleReportGuid
,
410 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
414 if (EFI_ERROR(Status
)) {
416 // There is no capsule variables, quit
422 if (Index
> 0xFFFF) {
431 Build Gather list for a list of capsule images.
433 @param[in] CapsuleBuffer An array of pointer to capsule images
434 @param[in] FileSize An array of UINTN to capsule images size
435 @param[in] CapsuleNum The count of capsule images
436 @param[out] BlockDescriptors The block descriptors for the capsule images
438 @retval EFI_SUCCESS The block descriptors for the capsule images are constructed.
442 IN VOID
**CapsuleBuffer
,
445 OUT EFI_CAPSULE_BLOCK_DESCRIPTOR
**BlockDescriptors
449 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptors1
;
450 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptors2
;
451 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptorPre
;
452 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptorsHeader
;
453 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockPtr
;
462 BlockDescriptors1
= NULL
;
463 BlockDescriptors2
= NULL
;
464 BlockDescriptorPre
= NULL
;
465 BlockDescriptorsHeader
= NULL
;
467 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
469 // Allocate memory for the descriptors.
471 if (NumberOfDescriptors
== 1) {
474 Count
= (INT32
)(NumberOfDescriptors
+ 2) / 2;
477 Size
= Count
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
478 BlockDescriptors1
= AllocateRuntimeZeroPool (Size
);
479 if (BlockDescriptors1
== NULL
) {
480 Print (L
"CapsuleApp: failed to allocate memory for descriptors\n");
481 Status
= EFI_OUT_OF_RESOURCES
;
484 Print (L
"CapsuleApp: creating capsule descriptors at 0x%X\n", (UINTN
) BlockDescriptors1
);
485 Print (L
"CapsuleApp: capsule data starts at 0x%X with size 0x%X\n", (UINTN
) CapsuleBuffer
, FileSize
);
489 // Record descirptor header
492 BlockDescriptorsHeader
= BlockDescriptors1
;
495 if (BlockDescriptorPre
!= NULL
) {
496 BlockDescriptorPre
->Union
.ContinuationPointer
= (UINTN
) BlockDescriptors1
;
497 BlockDescriptorPre
->Length
= 0;
503 TempBlockPtr
= BlockDescriptors1
;
504 TempDataPtr
= CapsuleBuffer
[Index
];
505 SizeLeft
= FileSize
[Index
];
506 for (Number
= 0; (Number
< Count
- 1) && (SizeLeft
!= 0); Number
++) {
508 // Divide remaining data in half
510 if (NumberOfDescriptors
!= 1) {
519 TempBlockPtr
->Union
.DataBlock
= (UINTN
)TempDataPtr
;
520 TempBlockPtr
->Length
= Size
;
521 Print (L
"CapsuleApp: capsule block/size 0x%X/0x%X\n", (UINTN
) TempDataPtr
, Size
);
528 // Allocate the second list, point the first block's last entry to point
529 // to this one, and fill this one in. Worst case is that the previous
530 // list only had one element that pointed here, so we need at least two
531 // elements -- one to point to all the data, another to terminate the list.
533 if ((NumberOfDescriptors
!= 1) && (SizeLeft
!= 0)) {
534 Count
= (INT32
)(NumberOfDescriptors
+ 2) - Count
;
539 Size
= Count
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
540 BlockDescriptors2
= AllocateRuntimeZeroPool (Size
);
541 if (BlockDescriptors2
== NULL
) {
542 Print (L
"CapsuleApp: failed to allocate memory for descriptors\n");
543 Status
= EFI_OUT_OF_RESOURCES
;
548 // Point the first list's last element to point to this second list.
550 TempBlockPtr
->Union
.ContinuationPointer
= (UINTN
) BlockDescriptors2
;
552 TempBlockPtr
->Length
= 0;
553 TempBlockPtr
= BlockDescriptors2
;
554 for (Number
= 0; Number
< Count
- 1; Number
++) {
556 // If second-to-last one, then dump rest to this element
558 if (Number
== (Count
- 2)) {
562 // Divide remaining data in half
571 TempBlockPtr
->Union
.DataBlock
= (UINTN
)TempDataPtr
;
572 TempBlockPtr
->Length
= Size
;
573 Print (L
"CapsuleApp: capsule block/size 0x%X/0x%X\n", (UINTN
) TempDataPtr
, Size
);
583 BlockDescriptorPre
= TempBlockPtr
;
584 BlockDescriptors1
= NULL
;
590 if (TempBlockPtr
!= NULL
) {
591 TempBlockPtr
->Union
.ContinuationPointer
= (UINTN
)NULL
;
592 TempBlockPtr
->Length
= 0;
593 *BlockDescriptors
= BlockDescriptorsHeader
;
599 if (BlockDescriptors1
!= NULL
) {
600 FreePool(BlockDescriptors1
);
603 if (BlockDescriptors2
!= NULL
) {
604 FreePool(BlockDescriptors2
);
611 Clear the Gather list for a list of capsule images.
613 @param[in] BlockDescriptors The block descriptors for the capsule images
614 @param[in] CapsuleNum The count of capsule images
618 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptors
,
622 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockPtr
;
623 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockPtr1
;
624 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockPtr2
;
627 if (BlockDescriptors
!= NULL
) {
628 TempBlockPtr1
= BlockDescriptors
;
630 TempBlockPtr
= TempBlockPtr1
;
631 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
632 if (TempBlockPtr
[Index
].Length
== 0) {
637 if (TempBlockPtr
[Index
].Union
.ContinuationPointer
== (UINTN
)NULL
) {
641 TempBlockPtr2
= (VOID
*) ((UINTN
) TempBlockPtr
->Union
.ContinuationPointer
);
642 FreePool(TempBlockPtr1
);
643 TempBlockPtr1
= TempBlockPtr2
;
656 Print(L
"CapsuleApp: usage\n");
657 Print(L
" CapsuleApp <Capsule...>\n");
658 Print(L
" CapsuleApp -S\n");
659 Print(L
" CapsuleApp -C\n");
660 Print(L
" CapsuleApp -P\n");
661 Print(L
" CapsuleApp -E\n");
662 Print(L
" CapsuleApp -G <BMP> -O <Capsule>\n");
663 Print(L
" CapsuleApp -N <Capsule> -O <NestedCapsule>\n");
664 Print(L
" CapsuleApp -D <Capsule>\n");
665 Print(L
"Parameter:\n");
666 Print(L
" -S: Dump capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
667 Print(L
" which is defined in UEFI specification.\n");
668 Print(L
" -C: Clear capsule report variable (EFI_CAPSULE_RPORT_GUID),\n");
669 Print(L
" which is defined in UEFI specification.\n");
670 Print(L
" -P: Dump UEFI FMP protocol info.\n");
671 Print(L
" -E: Dump UEFI ESRT table info.\n");
672 Print(L
" -G: Convert a BMP file to be a UX capsule,\n");
673 Print(L
" according to Windows Firmware Update document\n");
674 Print(L
" -N: Append a Capsule Header to an existing capsule image,\n");
675 Print(L
" according to Windows Firmware Update document\n");
676 Print(L
" -O: Output new Capsule file name\n");
677 Print(L
" -D: Dump Capsule image header information and FMP header information,\n");
678 Print(L
" if it is an FMP capsule.\n");
682 Update Capsule image.
684 @param[in] ImageHandle The image handle.
685 @param[in] SystemTable The system table.
687 @retval EFI_SUCCESS Command completed successfully.
688 @retval EFI_INVALID_PARAMETER Command usage error.
689 @retval EFI_NOT_FOUND The input file can't be found.
694 IN EFI_HANDLE ImageHandle
,
695 IN EFI_SYSTEM_TABLE
*SystemTable
699 UINTN FileSize
[MAX_CAPSULE_NUM
];
700 VOID
*CapsuleBuffer
[MAX_CAPSULE_NUM
];
701 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptors
;
702 EFI_CAPSULE_HEADER
*CapsuleHeaderArray
[MAX_CAPSULE_NUM
+ 1];
703 UINT64 MaxCapsuleSize
;
704 EFI_RESET_TYPE ResetType
;
711 if (EFI_ERROR(Status
)) {
712 Print(L
"Please use UEFI SHELL to run this application!\n", Status
);
717 return EFI_INVALID_PARAMETER
;
719 if (StrCmp(Argv
[1], L
"-D") == 0) {
720 Status
= DumpCapsule(Argv
[2]);
723 if (StrCmp(Argv
[1], L
"-G") == 0) {
724 Status
= CreateBmpFmp();
727 if (StrCmp(Argv
[1], L
"-N") == 0) {
728 Status
= CreateNestedFmp();
731 if (StrCmp(Argv
[1], L
"-S") == 0) {
732 Status
= DmpCapsuleStatusVariable();
735 if (StrCmp(Argv
[1], L
"-C") == 0) {
736 Status
= ClearCapsuleStatusVariable();
739 if (StrCmp(Argv
[1], L
"-P") == 0) {
743 if (StrCmp(Argv
[1], L
"-E") == 0) {
747 CapsuleFirstIndex
= 1;
748 CapsuleLastIndex
= Argc
- 1;
749 CapsuleNum
= CapsuleLastIndex
- CapsuleFirstIndex
+ 1;
751 if (CapsuleFirstIndex
> CapsuleLastIndex
) {
752 Print(L
"CapsuleApp: NO capsule image.\n");
753 return EFI_UNSUPPORTED
;
755 if (CapsuleNum
> MAX_CAPSULE_NUM
) {
756 Print(L
"CapsuleApp: Too many capsule images.\n");
757 return EFI_UNSUPPORTED
;
760 ZeroMem(&CapsuleBuffer
, sizeof(CapsuleBuffer
));
761 ZeroMem(&FileSize
, sizeof(FileSize
));
762 BlockDescriptors
= NULL
;
764 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
765 CapsuleName
= Argv
[CapsuleFirstIndex
+ Index
];
766 Status
= ReadFileToBuffer(CapsuleName
, &FileSize
[Index
], &CapsuleBuffer
[Index
]);
767 if (EFI_ERROR(Status
)) {
768 Print(L
"CapsuleApp: capsule image (%s) is not found.\n", CapsuleName
);
774 // Every capsule use 2 descriptor 1 for data 1 for end
776 Status
= BuildGatherList(CapsuleBuffer
, FileSize
, CapsuleNum
, &BlockDescriptors
);
777 if (EFI_ERROR(Status
)) {
782 // Call the runtime service capsule.
785 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
786 CapsuleHeaderArray
[Index
] = (EFI_CAPSULE_HEADER
*) CapsuleBuffer
[Index
];
787 if ((CapsuleHeaderArray
[Index
]->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) != 0) {
791 CapsuleHeaderArray
[CapsuleNum
] = NULL
;
794 // Inquire platform capability of UpdateCapsule.
796 Status
= gRT
->QueryCapsuleCapabilities (CapsuleHeaderArray
, CapsuleNum
, &MaxCapsuleSize
, &ResetType
);
797 if (EFI_ERROR(Status
)) {
798 Print (L
"CapsuleApp: failed to query capsule capability - %r\n", Status
);
802 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
803 if (FileSize
[Index
] > MaxCapsuleSize
) {
804 Print (L
"CapsuleApp: capsule is too large to update, %ld is allowed\n", MaxCapsuleSize
);
805 Status
= EFI_UNSUPPORTED
;
811 // Check whether the input capsule image has the flag of persist across system reset.
814 Status
= gRT
->UpdateCapsule(CapsuleHeaderArray
,CapsuleNum
,(UINTN
) BlockDescriptors
);
815 if (Status
!= EFI_SUCCESS
) {
816 Print (L
"CapsuleApp: failed to update capsule - %r\n", Status
);
820 // For capsule who has reset flag, after calling UpdateCapsule service,triger a
821 // system reset to process capsule persist across a system reset.
823 gRT
->ResetSystem (ResetType
, EFI_SUCCESS
, 0, NULL
);
826 // For capsule who has no reset flag, only call UpdateCapsule Service without a
827 // system reset. The service will process the capsule immediately.
829 Status
= gRT
->UpdateCapsule (CapsuleHeaderArray
,CapsuleNum
,(UINTN
) BlockDescriptors
);
830 if (Status
!= EFI_SUCCESS
) {
831 Print (L
"CapsuleApp: failed to update capsule - %r\n", Status
);
835 Status
= EFI_SUCCESS
;
838 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
839 if (CapsuleBuffer
[Index
] != NULL
) {
840 FreePool (CapsuleBuffer
[Index
]);
844 CleanGatherList(BlockDescriptors
, CapsuleNum
);