mirror of
https://github.com/Metabolix/HackBGRT.git
synced 2026-04-11 12:41:38 -07:00
Support creating boot entry with bcdedit
This commit is contained in:
@@ -30,6 +30,8 @@ When booting on a UEFI-based computer, Windows may show a vendor-defined logo wh
|
|||||||
* `install` – copy the files but don't enable.
|
* `install` – copy the files but don't enable.
|
||||||
* `enable-entry` – create a new EFI boot entry.
|
* `enable-entry` – create a new EFI boot entry.
|
||||||
* `disable-entry` – disable the EFI boot entry.
|
* `disable-entry` – disable the EFI boot entry.
|
||||||
|
* `enable-bcdedit` – use `bcdedit` to create a new EFI boot entry.
|
||||||
|
* `disable-bootmgr` – use `bcdedit` to disable the EFI boot entry.
|
||||||
* `enable-overwrite` – overwrite the MS boot loader.
|
* `enable-overwrite` – overwrite the MS boot loader.
|
||||||
* `disable-overwrite` – restore the MS boot loader.
|
* `disable-overwrite` – restore the MS boot loader.
|
||||||
* `allow-secure-boot` – ignore Secure Boot in subsequent commands.
|
* `allow-secure-boot` – ignore Secure Boot in subsequent commands.
|
||||||
|
|||||||
80
src/Setup.cs
80
src/Setup.cs
@@ -8,6 +8,7 @@ using System.Collections.Generic;
|
|||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HackBGRT Setup.
|
* HackBGRT Setup.
|
||||||
@@ -56,6 +57,7 @@ public class Setup {
|
|||||||
"allow-secure-boot",
|
"allow-secure-boot",
|
||||||
"allow-bad-loader",
|
"allow-bad-loader",
|
||||||
"enable-entry", "disable-entry",
|
"enable-entry", "disable-entry",
|
||||||
|
"enable-bcdedit", "disable-bcdedit",
|
||||||
"enable-overwrite", "disable-overwrite",
|
"enable-overwrite", "disable-overwrite",
|
||||||
"disable",
|
"disable",
|
||||||
"uninstall",
|
"uninstall",
|
||||||
@@ -326,6 +328,55 @@ public class Setup {
|
|||||||
WriteLine($"HackBGRT has been copied to {InstallPath}.");
|
WriteLine($"HackBGRT has been copied to {InstallPath}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable HackBGRT with bcdedit.
|
||||||
|
*/
|
||||||
|
protected void EnableBCDEdit() {
|
||||||
|
try {
|
||||||
|
var re = new Regex("[{][0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}[}]");
|
||||||
|
var guid = re.Match(Execute("bcdedit", "/copy {bootmgr} /d HackBGRT", true)).Value;
|
||||||
|
Execute("bcdedit", $"/set {guid} device partition={Esp.Location}", true);
|
||||||
|
Execute("bcdedit", $"/set {guid} path \\EFI\\HackBGRT\\loader.efi", true);
|
||||||
|
foreach (var arg in new string[] { "locale", "inherit", "default", "resumeobject", "displayorder", "toolsdisplayorder", "timeout" }) {
|
||||||
|
Execute("bcdedit", $"/deletevalue {guid} {arg}", true);
|
||||||
|
}
|
||||||
|
var fwbootmgr = "{fwbootmgr}";
|
||||||
|
Execute("bcdedit", $"/set {fwbootmgr} displayorder {guid} /addfirst", true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log($"EnableBCDEdit failed: {e.ToString()}");
|
||||||
|
throw new SetupException("Failed to enable HackBGRT with BCDEdit!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable HackBGRT with bcdedit.
|
||||||
|
*/
|
||||||
|
protected void DisableBCDEdit() {
|
||||||
|
bool found = false, disabled = false;
|
||||||
|
var fullOutput = Execute("bcdedit", "/enum firmware", true);
|
||||||
|
if (fullOutput == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var re = new Regex("[{][0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}[}]");
|
||||||
|
var guids = (from Match match in re.Matches(fullOutput) select match.Value).Distinct().ToArray();
|
||||||
|
foreach (var guid in guids) {
|
||||||
|
var entry = Execute("bcdedit", $"/enum {guid}", true);
|
||||||
|
if (entry == null) {
|
||||||
|
Log($"DisableBCDEdit failed to enum {guid}.");
|
||||||
|
} else if (entry.IndexOf("HackBGRT") >= 0) {
|
||||||
|
found = true;
|
||||||
|
Log($"Disabling HackBGRT entry {guid}.");
|
||||||
|
if (Execute("bcdedit", $"/delete {guid}", true) == null) {
|
||||||
|
Log($"DisableBCDEdit failed to delete {guid}.");
|
||||||
|
}
|
||||||
|
disabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (found && !disabled) {
|
||||||
|
throw new SetupException("Failed to disable HackBGRT with BCDEdit!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable HackBGRT boot entry.
|
* Enable HackBGRT boot entry.
|
||||||
*/
|
*/
|
||||||
@@ -441,12 +492,21 @@ public class Setup {
|
|||||||
WriteLine();
|
WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable HackBGRT completely.
|
||||||
|
*/
|
||||||
|
protected void Disable() {
|
||||||
|
RestoreMsLoader();
|
||||||
|
DisableBCDEdit();
|
||||||
|
DisableEntry();
|
||||||
|
WriteLine("HackBGRT has been disabled.");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uninstall HackBGRT completely.
|
* Uninstall HackBGRT completely.
|
||||||
*/
|
*/
|
||||||
protected void Uninstall() {
|
protected void Uninstall() {
|
||||||
RestoreMsLoader();
|
Disable();
|
||||||
DisableEntry();
|
|
||||||
try {
|
try {
|
||||||
Directory.Delete(InstallPath, true);
|
Directory.Delete(InstallPath, true);
|
||||||
WriteLine($"HackBGRT has been removed from {InstallPath}.");
|
WriteLine($"HackBGRT has been removed from {InstallPath}.");
|
||||||
@@ -571,6 +631,11 @@ public class Setup {
|
|||||||
WriteLine(" - creates a new EFI boot entry for HackBGRT");
|
WriteLine(" - creates a new EFI boot entry for HackBGRT");
|
||||||
WriteLine(" - sometimes needs to be enabled in firmware settings");
|
WriteLine(" - sometimes needs to be enabled in firmware settings");
|
||||||
WriteLine(" - should fall back to MS boot loader if HackBGRT fails");
|
WriteLine(" - should fall back to MS boot loader if HackBGRT fails");
|
||||||
|
WriteLine(" J = install (alternative)");
|
||||||
|
WriteLine(" - creates a new EFI boot entry with an alternative method (BCDEdit)");
|
||||||
|
WriteLine(" - always sets HackBGRT as the first boot option");
|
||||||
|
WriteLine(" - sometimes shows up as \"Windows Boot Manager\"");
|
||||||
|
WriteLine(" - should fall back to MS boot loader if HackBGRT fails");
|
||||||
WriteLine(" O = install (legacy)");
|
WriteLine(" O = install (legacy)");
|
||||||
WriteLine(" - overwrites the MS boot loader");
|
WriteLine(" - overwrites the MS boot loader");
|
||||||
WriteLine(" - may require re-install after Windows updates");
|
WriteLine(" - may require re-install after Windows updates");
|
||||||
@@ -591,6 +656,8 @@ public class Setup {
|
|||||||
}
|
}
|
||||||
if (k == ConsoleKey.I) {
|
if (k == ConsoleKey.I) {
|
||||||
RunPrivilegedActions(new string[] { "install", "enable-entry" });
|
RunPrivilegedActions(new string[] { "install", "enable-entry" });
|
||||||
|
} else if (k == ConsoleKey.J) {
|
||||||
|
RunPrivilegedActions(new string[] { "install", "enable-bcdedit" });
|
||||||
} else if (k == ConsoleKey.O) {
|
} else if (k == ConsoleKey.O) {
|
||||||
RunPrivilegedActions(new string[] { "install", "enable-overwrite" });
|
RunPrivilegedActions(new string[] { "install", "enable-overwrite" });
|
||||||
} else if (k == ConsoleKey.F) {
|
} else if (k == ConsoleKey.F) {
|
||||||
@@ -661,6 +728,12 @@ public class Setup {
|
|||||||
verify(() => DisableEntry());
|
verify(() => DisableEntry());
|
||||||
} else if (arg == "disable-entry") {
|
} else if (arg == "disable-entry") {
|
||||||
DisableEntry();
|
DisableEntry();
|
||||||
|
} else if (arg == "enable-bcdedit") {
|
||||||
|
HandleSecureBoot(allowSecureBoot);
|
||||||
|
EnableBCDEdit();
|
||||||
|
verify(() => DisableBCDEdit());
|
||||||
|
} else if (arg == "disable-bcdedit") {
|
||||||
|
DisableBCDEdit();
|
||||||
} else if (arg == "enable-overwrite") {
|
} else if (arg == "enable-overwrite") {
|
||||||
HandleSecureBoot(allowSecureBoot);
|
HandleSecureBoot(allowSecureBoot);
|
||||||
OverwriteMsLoader();
|
OverwriteMsLoader();
|
||||||
@@ -668,8 +741,7 @@ public class Setup {
|
|||||||
} else if (arg == "disable-overwrite") {
|
} else if (arg == "disable-overwrite") {
|
||||||
RestoreMsLoader();
|
RestoreMsLoader();
|
||||||
} else if (arg == "disable") {
|
} else if (arg == "disable") {
|
||||||
RestoreMsLoader();
|
Disable();
|
||||||
DisableEntry();
|
|
||||||
} else if (arg == "uninstall") {
|
} else if (arg == "uninstall") {
|
||||||
Uninstall();
|
Uninstall();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user