Fix other
This commit is contained in:
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
|
||||||
|
{
|
||||||
|
"name": "Launch Package",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${workspaceFolder}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
126
main.go
126
main.go
@@ -1,28 +1,21 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"Win2Linux/pkg/bcdedit"
|
||||||
"Win2Linux/pkg/windows"
|
"Win2Linux/pkg/windows"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"regexp"
|
"Win2Linux/pkg/errors"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/getlantern/systray"
|
"github.com/getlantern/systray"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate go-winres make
|
//go:generate go-winres make
|
||||||
|
|
||||||
type (
|
const (
|
||||||
Entry struct {
|
GeneralFailure int32 = 0x01
|
||||||
Key string
|
|
||||||
Attributes map[string]string
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
r *regexp.Regexp = regexp.MustCompile(`-+`)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed tray.ico
|
//go:embed tray.ico
|
||||||
@@ -35,10 +28,17 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func onReady() {
|
func onReady() {
|
||||||
entries := list()
|
|
||||||
systray.SetTitle("Win2Linux")
|
systray.SetTitle("Win2Linux")
|
||||||
systray.SetTooltip("Switch from Windows to Linux")
|
systray.SetTooltip("Switch from Windows to Linux")
|
||||||
systray.SetIcon(icon)
|
systray.SetIcon(icon)
|
||||||
|
entries, err := bcdedit.List()
|
||||||
|
if err != nil {
|
||||||
|
if wErr, ok := err.(*errors.WinError); ok {
|
||||||
|
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), int(wErr.ExitCode()))
|
||||||
|
} else {
|
||||||
|
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
fmt.Println(entry.Attributes)
|
fmt.Println(entry.Attributes)
|
||||||
@@ -49,14 +49,29 @@ func onReady() {
|
|||||||
}
|
}
|
||||||
mCustom := systray.AddMenuItem(key, "Switch to"+key)
|
mCustom := systray.AddMenuItem(key, "Switch to"+key)
|
||||||
go func() {
|
go func() {
|
||||||
|
for {
|
||||||
<-mCustom.ClickedCh
|
<-mCustom.ClickedCh
|
||||||
if uuid == "{fwbootmgr}" {
|
if uuid == "{fwbootmgr}" {
|
||||||
rebootToFirmware()
|
err := bcdedit.RebootToFirmware()
|
||||||
|
if err != nil {
|
||||||
|
if wErr, ok := err.(*errors.WinError); ok {
|
||||||
|
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), int(wErr.ExitCode()))
|
||||||
|
} else {
|
||||||
|
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reboot(uuid)
|
err := bcdedit.Reboot(uuid)
|
||||||
|
if err != nil {
|
||||||
|
if wErr, ok := err.(*errors.WinError); ok {
|
||||||
|
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), int(wErr.ExitCode()))
|
||||||
|
} else {
|
||||||
|
windows.Fatal("Win2Linux", fmt.Sprintf("error: %s", err), 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
}
|
}
|
||||||
systray.AddSeparator()
|
systray.AddSeparator()
|
||||||
mQuit := systray.AddMenuItem("Quit", "Quit")
|
mQuit := systray.AddMenuItem("Quit", "Quit")
|
||||||
@@ -70,82 +85,3 @@ func onReady() {
|
|||||||
func onExit() {
|
func onExit() {
|
||||||
// clean up here
|
// clean up here
|
||||||
}
|
}
|
||||||
|
|
||||||
func list() []Entry {
|
|
||||||
cmd := exec.Command("bcdedit", "/enum", "firmware")
|
|
||||||
out, err := cmd.Output()
|
|
||||||
if err != nil {
|
|
||||||
fatal(fmt.Sprintf("failed to run bcdedit: %s\n\nRun this program in administrator mode", err), 1)
|
|
||||||
}
|
|
||||||
return parse(string(out))
|
|
||||||
}
|
|
||||||
|
|
||||||
func parse(out string) []Entry {
|
|
||||||
lines := strings.Split(out, "\r\n")
|
|
||||||
section := false
|
|
||||||
lastLine := ""
|
|
||||||
secName := ""
|
|
||||||
a := make(map[string][]string)
|
|
||||||
for _, l := range lines {
|
|
||||||
if !section {
|
|
||||||
if r.Match([]byte(l)) {
|
|
||||||
secName = lastLine
|
|
||||||
section = true
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if len(l) != 0 {
|
|
||||||
a[secName] = append(a[secName], l)
|
|
||||||
} else {
|
|
||||||
section = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lastLine = l
|
|
||||||
}
|
|
||||||
|
|
||||||
var entries []Entry
|
|
||||||
for k, sec := range a {
|
|
||||||
entry := Entry{
|
|
||||||
Key: k,
|
|
||||||
Attributes: make(map[string]string),
|
|
||||||
}
|
|
||||||
for _, l := range sec {
|
|
||||||
l = strings.Join(strings.Fields(l), " ")
|
|
||||||
val := strings.Split(l, " ")
|
|
||||||
if len(val) != 2 {
|
|
||||||
if len(val) > 2 {
|
|
||||||
val[1] = strings.Join(val[1:], " ")
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
entry.Attributes[val[0]] = val[1]
|
|
||||||
}
|
|
||||||
entries = append(entries, entry)
|
|
||||||
}
|
|
||||||
return entries
|
|
||||||
}
|
|
||||||
|
|
||||||
func reboot(uuid string) {
|
|
||||||
out, err := exec.Command("bcdedit", "/Set", "{fwbootmgr}", "BootSequence", uuid, "/addFirst").Output()
|
|
||||||
if err != nil {
|
|
||||||
windows.MessageBox(windows.NULL, fmt.Sprintf("failed to execute bcdedit: %s", out), "Win2Linux", windows.MB_OK)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err = exec.Command("shutdown", "/r", "/t", "0").Output()
|
|
||||||
if err != nil {
|
|
||||||
windows.MessageBox(windows.NULL, fmt.Sprintf("failed to restart the computer: %s", out), "Win2Linux", windows.MB_OK)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func rebootToFirmware() {
|
|
||||||
out, err := exec.Command("shutdown", "/r", "/fw", "/t", "0").Output()
|
|
||||||
if err != nil {
|
|
||||||
windows.MessageBox(windows.NULL, fmt.Sprintf("failed to restart the computer: %s", out), "Win2Linux", windows.MB_OK)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fatal(message string, exitCode int) {
|
|
||||||
windows.MessageBox(windows.NULL, message, "Win2Linux", windows.MB_OK)
|
|
||||||
os.Exit(exitCode)
|
|
||||||
}
|
|
||||||
|
|||||||
119
pkg/bcdedit/bcdedit.go
Normal file
119
pkg/bcdedit/bcdedit.go
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package bcdedit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Win2Linux/pkg/windows/cmd"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Entry struct {
|
||||||
|
Key string
|
||||||
|
Attributes map[string]string
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
r *regexp.Regexp = regexp.MustCompile(`-+`)
|
||||||
|
)
|
||||||
|
|
||||||
|
func List() ([]Entry, error) {
|
||||||
|
args := []string{
|
||||||
|
"bcdedit",
|
||||||
|
"/enum",
|
||||||
|
"firmware",
|
||||||
|
}
|
||||||
|
out, err := cmd.Execute(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return parse(out), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Reboot(uuid string) error {
|
||||||
|
args := []string{
|
||||||
|
"bcdedit",
|
||||||
|
"/bootsequence",
|
||||||
|
uuid,
|
||||||
|
"/addfirst",
|
||||||
|
}
|
||||||
|
_, err := cmd.Execute(args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
args = []string{
|
||||||
|
"shutdown",
|
||||||
|
"/r",
|
||||||
|
"/t",
|
||||||
|
"0",
|
||||||
|
}
|
||||||
|
_, err = cmd.Execute(args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RebootToFirmware() error {
|
||||||
|
args := []string{
|
||||||
|
"shutdown",
|
||||||
|
"/r",
|
||||||
|
"/fw",
|
||||||
|
"/t",
|
||||||
|
"0",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := cmd.Execute(args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parse(out string) []Entry {
|
||||||
|
lines := strings.Split(out, "\r\n")
|
||||||
|
section := false
|
||||||
|
lastLine := ""
|
||||||
|
secName := ""
|
||||||
|
a := make(map[string][]string)
|
||||||
|
for _, l := range lines {
|
||||||
|
if !section {
|
||||||
|
if r.Match([]byte(l)) {
|
||||||
|
secName = lastLine
|
||||||
|
section = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if len(l) != 0 {
|
||||||
|
a[secName] = append(a[secName], l)
|
||||||
|
} else {
|
||||||
|
section = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastLine = l
|
||||||
|
}
|
||||||
|
|
||||||
|
var entries []Entry
|
||||||
|
for k, sec := range a {
|
||||||
|
entry := Entry{
|
||||||
|
Key: k,
|
||||||
|
Attributes: make(map[string]string),
|
||||||
|
}
|
||||||
|
for _, l := range sec {
|
||||||
|
l = strings.Join(strings.Fields(l), " ")
|
||||||
|
val := strings.Split(l, " ")
|
||||||
|
if len(val) != 2 {
|
||||||
|
if len(val) > 2 {
|
||||||
|
val[1] = strings.Join(val[1:], " ")
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entry.Attributes[val[0]] = val[1]
|
||||||
|
}
|
||||||
|
entries = append(entries, entry)
|
||||||
|
}
|
||||||
|
return entries
|
||||||
|
}
|
||||||
25
pkg/errors/errors.go
Normal file
25
pkg/errors/errors.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type (
|
||||||
|
WinError struct {
|
||||||
|
err string
|
||||||
|
_code uint32
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(err any, exitCode uint32) *WinError {
|
||||||
|
return &WinError{
|
||||||
|
err: fmt.Sprintf("%v", err),
|
||||||
|
_code: exitCode,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *WinError) Error() string {
|
||||||
|
return e.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *WinError) ExitCode() uint32 {
|
||||||
|
return e._code
|
||||||
|
}
|
||||||
18
pkg/windows/cmd/cmd.go
Normal file
18
pkg/windows/cmd/cmd.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Win2Linux/pkg/errors"
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Execute(c []string) (string, error) {
|
||||||
|
a := strings.Join(c, " ")
|
||||||
|
cmd := exec.Command("cmd", "/d", "/c", "chcp 65001>nul && "+a)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.New(fmt.Sprintf("failed to run the command: %s: %s", err, out), uint32(cmd.ProcessState.ExitCode()))
|
||||||
|
}
|
||||||
|
return string(out), nil
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package windows
|
package windows
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
@@ -10,8 +11,13 @@ const (
|
|||||||
MB_OK = 0
|
MB_OK = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Fatal(title, message string, exitCode int) {
|
||||||
|
messageBox(NULL, message, title, MB_OK)
|
||||||
|
os.Exit(exitCode)
|
||||||
|
}
|
||||||
|
|
||||||
// MessageBox of Win32 API.
|
// MessageBox of Win32 API.
|
||||||
func MessageBox(hwnd uintptr, caption, title string, flags uint) int {
|
func messageBox(hwnd uintptr, caption, title string, flags uint) int {
|
||||||
_caption, err := syscall.UTF16PtrFromString(caption)
|
_caption, err := syscall.UTF16PtrFromString(caption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
"minimum-os": "win10",
|
"minimum-os": "win10",
|
||||||
"execution-level": "requireAdministrator",
|
"execution-level": "requireAdministrator",
|
||||||
"ui-access": false,
|
"ui-access": false,
|
||||||
"auto-elevate": false,
|
"auto-elevate": true,
|
||||||
"dpi-awareness": "system",
|
"dpi-awareness": "system",
|
||||||
"disable-theming": false,
|
"disable-theming": false,
|
||||||
"disable-window-filtering": false,
|
"disable-window-filtering": false,
|
||||||
@@ -36,15 +36,15 @@
|
|||||||
"#1": {
|
"#1": {
|
||||||
"0000": {
|
"0000": {
|
||||||
"fixed": {
|
"fixed": {
|
||||||
"file_version": "0.0.1.0",
|
"file_version": "0.0.3.0",
|
||||||
"product_version": "0.0.1.0"
|
"product_version": "0.0.3.0"
|
||||||
},
|
},
|
||||||
"info": {
|
"info": {
|
||||||
"0409": {
|
"0409": {
|
||||||
"Comments": "Switch from Windows to Linux",
|
"Comments": "Switch from Windows to Linux",
|
||||||
"CompanyName": "Aurelie Delhaie",
|
"CompanyName": "Aurelie Delhaie",
|
||||||
"ProductName": "Win2Linux",
|
"ProductName": "Win2Linux",
|
||||||
"ProductVersion": "0.0.1.0"
|
"ProductVersion": "0.0.3.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user