Записывать состояние загрузчика в bootstate.txt
This commit is contained in:
1 parent
69b63deee5
commit
f8081df0a1
2 files changed
+66
No files matched your search
@@ -29,6 +29,9 @@ build\Release\Change_BootLoader.exe
|
||||
- Семь кнопок выполняют те же операции, что и AutoIt-версия:
|
||||
- Legacy BIOS: XorBoot, Bootmgr, Grub4Dos, Grub2
|
||||
- UEFI: Bootmgr, Grub2, Xorboot
|
||||
- После успешной смены записывает `\efi\Paravis\Boot\bootstate.txt`
|
||||
(`legacy=`, `uefi=`). Этот же файл читает Android-приложение, чтобы
|
||||
подсветить установленный загрузчик.
|
||||
- Bootice, xcopy и скрипт создания MBR для GRUB2 вызываются так же, как в
|
||||
AutoIt-версии. Скрипт:
|
||||
`\efi\Paravis\Boot\grub2\bios\make-paravis-mbr.ps1`.
|
||||
|
||||
@@ -6,6 +6,62 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
static int ReadStateValue(const std::wstring& path, const char* key)
|
||||
{
|
||||
HANDLE handle = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
||||
nullptr);
|
||||
if (handle == INVALID_HANDLE_VALUE) {
|
||||
return -1;
|
||||
}
|
||||
char buffer[256] = {};
|
||||
DWORD read = 0;
|
||||
ReadFile(handle, buffer, sizeof(buffer) - 1, &read, nullptr);
|
||||
CloseHandle(handle);
|
||||
|
||||
std::string text(buffer, read);
|
||||
std::string prefix = std::string(key) + "=";
|
||||
size_t pos = text.find(prefix);
|
||||
if (pos == std::string::npos) {
|
||||
return -1;
|
||||
}
|
||||
pos += prefix.size();
|
||||
bool digits = false;
|
||||
int value = 0;
|
||||
while (pos < text.size() && text[pos] >= '0' && text[pos] <= '9') {
|
||||
value = value * 10 + (text[pos] - '0');
|
||||
++pos;
|
||||
digits = true;
|
||||
}
|
||||
return digits ? value : -1;
|
||||
}
|
||||
|
||||
static void WriteBootState(const std::wstring& bootDir, int legacy, int uefi)
|
||||
{
|
||||
std::wstring path = bootDir + L"\\bootstate.txt";
|
||||
if (legacy < 0) {
|
||||
legacy = ReadStateValue(path, "legacy");
|
||||
}
|
||||
if (uefi < 0) {
|
||||
uefi = ReadStateValue(path, "uefi");
|
||||
}
|
||||
std::string text = "legacy=" + std::to_string(legacy) +
|
||||
"\nuefi=" + std::to_string(uefi) + "\n";
|
||||
|
||||
HANDLE handle = CreateFileW(path.c_str(), GENERIC_WRITE, 0, nullptr,
|
||||
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (handle == INVALID_HANDLE_VALUE) {
|
||||
DebugLog(L"bootstate: cannot write %s", path.c_str());
|
||||
return;
|
||||
}
|
||||
DWORD written = 0;
|
||||
WriteFile(handle, text.data(), static_cast<DWORD>(text.size()), &written, nullptr);
|
||||
FlushFileBuffers(handle);
|
||||
CloseHandle(handle);
|
||||
DebugLog(L"bootstate: legacy=%d uefi=%d", legacy, uefi);
|
||||
}
|
||||
|
||||
|
||||
void App::ShowMessage(const std::wstring& text, UINT flags)
|
||||
{
|
||||
bool previous = modal;
|
||||
@@ -84,6 +140,7 @@ void App::LegacyXor()
|
||||
L" /mbr /restore /file=" + boot + L"\\xor\\xmbr.bin /keep_dpt /quiet");
|
||||
RunWait(bootice + L" /DEVICE=" + disk +
|
||||
L" /pbr /restore /file=" + boot + L"\\xor\\xpbr.bin /keep_bpb /quiet");
|
||||
WriteBootState(boot, 0, -1);
|
||||
Done(L"XorBoot");
|
||||
}
|
||||
|
||||
@@ -94,6 +151,7 @@ void App::LegacyMgr()
|
||||
CopyFileOverwrite(boot + L"\\mgr\\bootmgr", disk + L"\\bootmgr");
|
||||
RunWait(bootice + L" /DEVICE=" + disk + L" /mbr /install /type=nt60 /quiet");
|
||||
RunWait(bootice + L" /DEVICE=" + disk + L" /pbr /install /type=bootmgr /quiet");
|
||||
WriteBootState(boot, 1, -1);
|
||||
Done(L"Bootmgr");
|
||||
}
|
||||
|
||||
@@ -104,6 +162,7 @@ void App::LegacyG4d()
|
||||
CopyFileOverwrite(boot + L"\\g4d\\grldr", disk + L"\\grldr");
|
||||
RunWait(bootice + L" /DEVICE=" + disk + L" /mbr /install /type=GRUB4DOS /quiet");
|
||||
RunWait(bootice + L" /DEVICE=" + disk + L" /pbr /install /type=GRUB4DOS /quiet");
|
||||
WriteBootState(boot, 2, -1);
|
||||
Done(L"Grub4Dos");
|
||||
}
|
||||
|
||||
@@ -171,6 +230,7 @@ void App::LegacyGrub2()
|
||||
return;
|
||||
}
|
||||
|
||||
WriteBootState(boot, 3, -1);
|
||||
Done(L"Grub2 (Legacy)");
|
||||
}
|
||||
|
||||
@@ -181,6 +241,7 @@ void App::UefiMgr()
|
||||
disk + L"\\efi\\boot\\bootx64.efi");
|
||||
CopyFileOverwrite(boot + L"\\mgr\\UEFI\\bootia32.efi",
|
||||
disk + L"\\efi\\boot\\bootia32.efi");
|
||||
WriteBootState(boot, -1, 0);
|
||||
Done(L"UEFI Bootmgr");
|
||||
}
|
||||
|
||||
@@ -195,6 +256,7 @@ void App::UefiGrub2()
|
||||
disk + L"\\efi\\boot\\fbx64.efi");
|
||||
CopyFileOverwrite(boot + L"\\loaders\\grub2\\mmx64.efi",
|
||||
disk + L"\\efi\\boot\\mmx64.efi");
|
||||
WriteBootState(boot, -1, 1);
|
||||
Done(L"UEFI GRUB2");
|
||||
}
|
||||
|
||||
@@ -205,6 +267,7 @@ void App::UefiXor()
|
||||
disk + L"\\efi\\boot\\bootx64.efi");
|
||||
CopyFileOverwrite(boot + L"\\xor\\UEFI\\bootia32.efi",
|
||||
disk + L"\\efi\\boot\\bootia32.efi");
|
||||
WriteBootState(boot, -1, 2);
|
||||
Done(L"UEFI Xorboot");
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user