-
Notifications
You must be signed in to change notification settings - Fork 458
/
efivars.go
83 lines (62 loc) · 2.33 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package sdboot
import (
"errors"
"github.com/ecks/uefi/efi/efiguid"
"github.com/ecks/uefi/efi/efivario"
"golang.org/x/sys/unix"
"golang.org/x/text/encoding/unicode"
"github.com/siderolabs/talos/pkg/machinery/constants"
)
const (
// SystemdBootGUIDString is the GUID of the SystemdBoot EFI variables.
SystemdBootGUIDString = "4a67b082-0a4c-41cf-b6c7-440b29bb8c4f"
// SystemdBootStubInfoPath is the path to the SystemdBoot StubInfo EFI variable.
SystemdBootStubInfoPath = constants.EFIVarsMountPoint + "/" + "StubInfo-" + SystemdBootGUIDString
)
// SystemdBootGUID is the GUID of the SystemdBoot EFI variables.
var SystemdBootGUID = efiguid.MustFromString(SystemdBootGUIDString)
// Variable names.
const (
LoaderEntryDefaultName = "LoaderEntryDefault"
LoaderEntrySelectedName = "LoaderEntrySelected"
LoaderConfigTimeoutName = "LoaderConfigTimeout"
)
// ReadVariable reads a SystemdBoot EFI variable.
func ReadVariable(c efivario.Context, name string) (string, error) {
_, data, err := efivario.ReadAll(c, name, SystemdBootGUID)
if err != nil {
if errors.Is(err, efivario.ErrNotFound) {
return "", nil
}
return "", err
}
out := make([]byte, len(data))
decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()
n, _, err := decoder.Transform(out, data, true)
if err != nil {
return "", err
}
if n > 0 && out[n-1] == 0 {
n--
}
return string(out[:n]), nil
}
// WriteVariable reads a SystemdBoot EFI variable.
func WriteVariable(c efivario.Context, name, value string) error {
// mount EFI vars as rw
if err := unix.Mount("efivarfs", constants.EFIVarsMountPoint, "efivarfs", unix.MS_REMOUNT, ""); err != nil {
return err
}
defer unix.Mount("efivarfs", constants.EFIVarsMountPoint, "efivarfs", unix.MS_REMOUNT|unix.MS_RDONLY, "") //nolint:errcheck
out := make([]byte, (len(value)+1)*2)
encoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
n, _, err := encoder.Transform(out, []byte(value), true)
if err != nil {
return err
}
out = append(out[:n], 0, 0)
return c.Set(name, SystemdBootGUID, efivario.BootServiceAccess|efivario.RuntimeAccess|efivario.NonVolatile, out)
}