]> git.proxmox.com Git - mirror_edk2.git/blob - FmpDevicePkg/FmpDxe/FmpDxe.c
eb452737543ec1585f985b976700e72e3cd41f87
[mirror_edk2.git] / FmpDevicePkg / FmpDxe / FmpDxe.c
1 /** @file
2 Produces a Firmware Management Protocol that supports updates to a firmware
3 image stored in a firmware device with platform and firmware device specific
4 information provided through PCDs and libraries.
5
6 Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>
7 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11 1. Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 **/
29
30 #include <PiDxe.h>
31 #include <Library/DebugLib.h>
32 #include <Library/BaseLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/MemoryAllocationLib.h>
36 #include <Library/UefiLib.h>
37 #include <Library/FmpAuthenticationLib.h>
38 #include <Library/FmpDeviceLib.h>
39 #include <Library/FmpPayloadHeaderLib.h>
40 #include <Library/CapsuleUpdatePolicyLib.h>
41 #include <Protocol/FirmwareManagement.h>
42 #include <Protocol/FirmwareManagementProgress.h>
43 #include <Guid/SystemResourceTable.h>
44 #include <Guid/EventGroup.h>
45 #include "VariableSupport.h"
46
47 #define VERSION_STRING_NOT_SUPPORTED L"VERSION STRING NOT SUPPORTED"
48 #define VERSION_STRING_NOT_AVAILABLE L"VERSION STRING NOT AVAILABLE"
49
50 /**
51 Check to see if any of the keys in PcdFmpDevicePkcs7CertBufferXdr matches
52 the test key. PcdFmpDeviceTestKeySha256Digest contains the SHA256 hash of
53 the test key. For each key in PcdFmpDevicePkcs7CertBufferXdr, compute the
54 SHA256 hash and compare it to PcdFmpDeviceTestKeySha256Digest. If the
55 SHA256 hash matches or there is then error computing the SHA256 hash, then
56 set PcdTestKeyUsed to TRUE. Skip this check if PcdTestKeyUsed is already
57 TRUE or PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE
58 bytes.
59 **/
60 VOID
61 DetectTestKey (
62 VOID
63 );
64
65 ///
66 /// FILE_GUID from FmpDxe.inf. When FmpDxe.inf is used in a platform, the
67 /// FILE_GUID must always be overridden in the <Defines> section to provide
68 /// the ESRT GUID value associated with the updatable firmware image. A
69 /// check is made in this module's driver entry point to verify that a
70 /// new FILE_GUID value has been defined.
71 ///
72 const EFI_GUID mDefaultModuleFileGuid = {
73 0x78ef0a56, 0x1cf0, 0x4535, { 0xb5, 0xda, 0xf6, 0xfd, 0x2f, 0x40, 0x5a, 0x11 }
74 };
75
76 EFI_FIRMWARE_IMAGE_DESCRIPTOR mDesc;
77 BOOLEAN mDescriptorPopulated = FALSE;
78 BOOLEAN mRuntimeVersionSupported = TRUE;
79 BOOLEAN mFmpInstalled = FALSE;
80
81 ///
82 /// Function pointer to progress function
83 ///
84 EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS mProgressFunc = NULL;
85 BOOLEAN mProgressSupported = FALSE;
86
87 CHAR16 *mImageIdName = NULL;
88 UINT64 mImageId = 0x1;
89 CHAR16 *mVersionName = NULL;
90
91 EFI_EVENT mFmpDeviceLockEvent;
92 BOOLEAN mFmpDeviceLocked = FALSE;
93
94 /**
95 Callback function to report the process of the firmware updating.
96
97 Wrap the caller's version in this so that progress from the device lib is
98 within the expected range. Convert device lib 0% - 100% to 6% - 98%.
99
100 FmpDxe 1% - 5% for validation
101 FmpDeviceLib 6% - 98% for flashing/update
102 FmpDxe 99% - 100% finish
103
104 @param[in] Completion A value between 1 and 100 indicating the current
105 completion progress of the firmware update. Completion
106 progress is reported as from 1 to 100 percent. A value
107 of 0 is used by the driver to indicate that progress
108 reporting is not supported.
109
110 @retval EFI_SUCCESS The progress was updated.
111 @retval EFI_UNSUPPORTED Updating progress is not supported.
112
113 **/
114 EFI_STATUS
115 EFIAPI
116 FmpDxeProgress (
117 IN UINTN Completion
118 )
119 {
120 EFI_STATUS Status;
121
122 Status = EFI_UNSUPPORTED;
123
124 if (!mProgressSupported) {
125 return Status;
126 }
127
128 if (mProgressFunc == NULL) {
129 return Status;
130 }
131
132 //
133 // Reserve 6% - 98% for the FmpDeviceLib. Call the real progress function.
134 //
135 Status = mProgressFunc (((Completion * 92) / 100) + 6);
136
137 if (Status == EFI_UNSUPPORTED) {
138 mProgressSupported = FALSE;
139 mProgressFunc = NULL;
140 }
141
142 return Status;
143 }
144
145 /**
146 Returns a pointer to the ImageTypeId GUID value. An attempt is made to get
147 the GUID value from the FmpDeviceLib. If the FmpDeviceLib does not provide
148 a GUID value, then gEfiCallerIdGuid is returned.
149
150 @return The ImageTypeId GUID
151
152 **/
153 EFI_GUID *
154 GetImageTypeIdGuid (
155 VOID
156 )
157 {
158 EFI_STATUS Status;
159 EFI_GUID *FmpDeviceLibGuid;
160
161 FmpDeviceLibGuid = NULL;
162 Status = FmpDeviceGetImageTypeIdGuidPtr (&FmpDeviceLibGuid);
163 if (EFI_ERROR (Status)) {
164 if (Status != EFI_UNSUPPORTED) {
165 DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid error %r\n", Status));
166 }
167 return &gEfiCallerIdGuid;
168 }
169 if (FmpDeviceLibGuid == NULL) {
170 DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid GUID\n"));
171 return &gEfiCallerIdGuid;
172 }
173 return FmpDeviceLibGuid;
174 }
175
176 /**
177 Returns a pointer to the Null-terminated Unicode ImageIdName string.
178
179 @return Null-terminated Unicode ImageIdName string.
180
181 **/
182 CHAR16 *
183 GetImageTypeNameString (
184 VOID
185 )
186 {
187 return mImageIdName;
188 }
189
190 /**
191 Lowest supported version is a combo of three parts.
192 1. Check if the device lib has a lowest supported version
193 2. Check if we have a variable for lowest supported version (this will be updated with each capsule applied)
194 3. Check Fixed at build PCD
195
196 Take the largest value
197
198 **/
199 UINT32
200 GetLowestSupportedVersion (
201 VOID
202 )
203 {
204 EFI_STATUS Status;
205 UINT32 DeviceLibLowestSupportedVersion;
206 UINT32 VariableLowestSupportedVersion;
207 UINT32 ReturnLsv;
208
209 //
210 // Get the LowestSupportedVersion.
211 //
212
213 if (!IsLowestSupportedVersionCheckRequired ()) {
214 //
215 // Any Version can pass the 0 LowestSupportedVersion check.
216 //
217 return 0;
218 }
219
220 ReturnLsv = PcdGet32 (PcdFmpDeviceBuildTimeLowestSupportedVersion);
221
222 //
223 // Check the FmpDeviceLib
224 //
225 Status = FmpDeviceGetLowestSupportedVersion (&DeviceLibLowestSupportedVersion);
226 if (EFI_ERROR (Status)) {
227 DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
228 }
229
230 if (DeviceLibLowestSupportedVersion > ReturnLsv) {
231 ReturnLsv = DeviceLibLowestSupportedVersion;
232 }
233
234 //
235 // Check the lowest supported version UEFI variable for this device
236 //
237 VariableLowestSupportedVersion = GetLowestSupportedVersionFromVariable();
238 if (VariableLowestSupportedVersion > ReturnLsv) {
239 ReturnLsv = VariableLowestSupportedVersion;
240 }
241
242 //
243 // Return the largest value
244 //
245 return ReturnLsv;
246 }
247
248 /**
249 Populates the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure in the module global
250 variable mDesc.
251
252 **/
253 VOID
254 PopulateDescriptor (
255 VOID
256 )
257 {
258 EFI_STATUS Status;
259
260 mDesc.ImageIndex = 1;
261 CopyGuid (&mDesc.ImageTypeId, GetImageTypeIdGuid());
262 mDesc.ImageId = mImageId;
263 mDesc.ImageIdName = GetImageTypeNameString();
264
265 //
266 // Get the version. Some devices don't support getting the firmware version
267 // at runtime. If FmpDeviceLib does not support returning a version, then
268 // it is stored in a UEFI variable.
269 //
270 Status = FmpDeviceGetVersion (&mDesc.Version);
271 if (Status == EFI_UNSUPPORTED) {
272 mRuntimeVersionSupported = FALSE;
273 mDesc.Version = GetVersionFromVariable();
274 } else if (EFI_ERROR (Status)) {
275 //
276 // Unexpected error. Use default version.
277 //
278 DEBUG ((DEBUG_ERROR, "FmpDxe: GetVersion() from FmpDeviceLib (%s) returned %r\n", GetImageTypeNameString(), Status));
279 mDesc.Version = DEFAULT_VERSION;
280 }
281
282 //
283 // Free the current version name. Shouldn't really happen but this populate
284 // function could be called multiple times (to refresh).
285 //
286 if (mVersionName != NULL) {
287 FreePool (mVersionName);
288 mVersionName = NULL;
289 }
290
291 //
292 // Attempt to get the version string from the FmpDeviceLib
293 //
294 Status = FmpDeviceGetVersionString (&mVersionName);
295 if (Status == EFI_UNSUPPORTED) {
296 DEBUG ((DEBUG_INFO, "FmpDxe: GetVersionString() unsupported in FmpDeviceLib.\n"));
297 mVersionName = AllocateCopyPool (
298 sizeof (VERSION_STRING_NOT_SUPPORTED),
299 VERSION_STRING_NOT_SUPPORTED
300 );
301 } else if (EFI_ERROR (Status)) {
302 DEBUG ((DEBUG_INFO, "FmpDxe: GetVersionString() not available in FmpDeviceLib.\n"));
303 mVersionName = AllocateCopyPool (
304 sizeof (VERSION_STRING_NOT_AVAILABLE),
305 VERSION_STRING_NOT_AVAILABLE
306 );
307 }
308
309 mDesc.VersionName = mVersionName;
310
311 mDesc.LowestSupportedImageVersion = GetLowestSupportedVersion();
312
313 //
314 // Get attributes from the FmpDeviceLib
315 //
316 FmpDeviceGetAttributes (&mDesc.AttributesSupported, &mDesc.AttributesSetting);
317
318 //
319 // Force set the updatable bits in the attributes;
320 //
321 mDesc.AttributesSupported |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
322 mDesc.AttributesSetting |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
323
324 //
325 // Force set the authentication bits in the attributes;
326 //
327 mDesc.AttributesSupported |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);
328 mDesc.AttributesSetting |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);
329
330 mDesc.Compatibilities = 0;
331
332 //
333 // Get the size of the firmware image from the FmpDeviceLib
334 //
335 Status = FmpDeviceGetSize (&mDesc.Size);
336 if (EFI_ERROR (Status)) {
337 mDesc.Size = 0;
338 }
339
340 mDesc.LastAttemptVersion = GetLastAttemptVersionFromVariable ();
341 mDesc.LastAttemptStatus = GetLastAttemptStatusFromVariable ();
342
343 mDescriptorPopulated = TRUE;
344 }
345
346 /**
347 Returns information about the current firmware image(s) of the device.
348
349 This function allows a copy of the current firmware image to be created and saved.
350 The saved copy could later been used, for example, in firmware image recovery or rollback.
351
352 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
353 @param[in, out] ImageInfoSize A pointer to the size, in bytes, of the ImageInfo buffer.
354 On input, this is the size of the buffer allocated by the caller.
355 On output, it is the size of the buffer returned by the firmware
356 if the buffer was large enough, or the size of the buffer needed
357 to contain the image(s) information if the buffer was too small.
358 @param[in, out] ImageInfo A pointer to the buffer in which firmware places the current image(s)
359 information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
360 @param[out] DescriptorVersion A pointer to the location in which firmware returns the version number
361 associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
362 @param[out] DescriptorCount A pointer to the location in which firmware returns the number of
363 descriptors or firmware images within this device.
364 @param[out] DescriptorSize A pointer to the location in which firmware returns the size, in bytes,
365 of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.
366 @param[out] PackageVersion A version number that represents all the firmware images in the device.
367 The format is vendor specific and new version must have a greater value
368 than the old version. If PackageVersion is not supported, the value is
369 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison
370 is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates
371 that package version update is in progress.
372 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing the
373 package version name. The buffer is allocated by this function with
374 AllocatePool(), and it is the caller's responsibility to free it with a call
375 to FreePool().
376
377 @retval EFI_SUCCESS The device was successfully updated with the new image.
378 @retval EFI_BUFFER_TOO_SMALL The ImageInfo buffer was too small. The current buffer size
379 needed to hold the image(s) information is returned in ImageInfoSize.
380 @retval EFI_INVALID_PARAMETER ImageInfoSize is NULL.
381 @retval EFI_DEVICE_ERROR Valid information could not be returned. Possible corrupted image.
382
383 **/
384 EFI_STATUS
385 EFIAPI
386 GetTheImageInfo (
387 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
388 IN OUT UINTN *ImageInfoSize,
389 IN OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageInfo,
390 OUT UINT32 *DescriptorVersion,
391 OUT UINT8 *DescriptorCount,
392 OUT UINTN *DescriptorSize,
393 OUT UINT32 *PackageVersion,
394 OUT CHAR16 **PackageVersionName
395 )
396 {
397 EFI_STATUS Status;
398
399 Status = EFI_SUCCESS;
400
401 //
402 // Check for valid pointer
403 //
404 if (ImageInfoSize == NULL) {
405 DEBUG ((DEBUG_ERROR, "FmpDxe: GetImageInfo() - ImageInfoSize is NULL.\n"));
406 Status = EFI_INVALID_PARAMETER;
407 goto cleanup;
408 }
409
410 //
411 // Check the buffer size
412 // NOTE: Check this first so caller can get the necessary memory size it must allocate.
413 //
414 if (*ImageInfoSize < (sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR))) {
415 *ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
416 DEBUG ((DEBUG_VERBOSE, "FmpDxe: GetImageInfo() - ImageInfoSize is to small.\n"));
417 Status = EFI_BUFFER_TOO_SMALL;
418 goto cleanup;
419 }
420
421 //
422 // Confirm that buffer isn't null
423 //
424 if ( (ImageInfo == NULL) || (DescriptorVersion == NULL) || (DescriptorCount == NULL) || (DescriptorSize == NULL)
425 || (PackageVersion == NULL)) {
426 DEBUG ((DEBUG_ERROR, "FmpDxe: GetImageInfo() - Pointer Parameter is NULL.\n"));
427 Status = EFI_INVALID_PARAMETER;
428 goto cleanup;
429 }
430
431 //
432 // Set the size to whatever we need
433 //
434 *ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
435
436
437 if (!mDescriptorPopulated) {
438 PopulateDescriptor();
439 }
440
441 //
442 // Copy the image descriptor
443 //
444 CopyMem (ImageInfo, &mDesc, sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR));
445
446 *DescriptorVersion = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;
447 *DescriptorCount = 1;
448 *DescriptorSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
449 //
450 // means unsupported
451 //
452 *PackageVersion = 0xFFFFFFFF;
453
454 //
455 // Do not update PackageVersionName since it is not supported in this instance.
456 //
457
458 cleanup:
459
460 return Status;
461 }
462
463 /**
464 Retrieves a copy of the current firmware image of the device.
465
466 This function allows a copy of the current firmware image to be created and saved.
467 The saved copy could later been used, for example, in firmware image recovery or rollback.
468
469 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
470 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
471 The number is between 1 and DescriptorCount.
472 @param[out] Image Points to the buffer where the current image is copied to.
473 @param[out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.
474 On return, points to the length of the image, in bytes.
475
476 @retval EFI_SUCCESS The device was successfully updated with the new image.
477 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the
478 image. The current buffer size needed to hold the image is returned
479 in ImageSize.
480 @retval EFI_INVALID_PARAMETER The Image was NULL.
481 @retval EFI_NOT_FOUND The current image is not copied to the buffer.
482 @retval EFI_UNSUPPORTED The operation is not supported.
483 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
484
485 **/
486 EFI_STATUS
487 EFIAPI
488 GetTheImage (
489 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
490 IN UINT8 ImageIndex,
491 IN OUT VOID *Image,
492 IN OUT UINTN *ImageSize
493 )
494 {
495 EFI_STATUS Status;
496 UINTN Size;
497
498 Status = EFI_SUCCESS;
499
500 if ((ImageSize == NULL)) {
501 DEBUG ((DEBUG_ERROR, "FmpDxe: GetImage() - ImageSize Pointer Parameter is NULL.\n"));
502 Status = EFI_INVALID_PARAMETER;
503 goto cleanup;
504 }
505
506 //
507 // Check the buffer size
508 //
509 Status = FmpDeviceGetSize (&Size);
510 if (EFI_ERROR (Status)) {
511 Size = 0;
512 }
513 if (*ImageSize < Size) {
514 *ImageSize = Size;
515 DEBUG ((DEBUG_VERBOSE, "FmpDxe: GetImage() - ImageSize is to small.\n"));
516 Status = EFI_BUFFER_TOO_SMALL;
517 goto cleanup;
518 }
519
520 if (Image == NULL) {
521 DEBUG ((DEBUG_ERROR, "FmpDxe: GetImage() - Image Pointer Parameter is NULL.\n"));
522 Status = EFI_INVALID_PARAMETER;
523 goto cleanup;
524 }
525
526 //
527 // Check to make sure index is 1 (only 1 image for this device)
528 //
529 if (ImageIndex != 1) {
530 DEBUG ((DEBUG_ERROR, "FmpDxe: GetImage() - Image Index Invalid.\n"));
531 Status = EFI_INVALID_PARAMETER;
532 goto cleanup;
533 }
534
535
536 Status = FmpDeviceGetImage (Image, ImageSize);
537 cleanup:
538
539 return Status;
540 }
541
542 /**
543 Helper function to safely retrieve the FMP header from
544 within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
545
546 @param[in] Image Pointer to the image.
547 @param[in] ImageSize Size of the image.
548 @param[out] PayloadSize
549
550 @retval !NULL Valid pointer to the header.
551 @retval NULL Structure is bad and pointer cannot be found.
552
553 **/
554 VOID *
555 GetFmpHeader (
556 IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
557 IN CONST UINTN ImageSize,
558 OUT UINTN *PayloadSize
559 )
560 {
561 //
562 // Check to make sure that operation can be safely performed.
563 //
564 if (((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) < (UINTN)Image || \
565 ((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) >= (UINTN)Image + ImageSize) {
566 //
567 // Pointer overflow. Invalid image.
568 //
569 return NULL;
570 }
571
572 *PayloadSize = ImageSize - (sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength);
573 return (VOID *)((UINT8 *)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength);
574 }
575
576 /**
577 Helper function to safely calculate the size of all headers
578 within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
579
580 @param[in] Image Pointer to the image.
581 @param[in] AdditionalHeaderSize Size of any headers that cannot be calculated by this function.
582
583 @retval UINT32>0 Valid size of all the headers.
584 @retval 0 Structure is bad and size cannot be found.
585
586 **/
587 UINT32
588 GetAllHeaderSize (
589 IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
590 IN UINT32 AdditionalHeaderSize
591 )
592 {
593 UINT32 CalculatedSize;
594
595 CalculatedSize = sizeof (Image->MonotonicCount) +
596 AdditionalHeaderSize +
597 Image->AuthInfo.Hdr.dwLength;
598
599 //
600 // Check to make sure that operation can be safely performed.
601 //
602 if (CalculatedSize < sizeof (Image->MonotonicCount) ||
603 CalculatedSize < AdditionalHeaderSize ||
604 CalculatedSize < Image->AuthInfo.Hdr.dwLength ) {
605 //
606 // Integer overflow. Invalid image.
607 //
608 return 0;
609 }
610
611 return CalculatedSize;
612 }
613
614 /**
615 Checks if the firmware image is valid for the device.
616
617 This function allows firmware update application to validate the firmware image without
618 invoking the SetImage() first.
619
620 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
621 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
622 The number is between 1 and DescriptorCount.
623 @param[in] Image Points to the new image.
624 @param[in] ImageSize Size of the new image in bytes.
625 @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
626 if available, additional information if the image is invalid.
627
628 @retval EFI_SUCCESS The image was successfully checked.
629 @retval EFI_ABORTED The operation is aborted.
630 @retval EFI_INVALID_PARAMETER The Image was NULL.
631 @retval EFI_UNSUPPORTED The operation is not supported.
632 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
633
634 **/
635 EFI_STATUS
636 EFIAPI
637 CheckTheImage (
638 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
639 IN UINT8 ImageIndex,
640 IN CONST VOID *Image,
641 IN UINTN ImageSize,
642 OUT UINT32 *ImageUpdateable
643 )
644 {
645 EFI_STATUS Status;
646 UINTN RawSize;
647 VOID *FmpPayloadHeader;
648 UINTN FmpPayloadSize;
649 UINT32 Version;
650 UINT32 FmpHeaderSize;
651 UINTN AllHeaderSize;
652 UINT32 Index;
653 VOID *PublicKeyData;
654 UINTN PublicKeyDataLength;
655 UINT8 *PublicKeyDataXdr;
656 UINT8 *PublicKeyDataXdrEnd;
657
658 Status = EFI_SUCCESS;
659 RawSize = 0;
660 FmpPayloadHeader = NULL;
661 FmpPayloadSize = 0;
662 Version = 0;
663 FmpHeaderSize = 0;
664 AllHeaderSize = 0;
665
666 //
667 // make sure the descriptor has already been loaded
668 //
669 if (!mDescriptorPopulated) {
670 PopulateDescriptor();
671 }
672
673 if (ImageUpdateable == NULL) {
674 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - ImageUpdateable Pointer Parameter is NULL.\n"));
675 Status = EFI_INVALID_PARAMETER;
676 goto cleanup;
677 }
678
679 //
680 //Set to valid and then if any tests fail it will update this flag.
681 //
682 *ImageUpdateable = IMAGE_UPDATABLE_VALID;
683
684 if (Image == NULL) {
685 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - Image Pointer Parameter is NULL.\n"));
686 //
687 // not sure if this is needed
688 //
689 *ImageUpdateable = IMAGE_UPDATABLE_INVALID;
690 return EFI_INVALID_PARAMETER;
691 }
692
693 PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
694 PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
695
696 if (PublicKeyDataXdr == NULL || (PublicKeyDataXdr == PublicKeyDataXdrEnd)) {
697 DEBUG ((DEBUG_ERROR, "FmpDxe: Invalid certificate, skipping it.\n"));
698 Status = EFI_ABORTED;
699 } else {
700 //
701 // Try each key from PcdFmpDevicePkcs7CertBufferXdr
702 //
703 for (Index = 1; PublicKeyDataXdr < PublicKeyDataXdrEnd; Index++) {
704 Index++;
705 DEBUG (
706 (DEBUG_INFO,
707 "FmpDxe: Certificate #%d [%p..%p].\n",
708 Index,
709 PublicKeyDataXdr,
710 PublicKeyDataXdrEnd
711 )
712 );
713
714 if ((PublicKeyDataXdr + sizeof (UINT32)) > PublicKeyDataXdrEnd) {
715 //
716 // Key data extends beyond end of PCD
717 //
718 DEBUG ((DEBUG_ERROR, "FmpDxe: Certificate size extends beyond end of PCD, skipping it.\n"));
719 Status = EFI_ABORTED;
720 break;
721 }
722 //
723 // Read key length stored in big-endian format
724 //
725 PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
726 //
727 // Point to the start of the key data
728 //
729 PublicKeyDataXdr += sizeof (UINT32);
730 if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
731 //
732 // Key data extends beyond end of PCD
733 //
734 DEBUG ((DEBUG_ERROR, "FmpDxe: Certificate extends beyond end of PCD, skipping it.\n"));
735 Status = EFI_ABORTED;
736 break;
737 }
738 PublicKeyData = PublicKeyDataXdr;
739 Status = AuthenticateFmpImage (
740 (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image,
741 ImageSize,
742 PublicKeyData,
743 PublicKeyDataLength
744 );
745 if (!EFI_ERROR (Status)) {
746 break;
747 }
748 PublicKeyDataXdr += PublicKeyDataLength;
749 PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
750 }
751 }
752
753 if (EFI_ERROR (Status)) {
754 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - Authentication Failed %r.\n", Status));
755 goto cleanup;
756 }
757
758 //
759 // Check to make sure index is 1
760 //
761 if (ImageIndex != 1) {
762 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - Image Index Invalid.\n"));
763 *ImageUpdateable = IMAGE_UPDATABLE_INVALID_TYPE;
764 Status = EFI_SUCCESS;
765 goto cleanup;
766 }
767
768
769 //
770 // Check the FmpPayloadHeader
771 //
772 FmpPayloadHeader = GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &FmpPayloadSize );
773 if (FmpPayloadHeader == NULL) {
774 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpHeader failed.\n"));
775 Status = EFI_ABORTED;
776 goto cleanup;
777 }
778 Status = GetFmpPayloadHeaderVersion (FmpPayloadHeader, FmpPayloadSize, &Version);
779 if (EFI_ERROR (Status)) {
780 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderVersion failed %r.\n", Status));
781 *ImageUpdateable = IMAGE_UPDATABLE_INVALID;
782 Status = EFI_SUCCESS;
783 goto cleanup;
784 }
785
786 //
787 // Check the lowest supported version
788 //
789 if (Version < mDesc.LowestSupportedImageVersion) {
790 DEBUG (
791 (DEBUG_ERROR,
792 "FmpDxe: CheckTheImage() - Version Lower than lowest supported version. 0x%08X < 0x%08X\n",
793 Version, mDesc.LowestSupportedImageVersion)
794 );
795 *ImageUpdateable = IMAGE_UPDATABLE_INVALID_OLD;
796 Status = EFI_SUCCESS;
797 goto cleanup;
798 }
799
800 //
801 // Get the FmpHeaderSize so we can determine the real payload size
802 //
803 Status = GetFmpPayloadHeaderSize (FmpPayloadHeader, FmpPayloadSize, &FmpHeaderSize);
804 if (EFI_ERROR (Status)) {
805 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status));
806 *ImageUpdateable = IMAGE_UPDATABLE_INVALID;
807 Status = EFI_SUCCESS;
808 goto cleanup;
809 }
810
811 //
812 // Call FmpDevice Lib Check Image on the
813 // Raw payload. So all headers need stripped off
814 //
815 AllHeaderSize = GetAllHeaderSize ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize );
816 if (AllHeaderSize == 0) {
817 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetAllHeaderSize failed.\n"));
818 Status = EFI_ABORTED;
819 goto cleanup;
820 }
821 RawSize = ImageSize - AllHeaderSize;
822
823 //
824 // FmpDeviceLib CheckImage function to do any specific checks
825 //
826 Status = FmpDeviceCheckImage ((((UINT8 *)Image) + AllHeaderSize), RawSize, ImageUpdateable);
827 if (EFI_ERROR (Status)) {
828 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - FmpDeviceLib CheckImage failed. Status = %r\n", Status));
829 }
830
831 cleanup:
832 return Status;
833 }
834
835 /**
836 Updates the firmware image of the device.
837
838 This function updates the hardware with the new firmware image.
839 This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
840 If the firmware image is updatable, the function should perform the following minimal validations
841 before proceeding to do the firmware image update.
842 - Validate the image authentication if image has attribute
843 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
844 EFI_SECURITY_VIOLATION if the validation fails.
845 - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
846 the image is unsupported. The function can optionally provide more detailed information on
847 why the image is not a supported image.
848 - Validate the data from VendorCode if not null. Image validation must be performed before
849 VendorCode data validation. VendorCode data is ignored or considered invalid if image
850 validation failed. The function returns EFI_ABORTED if the data is invalid.
851
852 VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
853 the caller did not specify the policy or use the default policy. As an example, vendor can implement
854 a policy to allow an option to force a firmware image update when the abort reason is due to the new
855 firmware image version is older than the current firmware image version or bad image checksum.
856 Sensitive operations such as those wiping the entire firmware image and render the device to be
857 non-functional should be encoded in the image itself rather than passed with the VendorCode.
858 AbortReason enables vendor to have the option to provide a more detailed description of the abort
859 reason to the caller.
860
861 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
862 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
863 The number is between 1 and DescriptorCount.
864 @param[in] Image Points to the new image.
865 @param[in] ImageSize Size of the new image in bytes.
866 @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
867 Null indicates the caller did not specify the policy or use the default policy.
868 @param[in] Progress A function used by the driver to report the progress of the firmware update.
869 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
870 details for the aborted operation. The buffer is allocated by this function
871 with AllocatePool(), and it is the caller's responsibility to free it with a
872 call to FreePool().
873
874 @retval EFI_SUCCESS The device was successfully updated with the new image.
875 @retval EFI_ABORTED The operation is aborted.
876 @retval EFI_INVALID_PARAMETER The Image was NULL.
877 @retval EFI_UNSUPPORTED The operation is not supported.
878 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
879
880 **/
881 EFI_STATUS
882 EFIAPI
883 SetTheImage (
884 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
885 IN UINT8 ImageIndex,
886 IN CONST VOID *Image,
887 IN UINTN ImageSize,
888 IN CONST VOID *VendorCode,
889 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
890 OUT CHAR16 **AbortReason
891 )
892 {
893 EFI_STATUS Status;
894 UINT32 Updateable;
895 BOOLEAN BooleanValue;
896 UINT32 FmpHeaderSize;
897 VOID *FmpHeader;
898 UINTN FmpPayloadSize;
899 UINT32 AllHeaderSize;
900 UINT32 IncommingFwVersion;
901 UINT32 LastAttemptStatus;
902
903 Status = EFI_SUCCESS;
904 Updateable = 0;
905 BooleanValue = FALSE;
906 FmpHeaderSize = 0;
907 FmpHeader = NULL;
908 FmpPayloadSize = 0;
909 AllHeaderSize = 0;
910 IncommingFwVersion = 0;
911 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
912
913
914 SetLastAttemptVersionInVariable (IncommingFwVersion); //set to 0 to clear any previous results.
915
916 //
917 // if we have locked the device, then skip the set operation.
918 // it should be blocked by hardware too but we can catch here even faster
919 //
920 if (mFmpDeviceLocked) {
921 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Device is already locked. Can't update.\n"));
922 Status = EFI_ACCESS_DENIED;
923 goto cleanup;
924 }
925
926 //
927 // Call check image to verify the image
928 //
929 Status = CheckTheImage (This, ImageIndex, Image, ImageSize, &Updateable);
930 if (EFI_ERROR (Status)) {
931 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Check The Image failed with %r.\n", Status));
932 if (Status == EFI_SECURITY_VIOLATION) {
933 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR;
934 }
935 goto cleanup;
936 }
937
938 //
939 // No functional error in CheckTheImage. Attempt to get the Version to
940 // support better error reporting.
941 //
942 FmpHeader = GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &FmpPayloadSize );
943 if (FmpHeader == NULL) {
944 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - GetFmpHeader failed.\n"));
945 Status = EFI_ABORTED;
946 goto cleanup;
947 }
948 Status = GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &IncommingFwVersion);
949 if (!EFI_ERROR (Status)) {
950 //
951 // Set to actual value
952 //
953 SetLastAttemptVersionInVariable (IncommingFwVersion);
954 }
955
956
957 if (Updateable != IMAGE_UPDATABLE_VALID) {
958 DEBUG (
959 (DEBUG_ERROR,
960 "FmpDxed: SetTheImage() - Check The Image returned that the Image was not valid for update. Updatable value = 0x%X.\n",
961 Updateable)
962 );
963 Status = EFI_ABORTED;
964 goto cleanup;
965 }
966
967 if (Progress == NULL) {
968 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Invalid progress callback\n"));
969 Status = EFI_INVALID_PARAMETER;
970 goto cleanup;
971 }
972
973 mProgressFunc = Progress;
974 mProgressSupported = TRUE;
975
976 //
977 // Checking the image is at least 1%
978 //
979 Status = Progress (1);
980 if (EFI_ERROR (Status)) {
981 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Progress Callback failed with Status %r.\n", Status));
982 }
983
984 //
985 //Check System Power
986 //
987 Status = CheckSystemPower (&BooleanValue);
988 if (EFI_ERROR (Status)) {
989 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - CheckSystemPower - API call failed %r.\n", Status));
990 goto cleanup;
991 }
992 if (!BooleanValue) {
993 Status = EFI_ABORTED;
994 DEBUG (
995 (DEBUG_ERROR,
996 "FmpDxe: SetTheImage() - CheckSystemPower - returned False. Update not allowed due to System Power.\n")
997 );
998 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_PWR_EVT_BATT;
999 goto cleanup;
1000 }
1001
1002 Progress (2);
1003
1004 //
1005 //Check System Thermal
1006 //
1007 Status = CheckSystemThermal (&BooleanValue);
1008 if (EFI_ERROR (Status)) {
1009 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - CheckSystemThermal - API call failed %r.\n", Status));
1010 goto cleanup;
1011 }
1012 if (!BooleanValue) {
1013 Status = EFI_ABORTED;
1014 DEBUG (
1015 (DEBUG_ERROR,
1016 "FmpDxe: SetTheImage() - CheckSystemThermal - returned False. Update not allowed due to System Thermal.\n")
1017 );
1018 goto cleanup;
1019 }
1020
1021 Progress (3);
1022
1023 //
1024 //Check System Environment
1025 //
1026 Status = CheckSystemEnvironment (&BooleanValue);
1027 if (EFI_ERROR (Status)) {
1028 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - CheckSystemEnvironment - API call failed %r.\n", Status));
1029 goto cleanup;
1030 }
1031 if (!BooleanValue) {
1032 Status = EFI_ABORTED;
1033 DEBUG (
1034 (DEBUG_ERROR,
1035 "FmpDxe: SetTheImage() - CheckSystemEnvironment - returned False. Update not allowed due to System Environment.\n")
1036 );
1037 goto cleanup;
1038 }
1039
1040 Progress (4);
1041
1042 //
1043 // Save LastAttemptStatus as error so that if SetImage never returns the error
1044 // state is recorded.
1045 //
1046 SetLastAttemptStatusInVariable (LastAttemptStatus);
1047
1048 //
1049 // Strip off all the headers so the device can process its firmware
1050 //
1051 Status = GetFmpPayloadHeaderSize (FmpHeader, FmpPayloadSize, &FmpHeaderSize);
1052 if (EFI_ERROR (Status)) {
1053 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status));
1054 goto cleanup;
1055 }
1056
1057 AllHeaderSize = GetAllHeaderSize ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize );
1058 if (AllHeaderSize == 0) {
1059 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - GetAllHeaderSize failed.\n"));
1060 Status = EFI_ABORTED;
1061 goto cleanup;
1062 }
1063
1064 //
1065 // Indicate that control is handed off to FmpDeviceLib
1066 //
1067 Progress (5);
1068
1069 //
1070 //Copy the requested image to the firmware using the FmpDeviceLib
1071 //
1072 Status = FmpDeviceSetImage (
1073 (((UINT8 *)Image) + AllHeaderSize),
1074 ImageSize - AllHeaderSize,
1075 VendorCode,
1076 FmpDxeProgress,
1077 IncommingFwVersion,
1078 AbortReason
1079 );
1080 if (EFI_ERROR (Status)) {
1081 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() SetImage from FmpDeviceLib failed. Status = %r.\n", Status));
1082 goto cleanup;
1083 }
1084
1085
1086 //
1087 // Finished the update without error
1088 // Indicate that control has been returned from FmpDeviceLib
1089 //
1090 Progress (99);
1091
1092 //
1093 // Update the version stored in variable
1094 //
1095 if (!mRuntimeVersionSupported) {
1096 UINT32 Version = DEFAULT_VERSION;
1097 GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &Version);
1098 SetVersionInVariable (Version);
1099 }
1100
1101 //
1102 // Update lowest supported variable
1103 //
1104 {
1105 UINT32 Version = DEFAULT_LOWESTSUPPORTEDVERSION;
1106 GetFmpPayloadHeaderLowestSupportedVersion (FmpHeader, FmpPayloadSize, &Version);
1107 SetLowestSupportedVersionInVariable (Version);
1108 }
1109
1110 LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
1111
1112 //
1113 // Set flag so the descriptor is repopulated
1114 // This only applied to devices that do not require system reboot
1115 //
1116 if (!PcdGetBool (PcdFmpDeviceSystemResetRequired)) {
1117 mDescriptorPopulated = FALSE;
1118 }
1119
1120 cleanup:
1121 mProgressFunc = NULL;
1122 mProgressSupported = FALSE;
1123 SetLastAttemptStatusInVariable (LastAttemptStatus);
1124
1125 if (Progress != NULL) {
1126 //
1127 // Set progress to 100 after everything is done including recording Status.
1128 //
1129 Progress (100);
1130 }
1131
1132 return Status;
1133 }
1134
1135 /**
1136 Returns information about the firmware package.
1137
1138 This function returns package information.
1139
1140 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
1141 @param[out] PackageVersion A version number that represents all the firmware images in the device.
1142 The format is vendor specific and new version must have a greater value
1143 than the old version. If PackageVersion is not supported, the value is
1144 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
1145 comparison is to be performed using PackageVersionName. A value of
1146 0xFFFFFFFD indicates that package version update is in progress.
1147 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing
1148 the package version name. The buffer is allocated by this function with
1149 AllocatePool(), and it is the caller's responsibility to free it with a
1150 call to FreePool().
1151 @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
1152 package version name. A value of 0 indicates the device does not support
1153 update of package version name. Length is the number of Unicode characters,
1154 including the terminating null character.
1155 @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute
1156 Definitions' for possible returned values of this parameter. A value of 1
1157 indicates the attribute is supported and the current setting value is
1158 indicated in AttributesSetting. A value of 0 indicates the attribute is not
1159 supported and the current setting value in AttributesSetting is meaningless.
1160 @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned
1161 values of this parameter
1162
1163 @retval EFI_SUCCESS The package information was successfully returned.
1164 @retval EFI_UNSUPPORTED The operation is not supported.
1165
1166 **/
1167 EFI_STATUS
1168 EFIAPI
1169 GetPackageInfo (
1170 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
1171 OUT UINT32 *PackageVersion,
1172 OUT CHAR16 **PackageVersionName,
1173 OUT UINT32 *PackageVersionNameMaxLen,
1174 OUT UINT64 *AttributesSupported,
1175 OUT UINT64 *AttributesSetting
1176 )
1177 {
1178 return EFI_UNSUPPORTED;
1179 }
1180
1181 /**
1182 Updates information about the firmware package.
1183
1184 This function updates package information.
1185 This function returns EFI_UNSUPPORTED if the package information is not updatable.
1186 VendorCode enables vendor to implement vendor-specific package information update policy.
1187 Null if the caller did not specify this policy or use the default policy.
1188
1189 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
1190 @param[in] Image Points to the authentication image.
1191 Null if authentication is not required.
1192 @param[in] ImageSize Size of the authentication image in bytes.
1193 0 if authentication is not required.
1194 @param[in] VendorCode This enables vendor to implement vendor-specific firmware
1195 image update policy.
1196 Null indicates the caller did not specify this policy or use
1197 the default policy.
1198 @param[in] PackageVersion The new package version.
1199 @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing
1200 the package version name.
1201 The string length is equal to or less than the value returned in
1202 PackageVersionNameMaxLen.
1203
1204 @retval EFI_SUCCESS The device was successfully updated with the new package
1205 information.
1206 @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value
1207 returned in PackageVersionNameMaxLen.
1208 @retval EFI_UNSUPPORTED The operation is not supported.
1209 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
1210
1211 **/
1212 EFI_STATUS
1213 EFIAPI
1214 SetPackageInfo (
1215 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
1216 IN CONST VOID *Image,
1217 IN UINTN ImageSize,
1218 IN CONST VOID *VendorCode,
1219 IN UINT32 PackageVersion,
1220 IN CONST CHAR16 *PackageVersionName
1221 )
1222 {
1223 return EFI_UNSUPPORTED;
1224 }
1225
1226 /**
1227 Event notification function that is invoked when the event GUID specified by
1228 PcdFmpDeviceLockEventGuid is signaled.
1229
1230 @param[in] Event Event whose notification function is being invoked.
1231 @param[in] Context The pointer to the notification function's context,
1232 which is implementation-dependent.
1233 **/
1234 VOID
1235 EFIAPI
1236 FmpDxeLockEventNotify (
1237 IN EFI_EVENT Event,
1238 IN VOID *Context
1239 )
1240 {
1241 EFI_STATUS Status;
1242
1243 if (!mFmpDeviceLocked) {
1244 if (IsLockFmpDeviceAtLockEventGuidRequired ()) {
1245 //
1246 // Lock all UEFI Variables used by this module.
1247 //
1248 Status = LockAllFmpVariables ();
1249 if (EFI_ERROR (Status)) {
1250 DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to lock variables. Status = %r.\n"));
1251 } else {
1252 DEBUG ((DEBUG_INFO, "FmpDxe: All variables locked\n"));
1253 }
1254
1255 //
1256 // Lock the firmware device
1257 //
1258 Status = FmpDeviceLock();
1259 if (EFI_ERROR (Status)) {
1260 if (Status != EFI_UNSUPPORTED) {
1261 DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLock() returned error. Status = %r\n", Status));
1262 } else {
1263 DEBUG ((DEBUG_WARN, "FmpDxe: FmpDeviceLock() returned error. Status = %r\n", Status));
1264 }
1265 }
1266 mFmpDeviceLocked = TRUE;
1267 } else {
1268 DEBUG ((DEBUG_VERBOSE, "FmpDxe: Not calling FmpDeviceLock() because mfg mode\n"));
1269 }
1270 }
1271 }
1272
1273 /**
1274 Function to install FMP instance.
1275
1276 @param[in] Handle The device handle to install a FMP instance on.
1277
1278 @retval EFI_SUCCESS FMP Installed
1279 @retval EFI_INVALID_PARAMETER Handle was invalid
1280 @retval other Error installing FMP
1281
1282 **/
1283 EFI_STATUS
1284 EFIAPI
1285 InstallFmpInstance (
1286 IN EFI_HANDLE Handle
1287 )
1288 {
1289 EFI_STATUS Status;
1290 EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;
1291 EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL *FmpProgress;
1292
1293 Status = EFI_SUCCESS;
1294 Fmp = NULL;
1295 FmpProgress = NULL;
1296
1297 //
1298 // Only allow a single FMP Protocol instance to be installed
1299 //
1300 if (mFmpInstalled) {
1301 return EFI_ALREADY_STARTED;
1302 }
1303
1304 //
1305 // Allocate FMP Protocol instance
1306 //
1307 Fmp = AllocateZeroPool (sizeof (EFI_FIRMWARE_MANAGEMENT_PROTOCOL));
1308 if (Fmp == NULL) {
1309 DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to allocate memory for FMP Protocol instance.\n"));
1310 Status = EFI_OUT_OF_RESOURCES;
1311 goto cleanup;
1312 }
1313
1314 //
1315 // Allocate FMP Progress Protocol instance
1316 //
1317 FmpProgress = AllocateZeroPool (sizeof (EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL));
1318 if (FmpProgress == NULL) {
1319 DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to allocate memory for FMP Progress Protocol instance.\n"));
1320 Status = EFI_OUT_OF_RESOURCES;
1321 FreePool (Fmp);
1322 goto cleanup;
1323 }
1324
1325 //
1326 // Set up FMP Protocol function pointers
1327 //
1328 Fmp->GetImageInfo = GetTheImageInfo;
1329 Fmp->GetImage = GetTheImage;
1330 Fmp->SetImage = SetTheImage;
1331 Fmp->CheckImage = CheckTheImage;
1332 Fmp->GetPackageInfo = GetPackageInfo;
1333 Fmp->SetPackageInfo = SetPackageInfo;
1334
1335 //
1336 // Fill in FMP Progress Protocol fields for Version 1
1337 //
1338 FmpProgress->Version = 1;
1339 FmpProgress->ProgressBarForegroundColor.Raw = PcdGet32 (PcdFmpDeviceProgressColor);
1340 FmpProgress->WatchdogSeconds = PcdGet8 (PcdFmpDeviceProgressWatchdogTimeInSeconds);
1341
1342 //
1343 // Install FMP Protocol and FMP Progress Protocol
1344 //
1345 Status = gBS->InstallMultipleProtocolInterfaces (
1346 &Handle,
1347 &gEfiFirmwareManagementProtocolGuid,
1348 Fmp,
1349 &gEdkiiFirmwareManagementProgressProtocolGuid,
1350 FmpProgress,
1351 NULL
1352 );
1353
1354 if (EFI_ERROR (Status)) {
1355 DEBUG ((DEBUG_ERROR, "FmpDxe: FMP Protocol install error. Status = %r.\n", Status));
1356 FreePool (Fmp);
1357 goto cleanup;
1358 }
1359
1360 DEBUG ((DEBUG_INFO, "FmpDxe: FMP Protocol Installed!\n"));
1361 mFmpInstalled = TRUE;
1362
1363 cleanup:
1364
1365 return Status;
1366 }
1367
1368 /**
1369 Main entry for this driver/library.
1370
1371 @param[in] ImageHandle Image handle this driver.
1372 @param[in] SystemTable Pointer to SystemTable.
1373
1374 **/
1375 EFI_STATUS
1376 EFIAPI
1377 FmpDxeEntryPoint (
1378 IN EFI_HANDLE ImageHandle,
1379 IN EFI_SYSTEM_TABLE *SystemTable
1380 )
1381 {
1382 EFI_STATUS Status;
1383 EFI_GUID *LockGuid;
1384
1385 //
1386 // Verify that a new FILE_GUID value has been provided in the <Defines>
1387 // section of this module. The FILE_GUID is the ESRT GUID that must be
1388 // unique for each updatable firmware image.
1389 //
1390 if (CompareGuid (&mDefaultModuleFileGuid, &gEfiCallerIdGuid)) {
1391 DEBUG ((DEBUG_ERROR, "FmpDxe: Use of default FILE_GUID detected. FILE_GUID must be set to a unique value.\n"));
1392 ASSERT (FALSE);
1393 return EFI_UNSUPPORTED;
1394 }
1395
1396 //
1397 // Get the ImageIdName value for the EFI_FIRMWARE_IMAGE_DESCRIPTOR from a PCD.
1398 //
1399 mImageIdName = (CHAR16 *) PcdGetPtr (PcdFmpDeviceImageIdName);
1400 if (PcdGetSize (PcdFmpDeviceImageIdName) <= 2 || mImageIdName[0] == 0) {
1401 //
1402 // PcdFmpDeviceImageIdName must be set to a non-empty Unicode string
1403 //
1404 DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib PcdFmpDeviceImageIdName is an empty string.\n"));
1405 ASSERT (FALSE);
1406 }
1407
1408 //
1409 // Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
1410 //
1411 DetectTestKey ();
1412
1413 //
1414 // Register with library the install function so if the library uses
1415 // UEFI driver model/driver binding protocol it can install FMP on its device handle
1416 // If library is simple lib that does not use driver binding then it should return
1417 // unsupported and this will install the FMP instance on the ImageHandle
1418 //
1419 Status = RegisterFmpInstaller (InstallFmpInstance);
1420 if (Status == EFI_UNSUPPORTED) {
1421 DEBUG ((DEBUG_INFO, "FmpDxe: FmpDeviceLib registration returned EFI_UNSUPPORTED. Installing single FMP instance.\n"));
1422 Status = InstallFmpInstance (ImageHandle);
1423 } else if (EFI_ERROR (Status)) {
1424 DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib registration returned %r. No FMP installed.\n", Status));
1425 } else {
1426 DEBUG ((
1427 DEBUG_INFO,
1428 "FmpDxe: FmpDeviceLib registration returned EFI_SUCCESS. Expect FMP to be installed during the BDS/Device connection phase.\n"
1429 ));
1430 }
1431
1432 //
1433 // Register notify function to lock the FMP device.
1434 // The lock event GUID is retrieved from PcdFmpDeviceLockEventGuid.
1435 // If PcdFmpDeviceLockEventGuid is not the size of an EFI_GUID, then
1436 // gEfiEndOfDxeEventGroupGuid is used.
1437 //
1438 LockGuid = &gEfiEndOfDxeEventGroupGuid;
1439 if (PcdGetSize (PcdFmpDeviceLockEventGuid) == sizeof (EFI_GUID)) {
1440 LockGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceLockEventGuid);
1441 }
1442 DEBUG ((DEBUG_INFO, "FmpDxe: Lock GUID: %g\n", LockGuid));
1443
1444 Status = gBS->CreateEventEx (
1445 EVT_NOTIFY_SIGNAL,
1446 TPL_CALLBACK,
1447 FmpDxeLockEventNotify,
1448 NULL,
1449 LockGuid,
1450 &mFmpDeviceLockEvent
1451 );
1452 if (EFI_ERROR (Status)) {
1453 DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to register for ready to boot. Status = %r\n", Status));
1454 }
1455 ASSERT_EFI_ERROR (Status);
1456
1457 return Status;
1458 }