@@ -28,6 +28,30 @@ impl OvmfFileType {
28
28
Self :: Vars => "vars" ,
29
29
}
30
30
}
31
+
32
+ /// Get a user-provided path for the given OVMF file type.
33
+ ///
34
+ /// This uses the command-line arg if present, otherwise it falls back to an
35
+ /// environment variable. If neither is present, returns `None`.
36
+ fn get_user_provided_path ( self , opt : & QemuOpt ) -> Option < PathBuf > {
37
+ let opt_path;
38
+ let var_name;
39
+ match self {
40
+ Self :: Code => {
41
+ opt_path = & opt. ovmf_code ;
42
+ var_name = "OVMF_CODE" ;
43
+ }
44
+ Self :: Vars => {
45
+ opt_path = & opt. ovmf_vars ;
46
+ var_name = "OVMF_VARS" ;
47
+ }
48
+ }
49
+ if let Some ( path) = opt_path {
50
+ Some ( path. clone ( ) )
51
+ } else {
52
+ env:: var_os ( var_name) . map ( PathBuf :: from)
53
+ }
54
+ }
31
55
}
32
56
33
57
struct OvmfPaths {
@@ -173,14 +197,14 @@ impl OvmfPaths {
173
197
/// that exists. If none of them exist, an error is returned.
174
198
fn find_ovmf_file (
175
199
file_type : OvmfFileType ,
176
- user_provided_path : & Option < PathBuf > ,
200
+ opt : & QemuOpt ,
177
201
candidates : & [ Self ] ,
178
202
) -> Result < PathBuf > {
179
- if let Some ( path) = user_provided_path {
203
+ if let Some ( path) = file_type . get_user_provided_path ( opt ) {
180
204
// The user provided an exact path to use; verify that it
181
205
// exists.
182
206
if path. exists ( ) {
183
- Ok ( path. to_owned ( ) )
207
+ Ok ( path)
184
208
} else {
185
209
bail ! (
186
210
"ovmf {} file does not exist: {}" ,
@@ -211,8 +235,8 @@ impl OvmfPaths {
211
235
fn find ( opt : & QemuOpt , arch : UefiArch ) -> Result < Self > {
212
236
let candidates = Self :: get_candidate_paths ( arch) ;
213
237
214
- let code = Self :: find_ovmf_file ( OvmfFileType :: Code , & opt. ovmf_code , & candidates) ?;
215
- let vars = Self :: find_ovmf_file ( OvmfFileType :: Vars , & opt. ovmf_vars , & candidates) ?;
238
+ let code = Self :: find_ovmf_file ( OvmfFileType :: Code , opt, & candidates) ?;
239
+ let vars = Self :: find_ovmf_file ( OvmfFileType :: Vars , opt, & candidates) ?;
216
240
217
241
Ok ( Self { code, vars } )
218
242
}
0 commit comments