8000 uefi: fs: Implement exists by Ayush1325 · Pull Request #135368 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Conversation

Ayush1325
Copy link
Contributor

Also adds the initial file abstractions.

The file opening algorithm is inspired from UEFI shell. It starts by classifying if the Path is Shell mapping, text representation of device path protocol, or a relative path and converts into an absolute text representation of device path protocol.

After that, it queries all handles supporting
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL and opens the volume that matches the device path protocol prefix (similar to Windows drive). After that, it opens the file in the volume using the remaining pat.

It also introduces OwnedDevicePath and BorrowedDevicePath abstractions to allow working with the base UEFI and Shell device paths efficiently.

DevicePath in UEFI behaves like an a group of nodes laied out in the memory contiguously and thus can be modeled using iterators.

This is an effort to break the original PR (#129700) into much smaller chunks for faster upstreaming.

@rustbot
Copy link
Collaborator
rustbot commented Jan 11, 2025

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 11, 2025
Copy link
Member
@jhpratt jhpratt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @dvdhrm and @nicholasbishop as UEFI target maintainers. r=me with approval from either of them and the typo fixed.

@nicholasbishop
Copy link
Contributor

There are still quite a few changes in this PR. For better review, could we break it up into some smaller pieces? For example, maybe we could start with a PR that just does some of the device-path helper changes.

@Ayush1325
Copy link
Contributor Author
Ayush1325 commented Jan 12, 2025

There are still quite a few changes in this PR. For better review, could we break it up into some smaller pieces? For example, maybe we could start with a PR that just does some of the device-path helper changes.

Would it be fine to have some dead code with the cfg attribute? I can add the device path abstraction and the file open stuff separately in that case.

Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025
This PR is split off from rust-lang#135368 to reduce noise.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025
This PR is split off from rust-lang#135368 to reduce noise.

No real functionality changes, just some quality of life improvements.

Also implement Debug for OwnedDevicePath for some quality of life
improvements.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
@Ayush1325
Copy link
Contributor Author

There are still quite a few changes in this PR. For better review, could we break it up into some smaller pieces? For example, maybe we could start with a PR that just does some of the device-path helper changes.

I have moved the changes to the old DevicePath to a separate PR: #135393

It only contains stuff for OwnedDevicePath, but it should reduce all the noise due to renaming and refactoring from this PR.

Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025
This PR is split off from rust-lang#135368 to reduce noise.

No real functionality changes, just some quality of life improvements.

Introduce `device_path_to_text_raw` which creates a Box<[u16]> (UTF-16
string) from path instead of creating OsString. OsString internally is
stored as WTF-8, which means converting OsString to Box<[u16]> requires
allocation. This is not ideal for std::fs APIs where we need to perform
Device Path Protocol matching while opening a volume, and create a UEFI
UTF-16 string from the remaining path (which represents file path inside
a volume). This remaining path is never used on the Rust side, and thus
does not need to be converted to WTF-8 to be used. By introducing direct
conversion to Box<[u16]>, we shorten the conversions from
`EFI_DEVICE_PATH_PROTOCOL` -> WTF-8 -> UTF-16 to
`EFI_DEVICE_PATH_PROTOCOL` -> UTF-16 which is required in every file
open operation.

Also implement Debug for OwnedDevicePath for some quality of life
improvements.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025
This PR is split off from rust-lang#135368 to reduce noise.

No real functionality changes, just some quality of life improvements.

Introduce `device_path_to_text_raw` which creates a Box<[u16]> (UTF-16
string) from path instead of creating OsString. The UTF-16 returned by
`EFI_DEVICE_PATH_TO_TEXT` protocol is owned by the caller, so we are
just moving the memory management to box instead of freeing it in the
function itself.

OsString internally is stored as WTF-8, which means converting OsString
to Box<[u16]> requires allocation. This is not ideal for std::fs APIs
where we need to perform Device Path Protocol matching while opening a
volume, and create a UEFI UTF-16 string from the remaining path (which
represents file path inside a volume). This remaining path is never used
on the Rust side, and thus does not need to be converted to WTF-8 to be
used. By introducing direct conversion to Box<[u16]>, we shorten the
conversions from `EFI_DEVICE_PATH_PROTOCOL` -> WTF-8 -> UTF-16 to
`EFI_DEVICE_PATH_PROTOCOL` -> UTF-16 which is required in every file
open operation.

Also implement Debug for OwnedDevicePath for some quality of life
improvements.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025
This PR is split off from rust-lang#135368 to reduce noise.

No real functionality changes, just some quality of life improvements.

Introduce `device_path_to_text_raw` which creates a Box<[u16]> (UTF-16
string) from path instead of creating OsString. The UTF-16 returned by
`EFI_DEVICE_PATH_TO_TEXT` protocol is owned by the caller, so we are
just moving the memory management to box instead of freeing it in the
function itself.

OsString internally is stored as WTF-8, which means converting OsString
to Box<[u16]> requires allocation. This is not ideal for std::fs APIs
where we need to perform Device Path Protocol matching while opening a
volume, and create a UEFI UTF-16 string from the remaining path (which
represents file path inside a volume). This remaining path is never used
on the Rust side, and thus does not need to be converted to WTF-8 to be
used. By introducing direct conversion to Box<[u16]>, we shorten the
conversions from `EFI_DEVICE_PATH_PROTOCOL` -> WTF-8 -> UTF-16 to
`EFI_DEVICE_PATH_PROTOCOL` -> UTF-16 which is required in every file
open operation. That is, we remove 2 intermediate allocation and 1
UTF-16 validation.

Also implement Debug for OwnedDevicePath for some quality of life
improvements.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025
I am working on fs support for UEFI [0], which similar to windows has prefix
components, but is not quite same as Windows. It also seems that Prefix
is tied closely to Windows and cannot really be extended [0].

This PR just tries to remove coupling between Prefix and absolute path
checking to allow platforms to provide there own implementation to check
if a path is absolute or not.

I am not sure if any platform other than windows currently uses Prefix,
so I have kept the path.prefix().is_some() check in most cases.

[0]: rust-lang#135368
[1]: rust-lang#52331 (comment)

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025
I am working on fs support for UEFI [0], which similar to windows has prefix
components, but is not quite same as Windows. It also seems that Prefix
is tied closely to Windows and cannot really be extended [1].

This PR just tries to remove coupling between Prefix and absolute path
checking to allow platforms to provide there own implementation to check
if a path is absolute or not.

I am not sure if any platform other than windows currently uses Prefix,
so I have kept the path.prefix().is_some() check in most cases.

[0]: rust-lang#135368
[1]: rust-lang#52331 (comment)

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025
This PR is split off from rust-lang#135368 to reduce noise.

No real functionality changes, just some quality of life improvements.

Introduce `device_path_to_text_raw` which creates a Box<[u16]> (UTF-16
string) from path instead of creating OsString. The UTF-16 returned by
`EFI_DEVICE_PATH_TO_TEXT` protocol is owned by the caller, so we are
just moving the memory management to box instead of freeing it in the
function itself.

OsString internally is stored as WTF-8, which means converting OsString
to Box<[u16]> requires allocation. This is not ideal for std::fs APIs
where we need to perform Device Path Protocol matching while opening a
volume, and create a UEFI UTF-16 string from the remaining path (which
represents file path inside a volume). This remaining path is never used
on the Rust side, and thus does not need to be converted to WTF-8 to be
used. By introducing direct conversion to Box<[u16]>, we shorten the
conversions from `EFI_DEVICE_PATH_PROTOCOL` -> WTF-8 -> UTF-16 to
`EFI_DEVICE_PATH_PROTOCOL` -> UTF-16 which is required in every file
open operation. That is, we remove 2 intermediate allocation and 1
UTF-16 validation.

Also implement Debug for OwnedDevicePath for some quality of life
improvements.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025
This PR is split off from rust-lang#135368 to reduce noise.

Also implement Debug for OwnedDevicePath for some quality of life
improvements.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
Ayush1325 added a commit to Ayush1325/rust that referenced this pull request Jan 12, 2025