mirror of
https://github.com/Metabolix/HackBGRT.git
synced 2026-09-26 05:51:22 -07:00
Reorder some BootLoaderInfo code
This commit is contained in:
+72
-55
@@ -72,69 +72,86 @@ public class Setup: SetupHelper {
|
|||||||
/**
|
/**
|
||||||
* Information about a boot loader: type and architecture.
|
* Information about a boot loader: type and architecture.
|
||||||
*/
|
*/
|
||||||
public struct BootLoaderInfo {
|
public class BootLoaderInfo {
|
||||||
public string Path;
|
public string Path;
|
||||||
public bool Exists;
|
public bool Exists;
|
||||||
public BootLoaderType Type;
|
public BootLoaderType Type;
|
||||||
public string Arch;
|
public string Arch;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recognize the boot loader type (MS/HackBGRT) and architecture.
|
* Constructor. Also recognizes the boot loader.
|
||||||
*
|
*/
|
||||||
* @param path Path to the boot loader.
|
public BootLoaderInfo(string path) {
|
||||||
* @return Information about the boot loader.
|
Path = path;
|
||||||
*/
|
Detect();
|
||||||
public static BootLoaderInfo RecognizeLoader(string path) {
|
}
|
||||||
BootLoaderInfo info;
|
|
||||||
info.Path = path;
|
/**
|
||||||
info.Exists = false;
|
* Recognize the boot loader type (MS/HackBGRT) and architecture.
|
||||||
info.Type = BootLoaderType.Unknown;
|
*/
|
||||||
info.Arch = null;
|
public void Detect() {
|
||||||
try {
|
Exists = false;
|
||||||
byte[] data = File.ReadAllBytes(path);
|
Type = BootLoaderType.Unknown;
|
||||||
info.Exists = true;
|
Arch = null;
|
||||||
string tmp = System.Text.Encoding.ASCII.GetString(data);
|
try {
|
||||||
if (tmp.IndexOf("HackBGRT") >= 0) {
|
byte[] data = File.ReadAllBytes(Path);
|
||||||
info.Type = BootLoaderType.HackBGRT;
|
Exists = true;
|
||||||
} else if (tmp.IndexOf("Microsoft Corporation") >= 0) {
|
string tmp = System.Text.Encoding.ASCII.GetString(data);
|
||||||
info.Type = BootLoaderType.MS;
|
if (tmp.IndexOf("HackBGRT") >= 0) {
|
||||||
|
Type = BootLoaderType.HackBGRT;
|
||||||
|
} else if (tmp.IndexOf("Microsoft Corporation") >= 0) {
|
||||||
|
Type = BootLoaderType.MS;
|
||||||
|
}
|
||||||
|
switch (BitConverter.ToUInt16(data, BitConverter.ToInt32(data, 0x3c) + 4)) {
|
||||||
|
case 0x014c: Arch = "ia32"; break;
|
||||||
|
case 0x0200: Arch = "ia64"; break;
|
||||||
|
case 0x8664: Arch = "x64"; break;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
}
|
}
|
||||||
switch (BitConverter.ToUInt16(data, BitConverter.ToInt32(data, 0x3c) + 4)) {
|
}
|
||||||
case 0x014c: info.Arch = "ia32"; break;
|
|
||||||
case 0x0200: info.Arch = "ia64"; break;
|
/**
|
||||||
case 0x8664: info.Arch = "x64"; break;
|
* Replace this boot loader with another.
|
||||||
|
*
|
||||||
|
* @param other The new boot loader.
|
||||||
|
* @return true if the replacement was successful.
|
||||||
|
*/
|
||||||
|
public bool ReplaceWith(BootLoaderInfo other) {
|
||||||
|
if (!other.Exists || !Copy(other.Path, Path)) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
} catch {
|
Exists = other.Exists;
|
||||||
|
Type = other.Type;
|
||||||
|
Arch = other.Arch;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return info;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Backup the original boot loader.
|
* Backup the original boot loader.
|
||||||
*
|
*
|
||||||
* @param hackbgrt Path to the HackBGRT directory on the ESP.
|
* @param hackbgrt Path to the HackBGRT directory on the ESP.
|
||||||
* @param msloader Path of the MS boot loader to backup.
|
* @param msloader Information of the MS boot loader to backup.
|
||||||
* @return Information about the boot loader backup.
|
* @return Information about the boot loader backup.
|
||||||
*/
|
*/
|
||||||
public static BootLoaderInfo Backup(string hackbgrt, string msloader) {
|
public static BootLoaderInfo Backup(string hackbgrt, BootLoaderInfo msloader) {
|
||||||
|
BootLoaderInfo msbackup = new BootLoaderInfo(hackbgrt + "\\bootmgfw-original.efi");
|
||||||
try {
|
try {
|
||||||
if (!Directory.Exists(hackbgrt)) {
|
if (!Directory.Exists(hackbgrt)) {
|
||||||
Directory.CreateDirectory(hackbgrt);
|
Directory.CreateDirectory(hackbgrt);
|
||||||
}
|
}
|
||||||
BootLoaderInfo info = RecognizeLoader(msloader);
|
if (msloader.Type == BootLoaderType.HackBGRT) {
|
||||||
if (info.Type == BootLoaderType.HackBGRT) {
|
Console.WriteLine(msloader.Path + " already contains a version of HackBGRT, skipping backup.");
|
||||||
Console.WriteLine(msloader + " already contains a version of HackBGRT, skipping backup.");
|
} else if (msloader.Type == BootLoaderType.MS) {
|
||||||
} else if (info.Type == BootLoaderType.MS) {
|
|
||||||
// Overwrite any previous backup, the file might have changed.
|
// Overwrite any previous backup, the file might have changed.
|
||||||
SetupHelper.Copy(msloader, hackbgrt + "\\bootmgfw-original.efi");
|
if (!msbackup.ReplaceWith(msloader)) {
|
||||||
|
throw new SetupException("Couldn't backup the original bootmgfw.efi.");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Console.WriteLine(msloader + " doesn't look right, skipping backup.");
|
Console.WriteLine(msloader.Path + " doesn't look right, skipping backup.");
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
}
|
|
||||||
BootLoaderInfo msbackup = RecognizeLoader(hackbgrt + "\\bootmgfw-original.efi");
|
|
||||||
if (!msbackup.Exists) {
|
|
||||||
throw new SetupException("Couldn't backup the original bootmgfw.efi.");
|
throw new SetupException("Couldn't backup the original bootmgfw.efi.");
|
||||||
}
|
}
|
||||||
if (msbackup.Arch == null || msbackup.Type != BootLoaderType.MS) {
|
if (msbackup.Arch == null || msbackup.Type != BootLoaderType.MS) {
|
||||||
@@ -148,10 +165,10 @@ public class Setup: SetupHelper {
|
|||||||
*
|
*
|
||||||
* @param src Path to the installation files.
|
* @param src Path to the installation files.
|
||||||
* @param hackbgrt Path to the HackBGRT directory on the ESP.
|
* @param hackbgrt Path to the HackBGRT directory on the ESP.
|
||||||
* @param msloader Path of the MS boot loader to replace.
|
* @param msloader Information of the MS boot loader to replace.
|
||||||
* @param msbackup Information of the boot loader backup.
|
* @param msbackup Information of the boot loader backup.
|
||||||
*/
|
*/
|
||||||
public static void Install(string src, string hackbgrt, string msloader, BootLoaderInfo msbackup) {
|
public static void Install(string src, string hackbgrt, BootLoaderInfo msloader, BootLoaderInfo msbackup) {
|
||||||
string loaderName = "boot" + msbackup.Arch + ".efi";
|
string loaderName = "boot" + msbackup.Arch + ".efi";
|
||||||
string loaderSrc = src + "\\" + loaderName;
|
string loaderSrc = src + "\\" + loaderName;
|
||||||
string loaderInst = hackbgrt + "\\" + loaderName;
|
string loaderInst = hackbgrt + "\\" + loaderName;
|
||||||
@@ -173,17 +190,17 @@ public class Setup: SetupHelper {
|
|||||||
* Enable HackBGRT, replace the msloader with our own.
|
* Enable HackBGRT, replace the msloader with our own.
|
||||||
*
|
*
|
||||||
* @param hackbgrt Path to the HackBGRT directory on the ESP.
|
* @param hackbgrt Path to the HackBGRT directory on the ESP.
|
||||||
* @param msloader Path of the MS boot loader to replace.
|
* @param msloader Information of the MS boot loader to replace.
|
||||||
* @param msbackup Information of the boot loader backup.
|
* @param msbackup Information of the boot loader backup.
|
||||||
*/
|
*/
|
||||||
public static void Enable(string hackbgrt, string msloader, BootLoaderInfo msbackup) {
|
public static void Enable(string hackbgrt, BootLoaderInfo msloader, BootLoaderInfo msbackup) {
|
||||||
string loaderName = "boot" + msbackup.Arch + ".efi";
|
string loaderName = "boot" + msbackup.Arch + ".efi";
|
||||||
string loaderInst = hackbgrt + "\\" + loaderName;
|
BootLoaderInfo loaderInst = new BootLoaderInfo(hackbgrt + "\\" + loaderName);
|
||||||
if (!File.Exists(loaderInst)) {
|
if (!loaderInst.Exists) {
|
||||||
throw new SetupException(loaderInst + " doesn't exist.");
|
throw new SetupException(loaderInst + " doesn't exist.");
|
||||||
}
|
}
|
||||||
if (!SetupHelper.Copy(loaderInst, msloader)) {
|
if (!msloader.ReplaceWith(loaderInst)) {
|
||||||
SetupHelper.Copy(msbackup.Path, msloader);
|
msloader.ReplaceWith(msbackup);
|
||||||
throw new SetupException("Couldn't install the new bootmgfw.efi.");
|
throw new SetupException("Couldn't install the new bootmgfw.efi.");
|
||||||
}
|
}
|
||||||
Console.WriteLine("HackBGRT is now enabled.");
|
Console.WriteLine("HackBGRT is now enabled.");
|
||||||
@@ -220,18 +237,18 @@ public class Setup: SetupHelper {
|
|||||||
* @param hackbgrt Where HackBGRT is installed.
|
* @param hackbgrt Where HackBGRT is installed.
|
||||||
* @param msloader Where to restore the MS boot loader.
|
* @param msloader Where to restore the MS boot loader.
|
||||||
*/
|
*/
|
||||||
public static void Disable(string hackbgrt, string msloader) {
|
public static void Disable(string hackbgrt, BootLoaderInfo msloader) {
|
||||||
BootLoaderInfo info = RecognizeLoader(msloader);
|
BootLoaderInfo msbackup = new BootLoaderInfo(hackbgrt + "\\bootmgfw-original.efi");
|
||||||
if (info.Type == BootLoaderType.MS) {
|
if (msloader.Type == BootLoaderType.MS) {
|
||||||
Console.WriteLine(msloader + " is already ok.");
|
Console.WriteLine(msloader + " is already ok.");
|
||||||
} else {
|
} else {
|
||||||
if (!File.Exists(hackbgrt + "\\bootmgfw-original.efi")) {
|
if (!msbackup.Exists) {
|
||||||
throw new SetupException("Missing the original bootmgfw.efi.");
|
throw new SetupException("Missing the original bootmgfw.efi.");
|
||||||
}
|
}
|
||||||
if (!SetupHelper.Copy(hackbgrt + "\\bootmgfw-original.efi", msloader)) {
|
if (!msloader.ReplaceWith(msbackup)) {
|
||||||
throw new SetupException("Failed to restore the original bootmgfw.efi.");
|
throw new SetupException("Failed to restore the original bootmgfw.efi.");
|
||||||
}
|
}
|
||||||
Console.WriteLine(msloader + " has been restored.");
|
Console.WriteLine(msloader.Path + " has been restored.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,7 +258,7 @@ public class Setup: SetupHelper {
|
|||||||
* @param hackbgrt Where HackBGRT is installed.
|
* @param hackbgrt Where HackBGRT is installed.
|
||||||
* @param msloader Where to restore the MS boot loader.
|
* @param msloader Where to restore the MS boot loader.
|
||||||
*/
|
*/
|
||||||
public static void Uninstall(string hackbgrt, string msloader) {
|
public static void Uninstall(string hackbgrt, BootLoaderInfo msloader) {
|
||||||
Disable(hackbgrt, msloader);
|
Disable(hackbgrt, msloader);
|
||||||
try {
|
try {
|
||||||
Directory.Delete(hackbgrt, true);
|
Directory.Delete(hackbgrt, true);
|
||||||
@@ -293,7 +310,7 @@ public class Setup: SetupHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
string hackbgrt = esp.Path + "\\EFI\\HackBGRT";
|
string hackbgrt = esp.Path + "\\EFI\\HackBGRT";
|
||||||
string msloader = esp.Path + "\\EFI\\Microsoft\\Boot\\bootmgfw.efi";
|
BootLoaderInfo msloader = new BootLoaderInfo(esp.Path + "\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
|
||||||
if (!Directory.Exists(hackbgrt)) {
|
if (!Directory.Exists(hackbgrt)) {
|
||||||
Install(src, hackbgrt, msloader, Backup(hackbgrt, msloader));
|
Install(src, hackbgrt, msloader, Backup(hackbgrt, msloader));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user