Skip to content

Commit

Permalink
add Nahum's UEFI variable code
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorVin committed Dec 22, 2023
1 parent 2cc267a commit 777f72d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
2 changes: 1 addition & 1 deletion actions/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type FirmwareChecksumCollector interface {
// UEFIVarsCollector defines an interface to collect EFI variables
type UEFIVarsCollector interface {
UtilAttributeGetter
UEFIVariables(ctx context.Context) (keyValues map[string]string, err error)
GetUEFIVars(ctx context.Context) (utils.UEFIVars, error)
}

// Updaters
Expand Down
21 changes: 14 additions & 7 deletions actions/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package actions

import (
"context"
"encoding/json"
"runtime/debug"
"strings"

Expand Down Expand Up @@ -150,8 +151,7 @@ func NewInventoryCollectorAction(options ...Option) *InventoryCollectorAction {
firmware.MakeOutputPath(),
firmware.TraceExecution(a.trace),
),
// implement uefi vars collector and plug in here
// UEFIVarsCollector: ,
UEFIVarsCollector: &utils.UEFIVariableCollector{},
}
}

Expand Down Expand Up @@ -738,20 +738,27 @@ func (a *InventoryCollectorAction) CollectUEFIVariables(ctx context.Context) err
return nil
}

keyValues, err := a.collectors.UEFIVariables(ctx)
keyValues, err := a.collectors.UEFIVarsCollector.GetUEFIVars(ctx)
if err != nil {
return err
}

if len(keyValues) == 0 || a.device.BIOS == nil {
if len(keyValues) == 0 {
// seems unlikely
return nil
}

for k, v := range keyValues {
// do we want a prefix?
a.device.Metadata["EFI_VAR-"+k] = v
if a.device.BIOS == nil {
a.device.BIOS.Metadata = map[string]string{}
}

jsonBytes, err := json.Marshal(keyValues)
if err != nil {
return errors.Wrap(err, "marshaling uefi variables")
}

a.device.BIOS.Metadata["uefi-variables"] = string(jsonBytes)

return nil
}

Expand Down
66 changes: 65 additions & 1 deletion utils/uefi_vars.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
//nolint:wsl // god it's useless
package utils

// UEFIVarsCollector implementation goes here
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
}

0 comments on commit 777f72d

Please sign in to comment.