/
uefi_vars.go
67 lines (58 loc) · 1.49 KB
/
uefi_vars.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
//nolint:wsl // god it's useless
package utils
import (
"context"
"crypto/sha256"
"fmt"
"io/fs"
//nolint:staticcheck // this is deprecated but I can't rewrite now
"io/ioutil"
"path/filepath"
"github.com/metal-toolbox/ironlib/model"
)
type UEFIVariableCollector struct{}
func (UEFIVariableCollector) Attributes() (model.CollectorUtility, string, error) {
return "uefi-variable-collector", "", nil
}
type UEFIVarEntry struct {
Path string `json:"path"`
Size int64 `json:"size"`
Sha256sum string `json:"sha256sum"`
Error bool `json:"error"`
}
type UEFIVars map[string]UEFIVarEntry
func (UEFIVariableCollector) GetUEFIVars(ctx context.Context) (UEFIVars, error) {
uefivars := make(map[string]UEFIVarEntry)
walkme := "/sys/firmware/efi/efivars"
err := filepath.Walk(walkme, func(path string, info fs.FileInfo, err error) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
entry := UEFIVarEntry{Path: path}
if err != nil {
// Capture all errors, even directories
entry.Error = true
uefivars[info.Name()] = entry
return nil // Keep walking
}
// No need to capture anything for directory entries without errors
if info.IsDir() {
return nil
}
entry.Size = info.Size()
b, err := ioutil.ReadFile(path)
if err != nil {
entry.Error = true
} else {
entry.Sha256sum = fmt.Sprintf("%x", sha256.Sum256(b))
}
uefivars[info.Name()] = entry
return nil // Keep walking
})
if err != nil {
return nil, err
}
return uefivars, nil
}