-
Notifications
You must be signed in to change notification settings - Fork 13.4k
uefi: Implement path #135475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
uefi: Implement path #135475
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#![forbid(unsafe_op_in_unsafe_fn)] | ||
use crate::ffi::OsStr; | ||
use crate::io; | ||
use crate::path::{Path, PathBuf, Prefix}; | ||
use crate::sys::{helpers, unsupported_err}; | ||
|
||
const FORWARD_SLASH: u8 = b'/'; | ||
const COLON: u8 = b':'; | ||
|
||
#[inline] | ||
pub fn is_sep_byte(b: u8) -> bool { | ||
b == b'\\' | ||
} | ||
|
||
#[inline] | ||
pub fn is_verbatim_sep(b: u8) -> bool { | ||
b == b'\\' | ||
} | ||
|
||
pub fn parse_prefix(_: &OsStr) -> Option<Prefix<'_>> { | ||
None | ||
} | ||
|
||
pub const MAIN_SEP_STR: &str = "\\"; | ||
pub const MAIN_SEP: char = '\\'; | ||
|
||
/// UEFI paths can be of 4 types: | ||
/// | ||
/// 1. Absolute Shell Path: Uses shell mappings (eg: `FS0:`). Does not exist if UEFI shell not present. | ||
/// It can be identified with `:`. | ||
/// Eg: FS0:\abc\run.efi | ||
/// | ||
/// 2. Absolute Device Path: this is what we want | ||
/// It can be identified with `/`. | ||
/// Eg: PciRoot(0x0)/Pci(0x1,0x1)/Ata(Secondary,Slave,0x0)/\abc\run.efi | ||
/// | ||
/// 3: Relative root: path relative to the current volume. | ||
/// It will start with `\`. | ||
/// Eg: \abc\run.efi | ||
/// | ||
/// 4: Relative | ||
/// Eg: run.efi | ||
/// | ||
/// The algorithm is mostly taken from edk2 UEFI shell implementation and is | ||
/// somewhat simple. Check for the path type in order. | ||
/// | ||
/// The volume mapping in Absolute Shell Path (not the rest of the path) can be converted to Device | ||
/// Path Protocol using `EFI_SHELL->GetDevicePathFromMap`. The rest of the path (Relative root | ||
/// path), can just be appended to the remaining path. | ||
/// | ||
/// For Relative root, we get the current volume (either in Shell Mapping, or Device Path Protocol | ||
/// form) and join it with the relative root path. We then recurse the function to resolve the Shell | ||
/// Mapping if present. | ||
/// | ||
/// For Relative paths, we use the current working directory to construct | ||
/// the new path and recurse the function to resolve the Shell mapping if present. | ||
/// | ||
/// Finally, at the end, we get the 2nd form, i.e. Absolute Device Path, which can be used in the | ||
/// normal UEFI APIs such as file, process, etc. | ||
/// Eg: PciRoot(0x0)/Pci(0x1,0x1)/Ata(Secondary,Slave,0x0)/\abc\run.efi | ||
pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> { | ||
Ayush1325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Absolute Shell Path | ||
if path.as_os_str().as_encoded_bytes().contains(&COLON) { | ||
let mut path_components = path.components(); | ||
// Since path is not empty, it has at least one Component | ||
let prefix = path_components.next().unwrap(); | ||
|
||
let dev_path = helpers::get_device_path_from_map(prefix.as_ref())?; | ||
let mut dev_path_text = dev_path.to_text().map_err(|_| unsupported_err())?; | ||
|
||
// UEFI Shell does not seem to end device path with `/` | ||
if *dev_path_text.as_encoded_bytes().last().unwrap() != FORWARD_SLASH { | ||
dev_path_text.push("/"); | ||
} | ||
|
||
let mut ans = PathBuf::from(dev_path_text); | ||
ans.push(path_components); | ||
|
||
return Ok(ans); | ||
} | ||
|
||
// Absolute Device Path | ||
if path.as_os_str().as_encoded_bytes().contains(&FORWARD_SLASH) { | ||
return Ok(path.to_path_buf()); | ||
} | ||
|
||
// cur_dir() always returns something | ||
let cur_dir = crate::env::current_dir().unwrap(); | ||
let mut path_components = path.components(); | ||
|
||
// Relative Root | ||
if path_components.next().unwrap() == crate::path::Component::RootDir { | ||
let mut ans = PathBuf::new(); | ||
ans.push(cur_dir.components().next().unwrap()); | ||
ans.push(path_components); | ||
return absolute(&ans); | ||
} | ||
|
||
absolute(&cur_dir.join(path)) | ||
} | ||
|
||
pub(crate) fn is_absolute(path: &Path) -> bool { | ||
let temp = path.as_os_str().as_encoded_bytes(); | ||
temp.contains(&COLON) || temp.contains(&FORWARD_SLASH) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.