2 Capsule update PEIM for UEFI2.0
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 EFI_PHYSICAL_ADDRESS
*mBufferAddress
;
22 Check every capsule header.
24 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
26 @retval FALSE Capsule is OK
27 @retval TRUE Capsule is corrupted
32 IN EFI_CAPSULE_HEADER
*CapsuleHeader
36 //A capsule to be updated across a system reset should contain CAPSULE_FLAGS_PERSIST_ACROSS_RESET.
38 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) == 0) {
42 //Make sure the flags combination is supported by the platform.
44 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
47 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
55 Check the integrity of the capsule descriptors.
57 @param BlockList Pointer to the capsule descriptors
59 @retval NULL BlockList is not valid.
60 @retval LastBlockDesc Last one Block in BlockList
63 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
64 ValidateCapsuleIntegrity (
65 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
68 EFI_CAPSULE_HEADER
*CapsuleHeader
;
71 EFI_CAPSULE_BLOCK_DESCRIPTOR
*Ptr
;
74 // Go through the list to look for inconsistencies. Check for:
75 // * misaligned block descriptors.
76 // * The first capsule header guid
77 // * The first capsule header flag
78 // * Data + Length < Data (wrap)
82 while ((Ptr
->Length
!= 0) || (Ptr
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
84 // Make sure the descriptor is aligned at UINT64 in memory
86 if ((UINTN
) Ptr
& 0x07) {
87 DEBUG ((EFI_D_ERROR
, "BlockList address failed alignment check\n"));
91 if (Ptr
->Length
== 0) {
93 // Descriptor points to another list of block descriptors somewhere
96 Ptr
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Ptr
->Union
.ContinuationPointer
;
99 //To enhance the reliability of check-up, the first capsule's header is checked here.
100 //More reliabilities check-up will do later.
102 if (CapsuleSize
== 0) {
104 //Move to the first capsule to check its header.
106 CapsuleHeader
= (EFI_CAPSULE_HEADER
*)((UINTN
)Ptr
->Union
.DataBlock
);
107 if (IsCapsuleCorrupted (CapsuleHeader
)) {
111 CapsuleSize
= CapsuleHeader
->CapsuleImageSize
;
113 if (CapsuleSize
>= Ptr
->Length
) {
114 CapsuleSize
= CapsuleSize
- Ptr
->Length
;
120 // Move to next BLOCK descriptor
126 if (CapsuleCount
== 0) {
128 // No any capsule is found in BlockList.
138 Checks for the presence of capsule descriptors.
139 Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
141 @param BlockList Pointer to the capsule descriptors
143 @retval EFI_SUCCESS a valid capsule is present
144 @retval EFI_NOT_FOUND if a valid capsule is not present
147 GetCapsuleDescriptors (
148 IN OUT EFI_CAPSULE_BLOCK_DESCRIPTOR
**BlockList OPTIONAL
157 CHAR16 CapsuleVarName
[30];
159 EFI_PHYSICAL_ADDRESS CapsuleDataPtr64
;
160 EFI_CAPSULE_BLOCK_DESCRIPTOR
*LastBlock
;
161 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlock
;
162 EFI_CAPSULE_BLOCK_DESCRIPTOR
*HeadBlock
;
163 EFI_PEI_READ_ONLY_VARIABLE2_PPI
*PPIVariableServices
;
170 CapsuleVarName
[0] = 0;
173 Status
= PeiServicesLocatePpi (
174 &gEfiPeiReadOnlyVariable2PpiGuid
,
177 (VOID
**) &PPIVariableServices
179 if (Status
== EFI_SUCCESS
) {
180 StrCpy (CapsuleVarName
, EFI_CAPSULE_VARIABLE_NAME
);
181 TempVarName
= CapsuleVarName
+ StrLen (CapsuleVarName
);
182 Size
= sizeof (CapsuleDataPtr64
);
186 // For the first Capsule Image
188 Status
= PPIVariableServices
->GetVariable (
191 &gEfiCapsuleVendorGuid
,
194 (VOID
*) &CapsuleDataPtr64
196 if (EFI_ERROR (Status
)) {
197 DEBUG ((EFI_D_ERROR
, "Capsule -- capsule variable not set\n"));
198 return EFI_NOT_FOUND
;
201 // We have a chicken/egg situation where the memory init code needs to
202 // know the boot mode prior to initializing memory. For this case, our
203 // validate function will fail. We can detect if this is the case if blocklist
204 // pointer is null. In that case, return success since we know that the
207 if (BlockList
== NULL
) {
211 // Test integrity of descriptors.
213 LastBlock
= ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR
*)(UINTN
)CapsuleDataPtr64
);
214 if (LastBlock
== NULL
) {
215 return EFI_NOT_FOUND
;
218 // Return the base of the block descriptors
220 HeadBlock
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*)(UINTN
)CapsuleDataPtr64
;
222 UnicodeValueToString (TempVarName
, 0, Index
, 0);
223 Status
= PPIVariableServices
->GetVariable (
226 &gEfiCapsuleVendorGuid
,
229 (VOID
*) &CapsuleDataPtr64
231 if (EFI_ERROR (Status
)) {
236 // If this BlockList has been linked before, skip this variable
239 for (TempIndex
= 0; TempIndex
< ValidIndex
; TempIndex
++) {
240 if (mBufferAddress
[TempIndex
] == CapsuleDataPtr64
) {
251 // Test integrity of descriptors.
253 TempBlock
= ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR
*)(UINTN
)CapsuleDataPtr64
);
254 if (TempBlock
== NULL
) {
255 return EFI_NOT_FOUND
;
258 // Combine the different BlockList into single BlockList.
260 LastBlock
->Union
.DataBlock
= CapsuleDataPtr64
;
261 LastBlock
->Length
= 0;
262 LastBlock
= TempBlock
;
266 // Cache BlockList which has been processed
268 mBufferAddress
[ValidIndex
++] = CapsuleDataPtr64
;
273 if (HeadBlock
!= NULL
) {
274 *BlockList
= HeadBlock
;
277 return EFI_NOT_FOUND
;
281 Given a pointer to a capsule block descriptor, traverse the list to figure
282 out how many legitimate descriptors there are, and how big the capsule it
285 @param Desc Pointer to the capsule block descriptors
286 NumDescriptors - optional pointer to where to return the number of descriptors
287 CapsuleSize - optional pointer to where to return the capsule size
288 @param NumDescriptors Optional pointer to where to return the number of descriptors
289 @param CapsuleSize Optional pointer to where to return the capsule size
291 @retval EFI_NOT_FOUND No descriptors containing data in the list
292 @retval EFI_SUCCESS Return data is valid
296 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*Desc
,
297 IN OUT UINTN
*NumDescriptors OPTIONAL
,
298 IN OUT UINTN
*CapsuleSize OPTIONAL
304 ASSERT (Desc
!= NULL
);
309 while (Desc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
310 if (Desc
->Length
== 0) {
312 // Descriptor points to another list of block descriptors somewhere
314 Desc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Desc
->Union
.ContinuationPointer
;
316 Size
+= (UINTN
) Desc
->Length
;
322 // If no descriptors, then fail
325 return EFI_NOT_FOUND
;
328 if (NumDescriptors
!= NULL
) {
329 *NumDescriptors
= Count
;
332 if (CapsuleSize
!= NULL
) {
341 Try to verify the integrity of a capsule test pattern before the
342 capsule gets coalesced. This can be useful in narrowing down
343 where capsule data corruption occurs.
345 The test pattern mode fills in memory with a counting UINT32 value.
346 If the capsule is not divided up in a multiple of 4-byte blocks, then
347 things get messy doing the check. Therefore there are some cases
348 here where we just give up and skip the pre-coalesce check.
350 @param PeiServices PEI services table
351 @param Desc Pointer to capsule descriptors
354 CapsuleTestPatternPreCoalesce (
355 IN EFI_PEI_SERVICES
**PeiServices
,
356 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*Desc
363 // Find first data descriptor
365 while ((Desc
->Length
== 0) && (Desc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
366 Desc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Desc
->Union
.ContinuationPointer
;
369 if (Desc
->Union
.ContinuationPointer
== 0) {
373 // First one better be long enough to at least hold the test signature
375 if (Desc
->Length
< sizeof (UINT32
)) {
376 DEBUG ((EFI_D_INFO
, "Capsule test pattern pre-coalesce punted #1\n"));
380 TestPtr
= (UINT32
*) (UINTN
) Desc
->Union
.DataBlock
;
381 if (*TestPtr
!= CAPSULE_TEST_SIGNATURE
) {
386 TestSize
= (UINT32
) Desc
->Length
- 2 * sizeof (UINT32
);
388 // Skip over the signature and the size fields in the pattern data header
392 if ((TestSize
& 0x03) != 0) {
393 DEBUG ((EFI_D_INFO
, "Capsule test pattern pre-coalesce punted #2\n"));
397 while (TestSize
> 0) {
398 if (*TestPtr
!= TestCounter
) {
399 DEBUG ((EFI_D_INFO
, "Capsule test pattern pre-coalesce failed data corruption check\n"));
403 TestSize
-= sizeof (UINT32
);
408 while ((Desc
->Length
== 0) && (Desc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
409 Desc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Desc
->Union
.ContinuationPointer
;
412 if (Desc
->Union
.ContinuationPointer
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
415 TestSize
= (UINT32
) Desc
->Length
;
416 TestPtr
= (UINT32
*) (UINTN
) Desc
->Union
.DataBlock
;
422 Determine if two buffers overlap in memory.
424 @param Buff1 pointer to first buffer
425 @param Size1 size of Buff1
426 @param Buff2 pointer to second buffer
427 @param Size2 size of Buff2
429 @retval TRUE Buffers overlap in memory.
430 @retval FALSE Buffer doesn't overlap.
441 // If buff1's end is less than the start of buff2, then it's ok.
442 // Also, if buff1's start is beyond buff2's end, then it's ok.
444 if (((Buff1
+ Size1
) <= Buff2
) || (Buff1
>= (Buff2
+ Size2
))) {
452 Given a pointer to the capsule block list, info on the available system
453 memory, and the size of a buffer, find a free block of memory where a
454 buffer of the given size can be copied to safely.
456 @param BlockList Pointer to head of capsule block descriptors
457 @param MemBase Pointer to the base of memory in which we want to find free space
458 @param MemSize The size of the block of memory pointed to by MemBase
459 @param DataSize How big a free block we want to find
461 @return A pointer to a memory block of at least DataSize that lies somewhere
462 between MemBase and (MemBase + MemSize). The memory pointed to does not
463 contain any of the capsule block descriptors or capsule blocks pointed to
468 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
475 EFI_CAPSULE_BLOCK_DESCRIPTOR
*CurrDesc
;
476 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempDesc
;
481 // Need at least enough to copy the data to at the end of the buffer, so
482 // say the end is less the data size for easy comparisons here.
484 MemEnd
= MemBase
+ MemSize
- DataSize
;
485 CurrDesc
= BlockList
;
487 // Go through all the descriptor blocks and see if any obstruct the range
489 while (CurrDesc
!= NULL
) {
491 // Get the size of this block list and see if it's in the way
495 Size
= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
496 while (TempDesc
->Length
!= 0) {
497 Size
+= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
501 if (IsOverlapped (MemBase
, DataSize
, (UINT8
*) CurrDesc
, Size
)) {
503 // Set our new base to the end of this block list and start all over
505 MemBase
= (UINT8
*) CurrDesc
+ Size
;
506 CurrDesc
= BlockList
;
507 if (MemBase
> MemEnd
) {
514 // Now go through all the blocks and make sure none are in the way
516 while ((CurrDesc
->Length
!= 0) && (!Failed
)) {
517 if (IsOverlapped (MemBase
, DataSize
, (UINT8
*) (UINTN
) CurrDesc
->Union
.DataBlock
, (UINTN
) CurrDesc
->Length
)) {
519 // Set our new base to the end of this block and start all over
522 MemBase
= (UINT8
*) ((UINTN
) CurrDesc
->Union
.DataBlock
) + CurrDesc
->Length
;
523 CurrDesc
= BlockList
;
524 if (MemBase
> MemEnd
) {
531 // Normal continuation -- jump to next block descriptor list
534 CurrDesc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) CurrDesc
->Union
.ContinuationPointer
;
541 The capsule block descriptors may be fragmented and spread all over memory.
542 To simplify the coalescing of capsule blocks, first coalesce all the
543 capsule block descriptors low in memory.
545 The descriptors passed in can be fragmented throughout memory. Here
546 they are relocated into memory to turn them into a contiguous (null
549 @param PeiServices pointer to PEI services table
550 @param BlockList pointer to the capsule block descriptors
551 @param MemBase base of system memory in which we can work
552 @param MemSize size of the system memory pointed to by MemBase
554 @retval NULL could not relocate the descriptors
555 @retval Pointer to the base of the successfully-relocated block descriptors.
557 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
558 RelocateBlockDescriptors (
559 IN EFI_PEI_SERVICES
**PeiServices
,
560 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
565 EFI_CAPSULE_BLOCK_DESCRIPTOR
*NewBlockList
;
566 EFI_CAPSULE_BLOCK_DESCRIPTOR
*CurrBlockDescHead
;
567 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockDesc
;
568 EFI_CAPSULE_BLOCK_DESCRIPTOR
*PrevBlockDescTail
;
569 UINTN NumDescriptors
;
574 // Get the info on the blocks and descriptors. Since we're going to move
575 // the descriptors low in memory, adjust the base/size values accordingly here.
576 // GetCapsuleInfo() returns the number of legit descriptors, so add one for
579 if (GetCapsuleInfo (BlockList
, &NumDescriptors
, NULL
) != EFI_SUCCESS
) {
584 BufferSize
= NumDescriptors
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
585 NewBlockList
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) MemBase
;
586 if (MemSize
< BufferSize
) {
590 MemSize
-= BufferSize
;
591 MemBase
+= BufferSize
;
593 // Go through all the blocks and make sure none are in the way
595 TempBlockDesc
= BlockList
;
596 while (TempBlockDesc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
597 if (TempBlockDesc
->Length
== 0) {
599 // Next block of descriptors
601 TempBlockDesc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) TempBlockDesc
->Union
.ContinuationPointer
;
604 // If the capsule data pointed to by this descriptor is in the way,
608 (UINT8
*) NewBlockList
,
610 (UINT8
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
,
611 (UINTN
) TempBlockDesc
->Length
614 // Relocate the block
616 RelocBuffer
= FindFreeMem (BlockList
, MemBase
, MemSize
, (UINTN
) TempBlockDesc
->Length
);
617 if (RelocBuffer
== NULL
) {
621 CopyMem ((VOID
*) RelocBuffer
, (VOID
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
, (UINTN
) TempBlockDesc
->Length
);
622 TempBlockDesc
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RelocBuffer
;
624 DEBUG ((EFI_D_INFO
, "Capsule relocate descriptors from/to/size 0x%X 0x%X 0x%X\n", (UINT32
)(UINTN
)TempBlockDesc
->Union
.DataBlock
, (UINT32
)(UINTN
)RelocBuffer
, (UINT32
)(UINTN
)TempBlockDesc
->Length
));
630 // Now go through all the block descriptors to make sure that they're not
631 // in the memory region we want to copy them to.
633 CurrBlockDescHead
= BlockList
;
634 PrevBlockDescTail
= NULL
;
635 while ((CurrBlockDescHead
!= NULL
) && (CurrBlockDescHead
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
637 // Get the size of this list then see if it overlaps our low region
639 TempBlockDesc
= CurrBlockDescHead
;
640 BlockListSize
= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
641 while (TempBlockDesc
->Length
!= 0) {
642 BlockListSize
+= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
647 (UINT8
*) NewBlockList
,
649 (UINT8
*) CurrBlockDescHead
,
653 // Overlaps, so move it out of the way
655 RelocBuffer
= FindFreeMem (BlockList
, MemBase
, MemSize
, BlockListSize
);
656 if (RelocBuffer
== NULL
) {
659 CopyMem ((VOID
*) RelocBuffer
, (VOID
*) CurrBlockDescHead
, BlockListSize
);
660 DEBUG ((EFI_D_INFO
, "Capsule reloc descriptor block #2\n"));
662 // Point the previous block's next point to this copied version. If
663 // the tail pointer is null, then this is the first descriptor block.
665 if (PrevBlockDescTail
== NULL
) {
666 BlockList
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) RelocBuffer
;
668 PrevBlockDescTail
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RelocBuffer
;
672 // Save our new tail and jump to the next block list
674 PrevBlockDescTail
= TempBlockDesc
;
675 CurrBlockDescHead
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) TempBlockDesc
->Union
.ContinuationPointer
;
678 // Cleared out low memory. Now copy the descriptors down there.
680 TempBlockDesc
= BlockList
;
681 CurrBlockDescHead
= NewBlockList
;
682 while ((TempBlockDesc
!= NULL
) && (TempBlockDesc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
683 if (TempBlockDesc
->Length
!= 0) {
684 CurrBlockDescHead
->Union
.DataBlock
= TempBlockDesc
->Union
.DataBlock
;
685 CurrBlockDescHead
->Length
= TempBlockDesc
->Length
;
689 TempBlockDesc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) TempBlockDesc
->Union
.ContinuationPointer
;
695 CurrBlockDescHead
->Union
.ContinuationPointer
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
;
696 CurrBlockDescHead
->Length
= 0;
701 Capsule PPI service to coalesce a fragmented capsule in memory.
703 Memory Map for coalesced capsule:
704 MemBase + ---->+---------------------------+<-----------+
705 MemSize | CapsuleOffset[49] | |
706 +---------------------------+ |
707 | ................ | |
708 +---------------------------+ |
709 | CapsuleOffset[2] | |
710 +---------------------------+ |
711 | CapsuleOffset[1] | |
712 +---------------------------+ |
713 | CapsuleOffset[0] | CapsuleSize
714 +---------------------------+ |
716 +---------------------------+ |
722 +---------------------------+ |
724 DestPtr ----> +---------------------------+<-----------+
726 | FreeMem | FreeMemSize
728 FreeMemBase --->+---------------------------+<-----------+
730 +---------------------------+
731 | BlockDescriptor n |
732 +---------------------------+
733 | ................. |
734 +---------------------------+
735 | BlockDescriptor 1 |
736 +---------------------------+
737 | BlockDescriptor 0 |
738 +---------------------------+
739 | PrivateDataDesc 0 |
740 MemBase ---->+---------------------------+<----- BlockList
743 @param PeiServices General purpose services available to every PEIM.
744 @param MemoryBase Pointer to the base of a block of memory that we can walk
745 all over while trying to coalesce our buffers.
746 On output, this variable will hold the base address of
748 @param MemorySize Size of the memory region pointed to by MemoryBase.
749 On output, this variable will contain the size of the
752 @retval EFI_NOT_FOUND if we can't determine the boot mode
753 if the boot mode is not flash-update
754 if we could not find the capsule descriptors
756 @retval EFI_BUFFER_TOO_SMALL
757 if we could not coalesce the capsule in the memory
758 region provided to us
760 @retval EFI_SUCCESS if there's no capsule, or if we processed the
761 capsule successfully.
766 IN EFI_PEI_SERVICES
**PeiServices
,
767 IN OUT VOID
**MemoryBase
,
768 IN OUT UINTN
*MemorySize
771 VOID
*NewCapsuleBase
;
777 UINT32 CapsuleOffset
[MAX_SUPPORT_CAPSULE_NUM
];
781 UINT64 CapsuleImageSize
;
783 UINTN DescriptorsSize
;
785 UINTN NumDescriptors
;
789 CHAR16 CapsuleVarName
[30];
791 EFI_PHYSICAL_ADDRESS CapsuleDataPtr64
;
793 BOOLEAN CapsuleBeginFlag
;
795 EFI_BOOT_MODE BootMode
;
796 EFI_CAPSULE_HEADER
*CapsuleHeader
;
797 EFI_CAPSULE_PEIM_PRIVATE_DATA PrivateData
;
798 EFI_CAPSULE_PEIM_PRIVATE_DATA
*PrivateDataPtr
;
799 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
;
800 EFI_CAPSULE_BLOCK_DESCRIPTOR
*CurrentBlockDesc
;
801 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockDesc
;
802 EFI_CAPSULE_BLOCK_DESCRIPTOR PrivateDataDesc
[2];
803 EFI_PEI_READ_ONLY_VARIABLE2_PPI
*PPIVariableServices
;
808 CapsuleImageSize
= 0;
809 PrivateDataPtr
= NULL
;
811 CapsuleHeader
= NULL
;
812 CapsuleBeginFlag
= TRUE
;
818 CapsuleVarName
[0] = 0;
821 // Someone should have already ascertained the boot mode. If it's not
822 // capsule update, then return normally.
824 Status
= PeiServicesGetBootMode (&BootMode
);
825 if (EFI_ERROR (Status
) || (BootMode
!= BOOT_ON_FLASH_UPDATE
)) {
826 return EFI_NOT_FOUND
;
830 // User may set the same ScatterGatherList with several different variables,
831 // so cache all ScatterGatherList for check later.
833 Status
= PeiServicesLocatePpi (
834 &gEfiPeiReadOnlyVariable2PpiGuid
,
837 (VOID
**) &PPIVariableServices
839 if (EFI_ERROR (Status
)) {
842 Size
= sizeof (CapsuleDataPtr64
);
843 StrCpy (CapsuleVarName
, EFI_CAPSULE_VARIABLE_NAME
);
844 TempVarName
= CapsuleVarName
+ StrLen (CapsuleVarName
);
847 UnicodeValueToString (TempVarName
, 0, Index
, 0);
849 Status
= PPIVariableServices
->GetVariable (
852 &gEfiCapsuleVendorGuid
,
855 (VOID
*) &CapsuleDataPtr64
857 if (EFI_ERROR (Status
)) {
859 // There is no capsule variables, quit
861 DEBUG ((EFI_D_ERROR
,"Capsule variable Index = %d\n", Index
));
868 DEBUG ((EFI_D_ERROR
,"Capsule variable count = %d\n", VariableCount
));
870 Status
= PeiServicesAllocatePool (
871 VariableCount
* sizeof (EFI_PHYSICAL_ADDRESS
),
872 (VOID
**)&mBufferAddress
875 if (Status
!= EFI_SUCCESS
) {
876 DEBUG ((EFI_D_ERROR
, "AllocatePages Failed!, Status = %x\n", Status
));
881 // Find out if we actually have a capsule.
883 Status
= GetCapsuleDescriptors (&BlockList
);
884 if (EFI_ERROR (Status
)) {
889 CapsuleTestPatternPreCoalesce (PeiServices
, BlockList
);
893 // Get the size of our descriptors and the capsule size. GetCapsuleInfo()
894 // returns the number of descriptors that actually point to data, so add
895 // one for a terminator. Do that below.
897 GetCapsuleInfo (BlockList
, &NumDescriptors
, &CapsuleSize
);
898 if ((CapsuleSize
== 0) || (NumDescriptors
== 0)) {
899 return EFI_NOT_FOUND
;
903 // Initialize our local copy of private data. When we're done, we'll create a
904 // descriptor for it as well so that it can be put into free memory without
905 // trashing anything.
907 PrivateData
.Signature
= EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE
;
908 PrivateData
.CapsuleSize
= CapsuleSize
;
909 PrivateDataDesc
[0].Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) &PrivateData
;
910 PrivateDataDesc
[0].Length
= sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA
);
911 PrivateDataDesc
[1].Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) BlockList
;
912 PrivateDataDesc
[1].Length
= 0;
914 // In addition to PrivateDataDesc[1:0], one terminator is added
915 // See below RelocateBlockDescriptors()
918 CapsuleSize
+= sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA
) + sizeof(CapsuleOffset
) + sizeof(UINT32
);
919 BlockList
= PrivateDataDesc
;
920 DescriptorsSize
= NumDescriptors
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
923 // Don't go below some min address. If the base is below it,
924 // then move it up and adjust the size accordingly.
926 DEBUG ((EFI_D_INFO
, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN
) *MemoryBase
, (UINTN
)*MemoryBase
+ *MemorySize
));
927 if ((UINTN
)*MemoryBase
< (UINTN
) MIN_COALESCE_ADDR
) {
928 if (((UINTN
)*MemoryBase
+ *MemorySize
) < (UINTN
) MIN_COALESCE_ADDR
) {
929 return EFI_BUFFER_TOO_SMALL
;
931 *MemorySize
= *MemorySize
- ((UINTN
) MIN_COALESCE_ADDR
- (UINTN
) *MemoryBase
);
932 *MemoryBase
= (VOID
*) (UINTN
) MIN_COALESCE_ADDR
;
936 if (*MemorySize
<= (CapsuleSize
+ DescriptorsSize
)) {
937 return EFI_BUFFER_TOO_SMALL
;
940 FreeMemBase
= *MemoryBase
;
941 FreeMemSize
= *MemorySize
;
942 DEBUG ((EFI_D_INFO
, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN
) FreeMemBase
, (UINTN
) FreeMemBase
+ FreeMemSize
));
945 // Relocate all the block descriptors to low memory to make further
946 // processing easier.
948 BlockList
= RelocateBlockDescriptors (PeiServices
, BlockList
, FreeMemBase
, FreeMemSize
);
949 if (BlockList
== NULL
) {
951 // Not enough room to relocate the descriptors
953 return EFI_BUFFER_TOO_SMALL
;
957 // Take the top of memory for the capsule. Naturally align.
959 DestPtr
= FreeMemBase
+ FreeMemSize
- CapsuleSize
;
960 DestPtr
= (UINT8
*) ((UINTN
) DestPtr
&~ (UINTN
) (sizeof (UINTN
) - 1));
961 FreeMemBase
= (UINT8
*) BlockList
+ DescriptorsSize
;
962 FreeMemSize
= FreeMemSize
- DescriptorsSize
- CapsuleSize
;
963 NewCapsuleBase
= (VOID
*) DestPtr
;
966 // Move all the blocks to the top (high) of memory.
967 // Relocate all the obstructing blocks. Note that the block descriptors
968 // were coalesced when they were relocated, so we can just ++ the pointer.
970 CurrentBlockDesc
= BlockList
;
971 while ((CurrentBlockDesc
->Length
!= 0) || (CurrentBlockDesc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
973 // See if any of the remaining capsule blocks are in the way
975 TempBlockDesc
= CurrentBlockDesc
;
976 while (TempBlockDesc
->Length
!= 0) {
978 // Is this block in the way of where we want to copy the current descriptor to?
982 (UINTN
) CurrentBlockDesc
->Length
,
983 (UINT8
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
,
984 (UINTN
) TempBlockDesc
->Length
987 // Relocate the block
989 RelocPtr
= FindFreeMem (BlockList
, FreeMemBase
, FreeMemSize
, (UINTN
) TempBlockDesc
->Length
);
990 if (RelocPtr
== NULL
) {
991 return EFI_BUFFER_TOO_SMALL
;
994 CopyMem ((VOID
*) RelocPtr
, (VOID
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
, (UINTN
) TempBlockDesc
->Length
);
995 DEBUG ((EFI_D_INFO
, "Capsule reloc data block from 0x%8X to 0x%8X with size 0x%8X\n",
996 (UINTN
) TempBlockDesc
->Union
.DataBlock
, (UINTN
) RelocPtr
, (UINTN
) TempBlockDesc
->Length
));
998 TempBlockDesc
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RelocPtr
;
1006 // Ok, we made it through. Copy the block.
1007 // we just support greping one capsule from the lists of block descs list.
1011 //Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA
1013 if (CapsuleTimes
> 1) {
1015 //For every capsule entry point, check its header to determine whether to relocate it.
1016 //If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.
1018 if (CapsuleBeginFlag
) {
1019 CapsuleBeginFlag
= FALSE
;
1020 CapsuleHeader
= (EFI_CAPSULE_HEADER
*)(UINTN
)CurrentBlockDesc
->Union
.DataBlock
;
1021 SizeLeft
= CapsuleHeader
->CapsuleImageSize
;
1022 if (!IsCapsuleCorrupted (CapsuleHeader
)) {
1024 if (CapsuleIndex
> (MAX_SUPPORT_CAPSULE_NUM
- 1)) {
1025 DEBUG ((EFI_D_ERROR
, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM
));
1026 return EFI_BUFFER_TOO_SMALL
;
1030 // Relocate this valid capsule
1032 IsCorrupted
= FALSE
;
1033 CapsuleImageSize
+= SizeLeft
;
1034 CopyMem ((VOID
*) DestPtr
, (VOID
*) (UINTN
) CurrentBlockDesc
->Union
.DataBlock
, (UINTN
) CurrentBlockDesc
->Length
);
1035 DEBUG ((EFI_D_INFO
, "Capsule coalesce block no.0x%8X from 0x%8X to 0x%8X with size 0x%8X\n",CapsuleTimes
,
1036 (UINTN
)CurrentBlockDesc
->Union
.DataBlock
, (UINTN
)DestPtr
, (UINTN
)CurrentBlockDesc
->Length
));
1038 // Cache the begin offset of this capsule
1040 CapsuleOffset
[CapsuleIndex
++] = (UINT32
) (UINTN
) DestPtr
- (UINT32
)(UINTN
)NewCapsuleBase
- (UINT32
)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA
);
1041 DestPtr
+= CurrentBlockDesc
->Length
;
1044 // If the current block length is greater than or equal to SizeLeft, this is the
1045 // start of the next capsule
1047 if (CurrentBlockDesc
->Length
< SizeLeft
) {
1048 SizeLeft
-= CurrentBlockDesc
->Length
;
1051 // Start the next cycle
1055 CapsuleBeginFlag
= TRUE
;
1059 //Go on relocating the current capule image.
1061 if (CurrentBlockDesc
->Length
< SizeLeft
) {
1063 CopyMem ((VOID
*) DestPtr
, (VOID
*) (UINTN
) (CurrentBlockDesc
->Union
.DataBlock
), (UINTN
)CurrentBlockDesc
->Length
);
1064 DEBUG ((EFI_D_INFO
, "Capsule coalesce block no.0x%8X from 0x%8X to 0x%8X with size 0x%8X\n",CapsuleTimes
,
1065 (UINTN
)CurrentBlockDesc
->Union
.DataBlock
, (UINTN
)DestPtr
, (UINTN
)CurrentBlockDesc
->Length
));
1066 DestPtr
+= CurrentBlockDesc
->Length
;
1068 SizeLeft
-= CurrentBlockDesc
->Length
;
1071 //Here is the end of the current capsule image.
1074 CopyMem ((VOID
*) DestPtr
, (VOID
*)(UINTN
)(CurrentBlockDesc
->Union
.DataBlock
), (UINTN
)CurrentBlockDesc
->Length
);
1075 DEBUG ((EFI_D_INFO
, "Capsule coalesce block no.0x%8X from 0x%8X to 0x%8X with size 0x%8X\n",CapsuleTimes
,
1076 (UINTN
)CurrentBlockDesc
->Union
.DataBlock
, (UINTN
)DestPtr
, (UINTN
)CurrentBlockDesc
->Length
));
1077 DestPtr
+= CurrentBlockDesc
->Length
;
1080 // Start the next cycle
1084 CapsuleBeginFlag
= TRUE
;
1089 //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.
1091 CopyMem ((VOID
*) DestPtr
, (VOID
*) (UINTN
) CurrentBlockDesc
->Union
.DataBlock
, (UINTN
) CurrentBlockDesc
->Length
);
1092 DestPtr
+= CurrentBlockDesc
->Length
;
1095 //Walk through the block descriptor list.
1100 // We return the base of memory we want reserved, and the size.
1101 // The memory peim should handle it appropriately from there.
1103 *MemorySize
= (UINTN
) CapsuleImageSize
;
1104 *MemoryBase
= (VOID
*) NewCapsuleBase
;
1107 //Append the offsets of mutiply capsules to the continous buffer
1109 DataPtr
= (VOID
*)((UINTN
)NewCapsuleBase
+ sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA
) + (UINTN
)CapsuleImageSize
);
1110 AddDataPtr
= (UINT32
*)(((UINTN
) DataPtr
+ sizeof(UINT32
) - 1) &~ (UINT32
) (sizeof (UINT32
) - 1));
1112 *AddDataPtr
++ = CapsuleIndex
;
1114 CopyMem (AddDataPtr
, &CapsuleOffset
[0], sizeof (UINT32
) * CapsuleIndex
);
1116 PrivateDataPtr
= (EFI_CAPSULE_PEIM_PRIVATE_DATA
*) NewCapsuleBase
;
1117 PrivateDataPtr
->CapsuleSize
= (UINTN
)CapsuleImageSize
;
1123 Determine if we're in capsule update boot mode.
1125 @param PeiServices PEI services table
1127 @retval EFI_SUCCESS if we have a capsule available
1128 @retval EFI_NOT_FOUND no capsule detected
1133 CheckCapsuleUpdate (
1134 IN EFI_PEI_SERVICES
**PeiServices
1138 Status
= GetCapsuleDescriptors (NULL
);
1142 This function will look at a capsule and determine if it's a test pattern.
1143 If it is, then it will verify it and emit an error message if corruption is detected.
1145 @param PeiServices Standard pei services pointer
1146 @param CapsuleBase Base address of coalesced capsule, which is preceeded
1147 by private data. Very implementation specific.
1149 @retval TRUE Capsule image is the test image
1150 @retval FALSE Capsule image is not the test image.
1154 CapsuleTestPattern (
1155 IN EFI_PEI_SERVICES
**PeiServices
,
1156 IN VOID
*CapsuleBase
1167 // Look at the capsule data and determine if it's a test pattern. If it
1168 // is, then test it now.
1170 TestPtr
= (UINT32
*) CapsuleBase
;
1171 if (*TestPtr
== CAPSULE_TEST_SIGNATURE
) {
1173 DEBUG ((EFI_D_INFO
, "Capsule test pattern mode activated...\n"));
1174 TestSize
= TestPtr
[1] / sizeof (UINT32
);
1176 // Skip over the signature and the size fields in the pattern data header
1180 while (TestSize
> 0) {
1181 if (*TestPtr
!= TestCounter
) {
1182 DEBUG ((EFI_D_INFO
, "Capsule test pattern mode FAILED: BaseAddr/FailAddr 0x%X 0x%X\n", (UINT32
)(UINTN
)(EFI_CAPSULE_PEIM_PRIVATE_DATA
*)CapsuleBase
, (UINT32
)(UINTN
)TestPtr
));
1191 DEBUG ((EFI_D_INFO
, "Capsule test pattern mode SUCCESS\n"));
1198 Capsule PPI service that gets called after memory is available. The
1199 capsule coalesce function, which must be called first, returns a base
1200 address and size, which can be anything actually. Once the memory init
1201 PEIM has discovered memory, then it should call this function and pass in
1202 the base address and size returned by the coalesce function. Then this
1203 function can create a capsule HOB and return.
1205 @param PeiServices standard pei services pointer
1206 @param CapsuleBase address returned by the capsule coalesce function. Most
1207 likely this will actually be a pointer to private data.
1208 @param CapsuleSize value returned by the capsule coalesce function.
1210 @retval EFI_VOLUME_CORRUPTED CapsuleBase does not appear to point to a
1212 @retval EFI_SUCCESS if all goes well.
1217 IN EFI_PEI_SERVICES
**PeiServices
,
1218 IN VOID
*CapsuleBase
,
1219 IN UINTN CapsuleSize
1223 EFI_CAPSULE_PEIM_PRIVATE_DATA
*PrivateData
;
1225 EFI_PHYSICAL_ADDRESS NewBuffer
;
1227 UINT32 CapsuleNumber
;
1229 EFI_PHYSICAL_ADDRESS BaseAddress
;
1234 PrivateData
= (EFI_CAPSULE_PEIM_PRIVATE_DATA
*) CapsuleBase
;
1235 if (PrivateData
->Signature
!= EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE
) {
1236 return EFI_VOLUME_CORRUPTED
;
1239 // Capsule Number and Capsule Offset is in the tail of Capsule data.
1241 Size
= (UINTN
) PrivateData
->CapsuleSize
;
1242 DataPtr
= (UINT32
*)((UINTN
)CapsuleBase
+ (UINTN
)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA
)+ Size
);
1243 DataPtr
= (UINT32
*)(((UINTN
) DataPtr
+ sizeof(UINT32
) - 1) & ~(sizeof (UINT32
) - 1));
1244 CapsuleNumber
= *DataPtr
++;
1246 // Allocate the memory so that it gets preserved into DXE
1248 Status
= PeiServicesAllocatePages (
1249 EfiRuntimeServicesData
,
1250 EFI_SIZE_TO_PAGES (Size
),
1254 if (Status
!= EFI_SUCCESS
) {
1255 DEBUG ((EFI_D_ERROR
, "AllocatePages Failed!\n"));
1259 // Copy to our new buffer for DXE
1261 DEBUG ((EFI_D_INFO
, "Capsule copy from 0x%8X to 0x%8X with size 0x%8X\n", (UINTN
) (PrivateData
+ 1), (UINTN
) NewBuffer
, Size
));
1262 CopyMem ((VOID
*) (UINTN
) NewBuffer
, (VOID
*) (UINTN
) (PrivateData
+ 1), Size
);
1264 // Check for test data pattern. If it is the test pattern, then we'll
1265 // test it ans still create the HOB so that it can be used to verify
1266 // that capsules don't get corrupted all the way into BDS. BDS will
1267 // still try to turn it into a firmware volume, but will think it's
1268 // corrupted so nothing will happen.
1271 CapsuleTestPattern (PeiServices
, (VOID
*) (UINTN
) NewBuffer
);
1275 // Build the UEFI Capsule Hob for each capsule image.
1277 for (Index
= 0; Index
< CapsuleNumber
; Index
++) {
1278 BaseAddress
= NewBuffer
+ DataPtr
[Index
];
1279 Length
= ((EFI_CAPSULE_HEADER
*)((UINTN
) BaseAddress
))->CapsuleImageSize
;
1281 BuildCvHob (BaseAddress
, Length
);
1287 CONST PEI_CAPSULE_PPI mCapsulePpi
= {
1293 CONST EFI_PEI_PPI_DESCRIPTOR mUefiPpiListCapsule
= {
1294 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
1295 &gPeiCapsulePpiGuid
,
1296 (PEI_CAPSULE_PPI
*) &mCapsulePpi
1300 Entry point function for the PEIM
1302 @param FileHandle Handle of the file being invoked.
1303 @param PeiServices Describes the list of possible PEI Services.
1305 @return EFI_SUCCESS If we installed our PPI
1311 IN EFI_PEI_FILE_HANDLE FileHandle
,
1312 IN CONST EFI_PEI_SERVICES
**PeiServices
1316 // Just produce our PPI
1318 return PeiServicesInstallPpi (&mUefiPpiListCapsule
);