~whynothugo/sb-backup

5d7d0543007d3f0cbe70e47eff297ca829f63046 — Hugo Osvaldo Barrera 2 years ago 77b0cf0
Use a couple more upstreamed functions
3 files changed, 7 insertions(+), 38 deletions(-)

M go.mod
M go.sum
M main.go
M go.mod => go.mod +1 -1
@@ 4,7 4,7 @@ go 1.16

require (
	github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
	github.com/foxboron/go-uefi v0.0.0-20210611230104-7a6a29e36155
	github.com/foxboron/go-uefi v0.0.0-20210627112259-5c9af3e9edec
	github.com/foxboron/pkcs7 v0.0.0-20200515184129-2907ba0539a4 // indirect
	github.com/foxboron/sbctl v0.0.0-20210613174236-faf366f5e7d6
	github.com/ugorji/go v1.1.4 // indirect

M go.sum => go.sum +4 -0
@@ 49,6 49,10 @@ github.com/foxboron/go-uefi v0.0.0-20210602193603-8589bbab9380 h1:D8hRHRCC/jFjOg
github.com/foxboron/go-uefi v0.0.0-20210602193603-8589bbab9380/go.mod h1:bLcrn48nYQOkijhTK2iQw1MjXbBqJTG0k8RP6ww+CGQ=
github.com/foxboron/go-uefi v0.0.0-20210611230104-7a6a29e36155 h1:9RnTC3NVUwcFpHGGzDYd2LqED59D929P9rl+bq8JL2c=
github.com/foxboron/go-uefi v0.0.0-20210611230104-7a6a29e36155/go.mod h1:bLcrn48nYQOkijhTK2iQw1MjXbBqJTG0k8RP6ww+CGQ=
github.com/foxboron/go-uefi v0.0.0-20210613211149-a603e1f83052 h1:eIVNQsxaiD8PhaDm9pUWfj5LBKYAI+JzMb849oIx4R4=
github.com/foxboron/go-uefi v0.0.0-20210613211149-a603e1f83052/go.mod h1:bLcrn48nYQOkijhTK2iQw1MjXbBqJTG0k8RP6ww+CGQ=
github.com/foxboron/go-uefi v0.0.0-20210627112259-5c9af3e9edec h1:QGNtWgySrEuovzPfHW0Oai5bEX/NaHLaL8kF2HDEuPc=
github.com/foxboron/go-uefi v0.0.0-20210627112259-5c9af3e9edec/go.mod h1:bLcrn48nYQOkijhTK2iQw1MjXbBqJTG0k8RP6ww+CGQ=
github.com/foxboron/pkcs7 v0.0.0-20200515184129-2907ba0539a4/go.mod h1:px0/6X5Ap1wlLCWQ1DmiBULqyLBpoiXpUm0Vce+ufSk=
github.com/foxboron/sbctl v0.0.0-20210522214850-7eb70e2ee94b h1:rDEPC/j9EQn/N1EufpnxHjH7SxaLvcku/oXrMfdAYlE=
github.com/foxboron/sbctl v0.0.0-20210522214850-7eb70e2ee94b/go.mod h1:zZH5PxarMArcbjeDUhnpbQDRlgToOay+HBgyue7enk4=

M main.go => main.go +2 -37
@@ 1,18 1,14 @@
package main

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

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

func CopyFile(sourcePath string, destPath string) error {


@@ 48,39 44,8 @@ func GenerateBackupFilename(original string) string {
	return fmt.Sprintf("%s_backup%s", name, extension)
}

// Parse an efivar as a UTF-16 string.
func ParseUtf16Var(data *bytes.Buffer) (string, error) {
	utf16 := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
	utf16Reader := transform.NewReader(data, utf16.NewDecoder())
	b, err := io.ReadAll(utf16Reader)

	if err != nil {
		return "", err
	}

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

// Return the boot entry which is currently booted.
func GetCurrentlyBootedEntry() (string, error) {
	_, data, err := attributes.ReadEfivarsWithGuid(
		"LoaderEntrySelected",
		*util.StringToGUID("4a67b0820a4c41cfb6c7440b29bb8c4f"),
	)
	if err != nil {
		return "", err
	}

	name, err := ParseUtf16Var(data)
	if err != nil {
		return "", err
	}

	return name, nil
}

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

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