2 The logic to process capsule.
4 Copyright (c) 2011, 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.
18 #include <Guid/CapsuleVendor.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/PrintLib.h>
23 #include <Library/BaseLib.h>
25 #define MIN_COALESCE_ADDR (1024 * 1024)
26 #define MAX_SUPPORT_CAPSULE_NUM 50
28 #define EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('C', 'a', 'p', 'D')
33 } EFI_CAPSULE_PEIM_PRIVATE_DATA
;
36 Given a pointer to the capsule block list, info on the available system
37 memory, and the size of a buffer, find a free block of memory where a
38 buffer of the given size can be copied to safely.
40 @param BlockList Pointer to head of capsule block descriptors
41 @param MemBase Pointer to the base of memory in which we want to find free space
42 @param MemSize The size of the block of memory pointed to by MemBase
43 @param DataSize How big a free block we want to find
45 @return A pointer to a memory block of at least DataSize that lies somewhere
46 between MemBase and (MemBase + MemSize). The memory pointed to does not
47 contain any of the capsule block descriptors or capsule blocks pointed to
53 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
60 Check the integrity of the capsule descriptors.
62 @param BlockList Pointer to the capsule descriptors
64 @retval NULL BlockList is not valid.
65 @retval LastBlockDesc Last one Block in BlockList
68 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
69 ValidateCapsuleIntegrity (
70 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
74 The capsule block descriptors may be fragmented and spread all over memory.
75 To simplify the coalescing of capsule blocks, first coalesce all the
76 capsule block descriptors low in memory.
78 The descriptors passed in can be fragmented throughout memory. Here
79 they are relocated into memory to turn them into a contiguous (null
82 @param PeiServices pointer to PEI services table
83 @param BlockList pointer to the capsule block descriptors
84 @param MemBase base of system memory in which we can work
85 @param MemSize size of the system memory pointed to by MemBase
87 @retval NULL could not relocate the descriptors
88 @retval Pointer to the base of the successfully-relocated block descriptors.
91 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
92 RelocateBlockDescriptors (
93 IN EFI_PEI_SERVICES
**PeiServices
,
94 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
100 Check every capsule header.
102 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
104 @retval FALSE Capsule is OK
105 @retval TRUE Capsule is corrupted
110 IN EFI_CAPSULE_HEADER
*CapsuleHeader
114 Determine if two buffers overlap in memory.
116 @param Buff1 pointer to first buffer
117 @param Size1 size of Buff1
118 @param Buff2 pointer to second buffer
119 @param Size2 size of Buff2
121 @retval TRUE Buffers overlap in memory.
122 @retval FALSE Buffer doesn't overlap.
134 Given a pointer to a capsule block descriptor, traverse the list to figure
135 out how many legitimate descriptors there are, and how big the capsule it
138 @param Desc Pointer to the capsule block descriptors
139 NumDescriptors - optional pointer to where to return the number of descriptors
140 CapsuleSize - optional pointer to where to return the capsule size
141 @param NumDescriptors Optional pointer to where to return the number of descriptors
142 @param CapsuleSize Optional pointer to where to return the capsule size
144 @retval EFI_NOT_FOUND No descriptors containing data in the list
145 @retval EFI_SUCCESS Return data is valid
150 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*Desc
,
151 IN OUT UINTN
*NumDescriptors OPTIONAL
,
152 IN OUT UINTN
*CapsuleSize OPTIONAL
156 Given a pointer to the capsule block list, info on the available system
157 memory, and the size of a buffer, find a free block of memory where a
158 buffer of the given size can be copied to safely.
160 @param BlockList Pointer to head of capsule block descriptors
161 @param MemBase Pointer to the base of memory in which we want to find free space
162 @param MemSize The size of the block of memory pointed to by MemBase
163 @param DataSize How big a free block we want to find
165 @return A pointer to a memory block of at least DataSize that lies somewhere
166 between MemBase and (MemBase + MemSize). The memory pointed to does not
167 contain any of the capsule block descriptors or capsule blocks pointed to
173 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
180 EFI_CAPSULE_BLOCK_DESCRIPTOR
*CurrDesc
;
181 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempDesc
;
186 // Need at least enough to copy the data to at the end of the buffer, so
187 // say the end is less the data size for easy comparisons here.
189 MemEnd
= MemBase
+ MemSize
- DataSize
;
190 CurrDesc
= BlockList
;
192 // Go through all the descriptor blocks and see if any obstruct the range
194 while (CurrDesc
!= NULL
) {
196 // Get the size of this block list and see if it's in the way
200 Size
= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
201 while (TempDesc
->Length
!= 0) {
202 Size
+= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
206 if (IsOverlapped (MemBase
, DataSize
, (UINT8
*) CurrDesc
, Size
)) {
208 // Set our new base to the end of this block list and start all over
210 MemBase
= (UINT8
*) CurrDesc
+ Size
;
211 CurrDesc
= BlockList
;
212 if (MemBase
> MemEnd
) {
219 // Now go through all the blocks and make sure none are in the way
221 while ((CurrDesc
->Length
!= 0) && (!Failed
)) {
222 if (IsOverlapped (MemBase
, DataSize
, (UINT8
*) (UINTN
) CurrDesc
->Union
.DataBlock
, (UINTN
) CurrDesc
->Length
)) {
224 // Set our new base to the end of this block and start all over
227 MemBase
= (UINT8
*) ((UINTN
) CurrDesc
->Union
.DataBlock
) + CurrDesc
->Length
;
228 CurrDesc
= BlockList
;
229 if (MemBase
> MemEnd
) {
236 // Normal continuation -- jump to next block descriptor list
239 CurrDesc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) CurrDesc
->Union
.ContinuationPointer
;
246 Check the integrity of the capsule descriptors.
248 @param BlockList Pointer to the capsule descriptors
250 @retval NULL BlockList is not valid.
251 @retval LastBlockDesc Last one Block in BlockList
254 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
255 ValidateCapsuleIntegrity (
256 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
259 EFI_CAPSULE_HEADER
*CapsuleHeader
;
262 EFI_CAPSULE_BLOCK_DESCRIPTOR
*Ptr
;
265 // Go through the list to look for inconsistencies. Check for:
266 // * misaligned block descriptors.
267 // * The first capsule header guid
268 // * The first capsule header flag
269 // * Data + Length < Data (wrap)
273 while ((Ptr
->Length
!= 0) || (Ptr
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
275 // Make sure the descriptor is aligned at UINT64 in memory
277 if ((UINTN
) Ptr
& 0x07) {
278 DEBUG ((EFI_D_ERROR
, "BlockList address failed alignment check\n"));
282 if (Ptr
->Length
== 0) {
284 // Descriptor points to another list of block descriptors somewhere
287 Ptr
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Ptr
->Union
.ContinuationPointer
;
290 //To enhance the reliability of check-up, the first capsule's header is checked here.
291 //More reliabilities check-up will do later.
293 if (CapsuleSize
== 0) {
295 //Move to the first capsule to check its header.
297 CapsuleHeader
= (EFI_CAPSULE_HEADER
*)((UINTN
)Ptr
->Union
.DataBlock
);
298 if (IsCapsuleCorrupted (CapsuleHeader
)) {
302 CapsuleSize
= CapsuleHeader
->CapsuleImageSize
;
304 if (CapsuleSize
>= Ptr
->Length
) {
305 CapsuleSize
= CapsuleSize
- Ptr
->Length
;
311 // Move to next BLOCK descriptor
317 if (CapsuleCount
== 0) {
319 // No any capsule is found in BlockList.
328 The capsule block descriptors may be fragmented and spread all over memory.
329 To simplify the coalescing of capsule blocks, first coalesce all the
330 capsule block descriptors low in memory.
332 The descriptors passed in can be fragmented throughout memory. Here
333 they are relocated into memory to turn them into a contiguous (null
336 @param PeiServices pointer to PEI services table
337 @param BlockList pointer to the capsule block descriptors
338 @param MemBase base of system memory in which we can work
339 @param MemSize size of the system memory pointed to by MemBase
341 @retval NULL could not relocate the descriptors
342 @retval Pointer to the base of the successfully-relocated block descriptors.
345 EFI_CAPSULE_BLOCK_DESCRIPTOR
*
346 RelocateBlockDescriptors (
347 IN EFI_PEI_SERVICES
**PeiServices
,
348 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
,
353 EFI_CAPSULE_BLOCK_DESCRIPTOR
*NewBlockList
;
354 EFI_CAPSULE_BLOCK_DESCRIPTOR
*CurrBlockDescHead
;
355 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockDesc
;
356 EFI_CAPSULE_BLOCK_DESCRIPTOR
*PrevBlockDescTail
;
357 UINTN NumDescriptors
;
362 // Get the info on the blocks and descriptors. Since we're going to move
363 // the descriptors low in memory, adjust the base/size values accordingly here.
364 // GetCapsuleInfo() returns the number of legit descriptors, so add one for
367 if (GetCapsuleInfo (BlockList
, &NumDescriptors
, NULL
) != EFI_SUCCESS
) {
372 BufferSize
= NumDescriptors
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
373 NewBlockList
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) MemBase
;
374 if (MemSize
< BufferSize
) {
378 MemSize
-= BufferSize
;
379 MemBase
+= BufferSize
;
381 // Go through all the blocks and make sure none are in the way
383 TempBlockDesc
= BlockList
;
384 while (TempBlockDesc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
385 if (TempBlockDesc
->Length
== 0) {
387 // Next block of descriptors
389 TempBlockDesc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) TempBlockDesc
->Union
.ContinuationPointer
;
392 // If the capsule data pointed to by this descriptor is in the way,
396 (UINT8
*) NewBlockList
,
398 (UINT8
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
,
399 (UINTN
) TempBlockDesc
->Length
402 // Relocate the block
404 RelocBuffer
= FindFreeMem (BlockList
, MemBase
, MemSize
, (UINTN
) TempBlockDesc
->Length
);
405 if (RelocBuffer
== NULL
) {
409 CopyMem ((VOID
*) RelocBuffer
, (VOID
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
, (UINTN
) TempBlockDesc
->Length
);
410 TempBlockDesc
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RelocBuffer
;
412 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
));
418 // Now go through all the block descriptors to make sure that they're not
419 // in the memory region we want to copy them to.
421 CurrBlockDescHead
= BlockList
;
422 PrevBlockDescTail
= NULL
;
423 while ((CurrBlockDescHead
!= NULL
) && (CurrBlockDescHead
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
425 // Get the size of this list then see if it overlaps our low region
427 TempBlockDesc
= CurrBlockDescHead
;
428 BlockListSize
= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
429 while (TempBlockDesc
->Length
!= 0) {
430 BlockListSize
+= sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
435 (UINT8
*) NewBlockList
,
437 (UINT8
*) CurrBlockDescHead
,
441 // Overlaps, so move it out of the way
443 RelocBuffer
= FindFreeMem (BlockList
, MemBase
, MemSize
, BlockListSize
);
444 if (RelocBuffer
== NULL
) {
447 CopyMem ((VOID
*) RelocBuffer
, (VOID
*) CurrBlockDescHead
, BlockListSize
);
448 DEBUG ((EFI_D_INFO
, "Capsule reloc descriptor block #2\n"));
450 // Point the previous block's next point to this copied version. If
451 // the tail pointer is null, then this is the first descriptor block.
453 if (PrevBlockDescTail
== NULL
) {
454 BlockList
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) RelocBuffer
;
456 PrevBlockDescTail
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RelocBuffer
;
460 // Save our new tail and jump to the next block list
462 PrevBlockDescTail
= TempBlockDesc
;
463 CurrBlockDescHead
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) TempBlockDesc
->Union
.ContinuationPointer
;
466 // Cleared out low memory. Now copy the descriptors down there.
468 TempBlockDesc
= BlockList
;
469 CurrBlockDescHead
= NewBlockList
;
470 while ((TempBlockDesc
!= NULL
) && (TempBlockDesc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
471 if (TempBlockDesc
->Length
!= 0) {
472 CurrBlockDescHead
->Union
.DataBlock
= TempBlockDesc
->Union
.DataBlock
;
473 CurrBlockDescHead
->Length
= TempBlockDesc
->Length
;
477 TempBlockDesc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) TempBlockDesc
->Union
.ContinuationPointer
;
483 CurrBlockDescHead
->Union
.ContinuationPointer
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
;
484 CurrBlockDescHead
->Length
= 0;
489 Determine if two buffers overlap in memory.
491 @param Buff1 pointer to first buffer
492 @param Size1 size of Buff1
493 @param Buff2 pointer to second buffer
494 @param Size2 size of Buff2
496 @retval TRUE Buffers overlap in memory.
497 @retval FALSE Buffer doesn't overlap.
509 // If buff1's end is less than the start of buff2, then it's ok.
510 // Also, if buff1's start is beyond buff2's end, then it's ok.
512 if (((Buff1
+ Size1
) <= Buff2
) || (Buff1
>= (Buff2
+ Size2
))) {
520 Given a pointer to a capsule block descriptor, traverse the list to figure
521 out how many legitimate descriptors there are, and how big the capsule it
524 @param Desc Pointer to the capsule block descriptors
525 NumDescriptors - optional pointer to where to return the number of descriptors
526 CapsuleSize - optional pointer to where to return the capsule size
527 @param NumDescriptors Optional pointer to where to return the number of descriptors
528 @param CapsuleSize Optional pointer to where to return the capsule size
530 @retval EFI_NOT_FOUND No descriptors containing data in the list
531 @retval EFI_SUCCESS Return data is valid
536 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*Desc
,
537 IN OUT UINTN
*NumDescriptors OPTIONAL
,
538 IN OUT UINTN
*CapsuleSize OPTIONAL
547 while (Desc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
548 if (Desc
->Length
== 0) {
550 // Descriptor points to another list of block descriptors somewhere
552 Desc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Desc
->Union
.ContinuationPointer
;
554 Size
+= (UINTN
) Desc
->Length
;
560 // If no descriptors, then fail
563 return EFI_NOT_FOUND
;
566 if (NumDescriptors
!= NULL
) {
567 *NumDescriptors
= Count
;
570 if (CapsuleSize
!= NULL
) {
578 Check every capsule header.
580 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
582 @retval FALSE Capsule is OK
583 @retval TRUE Capsule is corrupted
588 IN EFI_CAPSULE_HEADER
*CapsuleHeader
592 //A capsule to be updated across a system reset should contain CAPSULE_FLAGS_PERSIST_ACROSS_RESET.
594 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) == 0) {
598 //Make sure the flags combination is supported by the platform.
600 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
603 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
611 Try to verify the integrity of a capsule test pattern before the
612 capsule gets coalesced. This can be useful in narrowing down
613 where capsule data corruption occurs.
615 The test pattern mode fills in memory with a counting UINT32 value.
616 If the capsule is not divided up in a multiple of 4-byte blocks, then
617 things get messy doing the check. Therefore there are some cases
618 here where we just give up and skip the pre-coalesce check.
620 @param PeiServices PEI services table
621 @param Desc Pointer to capsule descriptors
624 CapsuleTestPatternPreCoalesce (
625 IN EFI_PEI_SERVICES
**PeiServices
,
626 IN EFI_CAPSULE_BLOCK_DESCRIPTOR
*Desc
633 // Find first data descriptor
635 while ((Desc
->Length
== 0) && (Desc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
636 Desc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Desc
->Union
.ContinuationPointer
;
639 if (Desc
->Union
.ContinuationPointer
== 0) {
643 // First one better be long enough to at least hold the test signature
645 if (Desc
->Length
< sizeof (UINT32
)) {
646 DEBUG ((EFI_D_INFO
, "Capsule test pattern pre-coalesce punted #1\n"));
650 TestPtr
= (UINT32
*) (UINTN
) Desc
->Union
.DataBlock
;
654 if (*TestPtr
!= 0x54534554) {
659 TestSize
= (UINT32
) Desc
->Length
- 2 * sizeof (UINT32
);
661 // Skip over the signature and the size fields in the pattern data header
665 if ((TestSize
& 0x03) != 0) {
666 DEBUG ((EFI_D_INFO
, "Capsule test pattern pre-coalesce punted #2\n"));
670 while (TestSize
> 0) {
671 if (*TestPtr
!= TestCounter
) {
672 DEBUG ((EFI_D_INFO
, "Capsule test pattern pre-coalesce failed data corruption check\n"));
676 TestSize
-= sizeof (UINT32
);
681 while ((Desc
->Length
== 0) && (Desc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
682 Desc
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*) (UINTN
) Desc
->Union
.ContinuationPointer
;
685 if (Desc
->Union
.ContinuationPointer
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
688 TestSize
= (UINT32
) Desc
->Length
;
689 TestPtr
= (UINT32
*) (UINTN
) Desc
->Union
.DataBlock
;
694 Checks for the presence of capsule descriptors.
695 Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
697 @param BlockListBuffer Pointer to the buffer of capsule descriptors variables
698 @param BlockDescriptorList Pointer to the capsule descriptors list
700 @retval EFI_SUCCESS a valid capsule is present
701 @retval EFI_NOT_FOUND if a valid capsule is not present
704 BuildCapsuleDescriptors (
705 IN EFI_PHYSICAL_ADDRESS
*BlockListBuffer
,
706 OUT EFI_CAPSULE_BLOCK_DESCRIPTOR
**BlockDescriptorList
710 EFI_CAPSULE_BLOCK_DESCRIPTOR
*LastBlock
;
711 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlock
;
712 EFI_CAPSULE_BLOCK_DESCRIPTOR
*HeadBlock
;
719 while (BlockListBuffer
[Index
] != 0) {
722 // For the first Capsule Image, test integrity of descriptors.
724 LastBlock
= ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR
*)(UINTN
)BlockListBuffer
[Index
]);
725 if (LastBlock
== NULL
) {
726 return EFI_NOT_FOUND
;
729 // Return the base of the block descriptors
731 HeadBlock
= (EFI_CAPSULE_BLOCK_DESCRIPTOR
*)(UINTN
)BlockListBuffer
[Index
];
734 // Test integrity of descriptors.
736 TempBlock
= ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR
*)(UINTN
)BlockListBuffer
[Index
]);
737 if (TempBlock
== NULL
) {
738 return EFI_NOT_FOUND
;
741 // Combine the different BlockList into single BlockList.
743 LastBlock
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)BlockListBuffer
[Index
];
744 LastBlock
->Length
= 0;
745 LastBlock
= TempBlock
;
750 if (HeadBlock
!= NULL
) {
751 *BlockDescriptorList
= HeadBlock
;
754 return EFI_NOT_FOUND
;
758 The function to coalesce a fragmented capsule in memory.
760 Memory Map for coalesced capsule:
761 MemBase + ---->+---------------------------+<-----------+
762 MemSize | CapsuleOffset[49] | |
763 +---------------------------+ |
764 | ................ | |
765 +---------------------------+ |
766 | CapsuleOffset[2] | |
767 +---------------------------+ |
768 | CapsuleOffset[1] | |
769 +---------------------------+ |
770 | CapsuleOffset[0] | CapsuleSize
771 +---------------------------+ |
773 +---------------------------+ |
779 +---------------------------+ |
781 DestPtr ----> +---------------------------+<-----------+
783 | FreeMem | FreeMemSize
785 FreeMemBase --->+---------------------------+<-----------+
787 +---------------------------+
788 | BlockDescriptor n |
789 +---------------------------+
790 | ................. |
791 +---------------------------+
792 | BlockDescriptor 1 |
793 +---------------------------+
794 | BlockDescriptor 0 |
795 +---------------------------+
796 | PrivateDataDesc 0 |
797 MemBase ---->+---------------------------+<----- BlockList
799 @param PeiServices General purpose services available to every PEIM.
800 @param BlockListBuffer Point to the buffer of Capsule Descriptor Variables.
801 @param MemoryBase Pointer to the base of a block of memory that we can walk
802 all over while trying to coalesce our buffers.
803 On output, this variable will hold the base address of
805 @param MemorySize Size of the memory region pointed to by MemoryBase.
806 On output, this variable will contain the size of the
809 @retval EFI_NOT_FOUND If we could not find the capsule descriptors.
811 @retval EFI_BUFFER_TOO_SMALL
812 If we could not coalesce the capsule in the memory
813 region provided to us.
815 @retval EFI_SUCCESS Processed the capsule successfully.
819 CapsuleDataCoalesce (
820 IN EFI_PEI_SERVICES
**PeiServices
,
821 IN EFI_PHYSICAL_ADDRESS
*BlockListBuffer
,
822 IN OUT VOID
**MemoryBase
,
823 IN OUT UINTN
*MemorySize
826 VOID
*NewCapsuleBase
;
832 UINT32 CapsuleOffset
[MAX_SUPPORT_CAPSULE_NUM
];
836 UINT64 CapsuleImageSize
;
838 UINTN DescriptorsSize
;
840 UINTN NumDescriptors
;
842 BOOLEAN CapsuleBeginFlag
;
844 EFI_CAPSULE_HEADER
*CapsuleHeader
;
845 EFI_CAPSULE_PEIM_PRIVATE_DATA PrivateData
;
846 EFI_CAPSULE_PEIM_PRIVATE_DATA
*PrivateDataPtr
;
847 EFI_CAPSULE_BLOCK_DESCRIPTOR
*BlockList
;
848 EFI_CAPSULE_BLOCK_DESCRIPTOR
*CurrentBlockDesc
;
849 EFI_CAPSULE_BLOCK_DESCRIPTOR
*TempBlockDesc
;
850 EFI_CAPSULE_BLOCK_DESCRIPTOR PrivateDataDesc
[2];
855 CapsuleImageSize
= 0;
856 PrivateDataPtr
= NULL
;
858 CapsuleHeader
= NULL
;
859 CapsuleBeginFlag
= TRUE
;
865 // Build capsule descriptors list
867 Status
= BuildCapsuleDescriptors (BlockListBuffer
, &BlockList
);
868 if (EFI_ERROR (Status
)) {
873 CapsuleTestPatternPreCoalesce (PeiServices
, BlockList
);
877 // Get the size of our descriptors and the capsule size. GetCapsuleInfo()
878 // returns the number of descriptors that actually point to data, so add
879 // one for a terminator. Do that below.
881 GetCapsuleInfo (BlockList
, &NumDescriptors
, &CapsuleSize
);
882 if ((CapsuleSize
== 0) || (NumDescriptors
== 0)) {
883 return EFI_NOT_FOUND
;
887 // Initialize our local copy of private data. When we're done, we'll create a
888 // descriptor for it as well so that it can be put into free memory without
889 // trashing anything.
891 PrivateData
.Signature
= EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE
;
892 PrivateData
.CapsuleSize
= (UINT32
) CapsuleSize
;
893 PrivateDataDesc
[0].Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) &PrivateData
;
894 PrivateDataDesc
[0].Length
= sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA
);
895 PrivateDataDesc
[1].Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) BlockList
;
896 PrivateDataDesc
[1].Length
= 0;
898 // In addition to PrivateDataDesc[1:0], one terminator is added
899 // See below RelocateBlockDescriptors()
902 CapsuleSize
+= sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA
) + sizeof(CapsuleOffset
) + sizeof(UINT32
);
903 BlockList
= PrivateDataDesc
;
904 DescriptorsSize
= NumDescriptors
* sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR
);
907 // Don't go below some min address. If the base is below it,
908 // then move it up and adjust the size accordingly.
910 DEBUG ((EFI_D_INFO
, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN
) *MemoryBase
, (UINTN
)*MemoryBase
+ *MemorySize
));
911 if ((UINTN
)*MemoryBase
< (UINTN
) MIN_COALESCE_ADDR
) {
912 if (((UINTN
)*MemoryBase
+ *MemorySize
) < (UINTN
) MIN_COALESCE_ADDR
) {
913 return EFI_BUFFER_TOO_SMALL
;
915 *MemorySize
= *MemorySize
- ((UINTN
) MIN_COALESCE_ADDR
- (UINTN
) *MemoryBase
);
916 *MemoryBase
= (VOID
*) (UINTN
) MIN_COALESCE_ADDR
;
920 if (*MemorySize
<= (CapsuleSize
+ DescriptorsSize
)) {
921 return EFI_BUFFER_TOO_SMALL
;
924 FreeMemBase
= *MemoryBase
;
925 FreeMemSize
= *MemorySize
;
926 DEBUG ((EFI_D_INFO
, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN
) FreeMemBase
, (UINTN
) FreeMemBase
+ FreeMemSize
));
929 // Relocate all the block descriptors to low memory to make further
930 // processing easier.
932 BlockList
= RelocateBlockDescriptors (PeiServices
, BlockList
, FreeMemBase
, FreeMemSize
);
933 if (BlockList
== NULL
) {
935 // Not enough room to relocate the descriptors
937 return EFI_BUFFER_TOO_SMALL
;
941 // Take the top of memory for the capsule. Naturally align.
943 DestPtr
= FreeMemBase
+ FreeMemSize
- CapsuleSize
;
944 DestPtr
= (UINT8
*) ((UINTN
) DestPtr
&~ (UINTN
) (sizeof (UINTN
) - 1));
945 FreeMemBase
= (UINT8
*) BlockList
+ DescriptorsSize
;
946 FreeMemSize
= FreeMemSize
- DescriptorsSize
- CapsuleSize
;
947 NewCapsuleBase
= (VOID
*) DestPtr
;
950 // Move all the blocks to the top (high) of memory.
951 // Relocate all the obstructing blocks. Note that the block descriptors
952 // were coalesced when they were relocated, so we can just ++ the pointer.
954 CurrentBlockDesc
= BlockList
;
955 while ((CurrentBlockDesc
->Length
!= 0) || (CurrentBlockDesc
->Union
.ContinuationPointer
!= (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
)) {
957 // See if any of the remaining capsule blocks are in the way
959 TempBlockDesc
= CurrentBlockDesc
;
960 while (TempBlockDesc
->Length
!= 0) {
962 // Is this block in the way of where we want to copy the current descriptor to?
966 (UINTN
) CurrentBlockDesc
->Length
,
967 (UINT8
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
,
968 (UINTN
) TempBlockDesc
->Length
971 // Relocate the block
973 RelocPtr
= FindFreeMem (BlockList
, FreeMemBase
, FreeMemSize
, (UINTN
) TempBlockDesc
->Length
);
974 if (RelocPtr
== NULL
) {
975 return EFI_BUFFER_TOO_SMALL
;
978 CopyMem ((VOID
*) RelocPtr
, (VOID
*) (UINTN
) TempBlockDesc
->Union
.DataBlock
, (UINTN
) TempBlockDesc
->Length
);
979 DEBUG ((EFI_D_INFO
, "Capsule reloc data block from 0x%8X to 0x%8X with size 0x%8X\n",
980 (UINTN
) TempBlockDesc
->Union
.DataBlock
, (UINTN
) RelocPtr
, (UINTN
) TempBlockDesc
->Length
));
982 TempBlockDesc
->Union
.DataBlock
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RelocPtr
;
990 // Ok, we made it through. Copy the block.
991 // we just support greping one capsule from the lists of block descs list.
995 //Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA
997 if (CapsuleTimes
> 1) {
999 //For every capsule entry point, check its header to determine whether to relocate it.
1000 //If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.
1002 if (CapsuleBeginFlag
) {
1003 CapsuleBeginFlag
= FALSE
;
1004 CapsuleHeader
= (EFI_CAPSULE_HEADER
*)(UINTN
)CurrentBlockDesc
->Union
.DataBlock
;
1005 SizeLeft
= CapsuleHeader
->CapsuleImageSize
;
1006 if (!IsCapsuleCorrupted (CapsuleHeader
)) {
1008 if (CapsuleIndex
> (MAX_SUPPORT_CAPSULE_NUM
- 1)) {
1009 DEBUG ((EFI_D_ERROR
, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM
));
1010 return EFI_BUFFER_TOO_SMALL
;
1014 // Relocate this valid capsule
1016 IsCorrupted
= FALSE
;
1017 CapsuleImageSize
+= SizeLeft
;
1018 CopyMem ((VOID
*) DestPtr
, (VOID
*) (UINTN
) CurrentBlockDesc
->Union
.DataBlock
, (UINTN
) CurrentBlockDesc
->Length
);
1019 DEBUG ((EFI_D_INFO
, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes
,
1020 (UINTN
)CurrentBlockDesc
->Union
.DataBlock
, (UINTN
)DestPtr
, (UINTN
)CurrentBlockDesc
->Length
));
1022 // Cache the begin offset of this capsule
1024 CapsuleOffset
[CapsuleIndex
++] = (UINT32
) (UINTN
) DestPtr
- (UINT32
)(UINTN
)NewCapsuleBase
- (UINT32
)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA
);
1025 DestPtr
+= CurrentBlockDesc
->Length
;
1028 // If the current block length is greater than or equal to SizeLeft, this is the
1029 // start of the next capsule
1031 if (CurrentBlockDesc
->Length
< SizeLeft
) {
1032 SizeLeft
-= CurrentBlockDesc
->Length
;
1035 // Start the next cycle
1039 CapsuleBeginFlag
= TRUE
;
1043 //Go on relocating the current capule image.
1045 if (CurrentBlockDesc
->Length
< SizeLeft
) {
1047 CopyMem ((VOID
*) DestPtr
, (VOID
*) (UINTN
) (CurrentBlockDesc
->Union
.DataBlock
), (UINTN
)CurrentBlockDesc
->Length
);
1048 DEBUG ((EFI_D_INFO
, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes
,
1049 (UINTN
)CurrentBlockDesc
->Union
.DataBlock
, (UINTN
)DestPtr
, (UINTN
)CurrentBlockDesc
->Length
));
1050 DestPtr
+= CurrentBlockDesc
->Length
;
1052 SizeLeft
-= CurrentBlockDesc
->Length
;
1055 //Here is the end of the current capsule image.
1058 CopyMem ((VOID
*) DestPtr
, (VOID
*)(UINTN
)(CurrentBlockDesc
->Union
.DataBlock
), (UINTN
)CurrentBlockDesc
->Length
);
1059 DEBUG ((EFI_D_INFO
, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes
,
1060 (UINTN
)CurrentBlockDesc
->Union
.DataBlock
, (UINTN
)DestPtr
, (UINTN
)CurrentBlockDesc
->Length
));
1061 DestPtr
+= CurrentBlockDesc
->Length
;
1064 // Start the next cycle
1068 CapsuleBeginFlag
= TRUE
;
1073 //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.
1075 CopyMem ((VOID
*) DestPtr
, (VOID
*) (UINTN
) CurrentBlockDesc
->Union
.DataBlock
, (UINTN
) CurrentBlockDesc
->Length
);
1076 DestPtr
+= CurrentBlockDesc
->Length
;
1079 //Walk through the block descriptor list.
1084 // We return the base of memory we want reserved, and the size.
1085 // The memory peim should handle it appropriately from there.
1087 *MemorySize
= (UINTN
) CapsuleImageSize
;
1088 *MemoryBase
= (VOID
*) NewCapsuleBase
;
1091 //Append the offsets of mutiply capsules to the continous buffer
1093 DataPtr
= (VOID
*)((UINTN
)NewCapsuleBase
+ sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA
) + (UINTN
)CapsuleImageSize
);
1094 AddDataPtr
= (UINT32
*)(((UINTN
) DataPtr
+ sizeof(UINT32
) - 1) &~ (UINT32
) (sizeof (UINT32
) - 1));
1096 *AddDataPtr
++ = CapsuleIndex
;
1098 CopyMem (AddDataPtr
, &CapsuleOffset
[0], sizeof (UINT32
) * CapsuleIndex
);
1100 PrivateDataPtr
= (EFI_CAPSULE_PEIM_PRIVATE_DATA
*) NewCapsuleBase
;
1101 PrivateDataPtr
->CapsuleSize
= (UINT32
) CapsuleImageSize
;