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