-
-
Notifications
You must be signed in to change notification settings - Fork 182
uefi: add basic integration test for BlockIo and BlockIo2 protocols + fix in BlockIo2 #1842
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
Open
phip1611
wants to merge
2
commits into
main
Choose a base branch
from
block-io
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
|
||
| //! Very basic tests for the BlockIo and BlockIo2 protocols. | ||
| //! | ||
| //! We look for some well-known data on a few well-known disks | ||
| //! of our test environment. | ||
| use alloc::string::String; | ||
| use uefi::boot::{OpenProtocolAttributes, OpenProtocolParams, ScopedProtocol}; | ||
| use uefi::proto::Protocol; | ||
| use uefi::proto::device_path::DevicePath; | ||
| use uefi::proto::device_path::text::{AllowShortcuts, DisplayOnly}; | ||
| use uefi::proto::media::block::{BlockIO, BlockIO2}; | ||
| use uefi::{CString16, Handle, boot}; | ||
| use uefi_raw::protocol::device_path::DeviceSubType; | ||
|
|
||
| fn verify_block_device(dvp: &DevicePath, first_block: &[u8]) { | ||
| // We only look for storage technologies that we are interested in. | ||
| let storage_device_types = [ | ||
| DeviceSubType::MESSAGING_SCSI, | ||
| DeviceSubType::MESSAGING_NVME_NAMESPACE, | ||
| DeviceSubType::MESSAGING_SATA, | ||
| ]; | ||
| let storage_node = dvp | ||
| .node_iter() | ||
| .skip_while(|x| !storage_device_types.contains(&x.sub_type())) | ||
| .next() | ||
| .unwrap(); | ||
| let storage_node_string = storage_node | ||
| .to_string(DisplayOnly(true), AllowShortcuts(true)) | ||
| .unwrap(); | ||
| debug!("Storage technology: {storage_node_string}"); | ||
|
|
||
| //debug!("First 512 bytes: {first_block:?}"); | ||
| match storage_node.sub_type() { | ||
| DeviceSubType::MESSAGING_SCSI => { /* empty disks so far, nothing to check for */ } | ||
| DeviceSubType::MESSAGING_NVME_NAMESPACE => { | ||
| /* empty disks so far, nothing to check for */ | ||
| } | ||
| DeviceSubType::MESSAGING_SATA => { | ||
| // We check that the right SATA disk indeed contains a correct | ||
| // FAT16 volume. | ||
| let expected = "MbrTestDisk"; | ||
| let contains_volume_label = first_block | ||
| .windows(expected.len()) | ||
| .any(|w| w == expected.as_bytes()); | ||
| let oem_name = { | ||
| let bytes = &first_block[3..10]; | ||
| String::from_utf8(bytes.to_vec()) | ||
| }; | ||
| let is_valid_fat = first_block[0] != 0 && oem_name.is_ok(); | ||
| if is_valid_fat && storage_node.data() == &[0x2, 0, 0xff, 0xff, 0x0, 0x0] { | ||
| if !contains_volume_label { | ||
| panic!( | ||
| "Sata disk {storage_node_string} does not contain {expected} in its first 512 bytes" | ||
| ) | ||
| } else { | ||
| debug!( | ||
| "Found volume label {expected} with OEM name: {}", | ||
| oem_name.unwrap() | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
|
|
||
| fn open_proto_and_dvp<P: Protocol>( | ||
| handle: Handle, | ||
| ) -> (ScopedProtocol<P>, ScopedProtocol<DevicePath>, CString16) { | ||
| let proto = unsafe { | ||
| boot::open_protocol::<P>( | ||
| OpenProtocolParams { | ||
| handle, | ||
| agent: boot::image_handle(), | ||
| controller: None, | ||
| }, | ||
| OpenProtocolAttributes::GetProtocol, | ||
| ) | ||
| .unwrap() | ||
| }; | ||
| let dvp = unsafe { | ||
| boot::open_protocol::<DevicePath>( | ||
| OpenProtocolParams { | ||
| handle, | ||
| agent: boot::image_handle(), | ||
| controller: None, | ||
| }, | ||
| OpenProtocolAttributes::GetProtocol, | ||
| ) | ||
| .unwrap() | ||
| }; | ||
| let dvp_string = dvp | ||
| .to_string(DisplayOnly(true), AllowShortcuts(true)) | ||
| .unwrap(); | ||
|
|
||
| (proto, dvp, dvp_string) | ||
| } | ||
|
|
||
| fn test_blockio_protocol() { | ||
| info!("Testing BLOCKIO protocol"); | ||
| for handle in boot::find_handles::<BlockIO>().unwrap() { | ||
| let (proto, dvp, dvp_string) = open_proto_and_dvp::<BlockIO>(handle); | ||
| debug!("Found handle supporting protocol: {dvp_string}"); | ||
| debug!("media: {:?}", proto.media()); | ||
| let mut first_block = vec![0; 512]; | ||
| proto | ||
| .read_blocks(proto.media().media_id(), 0, &mut first_block) | ||
| .unwrap(); | ||
|
|
||
| verify_block_device(&dvp, first_block.as_slice()); | ||
| } | ||
| } | ||
|
|
||
| fn test_blockio2_protocol() { | ||
| info!("Testing BLOCKIO 2 protocol"); | ||
|
|
||
| for handle in boot::find_handles::<BlockIO2>().unwrap() { | ||
| let (proto, dvp, dvp_string) = open_proto_and_dvp::<BlockIO2>(handle); | ||
| debug!("Found handle supporting protocol: {dvp_string}"); | ||
| debug!("media: {:?}", proto.media()); | ||
| let mut first_block = vec![0; 512]; | ||
| unsafe { | ||
| proto | ||
| .read_blocks_ex(proto.media().media_id(), 0, None, &mut first_block) | ||
| .unwrap(); | ||
| } | ||
|
|
||
| verify_block_device(&dvp, first_block.as_slice()); | ||
| } | ||
| } | ||
|
|
||
| pub fn test() { | ||
| test_blockio_protocol(); | ||
| test_blockio2_protocol(); | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @Virv12 FYI. Unfortunately, I missed this in the review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this change, using a reference introduces a lifetime, implying we are going to use the buffer only until the lifetime is alive, which is wrong.
There has been a similar discussion on DiskIO2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for pointing that out to me, I wasn't aware of this anymore!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching that, I had forgotten about that too :)