SignedCapsulePkg SystemFirmwareUpdateDxe: Fix failure caused by d69d922
[mirror_edk2.git] / SignedCapsulePkg / Universal / SystemFirmwareUpdate / SystemFirmwareUpdateDxe.c
CommitLineData
f6f91d38
JY
1/** @file\r
2 SetImage instance to update system firmware.\r
3\r
4 Caution: This module requires additional review when modified.\r
5 This module will have external input - capsule image.\r
6 This external input must be validated carefully to avoid security issue like\r
7 buffer overflow, integer overflow.\r
8\r
9 FmpSetImage() will receive untrusted input and do basic validation.\r
10\r
d69d9227 11 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
f6f91d38
JY
12 This program and the accompanying materials\r
13 are licensed and made available under the terms and conditions of the BSD License\r
14 which accompanies this distribution. The full text of the license may be found at\r
15 http://opensource.org/licenses/bsd-license.php\r
16\r
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
19\r
20**/\r
21\r
22#include "SystemFirmwareDxe.h"\r
23\r
24//\r
25// SystemFmp driver private data\r
26//\r
27SYSTEM_FMP_PRIVATE_DATA *mSystemFmpPrivate = NULL;\r
28\r
29EFI_GUID mCurrentImageTypeId;\r
30\r
31BOOLEAN mNvRamUpdated = FALSE;\r
32\r
33/**\r
34 Parse Config data file to get the updated data array.\r
35\r
36 @param[in] DataBuffer Config raw file buffer.\r
37 @param[in] BufferSize Size of raw buffer.\r
38 @param[in, out] ConfigHeader Pointer to the config header.\r
39 @param[in, out] UpdateArray Pointer to the config of update data.\r
40\r
41 @retval EFI_NOT_FOUND No config data is found.\r
42 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
43 @retval EFI_SUCCESS Parse the config file successfully.\r
44\r
45**/\r
46EFI_STATUS\r
47ParseUpdateDataFile (\r
48 IN UINT8 *DataBuffer,\r
49 IN UINTN BufferSize,\r
50 IN OUT CONFIG_HEADER *ConfigHeader,\r
51 IN OUT UPDATE_CONFIG_DATA **UpdateArray\r
52 );\r
53\r
54/**\r
55 Update System Firmware image component.\r
56\r
57 @param[in] SystemFirmwareImage Points to the System Firmware image.\r
58 @param[in] SystemFirmwareImageSize The length of the System Firmware image in bytes.\r
59 @param[in] ConfigData Points to the component configuration structure.\r
60 @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
61 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
62\r
63 @retval EFI_SUCCESS The System Firmware image is updated.\r
64 @retval EFI_WRITE_PROTECTED The flash device is read only.\r
65**/\r
66EFI_STATUS\r
67PerformUpdate (\r
68 IN VOID *SystemFirmwareImage,\r
69 IN UINTN SystemFirmwareImageSize,\r
70 IN UPDATE_CONFIG_DATA *ConfigData,\r
71 OUT UINT32 *LastAttemptVersion,\r
72 OUT UINT32 *LastAttemptStatus\r
73 )\r
74{\r
75 EFI_STATUS Status;\r
76\r
77 DEBUG((DEBUG_INFO, "PlatformUpdate:"));\r
78 DEBUG((DEBUG_INFO, " BaseAddress - 0x%lx,", ConfigData->BaseAddress));\r
79 DEBUG((DEBUG_INFO, " ImageOffset - 0x%x,", ConfigData->ImageOffset));\r
80 DEBUG((DEBUG_INFO, " Legnth - 0x%x\n", ConfigData->Length));\r
81 Status = PerformFlashWrite (\r
82 ConfigData->FirmwareType,\r
83 ConfigData->BaseAddress,\r
84 ConfigData->AddressType,\r
85 (VOID *)((UINTN)SystemFirmwareImage + (UINTN)ConfigData->ImageOffset),\r
86 ConfigData->Length\r
87 );\r
88 if (!EFI_ERROR(Status)) {\r
89 *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;\r
90 if (ConfigData->FirmwareType == PlatformFirmwareTypeNvRam) {\r
91 mNvRamUpdated = TRUE;\r
92 }\r
93 } else {\r
94 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;\r
95 }\r
96 return Status;\r
97}\r
98\r
99/**\r
100 Update System Firmware image.\r
101\r
102 @param[in] SystemFirmwareImage Points to the System Firmware image.\r
103 @param[in] SystemFirmwareImageSize The length of the System Firmware image in bytes.\r
104 @param[in] ConfigImage Points to the config file image.\r
105 @param[in] ConfigImageSize The length of the config file image in bytes.\r
106 @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
107 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
108\r
109 @retval EFI_SUCCESS The System Firmware image is updated.\r
110 @retval EFI_WRITE_PROTECTED The flash device is read only.\r
111**/\r
112EFI_STATUS\r
113UpdateImage (\r
114 IN VOID *SystemFirmwareImage,\r
115 IN UINTN SystemFirmwareImageSize,\r
116 IN VOID *ConfigImage,\r
117 IN UINTN ConfigImageSize,\r
118 OUT UINT32 *LastAttemptVersion,\r
119 OUT UINT32 *LastAttemptStatus\r
120 )\r
121{\r
122 EFI_STATUS Status;\r
123 UPDATE_CONFIG_DATA *ConfigData;\r
124 UPDATE_CONFIG_DATA *UpdateConfigData;\r
125 CONFIG_HEADER ConfigHeader;\r
126 UINTN Index;\r
127\r
128 if (ConfigImage == NULL) {\r
129 DEBUG((DEBUG_INFO, "PlatformUpdate (NoConfig):"));\r
130 DEBUG((DEBUG_INFO, " BaseAddress - 0x%x,", 0));\r
131 DEBUG((DEBUG_INFO, " Length - 0x%x\n", SystemFirmwareImageSize));\r
132 // ASSUME the whole System Firmware include NVRAM region.\r
133 Status = PerformFlashWrite (\r
134 PlatformFirmwareTypeNvRam,\r
135 0,\r
136 FlashAddressTypeRelativeAddress,\r
137 SystemFirmwareImage,\r
138 SystemFirmwareImageSize\r
139 );\r
140 if (!EFI_ERROR(Status)) {\r
141 *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;\r
142 mNvRamUpdated = TRUE;\r
143 } else {\r
144 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;\r
145 }\r
146 return Status;\r
147 }\r
148\r
149 DEBUG((DEBUG_INFO, "PlatformUpdate (With Config):\n"));\r
150 ConfigData = NULL;\r
151 ZeroMem (&ConfigHeader, sizeof(ConfigHeader));\r
152 Status = ParseUpdateDataFile (\r
153 ConfigImage,\r
154 ConfigImageSize,\r
155 &ConfigHeader,\r
156 &ConfigData\r
157 );\r
158 DEBUG((DEBUG_INFO, "ParseUpdateDataFile - %r\n", Status));\r
159 if (EFI_ERROR(Status)) {\r
160 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;\r
161 return EFI_INVALID_PARAMETER;\r
162 }\r
163 DEBUG((DEBUG_INFO, "ConfigHeader.NumOfUpdates - 0x%x\n", ConfigHeader.NumOfUpdates));\r
164 DEBUG((DEBUG_INFO, "PcdEdkiiSystemFirmwareFileGuid - %g\n", PcdGetPtr(PcdEdkiiSystemFirmwareFileGuid)));\r
165\r
166 Index = 0;\r
167 UpdateConfigData = ConfigData;\r
168 while (Index < ConfigHeader.NumOfUpdates) {\r
169 if (CompareGuid(&UpdateConfigData->FileGuid, PcdGetPtr(PcdEdkiiSystemFirmwareFileGuid))) {\r
170 DEBUG((DEBUG_INFO, "FileGuid - %g (processing)\n", &UpdateConfigData->FileGuid));\r
171 Status = PerformUpdate (\r
172 SystemFirmwareImage,\r
173 SystemFirmwareImageSize,\r
174 UpdateConfigData,\r
175 LastAttemptVersion,\r
176 LastAttemptStatus\r
177 );\r
178 //\r
179 // Shall updates be serialized so that if an update is not successfully completed,\r
180 // the remaining updates won't be performed.\r
181 //\r
182 if (EFI_ERROR (Status)) {\r
183 break;\r
184 }\r
185 } else {\r
186 DEBUG((DEBUG_INFO, "FileGuid - %g (ignored)\n", &UpdateConfigData->FileGuid));\r
187 }\r
188\r
189 Index++;\r
190 UpdateConfigData++;\r
191 }\r
192\r
193 return Status;\r
194}\r
195\r
196/**\r
197 Authenticate and update System Firmware image.\r
198\r
199 Caution: This function may receive untrusted input.\r
200\r
201 @param[in] Image The EDKII system FMP capsule image.\r
202 @param[in] ImageSize The size of the EDKII system FMP capsule image in bytes.\r
203 @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
204 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
205\r
206 @retval EFI_SUCCESS EDKII system FMP capsule passes authentication and the System Firmware image is updated.\r
207 @retval EFI_SECURITY_VIOLATION EDKII system FMP capsule fails authentication and the System Firmware image is not updated.\r
208 @retval EFI_WRITE_PROTECTED The flash device is read only.\r
209**/\r
210EFI_STATUS\r
211SystemFirmwareAuthenticatedUpdate (\r
212 IN VOID *Image,\r
213 IN UINTN ImageSize,\r
214 OUT UINT32 *LastAttemptVersion,\r
215 OUT UINT32 *LastAttemptStatus\r
216 )\r
217{\r
218 EFI_STATUS Status;\r
219 VOID *SystemFirmwareImage;\r
220 UINTN SystemFirmwareImageSize;\r
221 VOID *ConfigImage;\r
222 UINTN ConfigImageSize;\r
223 VOID *AuthenticatedImage;\r
224 UINTN AuthenticatedImageSize;\r
225\r
8b66342c
HW
226 AuthenticatedImage = NULL;\r
227 AuthenticatedImageSize = 0;\r
228\r
f6f91d38
JY
229 DEBUG((DEBUG_INFO, "SystemFirmwareAuthenticatedUpdate...\n"));\r
230\r
231 Status = CapsuleAuthenticateSystemFirmware(Image, ImageSize, FALSE, LastAttemptVersion, LastAttemptStatus, &AuthenticatedImage, &AuthenticatedImageSize);\r
232 if (EFI_ERROR(Status)) {\r
233 DEBUG((DEBUG_INFO, "SystemFirmwareAuthenticateImage - %r\n", Status));\r
234 return Status;\r
235 }\r
236\r
237 DEBUG((DEBUG_INFO, "ExtractSystemFirmwareImage ...\n"));\r
238 ExtractSystemFirmwareImage(AuthenticatedImage, AuthenticatedImageSize, &SystemFirmwareImage, &SystemFirmwareImageSize);\r
239 DEBUG((DEBUG_INFO, "ExtractConfigImage ...\n"));\r
240 ExtractConfigImage(AuthenticatedImage, AuthenticatedImageSize, &ConfigImage, &ConfigImageSize);\r
241\r
242 DEBUG((DEBUG_INFO, "UpdateImage ...\n"));\r
243 Status = UpdateImage(SystemFirmwareImage, SystemFirmwareImageSize, ConfigImage, ConfigImageSize, LastAttemptVersion, LastAttemptStatus);\r
244 if (EFI_ERROR(Status)) {\r
245 DEBUG((DEBUG_INFO, "UpdateImage - %r\n", Status));\r
246 return Status;\r
247 }\r
248\r
249 DEBUG((DEBUG_INFO, "SystemFirmwareAuthenticatedUpdate Done\n"));\r
250\r
251 return EFI_SUCCESS;\r
252}\r
253\r
254/**\r
255\r
256 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
257\r
258 @param[in] VariableName Name of Variable to be found.\r
259 @param[in] VendorGuid Variable vendor GUID.\r
260 @param[out] Attributes Attribute value of the variable found.\r
261 @param[in, out] DataSize Size of Data found. If size is less than the\r
262 data, this value contains the required size.\r
263 @param[out] Data Data pointer.\r
264\r
265 @return EFI_INVALID_PARAMETER Invalid parameter.\r
266 @return EFI_SUCCESS Find the specified variable.\r
267 @return EFI_NOT_FOUND Not found.\r
268 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
269\r
270**/\r
271EFI_STATUS\r
272EFIAPI\r
273GetVariableHook (\r
274 IN CHAR16 *VariableName,\r
275 IN EFI_GUID *VendorGuid,\r
276 OUT UINT32 *Attributes OPTIONAL,\r
277 IN OUT UINTN *DataSize,\r
278 OUT VOID *Data\r
279 )\r
280{\r
281 DEBUG((DEBUG_INFO, "GetVariableHook - %S, %g\n", VariableName, VendorGuid));\r
282 return EFI_NOT_AVAILABLE_YET;\r
283}\r
284\r
285/**\r
286\r
287 This code Finds the Next available variable.\r
288\r
289 @param[in, out] VariableNameSize Size of the variable name.\r
290 @param[in, out] VariableName Pointer to variable name.\r
291 @param[in, out] VendorGuid Variable Vendor Guid.\r
292\r
293 @return EFI_INVALID_PARAMETER Invalid parameter.\r
294 @return EFI_SUCCESS Find the specified variable.\r
295 @return EFI_NOT_FOUND Not found.\r
296 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
297\r
298**/\r
299EFI_STATUS\r
300EFIAPI\r
301GetNextVariableNameHook (\r
302 IN OUT UINTN *VariableNameSize,\r
303 IN OUT CHAR16 *VariableName,\r
304 IN OUT EFI_GUID *VendorGuid\r
305 )\r
306{\r
307 DEBUG((DEBUG_INFO, "GetNextVariableNameHook - %S, %g\n", VariableName, VendorGuid));\r
308 return EFI_NOT_AVAILABLE_YET;\r
309}\r
310\r
311/**\r
312\r
313 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
314\r
315 @param[in] VariableName Name of Variable to be found.\r
316 @param[in] VendorGuid Variable vendor GUID.\r
317 @param[in] Attributes Attribute value of the variable found\r
318 @param[in] DataSize Size of Data found. If size is less than the\r
319 data, this value contains the required size.\r
320 @param[in] Data Data pointer.\r
321\r
322 @return EFI_INVALID_PARAMETER Invalid parameter.\r
323 @return EFI_SUCCESS Set successfully.\r
324 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
325 @return EFI_NOT_FOUND Not found.\r
326 @return EFI_WRITE_PROTECTED Variable is read-only.\r
327\r
328**/\r
329EFI_STATUS\r
330EFIAPI\r
331SetVariableHook (\r
332 IN CHAR16 *VariableName,\r
333 IN EFI_GUID *VendorGuid,\r
334 IN UINT32 Attributes,\r
335 IN UINTN DataSize,\r
336 IN VOID *Data\r
337 )\r
338{\r
339 DEBUG((DEBUG_INFO, "SetVariableHook - %S, %g, 0x%x (0x%x)\n", VariableName, VendorGuid, Attributes, DataSize));\r
340 return EFI_NOT_AVAILABLE_YET;\r
341}\r
342\r
343/**\r
344\r
345 This code returns information about the EFI variables.\r
346\r
347 @param[in] Attributes Attributes bitmask to specify the type of variables\r
348 on which to return information.\r
349 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
350 for the EFI variables associated with the attributes specified.\r
351 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
352 for EFI variables associated with the attributes specified.\r
353 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
354 associated with the attributes specified.\r
355\r
356 @return EFI_SUCCESS Query successfully.\r
357\r
358**/\r
359EFI_STATUS\r
360EFIAPI\r
361QueryVariableInfoHook (\r
362 IN UINT32 Attributes,\r
363 OUT UINT64 *MaximumVariableStorageSize,\r
364 OUT UINT64 *RemainingVariableStorageSize,\r
365 OUT UINT64 *MaximumVariableSize\r
366 )\r
367{\r
368 DEBUG((DEBUG_INFO, "QueryVariableInfoHook - 0x%x\n", Attributes));\r
369 return EFI_NOT_AVAILABLE_YET;\r
370}\r
371\r
372/**\r
373 Updates the firmware image of the device.\r
374\r
375 This function updates the hardware with the new firmware image.\r
376 This function returns EFI_UNSUPPORTED if the firmware image is not updatable.\r
377 If the firmware image is updatable, the function should perform the following minimal validations\r
378 before proceeding to do the firmware image update.\r
379 - Validate the image authentication if image has attribute\r
380 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns\r
381 EFI_SECURITY_VIOLATION if the validation fails.\r
382 - Validate the image is a supported image for this device. The function returns EFI_ABORTED if\r
383 the image is unsupported. The function can optionally provide more detailed information on\r
384 why the image is not a supported image.\r
385 - Validate the data from VendorCode if not null. Image validation must be performed before\r
386 VendorCode data validation. VendorCode data is ignored or considered invalid if image\r
387 validation failed. The function returns EFI_ABORTED if the data is invalid.\r
388\r
389 VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if\r
390 the caller did not specify the policy or use the default policy. As an example, vendor can implement\r
391 a policy to allow an option to force a firmware image update when the abort reason is due to the new\r
392 firmware image version is older than the current firmware image version or bad image checksum.\r
393 Sensitive operations such as those wiping the entire firmware image and render the device to be\r
394 non-functional should be encoded in the image itself rather than passed with the VendorCode.\r
395 AbortReason enables vendor to have the option to provide a more detailed description of the abort\r
396 reason to the caller.\r
397\r
398 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
399 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.\r
400 The number is between 1 and DescriptorCount.\r
401 @param[in] Image Points to the new image.\r
402 @param[in] ImageSize Size of the new image in bytes.\r
403 @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.\r
404 Null indicates the caller did not specify the policy or use the default policy.\r
405 @param[in] Progress A function used by the driver to report the progress of the firmware update.\r
406 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more\r
407 details for the aborted operation. The buffer is allocated by this function\r
408 with AllocatePool(), and it is the caller's responsibility to free it with a\r
409 call to FreePool().\r
410\r
411 @retval EFI_SUCCESS The device was successfully updated with the new image.\r
412 @retval EFI_ABORTED The operation is aborted.\r
413 @retval EFI_INVALID_PARAMETER The Image was NULL.\r
414 @retval EFI_UNSUPPORTED The operation is not supported.\r
415 @retval EFI_SECURITY_VIOLATIO The operation could not be performed due to an authentication failure.\r
416\r
417**/\r
418EFI_STATUS\r
419EFIAPI\r
420FmpSetImage (\r
421 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
422 IN UINT8 ImageIndex,\r
423 IN CONST VOID *Image,\r
424 IN UINTN ImageSize,\r
425 IN CONST VOID *VendorCode,\r
426 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,\r
427 OUT CHAR16 **AbortReason\r
428 )\r
429{\r
430 EFI_STATUS Status;\r
431 EFI_STATUS VarStatus;\r
432 SYSTEM_FMP_PRIVATE_DATA *SystemFmpPrivate;\r
433\r
434 if (Image == NULL || ImageSize == 0 || AbortReason == NULL) {\r
435 return EFI_INVALID_PARAMETER;\r
436 }\r
437\r
438 SystemFmpPrivate = SYSTEM_FMP_PRIVATE_DATA_FROM_FMP(This);\r
439 *AbortReason = NULL;\r
440\r
441 if (ImageIndex == 0 || ImageIndex > SystemFmpPrivate->DescriptorCount) {\r
442 return EFI_INVALID_PARAMETER;\r
443 }\r
444\r
445 Status = SystemFirmwareAuthenticatedUpdate((VOID *)Image, ImageSize, &SystemFmpPrivate->LastAttempt.LastAttemptVersion, &SystemFmpPrivate->LastAttempt.LastAttemptStatus);\r
446 DEBUG((DEBUG_INFO, "SetImage - LastAttemp Version - 0x%x, State - 0x%x\n", SystemFmpPrivate->LastAttempt.LastAttemptVersion, SystemFmpPrivate->LastAttempt.LastAttemptStatus));\r
447\r
448 //\r
449 // If NVRAM is updated, we should no longer touch variable services, because\r
450 // the current variable driver may not manage the new NVRAM region.\r
451 //\r
452 if (mNvRamUpdated) {\r
453 DEBUG ((DEBUG_INFO, "NvRamUpdated, Update Variable Serivces\n"));\r
454 gRT->GetVariable = GetVariableHook;\r
455 gRT->GetNextVariableName = GetNextVariableNameHook;\r
456 gRT->SetVariable = SetVariableHook;\r
457 gRT->QueryVariableInfo = QueryVariableInfoHook;\r
458\r
459 gRT->Hdr.CRC32 = 0;\r
460 gBS->CalculateCrc32 (\r
461 (UINT8 *) &gRT->Hdr,\r
462 gRT->Hdr.HeaderSize,\r
463 &gRT->Hdr.CRC32\r
464 );\r
465 }\r
466\r
467 VarStatus = gRT->SetVariable(\r
468 SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME,\r
469 &gSystemFmpLastAttemptVariableGuid,\r
470 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
471 sizeof(SystemFmpPrivate->LastAttempt),\r
472 &SystemFmpPrivate->LastAttempt\r
473 );\r
474 DEBUG((DEBUG_INFO, "SetLastAttemp - %r\n", VarStatus));\r
475\r
476 return Status;\r
477}\r
478\r
d69d9227
KM
479/**\r
480 Get the set of EFI_FIRMWARE_IMAGE_DESCRIPTOR structures from an FMP Protocol.\r
481\r
482 @param[in] Handle Handle with an FMP Protocol or a System FMP\r
483 Protocol.\r
484 @param[in] ProtocolGuid Pointer to the FMP Protocol GUID or System FMP\r
485 Protocol GUID.\r
486 @param[out] FmpImageInfoCount Pointer to the number of\r
487 EFI_FIRMWARE_IMAGE_DESCRIPTOR structures.\r
488 @param[out] DescriptorSize Pointer to the size, in bytes, of each\r
489 EFI_FIRMWARE_IMAGE_DESCRIPTOR structure.\r
490\r
491 @return NULL No EFI_FIRMWARE_IMAGE_DESCRIPTOR structures found.\r
492 @return !NULL Pointer to a buffer of EFI_FIRMWARE_IMAGE_DESCRIPTOR structures\r
493 allocated using AllocatePool(). Caller must free buffer with\r
494 FreePool().\r
495**/\r
496EFI_FIRMWARE_IMAGE_DESCRIPTOR *\r
497GetFmpImageDescriptors (\r
498 IN EFI_HANDLE Handle,\r
499 IN EFI_GUID *ProtocolGuid,\r
500 OUT UINT8 *FmpImageInfoCount,\r
501 OUT UINTN *DescriptorSize\r
502 )\r
503{\r
504 EFI_STATUS Status;\r
505 EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;\r
506 UINTN ImageInfoSize;\r
507 EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf;\r
508 UINT32 FmpImageInfoDescriptorVer;\r
509 UINT32 PackageVersion;\r
510 CHAR16 *PackageVersionName;\r
511\r
512 *FmpImageInfoCount = 0;\r
513 *DescriptorSize = 0;\r
514\r
515 Status = gBS->HandleProtocol (\r
516 Handle,\r
517 ProtocolGuid,\r
518 (VOID **)&Fmp\r
519 );\r
520 if (EFI_ERROR (Status)) {\r
521 return NULL;\r
522 }\r
523\r
524 //\r
525 // Determine the size required for the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.\r
526 //\r
527 ImageInfoSize = 0;\r
528 Status = Fmp->GetImageInfo (\r
529 Fmp, // FMP Pointer\r
530 &ImageInfoSize, // Buffer Size (in this case 0)\r
531 NULL, // NULL so we can get size\r
532 &FmpImageInfoDescriptorVer, // DescriptorVersion\r
533 FmpImageInfoCount, // DescriptorCount\r
534 DescriptorSize, // DescriptorSize\r
535 &PackageVersion, // PackageVersion\r
536 &PackageVersionName // PackageVersionName\r
537 );\r
538 if (Status != EFI_BUFFER_TOO_SMALL) {\r
539 DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: Unexpected Failure. Status = %r\n", Status));\r
540 return NULL;\r
541 }\r
542\r
543 //\r
544 // Allocate buffer for the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.\r
545 //\r
546 FmpImageInfoBuf = NULL;\r
547 FmpImageInfoBuf = AllocateZeroPool (ImageInfoSize);\r
548 if (FmpImageInfoBuf == NULL) {\r
549 DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: Failed to allocate memory for descriptors.\n"));\r
550 return NULL;\r
551 }\r
552\r
553 //\r
554 // Retrieve the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.\r
555 //\r
556 PackageVersionName = NULL;\r
557 Status = Fmp->GetImageInfo (\r
558 Fmp,\r
559 &ImageInfoSize, // ImageInfoSize\r
560 FmpImageInfoBuf, // ImageInfo\r
561 &FmpImageInfoDescriptorVer, // DescriptorVersion\r
562 FmpImageInfoCount, // DescriptorCount\r
563 DescriptorSize, // DescriptorSize\r
564 &PackageVersion, // PackageVersion\r
565 &PackageVersionName // PackageVersionName\r
566 );\r
567\r
568 //\r
569 // Free unused PackageVersionName return buffer\r
570 //\r
571 if (PackageVersionName != NULL) {\r
572 FreePool (PackageVersionName);\r
573 PackageVersionName = NULL;\r
574 }\r
575\r
576 if (EFI_ERROR (Status)) {\r
577 DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: Failure in GetImageInfo. Status = %r\n", Status));\r
578 if (FmpImageInfoBuf != NULL) {\r
579 FreePool (FmpImageInfoBuf);\r
580 }\r
581 return NULL;\r
582 }\r
583\r
584 return FmpImageInfoBuf;\r
585}\r
586\r
587/**\r
588 Search for handles with an FMP protocol whose EFI_FIRMWARE_IMAGE_DESCRIPTOR\r
589 ImageTypeId matches the ImageTypeId produced by this module.\r
590\r
591 @param[in] ProtocolGuid Pointer to the GUID of the protocol to search.\r
592 @param[out] HandleCount Pointer to the number of returned handles.\r
593\r
594 @return NULL No matching handles found.\r
595 @return !NULL Pointer to a buffer of handles allocated using AllocatePool().\r
596 Caller must free buffer with FreePool().\r
597**/\r
598EFI_HANDLE *\r
599FindMatchingFmpHandles (\r
600 IN EFI_GUID *ProtocolGuid,\r
601 OUT UINTN *HandleCount\r
602 )\r
603{\r
604 EFI_STATUS Status;\r
665bfd41 605 UINTN TempHandleCount;\r
d69d9227
KM
606 EFI_HANDLE *HandleBuffer;\r
607 UINTN Index;\r
608 UINTN Index2;\r
609 UINTN Index3;\r
610 EFI_FIRMWARE_IMAGE_DESCRIPTOR *OriginalFmpImageInfoBuf;\r
611 EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf;\r
612 UINT8 FmpImageInfoCount;\r
613 UINTN DescriptorSize;\r
614 BOOLEAN MatchFound;\r
615\r
616 *HandleCount = 0;\r
665bfd41 617 TempHandleCount = 0;\r
d69d9227
KM
618 HandleBuffer = NULL;\r
619 Status = gBS->LocateHandleBuffer (\r
620 ByProtocol,\r
621 ProtocolGuid,\r
622 NULL,\r
665bfd41 623 &TempHandleCount,\r
d69d9227
KM
624 &HandleBuffer\r
625 );\r
626 if (EFI_ERROR (Status)) {\r
d69d9227
KM
627 return NULL;\r
628 }\r
629\r
665bfd41 630 for (Index = 0; Index < TempHandleCount; Index++) {\r
d69d9227
KM
631 OriginalFmpImageInfoBuf = GetFmpImageDescriptors (\r
632 HandleBuffer[Index],\r
633 ProtocolGuid,\r
634 &FmpImageInfoCount,\r
635 &DescriptorSize\r
636 );\r
637\r
638 //\r
639 // Loop through the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.\r
640 //\r
641 FmpImageInfoBuf = OriginalFmpImageInfoBuf;\r
642 MatchFound = FALSE;\r
643 for (Index2 = 0; Index2 < FmpImageInfoCount; Index2++) {\r
644 for (Index3 = 0; Index3 < mSystemFmpPrivate->DescriptorCount; Index3++) {\r
645 MatchFound = CompareGuid (\r
646 &FmpImageInfoBuf->ImageTypeId,\r
647 &mSystemFmpPrivate->ImageDescriptor[Index3].ImageTypeId\r
648 );\r
649 if (MatchFound) {\r
650 break;\r
651 }\r
652 }\r
653 if (MatchFound) {\r
654 break;\r
655 }\r
656 //\r
657 // Increment the buffer pointer ahead by the size of the descriptor\r
658 //\r
659 FmpImageInfoBuf = (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)(((UINT8 *)FmpImageInfoBuf) + DescriptorSize);\r
660 }\r
665bfd41
SZ
661 if (MatchFound) {\r
662 HandleBuffer[*HandleCount] = HandleBuffer[Index];\r
663 (*HandleCount)++;\r
d69d9227
KM
664 }\r
665\r
666 FreePool (OriginalFmpImageInfoBuf);\r
667 }\r
665bfd41
SZ
668\r
669 if ((*HandleCount) == 0) {\r
670 //\r
671 // No any matching handle.\r
672 //\r
673 FreePool (HandleBuffer);\r
674 return NULL;\r
675 }\r
d69d9227
KM
676 return HandleBuffer;\r
677}\r
678\r
679/**\r
680 Uninstall System FMP Protocol instances that may have been installed by\r
681 SystemFirmwareUpdateDxe drivers dispatches by other capsules.\r
682\r
683 @retval EFI_SUCCESS All System FMP Protocols found were uninstalled.\r
684 @return Other One or more System FMP Protocols could not be uninstalled.\r
685\r
686**/\r
687EFI_STATUS\r
688UninstallMatchingSystemFmpProtocols (\r
689 VOID\r
690 )\r
691{\r
692 EFI_STATUS Status;\r
693 EFI_HANDLE *HandleBuffer;\r
694 UINTN HandleCount;\r
695 UINTN Index;\r
696 EFI_FIRMWARE_MANAGEMENT_PROTOCOL *SystemFmp;\r
697\r
698 //\r
699 // Uninstall SystemFmpProtocol instances that may have been produced by\r
700 // the SystemFirmwareUpdate drivers in FVs dispatched by other capsules.\r
701 //\r
702 HandleBuffer = FindMatchingFmpHandles (\r
703 &gSystemFmpProtocolGuid,\r
704 &HandleCount\r
705 );\r
706 DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Found %d matching System FMP instances\n", HandleCount));\r
707\r
708 for (Index = 0; Index < HandleCount; Index++) {\r
709 Status = gBS->HandleProtocol(\r
710 HandleBuffer[Index],\r
711 &gSystemFmpProtocolGuid,\r
712 (VOID **)&SystemFmp\r
713 );\r
714 if (EFI_ERROR (Status)) {\r
715 continue;\r
716 }\r
717 DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Uninstall SystemFmp produced by another capsule\n"));\r
718 Status = gBS->UninstallProtocolInterface (\r
719 HandleBuffer[Index],\r
720 &gSystemFmpProtocolGuid,\r
721 SystemFmp\r
722 );\r
723 if (EFI_ERROR (Status)) {\r
724 DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: Failed to uninstall SystemFmp %r. Exiting.\n", Status));\r
725 FreePool (HandleBuffer);\r
726 return Status;\r
727 }\r
728 }\r
729 if (HandleBuffer != NULL) {\r
730 FreePool (HandleBuffer);\r
731 }\r
732\r
733 return EFI_SUCCESS;\r
734}\r
735\r
f6f91d38
JY
736/**\r
737 System FMP module entrypoint\r
738\r
d69d9227
KM
739 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
740 @param[in] SystemTable A pointer to the EFI System Table.\r
f6f91d38 741\r
d69d9227
KM
742 @retval EFI_SUCCESS System FMP module is initialized.\r
743 @retval EFI_OUT_OF_RESOURCES There are not enough resources avaulable to\r
744 initialize this module.\r
745 @retval Other System FMP Protocols could not be uninstalled.\r
746 @retval Other System FMP Protocol could not be installed.\r
747 @retval Other FMP Protocol could not be installed.\r
f6f91d38
JY
748**/\r
749EFI_STATUS\r
750EFIAPI\r
751SystemFirmwareUpdateMainDxe (\r
d69d9227
KM
752 IN EFI_HANDLE ImageHandle,\r
753 IN EFI_SYSTEM_TABLE *SystemTable\r
f6f91d38
JY
754 )\r
755{\r
d69d9227
KM
756 EFI_STATUS Status;\r
757 EFI_HANDLE *HandleBuffer;\r
758 UINTN HandleCount;\r
f6f91d38
JY
759\r
760 //\r
761 // Initialize SystemFmpPrivateData\r
762 //\r
d69d9227 763 mSystemFmpPrivate = AllocateZeroPool (sizeof (SYSTEM_FMP_PRIVATE_DATA));\r
f6f91d38
JY
764 if (mSystemFmpPrivate == NULL) {\r
765 return EFI_OUT_OF_RESOURCES;\r
766 }\r
767\r
d69d9227
KM
768 Status = InitializePrivateData (mSystemFmpPrivate);\r
769 if (EFI_ERROR (Status)) {\r
770 FreePool (mSystemFmpPrivate);\r
f6f91d38
JY
771 mSystemFmpPrivate = NULL;\r
772 return Status;\r
773 }\r
774\r
775 //\r
d69d9227
KM
776 // Uninstall SystemFmpProtocol instances that may have been produced by\r
777 // the SystemFirmwareUpdate drivers in FVs dispatched by other capsules.\r
f6f91d38 778 //\r
d69d9227 779 Status = UninstallMatchingSystemFmpProtocols ();\r
f6f91d38 780 if (EFI_ERROR (Status)) {\r
d69d9227 781 FreePool (mSystemFmpPrivate);\r
f6f91d38
JY
782 mSystemFmpPrivate = NULL;\r
783 return Status;\r
784 }\r
785\r
d69d9227
KM
786 //\r
787 // Look for a handle with matching Firmware Management Protocol\r
788 //\r
789 HandleCount = 0;\r
790 HandleBuffer = FindMatchingFmpHandles (\r
791 &gEfiFirmwareManagementProtocolGuid,\r
792 &HandleCount\r
793 );\r
794 DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Found %d matching FMP instances\n", HandleCount));\r
795\r
796 switch (HandleCount) {\r
797 case 0:\r
798 //\r
799 // Install FMP protocol onto a new handle.\r
800 //\r
801 DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Install FMP onto a new handle\n"));\r
802 Status = gBS->InstallMultipleProtocolInterfaces (\r
803 &mSystemFmpPrivate->Handle,\r
804 &gEfiFirmwareManagementProtocolGuid,\r
805 &mSystemFmpPrivate->Fmp,\r
806 NULL\r
807 );\r
808 break;\r
809 case 1:\r
810 //\r
811 // Install System FMP protocol onto handle with matching FMP Protocol\r
812 //\r
813 DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Install System FMP onto matching FMP handle\n"));\r
665bfd41 814 mSystemFmpPrivate->Handle = HandleBuffer[0];\r
d69d9227
KM
815 Status = gBS->InstallMultipleProtocolInterfaces (\r
816 &HandleBuffer[0],\r
817 &gSystemFmpProtocolGuid,\r
818 &mSystemFmpPrivate->Fmp,\r
819 NULL\r
820 );\r
821 break;\r
822 default:\r
823 //\r
824 // More than one matching handle is not expected. Unload driver.\r
825 //\r
826 DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: More than one matching FMP handle. Unload driver.\n"));\r
827 Status = EFI_DEVICE_ERROR;\r
828 break;\r
829 }\r
830\r
831 if (HandleBuffer != NULL) {\r
832 FreePool (HandleBuffer);\r
833 }\r
834\r
835 if (EFI_ERROR (Status)) {\r
836 FreePool (mSystemFmpPrivate);\r
837 mSystemFmpPrivate = NULL;\r
838 }\r
839\r
f6f91d38
JY
840 return Status;\r
841}\r