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