/** @file
CrystalRidge PEIM - Nonce Restoration via SW SMI
This PEIM registers an EndOfPei notification callback that generates
a Software SMI (SW SMI 0xFB via IO port 0xB2) to restore the
CrystalRidge nonce on production systems. The nonce is a one-time-use
value used during manufacturing/debug to authenticate access to
restricted hardware resources.
Source: e:\hs\PurleySktPkg\Pei\CrystalRidge\CrystalRidgePeim.c
Module: CrystalRidgePeim.efi (0426)
SHA256: 90d69f9f61a3b0d7758101a4dba3d247d012b6a5ecf171d86d8f47b9d6b5739e
Copyright (c) Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "CrystalRidgePeim.h"
//
// CrystalRidge PPI GUID
// {36232936-0E76-31C8-A13A-3AF2FC1C3932}
//
EFI_GUID gCrystalRidgeGuid = CRYSTAL_RIDGE_PPI_GUID;
//
// EndOfPei notification descriptor for CrystalRidge nonce restoration
//
EFI_PEI_NOTIFY_DESCRIPTOR gCrystalRidgeNotifyDescriptor = {
(EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
&gCrystalRidgeGuid,
GenerateSwSmiForNonceRestore
};
/**
PEIM entry point.
Registers an EndOfPei notification for CrystalRidge nonce restoration.
On production boot mode (17 = BOOT_IN_RECOVERY_FULL), the notify descriptor
is registered. If registration fails, an ASSERT is raised via the
CrystalRidge DebugLib PPI.
@param[in] FileHandle PEIM file handle.
@param[in] PeiServices General purpose PEI services table.
@retval EFI_SUCCESS Notification registered successfully or skipped.
@retval EFI_INVALID_PARAMETER Notification registration failed.
**/
EFI_STATUS
EFIAPI
CrystalRidgePeimEntry (
IN EFI_PEI_FILE_HANDLE FileHandle,
IN CONST EFI_PEI_SERVICES **PeiServices
)
{
EFI_STATUS Status;
EFI_BOOT_MODE BootMode;
CRYSTAL_RIDGE_PPI *CrystalRidgePpi;
Status = EFI_SUCCESS;
DEBUG ((DEBUG_INFO, "[CR] (PEI) Register EndOfPei Notify for Nonce restoration\n"));
PeiServices = GetPeiServicesTable ();
(*PeiServices)->GetBootMode (PeiServices, &BootMode);
if (BootMode == BOOT_IN_RECOVERY_FULL) {
Status = (*PeiServices)->NotifyPpi (PeiServices, &gCrystalRidgeNotifyDescriptor);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "\nASSERT_EFI_ERROR (Status = %r)\n", Status));
CrystalRidgePpi = LocateCrystalRidgePpi ();
if (CrystalRidgePpi != NULL) {
CrystalRidgePpi->DebugAssert (
"e:\\hs\\PurleySktPkg\\Pei\\CrystalRidge\\CrystalRidgePeim.c",
109,
"!EFI_ERROR (Status)"
);
}
}
}
return Status;
}
/**
EndOfPei callback: Generates SW SMI 0xFB via IO port 0xB2 to restore nonce.
This function is called when EndOfPei is signaled. It prints a debug message
then triggers Software SMI 0xFB by writing to IO port 0xB2. The SMI handler
in SMM performs the actual CrystalRidge nonce restoration.
@param[in] PeiServices General purpose PEI services table.
@param[in] NotifyDescriptor The notification descriptor that triggered this callback.
@param[in] Ppi Pointer to the PPI that was installed.
@retval EFI_SUCCESS SW SMI was triggered successfully.
**/
EFI_STATUS
EFIAPI
GenerateSwSmiForNonceRestore (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
IN VOID *Ppi
)
{
DEBUG ((DEBUG_INFO, "[CR] (PEI) Generating SW SMI for Nonce restoration\n"));
//
// Trigger SW SMI 0xFB to restore CrystalRidge nonce in SMM
//
IoWrite8 (0xB2, 0xFB);
return EFI_SUCCESS;
}
/**
Locates the CrystalRidge PPI in the PEI database.
@return Pointer to the CRYSTAL_RIDGE_PPI structure, or NULL if not found.
**/
CRYSTAL_RIDGE_PPI *
LocateCrystalRidgePpi (
VOID
)
{
EFI_STATUS Status;
CRYSTAL_RIDGE_PPI *CrystalRidgePpi;
EFI_PEI_SERVICES **PeiServices;
PeiServices = GetPeiServicesTable ();
Status = (*PeiServices)->LocatePpi (
PeiServices,
&gCrystalRidgeGuid,
0,
NULL,
(VOID **)&CrystalRidgePpi
);
if (EFI_ERROR (Status)) {
return NULL;
}
return CrystalRidgePpi;
}
/**
Debug print via CrystalRidge DebugLib PPI.
Locates the CrystalRidge PPI and calls its DebugPrint function if the
error level matches the current manufacturing/debug mode.
@param[in] ErrorLevel Debug error level mask.
@param[in] Format Format string.
@param[in] ... Variable arguments for format string.
@return The error level if printing was suppressed, or the value
returned by the PPI's DebugPrint function.
**/
UINTN
CrystalRidgeDebugPrint (
IN UINTN ErrorLevel,
IN CONST CHAR8 *Format,
...
)
{
UINTN Result;
CRYSTAL_RIDGE_PPI *CrystalRidgePpi;
VA_LIST VaList;
VA_START (VaList, Format);
CrystalRidgePpi = LocateCrystalRidgePpi ();
if (CrystalRidgePpi != NULL) {
Result = IsManufacturingMode ();
if ((Result & ErrorLevel) != 0) {
return CrystalRidgePpi->DebugPrint (ErrorLevel, Format, VaList);
}
}
VA_END (VaList);
return Result;
}
/**
Debug assertion handler via CrystalRidge DebugLib PPI.
Locates the CrystalRidge PPI and calls its DebugAssert function.
@param[in] FileName File name where the assertion failed.
@param[in] LineNumber Line number where the assertion failed.
@param[in] Description Assertion condition description.
**/
VOID
CrystalRidgeDebugAssert (
IN CONST CHAR8 *FileName,
IN UINTN LineNumber,
IN CONST CHAR8 *Description
)
{
CRYSTAL_RIDGE_PPI *CrystalRidgePpi;
CrystalRidgePpi = LocateCrystalRidgePpi ();
if (CrystalRidgePpi != NULL) {
CrystalRidgePpi->DebugAssert (FileName, LineNumber, Description);
}
}
/**
Checks if system is in manufacturing/debug mode.
Reads CMOS register 0x4A (via IO ports 0x70/0x71) to detect the
manufacturing/debug mode. On some platforms there is a memory-mapped
I/O override at 0xFDAF0490.
@retval 0 Manufacturing mode.
@retval 0x80000004 Debug mode.
@retval 0x80000046 Unknown/other mode.
@retval 0 Error reading CMOS (-1 sentinel), treated as manufacturing.
**/
UINTN
IsManufacturingMode (
VOID
)
{
UINT8 CmosValue;
//
// Read CMOS register 0x4A
//
IoWrite8 (0x70, (IoRead8 (0x70) & 0x80) | 0x4A);
CmosValue = IoRead8 (0x71);
//
// Evaluate CMOS value
//
if ((UINT8)CmosValue <= 3) {
//
// Small values (0-3): evaluate directly
//
if (CmosValue == 0) {
//
// Check hardware override at 0xFDAF0490
//
CmosValue = (MmioRead8 (0xFDAF0490) & 2) | 1;
if (CmosValue == 0) {
return 0; // Manufacturing mode
}
} else {
if (CmosValue == 0) {
return 0; // Manufacturing mode
}
}
goto CheckMode;
}
if (CmosValue == 0) {
CmosValue = (MmioRead8 (0xFDAF0490) & 2) | 1;
if (CmosValue == 0) {
return 0; // Manufacturing mode
}
}
CheckMode:
if (CmosValue == (UINT8)-1) {
return 0; // Error reading CMOS
}
if (CmosValue == 1) {
return 0x80000004; // Debug mode
}
return 0x80000046; // Unknown mode
}
/**
Retrieves the PEI Services Table pointer.
Reads the IDTR to locate the PEI Services Table pointer, which is stored
just below the IDT base address in memory.
@return Pointer to the PEI Services Table.
**/
EFI_PEI_SERVICES **
GetPeiServicesTable (
VOID
)
{
IA32_DESCRIPTOR Idtr;
EFI_PEI_SERVICES **PeiServices;
AsmReadIdtr (&Idtr);
PeiServices = *(EFI_PEI_SERVICES ***)(Idtr.Base - 4);
if (PeiServices == NULL) {
CrystalRidgeDebugAssert (
"PeiServices != ((void *) 0)",
0,
(CONST CHAR8 *)&Idtr
);
}
return PeiServices;
}
//
// Override PeiServicesTablePointerLibIdt's ReadIdtr with inline asm
// to avoid the library dependency. This reads the full 6-byte IDTR.
//
VOID
EFIAPI
AsmReadIdtr (
OUT IA32_DESCRIPTOR *Idtr
)
{
__asm {
pushfd
cli
sidt Idtr
popfd
}
}