Skip to content

Commit a5d83c0

Browse files
authored
Merge pull request #1352 from nicholasbishop/bishop-fs-generic
uefi: Make FileSystem work with both variants of ScopedProtocol
2 parents f529325 + 02e5f0e commit a5d83c0

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

uefi/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ how to integrate the `uefi` crate into them.
1717
- `allocator::init` and `allocator::exit_boot_services` have been
1818
deprecated. These functions are now no-ops. The allocator now internally uses
1919
the global system table.
20+
- `FileSystem::new` now accepts `boot::ScopedProtocol` in addition to
21+
`table::boot::ScopedProtocol`.
2022

2123

2224
# uefi - 0.31.0 (2024-08-21)

uefi/src/fs/file_system/fs.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Module for [`FileSystem`].
22
33
use crate::fs::*;
4-
use crate::table::boot::ScopedProtocol;
54
use crate::Status;
65
use alloc::boxed::Box;
76
use alloc::string::String;
@@ -14,20 +13,29 @@ use core::ops::Deref;
1413
/// Return type for public [`FileSystem`] operations.
1514
pub type FileSystemResult<T> = Result<T, Error>;
1615

16+
/// Contents of the `FileSystem` struct, allowing either variant of
17+
/// `ScopedProtocol` to be used. This is temporary; once `BootServices` and the
18+
/// associated `ScopedProtocol<'a>` structs are removed this inner type can be
19+
/// removed as well.
20+
enum FileSystemInner<'a> {
21+
WithLifetime(uefi::table::boot::ScopedProtocol<'a, SimpleFileSystemProtocol>),
22+
WithoutLifetime(uefi::boot::ScopedProtocol<SimpleFileSystemProtocol>),
23+
}
24+
1725
/// High-level file-system abstraction for UEFI volumes with an API that is
1826
/// close to `std::fs`. It acts as convenient accessor around the
1927
/// [`SimpleFileSystemProtocol`].
2028
///
2129
/// Please refer to the [module documentation] for more information.
2230
///
2331
/// [module documentation]: uefi::fs
24-
pub struct FileSystem<'a>(ScopedProtocol<'a, SimpleFileSystemProtocol>);
32+
pub struct FileSystem<'a>(FileSystemInner<'a>);
2533

2634
impl<'a> FileSystem<'a> {
2735
/// Constructor.
2836
#[must_use]
29-
pub const fn new(proto: ScopedProtocol<'a, SimpleFileSystemProtocol>) -> Self {
30-
Self(proto)
37+
pub fn new(proto: impl Into<Self>) -> Self {
38+
proto.into()
3139
}
3240

3341
/// Returns `Ok(true)` if the path points at an existing file.
@@ -387,7 +395,11 @@ impl<'a> FileSystem<'a> {
387395

388396
/// Opens a fresh handle to the root directory of the volume.
389397
fn open_root(&mut self) -> FileSystemResult<UefiDirectoryHandle> {
390-
self.0.open_volume().map_err(|err| {
398+
match &mut self.0 {
399+
FileSystemInner::WithLifetime(proto) => proto.open_volume(),
400+
FileSystemInner::WithoutLifetime(proto) => proto.open_volume(),
401+
}
402+
.map_err(|err| {
391403
Error::Io(IoError {
392404
path: {
393405
let mut path = PathBuf::new();
@@ -434,8 +446,22 @@ impl<'a> FileSystem<'a> {
434446

435447
impl<'a> Debug for FileSystem<'a> {
436448
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
437-
f.debug_tuple("FileSystem(<>))")
438-
.field(&(self.0.deref() as *const _))
439-
.finish()
449+
let ptr: *const _ = match &self.0 {
450+
FileSystemInner::WithLifetime(proto) => proto.deref(),
451+
FileSystemInner::WithoutLifetime(proto) => proto.deref(),
452+
};
453+
f.debug_tuple("FileSystem").field(&ptr).finish()
454+
}
455+
}
456+
457+
impl<'a> From<uefi::table::boot::ScopedProtocol<'a, SimpleFileSystemProtocol>> for FileSystem<'a> {
458+
fn from(proto: uefi::table::boot::ScopedProtocol<'a, SimpleFileSystemProtocol>) -> Self {
459+
Self(FileSystemInner::WithLifetime(proto))
460+
}
461+
}
462+
463+
impl<'a> From<uefi::boot::ScopedProtocol<SimpleFileSystemProtocol>> for FileSystem<'a> {
464+
fn from(proto: uefi::boot::ScopedProtocol<SimpleFileSystemProtocol>) -> Self {
465+
Self(FileSystemInner::WithoutLifetime(proto))
440466
}
441467
}

0 commit comments

Comments
 (0)