2 A shell application that triggers capsule update process.
4 Copyright (c) 2016 - 2018, 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 <Library/BmpSupportLib.h>
25 #include <Protocol/GraphicsOutput.h>
26 #include <Guid/GlobalVariable.h>
27 #include <Guid/CapsuleReport.h>
28 #include <Guid/SystemResourceTable.h>
29 #include <Guid/FmpCapsule.h>
30 #include <IndustryStandard/WindowsUxCapsule.h>
32 #define CAPSULE_HEADER_SIZE 0x20
34 #define NESTED_CAPSULE_HEADER_SIZE SIZE_4KB
35 #define SYSTEM_FIRMWARE_FLAG 0x50000
36 #define DEVICE_FIRMWARE_FLAG 0x78010
38 #define MAJOR_VERSION 1
39 #define MINOR_VERSION 0
41 #define MAX_CAPSULE_NUM 10
47 // Define how many block descriptors we want to test with.
49 UINTN NumberOfDescriptors
= 1;
50 UINTN CapsuleFirstIndex
;
51 UINTN CapsuleLastIndex
;
54 Dump capsule information
56 @param[in] CapsuleName The name of the capsule image.
58 @retval EFI_SUCCESS The capsule information is dumped.
59 @retval EFI_UNSUPPORTED Input parameter is not valid.
63 IN CHAR16
*CapsuleName
67 Dump capsule status variable.
69 @retval EFI_SUCCESS The capsule status variable is dumped.
70 @retval EFI_UNSUPPORTED Input parameter is not valid.
73 DmpCapsuleStatusVariable (
78 Dump FMP protocol info.
88 @param[in] ImageTypeId The ImageTypeId of the FMP image.
89 It is used to identify the FMP protocol.
90 @param[in] ImageIndex The ImageIndex of the FMP image.
91 It is the input parameter for FMP->GetImage().
92 @param[in] ImageName The file name to hold the output FMP image.
96 IN EFI_GUID
*ImageTypeId
,
112 @param[in] FileName The file to be read.
113 @param[out] BufferSize The file buffer size
114 @param[out] Buffer The file buffer
116 @retval EFI_SUCCESS Read file successfully
117 @retval EFI_NOT_FOUND Shell protocol or file not found
118 @retval others Read file failed
123 OUT UINTN
*BufferSize
,
130 @param[in] FileName The file to be written.
131 @param[in] BufferSize The file buffer size
132 @param[in] Buffer The file buffer
134 @retval EFI_SUCCESS Write file successfully
135 @retval EFI_NOT_FOUND Shell protocol not found
136 @retval others Write file failed
139 WriteFileFromBuffer (
147 This function parse application ARG.
159 @retval EFI_SUCCESS The capsule header is appended.
160 @retval EFI_UNSUPPORTED Input parameter is not valid.
161 @retval EFI_OUT_OF_RESOURCES No enough resource to create UX capsule.
168 CHAR16
*OutputCapsuleName
;
172 UINT8
*FullCapsuleBuffer
;
173 UINTN FullCapsuleBufferSize
;
174 EFI_DISPLAY_CAPSULE
*DisplayCapsule
;
176 EFI_GRAPHICS_OUTPUT_PROTOCOL
*Gop
;
177 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*Info
;
178 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*GopBlt
;
183 Status
= gBS
->LocateProtocol(&gEfiGraphicsOutputProtocolGuid
, NULL
, (VOID
**)&Gop
);
184 if (EFI_ERROR(Status
)) {
185 Print(L
"CapsuleApp: NO GOP is found.\n");
186 return EFI_UNSUPPORTED
;
188 Info
= Gop
->Mode
->Info
;
189 Print(L
"Current GOP: Mode - %d, ", Gop
->Mode
->Mode
);
190 Print(L
"HorizontalResolution - %d, ", Info
->HorizontalResolution
);
191 Print(L
"VerticalResolution - %d\n", Info
->VerticalResolution
);
192 // HorizontalResolution >= BMP_IMAGE_HEADER.PixelWidth
193 // VerticalResolution >= BMP_IMAGE_HEADER.PixelHeight
196 Print(L
"CapsuleApp: Incorrect parameter count.\n");
197 return EFI_UNSUPPORTED
;
200 if (StrCmp(Argv
[3], L
"-O") != 0) {
201 Print(L
"CapsuleApp: NO output capsule name.\n");
202 return EFI_UNSUPPORTED
;
204 OutputCapsuleName
= Argv
[4];
208 FullCapsuleBuffer
= NULL
;
211 Status
= ReadFileToBuffer(BmpName
, &FileSize
, &BmpBuffer
);
212 if (EFI_ERROR(Status
)) {
213 Print(L
"CapsuleApp: BMP image (%s) is not found.\n", BmpName
);
218 Status
= TranslateBmpToGopBlt (
226 if (EFI_ERROR(Status
)) {
227 Print(L
"CapsuleApp: BMP image (%s) is not valid.\n", BmpName
);
230 if (GopBlt
!= NULL
) {
233 Print(L
"BMP image (%s), Width - %d, Height - %d\n", BmpName
, Width
, Height
);
235 if (Height
> Info
->VerticalResolution
) {
236 Status
= EFI_INVALID_PARAMETER
;
237 Print(L
"CapsuleApp: BMP image (%s) height is larger than current resolution.\n", BmpName
);
240 if (Width
> Info
->HorizontalResolution
) {
241 Status
= EFI_INVALID_PARAMETER
;
242 Print(L
"CapsuleApp: BMP image (%s) width is larger than current resolution.\n", BmpName
);
246 FullCapsuleBufferSize
= sizeof(EFI_DISPLAY_CAPSULE
) + FileSize
;
247 FullCapsuleBuffer
= AllocatePool(FullCapsuleBufferSize
);
248 if (FullCapsuleBuffer
== NULL
) {
249 Print(L
"CapsuleApp: Capsule Buffer size (0x%x) too big.\n", FullCapsuleBufferSize
);
250 Status
= EFI_OUT_OF_RESOURCES
;
254 DisplayCapsule
= (EFI_DISPLAY_CAPSULE
*)FullCapsuleBuffer
;
255 CopyGuid(&DisplayCapsule
->CapsuleHeader
.CapsuleGuid
, &gWindowsUxCapsuleGuid
);
256 DisplayCapsule
->CapsuleHeader
.HeaderSize
= sizeof(DisplayCapsule
->CapsuleHeader
);
257 DisplayCapsule
->CapsuleHeader
.Flags
= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
258 DisplayCapsule
->CapsuleHeader
.CapsuleImageSize
= (UINT32
)FullCapsuleBufferSize
;
260 DisplayCapsule
->ImagePayload
.Version
= 1;
261 DisplayCapsule
->ImagePayload
.Checksum
= 0;
262 DisplayCapsule
->ImagePayload
.ImageType
= 0; // BMP
263 DisplayCapsule
->ImagePayload
.Reserved
= 0;
264 DisplayCapsule
->ImagePayload
.Mode
= Gop
->Mode
->Mode
;
267 // Center the bitmap horizontally
269 DisplayCapsule
->ImagePayload
.OffsetX
= (UINT32
)((Info
->HorizontalResolution
- Width
) / 2);
272 // Put bitmap 3/4 down the display. If bitmap is too tall, then align bottom
273 // of bitmap at bottom of display.
275 DisplayCapsule
->ImagePayload
.OffsetY
=
277 (UINT32
)(Info
->VerticalResolution
- Height
),
278 (UINT32
)(((3 * Info
->VerticalResolution
) - (2 * Height
)) / 4)
281 Print(L
"BMP image (%s), OffsetX - %d, OffsetY - %d\n",
283 DisplayCapsule
->ImagePayload
.OffsetX
,
284 DisplayCapsule
->ImagePayload
.OffsetY
287 CopyMem((DisplayCapsule
+ 1), BmpBuffer
, FileSize
);
289 DisplayCapsule
->ImagePayload
.Checksum
= CalculateCheckSum8(FullCapsuleBuffer
, FullCapsuleBufferSize
);
291 Status
= WriteFileFromBuffer(OutputCapsuleName
, FullCapsuleBufferSize
, FullCapsuleBuffer
);
292 Print(L
"CapsuleApp: Write %s %r\n", OutputCapsuleName
, Status
);
295 if (BmpBuffer
!= NULL
) {
299 if (FullCapsuleBuffer
!= NULL
) {
300 FreePool(FullCapsuleBuffer
);
307 Get ImageTypeId in the FMP capsule header.
309 @param[in] CapsuleHeader The FMP capsule image header.
314 GetCapsuleImageTypeId (
315 IN EFI_CAPSULE_HEADER
*CapsuleHeader
318 EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER
*FmpCapsuleHeader
;
319 UINT64
*ItemOffsetList
;
320 EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER
*ImageHeader
;
322 FmpCapsuleHeader
= (EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER
*)((UINT8
*)CapsuleHeader
+ CapsuleHeader
->HeaderSize
);
323 ItemOffsetList
= (UINT64
*)(FmpCapsuleHeader
+ 1);
324 if (FmpCapsuleHeader
->PayloadItemCount
== 0) {
327 ImageHeader
= (EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER
*)((UINT8
*)FmpCapsuleHeader
+ ItemOffsetList
[FmpCapsuleHeader
->EmbeddedDriverCount
]);
328 return &ImageHeader
->UpdateImageTypeId
;
332 Get ESRT FwType according to ImageTypeId
334 @param[in] ImageTypeId ImageTypeId of an FMP capsule.
340 IN EFI_GUID
*ImageTypeId
344 EFI_SYSTEM_RESOURCE_TABLE
*Esrt
;
345 EFI_SYSTEM_RESOURCE_ENTRY
*EsrtEntry
;
351 Status
= EfiGetSystemConfigurationTable(&gEfiSystemResourceTableGuid
, (VOID
**)&Esrt
);
352 if (!EFI_ERROR(Status
)) {
353 ASSERT(Esrt
!= NULL
);
354 EsrtEntry
= (VOID
*)(Esrt
+ 1);
355 for (Index
= 0; Index
< Esrt
->FwResourceCount
; Index
++, EsrtEntry
++) {
356 if (CompareGuid(&EsrtEntry
->FwClass
, ImageTypeId
)) {
357 return EsrtEntry
->FwType
;
362 return ESRT_FW_TYPE_UNKNOWN
;
366 Append a capsule header on top of current image.
367 This function follows Windows UEFI Firmware Update Platform document.
369 @retval EFI_SUCCESS The capsule header is appended.
370 @retval EFI_UNSUPPORTED Input parameter is not valid.
371 @retval EFI_OUT_OF_RESOURCES No enough resource to append capsule header.
378 CHAR16
*OutputCapsuleName
;
382 UINT8
*FullCapsuleBuffer
;
383 UINTN FullCapsuleBufferSize
;
384 EFI_CAPSULE_HEADER
*NestedCapsuleHeader
;
385 EFI_GUID
*ImageTypeId
;
390 Print(L
"CapsuleApp: Incorrect parameter count.\n");
391 return EFI_UNSUPPORTED
;
394 if (StrCmp(Argv
[3], L
"-O") != 0) {
395 Print(L
"CapsuleApp: NO output capsule name.\n");
396 return EFI_UNSUPPORTED
;
398 OutputCapsuleName
= Argv
[4];
400 CapsuleBuffer
= NULL
;
402 FullCapsuleBuffer
= NULL
;
404 CapsuleName
= Argv
[2];
405 Status
= ReadFileToBuffer(CapsuleName
, &FileSize
, &CapsuleBuffer
);
406 if (EFI_ERROR(Status
)) {
407 Print(L
"CapsuleApp: Capsule image (%s) is not found.\n", CapsuleName
);
411 ImageTypeId
= GetCapsuleImageTypeId(CapsuleBuffer
);
412 if (ImageTypeId
== NULL
) {
413 Print(L
"CapsuleApp: Capsule ImageTypeId is not found.\n");
416 FwType
= GetEsrtFwType(ImageTypeId
);
417 if ((FwType
!= ESRT_FW_TYPE_SYSTEMFIRMWARE
) && (FwType
!= ESRT_FW_TYPE_DEVICEFIRMWARE
)) {
418 Print(L
"CapsuleApp: Capsule FwType is invalid.\n");
422 FullCapsuleBufferSize
= NESTED_CAPSULE_HEADER_SIZE
+ FileSize
;
423 FullCapsuleBuffer
= AllocatePool(FullCapsuleBufferSize
);
424 if (FullCapsuleBuffer
== NULL
) {
425 Print(L
"CapsuleApp: Capsule Buffer size (0x%x) too big.\n", FullCapsuleBufferSize
);
426 Status
= EFI_OUT_OF_RESOURCES
;
430 NestedCapsuleHeader
= (EFI_CAPSULE_HEADER
*)FullCapsuleBuffer
;
431 ZeroMem(NestedCapsuleHeader
, NESTED_CAPSULE_HEADER_SIZE
);
432 CopyGuid(&NestedCapsuleHeader
->CapsuleGuid
, ImageTypeId
);
433 NestedCapsuleHeader
->HeaderSize
= NESTED_CAPSULE_HEADER_SIZE
;
434 NestedCapsuleHeader
->Flags
= (FwType
== ESRT_FW_TYPE_SYSTEMFIRMWARE
) ? SYSTEM_FIRMWARE_FLAG
: DEVICE_FIRMWARE_FLAG
;
435 NestedCapsuleHeader
->CapsuleImageSize
= (UINT32
)FullCapsuleBufferSize
;
437 CopyMem((UINT8
*)NestedCapsuleHeader
+ NestedCapsuleHeader
->HeaderSize
, CapsuleBuffer
, FileSize
);
439 Status
= WriteFileFromBuffer(OutputCapsuleName
, FullCapsuleBufferSize
, FullCapsuleBuffer
);
440 Print(L
"CapsuleApp: Write %s %r\n", OutputCapsuleName
, Status
);
443 if (CapsuleBuffer
!= NULL
) {
444 FreePool(CapsuleBuffer
);
447 if (FullCapsuleBuffer
!= NULL
) {
448 FreePool(FullCapsuleBuffer
);
456 Clear capsule status variable.
458 @retval EFI_SUCCESS The capsule status variable is cleared.
461 ClearCapsuleStatusVariable (
467 CHAR16 CapsuleVarName
[20];
470 StrCpyS (CapsuleVarName
, sizeof(CapsuleVarName
)/sizeof(CapsuleVarName
[0]), L
"Capsule");
471 TempVarName
= CapsuleVarName
+ StrLen (CapsuleVarName
);
475 UnicodeSPrint (TempVarName
, 5 * sizeof(CHAR16
), L
"%04x", Index
);
477 Status
= gRT
->SetVariable (
479 &gEfiCapsuleReportGuid
,
480 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
484 if (EFI_ERROR(Status
)) {
486 // There is no capsule variables, quit
492 if (Index
> 0xFFFF) {
501 Build Gather list for a list of capsule images.
503 @param[in] CapsuleBuffer An array of pointer to capsule images
504 @param[in] FileSize An array of UINTN to capsule images size
505 @param[in] CapsuleNum The count of capsule images
506 @param[out] BlockDescriptors The block descriptors for the capsule images
508 @retval EFI_SUCCESS The block descriptors for the capsule images are constructed.
512 IN VOID
**CapsuleBuffer
,
515 OUT EFI_CAPSULE_BLOCK_DESCRIPTOR
**BlockDescriptors
519 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptors1
;
520 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptors2
;
521 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptorPre
;
522 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptorsHeader
;
523 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockPtr
;
532 BlockDescriptors1
= NULL
;
533 BlockDescriptors2
= NULL
;
534 BlockDescriptorPre
= NULL
;
535 BlockDescriptorsHeader
= NULL
;
537 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
539 // Allocate memory for the descriptors.
541 if (NumberOfDescriptors
== 1) {
544 Count
= (INT32
)(NumberOfDescriptors
+ 2) / 2;
547 Size
= Count
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
548 BlockDescriptors1
= AllocateRuntimeZeroPool (Size
);
549 if (BlockDescriptors1
== NULL
) {
550 Print (L
"CapsuleApp: failed to allocate memory for descriptors\n");
551 Status
= EFI_OUT_OF_RESOURCES
;
554 Print (L
"CapsuleApp: creating capsule descriptors at 0x%X\n", (UINTN
) BlockDescriptors1
);
555 Print (L
"CapsuleApp: capsule data starts at 0x%X with size 0x%X\n", (UINTN
) CapsuleBuffer
[Index
], FileSize
[Index
]);
559 // Record descirptor header
562 BlockDescriptorsHeader
= BlockDescriptors1
;
565 if (BlockDescriptorPre
!= NULL
) {
566 BlockDescriptorPre
->Union
.ContinuationPointer
= (UINTN
) BlockDescriptors1
;
567 BlockDescriptorPre
->Length
= 0;
573 TempBlockPtr
= BlockDescriptors1
;
574 TempDataPtr
= CapsuleBuffer
[Index
];
575 SizeLeft
= FileSize
[Index
];
576 for (Number
= 0; (Number
< Count
- 1) && (SizeLeft
!= 0); Number
++) {
578 // Divide remaining data in half
580 if (NumberOfDescriptors
!= 1) {
589 TempBlockPtr
->Union
.DataBlock
= (UINTN
)TempDataPtr
;
590 TempBlockPtr
->Length
= Size
;
591 Print (L
"CapsuleApp: capsule block/size 0x%X/0x%X\n", (UINTN
) TempDataPtr
, Size
);
598 // Allocate the second list, point the first block's last entry to point
599 // to this one, and fill this one in. Worst case is that the previous
600 // list only had one element that pointed here, so we need at least two
601 // elements -- one to point to all the data, another to terminate the list.
603 if ((NumberOfDescriptors
!= 1) && (SizeLeft
!= 0)) {
604 Count
= (INT32
)(NumberOfDescriptors
+ 2) - Count
;
609 Size
= Count
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
610 BlockDescriptors2
= AllocateRuntimeZeroPool (Size
);
611 if (BlockDescriptors2
== NULL
) {
612 Print (L
"CapsuleApp: failed to allocate memory for descriptors\n");
613 Status
= EFI_OUT_OF_RESOURCES
;
618 // Point the first list's last element to point to this second list.
620 TempBlockPtr
->Union
.ContinuationPointer
= (UINTN
) BlockDescriptors2
;
622 TempBlockPtr
->Length
= 0;
623 TempBlockPtr
= BlockDescriptors2
;
624 for (Number
= 0; Number
< Count
- 1; Number
++) {
626 // If second-to-last one, then dump rest to this element
628 if (Number
== (Count
- 2)) {
632 // Divide remaining data in half
641 TempBlockPtr
->Union
.DataBlock
= (UINTN
)TempDataPtr
;
642 TempBlockPtr
->Length
= Size
;
643 Print (L
"CapsuleApp: capsule block/size 0x%X/0x%X\n", (UINTN
) TempDataPtr
, Size
);
653 BlockDescriptorPre
= TempBlockPtr
;
654 BlockDescriptors1
= NULL
;
660 if (TempBlockPtr
!= NULL
) {
661 TempBlockPtr
->Union
.ContinuationPointer
= (UINTN
)NULL
;
662 TempBlockPtr
->Length
= 0;
663 *BlockDescriptors
= BlockDescriptorsHeader
;
669 if (BlockDescriptors1
!= NULL
) {
670 FreePool(BlockDescriptors1
);
673 if (BlockDescriptors2
!= NULL
) {
674 FreePool(BlockDescriptors2
);
681 Clear the Gather list for a list of capsule images.
683 @param[in] BlockDescriptors The block descriptors for the capsule images
684 @param[in] CapsuleNum The count of capsule images
688 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptors
,
692 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockPtr
;
693 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockPtr1
;
694 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockPtr2
;
697 if (BlockDescriptors
!= NULL
) {
698 TempBlockPtr1
= BlockDescriptors
;
700 TempBlockPtr
= TempBlockPtr1
;
701 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
702 if (TempBlockPtr
[Index
].Length
== 0) {
707 if (TempBlockPtr
[Index
].Union
.ContinuationPointer
== (UINTN
)NULL
) {
711 TempBlockPtr2
= (VOID
*) ((UINTN
) TempBlockPtr
[Index
].Union
.ContinuationPointer
);
712 FreePool(TempBlockPtr1
);
713 TempBlockPtr1
= TempBlockPtr2
;
719 Validate if it is valid capsule header
721 This function assumes the caller provided correct CapsuleHeader pointer
724 This function validates the fields in EFI_CAPSULE_HEADER.
726 @param[in] CapsuleHeader Points to a capsule header.
727 @param[in] CapsuleSize Size of the whole capsule image.
731 IsValidCapsuleHeader (
732 IN EFI_CAPSULE_HEADER
*CapsuleHeader
,
733 IN UINT64 CapsuleSize
736 if (CapsuleSize
< sizeof (EFI_CAPSULE_HEADER
)) {
739 if (CapsuleHeader
->CapsuleImageSize
!= CapsuleSize
) {
742 if (CapsuleHeader
->HeaderSize
> CapsuleHeader
->CapsuleImageSize
) {
745 if (CapsuleHeader
->HeaderSize
< sizeof (EFI_CAPSULE_HEADER
)) {
760 Print(L
"CapsuleApp: usage\n");
761 Print(L
" CapsuleApp <Capsule...> [-NR]\n");
762 Print(L
" CapsuleApp -S\n");
763 Print(L
" CapsuleApp -C\n");
764 Print(L
" CapsuleApp -P\n");
765 Print(L
" CapsuleApp -E\n");
766 Print(L
" CapsuleApp -G <BMP> -O <Capsule>\n");
767 Print(L
" CapsuleApp -N <Capsule> -O <NestedCapsule>\n");
768 Print(L
" CapsuleApp -D <Capsule>\n");
769 Print(L
" CapsuleApp -P GET <ImageTypeId> <Index> -O <FileName>\n");
770 Print(L
"Parameter:\n");
771 Print(L
" -NR: No reset will be triggered for the capsule\n");
772 Print(L
" with CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without CAPSULE_FLAGS_INITIATE_RESET.\n");
773 Print(L
" -S: Dump capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
774 Print(L
" which is defined in UEFI specification.\n");
775 Print(L
" -C: Clear capsule report variable (EFI_CAPSULE_REPORT_GUID),\n");
776 Print(L
" which is defined in UEFI specification.\n");
777 Print(L
" -P: Dump UEFI FMP protocol info, or get image with specified\n");
778 Print(L
" ImageTypeId and index to a file if 'GET' option is used.\n");
779 Print(L
" -E: Dump UEFI ESRT table info.\n");
780 Print(L
" -G: Convert a BMP file to be an UX capsule,\n");
781 Print(L
" according to Windows Firmware Update document\n");
782 Print(L
" -N: Append a Capsule Header to an existing capsule image,\n");
783 Print(L
" according to Windows Firmware Update document\n");
784 Print(L
" -O: Output new Capsule file name\n");
785 Print(L
" -D: Dump Capsule image header information, image payload information if it is an UX capsule\n");
786 Print(L
" and FMP header information if it is a FMP capsule.\n");
790 Update Capsule image.
792 @param[in] ImageHandle The image handle.
793 @param[in] SystemTable The system table.
795 @retval EFI_SUCCESS Command completed successfully.
796 @retval EFI_UNSUPPORTED Command usage unsupported.
797 @retval EFI_INVALID_PARAMETER Command usage invalid.
798 @retval EFI_NOT_FOUND The input file can't be found.
803 IN EFI_HANDLE ImageHandle
,
804 IN EFI_SYSTEM_TABLE
*SystemTable
808 RETURN_STATUS RStatus
;
809 UINTN FileSize
[MAX_CAPSULE_NUM
];
810 VOID
*CapsuleBuffer
[MAX_CAPSULE_NUM
];
811 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockDescriptors
;
812 EFI_CAPSULE_HEADER
*CapsuleHeaderArray
[MAX_CAPSULE_NUM
+ 1];
813 UINT64 MaxCapsuleSize
;
814 EFI_RESET_TYPE ResetType
;
822 if (EFI_ERROR(Status
)) {
823 Print(L
"Please use UEFI SHELL to run this application!\n", Status
);
828 return EFI_UNSUPPORTED
;
830 if (StrCmp(Argv
[1], L
"-D") == 0) {
832 Print(L
"CapsuleApp: Incorrect parameter count.\n");
833 return EFI_UNSUPPORTED
;
835 Status
= DumpCapsule(Argv
[2]);
838 if (StrCmp(Argv
[1], L
"-G") == 0) {
839 Status
= CreateBmpFmp();
842 if (StrCmp(Argv
[1], L
"-N") == 0) {
843 Status
= CreateNestedFmp();
846 if (StrCmp(Argv
[1], L
"-S") == 0) {
847 Status
= DmpCapsuleStatusVariable();
850 if (StrCmp(Argv
[1], L
"-C") == 0) {
851 Status
= ClearCapsuleStatusVariable();
854 if (StrCmp(Argv
[1], L
"-P") == 0) {
859 if (StrCmp(Argv
[2], L
"GET") != 0) {
860 Print(L
"CapsuleApp: Unrecognized option(%s).\n", Argv
[2]);
861 return EFI_UNSUPPORTED
;
864 Print(L
"CapsuleApp: Incorrect parameter count.\n");
865 return EFI_UNSUPPORTED
;
868 EFI_GUID ImageTypeId
;
873 RStatus
= StrToGuid (Argv
[3], &ImageTypeId
);
874 if (RETURN_ERROR (RStatus
) || (Argv
[3][GUID_STRING_LENGTH
] != L
'\0')) {
875 Print (L
"Invalid ImageTypeId - %s\n", Argv
[3]);
876 return EFI_INVALID_PARAMETER
;
878 ImageIndex
= StrDecimalToUintn(Argv
[4]);
879 if (StrCmp(Argv
[5], L
"-O") != 0) {
880 Print(L
"CapsuleApp: NO output file name.\n");
881 return EFI_UNSUPPORTED
;
883 DumpFmpImage(&ImageTypeId
, ImageIndex
, Argv
[6]);
889 if (StrCmp(Argv
[1], L
"-E") == 0) {
894 if (Argv
[1][0] == L
'-') {
895 Print(L
"CapsuleApp: Unrecognized option(%s).\n", Argv
[1]);
896 return EFI_UNSUPPORTED
;
899 CapsuleFirstIndex
= 1;
901 if ((Argc
> 1) && (StrCmp(Argv
[Argc
- 1], L
"-NR") == 0)) {
903 CapsuleLastIndex
= Argc
- 2;
905 CapsuleLastIndex
= Argc
- 1;
907 CapsuleNum
= CapsuleLastIndex
- CapsuleFirstIndex
+ 1;
909 if (CapsuleFirstIndex
> CapsuleLastIndex
) {
910 Print(L
"CapsuleApp: NO capsule image.\n");
911 return EFI_UNSUPPORTED
;
913 if (CapsuleNum
> MAX_CAPSULE_NUM
) {
914 Print(L
"CapsuleApp: Too many capsule images.\n");
915 return EFI_UNSUPPORTED
;
918 ZeroMem(&CapsuleBuffer
, sizeof(CapsuleBuffer
));
919 ZeroMem(&FileSize
, sizeof(FileSize
));
920 BlockDescriptors
= NULL
;
922 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
923 CapsuleName
= Argv
[CapsuleFirstIndex
+ Index
];
924 Status
= ReadFileToBuffer(CapsuleName
, &FileSize
[Index
], &CapsuleBuffer
[Index
]);
925 if (EFI_ERROR(Status
)) {
926 Print(L
"CapsuleApp: capsule image (%s) is not found.\n", CapsuleName
);
929 if (!IsValidCapsuleHeader (CapsuleBuffer
[Index
], FileSize
[Index
])) {
930 Print(L
"CapsuleApp: Capsule image (%s) is not a valid capsule.\n", CapsuleName
);
931 return EFI_INVALID_PARAMETER
;
936 // Every capsule use 2 descriptor 1 for data 1 for end
938 Status
= BuildGatherList(CapsuleBuffer
, FileSize
, CapsuleNum
, &BlockDescriptors
);
939 if (EFI_ERROR(Status
)) {
944 // Call the runtime service capsule.
947 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
948 CapsuleHeaderArray
[Index
] = (EFI_CAPSULE_HEADER
*) CapsuleBuffer
[Index
];
949 if ((CapsuleHeaderArray
[Index
]->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) != 0) {
953 CapsuleHeaderArray
[CapsuleNum
] = NULL
;
956 // Inquire platform capability of UpdateCapsule.
958 Status
= gRT
->QueryCapsuleCapabilities (CapsuleHeaderArray
, CapsuleNum
, &MaxCapsuleSize
, &ResetType
);
959 if (EFI_ERROR(Status
)) {
960 Print (L
"CapsuleApp: failed to query capsule capability - %r\n", Status
);
964 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
965 if (FileSize
[Index
] > MaxCapsuleSize
) {
966 Print (L
"CapsuleApp: capsule is too large to update, %ld is allowed\n", MaxCapsuleSize
);
967 Status
= EFI_UNSUPPORTED
;
973 // Check whether the input capsule image has the flag of persist across system reset.
976 Status
= gRT
->UpdateCapsule(CapsuleHeaderArray
,CapsuleNum
,(UINTN
) BlockDescriptors
);
977 if (Status
!= EFI_SUCCESS
) {
978 Print (L
"CapsuleApp: failed to update capsule - %r\n", Status
);
982 // For capsule with CAPSULE_FLAGS_PERSIST_ACROSS_RESET + CAPSULE_FLAGS_INITIATE_RESET,
983 // a system reset should have been triggered by gRT->UpdateCapsule() calling above.
985 // For capsule with CAPSULE_FLAGS_PERSIST_ACROSS_RESET and without CAPSULE_FLAGS_INITIATE_RESET,
986 // check if -NR (no-reset) has been specified or not.
990 // For capsule who has reset flag and no -NR (no-reset) has been specified, after calling UpdateCapsule service,
991 // trigger a system reset to process capsule persist across a system reset.
993 gRT
->ResetSystem (ResetType
, EFI_SUCCESS
, 0, NULL
);
997 // For capsule who has no reset flag, only call UpdateCapsule Service without a
998 // system reset. The service will process the capsule immediately.
1000 Status
= gRT
->UpdateCapsule (CapsuleHeaderArray
,CapsuleNum
,(UINTN
) BlockDescriptors
);
1001 if (Status
!= EFI_SUCCESS
) {
1002 Print (L
"CapsuleApp: failed to update capsule - %r\n", Status
);
1006 Status
= EFI_SUCCESS
;
1009 for (Index
= 0; Index
< CapsuleNum
; Index
++) {
1010 if (CapsuleBuffer
[Index
] != NULL
) {
1011 FreePool (CapsuleBuffer
[Index
]);
1015 CleanGatherList(BlockDescriptors
, CapsuleNum
);