David Brown | 4440af8 | 2017-01-09 12:15:05 -0700 | [diff] [blame] | 1 | #[macro_use] extern crate log; |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 2 | extern crate ring; |
David Brown | 4440af8 | 2017-01-09 12:15:05 -0700 | [diff] [blame] | 3 | extern crate env_logger; |
David Brown | 1e15859 | 2017-07-11 12:29:25 -0600 | [diff] [blame] | 4 | #[macro_use] extern crate bitflags; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 5 | extern crate docopt; |
| 6 | extern crate libc; |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame^] | 7 | extern crate pem; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 8 | extern crate rand; |
| 9 | extern crate rustc_serialize; |
David Brown | 2cbc470 | 2017-07-06 14:18:58 -0600 | [diff] [blame] | 10 | extern crate simflash; |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame^] | 11 | extern crate untrusted; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 12 | |
| 13 | use docopt::Docopt; |
David Brown | 4cb2623 | 2017-04-11 08:15:18 -0600 | [diff] [blame] | 14 | use rand::{Rng, SeedableRng, XorShiftRng}; |
Fabio Utzig | bb5635e | 2017-04-10 09:07:02 -0300 | [diff] [blame] | 15 | use rand::distributions::{IndependentSample, Range}; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 16 | use rustc_serialize::{Decodable, Decoder}; |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 17 | use std::fmt; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 18 | use std::mem; |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 19 | use std::process; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 20 | use std::slice; |
| 21 | |
| 22 | mod area; |
| 23 | mod c; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 24 | pub mod api; |
David Brown | 902d617 | 2017-05-05 09:37:41 -0600 | [diff] [blame] | 25 | mod caps; |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 26 | mod tlv; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 27 | |
David Brown | 2cbc470 | 2017-07-06 14:18:58 -0600 | [diff] [blame] | 28 | use simflash::{Flash, SimFlash}; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 29 | use area::{AreaDesc, FlashId}; |
David Brown | 902d617 | 2017-05-05 09:37:41 -0600 | [diff] [blame] | 30 | use caps::Caps; |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 31 | use tlv::TlvGen; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 32 | |
| 33 | const USAGE: &'static str = " |
| 34 | Mcuboot simulator |
| 35 | |
| 36 | Usage: |
| 37 | bootsim sizes |
| 38 | bootsim run --device TYPE [--align SIZE] |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 39 | bootsim runall |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 40 | bootsim (--help | --version) |
| 41 | |
| 42 | Options: |
| 43 | -h, --help Show this message |
| 44 | --version Version |
| 45 | --device TYPE MCU to simulate |
| 46 | Valid values: stm32f4, k64f |
| 47 | --align SIZE Flash write alignment |
| 48 | "; |
| 49 | |
| 50 | #[derive(Debug, RustcDecodable)] |
| 51 | struct Args { |
| 52 | flag_help: bool, |
| 53 | flag_version: bool, |
| 54 | flag_device: Option<DeviceName>, |
| 55 | flag_align: Option<AlignArg>, |
| 56 | cmd_sizes: bool, |
| 57 | cmd_run: bool, |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 58 | cmd_runall: bool, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 59 | } |
| 60 | |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 61 | #[derive(Copy, Clone, Debug, RustcDecodable)] |
David Brown | 07fb8fa | 2017-03-20 12:40:57 -0600 | [diff] [blame] | 62 | enum DeviceName { Stm32f4, K64f, K64fBig, Nrf52840 } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 63 | |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 64 | static ALL_DEVICES: &'static [DeviceName] = &[ |
| 65 | DeviceName::Stm32f4, |
| 66 | DeviceName::K64f, |
| 67 | DeviceName::K64fBig, |
| 68 | DeviceName::Nrf52840, |
| 69 | ]; |
| 70 | |
| 71 | impl fmt::Display for DeviceName { |
| 72 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 73 | let name = match *self { |
| 74 | DeviceName::Stm32f4 => "stm32f4", |
| 75 | DeviceName::K64f => "k64f", |
| 76 | DeviceName::K64fBig => "k64fbig", |
| 77 | DeviceName::Nrf52840 => "nrf52840", |
| 78 | }; |
| 79 | f.write_str(name) |
| 80 | } |
| 81 | } |
| 82 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 83 | #[derive(Debug)] |
| 84 | struct AlignArg(u8); |
| 85 | |
| 86 | impl Decodable for AlignArg { |
| 87 | // Decode the alignment ourselves, to restrict it to the valid possible alignments. |
| 88 | fn decode<D: Decoder>(d: &mut D) -> Result<AlignArg, D::Error> { |
| 89 | let m = d.read_u8()?; |
| 90 | match m { |
| 91 | 1 | 2 | 4 | 8 => Ok(AlignArg(m)), |
| 92 | _ => Err(d.error("Invalid alignment")), |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn main() { |
David Brown | 4440af8 | 2017-01-09 12:15:05 -0700 | [diff] [blame] | 98 | env_logger::init().unwrap(); |
| 99 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 100 | let args: Args = Docopt::new(USAGE) |
| 101 | .and_then(|d| d.decode()) |
| 102 | .unwrap_or_else(|e| e.exit()); |
| 103 | // println!("args: {:#?}", args); |
| 104 | |
| 105 | if args.cmd_sizes { |
| 106 | show_sizes(); |
| 107 | return; |
| 108 | } |
| 109 | |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 110 | let mut status = RunStatus::new(); |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 111 | if args.cmd_run { |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 112 | |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 113 | let align = args.flag_align.map(|x| x.0).unwrap_or(1); |
David Brown | 562a7a0 | 2017-01-23 11:19:03 -0700 | [diff] [blame] | 114 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 115 | |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 116 | let device = match args.flag_device { |
| 117 | None => panic!("Missing mandatory device argument"), |
| 118 | Some(dev) => dev, |
| 119 | }; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 120 | |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 121 | status.run_single(device, align); |
| 122 | } |
| 123 | |
| 124 | if args.cmd_runall { |
| 125 | for &dev in ALL_DEVICES { |
| 126 | for &align in &[1, 2, 4, 8] { |
| 127 | status.run_single(dev, align); |
| 128 | } |
| 129 | } |
| 130 | } |
David Brown | 5c6b679 | 2017-03-20 12:51:28 -0600 | [diff] [blame] | 131 | |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 132 | if status.failures > 0 { |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 133 | error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures); |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 134 | process::exit(1); |
| 135 | } else { |
| 136 | warn!("{} Tests ran successfully", status.passes); |
| 137 | process::exit(0); |
| 138 | } |
| 139 | } |
David Brown | 5c6b679 | 2017-03-20 12:51:28 -0600 | [diff] [blame] | 140 | |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 141 | struct RunStatus { |
| 142 | failures: usize, |
| 143 | passes: usize, |
| 144 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 145 | |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 146 | impl RunStatus { |
| 147 | fn new() -> RunStatus { |
| 148 | RunStatus { |
| 149 | failures: 0, |
| 150 | passes: 0, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 153 | |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 154 | fn run_single(&mut self, device: DeviceName, align: u8) { |
David Brown | a3b93cf | 2017-03-29 12:41:26 -0600 | [diff] [blame] | 155 | warn!("Running on device {} with alignment {}", device, align); |
| 156 | |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 157 | let (mut flash, areadesc) = match device { |
| 158 | DeviceName::Stm32f4 => { |
| 159 | // STM style flash. Large sectors, with a large scratch area. |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 160 | let flash = SimFlash::new(vec![16 * 1024, 16 * 1024, 16 * 1024, 16 * 1024, |
| 161 | 64 * 1024, |
| 162 | 128 * 1024, 128 * 1024, 128 * 1024], |
| 163 | align as usize); |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 164 | let mut areadesc = AreaDesc::new(&flash); |
| 165 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0); |
| 166 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1); |
| 167 | areadesc.add_image(0x060000, 0x020000, FlashId::ImageScratch); |
| 168 | (flash, areadesc) |
| 169 | } |
| 170 | DeviceName::K64f => { |
| 171 | // NXP style flash. Small sectors, one small sector for scratch. |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 172 | let flash = SimFlash::new(vec![4096; 128], align as usize); |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 173 | |
| 174 | let mut areadesc = AreaDesc::new(&flash); |
| 175 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0); |
| 176 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1); |
| 177 | areadesc.add_image(0x060000, 0x001000, FlashId::ImageScratch); |
| 178 | (flash, areadesc) |
| 179 | } |
| 180 | DeviceName::K64fBig => { |
| 181 | // Simulating an STM style flash on top of an NXP style flash. Underlying flash device |
| 182 | // uses small sectors, but we tell the bootloader they are large. |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 183 | let flash = SimFlash::new(vec![4096; 128], align as usize); |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 184 | |
| 185 | let mut areadesc = AreaDesc::new(&flash); |
| 186 | areadesc.add_simple_image(0x020000, 0x020000, FlashId::Image0); |
| 187 | areadesc.add_simple_image(0x040000, 0x020000, FlashId::Image1); |
| 188 | areadesc.add_simple_image(0x060000, 0x020000, FlashId::ImageScratch); |
| 189 | (flash, areadesc) |
| 190 | } |
| 191 | DeviceName::Nrf52840 => { |
| 192 | // Simulating the flash on the nrf52840 with partitions set up so that the scratch size |
| 193 | // does not divide into the image size. |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 194 | let flash = SimFlash::new(vec![4096; 128], align as usize); |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 195 | |
| 196 | let mut areadesc = AreaDesc::new(&flash); |
| 197 | areadesc.add_image(0x008000, 0x034000, FlashId::Image0); |
| 198 | areadesc.add_image(0x03c000, 0x034000, FlashId::Image1); |
| 199 | areadesc.add_image(0x070000, 0x00d000, FlashId::ImageScratch); |
| 200 | (flash, areadesc) |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | let (slot0_base, slot0_len) = areadesc.find(FlashId::Image0); |
| 205 | let (slot1_base, slot1_len) = areadesc.find(FlashId::Image1); |
| 206 | let (scratch_base, _) = areadesc.find(FlashId::ImageScratch); |
| 207 | |
| 208 | // Code below assumes that the slots are consecutive. |
| 209 | assert_eq!(slot1_base, slot0_base + slot0_len); |
| 210 | assert_eq!(scratch_base, slot1_base + slot1_len); |
| 211 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 212 | let offset_from_end = c::boot_magic_sz() + c::boot_max_align() * 2; |
| 213 | |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 214 | // println!("Areas: {:#?}", areadesc.get_c()); |
| 215 | |
| 216 | // Install the boot trailer signature, so that the code will start an upgrade. |
| 217 | // TODO: This must be a multiple of flash alignment, add support for an image that is smaller, |
| 218 | // and just gets padded. |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 219 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 220 | // Create original and upgrade images |
| 221 | let slot0 = SlotInfo { |
| 222 | base_off: slot0_base as usize, |
| 223 | trailer_off: slot1_base - offset_from_end, |
| 224 | }; |
| 225 | |
| 226 | let slot1 = SlotInfo { |
| 227 | base_off: slot1_base as usize, |
| 228 | trailer_off: scratch_base - offset_from_end, |
| 229 | }; |
| 230 | |
| 231 | let images = Images { |
| 232 | slot0: slot0, |
| 233 | slot1: slot1, |
| 234 | primary: install_image(&mut flash, slot0_base, 32784), |
| 235 | upgrade: install_image(&mut flash, slot1_base, 41928), |
| 236 | }; |
| 237 | |
| 238 | let mut failed = false; |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 239 | |
| 240 | // Set an alignment, and position the magic value. |
| 241 | c::set_sim_flash_align(align); |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 242 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 243 | mark_upgrade(&mut flash, &images.slot1); |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 244 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 245 | // upgrades without fails, counts number of flash operations |
| 246 | let total_count = match run_basic_upgrade(&flash, &areadesc, &images) { |
| 247 | Ok(v) => v, |
| 248 | Err(_) => { |
| 249 | self.failures += 1; |
| 250 | return; |
| 251 | }, |
David Brown | 902d617 | 2017-05-05 09:37:41 -0600 | [diff] [blame] | 252 | }; |
Fabio Utzig | bb5635e | 2017-04-10 09:07:02 -0300 | [diff] [blame] | 253 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 254 | failed |= run_basic_revert(&flash, &areadesc, &images); |
| 255 | failed |= run_revert_with_fails(&flash, &areadesc, &images, total_count); |
| 256 | failed |= run_perm_with_fails(&flash, &areadesc, &images, total_count); |
| 257 | failed |= run_perm_with_random_fails(&flash, &areadesc, &images, |
| 258 | total_count, 5); |
| 259 | failed |= run_norevert(&flash, &areadesc, &images); |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 260 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 261 | //show_flash(&flash); |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 262 | |
David Brown | 361be7a | 2017-03-29 12:28:47 -0600 | [diff] [blame] | 263 | if failed { |
| 264 | self.failures += 1; |
| 265 | } else { |
| 266 | self.passes += 1; |
| 267 | } |
David Brown | c638f79 | 2017-01-10 12:34:33 -0700 | [diff] [blame] | 268 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 271 | /// A simple upgrade without forced failures. |
| 272 | /// |
| 273 | /// Returns the number of flash operations which can later be used to |
| 274 | /// inject failures at chosen steps. |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 275 | fn run_basic_upgrade(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 276 | -> Result<i32, ()> { |
| 277 | let (fl, total_count) = try_upgrade(&flash, &areadesc, &images, None); |
| 278 | info!("Total flash operation count={}", total_count); |
| 279 | |
| 280 | if !verify_image(&fl, images.slot0.base_off, &images.upgrade) { |
| 281 | warn!("Image mismatch after first boot"); |
| 282 | Err(()) |
| 283 | } else { |
| 284 | Ok(total_count) |
| 285 | } |
| 286 | } |
| 287 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 288 | fn run_basic_revert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool { |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 289 | let mut fails = 0; |
| 290 | |
| 291 | if Caps::SwapUpgrade.present() { |
| 292 | for count in 2 .. 5 { |
| 293 | info!("Try revert: {}", count); |
| 294 | let fl = try_revert(&flash, &areadesc, count); |
| 295 | if !verify_image(&fl, images.slot0.base_off, &images.primary) { |
| 296 | warn!("Revert failure on count {}", count); |
| 297 | fails += 1; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | fails > 0 |
| 303 | } |
| 304 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 305 | fn run_perm_with_fails(flash: &SimFlash, areadesc: &AreaDesc, images: &Images, |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 306 | total_flash_ops: i32) -> bool { |
| 307 | let mut fails = 0; |
| 308 | |
| 309 | // Let's try an image halfway through. |
| 310 | for i in 1 .. total_flash_ops { |
| 311 | info!("Try interruption at {}", i); |
| 312 | let (fl, count) = try_upgrade(&flash, &areadesc, &images, Some(i)); |
| 313 | info!("Second boot, count={}", count); |
| 314 | if !verify_image(&fl, images.slot0.base_off, &images.upgrade) { |
| 315 | warn!("FAIL at step {} of {}", i, total_flash_ops); |
| 316 | fails += 1; |
| 317 | } |
| 318 | |
| 319 | if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK, |
| 320 | COPY_DONE) { |
| 321 | warn!("Mismatched trailer for Slot 0"); |
| 322 | fails += 1; |
| 323 | } |
| 324 | |
| 325 | if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET, |
| 326 | UNSET) { |
| 327 | warn!("Mismatched trailer for Slot 1"); |
| 328 | fails += 1; |
| 329 | } |
| 330 | |
| 331 | if Caps::SwapUpgrade.present() { |
| 332 | if !verify_image(&fl, images.slot1.base_off, &images.primary) { |
| 333 | warn!("Slot 1 FAIL at step {} of {}", i, total_flash_ops); |
| 334 | fails += 1; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | info!("{} out of {} failed {:.2}%", fails, total_flash_ops, |
| 340 | fails as f32 * 100.0 / total_flash_ops as f32); |
| 341 | |
| 342 | fails > 0 |
| 343 | } |
| 344 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 345 | fn run_perm_with_random_fails(flash: &SimFlash, areadesc: &AreaDesc, |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 346 | images: &Images, total_flash_ops: i32, |
| 347 | total_fails: usize) -> bool { |
| 348 | let mut fails = 0; |
| 349 | let (fl, total_counts) = try_random_fails(&flash, &areadesc, &images, |
| 350 | total_flash_ops, total_fails); |
| 351 | info!("Random interruptions at reset points={:?}", total_counts); |
| 352 | |
| 353 | let slot0_ok = verify_image(&fl, images.slot0.base_off, &images.upgrade); |
| 354 | let slot1_ok = if Caps::SwapUpgrade.present() { |
| 355 | verify_image(&fl, images.slot1.base_off, &images.primary) |
| 356 | } else { |
| 357 | true |
| 358 | }; |
| 359 | if !slot0_ok || !slot1_ok { |
| 360 | error!("Image mismatch after random interrupts: slot0={} slot1={}", |
| 361 | if slot0_ok { "ok" } else { "fail" }, |
| 362 | if slot1_ok { "ok" } else { "fail" }); |
| 363 | fails += 1; |
| 364 | } |
| 365 | if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK, |
| 366 | COPY_DONE) { |
| 367 | error!("Mismatched trailer for Slot 0"); |
| 368 | fails += 1; |
| 369 | } |
| 370 | if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET, |
| 371 | UNSET) { |
| 372 | error!("Mismatched trailer for Slot 1"); |
| 373 | fails += 1; |
| 374 | } |
| 375 | |
| 376 | fails > 0 |
| 377 | } |
| 378 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 379 | fn run_revert_with_fails(flash: &SimFlash, areadesc: &AreaDesc, images: &Images, |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 380 | total_count: i32) -> bool { |
| 381 | let mut fails = 0; |
| 382 | |
| 383 | if Caps::SwapUpgrade.present() { |
| 384 | for i in 1 .. (total_count - 1) { |
| 385 | info!("Try interruption at {}", i); |
| 386 | if try_revert_with_fail_at(&flash, &areadesc, &images, i) { |
| 387 | fails += 1; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | fails > 0 |
| 393 | } |
| 394 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 395 | fn run_norevert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool { |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 396 | let mut fl = flash.clone(); |
| 397 | let mut fails = 0; |
| 398 | |
| 399 | info!("Try norevert"); |
| 400 | c::set_flash_counter(0); |
| 401 | |
| 402 | // First do a normal upgrade... |
| 403 | if c::boot_go(&mut fl, &areadesc) != 0 { |
| 404 | warn!("Failed first boot"); |
| 405 | fails += 1; |
| 406 | } |
| 407 | |
| 408 | if !verify_image(&fl, images.slot0.base_off, &images.upgrade) { |
| 409 | warn!("Slot 0 image verification FAIL"); |
| 410 | fails += 1; |
| 411 | } |
| 412 | if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, UNSET, |
| 413 | COPY_DONE) { |
| 414 | warn!("Mismatched trailer for Slot 0"); |
| 415 | fails += 1; |
| 416 | } |
| 417 | if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET, |
| 418 | UNSET) { |
| 419 | warn!("Mismatched trailer for Slot 1"); |
| 420 | fails += 1; |
| 421 | } |
| 422 | |
| 423 | // Marks image in slot0 as permanent, no revert should happen... |
| 424 | mark_permanent_upgrade(&mut fl, &images.slot0); |
| 425 | |
| 426 | if c::boot_go(&mut fl, &areadesc) != 0 { |
| 427 | warn!("Failed second boot"); |
| 428 | fails += 1; |
| 429 | } |
| 430 | |
| 431 | if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK, |
| 432 | COPY_DONE) { |
| 433 | warn!("Mismatched trailer for Slot 0"); |
| 434 | fails += 1; |
| 435 | } |
| 436 | if !verify_image(&fl, images.slot0.base_off, &images.upgrade) { |
| 437 | warn!("Failed image verification"); |
| 438 | fails += 1; |
| 439 | } |
| 440 | |
| 441 | fails > 0 |
| 442 | } |
| 443 | |
| 444 | /// Test a boot, optionally stopping after 'n' flash options. Returns a count |
| 445 | /// of the number of flash operations done total. |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 446 | fn try_upgrade(flash: &SimFlash, areadesc: &AreaDesc, images: &Images, |
| 447 | stop: Option<i32>) -> (SimFlash, i32) { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 448 | // Clone the flash to have a new copy. |
| 449 | let mut fl = flash.clone(); |
| 450 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 451 | mark_permanent_upgrade(&mut fl, &images.slot1); |
Fabio Utzig | 5765231 | 2017-04-25 19:54:26 -0300 | [diff] [blame] | 452 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 453 | c::set_flash_counter(stop.unwrap_or(0)); |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 454 | let (first_interrupted, count) = match c::boot_go(&mut fl, &areadesc) { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 455 | -0x13579 => (true, stop.unwrap()), |
| 456 | 0 => (false, -c::get_flash_counter()), |
| 457 | x => panic!("Unknown return: {}", x), |
| 458 | }; |
| 459 | c::set_flash_counter(0); |
| 460 | |
| 461 | if first_interrupted { |
| 462 | // fl.dump(); |
| 463 | match c::boot_go(&mut fl, &areadesc) { |
| 464 | -0x13579 => panic!("Shouldn't stop again"), |
| 465 | 0 => (), |
| 466 | x => panic!("Unknown return: {}", x), |
| 467 | } |
| 468 | } |
| 469 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 470 | (fl, count - c::get_flash_counter()) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 471 | } |
| 472 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 473 | fn try_revert(flash: &SimFlash, areadesc: &AreaDesc, count: usize) -> SimFlash { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 474 | let mut fl = flash.clone(); |
| 475 | c::set_flash_counter(0); |
| 476 | |
David Brown | 163ab23 | 2017-01-23 15:48:35 -0700 | [diff] [blame] | 477 | // fl.write_file("image0.bin").unwrap(); |
| 478 | for i in 0 .. count { |
| 479 | info!("Running boot pass {}", i + 1); |
David Brown | c638f79 | 2017-01-10 12:34:33 -0700 | [diff] [blame] | 480 | assert_eq!(c::boot_go(&mut fl, &areadesc), 0); |
| 481 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 482 | fl |
| 483 | } |
| 484 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 485 | fn try_revert_with_fail_at(flash: &SimFlash, areadesc: &AreaDesc, images: &Images, |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 486 | stop: i32) -> bool { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 487 | let mut fl = flash.clone(); |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 488 | let mut x: i32; |
| 489 | let mut fails = 0; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 490 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 491 | c::set_flash_counter(stop); |
| 492 | x = c::boot_go(&mut fl, &areadesc); |
| 493 | if x != -0x13579 { |
| 494 | warn!("Should have stopped at interruption point"); |
| 495 | fails += 1; |
| 496 | } |
| 497 | |
| 498 | if !verify_trailer(&fl, images.slot0.trailer_off, None, None, UNSET) { |
| 499 | warn!("copy_done should be unset"); |
| 500 | fails += 1; |
| 501 | } |
| 502 | |
| 503 | c::set_flash_counter(0); |
| 504 | x = c::boot_go(&mut fl, &areadesc); |
| 505 | if x != 0 { |
| 506 | warn!("Should have finished upgrade"); |
| 507 | fails += 1; |
| 508 | } |
| 509 | |
| 510 | if !verify_image(&fl, images.slot0.base_off, &images.upgrade) { |
| 511 | warn!("Image in slot 0 before revert is invalid at stop={}", stop); |
| 512 | fails += 1; |
| 513 | } |
| 514 | if !verify_image(&fl, images.slot1.base_off, &images.primary) { |
| 515 | warn!("Image in slot 1 before revert is invalid at stop={}", stop); |
| 516 | fails += 1; |
| 517 | } |
| 518 | if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, UNSET, |
| 519 | COPY_DONE) { |
| 520 | warn!("Mismatched trailer for Slot 0 before revert"); |
| 521 | fails += 1; |
| 522 | } |
| 523 | if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET, |
| 524 | UNSET) { |
| 525 | warn!("Mismatched trailer for Slot 1 before revert"); |
| 526 | fails += 1; |
| 527 | } |
| 528 | |
| 529 | // Do Revert |
| 530 | c::set_flash_counter(0); |
| 531 | x = c::boot_go(&mut fl, &areadesc); |
| 532 | if x != 0 { |
| 533 | warn!("Should have finished a revert"); |
| 534 | fails += 1; |
| 535 | } |
| 536 | |
| 537 | if !verify_image(&fl, images.slot0.base_off, &images.primary) { |
| 538 | warn!("Image in slot 0 after revert is invalid at stop={}", stop); |
| 539 | fails += 1; |
| 540 | } |
| 541 | if !verify_image(&fl, images.slot1.base_off, &images.upgrade) { |
| 542 | warn!("Image in slot 1 after revert is invalid at stop={}", stop); |
| 543 | fails += 1; |
| 544 | } |
| 545 | if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK, |
| 546 | COPY_DONE) { |
| 547 | warn!("Mismatched trailer for Slot 1 after revert"); |
| 548 | fails += 1; |
| 549 | } |
| 550 | if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET, |
| 551 | UNSET) { |
| 552 | warn!("Mismatched trailer for Slot 1 after revert"); |
| 553 | fails += 1; |
| 554 | } |
| 555 | |
| 556 | fails > 0 |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 557 | } |
| 558 | |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 559 | fn try_random_fails(flash: &SimFlash, areadesc: &AreaDesc, images: &Images, |
| 560 | total_ops: i32, count: usize) -> (SimFlash, Vec<i32>) { |
Fabio Utzig | bb5635e | 2017-04-10 09:07:02 -0300 | [diff] [blame] | 561 | let mut fl = flash.clone(); |
| 562 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 563 | mark_permanent_upgrade(&mut fl, &images.slot1); |
Fabio Utzig | bb5635e | 2017-04-10 09:07:02 -0300 | [diff] [blame] | 564 | |
| 565 | let mut rng = rand::thread_rng(); |
Fabio Utzig | 5765231 | 2017-04-25 19:54:26 -0300 | [diff] [blame] | 566 | let mut resets = vec![0i32; count]; |
| 567 | let mut remaining_ops = total_ops; |
Fabio Utzig | bb5635e | 2017-04-10 09:07:02 -0300 | [diff] [blame] | 568 | for i in 0 .. count { |
Fabio Utzig | 5765231 | 2017-04-25 19:54:26 -0300 | [diff] [blame] | 569 | let ops = Range::new(1, remaining_ops / 2); |
Fabio Utzig | bb5635e | 2017-04-10 09:07:02 -0300 | [diff] [blame] | 570 | let reset_counter = ops.ind_sample(&mut rng); |
| 571 | c::set_flash_counter(reset_counter); |
| 572 | match c::boot_go(&mut fl, &areadesc) { |
| 573 | 0 | -0x13579 => (), |
| 574 | x => panic!("Unknown return: {}", x), |
| 575 | } |
Fabio Utzig | 5765231 | 2017-04-25 19:54:26 -0300 | [diff] [blame] | 576 | remaining_ops -= reset_counter; |
| 577 | resets[i] = reset_counter; |
Fabio Utzig | bb5635e | 2017-04-10 09:07:02 -0300 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | c::set_flash_counter(0); |
| 581 | match c::boot_go(&mut fl, &areadesc) { |
| 582 | -0x13579 => panic!("Should not be have been interrupted!"), |
| 583 | 0 => (), |
| 584 | x => panic!("Unknown return: {}", x), |
| 585 | } |
| 586 | |
Fabio Utzig | 5765231 | 2017-04-25 19:54:26 -0300 | [diff] [blame] | 587 | (fl, resets) |
Fabio Utzig | bb5635e | 2017-04-10 09:07:02 -0300 | [diff] [blame] | 588 | } |
| 589 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 590 | /// Show the flash layout. |
| 591 | #[allow(dead_code)] |
| 592 | fn show_flash(flash: &Flash) { |
| 593 | println!("---- Flash configuration ----"); |
| 594 | for sector in flash.sector_iter() { |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 595 | println!(" {:3}: 0x{:08x}, 0x{:08x}", |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 596 | sector.num, sector.base, sector.size); |
| 597 | } |
| 598 | println!(""); |
| 599 | } |
| 600 | |
| 601 | /// Install a "program" into the given image. This fakes the image header, or at least all of the |
| 602 | /// fields used by the given code. Returns a copy of the image that was written. |
| 603 | fn install_image(flash: &mut Flash, offset: usize, len: usize) -> Vec<u8> { |
| 604 | let offset0 = offset; |
| 605 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame^] | 606 | let mut tlv = TlvGen::new_rsa_pss(); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 607 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 608 | // Generate a boot header. Note that the size doesn't include the header. |
| 609 | let header = ImageHeader { |
| 610 | magic: 0x96f3b83c, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 611 | tlv_size: tlv.get_size(), |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 612 | _pad1: 0, |
| 613 | hdr_size: 32, |
| 614 | key_id: 0, |
| 615 | _pad2: 0, |
| 616 | img_size: len as u32, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 617 | flags: tlv.get_flags(), |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 618 | ver: ImageVersion { |
David Brown | e380fa6 | 2017-01-23 15:49:09 -0700 | [diff] [blame] | 619 | major: (offset / (128 * 1024)) as u8, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 620 | minor: 0, |
| 621 | revision: 1, |
David Brown | e380fa6 | 2017-01-23 15:49:09 -0700 | [diff] [blame] | 622 | build_num: offset as u32, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 623 | }, |
| 624 | _pad3: 0, |
| 625 | }; |
| 626 | |
| 627 | let b_header = header.as_raw(); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 628 | tlv.add_bytes(&b_header); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 629 | /* |
| 630 | let b_header = unsafe { slice::from_raw_parts(&header as *const _ as *const u8, |
| 631 | mem::size_of::<ImageHeader>()) }; |
| 632 | */ |
| 633 | assert_eq!(b_header.len(), 32); |
| 634 | flash.write(offset, &b_header).unwrap(); |
| 635 | let offset = offset + b_header.len(); |
| 636 | |
| 637 | // The core of the image itself is just pseudorandom data. |
| 638 | let mut buf = vec![0; len]; |
| 639 | splat(&mut buf, offset); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 640 | tlv.add_bytes(&buf); |
| 641 | |
| 642 | // Get and append the TLV itself. |
| 643 | buf.append(&mut tlv.make_tlv()); |
| 644 | |
| 645 | // Pad the block to a flash alignment (8 bytes). |
| 646 | while buf.len() % 8 != 0 { |
| 647 | buf.push(0xFF); |
| 648 | } |
| 649 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 650 | flash.write(offset, &buf).unwrap(); |
| 651 | let offset = offset + buf.len(); |
| 652 | |
| 653 | // Copy out the image so that we can verify that the image was installed correctly later. |
| 654 | let mut copy = vec![0u8; offset - offset0]; |
| 655 | flash.read(offset0, &mut copy).unwrap(); |
| 656 | |
| 657 | copy |
| 658 | } |
| 659 | |
| 660 | /// Verify that given image is present in the flash at the given offset. |
| 661 | fn verify_image(flash: &Flash, offset: usize, buf: &[u8]) -> bool { |
| 662 | let mut copy = vec![0u8; buf.len()]; |
| 663 | flash.read(offset, &mut copy).unwrap(); |
| 664 | |
| 665 | if buf != ©[..] { |
| 666 | for i in 0 .. buf.len() { |
| 667 | if buf[i] != copy[i] { |
David Brown | 4440af8 | 2017-01-09 12:15:05 -0700 | [diff] [blame] | 668 | info!("First failure at {:#x}", offset + i); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 669 | break; |
| 670 | } |
| 671 | } |
| 672 | false |
| 673 | } else { |
| 674 | true |
| 675 | } |
| 676 | } |
| 677 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 678 | fn verify_trailer(flash: &Flash, offset: usize, |
| 679 | magic: Option<&[u8]>, image_ok: Option<u8>, |
| 680 | copy_done: Option<u8>) -> bool { |
| 681 | let mut copy = vec![0u8; c::boot_magic_sz() + c::boot_max_align() * 2]; |
| 682 | let mut failed = false; |
| 683 | |
| 684 | flash.read(offset, &mut copy).unwrap(); |
| 685 | |
| 686 | failed |= match magic { |
| 687 | Some(v) => { |
| 688 | if ©[16..] != v { |
| 689 | warn!("\"magic\" mismatch at {:#x}", offset); |
| 690 | true |
| 691 | } else { |
| 692 | false |
| 693 | } |
| 694 | }, |
| 695 | None => false, |
| 696 | }; |
| 697 | |
| 698 | failed |= match image_ok { |
| 699 | Some(v) => { |
| 700 | if copy[8] != v { |
| 701 | warn!("\"image_ok\" mismatch at {:#x}", offset); |
| 702 | true |
| 703 | } else { |
| 704 | false |
| 705 | } |
| 706 | }, |
| 707 | None => false, |
| 708 | }; |
| 709 | |
| 710 | failed |= match copy_done { |
| 711 | Some(v) => { |
| 712 | if copy[0] != v { |
| 713 | warn!("\"copy_done\" mismatch at {:#x}", offset); |
| 714 | true |
| 715 | } else { |
| 716 | false |
| 717 | } |
| 718 | }, |
| 719 | None => false, |
| 720 | }; |
| 721 | |
| 722 | !failed |
| 723 | } |
| 724 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 725 | /// The image header |
| 726 | #[repr(C)] |
| 727 | pub struct ImageHeader { |
| 728 | magic: u32, |
| 729 | tlv_size: u16, |
| 730 | key_id: u8, |
| 731 | _pad1: u8, |
| 732 | hdr_size: u16, |
| 733 | _pad2: u16, |
| 734 | img_size: u32, |
| 735 | flags: u32, |
| 736 | ver: ImageVersion, |
| 737 | _pad3: u32, |
| 738 | } |
| 739 | |
| 740 | impl AsRaw for ImageHeader {} |
| 741 | |
| 742 | #[repr(C)] |
| 743 | pub struct ImageVersion { |
| 744 | major: u8, |
| 745 | minor: u8, |
| 746 | revision: u16, |
| 747 | build_num: u32, |
| 748 | } |
| 749 | |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 750 | struct SlotInfo { |
| 751 | base_off: usize, |
| 752 | trailer_off: usize, |
| 753 | } |
| 754 | |
| 755 | struct Images { |
| 756 | slot0: SlotInfo, |
| 757 | slot1: SlotInfo, |
| 758 | primary: Vec<u8>, |
| 759 | upgrade: Vec<u8>, |
| 760 | } |
| 761 | |
| 762 | const MAGIC_VALID: Option<&[u8]> = Some(&[0x77, 0xc2, 0x95, 0xf3, |
| 763 | 0x60, 0xd2, 0xef, 0x7f, |
| 764 | 0x35, 0x52, 0x50, 0x0f, |
| 765 | 0x2c, 0xb6, 0x79, 0x80]); |
| 766 | const MAGIC_UNSET: Option<&[u8]> = Some(&[0xff; 16]); |
| 767 | |
| 768 | const COPY_DONE: Option<u8> = Some(1); |
| 769 | const IMAGE_OK: Option<u8> = Some(1); |
| 770 | const UNSET: Option<u8> = Some(0xff); |
| 771 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 772 | /// Write out the magic so that the loader tries doing an upgrade. |
Fabio Utzig | ebeecef | 2017-07-06 10:36:42 -0300 | [diff] [blame] | 773 | fn mark_upgrade(flash: &mut Flash, slot: &SlotInfo) { |
| 774 | let offset = slot.trailer_off + c::boot_max_align() * 2; |
| 775 | flash.write(offset, MAGIC_VALID.unwrap()).unwrap(); |
| 776 | } |
| 777 | |
| 778 | /// Writes the image_ok flag which, guess what, tells the bootloader |
| 779 | /// the this image is ok (not a test, and no revert is to be performed). |
| 780 | fn mark_permanent_upgrade(flash: &mut Flash, slot: &SlotInfo) { |
| 781 | let ok = [1u8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; |
| 782 | let align = c::get_sim_flash_align() as usize; |
| 783 | let off = slot.trailer_off + c::boot_max_align(); |
| 784 | flash.write(off, &ok[..align]).unwrap(); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | // Drop some pseudo-random gibberish onto the data. |
| 788 | fn splat(data: &mut [u8], seed: usize) { |
| 789 | let seed_block = [0x135782ea, 0x92184728, data.len() as u32, seed as u32]; |
| 790 | let mut rng: XorShiftRng = SeedableRng::from_seed(seed_block); |
| 791 | rng.fill_bytes(data); |
| 792 | } |
| 793 | |
| 794 | /// Return a read-only view into the raw bytes of this object |
| 795 | trait AsRaw : Sized { |
| 796 | fn as_raw<'a>(&'a self) -> &'a [u8] { |
| 797 | unsafe { slice::from_raw_parts(self as *const _ as *const u8, |
| 798 | mem::size_of::<Self>()) } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | fn show_sizes() { |
| 803 | // This isn't panic safe. |
| 804 | let old_align = c::get_sim_flash_align(); |
| 805 | for min in &[1, 2, 4, 8] { |
| 806 | c::set_sim_flash_align(*min); |
| 807 | let msize = c::boot_trailer_sz(); |
| 808 | println!("{:2}: {} (0x{:x})", min, msize, msize); |
| 809 | } |
| 810 | c::set_sim_flash_align(old_align); |
| 811 | } |