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