2 SetImage instance to update Microcode.
4 Caution: This module requires additional review when modified.
5 This module will have external input - capsule image.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
9 MicrocodeWrite() and VerifyMicrocode() will receive untrusted input and do basic validation.
11 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
12 This program and the accompanying materials
13 are licensed and made available under the terms and conditions of the BSD License
14 which accompanies this distribution. The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include "MicrocodeUpdate.h"
27 @param[out] MicrocodePatchAddress The address of Microcode
28 @param[out] MicrocodePatchRegionSize The region size of Microcode
30 @retval TRUE The Microcode region is returned.
31 @retval FALSE No Microcode region.
35 OUT VOID
**MicrocodePatchAddress
,
36 OUT UINTN
*MicrocodePatchRegionSize
39 *MicrocodePatchAddress
= (VOID
*)(UINTN
)PcdGet64(PcdCpuMicrocodePatchAddress
);
40 *MicrocodePatchRegionSize
= (UINTN
)PcdGet64(PcdCpuMicrocodePatchRegionSize
);
42 if ((*MicrocodePatchAddress
== NULL
) || (*MicrocodePatchRegionSize
== 0)) {
50 Get Microcode update signature of currently loaded Microcode update.
52 @return Microcode signature.
56 GetCurrentMicrocodeSignature (
62 AsmWriteMsr64(MSR_IA32_BIOS_SIGN_ID
, 0);
63 AsmCpuid(CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, NULL
);
64 Signature
= AsmReadMsr64(MSR_IA32_BIOS_SIGN_ID
);
65 return (UINT32
)RShiftU64(Signature
, 32);
69 Get current processor signature.
71 @return current processor signature.
74 GetCurrentProcessorSignature (
79 AsmCpuid(CPUID_VERSION_INFO
, &RegEax
, NULL
, NULL
, NULL
);
84 Get current platform ID.
86 @return current platform ID.
89 GetCurrentPlatformId (
95 PlatformId
= (UINT8
)AsmMsrBitFieldRead64(MSR_IA32_PLATFORM_ID
, 50, 52);
102 @param[in] Address The address of new Microcode.
104 @return Loaded Microcode signature.
112 AsmWriteMsr64(MSR_IA32_BIOS_UPDT_TRIG
, Address
);
113 return GetCurrentMicrocodeSignature();
117 Load Microcode on an Application Processor.
118 The function prototype for invoking a function on an Application Processor.
120 @param[in,out] Buffer The pointer to private data buffer.
128 MICROCODE_LOAD_BUFFER
*MicrocodeLoadBuffer
;
130 MicrocodeLoadBuffer
= Buffer
;
131 MicrocodeLoadBuffer
->Revision
= LoadMicrocode (MicrocodeLoadBuffer
->Address
);
135 Load new Microcode on this processor
137 @param[in] MicrocodeFmpPrivate The Microcode driver private data
138 @param[in] CpuIndex The index of the processor.
139 @param[in] Address The address of new Microcode.
141 @return Loaded Microcode signature.
145 LoadMicrocodeOnThis (
146 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
152 EFI_MP_SERVICES_PROTOCOL
*MpService
;
153 MICROCODE_LOAD_BUFFER MicrocodeLoadBuffer
;
155 if (CpuIndex
== MicrocodeFmpPrivate
->BspIndex
) {
156 return LoadMicrocode (Address
);
158 MpService
= MicrocodeFmpPrivate
->MpService
;
159 MicrocodeLoadBuffer
.Address
= Address
;
160 MicrocodeLoadBuffer
.Revision
= 0;
161 Status
= MpService
->StartupThisAP (
167 &MicrocodeLoadBuffer
,
170 ASSERT_EFI_ERROR(Status
);
171 return MicrocodeLoadBuffer
.Revision
;
176 Collect processor information.
177 The function prototype for invoking a function on an Application Processor.
179 @param[in,out] Buffer The pointer to private data buffer.
183 CollectProcessorInfo (
187 PROCESSOR_INFO
*ProcessorInfo
;
189 ProcessorInfo
= Buffer
;
190 ProcessorInfo
->ProcessorSignature
= GetCurrentProcessorSignature();
191 ProcessorInfo
->PlatformId
= GetCurrentPlatformId();
192 ProcessorInfo
->MicrocodeRevision
= GetCurrentMicrocodeSignature();
196 Get current Microcode information.
198 The ProcessorInformation (BspIndex/ProcessorCount/ProcessorInfo)
199 in MicrocodeFmpPrivate must be initialized.
201 The MicrocodeInformation (DescriptorCount/ImageDescriptor/MicrocodeInfo)
202 in MicrocodeFmpPrivate may not be avaiable in this function.
204 @param[in] MicrocodeFmpPrivate The Microcode driver private data
205 @param[in] DescriptorCount The count of Microcode ImageDescriptor allocated.
206 @param[out] ImageDescriptor Microcode ImageDescriptor
207 @param[out] MicrocodeInfo Microcode information
209 @return Microcode count
213 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
214 IN UINTN DescriptorCount
, OPTIONAL
215 OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR
*ImageDescriptor
, OPTIONAL
216 OUT MICROCODE_INFO
*MicrocodeInfo OPTIONAL
219 VOID
*MicrocodePatchAddress
;
220 UINTN MicrocodePatchRegionSize
;
221 CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
;
225 UINT64 ImageAttributes
;
228 UINT32 AttemptStatus
;
229 UINTN TargetCpuIndex
;
231 MicrocodePatchAddress
= MicrocodeFmpPrivate
->MicrocodePatchAddress
;
232 MicrocodePatchRegionSize
= MicrocodeFmpPrivate
->MicrocodePatchRegionSize
;
234 DEBUG((DEBUG_INFO
, "Microcode Region - 0x%x - 0x%x\n", MicrocodePatchAddress
, MicrocodePatchRegionSize
));
238 MicrocodeEnd
= (UINTN
)MicrocodePatchAddress
+ MicrocodePatchRegionSize
;
239 MicrocodeEntryPoint
= (CPU_MICROCODE_HEADER
*) (UINTN
) MicrocodePatchAddress
;
241 if (MicrocodeEntryPoint
->HeaderVersion
== 0x1 && MicrocodeEntryPoint
->LoaderRevision
== 0x1) {
243 // It is the microcode header. It is not the padding data between microcode patches
244 // becasue the padding data should not include 0x00000001 and it should be the repeated
245 // byte format (like 0xXYXYXYXY....).
247 if (MicrocodeEntryPoint
->DataSize
== 0) {
250 TotalSize
= MicrocodeEntryPoint
->TotalSize
;
253 TargetCpuIndex
= (UINTN
)-1;
254 Status
= VerifyMicrocode(MicrocodeFmpPrivate
, MicrocodeEntryPoint
, TotalSize
, FALSE
, &AttemptStatus
, NULL
, &TargetCpuIndex
);
255 if (!EFI_ERROR(Status
)) {
257 ASSERT (TargetCpuIndex
< MicrocodeFmpPrivate
->ProcessorCount
);
258 MicrocodeFmpPrivate
->ProcessorInfo
[TargetCpuIndex
].MicrocodeIndex
= Count
;
263 if (ImageDescriptor
!= NULL
&& DescriptorCount
> Count
) {
264 ImageDescriptor
[Count
].ImageIndex
= (UINT8
)(Count
+ 1);
265 CopyGuid (&ImageDescriptor
[Count
].ImageTypeId
, &gMicrocodeFmpImageTypeIdGuid
);
266 ImageDescriptor
[Count
].ImageId
= LShiftU64(MicrocodeEntryPoint
->ProcessorFlags
, 32) + MicrocodeEntryPoint
->ProcessorSignature
.Uint32
;
267 ImageDescriptor
[Count
].ImageIdName
= NULL
;
268 ImageDescriptor
[Count
].Version
= MicrocodeEntryPoint
->UpdateRevision
;
269 ImageDescriptor
[Count
].VersionName
= NULL
;
270 ImageDescriptor
[Count
].Size
= TotalSize
;
271 ImageAttributes
= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
| IMAGE_ATTRIBUTE_RESET_REQUIRED
;
273 ImageAttributes
|= IMAGE_ATTRIBUTE_IN_USE
;
275 ImageDescriptor
[Count
].AttributesSupported
= ImageAttributes
| IMAGE_ATTRIBUTE_IN_USE
;
276 ImageDescriptor
[Count
].AttributesSetting
= ImageAttributes
;
277 ImageDescriptor
[Count
].Compatibilities
= 0;
278 ImageDescriptor
[Count
].LowestSupportedImageVersion
= MicrocodeEntryPoint
->UpdateRevision
; // do not support rollback
279 ImageDescriptor
[Count
].LastAttemptVersion
= 0;
280 ImageDescriptor
[Count
].LastAttemptStatus
= 0;
281 ImageDescriptor
[Count
].HardwareInstance
= 0;
283 if (MicrocodeInfo
!= NULL
&& DescriptorCount
> Count
) {
284 MicrocodeInfo
[Count
].MicrocodeEntryPoint
= MicrocodeEntryPoint
;
285 MicrocodeInfo
[Count
].TotalSize
= TotalSize
;
286 MicrocodeInfo
[Count
].InUse
= IsInUse
;
290 // It is the padding data between the microcode patches for microcode patches alignment.
291 // Because the microcode patch is the multiple of 1-KByte, the padding data should not
292 // exist if the microcode patch alignment value is not larger than 1-KByte. So, the microcode
293 // alignment value should be larger than 1-KByte. We could skip SIZE_1KB padding data to
294 // find the next possible microcode patch header.
296 MicrocodeEntryPoint
= (CPU_MICROCODE_HEADER
*) (((UINTN
) MicrocodeEntryPoint
) + SIZE_1KB
);
301 ASSERT(Count
< 0xFF);
304 // Get the next patch.
306 MicrocodeEntryPoint
= (CPU_MICROCODE_HEADER
*) (((UINTN
) MicrocodeEntryPoint
) + TotalSize
);
307 } while (((UINTN
) MicrocodeEntryPoint
< MicrocodeEnd
));
313 Return matched processor information.
315 @param[in] MicrocodeFmpPrivate The Microcode driver private data
316 @param[in] ProcessorSignature The processor signature to be matched
317 @param[in] ProcessorFlags The processor flags to be matched
318 @param[in, out] TargetCpuIndex On input, the index of target CPU which tries to match the Microcode. (UINTN)-1 means to try all.
319 On output, the index of target CPU which matches the Microcode.
321 @return matched processor information.
324 GetMatchedProcessor (
325 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
326 IN UINT32 ProcessorSignature
,
327 IN UINT32 ProcessorFlags
,
328 IN OUT UINTN
*TargetCpuIndex
333 if (*TargetCpuIndex
!= (UINTN
)-1) {
334 Index
= *TargetCpuIndex
;
335 if ((ProcessorSignature
== MicrocodeFmpPrivate
->ProcessorInfo
[Index
].ProcessorSignature
) &&
336 ((ProcessorFlags
& (1 << MicrocodeFmpPrivate
->ProcessorInfo
[Index
].PlatformId
)) != 0)) {
337 return &MicrocodeFmpPrivate
->ProcessorInfo
[Index
];
343 for (Index
= 0; Index
< MicrocodeFmpPrivate
->ProcessorCount
; Index
++) {
344 if ((ProcessorSignature
== MicrocodeFmpPrivate
->ProcessorInfo
[Index
].ProcessorSignature
) &&
345 ((ProcessorFlags
& (1 << MicrocodeFmpPrivate
->ProcessorInfo
[Index
].PlatformId
)) != 0)) {
346 *TargetCpuIndex
= Index
;
347 return &MicrocodeFmpPrivate
->ProcessorInfo
[Index
];
356 Caution: This function may receive untrusted input.
358 @param[in] MicrocodeFmpPrivate The Microcode driver private data
359 @param[in] Image The Microcode image buffer.
360 @param[in] ImageSize The size of Microcode image buffer in bytes.
361 @param[in] TryLoad Try to load Microcode or not.
362 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
363 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
364 details for the aborted operation. The buffer is allocated by this function
365 with AllocatePool(), and it is the caller's responsibility to free it with a
367 @param[in, out] TargetCpuIndex On input, the index of target CPU which tries to match the Microcode. (UINTN)-1 means to try all.
368 On output, the index of target CPU which matches the Microcode.
370 @retval EFI_SUCCESS The Microcode image passes verification.
371 @retval EFI_VOLUME_CORRUPTED The Microcode image is corrupted.
372 @retval EFI_INCOMPATIBLE_VERSION The Microcode image version is incorrect.
373 @retval EFI_UNSUPPORTED The Microcode ProcessorSignature or ProcessorFlags is incorrect.
374 @retval EFI_SECURITY_VIOLATION The Microcode image fails to load.
378 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
382 OUT UINT32
*LastAttemptStatus
,
383 OUT CHAR16
**AbortReason
, OPTIONAL
384 IN OUT UINTN
*TargetCpuIndex
388 CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
;
391 UINT32 CurrentRevision
;
392 PROCESSOR_INFO
*ProcessorInfo
;
393 UINT32 InCompleteCheckSum32
;
395 UINTN ExtendedTableLength
;
396 UINT32 ExtendedTableCount
;
397 CPU_MICROCODE_EXTENDED_TABLE
*ExtendedTable
;
398 CPU_MICROCODE_EXTENDED_TABLE_HEADER
*ExtendedTableHeader
;
399 BOOLEAN CorrectMicrocode
;
402 // Check HeaderVersion
404 MicrocodeEntryPoint
= Image
;
405 if (MicrocodeEntryPoint
->HeaderVersion
!= 0x1) {
406 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on HeaderVersion\n"));
407 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
408 if (AbortReason
!= NULL
) {
409 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidHeaderVersion"), L
"InvalidHeaderVersion");
411 return EFI_INCOMPATIBLE_VERSION
;
414 // Check LoaderRevision
416 if (MicrocodeEntryPoint
->LoaderRevision
!= 0x1) {
417 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on LoaderRevision\n"));
418 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
419 if (AbortReason
!= NULL
) {
420 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidLoaderVersion"), L
"InvalidLoaderVersion");
422 return EFI_INCOMPATIBLE_VERSION
;
427 if (MicrocodeEntryPoint
->DataSize
== 0) {
430 TotalSize
= MicrocodeEntryPoint
->TotalSize
;
432 if (TotalSize
<= sizeof(CPU_MICROCODE_HEADER
)) {
433 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - TotalSize too small\n"));
434 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
435 if (AbortReason
!= NULL
) {
436 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidTotalSize"), L
"InvalidTotalSize");
438 return EFI_VOLUME_CORRUPTED
;
440 if ((TotalSize
& (SIZE_1KB
- 1)) != 0) {
441 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - TotalSize is not multiples of 1024 bytes (1 KBytes)\n"));
442 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
443 if (AbortReason
!= NULL
) {
444 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidTotalSize"), L
"InvalidTotalSize");
446 return EFI_VOLUME_CORRUPTED
;
448 if (TotalSize
!= ImageSize
) {
449 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - TotalSize not equal to ImageSize\n"));
450 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
451 if (AbortReason
!= NULL
) {
452 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidTotalSize"), L
"InvalidTotalSize");
454 return EFI_VOLUME_CORRUPTED
;
459 if (MicrocodeEntryPoint
->DataSize
== 0) {
460 DataSize
= 2048 - sizeof(CPU_MICROCODE_HEADER
);
462 DataSize
= MicrocodeEntryPoint
->DataSize
;
464 if (DataSize
> TotalSize
- sizeof(CPU_MICROCODE_HEADER
)) {
465 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - DataSize too big\n"));
466 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
467 if (AbortReason
!= NULL
) {
468 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidDataSize"), L
"InvalidDataSize");
470 return EFI_VOLUME_CORRUPTED
;
472 if ((DataSize
& 0x3) != 0) {
473 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - DataSize is not multiples of DWORDs\n"));
474 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
475 if (AbortReason
!= NULL
) {
476 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidDataSize"), L
"InvalidDataSize");
478 return EFI_VOLUME_CORRUPTED
;
483 CheckSum32
= CalculateSum32((UINT32
*)MicrocodeEntryPoint
, DataSize
+ sizeof(CPU_MICROCODE_HEADER
));
484 if (CheckSum32
!= 0) {
485 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on CheckSum32\n"));
486 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
487 if (AbortReason
!= NULL
) {
488 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidChecksum"), L
"InvalidChecksum");
490 return EFI_VOLUME_CORRUPTED
;
492 InCompleteCheckSum32
= CheckSum32
;
493 InCompleteCheckSum32
-= MicrocodeEntryPoint
->ProcessorSignature
.Uint32
;
494 InCompleteCheckSum32
-= MicrocodeEntryPoint
->ProcessorFlags
;
495 InCompleteCheckSum32
-= MicrocodeEntryPoint
->Checksum
;
498 // Check ProcessorSignature/ProcessorFlags
501 ProcessorInfo
= GetMatchedProcessor (MicrocodeFmpPrivate
, MicrocodeEntryPoint
->ProcessorSignature
.Uint32
, MicrocodeEntryPoint
->ProcessorFlags
, TargetCpuIndex
);
502 if (ProcessorInfo
== NULL
) {
503 CorrectMicrocode
= FALSE
;
504 ExtendedTableLength
= TotalSize
- (DataSize
+ sizeof(CPU_MICROCODE_HEADER
));
505 if (ExtendedTableLength
!= 0) {
507 // Extended Table exist, check if the CPU in support list
509 ExtendedTableHeader
= (CPU_MICROCODE_EXTENDED_TABLE_HEADER
*)((UINT8
*)(MicrocodeEntryPoint
) + DataSize
+ sizeof(CPU_MICROCODE_HEADER
));
511 // Calculate Extended Checksum
513 if ((ExtendedTableLength
> sizeof(CPU_MICROCODE_EXTENDED_TABLE_HEADER
)) && ((ExtendedTableLength
& 0x3) == 0)) {
514 CheckSum32
= CalculateSum32((UINT32
*)ExtendedTableHeader
, ExtendedTableLength
);
515 if (CheckSum32
!= 0) {
517 // Checksum incorrect
519 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - The checksum for extended table is incorrect\n"));
524 ExtendedTableCount
= ExtendedTableHeader
->ExtendedSignatureCount
;
525 if (ExtendedTableCount
> (ExtendedTableLength
- sizeof(CPU_MICROCODE_EXTENDED_TABLE_HEADER
)) / sizeof(CPU_MICROCODE_EXTENDED_TABLE
)) {
526 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - ExtendedTableCount %d is too big\n", ExtendedTableCount
));
528 ExtendedTable
= (CPU_MICROCODE_EXTENDED_TABLE
*)(ExtendedTableHeader
+ 1);
529 for (Index
= 0; Index
< ExtendedTableCount
; Index
++) {
530 CheckSum32
= InCompleteCheckSum32
;
531 CheckSum32
+= ExtendedTable
->ProcessorSignature
.Uint32
;
532 CheckSum32
+= ExtendedTable
->ProcessorFlag
;
533 CheckSum32
+= ExtendedTable
->Checksum
;
534 if (CheckSum32
!= 0) {
535 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - The checksum for ExtendedTable entry with index 0x%x is incorrect\n", Index
));
540 ProcessorInfo
= GetMatchedProcessor (MicrocodeFmpPrivate
, ExtendedTable
->ProcessorSignature
.Uint32
, ExtendedTable
->ProcessorFlag
, TargetCpuIndex
);
541 if (ProcessorInfo
!= NULL
) {
545 CorrectMicrocode
= TRUE
;
555 if (!CorrectMicrocode
) {
557 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on Current ProcessorSignature/ProcessorFlags\n"));
559 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION
;
560 if (AbortReason
!= NULL
) {
561 *AbortReason
= AllocateCopyPool(sizeof(L
"UnsupportedProcessorSignature/ProcessorFlags"), L
"UnsupportedProcessorSignature/ProcessorFlags");
563 return EFI_UNSUPPORTED
;
568 // Check UpdateRevision
570 CurrentRevision
= ProcessorInfo
->MicrocodeRevision
;
571 if ((MicrocodeEntryPoint
->UpdateRevision
< CurrentRevision
) ||
572 (TryLoad
&& (MicrocodeEntryPoint
->UpdateRevision
== CurrentRevision
))) {
574 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on UpdateRevision\n"));
576 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION
;
577 if (AbortReason
!= NULL
) {
578 *AbortReason
= AllocateCopyPool(sizeof(L
"IncorrectRevision"), L
"IncorrectRevision");
580 return EFI_INCOMPATIBLE_VERSION
;
587 CurrentRevision
= LoadMicrocodeOnThis(MicrocodeFmpPrivate
, ProcessorInfo
->CpuIndex
, (UINTN
)MicrocodeEntryPoint
+ sizeof(CPU_MICROCODE_HEADER
));
588 if (MicrocodeEntryPoint
->UpdateRevision
!= CurrentRevision
) {
589 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on LoadMicrocode\n"));
590 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR
;
591 if (AbortReason
!= NULL
) {
592 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidData"), L
"InvalidData");
594 return EFI_SECURITY_VIOLATION
;
602 Get next Microcode entrypoint.
604 @param[in] MicrocodeFmpPrivate The Microcode driver private data
605 @param[in] MicrocodeEntryPoint Current Microcode entrypoint
607 @return next Microcode entrypoint.
609 CPU_MICROCODE_HEADER
*
611 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
612 IN CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
617 for (Index
= 0; Index
< MicrocodeFmpPrivate
->DescriptorCount
; Index
++) {
618 if (MicrocodeEntryPoint
== MicrocodeFmpPrivate
->MicrocodeInfo
[Index
].MicrocodeEntryPoint
) {
619 if (Index
== (UINTN
)MicrocodeFmpPrivate
->DescriptorCount
- 1) {
624 return MicrocodeFmpPrivate
->MicrocodeInfo
[Index
+ 1].MicrocodeEntryPoint
;
634 Get next FIT Microcode entrypoint.
636 @param[in] MicrocodeFmpPrivate The Microcode driver private data
637 @param[in] MicrocodeEntryPoint Current Microcode entrypoint
639 @return next FIT Microcode entrypoint.
641 CPU_MICROCODE_HEADER
*
642 GetNextFitMicrocode (
643 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
644 IN CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
649 for (Index
= 0; Index
< MicrocodeFmpPrivate
->FitMicrocodeEntryCount
; Index
++) {
650 if (MicrocodeEntryPoint
== MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].MicrocodeEntryPoint
) {
651 if (Index
== (UINTN
) MicrocodeFmpPrivate
->FitMicrocodeEntryCount
- 1) {
656 return MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
+ 1].MicrocodeEntryPoint
;
666 Find empty FIT Microcode entrypoint.
668 @param[in] MicrocodeFmpPrivate The Microcode driver private data
669 @param[in] ImageSize The size of Microcode image buffer in bytes.
670 @param[out] AvailableSize Available size of the empty FIT Microcode entrypoint.
672 @return Empty FIT Microcode entrypoint.
674 CPU_MICROCODE_HEADER
*
675 FindEmptyFitMicrocode (
676 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
678 OUT UINTN
*AvailableSize
682 CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
;
683 CPU_MICROCODE_HEADER
*NextMicrocodeEntryPoint
;
684 VOID
*MicrocodePatchAddress
;
685 UINTN MicrocodePatchRegionSize
;
687 MicrocodePatchAddress
= MicrocodeFmpPrivate
->MicrocodePatchAddress
;
688 MicrocodePatchRegionSize
= MicrocodeFmpPrivate
->MicrocodePatchRegionSize
;
690 for (Index
= 0; Index
< MicrocodeFmpPrivate
->FitMicrocodeEntryCount
; Index
++) {
691 if (MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].Empty
) {
692 MicrocodeEntryPoint
= MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].MicrocodeEntryPoint
;
693 NextMicrocodeEntryPoint
= GetNextFitMicrocode (MicrocodeFmpPrivate
, MicrocodeEntryPoint
);
694 if (NextMicrocodeEntryPoint
!= NULL
) {
695 *AvailableSize
= (UINTN
) NextMicrocodeEntryPoint
- (UINTN
) MicrocodeEntryPoint
;
697 *AvailableSize
= (UINTN
) MicrocodePatchAddress
+ MicrocodePatchRegionSize
- (UINTN
) MicrocodeEntryPoint
;
699 if (*AvailableSize
>= ImageSize
) {
700 return MicrocodeEntryPoint
;
709 Find unused FIT Microcode entrypoint.
711 @param[in] MicrocodeFmpPrivate The Microcode driver private data
712 @param[in] ImageSize The size of Microcode image buffer in bytes.
713 @param[out] AvailableSize Available size of the unused FIT Microcode entrypoint.
715 @return Unused FIT Microcode entrypoint.
717 CPU_MICROCODE_HEADER
*
718 FindUnusedFitMicrocode (
719 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
721 OUT UINTN
*AvailableSize
725 CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
;
726 CPU_MICROCODE_HEADER
*NextMicrocodeEntryPoint
;
727 VOID
*MicrocodePatchAddress
;
728 UINTN MicrocodePatchRegionSize
;
730 MicrocodePatchAddress
= MicrocodeFmpPrivate
->MicrocodePatchAddress
;
731 MicrocodePatchRegionSize
= MicrocodeFmpPrivate
->MicrocodePatchRegionSize
;
733 for (Index
= 0; Index
< MicrocodeFmpPrivate
->FitMicrocodeEntryCount
; Index
++) {
734 if (!MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].InUse
) {
735 MicrocodeEntryPoint
= MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].MicrocodeEntryPoint
;
736 NextMicrocodeEntryPoint
= GetNextFitMicrocode (MicrocodeFmpPrivate
, MicrocodeEntryPoint
);
737 if (NextMicrocodeEntryPoint
!= NULL
) {
738 *AvailableSize
= (UINTN
) NextMicrocodeEntryPoint
- (UINTN
) MicrocodeEntryPoint
;
740 *AvailableSize
= (UINTN
) MicrocodePatchAddress
+ MicrocodePatchRegionSize
- (UINTN
) MicrocodeEntryPoint
;
742 if (*AvailableSize
>= ImageSize
) {
743 return MicrocodeEntryPoint
;
752 Get current Microcode used region size.
754 @param[in] MicrocodeFmpPrivate The Microcode driver private data
756 @return current Microcode used region size.
759 GetCurrentMicrocodeUsedRegionSize (
760 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
763 if (MicrocodeFmpPrivate
->DescriptorCount
== 0) {
767 return (UINTN
)MicrocodeFmpPrivate
->MicrocodeInfo
[MicrocodeFmpPrivate
->DescriptorCount
- 1].MicrocodeEntryPoint
768 + (UINTN
)MicrocodeFmpPrivate
->MicrocodeInfo
[MicrocodeFmpPrivate
->DescriptorCount
- 1].TotalSize
769 - (UINTN
)MicrocodeFmpPrivate
->MicrocodePatchAddress
;
775 @param[in] Address The flash address of Microcode.
776 @param[in] Image The Microcode image buffer.
777 @param[in] ImageSize The size of Microcode image buffer in bytes.
778 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
780 @retval EFI_SUCCESS The Microcode image is updated.
781 @retval EFI_WRITE_PROTECTED The flash device is read only.
788 OUT UINT32
*LastAttemptStatus
793 DEBUG((DEBUG_INFO
, "PlatformUpdate:"));
794 DEBUG((DEBUG_INFO
, " Address - 0x%lx,", Address
));
795 DEBUG((DEBUG_INFO
, " Length - 0x%x\n", ImageSize
));
797 Status
= MicrocodeFlashWrite (
802 if (!EFI_ERROR(Status
)) {
803 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_SUCCESS
;
805 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL
;
811 Update Microcode flash region with FIT.
813 @param[in] MicrocodeFmpPrivate The Microcode driver private data
814 @param[in] TargetMicrocodeEntryPoint Target Microcode entrypoint to be updated
815 @param[in] Image The Microcode image buffer.
816 @param[in] ImageSize The size of Microcode image buffer in bytes.
817 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
819 @retval EFI_SUCCESS The Microcode image is written.
820 @retval EFI_WRITE_PROTECTED The flash device is read only.
823 UpdateMicrocodeFlashRegionWithFit (
824 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
825 IN CPU_MICROCODE_HEADER
*TargetMicrocodeEntryPoint
,
828 OUT UINT32
*LastAttemptStatus
831 VOID
*MicrocodePatchAddress
;
832 UINTN MicrocodePatchRegionSize
;
833 UINTN TargetTotalSize
;
835 VOID
*MicrocodePatchScratchBuffer
;
836 UINT8
*ScratchBufferPtr
;
837 UINTN ScratchBufferSize
;
840 VOID
*NextMicrocodeEntryPoint
;
841 VOID
*EmptyFitMicrocodeEntry
;
842 VOID
*UnusedFitMicrocodeEntry
;
844 DEBUG((DEBUG_INFO
, "UpdateMicrocodeFlashRegionWithFit: Image - 0x%x, size - 0x%x\n", Image
, ImageSize
));
846 MicrocodePatchAddress
= MicrocodeFmpPrivate
->MicrocodePatchAddress
;
847 MicrocodePatchRegionSize
= MicrocodeFmpPrivate
->MicrocodePatchRegionSize
;
849 MicrocodePatchScratchBuffer
= AllocateZeroPool (MicrocodePatchRegionSize
);
850 if (MicrocodePatchScratchBuffer
== NULL
) {
851 DEBUG((DEBUG_ERROR
, "Fail to allocate Microcode Scratch buffer\n"));
852 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
853 return EFI_OUT_OF_RESOURCES
;
855 ScratchBufferPtr
= MicrocodePatchScratchBuffer
;
856 ScratchBufferSize
= 0;
859 // Target data collection
863 if (TargetMicrocodeEntryPoint
!= NULL
) {
864 if (TargetMicrocodeEntryPoint
->DataSize
== 0) {
865 TargetTotalSize
= 2048;
867 TargetTotalSize
= TargetMicrocodeEntryPoint
->TotalSize
;
869 DEBUG((DEBUG_INFO
, " TargetTotalSize - 0x%x\n", TargetTotalSize
));
870 NextMicrocodeEntryPoint
= GetNextFitMicrocode (MicrocodeFmpPrivate
, TargetMicrocodeEntryPoint
);
871 DEBUG((DEBUG_INFO
, " NextMicrocodeEntryPoint - 0x%x\n", NextMicrocodeEntryPoint
));
872 if (NextMicrocodeEntryPoint
!= NULL
) {
873 ASSERT ((UINTN
) NextMicrocodeEntryPoint
>= ((UINTN
) TargetMicrocodeEntryPoint
+ TargetTotalSize
));
874 AvailableSize
= (UINTN
) NextMicrocodeEntryPoint
- (UINTN
) TargetMicrocodeEntryPoint
;
876 AvailableSize
= (UINTN
) MicrocodePatchAddress
+ MicrocodePatchRegionSize
- (UINTN
) TargetMicrocodeEntryPoint
;
878 DEBUG((DEBUG_INFO
, " AvailableSize - 0x%x\n", AvailableSize
));
879 ASSERT (AvailableSize
>= TargetTotalSize
);
882 // Total Size means the Microcode size.
883 // Available Size means the Microcode size plus the pad till (1) next Microcode or (2) the end.
886 // +------+-----------+-----+------+===================+
887 // | MCU1 | Microcode | PAD | MCU2 | Empty |
888 // +------+-----------+-----+------+===================+
890 // |<-AvailableSize->|
893 // +------+-----------+===================+
894 // | MCU | Microcode | Empty |
895 // +------+-----------+===================+
897 // |<- AvailableSize ->|
901 // Update based on policy
905 // 1. If there is enough space to update old one in situ, replace old microcode in situ.
907 if (AvailableSize
>= ImageSize
) {
908 DEBUG((DEBUG_INFO
, "Replace old microcode in situ\n"));
910 // +------+------------+------+===================+
911 // |Other | Old Image | ... | Empty |
912 // +------+------------+------+===================+
914 // +------+---------+--+------+===================+
915 // |Other |New Image|FF| ... | Empty |
916 // +------+---------+--+------+===================+
918 // 1.1. Copy new image
919 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
920 ScratchBufferSize
+= ImageSize
;
921 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
923 RestSize
= AvailableSize
- ImageSize
;
925 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
926 ScratchBufferSize
+= RestSize
;
927 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
929 Status
= UpdateMicrocode((UINTN
)TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
934 // 2. If there is empty FIT microcode entry with enough space, use it.
936 EmptyFitMicrocodeEntry
= FindEmptyFitMicrocode (MicrocodeFmpPrivate
, ImageSize
, &AvailableSize
);
937 if (EmptyFitMicrocodeEntry
!= NULL
) {
938 DEBUG((DEBUG_INFO
, "Use empty FIT microcode entry\n"));
939 // 2.1. Copy new image
940 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
941 ScratchBufferSize
+= ImageSize
;
942 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
944 RestSize
= AvailableSize
- ImageSize
;
946 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
947 ScratchBufferSize
+= RestSize
;
948 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
950 Status
= UpdateMicrocode ((UINTN
) EmptyFitMicrocodeEntry
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
951 if (!EFI_ERROR (Status
) && (TargetMicrocodeEntryPoint
!= NULL
)) {
953 // Empty old microcode.
955 ScratchBufferPtr
= MicrocodePatchScratchBuffer
;
956 SetMem (ScratchBufferPtr
, TargetTotalSize
, 0xFF);
957 ScratchBufferSize
= TargetTotalSize
;
958 ScratchBufferPtr
= (UINT8
*) MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
959 UpdateMicrocode ((UINTN
) TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
965 // 3. If there is unused microcode entry with enough space, use it.
967 UnusedFitMicrocodeEntry
= FindUnusedFitMicrocode (MicrocodeFmpPrivate
, ImageSize
, &AvailableSize
);
968 if (UnusedFitMicrocodeEntry
!= NULL
) {
969 DEBUG((DEBUG_INFO
, "Use unused FIT microcode entry\n"));
970 // 3.1. Copy new image
971 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
972 ScratchBufferSize
+= ImageSize
;
973 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
975 RestSize
= AvailableSize
- ImageSize
;
977 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
978 ScratchBufferSize
+= RestSize
;
979 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
981 Status
= UpdateMicrocode ((UINTN
) UnusedFitMicrocodeEntry
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
982 if (!EFI_ERROR (Status
) && (TargetMicrocodeEntryPoint
!= NULL
)) {
984 // Empty old microcode.
986 ScratchBufferPtr
= MicrocodePatchScratchBuffer
;
987 SetMem (ScratchBufferPtr
, TargetTotalSize
, 0xFF);
988 ScratchBufferSize
= TargetTotalSize
;
989 ScratchBufferPtr
= (UINT8
*) MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
990 UpdateMicrocode ((UINTN
) TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
996 // 4. No usable FIT microcode entry.
998 DEBUG((DEBUG_ERROR
, "No usable FIT microcode entry\n"));
999 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
1000 Status
= EFI_OUT_OF_RESOURCES
;
1006 Update Microcode flash region.
1008 @param[in] MicrocodeFmpPrivate The Microcode driver private data
1009 @param[in] TargetMicrocodeEntryPoint Target Microcode entrypoint to be updated
1010 @param[in] Image The Microcode image buffer.
1011 @param[in] ImageSize The size of Microcode image buffer in bytes.
1012 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
1014 @retval EFI_SUCCESS The Microcode image is written.
1015 @retval EFI_WRITE_PROTECTED The flash device is read only.
1018 UpdateMicrocodeFlashRegion (
1019 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
1020 IN CPU_MICROCODE_HEADER
*TargetMicrocodeEntryPoint
,
1023 OUT UINT32
*LastAttemptStatus
1026 VOID
*MicrocodePatchAddress
;
1027 UINTN MicrocodePatchRegionSize
;
1028 UINTN TargetTotalSize
;
1029 UINTN UsedRegionSize
;
1031 VOID
*MicrocodePatchScratchBuffer
;
1032 UINT8
*ScratchBufferPtr
;
1033 UINTN ScratchBufferSize
;
1035 UINTN AvailableSize
;
1036 VOID
*NextMicrocodeEntryPoint
;
1037 MICROCODE_INFO
*MicrocodeInfo
;
1038 UINTN MicrocodeCount
;
1041 DEBUG((DEBUG_INFO
, "UpdateMicrocodeFlashRegion: Image - 0x%x, size - 0x%x\n", Image
, ImageSize
));
1043 MicrocodePatchAddress
= MicrocodeFmpPrivate
->MicrocodePatchAddress
;
1044 MicrocodePatchRegionSize
= MicrocodeFmpPrivate
->MicrocodePatchRegionSize
;
1046 MicrocodePatchScratchBuffer
= AllocateZeroPool (MicrocodePatchRegionSize
);
1047 if (MicrocodePatchScratchBuffer
== NULL
) {
1048 DEBUG((DEBUG_ERROR
, "Fail to allocate Microcode Scratch buffer\n"));
1049 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
1050 return EFI_OUT_OF_RESOURCES
;
1052 ScratchBufferPtr
= MicrocodePatchScratchBuffer
;
1053 ScratchBufferSize
= 0;
1056 // Target data collection
1058 TargetTotalSize
= 0;
1060 NextMicrocodeEntryPoint
= NULL
;
1061 if (TargetMicrocodeEntryPoint
!= NULL
) {
1062 if (TargetMicrocodeEntryPoint
->DataSize
== 0) {
1063 TargetTotalSize
= 2048;
1065 TargetTotalSize
= TargetMicrocodeEntryPoint
->TotalSize
;
1067 DEBUG((DEBUG_INFO
, " TargetTotalSize - 0x%x\n", TargetTotalSize
));
1068 NextMicrocodeEntryPoint
= GetNextMicrocode(MicrocodeFmpPrivate
, TargetMicrocodeEntryPoint
);
1069 DEBUG((DEBUG_INFO
, " NextMicrocodeEntryPoint - 0x%x\n", NextMicrocodeEntryPoint
));
1070 if (NextMicrocodeEntryPoint
!= NULL
) {
1071 ASSERT ((UINTN
)NextMicrocodeEntryPoint
>= ((UINTN
)TargetMicrocodeEntryPoint
+ TargetTotalSize
));
1072 AvailableSize
= (UINTN
)NextMicrocodeEntryPoint
- (UINTN
)TargetMicrocodeEntryPoint
;
1074 AvailableSize
= (UINTN
)MicrocodePatchAddress
+ MicrocodePatchRegionSize
- (UINTN
)TargetMicrocodeEntryPoint
;
1076 DEBUG((DEBUG_INFO
, " AvailableSize - 0x%x\n", AvailableSize
));
1077 ASSERT (AvailableSize
>= TargetTotalSize
);
1079 UsedRegionSize
= GetCurrentMicrocodeUsedRegionSize(MicrocodeFmpPrivate
);
1080 DEBUG((DEBUG_INFO
, " UsedRegionSize - 0x%x\n", UsedRegionSize
));
1081 ASSERT (UsedRegionSize
>= TargetTotalSize
);
1082 if (TargetMicrocodeEntryPoint
!= NULL
) {
1083 ASSERT ((UINTN
)MicrocodePatchAddress
+ UsedRegionSize
>= ((UINTN
)TargetMicrocodeEntryPoint
+ TargetTotalSize
));
1086 // Total Size means the Microcode size.
1087 // Available Size means the Microcode size plus the pad till (1) next Microcode or (2) the end.
1090 // +------+-----------+-----+------+===================+
1091 // | MCU1 | Microcode | PAD | MCU2 | Empty |
1092 // +------+-----------+-----+------+===================+
1094 // |<-AvailableSize->|
1095 // |<- UsedRegionSize ->|
1098 // +------+-----------+===================+
1099 // | MCU | Microcode | Empty |
1100 // +------+-----------+===================+
1102 // |<- AvailableSize ->|
1103 // |<-UsedRegionSize->|
1107 // Update based on policy
1111 // 1. If there is enough space to update old one in situ, replace old microcode in situ.
1113 if (AvailableSize
>= ImageSize
) {
1114 DEBUG((DEBUG_INFO
, "Replace old microcode in situ\n"));
1116 // +------+------------+------+===================+
1117 // |Other | Old Image | ... | Empty |
1118 // +------+------------+------+===================+
1120 // +------+---------+--+------+===================+
1121 // |Other |New Image|FF| ... | Empty |
1122 // +------+---------+--+------+===================+
1124 // 1.1. Copy new image
1125 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
1126 ScratchBufferSize
+= ImageSize
;
1127 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1129 RestSize
= AvailableSize
- ImageSize
;
1131 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
1132 ScratchBufferSize
+= RestSize
;
1133 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1135 Status
= UpdateMicrocode((UINTN
)TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
1140 // 2. If there is enough space to remove old one and add new one, reorg and replace old microcode.
1142 if (MicrocodePatchRegionSize
- (UsedRegionSize
- TargetTotalSize
) >= ImageSize
) {
1143 if (TargetMicrocodeEntryPoint
== NULL
) {
1144 DEBUG((DEBUG_INFO
, "Append new microcode\n"));
1146 // +------+------------+------+===================+
1147 // |Other1| Other |Other2| Empty |
1148 // +------+------------+------+===================+
1150 // +------+------------+------+-----------+=======+
1151 // |Other1| Other |Other2| New Image | Empty |
1152 // +------+------------+------+-----------+=======+
1154 Status
= UpdateMicrocode((UINTN
)MicrocodePatchAddress
+ UsedRegionSize
, Image
, ImageSize
, LastAttemptStatus
);
1156 DEBUG((DEBUG_INFO
, "Reorg and replace old microcode\n"));
1158 // +------+------------+------+===================+
1159 // |Other | Old Image | ... | Empty |
1160 // +------+------------+------+===================+
1162 // +------+---------------+------+================+
1163 // |Other | New Image | ... | Empty |
1164 // +------+---------------+------+================+
1166 // 2.1. Copy new image
1167 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
1168 ScratchBufferSize
+= ImageSize
;
1169 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1170 // 2.2. Copy rest images after the old image.
1171 if (NextMicrocodeEntryPoint
!= 0) {
1172 RestSize
= (UINTN
)MicrocodePatchAddress
+ UsedRegionSize
- ((UINTN
)NextMicrocodeEntryPoint
);
1173 CopyMem (ScratchBufferPtr
, NextMicrocodeEntryPoint
, RestSize
);
1174 ScratchBufferSize
+= RestSize
;
1175 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1177 Status
= UpdateMicrocode((UINTN
)TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
1183 // 3. The new image can be put in MCU region, but not all others can be put.
1184 // So all the unused MCU is removed.
1186 if (MicrocodePatchRegionSize
>= ImageSize
) {
1188 // +------+------------+------+===================+
1189 // |Other1| Old Image |Other2| Empty |
1190 // +------+------------+------+===================+
1192 // +-------------------------------------+--------+
1193 // | New Image | Other |
1194 // +-------------------------------------+--------+
1196 DEBUG((DEBUG_INFO
, "Add new microcode from beginning\n"));
1198 MicrocodeCount
= MicrocodeFmpPrivate
->DescriptorCount
;
1199 MicrocodeInfo
= MicrocodeFmpPrivate
->MicrocodeInfo
;
1201 // 3.1. Copy new image
1202 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
1203 ScratchBufferSize
+= ImageSize
;
1204 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1205 // 3.2. Copy some others to rest buffer
1206 for (Index
= 0; Index
< MicrocodeCount
; Index
++) {
1207 if (!MicrocodeInfo
[Index
].InUse
) {
1210 if (MicrocodeInfo
[Index
].MicrocodeEntryPoint
== TargetMicrocodeEntryPoint
) {
1213 if (MicrocodeInfo
[Index
].TotalSize
<= MicrocodePatchRegionSize
- ScratchBufferSize
) {
1214 CopyMem (ScratchBufferPtr
, MicrocodeInfo
[Index
].MicrocodeEntryPoint
, MicrocodeInfo
[Index
].TotalSize
);
1215 ScratchBufferSize
+= MicrocodeInfo
[Index
].TotalSize
;
1216 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1220 RestSize
= MicrocodePatchRegionSize
- ScratchBufferSize
;
1222 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
1223 ScratchBufferSize
+= RestSize
;
1224 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1226 Status
= UpdateMicrocode((UINTN
)MicrocodePatchAddress
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
1231 // 4. The new image size is bigger than the whole MCU region.
1233 DEBUG((DEBUG_ERROR
, "Microcode too big\n"));
1234 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
1235 Status
= EFI_OUT_OF_RESOURCES
;
1243 Caution: This function may receive untrusted input.
1245 @param[in] MicrocodeFmpPrivate The Microcode driver private data
1246 @param[in] Image The Microcode image buffer.
1247 @param[in] ImageSize The size of Microcode image buffer in bytes.
1248 @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
1249 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
1250 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
1251 details for the aborted operation. The buffer is allocated by this function
1252 with AllocatePool(), and it is the caller's responsibility to free it with a
1255 @retval EFI_SUCCESS The Microcode image is written.
1256 @retval EFI_VOLUME_CORRUPTED The Microcode image is corrupted.
1257 @retval EFI_INCOMPATIBLE_VERSION The Microcode image version is incorrect.
1258 @retval EFI_SECURITY_VIOLATION The Microcode image fails to load.
1259 @retval EFI_WRITE_PROTECTED The flash device is read only.
1263 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
1266 OUT UINT32
*LastAttemptVersion
,
1267 OUT UINT32
*LastAttemptStatus
,
1268 OUT CHAR16
**AbortReason
1273 CPU_MICROCODE_HEADER
*TargetMicrocodeEntryPoint
;
1274 UINTN TargetCpuIndex
;
1275 UINTN TargetMicrcodeIndex
;
1278 // MCU must be 16 bytes aligned
1280 AlignedImage
= AllocateCopyPool(ImageSize
, Image
);
1281 if (AlignedImage
== NULL
) {
1282 DEBUG((DEBUG_ERROR
, "Fail to allocate aligned image\n"));
1283 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
1284 return EFI_OUT_OF_RESOURCES
;
1287 TargetCpuIndex
= (UINTN
)-1;
1288 Status
= VerifyMicrocode(MicrocodeFmpPrivate
, AlignedImage
, ImageSize
, TRUE
, LastAttemptStatus
, AbortReason
, &TargetCpuIndex
);
1289 if (EFI_ERROR(Status
)) {
1290 DEBUG((DEBUG_ERROR
, "Fail to verify Microcode Region\n"));
1291 FreePool(AlignedImage
);
1294 DEBUG((DEBUG_INFO
, "Pass VerifyMicrocode\n"));
1295 *LastAttemptVersion
= ((CPU_MICROCODE_HEADER
*)Image
)->UpdateRevision
;
1297 DEBUG((DEBUG_INFO
, " TargetCpuIndex - 0x%x\n", TargetCpuIndex
));
1298 ASSERT (TargetCpuIndex
< MicrocodeFmpPrivate
->ProcessorCount
);
1299 TargetMicrcodeIndex
= MicrocodeFmpPrivate
->ProcessorInfo
[TargetCpuIndex
].MicrocodeIndex
;
1300 DEBUG((DEBUG_INFO
, " TargetMicrcodeIndex - 0x%x\n", TargetMicrcodeIndex
));
1301 if (TargetMicrcodeIndex
!= (UINTN
)-1) {
1302 ASSERT (TargetMicrcodeIndex
< MicrocodeFmpPrivate
->DescriptorCount
);
1303 TargetMicrocodeEntryPoint
= MicrocodeFmpPrivate
->MicrocodeInfo
[TargetMicrcodeIndex
].MicrocodeEntryPoint
;
1305 TargetMicrocodeEntryPoint
= NULL
;
1307 DEBUG((DEBUG_INFO
, " TargetMicrocodeEntryPoint - 0x%x\n", TargetMicrocodeEntryPoint
));
1309 if (MicrocodeFmpPrivate
->FitMicrocodeInfo
!= NULL
) {
1310 Status
= UpdateMicrocodeFlashRegionWithFit (
1311 MicrocodeFmpPrivate
,
1312 TargetMicrocodeEntryPoint
,
1318 Status
= UpdateMicrocodeFlashRegion (
1319 MicrocodeFmpPrivate
,
1320 TargetMicrocodeEntryPoint
,
1327 FreePool(AlignedImage
);