Skip to content
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

Tolerate some non-utf8 data when detecting boot mode over serial #573

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a missed `flush` call that may be causing communication errors (#521)
- Fix "SHA-256 comparison failed: [...] attempting to boot anyway..." (#567)
- Windows: Update RST/DTR order to avoid issues.
- Tolerate non-utf8 data in boot detection (#573)

### Changed
- Created `FlashData`, `FlashDataBuilder` and `FlashSettings` structs to reduce number of input arguments in some functions (#512, #566)
Expand Down
19 changes: 11 additions & 8 deletions espflash/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use std::{
io::{BufWriter, Write},
iter::zip,
str::from_utf8,
thread::sleep,
time::Duration,
};
Expand Down Expand Up @@ -156,7 +155,7 @@ impl Connection {
return Ok(());
}
let mut download_mode: bool = false;
let mut boot_mode: &str = "";
let mut boot_mode = String::new();
let mut boot_log_detected = false;
let mut buff: Vec<u8>;
if self.before_operation != ResetBeforeOperation::NoReset {
Expand All @@ -174,18 +173,22 @@ impl Connection {
)));
}

let read_slice = from_utf8(&buff[..read_bytes as usize]).map_err(|err| {
debug!("Error: {}", err);
Error::InvalidSerialRead
})?;
let read_slice = String::from_utf8_lossy(&buff[..read_bytes as usize]).into_owned();
SergioGasquez marked this conversation as resolved.
Show resolved Hide resolved
if !read_slice.contains("boot") {
return Err(Error::InvalidSerialRead);
}

let pattern = Regex::new(r"boot:(0x[0-9a-fA-F]+)(.*waiting for download)?").unwrap();

// Search for the pattern in the read data
if let Some(data) = pattern.captures(read_slice) {
if let Some(data) = pattern.captures(&read_slice) {
boot_log_detected = true;
// Boot log detected
boot_mode = data.get(1).map(|m| m.as_str()).unwrap_or_default();
boot_mode = data
.get(1)
.map(|m| m.as_str())
.unwrap_or_default()
.to_string();
download_mode = data.get(2).is_some();

// Further processing or printing the results
Expand Down
Loading