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