Skip to content

Commit 588bfb4

Browse files
committed
std: uefi: Add basic Env variables
- Implement environment variable functions - Using EFI Shell protocol. Signed-off-by: Ayush Singh <ayush@beagleboard.org>
1 parent 9ce3675 commit 588bfb4

2 files changed

Lines changed: 132 additions & 15 deletions

File tree

‎library/std/src/sys/pal/uefi/helpers.rs‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! - More information about protocols can be found [here](https://edk2-docs.gitbook.io/edk-ii-uefi-driver-writer-s-guide/3_foundation/36_protocols_and_handles)
1111
1212
use r_efi::efi::{self,Guid};
13-
use r_efi::protocols::{device_path, device_path_to_text};
13+
use r_efi::protocols::{device_path, device_path_to_text, shell};
1414

1515
usecrate::ffi::{OsStr,OsString};
1616
usecrate::io::{self, const_io_error};
@@ -424,3 +424,24 @@ pub(crate) fn os_string_to_raw(s: &OsStr) -> Option<Box<[r_efi::efi::Char16]>> {
424424
let temp = s.encode_wide().chain(Some(0)).collect::<Box<[r_efi::efi::Char16]>>();
425425
if temp[..temp.len() - 1].contains(&0){None}else{Some(temp)}
426426
}
427+
428+
pub(crate)fnopen_shell() -> Option<NonNull<shell::Protocol>>{
429+
staticLAST_VALID_HANDLE:AtomicPtr<crate::ffi::c_void> =
430+
AtomicPtr::new(crate::ptr::null_mut());
431+
432+
ifletSome(handle) = NonNull::new(LAST_VALID_HANDLE.load(Ordering::Acquire)){
433+
ifletOk(protocol) = open_protocol::<shell::Protocol>(handle, shell::PROTOCOL_GUID){
434+
returnSome(protocol);
435+
}
436+
}
437+
438+
let handles = locate_handles(shell::PROTOCOL_GUID).ok()?;
439+
for handle in handles {
440+
ifletOk(protocol) = open_protocol::<shell::Protocol>(handle, shell::PROTOCOL_GUID){
441+
LAST_VALID_HANDLE.store(handle.as_ptr(),Ordering::Release);
442+
returnSome(protocol);
443+
}
444+
}
445+
446+
None
447+
}

‎library/std/src/sys/pal/uefi/os.rs‎

Lines changed: 110 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,44 +192,58 @@ pub fn current_exe() -> io::Result<PathBuf> {
192192
helpers::device_path_to_text(protocol).map(PathBuf::from)
193193
}
194194

195-
pubstructEnv(!);
195+
pubstructEnvStrDebug<'a>{
196+
iter:&'a[(OsString,OsString)],
197+
}
198+
199+
impl fmt::DebugforEnvStrDebug<'_>{
200+
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
201+
letmut list = f.debug_list();
202+
for(a, b)inself.iter{
203+
list.entry(&(a.to_str().unwrap(), b.to_str().unwrap()));
204+
}
205+
list.finish()
206+
}
207+
}
208+
209+
pubstructEnv(crate::vec::IntoIter<(OsString,OsString)>);
196210

197211
implEnv{
198212
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
199213
pubfnstr_debug(&self) -> impl fmt::Debug + '_{
200-
letSelf(inner) = self;
201-
match*inner {}
214+
EnvStrDebug{iter:self.0.as_slice()}
202215
}
203216
}
204217

205218
implIteratorforEnv{
206219
typeItem = (OsString,OsString);
220+
207221
fnnext(&mutself) -> Option<(OsString,OsString)>{
208-
self.0
222+
self.0.next()
209223
}
210224
}
211225

212226
impl fmt::DebugforEnv{
213-
fnfmt(&self, _:&mut fmt::Formatter<'_>) -> fmt::Result{
214-
letSelf(inner) = self;
215-
match*inner {}
227+
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
228+
self.0.fmt(f)
216229
}
217230
}
218231

219232
pubfnenv() -> 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())
221235
}
222236

