mirror of
https://github.com/Metabolix/HackBGRT.git
synced 2026-09-26 05:51:22 -07:00
Improve ESP detection and move to another file
This commit is contained in:
@@ -9,7 +9,7 @@ GNUEFI_LIB = /usr/$(CC_PREFIX)/lib
|
|||||||
|
|
||||||
FILES_C = src/main.c src/util.c src/types.c src/config.c
|
FILES_C = src/main.c src/util.c src/types.c src/config.c
|
||||||
FILES_H = $(wildcard src/*.h)
|
FILES_H = $(wildcard src/*.h)
|
||||||
FILES_CS = src/Setup.cs src/SetupHelper.cs
|
FILES_CS = src/Setup.cs src/SetupHelper.cs src/Esp.cs
|
||||||
GIT_DESCRIBE = $(firstword $(shell git describe --tags) unknown)
|
GIT_DESCRIBE = $(firstword $(shell git describe --tags) unknown)
|
||||||
CFLAGS += '-DGIT_DESCRIBE=L"$(GIT_DESCRIBE)"'
|
CFLAGS += '-DGIT_DESCRIBE=L"$(GIT_DESCRIBE)"'
|
||||||
ZIPDIR = HackBGRT-$(GIT_DESCRIBE:v%=%)
|
ZIPDIR = HackBGRT-$(GIT_DESCRIBE:v%=%)
|
||||||
|
|||||||
+118
@@ -0,0 +1,118 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EFI System Partition mounter.
|
||||||
|
*/
|
||||||
|
public sealed class Esp {
|
||||||
|
/** The singleton instance of this class, if Path is mounted. */
|
||||||
|
private static Esp MountInstance;
|
||||||
|
|
||||||
|
/** EFI System Partition location, internal variable. */
|
||||||
|
private static string RealPath;
|
||||||
|
|
||||||
|
/** EFI System Partition location, read-only. */
|
||||||
|
public static string Path {
|
||||||
|
get {
|
||||||
|
return RealPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor: do nothing.
|
||||||
|
*/
|
||||||
|
private Esp() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor: unmount.
|
||||||
|
*/
|
||||||
|
~Esp() {
|
||||||
|
if (this == MountInstance) {
|
||||||
|
Unmount();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to find ESP at a path.
|
||||||
|
*
|
||||||
|
* @param tryPath The new path to try.
|
||||||
|
* @param requireMsLoader Look for MS boot loader specifically?
|
||||||
|
* @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) {
|
||||||
|
Unmount();
|
||||||
|
}
|
||||||
|
RealPath = tryPath;
|
||||||
|
if (Path != null && Path != "") {
|
||||||
|
if (File.Exists(Path + "\\EFI\\Microsoft\\Boot\\bootmgfw.efi")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!requireMsLoader && Directory.Exists(Path + "\\EFI")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RealPath = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the EFI System Partition, if it's already mounted.
|
||||||
|
*
|
||||||
|
* @return true if the drive vas found.
|
||||||
|
*/
|
||||||
|
public static bool Find() {
|
||||||
|
if (MountInstance != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// Match "The EFI System Partition is mounted at E:\" with some language support.
|
||||||
|
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 + ":")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mount the EFI System Partition.
|
||||||
|
*
|
||||||
|
* @return true if the drive was mounted, false otherwise.
|
||||||
|
*/
|
||||||
|
public static bool Mount() {
|
||||||
|
if (MountInstance != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
for (char c = 'A'; c <= 'Z'; ++c) {
|
||||||
|
if (Setup.Execute("mountvol", c + ": /S", true) != null) {
|
||||||
|
MountInstance = new Esp();
|
||||||
|
if (TryPath(c + ":", false)) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
throw new Exception("Mounted ESP at " + c + ": but it seems to be invalid!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unmount the EFI System Partition, if necessary.
|
||||||
|
*/
|
||||||
|
private static void Unmount() {
|
||||||
|
if (MountInstance != null) {
|
||||||
|
Setup.Execute("mountvol", Path + " /D", true);
|
||||||
|
RealPath = null;
|
||||||
|
MountInstance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
-44
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,47 +15,22 @@ public class Setup: SetupHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* EFI System Partition mounter.
|
* Find or mount or manually choose the EFI System Partition.
|
||||||
*/
|
*/
|
||||||
public class ESP {
|
public static void InitEspPath() {
|
||||||
/** EFI System Partition mount point. */
|
if (!Esp.Find() && !Esp.Mount()) {
|
||||||
public string Path = null;
|
Console.WriteLine("EFI System Partition was not found.");
|
||||||
|
Console.WriteLine("Press enter to exit, or give ESP path here: ");
|
||||||
/** Was the ESP mounted by this instance? */
|
string s = Console.ReadLine();
|
||||||
private bool Mounted = false;
|
if (s.Length == 1) {
|
||||||
|
s = s + ":";
|
||||||
/**
|
|
||||||
* Mount the EFI System Partition, or determine an existing mount point.
|
|
||||||
*/
|
|
||||||
public void Mount() {
|
|
||||||
try {
|
|
||||||
// Match "The EFI System Partition is mounted at E:\" with some language support.
|
|
||||||
var re = new Regex(" EFI[^\n]*(?:\n[ \t]*)?([A-Z]:)\\\\");
|
|
||||||
Path = re.Match(Execute("mountvol", "", false)).Groups[1].Captures[0].Value;
|
|
||||||
return;
|
|
||||||
} catch {
|
|
||||||
}
|
}
|
||||||
// Try to mount somewhere.
|
if (!Esp.TryPath(s, true)) {
|
||||||
for (char c = 'A'; c <= 'Z'; ++c) {
|
Console.WriteLine("That's not a valid ESP path!");
|
||||||
if (Execute("mountvol", c + ": /S", true) != null) {
|
|
||||||
Console.WriteLine("The EFI System Partition is mounted in " + c + ":\\");
|
|
||||||
Path = c + ":";
|
|
||||||
Mounted = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw new SetupException("The EFI System Partition is not mounted.");
|
|
||||||
}
|
}
|
||||||
|
if (Esp.Path == null) {
|
||||||
/**
|
throw new SetupException("EFI System Partition was not found.");
|
||||||
* Unmount the EFI System Partition, if it was previously mounted by this instance.
|
|
||||||
*/
|
|
||||||
public void Unmount() {
|
|
||||||
if (Mounted) {
|
|
||||||
Execute("mountvol", Path + " /D", true);
|
|
||||||
Mounted = false;
|
|
||||||
Path = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,9 +248,8 @@ public class Setup: SetupHelper {
|
|||||||
* @param src Path to the installation files.
|
* @param src Path to the installation files.
|
||||||
*/
|
*/
|
||||||
public static void RunSetup(string src) {
|
public static void RunSetup(string src) {
|
||||||
ESP esp = new ESP();
|
|
||||||
try {
|
try {
|
||||||
esp.Mount();
|
InitEspPath();
|
||||||
int secureBoot = -1;
|
int secureBoot = -1;
|
||||||
try {
|
try {
|
||||||
secureBoot = (int) Registry.GetValue(
|
secureBoot = (int) Registry.GetValue(
|
||||||
@@ -309,8 +282,8 @@ public class Setup: SetupHelper {
|
|||||||
throw new SetupException("Invalid choice!");
|
throw new SetupException("Invalid choice!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
string hackbgrt = esp.Path + "\\EFI\\HackBGRT";
|
string hackbgrt = Esp.Path + "\\EFI\\HackBGRT";
|
||||||
BootLoaderInfo msloader = new BootLoaderInfo(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 {
|
||||||
@@ -343,8 +316,6 @@ public class Setup: SetupHelper {
|
|||||||
Console.WriteLine("Unexpected error!\n{0}", e.ToString());
|
Console.WriteLine("Unexpected error!\n{0}", e.ToString());
|
||||||
Console.WriteLine("If this is the most current release, please report this bug.");
|
Console.WriteLine("If this is the most current release, please report this bug.");
|
||||||
Environment.ExitCode = 1;
|
Environment.ExitCode = 1;
|
||||||
} finally {
|
|
||||||
esp.Unmount();
|
|
||||||
}
|
}
|
||||||
Console.WriteLine("Press any key to quit.");
|
Console.WriteLine("Press any key to quit.");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
|||||||
Reference in New Issue
Block a user