Fix BCDEdit dry run and add more logging

This commit is contained in:
Lauri Kenttä
2023-12-31 17:50:14 +02:00
parent 518d7c8a97
commit 9948e5a306

View File

@@ -398,6 +398,10 @@ public class Setup {
* Enable HackBGRT with bcdedit. * Enable HackBGRT with bcdedit.
*/ */
protected void EnableBCDEdit() { protected void EnableBCDEdit() {
if (DryRun) {
WriteLine("Dry run, skip enabling with BCDEdit.");
return;
}
try { 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 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; var guid = re.Match(Execute("bcdedit", "/copy {bootmgr} /d HackBGRT", true)).Value;
@@ -408,9 +412,11 @@ public class Setup {
} }
var fwbootmgr = "{fwbootmgr}"; var fwbootmgr = "{fwbootmgr}";
Execute("bcdedit", $"/set {fwbootmgr} displayorder {guid} /addfirst", true); Execute("bcdedit", $"/set {fwbootmgr} displayorder {guid} /addfirst", true);
WriteLine("Enabled NVRAM entry for HackBGRT with BCDEdit.");
// Verify that the entry was created. // Verify that the entry was created.
Execute("bcdedit", "/enum firmware", true); Execute("bcdedit", "/enum firmware", true);
Execute("bcdedit", $"/enum {guid}", true); Execute("bcdedit", $"/enum {guid}", true);
Efi.LogBootEntries();
} catch (Exception e) { } catch (Exception e) {
Log($"EnableBCDEdit failed: {e.ToString()}"); Log($"EnableBCDEdit failed: {e.ToString()}");
throw new SetupException("Failed to enable HackBGRT with BCDEdit!"); throw new SetupException("Failed to enable HackBGRT with BCDEdit!");
@@ -435,14 +441,18 @@ public class Setup {
} else if (entry.IndexOf("HackBGRT") >= 0) { } else if (entry.IndexOf("HackBGRT") >= 0) {
found = true; found = true;
Log($"Disabling HackBGRT entry {guid}."); Log($"Disabling HackBGRT entry {guid}.");
if (Execute("bcdedit", $"/delete {guid}", true) == null) { if (!DryRun && Execute("bcdedit", $"/delete {guid}", true) == null) {
Log($"DisableBCDEdit failed to delete {guid}."); Log($"DisableBCDEdit failed to delete {guid}.");
} else {
disabled = true;
} }
disabled = true;
} }
} }
if (found && !disabled) { if (found) {
throw new SetupException("Failed to disable HackBGRT with BCDEdit!"); if (!disabled) {
throw new SetupException("Failed to disable HackBGRT with BCDEdit!");
}
WriteLine("Disabled NVRAM entry for HackBGRT with BCDEdit.");
} }
} }
@@ -454,6 +464,7 @@ public class Setup {
WriteLine("Enabled NVRAM entry for HackBGRT."); WriteLine("Enabled NVRAM entry for HackBGRT.");
// Verify that the entry was created. // Verify that the entry was created.
Efi.LogBootEntries(); Efi.LogBootEntries();
Execute("bcdedit", "/enum firmware", true);
} }
/** /**