Home | History | Annotate | Download | only in SystemFirmwareUpdate
      1 /** @file
      2   Produce FMP instance for system firmware.
      3 
      4   Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
      5   This program and the accompanying materials
      6   are licensed and made available under the terms and conditions of the BSD License
      7   which accompanies this distribution.  The full text of the license may be found at
      8   http://opensource.org/licenses/bsd-license.php
      9 
     10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     12 
     13 **/
     14 
     15 #include "SystemFirmwareDxe.h"
     16 
     17 EFI_GUID gSystemFmpLastAttemptVariableGuid = SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_GUID;
     18 EFI_GUID gSystemFmpProtocolGuid = SYSTEM_FMP_PROTOCOL_GUID;
     19 
     20 EFI_FIRMWARE_MANAGEMENT_PROTOCOL mFirmwareManagementProtocol = {
     21   FmpGetImageInfo,
     22   FmpGetImage,
     23   FmpSetImage,
     24   FmpCheckImage,
     25   FmpGetPackageInfo,
     26   FmpSetPackageInfo
     27 };
     28 
     29 /**
     30   Returns information about the current firmware image(s) of the device.
     31 
     32   This function allows a copy of the current firmware image to be created and saved.
     33   The saved copy could later been used, for example, in firmware image recovery or rollback.
     34 
     35   @param[in]      This               A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
     36   @param[in, out] ImageInfoSize      A pointer to the size, in bytes, of the ImageInfo buffer.
     37                                      On input, this is the size of the buffer allocated by the caller.
     38                                      On output, it is the size of the buffer returned by the firmware
     39                                      if the buffer was large enough, or the size of the buffer needed
     40                                      to contain the image(s) information if the buffer was too small.
     41   @param[in, out] ImageInfo          A pointer to the buffer in which firmware places the current image(s)
     42                                      information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
     43   @param[out]     DescriptorVersion  A pointer to the location in which firmware returns the version number
     44                                      associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
     45   @param[out]     DescriptorCount    A pointer to the location in which firmware returns the number of
     46                                      descriptors or firmware images within this device.
     47   @param[out]     DescriptorSize     A pointer to the location in which firmware returns the size, in bytes,
     48                                      of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.
     49   @param[out]     PackageVersion     A version number that represents all the firmware images in the device.
     50                                      The format is vendor specific and new version must have a greater value
     51                                      than the old version. If PackageVersion is not supported, the value is
     52                                      0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison
     53                                      is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates
     54                                      that package version update is in progress.
     55   @param[out]     PackageVersionName A pointer to a pointer to a null-terminated string representing the
     56                                      package version name. The buffer is allocated by this function with
     57                                      AllocatePool(), and it is the caller's responsibility to free it with a call
     58                                      to FreePool().
     59 
     60   @retval EFI_SUCCESS                The device was successfully updated with the new image.
     61   @retval EFI_BUFFER_TOO_SMALL       The ImageInfo buffer was too small. The current buffer size
     62                                      needed to hold the image(s) information is returned in ImageInfoSize.
     63   @retval EFI_INVALID_PARAMETER      ImageInfoSize is NULL.
     64   @retval EFI_DEVICE_ERROR           Valid information could not be returned. Possible corrupted image.
     65 
     66 **/
     67 EFI_STATUS
     68 EFIAPI
     69 FmpGetImageInfo (
     70   IN        EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
     71   IN OUT    UINTN                            *ImageInfoSize,
     72   IN OUT    EFI_FIRMWARE_IMAGE_DESCRIPTOR    *ImageInfo,
     73   OUT       UINT32                           *DescriptorVersion,
     74   OUT       UINT8                            *DescriptorCount,
     75   OUT       UINTN                            *DescriptorSize,
     76   OUT       UINT32                           *PackageVersion,
     77   OUT       CHAR16                           **PackageVersionName
     78   )
     79 {
     80   SYSTEM_FMP_PRIVATE_DATA                *SystemFmpPrivate;
     81   EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR *ImageDescriptor;
     82 
     83   SystemFmpPrivate = SYSTEM_FMP_PRIVATE_DATA_FROM_FMP(This);
     84 
     85   if(ImageInfoSize == NULL) {
     86     return EFI_INVALID_PARAMETER;
     87   }
     88 
     89   if (*ImageInfoSize < sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * SystemFmpPrivate->DescriptorCount) {
     90     *ImageInfoSize = sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * SystemFmpPrivate->DescriptorCount;
     91     return EFI_BUFFER_TOO_SMALL;
     92   }
     93 
     94   if (ImageInfo == NULL ||
     95       DescriptorVersion == NULL ||
     96       DescriptorCount == NULL ||
     97       DescriptorSize == NULL ||
     98       PackageVersion == NULL ||
     99       PackageVersionName == NULL) {
    100     return EFI_INVALID_PARAMETER;
    101   }
    102 
    103   *ImageInfoSize      = sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * SystemFmpPrivate->DescriptorCount;
    104   *DescriptorSize     = sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR);
    105   *DescriptorCount    = SystemFmpPrivate->DescriptorCount;
    106   *DescriptorVersion  = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;
    107 
    108   //
    109   // supports 1 ImageInfo descriptor
    110   //
    111   ImageDescriptor = SystemFmpPrivate->ImageDescriptor;
    112   ImageInfo->ImageIndex = ImageDescriptor->ImageIndex;
    113   CopyGuid (&ImageInfo->ImageTypeId, &ImageDescriptor->ImageTypeId);
    114   ImageInfo->ImageId = ImageDescriptor->ImageId;
    115   if (ImageDescriptor->ImageIdNameStringOffset != 0) {
    116     ImageInfo->ImageIdName = (CHAR16 *)((UINTN)ImageDescriptor + ImageDescriptor->ImageIdNameStringOffset);
    117   } else {
    118     ImageInfo->ImageIdName = NULL;
    119   }
    120   ImageInfo->Version = ImageDescriptor->Version;
    121   if (ImageDescriptor->VersionNameStringOffset != 0) {
    122     ImageInfo->VersionName = (CHAR16 *)((UINTN)ImageDescriptor + ImageDescriptor->VersionNameStringOffset);
    123   } else {
    124     ImageInfo->VersionName = NULL;
    125   }
    126   ImageInfo->Size = (UINTN)ImageDescriptor->Size;
    127   ImageInfo->AttributesSupported = ImageDescriptor->AttributesSupported;
    128   ImageInfo->AttributesSetting = ImageDescriptor->AttributesSetting;
    129   ImageInfo->Compatibilities = ImageDescriptor->Compatibilities;
    130   ImageInfo->LowestSupportedImageVersion = ImageDescriptor->LowestSupportedImageVersion;
    131   ImageInfo->LastAttemptVersion = SystemFmpPrivate->LastAttempt.LastAttemptVersion;
    132   ImageInfo->LastAttemptStatus = SystemFmpPrivate->LastAttempt.LastAttemptStatus;
    133   ImageInfo->HardwareInstance = ImageDescriptor->HardwareInstance;
    134 
    135   //
    136   // package version
    137   //
    138   *PackageVersion = ImageDescriptor->PackageVersion;
    139   if (ImageDescriptor->PackageVersionNameStringOffset != 0) {
    140     *PackageVersionName = (VOID *)((UINTN)ImageDescriptor + ImageDescriptor->PackageVersionNameStringOffset);
    141     *PackageVersionName = AllocateCopyPool(StrSize(*PackageVersionName), *PackageVersionName);
    142   } else {
    143     *PackageVersionName = NULL;
    144   }
    145 
    146   return EFI_SUCCESS;
    147 }
    148 
    149 /**
    150   Retrieves a copy of the current firmware image of the device.
    151 
    152   This function allows a copy of the current firmware image to be created and saved.
    153   The saved copy could later been used, for example, in firmware image recovery or rollback.
    154 
    155   @param[in]     This            A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
    156   @param[in]     ImageIndex      A unique number identifying the firmware image(s) within the device.
    157                                  The number is between 1 and DescriptorCount.
    158   @param[in,out] Image           Points to the buffer where the current image is copied to.
    159   @param[in,out] ImageSize       On entry, points to the size of the buffer pointed to by Image, in bytes.
    160                                  On return, points to the length of the image, in bytes.
    161 
    162   @retval EFI_SUCCESS            The device was successfully updated with the new image.
    163   @retval EFI_BUFFER_TOO_SMALL   The buffer specified by ImageSize is too small to hold the
    164                                  image. The current buffer size needed to hold the image is returned
    165                                  in ImageSize.
    166   @retval EFI_INVALID_PARAMETER  The Image was NULL.
    167   @retval EFI_NOT_FOUND          The current image is not copied to the buffer.
    168   @retval EFI_UNSUPPORTED        The operation is not supported.
    169   @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.
    170 
    171 **/
    172 EFI_STATUS
    173 EFIAPI
    174 FmpGetImage (
    175   IN  EFI_FIRMWARE_MANAGEMENT_PROTOCOL  *This,
    176   IN  UINT8                             ImageIndex,
    177   IN  OUT  VOID                         *Image,
    178   IN  OUT  UINTN                        *ImageSize
    179   )
    180 {
    181   return EFI_UNSUPPORTED;
    182 }
    183 
    184 /**
    185   Checks if the firmware image is valid for the device.
    186 
    187   This function allows firmware update application to validate the firmware image without
    188   invoking the SetImage() first.
    189 
    190   @param[in]  This               A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
    191   @param[in]  ImageIndex         A unique number identifying the firmware image(s) within the device.
    192                                  The number is between 1 and DescriptorCount.
    193   @param[in]  Image              Points to the new image.
    194   @param[in]  ImageSize          Size of the new image in bytes.
    195   @param[out] ImageUpdatable     Indicates if the new image is valid for update. It also provides,
    196                                  if available, additional information if the image is invalid.
    197 
    198   @retval EFI_SUCCESS            The image was successfully checked.
    199   @retval EFI_INVALID_PARAMETER  The Image was NULL.
    200   @retval EFI_UNSUPPORTED        The operation is not supported.
    201   @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.
    202 
    203 **/
    204 EFI_STATUS
    205 EFIAPI
    206 FmpCheckImage (
    207   IN  EFI_FIRMWARE_MANAGEMENT_PROTOCOL  *This,
    208   IN  UINT8                             ImageIndex,
    209   IN  CONST VOID                        *Image,
    210   IN  UINTN                             ImageSize,
    211   OUT UINT32                            *ImageUpdatable
    212   )
    213 {
    214   return EFI_UNSUPPORTED;
    215 }
    216 
    217 /**
    218   Returns information about the firmware package.
    219 
    220   This function returns package information.
    221 
    222   @param[in]  This                     A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
    223   @param[out] PackageVersion           A version number that represents all the firmware images in the device.
    224                                        The format is vendor specific and new version must have a greater value
    225                                        than the old version. If PackageVersion is not supported, the value is
    226                                        0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
    227                                        comparison is to be performed using PackageVersionName. A value of
    228                                        0xFFFFFFFD indicates that package version update is in progress.
    229   @param[out] PackageVersionName       A pointer to a pointer to a null-terminated string representing
    230                                        the package version name. The buffer is allocated by this function with
    231                                        AllocatePool(), and it is the caller's responsibility to free it with a
    232                                        call to FreePool().
    233   @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
    234                                        package version name. A value of 0 indicates the device does not support
    235                                        update of package version name. Length is the number of Unicode characters,
    236                                        including the terminating null character.
    237   @param[out] AttributesSupported      Package attributes that are supported by this device. See 'Package Attribute
    238                                        Definitions' for possible returned values of this parameter. A value of 1
    239                                        indicates the attribute is supported and the current setting value is
    240                                        indicated in AttributesSetting. A value of 0 indicates the attribute is not
    241                                        supported and the current setting value in AttributesSetting is meaningless.
    242   @param[out] AttributesSetting        Package attributes. See 'Package Attribute Definitions' for possible returned
    243                                        values of this parameter
    244 
    245   @retval EFI_SUCCESS                  The package information was successfully returned.
    246   @retval EFI_UNSUPPORTED              The operation is not supported.
    247 
    248 **/
    249 EFI_STATUS
    250 EFIAPI
    251 FmpGetPackageInfo (
    252   IN  EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
    253   OUT UINT32                           *PackageVersion,
    254   OUT CHAR16                           **PackageVersionName,
    255   OUT UINT32                           *PackageVersionNameMaxLen,
    256   OUT UINT64                           *AttributesSupported,
    257   OUT UINT64                           *AttributesSetting
    258   )
    259 {
    260   return EFI_UNSUPPORTED;
    261 }
    262 
    263 /**
    264   Updates information about the firmware package.
    265 
    266   This function updates package information.
    267   This function returns EFI_UNSUPPORTED if the package information is not updatable.
    268   VendorCode enables vendor to implement vendor-specific package information update policy.
    269   Null if the caller did not specify this policy or use the default policy.
    270 
    271   @param[in]  This               A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
    272   @param[in]  Image              Points to the authentication image.
    273                                  Null if authentication is not required.
    274   @param[in]  ImageSize          Size of the authentication image in bytes.
    275                                  0 if authentication is not required.
    276   @param[in]  VendorCode         This enables vendor to implement vendor-specific firmware
    277                                  image update policy.
    278                                  Null indicates the caller did not specify this policy or use
    279                                  the default policy.
    280   @param[in]  PackageVersion     The new package version.
    281   @param[in]  PackageVersionName A pointer to the new null-terminated Unicode string representing
    282                                  the package version name.
    283                                  The string length is equal to or less than the value returned in
    284                                  PackageVersionNameMaxLen.
    285 
    286   @retval EFI_SUCCESS            The device was successfully updated with the new package
    287                                  information.
    288   @retval EFI_INVALID_PARAMETER  The PackageVersionName length is longer than the value
    289                                  returned in PackageVersionNameMaxLen.
    290   @retval EFI_UNSUPPORTED        The operation is not supported.
    291   @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.
    292 
    293 **/
    294 EFI_STATUS
    295 EFIAPI
    296 FmpSetPackageInfo (
    297   IN  EFI_FIRMWARE_MANAGEMENT_PROTOCOL   *This,
    298   IN  CONST VOID                         *Image,
    299   IN  UINTN                              ImageSize,
    300   IN  CONST VOID                         *VendorCode,
    301   IN  UINT32                             PackageVersion,
    302   IN  CONST CHAR16                       *PackageVersionName
    303   )
    304 {
    305   return EFI_UNSUPPORTED;
    306 }
    307 
    308 /**
    309   Initialize SystemFmpDriver private data structure.
    310 
    311   @param[in] SystemFmpPrivate  private data structure to be initialized.
    312 
    313   @return EFI_SUCCESS private data is initialized.
    314 **/
    315 EFI_STATUS
    316 InitializePrivateData (
    317   IN SYSTEM_FMP_PRIVATE_DATA  *SystemFmpPrivate
    318   )
    319 {
    320   EFI_STATUS       VarStatus;
    321   UINTN            VarSize;
    322 
    323   SystemFmpPrivate->Signature       = SYSTEM_FMP_PRIVATE_DATA_SIGNATURE;
    324   SystemFmpPrivate->Handle          = NULL;
    325   SystemFmpPrivate->DescriptorCount = 1;
    326   CopyMem(&SystemFmpPrivate->Fmp, &mFirmwareManagementProtocol, sizeof(EFI_FIRMWARE_MANAGEMENT_PROTOCOL));
    327 
    328   SystemFmpPrivate->ImageDescriptor = PcdGetPtr(PcdEdkiiSystemFirmwareImageDescriptor);
    329 
    330   SystemFmpPrivate->LastAttempt.LastAttemptVersion = 0x0;
    331   SystemFmpPrivate->LastAttempt.LastAttemptStatus = 0x0;
    332   VarSize = sizeof(SystemFmpPrivate->LastAttempt);
    333   VarStatus = gRT->GetVariable(
    334                      SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME,
    335                      &gSystemFmpLastAttemptVariableGuid,
    336                      NULL,
    337                      &VarSize,
    338                      &SystemFmpPrivate->LastAttempt
    339                      );
    340   DEBUG((DEBUG_INFO, "GetLastAttemp - %r\n", VarStatus));
    341   DEBUG((DEBUG_INFO, "GetLastAttemp Version - 0x%x, State - 0x%x\n", SystemFmpPrivate->LastAttempt.LastAttemptVersion, SystemFmpPrivate->LastAttempt.LastAttemptStatus));
    342 
    343   return EFI_SUCCESS;
    344 }
    345 
    346 /**
    347   Return if this FMP is a system FMP or a device FMP, based upon FmpImageInfo.
    348 
    349   @param[in] FmpImageInfo A pointer to EFI_FIRMWARE_IMAGE_DESCRIPTOR
    350 
    351   @retval TRUE  It is a system FMP.
    352   @retval FALSE It is a device FMP.
    353 **/
    354 BOOLEAN
    355 IsSystemFmp (
    356   IN EFI_FIRMWARE_IMAGE_DESCRIPTOR   *FmpImageInfo
    357   )
    358 {
    359   GUID      *Guid;
    360   UINTN     Count;
    361   UINTN     Index;
    362 
    363   Guid = PcdGetPtr(PcdSystemFmpCapsuleImageTypeIdGuid);
    364   Count = PcdGetSize(PcdSystemFmpCapsuleImageTypeIdGuid) / sizeof(GUID);
    365 
    366   for (Index = 0; Index < Count; Index++, Guid++) {
    367     if (CompareGuid(&FmpImageInfo->ImageTypeId, Guid)) {
    368       return TRUE;
    369     }
    370   }
    371 
    372   return FALSE;
    373 }
    374