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