@@ -192,44 +192,58 @@ pub fn current_exe() -> io::Result<PathBuf> {
192
192
helpers:: device_path_to_text ( protocol) . map ( PathBuf :: from)
193
193
}
194
194
195
- pub struct Env ( !) ;
195
+ pub struct EnvStrDebug < ' a > {
196
+ iter : & ' a [ ( OsString , OsString ) ] ,
197
+ }
198
+
199
+ impl fmt:: Debug for EnvStrDebug < ' _ > {
200
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
201
+ let mut list = f. debug_list ( ) ;
202
+ for ( a, b) in self . iter {
203
+ list. entry ( & ( a. to_str ( ) . unwrap ( ) , b. to_str ( ) . unwrap ( ) ) ) ;
204
+ }
205
+ list. finish ( )
206
+ }
207
+ }
208
+
209
+ pub struct Env ( crate :: vec:: IntoIter < ( OsString , OsString ) > ) ;
196
210
197
211
impl Env {
198
212
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
199
213
pub fn str_debug ( & self ) -> impl fmt:: Debug + ' _ {
200
- let Self ( inner) = self ;
201
- match * inner { }
214
+ EnvStrDebug { iter : self . 0 . as_slice ( ) }
202
215
}
203
216
}
204
217
205
218
impl Iterator for Env {
206
219
type Item = ( OsString , OsString ) ;
220
+
207
221
fn next ( & mut self ) -> Option < ( OsString , OsString ) > {
208
- self . 0
222
+ self . 0 . next ( )
209
223
}
210
224
}
211
225
212
226
impl fmt:: Debug for Env {
213
- fn fmt ( & self , _: & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
214
- let Self ( inner) = self ;
215
- match * inner { }
227
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
228
+ self . 0 . fmt ( f)
216
229
}
217
230
}
218
231
219
232
pub fn env ( ) -> Env {
220
- panic ! ( "not supported on this platform" )
233
+ let env = uefi_env:: get_all ( ) . expect ( "not supported on this platform" ) ;
234
+ Env ( env. into_iter ( ) )
221
235
}
222
236
223
- pub fn getenv ( _ : & OsStr ) -> Option < OsString > {
224
- None
237
+ pub fn getenv ( key : & OsStr ) -> Option < OsString > {
238
+ uefi_env :: get ( key )
225
239
}
226
240
227
- pub unsafe fn setenv ( _ : & OsStr , _ : & OsStr ) -> io:: Result < ( ) > {
228
- Err ( io :: const_io_error! ( io :: ErrorKind :: Unsupported , "cannot set env vars on this platform" ) )
241
+ pub unsafe fn setenv ( key : & OsStr , val : & OsStr ) -> io:: Result < ( ) > {
242
+ uefi_env :: set ( key , val )
229
243
}
230
244
231
- pub unsafe fn unsetenv ( _ : & OsStr ) -> io:: Result < ( ) > {
232
- Err ( io :: const_io_error! ( io :: ErrorKind :: Unsupported , "cannot unset env vars on this platform ") )
245
+ pub unsafe fn unsetenv ( key : & OsStr ) -> io:: Result < ( ) > {
246
+ uefi_env :: set ( key , OsStr :: new ( " ") )
233
247
}
234
248
235
249
pub fn temp_dir ( ) -> PathBuf {
@@ -294,3 +308,81 @@ mod uefi_shell {
294
308
None
295
309
}
296
310
}
311
+
312
+ mod uefi_env {
313
+ use crate :: ffi:: { OsStr , OsString } ;
314
+ use crate :: io;
315
+ use crate :: os:: uefi:: ffi:: OsStringExt ;
316
+ use crate :: ptr:: NonNull ;
317
+ use crate :: sys:: { helpers, unsupported_err} ;
318
+
319
+ pub ( crate ) fn get ( key : & OsStr ) -> Option < OsString > {
320
+ let shell = helpers:: open_shell ( ) ?;
321
+ let mut key_ptr = helpers:: os_string_to_raw ( key) ?;
322
+ unsafe { get_raw ( shell, key_ptr. as_mut_ptr ( ) ) }
323
+ }
324
+
325
+ pub ( crate ) fn set ( key : & OsStr , val : & OsStr ) -> io:: Result < ( ) > {
326
+ let shell = helpers:: open_shell ( ) . ok_or ( unsupported_err ( ) ) ?;
327
+ let mut key_ptr = helpers:: os_string_to_raw ( key)
328
+ . ok_or ( io:: const_io_error!( io:: ErrorKind :: InvalidInput , "Invalid Key" ) ) ?;
329
+ let mut val_ptr = helpers:: os_string_to_raw ( val)
330
+ . ok_or ( io:: const_io_error!( io:: ErrorKind :: InvalidInput , "Invalid Value" ) ) ?;
331
+
332
+ let r = unsafe {
333
+ ( ( * shell. as_ptr ( ) ) . set_env ) (
334
+ key_ptr. as_mut_ptr ( ) ,
335
+ val_ptr. as_mut_ptr ( ) ,
336
+ r_efi:: efi:: Boolean :: FALSE ,
337
+ )
338
+ } ;
339
+
340
+ if r. is_error ( ) { Err ( io:: Error :: from_raw_os_error ( r. as_usize ( ) ) ) } else { Ok ( ( ) ) }
341
+ }
342
+
343
+ pub ( crate ) fn get_all ( ) -> io:: Result < Vec < ( OsString , OsString ) > > {
344
+ let shell = helpers:: open_shell ( ) . ok_or ( unsupported_err ( ) ) ?;
345
+
346
+ let mut vars = Vec :: new ( ) ;
347
+ let val = unsafe { ( ( * shell. as_ptr ( ) ) . get_env ) ( crate :: ptr:: null_mut ( ) ) } ;
348
+
349
+ if val. is_null ( ) {
350
+ return Ok ( vars) ;
351
+ }
352
+
353
+ let mut start = 0 ;
354
+
355
+ // UEFI Shell returns all keys seperated by NULL.
356
+ // End of string is denoted by two NULLs
357
+ for i in 0 .. {
358
+ let temp = unsafe { val. add ( i) } ;
359
+ // ptr.is_null() does not seem to check if *ptr == 0.
360
+ if temp. is_null ( ) || unsafe { * temp } == 0 {
361
+ // Two NULL signal end of string
362
+ if i == start {
363
+ break ;
364
+ }
365
+
366
+ let key = OsString :: from_wide ( unsafe {
367
+ crate :: slice:: from_raw_parts ( val. add ( start) , i - start)
368
+ } ) ;
369
+ // SAFETY: val.add(start) is always NULL terminated
370
+ let val = unsafe { get_raw ( shell, val. add ( start) ) }
371
+ . ok_or ( io:: const_io_error!( io:: ErrorKind :: InvalidInput , "Invalid Value" ) ) ?;
372
+
373
+ vars. push ( ( key, val) ) ;
374
+ start = i + 1 ;
375
+ }
376
+ }
377
+
378
+ Ok ( vars)
379
+ }
380
+
381
+ unsafe fn get_raw (
382
+ shell : NonNull < r_efi:: efi:: protocols:: shell:: Protocol > ,
383
+ key_ptr : * mut r_efi:: efi:: Char16 ,
384
+ ) -> Option < OsString > {
385
+ let val = unsafe { ( ( * shell. as_ptr ( ) ) . get_env ) ( key_ptr) } ;
386
+ helpers:: os_string_from_raw ( val)
387
+ }
388
+ }
0 commit comments