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