]> git.proxmox.com Git - mirror_edk2.git/blob - SignedCapsulePkg/Universal/SystemFirmwareUpdate/SystemFirmwareCommonDxe.c
SignedCapsulePkg/SystemFirmwareUpdate: Add SystemFirmwareUpdate.
[mirror_edk2.git] / SignedCapsulePkg / Universal / SystemFirmwareUpdate / SystemFirmwareCommonDxe.c
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 SYSTEM_FMP_PRIVATE_DATA *SystemFmpPrivate;
182
183 if (Image == NULL || ImageSize == NULL) {
184 return EFI_INVALID_PARAMETER;
185 }
186
187 SystemFmpPrivate = SYSTEM_FMP_PRIVATE_DATA_FROM_FMP(This);
188
189 if (ImageIndex == 0 || ImageIndex > SystemFmpPrivate->DescriptorCount || ImageSize == NULL || Image == NULL) {
190 return EFI_INVALID_PARAMETER;
191 }
192
193 return EFI_UNSUPPORTED;
194 }
195
196 /**
197 Checks if the firmware image is valid for the device.
198
199 This function allows firmware update application to validate the firmware image without
200 invoking the SetImage() first.
201
202 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
203 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
204 The number is between 1 and DescriptorCount.
205 @param[in] Image Points to the new image.
206 @param[in] ImageSize Size of the new image in bytes.
207 @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
208 if available, additional information if the image is invalid.
209
210 @retval EFI_SUCCESS The image was successfully checked.
211 @retval EFI_INVALID_PARAMETER The Image was NULL.
212 @retval EFI_UNSUPPORTED The operation is not supported.
213 @retval EFI_SECURITY_VIOLATIO The operation could not be performed due to an authentication failure.
214
215 **/
216 EFI_STATUS
217 EFIAPI
218 FmpCheckImage (
219 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
220 IN UINT8 ImageIndex,
221 IN CONST VOID *Image,
222 IN UINTN ImageSize,
223 OUT UINT32 *ImageUpdatable
224 )
225 {
226 return EFI_UNSUPPORTED;
227 }
228
229 /**
230 Returns information about the firmware package.
231
232 This function returns package information.
233
234 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
235 @param[out] PackageVersion A version number that represents all the firmware images in the device.
236 The format is vendor specific and new version must have a greater value
237 than the old version. If PackageVersion is not supported, the value is
238 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
239 comparison is to be performed using PackageVersionName. A value of
240 0xFFFFFFFD indicates that package version update is in progress.
241 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing
242 the package version name. The buffer is allocated by this function with
243 AllocatePool(), and it is the caller's responsibility to free it with a
244 call to FreePool().
245 @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
246 package version name. A value of 0 indicates the device does not support
247 update of package version name. Length is the number of Unicode characters,
248 including the terminating null character.
249 @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute
250 Definitions' for possible returned values of this parameter. A value of 1
251 indicates the attribute is supported and the current setting value is
252 indicated in AttributesSetting. A value of 0 indicates the attribute is not
253 supported and the current setting value in AttributesSetting is meaningless.
254 @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned
255 values of this parameter
256
257 @retval EFI_SUCCESS The package information was successfully returned.
258 @retval EFI_UNSUPPORTED The operation is not supported.
259
260 **/
261 EFI_STATUS
262 EFIAPI
263 FmpGetPackageInfo (
264 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
265 OUT UINT32 *PackageVersion,
266 OUT CHAR16 **PackageVersionName,
267 OUT UINT32 *PackageVersionNameMaxLen,
268 OUT UINT64 *AttributesSupported,
269 OUT UINT64 *AttributesSetting
270 )
271 {
272 return EFI_UNSUPPORTED;
273 }
274
275 /**
276 Updates information about the firmware package.
277
278 This function updates package information.
279 This function returns EFI_UNSUPPORTED if the package information is not updatable.
280 VendorCode enables vendor to implement vendor-specific package information update policy.
281 Null if the caller did not specify this policy or use the default policy.
282
283 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
284 @param[in] Image Points to the authentication image.
285 Null if authentication is not required.
286 @param[in] ImageSize Size of the authentication image in bytes.
287 0 if authentication is not required.
288 @param[in] VendorCode This enables vendor to implement vendor-specific firmware
289 image update policy.
290 Null indicates the caller did not specify this policy or use
291 the default policy.
292 @param[in] PackageVersion The new package version.
293 @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing
294 the package version name.
295 The string length is equal to or less than the value returned in
296 PackageVersionNameMaxLen.
297
298 @retval EFI_SUCCESS The device was successfully updated with the new package
299 information.
300 @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value
301 returned in PackageVersionNameMaxLen.
302 @retval EFI_UNSUPPORTED The operation is not supported.
303 @retval EFI_SECURITY_VIOLATIO The operation could not be performed due to an authentication failure.
304
305 **/
306 EFI_STATUS
307 EFIAPI
308 FmpSetPackageInfo (
309 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
310 IN CONST VOID *Image,
311 IN UINTN ImageSize,
312 IN CONST VOID *VendorCode,
313 IN UINT32 PackageVersion,
314 IN CONST CHAR16 *PackageVersionName
315 )
316 {
317 return EFI_UNSUPPORTED;
318 }
319
320 /**
321 Initialize SystemFmpDriver private data structure.
322
323 @param[in] SystemFmpPrivate private data structure to be initialized.
324
325 @return EFI_SUCCESS private data is initialized.
326 **/
327 EFI_STATUS
328 InitializePrivateData (
329 IN SYSTEM_FMP_PRIVATE_DATA *SystemFmpPrivate
330 )
331 {
332 EFI_STATUS VarStatus;
333 UINTN VarSize;
334
335 SystemFmpPrivate->Signature = SYSTEM_FMP_PRIVATE_DATA_SIGNATURE;
336 SystemFmpPrivate->Handle = NULL;
337 SystemFmpPrivate->DescriptorCount = 1;
338 CopyMem(&SystemFmpPrivate->Fmp, &mFirmwareManagementProtocol, sizeof(EFI_FIRMWARE_MANAGEMENT_PROTOCOL));
339
340 SystemFmpPrivate->ImageDescriptor = PcdGetPtr(PcdEdkiiSystemFirmwareImageDescriptor);
341
342 SystemFmpPrivate->LastAttempt.LastAttemptVersion = 0x0;
343 SystemFmpPrivate->LastAttempt.LastAttemptStatus = 0x0;
344 VarSize = sizeof(SystemFmpPrivate->LastAttempt);
345 VarStatus = gRT->GetVariable(
346 SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME,
347 &gSystemFmpLastAttemptVariableGuid,
348 NULL,
349 &VarSize,
350 &SystemFmpPrivate->LastAttempt
351 );
352 DEBUG((DEBUG_INFO, "GetLastAttemp - %r\n", VarStatus));
353 DEBUG((DEBUG_INFO, "GetLastAttemp Version - 0x%x, State - 0x%x\n", SystemFmpPrivate->LastAttempt.LastAttemptVersion, SystemFmpPrivate->LastAttempt.LastAttemptStatus));
354
355 return EFI_SUCCESS;
356 }
357
358 /**
359 Return if this FMP is a system FMP or a device FMP, based upon FmpImageInfo.
360
361 @param[in] FmpImageInfo A pointer to EFI_FIRMWARE_IMAGE_DESCRIPTOR
362
363 @retval TRUE It is a system FMP.
364 @retval FALSE It is a device FMP.
365 **/
366 BOOLEAN
367 IsSystemFmp (
368 IN EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfo
369 )
370 {
371 GUID *Guid;
372 UINTN Count;
373 UINTN Index;
374
375 Guid = PcdGetPtr(PcdSystemFmpCapsuleImageTypeIdGuid);
376 Count = PcdGetSize(PcdSystemFmpCapsuleImageTypeIdGuid) / sizeof(GUID);
377
378 for (Index = 0; Index < Count; Index++, Guid++) {
379 if (CompareGuid(&FmpImageInfo->ImageTypeId, Guid)) {
380 return TRUE;
381 }
382 }
383
384 return FALSE;
385 }