223-
pubfngetenv(_:&OsStr) -> Option<OsString>{
224-
None
237+
pubfngetenv(key:&OsStr) -> Option<OsString>{
238+
uefi_env::get(key)
225239
}
226240

227-
pubunsafefnsetenv(_:&OsStr,_:&OsStr) -> io::Result<()>{
228-
Err(io::const_io_error!(io::ErrorKind::Unsupported,"cannot set env vars on this platform"))
241+
pubunsafefnsetenv(key:&OsStr,val:&OsStr) -> io::Result<()>{
242+
uefi_env::set(key, val)
229243
}
230244

231-
pubunsafefnunsetenv(_:&OsStr) -> io::Result<()>{
232-
Err(io::const_io_error!(io::ErrorKind::Unsupported,"cannot unset env vars on this platform"))
245+
pubunsafefnunsetenv(key:&OsStr) -> io::Result<()>{
246+
uefi_env::unset(key)
233247
}
234248

235249
pubfntemp_dir() -> PathBuf{
@@ -294,3 +308,85 @@ mod uefi_shell {
294308
None
295309
}
296310
}
311+
312+
mod uefi_env {
313+
usecrate::ffi::{OsStr,OsString};
314+
usecrate::io;
315+
usecrate::os::uefi::ffi::OsStringExt;
316+
usecrate::ptr::NonNull;
317+
usecrate::sys::{helpers, unsupported_err};
318+
319+
pub(crate)fnget(key:&OsStr) -> Option<OsString>{
320+
let shell = helpers::open_shell()?;
321+
letmut key_ptr = helpers::os_string_to_raw(key)?;
322+
unsafe{get_raw(shell, key_ptr.as_mut_ptr())}
323+
}
324+
325+
pub(crate)fnset(key:&OsStr,val:&OsStr) -> io::Result<()>{
326+
letmut key_ptr = helpers::os_string_to_raw(key)
327+
.ok_or(io::const_io_error!(io::ErrorKind::InvalidInput,"Invalid Key"))?;
328+
letmut val_ptr = helpers::os_string_to_raw(val)
329+
.ok_or(io::const_io_error!(io::ErrorKind::InvalidInput,"Invalid Value"))?;
330+
unsafe{set_raw(key_ptr.as_mut_ptr(), val_ptr.as_mut_ptr())}
331+
}
332+
333+
pub(crate)fnunset(key:&OsStr) -> io::Result<()>{
334+
letmut key_ptr = helpers::os_string_to_raw(key)
335+
.ok_or(io::const_io_error!(io::ErrorKind::InvalidInput,"Invalid Key"))?;
336+
unsafe{set_raw(key_ptr.as_mut_ptr(),crate::ptr::null_mut())}
337+
}
338+
339+
pub(crate)fnget_all() -> io::Result<Vec<(OsString,OsString)>>{
340+
let shell = helpers::open_shell().ok_or(unsupported_err())?;
341+
342+
letmut vars = Vec::new();
343+
let val = unsafe{((*shell.as_ptr()).get_env)(crate::ptr::null_mut())};
344+
345+
if val.is_null(){
346+
returnOk(vars);
347+
}
348+
349+
letmut start = 0;
350+
351+
// UEFI Shell returns all keys seperated by NULL.
352+
// End of string is denoted by two NULLs
353+
for i in0.. {
354+
ifunsafe{*val.add(i)} == 0{
355+
// Two NULL signal end of string
356+
if i == start {
357+
break;
358+
}
359+
360+
let key = OsString::from_wide(unsafe{
361+
crate::slice::from_raw_parts(val.add(start), i - start)
362+
});
363+
// SAFETY: val.add(start) is always NULL terminated
364+
let val = unsafe{get_raw(shell, val.add(start))}
365+
.ok_or(io::const_io_error!(io::ErrorKind::InvalidInput,"Invalid Value"))?;
366+
367+
vars.push((key, val));
368+
start = i + 1;
369+
}
370+
}
371+
372+
Ok(vars)
373+
}
374+
375+
unsafefnget_raw(
376+
shell:NonNull<r_efi::efi::protocols::shell::Protocol>,
377+
key_ptr:*mut r_efi::efi::Char16,
378+
) -> Option<OsString>{
379+
let val = unsafe{((*shell.as_ptr()).get_env)(key_ptr)};
380+
helpers::os_string_from_raw(val)
381+
}
382+
383+
unsafefnset_raw(
384+
key_ptr:*mut r_efi::efi::Char16,
385+
val_ptr:*mut r_efi::efi::Char16,
386+
) -> io::Result<()>{
387+
let shell = helpers::open_shell().ok_or(unsupported_err())?;
388+
let r =
389+
unsafe{((*shell.as_ptr()).set_env)(key_ptr, val_ptr, r_efi::efi::Boolean::FALSE)};
390+
if r.is_error(){Err(io::Error::from_raw_os_error(r.as_usize()))}else{Ok(())}
391+
}
392+
}

0 commit comments

Comments
 (0)