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