|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.File; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.nio.file.Paths; |
| 7 | +import java.nio.file.StandardCopyOption; |
| 8 | +import java.util.Scanner; |
| 9 | + |
| 10 | +public class EFIFixer { |
| 11 | + // Color codes |
| 12 | + private static final String RED = "\u001B[31m"; |
| 13 | + private static final String BLUE = "\u001B[34m"; |
| 14 | + private static final String GREEN = "\u001B[32m"; |
| 15 | + private static final String YELLOW = "\u001B[33m"; |
| 16 | + private static final String UL = "\u001B[4m"; |
| 17 | + private static final String RESET = "\u001B[0m"; |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + clearScreen(); |
| 21 | + coderMark(); |
| 22 | + |
| 23 | + String backupDir = getBackupDir(); |
| 24 | + String efiPartition = getEfiPartition(); |
| 25 | + String volEfiDir = "/Volumes/EFI/"; |
| 26 | + String volEfiEfi = "/Volumes/EFI/EFI"; |
| 27 | + |
| 28 | + System.out.println(YELLOW + "\nChoose a method: \n" + RESET); |
| 29 | + System.out.println("1. " + GREEN + "Format EFI partition and restore backup" + RESET); |
| 30 | + System.out.println("2. " + BLUE + "Delete contents of EFI partition and restore backup" + RESET); |
| 31 | + System.out.println("3. " + RED + "Exit" + RESET); |
| 32 | + |
| 33 | + System.out.print("\nEnter your choice: "); |
| 34 | + Scanner scanner = new Scanner(System.in); |
| 35 | + String choice = scanner.nextLine(); |
| 36 | + |
| 37 | + switch (choice) { |
| 38 | + case "1": |
| 39 | + System.out.println(GREEN + "Starting format process and backup..." + RESET); |
| 40 | + executeCommand("cd " + volEfiDir + " && cp -r EFI " + backupDir); |
| 41 | + executeCommand("sudo diskutil unmount " + efiPartition); |
| 42 | + executeCommand("sudo newfs_msdos -v EFI -F 32 " + efiPartition); |
| 43 | + executeCommand("sudo diskutil mount " + efiPartition); |
| 44 | + executeCommand("cd " + backupDir + " && cp -r EFI " + volEfiDir); |
| 45 | + System.out.println(GREEN + "Fixed EFI Partition Successfully.." + RESET); |
| 46 | + break; |
| 47 | + case "2": |
| 48 | + System.out.println(RED + "Starting deletion process and backup..." + RESET); |
| 49 | + executeCommand("cd " + volEfiDir + " && cp -r EFI " + backupDir); |
| 50 | + executeCommand("sudo rm -rf " + volEfiEfi); |
| 51 | + executeCommand("cd " + backupDir + " && cp -r EFI " + volEfiDir); |
| 52 | + System.out.println(GREEN + "Fixed EFI Partition Successfully.." + RESET); |
| 53 | + break; |
| 54 | + case "3": |
| 55 | + clearScreen(); |
| 56 | + coderMark(); |
| 57 | + System.out.println(GREEN + "Thanks for using this script!" + RESET); |
| 58 | + break; |
| 59 | + default: |
| 60 | + System.out.println(RED + "Invalid choice." + RESET); |
| 61 | + } |
| 62 | + |
| 63 | + scanner.close(); |
| 64 | + } |
| 65 | + |
| 66 | + private static void coderMark() { |
| 67 | + System.out.println( |
| 68 | + "╭━━━╮╱╱╱╱╱╱╱╱╱╱╱╱╱╭━━━┳╮\n" + |
| 69 | + "┃╭━━╯╱╱╱╱╱╱╱╱╱╱╱╱╱┃╭━━┫┃" + GREEN + "\n" + |
| 70 | + "┃╰━━┳╮╭┳━┳━━┳━━┳━╮┃╰━━┫┃╭╮╱╭┳━╮╭━╮\n" + |
| 71 | + "┃╭━━┫┃┃┃╭┫╭╮┃╭╮┃╭╮┫╭━━┫┃┃┃╱┃┃╭╮┫╭╮╮" + BLUE + "\n" + |
| 72 | + "┃┃╱╱┃╰╯┃┃┃╰╯┃╰╯┃┃┃┃┃╱╱┃╰┫╰━╯┃┃┃┃┃┃┃\n" + |
| 73 | + "╰╯╱╱╰━━┻╯╰━╮┣━━┻╯╰┻╯╱╱╰━┻━╮╭┻╯╰┻╯╰╯" + RESET + "\n" + |
| 74 | + "╱╱╱╱╱╱╱╱╱╱╱┃┃╱╱╱╱╱╱╱╱╱╱╱╭━╯┃\n" + |
| 75 | + "╱╱╱╱╱╱╱╱╱╱╱╰╯╱╱╱╱╱╱╱╱╱╱╱╰━━╯\n\n" + |
| 76 | + UL + GREEN + "EFI Fixer Hackintosh " + RESET + UL + "v0.1" + RESET |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + private static boolean isMounted(String partition) { |
| 81 | + try { |
| 82 | + Process process = Runtime.getRuntime().exec("mount"); |
| 83 | + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 84 | + String line; |
| 85 | + while ((line = reader.readLine()) != null) { |
| 86 | + if (line.contains(partition) && line.contains("type fat32")) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + } |
| 90 | + return false; |
| 91 | + } catch (Exception e) { |
| 92 | + System.out.println(RED + "Error checking mount status: " + e.getMessage() + RESET); |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static String getEfiPartition() { |
| 98 | + System.out.println(YELLOW + "\n(Executed diskutil list ~ [just copy EFI Partition])" + RESET); |
| 99 | + executeCommand("diskutil list"); |
| 100 | + System.out.print(GREEN + "\nEnter EFI partition directory" + RESET + " (e.g., /dev/disk2s1): "); |
| 101 | + Scanner scanner = new Scanner(System.in); |
| 102 | + String efiPartition = scanner.nextLine().trim(); |
| 103 | + |
| 104 | + if (!isMounted(efiPartition)) { |
| 105 | + System.out.println(RED + "Partition is not mounted or invalid.\n" + RESET); |
| 106 | + System.out.print(YELLOW + "Do you want to mount now?" + RED + " Sudo Password Required!" + RESET + " (y/n) "); |
| 107 | + String answer = scanner.nextLine().toLowerCase(); |
| 108 | + if (answer.equals("y")) { |
| 109 | + executeCommand("sudo diskutil mount " + efiPartition); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return efiPartition; |
| 114 | + } |
| 115 | + |
| 116 | + private static String getBackupDir() { |
| 117 | + System.out.println(YELLOW + "\n(e.g., Below using pwd command ~ [just copy and paste enter])" + RESET); |
| 118 | + executeCommand("pwd"); |
| 119 | + System.out.print(GREEN + "\nEnter Backup directory: " + RESET); |
| 120 | + Scanner scanner = new Scanner(System.in); |
| 121 | + return scanner.nextLine().trim(); |
| 122 | + } |
| 123 | + |
| 124 | + private static void executeCommand(String command) { |
| 125 | + try { |
| 126 | + Process process = Runtime.getRuntime().exec(command); |
| 127 | + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 128 | + String line; |
| 129 | + while ((line = reader.readLine()) != null) { |
| 130 | + System.out.println(line); |
| 131 | + } |
| 132 | + process.waitFor(); |
| 133 | + } catch (Exception e) { |
| 134 | + System.out.println(RED + "Error executing command: " + YELLOW + e.getMessage() + RESET); |
| 135 | + |
| 136 | + if (e.getMessage().contains("already unmounted") || e.getMessage().contains("failed to unmount")) { |
| 137 | + System.out.println(YELLOW + "Trying to force unmount..." + RESET); |
| 138 | + try { |
| 139 | + Runtime.getRuntime().exec("sudo diskutil unmount force " + getEfiPartition()); |
| 140 | + } catch (Exception ex) { |
| 141 | + System.out.println(RED + "Forceful unmount failed: " + YELLOW + ex.getMessage() + RESET); |
| 142 | + System.out.println(RED + "Please try rebooting your system and running the script again." + RESET); |
| 143 | + System.exit(1); |
| 144 | + } |
| 145 | + } else { |
| 146 | + System.out.println(RED + "Unhandled error: " + YELLOW + e.getMessage() + RESET); |
| 147 | + System.out.println(RED + "Please check the system logs for more details." + RESET); |
| 148 | + System.exit(1); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static void clearScreen() { |
| 154 | + System.out.print("\033[H\033[2J"); |
| 155 | + System.out.flush(); |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments