mirror of
https://github.com/Metabolix/HackBGRT.git
synced 2025-12-07 09:36:10 -08:00
Improve path handling for platform independency
This commit is contained in:
38
src/Esp.cs
38
src/Esp.cs
@@ -6,16 +6,16 @@ using System.Text.RegularExpressions;
|
||||
* EFI System Partition mounter.
|
||||
*/
|
||||
public sealed class Esp {
|
||||
/** The singleton instance of this class, if Path is mounted. */
|
||||
/** The singleton instance of this class, if ESP is mounted. */
|
||||
private static Esp MountInstance;
|
||||
|
||||
/** EFI System Partition location, internal variable. */
|
||||
private static string RealPath;
|
||||
/** EFI System Partition location. */
|
||||
public static string Location { get; private set; }
|
||||
|
||||
/** EFI System Partition location, read-only. */
|
||||
public static string Path {
|
||||
/** MS boot loader path on ESP. */
|
||||
public static string MsLoaderPath {
|
||||
get {
|
||||
return RealPath;
|
||||
return Path.Combine(new string[] { Location, "EFI", "Microsoft", "Boot", "bootmgfw.efi"});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,26 +42,26 @@ public sealed class Esp {
|
||||
* @return true if the path was given and seems valid, false otherwise.
|
||||
*/
|
||||
public static bool TryPath(string tryPath, bool requireMsLoader = true) {
|
||||
if (MountInstance != null && Path != null) {
|
||||
if (MountInstance != null && Location != null) {
|
||||
Unmount();
|
||||
}
|
||||
RealPath = tryPath;
|
||||
if (Path != null && Path != "") {
|
||||
if (File.Exists(Path + "\\EFI\\Microsoft\\Boot\\bootmgfw.efi")) {
|
||||
Location = tryPath;
|
||||
if (Location != null && Location != "") {
|
||||
if (File.Exists(MsLoaderPath)) {
|
||||
return true;
|
||||
}
|
||||
if (!requireMsLoader && Directory.Exists(Path + "\\EFI")) {
|
||||
if (!requireMsLoader && Directory.Exists(Path.Combine(Location, "EFI"))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
RealPath = null;
|
||||
Location = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the EFI System Partition, if it's already mounted.
|
||||
*
|
||||
* @return true if the drive vas found.
|
||||
* @return true if the drive was found.
|
||||
*/
|
||||
public static bool Find() {
|
||||
if (MountInstance != null) {
|
||||
@@ -69,14 +69,14 @@ public sealed class Esp {
|
||||
}
|
||||
try {
|
||||
// Match "The EFI System Partition is mounted at E:\" with some language support.
|
||||
var re = new Regex(" EFI[^\n]*(?:\n[ \t]*)?([A-Z]:)\\\\");
|
||||
var re = new Regex(" EFI[^\n]*(?:\n[ \t]*)?([A-Z]:\\\\)");
|
||||
if (TryPath(re.Match(Setup.Execute("mountvol", "", false)).Groups[1].Captures[0].Value)) {
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
for (char c = 'A'; c <= 'Z'; ++c) {
|
||||
if (TryPath(c + ":")) {
|
||||
if (TryPath(c + ":\\")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -95,10 +95,10 @@ public sealed class Esp {
|
||||
for (char c = 'A'; c <= 'Z'; ++c) {
|
||||
if (Setup.Execute("mountvol", c + ": /S", true) != null) {
|
||||
MountInstance = new Esp();
|
||||
if (TryPath(c + ":", false)) {
|
||||
if (TryPath(c + ":\\", false)) {
|
||||
return true;
|
||||
} else {
|
||||
throw new Exception("Mounted ESP at " + c + ": but it seems to be invalid!");
|
||||
throw new Exception("Mounted ESP at " + c + ":\\ but it seems to be invalid!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,8 +110,8 @@ public sealed class Esp {
|
||||
*/
|
||||
private static void Unmount() {
|
||||
if (MountInstance != null) {
|
||||
Setup.Execute("mountvol", Path + " /D", true);
|
||||
RealPath = null;
|
||||
Setup.Execute("mountvol", Location + " /D", true);
|
||||
Location = null;
|
||||
MountInstance = null;
|
||||
}
|
||||
}
|
||||
|
||||
51
src/Setup.cs
51
src/Setup.cs
@@ -23,6 +23,16 @@ public class Setup: SetupHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/** @var The target directory. */
|
||||
protected string InstallPath;
|
||||
|
||||
/** @var The backup MS boot loader path. */
|
||||
protected string BackupLoaderPath {
|
||||
get {
|
||||
return Path.Combine(InstallPath, "bootmgfw-original.efi");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find or mount or manually choose the EFI System Partition.
|
||||
*/
|
||||
@@ -38,10 +48,10 @@ public class Setup: SetupHelper {
|
||||
Console.WriteLine("That's not a valid ESP path!");
|
||||
}
|
||||
}
|
||||
if (Esp.Path == null) {
|
||||
if (Esp.Location == null) {
|
||||
throw new SetupException("EFI System Partition was not found.");
|
||||
}
|
||||
Console.WriteLine("EFI System Partition location is " + Esp.Path);
|
||||
Console.WriteLine("EFI System Partition location is " + Esp.Location);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,8 +127,8 @@ public class Setup: SetupHelper {
|
||||
*/
|
||||
protected void Install() {
|
||||
try {
|
||||
if (!Directory.Exists(Destination)) {
|
||||
Directory.CreateDirectory(Destination);
|
||||
if (!Directory.Exists(InstallPath)) {
|
||||
Directory.CreateDirectory(InstallPath);
|
||||
}
|
||||
} catch {
|
||||
throw new SetupException("Failed to copy files to ESP!");
|
||||
@@ -136,10 +146,10 @@ public class Setup: SetupHelper {
|
||||
throw new SetupException("Couldn't copy new HackBGRT to ESP.");
|
||||
}
|
||||
if (!File.Exists(Config)) {
|
||||
Copy(Source + "\\config.txt", Config);
|
||||
Copy("config.txt", Config);
|
||||
}
|
||||
if (!File.Exists(Splash)) {
|
||||
Copy(Source + "\\splash.bmp", Splash);
|
||||
Copy("splash.bmp", Splash);
|
||||
}
|
||||
Configure();
|
||||
if (!MsLoader.ReplaceWith(NewLoader)) {
|
||||
@@ -191,10 +201,10 @@ public class Setup: SetupHelper {
|
||||
*/
|
||||
protected void Uninstall() {
|
||||
try {
|
||||
Directory.Delete(Destination, true);
|
||||
Directory.Delete(InstallPath, true);
|
||||
Console.WriteLine("HackBGRT has been removed.");
|
||||
} catch {
|
||||
throw new SetupException("The directory " + Destination + " couldn't be removed.");
|
||||
throw new SetupException($"The directory {InstallPath} couldn't be removed.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,20 +254,19 @@ public class Setup: SetupHelper {
|
||||
}
|
||||
}
|
||||
|
||||
protected string Source, Destination, Config, Splash;
|
||||
protected string Config, Splash;
|
||||
protected BootLoaderInfo MsLoader, MsLoaderBackup, NewLoader, NewLoaderSource;
|
||||
protected string EfiArch;
|
||||
|
||||
/**
|
||||
* Initialize information for the Setup.
|
||||
*/
|
||||
protected Setup(string source, string esp) {
|
||||
Source = source;
|
||||
Destination = esp + "\\EFI\\HackBGRT";
|
||||
Config = Destination + "\\config.txt";
|
||||
Splash = Destination + "\\splash.bmp";
|
||||
MsLoaderBackup = new BootLoaderInfo(Destination + "\\bootmgfw-original.efi");
|
||||
MsLoader = new BootLoaderInfo(esp + "\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
|
||||
protected Setup() {
|
||||
InstallPath = Path.Combine(Esp.Location, "EFI", "HackBGRT");
|
||||
Config = Path.Combine(InstallPath, "config.txt");
|
||||
Splash = Path.Combine(InstallPath, "splash.bmp");
|
||||
MsLoaderBackup = new BootLoaderInfo(BackupLoaderPath);
|
||||
MsLoader = new BootLoaderInfo(Esp.MsLoaderPath);
|
||||
if (MsLoader.Type == BootLoaderType.MS) {
|
||||
EfiArch = MsLoader.Arch;
|
||||
} else if (MsLoaderBackup.Type == BootLoaderType.MS) {
|
||||
@@ -266,8 +275,8 @@ public class Setup: SetupHelper {
|
||||
throw new SetupException("Failed to detect MS boot loader!");
|
||||
}
|
||||
string loaderName = "boot" + EfiArch + ".efi";
|
||||
NewLoaderSource = new BootLoaderInfo(Source + "\\" + loaderName);
|
||||
NewLoader = new BootLoaderInfo(Destination + "\\" + loaderName);
|
||||
NewLoaderSource = new BootLoaderInfo(loaderName);
|
||||
NewLoader = new BootLoaderInfo(Path.Combine(InstallPath, loaderName));
|
||||
if (!NewLoaderSource.Exists) {
|
||||
throw new SetupException("Couldn't find required files! Missing: " + NewLoaderSource.Path);
|
||||
}
|
||||
@@ -320,14 +329,12 @@ public class Setup: SetupHelper {
|
||||
|
||||
/**
|
||||
* Run the setup.
|
||||
*
|
||||
* @param source Path to the installation files.
|
||||
*/
|
||||
public static void RunSetup(string source) {
|
||||
public static void RunSetup() {
|
||||
try {
|
||||
InitEspPath();
|
||||
HandleSecureBoot();
|
||||
var s = new Setup(source, Esp.Path);
|
||||
var s = new Setup();
|
||||
s.HandleUserAction();
|
||||
} catch (ExitSetup e) {
|
||||
Environment.ExitCode = e.Code;
|
||||
|
||||
@@ -99,6 +99,7 @@ public class SetupHelper {
|
||||
Environment.ExitCode = 1;
|
||||
return;
|
||||
}
|
||||
Setup.RunSetup(Path.GetDirectoryName(self));
|
||||
Directory.SetCurrentDirectory(Path.GetDirectoryName(self));
|
||||
Setup.RunSetup();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user