David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2017-2019 Linaro LTD |
| 2 | // Copyright (c) 2017-2018 JUUL Labs |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 6 | //! A flash simulator |
| 7 | //! |
| 8 | //! This module is capable of simulating the type of NOR flash commonly used in microcontrollers. |
| 9 | //! These generally can be written as individual bytes, but must be erased in larger units. |
| 10 | |
David Brown | 2cbc470 | 2017-07-06 14:18:58 -0600 | [diff] [blame] | 11 | mod pdump; |
| 12 | |
David Brown | afabfcf | 2019-01-02 11:30:27 -0700 | [diff] [blame] | 13 | use crate::pdump::HexDump; |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 14 | use failure::Fail; |
David Brown | ea25c41 | 2019-01-02 11:33:55 -0700 | [diff] [blame] | 15 | use log::info; |
| 16 | use rand::{ |
| 17 | self, |
| 18 | distributions::{IndependentSample, Range}, |
| 19 | }; |
| 20 | use std::{ |
| 21 | collections::HashMap, |
| 22 | fs::File, |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 23 | io::{self, Write}, |
David Brown | ea25c41 | 2019-01-02 11:33:55 -0700 | [diff] [blame] | 24 | iter::Enumerate, |
| 25 | path::Path, |
| 26 | slice, |
| 27 | }; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 28 | |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 29 | pub type Result<T> = std::result::Result<T, FlashError>; |
| 30 | |
| 31 | #[derive(Fail, Debug)] |
| 32 | pub enum FlashError { |
| 33 | #[fail(display = "Offset out of bounds: {}", _0)] |
| 34 | OutOfBounds(String), |
| 35 | #[fail(display = "Invalid write: {}", _0)] |
| 36 | Write(String), |
| 37 | #[fail(display = "Write failed by chance: {}", _0)] |
| 38 | SimulatedFail(String), |
| 39 | #[fail(display = "{}", _0)] |
| 40 | Io(#[cause] io::Error), |
| 41 | } |
| 42 | |
| 43 | impl From<io::Error> for FlashError { |
| 44 | fn from(error: io::Error) -> Self { |
| 45 | FlashError::Io(error) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 49 | // Transition from error-chain. |
| 50 | macro_rules! bail { |
| 51 | ($item:expr) => (return Err($item.into());) |
| 52 | } |
| 53 | |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 54 | pub struct FlashPtr { |
David Brown | ea25c41 | 2019-01-02 11:33:55 -0700 | [diff] [blame] | 55 | pub ptr: *mut dyn Flash, |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 56 | } |
| 57 | unsafe impl Send for FlashPtr {} |
| 58 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 59 | pub trait Flash { |
| 60 | fn erase(&mut self, offset: usize, len: usize) -> Result<()>; |
| 61 | fn write(&mut self, offset: usize, payload: &[u8]) -> Result<()>; |
| 62 | fn read(&self, offset: usize, data: &mut [u8]) -> Result<()>; |
| 63 | |
Fabio Utzig | f5c895e | 2017-11-23 19:57:17 -0200 | [diff] [blame] | 64 | fn add_bad_region(&mut self, offset: usize, len: usize, rate: f32) -> Result<()>; |
| 65 | fn reset_bad_regions(&mut self); |
| 66 | |
Fabio Utzig | fa137fc | 2017-11-23 20:01:02 -0200 | [diff] [blame] | 67 | fn set_verify_writes(&mut self, enable: bool); |
| 68 | |
David Brown | ea25c41 | 2019-01-02 11:33:55 -0700 | [diff] [blame] | 69 | fn sector_iter(&self) -> SectorIter<'_>; |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 70 | fn device_size(&self) -> usize; |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 71 | |
Fabio Utzig | 269d286 | 2018-10-24 17:45:38 -0300 | [diff] [blame] | 72 | fn align(&self) -> usize; |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 73 | fn erased_val(&self) -> u8; |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 74 | } |
| 75 | |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 76 | fn ebounds<T: AsRef<str>>(message: T) -> FlashError { |
| 77 | FlashError::OutOfBounds(message.as_ref().to_owned()) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Fabio Utzig | 65935d7 | 2017-07-17 15:34:36 -0300 | [diff] [blame] | 80 | #[allow(dead_code)] |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 81 | fn ewrite<T: AsRef<str>>(message: T) -> FlashError { |
| 82 | FlashError::Write(message.as_ref().to_owned()) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Fabio Utzig | f5c895e | 2017-11-23 19:57:17 -0200 | [diff] [blame] | 85 | #[allow(dead_code)] |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 86 | fn esimulatedwrite<T: AsRef<str>>(message: T) -> FlashError { |
| 87 | FlashError::SimulatedFail(message.as_ref().to_owned()) |
Fabio Utzig | f5c895e | 2017-11-23 19:57:17 -0200 | [diff] [blame] | 88 | } |
| 89 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 90 | /// An emulated flash device. It is represented as a block of bytes, and a list of the sector |
Sam Bristow | d0ca0ff | 2019-10-30 20:51:35 +1300 | [diff] [blame] | 91 | /// mappings. |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 92 | #[derive(Clone)] |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 93 | pub struct SimFlash { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 94 | data: Vec<u8>, |
Marti Bolivar | 51d36dd | 2017-05-17 17:39:46 -0400 | [diff] [blame] | 95 | write_safe: Vec<bool>, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 96 | sectors: Vec<usize>, |
Fabio Utzig | f5c895e | 2017-11-23 19:57:17 -0200 | [diff] [blame] | 97 | bad_region: Vec<(usize, usize, f32)>, |
David Brown | 562a7a0 | 2017-01-23 11:19:03 -0700 | [diff] [blame] | 98 | // Alignment required for writes. |
| 99 | align: usize, |
Fabio Utzig | fa137fc | 2017-11-23 20:01:02 -0200 | [diff] [blame] | 100 | verify_writes: bool, |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 101 | erased_val: u8, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 102 | } |
| 103 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 104 | impl SimFlash { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 105 | /// Given a sector size map, construct a flash device for that. |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 106 | pub fn new(sectors: Vec<usize>, align: usize, erased_val: u8) -> SimFlash { |
David Brown | 562a7a0 | 2017-01-23 11:19:03 -0700 | [diff] [blame] | 107 | // Verify that the alignment is a positive power of two. |
| 108 | assert!(align > 0); |
| 109 | assert!(align & (align - 1) == 0); |
| 110 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 111 | let total = sectors.iter().sum(); |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 112 | SimFlash { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 113 | data: vec![erased_val; total], |
Marti Bolivar | 51d36dd | 2017-05-17 17:39:46 -0400 | [diff] [blame] | 114 | write_safe: vec![true; total], |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 115 | sectors: sectors, |
Fabio Utzig | f5c895e | 2017-11-23 19:57:17 -0200 | [diff] [blame] | 116 | bad_region: Vec::new(), |
David Brown | 562a7a0 | 2017-01-23 11:19:03 -0700 | [diff] [blame] | 117 | align: align, |
Fabio Utzig | fa137fc | 2017-11-23 20:01:02 -0200 | [diff] [blame] | 118 | verify_writes: true, |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 119 | erased_val: erased_val, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 123 | #[allow(dead_code)] |
| 124 | pub fn dump(&self) { |
| 125 | self.data.dump(); |
| 126 | } |
| 127 | |
| 128 | /// Dump this image to the given file. |
| 129 | #[allow(dead_code)] |
| 130 | pub fn write_file<P: AsRef<Path>>(&self, path: P) -> Result<()> { |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 131 | let mut fd = File::create(path)?; |
| 132 | fd.write_all(&self.data)?; |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 133 | Ok(()) |
| 134 | } |
| 135 | |
| 136 | // Scan the sector map, and return the base and offset within a sector for this given byte. |
| 137 | // Returns None if the value is outside of the device. |
| 138 | fn get_sector(&self, offset: usize) -> Option<(usize, usize)> { |
| 139 | let mut offset = offset; |
| 140 | for (sector, &size) in self.sectors.iter().enumerate() { |
| 141 | if offset < size { |
| 142 | return Some((sector, offset)); |
| 143 | } |
| 144 | offset -= size; |
| 145 | } |
| 146 | return None; |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 151 | pub type SimMultiFlash = HashMap<u8, SimFlash>; |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 152 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 153 | impl Flash for SimFlash { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 154 | /// The flash drivers tend to erase beyond the bounds of the given range. Instead, we'll be |
| 155 | /// strict, and make sure that the passed arguments are exactly at a sector boundary, otherwise |
| 156 | /// return an error. |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 157 | fn erase(&mut self, offset: usize, len: usize) -> Result<()> { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 158 | let (_start, slen) = self.get_sector(offset).ok_or_else(|| ebounds("start"))?; |
| 159 | let (end, elen) = self.get_sector(offset + len - 1).ok_or_else(|| ebounds("end"))?; |
| 160 | |
| 161 | if slen != 0 { |
| 162 | bail!(ebounds("offset not at start of sector")); |
| 163 | } |
| 164 | if elen != self.sectors[end] - 1 { |
| 165 | bail!(ebounds("end not at start of sector")); |
| 166 | } |
| 167 | |
| 168 | for x in &mut self.data[offset .. offset + len] { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 169 | *x = self.erased_val; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Marti Bolivar | 51d36dd | 2017-05-17 17:39:46 -0400 | [diff] [blame] | 172 | for x in &mut self.write_safe[offset .. offset + len] { |
| 173 | *x = true; |
| 174 | } |
| 175 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 176 | Ok(()) |
| 177 | } |
| 178 | |
Marti Bolivar | 51d36dd | 2017-05-17 17:39:46 -0400 | [diff] [blame] | 179 | /// We restrict to only allowing writes of values that are: |
| 180 | /// |
| 181 | /// 1. being written to for the first time |
| 182 | /// 2. being written to after being erased |
| 183 | /// |
| 184 | /// This emulates a flash device which starts out erased, with the |
| 185 | /// added restriction that repeated writes to the same location |
| 186 | /// are disallowed, even if they would be safe to do. |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 187 | fn write(&mut self, offset: usize, payload: &[u8]) -> Result<()> { |
Fabio Utzig | f5c895e | 2017-11-23 19:57:17 -0200 | [diff] [blame] | 188 | for &(off, len, rate) in &self.bad_region { |
| 189 | if offset >= off && (offset + payload.len()) <= (off + len) { |
| 190 | let mut rng = rand::thread_rng(); |
| 191 | let between = Range::new(0., 1.); |
| 192 | if between.ind_sample(&mut rng) < rate { |
| 193 | bail!(esimulatedwrite( |
| 194 | format!("Ignoring write to {:#x}-{:#x}", off, off + len))); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 199 | if offset + payload.len() > self.data.len() { |
David Brown | f253fa8 | 2017-01-23 15:43:47 -0700 | [diff] [blame] | 200 | panic!("Write outside of device"); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 201 | } |
| 202 | |
David Brown | 562a7a0 | 2017-01-23 11:19:03 -0700 | [diff] [blame] | 203 | // Verify the alignment (which must be a power of two). |
| 204 | if offset & (self.align - 1) != 0 { |
David Brown | f253fa8 | 2017-01-23 15:43:47 -0700 | [diff] [blame] | 205 | panic!("Misaligned write address"); |
David Brown | 562a7a0 | 2017-01-23 11:19:03 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | if payload.len() & (self.align - 1) != 0 { |
David Brown | f253fa8 | 2017-01-23 15:43:47 -0700 | [diff] [blame] | 209 | panic!("Write length not multiple of alignment"); |
David Brown | 562a7a0 | 2017-01-23 11:19:03 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Marti Bolivar | 51d36dd | 2017-05-17 17:39:46 -0400 | [diff] [blame] | 212 | for (i, x) in &mut self.write_safe[offset .. offset + payload.len()].iter_mut().enumerate() { |
Fabio Utzig | fa137fc | 2017-11-23 20:01:02 -0200 | [diff] [blame] | 213 | if self.verify_writes && !(*x) { |
Fabio Utzig | 65935d7 | 2017-07-17 15:34:36 -0300 | [diff] [blame] | 214 | panic!("Write to unerased location at 0x{:x}", offset + i); |
Fabio Utzig | 19b2c1a | 2017-04-20 07:32:44 -0300 | [diff] [blame] | 215 | } |
Marti Bolivar | 51d36dd | 2017-05-17 17:39:46 -0400 | [diff] [blame] | 216 | *x = false; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 217 | } |
| 218 | |
David Brown | 59ae522 | 2017-12-06 11:45:15 -0700 | [diff] [blame] | 219 | let sub = &mut self.data[offset .. offset + payload.len()]; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 220 | sub.copy_from_slice(payload); |
| 221 | Ok(()) |
| 222 | } |
| 223 | |
| 224 | /// Read is simple. |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 225 | fn read(&self, offset: usize, data: &mut [u8]) -> Result<()> { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 226 | if offset + data.len() > self.data.len() { |
| 227 | bail!(ebounds("Read outside of device")); |
| 228 | } |
| 229 | |
| 230 | let sub = &self.data[offset .. offset + data.len()]; |
| 231 | data.copy_from_slice(sub); |
| 232 | Ok(()) |
| 233 | } |
| 234 | |
Fabio Utzig | f5c895e | 2017-11-23 19:57:17 -0200 | [diff] [blame] | 235 | /// Adds a new flash bad region. Writes to this area fail with a chance |
| 236 | /// given by `rate`. |
| 237 | fn add_bad_region(&mut self, offset: usize, len: usize, rate: f32) -> Result<()> { |
| 238 | if rate < 0.0 || rate > 1.0 { |
| 239 | bail!(ebounds("Invalid rate")); |
| 240 | } |
| 241 | |
| 242 | info!("Adding new bad region {:#x}-{:#x}", offset, offset + len); |
| 243 | self.bad_region.push((offset, len, rate)); |
| 244 | |
| 245 | Ok(()) |
| 246 | } |
| 247 | |
| 248 | fn reset_bad_regions(&mut self) { |
| 249 | self.bad_region.clear(); |
| 250 | } |
| 251 | |
Fabio Utzig | fa137fc | 2017-11-23 20:01:02 -0200 | [diff] [blame] | 252 | fn set_verify_writes(&mut self, enable: bool) { |
| 253 | self.verify_writes = enable; |
| 254 | } |
| 255 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 256 | /// An iterator over each sector in the device. |
David Brown | ea25c41 | 2019-01-02 11:33:55 -0700 | [diff] [blame] | 257 | fn sector_iter(&self) -> SectorIter<'_> { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 258 | SectorIter { |
| 259 | iter: self.sectors.iter().enumerate(), |
| 260 | base: 0, |
| 261 | } |
| 262 | } |
| 263 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 264 | fn device_size(&self) -> usize { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 265 | self.data.len() |
| 266 | } |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 267 | |
Fabio Utzig | 269d286 | 2018-10-24 17:45:38 -0300 | [diff] [blame] | 268 | fn align(&self) -> usize { |
| 269 | self.align |
| 270 | } |
| 271 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 272 | fn erased_val(&self) -> u8 { |
| 273 | self.erased_val |
| 274 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /// It is possible to iterate over the sectors in the device, each element returning this. |
David Brown | 3f687dc | 2017-11-06 13:41:18 -0700 | [diff] [blame] | 278 | #[derive(Debug, Clone)] |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 279 | pub struct Sector { |
| 280 | /// Which sector is this, starting from 0. |
| 281 | pub num: usize, |
| 282 | /// The offset, in bytes, of the start of this sector. |
| 283 | pub base: usize, |
| 284 | /// The length, in bytes, of this sector. |
| 285 | pub size: usize, |
| 286 | } |
| 287 | |
| 288 | pub struct SectorIter<'a> { |
| 289 | iter: Enumerate<slice::Iter<'a, usize>>, |
| 290 | base: usize, |
| 291 | } |
| 292 | |
| 293 | impl<'a> Iterator for SectorIter<'a> { |
| 294 | type Item = Sector; |
| 295 | |
| 296 | fn next(&mut self) -> Option<Sector> { |
| 297 | match self.iter.next() { |
| 298 | None => None, |
| 299 | Some((num, &size)) => { |
| 300 | let base = self.base; |
| 301 | self.base += size; |
| 302 | Some(Sector { |
| 303 | num: num, |
| 304 | base: base, |
| 305 | size: size, |
| 306 | }) |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | #[cfg(test)] |
| 313 | mod test { |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 314 | use super::{Flash, FlashError, SimFlash, Result, Sector}; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 315 | |
| 316 | #[test] |
| 317 | fn test_flash() { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 318 | for &erased_val in &[0, 0xff] { |
| 319 | // NXP-style, uniform sectors. |
| 320 | let mut f1 = SimFlash::new(vec![4096usize; 256], 1, erased_val); |
| 321 | test_device(&mut f1, erased_val); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 322 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 323 | // STM style, non-uniform sectors. |
| 324 | let mut f2 = SimFlash::new(vec![16 * 1024, 16 * 1024, 16 * 1024, 64 * 1024, |
| 325 | 128 * 1024, 128 * 1024, 128 * 1024], 1, erased_val); |
| 326 | test_device(&mut f2, erased_val); |
| 327 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 328 | } |
| 329 | |
David Brown | ea25c41 | 2019-01-02 11:33:55 -0700 | [diff] [blame] | 330 | fn test_device(flash: &mut dyn Flash, erased_val: u8) { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 331 | let sectors: Vec<Sector> = flash.sector_iter().collect(); |
| 332 | |
| 333 | flash.erase(0, sectors[0].size).unwrap(); |
| 334 | let flash_size = flash.device_size(); |
| 335 | flash.erase(0, flash_size).unwrap(); |
| 336 | assert!(flash.erase(0, sectors[0].size - 1).is_bounds()); |
| 337 | |
| 338 | // Verify that write and erase do something. |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 339 | flash.write(0, &[0x55]).unwrap(); |
| 340 | let mut buf = [0xAA; 4]; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 341 | flash.read(0, &mut buf).unwrap(); |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 342 | assert_eq!(buf, [0x55, erased_val, erased_val, erased_val]); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 343 | |
| 344 | flash.erase(0, sectors[0].size).unwrap(); |
| 345 | flash.read(0, &mut buf).unwrap(); |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 346 | assert_eq!(buf, [erased_val; 4]); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 347 | |
| 348 | // Program the first and last byte of each sector, verify that has been done, and then |
| 349 | // erase to verify the erase boundaries. |
| 350 | for sector in §ors { |
| 351 | let byte = [(sector.num & 127) as u8]; |
| 352 | flash.write(sector.base, &byte).unwrap(); |
| 353 | flash.write(sector.base + sector.size - 1, &byte).unwrap(); |
| 354 | } |
| 355 | |
| 356 | // Verify the above |
| 357 | let mut buf = Vec::new(); |
| 358 | for sector in §ors { |
| 359 | let byte = (sector.num & 127) as u8; |
| 360 | buf.resize(sector.size, 0); |
| 361 | flash.read(sector.base, &mut buf).unwrap(); |
| 362 | assert_eq!(buf.first(), Some(&byte)); |
| 363 | assert_eq!(buf.last(), Some(&byte)); |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 364 | assert!(buf[1..buf.len()-1].iter().all(|&x| x == erased_val)); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | // Helper checks for the result type. |
| 369 | trait EChecker { |
| 370 | fn is_bounds(&self) -> bool; |
| 371 | } |
| 372 | |
| 373 | impl<T> EChecker for Result<T> { |
| 374 | |
| 375 | fn is_bounds(&self) -> bool { |
| 376 | match *self { |
David Brown | 96eb0de | 2019-02-22 16:23:59 -0700 | [diff] [blame] | 377 | Err(FlashError::OutOfBounds(_)) => true, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 378 | _ => false, |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |