Skip to content

Commit

Permalink
Use &mut File instead of raw fd for safe functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChocolateLoverRaj committed Jun 6, 2024
1 parent cb213aa commit 9625eb1
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 133 deletions.
33 changes: 17 additions & 16 deletions crosec/src/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
EC_MEM_MAP_BATTERY_SERIAL, EC_MEM_MAP_BATTERY_TYPE, EC_MEM_MAP_BATTERY_VERSION,
EC_MEM_MAP_BATTERY_VOLTAGE,
};
use std::ffi::c_int;
use std::fs::File;

#[derive(Debug, Clone)]
pub struct BatteryInfo {
Expand All @@ -28,31 +28,32 @@ pub struct BatteryInfo {
pub flags: u8,
}

pub fn battery(fd: c_int) -> EcCmdResult<BatteryInfo> {
if ec_cmd_get_cmd_versions(fd, CrosEcCmd::BatteryGetStatic)? & V1 != 0 {
pub fn battery(file: &mut File) -> EcCmdResult<BatteryInfo> {
if ec_cmd_get_cmd_versions(file, CrosEcCmd::BatteryGetStatic)? & V1 != 0 {
panic!(
"Battery info needs to be gotten with the {:?} command",
CrosEcCmd::BatteryGetStatic
);
} else {
let battery_version = read_mem_any::<i8>(fd, EC_MEM_MAP_BATTERY_VERSION).unwrap();
let battery_version = read_mem_any::<i8>(file, EC_MEM_MAP_BATTERY_VERSION).unwrap();
if battery_version < 1 {
panic!("Battery version {battery_version} is not supported");
}
let flags = read_mem_any::<u8>(fd, EC_MEM_MAP_BATTERY_FLAGS).unwrap();
let oem_name = read_mem_string(fd, EC_MEM_MAP_BATTERY_MANUFACTURER).unwrap();
let model_number = read_mem_string(fd, EC_MEM_MAP_BATTERY_MODEL).unwrap();
let chemistry = read_mem_string(fd, EC_MEM_MAP_BATTERY_TYPE).unwrap();
let serial_number = read_mem_string(fd, EC_MEM_MAP_BATTERY_SERIAL).unwrap();
let design_capacity = read_mem_any::<i32>(fd, EC_MEM_MAP_BATTERY_DESIGN_CAPACITY).unwrap();
let flags = read_mem_any::<u8>(file, EC_MEM_MAP_BATTERY_FLAGS).unwrap();
let oem_name = read_mem_string(file, EC_MEM_MAP_BATTERY_MANUFACTURER).unwrap();
let model_number = read_mem_string(file, EC_MEM_MAP_BATTERY_MODEL).unwrap();
let chemistry = read_mem_string(file, EC_MEM_MAP_BATTERY_TYPE).unwrap();
let serial_number = read_mem_string(file, EC_MEM_MAP_BATTERY_SERIAL).unwrap();
let design_capacity =
read_mem_any::<i32>(file, EC_MEM_MAP_BATTERY_DESIGN_CAPACITY).unwrap();
let last_full_charge =
read_mem_any::<i32>(fd, EC_MEM_MAP_BATTERY_LAST_FULL_CHARGE_CAPACITY).unwrap();
read_mem_any::<i32>(file, EC_MEM_MAP_BATTERY_LAST_FULL_CHARGE_CAPACITY).unwrap();
let design_output_voltage =
read_mem_any::<i32>(fd, EC_MEM_MAP_BATTERY_DESIGN_VOLTAGE).unwrap();
let cycle_count = read_mem_any::<i32>(fd, EC_MEM_MAP_BATTERY_CYCLE_COUNT).unwrap();
let present_voltage = read_mem_any::<i32>(fd, EC_MEM_MAP_BATTERY_VOLTAGE).unwrap();
let present_current = read_mem_any::<i32>(fd, EC_MEM_MAP_BATTERY_RATE).unwrap();
let remaining_capacity = read_mem_any::<i32>(fd, EC_MEM_MAP_BATTERY_CAPACITY).unwrap();
read_mem_any::<i32>(file, EC_MEM_MAP_BATTERY_DESIGN_VOLTAGE).unwrap();
let cycle_count = read_mem_any::<i32>(file, EC_MEM_MAP_BATTERY_CYCLE_COUNT).unwrap();
let present_voltage = read_mem_any::<i32>(file, EC_MEM_MAP_BATTERY_VOLTAGE).unwrap();
let present_current = read_mem_any::<i32>(file, EC_MEM_MAP_BATTERY_RATE).unwrap();
let remaining_capacity = read_mem_any::<i32>(file, EC_MEM_MAP_BATTERY_CAPACITY).unwrap();
Ok(BatteryInfo {
flags,
oem_name,
Expand Down
7 changes: 4 additions & 3 deletions crosec/src/commands/board_version.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::ffi::c_int;
use std::fs::File;
use std::os::fd::AsRawFd;

use crate::commands::CrosEcCmd;
use crate::ec_command::ec_command_bytemuck;
use crate::EcCmdResult;

pub fn ec_cmd_board_version(fd: c_int) -> EcCmdResult<u32> {
ec_command_bytemuck(CrosEcCmd::GetBoardVersion, 0, &(), fd)
pub fn ec_cmd_board_version(file: &mut File) -> EcCmdResult<u32> {
ec_command_bytemuck(CrosEcCmd::GetBoardVersion, 0, &(), file.as_raw_fd())
}
15 changes: 8 additions & 7 deletions crosec/src/commands/charge_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::commands::CrosEcCmd;
use crate::ec_command::ec_command_bytemuck;
use crate::EcCmdResult;
use bytemuck::{Pod, Zeroable};
use std::ffi::c_int;
use std::fs::File;
use std::os::fd::AsRawFd;

#[repr(C)]
#[derive(Pod, Zeroable, Copy, Clone, Default, Debug)]
Expand All @@ -19,8 +20,8 @@ pub enum ChargeControl {
Discharge,
}

pub fn supports_get_and_sustainer(fd: c_int) -> EcCmdResult<bool> {
let versions = ec_cmd_get_cmd_versions(fd, CrosEcCmd::ChargeControl)?;
pub fn supports_get_and_sustainer(file: &mut File) -> EcCmdResult<bool> {
let versions = ec_cmd_get_cmd_versions(file, CrosEcCmd::ChargeControl)?;
Ok(versions & V2 != 0)
}

Expand All @@ -40,15 +41,15 @@ const CHARGE_CONTROL_COMMAND_NORMAL: u32 = 0;
const CHARGE_CONTROL_COMMAND_IDLE: u32 = 1;
const CHARGE_CONTROL_COMMAND_DISCHARGE: u32 = 2;

pub fn get_charge_control(_fd: c_int) -> EcCmdResult<ChargeControl> {
pub fn get_charge_control(_file: &mut File) -> EcCmdResult<ChargeControl> {
panic!("Not implemented yet");
}

pub fn set_charge_control(fd: c_int, charge_control: ChargeControl) -> EcCmdResult<()> {
pub fn set_charge_control(file: &mut File, charge_control: ChargeControl) -> EcCmdResult<()> {
ec_command_bytemuck(
CrosEcCmd::ChargeControl,
{
let version = ec_cmd_get_cmd_versions(fd, CrosEcCmd::ChargeControl)?;
let version = ec_cmd_get_cmd_versions(file, CrosEcCmd::ChargeControl)?;
Ok(if version & V2 != 0 { 2 } else { 1 })
}?,
&EcParamsChargeControl {
Expand All @@ -67,7 +68,7 @@ pub fn set_charge_control(fd: c_int, charge_control: ChargeControl) -> EcCmdResu
_ => Default::default(),
},
},
fd,
file.as_raw_fd(),
)?;
Ok(())
}
7 changes: 4 additions & 3 deletions crosec/src/commands/fp_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::c_int;
use std::{fs::File, os::fd::AsRawFd};

use bytemuck::{Pod, Zeroable};

Expand Down Expand Up @@ -29,8 +29,9 @@ pub struct EcResponseFpInfo {
pub template_version: u32,
}

pub fn fp_info(fd: c_int) -> EcCmdResult<EcResponseFpInfo> {
let versions = ec_cmd_get_cmd_versions(fd, CrosEcCmd::FpInfo)?;
pub fn fp_info(file: &mut File) -> EcCmdResult<EcResponseFpInfo> {
let fd = file.as_raw_fd();
let versions = ec_cmd_get_cmd_versions(file, CrosEcCmd::FpInfo)?;
if versions & V1 == 0 {
panic!("fp doesn't support V1. Other versions are currently not implemented");
}
Expand Down
12 changes: 8 additions & 4 deletions crosec/src/commands/fp_mode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bytemuck::{Pod, Zeroable};
use std::ffi::c_int;
use std::{fs::File, os::fd::AsRawFd};
use strum::IntoEnumIterator;
use strum_macros::{EnumIter, EnumString, IntoStaticStr};

Expand Down Expand Up @@ -50,8 +50,12 @@ struct EcResponseFpMode {
mode: u32,
}

pub fn fp_mode(fd: c_int, mode: u32) -> EcCmdResult<u32> {
let response: EcResponseFpMode =
ec_command_bytemuck(CrosEcCmd::FpMode, 0, &EcParamsFpMode { mode }, fd)?;
pub fn fp_mode(file: &mut File, mode: u32) -> EcCmdResult<u32> {
let response: EcResponseFpMode = ec_command_bytemuck(
CrosEcCmd::FpMode,
0,
&EcParamsFpMode { mode },
file.as_raw_fd(),
)?;
Ok(response.mode)
}
6 changes: 3 additions & 3 deletions crosec/src/commands/fp_set_seed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::c_int;
use std::{fs::File, os::fd::AsRawFd};

use bytemuck::{Pod, Zeroable};

Expand All @@ -17,7 +17,7 @@ struct EcParamsFpSeed {
pub seed: [u8; FP_CONTEXT_TPM_BYTES],
}

pub fn fp_set_seed(fd: c_int, seed: [u8; FP_CONTEXT_TPM_BYTES]) -> EcCmdResult<()> {
pub fn fp_set_seed(file: &mut File, seed: [u8; FP_CONTEXT_TPM_BYTES]) -> EcCmdResult<()> {
ec_command_bytemuck(
CrosEcCmd::FpSetSeed,
0,
Expand All @@ -26,6 +26,6 @@ pub fn fp_set_seed(fd: c_int, seed: [u8; FP_CONTEXT_TPM_BYTES]) -> EcCmdResult<(
reserved: Default::default(),
seed,
},
fd,
file.as_raw_fd(),
)
}
6 changes: 3 additions & 3 deletions crosec/src/commands/fp_stats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::c_int;
use std::{fs::File, os::fd::AsRawFd};

use bytemuck::{Pod, Zeroable};

Expand All @@ -24,6 +24,6 @@ pub struct OverallT0 {
pub hi: u32,
}

pub fn fp_stats(fd: c_int) -> EcCmdResult<EcResponseFpStats> {
ec_command_bytemuck(CrosEcCmd::FpStats, 0, &(), fd)
pub fn fp_stats(file: &mut File) -> EcCmdResult<EcResponseFpStats> {
ec_command_bytemuck(CrosEcCmd::FpStats, 0, &(), file.as_raw_fd())
}
8 changes: 5 additions & 3 deletions crosec/src/commands/get_chip_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fs::File, os::fd::AsRawFd};

use bytemuck::{Pod, Zeroable};
use nix::libc::c_int;

use crate::{commands::CrosEcCmd, ec_command::ec_command_bytemuck, EcCmdResult};

Expand All @@ -11,8 +12,9 @@ struct EcResponseGetChipInfo {
revision: [u8; 32],
}

pub fn ec_cmd_get_chip_info(fd: c_int) -> EcCmdResult<(String, String, String)> {
let response: EcResponseGetChipInfo = ec_command_bytemuck(CrosEcCmd::GetChipInfo, 0, &(), fd)?;
pub fn ec_cmd_get_chip_info(file: &mut File) -> EcCmdResult<(String, String, String)> {
let response: EcResponseGetChipInfo =
ec_command_bytemuck(CrosEcCmd::GetChipInfo, 0, &(), file.as_raw_fd())?;

let vendor = String::from_utf8(response.vendor.to_vec()).unwrap_or_default();
let name = String::from_utf8(response.name.to_vec()).unwrap_or_default();
Expand Down
7 changes: 5 additions & 2 deletions crosec/src/commands/get_cmd_versions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fs::File;
use std::os::fd::AsRawFd;

use bytemuck::{Pod, Zeroable};
use nix::libc::c_int;

use crate::commands::CrosEcCmd;
use crate::ec_command::ec_command_bytemuck;
Expand Down Expand Up @@ -27,7 +29,8 @@ pub const V0: u32 = 0b001;
pub const V1: u32 = 0b010;
pub const V2: u32 = 0b100;

pub fn ec_cmd_get_cmd_versions(fd: c_int, cmd: CrosEcCmd) -> EcCmdResult<u32> {
pub fn ec_cmd_get_cmd_versions(file: &mut File, cmd: CrosEcCmd) -> EcCmdResult<u32> {
let fd = file.as_raw_fd();
let response: EcResponseGetCmdVersion = match ec_command_bytemuck(
CrosEcCmd::GetCmdVersions,
1,
Expand Down
7 changes: 4 additions & 3 deletions crosec/src/commands/get_features.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use nix::libc::c_int;
use std::fs::File;
use std::os::fd::AsRawFd;

use crate::commands::CrosEcCmd;
use crate::ec_command::ec_command_bytemuck;
use crate::EcCmdResult;

pub const EC_FEATURE_PWM_FAN: u64 = 0b100;

pub fn ec_cmd_get_features(fd: c_int) -> EcCmdResult<u64> {
ec_command_bytemuck(CrosEcCmd::GetFeatures, 0, &(), fd)
pub fn ec_cmd_get_features(file: &mut File) -> EcCmdResult<u64> {
ec_command_bytemuck(CrosEcCmd::GetFeatures, 0, &(), file.as_raw_fd())
}
6 changes: 3 additions & 3 deletions crosec/src/commands/get_protocol_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::c_int, mem::size_of};
use std::{fs::File, mem::size_of, os::fd::AsRawFd};

use bytemuck::{Pod, Zeroable};

Expand Down Expand Up @@ -42,6 +42,6 @@ struct EcHostResponse {
reserved: u16,
}

pub fn get_protocol_info(fd: c_int) -> EcCmdResult<EcResponseGetProtocolInfo> {
ec_command_bytemuck(CrosEcCmd::GetProtocolInfo, 0, &(), fd)
pub fn get_protocol_info(file: &mut File) -> EcCmdResult<EcResponseGetProtocolInfo> {
ec_command_bytemuck(CrosEcCmd::GetProtocolInfo, 0, &(), file.as_raw_fd())
}
8 changes: 5 additions & 3 deletions crosec/src/commands/hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fs::File;
use std::os::fd::AsRawFd;

use bytemuck::{NoUninit, Pod, Zeroable};
use nix::libc::c_int;

use crate::ec_command::ec_command_bytemuck;
use crate::{commands::CrosEcCmd, EcCmdResult};
Expand All @@ -19,14 +21,14 @@ struct EcResponseHello {
out_data: u32,
}

pub fn ec_cmd_hello(fd: c_int) -> EcCmdResult<bool> {
pub fn ec_cmd_hello(file: &mut File) -> EcCmdResult<bool> {
let response = ec_command_bytemuck::<_, EcResponseHello>(
CrosEcCmd::Hello,
0,
&EcParamsHello {
in_data: INPUT_DATA,
},
fd,
file.as_raw_fd(),
)?;
Ok(response.out_data == EXPECTED_OUTPUT)
}
6 changes: 3 additions & 3 deletions crosec/src/commands/read_mem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::CROS_EC_IOC_MAGIC;
use bytemuck::{Pod, Zeroable};
use nix::ioctl_readwrite;
use std::ffi::c_int;
use std::{ffi::c_int, fs::File, os::fd::AsRawFd};

const EC_MEM_MAP_SIZE: usize = 255;

Expand All @@ -21,13 +21,13 @@ struct EcResponseReadMemV2 {

ioctl_readwrite!(cros_ec_read_mem, CROS_EC_IOC_MAGIC, 1, EcResponseReadMemV2);

pub fn ec_cmd_read_mem(fd: c_int, offset: u32, bytes: u32) -> Result<Vec<u8>, c_int> {
pub fn ec_cmd_read_mem(file: &mut File, offset: u32, bytes: u32) -> Result<Vec<u8>, c_int> {
let mut response = EcResponseReadMemV2 {
offset,
bytes,
buffer: [0; EC_MEM_MAP_SIZE],
};
let status = unsafe { cros_ec_read_mem(fd, &mut response) }.unwrap();
let status = unsafe { cros_ec_read_mem(file.as_raw_fd(), &mut response) }.unwrap();
if status >= 0 {
Ok(response.buffer[..bytes as usize].to_vec())
} else {
Expand Down
13 changes: 9 additions & 4 deletions crosec/src/commands/set_fan_target_rpm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ffi::c_int;
use std::fs::File;
use std::mem::size_of;
use std::os::fd::AsRawFd;

use bytemuck::{Pod, Zeroable};

Expand Down Expand Up @@ -30,7 +31,11 @@ impl EcParamsSetFanTargetRpmV1 {
}
}

pub fn ec_cmd_set_fan_target_rpm(fd: c_int, rpm: u32, fan_index: Option<u8>) -> EcCmdResult<()> {
pub fn ec_cmd_set_fan_target_rpm(
file: &mut File,
rpm: u32,
fan_index: Option<u8>,
) -> EcCmdResult<()> {
// v0 can only set the RPM for all fans
// v1 can set the RPM for a specific fan
match fan_index {
Expand All @@ -43,15 +48,15 @@ pub fn ec_cmd_set_fan_target_rpm(fd: c_int, rpm: u32, fan_index: Option<u8>) ->
fan_index: index,
}
.to_le_bytes(),
fd,
file.as_raw_fd(),
)?;
}
None => {
ec_command_bytemuck(
CrosEcCmd::SetFanTargetRpm,
0,
&EcParamsSetFanTargetRpmV0 { rpm },
fd,
file.as_raw_fd(),
)?;
}
};
Expand Down
10 changes: 6 additions & 4 deletions crosec/src/commands/version.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fs::File, os::fd::AsRawFd};

use bytemuck::{Pod, Zeroable};
use nix::libc::c_int;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

Expand Down Expand Up @@ -33,7 +34,7 @@ enum EcImage {
}

pub fn ec_cmd_version(
fd: c_int,
file: &mut File,
protocol_info: &EcResponseGetProtocolInfo,
) -> EcCmdResult<(String, String, String, String, String)> {
let params = EcResponseVersionV1 {
Expand All @@ -44,7 +45,8 @@ pub fn ec_cmd_version(
cros_fwid_rw: [0; 32],
};

let response: EcResponseVersionV1 = ec_command_bytemuck(CrosEcCmd::Version, 0, &params, fd)?;
let response: EcResponseVersionV1 =
ec_command_bytemuck(CrosEcCmd::Version, 0, &params, file.as_raw_fd())?;

let ro_ver = String::from_utf8(response.version_string_ro.to_vec()).unwrap_or_default();
let rw_ver = String::from_utf8(response.version_string_rw.to_vec()).unwrap_or_default();
Expand All @@ -63,7 +65,7 @@ pub fn ec_cmd_version(
0,
&[0; 248],
protocol_info.max_ec_output_size(),
fd,
file.as_raw_fd(),
)?;

let build_info = String::from_utf8(result).unwrap_or(String::from(""));
Expand Down
Loading

0 comments on commit 9625eb1

Please sign in to comment.