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