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