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 | HobPointer.Raw = GET_NEXT_HOB (HobPointer);
|
---|
249 | }
|
---|
250 |
|
---|
251 | DEBUG ((DEBUG_INFO, "mCapsuleTotalNumber - 0x%x\n", mCapsuleTotalNumber));
|
---|
252 |
|
---|
253 | if (mCapsuleTotalNumber == 0) {
|
---|
254 | return ;
|
---|
255 | }
|
---|
256 |
|
---|
257 | //
|
---|
258 | // Init temp Capsule Data table.
|
---|
259 | //
|
---|
260 | mCapsulePtr = (VOID **) AllocateZeroPool (sizeof (VOID *) * mCapsuleTotalNumber);
|
---|
261 | if (mCapsulePtr == NULL) {
|
---|
262 | DEBUG ((DEBUG_ERROR, "Allocate mCapsulePtr fail!\n"));
|
---|
263 | mCapsuleTotalNumber = 0;
|
---|
264 | return ;
|
---|
265 | }
|
---|
266 | mCapsuleStatusArray = (EFI_STATUS *) AllocateZeroPool (sizeof (EFI_STATUS) * mCapsuleTotalNumber);
|
---|
267 | if (mCapsuleStatusArray == NULL) {
|
---|
268 | DEBUG ((DEBUG_ERROR, "Allocate mCapsuleStatusArray fail!\n"));
|
---|
269 | FreePool (mCapsulePtr);
|
---|
270 | mCapsulePtr = NULL;
|
---|
271 | mCapsuleTotalNumber = 0;
|
---|
272 | return ;
|
---|
273 | }
|
---|
274 | SetMemN (mCapsuleStatusArray, sizeof (EFI_STATUS) * mCapsuleTotalNumber, EFI_NOT_READY);
|
---|
275 |
|
---|
276 | CapsuleNameCapsulePtr = (VOID **) AllocateZeroPool (sizeof (VOID *) * CapsuleNameCapsuleTotalNumber);
|
---|
277 | if (CapsuleNameCapsulePtr == NULL) {
|
---|
278 | DEBUG ((DEBUG_ERROR, "Allocate CapsuleNameCapsulePtr fail!\n"));
|
---|
279 | FreePool (mCapsulePtr);
|
---|
280 | FreePool (mCapsuleStatusArray);
|
---|
281 | mCapsulePtr = NULL;
|
---|
282 | mCapsuleStatusArray = NULL;
|
---|
283 | mCapsuleTotalNumber = 0;
|
---|
284 | return ;
|
---|
285 | }
|
---|
286 |
|
---|
287 | //
|
---|
288 | // Find all capsule images from hob
|
---|
289 | //
|
---|
290 | HobPointer.Raw = GetHobList ();
|
---|
291 | Index = 0;
|
---|
292 | Index2 = 0;
|
---|
293 | while ((HobPointer.Raw = GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobPointer.Raw)) != NULL) {
|
---|
294 | if (IsCapsuleNameCapsule ((VOID *) (UINTN) HobPointer.Capsule->BaseAddress)) {
|
---|
295 | CapsuleNameCapsulePtr [Index2++] = (VOID *) (UINTN) HobPointer.Capsule->BaseAddress;
|
---|
296 | } else {
|
---|
297 | mCapsulePtr [Index++] = (VOID *) (UINTN) HobPointer.Capsule->BaseAddress;
|
---|
298 | }
|
---|
299 | HobPointer.Raw = GET_NEXT_HOB (HobPointer);
|
---|
300 | }
|
---|
301 |
|
---|
302 | //
|
---|
303 | // Find Capsule On Disk Names
|
---|
304 | //
|
---|
305 | for (Index = 0; Index < CapsuleNameCapsuleTotalNumber; Index ++) {
|
---|
306 | CapsuleNameAddress = ValidateCapsuleNameCapsuleIntegrity (CapsuleNameCapsulePtr[Index], &CapsuleNameNumber);
|
---|
307 | if (CapsuleNameAddress != NULL ) {
|
---|
308 | CapsuleNameTotalNumber += CapsuleNameNumber;
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | if (CapsuleNameTotalNumber == mCapsuleTotalNumber) {
|
---|
313 | mCapsuleNamePtr = (CHAR16 **) AllocateZeroPool (sizeof (CHAR16 *) * mCapsuleTotalNumber);
|
---|
314 | if (mCapsuleNamePtr == NULL) {
|
---|
315 | DEBUG ((DEBUG_ERROR, "Allocate mCapsuleNamePtr fail!\n"));
|
---|
316 | FreePool (mCapsulePtr);
|
---|
317 | FreePool (mCapsuleStatusArray);
|
---|
318 | FreePool (CapsuleNameCapsulePtr);
|
---|
319 | mCapsulePtr = NULL;
|
---|
320 | mCapsuleStatusArray = NULL;
|
---|
321 | mCapsuleTotalNumber = 0;
|
---|
322 | return ;
|
---|
323 | }
|
---|
324 |
|
---|
325 | for (Index = 0, Index3 = 0; Index < CapsuleNameCapsuleTotalNumber; Index ++) {
|
---|
326 | CapsuleNameAddress = ValidateCapsuleNameCapsuleIntegrity (CapsuleNameCapsulePtr[Index], &CapsuleNameNumber);
|
---|
327 | if (CapsuleNameAddress != NULL ) {
|
---|
328 | for (Index2 = 0; Index2 < CapsuleNameNumber; Index2 ++) {
|
---|
329 | mCapsuleNamePtr[Index3 ++] = (CHAR16 *)(UINTN) CapsuleNameAddress[Index2];
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 | } else {
|
---|
334 | mCapsuleNamePtr = NULL;
|
---|
335 | }
|
---|
336 |
|
---|
337 | FreePool (CapsuleNameCapsulePtr);
|
---|
338 | }
|
---|
339 |
|
---|
340 | /**
|
---|
341 | This function returns if all capsule images are processed.
|
---|
342 |
|
---|
343 | @retval TRUE All capsule images are processed.
|
---|
344 | @retval FALSE Not all capsule images are processed.
|
---|
345 | **/
|
---|
346 | BOOLEAN
|
---|
347 | AreAllImagesProcessed (
|
---|
348 | VOID
|
---|
349 | )
|
---|
350 | {
|
---|
351 | UINTN Index;
|
---|
352 |
|
---|
353 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
354 | if (mCapsuleStatusArray[Index] == EFI_NOT_READY) {
|
---|
355 | return FALSE;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | return TRUE;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | This function populates capsule in the configuration table.
|
---|
364 | **/
|
---|
365 | VOID
|
---|
366 | PopulateCapsuleInConfigurationTable (
|
---|
367 | VOID
|
---|
368 | )
|
---|
369 | {
|
---|
370 | VOID **CapsulePtrCache;
|
---|
371 | EFI_GUID *CapsuleGuidCache;
|
---|
372 | EFI_CAPSULE_HEADER *CapsuleHeader;
|
---|
373 | EFI_CAPSULE_TABLE *CapsuleTable;
|
---|
374 | UINT32 CacheIndex;
|
---|
375 | UINT32 CacheNumber;
|
---|
376 | UINT32 CapsuleNumber;
|
---|
377 | UINTN Index;
|
---|
378 | UINTN Size;
|
---|
379 | EFI_STATUS Status;
|
---|
380 |
|
---|
381 | if (mCapsuleTotalNumber == 0) {
|
---|
382 | return ;
|
---|
383 | }
|
---|
384 |
|
---|
385 | CapsulePtrCache = NULL;
|
---|
386 | CapsuleGuidCache = NULL;
|
---|
387 | CacheIndex = 0;
|
---|
388 | CacheNumber = 0;
|
---|
389 |
|
---|
390 | CapsulePtrCache = (VOID **) AllocateZeroPool (sizeof (VOID *) * mCapsuleTotalNumber);
|
---|
391 | if (CapsulePtrCache == NULL) {
|
---|
392 | DEBUG ((DEBUG_ERROR, "Allocate CapsulePtrCache fail!\n"));
|
---|
393 | return ;
|
---|
394 | }
|
---|
395 | CapsuleGuidCache = (EFI_GUID *) AllocateZeroPool (sizeof (EFI_GUID) * mCapsuleTotalNumber);
|
---|
396 | if (CapsuleGuidCache == NULL) {
|
---|
397 | DEBUG ((DEBUG_ERROR, "Allocate CapsuleGuidCache fail!\n"));
|
---|
398 | FreePool (CapsulePtrCache);
|
---|
399 | return ;
|
---|
400 | }
|
---|
401 |
|
---|
402 | //
|
---|
403 | // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating
|
---|
404 | // System to have information persist across a system reset. EFI System Table must
|
---|
405 | // point to an array of capsules that contains the same CapsuleGuid value. And agents
|
---|
406 | // searching for this type capsule will look in EFI System Table and search for the
|
---|
407 | // capsule's Guid and associated pointer to retrieve the data. Two steps below describes
|
---|
408 | // how to sorting the capsules by the unique guid and install the array to EFI System Table.
|
---|
409 | // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an
|
---|
410 | // array for later sorting capsules by CapsuleGuid.
|
---|
411 | //
|
---|
412 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
413 | CapsuleHeader = (EFI_CAPSULE_HEADER*) mCapsulePtr [Index];
|
---|
414 | if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
|
---|
415 | //
|
---|
416 | // For each capsule, we compare it with known CapsuleGuid in the CacheArray.
|
---|
417 | // If already has the Guid, skip it. Whereas, record it in the CacheArray as
|
---|
418 | // an additional one.
|
---|
419 | //
|
---|
420 | CacheIndex = 0;
|
---|
421 | while (CacheIndex < CacheNumber) {
|
---|
422 | if (CompareGuid(&CapsuleGuidCache[CacheIndex],&CapsuleHeader->CapsuleGuid)) {
|
---|
423 | break;
|
---|
424 | }
|
---|
425 | CacheIndex++;
|
---|
426 | }
|
---|
427 | if (CacheIndex == CacheNumber) {
|
---|
428 | CopyMem(&CapsuleGuidCache[CacheNumber++],&CapsuleHeader->CapsuleGuid,sizeof(EFI_GUID));
|
---|
429 | }
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | //
|
---|
434 | // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules
|
---|
435 | // whose guid is the same as it, and malloc memory for an array which preceding
|
---|
436 | // with UINT32. The array fills with entry point of capsules that have the same
|
---|
437 | // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install
|
---|
438 | // this array into EFI System Table, so that agents searching for this type capsule
|
---|
439 | // will look in EFI System Table and search for the capsule's Guid and associated
|
---|
440 | // pointer to retrieve the data.
|
---|
441 | //
|
---|
442 | for (CacheIndex = 0; CacheIndex < CacheNumber; CacheIndex++) {
|
---|
443 | CapsuleNumber = 0;
|
---|
444 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
445 | CapsuleHeader = (EFI_CAPSULE_HEADER*) mCapsulePtr [Index];
|
---|
446 | if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
|
---|
447 | if (CompareGuid (&CapsuleGuidCache[CacheIndex], &CapsuleHeader->CapsuleGuid)) {
|
---|
448 | //
|
---|
449 | // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
|
---|
450 | //
|
---|
451 | CapsulePtrCache[CapsuleNumber++] = (VOID*)CapsuleHeader;
|
---|
452 | }
|
---|
453 | }
|
---|
454 | }
|
---|
455 | if (CapsuleNumber != 0) {
|
---|
456 | Size = sizeof(EFI_CAPSULE_TABLE) + (CapsuleNumber - 1) * sizeof(VOID*);
|
---|
457 | CapsuleTable = AllocateRuntimePool (Size);
|
---|
458 | if (CapsuleTable == NULL) {
|
---|
459 | DEBUG ((DEBUG_ERROR, "Allocate CapsuleTable (%g) fail!\n", &CapsuleGuidCache[CacheIndex]));
|
---|
460 | continue;
|
---|
461 | }
|
---|
462 | CapsuleTable->CapsuleArrayNumber = CapsuleNumber;
|
---|
463 | CopyMem(&CapsuleTable->CapsulePtr[0], CapsulePtrCache, CapsuleNumber * sizeof(VOID*));
|
---|
464 | Status = gBS->InstallConfigurationTable (&CapsuleGuidCache[CacheIndex], (VOID*)CapsuleTable);
|
---|
465 | if (EFI_ERROR (Status)) {
|
---|
466 | DEBUG ((DEBUG_ERROR, "InstallConfigurationTable (%g) fail!\n", &CapsuleGuidCache[CacheIndex]));
|
---|
467 | }
|
---|
468 | }
|
---|
469 | }
|
---|
470 |
|
---|
471 | FreePool(CapsuleGuidCache);
|
---|
472 | FreePool(CapsulePtrCache);
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 |
|
---|
477 | This routine is called to process capsules.
|
---|
478 |
|
---|
479 | Caution: This function may receive untrusted input.
|
---|
480 |
|
---|
481 | Each individual capsule result is recorded in capsule record variable.
|
---|
482 |
|
---|
483 | @param[in] FirstRound TRUE: First round. Need skip the FMP capsules with non zero EmbeddedDriverCount.
|
---|
484 | FALSE: Process rest FMP capsules.
|
---|
485 |
|
---|
486 | @retval EFI_SUCCESS There is no error when processing capsules.
|
---|
487 | @retval EFI_OUT_OF_RESOURCES No enough resource to process capsules.
|
---|
488 |
|
---|
489 | **/
|
---|
490 | EFI_STATUS
|
---|
491 | ProcessTheseCapsules (
|
---|
492 | IN BOOLEAN FirstRound
|
---|
493 | )
|
---|
494 | {
|
---|
495 | EFI_STATUS Status;
|
---|
496 | EFI_CAPSULE_HEADER *CapsuleHeader;
|
---|
497 | UINT32 Index;
|
---|
498 | ESRT_MANAGEMENT_PROTOCOL *EsrtManagement;
|
---|
499 | UINT16 EmbeddedDriverCount;
|
---|
500 | BOOLEAN ResetRequired;
|
---|
501 | CHAR16 *CapsuleName;
|
---|
502 |
|
---|
503 | REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32(PcdStatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeProcessCapsulesBegin)));
|
---|
504 |
|
---|
505 | if (FirstRound) {
|
---|
506 | InitCapsulePtr ();
|
---|
507 | }
|
---|
508 |
|
---|
509 | if (mCapsuleTotalNumber == 0) {
|
---|
510 | //
|
---|
511 | // We didn't find a hob, so had no errors.
|
---|
512 | //
|
---|
513 | DEBUG ((DEBUG_ERROR, "We can not find capsule data in capsule update boot mode.\n"));
|
---|
514 | mNeedReset = TRUE;
|
---|
515 | return EFI_SUCCESS;
|
---|
516 | }
|
---|
517 |
|
---|
518 | if (AreAllImagesProcessed ()) {
|
---|
519 | return EFI_SUCCESS;
|
---|
520 | }
|
---|
521 |
|
---|
522 | //
|
---|
523 | // Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
|
---|
524 | // capsuleTable to configure table with EFI_CAPSULE_GUID
|
---|
525 | //
|
---|
526 | if (FirstRound) {
|
---|
527 | PopulateCapsuleInConfigurationTable ();
|
---|
528 | }
|
---|
529 |
|
---|
530 | REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32(PcdStatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeUpdatingFirmware)));
|
---|
531 |
|
---|
532 | //
|
---|
533 | // If Windows UX capsule exist, process it first
|
---|
534 | //
|
---|
535 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
536 | CapsuleHeader = (EFI_CAPSULE_HEADER*) mCapsulePtr [Index];
|
---|
537 | CapsuleName = (mCapsuleNamePtr == NULL) ? NULL : mCapsuleNamePtr[Index];
|
---|
538 | if (CompareGuid (&CapsuleHeader->CapsuleGuid, &gWindowsUxCapsuleGuid)) {
|
---|
539 | DEBUG ((DEBUG_INFO, "ProcessThisCapsuleImage (Ux) - 0x%x\n", CapsuleHeader));
|
---|
540 | DEBUG ((DEBUG_INFO, "Display logo capsule is found.\n"));
|
---|
541 | Status = ProcessThisCapsuleImage (CapsuleHeader, CapsuleName, NULL);
|
---|
542 | mCapsuleStatusArray [Index] = EFI_SUCCESS;
|
---|
543 | DEBUG((DEBUG_INFO, "ProcessThisCapsuleImage (Ux) - %r\n", Status));
|
---|
544 | break;
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | DEBUG ((DEBUG_INFO, "Updating the firmware ......\n"));
|
---|
549 |
|
---|
550 | //
|
---|
551 | // All capsules left are recognized by platform.
|
---|
552 | //
|
---|
553 | for (Index = 0; Index < mCapsuleTotalNumber; Index++) {
|
---|
554 | if (mCapsuleStatusArray [Index] != EFI_NOT_READY) {
|
---|
555 | // already processed
|
---|
556 | continue;
|
---|
557 | }
|
---|
558 | CapsuleHeader = (EFI_CAPSULE_HEADER*) mCapsulePtr [Index];
|
---|
559 | CapsuleName = (mCapsuleNamePtr == NULL) ? NULL : mCapsuleNamePtr[Index];
|
---|
560 | if (!CompareGuid (&CapsuleHeader->CapsuleGuid, &gWindowsUxCapsuleGuid)) {
|
---|
561 | //
|
---|
562 | // Call capsule library to process capsule image.
|
---|
563 | //
|
---|
564 | EmbeddedDriverCount = 0;
|
---|
565 | if (IsFmpCapsule(CapsuleHeader)) {
|
---|
566 | Status = ValidateFmpCapsule (CapsuleHeader, &EmbeddedDriverCount);
|
---|
567 | if (EFI_ERROR(Status)) {
|
---|
568 | DEBUG((DEBUG_ERROR, "ValidateFmpCapsule failed. Ignore!\n"));
|
---|
569 | mCapsuleStatusArray [Index] = EFI_ABORTED;
|
---|
570 | continue;
|
---|
571 | }
|
---|
572 | } else {
|
---|
573 | mCapsuleStatusArray [Index] = EFI_ABORTED;
|
---|
574 | continue;
|
---|
575 | }
|
---|
576 |
|
---|
577 | if ((!FirstRound) || (EmbeddedDriverCount == 0)) {
|
---|
578 | DEBUG((DEBUG_INFO, "ProcessThisCapsuleImage - 0x%x\n", CapsuleHeader));
|
---|
579 | ResetRequired = FALSE;
|
---|
580 | Status = ProcessThisCapsuleImage (CapsuleHeader, CapsuleName, &ResetRequired);
|
---|
581 | mCapsuleStatusArray [Index] = Status;
|
---|
582 | DEBUG((DEBUG_INFO, "ProcessThisCapsuleImage - %r\n", Status));
|
---|
583 |
|
---|
584 | if (Status != EFI_NOT_READY) {
|
---|
585 | if (EFI_ERROR(Status)) {
|
---|
586 | REPORT_STATUS_CODE(EFI_ERROR_CODE, (EFI_SOFTWARE | PcdGet32(PcdStatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeUpdateFirmwareFailed)));
|
---|
587 | DEBUG ((DEBUG_ERROR, "Capsule process failed!\n"));
|
---|
588 | } else {
|
---|
589 | REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32(PcdStatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeUpdateFirmwareSuccess)));
|
---|
590 | }
|
---|
591 |
|
---|
592 | mNeedReset |= ResetRequired;
|
---|
593 | if ((CapsuleHeader->Flags & PcdGet16(PcdSystemRebootAfterCapsuleProcessFlag)) != 0) {
|
---|
594 | mNeedReset = TRUE;
|
---|
595 | }
|
---|
596 | }
|
---|
597 | }
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | Status = gBS->LocateProtocol(&gEsrtManagementProtocolGuid, NULL, (VOID **)&EsrtManagement);
|
---|
602 | //
|
---|
603 | // Always sync ESRT Cache from FMP Instance
|
---|
604 | //
|
---|
605 | if (!EFI_ERROR(Status)) {
|
---|
606 | EsrtManagement->SyncEsrtFmp();
|
---|
607 | }
|
---|
608 | Status = EFI_SUCCESS;
|
---|
609 |
|
---|
610 | REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32(PcdStatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeProcessCapsulesEnd)));
|
---|
611 |
|
---|
612 | return Status;
|
---|
613 | }
|
---|
614 |
|
---|
615 | /**
|
---|
616 | Do reset system.
|
---|
617 | **/
|
---|
618 | VOID
|
---|
619 | DoResetSystem (
|
---|
620 | VOID
|
---|
621 | )
|
---|
622 | {
|
---|
623 | DEBUG((DEBUG_INFO, "Capsule Request Cold Reboot."));
|
---|
624 |
|
---|
625 | REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32(PcdStatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeResettingSystem)));
|
---|
626 |
|
---|
627 | gRT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
|
---|
628 |
|
---|
629 | CpuDeadLoop();
|
---|
630 | }
|
---|
631 |
|
---|
632 | /**
|
---|
633 |
|
---|
634 | This routine is called to process capsules.
|
---|
635 |
|
---|
636 | Caution: This function may receive untrusted input.
|
---|
637 |
|
---|
638 | The capsules reported in EFI_HOB_UEFI_CAPSULE are processed.
|
---|
639 | If there is no EFI_HOB_UEFI_CAPSULE, it means error occurs, force reset to
|
---|
640 | normal boot path.
|
---|
641 |
|
---|
642 | This routine should be called twice in BDS.
|
---|
643 | 1) The first call must be before EndOfDxe. The system capsules is processed.
|
---|
644 | If device capsule FMP protocols are exposted at this time and device FMP
|
---|
645 | capsule has zero EmbeddedDriverCount, the device capsules are processed.
|
---|
646 | Each individual capsule result is recorded in capsule record variable.
|
---|
647 | System may reset in this function, if reset is required by capsule and
|
---|
648 | all capsules are processed.
|
---|
649 | If not all capsules are processed, reset will be defered to second call.
|
---|
650 |
|
---|
651 | 2) The second call must be after EndOfDxe and after ConnectAll, so that all
|
---|
652 | device capsule FMP protocols are exposed.
|
---|
653 | The system capsules are skipped. If the device capsules are NOT processed
|
---|
654 | in first call, they are processed here.
|
---|
655 | Each individual capsule result is recorded in capsule record variable.
|
---|
656 | System may reset in this function, if reset is required by capsule
|
---|
657 | processed in first call and second call.
|
---|
658 |
|
---|
659 | @retval EFI_SUCCESS There is no error when processing capsules.
|
---|
660 | @retval EFI_OUT_OF_RESOURCES No enough resource to process capsules.
|
---|
661 |
|
---|
662 | **/
|
---|
663 | EFI_STATUS
|
---|
664 | EFIAPI
|
---|
665 | ProcessCapsules (
|
---|
666 | VOID
|
---|
667 | )
|
---|
668 | {
|
---|
669 | EFI_STATUS Status;
|
---|
670 |
|
---|
671 | if (!mDxeCapsuleLibEndOfDxe) {
|
---|
672 | Status = ProcessTheseCapsules(TRUE);
|
---|
673 |
|
---|
674 | //
|
---|
675 | // Reboot System if and only if all capsule processed.
|
---|
676 | // If not, defer reset to 2nd process.
|
---|
677 | //
|
---|
678 | if (mNeedReset && AreAllImagesProcessed()) {
|
---|
679 | DoResetSystem();
|
---|
680 | }
|
---|
681 | } else {
|
---|
682 | Status = ProcessTheseCapsules(FALSE);
|
---|
683 | //
|
---|
684 | // Reboot System if required after all capsule processed
|
---|
685 | //
|
---|
686 | if (mNeedReset) {
|
---|
687 | DoResetSystem();
|
---|
688 | }
|
---|
689 | }
|
---|
690 | return Status;
|
---|
691 | }
|
---|