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