Skip to content

chromium_ec: Fix reading EC console #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions framework_lib/src/chromium_ec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,24 +820,26 @@ impl CrosEc {
Ok(result.valid)
}

/// Requests recent console output from EC and constantly asks for more
/// Requests console output from EC and constantly asks for more
/// Prints the output and returns it when an error is encountered
pub fn console_read(&self) -> EcResult<String> {
let mut console = String::new();
pub fn console_read(&self) -> EcResult<()> {
EcRequestConsoleSnapshot {}.send_command(self)?;

let mut cmd = EcRequestConsoleRead {
subcmd: ConsoleReadSubCommand::ConsoleReadRecent as u8,
subcmd: ConsoleReadSubCommand::ConsoleReadNext as u8,
};

EcRequestConsoleSnapshot {}.send_command(self)?;
loop {
match cmd.send_command_vec(self) {
Ok(data) => {
// EC Buffer is empty. We can wait a bit and see if there's more
// Can't run it too quickly, otherwise the commands might fail
if data.is_empty() {
trace!("Empty EC response");
println!("---");
os_specific::sleep(1_000_000); // 1s
// EC Buffer is empty. That means we've read everything from the snapshot
// The windows crosecbus driver returns all NULL instead of empty response
if data.is_empty() || data.iter().all(|x| *x == 0) {
debug!("Empty EC response. Stopping console read");
// Don't read too fast, wait a second before reading more
os_specific::sleep(1_000_000);
EcRequestConsoleSnapshot {}.send_command(self)?;
cmd.subcmd = ConsoleReadSubCommand::ConsoleReadRecent as u8;
continue;
}

let utf8 = std::str::from_utf8(&data).unwrap();
Expand All @@ -846,35 +848,52 @@ impl CrosEc {
.replace(['\0'], "");

print!("{}", ascii);
console.push_str(ascii.as_str());
}
Err(err) => {
error!("Err: {:?}", err);
return Ok(console);
//return Err(err)
return Err(err);
}
};
cmd.subcmd = ConsoleReadSubCommand::ConsoleReadNext as u8;

// Need to explicitly handle CTRL-C termination on UEFI Shell
#[cfg(feature = "uefi")]
if shell_get_execution_break_flag() {
return Ok(console);
return Ok(());
}
}
}

/// Read all of EC console buffer and return it
pub fn console_read_one(&self) -> EcResult<String> {
EcRequestConsoleSnapshot {}.send_command(self)?;
let data = EcRequestConsoleRead {
subcmd: ConsoleReadSubCommand::ConsoleReadRecent as u8,

let mut console = String::new();
let cmd = EcRequestConsoleRead {
subcmd: ConsoleReadSubCommand::ConsoleReadNext as u8,
};
loop {
match cmd.send_command_vec(self) {
Ok(data) => {
// EC Buffer is empty. That means we've read everything
// The windows crosecbus driver returns all NULL instead of empty response
if data.is_empty() || data.iter().all(|x| *x == 0) {
debug!("Empty EC response. Stopping console read");
return Ok(console);
}

let utf8 = std::str::from_utf8(&data).unwrap();
let ascii = utf8
.replace(|c: char| !c.is_ascii(), "")
.replace(['\0'], "");

console.push_str(ascii.as_str());
}
Err(err) => {
error!("Err: {:?}", err);
return Err(err);
}
};
}
.send_command_vec(self)?;
let utf8 = std::str::from_utf8(&data).unwrap();
let ascii = utf8
.replace(|c: char| !c.is_ascii(), "")
.replace(['\0'], "");
Ok(ascii)
}

/// Check features supported by the firmware
Expand Down