]> git.proxmox.com Git - mirror_edk2.git/blame - FmpDevicePkg/FmpDxe/FmpDxe.c
FmpDevicePkg/FmpDxe: Add check image path Last Attempt Status capability
[mirror_edk2.git] / FmpDevicePkg / FmpDxe / FmpDxe.c
CommitLineData
a6d73269 1/** @file\r
b0bacc00
KM
2 Produces a Firmware Management Protocol that supports updates to a firmware\r
3 image stored in a firmware device with platform and firmware device specific\r
4 information provided through PCDs and libraries.\r
5\r
de6859ec 6 Copyright (c) Microsoft Corporation.<BR>\r
2ed845b3 7 Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.<BR>\r
b0bacc00 8\r
bcef758c 9 SPDX-License-Identifier: BSD-2-Clause-Patent\r
b0bacc00
KM
10\r
11**/\r
12\r
4f0544b1 13#include "FmpDxe.h"\r
67c1e5ee 14#include "VariableSupport.h"\r
b0bacc00
KM
15\r
16///\r
17/// FILE_GUID from FmpDxe.inf. When FmpDxe.inf is used in a platform, the\r
18/// FILE_GUID must always be overridden in the <Defines> section to provide\r
19/// the ESRT GUID value associated with the updatable firmware image. A\r
20/// check is made in this module's driver entry point to verify that a\r
21/// new FILE_GUID value has been defined.\r
22///\r
23const EFI_GUID mDefaultModuleFileGuid = {\r
24 0x78ef0a56, 0x1cf0, 0x4535, { 0xb5, 0xda, 0xf6, 0xfd, 0x2f, 0x40, 0x5a, 0x11 }\r
25};\r
26\r
b0bacc00 27///\r
4f0544b1 28/// TRUE if FmpDeviceLib manages a single firmware storage device.\r
b0bacc00 29///\r
4f0544b1 30BOOLEAN mFmpSingleInstance = FALSE;\r
b0bacc00 31\r
4f0544b1
EJ
32///\r
33/// Firmware Management Protocol instance that is initialized in the entry\r
34/// point from PCD settings.\r
35///\r
36EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL mFmpProgress;\r
b0bacc00 37\r
0760f5fe 38//\r
4f0544b1
EJ
39// Template of the private context structure for the Firmware Management\r
40// Protocol instance\r
0760f5fe 41//\r
4f0544b1
EJ
42const FIRMWARE_MANAGEMENT_PRIVATE_DATA mFirmwareManagementPrivateDataTemplate = {\r
43 FIRMWARE_MANAGEMENT_PRIVATE_DATA_SIGNATURE, // Signature\r
44 NULL, // Handle\r
45 { // Fmp\r
46 GetTheImageInfo,\r
47 GetTheImage,\r
48 SetTheImage,\r
49 CheckTheImage,\r
50 GetPackageInfo,\r
51 SetPackageInfo\r
52 },\r
53 FALSE, // DescriptorPopulated\r
54 { // Desc\r
55 1, // ImageIndex\r
56 //\r
57 // ImageTypeId\r
58 //\r
59 { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },\r
60 1, // ImageId\r
61 NULL, // ImageIdName\r
62 0, // Version\r
63 NULL, // VersionName\r
64 0, // Size\r
65 0, // AttributesSupported\r
66 0, // AttributesSetting\r
67 0, // Compatibilities\r
68 0, // LowestSupportedImageVersion\r
69 0, // LastAttemptVersion\r
70 0, // LastAttemptStatus\r
71 0 // HardwareInstance\r
72 },\r
73 NULL, // ImageIdName\r
74 NULL, // VersionName\r
75 TRUE, // RuntimeVersionSupported\r
76 NULL, // FmpDeviceLockEvent\r
67c1e5ee
EJ
77 FALSE, // FmpDeviceLocked\r
78 NULL, // FmpDeviceContext\r
79 NULL, // VersionVariableName\r
80 NULL, // LsvVariableName\r
81 NULL, // LastAttemptStatusVariableName\r
82 NULL, // LastAttemptVersionVariableName\r
0f30087b
WX
83 NULL, // FmpStateVariableName\r
84 TRUE // DependenciesSatisfied\r
4f0544b1
EJ
85};\r
86\r
87///\r
88/// GUID that is used to create event used to lock the firmware storage device.\r
89///\r
90EFI_GUID *mLockGuid = NULL;\r
91\r
92///\r
93/// Progress() function pointer passed into SetTheImage()\r
94///\r
95EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS mProgressFunc = NULL;\r
96\r
97///\r
98/// Null-terminated Unicode string retrieved from PcdFmpDeviceImageIdName.\r
99///\r
100CHAR16 *mImageIdName = NULL;\r
b0bacc00
KM
101\r
102/**\r
103 Callback function to report the process of the firmware updating.\r
104\r
105 Wrap the caller's version in this so that progress from the device lib is\r
106 within the expected range. Convert device lib 0% - 100% to 6% - 98%.\r
107\r
108 FmpDxe 1% - 5% for validation\r
109 FmpDeviceLib 6% - 98% for flashing/update\r
110 FmpDxe 99% - 100% finish\r
111\r
112 @param[in] Completion A value between 1 and 100 indicating the current\r
113 completion progress of the firmware update. Completion\r
114 progress is reported as from 1 to 100 percent. A value\r
115 of 0 is used by the driver to indicate that progress\r
116 reporting is not supported.\r
117\r
118 @retval EFI_SUCCESS The progress was updated.\r
119 @retval EFI_UNSUPPORTED Updating progress is not supported.\r
120\r
121**/\r
122EFI_STATUS\r
123EFIAPI\r
124FmpDxeProgress (\r
125 IN UINTN Completion\r
126 )\r
127{\r
128 EFI_STATUS Status;\r
129\r
130 Status = EFI_UNSUPPORTED;\r
131\r
b0bacc00
KM
132 if (mProgressFunc == NULL) {\r
133 return Status;\r
134 }\r
135\r
136 //\r
137 // Reserve 6% - 98% for the FmpDeviceLib. Call the real progress function.\r
138 //\r
139 Status = mProgressFunc (((Completion * 92) / 100) + 6);\r
140\r
141 if (Status == EFI_UNSUPPORTED) {\r
b0bacc00
KM
142 mProgressFunc = NULL;\r
143 }\r
144\r
145 return Status;\r
146}\r
147\r
148/**\r
149 Returns a pointer to the ImageTypeId GUID value. An attempt is made to get\r
150 the GUID value from the FmpDeviceLib. If the FmpDeviceLib does not provide\r
278c3d48
EJ
151 a GUID value, then PcdFmpDeviceImageTypeIdGuid is used. If the size of\r
152 PcdFmpDeviceImageTypeIdGuid is not the size of EFI_GUID, then gEfiCallerIdGuid\r
153 is returned.\r
b0bacc00 154\r
67c1e5ee 155 @retval The ImageTypeId GUID\r
b0bacc00
KM
156\r
157**/\r
158EFI_GUID *\r
159GetImageTypeIdGuid (\r
160 VOID\r
161 )\r
162{\r
163 EFI_STATUS Status;\r
164 EFI_GUID *FmpDeviceLibGuid;\r
278c3d48 165 UINTN ImageTypeIdGuidSize;\r
b0bacc00
KM
166\r
167 FmpDeviceLibGuid = NULL;\r
168 Status = FmpDeviceGetImageTypeIdGuidPtr (&FmpDeviceLibGuid);\r
169 if (EFI_ERROR (Status)) {\r
170 if (Status != EFI_UNSUPPORTED) {\r
e0961677 171 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid error %r\n", mImageIdName, Status));\r
b0bacc00 172 }\r
278c3d48 173 } else if (FmpDeviceLibGuid == NULL) {\r
e0961677 174 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid GUID\n", mImageIdName));\r
278c3d48
EJ
175 Status = EFI_NOT_FOUND;\r
176 }\r
177 if (EFI_ERROR (Status)) {\r
178 ImageTypeIdGuidSize = PcdGetSize (PcdFmpDeviceImageTypeIdGuid);\r
179 if (ImageTypeIdGuidSize == sizeof (EFI_GUID)) {\r
180 FmpDeviceLibGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceImageTypeIdGuid);\r
181 } else {\r
de6859ec 182 DEBUG ((DEBUG_WARN, "FmpDxe(%s): Fall back to ImageTypeIdGuid of gEfiCallerIdGuid\n", mImageIdName));\r
278c3d48
EJ
183 FmpDeviceLibGuid = &gEfiCallerIdGuid;\r
184 }\r
b0bacc00
KM
185 }\r
186 return FmpDeviceLibGuid;\r
187}\r
188\r
189/**\r
190 Returns a pointer to the Null-terminated Unicode ImageIdName string.\r
191\r
67c1e5ee 192 @retval Null-terminated Unicode ImageIdName string.\r
b0bacc00
KM
193\r
194**/\r
195CHAR16 *\r
196GetImageTypeNameString (\r
197 VOID\r
198 )\r
199{\r
200 return mImageIdName;\r
201}\r
202\r
203/**\r
204 Lowest supported version is a combo of three parts.\r
205 1. Check if the device lib has a lowest supported version\r
206 2. Check if we have a variable for lowest supported version (this will be updated with each capsule applied)\r
207 3. Check Fixed at build PCD\r
208\r
67c1e5ee
EJ
209 @param[in] Private Pointer to the private context structure for the\r
210 Firmware Management Protocol instance.\r
211\r
212 @retval The largest value\r
b0bacc00
KM
213\r
214**/\r
215UINT32\r
216GetLowestSupportedVersion (\r
67c1e5ee 217 FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private\r
b0bacc00
KM
218 )\r
219{\r
220 EFI_STATUS Status;\r
221 UINT32 DeviceLibLowestSupportedVersion;\r
222 UINT32 VariableLowestSupportedVersion;\r
223 UINT32 ReturnLsv;\r
224\r
225 //\r
226 // Get the LowestSupportedVersion.\r
227 //\r
228\r
b0bacc00 229 if (!IsLowestSupportedVersionCheckRequired ()) {\r
1bbb5126
SZ
230 //\r
231 // Any Version can pass the 0 LowestSupportedVersion check.\r
232 //\r
233 return 0;\r
b0bacc00
KM
234 }\r
235\r
1bbb5126
SZ
236 ReturnLsv = PcdGet32 (PcdFmpDeviceBuildTimeLowestSupportedVersion);\r
237\r
b0bacc00
KM
238 //\r
239 // Check the FmpDeviceLib\r
240 //\r
559b5d52 241 DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;\r
b0bacc00
KM
242 Status = FmpDeviceGetLowestSupportedVersion (&DeviceLibLowestSupportedVersion);\r
243 if (EFI_ERROR (Status)) {\r
244 DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;\r
245 }\r
246\r
247 if (DeviceLibLowestSupportedVersion > ReturnLsv) {\r
248 ReturnLsv = DeviceLibLowestSupportedVersion;\r
249 }\r
250\r
251 //\r
252 // Check the lowest supported version UEFI variable for this device\r
253 //\r
67c1e5ee 254 VariableLowestSupportedVersion = GetLowestSupportedVersionFromVariable (Private);\r
b0bacc00
KM
255 if (VariableLowestSupportedVersion > ReturnLsv) {\r
256 ReturnLsv = VariableLowestSupportedVersion;\r
257 }\r
258\r
259 //\r
260 // Return the largest value\r
261 //\r
262 return ReturnLsv;\r
263}\r
264\r
265/**\r
4f0544b1
EJ
266 Populates the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure in the private\r
267 context structure.\r
268\r
269 @param[in] Private Pointer to the private context structure for the\r
270 Firmware Management Protocol instance.\r
b0bacc00
KM
271\r
272**/\r
273VOID\r
274PopulateDescriptor (\r
4f0544b1 275 FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private\r
b0bacc00
KM
276 )\r
277{\r
278 EFI_STATUS Status;\r
0f30087b 279 UINT32 DependenciesSize;\r
b0bacc00 280\r
b4b9496b
MK
281 if (Private == NULL) {\r
282 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): PopulateDescriptor() - Private is NULL.\n", mImageIdName));\r
283 return;\r
284 }\r
285\r
4f0544b1
EJ
286 if (Private->DescriptorPopulated) {\r
287 return;\r
288 }\r
289\r
290 Private->Descriptor.ImageIndex = 1;\r
291 CopyGuid (&Private->Descriptor.ImageTypeId, GetImageTypeIdGuid());\r
292 Private->Descriptor.ImageId = Private->Descriptor.ImageIndex;\r
293 Private->Descriptor.ImageIdName = GetImageTypeNameString();\r
b0bacc00 294\r
67c1e5ee
EJ
295 //\r
296 // Get the hardware instance from FmpDeviceLib\r
297 //\r
298 Status = FmpDeviceGetHardwareInstance (&Private->Descriptor.HardwareInstance);\r
299 if (Status == EFI_UNSUPPORTED) {\r
300 Private->Descriptor.HardwareInstance = 0;\r
301 }\r
302\r
303 //\r
304 // Generate UEFI Variable names used to store status information for this\r
305 // FMP instance.\r
306 //\r
307 GenerateFmpVariableNames (Private);\r
308\r
b0bacc00
KM
309 //\r
310 // Get the version. Some devices don't support getting the firmware version\r
311 // at runtime. If FmpDeviceLib does not support returning a version, then\r
312 // it is stored in a UEFI variable.\r
313 //\r
4f0544b1 314 Status = FmpDeviceGetVersion (&Private->Descriptor.Version);\r
b0bacc00 315 if (Status == EFI_UNSUPPORTED) {\r
4f0544b1 316 Private->RuntimeVersionSupported = FALSE;\r
67c1e5ee 317 Private->Descriptor.Version = GetVersionFromVariable (Private);\r
b0bacc00
KM
318 } else if (EFI_ERROR (Status)) {\r
319 //\r
320 // Unexpected error. Use default version.\r
321 //\r
e0961677 322 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetVersion() from FmpDeviceLib (%s) returned %r\n", mImageIdName, GetImageTypeNameString(), Status));\r
4f0544b1 323 Private->Descriptor.Version = DEFAULT_VERSION;\r
b0bacc00
KM
324 }\r
325\r
326 //\r
327 // Free the current version name. Shouldn't really happen but this populate\r
328 // function could be called multiple times (to refresh).\r
329 //\r
4f0544b1
EJ
330 if (Private->Descriptor.VersionName != NULL) {\r
331 FreePool (Private->Descriptor.VersionName);\r
332 Private->Descriptor.VersionName = NULL;\r
b0bacc00
KM
333 }\r
334\r
335 //\r
336 // Attempt to get the version string from the FmpDeviceLib\r
337 //\r
4f0544b1 338 Status = FmpDeviceGetVersionString (&Private->Descriptor.VersionName);\r
b0bacc00 339 if (Status == EFI_UNSUPPORTED) {\r
e0961677 340 DEBUG ((DEBUG_INFO, "FmpDxe(%s): GetVersionString() unsupported in FmpDeviceLib.\n", mImageIdName));\r
4f0544b1
EJ
341 Private->Descriptor.VersionName = AllocateCopyPool (\r
342 sizeof (VERSION_STRING_NOT_SUPPORTED),\r
343 VERSION_STRING_NOT_SUPPORTED\r
344 );\r
b0bacc00 345 } else if (EFI_ERROR (Status)) {\r
e0961677 346 DEBUG ((DEBUG_INFO, "FmpDxe(%s): GetVersionString() not available in FmpDeviceLib.\n", mImageIdName));\r
4f0544b1
EJ
347 Private->Descriptor.VersionName = AllocateCopyPool (\r
348 sizeof (VERSION_STRING_NOT_AVAILABLE),\r
349 VERSION_STRING_NOT_AVAILABLE\r
350 );\r
b0bacc00
KM
351 }\r
352\r
67c1e5ee 353 Private->Descriptor.LowestSupportedImageVersion = GetLowestSupportedVersion (Private);\r
b0bacc00
KM
354\r
355 //\r
356 // Get attributes from the FmpDeviceLib\r
357 //\r
4f0544b1
EJ
358 FmpDeviceGetAttributes (\r
359 &Private->Descriptor.AttributesSupported,\r
360 &Private->Descriptor.AttributesSetting\r
361 );\r
b0bacc00
KM
362\r
363 //\r
364 // Force set the updatable bits in the attributes;\r
365 //\r
4f0544b1
EJ
366 Private->Descriptor.AttributesSupported |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;\r
367 Private->Descriptor.AttributesSetting |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;\r
b0bacc00
KM
368\r
369 //\r
370 // Force set the authentication bits in the attributes;\r
371 //\r
4f0544b1
EJ
372 Private->Descriptor.AttributesSupported |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);\r
373 Private->Descriptor.AttributesSetting |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);\r
b0bacc00 374\r
4f0544b1 375 Private->Descriptor.Compatibilities = 0;\r
b0bacc00
KM
376\r
377 //\r
378 // Get the size of the firmware image from the FmpDeviceLib\r
379 //\r
4f0544b1 380 Status = FmpDeviceGetSize (&Private->Descriptor.Size);\r
b0bacc00 381 if (EFI_ERROR (Status)) {\r
4f0544b1 382 Private->Descriptor.Size = 0;\r
b0bacc00
KM
383 }\r
384\r
67c1e5ee
EJ
385 Private->Descriptor.LastAttemptVersion = GetLastAttemptVersionFromVariable (Private);\r
386 Private->Descriptor.LastAttemptStatus = GetLastAttemptStatusFromVariable (Private);\r
4f0544b1 387\r
2ed845b3 388 //\r
0f30087b 389 // Get the dependency from the FmpDependencyDeviceLib.\r
2ed845b3
WX
390 //\r
391 Private->Descriptor.Dependencies = NULL;\r
392\r
393 //\r
394 // Check the attribute IMAGE_ATTRIBUTE_DEPENDENCY\r
395 //\r
0f30087b
WX
396 if (Private->Descriptor.AttributesSetting & IMAGE_ATTRIBUTE_DEPENDENCY) {\r
397 Private->Descriptor.Dependencies = GetFmpDependency (&DependenciesSize);\r
2ed845b3
WX
398 }\r
399\r
4f0544b1 400 Private->DescriptorPopulated = TRUE;\r
b0bacc00
KM
401}\r
402\r
403/**\r
404 Returns information about the current firmware image(s) of the device.\r
405\r
406 This function allows a copy of the current firmware image to be created and saved.\r
407 The saved copy could later been used, for example, in firmware image recovery or rollback.\r
408\r
409 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
410 @param[in, out] ImageInfoSize A pointer to the size, in bytes, of the ImageInfo buffer.\r
411 On input, this is the size of the buffer allocated by the caller.\r
412 On output, it is the size of the buffer returned by the firmware\r
413 if the buffer was large enough, or the size of the buffer needed\r
414 to contain the image(s) information if the buffer was too small.\r
415 @param[in, out] ImageInfo A pointer to the buffer in which firmware places the current image(s)\r
416 information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.\r
417 @param[out] DescriptorVersion A pointer to the location in which firmware returns the version number\r
418 associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
419 @param[out] DescriptorCount A pointer to the location in which firmware returns the number of\r
420 descriptors or firmware images within this device.\r
421 @param[out] DescriptorSize A pointer to the location in which firmware returns the size, in bytes,\r
422 of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
423 @param[out] PackageVersion A version number that represents all the firmware images in the device.\r
424 The format is vendor specific and new version must have a greater value\r
425 than the old version. If PackageVersion is not supported, the value is\r
426 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison\r
427 is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates\r
428 that package version update is in progress.\r
429 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing the\r
430 package version name. The buffer is allocated by this function with\r
431 AllocatePool(), and it is the caller's responsibility to free it with a call\r
432 to FreePool().\r
433\r
434 @retval EFI_SUCCESS The device was successfully updated with the new image.\r
435 @retval EFI_BUFFER_TOO_SMALL The ImageInfo buffer was too small. The current buffer size\r
436 needed to hold the image(s) information is returned in ImageInfoSize.\r
437 @retval EFI_INVALID_PARAMETER ImageInfoSize is NULL.\r
438 @retval EFI_DEVICE_ERROR Valid information could not be returned. Possible corrupted image.\r
439\r
440**/\r
441EFI_STATUS\r
442EFIAPI\r
443GetTheImageInfo (\r
444 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
445 IN OUT UINTN *ImageInfoSize,\r
446 IN OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageInfo,\r
447 OUT UINT32 *DescriptorVersion,\r
448 OUT UINT8 *DescriptorCount,\r
449 OUT UINTN *DescriptorSize,\r
450 OUT UINT32 *PackageVersion,\r
451 OUT CHAR16 **PackageVersionName\r
452 )\r
453{\r
4f0544b1
EJ
454 EFI_STATUS Status;\r
455 FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;\r
b0bacc00
KM
456\r
457 Status = EFI_SUCCESS;\r
458\r
b4b9496b
MK
459 if (This == NULL) {\r
460 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImageInfo() - This is NULL.\n", mImageIdName));\r
461 Status = EFI_INVALID_PARAMETER;\r
462 goto cleanup;\r
463 }\r
464\r
4f0544b1
EJ
465 //\r
466 // Retrieve the private context structure\r
467 //\r
468 Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);\r
469 FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);\r
470\r
b0bacc00
KM
471 //\r
472 // Check for valid pointer\r
473 //\r
474 if (ImageInfoSize == NULL) {\r
e0961677 475 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImageInfo() - ImageInfoSize is NULL.\n", mImageIdName));\r
b0bacc00
KM
476 Status = EFI_INVALID_PARAMETER;\r
477 goto cleanup;\r
478 }\r
479\r
480 //\r
481 // Check the buffer size\r
482 // NOTE: Check this first so caller can get the necessary memory size it must allocate.\r
483 //\r
484 if (*ImageInfoSize < (sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR))) {\r
485 *ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);\r
e0961677 486 DEBUG ((DEBUG_VERBOSE, "FmpDxe(%s): GetImageInfo() - ImageInfoSize is to small.\n", mImageIdName));\r
b0bacc00
KM
487 Status = EFI_BUFFER_TOO_SMALL;\r
488 goto cleanup;\r
489 }\r
490\r
491 //\r
492 // Confirm that buffer isn't null\r
493 //\r
494 if ( (ImageInfo == NULL) || (DescriptorVersion == NULL) || (DescriptorCount == NULL) || (DescriptorSize == NULL)\r
495 || (PackageVersion == NULL)) {\r
e0961677 496 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImageInfo() - Pointer Parameter is NULL.\n", mImageIdName));\r
b0bacc00
KM
497 Status = EFI_INVALID_PARAMETER;\r
498 goto cleanup;\r
499 }\r
500\r
501 //\r
502 // Set the size to whatever we need\r
503 //\r
504 *ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);\r
505\r
4f0544b1 506 //\r
67c1e5ee 507 // Make sure the descriptor has already been loaded or refreshed\r
4f0544b1
EJ
508 //\r
509 PopulateDescriptor (Private);\r
b0bacc00
KM
510\r
511 //\r
512 // Copy the image descriptor\r
513 //\r
4f0544b1 514 CopyMem (ImageInfo, &Private->Descriptor, sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR));\r
b0bacc00
KM
515\r
516 *DescriptorVersion = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;\r
517 *DescriptorCount = 1;\r
518 *DescriptorSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);\r
519 //\r
520 // means unsupported\r
521 //\r
522 *PackageVersion = 0xFFFFFFFF;\r
523\r
524 //\r
525 // Do not update PackageVersionName since it is not supported in this instance.\r
526 //\r
527\r
528cleanup:\r
529\r
530 return Status;\r
531}\r
532\r
533/**\r
534 Retrieves a copy of the current firmware image of the device.\r
535\r
536 This function allows a copy of the current firmware image to be created and saved.\r
537 The saved copy could later been used, for example, in firmware image recovery or rollback.\r
538\r
a6d73269
SZ
539 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
540 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.\r
b0bacc00 541 The number is between 1 and DescriptorCount.\r
a6d73269
SZ
542 @param[in, out] Image Points to the buffer where the current image is copied to.\r
543 @param[in, out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.\r
b0bacc00
KM
544 On return, points to the length of the image, in bytes.\r
545\r
546 @retval EFI_SUCCESS The device was successfully updated with the new image.\r
547 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the\r
548 image. The current buffer size needed to hold the image is returned\r
549 in ImageSize.\r
550 @retval EFI_INVALID_PARAMETER The Image was NULL.\r
551 @retval EFI_NOT_FOUND The current image is not copied to the buffer.\r
552 @retval EFI_UNSUPPORTED The operation is not supported.\r
5fc5867e 553 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
b0bacc00
KM
554\r
555**/\r
556EFI_STATUS\r
557EFIAPI\r
558GetTheImage (\r
559 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
560 IN UINT8 ImageIndex,\r
561 IN OUT VOID *Image,\r
562 IN OUT UINTN *ImageSize\r
563 )\r
564{\r
4f0544b1
EJ
565 EFI_STATUS Status;\r
566 FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;\r
567 UINTN Size;\r
b0bacc00 568\r
11d35494
EJ
569 if (!FeaturePcdGet (PcdFmpDeviceStorageAccessEnable)) {\r
570 return EFI_UNSUPPORTED;\r
571 }\r
572\r
0f30087b 573 Status = EFI_SUCCESS;\r
b0bacc00 574\r
b4b9496b
MK
575 if (This == NULL) {\r
576 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - This is NULL.\n", mImageIdName));\r
577 Status = EFI_INVALID_PARAMETER;\r
578 goto cleanup;\r
579 }\r
580\r
4f0544b1
EJ
581 //\r
582 // Retrieve the private context structure\r
583 //\r
584 Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);\r
585 FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);\r
586\r
ebfac291
SZ
587 //\r
588 // Check to make sure index is 1 (only 1 image for this device)\r
589 //\r
590 if (ImageIndex != 1) {\r
e0961677 591 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - Image Index Invalid.\n", mImageIdName));\r
ebfac291
SZ
592 Status = EFI_INVALID_PARAMETER;\r
593 goto cleanup;\r
594 }\r
595\r
03340683 596 if (ImageSize == NULL) {\r
e0961677 597 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - ImageSize Pointer Parameter is NULL.\n", mImageIdName));\r
b0bacc00
KM
598 Status = EFI_INVALID_PARAMETER;\r
599 goto cleanup;\r
600 }\r
601\r
602 //\r
603 // Check the buffer size\r
604 //\r
605 Status = FmpDeviceGetSize (&Size);\r
606 if (EFI_ERROR (Status)) {\r
607 Size = 0;\r
608 }\r
0f30087b
WX
609 if (*ImageSize < Size) {\r
610 *ImageSize = Size;\r
e0961677 611 DEBUG ((DEBUG_VERBOSE, "FmpDxe(%s): GetImage() - ImageSize is to small.\n", mImageIdName));\r
b0bacc00
KM
612 Status = EFI_BUFFER_TOO_SMALL;\r
613 goto cleanup;\r
614 }\r
615\r
616 if (Image == NULL) {\r
e0961677 617 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - Image Pointer Parameter is NULL.\n", mImageIdName));\r
b0bacc00
KM
618 Status = EFI_INVALID_PARAMETER;\r
619 goto cleanup;\r
620 }\r
621\r
0f30087b 622 Status = FmpDeviceGetImage (Image, ImageSize);\r
b0bacc00
KM
623cleanup:\r
624\r
625 return Status;\r
626}\r
627\r
628/**\r
629 Helper function to safely retrieve the FMP header from\r
630 within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.\r
631\r
0f30087b
WX
632 @param[in] Image Pointer to the image.\r
633 @param[in] ImageSize Size of the image.\r
634 @param[in] AdditionalHeaderSize Size of any headers that cannot be calculated by this function.\r
b4b9496b
MK
635 @param[out] PayloadSize An optional pointer to a UINTN that holds the size of the payload\r
636 (image size minus headers)\r
b0bacc00
KM
637\r
638 @retval !NULL Valid pointer to the header.\r
639 @retval NULL Structure is bad and pointer cannot be found.\r
640\r
641**/\r
642VOID *\r
643GetFmpHeader (\r
644 IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,\r
645 IN CONST UINTN ImageSize,\r
0f30087b 646 IN CONST UINTN AdditionalHeaderSize,\r
b4b9496b 647 OUT UINTN *PayloadSize OPTIONAL\r
b0bacc00
KM
648 )\r
649{\r
650 //\r
651 // Check to make sure that operation can be safely performed.\r
652 //\r
0f30087b
WX
653 if (((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) + AdditionalHeaderSize < (UINTN)Image || \\r
654 ((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) + AdditionalHeaderSize >= (UINTN)Image + ImageSize) {\r
b0bacc00
KM
655 //\r
656 // Pointer overflow. Invalid image.\r
657 //\r
658 return NULL;\r
659 }\r
660\r
b4b9496b
MK
661 if (PayloadSize != NULL) {\r
662 *PayloadSize = ImageSize - (sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength + AdditionalHeaderSize);\r
663 }\r
664\r
0f30087b 665 return (VOID *)((UINT8 *)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength + AdditionalHeaderSize);\r
b0bacc00
KM
666}\r
667\r
668/**\r
669 Helper function to safely calculate the size of all headers\r
670 within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.\r
671\r
672 @param[in] Image Pointer to the image.\r
673 @param[in] AdditionalHeaderSize Size of any headers that cannot be calculated by this function.\r
674\r
675 @retval UINT32>0 Valid size of all the headers.\r
676 @retval 0 Structure is bad and size cannot be found.\r
677\r
678**/\r
679UINT32\r
680GetAllHeaderSize (\r
681 IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,\r
682 IN UINT32 AdditionalHeaderSize\r
683 )\r
684{\r
685 UINT32 CalculatedSize;\r
686\r
b4b9496b
MK
687 if (Image == NULL) {\r
688 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetAllHeaderSize() - Image is NULL.\n", mImageIdName));\r
689 return 0;\r
690 }\r
691\r
b0bacc00
KM
692 CalculatedSize = sizeof (Image->MonotonicCount) +\r
693 AdditionalHeaderSize +\r
694 Image->AuthInfo.Hdr.dwLength;\r
695\r
696 //\r
697 // Check to make sure that operation can be safely performed.\r
698 //\r
699 if (CalculatedSize < sizeof (Image->MonotonicCount) ||\r
700 CalculatedSize < AdditionalHeaderSize ||\r
701 CalculatedSize < Image->AuthInfo.Hdr.dwLength ) {\r
702 //\r
703 // Integer overflow. Invalid image.\r
704 //\r
705 return 0;\r
706 }\r
707\r
708 return CalculatedSize;\r
709}\r
710\r
711/**\r
712 Checks if the firmware image is valid for the device.\r
713\r
714 This function allows firmware update application to validate the firmware image without\r
715 invoking the SetImage() first.\r
716\r
717 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
718 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.\r
719 The number is between 1 and DescriptorCount.\r
720 @param[in] Image Points to the new image.\r
721 @param[in] ImageSize Size of the new image in bytes.\r
722 @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,\r
723 if available, additional information if the image is invalid.\r
5550f4d3
MK
724 @param[out] LastAttemptStatus A pointer to a UINT32 that holds the last attempt status to report\r
725 back to the ESRT table in case of error. If an error does not occur,\r
726 this function will set the value to LAST_ATTEMPT_STATUS_SUCCESS.\r
727\r
728 This function will return error codes that occur within this function\r
729 implementation within a driver range of last attempt error codes from\r
730 LAST_ATTEMPT_STATUS_DRIVER_MIN_ERROR_CODE_VALUE\r
731 to LAST_ATTEMPT_STATUS_DRIVER_MAX_ERROR_CODE_VALUE.\r
b0bacc00
KM
732\r
733 @retval EFI_SUCCESS The image was successfully checked.\r
4e61b8d0 734 @retval EFI_ABORTED The operation is aborted.\r
b0bacc00
KM
735 @retval EFI_INVALID_PARAMETER The Image was NULL.\r
736 @retval EFI_UNSUPPORTED The operation is not supported.\r
5fc5867e 737 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
b0bacc00
KM
738\r
739**/\r
740EFI_STATUS\r
741EFIAPI\r
5550f4d3 742CheckTheImageInternal (\r
b0bacc00
KM
743 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
744 IN UINT8 ImageIndex,\r
745 IN CONST VOID *Image,\r
746 IN UINTN ImageSize,\r
5550f4d3
MK
747 OUT UINT32 *ImageUpdatable,\r
748 OUT UINT32 *LastAttemptStatus\r
b0bacc00
KM
749 )\r
750{\r
4f0544b1 751 EFI_STATUS Status;\r
5550f4d3 752 UINT32 LocalLastAttemptStatus;\r
4f0544b1
EJ
753 FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;\r
754 UINTN RawSize;\r
755 VOID *FmpPayloadHeader;\r
756 UINTN FmpPayloadSize;\r
757 UINT32 Version;\r
758 UINT32 FmpHeaderSize;\r
759 UINTN AllHeaderSize;\r
760 UINT32 Index;\r
761 VOID *PublicKeyData;\r
762 UINTN PublicKeyDataLength;\r
763 UINT8 *PublicKeyDataXdr;\r
764 UINT8 *PublicKeyDataXdrEnd;\r
2ed845b3
WX
765 EFI_FIRMWARE_IMAGE_DEP *Dependencies;\r
766 UINT32 DependenciesSize;\r
b0bacc00 767\r
5550f4d3
MK
768 Status = EFI_SUCCESS;\r
769 LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;\r
770 RawSize = 0;\r
771 FmpPayloadHeader = NULL;\r
772 FmpPayloadSize = 0;\r
773 Version = 0;\r
774 FmpHeaderSize = 0;\r
775 AllHeaderSize = 0;\r
776 Dependencies = NULL;\r
777 DependenciesSize = 0;\r
b0bacc00 778\r
11d35494
EJ
779 if (!FeaturePcdGet (PcdFmpDeviceStorageAccessEnable)) {\r
780 return EFI_UNSUPPORTED;\r
781 }\r
782\r
5550f4d3
MK
783 if (LastAttemptStatus == NULL) {\r
784 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImageInternal() - LastAttemptStatus is NULL.\n", mImageIdName));\r
785 Status = EFI_INVALID_PARAMETER;\r
786 goto cleanup;\r
787 }\r
788\r
789 //\r
790 // A last attempt status error code will always override the success\r
791 // value before returning from the function\r
792 //\r
793 *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;\r
794\r
b4b9496b
MK
795 if (This == NULL) {\r
796 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - This is NULL.\n", mImageIdName));\r
797 Status = EFI_INVALID_PARAMETER;\r
5550f4d3 798 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_PROTOCOL_ARG_MISSING;\r
b4b9496b
MK
799 goto cleanup;\r
800 }\r
801\r
4f0544b1
EJ
802 //\r
803 // Retrieve the private context structure\r
804 //\r
805 Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);\r
806 FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);\r
807\r
b0bacc00 808 //\r
67c1e5ee 809 // Make sure the descriptor has already been loaded or refreshed\r
b0bacc00 810 //\r
4f0544b1 811 PopulateDescriptor (Private);\r
b0bacc00 812\r
a6d73269 813 if (ImageUpdatable == NULL) {\r
e0961677 814 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - ImageUpdatable Pointer Parameter is NULL.\n", mImageIdName));\r
b0bacc00 815 Status = EFI_INVALID_PARAMETER;\r
5550f4d3 816 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_IMAGE_NOT_UPDATABLE;\r
b0bacc00
KM
817 goto cleanup;\r
818 }\r
819\r
820 //\r
821 //Set to valid and then if any tests fail it will update this flag.\r
822 //\r
a6d73269 823 *ImageUpdatable = IMAGE_UPDATABLE_VALID;\r
b0bacc00 824\r
0f30087b
WX
825 //\r
826 // Set to satisfied and then if dependency evaluates to false it will update this flag.\r
827 //\r
828 Private->DependenciesSatisfied = TRUE;\r
829\r
b0bacc00 830 if (Image == NULL) {\r
e0961677 831 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - Image Pointer Parameter is NULL.\n", mImageIdName));\r
b0bacc00
KM
832 //\r
833 // not sure if this is needed\r
834 //\r
a6d73269 835 *ImageUpdatable = IMAGE_UPDATABLE_INVALID;\r
5550f4d3 836 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_IMAGE_NOT_PROVIDED;\r
b0bacc00
KM
837 return EFI_INVALID_PARAMETER;\r
838 }\r
839\r
840 PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);\r
841 PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);\r
842\r
843 if (PublicKeyDataXdr == NULL || (PublicKeyDataXdr == PublicKeyDataXdrEnd)) {\r
e0961677 844 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Invalid certificate, skipping it.\n", mImageIdName));\r
b0bacc00 845 Status = EFI_ABORTED;\r
5550f4d3 846 LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_CERTIFICATE;\r
b0bacc00
KM
847 } else {\r
848 //\r
849 // Try each key from PcdFmpDevicePkcs7CertBufferXdr\r
850 //\r
851 for (Index = 1; PublicKeyDataXdr < PublicKeyDataXdrEnd; Index++) {\r
852 Index++;\r
853 DEBUG (\r
854 (DEBUG_INFO,\r
e0961677
EJ
855 "FmpDxe(%s): Certificate #%d [%p..%p].\n",\r
856 mImageIdName,\r
b0bacc00
KM
857 Index,\r
858 PublicKeyDataXdr,\r
859 PublicKeyDataXdrEnd\r
860 )\r
861 );\r
862\r
863 if ((PublicKeyDataXdr + sizeof (UINT32)) > PublicKeyDataXdrEnd) {\r
864 //\r
865 // Key data extends beyond end of PCD\r
866 //\r
e0961677 867 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Certificate size extends beyond end of PCD, skipping it.\n", mImageIdName));\r
b0bacc00 868 Status = EFI_ABORTED;\r
5550f4d3 869 LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_KEY_LENGTH_VALUE;\r
b0bacc00
KM
870 break;\r
871 }\r
872 //\r
873 // Read key length stored in big-endian format\r
874 //\r
875 PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));\r
876 //\r
877 // Point to the start of the key data\r
878 //\r
879 PublicKeyDataXdr += sizeof (UINT32);\r
880 if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {\r
881 //\r
882 // Key data extends beyond end of PCD\r
883 //\r
e0961677 884 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Certificate extends beyond end of PCD, skipping it.\n", mImageIdName));\r
b0bacc00 885 Status = EFI_ABORTED;\r
5550f4d3 886 LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_KEY_LENGTH;\r
b0bacc00
KM
887 break;\r
888 }\r
889 PublicKeyData = PublicKeyDataXdr;\r
890 Status = AuthenticateFmpImage (\r
891 (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image,\r
892 ImageSize,\r
893 PublicKeyData,\r
894 PublicKeyDataLength\r
895 );\r
896 if (!EFI_ERROR (Status)) {\r
897 break;\r
898 }\r
899 PublicKeyDataXdr += PublicKeyDataLength;\r
900 PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));\r
901 }\r
902 }\r
903\r
904 if (EFI_ERROR (Status)) {\r
e0961677 905 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - Authentication Failed %r.\n", mImageIdName, Status));\r
5550f4d3
MK
906 if (LocalLastAttemptStatus != LAST_ATTEMPT_STATUS_SUCCESS) {\r
907 *LastAttemptStatus = LocalLastAttemptStatus;\r
908 } else {\r
909 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_IMAGE_AUTH_FAILURE;\r
910 }\r
b0bacc00
KM
911 goto cleanup;\r
912 }\r
913\r
914 //\r
915 // Check to make sure index is 1\r
916 //\r
917 if (ImageIndex != 1) {\r
e0961677 918 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - Image Index Invalid.\n", mImageIdName));\r
a6d73269 919 *ImageUpdatable = IMAGE_UPDATABLE_INVALID_TYPE;\r
b4b9496b 920 Status = EFI_INVALID_PARAMETER;\r
5550f4d3 921 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_IMAGE_INDEX;\r
b0bacc00
KM
922 goto cleanup;\r
923 }\r
924\r
0f30087b
WX
925 //\r
926 // Get the dependency from Image.\r
927 //\r
928 Dependencies = GetImageDependency ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &DependenciesSize);\r
b0bacc00
KM
929\r
930 //\r
931 // Check the FmpPayloadHeader\r
932 //\r
0f30087b 933 FmpPayloadHeader = GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, DependenciesSize, &FmpPayloadSize );\r
b0bacc00 934 if (FmpPayloadHeader == NULL) {\r
e0961677 935 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - GetFmpHeader failed.\n", mImageIdName));\r
b0bacc00 936 Status = EFI_ABORTED;\r
5550f4d3 937 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER;\r
b0bacc00
KM
938 goto cleanup;\r
939 }\r
940 Status = GetFmpPayloadHeaderVersion (FmpPayloadHeader, FmpPayloadSize, &Version);\r
941 if (EFI_ERROR (Status)) {\r
0f30087b
WX
942 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - GetFmpPayloadHeaderVersion failed %r.\n", mImageIdName, Status));\r
943 *ImageUpdatable = IMAGE_UPDATABLE_INVALID;\r
944 Status = EFI_SUCCESS;\r
5550f4d3 945 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER_VERSION;\r
0f30087b 946 goto cleanup;\r
b0bacc00
KM
947 }\r
948\r
949 //\r
950 // Check the lowest supported version\r
951 //\r
4f0544b1 952 if (Version < Private->Descriptor.LowestSupportedImageVersion) {\r
b0bacc00
KM
953 DEBUG (\r
954 (DEBUG_ERROR,\r
e0961677
EJ
955 "FmpDxe(%s): CheckTheImage() - Version Lower than lowest supported version. 0x%08X < 0x%08X\n",\r
956 mImageIdName, Version, Private->Descriptor.LowestSupportedImageVersion)\r
b0bacc00 957 );\r
a6d73269 958 *ImageUpdatable = IMAGE_UPDATABLE_INVALID_OLD;\r
b0bacc00 959 Status = EFI_SUCCESS;\r
5550f4d3 960 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_VERSION_TOO_LOW;\r
b0bacc00
KM
961 goto cleanup;\r
962 }\r
963\r
2ed845b3
WX
964 //\r
965 // Evaluate dependency expression\r
966 //\r
0f30087b
WX
967 Private->DependenciesSatisfied = CheckFmpDependency (Private->Descriptor.ImageTypeId, Version, Dependencies, DependenciesSize);\r
968 if (!Private->DependenciesSatisfied) {\r
969 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - Dependency check failed.\n", mImageIdName));\r
2ed845b3
WX
970 *ImageUpdatable = IMAGE_UPDATABLE_INVALID;\r
971 Status = EFI_SUCCESS;\r
972 goto cleanup;\r
973 }\r
974\r
b0bacc00
KM
975 //\r
976 // Get the FmpHeaderSize so we can determine the real payload size\r
977 //\r
978 Status = GetFmpPayloadHeaderSize (FmpPayloadHeader, FmpPayloadSize, &FmpHeaderSize);\r
979 if (EFI_ERROR (Status)) {\r
980 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status));\r
a6d73269 981 *ImageUpdatable = IMAGE_UPDATABLE_INVALID;\r
b0bacc00 982 Status = EFI_SUCCESS;\r
5550f4d3 983 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER_SIZE;\r
b0bacc00
KM
984 goto cleanup;\r
985 }\r
986\r
987 //\r
988 // Call FmpDevice Lib Check Image on the\r
989 // Raw payload. So all headers need stripped off\r
990 //\r
2ed845b3 991 AllHeaderSize = GetAllHeaderSize ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize + DependenciesSize);\r
b0bacc00 992 if (AllHeaderSize == 0) {\r
e0961677 993 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - GetAllHeaderSize failed.\n", mImageIdName));\r
b0bacc00 994 Status = EFI_ABORTED;\r
5550f4d3 995 *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_ALL_HEADER_SIZE;\r
b0bacc00
KM
996 goto cleanup;\r
997 }\r
998 RawSize = ImageSize - AllHeaderSize;\r
999\r
1000 //\r
1001 // FmpDeviceLib CheckImage function to do any specific checks\r
1002 //\r
a6d73269 1003 Status = FmpDeviceCheckImage ((((UINT8 *)Image) + AllHeaderSize), RawSize, ImageUpdatable);\r
b0bacc00 1004 if (EFI_ERROR (Status)) {\r
e0961677 1005 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - FmpDeviceLib CheckImage failed. Status = %r\n", mImageIdName, Status));\r
b0bacc00
KM
1006 }\r
1007\r
1008cleanup:\r
1009 return Status;\r
1010}\r
1011\r
5550f4d3
MK
1012/**\r
1013 Checks if the firmware image is valid for the device.\r
1014\r
1015 This function allows firmware update application to validate the firmware image without\r
1016 invoking the SetImage() first.\r
1017\r
1018 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
1019 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.\r
1020 The number is between 1 and DescriptorCount.\r
1021 @param[in] Image Points to the new image.\r
1022 @param[in] ImageSize Size of the new image in bytes.\r
1023 @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,\r
1024 if available, additional information if the image is invalid.\r
1025\r
1026 @retval EFI_SUCCESS The image was successfully checked.\r
1027 @retval EFI_ABORTED The operation is aborted.\r
1028 @retval EFI_INVALID_PARAMETER The Image was NULL.\r
1029 @retval EFI_UNSUPPORTED The operation is not supported.\r
1030 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
1031\r
1032**/\r
1033EFI_STATUS\r
1034EFIAPI\r
1035CheckTheImage (\r
1036 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
1037 IN UINT8 ImageIndex,\r
1038 IN CONST VOID *Image,\r
1039 IN UINTN ImageSize,\r
1040 OUT UINT32 *ImageUpdatable\r
1041 )\r
1042{\r
1043 UINT32 LastAttemptStatus;\r
1044\r
1045 return CheckTheImageInternal (This, ImageIndex, Image, ImageSize, ImageUpdatable, &LastAttemptStatus);\r
1046}\r
1047\r
b0bacc00
KM
1048/**\r
1049 Updates the firmware image of the device.\r
1050\r
1051 This function updates the hardware with the new firmware image.\r
1052 This function returns EFI_UNSUPPORTED if the firmware image is not updatable.\r
1053 If the firmware image is updatable, the function should perform the following minimal validations\r
1054 before proceeding to do the firmware image update.\r
1055 - Validate the image authentication if image has attribute\r
1056 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns\r
1057 EFI_SECURITY_VIOLATION if the validation fails.\r
1058 - Validate the image is a supported image for this device. The function returns EFI_ABORTED if\r
1059 the image is unsupported. The function can optionally provide more detailed information on\r
1060 why the image is not a supported image.\r
1061 - Validate the data from VendorCode if not null. Image validation must be performed before\r
1062 VendorCode data validation. VendorCode data is ignored or considered invalid if image\r
1063 validation failed. The function returns EFI_ABORTED if the data is invalid.\r
1064\r
1065 VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if\r
1066 the caller did not specify the policy or use the default policy. As an example, vendor can implement\r
1067 a policy to allow an option to force a firmware image update when the abort reason is due to the new\r
1068 firmware image version is older than the current firmware image version or bad image checksum.\r
1069 Sensitive operations such as those wiping the entire firmware image and render the device to be\r
1070 non-functional should be encoded in the image itself rather than passed with the VendorCode.\r
1071 AbortReason enables vendor to have the option to provide a more detailed description of the abort\r
1072 reason to the caller.\r
1073\r
1074 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
1075 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.\r
1076 The number is between 1 and DescriptorCount.\r
1077 @param[in] Image Points to the new image.\r
1078 @param[in] ImageSize Size of the new image in bytes.\r
1079 @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.\r
1080 Null indicates the caller did not specify the policy or use the default policy.\r
1081 @param[in] Progress A function used by the driver to report the progress of the firmware update.\r
1082 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more\r
1083 details for the aborted operation. The buffer is allocated by this function\r
1084 with AllocatePool(), and it is the caller's responsibility to free it with a\r
1085 call to FreePool().\r
1086\r
1087 @retval EFI_SUCCESS The device was successfully updated with the new image.\r
1088 @retval EFI_ABORTED The operation is aborted.\r
1089 @retval EFI_INVALID_PARAMETER The Image was NULL.\r
1090 @retval EFI_UNSUPPORTED The operation is not supported.\r
5fc5867e 1091 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
b0bacc00
KM
1092\r
1093**/\r
1094EFI_STATUS\r
1095EFIAPI\r
1096SetTheImage (\r
1097 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
1098 IN UINT8 ImageIndex,\r
1099 IN CONST VOID *Image,\r
1100 IN UINTN ImageSize,\r
1101 IN CONST VOID *VendorCode,\r
1102 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,\r
1103 OUT CHAR16 **AbortReason\r
1104 )\r
1105{\r
4f0544b1
EJ
1106 EFI_STATUS Status;\r
1107 FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;\r
1108 UINT32 Updateable;\r
1109 BOOLEAN BooleanValue;\r
1110 UINT32 FmpHeaderSize;\r
1111 VOID *FmpHeader;\r
1112 UINTN FmpPayloadSize;\r
1113 UINT32 AllHeaderSize;\r
95d28836 1114 UINT32 IncomingFwVersion;\r
4f0544b1
EJ
1115 UINT32 LastAttemptStatus;\r
1116 UINT32 Version;\r
1117 UINT32 LowestSupportedVersion;\r
2ed845b3
WX
1118 EFI_FIRMWARE_IMAGE_DEP *Dependencies;\r
1119 UINT32 DependenciesSize;\r
b0bacc00
KM
1120\r
1121 Status = EFI_SUCCESS;\r
3633d530 1122 Private = NULL;\r
b0bacc00
KM
1123 Updateable = 0;\r
1124 BooleanValue = FALSE;\r
1125 FmpHeaderSize = 0;\r
1126 FmpHeader = NULL;\r
1127 FmpPayloadSize = 0;\r
1128 AllHeaderSize = 0;\r
2ed845b3 1129 IncomingFwVersion = 0;\r
b0bacc00 1130 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;\r
2ed845b3
WX
1131 Dependencies = NULL;\r
1132 DependenciesSize = 0;\r
b0bacc00 1133\r
11d35494
EJ
1134 if (!FeaturePcdGet (PcdFmpDeviceStorageAccessEnable)) {\r
1135 return EFI_UNSUPPORTED;\r
1136 }\r
1137\r
b4b9496b
MK
1138 if (This == NULL) {\r
1139 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - This is NULL.\n", mImageIdName));\r
1140 Status = EFI_INVALID_PARAMETER;\r
1141 goto cleanup;\r
1142 }\r
1143\r
4f0544b1
EJ
1144 //\r
1145 // Retrieve the private context structure\r
1146 //\r
1147 Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);\r
1148 FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);\r
b0bacc00 1149\r
67c1e5ee
EJ
1150 //\r
1151 // Make sure the descriptor has already been loaded or refreshed\r
1152 //\r
1153 PopulateDescriptor (Private);\r
1154\r
1155 //\r
1156 // Set to 0 to clear any previous results.\r
1157 //\r
95d28836 1158 SetLastAttemptVersionInVariable (Private, IncomingFwVersion);\r
b0bacc00
KM
1159\r
1160 //\r
1161 // if we have locked the device, then skip the set operation.\r
1162 // it should be blocked by hardware too but we can catch here even faster\r
1163 //\r
4f0544b1 1164 if (Private->FmpDeviceLocked) {\r
e0961677 1165 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Device is already locked. Can't update.\n", mImageIdName));\r
a4c35479 1166 Status = EFI_UNSUPPORTED;\r
b0bacc00
KM
1167 goto cleanup;\r
1168 }\r
1169\r
1170 //\r
1171 // Call check image to verify the image\r
1172 //\r
1173 Status = CheckTheImage (This, ImageIndex, Image, ImageSize, &Updateable);\r
1174 if (EFI_ERROR (Status)) {\r
e0961677 1175 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Check The Image failed with %r.\n", mImageIdName, Status));\r
b0bacc00
KM
1176 if (Status == EFI_SECURITY_VIOLATION) {\r
1177 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR;\r
1178 }\r
1179 goto cleanup;\r
1180 }\r
1181\r
0f30087b
WX
1182 //\r
1183 // Get the dependency from Image.\r
1184 //\r
1185 Dependencies = GetImageDependency ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &DependenciesSize);\r
1186\r
b0bacc00
KM
1187 //\r
1188 // No functional error in CheckTheImage. Attempt to get the Version to\r
1189 // support better error reporting.\r
1190 //\r
0f30087b 1191 FmpHeader = GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, DependenciesSize, &FmpPayloadSize );\r
b0bacc00 1192 if (FmpHeader == NULL) {\r
e0961677 1193 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - GetFmpHeader failed.\n", mImageIdName));\r
b0bacc00
KM
1194 Status = EFI_ABORTED;\r
1195 goto cleanup;\r
1196 }\r
95d28836 1197 Status = GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &IncomingFwVersion);\r
b0bacc00
KM
1198 if (!EFI_ERROR (Status)) {\r
1199 //\r
1200 // Set to actual value\r
1201 //\r
95d28836 1202 SetLastAttemptVersionInVariable (Private, IncomingFwVersion);\r
b0bacc00
KM
1203 }\r
1204\r
1205\r
1206 if (Updateable != IMAGE_UPDATABLE_VALID) {\r
1207 DEBUG (\r
1208 (DEBUG_ERROR,\r
e0961677
EJ
1209 "FmpDxe(%s): SetTheImage() - Check The Image returned that the Image was not valid for update. Updatable value = 0x%X.\n",\r
1210 mImageIdName, Updateable)\r
b0bacc00 1211 );\r
0f30087b 1212 if (Private->DependenciesSatisfied == FALSE) {\r
2ed845b3 1213 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSATISFIED_DEPENDENCIES;\r
2ed845b3 1214 }\r
b0bacc00
KM
1215 Status = EFI_ABORTED;\r
1216 goto cleanup;\r
1217 }\r
1218\r
1219 if (Progress == NULL) {\r
e0961677 1220 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Invalid progress callback\n", mImageIdName));\r
b0bacc00
KM
1221 Status = EFI_INVALID_PARAMETER;\r
1222 goto cleanup;\r
1223 }\r
1224\r
1225 mProgressFunc = Progress;\r
b0bacc00
KM
1226\r
1227 //\r
1228 // Checking the image is at least 1%\r
1229 //\r
1230 Status = Progress (1);\r
1231 if (EFI_ERROR (Status)) {\r
e0961677 1232 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Progress Callback failed with Status %r.\n", mImageIdName, Status));\r
b0bacc00
KM
1233 }\r
1234\r
1235 //\r
1236 //Check System Power\r
1237 //\r
1238 Status = CheckSystemPower (&BooleanValue);\r
1239 if (EFI_ERROR (Status)) {\r
e0961677 1240 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - CheckSystemPower - API call failed %r.\n", mImageIdName, Status));\r
b0bacc00
KM
1241 goto cleanup;\r
1242 }\r
1243 if (!BooleanValue) {\r
1244 Status = EFI_ABORTED;\r
1245 DEBUG (\r
1246 (DEBUG_ERROR,\r
e0961677 1247 "FmpDxe(%s): SetTheImage() - CheckSystemPower - returned False. Update not allowed due to System Power.\n", mImageIdName)\r
b0bacc00
KM
1248 );\r
1249 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_PWR_EVT_BATT;\r
1250 goto cleanup;\r
1251 }\r
1252\r
1253 Progress (2);\r
1254\r
1255 //\r
1256 //Check System Thermal\r
1257 //\r
1258 Status = CheckSystemThermal (&BooleanValue);\r
1259 if (EFI_ERROR (Status)) {\r
e0961677 1260 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - CheckSystemThermal - API call failed %r.\n", mImageIdName, Status));\r
b0bacc00
KM
1261 goto cleanup;\r
1262 }\r
1263 if (!BooleanValue) {\r
1264 Status = EFI_ABORTED;\r
1265 DEBUG (\r
1266 (DEBUG_ERROR,\r
e0961677 1267 "FmpDxe(%s): SetTheImage() - CheckSystemThermal - returned False. Update not allowed due to System Thermal.\n", mImageIdName)\r
b0bacc00
KM
1268 );\r
1269 goto cleanup;\r
1270 }\r
1271\r
1272 Progress (3);\r
1273\r
1274 //\r
1275 //Check System Environment\r
1276 //\r
1277 Status = CheckSystemEnvironment (&BooleanValue);\r
1278 if (EFI_ERROR (Status)) {\r
e0961677 1279 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - CheckSystemEnvironment - API call failed %r.\n", mImageIdName, Status));\r
b0bacc00
KM
1280 goto cleanup;\r
1281 }\r
1282 if (!BooleanValue) {\r
1283 Status = EFI_ABORTED;\r
1284 DEBUG (\r
1285 (DEBUG_ERROR,\r
e0961677 1286 "FmpDxe(%s): SetTheImage() - CheckSystemEnvironment - returned False. Update not allowed due to System Environment.\n", mImageIdName)\r
b0bacc00
KM
1287 );\r
1288 goto cleanup;\r
1289 }\r
1290\r
1291 Progress (4);\r
1292\r
1293 //\r
1294 // Save LastAttemptStatus as error so that if SetImage never returns the error\r
1295 // state is recorded.\r
1296 //\r
67c1e5ee 1297 SetLastAttemptStatusInVariable (Private, LastAttemptStatus);\r
b0bacc00
KM
1298\r
1299 //\r
1300 // Strip off all the headers so the device can process its firmware\r
1301 //\r
1302 Status = GetFmpPayloadHeaderSize (FmpHeader, FmpPayloadSize, &FmpHeaderSize);\r
1303 if (EFI_ERROR (Status)) {\r
e0961677 1304 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - GetFmpPayloadHeaderSize failed %r.\n", mImageIdName, Status));\r
b0bacc00
KM
1305 goto cleanup;\r
1306 }\r
1307\r
2ed845b3 1308 AllHeaderSize = GetAllHeaderSize ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize + DependenciesSize);\r
b0bacc00 1309 if (AllHeaderSize == 0) {\r
e0961677 1310 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - GetAllHeaderSize failed.\n", mImageIdName));\r
b0bacc00
KM
1311 Status = EFI_ABORTED;\r
1312 goto cleanup;\r
1313 }\r
1314\r
1315 //\r
1316 // Indicate that control is handed off to FmpDeviceLib\r
1317 //\r
1318 Progress (5);\r
1319\r
1320 //\r
1321 //Copy the requested image to the firmware using the FmpDeviceLib\r
1322 //\r
1323 Status = FmpDeviceSetImage (\r
0f30087b
WX
1324 (((UINT8 *)Image) + AllHeaderSize),\r
1325 ImageSize - AllHeaderSize,\r
b0bacc00
KM
1326 VendorCode,\r
1327 FmpDxeProgress,\r
95d28836 1328 IncomingFwVersion,\r
b0bacc00
KM
1329 AbortReason\r
1330 );\r
1331 if (EFI_ERROR (Status)) {\r
e0961677 1332 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() SetImage from FmpDeviceLib failed. Status = %r.\n", mImageIdName, Status));\r
b0bacc00
KM
1333 goto cleanup;\r
1334 }\r
1335\r
0f30087b
WX
1336 //\r
1337 // Store the dependency\r
1338 //\r
1339 if (Private->Descriptor.AttributesSetting & IMAGE_ATTRIBUTE_DEPENDENCY) {\r
1340 Status = SaveFmpDependency (Dependencies, DependenciesSize);\r
1341 if (EFI_ERROR (Status)) {\r
1342 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() SaveFmpDependency from FmpDependencyCheckLib failed. (%r)\n", mImageIdName, Status));\r
1343 }\r
1344 Status = EFI_SUCCESS;\r
1345 }\r
b0bacc00
KM
1346\r
1347 //\r
1348 // Finished the update without error\r
1349 // Indicate that control has been returned from FmpDeviceLib\r
1350 //\r
1351 Progress (99);\r
1352\r
1353 //\r
1354 // Update the version stored in variable\r
1355 //\r
4f0544b1 1356 if (!Private->RuntimeVersionSupported) {\r
a6d73269 1357 Version = DEFAULT_VERSION;\r
b0bacc00 1358 GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &Version);\r
67c1e5ee 1359 SetVersionInVariable (Private, Version);\r
b0bacc00
KM
1360 }\r
1361\r
1362 //\r
1363 // Update lowest supported variable\r
1364 //\r
67c1e5ee
EJ
1365 LowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;\r
1366 GetFmpPayloadHeaderLowestSupportedVersion (FmpHeader, FmpPayloadSize, &LowestSupportedVersion);\r
1367 SetLowestSupportedVersionInVariable (Private, LowestSupportedVersion);\r
b0bacc00
KM
1368\r
1369 LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;\r
1370\r
b0bacc00
KM
1371cleanup:\r
1372 mProgressFunc = NULL;\r
3633d530
MK
1373\r
1374 if (Private != NULL) {\r
1375 SetLastAttemptStatusInVariable (Private, LastAttemptStatus);\r
1376 }\r
b0bacc00 1377\r
c6c18d87
SZ
1378 if (Progress != NULL) {\r
1379 //\r
1380 // Set progress to 100 after everything is done including recording Status.\r
1381 //\r
1382 Progress (100);\r
1383 }\r
b0bacc00 1384\r
27e42bf6
SZ
1385 //\r
1386 // Need repopulate after SetImage is called to\r
1387 // update LastAttemptVersion and LastAttemptStatus.\r
1388 //\r
3633d530
MK
1389 if (Private != NULL) {\r
1390 Private->DescriptorPopulated = FALSE;\r
1391 }\r
27e42bf6 1392\r
b0bacc00
KM
1393 return Status;\r
1394}\r
1395\r
1396/**\r
1397 Returns information about the firmware package.\r
1398\r
1399 This function returns package information.\r
1400\r
1401 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
1402 @param[out] PackageVersion A version number that represents all the firmware images in the device.\r
1403 The format is vendor specific and new version must have a greater value\r
1404 than the old version. If PackageVersion is not supported, the value is\r
1405 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version\r
1406 comparison is to be performed using PackageVersionName. A value of\r
1407 0xFFFFFFFD indicates that package version update is in progress.\r
1408 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing\r
1409 the package version name. The buffer is allocated by this function with\r
1410 AllocatePool(), and it is the caller's responsibility to free it with a\r
1411 call to FreePool().\r
1412 @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of\r
1413 package version name. A value of 0 indicates the device does not support\r
1414 update of package version name. Length is the number of Unicode characters,\r
1415 including the terminating null character.\r
1416 @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute\r
1417 Definitions' for possible returned values of this parameter. A value of 1\r
1418 indicates the attribute is supported and the current setting value is\r
1419 indicated in AttributesSetting. A value of 0 indicates the attribute is not\r
1420 supported and the current setting value in AttributesSetting is meaningless.\r
1421 @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned\r
1422 values of this parameter\r
1423\r
1424 @retval EFI_SUCCESS The package information was successfully returned.\r
1425 @retval EFI_UNSUPPORTED The operation is not supported.\r
1426\r
1427**/\r
1428EFI_STATUS\r
1429EFIAPI\r
1430GetPackageInfo (\r
1431 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
1432 OUT UINT32 *PackageVersion,\r
1433 OUT CHAR16 **PackageVersionName,\r
1434 OUT UINT32 *PackageVersionNameMaxLen,\r
1435 OUT UINT64 *AttributesSupported,\r
1436 OUT UINT64 *AttributesSetting\r
1437 )\r
1438{\r
1439 return EFI_UNSUPPORTED;\r
1440}\r
1441\r
1442/**\r
1443 Updates information about the firmware package.\r
1444\r
1445 This function updates package information.\r
1446 This function returns EFI_UNSUPPORTED if the package information is not updatable.\r
1447 VendorCode enables vendor to implement vendor-specific package information update policy.\r
1448 Null if the caller did not specify this policy or use the default policy.\r
1449\r
1450 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
1451 @param[in] Image Points to the authentication image.\r
1452 Null if authentication is not required.\r
1453 @param[in] ImageSize Size of the authentication image in bytes.\r
1454 0 if authentication is not required.\r
1455 @param[in] VendorCode This enables vendor to implement vendor-specific firmware\r
1456 image update policy.\r
1457 Null indicates the caller did not specify this policy or use\r
1458 the default policy.\r
1459 @param[in] PackageVersion The new package version.\r
1460 @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing\r
1461 the package version name.\r
1462 The string length is equal to or less than the value returned in\r
1463 PackageVersionNameMaxLen.\r
1464\r
1465 @retval EFI_SUCCESS The device was successfully updated with the new package\r
1466 information.\r
1467 @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value\r
1468 returned in PackageVersionNameMaxLen.\r
1469 @retval EFI_UNSUPPORTED The operation is not supported.\r
5fc5867e 1470 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
b0bacc00
KM
1471\r
1472**/\r
1473EFI_STATUS\r
1474EFIAPI\r
1475SetPackageInfo (\r
1476 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
1477 IN CONST VOID *Image,\r
1478 IN UINTN ImageSize,\r
1479 IN CONST VOID *VendorCode,\r
1480 IN UINT32 PackageVersion,\r
1481 IN CONST CHAR16 *PackageVersionName\r
1482 )\r
1483{\r
1484 return EFI_UNSUPPORTED;\r
1485}\r
1486\r
1487/**\r
1488 Event notification function that is invoked when the event GUID specified by\r
1489 PcdFmpDeviceLockEventGuid is signaled.\r
1490\r
1491 @param[in] Event Event whose notification function is being invoked.\r
1492 @param[in] Context The pointer to the notification function's context,\r
1493 which is implementation-dependent.\r
1494**/\r
1495VOID\r
1496EFIAPI\r
1497FmpDxeLockEventNotify (\r
1498 IN EFI_EVENT Event,\r
1499 IN VOID *Context\r
1500 )\r
1501{\r
4f0544b1
EJ
1502 EFI_STATUS Status;\r
1503 FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;\r
1504\r
b4b9496b
MK
1505 if (Context == NULL) {\r
1506 ASSERT (Context != NULL);\r
1507 return;\r
1508 }\r
1509\r
4f0544b1 1510 Private = (FIRMWARE_MANAGEMENT_PRIVATE_DATA *)Context;\r
b0bacc00 1511\r
4f0544b1 1512 if (!Private->FmpDeviceLocked) {\r
9e6c4f15
SZ
1513 //\r
1514 // Lock the firmware device\r
1515 //\r
4f0544b1 1516 FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);\r
9e6c4f15
SZ
1517 Status = FmpDeviceLock();\r
1518 if (EFI_ERROR (Status)) {\r
1519 if (Status != EFI_UNSUPPORTED) {\r
e0961677 1520 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLock() returned error. Status = %r\n", mImageIdName, Status));\r
b0bacc00 1521 } else {\r
e0961677 1522 DEBUG ((DEBUG_WARN, "FmpDxe(%s): FmpDeviceLock() returned error. Status = %r\n", mImageIdName, Status));\r
b0bacc00 1523 }\r
b0bacc00 1524 }\r
4f0544b1 1525 Private->FmpDeviceLocked = TRUE;\r
b0bacc00
KM
1526 }\r
1527}\r
1528\r
1529/**\r
1530 Function to install FMP instance.\r
1531\r
1532 @param[in] Handle The device handle to install a FMP instance on.\r
1533\r
1534 @retval EFI_SUCCESS FMP Installed\r
1535 @retval EFI_INVALID_PARAMETER Handle was invalid\r
1536 @retval other Error installing FMP\r
1537\r
1538**/\r
1539EFI_STATUS\r
1540EFIAPI\r
1541InstallFmpInstance (\r
1542 IN EFI_HANDLE Handle\r
1543 )\r
1544{\r
4f0544b1
EJ
1545 EFI_STATUS Status;\r
1546 EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;\r
1547 FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;\r
b0bacc00 1548\r
b0bacc00
KM
1549 //\r
1550 // Only allow a single FMP Protocol instance to be installed\r
1551 //\r
4f0544b1
EJ
1552 Status = gBS->OpenProtocol (\r
1553 Handle,\r
1554 &gEfiFirmwareManagementProtocolGuid,\r
1555 (VOID **)&Fmp,\r
1556 NULL,\r
1557 NULL,\r
1558 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1559 );\r
1560 if (!EFI_ERROR (Status)) {\r
b0bacc00
KM
1561 return EFI_ALREADY_STARTED;\r
1562 }\r
1563\r
1564 //\r
1565 // Allocate FMP Protocol instance\r
1566 //\r
4f0544b1
EJ
1567 Private = AllocateCopyPool (\r
1568 sizeof (mFirmwareManagementPrivateDataTemplate),\r
1569 &mFirmwareManagementPrivateDataTemplate\r
1570 );\r
1571 if (Private == NULL) {\r
e0961677 1572 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to allocate memory for private structure.\n", mImageIdName));\r
b0bacc00
KM
1573 Status = EFI_OUT_OF_RESOURCES;\r
1574 goto cleanup;\r
1575 }\r
1576\r
1577 //\r
4f0544b1 1578 // Initialize private context data structure\r
b0bacc00 1579 //\r
4f0544b1 1580 Private->Handle = Handle;\r
4f0544b1
EJ
1581 Private->FmpDeviceContext = NULL;\r
1582 Status = FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);\r
1583 if (Status == EFI_UNSUPPORTED) {\r
1584 Private->FmpDeviceContext = NULL;\r
1585 } else if (EFI_ERROR (Status)) {\r
b0bacc00
KM
1586 goto cleanup;\r
1587 }\r
1588\r
67c1e5ee
EJ
1589 //\r
1590 // Make sure the descriptor has already been loaded or refreshed\r
1591 //\r
1592 PopulateDescriptor (Private);\r
1593\r
4f0544b1
EJ
1594 if (IsLockFmpDeviceAtLockEventGuidRequired ()) {\r
1595 //\r
e0961677 1596 // Register all UEFI Variables used by this module to be locked.\r
4f0544b1 1597 //\r
67c1e5ee 1598 Status = LockAllFmpVariables (Private);\r
4f0544b1 1599 if (EFI_ERROR (Status)) {\r
e0961677 1600 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to register variables to lock. Status = %r.\n", mImageIdName, Status));\r
4f0544b1 1601 } else {\r
e0961677 1602 DEBUG ((DEBUG_INFO, "FmpDxe(%s): All variables registered to lock\n", mImageIdName));\r
4f0544b1
EJ
1603 }\r
1604\r
1605 //\r
1606 // Create and register notify function to lock the FMP device.\r
1607 //\r
1608 Status = gBS->CreateEventEx (\r
1609 EVT_NOTIFY_SIGNAL,\r
1610 TPL_CALLBACK,\r
1611 FmpDxeLockEventNotify,\r
1612 Private,\r
1613 mLockGuid,\r
1614 &Private->FmpDeviceLockEvent\r
1615 );\r
1616 if (EFI_ERROR (Status)) {\r
e0961677 1617 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to register notification. Status = %r\n", mImageIdName, Status));\r
4f0544b1
EJ
1618 }\r
1619 ASSERT_EFI_ERROR (Status);\r
1620 } else {\r
e0961677 1621 DEBUG ((DEBUG_VERBOSE, "FmpDxe(%s): Not registering notification to call FmpDeviceLock() because mfg mode\n", mImageIdName));\r
4f0544b1 1622 }\r
b0bacc00
KM
1623\r
1624 //\r
1625 // Install FMP Protocol and FMP Progress Protocol\r
1626 //\r
1627 Status = gBS->InstallMultipleProtocolInterfaces (\r
4f0544b1
EJ
1628 &Private->Handle,\r
1629 &gEfiFirmwareManagementProtocolGuid, &Private->Fmp,\r
1630 &gEdkiiFirmwareManagementProgressProtocolGuid, &mFmpProgress,\r
b0bacc00
KM
1631 NULL\r
1632 );\r
b0bacc00 1633 if (EFI_ERROR (Status)) {\r
e0961677 1634 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Protocol install error. Status = %r.\n", mImageIdName, Status));\r
b0bacc00
KM
1635 goto cleanup;\r
1636 }\r
1637\r
b0bacc00
KM
1638cleanup:\r
1639\r
4f0544b1
EJ
1640 if (EFI_ERROR (Status)) {\r
1641 if (Private != NULL) {\r
1642 if (Private->FmpDeviceLockEvent != NULL) {\r
1643 gBS->CloseEvent (Private->FmpDeviceLockEvent);\r
1644 }\r
67c1e5ee
EJ
1645 if (Private->Descriptor.VersionName != NULL) {\r
1646 FreePool (Private->Descriptor.VersionName);\r
1647 }\r
1648 if (Private->FmpDeviceContext != NULL) {\r
1649 FmpDeviceSetContext (NULL, &Private->FmpDeviceContext);\r
1650 }\r
1651 if (Private->VersionVariableName != NULL) {\r
1652 FreePool (Private->VersionVariableName);\r
1653 }\r
1654 if (Private->LsvVariableName != NULL) {\r
1655 FreePool (Private->LsvVariableName);\r
1656 }\r
1657 if (Private->LastAttemptStatusVariableName != NULL) {\r
1658 FreePool (Private->LastAttemptStatusVariableName);\r
1659 }\r
1660 if (Private->LastAttemptVersionVariableName != NULL) {\r
1661 FreePool (Private->LastAttemptVersionVariableName);\r
1662 }\r
1663 if (Private->FmpStateVariableName != NULL) {\r
1664 FreePool (Private->FmpStateVariableName);\r
1665 }\r
4f0544b1
EJ
1666 FreePool (Private);\r
1667 }\r
1668 }\r
1669\r
b0bacc00
KM
1670 return Status;\r
1671}\r
1672\r
4f0544b1
EJ
1673/**\r
1674 Function to uninstall FMP instance.\r
1675\r
1676 @param[in] Handle The device handle to install a FMP instance on.\r
1677\r
1678 @retval EFI_SUCCESS FMP Installed\r
1679 @retval EFI_INVALID_PARAMETER Handle was invalid\r
1680 @retval other Error installing FMP\r
1681\r
1682**/\r
1683EFI_STATUS\r
1684EFIAPI\r
1685UninstallFmpInstance (\r
1686 IN EFI_HANDLE Handle\r
1687 )\r
1688{\r
1689 EFI_STATUS Status;\r
1690 EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;\r
1691 FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;\r
1692\r
1693 Status = gBS->OpenProtocol (\r
1694 Handle,\r
1695 &gEfiFirmwareManagementProtocolGuid,\r
1696 (VOID **)&Fmp,\r
1697 NULL,\r
1698 NULL,\r
1699 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1700 );\r
1701 if (EFI_ERROR (Status)) {\r
e0961677 1702 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Protocol open error. Status = %r.\n", mImageIdName, Status));\r
4f0544b1
EJ
1703 return Status;\r
1704 }\r
1705\r
1706 Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (Fmp);\r
1707 FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);\r
1708\r
1709 if (Private->FmpDeviceLockEvent != NULL) {\r
1710 gBS->CloseEvent (Private->FmpDeviceLockEvent);\r
1711 }\r
1712\r
1713 Status = gBS->UninstallMultipleProtocolInterfaces (\r
1714 Private->Handle,\r
1715 &gEfiFirmwareManagementProtocolGuid, &Private->Fmp,\r
1716 &gEdkiiFirmwareManagementProgressProtocolGuid, &mFmpProgress,\r
1717 NULL\r
1718 );\r
1719 if (EFI_ERROR (Status)) {\r
e0961677 1720 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Protocol uninstall error. Status = %r.\n", mImageIdName, Status));\r
4f0544b1
EJ
1721 return Status;\r
1722 }\r
1723\r
67c1e5ee
EJ
1724 if (Private->Descriptor.VersionName != NULL) {\r
1725 FreePool (Private->Descriptor.VersionName);\r
1726 }\r
1727 if (Private->FmpDeviceContext != NULL) {\r
1728 FmpDeviceSetContext (NULL, &Private->FmpDeviceContext);\r
1729 }\r
1730 if (Private->VersionVariableName != NULL) {\r
1731 FreePool (Private->VersionVariableName);\r
1732 }\r
1733 if (Private->LsvVariableName != NULL) {\r
1734 FreePool (Private->LsvVariableName);\r
1735 }\r
1736 if (Private->LastAttemptStatusVariableName != NULL) {\r
1737 FreePool (Private->LastAttemptStatusVariableName);\r
1738 }\r
1739 if (Private->LastAttemptVersionVariableName != NULL) {\r
1740 FreePool (Private->LastAttemptVersionVariableName);\r
1741 }\r
1742 if (Private->FmpStateVariableName != NULL) {\r
1743 FreePool (Private->FmpStateVariableName);\r
1744 }\r
4f0544b1
EJ
1745 FreePool (Private);\r
1746\r
1747 return EFI_SUCCESS;\r
1748}\r
1749\r
1750/**\r
1751 Unloads the application and its installed protocol.\r
1752\r
1753 @param ImageHandle Handle that identifies the image to be unloaded.\r
1754 @param SystemTable The system table.\r
1755\r
1756 @retval EFI_SUCCESS The image has been unloaded.\r
1757\r
1758**/\r
1759EFI_STATUS\r
1760EFIAPI\r
1761FmpDxeLibDestructor (\r
1762 IN EFI_HANDLE ImageHandle,\r
1763 IN EFI_SYSTEM_TABLE *SystemTable\r
1764 )\r
1765{\r
1766 if (mFmpSingleInstance) {\r
1767 return UninstallFmpInstance (ImageHandle);\r
1768 }\r
1769 return EFI_SUCCESS;\r
1770}\r
1771\r
b0bacc00 1772/**\r
e8619f82 1773 Main entry for this driver/library.\r
b0bacc00
KM
1774\r
1775 @param[in] ImageHandle Image handle this driver.\r
1776 @param[in] SystemTable Pointer to SystemTable.\r
1777\r
1778**/\r
1779EFI_STATUS\r
1780EFIAPI\r
1781FmpDxeEntryPoint (\r
1782 IN EFI_HANDLE ImageHandle,\r
1783 IN EFI_SYSTEM_TABLE *SystemTable\r
1784 )\r
1785{\r
1786 EFI_STATUS Status;\r
b0bacc00
KM
1787\r
1788 //\r
1789 // Verify that a new FILE_GUID value has been provided in the <Defines>\r
1790 // section of this module. The FILE_GUID is the ESRT GUID that must be\r
1791 // unique for each updatable firmware image.\r
1792 //\r
1793 if (CompareGuid (&mDefaultModuleFileGuid, &gEfiCallerIdGuid)) {\r
1794 DEBUG ((DEBUG_ERROR, "FmpDxe: Use of default FILE_GUID detected. FILE_GUID must be set to a unique value.\n"));\r
1795 ASSERT (FALSE);\r
1796 return EFI_UNSUPPORTED;\r
1797 }\r
1798\r
1799 //\r
1800 // Get the ImageIdName value for the EFI_FIRMWARE_IMAGE_DESCRIPTOR from a PCD.\r
1801 //\r
1802 mImageIdName = (CHAR16 *) PcdGetPtr (PcdFmpDeviceImageIdName);\r
1803 if (PcdGetSize (PcdFmpDeviceImageIdName) <= 2 || mImageIdName[0] == 0) {\r
1804 //\r
1805 // PcdFmpDeviceImageIdName must be set to a non-empty Unicode string\r
1806 //\r
c9fa9762 1807 DEBUG ((DEBUG_ERROR, "FmpDxe(%g): PcdFmpDeviceImageIdName is an empty string.\n", &gEfiCallerIdGuid));\r
b0bacc00 1808 ASSERT (FALSE);\r
e0961677 1809 return EFI_UNSUPPORTED;\r
b0bacc00
KM
1810 }\r
1811\r
1812 //\r
1813 // Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.\r
1814 //\r
1815 DetectTestKey ();\r
1816\r
4f0544b1
EJ
1817 //\r
1818 // Fill in FMP Progress Protocol fields for Version 1\r
1819 //\r
1820 mFmpProgress.Version = 1;\r
1821 mFmpProgress.ProgressBarForegroundColor.Raw = PcdGet32 (PcdFmpDeviceProgressColor);\r
1822 mFmpProgress.WatchdogSeconds = PcdGet8 (PcdFmpDeviceProgressWatchdogTimeInSeconds);\r
9e6c4f15 1823\r
4f0544b1
EJ
1824 // The lock event GUID is retrieved from PcdFmpDeviceLockEventGuid.\r
1825 // If PcdFmpDeviceLockEventGuid is not the size of an EFI_GUID, then\r
1826 // gEfiEndOfDxeEventGroupGuid is used.\r
1827 //\r
1828 mLockGuid = &gEfiEndOfDxeEventGroupGuid;\r
1829 if (PcdGetSize (PcdFmpDeviceLockEventGuid) == sizeof (EFI_GUID)) {\r
1830 mLockGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceLockEventGuid);\r
9e6c4f15 1831 }\r
e0961677 1832 DEBUG ((DEBUG_INFO, "FmpDxe(%s): Lock GUID: %g\n", mImageIdName, mLockGuid));\r
9e6c4f15 1833\r
b0bacc00
KM
1834 //\r
1835 // Register with library the install function so if the library uses\r
1836 // UEFI driver model/driver binding protocol it can install FMP on its device handle\r
1837 // If library is simple lib that does not use driver binding then it should return\r
1838 // unsupported and this will install the FMP instance on the ImageHandle\r
1839 //\r
1840 Status = RegisterFmpInstaller (InstallFmpInstance);\r
1841 if (Status == EFI_UNSUPPORTED) {\r
4f0544b1 1842 mFmpSingleInstance = TRUE;\r
e0961677 1843 DEBUG ((DEBUG_INFO, "FmpDxe(%s): FmpDeviceLib registration returned EFI_UNSUPPORTED. Installing single FMP instance.\n", mImageIdName));\r
4f0544b1
EJ
1844 Status = RegisterFmpUninstaller (UninstallFmpInstance);\r
1845 if (Status == EFI_UNSUPPORTED) {\r
1846 Status = InstallFmpInstance (ImageHandle);\r
1847 } else {\r
e0961677 1848 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib RegisterFmpInstaller and RegisterFmpUninstaller do not match.\n", mImageIdName));\r
4f0544b1
EJ
1849 Status = EFI_UNSUPPORTED;\r
1850 }\r
b0bacc00 1851 } else if (EFI_ERROR (Status)) {\r
e0961677 1852 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib registration returned %r. No FMP installed.\n", mImageIdName, Status));\r
b0bacc00
KM
1853 } else {\r
1854 DEBUG ((\r
1855 DEBUG_INFO,\r
e0961677
EJ
1856 "FmpDxe(%s): FmpDeviceLib registration returned EFI_SUCCESS. Expect FMP to be installed during the BDS/Device connection phase.\n",\r
1857 mImageIdName\r
b0bacc00 1858 ));\r
4f0544b1
EJ
1859 Status = RegisterFmpUninstaller (UninstallFmpInstance);\r
1860 if (EFI_ERROR (Status)) {\r
e0961677 1861 DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib RegisterFmpInstaller and RegisterFmpUninstaller do not match.\n", mImageIdName));\r
4f0544b1 1862 }\r
b0bacc00
KM
1863 }\r
1864\r
b0bacc00
KM
1865 return Status;\r
1866}\r