File: efifs.go

package info (click to toggle)
golang-github-foxboron-go-uefi 0.0~git20250207.69fb7db-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 9,140 kB
  • sloc: makefile: 29; sh: 14
file content (92 lines) | stat: -rw-r--r-- 2,441 bytes parent folder | download
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
86
87
88
89
90
91
92
package efivarfs

import (
	"bytes"
	"errors"

	"github.com/foxboron/go-uefi/efi/attr"
	"github.com/foxboron/go-uefi/efi/attributes"
	"github.com/foxboron/go-uefi/efivar"
	"github.com/foxboron/go-uefi/efivarfs/fswrapper"
)

// This package deals with the interface actually writing the variables properly
// to the efivarfs backend.

var (
	ErrImmutable           = attr.ErrIsImmutable
	ErrIncorrectAttributes = errors.New("efivar has the wrong attributes")
)

// EFIVars is the interface for interacting with writing and getting EFI variables.
type EFIVars interface {
	GetVar(efivar.Efivar, efivar.Unmarshallable) error
	GetVarWithAttributes(efivar.Efivar, efivar.Unmarshallable) (attributes.Attributes, error)
	WriteVar(efivar.Efivar, efivar.Marshallable) error
}

// EFIFS is a struct that combines reading variables from the file system while also ensuring we are
type EFIFS struct {
	*fswrapper.FSWrapper
}

var _ EFIVars = &EFIFS{}

// NewFS creates a new instance of *EFIFS
func NewFS() *EFIFS {
	return &EFIFS{
		fswrapper.NewFSWrapper(),
	}
}

// Open returns a initialization Efivarfs for high-level abstractions.
func (f *EFIFS) Open() *Efivarfs {
	return &Efivarfs{f}
}

// Check if file is immutable before writing to the file.
// Returns ErrImmutable if the file is immutable.
func (f *EFIFS) CheckImmutable() *EFIFS {
	f.FSWrapper.CheckImmutable()
	return f
}

// UnsetImmutable implicitly when writing towards a file.
func (f *EFIFS) UnsetImmutable() *EFIFS {
	f.FSWrapper.UnsetImmutable()
	return f
}

// GetVar parses and unmarshalls a EFI variable.
func (t *EFIFS) GetVar(v efivar.Efivar, e efivar.Unmarshallable) error {
	if _, err := t.GetVarWithAttributes(v, e); err != nil {
		return err
	}
	return nil
}

// GetVarWithAttributes parses and unmarshalls a EFI variable, while also
// returning the parsed attributes.
func (t *EFIFS) GetVarWithAttributes(v efivar.Efivar, e efivar.Unmarshallable) (attributes.Attributes, error) {
	attrs, buf, err := t.ReadEfivarsWithGuid(v.Name, *v.GUID)
	if err != nil {
		return 0, err
	}

	if !v.Attributes.Equal(attrs) {
		return attrs, ErrIncorrectAttributes
	}

	if err := e.Unmarshal(buf); err != nil {
		return 0, err
	}

	return attrs, nil
}

// WriteVar writes an EFI variables to the EFIFS.
func (t *EFIFS) WriteVar(v efivar.Efivar, e efivar.Marshallable) error {
	var b bytes.Buffer
	e.Marshal(&b)
	return t.WriteEfivarsWithGuid(v.Name, v.Attributes, b.Bytes(), *v.GUID)
}