Have a way to get the booted partition (#3979)

* Have a way to get the booted partition

* CHANGELOG.md

* Add inline docs
This commit is contained in:
Björn Quentin 2025-08-25 10:14:44 +02:00 committed by GitHub
parent 522ff0bb73
commit 50265b457c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 0 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- `FlashRegion::partition_size` (#3902)
- `PartitionTable::booted_partition`(#3979)
### Changed

View File

@ -301,6 +301,48 @@ impl<'a> PartitionTable<'a> {
}
Ok(None)
}
#[cfg(not(feature = "std"))]
/// Get the currently booted partition.
pub fn booted_partition(&self) -> Result<Option<PartitionEntry<'a>>, Error> {
// Read entry 0 from MMU to know which partition is mapped
//
// See <https://github.com/espressif/esp-idf/blob/758939caecb16e5542b3adfba0bc85025517db45/components/hal/mmu_hal.c#L124>
cfg_if::cfg_if! {
if #[cfg(feature = "esp32")] {
let paddr = unsafe {
((0x3FF10000 as *const u32).read_volatile() & 0xff) << 16
};
} else if #[cfg(feature = "esp32s2")] {
let paddr = unsafe {
(((0x61801000 + 128 * 4) as *const u32).read_volatile() & 0xff) << 16
};
} else if #[cfg(feature = "esp32s3")] {
// Revisit this once we support XiP from PSRAM for ESP32-S3
let paddr = unsafe {
((0x600C5000 as *const u32).read_volatile() & 0xff) << 16
};
} else if #[cfg(any(feature = "esp32c2", feature = "esp32c3"))] {
let paddr = unsafe {
((0x600c5000 as *const u32).read_volatile() & 0xff) << 16
};
} else if #[cfg(any(feature = "esp32c6", feature = "esp32h2"))] {
let paddr = unsafe {
((0x60002000 + 0x380) as *mut u32).write_volatile(0);
(((0x60002000 + 0x37c) as *const u32).read_volatile() & 0xff) << 16
};
}
}
for id in 0..self.len() {
let entry = self.get_partition(id)?;
if entry.offset() == paddr {
return Ok(Some(entry));
}
}
Ok(None)
}
}
/// A partition type including the sub-type.

View File

@ -66,6 +66,8 @@ fn main() -> ! {
println!("{:?}", pt.get_partition(i));
}
println!("Currently booted partition {:?}", pt.booted_partition());
// Find the OTA-data partition and show the currently active partition
let ota_part = pt
.find_partition(esp_bootloader_esp_idf::partitions::PartitionType::Data(