From 81a1e13f3ba006eae9a925d3ea53884a9b9a4154 Mon Sep 17 00:00:00 2001 From: Steffen Eiden Date: Thu, 15 Feb 2024 14:49:31 +0100 Subject: [PATCH] rust/pv: Provide default for UvCmd::cmd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provides a default for the cmd function for an UvCmd. This is enabled by requiring an associated constant for the IOCTL nr of the command. Signed-off-by: Steffen Eiden Reviewed-by: Julian Ruess Signed-off-by: Jan Höppner --- rust/pv_core/src/uvdevice.rs | 7 ++++++- rust/pv_core/src/uvdevice/info.rs | 4 +--- rust/pv_core/src/uvdevice/secret.rs | 16 +++++----------- rust/pv_core/src/uvdevice/test.rs | 2 ++ 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/rust/pv_core/src/uvdevice.rs b/rust/pv_core/src/uvdevice.rs index af78ef36..95e87408 100644 --- a/rust/pv_core/src/uvdevice.rs +++ b/rust/pv_core/src/uvdevice.rs @@ -83,15 +83,20 @@ fn rc_fmt(rc: u16, rrc: u16, cmd: &mut C) -> &'static str { /// Ultravisor Command. pub trait UvCmd { + /// The UV IOCTL number of the UV call + const UV_IOCTL_NR: u8; /// Returns the uvdevice IOCTL command that his command uses. /// /// # Returns /// The IOCTL cmd for this UvCmd usually sth like `uv_ioctl!(CMD_NR)` - fn cmd(&self) -> u64; + fn cmd(&self) -> u64 { + uv_ioctl(Self::UV_IOCTL_NR) + } /// Converts UV return codes into human readable error messages /// /// no need to handle `0x0000, 0x0001, 0x0002, 0x0005, 0x0030, 0x0031, 0x0032, 0x0100` fn rc_fmt(&self, rc: u16, rrc: u16) -> Option<&'static str>; + /// Returns data used by this command if available. fn data(&mut self) -> Option<&mut [u8]> { None diff --git a/rust/pv_core/src/uvdevice/info.rs b/rust/pv_core/src/uvdevice/info.rs index 53175fe5..59636e6f 100644 --- a/rust/pv_core/src/uvdevice/info.rs +++ b/rust/pv_core/src/uvdevice/info.rs @@ -72,9 +72,7 @@ impl From for UvDeviceInfo { } impl UvCmd for uvio_uvdev_info { - fn cmd(&self) -> u64 { - uv_ioctl(UvDevice::INFO_NR) - } + const UV_IOCTL_NR: u8 = UvDevice::INFO_NR; fn data(&mut self) -> Option<&mut [u8]> { Some(self.as_bytes_mut()) diff --git a/rust/pv_core/src/uvdevice/secret.rs b/rust/pv_core/src/uvdevice/secret.rs index 9bfed0fd..9863c54c 100644 --- a/rust/pv_core/src/uvdevice/secret.rs +++ b/rust/pv_core/src/uvdevice/secret.rs @@ -42,14 +42,12 @@ impl Default for ListCmd { } impl UvCmd for ListCmd { + const UV_IOCTL_NR: u8 = UvDevice::LIST_SECRET_NR; + fn data(&mut self) -> Option<&mut [u8]> { Some(self.0.as_mut_slice()) } - fn cmd(&self) -> u64 { - uv_ioctl(UvDevice::LIST_SECRET_NR) - } - fn rc_fmt(&self, _rc: u16, _rrc: u16) -> Option<&'static str> { None } @@ -80,14 +78,12 @@ impl AddCmd { } impl UvCmd for AddCmd { + const UV_IOCTL_NR: u8 = UvDevice::ADD_SECRET_NR; + fn data(&mut self) -> Option<&mut [u8]> { Some(&mut self.0) } - fn cmd(&self) -> u64 { - uv_ioctl(UvDevice::ADD_SECRET_NR) - } - fn rc_fmt(&self, rc: u16, _rrc: u16) -> Option<&'static str> { match rc { 0x0101 => Some("not allowed to modify the secret store"), @@ -116,9 +112,7 @@ impl UvCmd for AddCmd { /// request to modify the secret store will fail. pub struct LockCmd; impl UvCmd for LockCmd { - fn cmd(&self) -> u64 { - uv_ioctl(UvDevice::LOCK_SECRET_NR) - } + const UV_IOCTL_NR: u8 = UvDevice::LOCK_SECRET_NR; fn rc_fmt(&self, rc: u16, _rrc: u16) -> Option<&'static str> { match rc { diff --git a/rust/pv_core/src/uvdevice/test.rs b/rust/pv_core/src/uvdevice/test.rs index 1c65656f..69e20406 100644 --- a/rust/pv_core/src/uvdevice/test.rs +++ b/rust/pv_core/src/uvdevice/test.rs @@ -107,6 +107,8 @@ impl ffi::uvio_ioctl_cb { const TEST_CMD: u64 = 17; struct TestCmd(Option>); impl UvCmd for TestCmd { + const UV_IOCTL_NR: u8 = 42; + fn cmd(&self) -> u64 { TEST_CMD }