~whynothugo/sb-backup

73595ff46b32b49f0cc30381d805219c590f6df8 — Hugo Osvaldo Barrera 6 months ago eeb8304
Decode LoaderEntrySelected as a UTF-16 string
3 files changed, 37 insertions(+), 11 deletions(-)

M go.mod
M go.sum
M main.go
M go.mod => go.mod +1 -0
@@ 5,4 5,5 @@ go 1.16
require (
	github.com/foxboron/go-uefi v0.0.0-20210516000019-39e60f7f752f // indirect
	github.com/foxboron/sbctl v0.0.0-20210522162249-d7e4f3b603ec // indirect
	golang.org/x/text v0.3.6 // indirect
)

M go.sum => go.sum +3 -0
@@ 124,8 124,11 @@ golang.org/x/sys v0.0.0-20201109165425-215b40eba54c h1:+B+zPA6081G5cEb2triOIJpcv
golang.org/x/sys v0.0.0-20201109165425-215b40eba54c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=

M main.go => main.go +33 -11
@@ 2,13 2,16 @@ package main

import (
	"fmt"
	"path"
	"io"
	"bytes"
	"os"
	"path"
	"strings"

	"github.com/foxboron/go-uefi/efi/attributes"
	"github.com/foxboron/sbctl"
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
)

func CopyFile(sourcePath string, destPath string) error {


@@ 44,22 47,41 @@ func GenerateBackupFilename(original string) string {
	return fmt.Sprintf("%s_backup%s", name, extension)
}

func main() {
	sm, err := attributes.ReadEfivarsFile("/sys/firmware/efi/efivars/LoaderEntrySelected-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f")
// Parse an efivar as a UTF-16 string.
func ParseUtf16Var(data []byte) (string, error) {
	utf16 := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
	name, _, err := transform.Bytes(utf16.NewDecoder(), data)
	if err != nil {
		return "", err
	}

	return string(bytes.Trim(name, "\x00")), nil
}

// Return the boot entry which is currently booted.
func GetCurrentlyBootedEntry() (string, error) {
	entry, err := attributes.ReadEfivarsFile("/sys/firmware/efi/efivars/LoaderEntrySelected-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f")
	if err != nil {
		fmt.Printf("Error reading current entry: %v.\n", err)
		os.Exit(1)
		return "", err
	}

	var name []byte
	for _, b := range sm.Data {
		if b != 0 {
			name = append(name, b)
		}
	name, err := ParseUtf16Var(entry.Data)
	if err != nil {
		return "", err
	}

	return name, nil
}

func main() {
	name, err := GetCurrentlyBootedEntry()

	if err != nil {
		fmt.Printf("Error reading currently booted entry: %v.\n", err)
		os.Exit(1)
	}

	current := fmt.Sprintf("%s/EFI/Linux/%s", sbctl.GetESP(), string(name))
	current := fmt.Sprintf("%s/EFI/Linux/%s", sbctl.GetESP(), name)
	backup := GenerateBackupFilename(current)

	fmt.Printf("Backing up: %s -> %s.\n", current, backup)