Don't access ESP before showing the menu

This commit is contained in:
Lauri Kenttä
2023-09-10 15:02:58 +03:00
parent 31172f71ca
commit 199650a567
+23 -39
View File
@@ -56,6 +56,9 @@ public class Setup: SetupHelper {
} }
} }
/** @var The EFI architecture identifier. */
protected string EfiArch;
/** @var Run in batch mode? */ /** @var Run in batch mode? */
protected bool Batch; protected bool Batch;
@@ -196,15 +199,7 @@ public class Setup: SetupHelper {
} catch { } catch {
throw new SetupException("Failed to copy files to ESP!"); throw new SetupException("Failed to copy files to ESP!");
} }
if (MsLoader.Type == BootLoaderType.MS) {
if (!MsLoaderBackup.ReplaceWith(MsLoader)) {
throw new SetupException("Failed to backup MS boot loader!");
}
}
if (MsLoaderBackup.Type != BootLoaderType.MS) {
// Duplicate check, but better to be sure...
throw new SetupException("Failed to detect MS boot loader!");
}
InstallFile("config.txt"); InstallFile("config.txt");
foreach (var line in File.ReadAllLines("config.txt").Where(s => s.StartsWith("image="))) { foreach (var line in File.ReadAllLines("config.txt").Where(s => s.StartsWith("image="))) {
var delim = "path=\\EFI\\HackBGRT\\"; var delim = "path=\\EFI\\HackBGRT\\";
@@ -221,7 +216,18 @@ public class Setup: SetupHelper {
* Enable HackBGRT by overwriting the MS boot loader. * Enable HackBGRT by overwriting the MS boot loader.
*/ */
protected void OverwriteMsLoader() { protected void OverwriteMsLoader() {
var MsLoader = new BootLoaderInfo(Esp.MsLoaderPath);
var NewLoader = new BootLoaderInfo(Path.Combine(InstallPath, "loader.efi")); var NewLoader = new BootLoaderInfo(Path.Combine(InstallPath, "loader.efi"));
var MsLoaderBackup = new BootLoaderInfo(BackupLoaderPath);
if (MsLoader.Type == BootLoaderType.MS) {
if (!MsLoaderBackup.ReplaceWith(MsLoader)) {
throw new SetupException("Failed to backup MS boot loader!");
}
}
if (MsLoaderBackup.Type != BootLoaderType.MS) {
// Duplicate check, but better to be sure...
throw new SetupException("Failed to detect MS boot loader!");
}
if (!MsLoader.ReplaceWith(NewLoader)) { if (!MsLoader.ReplaceWith(NewLoader)) {
MsLoader.ReplaceWith(MsLoaderBackup); MsLoader.ReplaceWith(MsLoaderBackup);
throw new SetupException("Couldn't copy new HackBGRT over the MS loader (bootmgfw.efi)."); throw new SetupException("Couldn't copy new HackBGRT over the MS loader (bootmgfw.efi).");
@@ -233,7 +239,9 @@ public class Setup: SetupHelper {
* Restore the MS boot loader if it was previously replaced. * Restore the MS boot loader if it was previously replaced.
*/ */
protected void RestoreMsLoader() { protected void RestoreMsLoader() {
var MsLoader = new BootLoaderInfo(Esp.MsLoaderPath);
if (MsLoader.Type != BootLoaderType.MS) { if (MsLoader.Type != BootLoaderType.MS) {
var MsLoaderBackup = new BootLoaderInfo(BackupLoaderPath);
if (!MsLoader.ReplaceWith(MsLoaderBackup)) { if (!MsLoader.ReplaceWith(MsLoaderBackup)) {
throw new SetupException("Couldn't restore the old MS loader."); throw new SetupException("Couldn't restore the old MS loader.");
} }
@@ -326,22 +334,15 @@ public class Setup: SetupHelper {
} }
} }
protected BootLoaderInfo MsLoader, MsLoaderBackup;
protected string EfiArch;
/** /**
* Initialize information for the Setup. * Initialize information for the Setup.
*/ */
protected void InitEspInfo() { protected void InitEspInfo() {
InstallPath = Path.Combine(Esp.Location, "EFI", "HackBGRT"); InstallPath = Path.Combine(Esp.Location, "EFI", "HackBGRT");
MsLoaderBackup = new BootLoaderInfo(BackupLoaderPath); EfiArch = IntPtr.Size == 4 ? "ia32" : "x64";
MsLoader = new BootLoaderInfo(Esp.MsLoaderPath); var MsLoader = new BootLoaderInfo(Esp.MsLoaderPath);
if (MsLoader.Type == BootLoaderType.MS) { if (MsLoader.Arch != null) {
EfiArch = MsLoader.Arch; EfiArch = MsLoader.Arch;
} else if (MsLoaderBackup.Type == BootLoaderType.MS) {
EfiArch = MsLoaderBackup.Arch;
} else {
throw new SetupException("Failed to detect MS boot loader!");
} }
} }
@@ -349,29 +350,12 @@ public class Setup: SetupHelper {
* Ask for user's choice and install/uninstall. * Ask for user's choice and install/uninstall.
*/ */
protected void ShowMenu() { protected void ShowMenu() {
var NewLoader = new BootLoaderInfo(Path.Combine(InstallPath, "loader.efi"));
bool isEnabled = MsLoader.Type == BootLoaderType.Own;
bool isInstalled = NewLoader.Type == BootLoaderType.Own;
if (isEnabled) {
Console.WriteLine("HackBGRT is currently enabled.");
} else {
if (isInstalled) {
Console.WriteLine("HackBGRT is currently disabled.");
} else {
Console.WriteLine("HackBGRT is currently not installed.");
}
}
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Choose action (press a key):"); Console.WriteLine("Choose action (press a key):");
Console.WriteLine(" I = install, upgrade, repair, modify..."); Console.WriteLine(" I = install, enable, upgrade, repair, modify");
Console.WriteLine(" F = install files only, don't enable"); Console.WriteLine(" F = install files only, don't enable");
if (isEnabled) { Console.WriteLine(" D = disable, restore the original boot loader");
Console.WriteLine(" D = disable, restore the original boot loader"); Console.WriteLine(" R = remove completely; delete all HackBGRT files and images");
}
if (isInstalled) {
Console.WriteLine(" R = remove completely; delete all HackBGRT files and images");
}
Console.WriteLine(" C = cancel"); Console.WriteLine(" C = cancel");
var k = Console.ReadKey().Key; var k = Console.ReadKey().Key;