]>
Commit | Line | Data |
---|---|---|
ab7017fe | 1 | /** @file\r |
2 | The logic to process capsule.\r | |
3 | \r | |
4 | Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r | |
5 | This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT 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 | |
30 | typedef 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 | |
51 | UINT8 *\r | |
52 | FindFreeMem (\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 | |
68 | EFI_CAPSULE_BLOCK_DESCRIPTOR *\r | |
69 | ValidateCapsuleIntegrity (\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 | |
91 | EFI_CAPSULE_BLOCK_DESCRIPTOR *\r | |
92 | RelocateBlockDescriptors (\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 | |
108 | BOOLEAN\r | |
109 | IsCapsuleCorrupted (\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 | |
125 | BOOLEAN\r | |
126 | IsOverlapped (\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 | |
148 | EFI_STATUS\r | |
149 | GetCapsuleInfo (\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 | |
171 | UINT8 *\r | |
172 | FindFreeMem (\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 | |
254 | EFI_CAPSULE_BLOCK_DESCRIPTOR *\r | |
255 | ValidateCapsuleIntegrity (\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 | |
345 | EFI_CAPSULE_BLOCK_DESCRIPTOR *\r | |
346 | RelocateBlockDescriptors (\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 | |
500 | BOOLEAN\r | |
501 | IsOverlapped (\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 | |
534 | EFI_STATUS\r | |
535 | GetCapsuleInfo (\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 | |
588 | BOOLEAN\r | |
589 | IsCapsuleCorrupted (\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 | |
625 | VOID\r | |
626 | CapsuleTestPatternPreCoalesce (\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 | |
705 | EFI_STATUS\r | |
706 | BuildCapsuleDescriptors (\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 | |
722 | if (Index == 0) {\r | |
723 | //\r | |
724 | // For the first Capsule Image, test integrity of descriptors.\r | |
725 | //\r | |
726 | LastBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index]);\r | |
727 | if (LastBlock == NULL) {\r | |
728 | return EFI_NOT_FOUND;\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 | // Test integrity of descriptors.\r | |
737 | //\r | |
738 | TempBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index]);\r | |
739 | if (TempBlock == NULL) {\r | |
740 | return EFI_NOT_FOUND;\r | |
741 | }\r | |
742 | //\r | |
743 | // Combine the different BlockList into single BlockList.\r | |
744 | //\r | |
745 | LastBlock->Union.DataBlock = (EFI_PHYSICAL_ADDRESS)(UINTN)BlockListBuffer[Index];\r | |
746 | LastBlock->Length = 0;\r | |
747 | LastBlock = TempBlock;\r | |
748 | }\r | |
749 | Index ++;\r | |
750 | }\r | |
751 | \r | |
752 | if (HeadBlock != NULL) {\r | |
753 | *BlockDescriptorList = HeadBlock;\r | |
754 | return EFI_SUCCESS;\r | |
755 | }\r | |
756 | return EFI_NOT_FOUND;\r | |
757 | }\r | |
758 | \r | |
759 | /**\r | |
760 | The function to coalesce a fragmented capsule in memory.\r | |
761 | \r | |
762 | Memory Map for coalesced capsule:\r | |
763 | MemBase + ---->+---------------------------+<-----------+\r | |
764 | MemSize | CapsuleOffset[49] | |\r | |
765 | +---------------------------+ |\r | |
766 | | ................ | |\r | |
767 | +---------------------------+ |\r | |
768 | | CapsuleOffset[2] | |\r | |
769 | +---------------------------+ |\r | |
770 | | CapsuleOffset[1] | |\r | |
771 | +---------------------------+ |\r | |
772 | | CapsuleOffset[0] | CapsuleSize \r | |
773 | +---------------------------+ |\r | |
774 | | CapsuleNumber | |\r | |
775 | +---------------------------+ |\r | |
776 | | | | \r | |
777 | | | | \r | |
778 | | Capsule Image | | \r | |
779 | | | | \r | |
780 | | | | \r | |
781 | +---------------------------+ |\r | |
782 | | PrivateData | |\r | |
783 | DestPtr ----> +---------------------------+<-----------+\r | |
784 | | | |\r | |
785 | | FreeMem | FreeMemSize\r | |
786 | | | |\r | |
787 | FreeMemBase --->+---------------------------+<-----------+\r | |
788 | | Terminator |\r | |
789 | +---------------------------+\r | |
790 | | BlockDescriptor n |\r | |
791 | +---------------------------+\r | |
792 | | ................. |\r | |
793 | +---------------------------+\r | |
794 | | BlockDescriptor 1 |\r | |
795 | +---------------------------+\r | |
796 | | BlockDescriptor 0 |\r | |
797 | +---------------------------+\r | |
798 | | PrivateDataDesc 0 |\r | |
799 | MemBase ---->+---------------------------+<----- BlockList\r | |
800 | \r | |
801 | @param PeiServices General purpose services available to every PEIM.\r | |
802 | @param BlockListBuffer Point to the buffer of Capsule Descriptor Variables.\r | |
803 | @param MemoryBase Pointer to the base of a block of memory that we can walk\r | |
804 | all over while trying to coalesce our buffers.\r | |
805 | On output, this variable will hold the base address of\r | |
806 | a coalesced capsule.\r | |
807 | @param MemorySize Size of the memory region pointed to by MemoryBase.\r | |
808 | On output, this variable will contain the size of the\r | |
809 | coalesced capsule.\r | |
810 | \r | |
811 | @retval EFI_NOT_FOUND If we could not find the capsule descriptors.\r | |
812 | \r | |
813 | @retval EFI_BUFFER_TOO_SMALL\r | |
814 | If we could not coalesce the capsule in the memory\r | |
815 | region provided to us.\r | |
816 | \r | |
817 | @retval EFI_SUCCESS Processed the capsule successfully.\r | |
818 | **/\r | |
819 | EFI_STATUS\r | |
820 | EFIAPI\r | |
821 | CapsuleDataCoalesce (\r | |
822 | IN EFI_PEI_SERVICES **PeiServices,\r | |
823 | IN EFI_PHYSICAL_ADDRESS *BlockListBuffer,\r | |
824 | IN OUT VOID **MemoryBase,\r | |
825 | IN OUT UINTN *MemorySize\r | |
826 | )\r | |
827 | {\r | |
828 | VOID *NewCapsuleBase;\r | |
829 | VOID *DataPtr;\r | |
830 | UINT8 CapsuleIndex;\r | |
831 | UINT8 *FreeMemBase;\r | |
832 | UINT8 *DestPtr;\r | |
833 | UINT8 *RelocPtr;\r | |
834 | UINT32 CapsuleOffset[MAX_SUPPORT_CAPSULE_NUM]; \r | |
835 | UINT32 *AddDataPtr;\r | |
836 | UINT32 CapsuleTimes; \r | |
837 | UINT64 SizeLeft; \r | |
838 | UINT64 CapsuleImageSize; \r | |
839 | UINTN CapsuleSize;\r | |
840 | UINTN DescriptorsSize;\r | |
841 | UINTN FreeMemSize;\r | |
842 | UINTN NumDescriptors;\r | |
843 | BOOLEAN IsCorrupted;\r | |
844 | BOOLEAN CapsuleBeginFlag;\r | |
845 | EFI_STATUS Status;\r | |
846 | EFI_CAPSULE_HEADER *CapsuleHeader;\r | |
847 | EFI_CAPSULE_PEIM_PRIVATE_DATA PrivateData;\r | |
848 | EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateDataPtr;\r | |
849 | EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList;\r | |
850 | EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrentBlockDesc;\r | |
851 | EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;\r | |
852 | EFI_CAPSULE_BLOCK_DESCRIPTOR PrivateDataDesc[2];\r | |
853 | \r | |
854 | CapsuleIndex = 0;\r | |
855 | SizeLeft = 0;\r | |
856 | CapsuleTimes = 0;\r | |
857 | CapsuleImageSize = 0;\r | |
858 | PrivateDataPtr = NULL;\r | |
859 | AddDataPtr = NULL;\r | |
860 | CapsuleHeader = NULL;\r | |
861 | CapsuleBeginFlag = TRUE;\r | |
862 | IsCorrupted = TRUE;\r | |
863 | CapsuleSize = 0;\r | |
864 | NumDescriptors = 0;\r | |
865 | \r | |
866 | //\r | |
867 | // Build capsule descriptors list\r | |
868 | //\r | |
869 | Status = BuildCapsuleDescriptors (BlockListBuffer, &BlockList);\r | |
870 | if (EFI_ERROR (Status)) {\r | |
871 | return Status;\r | |
872 | }\r | |
873 | \r | |
874 | DEBUG_CODE (\r | |
875 | CapsuleTestPatternPreCoalesce (PeiServices, BlockList);\r | |
876 | );\r | |
877 | \r | |
878 | //\r | |
879 | // Get the size of our descriptors and the capsule size. GetCapsuleInfo()\r | |
880 | // returns the number of descriptors that actually point to data, so add\r | |
881 | // one for a terminator. Do that below.\r | |
882 | //\r | |
883 | GetCapsuleInfo (BlockList, &NumDescriptors, &CapsuleSize);\r | |
884 | if ((CapsuleSize == 0) || (NumDescriptors == 0)) {\r | |
885 | return EFI_NOT_FOUND;\r | |
886 | }\r | |
887 | \r | |
888 | //\r | |
889 | // Initialize our local copy of private data. When we're done, we'll create a\r | |
890 | // descriptor for it as well so that it can be put into free memory without\r | |
891 | // trashing anything.\r | |
892 | //\r | |
893 | PrivateData.Signature = EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE;\r | |
894 | PrivateData.CapsuleSize = (UINT32) CapsuleSize;\r | |
895 | PrivateDataDesc[0].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) &PrivateData;\r | |
896 | PrivateDataDesc[0].Length = sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA);\r | |
897 | PrivateDataDesc[1].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) BlockList;\r | |
898 | PrivateDataDesc[1].Length = 0;\r | |
899 | //\r | |
900 | // In addition to PrivateDataDesc[1:0], one terminator is added\r | |
901 | // See below RelocateBlockDescriptors()\r | |
902 | //\r | |
903 | NumDescriptors += 3;\r | |
904 | CapsuleSize += sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + sizeof(CapsuleOffset) + sizeof(UINT32);\r | |
905 | BlockList = PrivateDataDesc;\r | |
906 | DescriptorsSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r | |
907 | \r | |
908 | //\r | |
909 | // Don't go below some min address. If the base is below it,\r | |
910 | // then move it up and adjust the size accordingly.\r | |
911 | //\r | |
912 | DEBUG ((EFI_D_INFO, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN) *MemoryBase, (UINTN)*MemoryBase + *MemorySize));\r | |
913 | if ((UINTN)*MemoryBase < (UINTN) MIN_COALESCE_ADDR) {\r | |
914 | if (((UINTN)*MemoryBase + *MemorySize) < (UINTN) MIN_COALESCE_ADDR) {\r | |
915 | return EFI_BUFFER_TOO_SMALL;\r | |
916 | } else {\r | |
917 | *MemorySize = *MemorySize - ((UINTN) MIN_COALESCE_ADDR - (UINTN) *MemoryBase);\r | |
918 | *MemoryBase = (VOID *) (UINTN) MIN_COALESCE_ADDR;\r | |
919 | }\r | |
920 | }\r | |
921 | \r | |
922 | if (*MemorySize <= (CapsuleSize + DescriptorsSize)) {\r | |
923 | return EFI_BUFFER_TOO_SMALL;\r | |
924 | }\r | |
925 | \r | |
926 | FreeMemBase = *MemoryBase;\r | |
927 | FreeMemSize = *MemorySize;\r | |
928 | DEBUG ((EFI_D_INFO, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN) FreeMemBase, (UINTN) FreeMemBase + FreeMemSize));\r | |
929 | \r | |
930 | //\r | |
931 | // Relocate all the block descriptors to low memory to make further\r | |
932 | // processing easier.\r | |
933 | //\r | |
934 | BlockList = RelocateBlockDescriptors (PeiServices, BlockList, FreeMemBase, FreeMemSize);\r | |
935 | if (BlockList == NULL) {\r | |
936 | //\r | |
937 | // Not enough room to relocate the descriptors\r | |
938 | //\r | |
939 | return EFI_BUFFER_TOO_SMALL;\r | |
940 | }\r | |
941 | \r | |
942 | //\r | |
943 | // Take the top of memory for the capsule. Naturally align.\r | |
944 | //\r | |
945 | DestPtr = FreeMemBase + FreeMemSize - CapsuleSize;\r | |
946 | DestPtr = (UINT8 *) ((UINTN) DestPtr &~ (UINTN) (sizeof (UINTN) - 1));\r | |
947 | FreeMemBase = (UINT8 *) BlockList + DescriptorsSize;\r | |
948 | FreeMemSize = FreeMemSize - DescriptorsSize - CapsuleSize;\r | |
949 | NewCapsuleBase = (VOID *) DestPtr;\r | |
950 | \r | |
951 | //\r | |
952 | // Move all the blocks to the top (high) of memory.\r | |
953 | // Relocate all the obstructing blocks. Note that the block descriptors\r | |
954 | // were coalesced when they were relocated, so we can just ++ the pointer.\r | |
955 | //\r | |
956 | CurrentBlockDesc = BlockList;\r | |
957 | while ((CurrentBlockDesc->Length != 0) || (CurrentBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r | |
958 | //\r | |
959 | // See if any of the remaining capsule blocks are in the way\r | |
960 | //\r | |
961 | TempBlockDesc = CurrentBlockDesc;\r | |
962 | while (TempBlockDesc->Length != 0) {\r | |
963 | //\r | |
964 | // Is this block in the way of where we want to copy the current descriptor to?\r | |
965 | //\r | |
966 | if (IsOverlapped (\r | |
967 | (UINT8 *) DestPtr,\r | |
968 | (UINTN) CurrentBlockDesc->Length,\r | |
969 | (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,\r | |
970 | (UINTN) TempBlockDesc->Length\r | |
971 | )) {\r | |
972 | //\r | |
973 | // Relocate the block\r | |
974 | //\r | |
975 | RelocPtr = FindFreeMem (BlockList, FreeMemBase, FreeMemSize, (UINTN) TempBlockDesc->Length);\r | |
976 | if (RelocPtr == NULL) {\r | |
977 | return EFI_BUFFER_TOO_SMALL;\r | |
978 | }\r | |
979 | \r | |
980 | CopyMem ((VOID *) RelocPtr, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);\r | |
981 | DEBUG ((EFI_D_INFO, "Capsule reloc data block from 0x%8X to 0x%8X with size 0x%8X\n",\r | |
982 | (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) RelocPtr, (UINTN) TempBlockDesc->Length));\r | |
983 | \r | |
984 | TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocPtr;\r | |
985 | }\r | |
986 | //\r | |
987 | // Next descriptor\r | |
988 | //\r | |
989 | TempBlockDesc++;\r | |
990 | }\r | |
991 | //\r | |
992 | // Ok, we made it through. Copy the block.\r | |
993 | // we just support greping one capsule from the lists of block descs list.\r | |
994 | //\r | |
995 | CapsuleTimes ++;\r | |
996 | //\r | |
997 | //Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA\r | |
998 | //\r | |
999 | if (CapsuleTimes > 1) {\r | |
1000 | //\r | |
1001 | //For every capsule entry point, check its header to determine whether to relocate it.\r | |
1002 | //If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.\r | |
1003 | //\r | |
1004 | if (CapsuleBeginFlag) {\r | |
1005 | CapsuleBeginFlag = FALSE;\r | |
1006 | CapsuleHeader = (EFI_CAPSULE_HEADER*)(UINTN)CurrentBlockDesc->Union.DataBlock;\r | |
1007 | SizeLeft = CapsuleHeader->CapsuleImageSize;\r | |
1008 | if (!IsCapsuleCorrupted (CapsuleHeader)) {\r | |
1009 | \r | |
1010 | if (CapsuleIndex > (MAX_SUPPORT_CAPSULE_NUM - 1)) {\r | |
1011 | DEBUG ((EFI_D_ERROR, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM));\r | |
1012 | return EFI_BUFFER_TOO_SMALL;\r | |
1013 | }\r | |
1014 | \r | |
1015 | //\r | |
1016 | // Relocate this valid capsule\r | |
1017 | //\r | |
1018 | IsCorrupted = FALSE;\r | |
1019 | CapsuleImageSize += SizeLeft;\r | |
1020 | CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);\r | |
1021 | DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r | |
1022 | (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r | |
1023 | //\r | |
1024 | // Cache the begin offset of this capsule\r | |
1025 | //\r | |
1026 | CapsuleOffset[CapsuleIndex++] = (UINT32) (UINTN) DestPtr - (UINT32)(UINTN)NewCapsuleBase - (UINT32)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA);\r | |
1027 | DestPtr += CurrentBlockDesc->Length;\r | |
1028 | }\r | |
1029 | //\r | |
1030 | // If the current block length is greater than or equal to SizeLeft, this is the \r | |
1031 | // start of the next capsule\r | |
1032 | //\r | |
1033 | if (CurrentBlockDesc->Length < SizeLeft) {\r | |
1034 | SizeLeft -= CurrentBlockDesc->Length;\r | |
1035 | } else {\r | |
1036 | //\r | |
1037 | // Start the next cycle\r | |
1038 | //\r | |
1039 | SizeLeft = 0;\r | |
1040 | IsCorrupted = TRUE;\r | |
1041 | CapsuleBeginFlag = TRUE; \r | |
1042 | }\r | |
1043 | } else {\r | |
1044 | //\r | |
1045 | //Go on relocating the current capule image.\r | |
1046 | //\r | |
1047 | if (CurrentBlockDesc->Length < SizeLeft) {\r | |
1048 | if (!IsCorrupted) {\r | |
1049 | CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) (CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);\r | |
1050 | DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r | |
1051 | (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r | |
1052 | DestPtr += CurrentBlockDesc->Length;\r | |
1053 | }\r | |
1054 | SizeLeft -= CurrentBlockDesc->Length;\r | |
1055 | } else {\r | |
1056 | //\r | |
1057 | //Here is the end of the current capsule image.\r | |
1058 | //\r | |
1059 | if (!IsCorrupted) {\r | |
1060 | CopyMem ((VOID *) DestPtr, (VOID *)(UINTN)(CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);\r | |
1061 | DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r | |
1062 | (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r | |
1063 | DestPtr += CurrentBlockDesc->Length;\r | |
1064 | }\r | |
1065 | //\r | |
1066 | // Start the next cycle\r | |
1067 | //\r | |
1068 | SizeLeft = 0;\r | |
1069 | IsCorrupted = TRUE;\r | |
1070 | CapsuleBeginFlag = TRUE; \r | |
1071 | }\r | |
1072 | }\r | |
1073 | } else {\r | |
1074 | //\r | |
1075 | //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.\r | |
1076 | //\r | |
1077 | CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);\r | |
1078 | DestPtr += CurrentBlockDesc->Length;\r | |
1079 | }\r | |
1080 | //\r | |
1081 | //Walk through the block descriptor list.\r | |
1082 | //\r | |
1083 | CurrentBlockDesc++;\r | |
1084 | }\r | |
1085 | //\r | |
1086 | // We return the base of memory we want reserved, and the size.\r | |
1087 | // The memory peim should handle it appropriately from there.\r | |
1088 | //\r | |
1089 | *MemorySize = (UINTN) CapsuleImageSize;\r | |
1090 | *MemoryBase = (VOID *) NewCapsuleBase;\r | |
1091 | \r | |
1092 | //\r | |
1093 | //Append the offsets of mutiply capsules to the continous buffer\r | |
1094 | //\r | |
1095 | DataPtr = (VOID*)((UINTN)NewCapsuleBase + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (UINTN)CapsuleImageSize);\r | |
1096 | AddDataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) &~ (UINT32) (sizeof (UINT32) - 1));\r | |
1097 | \r | |
1098 | *AddDataPtr++ = CapsuleIndex;\r | |
1099 | \r | |
1100 | CopyMem (AddDataPtr, &CapsuleOffset[0], sizeof (UINT32) * CapsuleIndex);\r | |
1101 | \r | |
1102 | PrivateDataPtr = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) NewCapsuleBase;\r | |
1103 | PrivateDataPtr->CapsuleSize = (UINT32) CapsuleImageSize;\r | |
1104 | \r | |
1105 | return EFI_SUCCESS;\r | |
1106 | }\r |