David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1 | use log::{info, warn, error}; |
| 2 | use rand::{ |
| 3 | distributions::{IndependentSample, Range}, |
| 4 | Rng, SeedableRng, XorShiftRng, |
| 5 | }; |
| 6 | use std::{ |
| 7 | mem, |
| 8 | slice, |
| 9 | }; |
| 10 | use aes_ctr::{ |
| 11 | Aes128Ctr, |
| 12 | stream_cipher::{ |
| 13 | generic_array::GenericArray, |
| 14 | NewFixStreamCipher, |
| 15 | StreamCipherCore, |
| 16 | }, |
| 17 | }; |
| 18 | |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 19 | use simflash::{Flash, SimFlash, SimFlashMap}; |
| 20 | use mcuboot_sys::{c, AreaDesc, FlashId}; |
| 21 | use crate::{ |
| 22 | ALL_DEVICES, |
| 23 | DeviceName, |
| 24 | }; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 25 | use crate::caps::Caps; |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 26 | use crate::tlv::{ManifestGen, TlvGen, TlvFlags, AES_SEC_KEY}; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 27 | |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 28 | /// A builder for Images. This describes a single run of the simulator, |
| 29 | /// capturing the configuration of a particular set of devices, including |
| 30 | /// the flash simulator(s) and the information about the slots. |
| 31 | #[derive(Clone)] |
| 32 | pub struct ImagesBuilder { |
| 33 | flashmap: SimFlashMap, |
| 34 | areadesc: AreaDesc, |
| 35 | slots: [SlotInfo; 2], |
| 36 | } |
| 37 | |
David Brown | 998aa8d | 2019-02-28 10:54:50 -0700 | [diff] [blame] | 38 | /// Images represents the state of a simulation for a given set of images. |
| 39 | /// The flashmap holds the state of the simulated flash, whereas primaries |
| 40 | /// and upgrades hold the expected contents of these images. |
| 41 | pub struct Images { |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame^] | 42 | flashmap: SimFlashMap, |
| 43 | areadesc: AreaDesc, |
| 44 | slots: [SlotInfo; 2], |
| 45 | primaries: ImageData, |
| 46 | upgrades: ImageData, |
| 47 | total_count: Option<i32>, |
| 48 | } |
| 49 | |
| 50 | /// The Rust-side representation of an image. For unencrypted images, this |
| 51 | /// is just the unencrypted payload. For encrypted images, we store both |
| 52 | /// the encrypted and the plaintext. |
| 53 | struct ImageData { |
| 54 | plain: Vec<u8>, |
| 55 | cipher: Option<Vec<u8>>, |
David Brown | 998aa8d | 2019-02-28 10:54:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 58 | impl ImagesBuilder { |
| 59 | pub fn new(device: DeviceName, align: u8, erased_val: u8) -> Self { |
| 60 | let (flashmap, areadesc) = Self::make_device(device, align, erased_val); |
| 61 | |
| 62 | let (slot0_base, slot0_len, slot0_dev_id) = areadesc.find(FlashId::Image0); |
| 63 | let (slot1_base, slot1_len, slot1_dev_id) = areadesc.find(FlashId::Image1); |
| 64 | |
| 65 | // NOTE: not accounting "swap_size" because it is not used by sim... |
| 66 | let offset_from_end = c::boot_magic_sz() + c::boot_max_align() * 2; |
| 67 | |
| 68 | // Construct a primary image. |
| 69 | let slot0 = SlotInfo { |
| 70 | base_off: slot0_base as usize, |
| 71 | trailer_off: slot0_base + slot0_len - offset_from_end, |
| 72 | len: slot0_len as usize, |
| 73 | dev_id: slot0_dev_id, |
| 74 | }; |
| 75 | |
| 76 | // And an upgrade image. |
| 77 | let slot1 = SlotInfo { |
| 78 | base_off: slot1_base as usize, |
| 79 | trailer_off: slot1_base + slot1_len - offset_from_end, |
| 80 | len: slot1_len as usize, |
| 81 | dev_id: slot1_dev_id, |
| 82 | }; |
| 83 | |
| 84 | ImagesBuilder { |
| 85 | flashmap: flashmap, |
| 86 | areadesc: areadesc, |
| 87 | slots: [slot0, slot1], |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | pub fn each_device<F>(f: F) |
| 92 | where F: Fn(Self) |
| 93 | { |
| 94 | for &dev in ALL_DEVICES { |
| 95 | for &align in &[1, 2, 4, 8] { |
| 96 | for &erased_val in &[0, 0xff] { |
| 97 | let run = Self::new(dev, align, erased_val); |
| 98 | f(run); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// Construct an `Images` that doesn't expect an upgrade to happen. |
| 105 | pub fn make_no_upgrade_image(self) -> Images { |
| 106 | let mut flashmap = self.flashmap; |
| 107 | let primaries = install_image(&mut flashmap, &self.slots, 0, 32784, false); |
| 108 | let upgrades = install_image(&mut flashmap, &self.slots, 1, 41928, false); |
| 109 | Images { |
| 110 | flashmap: flashmap, |
| 111 | areadesc: self.areadesc, |
| 112 | slots: self.slots, |
| 113 | primaries: primaries, |
| 114 | upgrades: upgrades, |
| 115 | total_count: None, |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// Construct an `Images` for normal testing. |
| 120 | pub fn make_image(self) -> Images { |
| 121 | let mut images = self.make_no_upgrade_image(); |
| 122 | mark_upgrade(&mut images.flashmap, &images.slots[1]); |
| 123 | |
| 124 | // upgrades without fails, counts number of flash operations |
| 125 | let total_count = match images.run_basic_upgrade() { |
| 126 | Ok(v) => v, |
| 127 | Err(_) => { |
| 128 | panic!("Unable to perform basic upgrade"); |
| 129 | }, |
| 130 | }; |
| 131 | |
| 132 | images.total_count = Some(total_count); |
| 133 | images |
| 134 | } |
| 135 | |
| 136 | pub fn make_bad_secondary_slot_image(self) -> Images { |
| 137 | let mut bad_flashmap = self.flashmap; |
| 138 | let primaries = install_image(&mut bad_flashmap, &self.slots, 0, 32784, false); |
| 139 | let upgrades = install_image(&mut bad_flashmap, &self.slots, 1, 41928, true); |
| 140 | Images { |
| 141 | flashmap: bad_flashmap, |
| 142 | areadesc: self.areadesc, |
| 143 | slots: self.slots, |
| 144 | primaries: primaries, |
| 145 | upgrades: upgrades, |
| 146 | total_count: None, |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// Build the Flash and area descriptor for a given device. |
| 151 | pub fn make_device(device: DeviceName, align: u8, erased_val: u8) -> (SimFlashMap, AreaDesc) { |
| 152 | match device { |
| 153 | DeviceName::Stm32f4 => { |
| 154 | // STM style flash. Large sectors, with a large scratch area. |
| 155 | let flash = SimFlash::new(vec![16 * 1024, 16 * 1024, 16 * 1024, 16 * 1024, |
| 156 | 64 * 1024, |
| 157 | 128 * 1024, 128 * 1024, 128 * 1024], |
| 158 | align as usize, erased_val); |
| 159 | let dev_id = 0; |
| 160 | let mut areadesc = AreaDesc::new(); |
| 161 | areadesc.add_flash_sectors(dev_id, &flash); |
| 162 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 163 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 164 | areadesc.add_image(0x060000, 0x020000, FlashId::ImageScratch, dev_id); |
| 165 | |
| 166 | let mut flashmap = SimFlashMap::new(); |
| 167 | flashmap.insert(dev_id, flash); |
| 168 | (flashmap, areadesc) |
| 169 | } |
| 170 | DeviceName::K64f => { |
| 171 | // NXP style flash. Small sectors, one small sector for scratch. |
| 172 | let flash = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
| 173 | |
| 174 | let dev_id = 0; |
| 175 | let mut areadesc = AreaDesc::new(); |
| 176 | areadesc.add_flash_sectors(dev_id, &flash); |
| 177 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 178 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 179 | areadesc.add_image(0x060000, 0x001000, FlashId::ImageScratch, dev_id); |
| 180 | |
| 181 | let mut flashmap = SimFlashMap::new(); |
| 182 | flashmap.insert(dev_id, flash); |
| 183 | (flashmap, areadesc) |
| 184 | } |
| 185 | DeviceName::K64fBig => { |
| 186 | // Simulating an STM style flash on top of an NXP style flash. Underlying flash device |
| 187 | // uses small sectors, but we tell the bootloader they are large. |
| 188 | let flash = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
| 189 | |
| 190 | let dev_id = 0; |
| 191 | let mut areadesc = AreaDesc::new(); |
| 192 | areadesc.add_flash_sectors(dev_id, &flash); |
| 193 | areadesc.add_simple_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 194 | areadesc.add_simple_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 195 | areadesc.add_simple_image(0x060000, 0x020000, FlashId::ImageScratch, dev_id); |
| 196 | |
| 197 | let mut flashmap = SimFlashMap::new(); |
| 198 | flashmap.insert(dev_id, flash); |
| 199 | (flashmap, areadesc) |
| 200 | } |
| 201 | DeviceName::Nrf52840 => { |
| 202 | // Simulating the flash on the nrf52840 with partitions set up so that the scratch size |
| 203 | // does not divide into the image size. |
| 204 | let flash = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
| 205 | |
| 206 | let dev_id = 0; |
| 207 | let mut areadesc = AreaDesc::new(); |
| 208 | areadesc.add_flash_sectors(dev_id, &flash); |
| 209 | areadesc.add_image(0x008000, 0x034000, FlashId::Image0, dev_id); |
| 210 | areadesc.add_image(0x03c000, 0x034000, FlashId::Image1, dev_id); |
| 211 | areadesc.add_image(0x070000, 0x00d000, FlashId::ImageScratch, dev_id); |
| 212 | |
| 213 | let mut flashmap = SimFlashMap::new(); |
| 214 | flashmap.insert(dev_id, flash); |
| 215 | (flashmap, areadesc) |
| 216 | } |
| 217 | DeviceName::Nrf52840SpiFlash => { |
| 218 | // Simulate nrf52840 with external SPI flash. The external SPI flash |
| 219 | // has a larger sector size so for now store scratch on that flash. |
| 220 | let flash0 = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
| 221 | let flash1 = SimFlash::new(vec![8192; 64], align as usize, erased_val); |
| 222 | |
| 223 | let mut areadesc = AreaDesc::new(); |
| 224 | areadesc.add_flash_sectors(0, &flash0); |
| 225 | areadesc.add_flash_sectors(1, &flash1); |
| 226 | |
| 227 | areadesc.add_image(0x008000, 0x068000, FlashId::Image0, 0); |
| 228 | areadesc.add_image(0x000000, 0x068000, FlashId::Image1, 1); |
| 229 | areadesc.add_image(0x068000, 0x018000, FlashId::ImageScratch, 1); |
| 230 | |
| 231 | let mut flashmap = SimFlashMap::new(); |
| 232 | flashmap.insert(0, flash0); |
| 233 | flashmap.insert(1, flash1); |
| 234 | (flashmap, areadesc) |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 240 | impl Images { |
| 241 | /// A simple upgrade without forced failures. |
| 242 | /// |
| 243 | /// Returns the number of flash operations which can later be used to |
| 244 | /// inject failures at chosen steps. |
| 245 | pub fn run_basic_upgrade(&self) -> Result<i32, ()> { |
| 246 | let (flashmap, total_count) = try_upgrade(&self.flashmap, &self, None); |
| 247 | info!("Total flash operation count={}", total_count); |
| 248 | |
| 249 | if !verify_image(&flashmap, &self.slots, 0, &self.upgrades) { |
| 250 | warn!("Image mismatch after first boot"); |
| 251 | Err(()) |
| 252 | } else { |
| 253 | Ok(total_count) |
| 254 | } |
| 255 | } |
| 256 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 257 | pub fn run_basic_revert(&self) -> bool { |
David Brown | 3910ab1 | 2019-01-11 12:02:26 -0700 | [diff] [blame] | 258 | if Caps::OverwriteUpgrade.present() { |
| 259 | return false; |
| 260 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 261 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 262 | let mut fails = 0; |
| 263 | |
| 264 | // FIXME: this test would also pass if no swap is ever performed??? |
| 265 | if Caps::SwapUpgrade.present() { |
| 266 | for count in 2 .. 5 { |
| 267 | info!("Try revert: {}", count); |
| 268 | let flashmap = try_revert(&self.flashmap, &self.areadesc, count); |
| 269 | if !verify_image(&flashmap, &self.slots, 0, &self.primaries) { |
| 270 | error!("Revert failure on count {}", count); |
| 271 | fails += 1; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | fails > 0 |
| 277 | } |
| 278 | |
| 279 | pub fn run_perm_with_fails(&self) -> bool { |
| 280 | let mut fails = 0; |
| 281 | let total_flash_ops = self.total_count.unwrap(); |
| 282 | |
| 283 | // Let's try an image halfway through. |
| 284 | for i in 1 .. total_flash_ops { |
| 285 | info!("Try interruption at {}", i); |
| 286 | let (flashmap, count) = try_upgrade(&self.flashmap, &self, Some(i)); |
| 287 | info!("Second boot, count={}", count); |
| 288 | if !verify_image(&flashmap, &self.slots, 0, &self.upgrades) { |
| 289 | warn!("FAIL at step {} of {}", i, total_flash_ops); |
| 290 | fails += 1; |
| 291 | } |
| 292 | |
| 293 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 294 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 295 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 296 | fails += 1; |
| 297 | } |
| 298 | |
| 299 | if !verify_trailer(&flashmap, &self.slots, 1, BOOT_MAGIC_UNSET, |
| 300 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 301 | warn!("Mismatched trailer for the secondary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 302 | fails += 1; |
| 303 | } |
| 304 | |
| 305 | if Caps::SwapUpgrade.present() { |
| 306 | if !verify_image(&flashmap, &self.slots, 1, &self.primaries) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 307 | warn!("Secondary slot FAIL at step {} of {}", |
| 308 | i, total_flash_ops); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 309 | fails += 1; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if fails > 0 { |
| 315 | error!("{} out of {} failed {:.2}%", fails, total_flash_ops, |
| 316 | fails as f32 * 100.0 / total_flash_ops as f32); |
| 317 | } |
| 318 | |
| 319 | fails > 0 |
| 320 | } |
| 321 | |
| 322 | pub fn run_perm_with_random_fails_5(&self) -> bool { |
| 323 | self.run_perm_with_random_fails(5) |
| 324 | } |
| 325 | |
| 326 | pub fn run_perm_with_random_fails(&self, total_fails: usize) -> bool { |
| 327 | let mut fails = 0; |
| 328 | let total_flash_ops = self.total_count.unwrap(); |
| 329 | let (flashmap, total_counts) = try_random_fails(&self.flashmap, &self, |
| 330 | total_flash_ops, total_fails); |
| 331 | info!("Random interruptions at reset points={:?}", total_counts); |
| 332 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 333 | let primary_slot_ok = verify_image(&flashmap, &self.slots, |
| 334 | 0, &self.upgrades); |
| 335 | let secondary_slot_ok = if Caps::SwapUpgrade.present() { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 336 | verify_image(&flashmap, &self.slots, 1, &self.primaries) |
| 337 | } else { |
| 338 | true |
| 339 | }; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 340 | if !primary_slot_ok || !secondary_slot_ok { |
| 341 | error!("Image mismatch after random interrupts: primary slot={} \ |
| 342 | secondary slot={}", |
| 343 | if primary_slot_ok { "ok" } else { "fail" }, |
| 344 | if secondary_slot_ok { "ok" } else { "fail" }); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 345 | fails += 1; |
| 346 | } |
| 347 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 348 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 349 | error!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 350 | fails += 1; |
| 351 | } |
| 352 | if !verify_trailer(&flashmap, &self.slots, 1, BOOT_MAGIC_UNSET, |
| 353 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 354 | error!("Mismatched trailer for the secondary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 355 | fails += 1; |
| 356 | } |
| 357 | |
| 358 | if fails > 0 { |
| 359 | error!("Error testing perm upgrade with {} fails", total_fails); |
| 360 | } |
| 361 | |
| 362 | fails > 0 |
| 363 | } |
| 364 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 365 | pub fn run_revert_with_fails(&self) -> bool { |
David Brown | 3910ab1 | 2019-01-11 12:02:26 -0700 | [diff] [blame] | 366 | if Caps::OverwriteUpgrade.present() { |
| 367 | return false; |
| 368 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 369 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 370 | let mut fails = 0; |
| 371 | |
| 372 | if Caps::SwapUpgrade.present() { |
| 373 | for i in 1 .. (self.total_count.unwrap() - 1) { |
| 374 | info!("Try interruption at {}", i); |
| 375 | if try_revert_with_fail_at(&self.flashmap, &self, i) { |
| 376 | error!("Revert failed at interruption {}", i); |
| 377 | fails += 1; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | fails > 0 |
| 383 | } |
| 384 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 385 | pub fn run_norevert(&self) -> bool { |
David Brown | 3910ab1 | 2019-01-11 12:02:26 -0700 | [diff] [blame] | 386 | if Caps::OverwriteUpgrade.present() { |
| 387 | return false; |
| 388 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 389 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 390 | let mut flashmap = self.flashmap.clone(); |
| 391 | let mut fails = 0; |
| 392 | |
| 393 | info!("Try norevert"); |
| 394 | |
| 395 | // First do a normal upgrade... |
| 396 | let (result, _) = c::boot_go(&mut flashmap, &self.areadesc, None, false); |
| 397 | if result != 0 { |
| 398 | warn!("Failed first boot"); |
| 399 | fails += 1; |
| 400 | } |
| 401 | |
| 402 | //FIXME: copy_done is written by boot_go, is it ok if no copy |
| 403 | // was ever done? |
| 404 | |
| 405 | if !verify_image(&flashmap, &self.slots, 0, &self.upgrades) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 406 | warn!("Primary slot image verification FAIL"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 407 | fails += 1; |
| 408 | } |
| 409 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 410 | BOOT_FLAG_UNSET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 411 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 412 | fails += 1; |
| 413 | } |
| 414 | if !verify_trailer(&flashmap, &self.slots, 1, BOOT_MAGIC_UNSET, |
| 415 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 416 | warn!("Mismatched trailer for the secondary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 417 | fails += 1; |
| 418 | } |
| 419 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 420 | // Marks image in the primary slot as permanent, |
| 421 | // no revert should happen... |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 422 | mark_permanent_upgrade(&mut flashmap, &self.slots[0]); |
| 423 | |
| 424 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 425 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 426 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 427 | fails += 1; |
| 428 | } |
| 429 | |
| 430 | let (result, _) = c::boot_go(&mut flashmap, &self.areadesc, None, false); |
| 431 | if result != 0 { |
| 432 | warn!("Failed second boot"); |
| 433 | fails += 1; |
| 434 | } |
| 435 | |
| 436 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 437 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 438 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 439 | fails += 1; |
| 440 | } |
| 441 | if !verify_image(&flashmap, &self.slots, 0, &self.upgrades) { |
| 442 | warn!("Failed image verification"); |
| 443 | fails += 1; |
| 444 | } |
| 445 | |
| 446 | if fails > 0 { |
| 447 | error!("Error running upgrade without revert"); |
| 448 | } |
| 449 | |
| 450 | fails > 0 |
| 451 | } |
| 452 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 453 | // Tests a new image written to the primary slot that already has magic and |
| 454 | // image_ok set while there is no image on the secondary slot, so no revert |
| 455 | // should ever happen... |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 456 | pub fn run_norevert_newimage(&self) -> bool { |
| 457 | let mut flashmap = self.flashmap.clone(); |
| 458 | let mut fails = 0; |
| 459 | |
| 460 | info!("Try non-revert on imgtool generated image"); |
| 461 | |
| 462 | mark_upgrade(&mut flashmap, &self.slots[0]); |
| 463 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 464 | // This simulates writing an image created by imgtool to |
| 465 | // the primary slot |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 466 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 467 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 468 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 469 | fails += 1; |
| 470 | } |
| 471 | |
| 472 | // Run the bootloader... |
| 473 | let (result, _) = c::boot_go(&mut flashmap, &self.areadesc, None, false); |
| 474 | if result != 0 { |
| 475 | warn!("Failed first boot"); |
| 476 | fails += 1; |
| 477 | } |
| 478 | |
| 479 | // State should not have changed |
| 480 | if !verify_image(&flashmap, &self.slots, 0, &self.primaries) { |
| 481 | warn!("Failed image verification"); |
| 482 | fails += 1; |
| 483 | } |
| 484 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 485 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 486 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 487 | fails += 1; |
| 488 | } |
| 489 | if !verify_trailer(&flashmap, &self.slots, 1, BOOT_MAGIC_UNSET, |
| 490 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 491 | warn!("Mismatched trailer for the secondary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 492 | fails += 1; |
| 493 | } |
| 494 | |
| 495 | if fails > 0 { |
| 496 | error!("Expected a non revert with new image"); |
| 497 | } |
| 498 | |
| 499 | fails > 0 |
| 500 | } |
| 501 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 502 | // Tests a new image written to the primary slot that already has magic and |
| 503 | // image_ok set while there is no image on the secondary slot, so no revert |
| 504 | // should ever happen... |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 505 | pub fn run_signfail_upgrade(&self) -> bool { |
| 506 | let mut flashmap = self.flashmap.clone(); |
| 507 | let mut fails = 0; |
| 508 | |
| 509 | info!("Try upgrade image with bad signature"); |
| 510 | |
| 511 | mark_upgrade(&mut flashmap, &self.slots[0]); |
| 512 | mark_permanent_upgrade(&mut flashmap, &self.slots[0]); |
| 513 | mark_upgrade(&mut flashmap, &self.slots[1]); |
| 514 | |
| 515 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 516 | BOOT_FLAG_SET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 517 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 518 | fails += 1; |
| 519 | } |
| 520 | |
| 521 | // Run the bootloader... |
| 522 | let (result, _) = c::boot_go(&mut flashmap, &self.areadesc, None, false); |
| 523 | if result != 0 { |
| 524 | warn!("Failed first boot"); |
| 525 | fails += 1; |
| 526 | } |
| 527 | |
| 528 | // State should not have changed |
| 529 | if !verify_image(&flashmap, &self.slots, 0, &self.primaries) { |
| 530 | warn!("Failed image verification"); |
| 531 | fails += 1; |
| 532 | } |
| 533 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 534 | BOOT_FLAG_SET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 535 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 536 | fails += 1; |
| 537 | } |
| 538 | |
| 539 | if fails > 0 { |
| 540 | error!("Expected an upgrade failure when image has bad signature"); |
| 541 | } |
| 542 | |
| 543 | fails > 0 |
| 544 | } |
| 545 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 546 | fn trailer_sz(&self, align: usize) -> usize { |
| 547 | c::boot_trailer_sz(align as u8) as usize |
| 548 | } |
| 549 | |
| 550 | // FIXME: could get status sz from bootloader |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 551 | fn status_sz(&self, align: usize) -> usize { |
David Brown | 9930a3e | 2019-01-11 12:28:26 -0700 | [diff] [blame] | 552 | let bias = if Caps::EncRsa.present() || Caps::EncKw.present() { |
| 553 | 32 |
| 554 | } else { |
| 555 | 0 |
| 556 | }; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 557 | |
David Brown | 9930a3e | 2019-01-11 12:28:26 -0700 | [diff] [blame] | 558 | self.trailer_sz(align) - (16 + 24 + bias) |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /// This test runs a simple upgrade with no fails in the images, but |
| 562 | /// allowing for fails in the status area. This should run to the end |
| 563 | /// and warn that write fails were detected... |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 564 | pub fn run_with_status_fails_complete(&self) -> bool { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 565 | if !Caps::ValidatePrimarySlot.present() { |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 566 | return false; |
| 567 | } |
| 568 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 569 | let mut flashmap = self.flashmap.clone(); |
| 570 | let mut fails = 0; |
| 571 | |
| 572 | info!("Try swap with status fails"); |
| 573 | |
| 574 | mark_permanent_upgrade(&mut flashmap, &self.slots[1]); |
| 575 | self.mark_bad_status_with_rate(&mut flashmap, 0, 1.0); |
| 576 | |
| 577 | let (result, asserts) = c::boot_go(&mut flashmap, &self.areadesc, None, true); |
| 578 | if result != 0 { |
| 579 | warn!("Failed!"); |
| 580 | fails += 1; |
| 581 | } |
| 582 | |
| 583 | // Failed writes to the marked "bad" region don't assert anymore. |
| 584 | // Any detected assert() is happening in another part of the code. |
| 585 | if asserts != 0 { |
| 586 | warn!("At least one assert() was called"); |
| 587 | fails += 1; |
| 588 | } |
| 589 | |
| 590 | if !verify_trailer(&flashmap, &self.slots, 0, BOOT_MAGIC_GOOD, |
| 591 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 592 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 593 | fails += 1; |
| 594 | } |
| 595 | |
| 596 | if !verify_image(&flashmap, &self.slots, 0, &self.upgrades) { |
| 597 | warn!("Failed image verification"); |
| 598 | fails += 1; |
| 599 | } |
| 600 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 601 | info!("validate primary slot enabled; \ |
| 602 | re-run of boot_go should just work"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 603 | let (result, _) = c::boot_go(&mut flashmap, &self.areadesc, None, false); |
| 604 | if result != 0 { |
| 605 | warn!("Failed!"); |
| 606 | fails += 1; |
| 607 | } |
| 608 | |
| 609 | if fails > 0 { |
| 610 | error!("Error running upgrade with status write fails"); |
| 611 | } |
| 612 | |
| 613 | fails > 0 |
| 614 | } |
| 615 | |
| 616 | /// This test runs a simple upgrade with no fails in the images, but |
| 617 | /// allowing for fails in the status area. This should run to the end |
| 618 | /// and warn that write fails were detected... |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 619 | pub fn run_with_status_fails_with_reset(&self) -> bool { |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 620 | if Caps::OverwriteUpgrade.present() { |
| 621 | false |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 622 | } else if Caps::ValidatePrimarySlot.present() { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 623 | |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 624 | let mut flashmap = self.flashmap.clone(); |
| 625 | let mut fails = 0; |
| 626 | let mut count = self.total_count.unwrap() / 2; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 627 | |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 628 | //info!("count={}\n", count); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 629 | |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 630 | info!("Try interrupted swap with status fails"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 631 | |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 632 | mark_permanent_upgrade(&mut flashmap, &self.slots[1]); |
| 633 | self.mark_bad_status_with_rate(&mut flashmap, 0, 0.5); |
| 634 | |
| 635 | // Should not fail, writing to bad regions does not assert |
| 636 | let (_, asserts) = c::boot_go(&mut flashmap, &self.areadesc, Some(&mut count), true); |
| 637 | if asserts != 0 { |
| 638 | warn!("At least one assert() was called"); |
| 639 | fails += 1; |
| 640 | } |
| 641 | |
| 642 | self.reset_bad_status(&mut flashmap, 0); |
| 643 | |
| 644 | info!("Resuming an interrupted swap operation"); |
| 645 | let (_, asserts) = c::boot_go(&mut flashmap, &self.areadesc, None, true); |
| 646 | |
| 647 | // This might throw no asserts, for large sector devices, where |
| 648 | // a single failure writing is indistinguishable from no failure, |
| 649 | // or throw a single assert for small sector devices that fail |
| 650 | // multiple times... |
| 651 | if asserts > 1 { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 652 | warn!("Expected single assert validating the primary slot, \ |
| 653 | more detected {}", asserts); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 654 | fails += 1; |
| 655 | } |
| 656 | |
| 657 | if fails > 0 { |
| 658 | error!("Error running upgrade with status write fails"); |
| 659 | } |
| 660 | |
| 661 | fails > 0 |
| 662 | } else { |
| 663 | let mut flashmap = self.flashmap.clone(); |
| 664 | let mut fails = 0; |
| 665 | |
| 666 | info!("Try interrupted swap with status fails"); |
| 667 | |
| 668 | mark_permanent_upgrade(&mut flashmap, &self.slots[1]); |
| 669 | self.mark_bad_status_with_rate(&mut flashmap, 0, 1.0); |
| 670 | |
| 671 | // This is expected to fail while writing to bad regions... |
| 672 | let (_, asserts) = c::boot_go(&mut flashmap, &self.areadesc, None, true); |
| 673 | if asserts == 0 { |
| 674 | warn!("No assert() detected"); |
| 675 | fails += 1; |
| 676 | } |
| 677 | |
| 678 | fails > 0 |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 679 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /// Adds a new flash area that fails statistically |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 683 | fn mark_bad_status_with_rate(&self, flashmap: &mut SimFlashMap, slot: usize, |
| 684 | rate: f32) { |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 685 | if Caps::OverwriteUpgrade.present() { |
| 686 | return; |
| 687 | } |
| 688 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 689 | let dev_id = &self.slots[slot].dev_id; |
| 690 | let flash = flashmap.get_mut(&dev_id).unwrap(); |
| 691 | let align = flash.align(); |
| 692 | let off = &self.slots[0].base_off; |
| 693 | let len = &self.slots[0].len; |
| 694 | let status_off = off + len - self.trailer_sz(align); |
| 695 | |
| 696 | // Mark the status area as a bad area |
| 697 | let _ = flash.add_bad_region(status_off, self.status_sz(align), rate); |
| 698 | } |
| 699 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 700 | fn reset_bad_status(&self, flashmap: &mut SimFlashMap, slot: usize) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 701 | if !Caps::ValidatePrimarySlot.present() { |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 702 | return; |
| 703 | } |
| 704 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 705 | let dev_id = &self.slots[slot].dev_id; |
| 706 | let flash = flashmap.get_mut(&dev_id).unwrap(); |
| 707 | flash.reset_bad_regions(); |
| 708 | |
| 709 | // Disabling write verification the only assert triggered by |
| 710 | // boot_go should be checking for integrity of status bytes. |
| 711 | flash.set_verify_writes(false); |
| 712 | } |
| 713 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /// Test a boot, optionally stopping after 'n' flash options. Returns a count |
| 717 | /// of the number of flash operations done total. |
| 718 | fn try_upgrade(flashmap: &SimFlashMap, images: &Images, |
| 719 | stop: Option<i32>) -> (SimFlashMap, i32) { |
| 720 | // Clone the flash to have a new copy. |
| 721 | let mut flashmap = flashmap.clone(); |
| 722 | |
| 723 | mark_permanent_upgrade(&mut flashmap, &images.slots[1]); |
| 724 | |
| 725 | let mut counter = stop.unwrap_or(0); |
| 726 | |
| 727 | let (first_interrupted, count) = match c::boot_go(&mut flashmap, &images.areadesc, Some(&mut counter), false) { |
| 728 | (-0x13579, _) => (true, stop.unwrap()), |
| 729 | (0, _) => (false, -counter), |
| 730 | (x, _) => panic!("Unknown return: {}", x), |
| 731 | }; |
| 732 | |
| 733 | counter = 0; |
| 734 | if first_interrupted { |
| 735 | // fl.dump(); |
| 736 | match c::boot_go(&mut flashmap, &images.areadesc, Some(&mut counter), false) { |
| 737 | (-0x13579, _) => panic!("Shouldn't stop again"), |
| 738 | (0, _) => (), |
| 739 | (x, _) => panic!("Unknown return: {}", x), |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | (flashmap, count - counter) |
| 744 | } |
| 745 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 746 | fn try_revert(flashmap: &SimFlashMap, areadesc: &AreaDesc, count: usize) -> SimFlashMap { |
| 747 | let mut flashmap = flashmap.clone(); |
| 748 | |
| 749 | // fl.write_file("image0.bin").unwrap(); |
| 750 | for i in 0 .. count { |
| 751 | info!("Running boot pass {}", i + 1); |
| 752 | assert_eq!(c::boot_go(&mut flashmap, &areadesc, None, false), (0, 0)); |
| 753 | } |
| 754 | flashmap |
| 755 | } |
| 756 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 757 | fn try_revert_with_fail_at(flashmap: &SimFlashMap, images: &Images, |
| 758 | stop: i32) -> bool { |
| 759 | let mut flashmap = flashmap.clone(); |
| 760 | let mut fails = 0; |
| 761 | |
| 762 | let mut counter = stop; |
| 763 | let (x, _) = c::boot_go(&mut flashmap, &images.areadesc, Some(&mut counter), false); |
| 764 | if x != -0x13579 { |
| 765 | warn!("Should have stopped at interruption point"); |
| 766 | fails += 1; |
| 767 | } |
| 768 | |
| 769 | if !verify_trailer(&flashmap, &images.slots, 0, None, None, BOOT_FLAG_UNSET) { |
| 770 | warn!("copy_done should be unset"); |
| 771 | fails += 1; |
| 772 | } |
| 773 | |
| 774 | let (x, _) = c::boot_go(&mut flashmap, &images.areadesc, None, false); |
| 775 | if x != 0 { |
| 776 | warn!("Should have finished upgrade"); |
| 777 | fails += 1; |
| 778 | } |
| 779 | |
| 780 | if !verify_image(&flashmap, &images.slots, 0, &images.upgrades) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 781 | warn!("Image in the primary slot before revert is invalid at stop={}", |
| 782 | stop); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 783 | fails += 1; |
| 784 | } |
| 785 | if !verify_image(&flashmap, &images.slots, 1, &images.primaries) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 786 | warn!("Image in the secondary slot before revert is invalid at stop={}", |
| 787 | stop); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 788 | fails += 1; |
| 789 | } |
| 790 | if !verify_trailer(&flashmap, &images.slots, 0, BOOT_MAGIC_GOOD, |
| 791 | BOOT_FLAG_UNSET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 792 | warn!("Mismatched trailer for the primary slot before revert"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 793 | fails += 1; |
| 794 | } |
| 795 | if !verify_trailer(&flashmap, &images.slots, 1, BOOT_MAGIC_UNSET, |
| 796 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 797 | warn!("Mismatched trailer for the secondary slot before revert"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 798 | fails += 1; |
| 799 | } |
| 800 | |
| 801 | // Do Revert |
| 802 | let (x, _) = c::boot_go(&mut flashmap, &images.areadesc, None, false); |
| 803 | if x != 0 { |
| 804 | warn!("Should have finished a revert"); |
| 805 | fails += 1; |
| 806 | } |
| 807 | |
| 808 | if !verify_image(&flashmap, &images.slots, 0, &images.primaries) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 809 | warn!("Image in the primary slot after revert is invalid at stop={}", |
| 810 | stop); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 811 | fails += 1; |
| 812 | } |
| 813 | if !verify_image(&flashmap, &images.slots, 1, &images.upgrades) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 814 | warn!("Image in the secondary slot after revert is invalid at stop={}", |
| 815 | stop); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 816 | fails += 1; |
| 817 | } |
| 818 | if !verify_trailer(&flashmap, &images.slots, 0, BOOT_MAGIC_GOOD, |
| 819 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 820 | warn!("Mismatched trailer for the secondary slot after revert"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 821 | fails += 1; |
| 822 | } |
| 823 | if !verify_trailer(&flashmap, &images.slots, 1, BOOT_MAGIC_UNSET, |
| 824 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 825 | warn!("Mismatched trailer for the secondary slot after revert"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 826 | fails += 1; |
| 827 | } |
| 828 | |
| 829 | fails > 0 |
| 830 | } |
| 831 | |
| 832 | fn try_random_fails(flashmap: &SimFlashMap, images: &Images, |
| 833 | total_ops: i32, count: usize) -> (SimFlashMap, Vec<i32>) { |
| 834 | let mut flashmap = flashmap.clone(); |
| 835 | |
| 836 | mark_permanent_upgrade(&mut flashmap, &images.slots[1]); |
| 837 | |
| 838 | let mut rng = rand::thread_rng(); |
| 839 | let mut resets = vec![0i32; count]; |
| 840 | let mut remaining_ops = total_ops; |
| 841 | for i in 0 .. count { |
| 842 | let ops = Range::new(1, remaining_ops / 2); |
| 843 | let reset_counter = ops.ind_sample(&mut rng); |
| 844 | let mut counter = reset_counter; |
| 845 | match c::boot_go(&mut flashmap, &images.areadesc, Some(&mut counter), false) { |
| 846 | (0, _) | (-0x13579, _) => (), |
| 847 | (x, _) => panic!("Unknown return: {}", x), |
| 848 | } |
| 849 | remaining_ops -= reset_counter; |
| 850 | resets[i] = reset_counter; |
| 851 | } |
| 852 | |
| 853 | match c::boot_go(&mut flashmap, &images.areadesc, None, false) { |
| 854 | (-0x13579, _) => panic!("Should not be have been interrupted!"), |
| 855 | (0, _) => (), |
| 856 | (x, _) => panic!("Unknown return: {}", x), |
| 857 | } |
| 858 | |
| 859 | (flashmap, resets) |
| 860 | } |
| 861 | |
| 862 | /// Show the flash layout. |
| 863 | #[allow(dead_code)] |
| 864 | fn show_flash(flash: &dyn Flash) { |
| 865 | println!("---- Flash configuration ----"); |
| 866 | for sector in flash.sector_iter() { |
| 867 | println!(" {:3}: 0x{:08x}, 0x{:08x}", |
| 868 | sector.num, sector.base, sector.size); |
| 869 | } |
| 870 | println!(""); |
| 871 | } |
| 872 | |
| 873 | /// Install a "program" into the given image. This fakes the image header, or at least all of the |
| 874 | /// fields used by the given code. Returns a copy of the image that was written. |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame^] | 875 | fn install_image(flashmap: &mut SimFlashMap, slots: &[SlotInfo], slot: usize, len: usize, |
| 876 | bad_sig: bool) -> ImageData { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 877 | let offset = slots[slot].base_off; |
| 878 | let slot_len = slots[slot].len; |
| 879 | let dev_id = slots[slot].dev_id; |
| 880 | |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 881 | let mut tlv: Box<dyn ManifestGen> = Box::new(make_tlv()); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 882 | |
| 883 | const HDR_SIZE: usize = 32; |
| 884 | |
| 885 | // Generate a boot header. Note that the size doesn't include the header. |
| 886 | let header = ImageHeader { |
David Brown | ac46e26 | 2019-01-11 15:46:18 -0700 | [diff] [blame] | 887 | magic: tlv.get_magic(), |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 888 | load_addr: 0, |
| 889 | hdr_size: HDR_SIZE as u16, |
| 890 | _pad1: 0, |
| 891 | img_size: len as u32, |
| 892 | flags: tlv.get_flags(), |
| 893 | ver: ImageVersion { |
| 894 | major: (offset / (128 * 1024)) as u8, |
| 895 | minor: 0, |
| 896 | revision: 1, |
| 897 | build_num: offset as u32, |
| 898 | }, |
| 899 | _pad2: 0, |
| 900 | }; |
| 901 | |
| 902 | let mut b_header = [0; HDR_SIZE]; |
| 903 | b_header[..32].clone_from_slice(header.as_raw()); |
| 904 | assert_eq!(b_header.len(), HDR_SIZE); |
| 905 | |
| 906 | tlv.add_bytes(&b_header); |
| 907 | |
| 908 | // The core of the image itself is just pseudorandom data. |
| 909 | let mut b_img = vec![0; len]; |
| 910 | splat(&mut b_img, offset); |
| 911 | |
| 912 | // TLV signatures work over plain image |
| 913 | tlv.add_bytes(&b_img); |
| 914 | |
| 915 | // Generate encrypted images |
| 916 | let flag = TlvFlags::ENCRYPTED as u32; |
| 917 | let is_encrypted = (tlv.get_flags() & flag) == flag; |
| 918 | let mut b_encimg = vec![]; |
| 919 | if is_encrypted { |
| 920 | let key = GenericArray::from_slice(AES_SEC_KEY); |
| 921 | let nonce = GenericArray::from_slice(&[0; 16]); |
| 922 | let mut cipher = Aes128Ctr::new(&key, &nonce); |
| 923 | b_encimg = b_img.clone(); |
| 924 | cipher.apply_keystream(&mut b_encimg); |
| 925 | } |
| 926 | |
| 927 | // Build the TLV itself. |
| 928 | let mut b_tlv = if bad_sig { |
| 929 | let good_sig = &mut tlv.make_tlv(); |
| 930 | vec![0; good_sig.len()] |
| 931 | } else { |
| 932 | tlv.make_tlv() |
| 933 | }; |
| 934 | |
| 935 | // Pad the block to a flash alignment (8 bytes). |
| 936 | while b_tlv.len() % 8 != 0 { |
| 937 | //FIXME: should be erase_val? |
| 938 | b_tlv.push(0xFF); |
| 939 | } |
| 940 | |
| 941 | let mut buf = vec![]; |
| 942 | buf.append(&mut b_header.to_vec()); |
| 943 | buf.append(&mut b_img); |
| 944 | buf.append(&mut b_tlv.clone()); |
| 945 | |
| 946 | let mut encbuf = vec![]; |
| 947 | if is_encrypted { |
| 948 | encbuf.append(&mut b_header.to_vec()); |
| 949 | encbuf.append(&mut b_encimg); |
| 950 | encbuf.append(&mut b_tlv); |
| 951 | } |
| 952 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 953 | // Since images are always non-encrypted in the primary slot, we first write |
| 954 | // an encrypted image, re-read to use for verification, erase + flash |
| 955 | // un-encrypted. In the secondary slot the image is written un-encrypted, |
| 956 | // and if encryption is requested, it follows an erase + flash encrypted. |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 957 | |
| 958 | let flash = flashmap.get_mut(&dev_id).unwrap(); |
| 959 | |
| 960 | if slot == 0 { |
| 961 | let enc_copy: Option<Vec<u8>>; |
| 962 | |
| 963 | if is_encrypted { |
| 964 | flash.write(offset, &encbuf).unwrap(); |
| 965 | |
| 966 | let mut enc = vec![0u8; encbuf.len()]; |
| 967 | flash.read(offset, &mut enc).unwrap(); |
| 968 | |
| 969 | enc_copy = Some(enc); |
| 970 | |
| 971 | flash.erase(offset, slot_len).unwrap(); |
| 972 | } else { |
| 973 | enc_copy = None; |
| 974 | } |
| 975 | |
| 976 | flash.write(offset, &buf).unwrap(); |
| 977 | |
| 978 | let mut copy = vec![0u8; buf.len()]; |
| 979 | flash.read(offset, &mut copy).unwrap(); |
| 980 | |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame^] | 981 | ImageData { |
| 982 | plain: copy, |
| 983 | cipher: enc_copy, |
| 984 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 985 | } else { |
| 986 | |
| 987 | flash.write(offset, &buf).unwrap(); |
| 988 | |
| 989 | let mut copy = vec![0u8; buf.len()]; |
| 990 | flash.read(offset, &mut copy).unwrap(); |
| 991 | |
| 992 | let enc_copy: Option<Vec<u8>>; |
| 993 | |
| 994 | if is_encrypted { |
| 995 | flash.erase(offset, slot_len).unwrap(); |
| 996 | |
| 997 | flash.write(offset, &encbuf).unwrap(); |
| 998 | |
| 999 | let mut enc = vec![0u8; encbuf.len()]; |
| 1000 | flash.read(offset, &mut enc).unwrap(); |
| 1001 | |
| 1002 | enc_copy = Some(enc); |
| 1003 | } else { |
| 1004 | enc_copy = None; |
| 1005 | } |
| 1006 | |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame^] | 1007 | ImageData { |
| 1008 | plain: copy, |
| 1009 | cipher: enc_copy, |
| 1010 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1011 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1014 | fn make_tlv() -> TlvGen { |
David Brown | b888211 | 2019-01-11 14:04:11 -0700 | [diff] [blame] | 1015 | if Caps::EcdsaP224.present() { |
| 1016 | panic!("Ecdsa P224 not supported in Simulator"); |
| 1017 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1018 | |
David Brown | b888211 | 2019-01-11 14:04:11 -0700 | [diff] [blame] | 1019 | if Caps::EncKw.present() { |
| 1020 | if Caps::RSA2048.present() { |
| 1021 | TlvGen::new_rsa_kw() |
| 1022 | } else if Caps::EcdsaP256.present() { |
| 1023 | TlvGen::new_ecdsa_kw() |
| 1024 | } else { |
| 1025 | TlvGen::new_enc_kw() |
| 1026 | } |
| 1027 | } else if Caps::EncRsa.present() { |
| 1028 | if Caps::RSA2048.present() { |
| 1029 | TlvGen::new_sig_enc_rsa() |
| 1030 | } else { |
| 1031 | TlvGen::new_enc_rsa() |
| 1032 | } |
| 1033 | } else { |
| 1034 | // The non-encrypted configuration. |
| 1035 | if Caps::RSA2048.present() { |
| 1036 | TlvGen::new_rsa_pss() |
| 1037 | } else if Caps::EcdsaP256.present() { |
| 1038 | TlvGen::new_ecdsa() |
| 1039 | } else { |
| 1040 | TlvGen::new_hash_only() |
| 1041 | } |
| 1042 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame^] | 1045 | impl ImageData { |
| 1046 | /// Find the image contents for the given slot. This assumes that slot 0 |
| 1047 | /// is unencrypted, and slot 1 is encrypted. |
| 1048 | fn find(&self, slot: usize) -> &Vec<u8> { |
| 1049 | let encrypted = Caps::EncRsa.present() || Caps::EncKw.present(); |
| 1050 | match (encrypted, slot) { |
| 1051 | (false, _) => &self.plain, |
| 1052 | (true, 0) => &self.plain, |
| 1053 | (true, 1) => self.cipher.as_ref().expect("Invalid image"), |
| 1054 | _ => panic!("Invalid slot requested"), |
| 1055 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1059 | /// Verify that given image is present in the flash at the given offset. |
| 1060 | fn verify_image(flashmap: &SimFlashMap, slots: &[SlotInfo], slot: usize, |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame^] | 1061 | images: &ImageData) -> bool { |
| 1062 | let image = images.find(slot); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1063 | let buf = image.as_slice(); |
| 1064 | let dev_id = slots[slot].dev_id; |
| 1065 | |
| 1066 | let mut copy = vec![0u8; buf.len()]; |
| 1067 | let offset = slots[slot].base_off; |
| 1068 | let flash = flashmap.get(&dev_id).unwrap(); |
| 1069 | flash.read(offset, &mut copy).unwrap(); |
| 1070 | |
| 1071 | if buf != ©[..] { |
| 1072 | for i in 0 .. buf.len() { |
| 1073 | if buf[i] != copy[i] { |
| 1074 | info!("First failure for slot{} at {:#x} {:#x}!={:#x}", |
| 1075 | slot, offset + i, buf[i], copy[i]); |
| 1076 | break; |
| 1077 | } |
| 1078 | } |
| 1079 | false |
| 1080 | } else { |
| 1081 | true |
| 1082 | } |
| 1083 | } |
| 1084 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1085 | fn verify_trailer(flashmap: &SimFlashMap, slots: &[SlotInfo], slot: usize, |
| 1086 | magic: Option<u8>, image_ok: Option<u8>, |
| 1087 | copy_done: Option<u8>) -> bool { |
David Brown | 61a540d | 2019-01-11 14:29:14 -0700 | [diff] [blame] | 1088 | if Caps::OverwriteUpgrade.present() { |
| 1089 | return true; |
| 1090 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1091 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1092 | let offset = slots[slot].trailer_off; |
| 1093 | let dev_id = slots[slot].dev_id; |
| 1094 | let mut copy = vec![0u8; c::boot_magic_sz() + c::boot_max_align() * 2]; |
| 1095 | let mut failed = false; |
| 1096 | |
| 1097 | let flash = flashmap.get(&dev_id).unwrap(); |
| 1098 | let erased_val = flash.erased_val(); |
| 1099 | flash.read(offset, &mut copy).unwrap(); |
| 1100 | |
| 1101 | failed |= match magic { |
| 1102 | Some(v) => { |
| 1103 | if v == 1 && ©[16..] != MAGIC.unwrap() { |
| 1104 | warn!("\"magic\" mismatch at {:#x}", offset); |
| 1105 | true |
| 1106 | } else if v == 3 { |
| 1107 | let expected = [erased_val; 16]; |
| 1108 | if ©[16..] != expected { |
| 1109 | warn!("\"magic\" mismatch at {:#x}", offset); |
| 1110 | true |
| 1111 | } else { |
| 1112 | false |
| 1113 | } |
| 1114 | } else { |
| 1115 | false |
| 1116 | } |
| 1117 | }, |
| 1118 | None => false, |
| 1119 | }; |
| 1120 | |
| 1121 | failed |= match image_ok { |
| 1122 | Some(v) => { |
| 1123 | if (v == 1 && copy[8] != v) || (v == 3 && copy[8] != erased_val) { |
| 1124 | warn!("\"image_ok\" mismatch at {:#x} v={} val={:#x}", offset, v, copy[8]); |
| 1125 | true |
| 1126 | } else { |
| 1127 | false |
| 1128 | } |
| 1129 | }, |
| 1130 | None => false, |
| 1131 | }; |
| 1132 | |
| 1133 | failed |= match copy_done { |
| 1134 | Some(v) => { |
| 1135 | if (v == 1 && copy[0] != v) || (v == 3 && copy[0] != erased_val) { |
| 1136 | warn!("\"copy_done\" mismatch at {:#x} v={} val={:#x}", offset, v, copy[0]); |
| 1137 | true |
| 1138 | } else { |
| 1139 | false |
| 1140 | } |
| 1141 | }, |
| 1142 | None => false, |
| 1143 | }; |
| 1144 | |
| 1145 | !failed |
| 1146 | } |
| 1147 | |
| 1148 | /// The image header |
| 1149 | #[repr(C)] |
| 1150 | pub struct ImageHeader { |
| 1151 | magic: u32, |
| 1152 | load_addr: u32, |
| 1153 | hdr_size: u16, |
| 1154 | _pad1: u16, |
| 1155 | img_size: u32, |
| 1156 | flags: u32, |
| 1157 | ver: ImageVersion, |
| 1158 | _pad2: u32, |
| 1159 | } |
| 1160 | |
| 1161 | impl AsRaw for ImageHeader {} |
| 1162 | |
| 1163 | #[repr(C)] |
| 1164 | pub struct ImageVersion { |
| 1165 | major: u8, |
| 1166 | minor: u8, |
| 1167 | revision: u16, |
| 1168 | build_num: u32, |
| 1169 | } |
| 1170 | |
| 1171 | #[derive(Clone)] |
| 1172 | pub struct SlotInfo { |
| 1173 | pub base_off: usize, |
| 1174 | pub trailer_off: usize, |
| 1175 | pub len: usize, |
| 1176 | pub dev_id: u8, |
| 1177 | } |
| 1178 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1179 | const MAGIC: Option<&[u8]> = Some(&[0x77, 0xc2, 0x95, 0xf3, |
| 1180 | 0x60, 0xd2, 0xef, 0x7f, |
| 1181 | 0x35, 0x52, 0x50, 0x0f, |
| 1182 | 0x2c, 0xb6, 0x79, 0x80]); |
| 1183 | |
| 1184 | // Replicates defines found in bootutil.h |
| 1185 | const BOOT_MAGIC_GOOD: Option<u8> = Some(1); |
| 1186 | const BOOT_MAGIC_UNSET: Option<u8> = Some(3); |
| 1187 | |
| 1188 | const BOOT_FLAG_SET: Option<u8> = Some(1); |
| 1189 | const BOOT_FLAG_UNSET: Option<u8> = Some(3); |
| 1190 | |
| 1191 | /// Write out the magic so that the loader tries doing an upgrade. |
| 1192 | pub fn mark_upgrade(flashmap: &mut SimFlashMap, slot: &SlotInfo) { |
| 1193 | let flash = flashmap.get_mut(&slot.dev_id).unwrap(); |
| 1194 | let offset = slot.trailer_off + c::boot_max_align() * 2; |
| 1195 | flash.write(offset, MAGIC.unwrap()).unwrap(); |
| 1196 | } |
| 1197 | |
| 1198 | /// Writes the image_ok flag which, guess what, tells the bootloader |
| 1199 | /// the this image is ok (not a test, and no revert is to be performed). |
| 1200 | fn mark_permanent_upgrade(flashmap: &mut SimFlashMap, slot: &SlotInfo) { |
| 1201 | let flash = flashmap.get_mut(&slot.dev_id).unwrap(); |
| 1202 | let mut ok = [flash.erased_val(); 8]; |
| 1203 | ok[0] = 1u8; |
| 1204 | let off = slot.trailer_off + c::boot_max_align(); |
| 1205 | let align = flash.align(); |
| 1206 | flash.write(off, &ok[..align]).unwrap(); |
| 1207 | } |
| 1208 | |
| 1209 | // Drop some pseudo-random gibberish onto the data. |
| 1210 | fn splat(data: &mut [u8], seed: usize) { |
| 1211 | let seed_block = [0x135782ea, 0x92184728, data.len() as u32, seed as u32]; |
| 1212 | let mut rng: XorShiftRng = SeedableRng::from_seed(seed_block); |
| 1213 | rng.fill_bytes(data); |
| 1214 | } |
| 1215 | |
| 1216 | /// Return a read-only view into the raw bytes of this object |
| 1217 | trait AsRaw : Sized { |
| 1218 | fn as_raw<'a>(&'a self) -> &'a [u8] { |
| 1219 | unsafe { slice::from_raw_parts(self as *const _ as *const u8, |
| 1220 | mem::size_of::<Self>()) } |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | pub fn show_sizes() { |
| 1225 | // This isn't panic safe. |
| 1226 | for min in &[1, 2, 4, 8] { |
| 1227 | let msize = c::boot_trailer_sz(*min); |
| 1228 | println!("{:2}: {} (0x{:x})", min, msize, msize); |
| 1229 | } |
| 1230 | } |