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