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