2 The logic to process capsule.
4 Caution: This module requires additional review when modified.
5 This driver will have external input - capsule image.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
9 CapsuleDataCoalesce() will do basic validation before coalesce capsule data
12 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
13 This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26 #include <Guid/CapsuleVendor.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/PrintLib.h>
31 #include <Library/BaseLib.h>
33 #define MIN_COALESCE_ADDR (1024 * 1024)
34 #define MAX_SUPPORT_CAPSULE_NUM 50
36 #define EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('C', 'a', 'p', 'D')
41 } EFI_CAPSULE_PEIM_PRIVATE_DATA
;
44 Given a pointer to the capsule block list, info on the available system
45 memory, and the size of a buffer, find a free block of memory where a
46 buffer of the given size can be copied to safely.
48 @param BlockList Pointer to head of capsule block descriptors
49 @param MemBase Pointer to the base of memory in which we want to find free space
50 @param MemSize The size of the block of memory pointed to by MemBase
51 @param DataSize How big a free block we want to find
53 @return A pointer to a memory block of at least DataSize that lies somewhere
54 between MemBase and (MemBase + MemSize). The memory pointed to does not
55 contain any of the capsule block descriptors or capsule blocks pointed to
61 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
68 Check the integrity of the capsule descriptors.
70 @param BlockList Pointer to the capsule descriptors
72 @retval NULL BlockList is not valid.
73 @retval LastBlockDesc Last one Block in BlockList
76 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
77 ValidateCapsuleIntegrity (
78 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
82 The capsule block descriptors may be fragmented and spread all over memory.
83 To simplify the coalescing of capsule blocks, first coalesce all the
84 capsule block descriptors low in memory.
86 The descriptors passed in can be fragmented throughout memory. Here
87 they are relocated into memory to turn them into a contiguous (null
90 @param PeiServices pointer to PEI services table
91 @param BlockList pointer to the capsule block descriptors
92 @param MemBase base of system memory in which we can work
93 @param MemSize size of the system memory pointed to by MemBase
95 @retval NULL could not relocate the descriptors
96 @retval Pointer to the base of the successfully-relocated block descriptors.
99 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
100 RelocateBlockDescriptors (
101 IN EFI_PEI_SERVICES
**PeiServices
,
102 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
108 Check every capsule header.
110 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
112 @retval FALSE Capsule is OK
113 @retval TRUE Capsule is corrupted
118 IN EFI_CAPSULE_HEADER
*CapsuleHeader
122 Determine if two buffers overlap in memory.
124 @param Buff1 pointer to first buffer
125 @param Size1 size of Buff1
126 @param Buff2 pointer to second buffer
127 @param Size2 size of Buff2
129 @retval TRUE Buffers overlap in memory.
130 @retval FALSE Buffer doesn't overlap.
142 Given a pointer to a capsule block descriptor, traverse the list to figure
143 out how many legitimate descriptors there are, and how big the capsule it
146 @param Desc Pointer to the capsule block descriptors
147 NumDescriptors - optional pointer to where to return the number of descriptors
148 CapsuleSize - optional pointer to where to return the capsule size
149 @param NumDescriptors Optional pointer to where to return the number of descriptors
150 @param CapsuleSize Optional pointer to where to return the capsule size
152 @retval EFI_NOT_FOUND No descriptors containing data in the list
153 @retval EFI_SUCCESS Return data is valid
158 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*Desc
,
159 IN OUT UINTN
*NumDescriptors OPTIONAL
,
160 IN OUT UINTN
*CapsuleSize OPTIONAL
164 Given a pointer to the capsule block list, info on the available system
165 memory, and the size of a buffer, find a free block of memory where a
166 buffer of the given size can be copied to safely.
168 @param BlockList Pointer to head of capsule block descriptors
169 @param MemBase Pointer to the base of memory in which we want to find free space
170 @param MemSize The size of the block of memory pointed to by MemBase
171 @param DataSize How big a free block we want to find
173 @return A pointer to a memory block of at least DataSize that lies somewhere
174 between MemBase and (MemBase + MemSize). The memory pointed to does not
175 contain any of the capsule block descriptors or capsule blocks pointed to
181 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
188 EFI_CAPSULE_BLOCK_DESCRIPTOR
*CurrDesc
;
189 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempDesc
;
194 // Need at least enough to copy the data to at the end of the buffer, so
195 // say the end is less the data size for easy comparisons here.
197 MemEnd
= MemBase
+ MemSize
- DataSize
;
198 CurrDesc
= BlockList
;
200 // Go through all the descriptor blocks and see if any obstruct the range
202 while (CurrDesc
!= NULL
) {
204 // Get the size of this block list and see if it's in the way
208 Size
= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
209 while (TempDesc
->Length
!= 0) {
210 Size
+= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
214 if (IsOverlapped (MemBase
, DataSize
, (UINT8
*) CurrDesc
, Size
)) {
216 // Set our new base to the end of this block list and start all over
218 MemBase
= (UINT8
*) CurrDesc
+ Size
;
219 CurrDesc
= BlockList
;
220 if (MemBase
> MemEnd
) {
227 // Now go through all the blocks and make sure none are in the way
229 while ((CurrDesc
->Length
!= 0) && (!Failed
)) {
230 if (IsOverlapped (MemBase
, DataSize
, (UINT8
*) (UINTN
) CurrDesc
->Union
.DataBlock
, (UINTN
) CurrDesc
->Length
)) {
232 // Set our new base to the end of this block and start all over
235 MemBase
= (UINT8
*) ((UINTN
) CurrDesc
->Union
.DataBlock
) + CurrDesc
->Length
;
236 CurrDesc
= BlockList
;
237 if (MemBase
> MemEnd
) {
244 // Normal continuation -- jump to next block descriptor list
247 CurrDesc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) CurrDesc
->Union
.ContinuationPointer
;
254 Check the integrity of the capsule descriptors.
256 @param BlockList Pointer to the capsule descriptors
258 @retval NULL BlockList is not valid.
259 @retval LastBlockDesc Last one Block in BlockList
262 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
263 ValidateCapsuleIntegrity (
264 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
267 EFI_CAPSULE_HEADER
*CapsuleHeader
;
270 EFI_CAPSULE_BLOCK_DESCRIPTOR
*Ptr
;
273 // Go through the list to look for inconsistencies. Check for:
274 // * misaligned block descriptors.
275 // * The first capsule header guid
276 // * The first capsule header flag
277 // * Data + Length < Data (wrap)
281 while ((Ptr
->Length
!= 0) || (Ptr
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
283 // Make sure the descriptor is aligned at UINT64 in memory
285 if ((UINTN
) Ptr
& 0x07) {
286 DEBUG ((EFI_D_ERROR
, "BlockList address failed alignment check\n"));
290 if (Ptr
->Length
== 0) {
292 // Descriptor points to another list of block descriptors somewhere
295 Ptr
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Ptr
->Union
.ContinuationPointer
;
298 //To enhance the reliability of check-up, the first capsule's header is checked here.
299 //More reliabilities check-up will do later.
301 if (CapsuleSize
== 0) {
303 //Move to the first capsule to check its header.
305 CapsuleHeader
= (EFI_CAPSULE_HEADER
*)((UINTN
)Ptr
->Union
.DataBlock
);
306 if (IsCapsuleCorrupted (CapsuleHeader
)) {
310 CapsuleSize
= CapsuleHeader
->CapsuleImageSize
;
313 if (CapsuleSize
>= Ptr
->Length
) {
314 CapsuleSize
= CapsuleSize
- Ptr
->Length
;
320 // Move to next BLOCK descriptor
326 if ((CapsuleCount
== 0) || (CapsuleSize
!= 0)) {
328 // No any capsule is found in BlockList or capsule data is corrupted.
337 The capsule block descriptors may be fragmented and spread all over memory.
338 To simplify the coalescing of capsule blocks, first coalesce all the
339 capsule block descriptors low in memory.
341 The descriptors passed in can be fragmented throughout memory. Here
342 they are relocated into memory to turn them into a contiguous (null
345 @param PeiServices pointer to PEI services table
346 @param BlockList pointer to the capsule block descriptors
347 @param MemBase base of system memory in which we can work
348 @param MemSize size of the system memory pointed to by MemBase
350 @retval NULL could not relocate the descriptors
351 @retval Pointer to the base of the successfully-relocated block descriptors.
354 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
355 RelocateBlockDescriptors (
356 IN EFI_PEI_SERVICES
**PeiServices
,
357 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
362 EFI_CAPSULE_BLOCK_DESCRIPTOR
*NewBlockList
;
363 EFI_CAPSULE_BLOCK_DESCRIPTOR
*CurrBlockDescHead
;
364 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockDesc
;
365 EFI_CAPSULE_BLOCK_DESCRIPTOR
*PrevBlockDescTail
;
366 UINTN NumDescriptors
;
371 // Get the info on the blocks and descriptors. Since we're going to move
372 // the descriptors low in memory, adjust the base/size values accordingly here.
373 // GetCapsuleInfo() returns the number of legit descriptors, so add one for
376 if (GetCapsuleInfo (BlockList
, &NumDescriptors
, NULL
) != EFI_SUCCESS
) {
381 BufferSize
= NumDescriptors
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
382 NewBlockList
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) MemBase
;
383 if (MemSize
< BufferSize
) {
387 MemSize
-= BufferSize
;
388 MemBase
+= BufferSize
;
390 // Go through all the blocks and make sure none are in the way
392 TempBlockDesc
= BlockList
;
393 while (TempBlockDesc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
394 if (TempBlockDesc
->Length
== 0) {
396 // Next block of descriptors
398 TempBlockDesc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) TempBlockDesc
->Union
.ContinuationPointer
;
401 // If the capsule data pointed to by this descriptor is in the way,
405 (UINT8
*) NewBlockList
,
407 (UINT8
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
,
408 (UINTN
) TempBlockDesc
->Length
411 // Relocate the block
413 RelocBuffer
= FindFreeMem (BlockList
, MemBase
, MemSize
, (UINTN
) TempBlockDesc
->Length
);
414 if (RelocBuffer
== NULL
) {
418 CopyMem ((VOID
*) RelocBuffer
, (VOID
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
, (UINTN
) TempBlockDesc
->Length
);
419 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
));
420 TempBlockDesc
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RelocBuffer
;
426 // Now go through all the block descriptors to make sure that they're not
427 // in the memory region we want to copy them to.
429 CurrBlockDescHead
= BlockList
;
430 PrevBlockDescTail
= NULL
;
431 while ((CurrBlockDescHead
!= NULL
) && (CurrBlockDescHead
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
433 // Get the size of this list then see if it overlaps our low region
435 TempBlockDesc
= CurrBlockDescHead
;
436 BlockListSize
= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
437 while (TempBlockDesc
->Length
!= 0) {
438 BlockListSize
+= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
443 (UINT8
*) NewBlockList
,
445 (UINT8
*) CurrBlockDescHead
,
449 // Overlaps, so move it out of the way
451 RelocBuffer
= FindFreeMem (BlockList
, MemBase
, MemSize
, BlockListSize
);
452 if (RelocBuffer
== NULL
) {
455 CopyMem ((VOID
*) RelocBuffer
, (VOID
*) CurrBlockDescHead
, BlockListSize
);
456 DEBUG ((EFI_D_INFO
, "Capsule reloc descriptor block #2\n"));
458 // Point the previous block's next point to this copied version. If
459 // the tail pointer is null, then this is the first descriptor block.
461 if (PrevBlockDescTail
== NULL
) {
462 BlockList
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) RelocBuffer
;
464 PrevBlockDescTail
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RelocBuffer
;
468 // Save our new tail and jump to the next block list
470 PrevBlockDescTail
= TempBlockDesc
;
471 CurrBlockDescHead
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) TempBlockDesc
->Union
.ContinuationPointer
;
474 // Cleared out low memory. Now copy the descriptors down there.
476 TempBlockDesc
= BlockList
;
477 CurrBlockDescHead
= NewBlockList
;
478 while ((TempBlockDesc
!= NULL
) && (TempBlockDesc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
479 if (TempBlockDesc
->Length
!= 0) {
480 CurrBlockDescHead
->Union
.DataBlock
= TempBlockDesc
->Union
.DataBlock
;
481 CurrBlockDescHead
->Length
= TempBlockDesc
->Length
;
485 TempBlockDesc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) TempBlockDesc
->Union
.ContinuationPointer
;
491 CurrBlockDescHead
->Union
.ContinuationPointer
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
;
492 CurrBlockDescHead
->Length
= 0;
497 Determine if two buffers overlap in memory.
499 @param Buff1 pointer to first buffer
500 @param Size1 size of Buff1
501 @param Buff2 pointer to second buffer
502 @param Size2 size of Buff2
504 @retval TRUE Buffers overlap in memory.
505 @retval FALSE Buffer doesn't overlap.
517 // If buff1's end is less than the start of buff2, then it's ok.
518 // Also, if buff1's start is beyond buff2's end, then it's ok.
520 if (((Buff1
+ Size1
) <= Buff2
) || (Buff1
>= (Buff2
+ Size2
))) {
528 Given a pointer to a capsule block descriptor, traverse the list to figure
529 out how many legitimate descriptors there are, and how big the capsule it
532 @param Desc Pointer to the capsule block descriptors
533 NumDescriptors - optional pointer to where to return the number of descriptors
534 CapsuleSize - optional pointer to where to return the capsule size
535 @param NumDescriptors Optional pointer to where to return the number of descriptors
536 @param CapsuleSize Optional pointer to where to return the capsule size
538 @retval EFI_NOT_FOUND No descriptors containing data in the list
539 @retval EFI_SUCCESS Return data is valid
544 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*Desc
,
545 IN OUT UINTN
*NumDescriptors OPTIONAL
,
546 IN OUT UINTN
*CapsuleSize OPTIONAL
552 ASSERT (Desc
!= NULL
);
557 while (Desc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
558 if (Desc
->Length
== 0) {
560 // Descriptor points to another list of block descriptors somewhere
562 Desc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Desc
->Union
.ContinuationPointer
;
564 Size
+= (UINTN
) Desc
->Length
;
570 // If no descriptors, then fail
573 return EFI_NOT_FOUND
;
576 if (NumDescriptors
!= NULL
) {
577 *NumDescriptors
= Count
;
580 if (CapsuleSize
!= NULL
) {
588 Check every capsule header.
590 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
592 @retval FALSE Capsule is OK
593 @retval TRUE Capsule is corrupted
598 IN EFI_CAPSULE_HEADER
*CapsuleHeader
602 //A capsule to be updated across a system reset should contain CAPSULE_FLAGS_PERSIST_ACROSS_RESET.
604 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) == 0) {
608 //Make sure the flags combination is supported by the platform.
610 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
613 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
621 Try to verify the integrity of a capsule test pattern before the
622 capsule gets coalesced. This can be useful in narrowing down
623 where capsule data corruption occurs.
625 The test pattern mode fills in memory with a counting UINT32 value.
626 If the capsule is not divided up in a multiple of 4-byte blocks, then
627 things get messy doing the check. Therefore there are some cases
628 here where we just give up and skip the pre-coalesce check.
630 @param PeiServices PEI services table
631 @param Desc Pointer to capsule descriptors
634 CapsuleTestPatternPreCoalesce (
635 IN EFI_PEI_SERVICES
**PeiServices
,
636 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*Desc
643 // Find first data descriptor
645 while ((Desc
->Length
== 0) && (Desc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
646 Desc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Desc
->Union
.ContinuationPointer
;
649 if (Desc
->Union
.ContinuationPointer
== 0) {
653 // First one better be long enough to at least hold the test signature
655 if (Desc
->Length
< sizeof (UINT32
)) {
656 DEBUG ((EFI_D_INFO
, "Capsule test pattern pre-coalesce punted #1\n"));
660 TestPtr
= (UINT32
*) (UINTN
) Desc
->Union
.DataBlock
;
664 if (*TestPtr
!= 0x54534554) {
669 TestSize
= (UINT32
) Desc
->Length
- 2 * sizeof (UINT32
);
671 // Skip over the signature and the size fields in the pattern data header
675 if ((TestSize
& 0x03) != 0) {
676 DEBUG ((EFI_D_INFO
, "Capsule test pattern pre-coalesce punted #2\n"));
680 while (TestSize
> 0) {
681 if (*TestPtr
!= TestCounter
) {
682 DEBUG ((EFI_D_INFO
, "Capsule test pattern pre-coalesce failed data corruption check\n"));
686 TestSize
-= sizeof (UINT32
);
691 while ((Desc
->Length
== 0) && (Desc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
692 Desc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Desc
->Union
.ContinuationPointer
;
695 if (Desc
->Union
.ContinuationPointer
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
698 TestSize
= (UINT32
) Desc
->Length
;
699 TestPtr
= (UINT32
*) (UINTN
) Desc
->Union
.DataBlock
;
704 Checks for the presence of capsule descriptors.
705 Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
707 @param BlockListBuffer Pointer to the buffer of capsule descriptors variables
708 @param BlockDescriptorList Pointer to the capsule descriptors list
710 @retval EFI_SUCCESS a valid capsule is present
711 @retval EFI_NOT_FOUND if a valid capsule is not present
714 BuildCapsuleDescriptors (
715 IN EFI_PHYSICAL_ADDRESS
*BlockListBuffer
,
716 OUT EFI_CAPSULE_BLOCK_DESCRIPTOR
**BlockDescriptorList
720 EFI_CAPSULE_BLOCK_DESCRIPTOR
*LastBlock
;
721 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlock
;
722 EFI_CAPSULE_BLOCK_DESCRIPTOR
*HeadBlock
;
729 while (BlockListBuffer
[Index
] != 0) {
731 // Test integrity of descriptors.
733 TempBlock
= ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR
*)(UINTN
)BlockListBuffer
[Index
]);
734 if (TempBlock
!= NULL
) {
735 if (LastBlock
== NULL
) {
736 LastBlock
= TempBlock
;
739 // Return the base of the block descriptors
741 HeadBlock
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*)(UINTN
)BlockListBuffer
[Index
];
744 // Combine the different BlockList into single BlockList.
746 LastBlock
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)BlockListBuffer
[Index
];
747 LastBlock
->Length
= 0;
748 LastBlock
= TempBlock
;
754 if (HeadBlock
!= NULL
) {
755 *BlockDescriptorList
= HeadBlock
;
758 return EFI_NOT_FOUND
;
762 The function to coalesce a fragmented capsule in memory.
764 Memory Map for coalesced capsule:
765 MemBase + ---->+---------------------------+<-----------+
766 MemSize | CapsuleOffset[49] | |
767 +---------------------------+ |
768 | ................ | |
769 +---------------------------+ |
770 | CapsuleOffset[2] | |
771 +---------------------------+ |
772 | CapsuleOffset[1] | |
773 +---------------------------+ |
774 | CapsuleOffset[0] | CapsuleSize
775 +---------------------------+ |
777 +---------------------------+ |
783 +---------------------------+ |
785 DestPtr ----> +---------------------------+<-----------+
787 | FreeMem | FreeMemSize
789 FreeMemBase --->+---------------------------+<-----------+
791 +---------------------------+
792 | BlockDescriptor n |
793 +---------------------------+
794 | ................. |
795 +---------------------------+
796 | BlockDescriptor 1 |
797 +---------------------------+
798 | BlockDescriptor 0 |
799 +---------------------------+
800 | PrivateDataDesc 0 |
801 MemBase ---->+---------------------------+<----- BlockList
803 Caution: This function may receive untrusted input.
804 The capsule data is external input, so this routine will do basic validation before
805 coalesce capsule data into memory.
807 @param PeiServices General purpose services available to every PEIM.
808 @param BlockListBuffer Point to the buffer of Capsule Descriptor Variables.
809 @param MemoryBase Pointer to the base of a block of memory that we can walk
810 all over while trying to coalesce our buffers.
811 On output, this variable will hold the base address of
813 @param MemorySize Size of the memory region pointed to by MemoryBase.
814 On output, this variable will contain the size of the
817 @retval EFI_NOT_FOUND If we could not find the capsule descriptors.
819 @retval EFI_BUFFER_TOO_SMALL
820 If we could not coalesce the capsule in the memory
821 region provided to us.
823 @retval EFI_SUCCESS Processed the capsule successfully.
827 CapsuleDataCoalesce (
828 IN EFI_PEI_SERVICES
**PeiServices
,
829 IN EFI_PHYSICAL_ADDRESS
*BlockListBuffer
,
830 IN OUT VOID
**MemoryBase
,
831 IN OUT UINTN
*MemorySize
834 VOID
*NewCapsuleBase
;
840 UINT32 CapsuleOffset
[MAX_SUPPORT_CAPSULE_NUM
];
844 UINT64 CapsuleImageSize
;
846 UINTN DescriptorsSize
;
848 UINTN NumDescriptors
;
850 BOOLEAN CapsuleBeginFlag
;
852 EFI_CAPSULE_HEADER
*CapsuleHeader
;
853 EFI_CAPSULE_PEIM_PRIVATE_DATA PrivateData
;
854 EFI_CAPSULE_PEIM_PRIVATE_DATA
*PrivateDataPtr
;
855 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
;
856 EFI_CAPSULE_BLOCK_DESCRIPTOR
*CurrentBlockDesc
;
857 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockDesc
;
858 EFI_CAPSULE_BLOCK_DESCRIPTOR PrivateDataDesc
[2];
863 CapsuleImageSize
= 0;
864 PrivateDataPtr
= NULL
;
866 CapsuleHeader
= NULL
;
867 CapsuleBeginFlag
= TRUE
;
873 // Build capsule descriptors list
875 Status
= BuildCapsuleDescriptors (BlockListBuffer
, &BlockList
);
876 if (EFI_ERROR (Status
)) {
881 CapsuleTestPatternPreCoalesce (PeiServices
, BlockList
);
885 // Get the size of our descriptors and the capsule size. GetCapsuleInfo()
886 // returns the number of descriptors that actually point to data, so add
887 // one for a terminator. Do that below.
889 GetCapsuleInfo (BlockList
, &NumDescriptors
, &CapsuleSize
);
890 if ((CapsuleSize
== 0) || (NumDescriptors
== 0)) {
891 return EFI_NOT_FOUND
;
895 // Initialize our local copy of private data. When we're done, we'll create a
896 // descriptor for it as well so that it can be put into free memory without
897 // trashing anything.
899 PrivateData
.Signature
= EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE
;
900 PrivateData
.CapsuleSize
= (UINT32
) CapsuleSize
;
901 PrivateDataDesc
[0].Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) &PrivateData
;
902 PrivateDataDesc
[0].Length
= sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA
);
903 PrivateDataDesc
[1].Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) BlockList
;
904 PrivateDataDesc
[1].Length
= 0;
906 // In addition to PrivateDataDesc[1:0], one terminator is added
907 // See below RelocateBlockDescriptors()
910 CapsuleSize
+= sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA
) + sizeof(CapsuleOffset
) + sizeof(UINT32
);
911 BlockList
= PrivateDataDesc
;
912 DescriptorsSize
= NumDescriptors
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
915 // Don't go below some min address. If the base is below it,
916 // then move it up and adjust the size accordingly.
918 DEBUG ((EFI_D_INFO
, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN
) *MemoryBase
, (UINTN
)*MemoryBase
+ *MemorySize
));
919 if ((UINTN
)*MemoryBase
< (UINTN
) MIN_COALESCE_ADDR
) {
920 if (((UINTN
)*MemoryBase
+ *MemorySize
) < (UINTN
) MIN_COALESCE_ADDR
) {
921 return EFI_BUFFER_TOO_SMALL
;
923 *MemorySize
= *MemorySize
- ((UINTN
) MIN_COALESCE_ADDR
- (UINTN
) *MemoryBase
);
924 *MemoryBase
= (VOID
*) (UINTN
) MIN_COALESCE_ADDR
;
928 if (*MemorySize
<= (CapsuleSize
+ DescriptorsSize
)) {
929 return EFI_BUFFER_TOO_SMALL
;
932 FreeMemBase
= *MemoryBase
;
933 FreeMemSize
= *MemorySize
;
934 DEBUG ((EFI_D_INFO
, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN
) FreeMemBase
, (UINTN
) FreeMemBase
+ FreeMemSize
));
937 // Relocate all the block descriptors to low memory to make further
938 // processing easier.
940 BlockList
= RelocateBlockDescriptors (PeiServices
, BlockList
, FreeMemBase
, FreeMemSize
);
941 if (BlockList
== NULL
) {
943 // Not enough room to relocate the descriptors
945 return EFI_BUFFER_TOO_SMALL
;
949 // Take the top of memory for the capsule. Naturally align.
951 DestPtr
= FreeMemBase
+ FreeMemSize
- CapsuleSize
;
952 DestPtr
= (UINT8
*) ((UINTN
) DestPtr
&~ (UINTN
) (sizeof (UINTN
) - 1));
953 FreeMemBase
= (UINT8
*) BlockList
+ DescriptorsSize
;
954 FreeMemSize
= (UINTN
) DestPtr
- (UINTN
) FreeMemBase
;
955 NewCapsuleBase
= (VOID
*) DestPtr
;
958 // Move all the blocks to the top (high) of memory.
959 // Relocate all the obstructing blocks. Note that the block descriptors
960 // were coalesced when they were relocated, so we can just ++ the pointer.
962 CurrentBlockDesc
= BlockList
;
963 while ((CurrentBlockDesc
->Length
!= 0) || (CurrentBlockDesc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
965 // See if any of the remaining capsule blocks are in the way
967 TempBlockDesc
= CurrentBlockDesc
;
968 while (TempBlockDesc
->Length
!= 0) {
970 // Is this block in the way of where we want to copy the current descriptor to?
974 (UINTN
) CurrentBlockDesc
->Length
,
975 (UINT8
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
,
976 (UINTN
) TempBlockDesc
->Length
979 // Relocate the block
981 RelocPtr
= FindFreeMem (BlockList
, FreeMemBase
, FreeMemSize
, (UINTN
) TempBlockDesc
->Length
);
982 if (RelocPtr
== NULL
) {
983 return EFI_BUFFER_TOO_SMALL
;
986 CopyMem ((VOID
*) RelocPtr
, (VOID
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
, (UINTN
) TempBlockDesc
->Length
);
987 DEBUG ((EFI_D_INFO
, "Capsule reloc data block from 0x%8X to 0x%8X with size 0x%8X\n",
988 (UINTN
) TempBlockDesc
->Union
.DataBlock
, (UINTN
) RelocPtr
, (UINTN
) TempBlockDesc
->Length
));
990 TempBlockDesc
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RelocPtr
;
998 // Ok, we made it through. Copy the block.
999 // we just support greping one capsule from the lists of block descs list.
1003 //Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA
1005 if (CapsuleTimes
> 1) {
1007 //For every capsule entry point, check its header to determine whether to relocate it.
1008 //If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.
1010 if (CapsuleBeginFlag
) {
1011 CapsuleBeginFlag
= FALSE
;
1012 CapsuleHeader
= (EFI_CAPSULE_HEADER
*)(UINTN
)CurrentBlockDesc
->Union
.DataBlock
;
1013 SizeLeft
= CapsuleHeader
->CapsuleImageSize
;
1014 if (!IsCapsuleCorrupted (CapsuleHeader
)) {
1016 if (CapsuleIndex
> (MAX_SUPPORT_CAPSULE_NUM
- 1)) {
1017 DEBUG ((EFI_D_ERROR
, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM
));
1018 return EFI_BUFFER_TOO_SMALL
;
1022 // Relocate this valid capsule
1024 IsCorrupted
= FALSE
;
1025 CapsuleImageSize
+= SizeLeft
;
1027 // Cache the begin offset of this capsule
1029 CapsuleOffset
[CapsuleIndex
++] = (UINT32
) (UINTN
) DestPtr
- (UINT32
)(UINTN
)NewCapsuleBase
- (UINT32
)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA
);
1033 if (CurrentBlockDesc
->Length
< SizeLeft
) {
1035 CopyMem ((VOID
*) DestPtr
, (VOID
*) (UINTN
) (CurrentBlockDesc
->Union
.DataBlock
), (UINTN
)CurrentBlockDesc
->Length
);
1036 DEBUG ((EFI_D_INFO
, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes
,
1037 (UINTN
)CurrentBlockDesc
->Union
.DataBlock
, (UINTN
)DestPtr
, (UINTN
)CurrentBlockDesc
->Length
));
1038 DestPtr
+= CurrentBlockDesc
->Length
;
1040 SizeLeft
-= CurrentBlockDesc
->Length
;
1043 //Here is the end of the current capsule image.
1046 CopyMem ((VOID
*) DestPtr
, (VOID
*)(UINTN
)(CurrentBlockDesc
->Union
.DataBlock
), (UINTN
) SizeLeft
);
1047 DEBUG ((EFI_D_INFO
, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes
,
1048 (UINTN
)CurrentBlockDesc
->Union
.DataBlock
, (UINTN
)DestPtr
, (UINTN
) SizeLeft
));
1049 DestPtr
+= SizeLeft
;
1052 // Start the next cycle
1056 CapsuleBeginFlag
= TRUE
;
1060 //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.
1062 CopyMem ((VOID
*) DestPtr
, (VOID
*) (UINTN
) CurrentBlockDesc
->Union
.DataBlock
, (UINTN
) CurrentBlockDesc
->Length
);
1063 DestPtr
+= CurrentBlockDesc
->Length
;
1066 //Walk through the block descriptor list.
1071 // We return the base of memory we want reserved, and the size.
1072 // The memory peim should handle it appropriately from there.
1074 *MemorySize
= (UINTN
) CapsuleSize
;
1075 *MemoryBase
= (VOID
*) NewCapsuleBase
;
1078 //Append the offsets of mutiply capsules to the continous buffer
1080 DataPtr
= (VOID
*)((UINTN
)NewCapsuleBase
+ sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA
) + (UINTN
)CapsuleImageSize
);
1081 AddDataPtr
= (UINT32
*)(((UINTN
) DataPtr
+ sizeof(UINT32
) - 1) &~ (UINT32
) (sizeof (UINT32
) - 1));
1083 *AddDataPtr
++ = CapsuleIndex
;
1085 CopyMem (AddDataPtr
, &CapsuleOffset
[0], sizeof (UINT32
) * CapsuleIndex
);
1087 PrivateDataPtr
= (EFI_CAPSULE_PEIM_PRIVATE_DATA
*) NewCapsuleBase
;
1088 PrivateDataPtr
->CapsuleSize
= (UINT32
) CapsuleImageSize
;