Skip to content

Commit 1cde4fc

Browse files
Tolerate some non-utf8 data when detecting boot mode over serial (#573)
* Tolerate some non-utf8 data when detecting boot mode over serial * feat: Verify that the read sequence contains boot-mode --------- Co-authored-by: Sergio Gasquez Arcos <[email protected]>
1 parent 2279d3a commit 1cde4fc

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3333
- Fixed a missed `flush` call that may be causing communication errors (#521)
3434
- Fix "SHA-256 comparison failed: [...] attempting to boot anyway..." (#567)
3535
- Windows: Update RST/DTR order to avoid issues.
36+
- Tolerate non-utf8 data in boot detection (#573)
3637

3738
### Changed
3839

espflash/src/connection/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use std::{
88
io::{BufWriter, Write},
99
iter::zip,
10-
str::from_utf8,
1110
thread::sleep,
1211
time::Duration,
1312
};
@@ -155,7 +154,7 @@ impl Connection {
155154
return Ok(());
156155
}
157156
let mut download_mode: bool = false;
158-
let mut boot_mode: &str = "";
157+
let mut boot_mode = String::new();
159158
let mut boot_log_detected = false;
160159
let mut buff: Vec<u8>;
161160
if self.before_operation != ResetBeforeOperation::NoReset {
@@ -173,18 +172,22 @@ impl Connection {
173172
)));
174173
}
175174

176-
let read_slice = from_utf8(&buff[..read_bytes as usize]).map_err(|err| {
177-
debug!("Error: {}", err);
178-
Error::InvalidSerialRead
179-
})?;
175+
let read_slice = String::from_utf8_lossy(&buff[..read_bytes as usize]).into_owned();
176+
if !read_slice.contains("boot") {
177+
return Err(Error::InvalidSerialRead);
178+
}
180179

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

183182
// Search for the pattern in the read data
184-
if let Some(data) = pattern.captures(read_slice) {
183+
if let Some(data) = pattern.captures(&read_slice) {
185184
boot_log_detected = true;
186185
// Boot log detected
187-
boot_mode = data.get(1).map(|m| m.as_str()).unwrap_or_default();
186+
boot_mode = data
187+
.get(1)
188+
.map(|m| m.as_str())
189+
.unwrap_or_default()
190+
.to_string();
188191
download_mode = data.get(2).is_some();
189192

190193
// Further processing or printing the results

0 commit comments

Comments
 (0)