/** * Backup the original boot loader. * * @param hackbgrt Path to the HackBGRT directory on the ESP. * @param msloader Path of the MS boot loader to backup. * @return Information about the boot loader backup. */ public static BootLoaderInfo Backup(string hackbgrt, string msloader) { try { if (!Directory.Exists(hackbgrt)) { Directory.CreateDirectory(hackbgrt); } BootLoaderInfo info = RecognizeLoader(msloader); if (info.Type == BootLoaderType.HackBGRT) { Console.WriteLine(msloader + " already contains a version of HackBGRT, skipping backup."); } else if (info.Type == BootLoaderType.MS) { // Overwrite any previous backup, the file might have changed. Copy(msloader, hackbgrt + "\\bootmgfw-original.efi"); } else { Console.WriteLine(msloader + " doesn't look right, skipping backup."); } } catch { } BootLoaderInfo msbackup = RecognizeLoader(hackbgrt + "\\bootmgfw-original.efi"); if (!msbackup.Exists) { throw new SetupException("Couldn't backup the original bootmgfw.efi."); } if (msbackup.Arch == null || msbackup.Type != BootLoaderType.MS) { throw new SetupException("The boot loader backup (" + msbackup.Path + ") doesn't look like a supported MS boot loader"); } return(msbackup); }
/** * 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"); if (MsLoader.Type == BootLoaderType.MS) { EfiArch = MsLoader.Arch; } else if (MsLoaderBackup.Type == BootLoaderType.MS) { EfiArch = MsLoaderBackup.Arch; } else { throw new SetupException("Failed to detect MS boot loader!"); } string loaderName = "boot" + EfiArch + ".efi"; NewLoaderSource = new BootLoaderInfo(Source + "\\" + loaderName); NewLoader = new BootLoaderInfo(Destination + "\\" + loaderName); if (!NewLoaderSource.Exists) { throw new SetupException("Couldn't find required files! Missing: " + NewLoaderSource.Path); } }
/** * 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); } Exists = other.Exists; Type = other.Type; Arch = other.Arch; return(true); }
/** * Enable HackBGRT, replace the msloader with our own. * * @param hackbgrt Path to the HackBGRT directory on the ESP. * @param msloader Path of the MS boot loader to replace. * @param msbackup Information of the boot loader backup. */ public static void Enable(string hackbgrt, string msloader, BootLoaderInfo msbackup) { string loaderName = "boot" + msbackup.Arch + ".efi"; string loaderInst = hackbgrt + "\\" + loaderName; if (!File.Exists(loaderInst)) { throw new SetupException(loaderInst + " doesn't exist."); } if (!Copy(loaderInst, msloader)) { Copy(msbackup.Path, msloader); throw new SetupException("Couldn't install the new bootmgfw.efi."); } Console.WriteLine("HackBGRT is now enabled."); }
/** * Disable HackBGRT, restore the original boot loader. * * @param hackbgrt Where HackBGRT is installed. * @param msloader Where to restore the MS boot loader. */ public static void Disable(string hackbgrt, string msloader) { BootLoaderInfo info = RecognizeLoader(msloader); if (info.Type == BootLoaderType.MS) { Console.WriteLine(msloader + " is already ok."); } else { if (!File.Exists(hackbgrt + "\\bootmgfw-original.efi")) { throw new SetupException("Missing the original bootmgfw.efi."); } if (!Copy(hackbgrt + "\\bootmgfw-original.efi", msloader)) { throw new SetupException("Failed to restore the original bootmgfw.efi."); } Console.WriteLine(msloader + " has been restored."); } }
/** * Install HackBGRT, copy files and replace the msloader with our own. * * @param src Path to the installation files. * @param hackbgrt Path to the HackBGRT directory on the ESP. * @param msloader Path of the MS boot loader to replace. * @param msbackup Information of the boot loader backup. */ public static void Install(string src, string hackbgrt, string msloader, BootLoaderInfo msbackup) { string loaderName = "boot" + msbackup.Arch + ".efi"; string loaderSrc = src + "\\" + loaderName; string loaderInst = hackbgrt + "\\" + loaderName; if (!File.Exists(loaderSrc)) { throw new SetupException(loaderName + " doesn't exist."); } Copy(loaderSrc, loaderInst); if (!File.Exists(hackbgrt + "\\config.txt")) { Copy(src + "\\config.txt", hackbgrt + "\\config.txt"); } if (!File.Exists(hackbgrt + "\\splash.bmp")) { Copy(src + "\\splash.bmp", hackbgrt + "\\splash.bmp"); } Enable(hackbgrt, msloader, msbackup); Configure(hackbgrt); }