commit a86e103118cda115b86c3bcdc67e78014a97f874
parent 3192605c5915bde52998eec13718926edf033b36
Author: filipton <filipton12@gmail.com>
Date: Sun, 12 Jan 2025 16:07:08 +0100
feat: fully added rollback capabilities
Diffstat:
5 files changed, 25 insertions(+), 8 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -10,7 +10,7 @@ checksum = "a21dea9854beb860f3062d10228ce9b976da520a73474aed3171ec276bc0c032"
[[package]]
name = "esp-hal-ota"
-version = "0.2.0"
+version = "0.3.0"
dependencies = [
"embedded-storage",
"esp32",
diff --git a/Cargo.toml b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "esp-hal-ota"
-version = "0.2.0"
+version = "0.3.0"
edition = "2021"
license = "MIT"
description = "OTA library for esp-hal"
diff --git a/README.md b/README.md
@@ -43,7 +43,7 @@ loop {
let res = ota.ota_write_chunk(&buf[..n]);
if res == Ok(true) { // end of flash
- if ota.ota_flush(true).is_ok() { // true if you want to verify crc reading flash
+ if ota.ota_flush(true, true).is_ok() { // true if you want to verify crc reading flash, and true if you want rollbacks
esp_hal::reset::software_reset();
}
}
@@ -66,7 +66,7 @@ This will generate .bin file from build file for chip.
- [x] Simple example
- [x] Better errors
- [x] Other esp32's (like esp32c3, esp32s2, etc..)
-- [ ] Rollbacks
+- [x] Rollbacks
## Resources
- https://github.com/esp-rs/espflash (this led me to esp-idf-part)
diff --git a/examples/src/main.rs b/examples/src/main.rs
@@ -106,6 +106,12 @@ async fn main(spawner: Spawner) {
.expect("connection spawn");
spawner.spawn(net_task(stack)).expect("net task spawn");
+ // mark ota partition valid
+ {
+ let mut ota = Ota::new(FlashStorage::new()).expect("Cannot create ota");
+ ota.ota_mark_app_valid().unwrap();
+ }
+
loop {
log::info!("Wait for wifi!");
Timer::after(Duration::from_secs(1)).await;
@@ -161,7 +167,7 @@ async fn main(spawner: Spawner) {
match res {
Ok(true) => {
- let res = ota.ota_flush(false);
+ let res = ota.ota_flush(false, true);
if let Err(e) = res {
log::error!("Ota flush error: {e:?}");
break;
diff --git a/src/lib.rs b/src/lib.rs
@@ -118,7 +118,8 @@ where
}
/// verify - should it read flash and check crc
- pub fn ota_flush(&mut self, verify: bool) -> Result<()> {
+ /// rollback - if rollbacks enable (will set ota_state to ESP_OTA_IMG_NEW)
+ pub fn ota_flush(&mut self, verify: bool, rollback: bool) -> Result<()> {
if verify {
if !self.ota_verify()? {
#[cfg(feature = "log")]
@@ -144,7 +145,12 @@ where
return Err(OtaError::WrongCRC);
}
- self.set_target_ota_boot_partition(progress.target_partition);
+ let img_state = match rollback {
+ true => OtaImgState::EspOtaImgNew,
+ false => OtaImgState::EspOtaImgUndefined,
+ };
+
+ self.set_target_ota_boot_partition(progress.target_partition, img_state);
Ok(())
}
@@ -178,7 +184,7 @@ where
}
/// Sets ota boot target partition
- pub fn set_target_ota_boot_partition(&mut self, target: usize) {
+ pub fn set_target_ota_boot_partition(&mut self, target: usize, state: OtaImgState) {
let (slot1, slot2) = self.get_ota_boot_entries();
let (seq1, seq2) = (slot1.seq, slot2.seq);
@@ -195,10 +201,15 @@ where
let offset = self.pinfo.otadata_offset + (self.pinfo.otadata_size >> 1);
_ = flash.write(offset, &target_seq.to_le_bytes());
+ _ = flash.write(offset + 32 - 4 - 4, &(state as u32).to_le_bytes());
_ = flash.write(offset + 32 - 4, &target_crc.to_le_bytes());
} else {
_ = flash.write(self.pinfo.otadata_offset, &target_seq.to_le_bytes());
_ = flash.write(
+ self.pinfo.otadata_offset + 32 - 4 - 4,
+ &(state as u32).to_le_bytes(),
+ );
+ _ = flash.write(
self.pinfo.otadata_offset + 32 - 4,
&target_crc.to_le_bytes(),
);