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
;
394 UINTN ExtendedTableLength
;
395 UINT32 ExtendedTableCount
;
396 CPU_MICROCODE_EXTENDED_TABLE
*ExtendedTable
;
397 CPU_MICROCODE_EXTENDED_TABLE_HEADER
*ExtendedTableHeader
;
398 BOOLEAN CorrectMicrocode
;
401 // Check HeaderVersion
403 MicrocodeEntryPoint
= Image
;
404 if (MicrocodeEntryPoint
->HeaderVersion
!= 0x1) {
405 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on HeaderVersion\n"));
406 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
407 if (AbortReason
!= NULL
) {
408 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidHeaderVersion"), L
"InvalidHeaderVersion");
410 return EFI_INCOMPATIBLE_VERSION
;
413 // Check LoaderRevision
415 if (MicrocodeEntryPoint
->LoaderRevision
!= 0x1) {
416 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on LoaderRevision\n"));
417 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
418 if (AbortReason
!= NULL
) {
419 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidLoaderVersion"), L
"InvalidLoaderVersion");
421 return EFI_INCOMPATIBLE_VERSION
;
426 if (MicrocodeEntryPoint
->DataSize
== 0) {
429 TotalSize
= MicrocodeEntryPoint
->TotalSize
;
431 if (TotalSize
<= sizeof(CPU_MICROCODE_HEADER
)) {
432 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - TotalSize too small\n"));
433 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
434 if (AbortReason
!= NULL
) {
435 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidTotalSize"), L
"InvalidTotalSize");
437 return EFI_VOLUME_CORRUPTED
;
439 if ((TotalSize
& (SIZE_1KB
- 1)) != 0) {
440 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - TotalSize is not multiples of 1024 bytes (1 KBytes)\n"));
441 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
442 if (AbortReason
!= NULL
) {
443 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidTotalSize"), L
"InvalidTotalSize");
445 return EFI_VOLUME_CORRUPTED
;
447 if (TotalSize
!= ImageSize
) {
448 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - TotalSize not equal to ImageSize\n"));
449 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
450 if (AbortReason
!= NULL
) {
451 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidTotalSize"), L
"InvalidTotalSize");
453 return EFI_VOLUME_CORRUPTED
;
458 if (MicrocodeEntryPoint
->DataSize
== 0) {
459 DataSize
= 2048 - sizeof(CPU_MICROCODE_HEADER
);
461 DataSize
= MicrocodeEntryPoint
->DataSize
;
463 if (DataSize
> TotalSize
- sizeof(CPU_MICROCODE_HEADER
)) {
464 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - DataSize too big\n"));
465 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
466 if (AbortReason
!= NULL
) {
467 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidDataSize"), L
"InvalidDataSize");
469 return EFI_VOLUME_CORRUPTED
;
471 if ((DataSize
& 0x3) != 0) {
472 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - DataSize is not multiples of DWORDs\n"));
473 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
474 if (AbortReason
!= NULL
) {
475 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidDataSize"), L
"InvalidDataSize");
477 return EFI_VOLUME_CORRUPTED
;
482 CheckSum32
= CalculateSum32((UINT32
*)MicrocodeEntryPoint
, DataSize
+ sizeof(CPU_MICROCODE_HEADER
));
483 if (CheckSum32
!= 0) {
484 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on CheckSum32\n"));
485 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT
;
486 if (AbortReason
!= NULL
) {
487 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidChecksum"), L
"InvalidChecksum");
489 return EFI_VOLUME_CORRUPTED
;
493 // Check ProcessorSignature/ProcessorFlags
496 ProcessorInfo
= GetMatchedProcessor (MicrocodeFmpPrivate
, MicrocodeEntryPoint
->ProcessorSignature
.Uint32
, MicrocodeEntryPoint
->ProcessorFlags
, TargetCpuIndex
);
497 if (ProcessorInfo
== NULL
) {
498 CorrectMicrocode
= FALSE
;
499 ExtendedTableLength
= TotalSize
- (DataSize
+ sizeof(CPU_MICROCODE_HEADER
));
500 if (ExtendedTableLength
!= 0) {
502 // Extended Table exist, check if the CPU in support list
504 ExtendedTableHeader
= (CPU_MICROCODE_EXTENDED_TABLE_HEADER
*)((UINT8
*)(MicrocodeEntryPoint
) + DataSize
+ sizeof(CPU_MICROCODE_HEADER
));
506 // Calculate Extended Checksum
508 if ((ExtendedTableLength
> sizeof(CPU_MICROCODE_EXTENDED_TABLE_HEADER
)) && ((ExtendedTableLength
& 0x3) == 0)) {
509 CheckSum32
= CalculateSum32((UINT32
*)ExtendedTableHeader
, ExtendedTableLength
);
510 if (CheckSum32
!= 0) {
512 // Checksum incorrect
514 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - The checksum for extended table is incorrect\n"));
519 ExtendedTableCount
= ExtendedTableHeader
->ExtendedSignatureCount
;
520 if (ExtendedTableCount
> (ExtendedTableLength
- sizeof(CPU_MICROCODE_EXTENDED_TABLE_HEADER
)) / sizeof(CPU_MICROCODE_EXTENDED_TABLE
)) {
521 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - ExtendedTableCount %d is too big\n", ExtendedTableCount
));
523 ExtendedTable
= (CPU_MICROCODE_EXTENDED_TABLE
*)(ExtendedTableHeader
+ 1);
524 for (Index
= 0; Index
< ExtendedTableCount
; Index
++) {
525 CheckSum32
= CalculateSum32((UINT32
*)ExtendedTable
, sizeof(CPU_MICROCODE_EXTENDED_TABLE
));
526 if (CheckSum32
!= 0) {
527 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - The checksum for ExtendedTable entry with index 0x%x is incorrect\n", Index
));
532 ProcessorInfo
= GetMatchedProcessor (MicrocodeFmpPrivate
, ExtendedTable
->ProcessorSignature
.Uint32
, ExtendedTable
->ProcessorFlag
, TargetCpuIndex
);
533 if (ProcessorInfo
!= NULL
) {
537 CorrectMicrocode
= TRUE
;
547 if (!CorrectMicrocode
) {
549 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on Current ProcessorSignature/ProcessorFlags\n"));
551 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION
;
552 if (AbortReason
!= NULL
) {
553 *AbortReason
= AllocateCopyPool(sizeof(L
"UnsupportedProcessorSignature/ProcessorFlags"), L
"UnsupportedProcessorSignature/ProcessorFlags");
555 return EFI_UNSUPPORTED
;
560 // Check UpdateRevision
562 CurrentRevision
= ProcessorInfo
->MicrocodeRevision
;
563 if ((MicrocodeEntryPoint
->UpdateRevision
< CurrentRevision
) ||
564 (TryLoad
&& (MicrocodeEntryPoint
->UpdateRevision
== CurrentRevision
))) {
566 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on UpdateRevision\n"));
568 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION
;
569 if (AbortReason
!= NULL
) {
570 *AbortReason
= AllocateCopyPool(sizeof(L
"IncorrectRevision"), L
"IncorrectRevision");
572 return EFI_INCOMPATIBLE_VERSION
;
579 CurrentRevision
= LoadMicrocodeOnThis(MicrocodeFmpPrivate
, ProcessorInfo
->CpuIndex
, (UINTN
)MicrocodeEntryPoint
+ sizeof(CPU_MICROCODE_HEADER
));
580 if (MicrocodeEntryPoint
->UpdateRevision
!= CurrentRevision
) {
581 DEBUG((DEBUG_ERROR
, "VerifyMicrocode - fail on LoadMicrocode\n"));
582 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR
;
583 if (AbortReason
!= NULL
) {
584 *AbortReason
= AllocateCopyPool(sizeof(L
"InvalidData"), L
"InvalidData");
586 return EFI_SECURITY_VIOLATION
;
594 Get next Microcode entrypoint.
596 @param[in] MicrocodeFmpPrivate The Microcode driver private data
597 @param[in] MicrocodeEntryPoint Current Microcode entrypoint
599 @return next Microcode entrypoint.
601 CPU_MICROCODE_HEADER
*
603 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
604 IN CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
609 for (Index
= 0; Index
< MicrocodeFmpPrivate
->DescriptorCount
; Index
++) {
610 if (MicrocodeEntryPoint
== MicrocodeFmpPrivate
->MicrocodeInfo
[Index
].MicrocodeEntryPoint
) {
611 if (Index
== (UINTN
)MicrocodeFmpPrivate
->DescriptorCount
- 1) {
616 return MicrocodeFmpPrivate
->MicrocodeInfo
[Index
+ 1].MicrocodeEntryPoint
;
626 Get next FIT Microcode entrypoint.
628 @param[in] MicrocodeFmpPrivate The Microcode driver private data
629 @param[in] MicrocodeEntryPoint Current Microcode entrypoint
631 @return next FIT Microcode entrypoint.
633 CPU_MICROCODE_HEADER
*
634 GetNextFitMicrocode (
635 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
636 IN CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
641 for (Index
= 0; Index
< MicrocodeFmpPrivate
->FitMicrocodeEntryCount
; Index
++) {
642 if (MicrocodeEntryPoint
== MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].MicrocodeEntryPoint
) {
643 if (Index
== (UINTN
) MicrocodeFmpPrivate
->FitMicrocodeEntryCount
- 1) {
648 return MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
+ 1].MicrocodeEntryPoint
;
658 Find empty FIT Microcode entrypoint.
660 @param[in] MicrocodeFmpPrivate The Microcode driver private data
661 @param[in] ImageSize The size of Microcode image buffer in bytes.
662 @param[out] AvailableSize Available size of the empty FIT Microcode entrypoint.
664 @return Empty FIT Microcode entrypoint.
666 CPU_MICROCODE_HEADER
*
667 FindEmptyFitMicrocode (
668 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
670 OUT UINTN
*AvailableSize
674 CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
;
675 CPU_MICROCODE_HEADER
*NextMicrocodeEntryPoint
;
676 VOID
*MicrocodePatchAddress
;
677 UINTN MicrocodePatchRegionSize
;
679 MicrocodePatchAddress
= MicrocodeFmpPrivate
->MicrocodePatchAddress
;
680 MicrocodePatchRegionSize
= MicrocodeFmpPrivate
->MicrocodePatchRegionSize
;
682 for (Index
= 0; Index
< MicrocodeFmpPrivate
->FitMicrocodeEntryCount
; Index
++) {
683 if (MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].Empty
) {
684 MicrocodeEntryPoint
= MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].MicrocodeEntryPoint
;
685 NextMicrocodeEntryPoint
= GetNextFitMicrocode (MicrocodeFmpPrivate
, MicrocodeEntryPoint
);
686 if (NextMicrocodeEntryPoint
!= NULL
) {
687 *AvailableSize
= (UINTN
) NextMicrocodeEntryPoint
- (UINTN
) MicrocodeEntryPoint
;
689 *AvailableSize
= (UINTN
) MicrocodePatchAddress
+ MicrocodePatchRegionSize
- (UINTN
) MicrocodeEntryPoint
;
691 if (*AvailableSize
>= ImageSize
) {
692 return MicrocodeEntryPoint
;
701 Find unused FIT Microcode entrypoint.
703 @param[in] MicrocodeFmpPrivate The Microcode driver private data
704 @param[in] ImageSize The size of Microcode image buffer in bytes.
705 @param[out] AvailableSize Available size of the unused FIT Microcode entrypoint.
707 @return Unused FIT Microcode entrypoint.
709 CPU_MICROCODE_HEADER
*
710 FindUnusedFitMicrocode (
711 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
713 OUT UINTN
*AvailableSize
717 CPU_MICROCODE_HEADER
*MicrocodeEntryPoint
;
718 CPU_MICROCODE_HEADER
*NextMicrocodeEntryPoint
;
719 VOID
*MicrocodePatchAddress
;
720 UINTN MicrocodePatchRegionSize
;
722 MicrocodePatchAddress
= MicrocodeFmpPrivate
->MicrocodePatchAddress
;
723 MicrocodePatchRegionSize
= MicrocodeFmpPrivate
->MicrocodePatchRegionSize
;
725 for (Index
= 0; Index
< MicrocodeFmpPrivate
->FitMicrocodeEntryCount
; Index
++) {
726 if (!MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].InUse
) {
727 MicrocodeEntryPoint
= MicrocodeFmpPrivate
->FitMicrocodeInfo
[Index
].MicrocodeEntryPoint
;
728 NextMicrocodeEntryPoint
= GetNextFitMicrocode (MicrocodeFmpPrivate
, MicrocodeEntryPoint
);
729 if (NextMicrocodeEntryPoint
!= NULL
) {
730 *AvailableSize
= (UINTN
) NextMicrocodeEntryPoint
- (UINTN
) MicrocodeEntryPoint
;
732 *AvailableSize
= (UINTN
) MicrocodePatchAddress
+ MicrocodePatchRegionSize
- (UINTN
) MicrocodeEntryPoint
;
734 if (*AvailableSize
>= ImageSize
) {
735 return MicrocodeEntryPoint
;
744 Get current Microcode used region size.
746 @param[in] MicrocodeFmpPrivate The Microcode driver private data
748 @return current Microcode used region size.
751 GetCurrentMicrocodeUsedRegionSize (
752 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
755 if (MicrocodeFmpPrivate
->DescriptorCount
== 0) {
759 return (UINTN
)MicrocodeFmpPrivate
->MicrocodeInfo
[MicrocodeFmpPrivate
->DescriptorCount
- 1].MicrocodeEntryPoint
760 + (UINTN
)MicrocodeFmpPrivate
->MicrocodeInfo
[MicrocodeFmpPrivate
->DescriptorCount
- 1].TotalSize
761 - (UINTN
)MicrocodeFmpPrivate
->MicrocodePatchAddress
;
767 @param[in] Address The flash address of Microcode.
768 @param[in] Image The Microcode image buffer.
769 @param[in] ImageSize The size of Microcode image buffer in bytes.
770 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
772 @retval EFI_SUCCESS The Microcode image is updated.
773 @retval EFI_WRITE_PROTECTED The flash device is read only.
780 OUT UINT32
*LastAttemptStatus
785 DEBUG((DEBUG_INFO
, "PlatformUpdate:"));
786 DEBUG((DEBUG_INFO
, " Address - 0x%lx,", Address
));
787 DEBUG((DEBUG_INFO
, " Length - 0x%x\n", ImageSize
));
789 Status
= MicrocodeFlashWrite (
794 if (!EFI_ERROR(Status
)) {
795 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_SUCCESS
;
797 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL
;
803 Update Microcode flash region with FIT.
805 @param[in] MicrocodeFmpPrivate The Microcode driver private data
806 @param[in] TargetMicrocodeEntryPoint Target Microcode entrypoint to be updated
807 @param[in] Image The Microcode image buffer.
808 @param[in] ImageSize The size of Microcode image buffer in bytes.
809 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
811 @retval EFI_SUCCESS The Microcode image is written.
812 @retval EFI_WRITE_PROTECTED The flash device is read only.
815 UpdateMicrocodeFlashRegionWithFit (
816 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
817 IN CPU_MICROCODE_HEADER
*TargetMicrocodeEntryPoint
,
820 OUT UINT32
*LastAttemptStatus
823 VOID
*MicrocodePatchAddress
;
824 UINTN MicrocodePatchRegionSize
;
825 UINTN TargetTotalSize
;
827 VOID
*MicrocodePatchScratchBuffer
;
828 UINT8
*ScratchBufferPtr
;
829 UINTN ScratchBufferSize
;
832 VOID
*NextMicrocodeEntryPoint
;
833 VOID
*EmptyFitMicrocodeEntry
;
834 VOID
*UnusedFitMicrocodeEntry
;
836 DEBUG((DEBUG_INFO
, "UpdateMicrocodeFlashRegionWithFit: Image - 0x%x, size - 0x%x\n", Image
, ImageSize
));
838 MicrocodePatchAddress
= MicrocodeFmpPrivate
->MicrocodePatchAddress
;
839 MicrocodePatchRegionSize
= MicrocodeFmpPrivate
->MicrocodePatchRegionSize
;
841 MicrocodePatchScratchBuffer
= AllocateZeroPool (MicrocodePatchRegionSize
);
842 if (MicrocodePatchScratchBuffer
== NULL
) {
843 DEBUG((DEBUG_ERROR
, "Fail to allocate Microcode Scratch buffer\n"));
844 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
845 return EFI_OUT_OF_RESOURCES
;
847 ScratchBufferPtr
= MicrocodePatchScratchBuffer
;
848 ScratchBufferSize
= 0;
851 // Target data collection
855 if (TargetMicrocodeEntryPoint
!= NULL
) {
856 if (TargetMicrocodeEntryPoint
->DataSize
== 0) {
857 TargetTotalSize
= 2048;
859 TargetTotalSize
= TargetMicrocodeEntryPoint
->TotalSize
;
861 DEBUG((DEBUG_INFO
, " TargetTotalSize - 0x%x\n", TargetTotalSize
));
862 NextMicrocodeEntryPoint
= GetNextFitMicrocode (MicrocodeFmpPrivate
, TargetMicrocodeEntryPoint
);
863 DEBUG((DEBUG_INFO
, " NextMicrocodeEntryPoint - 0x%x\n", NextMicrocodeEntryPoint
));
864 if (NextMicrocodeEntryPoint
!= NULL
) {
865 ASSERT ((UINTN
) NextMicrocodeEntryPoint
>= ((UINTN
) TargetMicrocodeEntryPoint
+ TargetTotalSize
));
866 AvailableSize
= (UINTN
) NextMicrocodeEntryPoint
- (UINTN
) TargetMicrocodeEntryPoint
;
868 AvailableSize
= (UINTN
) MicrocodePatchAddress
+ MicrocodePatchRegionSize
- (UINTN
) TargetMicrocodeEntryPoint
;
870 DEBUG((DEBUG_INFO
, " AvailableSize - 0x%x\n", AvailableSize
));
871 ASSERT (AvailableSize
>= TargetTotalSize
);
874 // Total Size means the Microcode size.
875 // Available Size means the Microcode size plus the pad till (1) next Microcode or (2) the end.
878 // +------+-----------+-----+------+===================+
879 // | MCU1 | Microcode | PAD | MCU2 | Empty |
880 // +------+-----------+-----+------+===================+
882 // |<-AvailableSize->|
885 // +------+-----------+===================+
886 // | MCU | Microcode | Empty |
887 // +------+-----------+===================+
889 // |<- AvailableSize ->|
893 // Update based on policy
897 // 1. If there is enough space to update old one in situ, replace old microcode in situ.
899 if (AvailableSize
>= ImageSize
) {
900 DEBUG((DEBUG_INFO
, "Replace old microcode in situ\n"));
902 // +------+------------+------+===================+
903 // |Other | Old Image | ... | Empty |
904 // +------+------------+------+===================+
906 // +------+---------+--+------+===================+
907 // |Other |New Image|FF| ... | Empty |
908 // +------+---------+--+------+===================+
910 // 1.1. Copy new image
911 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
912 ScratchBufferSize
+= ImageSize
;
913 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
915 RestSize
= AvailableSize
- ImageSize
;
917 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
918 ScratchBufferSize
+= RestSize
;
919 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
921 Status
= UpdateMicrocode((UINTN
)TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
926 // 2. If there is empty FIT microcode entry with enough space, use it.
928 EmptyFitMicrocodeEntry
= FindEmptyFitMicrocode (MicrocodeFmpPrivate
, ImageSize
, &AvailableSize
);
929 if (EmptyFitMicrocodeEntry
!= NULL
) {
930 DEBUG((DEBUG_INFO
, "Use empty FIT microcode entry\n"));
931 // 2.1. Copy new image
932 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
933 ScratchBufferSize
+= ImageSize
;
934 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
936 RestSize
= AvailableSize
- ImageSize
;
938 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
939 ScratchBufferSize
+= RestSize
;
940 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
942 Status
= UpdateMicrocode ((UINTN
) EmptyFitMicrocodeEntry
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
943 if (!EFI_ERROR (Status
) && (TargetMicrocodeEntryPoint
!= NULL
)) {
945 // Empty old microcode.
947 ScratchBufferPtr
= MicrocodePatchScratchBuffer
;
948 SetMem (ScratchBufferPtr
, TargetTotalSize
, 0xFF);
949 ScratchBufferSize
= TargetTotalSize
;
950 ScratchBufferPtr
= (UINT8
*) MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
951 UpdateMicrocode ((UINTN
) TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
957 // 3. If there is unused microcode entry with enough space, use it.
959 UnusedFitMicrocodeEntry
= FindUnusedFitMicrocode (MicrocodeFmpPrivate
, ImageSize
, &AvailableSize
);
960 if (UnusedFitMicrocodeEntry
!= NULL
) {
961 DEBUG((DEBUG_INFO
, "Use unused FIT microcode entry\n"));
962 // 3.1. Copy new image
963 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
964 ScratchBufferSize
+= ImageSize
;
965 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
967 RestSize
= AvailableSize
- ImageSize
;
969 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
970 ScratchBufferSize
+= RestSize
;
971 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
973 Status
= UpdateMicrocode ((UINTN
) UnusedFitMicrocodeEntry
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
974 if (!EFI_ERROR (Status
) && (TargetMicrocodeEntryPoint
!= NULL
)) {
976 // Empty old microcode.
978 ScratchBufferPtr
= MicrocodePatchScratchBuffer
;
979 SetMem (ScratchBufferPtr
, TargetTotalSize
, 0xFF);
980 ScratchBufferSize
= TargetTotalSize
;
981 ScratchBufferPtr
= (UINT8
*) MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
982 UpdateMicrocode ((UINTN
) TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
988 // 4. No usable FIT microcode entry.
990 DEBUG((DEBUG_ERROR
, "No usable FIT microcode entry\n"));
991 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
992 Status
= EFI_OUT_OF_RESOURCES
;
998 Update Microcode flash region.
1000 @param[in] MicrocodeFmpPrivate The Microcode driver private data
1001 @param[in] TargetMicrocodeEntryPoint Target Microcode entrypoint to be updated
1002 @param[in] Image The Microcode image buffer.
1003 @param[in] ImageSize The size of Microcode image buffer in bytes.
1004 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
1006 @retval EFI_SUCCESS The Microcode image is written.
1007 @retval EFI_WRITE_PROTECTED The flash device is read only.
1010 UpdateMicrocodeFlashRegion (
1011 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
1012 IN CPU_MICROCODE_HEADER
*TargetMicrocodeEntryPoint
,
1015 OUT UINT32
*LastAttemptStatus
1018 VOID
*MicrocodePatchAddress
;
1019 UINTN MicrocodePatchRegionSize
;
1020 UINTN TargetTotalSize
;
1021 UINTN UsedRegionSize
;
1023 VOID
*MicrocodePatchScratchBuffer
;
1024 UINT8
*ScratchBufferPtr
;
1025 UINTN ScratchBufferSize
;
1027 UINTN AvailableSize
;
1028 VOID
*NextMicrocodeEntryPoint
;
1029 MICROCODE_INFO
*MicrocodeInfo
;
1030 UINTN MicrocodeCount
;
1033 DEBUG((DEBUG_INFO
, "UpdateMicrocodeFlashRegion: Image - 0x%x, size - 0x%x\n", Image
, ImageSize
));
1035 MicrocodePatchAddress
= MicrocodeFmpPrivate
->MicrocodePatchAddress
;
1036 MicrocodePatchRegionSize
= MicrocodeFmpPrivate
->MicrocodePatchRegionSize
;
1038 MicrocodePatchScratchBuffer
= AllocateZeroPool (MicrocodePatchRegionSize
);
1039 if (MicrocodePatchScratchBuffer
== NULL
) {
1040 DEBUG((DEBUG_ERROR
, "Fail to allocate Microcode Scratch buffer\n"));
1041 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
1042 return EFI_OUT_OF_RESOURCES
;
1044 ScratchBufferPtr
= MicrocodePatchScratchBuffer
;
1045 ScratchBufferSize
= 0;
1048 // Target data collection
1050 TargetTotalSize
= 0;
1052 NextMicrocodeEntryPoint
= NULL
;
1053 if (TargetMicrocodeEntryPoint
!= NULL
) {
1054 if (TargetMicrocodeEntryPoint
->DataSize
== 0) {
1055 TargetTotalSize
= 2048;
1057 TargetTotalSize
= TargetMicrocodeEntryPoint
->TotalSize
;
1059 DEBUG((DEBUG_INFO
, " TargetTotalSize - 0x%x\n", TargetTotalSize
));
1060 NextMicrocodeEntryPoint
= GetNextMicrocode(MicrocodeFmpPrivate
, TargetMicrocodeEntryPoint
);
1061 DEBUG((DEBUG_INFO
, " NextMicrocodeEntryPoint - 0x%x\n", NextMicrocodeEntryPoint
));
1062 if (NextMicrocodeEntryPoint
!= NULL
) {
1063 ASSERT ((UINTN
)NextMicrocodeEntryPoint
>= ((UINTN
)TargetMicrocodeEntryPoint
+ TargetTotalSize
));
1064 AvailableSize
= (UINTN
)NextMicrocodeEntryPoint
- (UINTN
)TargetMicrocodeEntryPoint
;
1066 AvailableSize
= (UINTN
)MicrocodePatchAddress
+ MicrocodePatchRegionSize
- (UINTN
)TargetMicrocodeEntryPoint
;
1068 DEBUG((DEBUG_INFO
, " AvailableSize - 0x%x\n", AvailableSize
));
1069 ASSERT (AvailableSize
>= TargetTotalSize
);
1071 UsedRegionSize
= GetCurrentMicrocodeUsedRegionSize(MicrocodeFmpPrivate
);
1072 DEBUG((DEBUG_INFO
, " UsedRegionSize - 0x%x\n", UsedRegionSize
));
1073 ASSERT (UsedRegionSize
>= TargetTotalSize
);
1074 if (TargetMicrocodeEntryPoint
!= NULL
) {
1075 ASSERT ((UINTN
)MicrocodePatchAddress
+ UsedRegionSize
>= ((UINTN
)TargetMicrocodeEntryPoint
+ TargetTotalSize
));
1078 // Total Size means the Microcode size.
1079 // Available Size means the Microcode size plus the pad till (1) next Microcode or (2) the end.
1082 // +------+-----------+-----+------+===================+
1083 // | MCU1 | Microcode | PAD | MCU2 | Empty |
1084 // +------+-----------+-----+------+===================+
1086 // |<-AvailableSize->|
1087 // |<- UsedRegionSize ->|
1090 // +------+-----------+===================+
1091 // | MCU | Microcode | Empty |
1092 // +------+-----------+===================+
1094 // |<- AvailableSize ->|
1095 // |<-UsedRegionSize->|
1099 // Update based on policy
1103 // 1. If there is enough space to update old one in situ, replace old microcode in situ.
1105 if (AvailableSize
>= ImageSize
) {
1106 DEBUG((DEBUG_INFO
, "Replace old microcode in situ\n"));
1108 // +------+------------+------+===================+
1109 // |Other | Old Image | ... | Empty |
1110 // +------+------------+------+===================+
1112 // +------+---------+--+------+===================+
1113 // |Other |New Image|FF| ... | Empty |
1114 // +------+---------+--+------+===================+
1116 // 1.1. Copy new image
1117 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
1118 ScratchBufferSize
+= ImageSize
;
1119 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1121 RestSize
= AvailableSize
- ImageSize
;
1123 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
1124 ScratchBufferSize
+= RestSize
;
1125 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1127 Status
= UpdateMicrocode((UINTN
)TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
1132 // 2. If there is enough space to remove old one and add new one, reorg and replace old microcode.
1134 if (MicrocodePatchRegionSize
- (UsedRegionSize
- TargetTotalSize
) >= ImageSize
) {
1135 if (TargetMicrocodeEntryPoint
== NULL
) {
1136 DEBUG((DEBUG_INFO
, "Append new microcode\n"));
1138 // +------+------------+------+===================+
1139 // |Other1| Other |Other2| Empty |
1140 // +------+------------+------+===================+
1142 // +------+------------+------+-----------+=======+
1143 // |Other1| Other |Other2| New Image | Empty |
1144 // +------+------------+------+-----------+=======+
1146 Status
= UpdateMicrocode((UINTN
)MicrocodePatchAddress
+ UsedRegionSize
, Image
, ImageSize
, LastAttemptStatus
);
1148 DEBUG((DEBUG_INFO
, "Reorg and replace old microcode\n"));
1150 // +------+------------+------+===================+
1151 // |Other | Old Image | ... | Empty |
1152 // +------+------------+------+===================+
1154 // +------+---------------+------+================+
1155 // |Other | New Image | ... | Empty |
1156 // +------+---------------+------+================+
1158 // 2.1. Copy new image
1159 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
1160 ScratchBufferSize
+= ImageSize
;
1161 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1162 // 2.2. Copy rest images after the old image.
1163 if (NextMicrocodeEntryPoint
!= 0) {
1164 RestSize
= (UINTN
)MicrocodePatchAddress
+ UsedRegionSize
- ((UINTN
)NextMicrocodeEntryPoint
);
1165 CopyMem (ScratchBufferPtr
, NextMicrocodeEntryPoint
, RestSize
);
1166 ScratchBufferSize
+= RestSize
;
1167 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1169 Status
= UpdateMicrocode((UINTN
)TargetMicrocodeEntryPoint
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
1175 // 3. The new image can be put in MCU region, but not all others can be put.
1176 // So all the unused MCU is removed.
1178 if (MicrocodePatchRegionSize
>= ImageSize
) {
1180 // +------+------------+------+===================+
1181 // |Other1| Old Image |Other2| Empty |
1182 // +------+------------+------+===================+
1184 // +-------------------------------------+--------+
1185 // | New Image | Other |
1186 // +-------------------------------------+--------+
1188 DEBUG((DEBUG_INFO
, "Add new microcode from beginning\n"));
1190 MicrocodeCount
= MicrocodeFmpPrivate
->DescriptorCount
;
1191 MicrocodeInfo
= MicrocodeFmpPrivate
->MicrocodeInfo
;
1193 // 3.1. Copy new image
1194 CopyMem (ScratchBufferPtr
, Image
, ImageSize
);
1195 ScratchBufferSize
+= ImageSize
;
1196 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1197 // 3.2. Copy some others to rest buffer
1198 for (Index
= 0; Index
< MicrocodeCount
; Index
++) {
1199 if (!MicrocodeInfo
[Index
].InUse
) {
1202 if (MicrocodeInfo
[Index
].MicrocodeEntryPoint
== TargetMicrocodeEntryPoint
) {
1205 if (MicrocodeInfo
[Index
].TotalSize
<= MicrocodePatchRegionSize
- ScratchBufferSize
) {
1206 CopyMem (ScratchBufferPtr
, MicrocodeInfo
[Index
].MicrocodeEntryPoint
, MicrocodeInfo
[Index
].TotalSize
);
1207 ScratchBufferSize
+= MicrocodeInfo
[Index
].TotalSize
;
1208 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1212 RestSize
= MicrocodePatchRegionSize
- ScratchBufferSize
;
1214 SetMem (ScratchBufferPtr
, RestSize
, 0xFF);
1215 ScratchBufferSize
+= RestSize
;
1216 ScratchBufferPtr
= (UINT8
*)MicrocodePatchScratchBuffer
+ ScratchBufferSize
;
1218 Status
= UpdateMicrocode((UINTN
)MicrocodePatchAddress
, MicrocodePatchScratchBuffer
, ScratchBufferSize
, LastAttemptStatus
);
1223 // 4. The new image size is bigger than the whole MCU region.
1225 DEBUG((DEBUG_ERROR
, "Microcode too big\n"));
1226 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
1227 Status
= EFI_OUT_OF_RESOURCES
;
1235 Caution: This function may receive untrusted input.
1237 @param[in] MicrocodeFmpPrivate The Microcode driver private data
1238 @param[in] Image The Microcode image buffer.
1239 @param[in] ImageSize The size of Microcode image buffer in bytes.
1240 @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
1241 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
1242 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
1243 details for the aborted operation. The buffer is allocated by this function
1244 with AllocatePool(), and it is the caller's responsibility to free it with a
1247 @retval EFI_SUCCESS The Microcode image is written.
1248 @retval EFI_VOLUME_CORRUPTED The Microcode image is corrupted.
1249 @retval EFI_INCOMPATIBLE_VERSION The Microcode image version is incorrect.
1250 @retval EFI_SECURITY_VIOLATION The Microcode image fails to load.
1251 @retval EFI_WRITE_PROTECTED The flash device is read only.
1255 IN MICROCODE_FMP_PRIVATE_DATA
*MicrocodeFmpPrivate
,
1258 OUT UINT32
*LastAttemptVersion
,
1259 OUT UINT32
*LastAttemptStatus
,
1260 OUT CHAR16
**AbortReason
1265 CPU_MICROCODE_HEADER
*TargetMicrocodeEntryPoint
;
1266 UINTN TargetCpuIndex
;
1267 UINTN TargetMicrcodeIndex
;
1270 // MCU must be 16 bytes aligned
1272 AlignedImage
= AllocateCopyPool(ImageSize
, Image
);
1273 if (AlignedImage
== NULL
) {
1274 DEBUG((DEBUG_ERROR
, "Fail to allocate aligned image\n"));
1275 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES
;
1276 return EFI_OUT_OF_RESOURCES
;
1279 TargetCpuIndex
= (UINTN
)-1;
1280 Status
= VerifyMicrocode(MicrocodeFmpPrivate
, AlignedImage
, ImageSize
, TRUE
, LastAttemptStatus
, AbortReason
, &TargetCpuIndex
);
1281 if (EFI_ERROR(Status
)) {
1282 DEBUG((DEBUG_ERROR
, "Fail to verify Microcode Region\n"));
1283 FreePool(AlignedImage
);
1286 DEBUG((DEBUG_INFO
, "Pass VerifyMicrocode\n"));
1287 *LastAttemptVersion
= ((CPU_MICROCODE_HEADER
*)Image
)->UpdateRevision
;
1289 DEBUG((DEBUG_INFO
, " TargetCpuIndex - 0x%x\n", TargetCpuIndex
));
1290 ASSERT (TargetCpuIndex
< MicrocodeFmpPrivate
->ProcessorCount
);
1291 TargetMicrcodeIndex
= MicrocodeFmpPrivate
->ProcessorInfo
[TargetCpuIndex
].MicrocodeIndex
;
1292 DEBUG((DEBUG_INFO
, " TargetMicrcodeIndex - 0x%x\n", TargetMicrcodeIndex
));
1293 if (TargetMicrcodeIndex
!= (UINTN
)-1) {
1294 ASSERT (TargetMicrcodeIndex
< MicrocodeFmpPrivate
->DescriptorCount
);
1295 TargetMicrocodeEntryPoint
= MicrocodeFmpPrivate
->MicrocodeInfo
[TargetMicrcodeIndex
].MicrocodeEntryPoint
;
1297 TargetMicrocodeEntryPoint
= NULL
;
1299 DEBUG((DEBUG_INFO
, " TargetMicrocodeEntryPoint - 0x%x\n", TargetMicrocodeEntryPoint
));
1301 if (MicrocodeFmpPrivate
->FitMicrocodeInfo
!= NULL
) {
1302 Status
= UpdateMicrocodeFlashRegionWithFit (
1303 MicrocodeFmpPrivate
,
1304 TargetMicrocodeEntryPoint
,
1310 Status
= UpdateMicrocodeFlashRegion (
1311 MicrocodeFmpPrivate
,
1312 TargetMicrocodeEntryPoint
,
1319 FreePool(AlignedImage
);