mirror of
https://github.com/veracrypt/VeraCrypt
synced 2024-11-27 21:43:29 +01:00
Windows: code refactoring for handling of ESP files (DcsProp and PlatformInfo).
This commit is contained in:
parent
374ba4c831
commit
4208b43581
@ -1565,9 +1565,6 @@ namespace VeraCrypt
|
|||||||
{
|
{
|
||||||
if (GetSystemDriveConfiguration().SystemPartition.IsGPT)
|
if (GetSystemDriveConfiguration().SystemPartition.IsGPT)
|
||||||
{
|
{
|
||||||
byte confContent[4096*8];
|
|
||||||
DWORD dwSize;
|
|
||||||
|
|
||||||
// for now, we don't support any boot config flags, like hidden OS one
|
// for now, we don't support any boot config flags, like hidden OS one
|
||||||
if (config)
|
if (config)
|
||||||
memset (config, 0, bufLength);
|
memset (config, 0, bufLength);
|
||||||
@ -1575,12 +1572,10 @@ namespace VeraCrypt
|
|||||||
// call ReadEfiConfig only when needed since it requires elevation
|
// call ReadEfiConfig only when needed since it requires elevation
|
||||||
if (userConfig || customUserMessage || bootLoaderVersion)
|
if (userConfig || customUserMessage || bootLoaderVersion)
|
||||||
{
|
{
|
||||||
ReadEfiConfig (L"\\EFI\\VeraCrypt\\DcsProp", confContent, sizeof (confContent) - 1, &dwSize);
|
std::string confContent = ReadESPFile (L"\\EFI\\VeraCrypt\\DcsProp", true);
|
||||||
|
|
||||||
confContent[dwSize] = 0;
|
|
||||||
|
|
||||||
EfiBootConf conf;
|
EfiBootConf conf;
|
||||||
conf.Load ((char*) confContent);
|
conf.Load ((char*) confContent.c_str());
|
||||||
|
|
||||||
if (userConfig)
|
if (userConfig)
|
||||||
{
|
{
|
||||||
@ -2095,29 +2090,96 @@ namespace VeraCrypt
|
|||||||
void
|
void
|
||||||
GetVolumeESP(wstring& path)
|
GetVolumeESP(wstring& path)
|
||||||
{
|
{
|
||||||
ULONG len;
|
static wstring g_EspPath;
|
||||||
NTSTATUS res;
|
static bool g_EspPathInitialized = false;
|
||||||
WCHAR tempBuf[1024];
|
|
||||||
memset(tempBuf, 0, sizeof(tempBuf));
|
|
||||||
|
|
||||||
// Load NtQuerySystemInformation function point
|
if (!g_EspPathInitialized)
|
||||||
if (!NtQuerySystemInformationPtr)
|
|
||||||
{
|
{
|
||||||
NtQuerySystemInformationPtr = (NtQuerySystemInformationFn) GetProcAddress (GetModuleHandle (L"ntdll.dll"), "NtQuerySystemInformation");
|
ULONG len;
|
||||||
|
NTSTATUS res;
|
||||||
|
WCHAR tempBuf[1024];
|
||||||
|
memset(tempBuf, 0, sizeof(tempBuf));
|
||||||
|
|
||||||
|
// Load NtQuerySystemInformation function point
|
||||||
if (!NtQuerySystemInformationPtr)
|
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);
|
throw SystemException (SRC_POS);
|
||||||
|
}
|
||||||
|
|
||||||
|
PUNICODE_STRING pStr = (PUNICODE_STRING) tempBuf;
|
||||||
|
g_EspPath = L"\\\\?";
|
||||||
|
g_EspPath += &pStr->Buffer[7];
|
||||||
|
g_EspPathInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = NtQuerySystemInformationPtr((SYSTEM_INFORMATION_CLASS)SYSPARTITIONINFORMATION, tempBuf, sizeof(tempBuf), &len);
|
path = g_EspPath;
|
||||||
if (res != S_OK)
|
}
|
||||||
{
|
|
||||||
SetLastError (res);
|
|
||||||
throw SystemException (SRC_POS);
|
|
||||||
}
|
|
||||||
|
|
||||||
PUNICODE_STRING pStr = (PUNICODE_STRING) tempBuf;
|
std::string ReadESPFile (LPCWSTR szFilePath, bool bSkipUTF8BOM)
|
||||||
path = L"\\\\?";
|
{
|
||||||
path += &pStr->Buffer[7];
|
if (!szFilePath || !szFilePath[0])
|
||||||
|
throw ParameterIncorrect (SRC_POS);
|
||||||
|
|
||||||
|
ByteArray fileContent;
|
||||||
|
DWORD dwSize = 0, dwOffset = 0;
|
||||||
|
std::wstring pathESP;
|
||||||
|
|
||||||
|
GetVolumeESP(pathESP);
|
||||||
|
if (szFilePath[0] != L'\\')
|
||||||
|
pathESP += L"\\";
|
||||||
|
File f(pathESP + szFilePath, true);
|
||||||
|
f.GetFileSize(dwSize);
|
||||||
|
fileContent.resize(dwSize + 1);
|
||||||
|
fileContent[dwSize] = 0;
|
||||||
|
f.Read(fileContent.data(), dwSize);
|
||||||
|
f.Close();
|
||||||
|
|
||||||
|
if (bSkipUTF8BOM)
|
||||||
|
{
|
||||||
|
// remove UTF-8 BOM if any
|
||||||
|
if (0 == memcmp (fileContent.data(), "\xEF\xBB\xBF", 3))
|
||||||
|
{
|
||||||
|
dwOffset = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (const char*) &fileContent[dwOffset];
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteESPFile (LPCWSTR szFilePath, LPBYTE pbData, DWORD dwDataLen, bool bAddUTF8BOM)
|
||||||
|
{
|
||||||
|
if (!szFilePath || !szFilePath[0] || !pbData || !dwDataLen)
|
||||||
|
throw ParameterIncorrect (SRC_POS);
|
||||||
|
|
||||||
|
ByteArray fileContent;
|
||||||
|
DWORD dwSize = dwDataLen, dwOffset = 0;
|
||||||
|
std::wstring pathESP;
|
||||||
|
|
||||||
|
if (bAddUTF8BOM)
|
||||||
|
{
|
||||||
|
dwSize += 3;
|
||||||
|
dwOffset = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
GetVolumeESP(pathESP);
|
||||||
|
if (szFilePath[0] != L'\\')
|
||||||
|
pathESP += L"\\";
|
||||||
|
File f(pathESP + szFilePath, false, true);
|
||||||
|
fileContent.resize(dwSize);
|
||||||
|
if (bAddUTF8BOM)
|
||||||
|
memcpy (fileContent.data(), "\xEF\xBB\xBF", 3);
|
||||||
|
memcpy (&fileContent[dwOffset], pbData, dwDataLen);
|
||||||
|
f.Write(fileContent.data(), dwSize);
|
||||||
|
f.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
EfiBoot::EfiBoot() {
|
EfiBoot::EfiBoot() {
|
||||||
|
@ -187,6 +187,8 @@ namespace VeraCrypt
|
|||||||
};
|
};
|
||||||
|
|
||||||
void GetVolumeESP(wstring& path);
|
void GetVolumeESP(wstring& path);
|
||||||
|
std::string ReadESPFile (LPCWSTR szFilePath, bool bSkipUTF8BOM);
|
||||||
|
void WriteESPFile (LPCWSTR szFilePath, LPBYTE pbData, DWORD dwDataLen, bool bAddUTF8BOM);
|
||||||
|
|
||||||
class EfiBoot {
|
class EfiBoot {
|
||||||
public:
|
public:
|
||||||
|
@ -11063,24 +11063,7 @@ static BOOL CALLBACK BootLoaderPreferencesDlgProc (HWND hwndDlg, UINT msg, WPARA
|
|||||||
// read PlatformInfo file if it exists
|
// read PlatformInfo file if it exists
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ByteArray fileContent;
|
platforminfo = ReadESPFile (L"\\EFI\\VeraCrypt\\PlatformInfo", true);
|
||||||
DWORD sz, offset;
|
|
||||||
std::wstring path;
|
|
||||||
GetVolumeESP(path);
|
|
||||||
path += L"\\EFI\\VeraCrypt\\PlatformInfo";
|
|
||||||
File fPlatformInfo(path);
|
|
||||||
fPlatformInfo.GetFileSize(sz);
|
|
||||||
fileContent.resize(sz + 1);
|
|
||||||
fileContent[sz] = 0;
|
|
||||||
fPlatformInfo.Read((byte*)&fileContent[0], sz);
|
|
||||||
// remove UTF-8 BOM if any
|
|
||||||
if (0 == memcmp (fileContent.data(), "\xEF\xBB\xBF", 3))
|
|
||||||
{
|
|
||||||
offset = 3;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
offset = 0;
|
|
||||||
platforminfo = (const char*) &fileContent[offset];
|
|
||||||
}
|
}
|
||||||
catch (Exception &e) {}
|
catch (Exception &e) {}
|
||||||
|
|
||||||
@ -11130,38 +11113,13 @@ static BOOL CALLBACK BootLoaderPreferencesDlgProc (HWND hwndDlg, UINT msg, WPARA
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::string dcsprop;
|
std::string dcsprop = ReadESPFile (L"\\EFI\\VeraCrypt\\DcsProp", true);
|
||||||
ByteArray fileContent;
|
|
||||||
DWORD sz, offset;
|
|
||||||
std::wstring path;
|
|
||||||
GetVolumeESP(path);
|
|
||||||
path += L"\\EFI\\VeraCrypt\\DcsProp";
|
|
||||||
File f1(path);
|
|
||||||
f1.GetFileSize(sz);
|
|
||||||
fileContent.resize(sz + 1);
|
|
||||||
fileContent[sz] = 0;
|
|
||||||
f1.Read((byte*)&fileContent[0], sz);
|
|
||||||
f1.Close();
|
|
||||||
// remove UTF-8 BOM if any
|
|
||||||
if (0 == memcmp (fileContent.data(), "\xEF\xBB\xBF", 3))
|
|
||||||
{
|
|
||||||
offset = 3;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
offset = 0;
|
|
||||||
|
|
||||||
dcsprop = (const char*) &fileContent[offset];
|
|
||||||
while (TextEditDialogBox(FALSE, hwndDlg, GetString ("BOOT_LOADER_CONFIGURATION_FILE"), dcsprop) == IDOK)
|
while (TextEditDialogBox(FALSE, hwndDlg, GetString ("BOOT_LOADER_CONFIGURATION_FILE"), dcsprop) == IDOK)
|
||||||
{
|
{
|
||||||
if (validateDcsPropXml (dcsprop.c_str()))
|
if (validateDcsPropXml (dcsprop.c_str()))
|
||||||
{
|
{
|
||||||
// Add UTF-8 BOM
|
WriteESPFile (L"\\EFI\\VeraCrypt\\DcsProp", (LPBYTE) dcsprop.c_str(), (DWORD) dcsprop.size(), true);
|
||||||
fileContent.resize (dcsprop.length() + 3);
|
|
||||||
memcpy (fileContent.data(), "\xEF\xBB\xBF", 3);
|
|
||||||
memcpy (&fileContent[3], &dcsprop[0], dcsprop.length());
|
|
||||||
File f2(path,false,true);
|
|
||||||
f2.Write(fileContent.data(), fileContent.size());
|
|
||||||
f2.Close();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
Loading…
Reference in New Issue
Block a user