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