|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | + |
| 3 | +use uefi::proto::scsi::pass_thru::ExtScsiPassThru; |
| 4 | +use uefi::proto::scsi::ScsiRequestBuilder; |
| 5 | + |
| 6 | +pub fn test() { |
| 7 | + info!("Running extended SCSI Pass Thru tests"); |
| 8 | + test_allocating_api(); |
| 9 | + test_reusing_buffer_api(); |
| 10 | +} |
| 11 | + |
| 12 | +fn test_allocating_api() { |
| 13 | + let scsi_ctrl_handles = uefi::boot::find_handles::<ExtScsiPassThru>().unwrap(); |
| 14 | + |
| 15 | + // On I440FX and Q35 (both x86 machines), Qemu configures an IDE and a SATA controller |
| 16 | + // by default respectively. We manually configure an additional SCSI controller. |
| 17 | + // Thus, we should see two controllers with support for EXT_SCSI_PASS_THRU on this platform |
| 18 | + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 19 | + assert_eq!(scsi_ctrl_handles.len(), 2); |
| 20 | + #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
| 21 | + assert_eq!(scsi_ctrl_handles.len(), 1); |
| 22 | + |
| 23 | + let mut found_drive = false; |
| 24 | + for handle in scsi_ctrl_handles { |
| 25 | + let scsi_pt = uefi::boot::open_protocol_exclusive::<ExtScsiPassThru>(handle).unwrap(); |
| 26 | + for device in scsi_pt.iter_devices() { |
| 27 | + // see: https://www.seagate.com/files/staticfiles/support/docs/manual/Interface%20manuals/100293068j.pdf |
| 28 | + // 3.6 INQUIRY command |
| 29 | + let request = ScsiRequestBuilder::read(scsi_pt.io_align()) |
| 30 | + .with_timeout(core::time::Duration::from_millis(500)) |
| 31 | + .with_command_data(&[0x12, 0x00, 0x00, 0x00, 0xFF, 0x00]) |
| 32 | + .unwrap() |
| 33 | + .with_read_buffer(255) |
| 34 | + .unwrap() |
| 35 | + .build(); |
| 36 | + let Ok(response) = device.execute_command(request) else { |
| 37 | + continue; // no device |
| 38 | + }; |
| 39 | + let bfr = response.read_buffer().unwrap(); |
| 40 | + // more no device checks |
| 41 | + if bfr.len() < 32 { |
| 42 | + continue; |
| 43 | + } |
| 44 | + if bfr[0] & 0b00011111 == 0x1F { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + // found device |
| 49 | + let vendor_id = core::str::from_utf8(&bfr[8..16]).unwrap().trim(); |
| 50 | + let product_id = core::str::from_utf8(&bfr[16..32]).unwrap().trim(); |
| 51 | + if vendor_id == "uefi-rs" && product_id == "ExtScsiPassThru" { |
| 52 | + info!( |
| 53 | + "Found Testdisk at: {:?} | {}", |
| 54 | + device.target(), |
| 55 | + device.lun() |
| 56 | + ); |
| 57 | + found_drive = true; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + assert!(found_drive); |
| 63 | +} |
| 64 | + |
| 65 | +fn test_reusing_buffer_api() { |
| 66 | + let scsi_ctrl_handles = uefi::boot::find_handles::<ExtScsiPassThru>().unwrap(); |
| 67 | + |
| 68 | + let mut found_drive = false; |
| 69 | + for handle in scsi_ctrl_handles { |
| 70 | + let scsi_pt = uefi::boot::open_protocol_exclusive::<ExtScsiPassThru>(handle).unwrap(); |
| 71 | + let mut cmd_bfr = scsi_pt.alloc_io_buffer(6).unwrap(); |
| 72 | + cmd_bfr.copy_from(&[0x12, 0x00, 0x00, 0x00, 0xFF, 0x00]); |
| 73 | + let mut read_bfr = scsi_pt.alloc_io_buffer(255).unwrap(); |
| 74 | + |
| 75 | + for device in scsi_pt.iter_devices() { |
| 76 | + // see: https://www.seagate.com/files/staticfiles/support/docs/manual/Interface%20manuals/100293068j.pdf |
| 77 | + // 3.6 INQUIRY command |
| 78 | + let request = ScsiRequestBuilder::read(scsi_pt.io_align()) |
| 79 | + .with_timeout(core::time::Duration::from_millis(500)) |
| 80 | + .use_command_buffer(&mut cmd_bfr) |
| 81 | + .unwrap() |
| 82 | + .use_read_buffer(&mut read_bfr) |
| 83 | + .unwrap() |
| 84 | + .build(); |
| 85 | + let Ok(response) = device.execute_command(request) else { |
| 86 | + continue; // no device |
| 87 | + }; |
| 88 | + let bfr = response.read_buffer().unwrap(); |
| 89 | + // more no device checks |
| 90 | + if bfr.len() < 32 { |
| 91 | + continue; |
| 92 | + } |
| 93 | + if bfr[0] & 0b00011111 == 0x1F { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + // found device |
| 98 | + let vendor_id = core::str::from_utf8(&bfr[8..16]).unwrap().trim(); |
| 99 | + let product_id = core::str::from_utf8(&bfr[16..32]).unwrap().trim(); |
| 100 | + if vendor_id == "uefi-rs" && product_id == "ExtScsiPassThru" { |
| 101 | + info!( |
| 102 | + "Found Testdisk at: {:?} | {}", |
| 103 | + device.target(), |
| 104 | + device.lun() |
| 105 | + ); |
| 106 | + found_drive = true; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + assert!(found_drive); |
| 112 | +} |
0 commit comments