]>
Commit | Line | Data |
---|---|---|
da58b0db | 1 | /** @file\r |
2 | Capsule update PEIM for UEFI2.0\r | |
3 | \r | |
4 | Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r | |
5 | \r | |
6 | This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions\r | |
8 | of the BSD License which accompanies this distribution. The\r | |
9 | full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include "Capsule.h"\r | |
18 | \r | |
19 | EFI_PHYSICAL_ADDRESS *mBufferAddress;\r | |
20 | \r | |
21 | /**\r | |
22 | Check every capsule header.\r | |
23 | \r | |
24 | @param CapsuleHeader The pointer to EFI_CAPSULE_HEADER\r | |
25 | \r | |
26 | @retval FALSE Capsule is OK\r | |
27 | @retval TRUE Capsule is corrupted \r | |
28 | \r | |
29 | **/\r | |
30 | BOOLEAN\r | |
31 | IsCapsuleCorrupted (\r | |
32 | IN EFI_CAPSULE_HEADER *CapsuleHeader\r | |
33 | )\r | |
34 | {\r | |
35 | //\r | |
36 | //A capsule to be updated across a system reset should contain CAPSULE_FLAGS_PERSIST_ACROSS_RESET.\r | |
37 | //\r | |
38 | if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {\r | |
39 | return TRUE;\r | |
40 | }\r | |
41 | //\r | |
42 | //Make sure the flags combination is supported by the platform.\r | |
43 | //\r | |
44 | if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r | |
45 | return TRUE;\r | |
46 | }\r | |
47 | if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r | |
48 | return TRUE;\r | |
49 | }\r | |
50 | \r | |
51 | return FALSE;\r | |
52 | }\r | |
53 | \r | |
54 | /**\r | |
55 | Check the integrity of the capsule descriptors.\r | |
56 | \r | |
57 | @param BlockList Pointer to the capsule descriptors\r | |
58 | \r | |
59 | @retval NULL BlockList is not valid.\r | |
60 | @retval LastBlockDesc Last one Block in BlockList\r | |
61 | \r | |
62 | **/\r | |
63 | EFI_CAPSULE_BLOCK_DESCRIPTOR *\r | |
64 | ValidateCapsuleIntegrity (\r | |
65 | IN EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList\r | |
66 | )\r | |
67 | {\r | |
68 | EFI_CAPSULE_HEADER *CapsuleHeader;\r | |
69 | UINT64 CapsuleSize;\r | |
70 | UINT32 CapsuleCount;\r | |
71 | EFI_CAPSULE_BLOCK_DESCRIPTOR *Ptr;\r | |
72 | \r | |
73 | //\r | |
74 | // Go through the list to look for inconsistencies. Check for:\r | |
75 | // * misaligned block descriptors.\r | |
76 | // * The first capsule header guid\r | |
77 | // * The first capsule header flag\r | |
78 | // * Data + Length < Data (wrap)\r | |
79 | CapsuleSize = 0;\r | |
80 | CapsuleCount = 0;\r | |
81 | Ptr = BlockList;\r | |
82 | while ((Ptr->Length != 0) || (Ptr->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r | |
83 | //\r | |
84 | // Make sure the descriptor is aligned at UINT64 in memory\r | |
85 | //\r | |
86 | if ((UINTN) Ptr & 0x07) {\r | |
87 | DEBUG ((EFI_D_ERROR, "BlockList address failed alignment check\n"));\r | |
88 | return NULL;\r | |
89 | }\r | |
90 | \r | |
91 | if (Ptr->Length == 0) {\r | |
92 | //\r | |
93 | // Descriptor points to another list of block descriptors somewhere\r | |
94 | // else.\r | |
95 | //\r | |
96 | Ptr = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Ptr->Union.ContinuationPointer;\r | |
97 | } else {\r | |
98 | //\r | |
99 | //To enhance the reliability of check-up, the first capsule's header is checked here.\r | |
100 | //More reliabilities check-up will do later.\r | |
101 | //\r | |
102 | if (CapsuleSize == 0) {\r | |
103 | //\r | |
104 | //Move to the first capsule to check its header.\r | |
105 | //\r | |
106 | CapsuleHeader = (EFI_CAPSULE_HEADER*)((UINTN)Ptr->Union.DataBlock);\r | |
107 | if (IsCapsuleCorrupted (CapsuleHeader)) {\r | |
108 | return NULL;\r | |
109 | }\r | |
110 | CapsuleCount ++;\r | |
111 | CapsuleSize = CapsuleHeader->CapsuleImageSize;\r | |
112 | } else {\r | |
113 | if (CapsuleSize >= Ptr->Length) {\r | |
114 | CapsuleSize = CapsuleSize - Ptr->Length;\r | |
115 | } else {\r | |
116 | CapsuleSize = 0;\r | |
117 | }\r | |
118 | }\r | |
119 | //\r | |
120 | // Move to next BLOCK descriptor\r | |
121 | //\r | |
122 | Ptr++;\r | |
123 | }\r | |
124 | }\r | |
125 | \r | |
126 | if (CapsuleCount == 0) {\r | |
127 | //\r | |
128 | // No any capsule is found in BlockList.\r | |
129 | //\r | |
130 | return NULL;\r | |
131 | }\r | |
132 | \r | |
133 | return Ptr;\r | |
134 | }\r | |
135 | \r | |
136 | \r | |
137 | /**\r | |
138 | Checks for the presence of capsule descriptors.\r | |
139 | Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...\r | |
140 | \r | |
141 | @param BlockList Pointer to the capsule descriptors\r | |
142 | \r | |
143 | @retval EFI_SUCCESS a valid capsule is present\r | |
144 | @retval EFI_NOT_FOUND if a valid capsule is not present\r | |
145 | **/\r | |
146 | EFI_STATUS\r | |
147 | GetCapsuleDescriptors (\r | |
148 | IN OUT EFI_CAPSULE_BLOCK_DESCRIPTOR **BlockList OPTIONAL\r | |
149 | )\r | |
150 | {\r | |
151 | EFI_STATUS Status;\r | |
152 | UINTN Size;\r | |
153 | UINTN Index;\r | |
154 | UINTN TempIndex;\r | |
155 | UINTN ValidIndex;\r | |
156 | BOOLEAN Flag;\r | |
157 | CHAR16 CapsuleVarName[30];\r | |
158 | CHAR16 *TempVarName;\r | |
159 | EFI_PHYSICAL_ADDRESS CapsuleDataPtr64;\r | |
160 | EFI_CAPSULE_BLOCK_DESCRIPTOR *LastBlock;\r | |
161 | EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlock;\r | |
162 | EFI_CAPSULE_BLOCK_DESCRIPTOR *HeadBlock;\r | |
163 | EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices;\r | |
164 | \r | |
165 | LastBlock = NULL;\r | |
166 | HeadBlock = NULL;\r | |
167 | TempBlock = NULL;\r | |
168 | Index = 0;\r | |
169 | TempVarName = NULL;\r | |
170 | CapsuleVarName[0] = 0;\r | |
171 | ValidIndex = 0;\r | |
172 | \r | |
173 | Status = PeiServicesLocatePpi (\r | |
174 | &gEfiPeiReadOnlyVariable2PpiGuid,\r | |
175 | 0,\r | |
176 | NULL,\r | |
177 | (VOID **) &PPIVariableServices\r | |
178 | );\r | |
179 | if (Status == EFI_SUCCESS) {\r | |
180 | StrCpy (CapsuleVarName, EFI_CAPSULE_VARIABLE_NAME);\r | |
181 | TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r | |
182 | Size = sizeof (CapsuleDataPtr64);\r | |
183 | while (1) {\r | |
184 | if (Index == 0) {\r | |
185 | //\r | |
186 | // For the first Capsule Image\r | |
187 | //\r | |
188 | Status = PPIVariableServices->GetVariable (\r | |
189 | PPIVariableServices,\r | |
190 | CapsuleVarName,\r | |
191 | &gEfiCapsuleVendorGuid,\r | |
192 | NULL,\r | |
193 | &Size,\r | |
194 | (VOID *) &CapsuleDataPtr64\r | |
195 | );\r | |
196 | if (EFI_ERROR (Status)) {\r | |
197 | DEBUG ((EFI_D_ERROR, "Capsule -- capsule variable not set\n"));\r | |
198 | return EFI_NOT_FOUND;\r | |
199 | }\r | |
200 | //\r | |
201 | // We have a chicken/egg situation where the memory init code needs to\r | |
202 | // know the boot mode prior to initializing memory. For this case, our\r | |
203 | // validate function will fail. We can detect if this is the case if blocklist\r | |
204 | // pointer is null. In that case, return success since we know that the\r | |
205 | // variable is set.\r | |
206 | //\r | |
207 | if (BlockList == NULL) {\r | |
208 | return EFI_SUCCESS;\r | |
209 | }\r | |
210 | //\r | |
211 | // Test integrity of descriptors.\r | |
212 | //\r | |
213 | LastBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)CapsuleDataPtr64);\r | |
214 | if (LastBlock == NULL) {\r | |
215 | return EFI_NOT_FOUND;\r | |
216 | }\r | |
217 | //\r | |
218 | // Return the base of the block descriptors\r | |
219 | //\r | |
220 | HeadBlock = (EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)CapsuleDataPtr64;\r | |
221 | } else {\r | |
222 | UnicodeValueToString (TempVarName, 0, Index, 0);\r | |
223 | Status = PPIVariableServices->GetVariable (\r | |
224 | PPIVariableServices,\r | |
225 | CapsuleVarName,\r | |
226 | &gEfiCapsuleVendorGuid,\r | |
227 | NULL,\r | |
228 | &Size,\r | |
229 | (VOID *) &CapsuleDataPtr64\r | |
230 | );\r | |
231 | if (EFI_ERROR (Status)) {\r | |
232 | break;\r | |
233 | }\r | |
234 | \r | |
235 | //\r | |
236 | // If this BlockList has been linked before, skip this variable\r | |
237 | //\r | |
238 | Flag = FALSE;\r | |
239 | for (TempIndex = 0; TempIndex < ValidIndex; TempIndex++) {\r | |
240 | if (mBufferAddress[TempIndex] == CapsuleDataPtr64) {\r | |
241 | Flag = TRUE;\r | |
242 | break;\r | |
243 | }\r | |
244 | }\r | |
245 | if (Flag) {\r | |
246 | Index ++;\r | |
247 | continue;\r | |
248 | }\r | |
249 | \r | |
250 | //\r | |
251 | // Test integrity of descriptors.\r | |
252 | //\r | |
253 | TempBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)CapsuleDataPtr64);\r | |
254 | if (TempBlock == NULL) {\r | |
255 | return EFI_NOT_FOUND;\r | |
256 | }\r | |
257 | //\r | |
258 | // Combine the different BlockList into single BlockList.\r | |
259 | //\r | |
260 | LastBlock->Union.DataBlock = CapsuleDataPtr64;\r | |
261 | LastBlock->Length = 0;\r | |
262 | LastBlock = TempBlock;\r | |
263 | }\r | |
264 | \r | |
265 | //\r | |
266 | // Cache BlockList which has been processed\r | |
267 | //\r | |
268 | mBufferAddress[ValidIndex++] = CapsuleDataPtr64;\r | |
269 | Index ++;\r | |
270 | }\r | |
271 | }\r | |
272 | \r | |
273 | if (HeadBlock != NULL) {\r | |
274 | *BlockList = HeadBlock;\r | |
275 | return EFI_SUCCESS;\r | |
276 | }\r | |
277 | return EFI_NOT_FOUND;\r | |
278 | }\r | |
279 | \r | |
280 | /**\r | |
281 | Given a pointer to a capsule block descriptor, traverse the list to figure\r | |
282 | out how many legitimate descriptors there are, and how big the capsule it\r | |
283 | refers to is.\r | |
284 | \r | |
285 | @param Desc Pointer to the capsule block descriptors\r | |
286 | NumDescriptors - optional pointer to where to return the number of descriptors\r | |
287 | CapsuleSize - optional pointer to where to return the capsule size\r | |
288 | @param NumDescriptors Optional pointer to where to return the number of descriptors\r | |
289 | @param CapsuleSize Optional pointer to where to return the capsule size\r | |
290 | \r | |
291 | @retval EFI_NOT_FOUND No descriptors containing data in the list\r | |
292 | @retval EFI_SUCCESS Return data is valid\r | |
293 | **/\r | |
294 | EFI_STATUS\r | |
295 | GetCapsuleInfo (\r | |
296 | IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc,\r | |
297 | IN OUT UINTN *NumDescriptors OPTIONAL,\r | |
298 | IN OUT UINTN *CapsuleSize OPTIONAL\r | |
299 | )\r | |
300 | {\r | |
301 | UINTN Count;\r | |
302 | UINTN Size;\r | |
303 | \r | |
304 | ASSERT (Desc != NULL);\r | |
305 | \r | |
306 | Count = 0;\r | |
307 | Size = 0;\r | |
308 | \r | |
309 | while (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r | |
310 | if (Desc->Length == 0) {\r | |
311 | //\r | |
312 | // Descriptor points to another list of block descriptors somewhere\r | |
313 | //\r | |
314 | Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;\r | |
315 | } else {\r | |
316 | Size += (UINTN) Desc->Length;\r | |
317 | Count++;\r | |
318 | Desc++;\r | |
319 | }\r | |
320 | }\r | |
321 | //\r | |
322 | // If no descriptors, then fail\r | |
323 | //\r | |
324 | if (Count == 0) {\r | |
325 | return EFI_NOT_FOUND;\r | |
326 | }\r | |
327 | \r | |
328 | if (NumDescriptors != NULL) {\r | |
329 | *NumDescriptors = Count;\r | |
330 | }\r | |
331 | \r | |
332 | if (CapsuleSize != NULL) {\r | |
333 | *CapsuleSize = Size;\r | |
334 | }\r | |
335 | \r | |
336 | return EFI_SUCCESS;\r | |
337 | }\r | |
338 | \r | |
339 | \r | |
340 | /**\r | |
341 | Try to verify the integrity of a capsule test pattern before the\r | |
342 | capsule gets coalesced. This can be useful in narrowing down\r | |
343 | where capsule data corruption occurs.\r | |
344 | \r | |
345 | The test pattern mode fills in memory with a counting UINT32 value. \r | |
346 | If the capsule is not divided up in a multiple of 4-byte blocks, then\r | |
347 | things get messy doing the check. Therefore there are some cases\r | |
348 | here where we just give up and skip the pre-coalesce check.\r | |
349 | \r | |
350 | @param PeiServices PEI services table\r | |
351 | @param Desc Pointer to capsule descriptors\r | |
352 | **/\r | |
353 | VOID\r | |
354 | CapsuleTestPatternPreCoalesce (\r | |
355 | IN EFI_PEI_SERVICES **PeiServices,\r | |
356 | IN EFI_CAPSULE_BLOCK_DESCRIPTOR *Desc\r | |
357 | )\r | |
358 | {\r | |
359 | UINT32 *TestPtr;\r | |
360 | UINT32 TestCounter;\r | |
361 | UINT32 TestSize;\r | |
362 | //\r | |
363 | // Find first data descriptor\r | |
364 | //\r | |
365 | while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r | |
366 | Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;\r | |
367 | }\r | |
368 | \r | |
369 | if (Desc->Union.ContinuationPointer == 0) {\r | |
370 | return ;\r | |
371 | }\r | |
372 | //\r | |
373 | // First one better be long enough to at least hold the test signature\r | |
374 | //\r | |
375 | if (Desc->Length < sizeof (UINT32)) {\r | |
376 | DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #1\n"));\r | |
377 | return ;\r | |
378 | }\r | |
379 | \r | |
380 | TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;\r | |
381 | if (*TestPtr != CAPSULE_TEST_SIGNATURE) {\r | |
382 | return ;\r | |
383 | }\r | |
384 | \r | |
385 | TestCounter = 0;\r | |
386 | TestSize = (UINT32) Desc->Length - 2 * sizeof (UINT32);\r | |
387 | //\r | |
388 | // Skip over the signature and the size fields in the pattern data header\r | |
389 | //\r | |
390 | TestPtr += 2;\r | |
391 | while (1) {\r | |
392 | if ((TestSize & 0x03) != 0) {\r | |
393 | DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce punted #2\n"));\r | |
394 | return ;\r | |
395 | }\r | |
396 | \r | |
397 | while (TestSize > 0) {\r | |
398 | if (*TestPtr != TestCounter) {\r | |
399 | DEBUG ((EFI_D_INFO, "Capsule test pattern pre-coalesce failed data corruption check\n"));\r | |
400 | return ;\r | |
401 | }\r | |
402 | \r | |
403 | TestSize -= sizeof (UINT32);\r | |
404 | TestCounter++;\r | |
405 | TestPtr++;\r | |
406 | }\r | |
407 | Desc++;\r | |
408 | while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r | |
409 | Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) Desc->Union.ContinuationPointer;\r | |
410 | }\r | |
411 | \r | |
412 | if (Desc->Union.ContinuationPointer == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r | |
413 | return ;\r | |
414 | }\r | |
415 | TestSize = (UINT32) Desc->Length;\r | |
416 | TestPtr = (UINT32 *) (UINTN) Desc->Union.DataBlock;\r | |
417 | }\r | |
418 | }\r | |
419 | \r | |
420 | \r | |
421 | /**\r | |
422 | Determine if two buffers overlap in memory.\r | |
423 | \r | |
424 | @param Buff1 pointer to first buffer\r | |
425 | @param Size1 size of Buff1\r | |
426 | @param Buff2 pointer to second buffer\r | |
427 | @param Size2 size of Buff2\r | |
428 | \r | |
429 | @retval TRUE Buffers overlap in memory.\r | |
430 | @retval FALSE Buffer doesn't overlap.\r | |
431 | **/\r | |
432 | BOOLEAN\r | |
433 | IsOverlapped (\r | |
434 | UINT8 *Buff1,\r | |
435 | UINTN Size1,\r | |
436 | UINT8 *Buff2,\r | |
437 | UINTN Size2\r | |
438 | )\r | |
439 | {\r | |
440 | //\r | |
441 | // If buff1's end is less than the start of buff2, then it's ok.\r | |
442 | // Also, if buff1's start is beyond buff2's end, then it's ok.\r | |
443 | //\r | |
444 | if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {\r | |
445 | return FALSE;\r | |
446 | }\r | |
447 | \r | |
448 | return TRUE;\r | |
449 | }\r | |
450 | \r | |
451 | /**\r | |
452 | Given a pointer to the capsule block list, info on the available system\r | |
453 | memory, and the size of a buffer, find a free block of memory where a\r | |
454 | buffer of the given size can be copied to safely.\r | |
455 | \r | |
456 | @param BlockList Pointer to head of capsule block descriptors\r | |
457 | @param MemBase Pointer to the base of memory in which we want to find free space\r | |
458 | @param MemSize The size of the block of memory pointed to by MemBase\r | |
459 | @param DataSize How big a free block we want to find\r | |
460 | \r | |
461 | @return A pointer to a memory block of at least DataSize that lies somewhere \r | |
462 | between MemBase and (MemBase + MemSize). The memory pointed to does not\r | |
463 | contain any of the capsule block descriptors or capsule blocks pointed to\r | |
464 | by the BlockList.\r | |
465 | **/\r | |
466 | UINT8 *\r | |
467 | FindFreeMem (\r | |
468 | EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList,\r | |
469 | UINT8 *MemBase,\r | |
470 | UINTN MemSize,\r | |
471 | UINTN DataSize\r | |
472 | )\r | |
473 | {\r | |
474 | UINTN Size;\r | |
475 | EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrDesc;\r | |
476 | EFI_CAPSULE_BLOCK_DESCRIPTOR *TempDesc;\r | |
477 | UINT8 *MemEnd;\r | |
478 | BOOLEAN Failed;\r | |
479 | \r | |
480 | //\r | |
481 | // Need at least enough to copy the data to at the end of the buffer, so\r | |
482 | // say the end is less the data size for easy comparisons here.\r | |
483 | //\r | |
484 | MemEnd = MemBase + MemSize - DataSize;\r | |
485 | CurrDesc = BlockList;\r | |
486 | //\r | |
487 | // Go through all the descriptor blocks and see if any obstruct the range\r | |
488 | //\r | |
489 | while (CurrDesc != NULL) {\r | |
490 | //\r | |
491 | // Get the size of this block list and see if it's in the way\r | |
492 | //\r | |
493 | Failed = FALSE;\r | |
494 | TempDesc = CurrDesc;\r | |
495 | Size = sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r | |
496 | while (TempDesc->Length != 0) {\r | |
497 | Size += sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r | |
498 | TempDesc++;\r | |
499 | }\r | |
500 | \r | |
501 | if (IsOverlapped (MemBase, DataSize, (UINT8 *) CurrDesc, Size)) {\r | |
502 | //\r | |
503 | // Set our new base to the end of this block list and start all over\r | |
504 | //\r | |
505 | MemBase = (UINT8 *) CurrDesc + Size;\r | |
506 | CurrDesc = BlockList;\r | |
507 | if (MemBase > MemEnd) {\r | |
508 | return NULL;\r | |
509 | }\r | |
510 | \r | |
511 | Failed = TRUE;\r | |
512 | }\r | |
513 | //\r | |
514 | // Now go through all the blocks and make sure none are in the way\r | |
515 | //\r | |
516 | while ((CurrDesc->Length != 0) && (!Failed)) {\r | |
517 | if (IsOverlapped (MemBase, DataSize, (UINT8 *) (UINTN) CurrDesc->Union.DataBlock, (UINTN) CurrDesc->Length)) {\r | |
518 | //\r | |
519 | // Set our new base to the end of this block and start all over\r | |
520 | //\r | |
521 | Failed = TRUE;\r | |
522 | MemBase = (UINT8 *) ((UINTN) CurrDesc->Union.DataBlock) + CurrDesc->Length;\r | |
523 | CurrDesc = BlockList;\r | |
524 | if (MemBase > MemEnd) {\r | |
525 | return NULL;\r | |
526 | }\r | |
527 | }\r | |
528 | CurrDesc++;\r | |
529 | }\r | |
530 | //\r | |
531 | // Normal continuation -- jump to next block descriptor list\r | |
532 | //\r | |
533 | if (!Failed) {\r | |
534 | CurrDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) CurrDesc->Union.ContinuationPointer;\r | |
535 | }\r | |
536 | }\r | |
537 | return MemBase;\r | |
538 | }\r | |
539 | \r | |
540 | /**\r | |
541 | The capsule block descriptors may be fragmented and spread all over memory.\r | |
542 | To simplify the coalescing of capsule blocks, first coalesce all the\r | |
543 | capsule block descriptors low in memory.\r | |
544 | \r | |
545 | The descriptors passed in can be fragmented throughout memory. Here\r | |
546 | they are relocated into memory to turn them into a contiguous (null\r | |
547 | terminated) array.\r | |
548 | \r | |
549 | @param PeiServices pointer to PEI services table\r | |
550 | @param BlockList pointer to the capsule block descriptors\r | |
551 | @param MemBase base of system memory in which we can work\r | |
552 | @param MemSize size of the system memory pointed to by MemBase\r | |
553 | \r | |
554 | @retval NULL could not relocate the descriptors\r | |
555 | @retval Pointer to the base of the successfully-relocated block descriptors. \r | |
556 | **/\r | |
557 | EFI_CAPSULE_BLOCK_DESCRIPTOR *\r | |
558 | RelocateBlockDescriptors (\r | |
559 | IN EFI_PEI_SERVICES **PeiServices,\r | |
560 | IN EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList,\r | |
561 | IN UINT8 *MemBase,\r | |
562 | IN UINTN MemSize\r | |
563 | )\r | |
564 | {\r | |
565 | EFI_CAPSULE_BLOCK_DESCRIPTOR *NewBlockList;\r | |
566 | EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrBlockDescHead;\r | |
567 | EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;\r | |
568 | EFI_CAPSULE_BLOCK_DESCRIPTOR *PrevBlockDescTail;\r | |
569 | UINTN NumDescriptors;\r | |
570 | UINTN BufferSize;\r | |
571 | UINT8 *RelocBuffer;\r | |
572 | UINTN BlockListSize;\r | |
573 | //\r | |
574 | // Get the info on the blocks and descriptors. Since we're going to move\r | |
575 | // the descriptors low in memory, adjust the base/size values accordingly here.\r | |
576 | // GetCapsuleInfo() returns the number of legit descriptors, so add one for\r | |
577 | // a terminator.\r | |
578 | //\r | |
579 | if (GetCapsuleInfo (BlockList, &NumDescriptors, NULL) != EFI_SUCCESS) {\r | |
580 | return NULL;\r | |
581 | }\r | |
582 | \r | |
583 | NumDescriptors++;\r | |
584 | BufferSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r | |
585 | NewBlockList = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) MemBase;\r | |
586 | if (MemSize < BufferSize) {\r | |
587 | return NULL;\r | |
588 | }\r | |
589 | \r | |
590 | MemSize -= BufferSize;\r | |
591 | MemBase += BufferSize;\r | |
592 | //\r | |
593 | // Go through all the blocks and make sure none are in the way\r | |
594 | //\r | |
595 | TempBlockDesc = BlockList;\r | |
596 | while (TempBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r | |
597 | if (TempBlockDesc->Length == 0) {\r | |
598 | //\r | |
599 | // Next block of descriptors\r | |
600 | //\r | |
601 | TempBlockDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;\r | |
602 | } else {\r | |
603 | //\r | |
604 | // If the capsule data pointed to by this descriptor is in the way,\r | |
605 | // move it.\r | |
606 | //\r | |
607 | if (IsOverlapped (\r | |
608 | (UINT8 *) NewBlockList,\r | |
609 | BufferSize,\r | |
610 | (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,\r | |
611 | (UINTN) TempBlockDesc->Length\r | |
612 | )) {\r | |
613 | //\r | |
614 | // Relocate the block\r | |
615 | //\r | |
616 | RelocBuffer = FindFreeMem (BlockList, MemBase, MemSize, (UINTN) TempBlockDesc->Length);\r | |
617 | if (RelocBuffer == NULL) {\r | |
618 | return NULL;\r | |
619 | }\r | |
620 | \r | |
621 | CopyMem ((VOID *) RelocBuffer, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);\r | |
622 | TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;\r | |
623 | \r | |
624 | 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 | |
625 | }\r | |
626 | }\r | |
627 | TempBlockDesc++;\r | |
628 | }\r | |
629 | //\r | |
630 | // Now go through all the block descriptors to make sure that they're not\r | |
631 | // in the memory region we want to copy them to.\r | |
632 | //\r | |
633 | CurrBlockDescHead = BlockList;\r | |
634 | PrevBlockDescTail = NULL;\r | |
635 | while ((CurrBlockDescHead != NULL) && (CurrBlockDescHead->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r | |
636 | //\r | |
637 | // Get the size of this list then see if it overlaps our low region\r | |
638 | //\r | |
639 | TempBlockDesc = CurrBlockDescHead;\r | |
640 | BlockListSize = sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r | |
641 | while (TempBlockDesc->Length != 0) {\r | |
642 | BlockListSize += sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r | |
643 | TempBlockDesc++;\r | |
644 | }\r | |
645 | \r | |
646 | if (IsOverlapped (\r | |
647 | (UINT8 *) NewBlockList,\r | |
648 | BufferSize,\r | |
649 | (UINT8 *) CurrBlockDescHead,\r | |
650 | BlockListSize\r | |
651 | )) {\r | |
652 | //\r | |
653 | // Overlaps, so move it out of the way\r | |
654 | //\r | |
655 | RelocBuffer = FindFreeMem (BlockList, MemBase, MemSize, BlockListSize);\r | |
656 | if (RelocBuffer == NULL) {\r | |
657 | return NULL;\r | |
658 | }\r | |
659 | CopyMem ((VOID *) RelocBuffer, (VOID *) CurrBlockDescHead, BlockListSize);\r | |
660 | DEBUG ((EFI_D_INFO, "Capsule reloc descriptor block #2\n"));\r | |
661 | //\r | |
662 | // Point the previous block's next point to this copied version. If\r | |
663 | // the tail pointer is null, then this is the first descriptor block.\r | |
664 | //\r | |
665 | if (PrevBlockDescTail == NULL) {\r | |
666 | BlockList = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) RelocBuffer;\r | |
667 | } else {\r | |
668 | PrevBlockDescTail->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;\r | |
669 | }\r | |
670 | }\r | |
671 | //\r | |
672 | // Save our new tail and jump to the next block list\r | |
673 | //\r | |
674 | PrevBlockDescTail = TempBlockDesc;\r | |
675 | CurrBlockDescHead = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;\r | |
676 | }\r | |
677 | //\r | |
678 | // Cleared out low memory. Now copy the descriptors down there.\r | |
679 | //\r | |
680 | TempBlockDesc = BlockList;\r | |
681 | CurrBlockDescHead = NewBlockList;\r | |
682 | while ((TempBlockDesc != NULL) && (TempBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r | |
683 | if (TempBlockDesc->Length != 0) {\r | |
684 | CurrBlockDescHead->Union.DataBlock = TempBlockDesc->Union.DataBlock;\r | |
685 | CurrBlockDescHead->Length = TempBlockDesc->Length;\r | |
686 | CurrBlockDescHead++;\r | |
687 | TempBlockDesc++;\r | |
688 | } else {\r | |
689 | TempBlockDesc = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;\r | |
690 | }\r | |
691 | }\r | |
692 | //\r | |
693 | // Null terminate\r | |
694 | //\r | |
695 | CurrBlockDescHead->Union.ContinuationPointer = (EFI_PHYSICAL_ADDRESS) (UINTN) NULL;\r | |
696 | CurrBlockDescHead->Length = 0;\r | |
697 | return NewBlockList;\r | |
698 | }\r | |
699 | \r | |
700 | /**\r | |
701 | Capsule PPI service to coalesce a fragmented capsule in memory.\r | |
702 | \r | |
703 | Memory Map for coalesced capsule:\r | |
704 | MemBase + ---->+---------------------------+<-----------+\r | |
705 | MemSize | CapsuleOffset[49] | |\r | |
706 | +---------------------------+ |\r | |
707 | | ................ | |\r | |
708 | +---------------------------+ |\r | |
709 | | CapsuleOffset[2] | |\r | |
710 | +---------------------------+ |\r | |
711 | | CapsuleOffset[1] | |\r | |
712 | +---------------------------+ |\r | |
713 | | CapsuleOffset[0] | CapsuleSize \r | |
714 | +---------------------------+ |\r | |
715 | | CapsuleNumber | |\r | |
716 | +---------------------------+ |\r | |
717 | | | | \r | |
718 | | | | \r | |
719 | | Capsule Image | | \r | |
720 | | | | \r | |
721 | | | | \r | |
722 | +---------------------------+ |\r | |
723 | | PrivateData | |\r | |
724 | DestPtr ----> +---------------------------+<-----------+\r | |
725 | | | |\r | |
726 | | FreeMem | FreeMemSize\r | |
727 | | | |\r | |
728 | FreeMemBase --->+---------------------------+<-----------+\r | |
729 | | Terminator |\r | |
730 | +---------------------------+\r | |
731 | | BlockDescriptor n |\r | |
732 | +---------------------------+\r | |
733 | | ................. |\r | |
734 | +---------------------------+\r | |
735 | | BlockDescriptor 1 |\r | |
736 | +---------------------------+\r | |
737 | | BlockDescriptor 0 |\r | |
738 | +---------------------------+\r | |
739 | | PrivateDataDesc 0 |\r | |
740 | MemBase ---->+---------------------------+<----- BlockList\r | |
741 | \r | |
742 | \r | |
743 | @param PeiServices General purpose services available to every PEIM.\r | |
744 | @param MemoryBase Pointer to the base of a block of memory that we can walk\r | |
745 | all over while trying to coalesce our buffers.\r | |
746 | On output, this variable will hold the base address of\r | |
747 | a coalesced capsule.\r | |
748 | @param MemorySize Size of the memory region pointed to by MemoryBase.\r | |
749 | On output, this variable will contain the size of the\r | |
750 | coalesced capsule.\r | |
751 | \r | |
752 | @retval EFI_NOT_FOUND if we can't determine the boot mode\r | |
753 | if the boot mode is not flash-update\r | |
754 | if we could not find the capsule descriptors\r | |
755 | \r | |
756 | @retval EFI_BUFFER_TOO_SMALL\r | |
757 | if we could not coalesce the capsule in the memory\r | |
758 | region provided to us\r | |
759 | \r | |
760 | @retval EFI_SUCCESS if there's no capsule, or if we processed the\r | |
761 | capsule successfully.\r | |
762 | **/\r | |
763 | EFI_STATUS\r | |
764 | EFIAPI\r | |
765 | CapsuleCoalesce (\r | |
766 | IN EFI_PEI_SERVICES **PeiServices,\r | |
767 | IN OUT VOID **MemoryBase,\r | |
768 | IN OUT UINTN *MemorySize\r | |
769 | )\r | |
770 | {\r | |
771 | VOID *NewCapsuleBase;\r | |
772 | VOID *DataPtr;\r | |
773 | UINT8 CapsuleIndex;\r | |
774 | UINT8 *FreeMemBase;\r | |
775 | UINT8 *DestPtr;\r | |
776 | UINT8 *RelocPtr;\r | |
777 | UINT32 CapsuleOffset[MAX_SUPPORT_CAPSULE_NUM]; \r | |
778 | UINT32 *AddDataPtr;\r | |
779 | UINT32 CapsuleTimes; \r | |
780 | UINT64 SizeLeft; \r | |
781 | UINT64 CapsuleImageSize; \r | |
782 | UINTN CapsuleSize;\r | |
783 | UINTN DescriptorsSize;\r | |
784 | UINTN FreeMemSize;\r | |
785 | UINTN NumDescriptors;\r | |
786 | UINTN Index;\r | |
787 | UINTN Size;\r | |
788 | UINTN VariableCount;\r | |
789 | CHAR16 CapsuleVarName[30];\r | |
790 | CHAR16 *TempVarName;\r | |
791 | EFI_PHYSICAL_ADDRESS CapsuleDataPtr64; \r | |
792 | BOOLEAN IsCorrupted;\r | |
793 | BOOLEAN CapsuleBeginFlag;\r | |
794 | EFI_STATUS Status;\r | |
795 | EFI_BOOT_MODE BootMode;\r | |
796 | EFI_CAPSULE_HEADER *CapsuleHeader;\r | |
797 | EFI_CAPSULE_PEIM_PRIVATE_DATA PrivateData;\r | |
798 | EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateDataPtr;\r | |
799 | EFI_CAPSULE_BLOCK_DESCRIPTOR *BlockList;\r | |
800 | EFI_CAPSULE_BLOCK_DESCRIPTOR *CurrentBlockDesc;\r | |
801 | EFI_CAPSULE_BLOCK_DESCRIPTOR *TempBlockDesc;\r | |
802 | EFI_CAPSULE_BLOCK_DESCRIPTOR PrivateDataDesc[2];\r | |
803 | EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices;\r | |
804 | \r | |
805 | CapsuleIndex = 0;\r | |
806 | SizeLeft = 0;\r | |
807 | CapsuleTimes = 0;\r | |
808 | CapsuleImageSize = 0;\r | |
809 | PrivateDataPtr = NULL;\r | |
810 | AddDataPtr = NULL;\r | |
811 | CapsuleHeader = NULL;\r | |
812 | CapsuleBeginFlag = TRUE;\r | |
813 | IsCorrupted = TRUE;\r | |
814 | CapsuleSize = 0;\r | |
815 | NumDescriptors = 0;\r | |
816 | Index = 0;\r | |
817 | VariableCount = 0;\r | |
818 | CapsuleVarName[0] = 0; \r | |
819 | \r | |
820 | //\r | |
821 | // Someone should have already ascertained the boot mode. If it's not\r | |
822 | // capsule update, then return normally.\r | |
823 | //\r | |
824 | Status = PeiServicesGetBootMode (&BootMode);\r | |
825 | if (EFI_ERROR (Status) || (BootMode != BOOT_ON_FLASH_UPDATE)) {\r | |
826 | return EFI_NOT_FOUND;\r | |
827 | }\r | |
828 | \r | |
829 | //\r | |
830 | // User may set the same ScatterGatherList with several different variables,\r | |
831 | // so cache all ScatterGatherList for check later.\r | |
832 | //\r | |
833 | Status = PeiServicesLocatePpi (\r | |
834 | &gEfiPeiReadOnlyVariable2PpiGuid,\r | |
835 | 0,\r | |
836 | NULL,\r | |
837 | (VOID **) &PPIVariableServices\r | |
838 | );\r | |
839 | if (EFI_ERROR (Status)) {\r | |
840 | return Status;\r | |
841 | }\r | |
842 | Size = sizeof (CapsuleDataPtr64);\r | |
843 | StrCpy (CapsuleVarName, EFI_CAPSULE_VARIABLE_NAME);\r | |
844 | TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r | |
845 | while (TRUE) {\r | |
846 | if (Index > 0) {\r | |
847 | UnicodeValueToString (TempVarName, 0, Index, 0);\r | |
848 | }\r | |
849 | Status = PPIVariableServices->GetVariable (\r | |
850 | PPIVariableServices,\r | |
851 | CapsuleVarName,\r | |
852 | &gEfiCapsuleVendorGuid,\r | |
853 | NULL,\r | |
854 | &Size,\r | |
855 | (VOID *) &CapsuleDataPtr64\r | |
856 | );\r | |
857 | if (EFI_ERROR (Status)) {\r | |
858 | //\r | |
859 | // There is no capsule variables, quit\r | |
860 | //\r | |
861 | DEBUG ((EFI_D_ERROR,"Capsule variable Index = %d\n", Index));\r | |
862 | break;\r | |
863 | }\r | |
864 | VariableCount++;\r | |
865 | Index++;\r | |
866 | }\r | |
867 | \r | |
868 | DEBUG ((EFI_D_ERROR,"Capsule variable count = %d\n", VariableCount));\r | |
869 | \r | |
870 | Status = PeiServicesAllocatePool (\r | |
871 | VariableCount * sizeof (EFI_PHYSICAL_ADDRESS),\r | |
872 | (VOID **)&mBufferAddress\r | |
873 | );\r | |
874 | \r | |
875 | if (Status != EFI_SUCCESS) {\r | |
876 | DEBUG ((EFI_D_ERROR, "AllocatePages Failed!, Status = %x\n", Status));\r | |
877 | return Status;\r | |
878 | }\r | |
879 | \r | |
880 | //\r | |
881 | // Find out if we actually have a capsule.\r | |
882 | //\r | |
883 | Status = GetCapsuleDescriptors (&BlockList);\r | |
884 | if (EFI_ERROR (Status)) {\r | |
885 | return Status;\r | |
886 | }\r | |
887 | \r | |
888 | DEBUG_CODE (\r | |
889 | CapsuleTestPatternPreCoalesce (PeiServices, BlockList);\r | |
890 | );\r | |
891 | \r | |
892 | //\r | |
893 | // Get the size of our descriptors and the capsule size. GetCapsuleInfo()\r | |
894 | // returns the number of descriptors that actually point to data, so add\r | |
895 | // one for a terminator. Do that below.\r | |
896 | //\r | |
897 | GetCapsuleInfo (BlockList, &NumDescriptors, &CapsuleSize);\r | |
898 | if ((CapsuleSize == 0) || (NumDescriptors == 0)) {\r | |
899 | return EFI_NOT_FOUND;\r | |
900 | }\r | |
901 | \r | |
902 | //\r | |
903 | // Initialize our local copy of private data. When we're done, we'll create a\r | |
904 | // descriptor for it as well so that it can be put into free memory without\r | |
905 | // trashing anything.\r | |
906 | //\r | |
907 | PrivateData.Signature = EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE;\r | |
908 | PrivateData.CapsuleSize = CapsuleSize;\r | |
909 | PrivateDataDesc[0].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) &PrivateData;\r | |
910 | PrivateDataDesc[0].Length = sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA);\r | |
911 | PrivateDataDesc[1].Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) BlockList;\r | |
912 | PrivateDataDesc[1].Length = 0;\r | |
913 | //\r | |
914 | // In addition to PrivateDataDesc[1:0], one terminator is added\r | |
915 | // See below RelocateBlockDescriptors()\r | |
916 | //\r | |
917 | NumDescriptors += 3;\r | |
918 | CapsuleSize += sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + sizeof(CapsuleOffset) + sizeof(UINT32);\r | |
919 | BlockList = PrivateDataDesc;\r | |
920 | DescriptorsSize = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r | |
921 | \r | |
922 | //\r | |
923 | // Don't go below some min address. If the base is below it,\r | |
924 | // then move it up and adjust the size accordingly.\r | |
925 | //\r | |
926 | DEBUG ((EFI_D_INFO, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN) *MemoryBase, (UINTN)*MemoryBase + *MemorySize));\r | |
927 | if ((UINTN)*MemoryBase < (UINTN) MIN_COALESCE_ADDR) {\r | |
928 | if (((UINTN)*MemoryBase + *MemorySize) < (UINTN) MIN_COALESCE_ADDR) {\r | |
929 | return EFI_BUFFER_TOO_SMALL;\r | |
930 | } else {\r | |
931 | *MemorySize = *MemorySize - ((UINTN) MIN_COALESCE_ADDR - (UINTN) *MemoryBase);\r | |
932 | *MemoryBase = (VOID *) (UINTN) MIN_COALESCE_ADDR;\r | |
933 | }\r | |
934 | }\r | |
935 | \r | |
936 | if (*MemorySize <= (CapsuleSize + DescriptorsSize)) {\r | |
937 | return EFI_BUFFER_TOO_SMALL;\r | |
938 | }\r | |
939 | \r | |
940 | FreeMemBase = *MemoryBase;\r | |
941 | FreeMemSize = *MemorySize;\r | |
942 | DEBUG ((EFI_D_INFO, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN) FreeMemBase, (UINTN) FreeMemBase + FreeMemSize));\r | |
943 | \r | |
944 | //\r | |
945 | // Relocate all the block descriptors to low memory to make further\r | |
946 | // processing easier.\r | |
947 | //\r | |
948 | BlockList = RelocateBlockDescriptors (PeiServices, BlockList, FreeMemBase, FreeMemSize);\r | |
949 | if (BlockList == NULL) {\r | |
950 | //\r | |
951 | // Not enough room to relocate the descriptors\r | |
952 | //\r | |
953 | return EFI_BUFFER_TOO_SMALL;\r | |
954 | }\r | |
955 | \r | |
956 | //\r | |
957 | // Take the top of memory for the capsule. Naturally align.\r | |
958 | //\r | |
959 | DestPtr = FreeMemBase + FreeMemSize - CapsuleSize;\r | |
960 | DestPtr = (UINT8 *) ((UINTN) DestPtr &~ (UINTN) (sizeof (UINTN) - 1));\r | |
961 | FreeMemBase = (UINT8 *) BlockList + DescriptorsSize;\r | |
962 | FreeMemSize = FreeMemSize - DescriptorsSize - CapsuleSize;\r | |
963 | NewCapsuleBase = (VOID *) DestPtr;\r | |
964 | \r | |
965 | //\r | |
966 | // Move all the blocks to the top (high) of memory.\r | |
967 | // Relocate all the obstructing blocks. Note that the block descriptors\r | |
968 | // were coalesced when they were relocated, so we can just ++ the pointer.\r | |
969 | //\r | |
970 | CurrentBlockDesc = BlockList;\r | |
971 | while ((CurrentBlockDesc->Length != 0) || (CurrentBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r | |
972 | //\r | |
973 | // See if any of the remaining capsule blocks are in the way\r | |
974 | //\r | |
975 | TempBlockDesc = CurrentBlockDesc;\r | |
976 | while (TempBlockDesc->Length != 0) {\r | |
977 | //\r | |
978 | // Is this block in the way of where we want to copy the current descriptor to?\r | |
979 | //\r | |
980 | if (IsOverlapped (\r | |
981 | (UINT8 *) DestPtr,\r | |
982 | (UINTN) CurrentBlockDesc->Length,\r | |
983 | (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,\r | |
984 | (UINTN) TempBlockDesc->Length\r | |
985 | )) {\r | |
986 | //\r | |
987 | // Relocate the block\r | |
988 | //\r | |
989 | RelocPtr = FindFreeMem (BlockList, FreeMemBase, FreeMemSize, (UINTN) TempBlockDesc->Length);\r | |
990 | if (RelocPtr == NULL) {\r | |
991 | return EFI_BUFFER_TOO_SMALL;\r | |
992 | }\r | |
993 | \r | |
994 | CopyMem ((VOID *) RelocPtr, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);\r | |
995 | DEBUG ((EFI_D_INFO, "Capsule reloc data block from 0x%8X to 0x%8X with size 0x%8X\n",\r | |
996 | (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) RelocPtr, (UINTN) TempBlockDesc->Length));\r | |
997 | \r | |
998 | TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocPtr;\r | |
999 | }\r | |
1000 | //\r | |
1001 | // Next descriptor\r | |
1002 | //\r | |
1003 | TempBlockDesc++;\r | |
1004 | }\r | |
1005 | //\r | |
1006 | // Ok, we made it through. Copy the block.\r | |
1007 | // we just support greping one capsule from the lists of block descs list.\r | |
1008 | //\r | |
1009 | CapsuleTimes ++;\r | |
1010 | //\r | |
1011 | //Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA\r | |
1012 | //\r | |
1013 | if (CapsuleTimes > 1) {\r | |
1014 | //\r | |
1015 | //For every capsule entry point, check its header to determine whether to relocate it.\r | |
1016 | //If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.\r | |
1017 | //\r | |
1018 | if (CapsuleBeginFlag) {\r | |
1019 | CapsuleBeginFlag = FALSE;\r | |
1020 | CapsuleHeader = (EFI_CAPSULE_HEADER*)(UINTN)CurrentBlockDesc->Union.DataBlock;\r | |
1021 | SizeLeft = CapsuleHeader->CapsuleImageSize;\r | |
1022 | if (!IsCapsuleCorrupted (CapsuleHeader)) {\r | |
1023 | \r | |
1024 | if (CapsuleIndex > (MAX_SUPPORT_CAPSULE_NUM - 1)) {\r | |
1025 | DEBUG ((EFI_D_ERROR, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM));\r | |
1026 | return EFI_BUFFER_TOO_SMALL;\r | |
1027 | }\r | |
1028 | \r | |
1029 | //\r | |
1030 | // Relocate this valid capsule\r | |
1031 | //\r | |
1032 | IsCorrupted = FALSE;\r | |
1033 | CapsuleImageSize += SizeLeft;\r | |
1034 | CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);\r | |
1035 | DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8X to 0x%8X with size 0x%8X\n",CapsuleTimes,\r | |
1036 | (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r | |
1037 | //\r | |
1038 | // Cache the begin offset of this capsule\r | |
1039 | //\r | |
1040 | CapsuleOffset[CapsuleIndex++] = (UINT32) (UINTN) DestPtr - (UINT32)(UINTN)NewCapsuleBase - (UINT32)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA);\r | |
1041 | DestPtr += CurrentBlockDesc->Length;\r | |
1042 | }\r | |
1043 | //\r | |
1044 | // If the current block length is greater than or equal to SizeLeft, this is the \r | |
1045 | // start of the next capsule\r | |
1046 | //\r | |
1047 | if (CurrentBlockDesc->Length < SizeLeft) {\r | |
1048 | SizeLeft -= CurrentBlockDesc->Length;\r | |
1049 | } else {\r | |
1050 | //\r | |
1051 | // Start the next cycle\r | |
1052 | //\r | |
1053 | SizeLeft = 0;\r | |
1054 | IsCorrupted = TRUE;\r | |
1055 | CapsuleBeginFlag = TRUE; \r | |
1056 | }\r | |
1057 | } else {\r | |
1058 | //\r | |
1059 | //Go on relocating the current capule image.\r | |
1060 | //\r | |
1061 | if (CurrentBlockDesc->Length < SizeLeft) {\r | |
1062 | if (!IsCorrupted) {\r | |
1063 | CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) (CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);\r | |
1064 | DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8X to 0x%8X with size 0x%8X\n",CapsuleTimes,\r | |
1065 | (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r | |
1066 | DestPtr += CurrentBlockDesc->Length;\r | |
1067 | }\r | |
1068 | SizeLeft -= CurrentBlockDesc->Length;\r | |
1069 | } else {\r | |
1070 | //\r | |
1071 | //Here is the end of the current capsule image.\r | |
1072 | //\r | |
1073 | if (!IsCorrupted) {\r | |
1074 | CopyMem ((VOID *) DestPtr, (VOID *)(UINTN)(CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);\r | |
1075 | DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8X to 0x%8X with size 0x%8X\n",CapsuleTimes,\r | |
1076 | (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r | |
1077 | DestPtr += CurrentBlockDesc->Length;\r | |
1078 | }\r | |
1079 | //\r | |
1080 | // Start the next cycle\r | |
1081 | //\r | |
1082 | SizeLeft = 0;\r | |
1083 | IsCorrupted = TRUE;\r | |
1084 | CapsuleBeginFlag = TRUE; \r | |
1085 | }\r | |
1086 | }\r | |
1087 | } else {\r | |
1088 | //\r | |
1089 | //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.\r | |
1090 | //\r | |
1091 | CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);\r | |
1092 | DestPtr += CurrentBlockDesc->Length;\r | |
1093 | }\r | |
1094 | //\r | |
1095 | //Walk through the block descriptor list.\r | |
1096 | //\r | |
1097 | CurrentBlockDesc++;\r | |
1098 | }\r | |
1099 | //\r | |
1100 | // We return the base of memory we want reserved, and the size.\r | |
1101 | // The memory peim should handle it appropriately from there.\r | |
1102 | //\r | |
1103 | *MemorySize = (UINTN) CapsuleImageSize;\r | |
1104 | *MemoryBase = (VOID *) NewCapsuleBase;\r | |
1105 | \r | |
1106 | //\r | |
1107 | //Append the offsets of mutiply capsules to the continous buffer\r | |
1108 | //\r | |
1109 | DataPtr = (VOID*)((UINTN)NewCapsuleBase + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (UINTN)CapsuleImageSize);\r | |
1110 | AddDataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) &~ (UINT32) (sizeof (UINT32) - 1));\r | |
1111 | \r | |
1112 | *AddDataPtr++ = CapsuleIndex;\r | |
1113 | \r | |
1114 | CopyMem (AddDataPtr, &CapsuleOffset[0], sizeof (UINT32) * CapsuleIndex);\r | |
1115 | \r | |
1116 | PrivateDataPtr = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) NewCapsuleBase;\r | |
1117 | PrivateDataPtr->CapsuleSize = (UINTN)CapsuleImageSize;\r | |
1118 | \r | |
1119 | return Status;\r | |
1120 | }\r | |
1121 | \r | |
1122 | /**\r | |
1123 | Determine if we're in capsule update boot mode.\r | |
1124 | \r | |
1125 | @param PeiServices PEI services table\r | |
1126 | \r | |
1127 | @retval EFI_SUCCESS if we have a capsule available\r | |
1128 | @retval EFI_NOT_FOUND no capsule detected\r | |
1129 | \r | |
1130 | **/\r | |
1131 | EFI_STATUS\r | |
1132 | EFIAPI\r | |
1133 | CheckCapsuleUpdate (\r | |
1134 | IN EFI_PEI_SERVICES **PeiServices\r | |
1135 | )\r | |
1136 | {\r | |
1137 | EFI_STATUS Status;\r | |
1138 | Status = GetCapsuleDescriptors (NULL);\r | |
1139 | return Status;\r | |
1140 | }\r | |
1141 | /**\r | |
1142 | This function will look at a capsule and determine if it's a test pattern. \r | |
1143 | If it is, then it will verify it and emit an error message if corruption is detected.\r | |
1144 | \r | |
1145 | @param PeiServices Standard pei services pointer\r | |
1146 | @param CapsuleBase Base address of coalesced capsule, which is preceeded\r | |
1147 | by private data. Very implementation specific.\r | |
1148 | \r | |
1149 | @retval TRUE Capsule image is the test image\r | |
1150 | @retval FALSE Capsule image is not the test image.\r | |
1151 | \r | |
1152 | **/\r | |
1153 | BOOLEAN\r | |
1154 | CapsuleTestPattern (\r | |
1155 | IN EFI_PEI_SERVICES **PeiServices,\r | |
1156 | IN VOID *CapsuleBase\r | |
1157 | )\r | |
1158 | {\r | |
1159 | UINT32 *TestPtr;\r | |
1160 | UINT32 TestCounter;\r | |
1161 | UINT32 TestSize;\r | |
1162 | BOOLEAN RetValue;\r | |
1163 | \r | |
1164 | RetValue = FALSE;\r | |
1165 | \r | |
1166 | //\r | |
1167 | // Look at the capsule data and determine if it's a test pattern. If it\r | |
1168 | // is, then test it now.\r | |
1169 | //\r | |
1170 | TestPtr = (UINT32 *) CapsuleBase;\r | |
1171 | if (*TestPtr == CAPSULE_TEST_SIGNATURE) {\r | |
1172 | RetValue = TRUE;\r | |
1173 | DEBUG ((EFI_D_INFO, "Capsule test pattern mode activated...\n"));\r | |
1174 | TestSize = TestPtr[1] / sizeof (UINT32);\r | |
1175 | //\r | |
1176 | // Skip over the signature and the size fields in the pattern data header\r | |
1177 | //\r | |
1178 | TestPtr += 2;\r | |
1179 | TestCounter = 0;\r | |
1180 | while (TestSize > 0) {\r | |
1181 | if (*TestPtr != TestCounter) {\r | |
1182 | DEBUG ((EFI_D_INFO, "Capsule test pattern mode FAILED: BaseAddr/FailAddr 0x%X 0x%X\n", (UINT32)(UINTN)(EFI_CAPSULE_PEIM_PRIVATE_DATA *)CapsuleBase, (UINT32)(UINTN)TestPtr));\r | |
1183 | return TRUE;\r | |
1184 | }\r | |
1185 | \r | |
1186 | TestPtr++;\r | |
1187 | TestCounter++;\r | |
1188 | TestSize--;\r | |
1189 | }\r | |
1190 | \r | |
1191 | DEBUG ((EFI_D_INFO, "Capsule test pattern mode SUCCESS\n"));\r | |
1192 | }\r | |
1193 | \r | |
1194 | return RetValue;\r | |
1195 | }\r | |
1196 | \r | |
1197 | /**\r | |
1198 | Capsule PPI service that gets called after memory is available. The\r | |
1199 | capsule coalesce function, which must be called first, returns a base\r | |
1200 | address and size, which can be anything actually. Once the memory init\r | |
1201 | PEIM has discovered memory, then it should call this function and pass in\r | |
1202 | the base address and size returned by the coalesce function. Then this\r | |
1203 | function can create a capsule HOB and return.\r | |
1204 | \r | |
1205 | @param PeiServices standard pei services pointer\r | |
1206 | @param CapsuleBase address returned by the capsule coalesce function. Most\r | |
1207 | likely this will actually be a pointer to private data.\r | |
1208 | @param CapsuleSize value returned by the capsule coalesce function.\r | |
1209 | \r | |
1210 | @retval EFI_VOLUME_CORRUPTED CapsuleBase does not appear to point to a\r | |
1211 | coalesced capsule\r | |
1212 | @retval EFI_SUCCESS if all goes well.\r | |
1213 | **/\r | |
1214 | EFI_STATUS\r | |
1215 | EFIAPI\r | |
1216 | CreateState (\r | |
1217 | IN EFI_PEI_SERVICES **PeiServices,\r | |
1218 | IN VOID *CapsuleBase,\r | |
1219 | IN UINTN CapsuleSize\r | |
1220 | )\r | |
1221 | {\r | |
1222 | EFI_STATUS Status;\r | |
1223 | EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateData;\r | |
1224 | UINTN Size;\r | |
1225 | EFI_PHYSICAL_ADDRESS NewBuffer;\r | |
1226 | UINT32 *DataPtr;\r | |
1227 | UINT32 CapsuleNumber;\r | |
1228 | UINT32 Index;\r | |
1229 | EFI_PHYSICAL_ADDRESS BaseAddress;\r | |
1230 | UINT64 Length;\r | |
1231 | \r | |
1232 | DataPtr = NULL;\r | |
1233 | CapsuleNumber = 0;\r | |
1234 | PrivateData = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) CapsuleBase;\r | |
1235 | if (PrivateData->Signature != EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE) {\r | |
1236 | return EFI_VOLUME_CORRUPTED;\r | |
1237 | }\r | |
1238 | //\r | |
1239 | // Capsule Number and Capsule Offset is in the tail of Capsule data.\r | |
1240 | //\r | |
1241 | Size = (UINTN) PrivateData->CapsuleSize;\r | |
1242 | DataPtr = (UINT32*)((UINTN)CapsuleBase + (UINTN)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA)+ Size);\r | |
1243 | DataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) & ~(sizeof (UINT32) - 1));\r | |
1244 | CapsuleNumber = *DataPtr++;\r | |
1245 | //\r | |
1246 | // Allocate the memory so that it gets preserved into DXE\r | |
1247 | //\r | |
1248 | Status = PeiServicesAllocatePages (\r | |
1249 | EfiRuntimeServicesData,\r | |
1250 | EFI_SIZE_TO_PAGES (Size),\r | |
1251 | &NewBuffer\r | |
1252 | );\r | |
1253 | \r | |
1254 | if (Status != EFI_SUCCESS) {\r | |
1255 | DEBUG ((EFI_D_ERROR, "AllocatePages Failed!\n"));\r | |
1256 | return Status;\r | |
1257 | }\r | |
1258 | //\r | |
1259 | // Copy to our new buffer for DXE\r | |
1260 | //\r | |
1261 | DEBUG ((EFI_D_INFO, "Capsule copy from 0x%8X to 0x%8X with size 0x%8X\n", (UINTN) (PrivateData + 1), (UINTN) NewBuffer, Size));\r | |
1262 | CopyMem ((VOID *) (UINTN) NewBuffer, (VOID *) (UINTN) (PrivateData + 1), Size);\r | |
1263 | //\r | |
1264 | // Check for test data pattern. If it is the test pattern, then we'll\r | |
1265 | // test it ans still create the HOB so that it can be used to verify\r | |
1266 | // that capsules don't get corrupted all the way into BDS. BDS will\r | |
1267 | // still try to turn it into a firmware volume, but will think it's\r | |
1268 | // corrupted so nothing will happen.\r | |
1269 | //\r | |
1270 | DEBUG_CODE (\r | |
1271 | CapsuleTestPattern (PeiServices, (VOID *) (UINTN) NewBuffer);\r | |
1272 | );\r | |
1273 | \r | |
1274 | //\r | |
1275 | // Build the UEFI Capsule Hob for each capsule image.\r | |
1276 | //\r | |
1277 | for (Index = 0; Index < CapsuleNumber; Index ++) {\r | |
1278 | BaseAddress = NewBuffer + DataPtr[Index];\r | |
1279 | Length = ((EFI_CAPSULE_HEADER *)((UINTN) BaseAddress))->CapsuleImageSize;\r | |
1280 | \r | |
1281 | BuildCvHob (BaseAddress, Length);\r | |
1282 | }\r | |
1283 | \r | |
1284 | return EFI_SUCCESS;\r | |
1285 | }\r | |
1286 | \r | |
1287 | CONST PEI_CAPSULE_PPI mCapsulePpi = {\r | |
1288 | CapsuleCoalesce,\r | |
1289 | CheckCapsuleUpdate,\r | |
1290 | CreateState\r | |
1291 | };\r | |
1292 | \r | |
1293 | CONST EFI_PEI_PPI_DESCRIPTOR mUefiPpiListCapsule = {\r | |
1294 | (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r | |
1295 | &gPeiCapsulePpiGuid,\r | |
1296 | (PEI_CAPSULE_PPI *) &mCapsulePpi\r | |
1297 | };\r | |
1298 | \r | |
1299 | /**\r | |
1300 | Entry point function for the PEIM\r | |
1301 | \r | |
1302 | @param FileHandle Handle of the file being invoked.\r | |
1303 | @param PeiServices Describes the list of possible PEI Services.\r | |
1304 | \r | |
1305 | @return EFI_SUCCESS If we installed our PPI\r | |
1306 | \r | |
1307 | **/\r | |
1308 | EFI_STATUS\r | |
1309 | EFIAPI\r | |
1310 | CapsuleMain (\r | |
1311 | IN EFI_PEI_FILE_HANDLE FileHandle,\r | |
1312 | IN CONST EFI_PEI_SERVICES **PeiServices\r | |
1313 | )\r | |
1314 | {\r | |
1315 | //\r | |
1316 | // Just produce our PPI\r | |
1317 | //\r | |
1318 | return PeiServicesInstallPpi (&mUefiPpiListCapsule);\r | |
1319 | }\r |