]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsulePei/UefiCapsule.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / CapsulePei / UefiCapsule.c
1 /** @file
2 Capsule update PEIM for UEFI2.0
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions
9 of the BSD License which accompanies this distribution. The
10 full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include "Capsule.h"
19
20 #ifdef MDE_CPU_IA32
21 //
22 // Global Descriptor Table (GDT)
23 //
24 GLOBAL_REMOVE_IF_UNREFERENCED IA32_SEGMENT_DESCRIPTOR mGdtEntries[] = {
25 /* selector { Global Segment Descriptor } */
26 /* 0x00 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //null descriptor
27 /* 0x08 */ {{0xffff, 0, 0, 0x3, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear data segment descriptor
28 /* 0x10 */ {{0xffff, 0, 0, 0xf, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear code segment descriptor
29 /* 0x18 */ {{0xffff, 0, 0, 0x3, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor
30 /* 0x20 */ {{0xffff, 0, 0, 0xb, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system code segment descriptor
31 /* 0x28 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor
32 /* 0x30 */ {{0xffff, 0, 0, 0x3, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor
33 /* 0x38 */ {{0xffff, 0, 0, 0xb, 1, 0, 1, 0xf, 0, 1, 0, 1, 0}}, //system code segment descriptor
34 /* 0x40 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor
35 };
36
37 //
38 // IA32 Gdt register
39 //
40 GLOBAL_REMOVE_IF_UNREFERENCED CONST IA32_DESCRIPTOR mGdt = {
41 sizeof (mGdtEntries) - 1,
42 (UINTN) mGdtEntries
43 };
44
45
46 /**
47 The function will check if 1G page is supported.
48
49 @retval TRUE 1G page is supported.
50 @retval FALSE 1G page is not supported.
51
52 **/
53 BOOLEAN
54 IsPage1GSupport (
55 VOID
56 )
57 {
58 UINT32 RegEax;
59 UINT32 RegEdx;
60 BOOLEAN Page1GSupport;
61
62 Page1GSupport = FALSE;
63 if (PcdGetBool(PcdUse1GPageTable)) {
64 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
65 if (RegEax >= 0x80000001) {
66 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);
67 if ((RegEdx & BIT26) != 0) {
68 Page1GSupport = TRUE;
69 }
70 }
71 }
72
73 return Page1GSupport;
74 }
75
76 /**
77 Calculate the total size of page table.
78
79 @param[in] Page1GSupport 1G page support or not.
80
81 @return The size of page table.
82
83 **/
84 UINTN
85 CalculatePageTableSize (
86 IN BOOLEAN Page1GSupport
87 )
88 {
89 UINTN ExtraPageTablePages;
90 UINTN TotalPagesNum;
91 UINT8 PhysicalAddressBits;
92 UINT32 NumberOfPml4EntriesNeeded;
93 UINT32 NumberOfPdpEntriesNeeded;
94
95 //
96 // Create 4G page table by default,
97 // and let PF handler to handle > 4G request.
98 //
99 PhysicalAddressBits = 32;
100 ExtraPageTablePages = EXTRA_PAGE_TABLE_PAGES;
101
102 //
103 // Calculate the table entries needed.
104 //
105 if (PhysicalAddressBits <= 39 ) {
106 NumberOfPml4EntriesNeeded = 1;
107 NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 30));
108 } else {
109 NumberOfPml4EntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 39));
110 NumberOfPdpEntriesNeeded = 512;
111 }
112
113 if (!Page1GSupport) {
114 TotalPagesNum = (NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1;
115 } else {
116 TotalPagesNum = NumberOfPml4EntriesNeeded + 1;
117 }
118 TotalPagesNum += ExtraPageTablePages;
119
120 return EFI_PAGES_TO_SIZE (TotalPagesNum);
121 }
122
123 /**
124 Allocates and fills in the Page Directory and Page Table Entries to
125 establish a 4G page table.
126
127 @param[in] PageTablesAddress The base address of page table.
128 @param[in] Page1GSupport 1G page support or not.
129
130 **/
131 VOID
132 Create4GPageTables (
133 IN EFI_PHYSICAL_ADDRESS PageTablesAddress,
134 IN BOOLEAN Page1GSupport
135 )
136 {
137 UINT8 PhysicalAddressBits;
138 EFI_PHYSICAL_ADDRESS PageAddress;
139 UINTN IndexOfPml4Entries;
140 UINTN IndexOfPdpEntries;
141 UINTN IndexOfPageDirectoryEntries;
142 UINT32 NumberOfPml4EntriesNeeded;
143 UINT32 NumberOfPdpEntriesNeeded;
144 PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry;
145 PAGE_MAP_AND_DIRECTORY_POINTER *PageMap;
146 PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry;
147 PAGE_TABLE_ENTRY *PageDirectoryEntry;
148 UINTN BigPageAddress;
149 PAGE_TABLE_1G_ENTRY *PageDirectory1GEntry;
150 UINT64 AddressEncMask;
151
152 //
153 // Make sure AddressEncMask is contained to smallest supported address field.
154 //
155 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
156
157 //
158 // Create 4G page table by default,
159 // and let PF handler to handle > 4G request.
160 //
161 PhysicalAddressBits = 32;
162
163 //
164 // Calculate the table entries needed.
165 //
166 if (PhysicalAddressBits <= 39 ) {
167 NumberOfPml4EntriesNeeded = 1;
168 NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 30));
169 } else {
170 NumberOfPml4EntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 39));
171 NumberOfPdpEntriesNeeded = 512;
172 }
173
174 //
175 // Pre-allocate big pages to avoid later allocations.
176 //
177 BigPageAddress = (UINTN) PageTablesAddress;
178
179 //
180 // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.
181 //
182 PageMap = (VOID *) BigPageAddress;
183 BigPageAddress += SIZE_4KB;
184
185 PageMapLevel4Entry = PageMap;
186 PageAddress = 0;
187 for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {
188 //
189 // Each PML4 entry points to a page of Page Directory Pointer entires.
190 // So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.
191 //
192 PageDirectoryPointerEntry = (VOID *) BigPageAddress;
193 BigPageAddress += SIZE_4KB;
194
195 //
196 // Make a PML4 Entry
197 //
198 PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry | AddressEncMask;
199 PageMapLevel4Entry->Bits.ReadWrite = 1;
200 PageMapLevel4Entry->Bits.Present = 1;
201
202 if (Page1GSupport) {
203 PageDirectory1GEntry = (VOID *) PageDirectoryPointerEntry;
204
205 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += SIZE_1GB) {
206 //
207 // Fill in the Page Directory entries
208 //
209 PageDirectory1GEntry->Uint64 = (UINT64)PageAddress | AddressEncMask;
210 PageDirectory1GEntry->Bits.ReadWrite = 1;
211 PageDirectory1GEntry->Bits.Present = 1;
212 PageDirectory1GEntry->Bits.MustBe1 = 1;
213 }
214 } else {
215 for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
216 //
217 // Each Directory Pointer entries points to a page of Page Directory entires.
218 // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.
219 //
220 PageDirectoryEntry = (VOID *) BigPageAddress;
221 BigPageAddress += SIZE_4KB;
222
223 //
224 // Fill in a Page Directory Pointer Entries
225 //
226 PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask;
227 PageDirectoryPointerEntry->Bits.ReadWrite = 1;
228 PageDirectoryPointerEntry->Bits.Present = 1;
229
230 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += SIZE_2MB) {
231 //
232 // Fill in the Page Directory entries
233 //
234 PageDirectoryEntry->Uint64 = (UINT64)PageAddress | AddressEncMask;
235 PageDirectoryEntry->Bits.ReadWrite = 1;
236 PageDirectoryEntry->Bits.Present = 1;
237 PageDirectoryEntry->Bits.MustBe1 = 1;
238 }
239 }
240
241 for (; IndexOfPdpEntries < 512; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
242 ZeroMem (
243 PageDirectoryPointerEntry,
244 sizeof(PAGE_MAP_AND_DIRECTORY_POINTER)
245 );
246 }
247 }
248 }
249
250 //
251 // For the PML4 entries we are not using fill in a null entry.
252 //
253 for (; IndexOfPml4Entries < 512; IndexOfPml4Entries++, PageMapLevel4Entry++) {
254 ZeroMem (
255 PageMapLevel4Entry,
256 sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)
257 );
258 }
259 }
260
261 /**
262 Return function from long mode to 32-bit mode.
263
264 @param EntrypointContext Context for mode switching
265 @param ReturnContext Context for mode switching
266
267 **/
268 VOID
269 ReturnFunction (
270 SWITCH_32_TO_64_CONTEXT *EntrypointContext,
271 SWITCH_64_TO_32_CONTEXT *ReturnContext
272 )
273 {
274 //
275 // Restore original GDT
276 //
277 AsmWriteGdtr (&ReturnContext->Gdtr);
278
279 //
280 // return to original caller
281 //
282 LongJump ((BASE_LIBRARY_JUMP_BUFFER *)(UINTN)EntrypointContext->JumpBuffer, 1);
283
284 //
285 // never be here
286 //
287 ASSERT (FALSE);
288 }
289
290 /**
291 Thunk function from 32-bit protection mode to long mode.
292
293 @param PageTableAddress Page table base address
294 @param Context Context for mode switching
295 @param ReturnContext Context for mode switching
296
297 @retval EFI_SUCCESS Function successfully executed.
298
299 **/
300 EFI_STATUS
301 Thunk32To64 (
302 EFI_PHYSICAL_ADDRESS PageTableAddress,
303 SWITCH_32_TO_64_CONTEXT *Context,
304 SWITCH_64_TO_32_CONTEXT *ReturnContext
305 )
306 {
307 UINTN SetJumpFlag;
308 EFI_STATUS Status;
309
310 //
311 // Save return address, LongJump will return here then
312 //
313 SetJumpFlag = SetJump ((BASE_LIBRARY_JUMP_BUFFER *) (UINTN) Context->JumpBuffer);
314
315 if (SetJumpFlag == 0) {
316
317 //
318 // Build 4G Page Tables.
319 //
320 Create4GPageTables (PageTableAddress, Context->Page1GSupport);
321
322 //
323 // Create 64-bit GDT
324 //
325 AsmWriteGdtr (&mGdt);
326
327 //
328 // Write CR3
329 //
330 AsmWriteCr3 ((UINTN) PageTableAddress);
331
332 DEBUG ((
333 DEBUG_INFO,
334 "%a() Stack Base: 0x%lx, Stack Size: 0x%lx\n",
335 __FUNCTION__,
336 Context->StackBufferBase,
337 Context->StackBufferLength
338 ));
339
340 //
341 // Disable interrupt of Debug timer, since the IDT table cannot work in long mode
342 //
343 SaveAndSetDebugTimerInterrupt (FALSE);
344 //
345 // Transfer to long mode
346 //
347 AsmEnablePaging64 (
348 0x38,
349 (UINT64) Context->EntryPoint,
350 (UINT64)(UINTN) Context,
351 (UINT64)(UINTN) ReturnContext,
352 Context->StackBufferBase + Context->StackBufferLength
353 );
354 }
355
356 //
357 // Convert to 32-bit Status and return
358 //
359 Status = EFI_SUCCESS;
360 if ((UINTN) ReturnContext->ReturnStatus != 0) {
361 Status = ENCODE_ERROR ((UINTN) ReturnContext->ReturnStatus);
362 }
363
364 return Status;
365 }
366
367 /**
368 If in 32 bit protection mode, and coalesce image is of X64, switch to long mode.
369
370 @param LongModeBuffer The context of long mode.
371 @param CoalesceEntry Entry of coalesce image.
372 @param BlockListAddr Address of block list.
373 @param MemoryResource Pointer to the buffer of memory resource descriptor.
374 @param MemoryBase Base of memory range.
375 @param MemorySize Size of memory range.
376
377 @retval EFI_SUCCESS Successfully switched to long mode and execute coalesce.
378 @retval Others Failed to execute coalesce in long mode.
379
380 **/
381 EFI_STATUS
382 ModeSwitch (
383 IN EFI_CAPSULE_LONG_MODE_BUFFER *LongModeBuffer,
384 IN COALESCE_ENTRY CoalesceEntry,
385 IN EFI_PHYSICAL_ADDRESS BlockListAddr,
386 IN MEMORY_RESOURCE_DESCRIPTOR *MemoryResource,
387 IN OUT VOID **MemoryBase,
388 IN OUT UINTN *MemorySize
389 )
390 {
391 EFI_STATUS Status;
392 EFI_PHYSICAL_ADDRESS MemoryBase64;
393 UINT64 MemorySize64;
394 EFI_PHYSICAL_ADDRESS MemoryEnd64;
395 SWITCH_32_TO_64_CONTEXT Context;
396 SWITCH_64_TO_32_CONTEXT ReturnContext;
397 BASE_LIBRARY_JUMP_BUFFER JumpBuffer;
398 EFI_PHYSICAL_ADDRESS ReservedRangeBase;
399 EFI_PHYSICAL_ADDRESS ReservedRangeEnd;
400 BOOLEAN Page1GSupport;
401
402 ZeroMem (&Context, sizeof (SWITCH_32_TO_64_CONTEXT));
403 ZeroMem (&ReturnContext, sizeof (SWITCH_64_TO_32_CONTEXT));
404
405 MemoryBase64 = (UINT64) (UINTN) *MemoryBase;
406 MemorySize64 = (UINT64) (UINTN) *MemorySize;
407 MemoryEnd64 = MemoryBase64 + MemorySize64;
408
409 Page1GSupport = IsPage1GSupport ();
410
411 //
412 // Merge memory range reserved for stack and page table
413 //
414 if (LongModeBuffer->StackBaseAddress < LongModeBuffer->PageTableAddress) {
415 ReservedRangeBase = LongModeBuffer->StackBaseAddress;
416 ReservedRangeEnd = LongModeBuffer->PageTableAddress + CalculatePageTableSize (Page1GSupport);
417 } else {
418 ReservedRangeBase = LongModeBuffer->PageTableAddress;
419 ReservedRangeEnd = LongModeBuffer->StackBaseAddress + LongModeBuffer->StackSize;
420 }
421
422 //
423 // Check if memory range reserved is overlap with MemoryBase ~ MemoryBase + MemorySize.
424 // If they are overlapped, get a larger range to process capsule data.
425 //
426 if (ReservedRangeBase <= MemoryBase64) {
427 if (ReservedRangeEnd < MemoryEnd64) {
428 MemoryBase64 = ReservedRangeEnd;
429 } else {
430 DEBUG ((EFI_D_ERROR, "Memory is not enough to process capsule!\n"));
431 return EFI_OUT_OF_RESOURCES;
432 }
433 } else if (ReservedRangeBase < MemoryEnd64) {
434 if (ReservedRangeEnd < MemoryEnd64 &&
435 ReservedRangeBase - MemoryBase64 < MemoryEnd64 - ReservedRangeEnd) {
436 MemoryBase64 = ReservedRangeEnd;
437 } else {
438 MemorySize64 = (UINT64)(UINTN)(ReservedRangeBase - MemoryBase64);
439 }
440 }
441
442 //
443 // Initialize context jumping to 64-bit enviroment
444 //
445 Context.JumpBuffer = (EFI_PHYSICAL_ADDRESS)(UINTN)&JumpBuffer;
446 Context.StackBufferBase = LongModeBuffer->StackBaseAddress;
447 Context.StackBufferLength = LongModeBuffer->StackSize;
448 Context.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)CoalesceEntry;
449 Context.BlockListAddr = BlockListAddr;
450 Context.MemoryResource = (EFI_PHYSICAL_ADDRESS)(UINTN)MemoryResource;
451 Context.MemoryBase64Ptr = (EFI_PHYSICAL_ADDRESS)(UINTN)&MemoryBase64;
452 Context.MemorySize64Ptr = (EFI_PHYSICAL_ADDRESS)(UINTN)&MemorySize64;
453 Context.Page1GSupport = Page1GSupport;
454 Context.AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
455
456 //
457 // Prepare data for return back
458 //
459 ReturnContext.ReturnCs = 0x10;
460 ReturnContext.ReturnEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)ReturnFunction;
461 //
462 // Will save the return status of processing capsule
463 //
464 ReturnContext.ReturnStatus = 0;
465
466 //
467 // Save original GDT
468 //
469 AsmReadGdtr ((IA32_DESCRIPTOR *)&ReturnContext.Gdtr);
470
471 Status = Thunk32To64 (LongModeBuffer->PageTableAddress, &Context, &ReturnContext);
472
473 if (!EFI_ERROR (Status)) {
474 *MemoryBase = (VOID *) (UINTN) MemoryBase64;
475 *MemorySize = (UINTN) MemorySize64;
476 }
477
478 return Status;
479
480 }
481
482 /**
483 Locates the coalesce image entry point, and detects its machine type.
484
485 @param CoalesceImageEntryPoint Pointer to coalesce image entry point for output.
486 @param CoalesceImageMachineType Pointer to machine type of coalesce image.
487
488 @retval EFI_SUCCESS Coalesce image successfully located.
489 @retval Others Failed to locate the coalesce image.
490
491 **/
492 EFI_STATUS
493 FindCapsuleCoalesceImage (
494 OUT EFI_PHYSICAL_ADDRESS *CoalesceImageEntryPoint,
495 OUT UINT16 *CoalesceImageMachineType
496 )
497 {
498 EFI_STATUS Status;
499 UINTN Instance;
500 EFI_PEI_LOAD_FILE_PPI *LoadFile;
501 EFI_PEI_FV_HANDLE VolumeHandle;
502 EFI_PEI_FILE_HANDLE FileHandle;
503 EFI_PHYSICAL_ADDRESS CoalesceImageAddress;
504 UINT64 CoalesceImageSize;
505 UINT32 AuthenticationState;
506
507 Instance = 0;
508
509 while (TRUE) {
510 Status = PeiServicesFfsFindNextVolume (Instance++, &VolumeHandle);
511 if (EFI_ERROR (Status)) {
512 return Status;
513 }
514 Status = PeiServicesFfsFindFileByName (PcdGetPtr(PcdCapsuleCoalesceFile), VolumeHandle, &FileHandle);
515 if (!EFI_ERROR (Status)) {
516 Status = PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid, 0, NULL, (VOID **) &LoadFile);
517 ASSERT_EFI_ERROR (Status);
518
519 Status = LoadFile->LoadFile (
520 LoadFile,
521 FileHandle,
522 &CoalesceImageAddress,
523 &CoalesceImageSize,
524 CoalesceImageEntryPoint,
525 &AuthenticationState
526 );
527 if (EFI_ERROR (Status)) {
528 DEBUG ((EFI_D_ERROR, "Unable to find PE32 section in CapsuleX64 image ffs %r!\n", Status));
529 return Status;
530 }
531 *CoalesceImageMachineType = PeCoffLoaderGetMachineType ((VOID *) (UINTN) CoalesceImageAddress);
532 break;
533 } else {
534 continue;
535 }
536 }
537
538 return Status;
539 }
540
541 /**
542 Gets the reserved long mode buffer.
543
544 @param LongModeBuffer Pointer to the long mode buffer for output.
545
546 @retval EFI_SUCCESS Long mode buffer successfully retrieved.
547 @retval Others Variable storing long mode buffer not found.
548
549 **/
550 EFI_STATUS
551 GetLongModeContext (
552 OUT EFI_CAPSULE_LONG_MODE_BUFFER *LongModeBuffer
553 )
554 {
555 EFI_STATUS Status;
556 UINTN Size;
557 EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices;
558
559 Status = PeiServicesLocatePpi (
560 &gEfiPeiReadOnlyVariable2PpiGuid,
561 0,
562 NULL,
563 (VOID **) &PPIVariableServices
564 );
565 ASSERT_EFI_ERROR (Status);
566
567 Size = sizeof (EFI_CAPSULE_LONG_MODE_BUFFER);
568 Status = PPIVariableServices->GetVariable (
569 PPIVariableServices,
570 EFI_CAPSULE_LONG_MODE_BUFFER_NAME,
571 &gEfiCapsuleVendorGuid,
572 NULL,
573 &Size,
574 LongModeBuffer
575 );
576 if (EFI_ERROR (Status)) {
577 DEBUG (( EFI_D_ERROR, "Error Get LongModeBuffer variable %r!\n", Status));
578 }
579 return Status;
580 }
581 #endif
582
583 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
584 /**
585 Get physical address bits.
586
587 @return Physical address bits.
588
589 **/
590 UINT8
591 GetPhysicalAddressBits (
592 VOID
593 )
594 {
595 UINT32 RegEax;
596 UINT8 PhysicalAddressBits;
597 VOID *Hob;
598
599 //
600 // Get physical address bits supported.
601 //
602 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
603 if (Hob != NULL) {
604 PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
605 } else {
606 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
607 if (RegEax >= 0x80000008) {
608 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
609 PhysicalAddressBits = (UINT8) RegEax;
610 } else {
611 PhysicalAddressBits = 36;
612 }
613 }
614
615 //
616 // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
617 //
618 ASSERT (PhysicalAddressBits <= 52);
619 if (PhysicalAddressBits > 48) {
620 PhysicalAddressBits = 48;
621 }
622
623 return PhysicalAddressBits;
624 }
625 #endif
626
627 /**
628 Sort memory resource entries based upon PhysicalStart, from low to high.
629
630 @param[in, out] MemoryResource A pointer to the memory resource entry buffer.
631
632 **/
633 VOID
634 SortMemoryResourceDescriptor (
635 IN OUT MEMORY_RESOURCE_DESCRIPTOR *MemoryResource
636 )
637 {
638 MEMORY_RESOURCE_DESCRIPTOR *MemoryResourceEntry;
639 MEMORY_RESOURCE_DESCRIPTOR *NextMemoryResourceEntry;
640 MEMORY_RESOURCE_DESCRIPTOR TempMemoryResource;
641
642 MemoryResourceEntry = MemoryResource;
643 NextMemoryResourceEntry = MemoryResource + 1;
644 while (MemoryResourceEntry->ResourceLength != 0) {
645 while (NextMemoryResourceEntry->ResourceLength != 0) {
646 if (MemoryResourceEntry->PhysicalStart > NextMemoryResourceEntry->PhysicalStart) {
647 CopyMem (&TempMemoryResource, MemoryResourceEntry, sizeof (MEMORY_RESOURCE_DESCRIPTOR));
648 CopyMem (MemoryResourceEntry, NextMemoryResourceEntry, sizeof (MEMORY_RESOURCE_DESCRIPTOR));
649 CopyMem (NextMemoryResourceEntry, &TempMemoryResource, sizeof (MEMORY_RESOURCE_DESCRIPTOR));
650 }
651
652 NextMemoryResourceEntry = NextMemoryResourceEntry + 1;
653 }
654
655 MemoryResourceEntry = MemoryResourceEntry + 1;
656 NextMemoryResourceEntry = MemoryResourceEntry + 1;
657 }
658 }
659
660 /**
661 Merge continous memory resource entries.
662
663 @param[in, out] MemoryResource A pointer to the memory resource entry buffer.
664
665 **/
666 VOID
667 MergeMemoryResourceDescriptor (
668 IN OUT MEMORY_RESOURCE_DESCRIPTOR *MemoryResource
669 )
670 {
671 MEMORY_RESOURCE_DESCRIPTOR *MemoryResourceEntry;
672 MEMORY_RESOURCE_DESCRIPTOR *NewMemoryResourceEntry;
673 MEMORY_RESOURCE_DESCRIPTOR *NextMemoryResourceEntry;
674 MEMORY_RESOURCE_DESCRIPTOR *MemoryResourceEnd;
675
676 MemoryResourceEntry = MemoryResource;
677 NewMemoryResourceEntry = MemoryResource;
678 while (MemoryResourceEntry->ResourceLength != 0) {
679 CopyMem (NewMemoryResourceEntry, MemoryResourceEntry, sizeof (MEMORY_RESOURCE_DESCRIPTOR));
680 NextMemoryResourceEntry = MemoryResourceEntry + 1;
681
682 while ((NextMemoryResourceEntry->ResourceLength != 0) &&
683 (NextMemoryResourceEntry->PhysicalStart == (MemoryResourceEntry->PhysicalStart + MemoryResourceEntry->ResourceLength))) {
684 MemoryResourceEntry->ResourceLength += NextMemoryResourceEntry->ResourceLength;
685 if (NewMemoryResourceEntry != MemoryResourceEntry) {
686 NewMemoryResourceEntry->ResourceLength += NextMemoryResourceEntry->ResourceLength;
687 }
688
689 NextMemoryResourceEntry = NextMemoryResourceEntry + 1;
690 }
691
692 MemoryResourceEntry = NextMemoryResourceEntry;
693 NewMemoryResourceEntry = NewMemoryResourceEntry + 1;
694 }
695
696 //
697 // Set NULL terminate memory resource descriptor after merging.
698 //
699 MemoryResourceEnd = NewMemoryResourceEntry;
700 ZeroMem (MemoryResourceEnd, sizeof (MEMORY_RESOURCE_DESCRIPTOR));
701 }
702
703 /**
704 Build memory resource descriptor from resource descriptor in HOB list.
705
706 @return Pointer to the buffer of memory resource descriptor.
707 NULL if no memory resource descriptor reported in HOB list
708 before capsule Coalesce.
709
710 **/
711 MEMORY_RESOURCE_DESCRIPTOR *
712 BuildMemoryResourceDescriptor (
713 VOID
714 )
715 {
716 EFI_PEI_HOB_POINTERS Hob;
717 UINTN Index;
718 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceDescriptor;
719 MEMORY_RESOURCE_DESCRIPTOR *MemoryResource;
720 EFI_STATUS Status;
721
722 //
723 // Get the count of memory resource descriptor.
724 //
725 Index = 0;
726 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
727 while (Hob.Raw != NULL) {
728 ResourceDescriptor = (EFI_HOB_RESOURCE_DESCRIPTOR *) Hob.Raw;
729 if (ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) {
730 Index++;
731 }
732 Hob.Raw = GET_NEXT_HOB (Hob);
733 Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
734 }
735
736 if (Index == 0) {
737 DEBUG ((EFI_D_INFO | EFI_D_WARN, "No memory resource descriptor reported in HOB list before capsule Coalesce\n"));
738 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
739 //
740 // Allocate memory to hold memory resource descriptor,
741 // include extra one NULL terminate memory resource descriptor.
742 //
743 Status = PeiServicesAllocatePool ((1 + 1) * sizeof (MEMORY_RESOURCE_DESCRIPTOR), (VOID **) &MemoryResource);
744 ASSERT_EFI_ERROR (Status);
745 ZeroMem (MemoryResource, (1 + 1) * sizeof (MEMORY_RESOURCE_DESCRIPTOR));
746
747 MemoryResource[0].PhysicalStart = 0;
748 MemoryResource[0].ResourceLength = LShiftU64 (1, GetPhysicalAddressBits ());
749 DEBUG ((EFI_D_INFO, "MemoryResource[0x0] - Start(0x%0lx) Length(0x%0lx)\n",
750 MemoryResource[0x0].PhysicalStart, MemoryResource[0x0].ResourceLength));
751 return MemoryResource;
752 #else
753 return NULL;
754 #endif
755 }
756
757 //
758 // Allocate memory to hold memory resource descriptor,
759 // include extra one NULL terminate memory resource descriptor.
760 //
761 Status = PeiServicesAllocatePool ((Index + 1) * sizeof (MEMORY_RESOURCE_DESCRIPTOR), (VOID **) &MemoryResource);
762 ASSERT_EFI_ERROR (Status);
763 ZeroMem (MemoryResource, (Index + 1) * sizeof (MEMORY_RESOURCE_DESCRIPTOR));
764
765 //
766 // Get the content of memory resource descriptor.
767 //
768 Index = 0;
769 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
770 while (Hob.Raw != NULL) {
771 ResourceDescriptor = (EFI_HOB_RESOURCE_DESCRIPTOR *) Hob.Raw;
772 if (ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) {
773 DEBUG ((EFI_D_INFO, "MemoryResource[0x%x] - Start(0x%0lx) Length(0x%0lx)\n",
774 Index, ResourceDescriptor->PhysicalStart, ResourceDescriptor->ResourceLength));
775 MemoryResource[Index].PhysicalStart = ResourceDescriptor->PhysicalStart;
776 MemoryResource[Index].ResourceLength = ResourceDescriptor->ResourceLength;
777 Index++;
778 }
779 Hob.Raw = GET_NEXT_HOB (Hob);
780 Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
781 }
782
783 SortMemoryResourceDescriptor (MemoryResource);
784 MergeMemoryResourceDescriptor (MemoryResource);
785
786 DEBUG ((DEBUG_INFO, "Dump MemoryResource[] after sorted and merged\n"));
787 for (Index = 0; MemoryResource[Index].ResourceLength != 0; Index++) {
788 DEBUG ((
789 DEBUG_INFO,
790 " MemoryResource[0x%x] - Start(0x%0lx) Length(0x%0lx)\n",
791 Index,
792 MemoryResource[Index].PhysicalStart,
793 MemoryResource[Index].ResourceLength
794 ));
795 }
796
797 return MemoryResource;
798 }
799
800 /**
801 Checks for the presence of capsule descriptors.
802 Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
803 and save to DescriptorBuffer.
804
805 @param DescriptorBuffer Pointer to the capsule descriptors
806
807 @retval EFI_SUCCESS a valid capsule is present
808 @retval EFI_NOT_FOUND if a valid capsule is not present
809 **/
810 EFI_STATUS
811 GetCapsuleDescriptors (
812 IN EFI_PHYSICAL_ADDRESS *DescriptorBuffer
813 )
814 {
815 EFI_STATUS Status;
816 UINTN Size;
817 UINTN Index;
818 UINTN TempIndex;
819 UINTN ValidIndex;
820 BOOLEAN Flag;
821 CHAR16 CapsuleVarName[30];
822 CHAR16 *TempVarName;
823 EFI_PHYSICAL_ADDRESS CapsuleDataPtr64;
824 EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices;
825
826 Index = 0;
827 TempVarName = NULL;
828 CapsuleVarName[0] = 0;
829 ValidIndex = 0;
830 CapsuleDataPtr64 = 0;
831
832 Status = PeiServicesLocatePpi (
833 &gEfiPeiReadOnlyVariable2PpiGuid,
834 0,
835 NULL,
836 (VOID **) &PPIVariableServices
837 );
838 if (Status == EFI_SUCCESS) {
839 StrCpyS (CapsuleVarName, sizeof(CapsuleVarName)/sizeof(CHAR16), EFI_CAPSULE_VARIABLE_NAME);
840 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);
841 Size = sizeof (CapsuleDataPtr64);
842 while (1) {
843 if (Index == 0) {
844 //
845 // For the first Capsule Image
846 //
847 Status = PPIVariableServices->GetVariable (
848 PPIVariableServices,
849 CapsuleVarName,
850 &gEfiCapsuleVendorGuid,
851 NULL,
852 &Size,
853 (VOID *) &CapsuleDataPtr64
854 );
855 if (EFI_ERROR (Status)) {
856 DEBUG ((DEBUG_INFO, "Capsule -- capsule variable not set\n"));
857 return EFI_NOT_FOUND;
858 }
859 //
860 // We have a chicken/egg situation where the memory init code needs to
861 // know the boot mode prior to initializing memory. For this case, our
862 // validate function will fail. We can detect if this is the case if blocklist
863 // pointer is null. In that case, return success since we know that the
864 // variable is set.
865 //
866 if (DescriptorBuffer == NULL) {
867 return EFI_SUCCESS;
868 }
869 } else {
870 UnicodeValueToStringS (
871 TempVarName,
872 sizeof (CapsuleVarName) - ((UINTN)TempVarName - (UINTN)CapsuleVarName),
873 0,
874 Index,
875 0
876 );
877 Status = PPIVariableServices->GetVariable (
878 PPIVariableServices,
879 CapsuleVarName,
880 &gEfiCapsuleVendorGuid,
881 NULL,
882 &Size,
883 (VOID *) &CapsuleDataPtr64
884 );
885 if (EFI_ERROR (Status)) {
886 break;
887 }
888
889 //
890 // If this BlockList has been linked before, skip this variable
891 //
892 Flag = FALSE;
893 for (TempIndex = 0; TempIndex < ValidIndex; TempIndex++) {
894 if (DescriptorBuffer[TempIndex] == CapsuleDataPtr64) {
895 Flag = TRUE;
896 break;
897 }
898 }
899 if (Flag) {
900 Index ++;
901 continue;
902 }
903 }
904
905 //
906 // Cache BlockList which has been processed
907 //
908 DescriptorBuffer[ValidIndex++] = CapsuleDataPtr64;
909 Index ++;
910 }
911 }
912
913 return EFI_SUCCESS;
914 }
915
916 /**
917 Capsule PPI service to coalesce a fragmented capsule in memory.
918
919 @param PeiServices General purpose services available to every PEIM.
920 @param MemoryBase Pointer to the base of a block of memory that we can walk
921 all over while trying to coalesce our buffers.
922 On output, this variable will hold the base address of
923 a coalesced capsule.
924 @param MemorySize Size of the memory region pointed to by MemoryBase.
925 On output, this variable will contain the size of the
926 coalesced capsule.
927
928 @retval EFI_NOT_FOUND if we can't determine the boot mode
929 if the boot mode is not flash-update
930 if we could not find the capsule descriptors
931
932 @retval EFI_BUFFER_TOO_SMALL
933 if we could not coalesce the capsule in the memory
934 region provided to us
935
936 @retval EFI_SUCCESS if there's no capsule, or if we processed the
937 capsule successfully.
938 **/
939 EFI_STATUS
940 EFIAPI
941 CapsuleCoalesce (
942 IN EFI_PEI_SERVICES **PeiServices,
943 IN OUT VOID **MemoryBase,
944 IN OUT UINTN *MemorySize
945 )
946 {
947 UINTN Index;
948 UINTN Size;
949 UINTN VariableCount;
950 CHAR16 CapsuleVarName[30];
951 CHAR16 *TempVarName;
952 EFI_PHYSICAL_ADDRESS CapsuleDataPtr64;
953 EFI_STATUS Status;
954 EFI_BOOT_MODE BootMode;
955 EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices;
956 EFI_PHYSICAL_ADDRESS *VariableArrayAddress;
957 MEMORY_RESOURCE_DESCRIPTOR *MemoryResource;
958 #ifdef MDE_CPU_IA32
959 UINT16 CoalesceImageMachineType;
960 EFI_PHYSICAL_ADDRESS CoalesceImageEntryPoint;
961 COALESCE_ENTRY CoalesceEntry;
962 EFI_CAPSULE_LONG_MODE_BUFFER LongModeBuffer;
963 #endif
964
965 Index = 0;
966 VariableCount = 0;
967 CapsuleVarName[0] = 0;
968 CapsuleDataPtr64 = 0;
969
970 //
971 // Someone should have already ascertained the boot mode. If it's not
972 // capsule update, then return normally.
973 //
974 Status = PeiServicesGetBootMode (&BootMode);
975 if (EFI_ERROR (Status) || (BootMode != BOOT_ON_FLASH_UPDATE)) {
976 DEBUG ((EFI_D_ERROR, "Boot mode is not correct for capsule update path.\n"));
977 Status = EFI_NOT_FOUND;
978 goto Done;
979 }
980
981 //
982 // User may set the same ScatterGatherList with several different variables,
983 // so cache all ScatterGatherList for check later.
984 //
985 Status = PeiServicesLocatePpi (
986 &gEfiPeiReadOnlyVariable2PpiGuid,
987 0,
988 NULL,
989 (VOID **) &PPIVariableServices
990 );
991 if (EFI_ERROR (Status)) {
992 goto Done;
993 }
994 Size = sizeof (CapsuleDataPtr64);
995 StrCpyS (CapsuleVarName, sizeof(CapsuleVarName)/sizeof(CHAR16), EFI_CAPSULE_VARIABLE_NAME);
996 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);
997 while (TRUE) {
998 if (Index > 0) {
999 UnicodeValueToStringS (
1000 TempVarName,
1001 sizeof (CapsuleVarName) - ((UINTN)TempVarName - (UINTN)CapsuleVarName),
1002 0,
1003 Index,
1004 0
1005 );
1006 }
1007 Status = PPIVariableServices->GetVariable (
1008 PPIVariableServices,
1009 CapsuleVarName,
1010 &gEfiCapsuleVendorGuid,
1011 NULL,
1012 &Size,
1013 (VOID *) &CapsuleDataPtr64
1014 );
1015 if (EFI_ERROR (Status)) {
1016 //
1017 // There is no capsule variables, quit
1018 //
1019 DEBUG ((EFI_D_INFO,"Capsule variable Index = %d\n", Index));
1020 break;
1021 }
1022 VariableCount++;
1023 Index++;
1024 }
1025
1026 DEBUG ((EFI_D_INFO,"Capsule variable count = %d\n", VariableCount));
1027
1028 //
1029 // The last entry is the end flag.
1030 //
1031 Status = PeiServicesAllocatePool (
1032 (VariableCount + 1) * sizeof (EFI_PHYSICAL_ADDRESS),
1033 (VOID **)&VariableArrayAddress
1034 );
1035
1036 if (Status != EFI_SUCCESS) {
1037 DEBUG ((EFI_D_ERROR, "AllocatePages Failed!, Status = %x\n", Status));
1038 goto Done;
1039 }
1040
1041 ZeroMem (VariableArrayAddress, (VariableCount + 1) * sizeof (EFI_PHYSICAL_ADDRESS));
1042
1043 //
1044 // Find out if we actually have a capsule.
1045 // GetCapsuleDescriptors depends on variable PPI, so it should run in 32-bit environment.
1046 //
1047 Status = GetCapsuleDescriptors (VariableArrayAddress);
1048 if (EFI_ERROR (Status)) {
1049 DEBUG ((EFI_D_ERROR, "Fail to find capsule variables.\n"));
1050 goto Done;
1051 }
1052
1053 MemoryResource = BuildMemoryResourceDescriptor ();
1054
1055 #ifdef MDE_CPU_IA32
1056 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
1057 //
1058 // Switch to 64-bit mode to process capsule data when:
1059 // 1. When DXE phase is 64-bit
1060 // 2. When the buffer for 64-bit transition exists
1061 // 3. When Capsule X64 image is built in BIOS image
1062 // In 64-bit mode, we can process capsule data above 4GB.
1063 //
1064 CoalesceImageEntryPoint = 0;
1065 Status = GetLongModeContext (&LongModeBuffer);
1066 if (EFI_ERROR (Status)) {
1067 DEBUG ((EFI_D_ERROR, "Fail to find the variable for long mode context!\n"));
1068 Status = EFI_NOT_FOUND;
1069 goto Done;
1070 }
1071
1072 Status = FindCapsuleCoalesceImage (&CoalesceImageEntryPoint, &CoalesceImageMachineType);
1073 if ((EFI_ERROR (Status)) || (CoalesceImageMachineType != EFI_IMAGE_MACHINE_X64)) {
1074 DEBUG ((EFI_D_ERROR, "Fail to find CapsuleX64 module in FV!\n"));
1075 Status = EFI_NOT_FOUND;
1076 goto Done;
1077 }
1078 ASSERT (CoalesceImageEntryPoint != 0);
1079 CoalesceEntry = (COALESCE_ENTRY) (UINTN) CoalesceImageEntryPoint;
1080 Status = ModeSwitch (&LongModeBuffer, CoalesceEntry, (EFI_PHYSICAL_ADDRESS)(UINTN)VariableArrayAddress, MemoryResource, MemoryBase, MemorySize);
1081 } else {
1082 //
1083 // Capsule is processed in IA32 mode.
1084 //
1085 Status = CapsuleDataCoalesce (PeiServices, (EFI_PHYSICAL_ADDRESS *)(UINTN)VariableArrayAddress, MemoryResource, MemoryBase, MemorySize);
1086 }
1087 #else
1088 //
1089 // Process capsule directly.
1090 //
1091 Status = CapsuleDataCoalesce (PeiServices, (EFI_PHYSICAL_ADDRESS *)(UINTN)VariableArrayAddress, MemoryResource, MemoryBase, MemorySize);
1092 #endif
1093
1094 DEBUG ((EFI_D_INFO, "Capsule Coalesce Status = %r!\n", Status));
1095
1096 if (Status == EFI_BUFFER_TOO_SMALL) {
1097 DEBUG ((EFI_D_ERROR, "There is not enough memory to process capsule!\n"));
1098 }
1099
1100 if (Status == EFI_NOT_FOUND) {
1101 DEBUG ((EFI_D_ERROR, "Fail to parse capsule descriptor in memory!\n"));
1102 REPORT_STATUS_CODE (
1103 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
1104 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_INVALID_CAPSULE_DESCRIPTOR)
1105 );
1106 }
1107
1108 Done:
1109 return Status;
1110 }
1111
1112 /**
1113 Determine if we're in capsule update boot mode.
1114
1115 @param PeiServices PEI services table
1116
1117 @retval EFI_SUCCESS if we have a capsule available
1118 @retval EFI_NOT_FOUND no capsule detected
1119
1120 **/
1121 EFI_STATUS
1122 EFIAPI
1123 CheckCapsuleUpdate (
1124 IN EFI_PEI_SERVICES **PeiServices
1125 )
1126 {
1127 EFI_STATUS Status;
1128 Status = GetCapsuleDescriptors (NULL);
1129 return Status;
1130 }
1131 /**
1132 This function will look at a capsule and determine if it's a test pattern.
1133 If it is, then it will verify it and emit an error message if corruption is detected.
1134
1135 @param PeiServices Standard pei services pointer
1136 @param CapsuleBase Base address of coalesced capsule, which is preceeded
1137 by private data. Very implementation specific.
1138
1139 @retval TRUE Capsule image is the test image
1140 @retval FALSE Capsule image is not the test image.
1141
1142 **/
1143 BOOLEAN
1144 CapsuleTestPattern (
1145 IN EFI_PEI_SERVICES **PeiServices,
1146 IN VOID *CapsuleBase
1147 )
1148 {
1149 UINT32 *TestPtr;
1150 UINT32 TestCounter;
1151 UINT32 TestSize;
1152 BOOLEAN RetValue;
1153
1154 RetValue = FALSE;
1155
1156 //
1157 // Look at the capsule data and determine if it's a test pattern. If it
1158 // is, then test it now.
1159 //
1160 TestPtr = (UINT32 *) CapsuleBase;
1161 //
1162 // 0x54534554 "TEST"
1163 //
1164 if (*TestPtr == 0x54534554) {
1165 RetValue = TRUE;
1166 DEBUG ((EFI_D_INFO, "Capsule test pattern mode activated...\n"));
1167 TestSize = TestPtr[1] / sizeof (UINT32);
1168 //
1169 // Skip over the signature and the size fields in the pattern data header
1170 //
1171 TestPtr += 2;
1172 TestCounter = 0;
1173 while (TestSize > 0) {
1174 if (*TestPtr != TestCounter) {
1175 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));
1176 return TRUE;
1177 }
1178
1179 TestPtr++;
1180 TestCounter++;
1181 TestSize--;
1182 }
1183
1184 DEBUG ((EFI_D_INFO, "Capsule test pattern mode SUCCESS\n"));
1185 }
1186
1187 return RetValue;
1188 }
1189
1190 /**
1191 Capsule PPI service that gets called after memory is available. The
1192 capsule coalesce function, which must be called first, returns a base
1193 address and size, which can be anything actually. Once the memory init
1194 PEIM has discovered memory, then it should call this function and pass in
1195 the base address and size returned by the coalesce function. Then this
1196 function can create a capsule HOB and return.
1197
1198 @param PeiServices standard pei services pointer
1199 @param CapsuleBase address returned by the capsule coalesce function. Most
1200 likely this will actually be a pointer to private data.
1201 @param CapsuleSize value returned by the capsule coalesce function.
1202
1203 @retval EFI_VOLUME_CORRUPTED CapsuleBase does not appear to point to a
1204 coalesced capsule
1205 @retval EFI_SUCCESS if all goes well.
1206 **/
1207 EFI_STATUS
1208 EFIAPI
1209 CreateState (
1210 IN EFI_PEI_SERVICES **PeiServices,
1211 IN VOID *CapsuleBase,
1212 IN UINTN CapsuleSize
1213 )
1214 {
1215 EFI_STATUS Status;
1216 EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateData;
1217 UINTN Size;
1218 EFI_PHYSICAL_ADDRESS NewBuffer;
1219 UINTN CapsuleNumber;
1220 UINT32 Index;
1221 EFI_PHYSICAL_ADDRESS BaseAddress;
1222 UINT64 Length;
1223
1224 PrivateData = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) CapsuleBase;
1225 if (PrivateData->Signature != EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE) {
1226 return EFI_VOLUME_CORRUPTED;
1227 }
1228 if (PrivateData->CapsuleAllImageSize >= MAX_ADDRESS) {
1229 DEBUG ((EFI_D_ERROR, "CapsuleAllImageSize too big - 0x%lx\n", PrivateData->CapsuleAllImageSize));
1230 return EFI_OUT_OF_RESOURCES;
1231 }
1232 if (PrivateData->CapsuleNumber >= MAX_ADDRESS) {
1233 DEBUG ((EFI_D_ERROR, "CapsuleNumber too big - 0x%lx\n", PrivateData->CapsuleNumber));
1234 return EFI_OUT_OF_RESOURCES;
1235 }
1236 //
1237 // Capsule Number and Capsule Offset is in the tail of Capsule data.
1238 //
1239 Size = (UINTN)PrivateData->CapsuleAllImageSize;
1240 CapsuleNumber = (UINTN)PrivateData->CapsuleNumber;
1241 //
1242 // Allocate the memory so that it gets preserved into DXE
1243 //
1244 Status = PeiServicesAllocatePages (
1245 EfiRuntimeServicesData,
1246 EFI_SIZE_TO_PAGES (Size),
1247 &NewBuffer
1248 );
1249
1250 if (Status != EFI_SUCCESS) {
1251 DEBUG ((EFI_D_ERROR, "AllocatePages Failed!\n"));
1252 return Status;
1253 }
1254 //
1255 // Copy to our new buffer for DXE
1256 //
1257 DEBUG ((EFI_D_INFO, "Capsule copy from 0x%8X to 0x%8X with size 0x%8X\n", (UINTN)((UINT8 *)PrivateData + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (CapsuleNumber - 1) * sizeof(UINT64)), (UINTN) NewBuffer, Size));
1258 CopyMem ((VOID *) (UINTN) NewBuffer, (VOID *) (UINTN) ((UINT8 *)PrivateData + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (CapsuleNumber - 1) * sizeof(UINT64)), Size);
1259 //
1260 // Check for test data pattern. If it is the test pattern, then we'll
1261 // test it and still create the HOB so that it can be used to verify
1262 // that capsules don't get corrupted all the way into BDS. BDS will
1263 // still try to turn it into a firmware volume, but will think it's
1264 // corrupted so nothing will happen.
1265 //
1266 DEBUG_CODE (
1267 CapsuleTestPattern (PeiServices, (VOID *) (UINTN) NewBuffer);
1268 );
1269
1270 //
1271 // Build the UEFI Capsule Hob for each capsule image.
1272 //
1273 for (Index = 0; Index < CapsuleNumber; Index ++) {
1274 BaseAddress = NewBuffer + PrivateData->CapsuleOffset[Index];
1275 Length = ((EFI_CAPSULE_HEADER *)((UINTN) BaseAddress))->CapsuleImageSize;
1276
1277 BuildCvHob (BaseAddress, Length);
1278 }
1279
1280 return EFI_SUCCESS;
1281 }
1282
1283 CONST EFI_PEI_CAPSULE_PPI mCapsulePpi = {
1284 CapsuleCoalesce,
1285 CheckCapsuleUpdate,
1286 CreateState
1287 };
1288
1289 CONST EFI_PEI_PPI_DESCRIPTOR mUefiPpiListCapsule = {
1290 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
1291 &gEfiPeiCapsulePpiGuid,
1292 (EFI_PEI_CAPSULE_PPI *) &mCapsulePpi
1293 };
1294
1295 /**
1296 Entry point function for the PEIM
1297
1298 @param FileHandle Handle of the file being invoked.
1299 @param PeiServices Describes the list of possible PEI Services.
1300
1301 @return EFI_SUCCESS If we installed our PPI
1302
1303 **/
1304 EFI_STATUS
1305 EFIAPI
1306 CapsuleMain (
1307 IN EFI_PEI_FILE_HANDLE FileHandle,
1308 IN CONST EFI_PEI_SERVICES **PeiServices
1309 )
1310 {
1311 //
1312 // Just produce our PPI
1313 //
1314 return PeiServicesInstallPpi (&mUefiPpiListCapsule);
1315 }