commit 7b111fab774e18407da3bc68527e88394d7f2cba
parent a3a76d8eb99b94188f10842ba155bbb910119d03
Author: filipton <filipton12@gmail.com>
Date: Fri, 5 Jul 2024 10:43:28 +0200
feat: calculate crc32 during ota update (to check it later)
Diffstat:
5 files changed, 43 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
@@ -51,7 +51,7 @@ loop {
- [ ] Partitions macro (to generate PARTITIONS const at compile time)
- [ ] Other esp32's (like esp32c3, esp32s2, etc..)
- [ ] Rollbacks
-- [ ] Simple example
+- [x] Simple example
## Resources
- https://github.com/esp-rs/espflash (this led me to esp-idf-part)
diff --git a/simple-ota-server/Cargo.lock b/simple-ota-server/Cargo.lock
@@ -3,5 +3,23 @@
version = 3
[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "crc32fast"
+version = "1.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
name = "simple-ota-server"
version = "0.1.0"
+dependencies = [
+ "crc32fast",
+]
diff --git a/simple-ota-server/Cargo.toml b/simple-ota-server/Cargo.toml
@@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+crc32fast = "1.4.2"
diff --git a/simple-ota-server/src/main.rs b/simple-ota-server/src/main.rs
@@ -3,19 +3,26 @@ use std::{
net::TcpListener,
};
-const BINARY: &[u8] = include_bytes!("../firmware.bin");
fn main() {
- println!("BINARY_SIZE: {}", BINARY.len());
+ let binary = std::fs::read("./firmware.bin").unwrap();
+ let mut binary_bytes = Vec::with_capacity(binary.len() + 4);
+ binary_bytes.extend_from_slice(&[255, 255, 255, 255]); // to calc crc32 from binary, append
+ // 4 x 255's at the beggining
+ binary_bytes.extend_from_slice(&binary);
+ let binary_crc = crc32fast::hash(&binary_bytes);
+
+ println!("BINARY_SIZE: {}", binary.len());
+ println!("BINARY CRC32: {}", binary_crc);
let listener = TcpListener::bind("0.0.0.0:6969").unwrap();
for stream in listener.incoming() {
println!("Connection");
let mut stream = stream.unwrap();
- let buf = (BINARY.len() as u32).to_le_bytes();
+ let buf = (binary.len() as u32).to_le_bytes();
_ = stream.write_all(&buf);
- let chunks = BINARY.chunks(4096 * 2);
+ let chunks = binary.chunks(4096 * 2);
let mut buf = [0; 1];
for chunk in chunks {
println!("Writing: {}", chunk.len());
diff --git a/src/lib.rs b/src/lib.rs
@@ -24,6 +24,8 @@ where
S: ReadStorage + Storage,
{
flash: S,
+
+ last_crc: Option<u32>,
ota_offset: Option<u32>,
target_partition: Option<usize>,
flash_size: u32,
@@ -39,6 +41,8 @@ where
pub fn new(flash: S) -> Self {
Ota {
flash,
+
+ last_crc: None,
ota_offset: None,
target_partition: None,
ota_remaining: 0,
@@ -94,6 +98,12 @@ where
ota_offset
);
+ self.last_crc = if let Some(last_crc) = self.last_crc {
+ Some(crc32::calc_crc32(&chunk[..write_size], last_crc))
+ } else {
+ Some(crc32::calc_crc32(&chunk[..write_size], 0xFFFFFFFF))
+ };
+
*ota_offset += write_size as u32;
self.ota_remaining -= write_size as u32;
Ok(self.ota_remaining == 0)
@@ -105,6 +115,8 @@ where
self.set_target_ota_boot_partition(target_partition);
}
+ log::info!("Calculated crc hash: {:?}", self.last_crc);
+
Ok(())
}