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