]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsulePei/Common/CapsuleCoalesce.c
Currently, CapsulePei assumes ScatterGatherList are all related. But, UEFI spec has...
[mirror_edk2.git] / MdeModulePkg / Universal / CapsulePei / Common / CapsuleCoalesce.c
1 /** @file
2 The logic to process capsule.
3
4 Copyright (c) 2011 - 2012, 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
9
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.
12
13 **/
14
15 #include <Uefi.h>
16 #include <PiPei.h>
17
18 #include <Guid/CapsuleVendor.h>
19
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/PrintLib.h>
23 #include <Library/BaseLib.h>
24
25 #define MIN_COALESCE_ADDR (1024 * 1024)
26 #define MAX_SUPPORT_CAPSULE_NUM 50
27
28 #define EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('C', 'a', 'p', 'D')
29
30 typedef struct {
31 UINT32 Signature;
32 UINT32 CapsuleSize;
33 } EFI_CAPSULE_PEIM_PRIVATE_DATA;
34
35 /**
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.
39
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
44
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
48 by the BlockList.
49
50 **/
51 UINT8 *
52 FindFreeMem (
53 EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList,
54 UINT8 *MemBase,
55 UINTN MemSize,
56 UINTN DataSize
57 );
58
59 /**
60 Check the integrity of the capsule descriptors.
61
62 @param BlockList Pointer to the capsule descriptors
63
64 @retval NULL BlockList is not valid.
65 @retval LastBlockDesc Last one Block in BlockList
66
67 **/
68 EFI_CAPSULE_BLOCK_DESCRIPTOR *
69 ValidateCapsuleIntegrity (
70 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList
71 );
72
73 /**
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.
77
78 The descriptors passed in can be fragmented throughout memory. Here
79 they are relocated into memory to turn them into a contiguous (null
80 terminated) array.
81
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
86
87 @retval NULL could not relocate the descriptors
88 @retval Pointer to the base of the successfully-relocated block descriptors.
89
90 **/
91 EFI_CAPSULE_BLOCK_DESCRIPTOR *
92 RelocateBlockDescriptors (
93 IN EFI_PEI_SERVICES **PeiServices,
94 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList,
95 IN UINT8 *MemBase,
96 IN UINTN MemSize
97 );
98
99 /**
100 Check every capsule header.
101
102 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
103
104 @retval FALSE Capsule is OK
105 @retval TRUE Capsule is corrupted
106
107 **/
108 BOOLEAN
109 IsCapsuleCorrupted (
110 IN EFI_CAPSULE_HEADER *CapsuleHeader
111 );
112
113 /**
114 Determine if two buffers overlap in memory.
115
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
120
121 @retval TRUE Buffers overlap in memory.
122 @retval FALSE Buffer doesn't overlap.
123
124 **/
125 BOOLEAN
126 IsOverlapped (
127 UINT8 *Buff1,
128 UINTN Size1,
129 UINT8 *Buff2,
130 UINTN Size2
131 );
132
133 /**
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
136 refers to is.
137
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
143
144 @retval EFI_NOT_FOUND No descriptors containing data in the list
145 @retval EFI_SUCCESS Return data is valid
146
147 **/
148 EFI_STATUS
149 GetCapsuleInfo (
150 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc,
151 IN OUT UINTN *NumDescriptors OPTIONAL,
152 IN OUT UINTN *CapsuleSize OPTIONAL
153 );
154
155 /**
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.
159
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
164
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
168 by the BlockList.
169
170 **/
171 UINT8 *
172 FindFreeMem (
173 EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList,
174 UINT8 *MemBase,
175 UINTN MemSize,
176 UINTN DataSize
177 )
178 {
179 UINTN Size;
180 EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrDesc;
181 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempDesc;
182 UINT8 *MemEnd;
183 BOOLEAN Failed;
184
185 //
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.
188 //
189 MemEnd = MemBase + MemSize - DataSize;
190 CurrDesc = BlockList;
191 //
192 // Go through all the descriptor blocks and see if any obstruct the range
193 //
194 while (CurrDesc != NULL) {
195 //
196 // Get the size of this block list and see if it's in the way
197 //
198 Failed = FALSE;
199 TempDesc = CurrDesc;
200 Size = sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
201 while (TempDesc->Length != 0) {
202 Size += sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
203 TempDesc++;
204 }
205
206 if (IsOverlapped (MemBase, DataSize, (UINT8 *) CurrDesc, Size)) {
207 //
208 // Set our new base to the end of this block list and start all over
209 //
210 MemBase = (UINT8 *) CurrDesc + Size;
211 CurrDesc = BlockList;
212 if (MemBase > MemEnd) {
213 return NULL;
214 }
215
216 Failed = TRUE;
217 }
218 //
219 // Now go through all the blocks and make sure none are in the way
220 //
221 while ((CurrDesc->Length != 0) && (!Failed)) {
222 if (IsOverlapped (MemBase, DataSize, (UINT8 *) (UINTN) CurrDesc->Union.DataBlock, (UINTN) CurrDesc->Length)) {
223 //
224 // Set our new base to the end of this block and start all over
225 //
226 Failed = TRUE;
227 MemBase = (UINT8 *) ((UINTN) CurrDesc->Union.DataBlock) + CurrDesc->Length;
228 CurrDesc = BlockList;
229 if (MemBase > MemEnd) {
230 return NULL;
231 }
232 }
233 CurrDesc++;
234 }
235 //
236 // Normal continuation -- jump to next block descriptor list
237 //
238 if (!Failed) {
239 CurrDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) CurrDesc->Union.ContinuationPointer;
240 }
241 }
242 return MemBase;
243 }
244
245 /**
246 Check the integrity of the capsule descriptors.
247
248 @param BlockList Pointer to the capsule descriptors
249
250 @retval NULL BlockList is not valid.
251 @retval LastBlockDesc Last one Block in BlockList
252
253 **/
254 EFI_CAPSULE_BLOCK_DESCRIPTOR *
255 ValidateCapsuleIntegrity (
256 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList
257 )
258 {
259 EFI_CAPSULE_HEADER *CapsuleHeader;
260 UINT64 CapsuleSize;
261 UINT32 CapsuleCount;
262 EFI_CAPSULE_BLOCK_DESCRIPTOR *Ptr;
263
264 //
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)
270 CapsuleSize = 0;
271 CapsuleCount = 0;
272 Ptr = BlockList;
273 while ((Ptr->Length != 0) || (Ptr->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
274 //
275 // Make sure the descriptor is aligned at UINT64 in memory
276 //
277 if ((UINTN) Ptr & 0x07) {
278 DEBUG ((EFI_D_ERROR, "BlockList address failed alignment check\n"));
279 return NULL;
280 }
281
282 if (Ptr->Length == 0) {
283 //
284 // Descriptor points to another list of block descriptors somewhere
285 // else.
286 //
287 Ptr = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Ptr->Union.ContinuationPointer;
288 } else {
289 //
290 //To enhance the reliability of check-up, the first capsule's header is checked here.
291 //More reliabilities check-up will do later.
292 //
293 if (CapsuleSize == 0) {
294 //
295 //Move to the first capsule to check its header.
296 //
297 CapsuleHeader = (EFI_CAPSULE_HEADER*)((UINTN)Ptr->Union.DataBlock);
298 if (IsCapsuleCorrupted (CapsuleHeader)) {
299 return NULL;
300 }
301 CapsuleCount ++;
302 CapsuleSize = CapsuleHeader->CapsuleImageSize;
303 }
304
305 if (CapsuleSize >= Ptr->Length) {
306 CapsuleSize = CapsuleSize - Ptr->Length;
307 } else {
308 CapsuleSize = 0;
309 }
310
311 //
312 // Move to next BLOCK descriptor
313 //
314 Ptr++;
315 }
316 }
317
318 if ((CapsuleCount == 0) || (CapsuleSize != 0)) {
319 //
320 // No any capsule is found in BlockList or capsule data is corrupted.
321 //
322 return NULL;
323 }
324
325 return Ptr;
326 }
327
328 /**
329 The capsule block descriptors may be fragmented and spread all over memory.
330 To simplify the coalescing of capsule blocks, first coalesce all the
331 capsule block descriptors low in memory.
332
333 The descriptors passed in can be fragmented throughout memory. Here
334 they are relocated into memory to turn them into a contiguous (null
335 terminated) array.
336
337 @param PeiServices pointer to PEI services table
338 @param BlockList pointer to the capsule block descriptors
339 @param MemBase base of system memory in which we can work
340 @param MemSize size of the system memory pointed to by MemBase
341
342 @retval NULL could not relocate the descriptors
343 @retval Pointer to the base of the successfully-relocated block descriptors.
344
345 **/
346 EFI_CAPSULE_BLOCK_DESCRIPTOR *
347 RelocateBlockDescriptors (
348 IN EFI_PEI_SERVICES **PeiServices,
349 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList,
350 IN UINT8 *MemBase,
351 IN UINTN MemSize
352 )
353 {
354 EFI_CAPSULE_BLOCK_DESCRIPTOR *NewBlockList;
355 EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrBlockDescHead;
356 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;
357 EFI_CAPSULE_BLOCK_DESCRIPTOR *PrevBlockDescTail;
358 UINTN NumDescriptors;
359 UINTN BufferSize;
360 UINT8 *RelocBuffer;
361 UINTN BlockListSize;
362 //
363 // Get the info on the blocks and descriptors. Since we're going to move
364 // the descriptors low in memory, adjust the base/size values accordingly here.
365 // GetCapsuleInfo() returns the number of legit descriptors, so add one for
366 // a terminator.
367 //
368 if (GetCapsuleInfo (BlockList, &NumDescriptors, NULL) != EFI_SUCCESS) {
369 return NULL;
370 }
371
372 NumDescriptors++;
373 BufferSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
374 NewBlockList = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) MemBase;
375 if (MemSize < BufferSize) {
376 return NULL;
377 }
378
379 MemSize -= BufferSize;
380 MemBase += BufferSize;
381 //
382 // Go through all the blocks and make sure none are in the way
383 //
384 TempBlockDesc = BlockList;
385 while (TempBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
386 if (TempBlockDesc->Length == 0) {
387 //
388 // Next block of descriptors
389 //
390 TempBlockDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;
391 } else {
392 //
393 // If the capsule data pointed to by this descriptor is in the way,
394 // move it.
395 //
396 if (IsOverlapped (
397 (UINT8 *) NewBlockList,
398 BufferSize,
399 (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,
400 (UINTN) TempBlockDesc->Length
401 )) {
402 //
403 // Relocate the block
404 //
405 RelocBuffer = FindFreeMem (BlockList, MemBase, MemSize, (UINTN) TempBlockDesc->Length);
406 if (RelocBuffer == NULL) {
407 return NULL;
408 }
409
410 CopyMem ((VOID *) RelocBuffer, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);
411 TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;
412
413 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));
414 }
415 }
416 TempBlockDesc++;
417 }
418 //
419 // Now go through all the block descriptors to make sure that they're not
420 // in the memory region we want to copy them to.
421 //
422 CurrBlockDescHead = BlockList;
423 PrevBlockDescTail = NULL;
424 while ((CurrBlockDescHead != NULL) && (CurrBlockDescHead->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
425 //
426 // Get the size of this list then see if it overlaps our low region
427 //
428 TempBlockDesc = CurrBlockDescHead;
429 BlockListSize = sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
430 while (TempBlockDesc->Length != 0) {
431 BlockListSize += sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
432 TempBlockDesc++;
433 }
434
435 if (IsOverlapped (
436 (UINT8 *) NewBlockList,
437 BufferSize,
438 (UINT8 *) CurrBlockDescHead,
439 BlockListSize
440 )) {
441 //
442 // Overlaps, so move it out of the way
443 //
444 RelocBuffer = FindFreeMem (BlockList, MemBase, MemSize, BlockListSize);
445 if (RelocBuffer == NULL) {
446 return NULL;
447 }
448 CopyMem ((VOID *) RelocBuffer, (VOID *) CurrBlockDescHead, BlockListSize);
449 DEBUG ((EFI_D_INFO, "Capsule reloc descriptor block #2\n"));
450 //
451 // Point the previous block's next point to this copied version. If
452 // the tail pointer is null, then this is the first descriptor block.
453 //
454 if (PrevBlockDescTail == NULL) {
455 BlockList = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) RelocBuffer;
456 } else {
457 PrevBlockDescTail->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;
458 }
459 }
460 //
461 // Save our new tail and jump to the next block list
462 //
463 PrevBlockDescTail = TempBlockDesc;
464 CurrBlockDescHead = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;
465 }
466 //
467 // Cleared out low memory. Now copy the descriptors down there.
468 //
469 TempBlockDesc = BlockList;
470 CurrBlockDescHead = NewBlockList;
471 while ((TempBlockDesc != NULL) && (TempBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
472 if (TempBlockDesc->Length != 0) {
473 CurrBlockDescHead->Union.DataBlock = TempBlockDesc->Union.DataBlock;
474 CurrBlockDescHead->Length = TempBlockDesc->Length;
475 CurrBlockDescHead++;
476 TempBlockDesc++;
477 } else {
478 TempBlockDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;
479 }
480 }
481 //
482 // Null terminate
483 //
484 CurrBlockDescHead->Union.ContinuationPointer = (EFI_PHYSICAL_ADDRESS) (UINTN) NULL;
485 CurrBlockDescHead->Length = 0;
486 return NewBlockList;
487 }
488
489 /**
490 Determine if two buffers overlap in memory.
491
492 @param Buff1 pointer to first buffer
493 @param Size1 size of Buff1
494 @param Buff2 pointer to second buffer
495 @param Size2 size of Buff2
496
497 @retval TRUE Buffers overlap in memory.
498 @retval FALSE Buffer doesn't overlap.
499
500 **/
501 BOOLEAN
502 IsOverlapped (
503 UINT8 *Buff1,
504 UINTN Size1,
505 UINT8 *Buff2,
506 UINTN Size2
507 )
508 {
509 //
510 // If buff1's end is less than the start of buff2, then it's ok.
511 // Also, if buff1's start is beyond buff2's end, then it's ok.
512 //
513 if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {
514 return FALSE;
515 }
516
517 return TRUE;
518 }
519
520 /**
521 Given a pointer to a capsule block descriptor, traverse the list to figure
522 out how many legitimate descriptors there are, and how big the capsule it
523 refers to is.
524
525 @param Desc Pointer to the capsule block descriptors
526 NumDescriptors - optional pointer to where to return the number of descriptors
527 CapsuleSize - optional pointer to where to return the capsule size
528 @param NumDescriptors Optional pointer to where to return the number of descriptors
529 @param CapsuleSize Optional pointer to where to return the capsule size
530
531 @retval EFI_NOT_FOUND No descriptors containing data in the list
532 @retval EFI_SUCCESS Return data is valid
533
534 **/
535 EFI_STATUS
536 GetCapsuleInfo (
537 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc,
538 IN OUT UINTN *NumDescriptors OPTIONAL,
539 IN OUT UINTN *CapsuleSize OPTIONAL
540 )
541 {
542 UINTN Count;
543 UINTN Size;
544
545 ASSERT (Desc != NULL);
546
547 Count = 0;
548 Size = 0;
549
550 while (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
551 if (Desc->Length == 0) {
552 //
553 // Descriptor points to another list of block descriptors somewhere
554 //
555 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;
556 } else {
557 Size += (UINTN) Desc->Length;
558 Count++;
559 Desc++;
560 }
561 }
562 //
563 // If no descriptors, then fail
564 //
565 if (Count == 0) {
566 return EFI_NOT_FOUND;
567 }
568
569 if (NumDescriptors != NULL) {
570 *NumDescriptors = Count;
571 }
572
573 if (CapsuleSize != NULL) {
574 *CapsuleSize = Size;
575 }
576
577 return EFI_SUCCESS;
578 }
579
580 /**
581 Check every capsule header.
582
583 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
584
585 @retval FALSE Capsule is OK
586 @retval TRUE Capsule is corrupted
587
588 **/
589 BOOLEAN
590 IsCapsuleCorrupted (
591 IN EFI_CAPSULE_HEADER *CapsuleHeader
592 )
593 {
594 //
595 //A capsule to be updated across a system reset should contain CAPSULE_FLAGS_PERSIST_ACROSS_RESET.
596 //
597 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {
598 return TRUE;
599 }
600 //
601 //Make sure the flags combination is supported by the platform.
602 //
603 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
604 return TRUE;
605 }
606 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
607 return TRUE;
608 }
609
610 return FALSE;
611 }
612
613 /**
614 Try to verify the integrity of a capsule test pattern before the
615 capsule gets coalesced. This can be useful in narrowing down
616 where capsule data corruption occurs.
617
618 The test pattern mode fills in memory with a counting UINT32 value.
619 If the capsule is not divided up in a multiple of 4-byte blocks, then
620 things get messy doing the check. Therefore there are some cases
621 here where we just give up and skip the pre-coalesce check.
622
623 @param PeiServices PEI services table
624 @param Desc Pointer to capsule descriptors
625 **/
626 VOID
627 CapsuleTestPatternPreCoalesce (
628 IN EFI_PEI_SERVICES **PeiServices,
629 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc
630 )
631 {
632 UINT32 *TestPtr;
633 UINT32 TestCounter;
634 UINT32 TestSize;
635 //
636 // Find first data descriptor
637 //
638 while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
639 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;
640 }
641
642 if (Desc->Union.ContinuationPointer == 0) {
643 return ;
644 }
645 //
646 // First one better be long enough to at least hold the test signature
647 //
648 if (Desc->Length < sizeof (UINT32)) {
649 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #1\n"));
650 return ;
651 }
652
653 TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;
654 //
655 // 0x54534554 "TEST"
656 //
657 if (*TestPtr != 0x54534554) {
658 return ;
659 }
660
661 TestCounter = 0;
662 TestSize = (UINT32) Desc->Length - 2 * sizeof (UINT32);
663 //
664 // Skip over the signature and the size fields in the pattern data header
665 //
666 TestPtr += 2;
667 while (1) {
668 if ((TestSize & 0x03) != 0) {
669 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #2\n"));
670 return ;
671 }
672
673 while (TestSize > 0) {
674 if (*TestPtr != TestCounter) {
675 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce failed data corruption check\n"));
676 return ;
677 }
678
679 TestSize -= sizeof (UINT32);
680 TestCounter++;
681 TestPtr++;
682 }
683 Desc++;
684 while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
685 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;
686 }
687
688 if (Desc->Union.ContinuationPointer == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
689 return ;
690 }
691 TestSize = (UINT32) Desc->Length;
692 TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;
693 }
694 }
695
696 /**
697 Checks for the presence of capsule descriptors.
698 Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
699
700 @param BlockListBuffer Pointer to the buffer of capsule descriptors variables
701 @param BlockDescriptorList Pointer to the capsule descriptors list
702
703 @retval EFI_SUCCESS a valid capsule is present
704 @retval EFI_NOT_FOUND if a valid capsule is not present
705 **/
706 EFI_STATUS
707 BuildCapsuleDescriptors (
708 IN EFI_PHYSICAL_ADDRESS *BlockListBuffer,
709 OUT EFI_CAPSULE_BLOCK_DESCRIPTOR **BlockDescriptorList
710 )
711 {
712 UINTN Index;
713 EFI_CAPSULE_BLOCK_DESCRIPTOR *LastBlock;
714 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlock;
715 EFI_CAPSULE_BLOCK_DESCRIPTOR *HeadBlock;
716
717 LastBlock = NULL;
718 HeadBlock = NULL;
719 TempBlock = NULL;
720 Index = 0;
721
722 while (BlockListBuffer[Index] != 0) {
723 //
724 // Test integrity of descriptors.
725 //
726 TempBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index]);
727 if (TempBlock != NULL) {
728 if (LastBlock == NULL) {
729 LastBlock = TempBlock;
730
731 //
732 // Return the base of the block descriptors
733 //
734 HeadBlock = (EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index];
735 } else {
736 //
737 // Combine the different BlockList into single BlockList.
738 //
739 LastBlock->Union.DataBlock = (EFI_PHYSICAL_ADDRESS)(UINTN)BlockListBuffer[Index];
740 LastBlock->Length = 0;
741 LastBlock = TempBlock;
742 }
743 }
744 Index ++;
745 }
746
747 if (HeadBlock != NULL) {
748 *BlockDescriptorList = HeadBlock;
749 return EFI_SUCCESS;
750 }
751 return EFI_NOT_FOUND;
752 }
753
754 /**
755 The function to coalesce a fragmented capsule in memory.
756
757 Memory Map for coalesced capsule:
758 MemBase + ---->+---------------------------+<-----------+
759 MemSize | CapsuleOffset[49] | |
760 +---------------------------+ |
761 | ................ | |
762 +---------------------------+ |
763 | CapsuleOffset[2] | |
764 +---------------------------+ |
765 | CapsuleOffset[1] | |
766 +---------------------------+ |
767 | CapsuleOffset[0] | CapsuleSize
768 +---------------------------+ |
769 | CapsuleNumber | |
770 +---------------------------+ |
771 | | |
772 | | |
773 | Capsule Image | |
774 | | |
775 | | |
776 +---------------------------+ |
777 | PrivateData | |
778 DestPtr ----> +---------------------------+<-----------+
779 | | |
780 | FreeMem | FreeMemSize
781 | | |
782 FreeMemBase --->+---------------------------+<-----------+
783 | Terminator |
784 +---------------------------+
785 | BlockDescriptor n |
786 +---------------------------+
787 | ................. |
788 +---------------------------+
789 | BlockDescriptor 1 |
790 +---------------------------+
791 | BlockDescriptor 0 |
792 +---------------------------+
793 | PrivateDataDesc 0 |
794 MemBase ---->+---------------------------+<----- BlockList
795
796 @param PeiServices General purpose services available to every PEIM.
797 @param BlockListBuffer Point to the buffer of Capsule Descriptor Variables.
798 @param MemoryBase Pointer to the base of a block of memory that we can walk
799 all over while trying to coalesce our buffers.
800 On output, this variable will hold the base address of
801 a coalesced capsule.
802 @param MemorySize Size of the memory region pointed to by MemoryBase.
803 On output, this variable will contain the size of the
804 coalesced capsule.
805
806 @retval EFI_NOT_FOUND If we could not find the capsule descriptors.
807
808 @retval EFI_BUFFER_TOO_SMALL
809 If we could not coalesce the capsule in the memory
810 region provided to us.
811
812 @retval EFI_SUCCESS Processed the capsule successfully.
813 **/
814 EFI_STATUS
815 EFIAPI
816 CapsuleDataCoalesce (
817 IN EFI_PEI_SERVICES **PeiServices,
818 IN EFI_PHYSICAL_ADDRESS *BlockListBuffer,
819 IN OUT VOID **MemoryBase,
820 IN OUT UINTN *MemorySize
821 )
822 {
823 VOID *NewCapsuleBase;
824 VOID *DataPtr;
825 UINT8 CapsuleIndex;
826 UINT8 *FreeMemBase;
827 UINT8 *DestPtr;
828 UINT8 *RelocPtr;
829 UINT32 CapsuleOffset[MAX_SUPPORT_CAPSULE_NUM];
830 UINT32 *AddDataPtr;
831 UINT32 CapsuleTimes;
832 UINT64 SizeLeft;
833 UINT64 CapsuleImageSize;
834 UINTN CapsuleSize;
835 UINTN DescriptorsSize;
836 UINTN FreeMemSize;
837 UINTN NumDescriptors;
838 BOOLEAN IsCorrupted;
839 BOOLEAN CapsuleBeginFlag;
840 EFI_STATUS Status;
841 EFI_CAPSULE_HEADER *CapsuleHeader;
842 EFI_CAPSULE_PEIM_PRIVATE_DATA PrivateData;
843 EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateDataPtr;
844 EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList;
845 EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrentBlockDesc;
846 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;
847 EFI_CAPSULE_BLOCK_DESCRIPTOR PrivateDataDesc[2];
848
849 CapsuleIndex = 0;
850 SizeLeft = 0;
851 CapsuleTimes = 0;
852 CapsuleImageSize = 0;
853 PrivateDataPtr = NULL;
854 AddDataPtr = NULL;
855 CapsuleHeader = NULL;
856 CapsuleBeginFlag = TRUE;
857 IsCorrupted = TRUE;
858 CapsuleSize = 0;
859 NumDescriptors = 0;
860
861 //
862 // Build capsule descriptors list
863 //
864 Status = BuildCapsuleDescriptors (BlockListBuffer, &BlockList);
865 if (EFI_ERROR (Status)) {
866 return Status;
867 }
868
869 DEBUG_CODE (
870 CapsuleTestPatternPreCoalesce (PeiServices, BlockList);
871 );
872
873 //
874 // Get the size of our descriptors and the capsule size. GetCapsuleInfo()
875 // returns the number of descriptors that actually point to data, so add
876 // one for a terminator. Do that below.
877 //
878 GetCapsuleInfo (BlockList, &NumDescriptors, &CapsuleSize);
879 if ((CapsuleSize == 0) || (NumDescriptors == 0)) {
880 return EFI_NOT_FOUND;
881 }
882
883 //
884 // Initialize our local copy of private data. When we're done, we'll create a
885 // descriptor for it as well so that it can be put into free memory without
886 // trashing anything.
887 //
888 PrivateData.Signature = EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE;
889 PrivateData.CapsuleSize = (UINT32) CapsuleSize;
890 PrivateDataDesc[0].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) &PrivateData;
891 PrivateDataDesc[0].Length = sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA);
892 PrivateDataDesc[1].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) BlockList;
893 PrivateDataDesc[1].Length = 0;
894 //
895 // In addition to PrivateDataDesc[1:0], one terminator is added
896 // See below RelocateBlockDescriptors()
897 //
898 NumDescriptors += 3;
899 CapsuleSize += sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + sizeof(CapsuleOffset) + sizeof(UINT32);
900 BlockList = PrivateDataDesc;
901 DescriptorsSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
902
903 //
904 // Don't go below some min address. If the base is below it,
905 // then move it up and adjust the size accordingly.
906 //
907 DEBUG ((EFI_D_INFO, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN) *MemoryBase, (UINTN)*MemoryBase + *MemorySize));
908 if ((UINTN)*MemoryBase < (UINTN) MIN_COALESCE_ADDR) {
909 if (((UINTN)*MemoryBase + *MemorySize) < (UINTN) MIN_COALESCE_ADDR) {
910 return EFI_BUFFER_TOO_SMALL;
911 } else {
912 *MemorySize = *MemorySize - ((UINTN) MIN_COALESCE_ADDR - (UINTN) *MemoryBase);
913 *MemoryBase = (VOID *) (UINTN) MIN_COALESCE_ADDR;
914 }
915 }
916
917 if (*MemorySize <= (CapsuleSize + DescriptorsSize)) {
918 return EFI_BUFFER_TOO_SMALL;
919 }
920
921 FreeMemBase = *MemoryBase;
922 FreeMemSize = *MemorySize;
923 DEBUG ((EFI_D_INFO, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN) FreeMemBase, (UINTN) FreeMemBase + FreeMemSize));
924
925 //
926 // Relocate all the block descriptors to low memory to make further
927 // processing easier.
928 //
929 BlockList = RelocateBlockDescriptors (PeiServices, BlockList, FreeMemBase, FreeMemSize);
930 if (BlockList == NULL) {
931 //
932 // Not enough room to relocate the descriptors
933 //
934 return EFI_BUFFER_TOO_SMALL;
935 }
936
937 //
938 // Take the top of memory for the capsule. Naturally align.
939 //
940 DestPtr = FreeMemBase + FreeMemSize - CapsuleSize;
941 DestPtr = (UINT8 *) ((UINTN) DestPtr &~ (UINTN) (sizeof (UINTN) - 1));
942 FreeMemBase = (UINT8 *) BlockList + DescriptorsSize;
943 FreeMemSize = (UINTN) DestPtr - (UINTN) FreeMemBase;
944 NewCapsuleBase = (VOID *) DestPtr;
945
946 //
947 // Move all the blocks to the top (high) of memory.
948 // Relocate all the obstructing blocks. Note that the block descriptors
949 // were coalesced when they were relocated, so we can just ++ the pointer.
950 //
951 CurrentBlockDesc = BlockList;
952 while ((CurrentBlockDesc->Length != 0) || (CurrentBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
953 //
954 // See if any of the remaining capsule blocks are in the way
955 //
956 TempBlockDesc = CurrentBlockDesc;
957 while (TempBlockDesc->Length != 0) {
958 //
959 // Is this block in the way of where we want to copy the current descriptor to?
960 //
961 if (IsOverlapped (
962 (UINT8 *) DestPtr,
963 (UINTN) CurrentBlockDesc->Length,
964 (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,
965 (UINTN) TempBlockDesc->Length
966 )) {
967 //
968 // Relocate the block
969 //
970 RelocPtr = FindFreeMem (BlockList, FreeMemBase, FreeMemSize, (UINTN) TempBlockDesc->Length);
971 if (RelocPtr == NULL) {
972 return EFI_BUFFER_TOO_SMALL;
973 }
974
975 CopyMem ((VOID *) RelocPtr, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);
976 DEBUG ((EFI_D_INFO, "Capsule reloc data block from 0x%8X to 0x%8X with size 0x%8X\n",
977 (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) RelocPtr, (UINTN) TempBlockDesc->Length));
978
979 TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocPtr;
980 }
981 //
982 // Next descriptor
983 //
984 TempBlockDesc++;
985 }
986 //
987 // Ok, we made it through. Copy the block.
988 // we just support greping one capsule from the lists of block descs list.
989 //
990 CapsuleTimes ++;
991 //
992 //Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA
993 //
994 if (CapsuleTimes > 1) {
995 //
996 //For every capsule entry point, check its header to determine whether to relocate it.
997 //If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.
998 //
999 if (CapsuleBeginFlag) {
1000 CapsuleBeginFlag = FALSE;
1001 CapsuleHeader = (EFI_CAPSULE_HEADER*)(UINTN)CurrentBlockDesc->Union.DataBlock;
1002 SizeLeft = CapsuleHeader->CapsuleImageSize;
1003 if (!IsCapsuleCorrupted (CapsuleHeader)) {
1004
1005 if (CapsuleIndex > (MAX_SUPPORT_CAPSULE_NUM - 1)) {
1006 DEBUG ((EFI_D_ERROR, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM));
1007 return EFI_BUFFER_TOO_SMALL;
1008 }
1009
1010 //
1011 // Relocate this valid capsule
1012 //
1013 IsCorrupted = FALSE;
1014 CapsuleImageSize += SizeLeft;
1015 //
1016 // Cache the begin offset of this capsule
1017 //
1018 CapsuleOffset[CapsuleIndex++] = (UINT32) (UINTN) DestPtr - (UINT32)(UINTN)NewCapsuleBase - (UINT32)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA);
1019 }
1020 }
1021
1022 if (CurrentBlockDesc->Length < SizeLeft) {
1023 if (!IsCorrupted) {
1024 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) (CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);
1025 DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,
1026 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));
1027 DestPtr += CurrentBlockDesc->Length;
1028 }
1029 SizeLeft -= CurrentBlockDesc->Length;
1030 } else {
1031 //
1032 //Here is the end of the current capsule image.
1033 //
1034 if (!IsCorrupted) {
1035 CopyMem ((VOID *) DestPtr, (VOID *)(UINTN)(CurrentBlockDesc->Union.DataBlock), (UINTN) SizeLeft);
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) SizeLeft));
1038 DestPtr += SizeLeft;
1039 }
1040 //
1041 // Start the next cycle
1042 //
1043 SizeLeft = 0;
1044 IsCorrupted = TRUE;
1045 CapsuleBeginFlag = TRUE;
1046 }
1047 } else {
1048 //
1049 //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.
1050 //
1051 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);
1052 DestPtr += CurrentBlockDesc->Length;
1053 }
1054 //
1055 //Walk through the block descriptor list.
1056 //
1057 CurrentBlockDesc++;
1058 }
1059 //
1060 // We return the base of memory we want reserved, and the size.
1061 // The memory peim should handle it appropriately from there.
1062 //
1063 *MemorySize = (UINTN) CapsuleSize;
1064 *MemoryBase = (VOID *) NewCapsuleBase;
1065
1066 //
1067 //Append the offsets of mutiply capsules to the continous buffer
1068 //
1069 DataPtr = (VOID*)((UINTN)NewCapsuleBase + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (UINTN)CapsuleImageSize);
1070 AddDataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) &~ (UINT32) (sizeof (UINT32) - 1));
1071
1072 *AddDataPtr++ = CapsuleIndex;
1073
1074 CopyMem (AddDataPtr, &CapsuleOffset[0], sizeof (UINT32) * CapsuleIndex);
1075
1076 PrivateDataPtr = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) NewCapsuleBase;
1077 PrivateDataPtr->CapsuleSize = (UINT32) CapsuleImageSize;
1078
1079 return EFI_SUCCESS;
1080 }