diff options
Diffstat (limited to 'util/flashrom_tester/src')
-rw-r--r-- | util/flashrom_tester/src/main.rs | 9 | ||||
-rw-r--r-- | util/flashrom_tester/src/tester.rs | 51 | ||||
-rw-r--r-- | util/flashrom_tester/src/tests.rs | 25 |
3 files changed, 46 insertions, 39 deletions
diff --git a/util/flashrom_tester/src/main.rs b/util/flashrom_tester/src/main.rs index 80484d20..69bb92db 100644 --- a/util/flashrom_tester/src/main.rs +++ b/util/flashrom_tester/src/main.rs @@ -39,7 +39,7 @@ extern crate log; mod logger; use clap::{App, Arg}; -use flashrom::FlashChip; +use flashrom::{FlashChip, Flashrom, FlashromCmd}; use flashrom_tester::{tester, tests}; use std::path::PathBuf; use std::sync::atomic::AtomicBool; @@ -123,6 +123,11 @@ fn main() { ) .expect("ccd_target_type should admit only known types"); + let cmd: Box<dyn Flashrom> = Box::new(FlashromCmd { + path: flashrom_path.to_string(), + fc: ccd_type, + }); + let print_layout = matches.is_present("print-layout"); let output_format = matches .value_of("output-format") @@ -132,7 +137,7 @@ fn main() { let test_names = matches.values_of("test_name"); if let Err(e) = tests::generic( - flashrom_path, + cmd.as_ref(), ccd_type, print_layout, output_format, diff --git a/util/flashrom_tester/src/tester.rs b/util/flashrom_tester/src/tester.rs index 3150a435..172f9958 100644 --- a/util/flashrom_tester/src/tester.rs +++ b/util/flashrom_tester/src/tester.rs @@ -36,7 +36,7 @@ use super::rand_util; use super::types; use super::utils::{self, LayoutSizes}; -use flashrom::{FlashChip, Flashrom, FlashromCmd}; +use flashrom::{FlashChip, Flashrom}; use serde_json::json; use std::mem::MaybeUninit; use std::sync::atomic::{AtomicBool, Ordering}; @@ -52,7 +52,7 @@ pub struct TestEnv<'a> { /// /// Where possible, prefer to use methods on the TestEnv rather than delegating /// to the raw flashrom functions. - pub cmd: &'a FlashromCmd, + pub cmd: &'a dyn Flashrom, layout: LayoutSizes, pub wp: WriteProtectState<'a, 'static>, @@ -65,20 +65,20 @@ pub struct TestEnv<'a> { } impl<'a> TestEnv<'a> { - pub fn create(chip_type: FlashChip, cmd: &'a FlashromCmd) -> Result<Self, String> { + pub fn create(chip_type: FlashChip, cmd: &'a dyn Flashrom) -> Result<Self, String> { let rom_sz = cmd.get_size()?; let out = TestEnv { chip_type: chip_type, cmd: cmd, layout: utils::get_layout_sizes(rom_sz)?, - wp: WriteProtectState::from_hardware(cmd)?, + wp: WriteProtectState::from_hardware(cmd, chip_type)?, original_flash_contents: "/tmp/flashrom_tester_golden.bin".into(), random_data: "/tmp/random_content.bin".into(), }; info!("Stashing golden image for verification/recovery on completion"); - flashrom::read(&out.cmd, &out.original_flash_contents)?; - flashrom::verify(&out.cmd, &out.original_flash_contents)?; + out.cmd.read(&out.original_flash_contents)?; + out.cmd.verify(&out.original_flash_contents)?; info!("Generating random flash-sized data"); rand_util::gen_rand_testdata(&out.random_data, rom_sz as usize) @@ -123,19 +123,19 @@ impl<'a> TestEnv<'a> { /// Return true if the current Flash contents are the same as the golden image /// that was present at the start of testing. pub fn is_golden(&self) -> bool { - flashrom::verify(&self.cmd, &self.original_flash_contents).is_ok() + self.cmd.verify(&self.original_flash_contents).is_ok() } /// Do whatever is necessary to make the current Flash contents the same as they /// were at the start of testing. pub fn ensure_golden(&mut self) -> Result<(), String> { self.wp.set_hw(false)?.set_sw(false)?; - flashrom::write(&self.cmd, &self.original_flash_contents) + self.cmd.write(&self.original_flash_contents) } /// Attempt to erase the flash. pub fn erase(&self) -> Result<(), String> { - flashrom::erase(self.cmd) + self.cmd.erase() } /// Verify that the current Flash contents are the same as the file at the given @@ -143,11 +143,11 @@ impl<'a> TestEnv<'a> { /// /// Returns Err if they are not the same. pub fn verify(&self, contents_path: &str) -> Result<(), String> { - flashrom::verify(self.cmd, contents_path) + self.cmd.verify(contents_path) } } -impl Drop for TestEnv<'_> { +impl<'a> Drop for TestEnv<'a> { fn drop(&mut self) { info!("Verifying flash remains unmodified"); if !self.is_golden() { @@ -177,7 +177,8 @@ pub struct WriteProtectState<'a, 'p> { initial: InitialState<'p>, // Tuples are (hardware, software) current: (bool, bool), - cmd: &'a FlashromCmd, + cmd: &'a dyn Flashrom, + fc: FlashChip, } enum InitialState<'p> { @@ -199,7 +200,7 @@ impl<'a> WriteProtectState<'a, 'static> { /// /// Panics if there is already a live state derived from hardware. In such a situation the /// new state must be derived from the live one, or the live one must be dropped first. - pub fn from_hardware(cmd: &'a FlashromCmd) -> Result<Self, String> { + pub fn from_hardware(cmd: &'a dyn Flashrom, fc: FlashChip) -> Result<Self, String> { let mut lock = Self::get_liveness_lock() .lock() .expect("Somebody panicked during WriteProtectState init from hardware"); @@ -217,12 +218,13 @@ impl<'a> WriteProtectState<'a, 'static> { initial: InitialState::Hardware(hw, sw), current: (hw, sw), cmd, + fc, }) } /// Get the actual hardware write protect state. - fn get_hw(cmd: &FlashromCmd) -> Result<bool, String> { - if cmd.fc.can_control_hw_wp() { + fn get_hw(cmd: &dyn Flashrom) -> Result<bool, String> { + if cmd.can_control_hw_wp() { super::utils::get_hardware_wp() } else { Ok(false) @@ -230,8 +232,8 @@ impl<'a> WriteProtectState<'a, 'static> { } /// Get the actual software write protect state. - fn get_sw(cmd: &FlashromCmd) -> Result<bool, String> { - flashrom::wp_status(cmd, true) + fn get_sw(cmd: &dyn Flashrom) -> Result<bool, String> { + cmd.wp_status(true) } } @@ -241,14 +243,14 @@ impl<'a, 'p> WriteProtectState<'a, 'p> { /// /// If false, calls to set_hw() will do nothing. pub fn can_control_hw_wp(&self) -> bool { - self.cmd.fc.can_control_hw_wp() + self.cmd.can_control_hw_wp() } /// Set the software write protect. pub fn set_sw(&mut self, enable: bool) -> Result<&mut Self, String> { info!("request={}, current={}", enable, self.current.1); if self.current.1 != enable { - flashrom::wp_toggle(self.cmd, /* en= */ enable)?; + self.cmd.wp_toggle(/* en= */ enable)?; self.current.1 = enable; } Ok(self) @@ -263,7 +265,7 @@ impl<'a, 'p> WriteProtectState<'a, 'p> { } else if enable { info!( "Ignoring attempt to enable hardware WP with {:?} programmer", - self.cmd.fc + self.fc ); } } @@ -277,7 +279,7 @@ impl<'a, 'p> WriteProtectState<'a, 'p> { /// ```no_run /// # fn main() -> Result<(), String> { /// # let cmd: flashrom::FlashromCmd = unimplemented!(); - /// let wp = flashrom_tester::tester::WriteProtectState::from_hardware(&cmd)?; + /// let wp = flashrom_tester::tester::WriteProtectState::from_hardware(&cmd, flashrom::FlashChip::SERVO)?; /// { /// let mut wp = wp.push(); /// wp.set_sw(false)?; @@ -297,6 +299,7 @@ impl<'a, 'p> WriteProtectState<'a, 'p> { initial: InitialState::Previous(self), current: self.current, cmd: self.cmd, + fc: self.fc, } } @@ -376,7 +379,7 @@ impl<'a, 'p> WriteProtectState<'a, 'p> { ) })?; } - flashrom::wp_toggle(self.cmd, /* en= */ sw).map_err(|e| { + self.cmd.wp_toggle(/* en= */ sw).map_err(|e| { format!( "Failed to {}able software write protect: {}", enable_str(sw), @@ -386,7 +389,7 @@ impl<'a, 'p> WriteProtectState<'a, 'p> { } assert!( - self.cmd.fc.can_control_hw_wp() || (!self.current.0 && !hw), + self.cmd.can_control_hw_wp() || (!self.current.0 && !hw), "HW WP must be disabled if it cannot be controlled" ); if hw != self.current.0 { @@ -479,7 +482,7 @@ fn decode_test_result(res: TestResult, con: TestConclusion) -> (TestConclusion, pub fn run_all_tests<T, TS>( chip: FlashChip, - cmd: &FlashromCmd, + cmd: &dyn Flashrom, ts: TS, terminate_flag: Option<&AtomicBool>, ) -> Vec<(String, (TestConclusion, Option<TestError>))> diff --git a/util/flashrom_tester/src/tests.rs b/util/flashrom_tester/src/tests.rs index 86c0da5b..208e98cb 100644 --- a/util/flashrom_tester/src/tests.rs +++ b/util/flashrom_tester/src/tests.rs @@ -36,7 +36,7 @@ use super::cros_sysinfo; use super::tester::{self, OutputFormat, TestCase, TestEnv, TestResult}; use super::utils::{self, LayoutNames}; -use flashrom::{FlashChip, Flashrom, FlashromCmd}; +use flashrom::{FlashChip, Flashrom}; use std::collections::{HashMap, HashSet}; use std::fs::File; use std::io::{BufRead, Write}; @@ -78,16 +78,13 @@ fn filter_tests<'n, 't: 'n, T: TestCase>( /// tests are run. Provided names that don't match any known test will be logged /// as a warning. pub fn generic<'a, TN: Iterator<Item = &'a str>>( - path: &str, + cmd: &dyn Flashrom, fc: FlashChip, print_layout: bool, output_format: OutputFormat, test_names: Option<TN>, terminate_flag: Option<&AtomicBool>, ) -> Result<(), Box<dyn std::error::Error>> { - let p = path.to_string(); - let cmd = FlashromCmd { path: p, fc }; - utils::ac_power_warning(); info!("Calculate ROM partition sizes & Create the layout file."); @@ -142,18 +139,20 @@ pub fn generic<'a, TN: Iterator<Item = &'a str>>( }; let tests = filter_tests(tests, &mut filter_names); + let chip_name = cmd + .name() + .map(|x| format!("vendor=\"{}\" name=\"{}\"", x.0, x.1)) + .unwrap_or("<Unknown chip>".into()); + // ------------------------. // Run all the tests and collate the findings: - let results = tester::run_all_tests(fc, &cmd, tests, terminate_flag); + let results = tester::run_all_tests(fc, cmd, tests, terminate_flag); // Any leftover filtered names were specified to be run but don't exist for leftover in filter_names.iter().flatten() { warn!("No test matches filter name \"{}\"", leftover); } - let chip_name = flashrom::name(&cmd) - .map(|x| format!("vendor=\"{}\" name=\"{}\"", x.0, x.1)) - .unwrap_or("<Unknown chip>".into()); let os_rel = sys_info::os_release().unwrap_or("<Unknown OS>".to_string()); let system_info = cros_sysinfo::system_info().unwrap_or("<Unknown System>".to_string()); let bios_info = cros_sysinfo::bios_info().unwrap_or("<Unknown BIOS>".to_string()); @@ -170,7 +169,7 @@ pub fn generic<'a, TN: Iterator<Item = &'a str>>( fn get_device_name_test(env: &mut TestEnv) -> TestResult { // Success means we got something back, which is good enough. - flashrom::name(env.cmd)?; + env.cmd.name()?; Ok(()) } @@ -178,7 +177,7 @@ fn wp_toggle_test(env: &mut TestEnv) -> TestResult { // NOTE: This is not strictly a 'test' as it is allowed to fail on some platforms. // However, we will warn when it does fail. // List the write-protected regions of flash. - match flashrom::wp_list(env.cmd) { + match env.cmd.wp_list() { Ok(list_str) => info!("\n{}", list_str), Err(e) => warn!("{}", e), }; @@ -289,7 +288,7 @@ fn partial_lock_test(section: LayoutNames) -> impl Fn(&mut TestEnv) -> TestResul // Disable software WP so we can do range protection, but hardware WP // must remain enabled for (most) range protection to do anything. env.wp.set_hw(false)?.set_sw(false)?; - flashrom::wp_range(env.cmd, (start, len), true)?; + env.cmd.wp_range((start, len), true)?; env.wp.set_hw(true)?; let rws = flashrom::ROMWriteSpecifics { @@ -297,7 +296,7 @@ fn partial_lock_test(section: LayoutNames) -> impl Fn(&mut TestEnv) -> TestResul write_file: Some(env.random_data_file()), name_file: Some(name), }; - if flashrom::write_file_with_layout(env.cmd, &rws).is_ok() { + if env.cmd.write_file_with_layout(&rws).is_ok() { return Err( "Section should be locked, should not have been overwritable with random data" .into(), |