From 41d0de0ad81e98fe9e9500bd71e6c0bfbef1b5b9 Mon Sep 17 00:00:00 2001 From: Evan Benn Date: Thu, 28 Jul 2022 15:38:36 +1000 Subject: flashrom_tester: Write a newline with the wp prompt Write a newline after the hardware write protect prompt. Automated tests read stdout and wait for this message, and split on newline, so write a newline. Also modify the function to not be recursive. Try to handle a closed input correctly - panicing in that case. Behaviour is now to wait for a newline instead of for 1 character. BUG=b:240512896 BRANCH=None TEST=tast run localhost:2222 firmware.FlashromTester TEST=flashrom_tester < /dev/null TEST=flashrom_tester; type some things, hold enter, then close stdin Change-Id: I07ec242ca0d41787030d5d27fc88d78ed884d746 Signed-off-by: Evan Benn Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/flashrom/+/3809595 Reviewed-by: Nikolai Artemiev Reviewed-on: https://review.coreboot.org/c/flashrom/+/66587 Tested-by: build bot (Jenkins) Reviewed-by: Angel Pons Reviewed-by: Anastasia Klimchuk Reviewed-by: Edward O'Callaghan --- util/flashrom_tester/src/utils.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'util/flashrom_tester') diff --git a/util/flashrom_tester/src/utils.rs b/util/flashrom_tester/src/utils.rs index 5a932a060..717e103ea 100644 --- a/util/flashrom_tester/src/utils.rs +++ b/util/flashrom_tester/src/utils.rs @@ -103,17 +103,17 @@ pub fn toggle_hw_wp(dis: bool) -> Result<(), String> { // The easist way to toggle the hardware write-protect is // to {dis}connect the battery (and/or open the WP screw). let s = if dis { "dis" } else { "" }; - info!("Prompt for hardware WP {}able", s); - eprintln!(" > {}connect the battery (and/or open the WP screw)", s); - pause(); - let wp = get_hardware_wp()?; - if wp && dis { - eprintln!("Hardware write protect is still ENABLED!"); - return toggle_hw_wp(dis); - } - if !wp && !dis { - eprintln!("Hardware write protect is still DISABLED!"); - return toggle_hw_wp(dis); + // Print a failure message, but not on the first try. + let mut fail_msg = None; + while dis == get_hardware_wp()? { + if let Some(msg) = fail_msg { + eprintln!("{msg}"); + } + fail_msg = Some(format!("Hardware write protect is still {}!", !dis)); + // The following message is read by the tast test. Do not modify. + info!("Prompt for hardware WP {}able", s); + eprintln!(" > {}connect the battery (and/or open the WP screw)", s); + pause(); } Ok(()) } @@ -126,12 +126,16 @@ pub fn ac_power_warning() { } fn pause() { - let mut stdout = std::io::stdout(); - // We want the cursor to stay at the end of the line, so we print without a newline - // and flush manually. - stdout.write(b"Press any key to continue...").unwrap(); - stdout.flush().unwrap(); - std::io::stdin().read(&mut [0]).unwrap(); + // The following message is read by the tast test. Do not modify. + println!("Press enter to continue..."); + // Rust stdout is always LineBuffered at time of writing. + // But this is not guaranteed, so flush anyway. + std::io::stdout().flush().unwrap(); + // This reads one line, there is no guarantee the line came + // after the above prompt. But it is good enough. + if std::io::stdin().read_line(&mut String::new()).unwrap() == 0 { + panic!("stdin closed"); + } } pub fn get_hardware_wp() -> std::result::Result { -- cgit v1.2.3