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