diff options
-rw-r--r-- | src/Common/BootEncryption.cpp | 26 |
1 files changed, 6 insertions, 20 deletions
diff --git a/src/Common/BootEncryption.cpp b/src/Common/BootEncryption.cpp index bf7a0f64..f79e7339 100644 --- a/src/Common/BootEncryption.cpp +++ b/src/Common/BootEncryption.cpp @@ -2624,139 +2624,125 @@ namespace VeraCrypt bDeviceInfoValid = dev.IoCtl(IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn)) && dev.IoCtl(IOCTL_DISK_GET_PARTITION_INFO_EX, NULL, 0, &partInfo, sizeof(partInfo)); DWORD dwLastError = GetLastError (); dev.Close(); if (!bDeviceInfoValid && !bDisableException) { SetLastError (dwLastError); throw SystemException(SRC_POS); } } } bool EfiBoot::IsEfiBoot() { DWORD BootOrderLen; BootOrderLen = GetFirmwareEnvironmentVariable(L"BootOrder", EfiVarGuid, tempBuf, sizeof(tempBuf)); return (BootOrderLen != 0) || (GetLastError() != ERROR_INVALID_FUNCTION); } void EfiBoot::DeleteStartExec(uint16 statrtOrderNum, wchar_t* type) { DWORD dwLastError; BOOL bPrivilegesSet = IsPrivilegeEnabled (SE_SYSTEM_ENVIRONMENT_NAME); if (!bPrivilegesSet && !SetPrivilege(SE_SYSTEM_ENVIRONMENT_NAME, TRUE)) { dwLastError = GetLastError(); wchar_t szMsg[128]; StringCchPrintfW(szMsg, ARRAYSIZE(szMsg), L"Failed to set SE_SYSTEM_ENVIRONMENT_NAME privilege (error code 0x%.8X)", dwLastError); throw ErrorException(szMsg, SRC_POS); } // Check EFI if (!IsEfiBoot()) { - dwLastError = GetLastError(); - if (dwLastError != ERROR_SUCCESS) - { - if (!bPrivilegesSet) - SetPrivilege(SE_SYSTEM_ENVIRONMENT_NAME, FALSE); - // format message to append the error code to the exception message - wchar_t szMsg[128]; - StringCchPrintfW(szMsg, ARRAYSIZE(szMsg), L"Failed to detect EFI environment (error code 0x%.8X)", dwLastError); - throw ErrorException(szMsg, SRC_POS); - } + if (!bPrivilegesSet) + SetPrivilege(SE_SYSTEM_ENVIRONMENT_NAME, FALSE); + throw ErrorException(L"Failed to detect EFI environment (error ERROR_INVALID_FUNCTION)", SRC_POS); } wchar_t varName[256]; StringCchPrintfW(varName, ARRAYSIZE (varName), L"%s%04X", type == NULL ? L"Boot" : type, statrtOrderNum); SetFirmwareEnvironmentVariable(varName, EfiVarGuid, NULL, 0); wstring order = L"Order"; order.insert(0, type == NULL ? L"Boot" : type); uint32 startOrderLen = GetFirmwareEnvironmentVariable(order.c_str(), EfiVarGuid, tempBuf, sizeof(tempBuf)); uint32 startOrderNumPos = UINT_MAX; bool startOrderUpdate = false; uint16* startOrder = (uint16*)tempBuf; for (uint32 i = 0; i < startOrderLen / 2; i++) { if (startOrder[i] == statrtOrderNum) { startOrderNumPos = i; break; } } // delete entry if present if (startOrderNumPos != UINT_MAX) { for (uint32 i = startOrderNumPos; i < ((startOrderLen / 2) - 1); ++i) { startOrder[i] = startOrder[i + 1]; } startOrderLen -= 2; startOrderUpdate = true; } if (startOrderUpdate) { SetFirmwareEnvironmentVariable(order.c_str(), EfiVarGuid, startOrder, startOrderLen); // remove ourselves from BootNext value uint16 bootNextValue = 0; wstring next = L"Next"; next.insert(0, type == NULL ? L"Boot" : type); if ( (GetFirmwareEnvironmentVariable(next.c_str(), EfiVarGuid, &bootNextValue, 2) == 2) && (bootNextValue == statrtOrderNum) ) { SetFirmwareEnvironmentVariable(next.c_str(), EfiVarGuid, startOrder, 0); } } if (!bPrivilegesSet) SetPrivilege(SE_SYSTEM_ENVIRONMENT_NAME, FALSE); } void EfiBoot::SetStartExec(wstring description, wstring execPath, bool setBootEntry, bool forceFirstBootEntry, bool setBootNext, uint16 statrtOrderNum , wchar_t* type, uint32 attr) { DWORD dwLastError; BOOL bPrivilegesSet = IsPrivilegeEnabled (SE_SYSTEM_ENVIRONMENT_NAME); if (!bPrivilegesSet && !SetPrivilege(SE_SYSTEM_ENVIRONMENT_NAME, TRUE)) { dwLastError = GetLastError(); wchar_t szMsg[128]; StringCchPrintfW(szMsg, ARRAYSIZE(szMsg), L"Failed to set SE_SYSTEM_ENVIRONMENT_NAME privilege (error code 0x%.8X)", dwLastError); throw ErrorException(szMsg, SRC_POS); } // Check EFI if (!IsEfiBoot()) { - dwLastError = GetLastError(); - if (dwLastError != ERROR_SUCCESS) - { - if (!bPrivilegesSet) - SetPrivilege(SE_SYSTEM_ENVIRONMENT_NAME, FALSE); - // format message to append the error code to the exception message - wchar_t szMsg[1024]; - StringCchPrintfW(szMsg, ARRAYSIZE(szMsg), L"Failed to detect EFI environment (error code 0x%.8X)", dwLastError); - throw ErrorException(szMsg, SRC_POS); - } + if (!bPrivilegesSet) + SetPrivilege(SE_SYSTEM_ENVIRONMENT_NAME, FALSE); + throw ErrorException(L"Failed to detect EFI environment (error ERROR_INVALID_FUNCTION)", SRC_POS); } if (bDeviceInfoValid) { uint32 varSize = 56; varSize += ((uint32) description.length()) * 2 + 2; varSize += ((uint32) execPath.length()) * 2 + 2; uint8 *startVar = new uint8[varSize]; uint8 *pVar = startVar; // Attributes (1b Active, 1000b - Hidden) *(uint32 *)pVar = attr; pVar += sizeof(uint32); // Size Of device path + file path *(uint16 *)pVar = (uint16)(50 + execPath.length() * 2 + 2); pVar += sizeof(uint16); // description for (uint32 i = 0; i < description.length(); i++) { *(uint16 *)pVar = description[i]; pVar += sizeof(uint16); } *(uint16 *)pVar = 0; pVar += sizeof(uint16); /* EFI_DEVICE_PATH_PROTOCOL (HARDDRIVE_DEVICE_PATH \ FILE_PATH \ END) */ // Type *(uint8 *)pVar = 0x04; |