-
Notifications
You must be signed in to change notification settings - Fork 0
/
efivars.go
85 lines (66 loc) · 1.36 KB
/
efivars.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"os"
"strings"
)
const efivar_path string = "/sys/firmware/efi/efivars/"
type EfiVar struct {
Name string
FullName string
Data []byte
}
type EfiVars []EfiVar
func (e EfiVars) Search(name string) (EfiVar, bool) {
for _, efivar := range e {
if efivar.Name == name {
return efivar, true
}
}
return EfiVar{}, false
}
func main() {
if len(os.Args) != 2 {
usage(os.Args[0])
}
efivars := readEfiVars()
efivar, ok := efivars.Search(os.Args[1])
if !ok {
err("'%s' cannot be found\n", os.Args[1])
}
fmt.Println(efivar.Name)
fmt.Println(efivar.Data)
}
func readEfiVars() EfiVars {
dirents, err := os.ReadDir(efivar_path)
check(err)
var efivars []EfiVar
for _, dirent := range dirents {
var efivar EfiVar
efivar.FullName = dirent.Name()
efivar.Name = strings.Split(efivar.FullName, "-")[0]
efivar.Data = readEfiVar(efivar.FullName)
efivars = append(efivars, efivar)
}
return efivars
}
func readEfiVar(v string) []byte {
fullpath := efivar_path + v
data, err := os.ReadFile(fullpath)
check(err)
return data
}
func check(e error) {
if e != nil {
panic(e)
}
}
func err(format string, a ...any) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(1)
}
func usage(prog string) {
fmt.Fprintf(os.Stderr, "usage: %s efivar\n", prog)
fmt.Fprintf(os.Stderr, "example: %s VendorKeys\n", prog)
os.Exit(1)
}