218 lines
6.9 KiB
C++
218 lines
6.9 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (c) 2026 Paravis
|
|
|
|
#include "app.h"
|
|
#include "util.h"
|
|
|
|
#include <string>
|
|
|
|
void App::ShowMessage(const std::wstring& text, UINT flags)
|
|
{
|
|
bool previous = modal;
|
|
modal = true;
|
|
EnableWindow(hwnd, FALSE);
|
|
InvalidateRect(hwnd, nullptr, FALSE);
|
|
UpdateWindow(hwnd);
|
|
SetForegroundWindow(hwnd);
|
|
MessageBoxW(hwnd, text.c_str(), L"Change BootLoader",
|
|
flags | MB_SETFOREGROUND | MB_TOPMOST);
|
|
EnableWindow(hwnd, TRUE);
|
|
SetForegroundWindow(hwnd);
|
|
modal = previous;
|
|
InvalidateRect(hwnd, nullptr, FALSE);
|
|
}
|
|
|
|
void App::RunAction(int index)
|
|
{
|
|
if (busy) {
|
|
return;
|
|
}
|
|
|
|
int diskNumber = GetDiskNumberForDrive(disk[0]);
|
|
DebugLog(L"RunAction index=%d disk=%s physical=%d", index, disk.c_str(), diskNumber);
|
|
|
|
if (!FileExists(boot + L"\\NOT.DEL") || diskNumber < 0) {
|
|
ShowMessage(L"Целевой диск не подтверждён:\r\n" + disk +
|
|
L"\r\nнет \\efi\\Paravis\\Boot\\NOT.DEL или не найден PhysicalDrive.");
|
|
return;
|
|
}
|
|
|
|
busy = true;
|
|
activeButton = index;
|
|
InvalidateRect(hwnd, nullptr, FALSE);
|
|
UpdateWindow(hwnd);
|
|
switch (index) {
|
|
case 0:
|
|
LegacyXor();
|
|
break;
|
|
case 1:
|
|
LegacyMgr();
|
|
break;
|
|
case 2:
|
|
LegacyG4d();
|
|
break;
|
|
case 3:
|
|
LegacyGrub2();
|
|
break;
|
|
case 4:
|
|
UefiMgr();
|
|
break;
|
|
case 5:
|
|
UefiGrub2();
|
|
break;
|
|
case 6:
|
|
UefiXor();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
busy = false;
|
|
activeButton = -1;
|
|
InvalidateRect(hwnd, nullptr, FALSE);
|
|
}
|
|
|
|
void App::Done(const std::wstring& name)
|
|
{
|
|
ShowMessage(L"Загрузчик изменён: " + name);
|
|
}
|
|
|
|
void App::LegacyXor()
|
|
{
|
|
DeleteFileIfExists(disk + L"\\bootmgr");
|
|
DeleteFileIfExists(disk + L"\\grldr");
|
|
RunWait(bootice + L" /DEVICE=" + disk +
|
|
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");
|
|
Done(L"XorBoot");
|
|
}
|
|
|
|
void App::LegacyMgr()
|
|
{
|
|
DeleteFileIfExists(disk + L"\\bootmgr");
|
|
DeleteFileIfExists(disk + L"\\grldr");
|
|
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");
|
|
Done(L"Bootmgr");
|
|
}
|
|
|
|
void App::LegacyG4d()
|
|
{
|
|
DeleteFileIfExists(disk + L"\\bootmgr");
|
|
DeleteFileIfExists(disk + L"\\grldr");
|
|
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");
|
|
Done(L"Grub4Dos");
|
|
}
|
|
|
|
void App::LegacyGrub2()
|
|
{
|
|
std::wstring source = boot + L"\\loaders\\grub2-bios";
|
|
std::wstring destination = boot + L"\\grub2\\bios";
|
|
|
|
if (!FileExists(source + L"\\boot.img") || !FileExists(source + L"\\core.img")) {
|
|
ShowMessage(L"Файлы GRUB2 для Legacy BIOS не найдены:\r\n" + source);
|
|
return;
|
|
}
|
|
if (!FileExists(destination + L"\\grub.cfg")) {
|
|
ShowMessage(L"Не найден основной grub.cfg:\r\n" + destination + L"\\grub.cfg");
|
|
return;
|
|
}
|
|
|
|
int diskNumber = GetDiskNumberForDrive(disk[0]);
|
|
if (diskNumber < 0) {
|
|
ShowMessage(L"Не удалось определить номер физического диска для " + disk);
|
|
return;
|
|
}
|
|
|
|
DeleteFileIfExists(disk + L"\\bootmgr");
|
|
DeleteFileIfExists(disk + L"\\grldr");
|
|
|
|
EnsureDirectory(destination);
|
|
EnsureDirectory(destination + L"\\i386-pc");
|
|
CopyFileOverwrite(source + L"\\boot.img", destination + L"\\boot.img");
|
|
CopyFileOverwrite(source + L"\\core.img", destination + L"\\core.img");
|
|
CopyFileOverwrite(source + L"\\make-paravis-mbr.ps1",
|
|
destination + L"\\make-paravis-mbr.ps1");
|
|
|
|
std::wstring xcopy = L"\"" + GetComSpec() +
|
|
L"\" /c xcopy /E /I /Y /Q \"" + source + L"\\i386-pc\" \"" +
|
|
destination + L"\\i386-pc\" > nul 2>&1";
|
|
RunWait(xcopy);
|
|
|
|
std::wstring powershell = L"powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"" +
|
|
destination + L"\\make-paravis-mbr.ps1\" -Disk " +
|
|
std::to_wstring(diskNumber) + L" -Boot \"" + destination +
|
|
L"\\boot.img\" -Out \"" + destination + L"\\paravis-mbr-table.img\"";
|
|
int rc1 = RunWait(powershell);
|
|
if (rc1 != 0) {
|
|
ShowMessage(L"Не удалось сформировать MBR-образ (код " +
|
|
std::to_wstring(rc1) + L").");
|
|
return;
|
|
}
|
|
|
|
std::wstring mbr = L"\"" + bootice + L"\" /DEVICE=" + disk +
|
|
L" /sectors /restore /file=\"" + destination +
|
|
L"\\paravis-mbr-table.img\" /lba=0 /sectors=1 /quiet";
|
|
int rc2 = RunWait(mbr);
|
|
if (rc2 != 0) {
|
|
ShowMessage(L"Ошибка записи MBR (код " + std::to_wstring(rc2) + L").");
|
|
return;
|
|
}
|
|
|
|
std::wstring core = L"\"" + bootice + L"\" /DEVICE=" + disk +
|
|
L" /sectors /restore /file=\"" + destination +
|
|
L"\\core.img\" /lba=1 /sectors=591 /quiet";
|
|
int rc3 = RunWait(core);
|
|
if (rc3 != 0) {
|
|
ShowMessage(L"Ошибка записи core.img (код " + std::to_wstring(rc3) + L").");
|
|
return;
|
|
}
|
|
|
|
Done(L"Grub2 (Legacy)");
|
|
}
|
|
|
|
void App::UefiMgr()
|
|
{
|
|
UefiClean();
|
|
CopyFileOverwrite(boot + L"\\mgr\\UEFI\\bootx64.efi",
|
|
disk + L"\\efi\\boot\\bootx64.efi");
|
|
CopyFileOverwrite(boot + L"\\mgr\\UEFI\\bootia32.efi",
|
|
disk + L"\\efi\\boot\\bootia32.efi");
|
|
Done(L"UEFI Bootmgr");
|
|
}
|
|
|
|
void App::UefiGrub2()
|
|
{
|
|
DeleteFileIfExists(disk + L"\\efi\\boot\\bootx64.efi");
|
|
DeleteFileIfExists(disk + L"\\efi\\boot\\fbx64.efi");
|
|
DeleteFileIfExists(disk + L"\\efi\\boot\\mmx64.efi");
|
|
CopyFileOverwrite(boot + L"\\loaders\\grub2\\bootx64.efi",
|
|
disk + L"\\efi\\boot\\bootx64.efi");
|
|
CopyFileOverwrite(boot + L"\\loaders\\grub2\\fbx64.efi",
|
|
disk + L"\\efi\\boot\\fbx64.efi");
|
|
CopyFileOverwrite(boot + L"\\loaders\\grub2\\mmx64.efi",
|
|
disk + L"\\efi\\boot\\mmx64.efi");
|
|
Done(L"UEFI GRUB2");
|
|
}
|
|
|
|
void App::UefiXor()
|
|
{
|
|
UefiClean();
|
|
CopyFileOverwrite(boot + L"\\xor\\UEFI\\bootx64.efi",
|
|
disk + L"\\efi\\boot\\bootx64.efi");
|
|
CopyFileOverwrite(boot + L"\\xor\\UEFI\\bootia32.efi",
|
|
disk + L"\\efi\\boot\\bootia32.efi");
|
|
Done(L"UEFI Xorboot");
|
|
}
|
|
|
|
void App::UefiClean()
|
|
{
|
|
DeleteFileIfExists(disk + L"\\efi\\boot\\bootx64.efi");
|
|
DeleteFileIfExists(disk + L"\\efi\\boot\\bootia32.efi");
|
|
DeleteFileIfExists(disk + L"\\efi\\boot\\fbx64.efi");
|
|
DeleteFileIfExists(disk + L"\\efi\\boot\\mmx64.efi");
|
|
}
|