FmpDevicePkg FmpDxe: Return 0 when LSV check is not required
[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_VIOLATIO 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_INVALID_PARAMETER The Image was NULL.
630 @retval EFI_UNSUPPORTED The operation is not supported.
631 @retval EFI_SECURITY_VIOLATIO The operation could not be performed due to an authentication failure.
632
633 **/
634 EFI_STATUS
635 EFIAPI
636 CheckTheImage (
637 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
638 IN UINT8 ImageIndex,
639 IN CONST VOID *Image,
640 IN UINTN ImageSize,
641 OUT UINT32 *ImageUpdateable
642 )
643 {
644 EFI_STATUS Status;
645 UINTN RawSize;
646 VOID *FmpPayloadHeader;
647 UINTN FmpPayloadSize;
648 UINT32 Version;
649 UINT32 FmpHeaderSize;
650 UINTN AllHeaderSize;
651 UINT32 Index;
652 VOID *PublicKeyData;
653 UINTN PublicKeyDataLength;
654 UINT8 *PublicKeyDataXdr;
655 UINT8 *PublicKeyDataXdrEnd;
656
657 Status = EFI_SUCCESS;
658 RawSize = 0;
659 FmpPayloadHeader = NULL;
660 FmpPayloadSize = 0;
661 Version = 0;
662 FmpHeaderSize = 0;
663 AllHeaderSize = 0;
664
665 //
666 // make sure the descriptor has already been loaded
667 //
668 if (!mDescriptorPopulated) {
669 PopulateDescriptor();
670 }
671
672 if (ImageUpdateable == NULL) {
673 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - ImageUpdateable Pointer Parameter is NULL.\n"));
674 Status = EFI_INVALID_PARAMETER;
675 goto cleanup;
676 }
677
678 //
679 //Set to valid and then if any tests fail it will update this flag.
680 //
681 *ImageUpdateable = IMAGE_UPDATABLE_VALID;
682
683 if (Image == NULL) {
684 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - Image Pointer Parameter is NULL.\n"));
685 //
686 // not sure if this is needed
687 //
688 *ImageUpdateable = IMAGE_UPDATABLE_INVALID;
689 return EFI_INVALID_PARAMETER;
690 }
691
692 PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
693 PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
694
695 if (PublicKeyDataXdr == NULL || (PublicKeyDataXdr == PublicKeyDataXdrEnd)) {
696 DEBUG ((DEBUG_ERROR, "FmpDxe: Invalid certificate, skipping it.\n"));
697 Status = EFI_ABORTED;
698 } else {
699 //
700 // Try each key from PcdFmpDevicePkcs7CertBufferXdr
701 //
702 for (Index = 1; PublicKeyDataXdr < PublicKeyDataXdrEnd; Index++) {
703 Index++;
704 DEBUG (
705 (DEBUG_INFO,
706 "FmpDxe: Certificate #%d [%p..%p].\n",
707 Index,
708 PublicKeyDataXdr,
709 PublicKeyDataXdrEnd
710 )
711 );
712
713 if ((PublicKeyDataXdr + sizeof (UINT32)) > PublicKeyDataXdrEnd) {
714 //
715 // Key data extends beyond end of PCD
716 //
717 DEBUG ((DEBUG_ERROR, "FmpDxe: Certificate size extends beyond end of PCD, skipping it.\n"));
718 Status = EFI_ABORTED;
719 break;
720 }
721 //
722 // Read key length stored in big-endian format
723 //
724 PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
725 //
726 // Point to the start of the key data
727 //
728 PublicKeyDataXdr += sizeof (UINT32);
729 if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
730 //
731 // Key data extends beyond end of PCD
732 //
733 DEBUG ((DEBUG_ERROR, "FmpDxe: Certificate extends beyond end of PCD, skipping it.\n"));
734 Status = EFI_ABORTED;
735 break;
736 }
737 PublicKeyData = PublicKeyDataXdr;
738 Status = AuthenticateFmpImage (
739 (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image,
740 ImageSize,
741 PublicKeyData,
742 PublicKeyDataLength
743 );
744 if (!EFI_ERROR (Status)) {
745 break;
746 }
747 PublicKeyDataXdr += PublicKeyDataLength;
748 PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
749 }
750 }
751
752 if (EFI_ERROR (Status)) {
753 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - Authentication Failed %r.\n", Status));
754 goto cleanup;
755 }
756
757 //
758 // Check to make sure index is 1
759 //
760 if (ImageIndex != 1) {
761 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - Image Index Invalid.\n"));
762 *ImageUpdateable = IMAGE_UPDATABLE_INVALID_TYPE;
763 Status = EFI_SUCCESS;
764 goto cleanup;
765 }
766
767
768 //
769 // Check the FmpPayloadHeader
770 //
771 FmpPayloadHeader = GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &FmpPayloadSize );
772 if (FmpPayloadHeader == NULL) {
773 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpHeader failed.\n"));
774 Status = EFI_ABORTED;
775 goto cleanup;
776 }
777 Status = GetFmpPayloadHeaderVersion (FmpPayloadHeader, FmpPayloadSize, &Version);
778 if (EFI_ERROR (Status)) {
779 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderVersion failed %r.\n", Status));
780 *ImageUpdateable = IMAGE_UPDATABLE_INVALID;
781 Status = EFI_SUCCESS;
782 goto cleanup;
783 }
784
785 //
786 // Check the lowest supported version
787 //
788 if (Version < mDesc.LowestSupportedImageVersion) {
789 DEBUG (
790 (DEBUG_ERROR,
791 "FmpDxe: CheckTheImage() - Version Lower than lowest supported version. 0x%08X < 0x%08X\n",
792 Version, mDesc.LowestSupportedImageVersion)
793 );
794 *ImageUpdateable = IMAGE_UPDATABLE_INVALID_OLD;
795 Status = EFI_SUCCESS;
796 goto cleanup;
797 }
798
799 //
800 // Get the FmpHeaderSize so we can determine the real payload size
801 //
802 Status = GetFmpPayloadHeaderSize (FmpPayloadHeader, FmpPayloadSize, &FmpHeaderSize);
803 if (EFI_ERROR (Status)) {
804 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status));
805 *ImageUpdateable = IMAGE_UPDATABLE_INVALID;
806 Status = EFI_SUCCESS;
807 goto cleanup;
808 }
809
810 //
811 // Call FmpDevice Lib Check Image on the
812 // Raw payload. So all headers need stripped off
813 //
814 AllHeaderSize = GetAllHeaderSize ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize );
815 if (AllHeaderSize == 0) {
816 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetAllHeaderSize failed.\n"));
817 Status = EFI_ABORTED;
818 goto cleanup;
819 }
820 RawSize = ImageSize - AllHeaderSize;
821
822 //
823 // FmpDeviceLib CheckImage function to do any specific checks
824 //
825 Status = FmpDeviceCheckImage ((((UINT8 *)Image) + AllHeaderSize), RawSize, ImageUpdateable);
826 if (EFI_ERROR (Status)) {
827 DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - FmpDeviceLib CheckImage failed. Status = %r\n", Status));
828 }
829
830 cleanup:
831 return Status;
832 }
833
834 /**
835 Updates the firmware image of the device.
836
837 This function updates the hardware with the new firmware image.
838 This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
839 If the firmware image is updatable, the function should perform the following minimal validations
840 before proceeding to do the firmware image update.
841 - Validate the image authentication if image has attribute
842 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
843 EFI_SECURITY_VIOLATION if the validation fails.
844 - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
845 the image is unsupported. The function can optionally provide more detailed information on
846 why the image is not a supported image.
847 - Validate the data from VendorCode if not null. Image validation must be performed before
848 VendorCode data validation. VendorCode data is ignored or considered invalid if image
849 validation failed. The function returns EFI_ABORTED if the data is invalid.
850
851 VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
852 the caller did not specify the policy or use the default policy. As an example, vendor can implement
853 a policy to allow an option to force a firmware image update when the abort reason is due to the new
854 firmware image version is older than the current firmware image version or bad image checksum.
855 Sensitive operations such as those wiping the entire firmware image and render the device to be
856 non-functional should be encoded in the image itself rather than passed with the VendorCode.
857 AbortReason enables vendor to have the option to provide a more detailed description of the abort
858 reason to the caller.
859
860 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
861 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
862 The number is between 1 and DescriptorCount.
863 @param[in] Image Points to the new image.
864 @param[in] ImageSize Size of the new image in bytes.
865 @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
866 Null indicates the caller did not specify the policy or use the default policy.
867 @param[in] Progress A function used by the driver to report the progress of the firmware update.
868 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
869 details for the aborted operation. The buffer is allocated by this function
870 with AllocatePool(), and it is the caller's responsibility to free it with a
871 call to FreePool().
872
873 @retval EFI_SUCCESS The device was successfully updated with the new image.
874 @retval EFI_ABORTED The operation is aborted.
875 @retval EFI_INVALID_PARAMETER The Image was NULL.
876 @retval EFI_UNSUPPORTED The operation is not supported.
877 @retval EFI_SECURITY_VIOLATIO The operation could not be performed due to an authentication failure.
878
879 **/
880 EFI_STATUS
881 EFIAPI
882 SetTheImage (
883 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
884 IN UINT8 ImageIndex,
885 IN CONST VOID *Image,
886 IN UINTN ImageSize,
887 IN CONST VOID *VendorCode,
888 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
889 OUT CHAR16 **AbortReason
890 )
891 {
892 EFI_STATUS Status;
893 UINT32 Updateable;
894 BOOLEAN BooleanValue;
895 UINT32 FmpHeaderSize;
896 VOID *FmpHeader;
897 UINTN FmpPayloadSize;
898 UINT32 AllHeaderSize;
899 UINT32 IncommingFwVersion;
900 UINT32 LastAttemptStatus;
901
902 Status = EFI_SUCCESS;
903 Updateable = 0;
904 BooleanValue = FALSE;
905 FmpHeaderSize = 0;
906 FmpHeader = NULL;
907 FmpPayloadSize = 0;
908 AllHeaderSize = 0;
909 IncommingFwVersion = 0;
910 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
911
912
913 SetLastAttemptVersionInVariable (IncommingFwVersion); //set to 0 to clear any previous results.
914
915 //
916 // if we have locked the device, then skip the set operation.
917 // it should be blocked by hardware too but we can catch here even faster
918 //
919 if (mFmpDeviceLocked) {
920 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Device is already locked. Can't update.\n"));
921 Status = EFI_ACCESS_DENIED;
922 goto cleanup;
923 }
924
925 //
926 // Call check image to verify the image
927 //
928 Status = CheckTheImage (This, ImageIndex, Image, ImageSize, &Updateable);
929 if (EFI_ERROR (Status)) {
930 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Check The Image failed with %r.\n", Status));
931 if (Status == EFI_SECURITY_VIOLATION) {
932 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR;
933 }
934 goto cleanup;
935 }
936
937 //
938 // No functional error in CheckTheImage. Attempt to get the Version to
939 // support better error reporting.
940 //
941 FmpHeader = GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &FmpPayloadSize );
942 if (FmpHeader == NULL) {
943 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - GetFmpHeader failed.\n"));
944 Status = EFI_ABORTED;
945 goto cleanup;
946 }
947 Status = GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &IncommingFwVersion);
948 if (!EFI_ERROR (Status)) {
949 //
950 // Set to actual value
951 //
952 SetLastAttemptVersionInVariable (IncommingFwVersion);
953 }
954
955
956 if (Updateable != IMAGE_UPDATABLE_VALID) {
957 DEBUG (
958 (DEBUG_ERROR,
959 "FmpDxed: SetTheImage() - Check The Image returned that the Image was not valid for update. Updatable value = 0x%X.\n",
960 Updateable)
961 );
962 Status = EFI_ABORTED;
963 goto cleanup;
964 }
965
966 if (Progress == NULL) {
967 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Invalid progress callback\n"));
968 Status = EFI_INVALID_PARAMETER;
969 goto cleanup;
970 }
971
972 mProgressFunc = Progress;
973 mProgressSupported = TRUE;
974
975 //
976 // Checking the image is at least 1%
977 //
978 Status = Progress (1);
979 if (EFI_ERROR (Status)) {
980 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Progress Callback failed with Status %r.\n", Status));
981 }
982
983 //
984 //Check System Power
985 //
986 Status = CheckSystemPower (&BooleanValue);
987 if (EFI_ERROR (Status)) {
988 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - CheckSystemPower - API call failed %r.\n", Status));
989 goto cleanup;
990 }
991 if (!BooleanValue) {
992 Status = EFI_ABORTED;
993 DEBUG (
994 (DEBUG_ERROR,
995 "FmpDxe: SetTheImage() - CheckSystemPower - returned False. Update not allowed due to System Power.\n")
996 );
997 LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_PWR_EVT_BATT;
998 goto cleanup;
999 }
1000
1001 Progress (2);
1002
1003 //
1004 //Check System Thermal
1005 //
1006 Status = CheckSystemThermal (&BooleanValue);
1007 if (EFI_ERROR (Status)) {
1008 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - CheckSystemThermal - API call failed %r.\n", Status));
1009 goto cleanup;
1010 }
1011 if (!BooleanValue) {
1012 Status = EFI_ABORTED;
1013 DEBUG (
1014 (DEBUG_ERROR,
1015 "FmpDxe: SetTheImage() - CheckSystemThermal - returned False. Update not allowed due to System Thermal.\n")
1016 );
1017 goto cleanup;
1018 }
1019
1020 Progress (3);
1021
1022 //
1023 //Check System Environment
1024 //
1025 Status = CheckSystemEnvironment (&BooleanValue);
1026 if (EFI_ERROR (Status)) {
1027 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - CheckSystemEnvironment - API call failed %r.\n", Status));
1028 goto cleanup;
1029 }
1030 if (!BooleanValue) {
1031 Status = EFI_ABORTED;
1032 DEBUG (
1033 (DEBUG_ERROR,
1034 "FmpDxe: SetTheImage() - CheckSystemEnvironment - returned False. Update not allowed due to System Environment.\n")
1035 );
1036 goto cleanup;
1037 }
1038
1039 Progress (4);
1040
1041 //
1042 // Save LastAttemptStatus as error so that if SetImage never returns the error
1043 // state is recorded.
1044 //
1045 SetLastAttemptStatusInVariable (LastAttemptStatus);
1046
1047 //
1048 // Strip off all the headers so the device can process its firmware
1049 //
1050 Status = GetFmpPayloadHeaderSize (FmpHeader, FmpPayloadSize, &FmpHeaderSize);
1051 if (EFI_ERROR (Status)) {
1052 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status));
1053 goto cleanup;
1054 }
1055
1056 AllHeaderSize = GetAllHeaderSize ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize );
1057 if (AllHeaderSize == 0) {
1058 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - GetAllHeaderSize failed.\n"));
1059 Status = EFI_ABORTED;
1060 goto cleanup;
1061 }
1062
1063 //
1064 // Indicate that control is handed off to FmpDeviceLib
1065 //
1066 Progress (5);
1067
1068 //
1069 //Copy the requested image to the firmware using the FmpDeviceLib
1070 //
1071 Status = FmpDeviceSetImage (
1072 (((UINT8 *)Image) + AllHeaderSize),
1073 ImageSize - AllHeaderSize,
1074 VendorCode,
1075 FmpDxeProgress,
1076 IncommingFwVersion,
1077 AbortReason
1078 );
1079 if (EFI_ERROR (Status)) {
1080 DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() SetImage from FmpDeviceLib failed. Status = %r.\n", Status));
1081 goto cleanup;
1082 }
1083
1084
1085 //
1086 // Finished the update without error
1087 // Indicate that control has been returned from FmpDeviceLib
1088 //
1089 Progress (99);
1090
1091 //
1092 // Update the version stored in variable
1093 //
1094 if (!mRuntimeVersionSupported) {
1095 UINT32 Version = DEFAULT_VERSION;
1096 GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &Version);
1097 SetVersionInVariable (Version);
1098 }
1099
1100 //
1101 // Update lowest supported variable
1102 //
1103 {
1104 UINT32 Version = DEFAULT_LOWESTSUPPORTEDVERSION;
1105 GetFmpPayloadHeaderLowestSupportedVersion (FmpHeader, FmpPayloadSize, &Version);
1106 SetLowestSupportedVersionInVariable (Version);
1107 }
1108
1109 LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
1110
1111 //
1112 // Set flag so the descriptor is repopulated
1113 // This only applied to devices that do not require system reboot
1114 //
1115 if (!PcdGetBool (PcdFmpDeviceSystemResetRequired)) {
1116 mDescriptorPopulated = FALSE;
1117 }
1118
1119 cleanup:
1120 mProgressFunc = NULL;
1121 mProgressSupported = FALSE;
1122 SetLastAttemptStatusInVariable (LastAttemptStatus);
1123
1124 //
1125 // Set progress to 100 after everything is done including recording Status.
1126 //
1127 Progress (100);
1128
1129 return Status;
1130 }
1131
1132 /**
1133 Returns information about the firmware package.
1134
1135 This function returns package information.
1136
1137 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
1138 @param[out] PackageVersion A version number that represents all the firmware images in the device.
1139 The format is vendor specific and new version must have a greater value
1140 than the old version. If PackageVersion is not supported, the value is
1141 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
1142 comparison is to be performed using PackageVersionName. A value of
1143 0xFFFFFFFD indicates that package version update is in progress.
1144 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing
1145 the package version name. The buffer is allocated by this function with
1146 AllocatePool(), and it is the caller's responsibility to free it with a
1147 call to FreePool().
1148 @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
1149 package version name. A value of 0 indicates the device does not support
1150 update of package version name. Length is the number of Unicode characters,
1151 including the terminating null character.
1152 @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute
1153 Definitions' for possible returned values of this parameter. A value of 1
1154 indicates the attribute is supported and the current setting value is
1155 indicated in AttributesSetting. A value of 0 indicates the attribute is not
1156 supported and the current setting value in AttributesSetting is meaningless.
1157 @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned
1158 values of this parameter
1159
1160 @retval EFI_SUCCESS The package information was successfully returned.
1161 @retval EFI_UNSUPPORTED The operation is not supported.
1162
1163 **/
1164 EFI_STATUS
1165 EFIAPI
1166 GetPackageInfo (
1167 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
1168 OUT UINT32 *PackageVersion,
1169 OUT CHAR16 **PackageVersionName,
1170 OUT UINT32 *PackageVersionNameMaxLen,
1171 OUT UINT64 *AttributesSupported,
1172 OUT UINT64 *AttributesSetting
1173 )
1174 {
1175 return EFI_UNSUPPORTED;
1176 }
1177
1178 /**
1179 Updates information about the firmware package.
1180
1181 This function updates package information.
1182 This function returns EFI_UNSUPPORTED if the package information is not updatable.
1183 VendorCode enables vendor to implement vendor-specific package information update policy.
1184 Null if the caller did not specify this policy or use the default policy.
1185
1186 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
1187 @param[in] Image Points to the authentication image.
1188 Null if authentication is not required.
1189 @param[in] ImageSize Size of the authentication image in bytes.
1190 0 if authentication is not required.
1191 @param[in] VendorCode This enables vendor to implement vendor-specific firmware
1192 image update policy.
1193 Null indicates the caller did not specify this policy or use
1194 the default policy.
1195 @param[in] PackageVersion The new package version.
1196 @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing
1197 the package version name.
1198 The string length is equal to or less than the value returned in
1199 PackageVersionNameMaxLen.
1200
1201 @retval EFI_SUCCESS The device was successfully updated with the new package
1202 information.
1203 @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value
1204 returned in PackageVersionNameMaxLen.
1205 @retval EFI_UNSUPPORTED The operation is not supported.
1206 @retval EFI_SECURITY_VIOLATIO The operation could not be performed due to an authentication failure.
1207
1208 **/
1209 EFI_STATUS
1210 EFIAPI
1211 SetPackageInfo (
1212 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
1213 IN CONST VOID *Image,
1214 IN UINTN ImageSize,
1215 IN CONST VOID *VendorCode,
1216 IN UINT32 PackageVersion,
1217 IN CONST CHAR16 *PackageVersionName
1218 )
1219 {
1220 return EFI_UNSUPPORTED;
1221 }
1222
1223 /**
1224 Event notification function that is invoked when the event GUID specified by
1225 PcdFmpDeviceLockEventGuid is signaled.
1226
1227 @param[in] Event Event whose notification function is being invoked.
1228 @param[in] Context The pointer to the notification function's context,
1229 which is implementation-dependent.
1230 **/
1231 VOID
1232 EFIAPI
1233 FmpDxeLockEventNotify (
1234 IN EFI_EVENT Event,
1235 IN VOID *Context
1236 )
1237 {
1238 EFI_STATUS Status;
1239
1240 if (!mFmpDeviceLocked) {
1241 if (IsLockFmpDeviceAtLockEventGuidRequired ()) {
1242 //
1243 // Lock all UEFI Variables used by this module.
1244 //
1245 Status = LockAllFmpVariables ();
1246 if (EFI_ERROR (Status)) {
1247 DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to lock variables. Status = %r.\n"));
1248 } else {
1249 DEBUG ((DEBUG_INFO, "FmpDxe: All variables locked\n"));
1250 }
1251
1252 //
1253 // Lock the firmware device
1254 //
1255 Status = FmpDeviceLock();
1256 if (EFI_ERROR (Status)) {
1257 if (Status != EFI_UNSUPPORTED) {
1258 DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLock() returned error. Status = %r\n", Status));
1259 } else {
1260 DEBUG ((DEBUG_WARN, "FmpDxe: FmpDeviceLock() returned error. Status = %r\n", Status));
1261 }
1262 }
1263 mFmpDeviceLocked = TRUE;
1264 } else {
1265 DEBUG ((DEBUG_VERBOSE, "FmpDxe: Not calling FmpDeviceLock() because mfg mode\n"));
1266 }
1267 }
1268 }
1269
1270 /**
1271 Function to install FMP instance.
1272
1273 @param[in] Handle The device handle to install a FMP instance on.
1274
1275 @retval EFI_SUCCESS FMP Installed
1276 @retval EFI_INVALID_PARAMETER Handle was invalid
1277 @retval other Error installing FMP
1278
1279 **/
1280 EFI_STATUS
1281 EFIAPI
1282 InstallFmpInstance (
1283 IN EFI_HANDLE Handle
1284 )
1285 {
1286 EFI_STATUS Status;
1287 EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;
1288 EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL *FmpProgress;
1289
1290 Status = EFI_SUCCESS;
1291 Fmp = NULL;
1292 FmpProgress = NULL;
1293
1294 //
1295 // Only allow a single FMP Protocol instance to be installed
1296 //
1297 if (mFmpInstalled) {
1298 return EFI_ALREADY_STARTED;
1299 }
1300
1301 //
1302 // Allocate FMP Protocol instance
1303 //
1304 Fmp = AllocateZeroPool (sizeof (EFI_FIRMWARE_MANAGEMENT_PROTOCOL));
1305 if (Fmp == NULL) {
1306 DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to allocate memory for FMP Protocol instance.\n"));
1307 Status = EFI_OUT_OF_RESOURCES;
1308 goto cleanup;
1309 }
1310
1311 //
1312 // Allocate FMP Progress Protocol instance
1313 //
1314 FmpProgress = AllocateZeroPool (sizeof (EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL));
1315 if (FmpProgress == NULL) {
1316 DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to allocate memory for FMP Progress Protocol instance.\n"));
1317 Status = EFI_OUT_OF_RESOURCES;
1318 FreePool (Fmp);
1319 goto cleanup;
1320 }
1321
1322 //
1323 // Set up FMP Protocol function pointers
1324 //
1325 Fmp->GetImageInfo = GetTheImageInfo;
1326 Fmp->GetImage = GetTheImage;
1327 Fmp->SetImage = SetTheImage;
1328 Fmp->CheckImage = CheckTheImage;
1329 Fmp->GetPackageInfo = GetPackageInfo;
1330 Fmp->SetPackageInfo = SetPackageInfo;
1331
1332 //
1333 // Fill in FMP Progress Protocol fields for Version 1
1334 //
1335 FmpProgress->Version = 1;
1336 FmpProgress->ProgressBarForegroundColor.Raw = PcdGet32 (PcdFmpDeviceProgressColor);
1337 FmpProgress->WatchdogSeconds = PcdGet8 (PcdFmpDeviceProgressWatchdogTimeInSeconds);
1338
1339 //
1340 // Install FMP Protocol and FMP Progress Protocol
1341 //
1342 Status = gBS->InstallMultipleProtocolInterfaces (
1343 &Handle,
1344 &gEfiFirmwareManagementProtocolGuid,
1345 Fmp,
1346 &gEdkiiFirmwareManagementProgressProtocolGuid,
1347 FmpProgress,
1348 NULL
1349 );
1350
1351 if (EFI_ERROR (Status)) {
1352 DEBUG ((DEBUG_ERROR, "FmpDxe: FMP Protocol install error. Status = %r.\n", Status));
1353 FreePool (Fmp);
1354 goto cleanup;
1355 }
1356
1357 DEBUG ((DEBUG_INFO, "FmpDxe: FMP Protocol Installed!\n"));
1358 mFmpInstalled = TRUE;
1359
1360 cleanup:
1361
1362 return Status;
1363 }
1364
1365 /**
1366 Main entry for this driver/library.
1367
1368 @param[in] ImageHandle Image handle this driver.
1369 @param[in] SystemTable Pointer to SystemTable.
1370
1371 **/
1372 EFI_STATUS
1373 EFIAPI
1374 FmpDxeEntryPoint (
1375 IN EFI_HANDLE ImageHandle,
1376 IN EFI_SYSTEM_TABLE *SystemTable
1377 )
1378 {
1379 EFI_STATUS Status;
1380 EFI_GUID *LockGuid;
1381
1382 //
1383 // Verify that a new FILE_GUID value has been provided in the <Defines>
1384 // section of this module. The FILE_GUID is the ESRT GUID that must be
1385 // unique for each updatable firmware image.
1386 //
1387 if (CompareGuid (&mDefaultModuleFileGuid, &gEfiCallerIdGuid)) {
1388 DEBUG ((DEBUG_ERROR, "FmpDxe: Use of default FILE_GUID detected. FILE_GUID must be set to a unique value.\n"));
1389 ASSERT (FALSE);
1390 return EFI_UNSUPPORTED;
1391 }
1392
1393 //
1394 // Get the ImageIdName value for the EFI_FIRMWARE_IMAGE_DESCRIPTOR from a PCD.
1395 //
1396 mImageIdName = (CHAR16 *) PcdGetPtr (PcdFmpDeviceImageIdName);
1397 if (PcdGetSize (PcdFmpDeviceImageIdName) <= 2 || mImageIdName[0] == 0) {
1398 //
1399 // PcdFmpDeviceImageIdName must be set to a non-empty Unicode string
1400 //
1401 DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib PcdFmpDeviceImageIdName is an empty string.\n"));
1402 ASSERT (FALSE);
1403 }
1404
1405 //
1406 // Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
1407 //
1408 DetectTestKey ();
1409
1410 //
1411 // Register with library the install function so if the library uses
1412 // UEFI driver model/driver binding protocol it can install FMP on its device handle
1413 // If library is simple lib that does not use driver binding then it should return
1414 // unsupported and this will install the FMP instance on the ImageHandle
1415 //
1416 Status = RegisterFmpInstaller (InstallFmpInstance);
1417 if (Status == EFI_UNSUPPORTED) {
1418 DEBUG ((DEBUG_INFO, "FmpDxe: FmpDeviceLib registration returned EFI_UNSUPPORTED. Installing single FMP instance.\n"));
1419 Status = InstallFmpInstance (ImageHandle);
1420 } else if (EFI_ERROR (Status)) {
1421 DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib registration returned %r. No FMP installed.\n", Status));
1422 } else {
1423 DEBUG ((
1424 DEBUG_INFO,
1425 "FmpDxe: FmpDeviceLib registration returned EFI_SUCCESS. Expect FMP to be installed during the BDS/Device connection phase.\n"
1426 ));
1427 }
1428
1429 //
1430 // Register notify function to lock the FMP device.
1431 // The lock event GUID is retrieved from PcdFmpDeviceLockEventGuid.
1432 // If PcdFmpDeviceLockEventGuid is not the size of an EFI_GUID, then
1433 // gEfiEndOfDxeEventGroupGuid is used.
1434 //
1435 LockGuid = &gEfiEndOfDxeEventGroupGuid;
1436 if (PcdGetSize (PcdFmpDeviceLockEventGuid) == sizeof (EFI_GUID)) {
1437 LockGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceLockEventGuid);
1438 }
1439 DEBUG ((DEBUG_INFO, "FmpDxe: Lock GUID: %g\n", LockGuid));
1440
1441 Status = gBS->CreateEventEx (
1442 EVT_NOTIFY_SIGNAL,
1443 TPL_CALLBACK,
1444 FmpDxeLockEventNotify,
1445 NULL,
1446 LockGuid,
1447 &mFmpDeviceLockEvent
1448 );
1449 if (EFI_ERROR (Status)) {
1450 DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to register for ready to boot. Status = %r\n", Status));
1451 }
1452 ASSERT_EFI_ERROR (Status);
1453
1454 return Status;
1455 }