]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/CapsulePei/Common/CapsuleCoalesce.c
Currently, CapsulePei assumes ScatterGatherList are all related. But, UEFI spec has...
[mirror_edk2.git] / MdeModulePkg / Universal / CapsulePei / Common / CapsuleCoalesce.c
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
411 TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;\r
412\r
413 DEBUG ((EFI_D_INFO, "Capsule relocate descriptors from/to/size 0x%X 0x%X 0x%X\n", (UINT32)(UINTN)TempBlockDesc->Union.DataBlock, (UINT32)(UINTN)RelocBuffer, (UINT32)(UINTN)TempBlockDesc->Length));\r
414 }\r
415 }\r
416 TempBlockDesc++;\r
417 }\r
418 //\r
419 // Now go through all the block descriptors to make sure that they're not\r
420 // in the memory region we want to copy them to.\r
421 //\r
422 CurrBlockDescHead = BlockList;\r
423 PrevBlockDescTail = NULL;\r
424 while ((CurrBlockDescHead != NULL) && (CurrBlockDescHead->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
425 //\r
426 // Get the size of this list then see if it overlaps our low region\r
427 //\r
428 TempBlockDesc = CurrBlockDescHead;\r
429 BlockListSize = sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r
430 while (TempBlockDesc->Length != 0) {\r
431 BlockListSize += sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r
432 TempBlockDesc++;\r
433 }\r
434\r
435 if (IsOverlapped (\r
436 (UINT8 *) NewBlockList,\r
437 BufferSize,\r
438 (UINT8 *) CurrBlockDescHead,\r
439 BlockListSize\r
440 )) {\r
441 //\r
442 // Overlaps, so move it out of the way\r
443 //\r
444 RelocBuffer = FindFreeMem (BlockList, MemBase, MemSize, BlockListSize);\r
445 if (RelocBuffer == NULL) {\r
446 return NULL;\r
447 }\r
448 CopyMem ((VOID *) RelocBuffer, (VOID *) CurrBlockDescHead, BlockListSize);\r
449 DEBUG ((EFI_D_INFO, "Capsule reloc descriptor block #2\n"));\r
450 //\r
451 // Point the previous block's next point to this copied version. If\r
452 // the tail pointer is null, then this is the first descriptor block.\r
453 //\r
454 if (PrevBlockDescTail == NULL) {\r
455 BlockList = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) RelocBuffer;\r
456 } else {\r
457 PrevBlockDescTail->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;\r
458 }\r
459 }\r
460 //\r
461 // Save our new tail and jump to the next block list\r
462 //\r
463 PrevBlockDescTail = TempBlockDesc;\r
464 CurrBlockDescHead = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;\r
465 }\r
466 //\r
467 // Cleared out low memory. Now copy the descriptors down there.\r
468 //\r
469 TempBlockDesc = BlockList;\r
470 CurrBlockDescHead = NewBlockList;\r
471 while ((TempBlockDesc != NULL) && (TempBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
472 if (TempBlockDesc->Length != 0) {\r
473 CurrBlockDescHead->Union.DataBlock = TempBlockDesc->Union.DataBlock;\r
474 CurrBlockDescHead->Length = TempBlockDesc->Length;\r
475 CurrBlockDescHead++;\r
476 TempBlockDesc++;\r
477 } else {\r
478 TempBlockDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;\r
479 }\r
480 }\r
481 //\r
482 // Null terminate\r
483 //\r
484 CurrBlockDescHead->Union.ContinuationPointer = (EFI_PHYSICAL_ADDRESS) (UINTN) NULL;\r
485 CurrBlockDescHead->Length = 0;\r
486 return NewBlockList;\r
487}\r
488\r
489/**\r
490 Determine if two buffers overlap in memory.\r
491\r
492 @param Buff1 pointer to first buffer\r
493 @param Size1 size of Buff1\r
494 @param Buff2 pointer to second buffer\r
495 @param Size2 size of Buff2\r
496\r
497 @retval TRUE Buffers overlap in memory.\r
498 @retval FALSE Buffer doesn't overlap.\r
499\r
500**/\r
501BOOLEAN\r
502IsOverlapped (\r
503 UINT8 *Buff1,\r
504 UINTN Size1,\r
505 UINT8 *Buff2,\r
506 UINTN Size2\r
507 )\r
508{\r
509 //\r
510 // If buff1's end is less than the start of buff2, then it's ok.\r
511 // Also, if buff1's start is beyond buff2's end, then it's ok.\r
512 //\r
513 if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {\r
514 return FALSE;\r
515 }\r
516\r
517 return TRUE;\r
518}\r
519\r
520/**\r
521 Given a pointer to a capsule block descriptor, traverse the list to figure\r
522 out how many legitimate descriptors there are, and how big the capsule it\r
523 refers to is.\r
524\r
525 @param Desc Pointer to the capsule block descriptors\r
526 NumDescriptors - optional pointer to where to return the number of descriptors\r
527 CapsuleSize - optional pointer to where to return the capsule size\r
528 @param NumDescriptors Optional pointer to where to return the number of descriptors\r
529 @param CapsuleSize Optional pointer to where to return the capsule size\r
530\r
531 @retval EFI_NOT_FOUND No descriptors containing data in the list\r
532 @retval EFI_SUCCESS Return data is valid\r
533\r
534**/\r
535EFI_STATUS\r
536GetCapsuleInfo (\r
537 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc,\r
538 IN OUT UINTN *NumDescriptors OPTIONAL,\r
539 IN OUT UINTN *CapsuleSize OPTIONAL\r
540 )\r
541{\r
542 UINTN Count;\r
543 UINTN Size;\r
544\r
f63085f5 545 ASSERT (Desc != NULL);\r
546\r
ab7017fe 547 Count = 0;\r
548 Size = 0;\r
549\r
550 while (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r
551 if (Desc->Length == 0) {\r
552 //\r
553 // Descriptor points to another list of block descriptors somewhere\r
554 //\r
555 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;\r
556 } else {\r
557 Size += (UINTN) Desc->Length;\r
558 Count++;\r
559 Desc++;\r
560 }\r
561 }\r
562 //\r
563 // If no descriptors, then fail\r
564 //\r
565 if (Count == 0) {\r
566 return EFI_NOT_FOUND;\r
567 }\r
568\r
569 if (NumDescriptors != NULL) {\r
570 *NumDescriptors = Count;\r
571 }\r
572\r
573 if (CapsuleSize != NULL) {\r
574 *CapsuleSize = Size;\r
575 }\r
576\r
577 return EFI_SUCCESS;\r
578}\r
579\r
580/**\r
581 Check every capsule header.\r
582\r
583 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER\r
584\r
585 @retval FALSE Capsule is OK\r
586 @retval TRUE Capsule is corrupted \r
587\r
588**/\r
589BOOLEAN\r
590IsCapsuleCorrupted (\r
591 IN EFI_CAPSULE_HEADER *CapsuleHeader\r
592 )\r
593{\r
594 //\r
595 //A capsule to be updated across a system reset should contain CAPSULE_FLAGS_PERSIST_ACROSS_RESET.\r
596 //\r
597 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {\r
598 return TRUE;\r
599 }\r
600 //\r
601 //Make sure the flags combination is supported by the platform.\r
602 //\r
603 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r
604 return TRUE;\r
605 }\r
606 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
607 return TRUE;\r
608 }\r
609\r
610 return FALSE;\r
611}\r
612\r
613/**\r
614 Try to verify the integrity of a capsule test pattern before the\r
615 capsule gets coalesced. This can be useful in narrowing down\r
616 where capsule data corruption occurs.\r
617\r
618 The test pattern mode fills in memory with a counting UINT32 value. \r
619 If the capsule is not divided up in a multiple of 4-byte blocks, then\r
620 things get messy doing the check. Therefore there are some cases\r
621 here where we just give up and skip the pre-coalesce check.\r
622\r
623 @param PeiServices PEI services table\r
624 @param Desc Pointer to capsule descriptors\r
625**/\r
626VOID\r
627CapsuleTestPatternPreCoalesce (\r
628 IN EFI_PEI_SERVICES **PeiServices,\r
629 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc\r
630 )\r
631{\r
632 UINT32 *TestPtr;\r
633 UINT32 TestCounter;\r
634 UINT32 TestSize;\r
635 //\r
636 // Find first data descriptor\r
637 //\r
638 while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
639 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;\r
640 }\r
641\r
642 if (Desc->Union.ContinuationPointer == 0) {\r
643 return ;\r
644 }\r
645 //\r
646 // First one better be long enough to at least hold the test signature\r
647 //\r
648 if (Desc->Length < sizeof (UINT32)) {\r
649 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #1\n"));\r
650 return ;\r
651 }\r
652\r
653 TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;\r
654 //\r
655 // 0x54534554 "TEST"\r
656 //\r
657 if (*TestPtr != 0x54534554) {\r
658 return ;\r
659 }\r
660\r
661 TestCounter = 0;\r
662 TestSize = (UINT32) Desc->Length - 2 * sizeof (UINT32);\r
663 //\r
664 // Skip over the signature and the size fields in the pattern data header\r
665 //\r
666 TestPtr += 2;\r
667 while (1) {\r
668 if ((TestSize & 0x03) != 0) {\r
669 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #2\n"));\r
670 return ;\r
671 }\r
672\r
673 while (TestSize > 0) {\r
674 if (*TestPtr != TestCounter) {\r
675 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce failed data corruption check\n"));\r
676 return ;\r
677 }\r
678\r
679 TestSize -= sizeof (UINT32);\r
680 TestCounter++;\r
681 TestPtr++;\r
682 }\r
683 Desc++;\r
684 while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
685 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;\r
686 }\r
687\r
688 if (Desc->Union.ContinuationPointer == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r
689 return ;\r
690 }\r
691 TestSize = (UINT32) Desc->Length;\r
692 TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;\r
693 }\r
694}\r
695\r
696/**\r
697 Checks for the presence of capsule descriptors.\r
698 Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...\r
699\r
700 @param BlockListBuffer Pointer to the buffer of capsule descriptors variables\r
701 @param BlockDescriptorList Pointer to the capsule descriptors list\r
702\r
703 @retval EFI_SUCCESS a valid capsule is present\r
704 @retval EFI_NOT_FOUND if a valid capsule is not present\r
705**/\r
706EFI_STATUS\r
707BuildCapsuleDescriptors (\r
708 IN EFI_PHYSICAL_ADDRESS *BlockListBuffer,\r
709 OUT EFI_CAPSULE_BLOCK_DESCRIPTOR **BlockDescriptorList \r
710 )\r
711{\r
712 UINTN Index;\r
713 EFI_CAPSULE_BLOCK_DESCRIPTOR *LastBlock;\r
714 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlock;\r
715 EFI_CAPSULE_BLOCK_DESCRIPTOR *HeadBlock;\r
716\r
717 LastBlock = NULL;\r
718 HeadBlock = NULL;\r
719 TempBlock = NULL;\r
720 Index = 0;\r
721\r
722 while (BlockListBuffer[Index] != 0) {\r
31abeada
SZ
723 //\r
724 // Test integrity of descriptors.\r
725 //\r
726 TempBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index]);\r
727 if (TempBlock != NULL) {\r
ab7017fe 728 if (LastBlock == NULL) {\r
31abeada
SZ
729 LastBlock = TempBlock;\r
730\r
731 //\r
732 // Return the base of the block descriptors\r
733 //\r
734 HeadBlock = (EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index];\r
735 } else {\r
736 //\r
737 // Combine the different BlockList into single BlockList.\r
738 //\r
739 LastBlock->Union.DataBlock = (EFI_PHYSICAL_ADDRESS)(UINTN)BlockListBuffer[Index];\r
740 LastBlock->Length = 0;\r
741 LastBlock = TempBlock;\r
ab7017fe 742 }\r
ab7017fe 743 }\r
744 Index ++;\r
745 }\r
746 \r
747 if (HeadBlock != NULL) {\r
748 *BlockDescriptorList = HeadBlock;\r
749 return EFI_SUCCESS;\r
750 }\r
751 return EFI_NOT_FOUND;\r
752}\r
753\r
754/**\r
755 The function to coalesce a fragmented capsule in memory.\r
756\r
757 Memory Map for coalesced capsule:\r
758 MemBase + ---->+---------------------------+<-----------+\r
759 MemSize | CapsuleOffset[49] | |\r
760 +---------------------------+ |\r
761 | ................ | |\r
762 +---------------------------+ |\r
763 | CapsuleOffset[2] | |\r
764 +---------------------------+ |\r
765 | CapsuleOffset[1] | |\r
766 +---------------------------+ |\r
767 | CapsuleOffset[0] | CapsuleSize \r
768 +---------------------------+ |\r
769 | CapsuleNumber | |\r
770 +---------------------------+ |\r
771 | | | \r
772 | | | \r
773 | Capsule Image | | \r
774 | | | \r
775 | | | \r
776 +---------------------------+ |\r
777 | PrivateData | |\r
778 DestPtr ----> +---------------------------+<-----------+\r
779 | | |\r
780 | FreeMem | FreeMemSize\r
781 | | |\r
782 FreeMemBase --->+---------------------------+<-----------+\r
783 | Terminator |\r
784 +---------------------------+\r
785 | BlockDescriptor n |\r
786 +---------------------------+\r
787 | ................. |\r
788 +---------------------------+\r
789 | BlockDescriptor 1 |\r
790 +---------------------------+\r
791 | BlockDescriptor 0 |\r
792 +---------------------------+\r
793 | PrivateDataDesc 0 |\r
794 MemBase ---->+---------------------------+<----- BlockList\r
795\r
796 @param PeiServices General purpose services available to every PEIM.\r
797 @param BlockListBuffer Point to the buffer of Capsule Descriptor Variables.\r
798 @param MemoryBase Pointer to the base of a block of memory that we can walk\r
799 all over while trying to coalesce our buffers.\r
800 On output, this variable will hold the base address of\r
801 a coalesced capsule.\r
802 @param MemorySize Size of the memory region pointed to by MemoryBase.\r
803 On output, this variable will contain the size of the\r
804 coalesced capsule.\r
805\r
806 @retval EFI_NOT_FOUND If we could not find the capsule descriptors.\r
807\r
808 @retval EFI_BUFFER_TOO_SMALL\r
809 If we could not coalesce the capsule in the memory\r
810 region provided to us.\r
811\r
812 @retval EFI_SUCCESS Processed the capsule successfully.\r
813**/\r
814EFI_STATUS\r
815EFIAPI\r
816CapsuleDataCoalesce (\r
817 IN EFI_PEI_SERVICES **PeiServices,\r
818 IN EFI_PHYSICAL_ADDRESS *BlockListBuffer,\r
819 IN OUT VOID **MemoryBase,\r
820 IN OUT UINTN *MemorySize\r
821 )\r
822{\r
823 VOID *NewCapsuleBase;\r
824 VOID *DataPtr;\r
825 UINT8 CapsuleIndex;\r
826 UINT8 *FreeMemBase;\r
827 UINT8 *DestPtr;\r
828 UINT8 *RelocPtr;\r
829 UINT32 CapsuleOffset[MAX_SUPPORT_CAPSULE_NUM]; \r
830 UINT32 *AddDataPtr;\r
831 UINT32 CapsuleTimes; \r
832 UINT64 SizeLeft; \r
833 UINT64 CapsuleImageSize; \r
834 UINTN CapsuleSize;\r
835 UINTN DescriptorsSize;\r
836 UINTN FreeMemSize;\r
837 UINTN NumDescriptors;\r
838 BOOLEAN IsCorrupted;\r
839 BOOLEAN CapsuleBeginFlag;\r
840 EFI_STATUS Status;\r
841 EFI_CAPSULE_HEADER *CapsuleHeader;\r
842 EFI_CAPSULE_PEIM_PRIVATE_DATA PrivateData;\r
843 EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateDataPtr;\r
844 EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList;\r
845 EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrentBlockDesc;\r
846 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;\r
847 EFI_CAPSULE_BLOCK_DESCRIPTOR PrivateDataDesc[2];\r
848\r
849 CapsuleIndex = 0;\r
850 SizeLeft = 0;\r
851 CapsuleTimes = 0;\r
852 CapsuleImageSize = 0;\r
853 PrivateDataPtr = NULL;\r
854 AddDataPtr = NULL;\r
855 CapsuleHeader = NULL;\r
856 CapsuleBeginFlag = TRUE;\r
857 IsCorrupted = TRUE;\r
858 CapsuleSize = 0;\r
859 NumDescriptors = 0;\r
860 \r
861 //\r
862 // Build capsule descriptors list\r
863 //\r
864 Status = BuildCapsuleDescriptors (BlockListBuffer, &BlockList);\r
865 if (EFI_ERROR (Status)) {\r
866 return Status;\r
867 }\r
868\r
869 DEBUG_CODE (\r
870 CapsuleTestPatternPreCoalesce (PeiServices, BlockList);\r
871 );\r
872\r
873 //\r
874 // Get the size of our descriptors and the capsule size. GetCapsuleInfo()\r
875 // returns the number of descriptors that actually point to data, so add\r
876 // one for a terminator. Do that below.\r
877 //\r
878 GetCapsuleInfo (BlockList, &NumDescriptors, &CapsuleSize);\r
879 if ((CapsuleSize == 0) || (NumDescriptors == 0)) {\r
880 return EFI_NOT_FOUND;\r
881 }\r
882\r
883 //\r
884 // Initialize our local copy of private data. When we're done, we'll create a\r
885 // descriptor for it as well so that it can be put into free memory without\r
886 // trashing anything.\r
887 //\r
888 PrivateData.Signature = EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE;\r
889 PrivateData.CapsuleSize = (UINT32) CapsuleSize;\r
890 PrivateDataDesc[0].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) &PrivateData;\r
891 PrivateDataDesc[0].Length = sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA);\r
892 PrivateDataDesc[1].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) BlockList;\r
893 PrivateDataDesc[1].Length = 0;\r
894 //\r
895 // In addition to PrivateDataDesc[1:0], one terminator is added\r
896 // See below RelocateBlockDescriptors()\r
897 //\r
898 NumDescriptors += 3;\r
899 CapsuleSize += sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + sizeof(CapsuleOffset) + sizeof(UINT32);\r
900 BlockList = PrivateDataDesc;\r
901 DescriptorsSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r
902\r
903 //\r
904 // Don't go below some min address. If the base is below it,\r
905 // then move it up and adjust the size accordingly.\r
906 //\r
907 DEBUG ((EFI_D_INFO, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN) *MemoryBase, (UINTN)*MemoryBase + *MemorySize));\r
908 if ((UINTN)*MemoryBase < (UINTN) MIN_COALESCE_ADDR) {\r
909 if (((UINTN)*MemoryBase + *MemorySize) < (UINTN) MIN_COALESCE_ADDR) {\r
910 return EFI_BUFFER_TOO_SMALL;\r
911 } else {\r
912 *MemorySize = *MemorySize - ((UINTN) MIN_COALESCE_ADDR - (UINTN) *MemoryBase);\r
913 *MemoryBase = (VOID *) (UINTN) MIN_COALESCE_ADDR;\r
914 }\r
915 }\r
916\r
917 if (*MemorySize <= (CapsuleSize + DescriptorsSize)) {\r
918 return EFI_BUFFER_TOO_SMALL;\r
919 }\r
920\r
921 FreeMemBase = *MemoryBase;\r
922 FreeMemSize = *MemorySize;\r
923 DEBUG ((EFI_D_INFO, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN) FreeMemBase, (UINTN) FreeMemBase + FreeMemSize));\r
924\r
925 //\r
926 // Relocate all the block descriptors to low memory to make further\r
927 // processing easier.\r
928 //\r
929 BlockList = RelocateBlockDescriptors (PeiServices, BlockList, FreeMemBase, FreeMemSize);\r
930 if (BlockList == NULL) {\r
931 //\r
932 // Not enough room to relocate the descriptors\r
933 //\r
934 return EFI_BUFFER_TOO_SMALL;\r
935 }\r
936\r
937 //\r
938 // Take the top of memory for the capsule. Naturally align.\r
939 //\r
940 DestPtr = FreeMemBase + FreeMemSize - CapsuleSize;\r
941 DestPtr = (UINT8 *) ((UINTN) DestPtr &~ (UINTN) (sizeof (UINTN) - 1));\r
942 FreeMemBase = (UINT8 *) BlockList + DescriptorsSize;\r
4694dd1b 943 FreeMemSize = (UINTN) DestPtr - (UINTN) FreeMemBase;\r
ab7017fe 944 NewCapsuleBase = (VOID *) DestPtr;\r
945\r
946 //\r
947 // Move all the blocks to the top (high) of memory.\r
948 // Relocate all the obstructing blocks. Note that the block descriptors\r
949 // were coalesced when they were relocated, so we can just ++ the pointer.\r
950 //\r
951 CurrentBlockDesc = BlockList;\r
952 while ((CurrentBlockDesc->Length != 0) || (CurrentBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
953 //\r
954 // See if any of the remaining capsule blocks are in the way\r
955 //\r
956 TempBlockDesc = CurrentBlockDesc;\r
957 while (TempBlockDesc->Length != 0) {\r
958 //\r
959 // Is this block in the way of where we want to copy the current descriptor to?\r
960 //\r
961 if (IsOverlapped (\r
962 (UINT8 *) DestPtr,\r
963 (UINTN) CurrentBlockDesc->Length,\r
964 (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,\r
965 (UINTN) TempBlockDesc->Length\r
966 )) {\r
967 //\r
968 // Relocate the block\r
969 //\r
970 RelocPtr = FindFreeMem (BlockList, FreeMemBase, FreeMemSize, (UINTN) TempBlockDesc->Length);\r
971 if (RelocPtr == NULL) {\r
972 return EFI_BUFFER_TOO_SMALL;\r
973 }\r
974\r
975 CopyMem ((VOID *) RelocPtr, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);\r
976 DEBUG ((EFI_D_INFO, "Capsule reloc data block from 0x%8X to 0x%8X with size 0x%8X\n",\r
977 (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) RelocPtr, (UINTN) TempBlockDesc->Length));\r
978\r
979 TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocPtr;\r
980 }\r
981 //\r
982 // Next descriptor\r
983 //\r
984 TempBlockDesc++;\r
985 }\r
986 //\r
987 // Ok, we made it through. Copy the block.\r
988 // we just support greping one capsule from the lists of block descs list.\r
989 //\r
990 CapsuleTimes ++;\r
991 //\r
992 //Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA\r
993 //\r
994 if (CapsuleTimes > 1) {\r
995 //\r
996 //For every capsule entry point, check its header to determine whether to relocate it.\r
997 //If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.\r
998 //\r
999 if (CapsuleBeginFlag) {\r
1000 CapsuleBeginFlag = FALSE;\r
1001 CapsuleHeader = (EFI_CAPSULE_HEADER*)(UINTN)CurrentBlockDesc->Union.DataBlock;\r
1002 SizeLeft = CapsuleHeader->CapsuleImageSize;\r
1003 if (!IsCapsuleCorrupted (CapsuleHeader)) {\r
1004\r
1005 if (CapsuleIndex > (MAX_SUPPORT_CAPSULE_NUM - 1)) {\r
1006 DEBUG ((EFI_D_ERROR, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM));\r
1007 return EFI_BUFFER_TOO_SMALL;\r
1008 }\r
1009\r
1010 //\r
1011 // Relocate this valid capsule\r
1012 //\r
1013 IsCorrupted = FALSE;\r
1014 CapsuleImageSize += SizeLeft;\r
ab7017fe 1015 //\r
1016 // Cache the begin offset of this capsule\r
1017 //\r
1018 CapsuleOffset[CapsuleIndex++] = (UINT32) (UINTN) DestPtr - (UINT32)(UINTN)NewCapsuleBase - (UINT32)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA);\r
240bc4ee
SZ
1019 }\r
1020 }\r
1021\r
1022 if (CurrentBlockDesc->Length < SizeLeft) {\r
1023 if (!IsCorrupted) {\r
1024 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) (CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);\r
1025 DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r
1026 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r
ab7017fe 1027 DestPtr += CurrentBlockDesc->Length;\r
1028 }\r
240bc4ee
SZ
1029 SizeLeft -= CurrentBlockDesc->Length;\r
1030 } else {\r
ab7017fe 1031 //\r
240bc4ee 1032 //Here is the end of the current capsule image.\r
ab7017fe 1033 //\r
240bc4ee
SZ
1034 if (!IsCorrupted) {\r
1035 CopyMem ((VOID *) DestPtr, (VOID *)(UINTN)(CurrentBlockDesc->Union.DataBlock), (UINTN) SizeLeft);\r
1036 DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r
1037 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN) SizeLeft));\r
1038 DestPtr += SizeLeft;\r
ab7017fe 1039 }\r
ab7017fe 1040 //\r
240bc4ee 1041 // Start the next cycle\r
ab7017fe 1042 //\r
240bc4ee
SZ
1043 SizeLeft = 0;\r
1044 IsCorrupted = TRUE;\r
1045 CapsuleBeginFlag = TRUE; \r
ab7017fe 1046 }\r
1047 } else {\r
1048 //\r
1049 //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.\r
1050 //\r
1051 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);\r
1052 DestPtr += CurrentBlockDesc->Length;\r
1053 }\r
1054 //\r
1055 //Walk through the block descriptor list.\r
1056 //\r
1057 CurrentBlockDesc++;\r
1058 }\r
1059 //\r
1060 // We return the base of memory we want reserved, and the size.\r
1061 // The memory peim should handle it appropriately from there.\r
1062 //\r
2e4c9e01 1063 *MemorySize = (UINTN) CapsuleSize;\r
ab7017fe 1064 *MemoryBase = (VOID *) NewCapsuleBase;\r
1065\r
1066 //\r
1067 //Append the offsets of mutiply capsules to the continous buffer\r
1068 //\r
1069 DataPtr = (VOID*)((UINTN)NewCapsuleBase + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (UINTN)CapsuleImageSize);\r
1070 AddDataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) &~ (UINT32) (sizeof (UINT32) - 1));\r
1071\r
1072 *AddDataPtr++ = CapsuleIndex;\r
1073\r
1074 CopyMem (AddDataPtr, &CapsuleOffset[0], sizeof (UINT32) * CapsuleIndex);\r
1075\r
1076 PrivateDataPtr = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) NewCapsuleBase;\r
1077 PrivateDataPtr->CapsuleSize = (UINT32) CapsuleImageSize;\r
1078\r
1079 return EFI_SUCCESS;\r
1080}\r