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