|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "regexp" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/tensorworks/bootnext/internal/constants" |
| 13 | + "github.com/tensorworks/bootnext/internal/elevate" |
| 14 | + "github.com/tensorworks/bootnext/internal/process" |
| 15 | + "github.com/tensorworks/bootnext/internal/reboot" |
| 16 | + "github.com/tensorworks/bootnext/internal/uefi" |
| 17 | +) |
| 18 | + |
| 19 | +func run(pattern string, dryRun bool, listOnly bool, noElevate bool, noReboot bool) error { |
| 20 | + |
| 21 | + // Verify that the operating system has been booted in UEFI mode |
| 22 | + enabled, err := uefi.IsUEFIEnabled() |
| 23 | + if err != nil { |
| 24 | + return fmt.Errorf("failed to query system UEFI status: %v", err) |
| 25 | + } else if !enabled { |
| 26 | + return fmt.Errorf("unsupported system configuration: the operating system has not been booted in UEFI mode") |
| 27 | + } |
| 28 | + |
| 29 | + // Verify that all of the system tools we require for interacting with UEFI NVRAM variables are available |
| 30 | + requiredTools := uefi.RequiredTools() |
| 31 | + for _, tool := range requiredTools { |
| 32 | + if _, err := exec.LookPath(tool); err != nil { |
| 33 | + return fmt.Errorf("a required application was not found in the system PATH: %v", tool) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // Determine whether we require elevated privileges |
| 38 | + // (We need them for writing to NVRAM variables under Linux, and for both reading and writing under Windows) |
| 39 | + requireElevation := (!dryRun && !listOnly) || runtime.GOOS == "windows" |
| 40 | + |
| 41 | + // Determine whether the process is running with insufficient privileges |
| 42 | + if requireElevation && !elevate.IsElevated() { |
| 43 | + |
| 44 | + // Determine whether we should automatically request elevated privileges |
| 45 | + if !noElevate { |
| 46 | + |
| 47 | + // Re-run the process with elevated privileges and propagate the exit code |
| 48 | + exitCode, err := elevate.RunElevated() |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("failed to re-launch the process with elevated privileges: %v", err) |
| 51 | + } else { |
| 52 | + os.Exit(exitCode) |
| 53 | + } |
| 54 | + |
| 55 | + } else { |
| 56 | + fmt.Print("Warning: running without elevated privileges, access to UEFI NVRAM variables may be denied.\n\n") |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Retrieve the list of UEFI boot entries |
| 61 | + entries, err := uefi.ListBootEntries() |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to list UEFI boot entries: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + // Print the list of boot entries |
| 67 | + fmt.Println("Detected the following UEFI boot entries:") |
| 68 | + for _, entry := range entries { |
| 69 | + fmt.Print("- ID: \"", entry.ID, "\", Description: \"", entry.Description, "\"\n") |
| 70 | + } |
| 71 | + |
| 72 | + // If we are just listing the boot entries then stop here |
| 73 | + if listOnly { |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + // Compile the regular expression pattern supplied by the user, enabling case-insensitive matching |
| 78 | + regex, err := regexp.Compile(fmt.Sprintf("(?i)%s", pattern)) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to compile regular expression \"%s\": %v", pattern, err) |
| 81 | + } |
| 82 | + |
| 83 | + // Identify the first boot entry that matches the pattern |
| 84 | + fmt.Printf("\nMatching boot entries against regular expression \"%s\"\n", pattern) |
| 85 | + for _, entry := range entries { |
| 86 | + if regex.MatchString(entry.Description) { |
| 87 | + |
| 88 | + // Print the matching boot entry |
| 89 | + fmt.Printf("Found matching boot entry: \"%s\"\n", entry.Description) |
| 90 | + |
| 91 | + // Don't modify the BootNext variable or reboot if we are performing a dry run |
| 92 | + if !dryRun { |
| 93 | + |
| 94 | + // Set the value of the BootNext variable to the entry's identifier |
| 95 | + fmt.Println("Setting the BootNext variable...") |
| 96 | + if err := uefi.SetBootNext(entry); err != nil { |
| 97 | + return fmt.Errorf("failed to set BootNext variable value: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + // Determine whether we are triggering a reboot |
| 101 | + if !noReboot { |
| 102 | + fmt.Println("Rebooting now...") |
| 103 | + if err := reboot.Reboot(); err != nil { |
| 104 | + return fmt.Errorf("failed to reboot: %v", err) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // If we reach this point then none of the boot entries matched the pattern |
| 114 | + return fmt.Errorf("could not find any UEFI boot entries matching the pattern \"%s\"", pattern) |
| 115 | +} |
| 116 | + |
| 117 | +func main() { |
| 118 | + |
| 119 | + // Define our Cobra command |
| 120 | + command := &cobra.Command{ |
| 121 | + |
| 122 | + Long: strings.Join([]string{ |
| 123 | + fmt.Sprintf("bootnext v%s", constants.VERSION), |
| 124 | + "Copyright (c) 2023, TensorWorks Pty Ltd", |
| 125 | + "", |
| 126 | + "Sets the UEFI \"BootNext\" variable and triggers a reboot into the target operating system.", |
| 127 | + "This facilitates quickly switching to another OS without modifying the default boot order.", |
| 128 | + }, "\n"), |
| 129 | + |
| 130 | + Use: "bootnext pattern", |
| 131 | + |
| 132 | + SilenceUsage: true, |
| 133 | + |
| 134 | + Example: strings.Join([]string{ |
| 135 | + " bootnext windows Selects the Windows Boot Manager and boots into it", |
| 136 | + " bootnext ubuntu Selects the GRUB bootloader installed by Ubuntu Linux and boots into it", |
| 137 | + " bootnext USB Selects the first available bootable USB device and boots into it", |
| 138 | + }, "\n"), |
| 139 | + } |
| 140 | + |
| 141 | + // Inject the usage information for our command's positional arguments |
| 142 | + patternUsage := strings.Join([]string{ |
| 143 | + " pattern A regular expression that will be used to select the target boot entry", |
| 144 | + " (case insensitive)", |
| 145 | + }, "\n") |
| 146 | + template := command.UsageTemplate() |
| 147 | + template = strings.Replace(template, "\nFlags:\n", fmt.Sprintf("\nPositional Arguments:\n%s\n\nFlags:\n", patternUsage), 1) |
| 148 | + command.SetUsageTemplate(template) |
| 149 | + |
| 150 | + // Define the command-line flags for our command |
| 151 | + dryRun := command.Flags().Bool("dry-run", false, "Describe the actions that would be performed but do not make any changes to the system") |
| 152 | + listOnly := command.Flags().Bool("list", false, "Print the list of UEFI boot entries but do not set the BootNext variable") |
| 153 | + noElevate := command.Flags().Bool("no-elevate", false, "Do not automatically prompt for elevated privileges when required") |
| 154 | + noReboot := command.Flags().Bool("no-reboot", false, "Do not automatically reboot after setting the BootNext variable") |
| 155 | + pause := command.Flags().Bool("pause", false, "Pause for input when the application is finished running") |
| 156 | + |
| 157 | + // Wire up the validation logic for our command-line flags and positional arguments |
| 158 | + command.RunE = func(cmd *cobra.Command, args []string) error { |
| 159 | + |
| 160 | + // If no flags or arguments were specified then print the usage message |
| 161 | + if len(os.Args) < 2 { |
| 162 | + cmd.Help() |
| 163 | + return nil |
| 164 | + } |
| 165 | + |
| 166 | + // Verify that a pattern was provided if `--list` was not specified |
| 167 | + pattern := "" |
| 168 | + if len(args) > 0 { |
| 169 | + pattern = args[0] |
| 170 | + } else if !*listOnly { |
| 171 | + return fmt.Errorf("a pattern must be specified for selecting the target UEFI boot entry") |
| 172 | + } |
| 173 | + |
| 174 | + // Process the provided input values and propagate any errors |
| 175 | + return run(pattern, *dryRun, *listOnly, *noElevate, *noReboot) |
| 176 | + } |
| 177 | + |
| 178 | + // Execute the command |
| 179 | + err := command.Execute() |
| 180 | + if err != nil { |
| 181 | + process.ExitWithPause(1, *pause) |
| 182 | + } |
| 183 | + |
| 184 | + process.ExitWithPause(0, *pause) |
| 185 | +} |
0 commit comments