1 /** @file
2   System Firmware update header file.
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 #ifndef _SYSTEM_FIRMWARE_UPDATE_H_
16 #define _SYSTEM_FIRMWARE_UPDATE_H_
17 
18 #include <PiDxe.h>
19 
20 #include <Guid/SystemResourceTable.h>
21 #include <Guid/FirmwareContentsSigned.h>
22 #include <Guid/WinCertificate.h>
23 #include <Guid/EdkiiSystemFmpCapsule.h>
24 
25 #include <Protocol/FirmwareManagement.h>
26 #include <Protocol/FirmwareVolumeBlock.h>
27 
28 #include <Library/BaseLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/DebugLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/PcdLib.h>
33 #include <Library/UefiBootServicesTableLib.h>
34 #include <Library/UefiLib.h>
35 #include <Library/UefiRuntimeServicesTableLib.h>
36 #include <Library/UefiDriverEntryPoint.h>
37 #include <Library/DevicePathLib.h>
38 #include <Library/HobLib.h>
39 #include <Library/DxeServicesLib.h>
40 #include <Library/DxeServicesTableLib.h>
41 #include <Library/PlatformFlashAccessLib.h>
42 #include <Library/EdkiiSystemCapsuleLib.h>
43 
44 typedef struct {
45   UINT32 LastAttemptVersion;
46   UINT32 LastAttemptStatus;
47 } SYSTEM_FMP_LAST_ATTEMPT_VARIABLE;
48 
49 #define SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME  L"SystemLastAttempVar"
50 
51 #define SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_GUID {0x2f564d6f, 0xcc2c, 0x4838, { 0xb9, 0xa8, 0xbe, 0x59, 0x48, 0xb0, 0x3d, 0x59 }}
52 
53 #define SYSTEM_FMP_PRIVATE_DATA_SIGNATURE  SIGNATURE_32('S', 'Y', 'S', 'F')
54 
55 #define SYSTEM_FMP_PROTOCOL_GUID {0x6d16624a, 0x26a6, 0x4cb4, { 0x84, 0xfa, 0x6, 0x78, 0x5a, 0x7e, 0x82, 0x6a }}
56 
57 //
58 // SYSTEM FMP private data structure.
59 //
60 
61 struct _SYSTEM_FMP_PRIVATE_DATA {
62   UINT32                                          Signature;
63   EFI_FIRMWARE_MANAGEMENT_PROTOCOL                Fmp;
64   EFI_HANDLE                                      Handle;
65   UINT8                                           DescriptorCount;
66   EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR          *ImageDescriptor;
67   SYSTEM_FMP_LAST_ATTEMPT_VARIABLE                LastAttempt;
68 };
69 
70 typedef struct _SYSTEM_FMP_PRIVATE_DATA  SYSTEM_FMP_PRIVATE_DATA;
71 
72 /**
73   Returns a pointer to the SYSTEM_FMP_PRIVATE_DATA structure from the input a as Fmp.
74 
75   If the signatures matches, then a pointer to the data structure that contains
76   a specified field of that data structure is returned.
77 
78   @param  a              Pointer to the field specified by ServiceBinding within
79                          a data structure of type SYSTEM_FMP_PRIVATE_DATA.
80 
81 **/
82 #define SYSTEM_FMP_PRIVATE_DATA_FROM_FMP(a) \
83   CR ( \
84   (a), \
85   SYSTEM_FMP_PRIVATE_DATA, \
86   Fmp, \
87   SYSTEM_FMP_PRIVATE_DATA_SIGNATURE \
88   )
89 
90 
91 //
92 // Update data
93 //
94 
95 typedef struct {
96   UINTN                           NumOfUpdates;
97 } CONFIG_HEADER;
98 
99 typedef struct {
100   UINTN                           Index;
101   PLATFORM_FIRMWARE_TYPE          FirmwareType;
102   FLASH_ADDRESS_TYPE              AddressType;
103   EFI_GUID                        FileGuid;
104   EFI_PHYSICAL_ADDRESS            BaseAddress;
105   UINTN                           Length;
106   UINTN                           ImageOffset;
107 } UPDATE_CONFIG_DATA;
108 
109 //
110 // System Firmware Update SMM Communication
111 //
112 
113 #define SYSTEM_FIRMWARE_UPDATE_COMMUNICATION_FUNCTION_SET_IMAGE 1
114 
115 typedef struct {
116   UINTN       Function;
117   EFI_STATUS  ReturnStatus;
118 //UINT8       Data[];
119 } SYSTEM_FIRMWARE_UPDATE_COMMUNICATION_HEAD;
120 
121 #define  ABORT_REASON_MAX_SIZE              0x40  // UnicodeStringSize including final L'\0'
122 
123 #define  CAPSULE_IMAGE_ADDITIONAL_MAX_SIZE  (0x20020 + 0xA0000) // Addtional size for Capsule Header, FV block alignment + DispatchImage.
124 
125 typedef struct {
126   UINT8       ImageIndex;
127   UINTN       ImageSize;
128   UINTN       AbortReasonSize;
129   UINT32      LastAttemptVersion;
130   UINT32      LastAttemptStatus;
131 //UINT8       Data[AbortReasonMaxSize + ImageSize];
132 } SYSTEM_FIRMWARE_UPDATE_COMMUNICATION_SET_IMAGE;
133 
134 
135 /**
136   Returns information about the current firmware image(s) of the device.
137 
138   This function allows a copy of the current firmware image to be created and saved.
139   The saved copy could later been used, for example, in firmware image recovery or rollback.
140 
141   @param[in]      This               A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
142   @param[in, out] ImageInfoSize      A pointer to the size, in bytes, of the ImageInfo buffer.
143                                      On input, this is the size of the buffer allocated by the caller.
144                                      On output, it is the size of the buffer returned by the firmware
145                                      if the buffer was large enough, or the size of the buffer needed
146                                      to contain the image(s) information if the buffer was too small.
147   @param[in, out] ImageInfo          A pointer to the buffer in which firmware places the current image(s)
148                                      information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
149   @param[out]     DescriptorVersion  A pointer to the location in which firmware returns the version number
150                                      associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
151   @param[out]     DescriptorCount    A pointer to the location in which firmware returns the number of
152                                      descriptors or firmware images within this device.
153   @param[out]     DescriptorSize     A pointer to the location in which firmware returns the size, in bytes,
154                                      of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.
155   @param[out]     PackageVersion     A version number that represents all the firmware images in the device.
156                                      The format is vendor specific and new version must have a greater value
157                                      than the old version. If PackageVersion is not supported, the value is
158                                      0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison
159                                      is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates
160                                      that package version update is in progress.
161   @param[out]     PackageVersionName A pointer to a pointer to a null-terminated string representing the
162                                      package version name. The buffer is allocated by this function with
163                                      AllocatePool(), and it is the caller's responsibility to free it with a call
164                                      to FreePool().
165 
166   @retval EFI_SUCCESS                The device was successfully updated with the new image.
167   @retval EFI_BUFFER_TOO_SMALL       The ImageInfo buffer was too small. The current buffer size
168                                      needed to hold the image(s) information is returned in ImageInfoSize.
169   @retval EFI_INVALID_PARAMETER      ImageInfoSize is NULL.
170   @retval EFI_DEVICE_ERROR           Valid information could not be returned. Possible corrupted image.
171 
172 **/
173 EFI_STATUS
174 EFIAPI
175 FmpGetImageInfo (
176   IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL       *This,
177   IN OUT    UINTN                           *ImageInfoSize,
178   IN OUT    EFI_FIRMWARE_IMAGE_DESCRIPTOR   *ImageInfo,
179   OUT       UINT32                          *DescriptorVersion,
180   OUT       UINT8                           *DescriptorCount,
181   OUT       UINTN                           *DescriptorSize,
182   OUT       UINT32                          *PackageVersion,
183   OUT       CHAR16                          **PackageVersionName
184   );
185 
186 /**
187   Retrieves a copy of the current firmware image of the device.
188 
189   This function allows a copy of the current firmware image to be created and saved.
190   The saved copy could later been used, for example, in firmware image recovery or rollback.
191 
192   @param[in]     This            A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
193   @param[in]     ImageIndex      A unique number identifying the firmware image(s) within the device.
194                                  The number is between 1 and DescriptorCount.
195   @param[in,out] Image           Points to the buffer where the current image is copied to.
196   @param[in,out] ImageSize       On entry, points to the size of the buffer pointed to by Image, in bytes.
197                                  On return, points to the length of the image, in bytes.
198 
199   @retval EFI_SUCCESS            The device was successfully updated with the new image.
200   @retval EFI_BUFFER_TOO_SMALL   The buffer specified by ImageSize is too small to hold the
201                                  image. The current buffer size needed to hold the image is returned
202                                  in ImageSize.
203   @retval EFI_INVALID_PARAMETER  The Image was NULL.
204   @retval EFI_NOT_FOUND          The current image is not copied to the buffer.
205   @retval EFI_UNSUPPORTED        The operation is not supported.
206   @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.
207 
208 **/
209 EFI_STATUS
210 EFIAPI
211 FmpGetImage (
212   IN  EFI_FIRMWARE_MANAGEMENT_PROTOCOL  *This,
213   IN  UINT8                             ImageIndex,
214   IN  OUT  VOID                         *Image,
215   IN  OUT  UINTN                        *ImageSize
216   );
217 
218 /**
219   Updates the firmware image of the device.
220 
221   This function updates the hardware with the new firmware image.
222   This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
223   If the firmware image is updatable, the function should perform the following minimal validations
224   before proceeding to do the firmware image update.
225   - Validate the image authentication if image has attribute
226     IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
227     EFI_SECURITY_VIOLATION if the validation fails.
228   - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
229     the image is unsupported. The function can optionally provide more detailed information on
230     why the image is not a supported image.
231   - Validate the data from VendorCode if not null. Image validation must be performed before
232     VendorCode data validation. VendorCode data is ignored or considered invalid if image
233     validation failed. The function returns EFI_ABORTED if the data is invalid.
234 
235   VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
236   the caller did not specify the policy or use the default policy. As an example, vendor can implement
237   a policy to allow an option to force a firmware image update when the abort reason is due to the new
238   firmware image version is older than the current firmware image version or bad image checksum.
239   Sensitive operations such as those wiping the entire firmware image and render the device to be
240   non-functional should be encoded in the image itself rather than passed with the VendorCode.
241   AbortReason enables vendor to have the option to provide a more detailed description of the abort
242   reason to the caller.
243 
244   @param[in]  This               A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
245   @param[in]  ImageIndex         A unique number identifying the firmware image(s) within the device.
246                                  The number is between 1 and DescriptorCount.
247   @param[in]  Image              Points to the new image.
248   @param[in]  ImageSize          Size of the new image in bytes.
249   @param[in]  VendorCode         This enables vendor to implement vendor-specific firmware image update policy.
250                                  Null indicates the caller did not specify the policy or use the default policy.
251   @param[in]  Progress           A function used by the driver to report the progress of the firmware update.
252   @param[out] AbortReason        A pointer to a pointer to a null-terminated string providing more
253                                  details for the aborted operation. The buffer is allocated by this function
254                                  with AllocatePool(), and it is the caller's responsibility to free it with a
255                                  call to FreePool().
256 
257   @retval EFI_SUCCESS            The device was successfully updated with the new image.
258   @retval EFI_ABORTED            The operation is aborted.
259   @retval EFI_INVALID_PARAMETER  The Image was NULL.
260   @retval EFI_UNSUPPORTED        The operation is not supported.
261   @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.
262 
263 **/
264 EFI_STATUS
265 EFIAPI
266 FmpSetImage (
267   IN  EFI_FIRMWARE_MANAGEMENT_PROTOCOL                 *This,
268   IN  UINT8                                            ImageIndex,
269   IN  CONST VOID                                       *Image,
270   IN  UINTN                                            ImageSize,
271   IN  CONST VOID                                       *VendorCode,
272   IN  EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS    Progress,
273   OUT CHAR16                                           **AbortReason
274   );
275 
276 /**
277   Checks if the firmware image is valid for the device.
278 
279   This function allows firmware update application to validate the firmware image without
280   invoking the SetImage() first.
281 
282   @param[in]  This               A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
283   @param[in]  ImageIndex         A unique number identifying the firmware image(s) within the device.
284                                  The number is between 1 and DescriptorCount.
285   @param[in]  Image              Points to the new image.
286   @param[in]  ImageSize          Size of the new image in bytes.
287   @param[out] ImageUpdatable     Indicates if the new image is valid for update. It also provides,
288                                  if available, additional information if the image is invalid.
289 
290   @retval EFI_SUCCESS            The image was successfully checked.
291   @retval EFI_INVALID_PARAMETER  The Image was NULL.
292   @retval EFI_UNSUPPORTED        The operation is not supported.
293   @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.
294 
295 **/
296 EFI_STATUS
297 EFIAPI
298 FmpCheckImage (
299   IN  EFI_FIRMWARE_MANAGEMENT_PROTOCOL  *This,
300   IN  UINT8                             ImageIndex,
301   IN  CONST VOID                        *Image,
302   IN  UINTN                             ImageSize,
303   OUT UINT32                            *ImageUpdatable
304   );
305 
306 /**
307   Returns information about the firmware package.
308 
309   This function returns package information.
310 
311   @param[in]  This                     A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
312   @param[out] PackageVersion           A version number that represents all the firmware images in the device.
313                                        The format is vendor specific and new version must have a greater value
314                                        than the old version. If PackageVersion is not supported, the value is
315                                        0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
316                                        comparison is to be performed using PackageVersionName. A value of
317                                        0xFFFFFFFD indicates that package version update is in progress.
318   @param[out] PackageVersionName       A pointer to a pointer to a null-terminated string representing
319                                        the package version name. The buffer is allocated by this function with
320                                        AllocatePool(), and it is the caller's responsibility to free it with a
321                                        call to FreePool().
322   @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
323                                        package version name. A value of 0 indicates the device does not support
324                                        update of package version name. Length is the number of Unicode characters,
325                                        including the terminating null character.
326   @param[out] AttributesSupported      Package attributes that are supported by this device. See 'Package Attribute
327                                        Definitions' for possible returned values of this parameter. A value of 1
328                                        indicates the attribute is supported and the current setting value is
329                                        indicated in AttributesSetting. A value of 0 indicates the attribute is not
330                                        supported and the current setting value in AttributesSetting is meaningless.
331   @param[out] AttributesSetting        Package attributes. See 'Package Attribute Definitions' for possible returned
332                                        values of this parameter
333 
334   @retval EFI_SUCCESS                  The package information was successfully returned.
335   @retval EFI_UNSUPPORTED              The operation is not supported.
336 
337 **/
338 EFI_STATUS
339 EFIAPI
340 FmpGetPackageInfo (
341   IN  EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
342   OUT UINT32                           *PackageVersion,
343   OUT CHAR16                           **PackageVersionName,
344   OUT UINT32                           *PackageVersionNameMaxLen,
345   OUT UINT64                           *AttributesSupported,
346   OUT UINT64                           *AttributesSetting
347   );
348 
349 /**
350   Updates information about the firmware package.
351 
352   This function updates package information.
353   This function returns EFI_UNSUPPORTED if the package information is not updatable.
354   VendorCode enables vendor to implement vendor-specific package information update policy.
355   Null if the caller did not specify this policy or use the default policy.
356 
357   @param[in]  This               A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
358   @param[in]  Image              Points to the authentication image.
359                                  Null if authentication is not required.
360   @param[in]  ImageSize          Size of the authentication image in bytes.
361                                  0 if authentication is not required.
362   @param[in]  VendorCode         This enables vendor to implement vendor-specific firmware
363                                  image update policy.
364                                  Null indicates the caller did not specify this policy or use
365                                  the default policy.
366   @param[in]  PackageVersion     The new package version.
367   @param[in]  PackageVersionName A pointer to the new null-terminated Unicode string representing
368                                  the package version name.
369                                  The string length is equal to or less than the value returned in
370                                  PackageVersionNameMaxLen.
371 
372   @retval EFI_SUCCESS            The device was successfully updated with the new package
373                                  information.
374   @retval EFI_INVALID_PARAMETER  The PackageVersionName length is longer than the value
375                                  returned in PackageVersionNameMaxLen.
376   @retval EFI_UNSUPPORTED        The operation is not supported.
377   @retval EFI_SECURITY_VIOLATIO  The operation could not be performed due to an authentication failure.
378 
379 **/
380 EFI_STATUS
381 EFIAPI
382 FmpSetPackageInfo (
383   IN  EFI_FIRMWARE_MANAGEMENT_PROTOCOL   *This,
384   IN  CONST VOID                         *Image,
385   IN  UINTN                              ImageSize,
386   IN  CONST VOID                         *VendorCode,
387   IN  UINT32                             PackageVersion,
388   IN  CONST CHAR16                       *PackageVersionName
389   );
390 
391 /**
392   Initialize SystemFmpDriver private data structure.
393 
394   @param[in] SystemFmpPrivate  private data structure to be initialized.
395 
396   @return EFI_SUCCESS private data is initialized.
397 **/
398 EFI_STATUS
399 InitializePrivateData (
400   IN SYSTEM_FMP_PRIVATE_DATA  *SystemFmpPrivate
401   );
402 
403 extern EFI_GUID gSystemFmpLastAttemptVariableGuid;
404 extern EFI_GUID mCurrentImageTypeId;
405 extern EFI_GUID gSystemFmpProtocolGuid;
406 
407 #endif
408 
409