embassy-rs/embassy · error

Partition size must be a multiple of read, write and erase…

Error message

Partition size must be a multiple of read, write and erase size

What it means

The same `Partition::new` constructor also validates that the partition SIZE is a multiple of READ_SIZE, WRITE_SIZE and ERASE_SIZE. A size that isn't sector-aligned would leave a trailing partial sector the underlying flash cannot erase or program atomically, so the const constructor panics.

Solutions

  1. Round the size up to a multiple of T::ERASE_SIZE
  2. Express partition sizes in erase sectors: size = n * T::ERASE_SIZE
  3. Add a build-time assert comparing your layout constants with the flash's erase size
  4. Adjust adjacent partitions so the total still covers the flash exactly

Example fix

// before
let part = Partition::new(&flash, 0, 100 * 1024); // 100 KiB not multiple of 64 KiB erase
// after
let part = Partition::new(&flash, 0, 128 * 1024); // multiple of 64 KiB
Defensive patterns

Strategy: validation

Validate before calling

const fn size_aligned<T: NorFlash>(size: u32) -> bool {
    size % T::READ_SIZE as u32 == 0
        && size % T::WRITE_SIZE as u32 == 0
        && size % T::ERASE_SIZE as u32 == 0
}

Prevention

When it happens

Trigger: Calling `Partition::new(&mutex_flash, offset, size)` where `size % T::READ_SIZE != 0`, `size % T::WRITE_SIZE != 0`, or `size % T::ERASE_SIZE != 0` — e.g. a 100 KiB partition on a flash with 4 KiB erase (100 KiB is fine) vs a 100 KiB partition with 64 KiB erase sectors (not a multiple).

Common situations: Sizing partitions to exact image byte lengths instead of sector multiples; dividing a flash evenly leaving a non-aligned remainder; reusing layouts across chips with different sector sizes.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of embassy-rs/embassy@463a07b963 (2026-09-10). Data as JSON: /api/errors/240f5cc007124de7. Report an issue: GitHub.

Appendix: source

Thrown at embassy-embedded-hal/src/flash/partition/asynch.rs:39

impl<'a, M: RawMutex, T: NorFlash> Clone for Partition<'a, M, T> {
    fn clone(&self) -> Self {
        Self {
            flash: self.flash,
            offset: self.offset,
            size: self.size,
        }
    }
}

impl<'a, M: RawMutex, T: NorFlash> Partition<'a, M, T> {
    /// Create a new partition
    pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self {
        if offset % T::READ_SIZE as u32 != 0 || offset % T::WRITE_SIZE as u32 != 0 || offset % T::ERASE_SIZE as u32 != 0
        {
            panic!("Partition offset must be a multiple of read, write and erase size");
        }
        if size % T::READ_SIZE as u32 != 0 || size % T::WRITE_SIZE as u32 != 0 || size % T::ERASE_SIZE as u32 != 0 {
            panic!("Partition size must be a multiple of read, write and erase size");
        }
        Self { flash, offset, size }
    }

    /// Get the partition offset within the flash
    pub const fn offset(&self) -> u32 {
        self.offset
    }

    /// Get the partition size
    pub const fn size(&self) -> u32 {
        self.size
    }
}

impl<M: RawMutex, T: NorFlash> ErrorType for Partition<'_, M, T> {
    type Error = Error<T::Error>;
}

View on GitHub (pinned to 463a07b963)