Skip to content

Commit

Permalink
set fp seed command
Browse files Browse the repository at this point in the history
This command can only be run once per reboot (I'm pretty sure) so it can be hard to debug. Luckily it worked 1st try for me.
  • Loading branch information
ChocolateLoverRaj committed Jun 5, 2024
1 parent 2808fdb commit 1d1d30a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
31 changes: 31 additions & 0 deletions crosec/src/commands/fp_set_seed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::ffi::c_int;

use bytemuck::{Pod, Zeroable};

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

use super::CrosEcCmd;

pub const FP_CONTEXT_TPM_BYTES: usize = 32;
const FP_TEMPLATE_FORMAT_VERSION: u16 = 4;

#[repr(C, align(4))]
#[derive(Pod, Zeroable, Clone, Copy)]
struct EcParamsFpSeed {
pub struct_version: u16,
pub reserved: u16,
pub seed: [u8; FP_CONTEXT_TPM_BYTES],
}

pub fn fp_set_seed(fd: c_int, seed: [u8; FP_CONTEXT_TPM_BYTES]) -> EcCmdResult<()> {
ec_command_bytemuck(
CrosEcCmd::FpSetSeed,
0,
&EcParamsFpSeed {
struct_version: FP_TEMPLATE_FORMAT_VERSION,
reserved: Default::default(),
seed,
},
fd,
)
}
2 changes: 2 additions & 0 deletions crosec/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ pub enum CrosEcCmd {
ConsoleRead = 0x0098,
FpInfo = 0x0403,
FpStats = 0x0407,
FpSetSeed = 0x0408,
BatteryGetStatic = 0x0600,
}

pub mod board_version;
pub mod charge_control;
pub mod fp_info;
pub mod fp_set_seed;
pub mod fp_stats;
pub mod get_chip_info;
pub mod get_cmd_versions;
Expand Down
20 changes: 20 additions & 0 deletions ectool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::os::fd::AsRawFd;
use clap::{Parser, Subcommand, ValueEnum};
use color_eyre::eyre::Result;
use crosec::commands::fp_info::fp_info;
use crosec::commands::fp_set_seed::{fp_set_seed, FP_CONTEXT_TPM_BYTES};
use crosec::commands::fp_stats::fp_stats;
use crosec::commands::get_protocol_info::get_protocol_info;
use num_traits::cast::FromPrimitive;
Expand Down Expand Up @@ -92,6 +93,9 @@ enum Commands {
},
FpInfo,
FpStats,
FpSetSeed {
seed: String,
},
}

#[derive(Subcommand)]
Expand Down Expand Up @@ -280,6 +284,22 @@ fn main() -> Result<()> {
let stats = fp_stats(fd)?;
println!("{stats:#?}");
}
Commands::FpSetSeed { seed } => {
match <Vec<u8> as TryInto<[u8; FP_CONTEXT_TPM_BYTES]>>::try_into(
seed.as_bytes().to_owned(),
) {
Ok(seed) => {
let file = File::open(CROS_FP_PATH).unwrap();
let fd = file.as_raw_fd();
fp_set_seed(fd, seed)?;
println!("Set fp seed");
}
Err(seed) => {
let seed_len = seed.len();
println!("The seed must be {FP_CONTEXT_TPM_BYTES} bytes long. The seed you inputted is {seed_len} bytes long.");
}
}
}
}

Ok(())
Expand Down

0 comments on commit 1d1d30a

Please sign in to comment.