Skip to content

Commit d560001

Browse files
committed
system: support EFI variables
1 parent 31611f0 commit d560001

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

Managers/UTMQemuSystem.m

+40-6
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@
3535
NSInteger threads;
3636
} CPUCount;
3737

38+
extern NSString *const kUTMErrorDomain;
39+
3840
@interface UTMQemuSystem ()
3941

4042
@property (nonatomic, readonly) NSURL *resourceURL;
4143
@property (nonatomic, readonly) CPUCount emulatedCpuCount;
4244
@property (nonatomic, readonly) BOOL useHypervisor;
4345
@property (nonatomic, readonly) BOOL hasCustomBios;
4446
@property (nonatomic, readonly) BOOL usbSupported;
45-
@property (nonatomic, readonly) BOOL hasVirtio;
47+
@property (nonatomic, readonly) NSURL *efiVariablesURL;
4648

4749
@end
4850

@@ -169,12 +171,18 @@ - (void)architectureSpecificConfiguration {
169171
[self pushArgv:@"-global"];
170172
[self pushArgv:@"ICH9-LPC.disable_s3=1"]; // applies for pc-q35-* types
171173
}
172-
if (self.hasVirtio) {
174+
if (self.configuration.systemBootUefi) {
173175
NSString *name = [NSString stringWithFormat:@"edk2-%@-code.fd", arch];
174176
NSURL *path = [self.resourceURL URLByAppendingPathComponent:name];
175177
if (!self.hasCustomBios && [[NSFileManager defaultManager] fileExistsAtPath:path.path]) {
176178
[self pushArgv:@"-drive"];
177-
[self pushArgv:[NSString stringWithFormat:@"if=pflash,format=raw,file=%@,readonly=on", path.path]]; // accessDataWithBookmark called already
179+
[self pushArgv:[NSString stringWithFormat:@"if=pflash,format=raw,unit=0,file=%@,readonly=on", path.path]]; // accessDataWithBookmark called already
180+
[self pushArgv:@"-drive"];
181+
[self pushArgv:[NSString stringWithFormat:@"if=pflash,format=raw,unit=1,file=%@", self.efiVariablesURL.path]];
182+
[self accessDataWithBookmark:[self.efiVariablesURL bookmarkDataWithOptions:0
183+
includingResourceValuesForKeys:nil
184+
relativeToURL:nil
185+
error:nil]];
178186
}
179187
}
180188
}
@@ -549,9 +557,8 @@ - (BOOL)usbSupported {
549557
return ![self.configuration.systemTarget isEqualToString:@"isapc"];
550558
}
551559

552-
- (BOOL)hasVirtio {
553-
return [self.configuration.systemTarget hasPrefix:@"virt"] ||
554-
(!self.configuration.displayConsoleOnly && [self.configuration.displayCard hasPrefix:@"virtio"]);
560+
- (NSURL *)efiVariablesURL {
561+
return [[self.imgPath URLByAppendingPathComponent:[UTMConfiguration diskImagesDirectory]] URLByAppendingPathComponent:@"efi_vars.fd"];
555562
}
556563

557564
- (NSString *)machineProperties {
@@ -692,7 +699,34 @@ - (void)updateArgvWithUserOptions:(BOOL)userOptions {
692699
}
693700
}
694701

702+
- (BOOL)createEfiVariablesIfNeededWithError:(NSError **)error {
703+
NSString *arch = self.configuration.systemArchitecture;
704+
NSFileManager *fileManager = NSFileManager.defaultManager;
705+
NSURL *srcUrl = nil;
706+
if (![fileManager fileExistsAtPath:self.efiVariablesURL.path]) {
707+
if ([arch isEqualToString:@"arm"] || [arch isEqualToString:@"aarch64"]) {
708+
srcUrl = [self.resourceURL URLByAppendingPathComponent:@"edk2-arm-vars.fd"];
709+
} else if ([arch isEqualToString:@"i386"] || [arch isEqualToString:@"x86_64"]) {
710+
srcUrl = [self.resourceURL URLByAppendingPathComponent:@"edk2-i386-vars.fd"];
711+
} else {
712+
if (error) {
713+
*error = [NSError errorWithDomain:kUTMErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"UEFI is not supported with this architecture.", "UTMQemuSystem")}];
714+
}
715+
return NO;
716+
}
717+
return [fileManager copyItemAtURL:srcUrl toURL:self.efiVariablesURL error:error];
718+
}
719+
return YES;
720+
}
721+
695722
- (void)startWithCompletion:(void (^)(BOOL, NSString * _Nonnull))completion {
723+
if (self.configuration.systemBootUefi) {
724+
NSError *err;
725+
if (![self createEfiVariablesIfNeededWithError:&err]) {
726+
completion(NO, err.localizedDescription);
727+
return;
728+
}
729+
}
696730
[self updateArgvWithUserOptions:YES];
697731
[self startQemu:self.configuration.systemArchitecture completion:completion];
698732
}

0 commit comments

Comments
 (0)