]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/CapsulePei/Common/CapsuleCoalesce.c
Add capsule > 4GB support. When capsule data is put above 4GB, IA32 PEI transfers...
[mirror_edk2.git] / MdeModulePkg / Universal / CapsulePei / Common / CapsuleCoalesce.c
CommitLineData
ab7017fe 1/** @file\r
2 The logic to process capsule.\r
3\r
4Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
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
303 } else {\r
304 if (CapsuleSize >= Ptr->Length) {\r
305 CapsuleSize = CapsuleSize - Ptr->Length;\r
306 } else {\r
307 CapsuleSize = 0;\r
308 }\r
309 }\r
310 //\r
311 // Move to next BLOCK descriptor\r
312 //\r
313 Ptr++;\r
314 }\r
315 }\r
316\r
317 if (CapsuleCount == 0) {\r
318 //\r
319 // No any capsule is found in BlockList.\r
320 //\r
321 return NULL;\r
322 }\r
323\r
324 return Ptr;\r
325}\r
326\r
327/**\r
328 The capsule block descriptors may be fragmented and spread all over memory.\r
329 To simplify the coalescing of capsule blocks, first coalesce all the\r
330 capsule block descriptors low in memory.\r
331\r
332 The descriptors passed in can be fragmented throughout memory. Here\r
333 they are relocated into memory to turn them into a contiguous (null\r
334 terminated) array.\r
335\r
336 @param PeiServices pointer to PEI services table\r
337 @param BlockList pointer to the capsule block descriptors\r
338 @param MemBase base of system memory in which we can work\r
339 @param MemSize size of the system memory pointed to by MemBase\r
340\r
341 @retval NULL could not relocate the descriptors\r
342 @retval Pointer to the base of the successfully-relocated block descriptors. \r
343\r
344**/\r
345EFI_CAPSULE_BLOCK_DESCRIPTOR *\r
346RelocateBlockDescriptors (\r
347 IN EFI_PEI_SERVICES **PeiServices,\r
348 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList,\r
349 IN UINT8 *MemBase,\r
350 IN UINTN MemSize\r
351 )\r
352{\r
353 EFI_CAPSULE_BLOCK_DESCRIPTOR *NewBlockList;\r
354 EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrBlockDescHead;\r
355 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;\r
356 EFI_CAPSULE_BLOCK_DESCRIPTOR *PrevBlockDescTail;\r
357 UINTN NumDescriptors;\r
358 UINTN BufferSize;\r
359 UINT8 *RelocBuffer;\r
360 UINTN BlockListSize;\r
361 //\r
362 // Get the info on the blocks and descriptors. Since we're going to move\r
363 // the descriptors low in memory, adjust the base/size values accordingly here.\r
364 // GetCapsuleInfo() returns the number of legit descriptors, so add one for\r
365 // a terminator.\r
366 //\r
367 if (GetCapsuleInfo (BlockList, &NumDescriptors, NULL) != EFI_SUCCESS) {\r
368 return NULL;\r
369 }\r
370\r
371 NumDescriptors++;\r
372 BufferSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r
373 NewBlockList = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) MemBase;\r
374 if (MemSize < BufferSize) {\r
375 return NULL;\r
376 }\r
377\r
378 MemSize -= BufferSize;\r
379 MemBase += BufferSize;\r
380 //\r
381 // Go through all the blocks and make sure none are in the way\r
382 //\r
383 TempBlockDesc = BlockList;\r
384 while (TempBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r
385 if (TempBlockDesc->Length == 0) {\r
386 //\r
387 // Next block of descriptors\r
388 //\r
389 TempBlockDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;\r
390 } else {\r
391 //\r
392 // If the capsule data pointed to by this descriptor is in the way,\r
393 // move it.\r
394 //\r
395 if (IsOverlapped (\r
396 (UINT8 *) NewBlockList,\r
397 BufferSize,\r
398 (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,\r
399 (UINTN) TempBlockDesc->Length\r
400 )) {\r
401 //\r
402 // Relocate the block\r
403 //\r
404 RelocBuffer = FindFreeMem (BlockList, MemBase, MemSize, (UINTN) TempBlockDesc->Length);\r
405 if (RelocBuffer == NULL) {\r
406 return NULL;\r
407 }\r
408\r
409 CopyMem ((VOID *) RelocBuffer, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);\r
410 TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;\r
411\r
412 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
413 }\r
414 }\r
415 TempBlockDesc++;\r
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
544 Count = 0;\r
545 Size = 0;\r
546\r
547 while (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r
548 if (Desc->Length == 0) {\r
549 //\r
550 // Descriptor points to another list of block descriptors somewhere\r
551 //\r
552 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;\r
553 } else {\r
554 Size += (UINTN) Desc->Length;\r
555 Count++;\r
556 Desc++;\r
557 }\r
558 }\r
559 //\r
560 // If no descriptors, then fail\r
561 //\r
562 if (Count == 0) {\r
563 return EFI_NOT_FOUND;\r
564 }\r
565\r
566 if (NumDescriptors != NULL) {\r
567 *NumDescriptors = Count;\r
568 }\r
569\r
570 if (CapsuleSize != NULL) {\r
571 *CapsuleSize = Size;\r
572 }\r
573\r
574 return EFI_SUCCESS;\r
575}\r
576\r
577/**\r
578 Check every capsule header.\r
579\r
580 @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER\r
581\r
582 @retval FALSE Capsule is OK\r
583 @retval TRUE Capsule is corrupted \r
584\r
585**/\r
586BOOLEAN\r
587IsCapsuleCorrupted (\r
588 IN EFI_CAPSULE_HEADER *CapsuleHeader\r
589 )\r
590{\r
591 //\r
592 //A capsule to be updated across a system reset should contain CAPSULE_FLAGS_PERSIST_ACROSS_RESET.\r
593 //\r
594 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {\r
595 return TRUE;\r
596 }\r
597 //\r
598 //Make sure the flags combination is supported by the platform.\r
599 //\r
600 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r
601 return TRUE;\r
602 }\r
603 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
604 return TRUE;\r
605 }\r
606\r
607 return FALSE;\r
608}\r
609\r
610/**\r
611 Try to verify the integrity of a capsule test pattern before the\r
612 capsule gets coalesced. This can be useful in narrowing down\r
613 where capsule data corruption occurs.\r
614\r
615 The test pattern mode fills in memory with a counting UINT32 value. \r
616 If the capsule is not divided up in a multiple of 4-byte blocks, then\r
617 things get messy doing the check. Therefore there are some cases\r
618 here where we just give up and skip the pre-coalesce check.\r
619\r
620 @param PeiServices PEI services table\r
621 @param Desc Pointer to capsule descriptors\r
622**/\r
623VOID\r
624CapsuleTestPatternPreCoalesce (\r
625 IN EFI_PEI_SERVICES **PeiServices,\r
626 IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc\r
627 )\r
628{\r
629 UINT32 *TestPtr;\r
630 UINT32 TestCounter;\r
631 UINT32 TestSize;\r
632 //\r
633 // Find first data descriptor\r
634 //\r
635 while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
636 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;\r
637 }\r
638\r
639 if (Desc->Union.ContinuationPointer == 0) {\r
640 return ;\r
641 }\r
642 //\r
643 // First one better be long enough to at least hold the test signature\r
644 //\r
645 if (Desc->Length < sizeof (UINT32)) {\r
646 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #1\n"));\r
647 return ;\r
648 }\r
649\r
650 TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;\r
651 //\r
652 // 0x54534554 "TEST"\r
653 //\r
654 if (*TestPtr != 0x54534554) {\r
655 return ;\r
656 }\r
657\r
658 TestCounter = 0;\r
659 TestSize = (UINT32) Desc->Length - 2 * sizeof (UINT32);\r
660 //\r
661 // Skip over the signature and the size fields in the pattern data header\r
662 //\r
663 TestPtr += 2;\r
664 while (1) {\r
665 if ((TestSize & 0x03) != 0) {\r
666 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #2\n"));\r
667 return ;\r
668 }\r
669\r
670 while (TestSize > 0) {\r
671 if (*TestPtr != TestCounter) {\r
672 DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce failed data corruption check\n"));\r
673 return ;\r
674 }\r
675\r
676 TestSize -= sizeof (UINT32);\r
677 TestCounter++;\r
678 TestPtr++;\r
679 }\r
680 Desc++;\r
681 while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
682 Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;\r
683 }\r
684\r
685 if (Desc->Union.ContinuationPointer == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r
686 return ;\r
687 }\r
688 TestSize = (UINT32) Desc->Length;\r
689 TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;\r
690 }\r
691}\r
692\r
693/**\r
694 Checks for the presence of capsule descriptors.\r
695 Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...\r
696\r
697 @param BlockListBuffer Pointer to the buffer of capsule descriptors variables\r
698 @param BlockDescriptorList Pointer to the capsule descriptors list\r
699\r
700 @retval EFI_SUCCESS a valid capsule is present\r
701 @retval EFI_NOT_FOUND if a valid capsule is not present\r
702**/\r
703EFI_STATUS\r
704BuildCapsuleDescriptors (\r
705 IN EFI_PHYSICAL_ADDRESS *BlockListBuffer,\r
706 OUT EFI_CAPSULE_BLOCK_DESCRIPTOR **BlockDescriptorList \r
707 )\r
708{\r
709 UINTN Index;\r
710 EFI_CAPSULE_BLOCK_DESCRIPTOR *LastBlock;\r
711 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlock;\r
712 EFI_CAPSULE_BLOCK_DESCRIPTOR *HeadBlock;\r
713\r
714 LastBlock = NULL;\r
715 HeadBlock = NULL;\r
716 TempBlock = NULL;\r
717 Index = 0;\r
718\r
719 while (BlockListBuffer[Index] != 0) {\r
720 if (Index == 0) {\r
721 //\r
722 // For the first Capsule Image, test integrity of descriptors.\r
723 //\r
724 LastBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index]);\r
725 if (LastBlock == NULL) {\r
726 return EFI_NOT_FOUND;\r
727 }\r
728 //\r
729 // Return the base of the block descriptors\r
730 //\r
731 HeadBlock = (EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index];\r
732 } else { \r
733 //\r
734 // Test integrity of descriptors.\r
735 //\r
736 TempBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index]);\r
737 if (TempBlock == NULL) {\r
738 return EFI_NOT_FOUND;\r
739 }\r
740 //\r
741 // Combine the different BlockList into single BlockList.\r
742 //\r
743 LastBlock->Union.DataBlock = (EFI_PHYSICAL_ADDRESS)(UINTN)BlockListBuffer[Index];\r
744 LastBlock->Length = 0;\r
745 LastBlock = TempBlock;\r
746 }\r
747 Index ++;\r
748 }\r
749 \r
750 if (HeadBlock != NULL) {\r
751 *BlockDescriptorList = HeadBlock;\r
752 return EFI_SUCCESS;\r
753 }\r
754 return EFI_NOT_FOUND;\r
755}\r
756\r
757/**\r
758 The function to coalesce a fragmented capsule in memory.\r
759\r
760 Memory Map for coalesced capsule:\r
761 MemBase + ---->+---------------------------+<-----------+\r
762 MemSize | CapsuleOffset[49] | |\r
763 +---------------------------+ |\r
764 | ................ | |\r
765 +---------------------------+ |\r
766 | CapsuleOffset[2] | |\r
767 +---------------------------+ |\r
768 | CapsuleOffset[1] | |\r
769 +---------------------------+ |\r
770 | CapsuleOffset[0] | CapsuleSize \r
771 +---------------------------+ |\r
772 | CapsuleNumber | |\r
773 +---------------------------+ |\r
774 | | | \r
775 | | | \r
776 | Capsule Image | | \r
777 | | | \r
778 | | | \r
779 +---------------------------+ |\r
780 | PrivateData | |\r
781 DestPtr ----> +---------------------------+<-----------+\r
782 | | |\r
783 | FreeMem | FreeMemSize\r
784 | | |\r
785 FreeMemBase --->+---------------------------+<-----------+\r
786 | Terminator |\r
787 +---------------------------+\r
788 | BlockDescriptor n |\r
789 +---------------------------+\r
790 | ................. |\r
791 +---------------------------+\r
792 | BlockDescriptor 1 |\r
793 +---------------------------+\r
794 | BlockDescriptor 0 |\r
795 +---------------------------+\r
796 | PrivateDataDesc 0 |\r
797 MemBase ---->+---------------------------+<----- BlockList\r
798\r
799 @param PeiServices General purpose services available to every PEIM.\r
800 @param BlockListBuffer Point to the buffer of Capsule Descriptor Variables.\r
801 @param MemoryBase Pointer to the base of a block of memory that we can walk\r
802 all over while trying to coalesce our buffers.\r
803 On output, this variable will hold the base address of\r
804 a coalesced capsule.\r
805 @param MemorySize Size of the memory region pointed to by MemoryBase.\r
806 On output, this variable will contain the size of the\r
807 coalesced capsule.\r
808\r
809 @retval EFI_NOT_FOUND If we could not find the capsule descriptors.\r
810\r
811 @retval EFI_BUFFER_TOO_SMALL\r
812 If we could not coalesce the capsule in the memory\r
813 region provided to us.\r
814\r
815 @retval EFI_SUCCESS Processed the capsule successfully.\r
816**/\r
817EFI_STATUS\r
818EFIAPI\r
819CapsuleDataCoalesce (\r
820 IN EFI_PEI_SERVICES **PeiServices,\r
821 IN EFI_PHYSICAL_ADDRESS *BlockListBuffer,\r
822 IN OUT VOID **MemoryBase,\r
823 IN OUT UINTN *MemorySize\r
824 )\r
825{\r
826 VOID *NewCapsuleBase;\r
827 VOID *DataPtr;\r
828 UINT8 CapsuleIndex;\r
829 UINT8 *FreeMemBase;\r
830 UINT8 *DestPtr;\r
831 UINT8 *RelocPtr;\r
832 UINT32 CapsuleOffset[MAX_SUPPORT_CAPSULE_NUM]; \r
833 UINT32 *AddDataPtr;\r
834 UINT32 CapsuleTimes; \r
835 UINT64 SizeLeft; \r
836 UINT64 CapsuleImageSize; \r
837 UINTN CapsuleSize;\r
838 UINTN DescriptorsSize;\r
839 UINTN FreeMemSize;\r
840 UINTN NumDescriptors;\r
841 BOOLEAN IsCorrupted;\r
842 BOOLEAN CapsuleBeginFlag;\r
843 EFI_STATUS Status;\r
844 EFI_CAPSULE_HEADER *CapsuleHeader;\r
845 EFI_CAPSULE_PEIM_PRIVATE_DATA PrivateData;\r
846 EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateDataPtr;\r
847 EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList;\r
848 EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrentBlockDesc;\r
849 EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;\r
850 EFI_CAPSULE_BLOCK_DESCRIPTOR PrivateDataDesc[2];\r
851\r
852 CapsuleIndex = 0;\r
853 SizeLeft = 0;\r
854 CapsuleTimes = 0;\r
855 CapsuleImageSize = 0;\r
856 PrivateDataPtr = NULL;\r
857 AddDataPtr = NULL;\r
858 CapsuleHeader = NULL;\r
859 CapsuleBeginFlag = TRUE;\r
860 IsCorrupted = TRUE;\r
861 CapsuleSize = 0;\r
862 NumDescriptors = 0;\r
863 \r
864 //\r
865 // Build capsule descriptors list\r
866 //\r
867 Status = BuildCapsuleDescriptors (BlockListBuffer, &BlockList);\r
868 if (EFI_ERROR (Status)) {\r
869 return Status;\r
870 }\r
871\r
872 DEBUG_CODE (\r
873 CapsuleTestPatternPreCoalesce (PeiServices, BlockList);\r
874 );\r
875\r
876 //\r
877 // Get the size of our descriptors and the capsule size. GetCapsuleInfo()\r
878 // returns the number of descriptors that actually point to data, so add\r
879 // one for a terminator. Do that below.\r
880 //\r
881 GetCapsuleInfo (BlockList, &NumDescriptors, &CapsuleSize);\r
882 if ((CapsuleSize == 0) || (NumDescriptors == 0)) {\r
883 return EFI_NOT_FOUND;\r
884 }\r
885\r
886 //\r
887 // Initialize our local copy of private data. When we're done, we'll create a\r
888 // descriptor for it as well so that it can be put into free memory without\r
889 // trashing anything.\r
890 //\r
891 PrivateData.Signature = EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE;\r
892 PrivateData.CapsuleSize = (UINT32) CapsuleSize;\r
893 PrivateDataDesc[0].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) &PrivateData;\r
894 PrivateDataDesc[0].Length = sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA);\r
895 PrivateDataDesc[1].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) BlockList;\r
896 PrivateDataDesc[1].Length = 0;\r
897 //\r
898 // In addition to PrivateDataDesc[1:0], one terminator is added\r
899 // See below RelocateBlockDescriptors()\r
900 //\r
901 NumDescriptors += 3;\r
902 CapsuleSize += sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + sizeof(CapsuleOffset) + sizeof(UINT32);\r
903 BlockList = PrivateDataDesc;\r
904 DescriptorsSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r
905\r
906 //\r
907 // Don't go below some min address. If the base is below it,\r
908 // then move it up and adjust the size accordingly.\r
909 //\r
910 DEBUG ((EFI_D_INFO, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN) *MemoryBase, (UINTN)*MemoryBase + *MemorySize));\r
911 if ((UINTN)*MemoryBase < (UINTN) MIN_COALESCE_ADDR) {\r
912 if (((UINTN)*MemoryBase + *MemorySize) < (UINTN) MIN_COALESCE_ADDR) {\r
913 return EFI_BUFFER_TOO_SMALL;\r
914 } else {\r
915 *MemorySize = *MemorySize - ((UINTN) MIN_COALESCE_ADDR - (UINTN) *MemoryBase);\r
916 *MemoryBase = (VOID *) (UINTN) MIN_COALESCE_ADDR;\r
917 }\r
918 }\r
919\r
920 if (*MemorySize <= (CapsuleSize + DescriptorsSize)) {\r
921 return EFI_BUFFER_TOO_SMALL;\r
922 }\r
923\r
924 FreeMemBase = *MemoryBase;\r
925 FreeMemSize = *MemorySize;\r
926 DEBUG ((EFI_D_INFO, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN) FreeMemBase, (UINTN) FreeMemBase + FreeMemSize));\r
927\r
928 //\r
929 // Relocate all the block descriptors to low memory to make further\r
930 // processing easier.\r
931 //\r
932 BlockList = RelocateBlockDescriptors (PeiServices, BlockList, FreeMemBase, FreeMemSize);\r
933 if (BlockList == NULL) {\r
934 //\r
935 // Not enough room to relocate the descriptors\r
936 //\r
937 return EFI_BUFFER_TOO_SMALL;\r
938 }\r
939\r
940 //\r
941 // Take the top of memory for the capsule. Naturally align.\r
942 //\r
943 DestPtr = FreeMemBase + FreeMemSize - CapsuleSize;\r
944 DestPtr = (UINT8 *) ((UINTN) DestPtr &~ (UINTN) (sizeof (UINTN) - 1));\r
945 FreeMemBase = (UINT8 *) BlockList + DescriptorsSize;\r
946 FreeMemSize = FreeMemSize - DescriptorsSize - CapsuleSize;\r
947 NewCapsuleBase = (VOID *) DestPtr;\r
948\r
949 //\r
950 // Move all the blocks to the top (high) of memory.\r
951 // Relocate all the obstructing blocks. Note that the block descriptors\r
952 // were coalesced when they were relocated, so we can just ++ the pointer.\r
953 //\r
954 CurrentBlockDesc = BlockList;\r
955 while ((CurrentBlockDesc->Length != 0) || (CurrentBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
956 //\r
957 // See if any of the remaining capsule blocks are in the way\r
958 //\r
959 TempBlockDesc = CurrentBlockDesc;\r
960 while (TempBlockDesc->Length != 0) {\r
961 //\r
962 // Is this block in the way of where we want to copy the current descriptor to?\r
963 //\r
964 if (IsOverlapped (\r
965 (UINT8 *) DestPtr,\r
966 (UINTN) CurrentBlockDesc->Length,\r
967 (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,\r
968 (UINTN) TempBlockDesc->Length\r
969 )) {\r
970 //\r
971 // Relocate the block\r
972 //\r
973 RelocPtr = FindFreeMem (BlockList, FreeMemBase, FreeMemSize, (UINTN) TempBlockDesc->Length);\r
974 if (RelocPtr == NULL) {\r
975 return EFI_BUFFER_TOO_SMALL;\r
976 }\r
977\r
978 CopyMem ((VOID *) RelocPtr, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);\r
979 DEBUG ((EFI_D_INFO, "Capsule reloc data block from 0x%8X to 0x%8X with size 0x%8X\n",\r
980 (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) RelocPtr, (UINTN) TempBlockDesc->Length));\r
981\r
982 TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocPtr;\r
983 }\r
984 //\r
985 // Next descriptor\r
986 //\r
987 TempBlockDesc++;\r
988 }\r
989 //\r
990 // Ok, we made it through. Copy the block.\r
991 // we just support greping one capsule from the lists of block descs list.\r
992 //\r
993 CapsuleTimes ++;\r
994 //\r
995 //Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA\r
996 //\r
997 if (CapsuleTimes > 1) {\r
998 //\r
999 //For every capsule entry point, check its header to determine whether to relocate it.\r
1000 //If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.\r
1001 //\r
1002 if (CapsuleBeginFlag) {\r
1003 CapsuleBeginFlag = FALSE;\r
1004 CapsuleHeader = (EFI_CAPSULE_HEADER*)(UINTN)CurrentBlockDesc->Union.DataBlock;\r
1005 SizeLeft = CapsuleHeader->CapsuleImageSize;\r
1006 if (!IsCapsuleCorrupted (CapsuleHeader)) {\r
1007\r
1008 if (CapsuleIndex > (MAX_SUPPORT_CAPSULE_NUM - 1)) {\r
1009 DEBUG ((EFI_D_ERROR, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM));\r
1010 return EFI_BUFFER_TOO_SMALL;\r
1011 }\r
1012\r
1013 //\r
1014 // Relocate this valid capsule\r
1015 //\r
1016 IsCorrupted = FALSE;\r
1017 CapsuleImageSize += SizeLeft;\r
1018 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);\r
1019 DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r
1020 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r
1021 //\r
1022 // Cache the begin offset of this capsule\r
1023 //\r
1024 CapsuleOffset[CapsuleIndex++] = (UINT32) (UINTN) DestPtr - (UINT32)(UINTN)NewCapsuleBase - (UINT32)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA);\r
1025 DestPtr += CurrentBlockDesc->Length;\r
1026 }\r
1027 //\r
1028 // If the current block length is greater than or equal to SizeLeft, this is the \r
1029 // start of the next capsule\r
1030 //\r
1031 if (CurrentBlockDesc->Length < SizeLeft) {\r
1032 SizeLeft -= CurrentBlockDesc->Length;\r
1033 } else {\r
1034 //\r
1035 // Start the next cycle\r
1036 //\r
1037 SizeLeft = 0;\r
1038 IsCorrupted = TRUE;\r
1039 CapsuleBeginFlag = TRUE; \r
1040 }\r
1041 } else {\r
1042 //\r
1043 //Go on relocating the current capule image.\r
1044 //\r
1045 if (CurrentBlockDesc->Length < SizeLeft) {\r
1046 if (!IsCorrupted) {\r
1047 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) (CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);\r
1048 DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r
1049 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r
1050 DestPtr += CurrentBlockDesc->Length;\r
1051 }\r
1052 SizeLeft -= CurrentBlockDesc->Length;\r
1053 } else {\r
1054 //\r
1055 //Here is the end of the current capsule image.\r
1056 //\r
1057 if (!IsCorrupted) {\r
1058 CopyMem ((VOID *) DestPtr, (VOID *)(UINTN)(CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);\r
1059 DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r
1060 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r
1061 DestPtr += CurrentBlockDesc->Length;\r
1062 }\r
1063 //\r
1064 // Start the next cycle\r
1065 //\r
1066 SizeLeft = 0;\r
1067 IsCorrupted = TRUE;\r
1068 CapsuleBeginFlag = TRUE; \r
1069 }\r
1070 }\r
1071 } else {\r
1072 //\r
1073 //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.\r
1074 //\r
1075 CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);\r
1076 DestPtr += CurrentBlockDesc->Length;\r
1077 }\r
1078 //\r
1079 //Walk through the block descriptor list.\r
1080 //\r
1081 CurrentBlockDesc++;\r
1082 }\r
1083 //\r
1084 // We return the base of memory we want reserved, and the size.\r
1085 // The memory peim should handle it appropriately from there.\r
1086 //\r
1087 *MemorySize = (UINTN) CapsuleImageSize;\r
1088 *MemoryBase = (VOID *) NewCapsuleBase;\r
1089\r
1090 //\r
1091 //Append the offsets of mutiply capsules to the continous buffer\r
1092 //\r
1093 DataPtr = (VOID*)((UINTN)NewCapsuleBase + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (UINTN)CapsuleImageSize);\r
1094 AddDataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) &~ (UINT32) (sizeof (UINT32) - 1));\r
1095\r
1096 *AddDataPtr++ = CapsuleIndex;\r
1097\r
1098 CopyMem (AddDataPtr, &CapsuleOffset[0], sizeof (UINT32) * CapsuleIndex);\r
1099\r
1100 PrivateDataPtr = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) NewCapsuleBase;\r
1101 PrivateDataPtr->CapsuleSize = (UINT32) CapsuleImageSize;\r
1102\r
1103 return EFI_SUCCESS;\r
1104}\r