Improve path handling for platform independency

This commit is contained in:
Lauri Kenttä
2023-09-09 17:55:39 +03:00
parent 7d5f4eac17
commit 691fbd164b
3 changed files with 50 additions and 42 deletions
+19 -19
View File
@@ -6,16 +6,16 @@ using System.Text.RegularExpressions;
* EFI System Partition mounter. * EFI System Partition mounter.
*/ */
public sealed class Esp { 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; private static Esp MountInstance;
/** EFI System Partition location, internal variable. */ /** EFI System Partition location. */
private static string RealPath; public static string Location { get; private set; }
/** EFI System Partition location, read-only. */ /** MS boot loader path on ESP. */
public static string Path { public static string MsLoaderPath {
get { 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. * @return true if the path was given and seems valid, false otherwise.
*/ */
public static bool TryPath(string tryPath, bool requireMsLoader = true) { public static bool TryPath(string tryPath, bool requireMsLoader = true) {
if (MountInstance != null && Path != null) { if (MountInstance != null && Location != null) {
Unmount(); Unmount();
} }
RealPath = tryPath; Location = tryPath;
if (Path != null && Path != "") { if (Location != null && Location != "") {
if (File.Exists(Path + "\\EFI\\Microsoft\\Boot\\bootmgfw.efi")) { if (File.Exists(MsLoaderPath)) {
return true; return true;
} }
if (!requireMsLoader && Directory.Exists(Path + "\\EFI")) { if (!requireMsLoader && Directory.Exists(Path.Combine(Location, "EFI"))) {
return true; return true;
} }
} }
RealPath = null; Location = null;
return false; return false;
} }
/** /**
* Find the EFI System Partition, if it's already mounted. * 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() { public static bool Find() {
if (MountInstance != null) { if (MountInstance != null) {
@@ -69,14 +69,14 @@ public sealed class Esp {
} }
try { try {
// Match "The EFI System Partition is mounted at E:\" with some language support. // 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)) { if (TryPath(re.Match(Setup.Execute("mountvol", "", false)).Groups[1].Captures[0].Value)) {
return true; return true;
} }
} catch { } catch {
} }
for (char c = 'A'; c <= 'Z'; ++c) { for (char c = 'A'; c <= 'Z'; ++c) {
if (TryPath(c + ":")) { if (TryPath(c + ":\\")) {
return true; return true;
} }
} }
@@ -95,10 +95,10 @@ public sealed class Esp {
for (char c = 'A'; c <= 'Z'; ++c) { for (char c = 'A'; c <= 'Z'; ++c) {
if (Setup.Execute("mountvol", c + ": /S", true) != null) { if (Setup.Execute("mountvol", c + ": /S", true) != null) {
MountInstance = new Esp(); MountInstance = new Esp();
if (TryPath(c + ":", false)) { if (TryPath(c + ":\\", false)) {
return true; return true;
} else { } 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() { private static void Unmount() {
if (MountInstance != null) { if (MountInstance != null) {
Setup.Execute("mountvol", Path + " /D", true); Setup.Execute("mountvol", Location + " /D", true);
RealPath = null; Location = null;
MountInstance = null; MountInstance = null;
} }
} }
+29 -22
View File
@@ -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. * 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!"); 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."); 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() { protected void Install() {
try { try {
if (!Directory.Exists(Destination)) { if (!Directory.Exists(InstallPath)) {
Directory.CreateDirectory(Destination); Directory.CreateDirectory(InstallPath);
} }
} catch { } catch {
throw new SetupException("Failed to copy files to ESP!"); 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."); throw new SetupException("Couldn't copy new HackBGRT to ESP.");
} }
if (!File.Exists(Config)) { if (!File.Exists(Config)) {
Copy(Source + "\\config.txt", Config); Copy("config.txt", Config);
} }
if (!File.Exists(Splash)) { if (!File.Exists(Splash)) {
Copy(Source + "\\splash.bmp", Splash); Copy("splash.bmp", Splash);
} }
Configure(); Configure();
if (!MsLoader.ReplaceWith(NewLoader)) { if (!MsLoader.ReplaceWith(NewLoader)) {
@@ -191,10 +201,10 @@ public class Setup: SetupHelper {
*/ */
protected void Uninstall() { protected void Uninstall() {
try { try {
Directory.Delete(Destination, true); Directory.Delete(InstallPath, true);
Console.WriteLine("HackBGRT has been removed."); Console.WriteLine("HackBGRT has been removed.");
} catch { } 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 BootLoaderInfo MsLoader, MsLoaderBackup, NewLoader, NewLoaderSource;
protected string EfiArch; protected string EfiArch;
/** /**
* Initialize information for the Setup. * Initialize information for the Setup.
*/ */
protected Setup(string source, string esp) { protected Setup() {
Source = source; InstallPath = Path.Combine(Esp.Location, "EFI", "HackBGRT");
Destination = esp + "\\EFI\\HackBGRT"; Config = Path.Combine(InstallPath, "config.txt");
Config = Destination + "\\config.txt"; Splash = Path.Combine(InstallPath, "splash.bmp");
Splash = Destination + "\\splash.bmp"; MsLoaderBackup = new BootLoaderInfo(BackupLoaderPath);
MsLoaderBackup = new BootLoaderInfo(Destination + "\\bootmgfw-original.efi"); MsLoader = new BootLoaderInfo(Esp.MsLoaderPath);
MsLoader = new BootLoaderInfo(esp + "\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
if (MsLoader.Type == BootLoaderType.MS) { if (MsLoader.Type == BootLoaderType.MS) {
EfiArch = MsLoader.Arch; EfiArch = MsLoader.Arch;
} else if (MsLoaderBackup.Type == BootLoaderType.MS) { } else if (MsLoaderBackup.Type == BootLoaderType.MS) {
@@ -266,8 +275,8 @@ public class Setup: SetupHelper {
throw new SetupException("Failed to detect MS boot loader!"); throw new SetupException("Failed to detect MS boot loader!");
} }
string loaderName = "boot" + EfiArch + ".efi"; string loaderName = "boot" + EfiArch + ".efi";
NewLoaderSource = new BootLoaderInfo(Source + "\\" + loaderName); NewLoaderSource = new BootLoaderInfo(loaderName);
NewLoader = new BootLoaderInfo(Destination + "\\" + loaderName); NewLoader = new BootLoaderInfo(Path.Combine(InstallPath, loaderName));
if (!NewLoaderSource.Exists) { if (!NewLoaderSource.Exists) {
throw new SetupException("Couldn't find required files! Missing: " + NewLoaderSource.Path); throw new SetupException("Couldn't find required files! Missing: " + NewLoaderSource.Path);
} }
@@ -320,14 +329,12 @@ public class Setup: SetupHelper {
/** /**
* Run the setup. * Run the setup.
*
* @param source Path to the installation files.
*/ */
public static void RunSetup(string source) { public static void RunSetup() {
try { try {
InitEspPath(); InitEspPath();
HandleSecureBoot(); HandleSecureBoot();
var s = new Setup(source, Esp.Path); var s = new Setup();
s.HandleUserAction(); s.HandleUserAction();
} catch (ExitSetup e) { } catch (ExitSetup e) {
Environment.ExitCode = e.Code; Environment.ExitCode = e.Code;
+2 -1
View File
@@ -99,6 +99,7 @@ public class SetupHelper {
Environment.ExitCode = 1; Environment.ExitCode = 1;
return; return;
} }
Setup.RunSetup(Path.GetDirectoryName(self)); Directory.SetCurrentDirectory(Path.GetDirectoryName(self));
Setup.RunSetup();
} }
} }