// // env_windowsBootLib.c // Platform-specific code for windows boot library environment. // // Copyright (c) Microsoft Corporation. Licensed under the MIT license. // #pragma warning(push) #pragma warning(disable: 5103) // Arm64's wdm.h included below currently generate a lot of 5103 warnings #include #pragma warning(pop) #include #include "symcrypt.h" #include "sc_lib.h" // // The BlStatusError function, part of the bootlib, is normally defined in blstatus.h // We can't include that header file from outside the onecore codebase. // We copied the definition here. // VOID BlStatusError ( __in ULONG ErrorCode, __in ULONG_PTR ErrorParameter1, __in ULONG_PTR ErrorParameter2, __in ULONG_PTR ErrorParameter3, __in ULONG_PTR ErrorParameter4 ); SYMCRYPT_CPU_FEATURES SYMCRYPT_CALL SymCryptCpuFeaturesNeverPresentEnvWindowsBootlibrary() { // We explicitly opt-out of using CPU features besides those related to AES, SHA-2 and // RDRAND/RDSEED. This ensures that new features requiring new state will not be used // unintentionally, and reduces code size for the boot library environment, which is // size-constrained on some devices. #if SYMCRYPT_CPU_AMD64 | SYMCRYPT_CPU_X86 return (SYMCRYPT_CPU_FEATURES) ~(SYMCRYPT_CPU_FEATURES_FOR_AESNI_PCLMULQDQ_CODE | SYMCRYPT_CPU_FEATURES_FOR_SHANI_CODE | SYMCRYPT_CPU_FEATURE_RDRAND | SYMCRYPT_CPU_FEATURE_RDSEED); #elif SYMCRYPT_CPU_ARM64 | SYMCRYPT_CPU_ARM return (SYMCRYPT_CPU_FEATURES) ~(SYMCRYPT_CPU_FEATURE_NEON | SYMCRYPT_CPU_FEATURE_NEON_AES | SYMCRYPT_CPU_FEATURE_NEON_PMULL | SYMCRYPT_CPU_FEATURE_NEON_SHA256); #endif } // // Bootlib code calls the init multiple times, and it gets inlined too often. // SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptInitEnvWindowsBootlibrary( UINT32 version ) { if( g_SymCryptFlags & SYMCRYPT_FLAG_LIB_INITIALIZED ) { return; } #if SYMCRYPT_CPU_X86 | SYMCRYPT_CPU_AMD64 SymCryptDetectCpuFeaturesByCpuid( SYMCRYPT_CPUID_DETECT_FLAG_CHECK_OS_SUPPORT_FOR_YMM ); // // Our SaveXmm functions never fail because they don't do anything. // g_SymCryptCpuFeaturesNotPresent &= ~SYMCRYPT_CPU_FEATURE_SAVEXMM_NOFAIL; #elif SYMCRYPT_CPU_ARM // We run in a CPU mode that allows us to read the configuration registers SymCryptDetectCpuFeaturesFromRegisters(); #elif SYMCRYPT_CPU_ARM64 // We run in a CPU mode that allows us to read the configuration registers SymCryptDetectCpuFeaturesFromRegistersNoTry(); #endif SymCryptInitEnvCommon( version ); } _Analysis_noreturn_ VOID SYMCRYPT_CALL SymCryptFatalEnvWindowsBootlibrary( UINT32 fatalCode ) { UINT32 fatalCodeVar; SymCryptFatalIntercept( fatalCode ); // // Put the fatal code in a location where it shows up in the dump // SYMCRYPT_FORCE_WRITE32( &fatalCodeVar, fatalCode ); // // First we try to report the error using the Boot library infrastructure // BlStatusError ( fatalCode, 0, 0, 0, 0 ); // // Then try a breakpoint; maybe that will work // __debugbreak(); SymCryptFatalHang( fatalCode ); } #if SYMCRYPT_CPU_X86 | SYMCRYPT_CPU_AMD64 SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSaveXmmEnvWindowsBootlibrary( _Out_ PSYMCRYPT_EXTENDED_SAVE_DATA pSaveArea ) { // // In Bootlibrary there is no need to save XMM registers. // The compiler should inline this function and optimize it away. // UNREFERENCED_PARAMETER( pSaveArea ); return SYMCRYPT_NO_ERROR; } VOID SYMCRYPT_CALL SymCryptRestoreXmmEnvWindowsBootlibrary( _Inout_ PSYMCRYPT_EXTENDED_SAVE_DATA pSaveArea ) { // // In Bootlibrary there is no need to save XMM registers. // The compiler should inline this function and optimize it away. // UNREFERENCED_PARAMETER( pSaveArea ); } SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSaveYmmEnvWindowsBootlibrary( _Out_ PSYMCRYPT_EXTENDED_SAVE_DATA pSaveArea ) { // // In Bootlibrary there is no need to save YMM registers. // The compiler should inline this function and optimize it away. // UNREFERENCED_PARAMETER( pSaveArea ); return SYMCRYPT_NO_ERROR; } VOID SYMCRYPT_CALL SymCryptRestoreYmmEnvWindowsBootlibrary( _Inout_ PSYMCRYPT_EXTENDED_SAVE_DATA pSaveArea ) { // // In Bootlibrary there is no need to save YMM registers. // The compiler should inline this function and optimize it away. // UNREFERENCED_PARAMETER( pSaveArea ); } SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptSaveZmmEnvWindowsBootlibrary( _Out_ PSYMCRYPT_EXTENDED_SAVE_DATA pSaveArea ) { UNREFERENCED_PARAMETER( pSaveArea ); SymCryptFatal( 'mmzs' ); return SYMCRYPT_NOT_IMPLEMENTED; } VOID SYMCRYPT_CALL SymCryptRestoreZmmEnvWindowsBootlibrary( _Inout_ PSYMCRYPT_EXTENDED_SAVE_DATA pSaveArea ) { SymCryptFatal( 'mmzs' ); UNREFERENCED_PARAMETER( pSaveArea ); } #endif VOID SYMCRYPT_CALL SymCryptTestInjectErrorEnvWindowsBootlibrary( PBYTE pbBuf, SIZE_T cbBuf ) { // // This feature is only used during testing. In production it is always // an empty function that the compiler can optimize away. // UNREFERENCED_PARAMETER( pbBuf ); UNREFERENCED_PARAMETER( cbBuf ); } #if SYMCRYPT_CPU_AMD64 | SYMCRYPT_CPU_X86 VOID SYMCRYPT_CALL SymCryptCpuidExFuncEnvWindowsBootlibrary( int cpuInfo[4], int function_id, int subfunction_id ) { __cpuidex( cpuInfo, function_id, subfunction_id ); } #endif