From f8081df0a1226022a13fce7ce7bbdccafb95745b Mon Sep 17 00:00:00 2001 From: Paravis Date: Sat, 14 Nov 2026 17:48:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D0=B8=D1=81=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D1=8C=20=D1=81=D0=BE=D1=81=D1=82=D0=BE=D1=8F=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D1=87=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B2=20bootstate.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ src/boot.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/README.md b/README.md index 0037c1e..c29492c 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/src/boot.cpp b/src/boot.cpp index e961d1d..cae313d 100644 --- a/src/boot.cpp +++ b/src/boot.cpp @@ -6,6 +6,62 @@ #include +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(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"); }