-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuefi.go
197 lines (175 loc) · 5.24 KB
/
uefi.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package qcli
import (
"fmt"
"path/filepath"
"runtime"
"strings"
)
type UEFIFirmwareDevice struct {
Code string `yaml:"uefi-code"`
Vars string `yaml:"uefi-vars"`
}
var VMFHostPrefix = "/usr/share"
const (
UEFIVarsFileName = "uefi-nvram.fd"
VMFCode = "VMF_CODE" // OVMF_CODE , AAVMF_CODE
VMFVars = "VMF_VARS"
VMFMs = ".ms"
VMFSecboot = ".secboot"
VMF4MB = "_4M"
VMFSuffix = ".fd"
VMF32Bit = "32"
)
func VMFPrefix() string {
switch runtime.GOARCH {
case "aarch64", "arm64":
return "AA"
case "amd64", "x86_64":
return "O"
}
return ""
}
func VMFPathBase() string {
return filepath.Join(VMFHostPrefix, VMFPrefix()+"VMF")
}
func (u UEFIFirmwareDevice) Valid() error {
if u.Code == "" {
return fmt.Errorf("UEFIFirmwareDevice has empty Code field")
}
if u.Vars == "" {
return fmt.Errorf("UEFIFirmwareDevice has empty Vars field")
}
return nil
}
func (u UEFIFirmwareDevice) QemuParams(config *Config) []string {
var qemuParams []string
if u.Code != "" {
qemuParams = append(qemuParams, "-drive", "if=pflash,format=raw,readonly=on,file="+u.Code)
}
if u.Vars != "" {
qemuParams = append(qemuParams, "-drive", "if=pflash,format=raw,file="+u.Vars)
}
return qemuParams
}
func (u UEFIFirmwareDevice) IsSecureBoot() bool {
if strings.HasSuffix(u.Code, VMFSecboot) {
return true
}
return false
}
func (u UEFIFirmwareDevice) Is4MB() bool {
if strings.HasSuffix(u.Code, VMF4MB) {
return true
}
return false
}
func (u UEFIFirmwareDevice) Exists() (bool, error) {
if u.Code == "" {
return false, fmt.Errorf("UEFIFirmwareDevice.Code is empty: %+v", u)
}
if u.Vars == "" {
return false, fmt.Errorf("UEFIFirmwareDevice.Vars is empty: %+v", u)
}
codeFound := PathExists(u.Code)
varsFound := PathExists(u.Vars)
if codeFound && varsFound {
return true, nil
}
missing := []string{}
if !codeFound {
missing = append(missing, fmt.Sprintf("Code not found at %q", u.Code))
}
if !varsFound {
missing = append(missing, fmt.Sprintf("Vars not found at %q", u.Vars))
}
return false, fmt.Errorf("Failed to find UEFIFirmwareDevice paths: %s", strings.Join(missing, ", "))
}
// NewSystemUEFIFirmwareDevice looks at the local system to collect expected
// OVMF firmware files, callers will need to make a copy of the of the Vars
// template file before using it in a running VM.
func NewSystemUEFIFirmwareDevice(useSecureBoot bool) (*UEFIFirmwareDevice, error) {
pfx := VMFPrefix()
pathBase := VMFPathBase()
// SecureBoot+4M
secBoot4M := UEFIFirmwareDevice{
Code: filepath.Join(pathBase, pfx+VMFCode+VMF4MB+VMFSecboot+VMFSuffix), // /usr/share/*VMF/*VMF_CODE_4M.secboot.fd
Vars: filepath.Join(pathBase, pfx+VMFVars+VMF4MB+VMFSecboot+VMFSuffix), // OVMF_VARS_4M.secboot.fd
}
// SecureBoot+4M+MSVars
secBoot4MVarsMs := UEFIFirmwareDevice{
Code: filepath.Join(pathBase, pfx+VMFCode+VMF4MB+VMFSecboot+VMFSuffix), // OVMF_CODE_4M.secboot.fd
Vars: filepath.Join(pathBase, pfx+VMFVars+VMF4MB+VMFMs+VMFSuffix), // OVMF_VARS_4M.ms.fd
}
// SecureBoot
secBoot := UEFIFirmwareDevice{
Code: filepath.Join(pathBase, pfx+VMFCode+VMFSecboot+VMFSuffix), // OVMF_CODE.secboot.fd
Vars: filepath.Join(pathBase, pfx+VMFVars+VMFSecboot+VMFSuffix), // OVMF_VARS.secboot.fd
}
// SecureBoot+MSVars (amd64 or arm64)
secBootVarsMs := UEFIFirmwareDevice{
Code: filepath.Join(pathBase, pfx+VMFCode+VMFMs+VMFSuffix), // {O,AA}VMF_CODE.ms.fd
Vars: filepath.Join(pathBase, pfx+VMFVars+VMFMs+VMFSuffix), // {O,AA}VMF_VARS.ms.fd
}
// Insecure+4M
insecureBoot4M := UEFIFirmwareDevice{
Code: filepath.Join(pathBase, pfx+VMFCode+VMF4MB+VMFSuffix), // OVMF_CODE_4M.fd
Vars: filepath.Join(pathBase, pfx+VMFVars+VMF4MB+VMFSuffix), // OVMF_Vars_4M.fd
}
// Insecure
insecure := UEFIFirmwareDevice{
Code: filepath.Join(pathBase, pfx+VMFCode+VMFSuffix), // {O,AA}VMF_CODE.fd
Vars: filepath.Join(pathBase, pfx+VMFVars+VMFSuffix), // {O,AA}VMF_Vars.fd
}
var found bool
var err error
if useSecureBoot {
// 4M and .secboot variants are only on x86
switch runtime.GOARCH {
case "amd64", "x86_64":
found, err = secBoot4MVarsMs.Exists()
if err != nil {
return &UEFIFirmwareDevice{}, fmt.Errorf("SecureBoot 4M MS Vars erorr: %s", err)
}
if found {
return &secBoot4MVarsMs, nil
}
found, err = secBoot4M.Exists()
if err != nil {
return &UEFIFirmwareDevice{}, fmt.Errorf("SecureBoot 4M Vars error: %s", err)
}
if found {
return &secBoot4M, nil
}
found, err = secBoot.Exists()
if found {
return &secBoot, nil
}
if err != nil {
return &UEFIFirmwareDevice{}, fmt.Errorf("SecureBoot Vars error: %s", err)
}
}
found, err = secBootVarsMs.Exists()
if err != nil {
return &UEFIFirmwareDevice{}, fmt.Errorf("SecureBoot MS Vars error: %s", err)
}
if found {
return &secBootVarsMs, nil
}
return &UEFIFirmwareDevice{}, fmt.Errorf("%sVMF secureboot code,vars missing, check: %s", pfx, pathBase)
}
switch runtime.GOARCH {
case "amd64", "x86_64":
found, err = insecureBoot4M.Exists()
if err != nil {
return &UEFIFirmwareDevice{}, err
}
if found {
return &insecureBoot4M, nil
}
}
found, err = insecure.Exists()
if found {
return &insecure, nil
}
return &UEFIFirmwareDevice{}, fmt.Errorf("%sVMF code,vars missing, check: %s", pfx, pathBase)
}