1 | /** @file
|
---|
2 | DXE capsule process.
|
---|
3 |
|
---|
4 | Caution: This module requires additional review when modified.
|
---|
5 | This module will have external input - capsule image.
|
---|
6 | This external input must be validated carefully to avoid security issue like
|
---|
7 | buffer overflow, integer overflow.
|
---|
8 |
|
---|
9 | ProcessCapsules(), ProcessTheseCapsules() will receive untrusted
|
---|
10 | input and do basic validation.
|
---|
11 |
|
---|
12 | Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
|
---|
13 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
14 |
|
---|
15 | **/
|
---|
16 |
|
---|
17 | #include <PiDxe.h>
|
---|
18 | #include <Protocol/EsrtManagement.h>
|
---|
19 | #include <Protocol/FirmwareManagementProgress.h>
|
---|
20 |
|
---|
21 | #include <Library/BaseLib.h>
|
---|
22 | #include <Library/DebugLib.h>
|
---|
23 | #include <Library/BaseMemoryLib.h>
|
---|
24 | #include <Library/UefiBootServicesTableLib.h>
|
---|
25 | #include <Library/UefiRuntimeServicesTableLib.h>
|
---|
26 | #include <Library/MemoryAllocationLib.h>
|
---|
27 | #include <Library/UefiLib.h>
|
---|
28 | #include <Library/PcdLib.h>
|
---|
29 | #include <Library/HobLib.h>
|
---|
30 | #include <Library/ReportStatusCodeLib.h>
|
---|
31 | #include <Library/CapsuleLib.h>
|
---|
32 | #include <Library/DisplayUpdateProgressLib.h>
|
---|
33 |
|
---|
34 | #include <IndustryStandard/WindowsUxCapsule.h>
|
---|
35 |
|
---|
36 | extern EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL *mFmpProgress;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | Return if this FMP is a system FMP or a device FMP, based upon CapsuleHeader.
|
---|
40 |
|
---|
41 | @param[in] CapsuleHeader A pointer to EFI_CAPSULE_HEADER
|
---|
42 |
|
---|
43 | @retval TRUE It is a system FMP.
|
---|
44 | @retval FALSE It is a device FMP.
|
---|
45 | **/
|
---|
46 | BOOLEAN
|
---|
47 | IsFmpCapsule (
|
---|
48 | IN EFI_CAPSULE_HEADER *CapsuleHeader
|
---|
49 | );
|
---|
50 |
|
---|
51 | /**
|
---|
52 | Validate Fmp capsules layout.
|
---|
53 |
|
---|
54 | Caution: This function may receive untrusted input.
|
---|
55 |
|
---|
56 | This function assumes the caller validated the capsule by using
|
---|
57 | IsValidCapsuleHeader(), so that all fields in EFI_CAPSULE_HEADER are correct.
|
---|
58 | The capsule buffer size is CapsuleHeader->CapsuleImageSize.
|
---|
59 |
|
---|
60 | This function validates the fields in EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER
|
---|
61 | and EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER.
|
---|
62 |
|
---|
63 | This function need support nested FMP capsule.
|
---|
64 |
|
---|
65 | @param[in] CapsuleHeader Points to a capsule header.
|
---|
66 | @param[out] EmbeddedDriverCount The EmbeddedDriverCount in the FMP capsule.
|
---|
67 |
|
---|
68 | @retval EFI_SUCESS Input capsule is a correct FMP capsule.
|
---|
69 | @retval EFI_INVALID_PARAMETER Input capsule is not a correct FMP capsule.
|
---|
70 | **/
|
---|
71 | EFI_STATUS
|
---|
72 | ValidateFmpCapsule (
|
---|
73 | IN EFI_CAPSULE_HEADER *CapsuleHeader,
|
---|
74 | OUT UINT16 *EmbeddedDriverCount OPTIONAL
|
---|
75 | );
|
---|
76 |
|
---|
77 | /**
|
---|
78 | Validate if it is valid capsule header
|
---|
79 |
|
---|
80 | This function assumes the caller provided correct CapsuleHeader pointer
|
---|
81 | and CapsuleSize.
|
---|
82 |
|
---|
83 | This function validates the fields in EFI_CAPSULE_HEADER.
|
---|
84 |
|
---|
85 | @param[in] CapsuleHeader Points to a capsule header.
|
---|
86 | @param[in] CapsuleSize Size of the whole capsule image.
|
---|
87 |
|
---|
88 | **/
|
---|
89 | BOOLEAN
|
---|
90 | IsValidCapsuleHeader (
|
---|
91 | IN EFI_CAPSULE_HEADER *CapsuleHeader,
|
---|
92 | IN UINT64 CapsuleSize
|
---|
93 | );
|
---|
94 |
|
---|
95 | /**
|
---|
96 | Return if this capsule is a capsule name capsule, based upon CapsuleHeader.
|
---|
97 |
|
---|
98 | @param[in] CapsuleHeader A pointer to EFI_CAPSULE_HEADER
|
---|
99 |
|
---|
100 | @retval TRUE It is a capsule name capsule.
|
---|
101 | @retval FALSE It is not a capsule name capsule.
|
---|
102 | **/
|
---|
103 | BOOLEAN
|
---|
104 | IsCapsuleNameCapsule (
|
---|
105 | IN EFI_CAPSULE_HEADER *CapsuleHeader
|
---|
106 | );
|
---|
107 |
|
---|
108 | /**
|
---|
109 | Check the integrity of the capsule name capsule.
|
---|
110 | If the capsule is vaild, return the physical address of each capsule name string.
|
---|
111 |
|
---|
112 | @param[in] CapsuleHeader Pointer to the capsule header of a capsule name capsule.
|
---|
113 | @param[out] CapsuleNameNum Number of capsule name.
|
---|
114 |
|
---|
115 | @retval NULL Capsule name capsule is not valid.
|
---|
116 | @retval CapsuleNameBuf Array of capsule name physical address.
|
---|
117 |
|
---|
118 | **/
|
---|
119 | EFI_PHYSICAL_ADDRESS *
|
---|
120 | ValidateCapsuleNameCapsuleIntegrity (
|
---|
121 | IN EFI_CAPSULE_HEADER *CapsuleHeader,
|
---|
122 | OUT UINTN *CapsuleNameNum
|
---|
123 | );
|
---|
124 |
|
---|
125 | extern BOOLEAN mDxeCapsuleLibEndOfDxe;
|
---|
126 | BOOLEAN mNeedReset = FALSE;
|
---|
127 |
|
---|
128 | VOID **mCapsulePtr;
|
---|
129 | CHAR16 **mCapsuleNamePtr;
|
---|
130 | EFI_STATUS *mCapsuleStatusArray;
|
---|
131 | UINT32 mCapsuleTotalNumber;
|
---|
132 |
|
---|
133 | /**
|
---|
134 | The firmware implements to process the capsule image.
|
---|
135 |
|
---|
136 | Caution: This function may receive untrusted input.
|
---|
137 |
|
---|
138 | @param[in] CapsuleHeader Points to a capsule header.
|
---|
139 | @param[in] CapFileName Capsule file name.
|
---|
140 | @param[out] ResetRequired Indicates whether reset is required or not.
|
---|
141 |
|
---|
142 | @retval EFI_SUCESS Process Capsule Image successfully.
|
---|
143 | @retval EFI_UNSUPPORTED Capsule image is not supported by the firmware.
|
---|
144 | @retval EFI_VOLUME_CORRUPTED FV volume in the capsule is corrupted.
|
---|
145 | @retval EFI_OUT_OF_RESOURCES Not enough memory.
|
---|
146 | **/
|
---|
147 | EFI_STATUS
|
---|
148 | EFIAPI
|
---|
149 | ProcessThisCapsuleImage (
|
---|
150 | IN EFI_CAPSULE_HEADER *CapsuleHeader,
|
---|
151 | IN CHAR16 *CapFileName OPTIONAL,
|
---|
152 | OUT BOOLEAN *ResetRequired OPTIONAL
|
---|
153 | );
|
---|
154 |
|
---|
155 | /**
|
---|
156 | Function indicate the current completion progress of the firmware
|
---|
157 | update. Platform may override with own specific progress function.
|
---|
158 |
|
---|
159 | @param[in] Completion A value between 1 and 100 indicating the current
|
---|
160 | completion progress of the firmware update
|
---|
161 |
|
---|
162 | @retval EFI_SUCESS The capsule update progress was updated.
|
---|
163 | @retval EFI_INVALID_PARAMETER Completion is greater than 100%.
|
---|
164 | **/
|
---|
165 | EFI_STATUS
|
---|
166 | EFIAPI
|
---|
167 | UpdateImageProgress (
|
---|
168 | IN UINTN Completion
|
---|
169 | )
|
---|
170 | {
|
---|
171 | EFI_STATUS Status;
|
---|
172 | UINTN Seconds;
|
---|
173 | EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION *Color;
|
---|
174 |
|
---|
175 | DEBUG ((DEBUG_INFO, "Update Progress - %d%%\n", Completion));
|
---|
176 |
|
---|
177 | if (Completion > 100) {
|
---|
178 | return EFI_INVALID_PARAMETER;
|
---|
179 | }
|
---|
180 |
|
---|
181 | //
|
---|
182 | // Use a default timeout of 5 minutes if there is not FMP Progress Protocol.
|
---|
183 | //
|
---|
184 | Seconds = 5 * 60;
|
---|
185 | Color = NULL;
|
---|
186 | if (mFmpProgress != NULL) {
|
---|
187 | Seconds = mFmpProgress->WatchdogSeconds;
|
---|
188 | Color = &mFmpProgress->ProgressBarForegroundColor;
|
---|
189 | }
|
---|
190 |
|
---|
191 | //
|
---|
192 | // Cancel the watchdog timer
|
---|
193 | //
|
---|
194 | gBS->SetWatchdogTimer (0, 0x0000, 0, NULL);
|
---|
195 |
|
---|
196 | if (Completion != 100) {
|
---|
197 | //
|
---|
198 | // Arm the watchdog timer from PCD setting
|
---|
199 | //
|
---|
200 | if (Seconds != 0) {
|
---|
201 | DEBUG ((DEBUG_VERBOSE, "Arm watchdog timer %d seconds\n", Seconds));
|
---|
202 | gBS->SetWatchdogTimer (Seconds, 0x0000, 0, NULL);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | Status = DisplayUpdateProgress (Completion, Color);
|
---|
207 |
|
---|
208 | return Status;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | This function initializes the mCapsulePtr, mCapsuleStatusArray and mCapsuleTotalNumber.
|
---|
213 | **/
|
---|
214 | VOID
|
---|
215 | InitCapsulePtr (
|
---|
216 | VOID
|
---|
217 | )
|
---|
218 | {
|
---|
219 | EFI_PEI_HOB_POINTERS HobPointer;
|
---|
220 | UINTN Index;
|
---|
221 | UINTN Index2;
|
---|
222 | UINTN Index3;
|
---|
223 | UINTN CapsuleNameNumber;
|
---|
224 | UINTN CapsuleNameTotalNumber;
|
---|
225 | UINTN CapsuleNameCapsuleTotalNumber;
|
---|
226 | VOID **CapsuleNameCapsulePtr;
|
---|
227 | EFI_PHYSICAL_ADDRESS *CapsuleNameAddress;
|
---|
228 |
|
---|
229 | CapsuleNameNumber = 0;
|
---|
230 | CapsuleNameTotalNumber = 0;
|
---|
231 | CapsuleNameCapsuleTotalNumber = 0;
|
---|
232 | CapsuleNameCapsulePtr = NULL;
|
---|
233 |
|
---|
234 | //
|
---|
235 | // Find all capsule images from hob
|
---|
236 | //
|
---|
237 | HobPointer.Raw = GetHobList ();
|
---|
238 | while ((HobPointer.Raw = GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobPointer.Raw)) != NULL) {
|
---|
239 | if (!IsValidCapsuleHeader ((VOID *)(UINTN)HobPointer.Capsule->BaseAddress, HobPointer.Capsule->Length)) {
|
---|
240 | HobPointer.Header->HobType = EFI_HOB_TYPE_UNUSED; // Mark this hob as invalid
|
---|
241 | } else {
|
---|
242 | if (IsCapsuleNameCapsule ((VOID *)(UINTN)HobPointer.Capsule->BaseAddress)) {
|
---|
243 | CapsuleNameCapsuleTotalNumber++;
|
---|
244 | } else {
|
---|
245 | mCapsuleTotalNumber++;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | HobPointer.Raw = GET_NEXT_HOB (HobPointer);
|
---|
250 | }
|
---|
251 |
|
---|
252 | DEBUG ((DEBUG_INFO, "mCapsuleTotalNumber - 0x%x\n", mCapsuleTotalNumber));
|
---|
253 |
|
---|
254 | if (mCapsuleTotalNumber == 0) {
|
---|
255 | return;
|
---|
256 | }
|
---|
257 |
|
---|
258 | //
|
---|
259 | // Init temp Capsule Data table.
|
---|
260 | //
|
---|
261 | mCapsulePtr = (VOID **)AllocateZeroPool (sizeof (VOID *) * mCapsuleTotalNumber);
|
---|
262 | if (mCapsulePtr == NULL) {
|
---|
263 | DEBUG ((DEBUG_ERROR, "Allocate mCapsulePtr fail!\n"));
|
---|
264 | mCapsuleTotalNumber = 0;
|
---|
265 | return;
|
---|
266 | }
|
---|
267 |
|
---|
268 | mCapsuleStatusArray = (EFI_STATUS *)AllocateZeroPool (sizeof (EFI_STATUS) * mCapsuleTotalNumber);
|
---|
269 | if (mCapsuleStatusArray == NULL) {
|
---|
270 | DEBUG ((DEBUG_ERROR, "Allocate mCapsuleStatusArray fail!\n"));
|
---|
271 | FreePool (mCapsulePtr);
|
---|
272 | mCapsulePtr = NULL;
|
---|
273 | mCapsuleTotalNumber = 0;
|
---|
274 | return;
|
---|
275 | }
|
---|
276 |
|
---|
277 | SetMemN (mCapsuleStatusArray, sizeof (EFI_STATUS) * mCapsuleTotalNumber, EFI_NOT_READY);
|
---|
278 |
|
---|
279 | CapsuleNameCapsulePtr = (VOID **)AllocateZeroPool (sizeof (VOID *) * CapsuleNameCapsuleTotalNumber);
|
---|
280 | if (CapsuleNameCapsulePtr == NULL) {
|
---|
281 | DEBUG ((DEBUG_ERROR, "Allocate CapsuleNameCapsulePtr fail!\n"));
|
---|
282 | FreePool (mCapsulePtr);
|
---|
283 | FreePool (mCapsuleStatusArray);
|
---|
284 | mCapsulePtr = NULL;
|
---|
285 | mCapsuleStatusArray = NULL;
|
---|
286 | mCapsuleTotalNumber = 0;
|
---|
287 | return;
|
---|
288 | }
|
---|
289 |
|
---|
290 | //
|
---|
291 | // Find all capsule images from hob
|
---|
292 | //
|
---|
293 | HobPointer.Raw = GetHobList ();
|
---|
294 | Index = 0;
|
---|
295 | Index2 = 0;
|
---|
296 | while ((HobPointer.Raw = GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobPointer.Raw)) != NULL) {
|
---|
297 | if (IsCapsuleNameCapsule ((VOID *)(UINTN)HobPointer.Capsule->BaseAddress)) {
|
---|
298 | CapsuleNameCapsulePtr[Index2++] = (VOID *)(UINTN)HobPointer.Capsule->BaseAddress;
|
---|
299 | } else {
|
---|
300 | mCapsulePtr[Index++] = (VOID *)(UINTN)HobPointer.Capsule->BaseAddress;
|
---|
301 | }
|
---|
302 |
|
---|
303 | HobPointer.Raw = GET_NEXT_HOB (HobPointer);
|
---|
304 | }
|
---|
305 |
|
---|
306 | //
|
---|
307 | // Find Capsule On Disk Names
|
---|
308 | //
|
---|
309 | for (Index = 0; Index < CapsuleNameCapsuleTotalNumber; Index++) {
|
---|
310 | CapsuleNameAddress = ValidateCapsuleNameCapsuleIntegrity (CapsuleNameCapsulePtr[Index], &CapsuleNameNumber);
|
---|
311 | if (CapsuleNameAddress != NULL ) {
|
---|
312 | CapsuleNameTotalNumber += CapsuleNameNumber;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | if (CapsuleNameTotalNumber == mCapsuleTotalNumber) {
|
---|
317 | mCapsuleNamePtr = (CHAR16 **)AllocateZeroPool (sizeof (CHAR16 *) * mCapsuleTotalNumber);
|
---|
318 | if (mCapsuleNamePtr == NULL) {
|
---|
319 | DEBUG ((DEBUG_ERROR, "Allocate mCapsuleNamePtr fail!\n"));
|
---|
320 | FreePool (mCapsulePtr);
|
---|
321 | FreePool (mCapsuleStatusArray);
|
---|
322 | FreePool (CapsuleNameCapsulePtr);
|
---|
323 | mCapsulePtr = NULL;
|
---|
324 | mCapsuleStatusArray = NULL;
|
---|
325 | mCapsuleTotalNumber = 0;
|
---|
326 | return;
|
---|
327 | }
|
---|
328 |
|
---|
329 | for (Index = 0, Index3 = 0; Index < CapsuleNameCapsuleTotalNumber; Index++) {
|
---|
330 | CapsuleNameAddress = ValidateCapsuleNameCapsuleIntegrity (CapsuleNameCapsulePtr[Index], &CapsuleNameNumber);
|
---|
331 | if (CapsuleNameAddress != NULL ) {
|
---|
332 | for (Index2 = 0; Index2 < CapsuleNameNumber; Index2++) {
|
---|
333 | mCapsuleNamePtr[Index3++] = (CHAR16 *)(UINTN)CapsuleNameAddress[Index2];
|
---|
334 | }
|
---|
335 | }
|
---|
336 | }
|
---|
337 | } else {
|
---|
338 | mCapsuleNamePtr = NULL;
|
---|
339 | }
|
---|
340 |
|
---|
341 | FreePool (CapsuleNameCapsulePtr);
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | This function returns if all capsule images are processed.
|
---|
346 |
|
---|
347 | @retval TRUE All capsule images are processed.
|
---|
348 | @retval FALSE Not all capsule images are processed.
|
---|
349 | **/
|
---|
350 | BOOLEAN
|
---|
351 | AreAllImagesProcessed (
|
---|
352 | VOID
|
---|
353 | )
|
---|
354 | {
|
---|
355 | UINTN Index;
|
---|
356 |
|
---|
357 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
358 | if (mCapsuleStatusArray[Index] == EFI_NOT_READY) {
|
---|
359 | return FALSE;
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | return TRUE;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | This function populates capsule in the configuration table.
|
---|
368 | **/
|
---|
369 | VOID
|
---|
370 | PopulateCapsuleInConfigurationTable (
|
---|
371 | VOID
|
---|
372 | )
|
---|
373 | {
|
---|
374 | VOID **CapsulePtrCache;
|
---|
375 | EFI_GUID *CapsuleGuidCache;
|
---|
376 | EFI_CAPSULE_HEADER *CapsuleHeader;
|
---|
377 | EFI_CAPSULE_TABLE *CapsuleTable;
|
---|
378 | UINT32 CacheIndex;
|
---|
379 | UINT32 CacheNumber;
|
---|
380 | UINT32 CapsuleNumber;
|
---|
381 | UINTN Index;
|
---|
382 | UINTN Size;
|
---|
383 | EFI_STATUS Status;
|
---|
384 |
|
---|
385 | if (mCapsuleTotalNumber == 0) {
|
---|
386 | return;
|
---|
387 | }
|
---|
388 |
|
---|
389 | CapsulePtrCache = NULL;
|
---|
390 | CapsuleGuidCache = NULL;
|
---|
391 | CacheIndex = 0;
|
---|
392 | CacheNumber = 0;
|
---|
393 |
|
---|
394 | CapsulePtrCache = (VOID **)AllocateZeroPool (sizeof (VOID *) * mCapsuleTotalNumber);
|
---|
395 | if (CapsulePtrCache == NULL) {
|
---|
396 | DEBUG ((DEBUG_ERROR, "Allocate CapsulePtrCache fail!\n"));
|
---|
397 | return;
|
---|
398 | }
|
---|
399 |
|
---|
400 | CapsuleGuidCache = (EFI_GUID *)AllocateZeroPool (sizeof (EFI_GUID) * mCapsuleTotalNumber);
|
---|
401 | if (CapsuleGuidCache == NULL) {
|
---|
402 | DEBUG ((DEBUG_ERROR, "Allocate CapsuleGuidCache fail!\n"));
|
---|
403 | FreePool (CapsulePtrCache);
|
---|
404 | return;
|
---|
405 | }
|
---|
406 |
|
---|
407 | //
|
---|
408 | // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating
|
---|
409 | // System to have information persist across a system reset. EFI System Table must
|
---|
410 | // point to an array of capsules that contains the same CapsuleGuid value. And agents
|
---|
411 | // searching for this type capsule will look in EFI System Table and search for the
|
---|
412 | // capsule's Guid and associated pointer to retrieve the data. Two steps below describes
|
---|
413 | // how to sorting the capsules by the unique guid and install the array to EFI System Table.
|
---|
414 | // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an
|
---|
415 | // array for later sorting capsules by CapsuleGuid.
|
---|
416 | //
|
---|
417 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
418 | CapsuleHeader = (EFI_CAPSULE_HEADER *)mCapsulePtr[Index];
|
---|
419 | if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
|
---|
420 | //
|
---|
421 | // For each capsule, we compare it with known CapsuleGuid in the CacheArray.
|
---|
422 | // If already has the Guid, skip it. Whereas, record it in the CacheArray as
|
---|
423 | // an additional one.
|
---|
424 | //
|
---|
425 | CacheIndex = 0;
|
---|
426 | while (CacheIndex < CacheNumber) {
|
---|
427 | if (CompareGuid (&CapsuleGuidCache[CacheIndex], &CapsuleHeader->CapsuleGuid)) {
|
---|
428 | break;
|
---|
429 | }
|
---|
430 |
|
---|
431 | CacheIndex++;
|
---|
432 | }
|
---|
433 |
|
---|
434 | if (CacheIndex == CacheNumber) {
|
---|
435 | CopyMem (&CapsuleGuidCache[CacheNumber++], &CapsuleHeader->CapsuleGuid, sizeof (EFI_GUID));
|
---|
436 | }
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | //
|
---|
441 | // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules
|
---|
442 | // whose guid is the same as it, and malloc memory for an array which preceding
|
---|
443 | // with UINT32. The array fills with entry point of capsules that have the same
|
---|
444 | // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install
|
---|
445 | // this array into EFI System Table, so that agents searching for this type capsule
|
---|
446 | // will look in EFI System Table and search for the capsule's Guid and associated
|
---|
447 | // pointer to retrieve the data.
|
---|
448 | //
|
---|
449 | for (CacheIndex = 0; CacheIndex < CacheNumber; CacheIndex++) {
|
---|
450 | CapsuleNumber = 0;
|
---|
451 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
452 | CapsuleHeader = (EFI_CAPSULE_HEADER *)mCapsulePtr[Index];
|
---|
453 | if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
|
---|
454 | if (CompareGuid (&CapsuleGuidCache[CacheIndex], &CapsuleHeader->CapsuleGuid)) {
|
---|
455 | //
|
---|
456 | // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
|
---|
457 | //
|
---|
458 | CapsulePtrCache[CapsuleNumber++] = (VOID *)CapsuleHeader;
|
---|
459 | }
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | if (CapsuleNumber != 0) {
|
---|
464 | Size = sizeof (EFI_CAPSULE_TABLE) + (CapsuleNumber - 1) * sizeof (VOID *);
|
---|
465 | CapsuleTable = AllocateRuntimePool (Size);
|
---|
466 | if (CapsuleTable == NULL) {
|
---|
467 | DEBUG ((DEBUG_ERROR, "Allocate CapsuleTable (%g) fail!\n", &CapsuleGuidCache[CacheIndex]));
|
---|
468 | continue;
|
---|
469 | }
|
---|
470 |
|
---|
471 | CapsuleTable->CapsuleArrayNumber = CapsuleNumber;
|
---|
472 | CopyMem (&CapsuleTable->CapsulePtr[0], CapsulePtrCache, CapsuleNumber * sizeof (VOID *));
|
---|
473 | Status = gBS->InstallConfigurationTable (&CapsuleGuidCache[CacheIndex], (VOID *)CapsuleTable);
|
---|
474 | if (EFI_ERROR (Status)) {
|
---|
475 | DEBUG ((DEBUG_ERROR, "InstallConfigurationTable (%g) fail!\n", &CapsuleGuidCache[CacheIndex]));
|
---|
476 | }
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | FreePool (CapsuleGuidCache);
|
---|
481 | FreePool (CapsulePtrCache);
|
---|
482 | }
|
---|
483 |
|
---|
484 | /**
|
---|
485 |
|
---|
486 | This routine is called to process capsules.
|
---|
487 |
|
---|
488 | Caution: This function may receive untrusted input.
|
---|
489 |
|
---|
490 | Each individual capsule result is recorded in capsule record variable.
|
---|
491 |
|
---|
492 | @param[in] FirstRound TRUE: First round. Need skip the FMP capsules with non zero EmbeddedDriverCount.
|
---|
493 | FALSE: Process rest FMP capsules.
|
---|
494 |
|
---|
495 | @retval EFI_SUCCESS There is no error when processing capsules.
|
---|
496 | @retval EFI_OUT_OF_RESOURCES No enough resource to process capsules.
|
---|
497 |
|
---|
498 | **/
|
---|
499 | EFI_STATUS
|
---|
500 | ProcessTheseCapsules (
|
---|
501 | IN BOOLEAN FirstRound
|
---|
502 | )
|
---|
503 | {
|
---|
504 | EFI_STATUS Status;
|
---|
505 | EFI_CAPSULE_HEADER *CapsuleHeader;
|
---|
506 | UINT32 Index;
|
---|
507 | ESRT_MANAGEMENT_PROTOCOL *EsrtManagement;
|
---|
508 | UINT16 EmbeddedDriverCount;
|
---|
509 | BOOLEAN ResetRequired;
|
---|
510 | CHAR16 *CapsuleName;
|
---|
511 |
|
---|
512 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32 (PcdStatusCodeSubClassCapsule) | PcdGet32 (PcdCapsuleStatusCodeProcessCapsulesBegin)));
|
---|
513 |
|
---|
514 | if (FirstRound) {
|
---|
515 | InitCapsulePtr ();
|
---|
516 | }
|
---|
517 |
|
---|
518 | if (mCapsuleTotalNumber == 0) {
|
---|
519 | //
|
---|
520 | // We didn't find a hob, so had no errors.
|
---|
521 | //
|
---|
522 | DEBUG ((DEBUG_ERROR, "We can not find capsule data in capsule update boot mode.\n"));
|
---|
523 | mNeedReset = TRUE;
|
---|
524 | return EFI_SUCCESS;
|
---|
525 | }
|
---|
526 |
|
---|
527 | if (AreAllImagesProcessed ()) {
|
---|
528 | return EFI_SUCCESS;
|
---|
529 | }
|
---|
530 |
|
---|
531 | //
|
---|
532 | // Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
|
---|
533 | // capsuleTable to configure table with EFI_CAPSULE_GUID
|
---|
534 | //
|
---|
535 | if (FirstRound) {
|
---|
536 | PopulateCapsuleInConfigurationTable ();
|
---|
537 | }
|
---|
538 |
|
---|
539 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32 (PcdStatusCodeSubClassCapsule) | PcdGet32 (PcdCapsuleStatusCodeUpdatingFirmware)));
|
---|
540 |
|
---|
541 | //
|
---|
542 | // If Windows UX capsule exist, process it first
|
---|
543 | //
|
---|
544 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
545 | CapsuleHeader = (EFI_CAPSULE_HEADER *)mCapsulePtr[Index];
|
---|
546 | CapsuleName = (mCapsuleNamePtr == NULL) ? NULL : mCapsuleNamePtr[Index];
|
---|
547 | if (CompareGuid (&CapsuleHeader->CapsuleGuid, &gWindowsUxCapsuleGuid)) {
|
---|
548 | DEBUG ((DEBUG_INFO, "ProcessThisCapsuleImage (Ux) - 0x%x\n", CapsuleHeader));
|
---|
549 | DEBUG ((DEBUG_INFO, "Display logo capsule is found.\n"));
|
---|
550 | Status = ProcessThisCapsuleImage (CapsuleHeader, CapsuleName, NULL);
|
---|
551 | mCapsuleStatusArray[Index] = EFI_SUCCESS;
|
---|
552 | DEBUG ((DEBUG_INFO, "ProcessThisCapsuleImage (Ux) - %r\n", Status));
|
---|
553 | break;
|
---|
554 | }
|
---|
555 | }
|
---|
556 |
|
---|
557 | DEBUG ((DEBUG_INFO, "Updating the firmware ......\n"));
|
---|
558 |
|
---|
559 | //
|
---|
560 | // All capsules left are recognized by platform.
|
---|
561 | //
|
---|
562 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
563 | if (mCapsuleStatusArray[Index] != EFI_NOT_READY) {
|
---|
564 | // already processed
|
---|
565 | continue;
|
---|
566 | }
|
---|
567 |
|
---|
568 | CapsuleHeader = (EFI_CAPSULE_HEADER *)mCapsulePtr[Index];
|
---|
569 | CapsuleName = (mCapsuleNamePtr == NULL) ? NULL : mCapsuleNamePtr[Index];
|
---|
570 | if (!CompareGuid (&CapsuleHeader->CapsuleGuid, &gWindowsUxCapsuleGuid)) {
|
---|
571 | //
|
---|
572 | // Call capsule library to process capsule image.
|
---|
573 | //
|
---|
574 | EmbeddedDriverCount = 0;
|
---|
575 | if (IsFmpCapsule (CapsuleHeader)) {
|
---|
576 | Status = ValidateFmpCapsule (CapsuleHeader, &EmbeddedDriverCount);
|
---|
577 | if (EFI_ERROR (Status)) {
|
---|
578 | DEBUG ((DEBUG_ERROR, "ValidateFmpCapsule failed. Ignore!\n"));
|
---|
579 | mCapsuleStatusArray[Index] = EFI_ABORTED;
|
---|
580 | continue;
|
---|
581 | }
|
---|
582 | } else {
|
---|
583 | mCapsuleStatusArray[Index] = EFI_ABORTED;
|
---|
584 | continue;
|
---|
585 | }
|
---|
586 |
|
---|
587 | if ((!FirstRound) || (EmbeddedDriverCount == 0)) {
|
---|
588 | DEBUG ((DEBUG_INFO, "ProcessThisCapsuleImage - 0x%x\n", CapsuleHeader));
|
---|
589 | ResetRequired = FALSE;
|
---|
590 | Status = ProcessThisCapsuleImage (CapsuleHeader, CapsuleName, &ResetRequired);
|
---|
591 | mCapsuleStatusArray[Index] = Status;
|
---|
592 | DEBUG ((DEBUG_INFO, "ProcessThisCapsuleImage - %r\n", Status));
|
---|
593 |
|
---|
594 | if (Status != EFI_NOT_READY) {
|
---|
595 | if (EFI_ERROR (Status)) {
|
---|
596 | REPORT_STATUS_CODE (EFI_ERROR_CODE, (EFI_SOFTWARE | PcdGet32 (PcdStatusCodeSubClassCapsule) | PcdGet32 (PcdCapsuleStatusCodeUpdateFirmwareFailed)));
|
---|
597 | DEBUG ((DEBUG_ERROR, "Capsule process failed!\n"));
|
---|
598 | } else {
|
---|
599 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32 (PcdStatusCodeSubClassCapsule) | PcdGet32 (PcdCapsuleStatusCodeUpdateFirmwareSuccess)));
|
---|
600 | }
|
---|
601 |
|
---|
602 | mNeedReset |= ResetRequired;
|
---|
603 | if ((CapsuleHeader->Flags & PcdGet16 (PcdSystemRebootAfterCapsuleProcessFlag)) != 0) {
|
---|
604 | mNeedReset = TRUE;
|
---|
605 | }
|
---|
606 | }
|
---|
607 | }
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | Status = gBS->LocateProtocol (&gEsrtManagementProtocolGuid, NULL, (VOID **)&EsrtManagement);
|
---|
612 | //
|
---|
613 | // Always sync ESRT Cache from FMP Instance
|
---|
614 | //
|
---|
615 | if (!EFI_ERROR (Status)) {
|
---|
616 | EsrtManagement->SyncEsrtFmp ();
|
---|
617 | }
|
---|
618 |
|
---|
619 | Status = EFI_SUCCESS;
|
---|
620 |
|
---|
621 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32 (PcdStatusCodeSubClassCapsule) | PcdGet32 (PcdCapsuleStatusCodeProcessCapsulesEnd)));
|
---|
622 |
|
---|
623 | return Status;
|
---|
624 | }
|
---|
625 |
|
---|
626 | /**
|
---|
627 | Do reset system.
|
---|
628 | **/
|
---|
629 | VOID
|
---|
630 | DoResetSystem (
|
---|
631 | VOID
|
---|
632 | )
|
---|
633 | {
|
---|
634 | DEBUG ((DEBUG_INFO, "Capsule Request Cold Reboot."));
|
---|
635 |
|
---|
636 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32 (PcdStatusCodeSubClassCapsule) | PcdGet32 (PcdCapsuleStatusCodeResettingSystem)));
|
---|
637 |
|
---|
638 | gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);
|
---|
639 |
|
---|
640 | CpuDeadLoop ();
|
---|
641 | }
|
---|
642 |
|
---|
643 | /**
|
---|
644 |
|
---|
645 | This routine is called to process capsules.
|
---|
646 |
|
---|
647 | Caution: This function may receive untrusted input.
|
---|
648 |
|
---|
649 | The capsules reported in EFI_HOB_UEFI_CAPSULE are processed.
|
---|
650 | If there is no EFI_HOB_UEFI_CAPSULE, it means error occurs, force reset to
|
---|
651 | normal boot path.
|
---|
652 |
|
---|
653 | This routine should be called twice in BDS.
|
---|
654 | 1) The first call must be before EndOfDxe. The system capsules is processed.
|
---|
655 | If device capsule FMP protocols are exposted at this time and device FMP
|
---|
656 | capsule has zero EmbeddedDriverCount, the device capsules are processed.
|
---|
657 | Each individual capsule result is recorded in capsule record variable.
|
---|
658 | System may reset in this function, if reset is required by capsule and
|
---|
659 | all capsules are processed.
|
---|
660 | If not all capsules are processed, reset will be defered to second call.
|
---|
661 |
|
---|
662 | 2) The second call must be after EndOfDxe and after ConnectAll, so that all
|
---|
663 | device capsule FMP protocols are exposed.
|
---|
664 | The system capsules are skipped. If the device capsules are NOT processed
|
---|
665 | in first call, they are processed here.
|
---|
666 | Each individual capsule result is recorded in capsule record variable.
|
---|
667 | System may reset in this function, if reset is required by capsule
|
---|
668 | processed in first call and second call.
|
---|
669 |
|
---|
670 | @retval EFI_SUCCESS There is no error when processing capsules.
|
---|
671 | @retval EFI_OUT_OF_RESOURCES No enough resource to process capsules.
|
---|
672 |
|
---|
673 | **/
|
---|
674 | EFI_STATUS
|
---|
675 | EFIAPI
|
---|
676 | ProcessCapsules (
|
---|
677 | VOID
|
---|
678 | )
|
---|
679 | {
|
---|
680 | EFI_STATUS Status;
|
---|
681 |
|
---|
682 | if (!mDxeCapsuleLibEndOfDxe) {
|
---|
683 | Status = ProcessTheseCapsules (TRUE);
|
---|
684 |
|
---|
685 | //
|
---|
686 | // Reboot System if and only if all capsule processed.
|
---|
687 | // If not, defer reset to 2nd process.
|
---|
688 | //
|
---|
689 | if (mNeedReset && AreAllImagesProcessed ()) {
|
---|
690 | DoResetSystem ();
|
---|
691 | }
|
---|
692 | } else {
|
---|
693 | Status = ProcessTheseCapsules (FALSE);
|
---|
694 | //
|
---|
695 | // Reboot System if required after all capsule processed
|
---|
696 | //
|
---|
697 | if (mNeedReset) {
|
---|
698 | DoResetSystem ();
|
---|
699 | }
|
---|
700 | }
|
---|
701 |
|
---|
702 | return Status;
|
---|
703 | }
|
---|