-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |