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