Repository navigation
Expand file tree
/
Copy pathbootloader.go
More file actions
234 lines (203 loc) · 7.14 KB
/
Copy pathbootloader.go
File metadata and controls
234 lines (203 loc) · 7.14 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package vz
import (
"fmt"
"os"
"unsafe"
"github.com/Code-Hex/vz/v4/internal/objc"
"github.com/Code-Hex/vz/v4/internal/objcutil"
"github.com/Code-Hex/vz/v4/internal/vzbridge"
)
// BootLoader is the interface of boot loader definitions.
type BootLoader interface {
objc.NSObject
bootLoader()
}
type baseBootLoader struct{}
func (*baseBootLoader) bootLoader() {}
var _ BootLoader = (*LinuxBootLoader)(nil)
// LinuxBootLoader Boot loader configuration for a Linux kernel.
// see: https://developer.apple.com/documentation/virtualization/vzlinuxbootloader?language=objc
type LinuxBootLoader struct {
vmlinuzPath string
initrdPath string
cmdLine string
*pointer
*baseBootLoader
}
func (b *LinuxBootLoader) String() string {
return fmt.Sprintf(
"vmlinuz: %q, initrd: %q, command-line: %q",
b.vmlinuzPath,
b.initrdPath,
b.cmdLine,
)
}
// LinuxBootLoaderOption is an option for LinuxBootLoader.
type LinuxBootLoaderOption func(b *LinuxBootLoader) error
// WithCommandLine sets the command-line parameters.
// see: https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
func WithCommandLine(cmdLine string) LinuxBootLoaderOption {
return func(b *LinuxBootLoader) error {
b.cmdLine = cmdLine
vzbridge.VZLinuxBootLoader_SetCommandLine(b, vzbridge.NSString_StringWithUTF8String(cmdLine))
return nil
}
}
// WithInitrd sets the optional initial RAM disk.
func WithInitrd(initrdPath string) LinuxBootLoaderOption {
return func(b *LinuxBootLoader) error {
if _, err := os.Stat(initrdPath); err != nil {
return fmt.Errorf("invalid initial RAM disk path: %w", err)
}
b.initrdPath = initrdPath
vzbridge.VZLinuxBootLoader_SetInitialRamdiskURL(b, vzbridge.NSURL_FileURLWithPath(vzbridge.NSString_StringWithUTF8String(initrdPath)))
return nil
}
}
// NewLinuxBootLoader creates a LinuxBootLoader with the Linux kernel passed as Path.
//
// This is only supported on macOS 11 and newer, error will
// be returned on older versions.
func NewLinuxBootLoader(vmlinuz string, opts ...LinuxBootLoaderOption) (*LinuxBootLoader, error) {
if err := macOSAvailable(11); err != nil {
return nil, err
}
if _, err := os.Stat(vmlinuz); err != nil {
return nil, fmt.Errorf("invalid linux kernel path: %w", err)
}
bootLoader := &LinuxBootLoader{
vmlinuzPath: vmlinuz,
pointer: vzbridge.VZLinuxBootLoader_InitWithKernelURL(vzbridge.NSURL_FileURLWithPath(vzbridge.NSString_StringWithUTF8String(vmlinuz))),
}
for _, opt := range opts {
if err := opt(bootLoader); err != nil {
return nil, err
}
}
return bootLoader, nil
}
var _ BootLoader = (*LinuxBootLoader)(nil)
// EFIBootLoader Boot loader configuration for booting guest operating systems expecting an EFI ROM.
// see: https://developer.apple.com/documentation/virtualization/vzefibootloader?language=objc
type EFIBootLoader struct {
*pointer
*baseBootLoader
variableStore *EFIVariableStore
}
// NewEFIBootLoaderOption is an option type to initialize a new EFIBootLoader.
type NewEFIBootLoaderOption func(b *EFIBootLoader)
// WithEFIVariableStore sets the optional EFI variable store.
func WithEFIVariableStore(variableStore *EFIVariableStore) NewEFIBootLoaderOption {
return func(e *EFIBootLoader) {
vzbridge.VZEFIBootLoader_SetVariableStore(e, variableStore)
e.variableStore = variableStore
}
}
// NewEFIBootLoader creates a new EFI boot loader.
//
// This is only supported on macOS 13 and newer, error will
// be returned on older versions.
func NewEFIBootLoader(opts ...NewEFIBootLoaderOption) (*EFIBootLoader, error) {
if err := macOSAvailable(13); err != nil {
return nil, err
}
bootLoader := &EFIBootLoader{
pointer: vzbridge.VZEFIBootLoader_Init(),
}
for _, optFunc := range opts {
optFunc(bootLoader)
}
return bootLoader, nil
}
// VariableStore returns EFI variable store.
func (e *EFIBootLoader) VariableStore() *EFIVariableStore {
return e.variableStore
}
// EFIVariableStore is EFI variable store.
// The EFI variable store contains NVRAM variables exposed by the EFI ROM.
//
// see: https://developer.apple.com/documentation/virtualization/vzefivariablestore?language=objc
type EFIVariableStore struct {
*pointer
path string
}
// NewEFIVariableStoreOption is an option type to initialize a new EFIVariableStore.
type NewEFIVariableStoreOption func(*EFIVariableStore) error
// EFIVariableStoreCreationOption configures creation of an EFI variable store.
//
// See: https://developer.apple.com/documentation/virtualization/vzefivariablestore/initializationoptions?language=objc
type EFIVariableStoreCreationOption func(*efiVariableStoreCreationOptions)
type efiVariableStoreCreationOptions struct {
allowOverwrite bool
}
// WithEFIVariableStoreAllowOverwrite controls whether creation may replace an
// existing file. Overwriting is allowed by default.
//
// See: https://developer.apple.com/documentation/virtualization/vzefivariablestore/initializationoptions/allowoverwrite?language=objc
func WithEFIVariableStoreAllowOverwrite(allow bool) EFIVariableStoreCreationOption {
return func(options *efiVariableStoreCreationOptions) {
options.allowOverwrite = allow
}
}
// WithCreatingEFIVariableStore creates an initialized EFI variable store at the
// specified path. By default, it overwrites an existing file.
//
// See: https://developer.apple.com/documentation/virtualization/vzefivariablestore/initcreatingvariablestoreaturl:options:error:?language=objc
func WithCreatingEFIVariableStore(opts ...EFIVariableStoreCreationOption) NewEFIVariableStoreOption {
return func(es *EFIVariableStore) error {
options := efiVariableStoreCreationOptions{
allowOverwrite: true,
}
for _, opt := range opts {
if opt == nil {
return fmt.Errorf("nil EFI variable store creation option")
}
opt(&options)
}
var nativeOptions uint64
if options.allowOverwrite {
nativeOptions = vzbridge.VZEFIVariableStoreInitializationOptionAllowOverwrite
}
path := vzbridge.NSString_StringWithUTF8String(es.path)
defer objc.Release(path)
url := vzbridge.NSURL_FileURLWithPath(path)
defer objc.Release(url)
var nserrPtr unsafe.Pointer
es.pointer = vzbridge.VZEFIVariableStore_InitCreatingVariableStoreAtURL_Options_Error(
url,
nativeOptions,
&nserrPtr,
)
if err := objcutil.NewNSError(nserrPtr); err != nil {
return err
}
return nil
}
}
// NewEFIVariableStore Initialize the variable store. If no options are specified,
// it initializes from the paths that exist.
//
// This is only supported on macOS 13 and newer, error will
// be returned on older versions.
func NewEFIVariableStore(path string, opts ...NewEFIVariableStoreOption) (*EFIVariableStore, error) {
if err := macOSAvailable(13); err != nil {
return nil, err
}
variableStore := &EFIVariableStore{
path: path,
}
for _, optFunc := range opts {
if err := optFunc(variableStore); err != nil {
return nil, err
}
}
if objc.Ptr(variableStore) == nil {
if _, err := os.Stat(path); err != nil {
return nil, err
}
variableStore.pointer = vzbridge.VZEFIVariableStore_InitWithURL(vzbridge.NSURL_FileURLWithPath(vzbridge.NSString_StringWithUTF8String(path)))
}
return variableStore, nil
}
// Path returns the path of the variable store on the local file system.
func (e *EFIVariableStore) Path() string { return e.path }