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