Windows: code refactoring and convert NTSTATUS error code to WIN32 equivalent before displaying error message.
This commit is contained in:
parent
89e2547851
commit
2722b46530
|
@ -2478,9 +2478,10 @@ namespace VeraCrypt
|
||||||
static const wchar_t* EfiVarGuid = L"{8BE4DF61-93CA-11D2-AA0D-00E098032B8C}";
|
static const wchar_t* EfiVarGuid = L"{8BE4DF61-93CA-11D2-AA0D-00E098032B8C}";
|
||||||
|
|
||||||
void
|
void
|
||||||
GetVolumeESP(wstring& path)
|
GetVolumeESP(wstring& path, wstring& bootVolumePath)
|
||||||
{
|
{
|
||||||
static wstring g_EspPath;
|
static wstring g_EspPath;
|
||||||
|
static wstring g_BootVolumePath;
|
||||||
static bool g_EspPathInitialized = false;
|
static bool g_EspPathInitialized = false;
|
||||||
|
|
||||||
if (!g_EspPathInitialized)
|
if (!g_EspPathInitialized)
|
||||||
|
@ -2501,17 +2502,29 @@ namespace VeraCrypt
|
||||||
res = NtQuerySystemInformationPtr((SYSTEM_INFORMATION_CLASS)SYSPARTITIONINFORMATION, tempBuf, sizeof(tempBuf), &len);
|
res = NtQuerySystemInformationPtr((SYSTEM_INFORMATION_CLASS)SYSPARTITIONINFORMATION, tempBuf, sizeof(tempBuf), &len);
|
||||||
if (res != S_OK)
|
if (res != S_OK)
|
||||||
{
|
{
|
||||||
|
/* try to convert the returned NTSTATUS to a WIN32 system error using RtlNtStatusToDosError */
|
||||||
|
RtlNtStatusToDosErrorFn RtlNtStatusToDosErrorPtr = (RtlNtStatusToDosErrorFn) GetProcAddress (GetModuleHandle (L"ntdll.dll"), "RtlNtStatusToDosError");
|
||||||
|
if (RtlNtStatusToDosErrorPtr)
|
||||||
|
{
|
||||||
|
ULONG win32err = RtlNtStatusToDosErrorPtr (res);
|
||||||
|
if (win32err != ERROR_MR_MID_NOT_FOUND)
|
||||||
|
res = (NTSTATUS) win32err;
|
||||||
|
}
|
||||||
|
|
||||||
SetLastError (res);
|
SetLastError (res);
|
||||||
throw SystemException (SRC_POS);
|
throw SystemException (SRC_POS);
|
||||||
}
|
}
|
||||||
|
|
||||||
PUNICODE_STRING pStr = (PUNICODE_STRING) tempBuf;
|
PUNICODE_STRING pStr = (PUNICODE_STRING) tempBuf;
|
||||||
|
|
||||||
|
g_BootVolumePath = pStr->Buffer;
|
||||||
g_EspPath = L"\\\\?";
|
g_EspPath = L"\\\\?";
|
||||||
g_EspPath += &pStr->Buffer[7];
|
g_EspPath += &pStr->Buffer[7];
|
||||||
g_EspPathInitialized = true;
|
g_EspPathInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
path = g_EspPath;
|
path = g_EspPath;
|
||||||
|
bootVolumePath = g_BootVolumePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ReadESPFile (LPCWSTR szFilePath, bool bSkipUTF8BOM)
|
std::string ReadESPFile (LPCWSTR szFilePath, bool bSkipUTF8BOM)
|
||||||
|
@ -2521,9 +2534,9 @@ namespace VeraCrypt
|
||||||
|
|
||||||
ByteArray fileContent;
|
ByteArray fileContent;
|
||||||
DWORD dwSize = 0, dwOffset = 0;
|
DWORD dwSize = 0, dwOffset = 0;
|
||||||
std::wstring pathESP;
|
std::wstring pathESP, bootVolumePath;
|
||||||
|
|
||||||
GetVolumeESP(pathESP);
|
GetVolumeESP(pathESP, bootVolumePath);
|
||||||
if (szFilePath[0] != L'\\')
|
if (szFilePath[0] != L'\\')
|
||||||
pathESP += L"\\";
|
pathESP += L"\\";
|
||||||
File f(pathESP + szFilePath, true);
|
File f(pathESP + szFilePath, true);
|
||||||
|
@ -2552,7 +2565,7 @@ namespace VeraCrypt
|
||||||
|
|
||||||
ByteArray fileContent;
|
ByteArray fileContent;
|
||||||
DWORD dwSize = dwDataLen, dwOffset = 0;
|
DWORD dwSize = dwDataLen, dwOffset = 0;
|
||||||
std::wstring pathESP;
|
std::wstring pathESP, bootVolumePath;
|
||||||
|
|
||||||
if (bAddUTF8BOM)
|
if (bAddUTF8BOM)
|
||||||
{
|
{
|
||||||
|
@ -2560,7 +2573,7 @@ namespace VeraCrypt
|
||||||
dwOffset = 3;
|
dwOffset = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetVolumeESP(pathESP);
|
GetVolumeESP(pathESP, bootVolumePath);
|
||||||
if (szFilePath[0] != L'\\')
|
if (szFilePath[0] != L'\\')
|
||||||
pathESP += L"\\";
|
pathESP += L"\\";
|
||||||
|
|
||||||
|
@ -2580,42 +2593,12 @@ namespace VeraCrypt
|
||||||
ZeroMemory (&partInfo, sizeof (partInfo));
|
ZeroMemory (&partInfo, sizeof (partInfo));
|
||||||
m_bMounted = false;
|
m_bMounted = false;
|
||||||
bDeviceInfoValid = false;
|
bDeviceInfoValid = false;
|
||||||
bBootVolumePathSelected = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void EfiBoot::SelectBootVolumeESP() {
|
|
||||||
NTSTATUS res;
|
|
||||||
ULONG len;
|
|
||||||
memset(tempBuf, 0, sizeof(tempBuf));
|
|
||||||
|
|
||||||
// Load NtQuerySystemInformation function point
|
|
||||||
if (!NtQuerySystemInformationPtr)
|
|
||||||
{
|
|
||||||
NtQuerySystemInformationPtr = (NtQuerySystemInformationFn) GetProcAddress (GetModuleHandle (L"ntdll.dll"), "NtQuerySystemInformation");
|
|
||||||
if (!NtQuerySystemInformationPtr)
|
|
||||||
throw SystemException (SRC_POS);
|
|
||||||
}
|
|
||||||
|
|
||||||
res = NtQuerySystemInformationPtr((SYSTEM_INFORMATION_CLASS)SYSPARTITIONINFORMATION, tempBuf, sizeof(tempBuf), &len);
|
|
||||||
if (res != S_OK)
|
|
||||||
{
|
|
||||||
SetLastError (res);
|
|
||||||
throw SystemException (SRC_POS);
|
|
||||||
}
|
|
||||||
|
|
||||||
PUNICODE_STRING pStr = (PUNICODE_STRING) tempBuf;
|
|
||||||
BootVolumePath = pStr->Buffer;
|
|
||||||
|
|
||||||
EfiBootPartPath = L"\\\\?";
|
|
||||||
EfiBootPartPath += &pStr->Buffer[7];
|
|
||||||
|
|
||||||
bBootVolumePathSelected = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EfiBoot::PrepareBootPartition(bool bDisableException) {
|
void EfiBoot::PrepareBootPartition(bool bDisableException) {
|
||||||
if (!bBootVolumePathSelected) {
|
|
||||||
SelectBootVolumeESP();
|
GetVolumeESP (EfiBootPartPath, BootVolumePath);
|
||||||
}
|
|
||||||
std::wstring devicePath = L"\\\\?\\GLOBALROOT";
|
std::wstring devicePath = L"\\\\?\\GLOBALROOT";
|
||||||
devicePath += BootVolumePath;
|
devicePath += BootVolumePath;
|
||||||
Device dev(devicePath.c_str(), TRUE);
|
Device dev(devicePath.c_str(), TRUE);
|
||||||
|
|
|
@ -29,6 +29,10 @@ typedef NTSTATUS (WINAPI *NtQuerySystemInformationFn)(
|
||||||
PULONG ReturnLength
|
PULONG ReturnLength
|
||||||
);
|
);
|
||||||
|
|
||||||
|
typedef ULONG (WINAPI *RtlNtStatusToDosErrorFn)(
|
||||||
|
NTSTATUS Status
|
||||||
|
);
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace VeraCrypt
|
namespace VeraCrypt
|
||||||
|
@ -193,7 +197,7 @@ namespace VeraCrypt
|
||||||
static BOOL IsPostExecFileField (const string& szFieldValue, wstring& filePath);
|
static BOOL IsPostExecFileField (const string& szFieldValue, wstring& filePath);
|
||||||
};
|
};
|
||||||
|
|
||||||
void GetVolumeESP(wstring& path);
|
void GetVolumeESP(wstring& path, wstring& bootVolumePath);
|
||||||
std::string ReadESPFile (LPCWSTR szFilePath, bool bSkipUTF8BOM);
|
std::string ReadESPFile (LPCWSTR szFilePath, bool bSkipUTF8BOM);
|
||||||
void WriteESPFile (LPCWSTR szFilePath, LPBYTE pbData, DWORD dwDataLen, bool bAddUTF8BOM);
|
void WriteESPFile (LPCWSTR szFilePath, LPBYTE pbData, DWORD dwDataLen, bool bAddUTF8BOM);
|
||||||
|
|
||||||
|
@ -221,7 +225,6 @@ namespace VeraCrypt
|
||||||
BOOL UpdateConfig (const wchar_t* name, int pim, int hashAlgo, HWND hwndDlg);
|
BOOL UpdateConfig (const wchar_t* name, int pim, int hashAlgo, HWND hwndDlg);
|
||||||
BOOL WriteConfig (const wchar_t* name, bool preserveUserConfig, int pim, int hashAlgo, const char* passPromptMsg, HWND hwndDlg);
|
BOOL WriteConfig (const wchar_t* name, bool preserveUserConfig, int pim, int hashAlgo, const char* passPromptMsg, HWND hwndDlg);
|
||||||
BOOL DelDir(const wchar_t* name);
|
BOOL DelDir(const wchar_t* name);
|
||||||
void SelectBootVolumeESP();
|
|
||||||
PSTORAGE_DEVICE_NUMBER GetStorageDeviceNumber () { if (bDeviceInfoValid) return &sdn; else { SetLastError (ERROR_INVALID_DRIVE); throw SystemException(SRC_POS);}}
|
PSTORAGE_DEVICE_NUMBER GetStorageDeviceNumber () { if (bDeviceInfoValid) return &sdn; else { SetLastError (ERROR_INVALID_DRIVE); throw SystemException(SRC_POS);}}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -231,7 +234,6 @@ namespace VeraCrypt
|
||||||
PARTITION_INFORMATION_EX partInfo;
|
PARTITION_INFORMATION_EX partInfo;
|
||||||
bool bDeviceInfoValid;
|
bool bDeviceInfoValid;
|
||||||
WCHAR tempBuf[1024];
|
WCHAR tempBuf[1024];
|
||||||
bool bBootVolumePathSelected;
|
|
||||||
std::wstring BootVolumePath;
|
std::wstring BootVolumePath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue