在 Setup 中有一个显示当前加载的CPU Micro Code 的版本的选项。比如,在 EDK2 的代码中有如下片段:
\Vlv2TbltDevicePkg\PlatformSetupDxe\Main.vfi
text
help = STRING_TOKEN(STR_NULL_STRING),
text = STRING_TOKEN(STR_PROCESSOR_MICROCODE_STRING),
text = STRING_TOKEN(STR_PROCESSOR_MICROCODE_VALUE),
flags = 0,
key = 0;
对应实现的代码: \Vlv2TbltDevicePkg\PlatformSetupDxe\SetupInfoRecords.c
//
// Microcode Revision
//
EfiWriteMsr (EFI_MSR_IA32_BIOS_SIGN_ID, 0);
EfiCpuid (EFI_CPUID_VERSION_INFO, NULL);
MicroCodeVersion = (UINT32) RShiftU64 (EfiReadMsr (EFI_MSR_IA32_BIOS_SIGN_ID), 32);
UnicodeSPrint (Buffer, sizeof (Buffer), L"%x", MicroCodeVersion);
HiiSetString(mHiiHandle,STRING_TOKEN(STR_PROCESSOR_MICROCODE_VALUE), Buffer, NULL);
在 “Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 4: Model-Specific Registers”中有描述如下:

从上面的 DataSheet看不明显,这个MSR寄存器的高32Bits 就是当前 Microcode 的 Version。对此,编写一个 Application如下:
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/UefiLib.h>
#include <Library/ShellCEntryLib.h>
extern EFI_SYSTEM_TABLE *gST;
extern EFI_BOOT_SERVICES *gBS;
//Define in \Vlv2TbltDevicePkg\Include\Library\CpuIA32.h
#define EFI_MSR_IA32_BIOS_SIGN_ID 0x8B
INTN
EFIAPI
ShellAppMain (
IN UINTN Argc,
IN CHAR16 **Argv
)
{
UINT32 MicroCodeVersion;
MicroCodeVersion =
(UINT32) RShiftU64 (AsmReadMsr64 (EFI_MSR_IA32_BIOS_SIGN_ID), 32);
Print(L"Microcode Resision [%X]\n",MicroCodeVersion);
return(0);
}
运行之后显示的版本信息和 Setup 中的 MicroCode 版本信息相同(KBL-R HDK 平台)。
这里使用的AsmReadMsr64在 \MdePkg\Include\Library\BaseLib.h 有定义,可以用来读取 64Bits的MSR:
/**
Returns a 64-bit Machine Specific Register(MSR).
Reads and returns the 64-bit MSR specified by Index. No parameter checking is
performed on Index, and some Index values may cause CPU exceptions. The
caller must either guarantee that Index is valid, or the caller must set up
exception handlers to catch the exceptions. This function is only available
on IA-32 and x64.
@param Index The 32-bit MSR index to read.
@return The value of the MSR identified by Index.
**/
UINT64
EFIAPI
AsmReadMsr64 (
IN UINT32 Index
);
完整的代码和 EFI 下载:
参考:
1. https://github.com/theChiChen/UEFI_SHELL_Utilities/tree/master/ChiChenPkg/Application/MicrocodeVersion
2021年8月13日 经过测试,在 ADL-P 平台上该工具工作正常,显示结果和 Setup 中一致。