From 95d5fef771b63184012897d480476fd6b4177583 Mon Sep 17 00:00:00 2001 From: Rajas Paranjpe <52586855+ChocolateLoverRaj@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:00:11 -0700 Subject: [PATCH] set fp seed command 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. --- crosec/src/commands/fp_set_seed.rs | 31 ++++++++++++++++++++++++++++++ crosec/src/commands/mod.rs | 2 ++ ectool/src/main.rs | 20 +++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 crosec/src/commands/fp_set_seed.rs diff --git a/crosec/src/commands/fp_set_seed.rs b/crosec/src/commands/fp_set_seed.rs new file mode 100644 index 0000000..884e374 --- /dev/null +++ b/crosec/src/commands/fp_set_seed.rs @@ -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, + ) +} diff --git a/crosec/src/commands/mod.rs b/crosec/src/commands/mod.rs index bf588ae..c77c42a 100644 --- a/crosec/src/commands/mod.rs +++ b/crosec/src/commands/mod.rs @@ -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; diff --git a/ectool/src/main.rs b/ectool/src/main.rs index f059d21..a5c0b90 100644 --- a/ectool/src/main.rs +++ b/ectool/src/main.rs @@ -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; @@ -92,6 +93,9 @@ enum Commands { }, FpInfo, FpStats, + FpSetSeed { + seed: String, + }, } #[derive(Subcommand)] @@ -280,6 +284,22 @@ fn main() -> Result<()> { let stats = fp_stats(fd)?; println!("{stats:#?}"); } + Commands::FpSetSeed { seed } => { + match 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(())