2021-01-21 12:19:07 +00:00
|
|
|
//! Bindings for libsgutils2
|
|
|
|
//!
|
|
|
|
//! Incomplete, but we currently do not need more.
|
|
|
|
//!
|
|
|
|
//! See: `/usr/include/scsi/sg_pt.h`
|
2020-12-23 08:44:53 +00:00
|
|
|
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
|
|
|
|
use anyhow::{bail, Error};
|
|
|
|
use libc::{c_char, c_int};
|
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
// Opaque wrapper for sg_pt_base
|
2020-12-23 08:44:53 +00:00
|
|
|
#[repr(C)]
|
2021-01-21 15:38:24 +00:00
|
|
|
struct SgPtBase { _private: [u8; 0] }
|
2020-12-23 08:44:53 +00:00
|
|
|
|
|
|
|
impl Drop for SgPtBase {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { destruct_scsi_pt_obj(self as *mut SgPtBase) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 14:15:54 +00:00
|
|
|
pub const SCSI_PT_RESULT_GOOD:c_int = 0;
|
|
|
|
pub const SCSI_PT_RESULT_STATUS:c_int = 1;
|
|
|
|
pub const SCSI_PT_RESULT_SENSE:c_int = 2;
|
|
|
|
pub const SCSI_PT_RESULT_TRANSPORT_ERR:c_int = 3;
|
|
|
|
pub const SCSI_PT_RESULT_OS_ERR:c_int = 4;
|
|
|
|
|
2020-12-23 08:44:53 +00:00
|
|
|
#[link(name = "sgutils2")]
|
|
|
|
extern {
|
2021-01-21 15:38:24 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn scsi_pt_open_device(
|
2020-12-23 08:44:53 +00:00
|
|
|
device_name: * const c_char,
|
|
|
|
read_only: bool,
|
|
|
|
verbose: c_int,
|
|
|
|
) -> c_int;
|
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn sg_is_scsi_cdb(
|
2020-12-23 08:44:53 +00:00
|
|
|
cdbp: *const u8,
|
|
|
|
clen: c_int,
|
|
|
|
) -> bool;
|
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn construct_scsi_pt_obj() -> *mut SgPtBase;
|
|
|
|
fn destruct_scsi_pt_obj(objp: *mut SgPtBase);
|
2020-12-23 08:44:53 +00:00
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn set_scsi_pt_data_in(
|
2020-12-23 08:44:53 +00:00
|
|
|
objp: *mut SgPtBase,
|
|
|
|
dxferp: *const u8,
|
|
|
|
dxfer_ilen: c_int,
|
|
|
|
);
|
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn set_scsi_pt_data_out(
|
2021-01-16 14:15:54 +00:00
|
|
|
objp: *mut SgPtBase,
|
|
|
|
dxferp: *const u8,
|
|
|
|
dxfer_olen: c_int,
|
|
|
|
);
|
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn set_scsi_pt_cdb(
|
2020-12-23 08:44:53 +00:00
|
|
|
objp: *mut SgPtBase,
|
|
|
|
cdb: *const u8,
|
|
|
|
cdb_len: c_int,
|
|
|
|
);
|
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn set_scsi_pt_sense(
|
2020-12-23 08:44:53 +00:00
|
|
|
objp: *mut SgPtBase,
|
|
|
|
sense: *const u8,
|
|
|
|
max_sense_len: c_int,
|
|
|
|
);
|
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn do_scsi_pt(
|
2020-12-23 08:44:53 +00:00
|
|
|
objp: *mut SgPtBase,
|
|
|
|
fd: c_int,
|
|
|
|
timeout_secs: c_int,
|
|
|
|
verbose: c_int,
|
|
|
|
) -> c_int;
|
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn get_scsi_pt_resid(objp: *const SgPtBase) -> c_int;
|
2020-12-23 08:44:53 +00:00
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn get_scsi_pt_sense_len(objp: *const SgPtBase) -> c_int;
|
2020-12-23 08:44:53 +00:00
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
fn get_scsi_pt_status_response(objp: *const SgPtBase) -> c_int;
|
2021-01-16 14:15:54 +00:00
|
|
|
|
2021-01-21 15:38:24 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
fn get_scsi_pt_result_category(objp: *const SgPtBase) -> c_int;
|
2020-12-23 08:44:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 12:19:07 +00:00
|
|
|
/// Creates a `Box<SgPtBase>`
|
2020-12-23 08:44:53 +00:00
|
|
|
///
|
|
|
|
/// Which get automatically dropped, so you do not need to call
|
|
|
|
/// destruct_scsi_pt_obj yourself.
|
2021-01-21 15:38:24 +00:00
|
|
|
fn boxed_scsi_pt_obj() -> Result<Box<SgPtBase>, Error> {
|
2020-12-23 08:44:53 +00:00
|
|
|
let objp = unsafe {
|
|
|
|
construct_scsi_pt_obj()
|
|
|
|
};
|
|
|
|
if objp.is_null() {
|
|
|
|
bail!("construct_scsi_pt_ob failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(unsafe { std::mem::transmute(objp)})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Safe interface to run RAW SCSI commands
|
|
|
|
pub struct SgRaw<'a, F> {
|
|
|
|
file: &'a mut F,
|
|
|
|
buffer: Box<[u8]>,
|
|
|
|
sense_buffer: [u8; 32],
|
2021-01-24 13:54:30 +00:00
|
|
|
timeout: i32,
|
2020-12-23 08:44:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 12:19:07 +00:00
|
|
|
/// Allocate a page aligned buffer
|
|
|
|
///
|
|
|
|
/// SG RAWIO commands needs page aligned transfer buffers.
|
2021-01-16 14:15:54 +00:00
|
|
|
pub fn alloc_page_aligned_buffer(buffer_size: usize) -> Result<Box<[u8]> , Error> {
|
|
|
|
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
|
|
|
|
let layout = std::alloc::Layout::from_size_align(buffer_size, page_size)?;
|
|
|
|
let dinp = unsafe { std::alloc::alloc_zeroed(layout) };
|
|
|
|
if dinp.is_null() {
|
|
|
|
bail!("alloc SCSI output buffer failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffer_slice = unsafe { std::slice::from_raw_parts_mut(dinp, buffer_size)};
|
|
|
|
Ok(unsafe { Box::from_raw(buffer_slice) })
|
|
|
|
}
|
|
|
|
|
2020-12-23 08:44:53 +00:00
|
|
|
impl <'a, F: AsRawFd> SgRaw<'a, F> {
|
|
|
|
|
|
|
|
/// Create a new instance to run commands
|
|
|
|
///
|
|
|
|
/// The file must be a handle to a SCSI device.
|
|
|
|
pub fn new(file: &'a mut F, buffer_size: usize) -> Result<Self, Error> {
|
|
|
|
|
2021-01-16 14:15:54 +00:00
|
|
|
let buffer;
|
2020-12-23 08:44:53 +00:00
|
|
|
|
2021-01-16 14:15:54 +00:00
|
|
|
if buffer_size > 0 {
|
|
|
|
buffer = alloc_page_aligned_buffer(buffer_size)?;
|
|
|
|
} else {
|
|
|
|
buffer = Box::new([]);
|
|
|
|
}
|
2020-12-23 08:44:53 +00:00
|
|
|
|
|
|
|
let sense_buffer = [0u8; 32];
|
|
|
|
|
2021-01-24 13:54:30 +00:00
|
|
|
Ok(Self { file, buffer, sense_buffer, timeout: 0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the command timeout in seconds (0 means default (60 seconds))
|
|
|
|
pub fn set_timeout(&mut self, seconds: usize) {
|
|
|
|
if seconds > (i32::MAX as usize) {
|
|
|
|
self.timeout = i32::MAX; // don't care about larger values
|
|
|
|
} else {
|
|
|
|
self.timeout = seconds as i32;
|
|
|
|
}
|
2020-12-23 08:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create new object with initialized data_in and sense buffer
|
|
|
|
fn create_boxed_scsi_pt_obj(&mut self) -> Result<Box<SgPtBase>, Error> {
|
|
|
|
|
|
|
|
let mut ptvp = boxed_scsi_pt_obj()?;
|
|
|
|
|
2021-01-16 14:15:54 +00:00
|
|
|
if self.buffer.len() > 0 {
|
|
|
|
unsafe {
|
|
|
|
set_scsi_pt_data_in(
|
|
|
|
&mut *ptvp,
|
|
|
|
self.buffer.as_ptr(),
|
|
|
|
self.buffer.len() as c_int,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2020-12-23 08:44:53 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
set_scsi_pt_sense(
|
|
|
|
&mut *ptvp,
|
|
|
|
self.sense_buffer.as_ptr(),
|
|
|
|
self.sense_buffer.len() as c_int,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ptvp)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run the specified RAW SCSI command
|
|
|
|
pub fn do_command(&mut self, cmd: &[u8]) -> Result<&[u8], Error> {
|
|
|
|
|
|
|
|
if !unsafe { sg_is_scsi_cdb(cmd.as_ptr(), cmd.len() as c_int) } {
|
|
|
|
bail!("no valid SCSI command");
|
|
|
|
}
|
|
|
|
|
2021-01-16 14:15:54 +00:00
|
|
|
if self.buffer.len() < 16 {
|
|
|
|
bail!("output buffer too small");
|
|
|
|
}
|
|
|
|
|
2020-12-23 08:44:53 +00:00
|
|
|
let mut ptvp = self.create_boxed_scsi_pt_obj()?;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
set_scsi_pt_cdb(
|
|
|
|
&mut *ptvp,
|
|
|
|
cmd.as_ptr(),
|
|
|
|
cmd.len() as c_int,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2021-01-24 13:54:30 +00:00
|
|
|
let res = unsafe { do_scsi_pt(&mut *ptvp, self.file.as_raw_fd(), self.timeout, 0) };
|
2020-12-23 08:44:53 +00:00
|
|
|
if res < 0 {
|
|
|
|
let err = nix::Error::last();
|
|
|
|
bail!("do_scsi_pt failed - {}", err);
|
|
|
|
}
|
|
|
|
if res != 0 {
|
|
|
|
bail!("do_scsi_pt failed {}", res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: what about sense data?
|
2021-01-19 13:40:13 +00:00
|
|
|
let _sense_len = unsafe { get_scsi_pt_sense_len(&*ptvp) };
|
2020-12-23 08:44:53 +00:00
|
|
|
|
2021-01-19 13:40:13 +00:00
|
|
|
let status = unsafe { get_scsi_pt_status_response(&*ptvp) };
|
2020-12-23 08:44:53 +00:00
|
|
|
if status != 0 {
|
|
|
|
// toto: improve error reporting
|
|
|
|
bail!("unknown scsi error - status response {}", status);
|
|
|
|
}
|
|
|
|
|
2021-01-24 14:19:43 +00:00
|
|
|
let resid = unsafe { get_scsi_pt_resid(&*ptvp) } as usize;
|
|
|
|
if resid > self.buffer.len() {
|
|
|
|
bail!("do_scsi_pt failed - got strange resid (value too big)");
|
2020-12-23 08:44:53 +00:00
|
|
|
}
|
2021-01-24 14:19:43 +00:00
|
|
|
let data_len = self.buffer.len() - resid;
|
2020-12-23 08:44:53 +00:00
|
|
|
|
|
|
|
Ok(&self.buffer[..data_len])
|
|
|
|
}
|
2021-01-16 14:15:54 +00:00
|
|
|
|
|
|
|
/// Run dataout command
|
|
|
|
///
|
|
|
|
/// Note: use alloc_page_aligned_buffer to alloc data transfer buffer
|
|
|
|
pub fn do_out_command(&mut self, cmd: &[u8], data: &[u8]) -> Result<(), Error> {
|
|
|
|
|
|
|
|
if !unsafe { sg_is_scsi_cdb(cmd.as_ptr(), cmd.len() as c_int) } {
|
|
|
|
bail!("no valid SCSI command");
|
|
|
|
}
|
|
|
|
|
|
|
|
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
|
|
|
|
if ((data.as_ptr() as usize) & (page_size -1)) != 0 {
|
|
|
|
bail!("wrong transfer buffer alignment");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut ptvp = self.create_boxed_scsi_pt_obj()?;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
set_scsi_pt_data_out(
|
|
|
|
&mut *ptvp,
|
|
|
|
data.as_ptr(),
|
|
|
|
data.len() as c_int,
|
|
|
|
);
|
|
|
|
|
|
|
|
set_scsi_pt_cdb(
|
|
|
|
&mut *ptvp,
|
|
|
|
cmd.as_ptr(),
|
|
|
|
cmd.len() as c_int,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-01-24 13:54:30 +00:00
|
|
|
let res = unsafe { do_scsi_pt(&mut *ptvp, self.file.as_raw_fd(), self.timeout, 0) };
|
2021-01-16 14:15:54 +00:00
|
|
|
if res < 0 {
|
|
|
|
let err = nix::Error::last();
|
|
|
|
bail!("do_scsi_pt failed - {}", err);
|
|
|
|
}
|
|
|
|
if res != 0 {
|
|
|
|
bail!("do_scsi_pt failed {}", res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: what about sense data?
|
2021-01-19 13:40:13 +00:00
|
|
|
let _sense_len = unsafe { get_scsi_pt_sense_len(&*ptvp) };
|
2021-01-16 14:15:54 +00:00
|
|
|
|
2021-01-19 13:40:13 +00:00
|
|
|
let status = unsafe { get_scsi_pt_status_response(&*ptvp) };
|
2021-01-16 14:15:54 +00:00
|
|
|
if status != 0 {
|
|
|
|
// toto: improve error reporting
|
|
|
|
bail!("unknown scsi error - status response {}", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-23 08:44:53 +00:00
|
|
|
}
|