]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsulePei/UefiCapsule.c
MdeModulePkg: Add Capsule PEIM
[mirror_edk2.git] / MdeModulePkg / Universal / CapsulePei / UefiCapsule.c
1 /** @file
2 Capsule update PEIM for UEFI2.0
3
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "Capsule.h"
18
19 EFI_PHYSICAL_ADDRESS *mBufferAddress;
20
21 /**
22 Check every capsule header.
23
24 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
25
26 @retval FALSE Capsule is OK
27 @retval TRUE Capsule is corrupted
28
29 **/
30 BOOLEAN
31 IsCapsuleCorrupted (
32 IN EFI_CAPSULE_HEADER *CapsuleHeader
33 )
34 {
35 //
36 //A capsule to be updated across a system reset should contain CAPSULE_FLAGS_PERSIST_ACROSS_RESET.
37 //
38 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {
39 return TRUE;
40 }
41 //
42 //Make sure the flags combination is supported by the platform.
43 //
44 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
45 return TRUE;
46 }
47 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
48 return TRUE;
49 }
50
51 return FALSE;
52 }
53
54 /**
55 Check the integrity of the capsule descriptors.
56
57 @param BlockList Pointer to the capsule descriptors
58
59 @retval NULL BlockList is not valid.
60 @retval LastBlockDesc Last one Block in BlockList
61
62 **/
63 EFI_CAPSULE_BLOCK_DESCRIPTOR *
64 ValidateCapsuleIntegrity (
65 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList
66 )
67 {
68 EFI_CAPSULE_HEADER *CapsuleHeader;
69 UINT64 CapsuleSize;
70 UINT32 CapsuleCount;
71 EFI_CAPSULE_BLOCK_DESCRIPTOR *Ptr;
72
73 //
74 // Go through the list to look for inconsistencies. Check for:
75 // * misaligned block descriptors.
76 // * The first capsule header guid
77 // * The first capsule header flag
78 // * Data + Length < Data (wrap)
79 CapsuleSize = 0;
80 CapsuleCount = 0;
81 Ptr = BlockList;
82 while ((Ptr->Length != 0) || (Ptr->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
83 //
84 // Make sure the descriptor is aligned at UINT64 in memory
85 //
86 if ((UINTN) Ptr & 0x07) {
87 DEBUG ((EFI_D_ERROR, "BlockList address failed alignment check\n"));
88 return NULL;
89 }
90
91 if (Ptr->Length == 0) {
92 //
93 // Descriptor points to another list of block descriptors somewhere
94 // else.
95 //
96 Ptr = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Ptr->Union.ContinuationPointer;
97 } else {
98 //
99 //To enhance the reliability of check-up, the first capsule's header is checked here.
100 //More reliabilities check-up will do later.
101 //
102 if (CapsuleSize == 0) {
103 //
104 //Move to the first capsule to check its header.
105 //
106 CapsuleHeader = (EFI_CAPSULE_HEADER*)((UINTN)Ptr->Union.DataBlock);
107 if (IsCapsuleCorrupted (CapsuleHeader)) {
108 return NULL;
109 }
110 CapsuleCount ++;
111 CapsuleSize = CapsuleHeader->CapsuleImageSize;
112 } else {
113 if (CapsuleSize >= Ptr->Length) {
114 CapsuleSize = CapsuleSize - Ptr->Length;
115 } else {
116 CapsuleSize = 0;
117 }
118 }
119 //
120 // Move to next BLOCK descriptor
121 //
122 Ptr++;
123 }
124 }
125
126 if (CapsuleCount == 0) {
127 //
128 // No any capsule is found in BlockList.
129 //
130 return NULL;
131 }
132
133 return Ptr;
134 }
135
136
137 /**
138 Checks for the presence of capsule descriptors.
139 Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
140
141 @param BlockList Pointer to the capsule descriptors
142
143 @retval EFI_SUCCESS a valid capsule is present
144 @retval EFI_NOT_FOUND if a valid capsule is not present
145 **/
146 EFI_STATUS
147 GetCapsuleDescriptors (
148 IN OUT EFI_CAPSULE_BLOCK_DESCRIPTOR **BlockList OPTIONAL
149 )
150 {
151 EFI_STATUS Status;
152 UINTN Size;
153 UINTN Index;
154 UINTN TempIndex;
155 UINTN ValidIndex;
156 BOOLEAN Flag;
157 CHAR16 CapsuleVarName[30];
158 CHAR16 *TempVarName;
159 EFI_PHYSICAL_ADDRESS CapsuleDataPtr64;
160 EFI_CAPSULE_BLOCK_DESCRIPTOR *LastBlock;
161 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlock;
162 EFI_CAPSULE_BLOCK_DESCRIPTOR *HeadBlock;
163 EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices;
164
165 LastBlock = NULL;
166 HeadBlock = NULL;
167 TempBlock = NULL;
168 Index = 0;
169 TempVarName = NULL;
170 CapsuleVarName[0] = 0;
171 ValidIndex = 0;
172
173 Status = PeiServicesLocatePpi (
174 &gEfiPeiReadOnlyVariable2PpiGuid,
175 0,
176 NULL,
177 (VOID **) &PPIVariableServices
178 );
179 if (Status == EFI_SUCCESS) {
180 StrCpy (CapsuleVarName, EFI_CAPSULE_VARIABLE_NAME);
181 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);
182 Size = sizeof (CapsuleDataPtr64);
183 while (1) {
184 if (Index == 0) {
185 //
186 // For the first Capsule Image
187 //
188 Status = PPIVariableServices->GetVariable (
189 PPIVariableServices,
190 CapsuleVarName,
191 &gEfiCapsuleVendorGuid,
192 NULL,
193 &Size,
194 (VOID *) &CapsuleDataPtr64
195 );
196 if (EFI_ERROR (Status)) {
197 DEBUG ((EFI_D_ERROR, "Capsule -- capsule variable not set\n"));
198 return EFI_NOT_FOUND;
199 }
200 //
201 // We have a chicken/egg situation where the memory init code needs to
202 // know the boot mode prior to initializing memory. For this case, our
203 // validate function will fail. We can detect if this is the case if blocklist
204 // pointer is null. In that case, return success since we know that the
205 // variable is set.
206 //
207 if (BlockList == NULL) {
208 return EFI_SUCCESS;
209 }
210 //
211 // Test integrity of descriptors.
212 //
213 LastBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)CapsuleDataPtr64);
214 if (LastBlock == NULL) {
215 return EFI_NOT_FOUND;
216 }
217 //
218 // Return the base of the block descriptors
219 //
220 HeadBlock = (EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)CapsuleDataPtr64;
221 } else {
222 UnicodeValueToString (TempVarName, 0, Index, 0);
223 Status = PPIVariableServices->GetVariable (
224 PPIVariableServices,
225 CapsuleVarName,
226 &gEfiCapsuleVendorGuid,
227 NULL,
228 &Size,
229 (VOID *) &CapsuleDataPtr64
230 );
231 if (EFI_ERROR (Status)) {
232 break;
233 }
234
235 //
236 // If this BlockList has been linked before, skip this variable
237 //
238 Flag = FALSE;
239 for (TempIndex = 0; TempIndex < ValidIndex; TempIndex++) {
240 if (mBufferAddress[TempIndex] == CapsuleDataPtr64) {
241 Flag = TRUE;
242 break;
243 }
244 }
245 if (Flag) {
246 Index ++;
247 continue;
248 }
249
250 //
251 // Test integrity of descriptors.
252 //
253 TempBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)CapsuleDataPtr64);
254 if (TempBlock == NULL) {
255 return EFI_NOT_FOUND;
256 }
257 //
258 // Combine the different BlockList into single BlockList.
259 //
260 LastBlock->Union.DataBlock = CapsuleDataPtr64;
261 LastBlock->Length = 0;
262 LastBlock = TempBlock;
263 }
264
265 //
266 // Cache BlockList which has been processed
267 //
268 mBufferAddress[ValidIndex++] = CapsuleDataPtr64;
269 Index ++;
270 }
271 }
272
273 if (HeadBlock != NULL) {
274 *BlockList = HeadBlock;
275 return EFI_SUCCESS;
276 }
277 return EFI_NOT_FOUND;
278 }
279
280 /**
281 Given a pointer to a capsule block descriptor, traverse the list to figure
282 out how many legitimate descriptors there are, and how big the capsule it
283 refers to is.
284
285 @param Desc Pointer to the capsule block descriptors
286 NumDescriptors - optional pointer to where to return the number of descriptors
287 CapsuleSize - optional pointer to where to return the capsule size
288 @param NumDescriptors Optional pointer to where to return the number of descriptors
289 @param CapsuleSize Optional pointer to where to return the capsule size
290
291 @retval EFI_NOT_FOUND No descriptors containing data in the list
292 @retval EFI_SUCCESS Return data is valid
293 **/
294 EFI_STATUS
295 GetCapsuleInfo (
296 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc,
297 IN OUT UINTN *NumDescriptors OPTIONAL,
298 IN OUT UINTN *CapsuleSize OPTIONAL
299 )
300 {
301 UINTN Count;
302 UINTN Size;
303
304 ASSERT (Desc != NULL);
305
306 Count = 0;
307 Size = 0;
308
309 while (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
310 if (Desc->Length == 0) {
311 //
312 // Descriptor points to another list of block descriptors somewhere
313 //
314 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;
315 } else {
316 Size += (UINTN) Desc->Length;
317 Count++;
318 Desc++;
319 }
320 }
321 //
322 // If no descriptors, then fail
323 //
324 if (Count == 0) {
325 return EFI_NOT_FOUND;
326 }
327
328 if (NumDescriptors != NULL) {
329 *NumDescriptors = Count;
330 }
331
332 if (CapsuleSize != NULL) {
333 *CapsuleSize = Size;
334 }
335
336 return EFI_SUCCESS;
337 }
338
339
340 /**
341 Try to verify the integrity of a capsule test pattern before the
342 capsule gets coalesced. This can be useful in narrowing down
343 where capsule data corruption occurs.
344
345 The test pattern mode fills in memory with a counting UINT32 value.
346 If the capsule is not divided up in a multiple of 4-byte blocks, then
347 things get messy doing the check. Therefore there are some cases
348 here where we just give up and skip the pre-coalesce check.
349
350 @param PeiServices PEI services table
351 @param Desc Pointer to capsule descriptors
352 **/
353 VOID
354 CapsuleTestPatternPreCoalesce (
355 IN EFI_PEI_SERVICES **PeiServices,
356 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc
357 )
358 {
359 UINT32 *TestPtr;
360 UINT32 TestCounter;
361 UINT32 TestSize;
362 //
363 // Find first data descriptor
364 //
365 while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
366 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;
367 }
368
369 if (Desc->Union.ContinuationPointer == 0) {
370 return ;
371 }
372 //
373 // First one better be long enough to at least hold the test signature
374 //
375 if (Desc->Length < sizeof (UINT32)) {
376 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #1\n"));
377 return ;
378 }
379
380 TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;
381 if (*TestPtr != CAPSULE_TEST_SIGNATURE) {
382 return ;
383 }
384
385 TestCounter = 0;
386 TestSize = (UINT32) Desc->Length - 2 * sizeof (UINT32);
387 //
388 // Skip over the signature and the size fields in the pattern data header
389 //
390 TestPtr += 2;
391 while (1) {
392 if ((TestSize & 0x03) != 0) {
393 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #2\n"));
394 return ;
395 }
396
397 while (TestSize > 0) {
398 if (*TestPtr != TestCounter) {
399 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce failed data corruption check\n"));
400 return ;
401 }
402
403 TestSize -= sizeof (UINT32);
404 TestCounter++;
405 TestPtr++;
406 }
407 Desc++;
408 while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
409 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;
410 }
411
412 if (Desc->Union.ContinuationPointer == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
413 return ;
414 }
415 TestSize = (UINT32) Desc->Length;
416 TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;
417 }
418 }
419
420
421 /**
422 Determine if two buffers overlap in memory.
423
424 @param Buff1 pointer to first buffer
425 @param Size1 size of Buff1
426 @param Buff2 pointer to second buffer
427 @param Size2 size of Buff2
428
429 @retval TRUE Buffers overlap in memory.
430 @retval FALSE Buffer doesn't overlap.
431 **/
432 BOOLEAN
433 IsOverlapped (
434 UINT8 *Buff1,
435 UINTN Size1,
436 UINT8 *Buff2,
437 UINTN Size2
438 )
439 {
440 //
441 // If buff1's end is less than the start of buff2, then it's ok.
442 // Also, if buff1's start is beyond buff2's end, then it's ok.
443 //
444 if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {
445 return FALSE;
446 }
447
448 return TRUE;
449 }
450
451 /**
452 Given a pointer to the capsule block list, info on the available system
453 memory, and the size of a buffer, find a free block of memory where a
454 buffer of the given size can be copied to safely.
455
456 @param BlockList Pointer to head of capsule block descriptors
457 @param MemBase Pointer to the base of memory in which we want to find free space
458 @param MemSize The size of the block of memory pointed to by MemBase
459 @param DataSize How big a free block we want to find
460
461 @return A pointer to a memory block of at least DataSize that lies somewhere
462 between MemBase and (MemBase + MemSize). The memory pointed to does not
463 contain any of the capsule block descriptors or capsule blocks pointed to
464 by the BlockList.
465 **/
466 UINT8 *
467 FindFreeMem (
468 EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList,
469 UINT8 *MemBase,
470 UINTN MemSize,
471 UINTN DataSize
472 )
473 {
474 UINTN Size;
475 EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrDesc;
476 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempDesc;
477 UINT8 *MemEnd;
478 BOOLEAN Failed;
479
480 //
481 // Need at least enough to copy the data to at the end of the buffer, so
482 // say the end is less the data size for easy comparisons here.
483 //
484 MemEnd = MemBase + MemSize - DataSize;
485 CurrDesc = BlockList;
486 //
487 // Go through all the descriptor blocks and see if any obstruct the range
488 //
489 while (CurrDesc != NULL) {
490 //
491 // Get the size of this block list and see if it's in the way
492 //
493 Failed = FALSE;
494 TempDesc = CurrDesc;
495 Size = sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
496 while (TempDesc->Length != 0) {
497 Size += sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
498 TempDesc++;
499 }
500
501 if (IsOverlapped (MemBase, DataSize, (UINT8 *) CurrDesc, Size)) {
502 //
503 // Set our new base to the end of this block list and start all over
504 //
505 MemBase = (UINT8 *) CurrDesc + Size;
506 CurrDesc = BlockList;
507 if (MemBase > MemEnd) {
508 return NULL;
509 }
510
511 Failed = TRUE;
512 }
513 //
514 // Now go through all the blocks and make sure none are in the way
515 //
516 while ((CurrDesc->Length != 0) && (!Failed)) {
517 if (IsOverlapped (MemBase, DataSize, (UINT8 *) (UINTN) CurrDesc->Union.DataBlock, (UINTN) CurrDesc->Length)) {
518 //
519 // Set our new base to the end of this block and start all over
520 //
521 Failed = TRUE;
522 MemBase = (UINT8 *) ((UINTN) CurrDesc->Union.DataBlock) + CurrDesc->Length;
523 CurrDesc = BlockList;
524 if (MemBase > MemEnd) {
525 return NULL;
526 }
527 }
528 CurrDesc++;
529 }
530 //
531 // Normal continuation -- jump to next block descriptor list
532 //
533 if (!Failed) {
534 CurrDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) CurrDesc->Union.ContinuationPointer;
535 }
536 }
537 return MemBase;
538 }
539
540 /**
541 The capsule block descriptors may be fragmented and spread all over memory.
542 To simplify the coalescing of capsule blocks, first coalesce all the
543 capsule block descriptors low in memory.
544
545 The descriptors passed in can be fragmented throughout memory. Here
546 they are relocated into memory to turn them into a contiguous (null
547 terminated) array.
548
549 @param PeiServices pointer to PEI services table
550 @param BlockList pointer to the capsule block descriptors
551 @param MemBase base of system memory in which we can work
552 @param MemSize size of the system memory pointed to by MemBase
553
554 @retval NULL could not relocate the descriptors
555 @retval Pointer to the base of the successfully-relocated block descriptors.
556 **/
557 EFI_CAPSULE_BLOCK_DESCRIPTOR *
558 RelocateBlockDescriptors (
559 IN EFI_PEI_SERVICES **PeiServices,
560 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList,
561 IN UINT8 *MemBase,
562 IN UINTN MemSize
563 )
564 {
565 EFI_CAPSULE_BLOCK_DESCRIPTOR *NewBlockList;
566 EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrBlockDescHead;
567 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;
568 EFI_CAPSULE_BLOCK_DESCRIPTOR *PrevBlockDescTail;
569 UINTN NumDescriptors;
570 UINTN BufferSize;
571 UINT8 *RelocBuffer;
572 UINTN BlockListSize;
573 //
574 // Get the info on the blocks and descriptors. Since we're going to move
575 // the descriptors low in memory, adjust the base/size values accordingly here.
576 // GetCapsuleInfo() returns the number of legit descriptors, so add one for
577 // a terminator.
578 //
579 if (GetCapsuleInfo (BlockList, &NumDescriptors, NULL) != EFI_SUCCESS) {
580 return NULL;
581 }
582
583 NumDescriptors++;
584 BufferSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
585 NewBlockList = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) MemBase;
586 if (MemSize < BufferSize) {
587 return NULL;
588 }
589
590 MemSize -= BufferSize;
591 MemBase += BufferSize;
592 //
593 // Go through all the blocks and make sure none are in the way
594 //
595 TempBlockDesc = BlockList;
596 while (TempBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
597 if (TempBlockDesc->Length == 0) {
598 //
599 // Next block of descriptors
600 //
601 TempBlockDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;
602 } else {
603 //
604 // If the capsule data pointed to by this descriptor is in the way,
605 // move it.
606 //
607 if (IsOverlapped (
608 (UINT8 *) NewBlockList,
609 BufferSize,
610 (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,
611 (UINTN) TempBlockDesc->Length
612 )) {
613 //
614 // Relocate the block
615 //
616 RelocBuffer = FindFreeMem (BlockList, MemBase, MemSize, (UINTN) TempBlockDesc->Length);
617 if (RelocBuffer == NULL) {
618 return NULL;
619 }
620
621 CopyMem ((VOID *) RelocBuffer, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);
622 TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;
623
624 DEBUG ((EFI_D_INFO, "Capsule relocate descriptors from/to/size 0x%X 0x%X 0x%X\n", (UINT32)(UINTN)TempBlockDesc->Union.DataBlock, (UINT32)(UINTN)RelocBuffer, (UINT32)(UINTN)TempBlockDesc->Length));
625 }
626 }
627 TempBlockDesc++;
628 }
629 //
630 // Now go through all the block descriptors to make sure that they're not
631 // in the memory region we want to copy them to.
632 //
633 CurrBlockDescHead = BlockList;
634 PrevBlockDescTail = NULL;
635 while ((CurrBlockDescHead != NULL) && (CurrBlockDescHead->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
636 //
637 // Get the size of this list then see if it overlaps our low region
638 //
639 TempBlockDesc = CurrBlockDescHead;
640 BlockListSize = sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
641 while (TempBlockDesc->Length != 0) {
642 BlockListSize += sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
643 TempBlockDesc++;
644 }
645
646 if (IsOverlapped (
647 (UINT8 *) NewBlockList,
648 BufferSize,
649 (UINT8 *) CurrBlockDescHead,
650 BlockListSize
651 )) {
652 //
653 // Overlaps, so move it out of the way
654 //
655 RelocBuffer = FindFreeMem (BlockList, MemBase, MemSize, BlockListSize);
656 if (RelocBuffer == NULL) {
657 return NULL;
658 }
659 CopyMem ((VOID *) RelocBuffer, (VOID *) CurrBlockDescHead, BlockListSize);
660 DEBUG ((EFI_D_INFO, "Capsule reloc descriptor block #2\n"));
661 //
662 // Point the previous block's next point to this copied version. If
663 // the tail pointer is null, then this is the first descriptor block.
664 //
665 if (PrevBlockDescTail == NULL) {
666 BlockList = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) RelocBuffer;
667 } else {
668 PrevBlockDescTail->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;
669 }
670 }
671 //
672 // Save our new tail and jump to the next block list
673 //
674 PrevBlockDescTail = TempBlockDesc;
675 CurrBlockDescHead = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;
676 }
677 //
678 // Cleared out low memory. Now copy the descriptors down there.
679 //
680 TempBlockDesc = BlockList;
681 CurrBlockDescHead = NewBlockList;
682 while ((TempBlockDesc != NULL) && (TempBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
683 if (TempBlockDesc->Length != 0) {
684 CurrBlockDescHead->Union.DataBlock = TempBlockDesc->Union.DataBlock;
685 CurrBlockDescHead->Length = TempBlockDesc->Length;
686 CurrBlockDescHead++;
687 TempBlockDesc++;
688 } else {
689 TempBlockDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;
690 }
691 }
692 //
693 // Null terminate
694 //
695 CurrBlockDescHead->Union.ContinuationPointer = (EFI_PHYSICAL_ADDRESS) (UINTN) NULL;
696 CurrBlockDescHead->Length = 0;
697 return NewBlockList;
698 }
699
700 /**
701 Capsule PPI service to coalesce a fragmented capsule in memory.
702
703 Memory Map for coalesced capsule:
704 MemBase + ---->+---------------------------+<-----------+
705 MemSize | CapsuleOffset[49] | |
706 +---------------------------+ |
707 | ................ | |
708 +---------------------------+ |
709 | CapsuleOffset[2] | |
710 +---------------------------+ |
711 | CapsuleOffset[1] | |
712 +---------------------------+ |
713 | CapsuleOffset[0] | CapsuleSize
714 +---------------------------+ |
715 | CapsuleNumber | |
716 +---------------------------+ |
717 | | |
718 | | |
719 | Capsule Image | |
720 | | |
721 | | |
722 +---------------------------+ |
723 | PrivateData | |
724 DestPtr ----> +---------------------------+<-----------+
725 | | |
726 | FreeMem | FreeMemSize
727 | | |
728 FreeMemBase --->+---------------------------+<-----------+
729 | Terminator |
730 +---------------------------+
731 | BlockDescriptor n |
732 +---------------------------+
733 | ................. |
734 +---------------------------+
735 | BlockDescriptor 1 |
736 +---------------------------+
737 | BlockDescriptor 0 |
738 +---------------------------+
739 | PrivateDataDesc 0 |
740 MemBase ---->+---------------------------+<----- BlockList
741
742
743 @param PeiServices General purpose services available to every PEIM.
744 @param MemoryBase Pointer to the base of a block of memory that we can walk
745 all over while trying to coalesce our buffers.
746 On output, this variable will hold the base address of
747 a coalesced capsule.
748 @param MemorySize Size of the memory region pointed to by MemoryBase.
749 On output, this variable will contain the size of the
750 coalesced capsule.
751
752 @retval EFI_NOT_FOUND if we can't determine the boot mode
753 if the boot mode is not flash-update
754 if we could not find the capsule descriptors
755
756 @retval EFI_BUFFER_TOO_SMALL
757 if we could not coalesce the capsule in the memory
758 region provided to us
759
760 @retval EFI_SUCCESS if there's no capsule, or if we processed the
761 capsule successfully.
762 **/
763 EFI_STATUS
764 EFIAPI
765 CapsuleCoalesce (
766 IN EFI_PEI_SERVICES **PeiServices,
767 IN OUT VOID **MemoryBase,
768 IN OUT UINTN *MemorySize
769 )
770 {
771 VOID *NewCapsuleBase;
772 VOID *DataPtr;
773 UINT8 CapsuleIndex;
774 UINT8 *FreeMemBase;
775 UINT8 *DestPtr;
776 UINT8 *RelocPtr;
777 UINT32 CapsuleOffset[MAX_SUPPORT_CAPSULE_NUM];
778 UINT32 *AddDataPtr;
779 UINT32 CapsuleTimes;
780 UINT64 SizeLeft;
781 UINT64 CapsuleImageSize;
782 UINTN CapsuleSize;
783 UINTN DescriptorsSize;
784 UINTN FreeMemSize;
785 UINTN NumDescriptors;
786 UINTN Index;
787 UINTN Size;
788 UINTN VariableCount;
789 CHAR16 CapsuleVarName[30];
790 CHAR16 *TempVarName;
791 EFI_PHYSICAL_ADDRESS CapsuleDataPtr64;
792 BOOLEAN IsCorrupted;
793 BOOLEAN CapsuleBeginFlag;
794 EFI_STATUS Status;
795 EFI_BOOT_MODE BootMode;
796 EFI_CAPSULE_HEADER *CapsuleHeader;
797 EFI_CAPSULE_PEIM_PRIVATE_DATA PrivateData;
798 EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateDataPtr;
799 EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList;
800 EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrentBlockDesc;
801 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;
802 EFI_CAPSULE_BLOCK_DESCRIPTOR PrivateDataDesc[2];
803 EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices;
804
805 CapsuleIndex = 0;
806 SizeLeft = 0;
807 CapsuleTimes = 0;
808 CapsuleImageSize = 0;
809 PrivateDataPtr = NULL;
810 AddDataPtr = NULL;
811 CapsuleHeader = NULL;
812 CapsuleBeginFlag = TRUE;
813 IsCorrupted = TRUE;
814 CapsuleSize = 0;
815 NumDescriptors = 0;
816 Index = 0;
817 VariableCount = 0;
818 CapsuleVarName[0] = 0;
819
820 //
821 // Someone should have already ascertained the boot mode. If it's not
822 // capsule update, then return normally.
823 //
824 Status = PeiServicesGetBootMode (&BootMode);
825 if (EFI_ERROR (Status) || (BootMode != BOOT_ON_FLASH_UPDATE)) {
826 return EFI_NOT_FOUND;
827 }
828
829 //
830 // User may set the same ScatterGatherList with several different variables,
831 // so cache all ScatterGatherList for check later.
832 //
833 Status = PeiServicesLocatePpi (
834 &gEfiPeiReadOnlyVariable2PpiGuid,
835 0,
836 NULL,
837 (VOID **) &PPIVariableServices
838 );
839 if (EFI_ERROR (Status)) {
840 return Status;
841 }
842 Size = sizeof (CapsuleDataPtr64);
843 StrCpy (CapsuleVarName, EFI_CAPSULE_VARIABLE_NAME);
844 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);
845 while (TRUE) {
846 if (Index > 0) {
847 UnicodeValueToString (TempVarName, 0, Index, 0);
848 }
849 Status = PPIVariableServices->GetVariable (
850 PPIVariableServices,
851 CapsuleVarName,
852 &gEfiCapsuleVendorGuid,
853 NULL,
854 &Size,
855 (VOID *) &CapsuleDataPtr64
856 );
857 if (EFI_ERROR (Status)) {
858 //
859 // There is no capsule variables, quit
860 //
861 DEBUG ((EFI_D_ERROR,"Capsule variable Index = %d\n", Index));
862 break;
863 }
864 VariableCount++;
865 Index++;
866 }
867
868 DEBUG ((EFI_D_ERROR,"Capsule variable count = %d\n", VariableCount));
869
870 Status = PeiServicesAllocatePool (
871 VariableCount * sizeof (EFI_PHYSICAL_ADDRESS),
872 (VOID **)&mBufferAddress
873 );
874
875 if (Status != EFI_SUCCESS) {
876 DEBUG ((EFI_D_ERROR, "AllocatePages Failed!, Status = %x\n", Status));
877 return Status;
878 }
879
880 //
881 // Find out if we actually have a capsule.
882 //
883 Status = GetCapsuleDescriptors (&BlockList);
884 if (EFI_ERROR (Status)) {
885 return Status;
886 }
887
888 DEBUG_CODE (
889 CapsuleTestPatternPreCoalesce (PeiServices, BlockList);
890 );
891
892 //
893 // Get the size of our descriptors and the capsule size. GetCapsuleInfo()
894 // returns the number of descriptors that actually point to data, so add
895 // one for a terminator. Do that below.
896 //
897 GetCapsuleInfo (BlockList, &NumDescriptors, &CapsuleSize);
898 if ((CapsuleSize == 0) || (NumDescriptors == 0)) {
899 return EFI_NOT_FOUND;
900 }
901
902 //
903 // Initialize our local copy of private data. When we're done, we'll create a
904 // descriptor for it as well so that it can be put into free memory without
905 // trashing anything.
906 //
907 PrivateData.Signature = EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE;
908 PrivateData.CapsuleSize = CapsuleSize;
909 PrivateDataDesc[0].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) &PrivateData;
910 PrivateDataDesc[0].Length = sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA);
911 PrivateDataDesc[1].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) BlockList;
912 PrivateDataDesc[1].Length = 0;
913 //
914 // In addition to PrivateDataDesc[1:0], one terminator is added
915 // See below RelocateBlockDescriptors()
916 //
917 NumDescriptors += 3;
918 CapsuleSize += sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + sizeof(CapsuleOffset) + sizeof(UINT32);
919 BlockList = PrivateDataDesc;
920 DescriptorsSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
921
922 //
923 // Don't go below some min address. If the base is below it,
924 // then move it up and adjust the size accordingly.
925 //
926 DEBUG ((EFI_D_INFO, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN) *MemoryBase, (UINTN)*MemoryBase + *MemorySize));
927 if ((UINTN)*MemoryBase < (UINTN) MIN_COALESCE_ADDR) {
928 if (((UINTN)*MemoryBase + *MemorySize) < (UINTN) MIN_COALESCE_ADDR) {
929 return EFI_BUFFER_TOO_SMALL;
930 } else {
931 *MemorySize = *MemorySize - ((UINTN) MIN_COALESCE_ADDR - (UINTN) *MemoryBase);
932 *MemoryBase = (VOID *) (UINTN) MIN_COALESCE_ADDR;
933 }
934 }
935
936 if (*MemorySize <= (CapsuleSize + DescriptorsSize)) {
937 return EFI_BUFFER_TOO_SMALL;
938 }
939
940 FreeMemBase = *MemoryBase;
941 FreeMemSize = *MemorySize;
942 DEBUG ((EFI_D_INFO, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN) FreeMemBase, (UINTN) FreeMemBase + FreeMemSize));
943
944 //
945 // Relocate all the block descriptors to low memory to make further
946 // processing easier.
947 //
948 BlockList = RelocateBlockDescriptors (PeiServices, BlockList, FreeMemBase, FreeMemSize);
949 if (BlockList == NULL) {
950 //
951 // Not enough room to relocate the descriptors
952 //
953 return EFI_BUFFER_TOO_SMALL;
954 }
955
956 //
957 // Take the top of memory for the capsule. Naturally align.
958 //
959 DestPtr = FreeMemBase + FreeMemSize - CapsuleSize;
960 DestPtr = (UINT8 *) ((UINTN) DestPtr &~ (UINTN) (sizeof (UINTN) - 1));
961 FreeMemBase = (UINT8 *) BlockList + DescriptorsSize;
962 FreeMemSize = FreeMemSize - DescriptorsSize - CapsuleSize;
963 NewCapsuleBase = (VOID *) DestPtr;
964
965 //
966 // Move all the blocks to the top (high) of memory.
967 // Relocate all the obstructing blocks. Note that the block descriptors
968 // were coalesced when they were relocated, so we can just ++ the pointer.
969 //
970 CurrentBlockDesc = BlockList;
971 while ((CurrentBlockDesc->Length != 0) || (CurrentBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
972 //
973 // See if any of the remaining capsule blocks are in the way
974 //
975 TempBlockDesc = CurrentBlockDesc;
976 while (TempBlockDesc->Length != 0) {
977 //
978 // Is this block in the way of where we want to copy the current descriptor to?
979 //
980 if (IsOverlapped (
981 (UINT8 *) DestPtr,
982 (UINTN) CurrentBlockDesc->Length,
983 (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,
984 (UINTN) TempBlockDesc->Length
985 )) {
986 //
987 // Relocate the block
988 //
989 RelocPtr = FindFreeMem (BlockList, FreeMemBase, FreeMemSize, (UINTN) TempBlockDesc->Length);
990 if (RelocPtr == NULL) {
991 return EFI_BUFFER_TOO_SMALL;
992 }
993
994 CopyMem ((VOID *) RelocPtr, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);
995 DEBUG ((EFI_D_INFO, "Capsule reloc data block from 0x%8X to 0x%8X with size 0x%8X\n",
996 (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) RelocPtr, (UINTN) TempBlockDesc->Length));
997
998 TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocPtr;
999 }
1000 //
1001 // Next descriptor
1002 //
1003 TempBlockDesc++;
1004 }
1005 //
1006 // Ok, we made it through. Copy the block.
1007 // we just support greping one capsule from the lists of block descs list.
1008 //
1009 CapsuleTimes ++;
1010 //
1011 //Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA
1012 //
1013 if (CapsuleTimes > 1) {
1014 //
1015 //For every capsule entry point, check its header to determine whether to relocate it.
1016 //If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.
1017 //
1018 if (CapsuleBeginFlag) {
1019 CapsuleBeginFlag = FALSE;
1020 CapsuleHeader = (EFI_CAPSULE_HEADER*)(UINTN)CurrentBlockDesc->Union.DataBlock;
1021 SizeLeft = CapsuleHeader->CapsuleImageSize;
1022 if (!IsCapsuleCorrupted (CapsuleHeader)) {
1023
1024 if (CapsuleIndex > (MAX_SUPPORT_CAPSULE_NUM - 1)) {
1025 DEBUG ((EFI_D_ERROR, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM));
1026 return EFI_BUFFER_TOO_SMALL;
1027 }
1028
1029 //
1030 // Relocate this valid capsule
1031 //
1032 IsCorrupted = FALSE;
1033 CapsuleImageSize += SizeLeft;
1034 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);
1035 DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8X to 0x%8X with size 0x%8X\n",CapsuleTimes,
1036 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));
1037 //
1038 // Cache the begin offset of this capsule
1039 //
1040 CapsuleOffset[CapsuleIndex++] = (UINT32) (UINTN) DestPtr - (UINT32)(UINTN)NewCapsuleBase - (UINT32)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA);
1041 DestPtr += CurrentBlockDesc->Length;
1042 }
1043 //
1044 // If the current block length is greater than or equal to SizeLeft, this is the
1045 // start of the next capsule
1046 //
1047 if (CurrentBlockDesc->Length < SizeLeft) {
1048 SizeLeft -= CurrentBlockDesc->Length;
1049 } else {
1050 //
1051 // Start the next cycle
1052 //
1053 SizeLeft = 0;
1054 IsCorrupted = TRUE;
1055 CapsuleBeginFlag = TRUE;
1056 }
1057 } else {
1058 //
1059 //Go on relocating the current capule image.
1060 //
1061 if (CurrentBlockDesc->Length < SizeLeft) {
1062 if (!IsCorrupted) {
1063 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) (CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);
1064 DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8X to 0x%8X with size 0x%8X\n",CapsuleTimes,
1065 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));
1066 DestPtr += CurrentBlockDesc->Length;
1067 }
1068 SizeLeft -= CurrentBlockDesc->Length;
1069 } else {
1070 //
1071 //Here is the end of the current capsule image.
1072 //
1073 if (!IsCorrupted) {
1074 CopyMem ((VOID *) DestPtr, (VOID *)(UINTN)(CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);
1075 DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8X to 0x%8X with size 0x%8X\n",CapsuleTimes,
1076 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));
1077 DestPtr += CurrentBlockDesc->Length;
1078 }
1079 //
1080 // Start the next cycle
1081 //
1082 SizeLeft = 0;
1083 IsCorrupted = TRUE;
1084 CapsuleBeginFlag = TRUE;
1085 }
1086 }
1087 } else {
1088 //
1089 //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.
1090 //
1091 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);
1092 DestPtr += CurrentBlockDesc->Length;
1093 }
1094 //
1095 //Walk through the block descriptor list.
1096 //
1097 CurrentBlockDesc++;
1098 }
1099 //
1100 // We return the base of memory we want reserved, and the size.
1101 // The memory peim should handle it appropriately from there.
1102 //
1103 *MemorySize = (UINTN) CapsuleImageSize;
1104 *MemoryBase = (VOID *) NewCapsuleBase;
1105
1106 //
1107 //Append the offsets of mutiply capsules to the continous buffer
1108 //
1109 DataPtr = (VOID*)((UINTN)NewCapsuleBase + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (UINTN)CapsuleImageSize);
1110 AddDataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) &~ (UINT32) (sizeof (UINT32) - 1));
1111
1112 *AddDataPtr++ = CapsuleIndex;
1113
1114 CopyMem (AddDataPtr, &CapsuleOffset[0], sizeof (UINT32) * CapsuleIndex);
1115
1116 PrivateDataPtr = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) NewCapsuleBase;
1117 PrivateDataPtr->CapsuleSize = (UINTN)CapsuleImageSize;
1118
1119 return Status;
1120 }
1121
1122 /**
1123 Determine if we're in capsule update boot mode.
1124
1125 @param PeiServices PEI services table
1126
1127 @retval EFI_SUCCESS if we have a capsule available
1128 @retval EFI_NOT_FOUND no capsule detected
1129
1130 **/
1131 EFI_STATUS
1132 EFIAPI
1133 CheckCapsuleUpdate (
1134 IN EFI_PEI_SERVICES **PeiServices
1135 )
1136 {
1137 EFI_STATUS Status;
1138 Status = GetCapsuleDescriptors (NULL);
1139 return Status;
1140 }
1141 /**
1142 This function will look at a capsule and determine if it's a test pattern.
1143 If it is, then it will verify it and emit an error message if corruption is detected.
1144
1145 @param PeiServices Standard pei services pointer
1146 @param CapsuleBase Base address of coalesced capsule, which is preceeded
1147 by private data. Very implementation specific.
1148
1149 @retval TRUE Capsule image is the test image
1150 @retval FALSE Capsule image is not the test image.
1151
1152 **/
1153 BOOLEAN
1154 CapsuleTestPattern (
1155 IN EFI_PEI_SERVICES **PeiServices,
1156 IN VOID *CapsuleBase
1157 )
1158 {
1159 UINT32 *TestPtr;
1160 UINT32 TestCounter;
1161 UINT32 TestSize;
1162 BOOLEAN RetValue;
1163
1164 RetValue = FALSE;
1165
1166 //
1167 // Look at the capsule data and determine if it's a test pattern. If it
1168 // is, then test it now.
1169 //
1170 TestPtr = (UINT32 *) CapsuleBase;
1171 if (*TestPtr == CAPSULE_TEST_SIGNATURE) {
1172 RetValue = TRUE;
1173 DEBUG ((EFI_D_INFO, "Capsule test pattern mode activated...\n"));
1174 TestSize = TestPtr[1] / sizeof (UINT32);
1175 //
1176 // Skip over the signature and the size fields in the pattern data header
1177 //
1178 TestPtr += 2;
1179 TestCounter = 0;
1180 while (TestSize > 0) {
1181 if (*TestPtr != TestCounter) {
1182 DEBUG ((EFI_D_INFO, "Capsule test pattern mode FAILED: BaseAddr/FailAddr 0x%X 0x%X\n", (UINT32)(UINTN)(EFI_CAPSULE_PEIM_PRIVATE_DATA *)CapsuleBase, (UINT32)(UINTN)TestPtr));
1183 return TRUE;
1184 }
1185
1186 TestPtr++;
1187 TestCounter++;
1188 TestSize--;
1189 }
1190
1191 DEBUG ((EFI_D_INFO, "Capsule test pattern mode SUCCESS\n"));
1192 }
1193
1194 return RetValue;
1195 }
1196
1197 /**
1198 Capsule PPI service that gets called after memory is available. The
1199 capsule coalesce function, which must be called first, returns a base
1200 address and size, which can be anything actually. Once the memory init
1201 PEIM has discovered memory, then it should call this function and pass in
1202 the base address and size returned by the coalesce function. Then this
1203 function can create a capsule HOB and return.
1204
1205 @param PeiServices standard pei services pointer
1206 @param CapsuleBase address returned by the capsule coalesce function. Most
1207 likely this will actually be a pointer to private data.
1208 @param CapsuleSize value returned by the capsule coalesce function.
1209
1210 @retval EFI_VOLUME_CORRUPTED CapsuleBase does not appear to point to a
1211 coalesced capsule
1212 @retval EFI_SUCCESS if all goes well.
1213 **/
1214 EFI_STATUS
1215 EFIAPI
1216 CreateState (
1217 IN EFI_PEI_SERVICES **PeiServices,
1218 IN VOID *CapsuleBase,
1219 IN UINTN CapsuleSize
1220 )
1221 {
1222 EFI_STATUS Status;
1223 EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateData;
1224 UINTN Size;
1225 EFI_PHYSICAL_ADDRESS NewBuffer;
1226 UINT32 *DataPtr;
1227 UINT32 CapsuleNumber;
1228 UINT32 Index;
1229 EFI_PHYSICAL_ADDRESS BaseAddress;
1230 UINT64 Length;
1231
1232 DataPtr = NULL;
1233 CapsuleNumber = 0;
1234 PrivateData = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) CapsuleBase;
1235 if (PrivateData->Signature != EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE) {
1236 return EFI_VOLUME_CORRUPTED;
1237 }
1238 //
1239 // Capsule Number and Capsule Offset is in the tail of Capsule data.
1240 //
1241 Size = (UINTN) PrivateData->CapsuleSize;
1242 DataPtr = (UINT32*)((UINTN)CapsuleBase + (UINTN)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA)+ Size);
1243 DataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) & ~(sizeof (UINT32) - 1));
1244 CapsuleNumber = *DataPtr++;
1245 //
1246 // Allocate the memory so that it gets preserved into DXE
1247 //
1248 Status = PeiServicesAllocatePages (
1249 EfiRuntimeServicesData,
1250 EFI_SIZE_TO_PAGES (Size),
1251 &NewBuffer
1252 );
1253
1254 if (Status != EFI_SUCCESS) {
1255 DEBUG ((EFI_D_ERROR, "AllocatePages Failed!\n"));
1256 return Status;
1257 }
1258 //
1259 // Copy to our new buffer for DXE
1260 //
1261 DEBUG ((EFI_D_INFO, "Capsule copy from 0x%8X to 0x%8X with size 0x%8X\n", (UINTN) (PrivateData + 1), (UINTN) NewBuffer, Size));
1262 CopyMem ((VOID *) (UINTN) NewBuffer, (VOID *) (UINTN) (PrivateData + 1), Size);
1263 //
1264 // Check for test data pattern. If it is the test pattern, then we'll
1265 // test it ans still create the HOB so that it can be used to verify
1266 // that capsules don't get corrupted all the way into BDS. BDS will
1267 // still try to turn it into a firmware volume, but will think it's
1268 // corrupted so nothing will happen.
1269 //
1270 DEBUG_CODE (
1271 CapsuleTestPattern (PeiServices, (VOID *) (UINTN) NewBuffer);
1272 );
1273
1274 //
1275 // Build the UEFI Capsule Hob for each capsule image.
1276 //
1277 for (Index = 0; Index < CapsuleNumber; Index ++) {
1278 BaseAddress = NewBuffer + DataPtr[Index];
1279 Length = ((EFI_CAPSULE_HEADER *)((UINTN) BaseAddress))->CapsuleImageSize;
1280
1281 BuildCvHob (BaseAddress, Length);
1282 }
1283
1284 return EFI_SUCCESS;
1285 }
1286
1287 CONST PEI_CAPSULE_PPI mCapsulePpi = {
1288 CapsuleCoalesce,
1289 CheckCapsuleUpdate,
1290 CreateState
1291 };
1292
1293 CONST EFI_PEI_PPI_DESCRIPTOR mUefiPpiListCapsule = {
1294 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
1295 &gPeiCapsulePpiGuid,
1296 (PEI_CAPSULE_PPI *) &mCapsulePpi
1297 };
1298
1299 /**
1300 Entry point function for the PEIM
1301
1302 @param FileHandle Handle of the file being invoked.
1303 @param PeiServices Describes the list of possible PEI Services.
1304
1305 @return EFI_SUCCESS If we installed our PPI
1306
1307 **/
1308 EFI_STATUS
1309 EFIAPI
1310 CapsuleMain (
1311 IN EFI_PEI_FILE_HANDLE FileHandle,
1312 IN CONST EFI_PEI_SERVICES **PeiServices
1313 )
1314 {
1315 //
1316 // Just produce our PPI
1317 //
1318 return PeiServicesInstallPpi (&mUefiPpiListCapsule);
1319 }