David Brown | 297029a | 2019-08-13 14:29:51 -0600 | [diff] [blame] | 1 | use byteorder::{ |
| 2 | LittleEndian, WriteBytesExt, |
| 3 | }; |
| 4 | use log::{ |
| 5 | Level::Info, |
| 6 | error, |
| 7 | info, |
| 8 | log_enabled, |
| 9 | warn, |
| 10 | }; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 11 | use rand::{ |
| 12 | distributions::{IndependentSample, Range}, |
| 13 | Rng, SeedableRng, XorShiftRng, |
| 14 | }; |
| 15 | use std::{ |
David Brown | 297029a | 2019-08-13 14:29:51 -0600 | [diff] [blame] | 16 | collections::HashSet, |
David Brown | cb47dd7 | 2019-08-05 14:21:49 -0600 | [diff] [blame] | 17 | io::{Cursor, Write}, |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 18 | mem, |
| 19 | slice, |
| 20 | }; |
| 21 | use aes_ctr::{ |
| 22 | Aes128Ctr, |
| 23 | stream_cipher::{ |
| 24 | generic_array::GenericArray, |
| 25 | NewFixStreamCipher, |
| 26 | StreamCipherCore, |
| 27 | }, |
| 28 | }; |
| 29 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 30 | use simflash::{Flash, SimFlash, SimMultiFlash}; |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 31 | use mcuboot_sys::{c, AreaDesc, FlashId}; |
| 32 | use crate::{ |
| 33 | ALL_DEVICES, |
| 34 | DeviceName, |
| 35 | }; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 36 | use crate::caps::Caps; |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 37 | use crate::depends::{ |
| 38 | BoringDep, |
| 39 | Depender, |
| 40 | DepTest, |
David Brown | 873be31 | 2019-09-03 12:22:32 -0600 | [diff] [blame] | 41 | DepType, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 42 | PairDep, |
| 43 | UpgradeInfo, |
| 44 | }; |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 45 | use crate::tlv::{ManifestGen, TlvGen, TlvFlags}; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 46 | |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 47 | /// A builder for Images. This describes a single run of the simulator, |
| 48 | /// capturing the configuration of a particular set of devices, including |
| 49 | /// the flash simulator(s) and the information about the slots. |
| 50 | #[derive(Clone)] |
| 51 | pub struct ImagesBuilder { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 52 | flash: SimMultiFlash, |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 53 | areadesc: AreaDesc, |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 54 | slots: Vec<[SlotInfo; 2]>, |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 55 | } |
| 56 | |
David Brown | 998aa8d | 2019-02-28 10:54:50 -0700 | [diff] [blame] | 57 | /// Images represents the state of a simulation for a given set of images. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 58 | /// The flash holds the state of the simulated flash, whereas primaries |
David Brown | 998aa8d | 2019-02-28 10:54:50 -0700 | [diff] [blame] | 59 | /// and upgrades hold the expected contents of these images. |
| 60 | pub struct Images { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 61 | flash: SimMultiFlash, |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame] | 62 | areadesc: AreaDesc, |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 63 | images: Vec<OneImage>, |
| 64 | total_count: Option<i32>, |
| 65 | } |
| 66 | |
| 67 | /// When doing multi-image, there is an instance of this information for |
| 68 | /// each of the images. Single image there will be one of these. |
| 69 | struct OneImage { |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame] | 70 | slots: [SlotInfo; 2], |
| 71 | primaries: ImageData, |
| 72 | upgrades: ImageData, |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /// The Rust-side representation of an image. For unencrypted images, this |
| 76 | /// is just the unencrypted payload. For encrypted images, we store both |
| 77 | /// the encrypted and the plaintext. |
| 78 | struct ImageData { |
| 79 | plain: Vec<u8>, |
| 80 | cipher: Option<Vec<u8>>, |
David Brown | 998aa8d | 2019-02-28 10:54:50 -0700 | [diff] [blame] | 81 | } |
| 82 | |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 83 | impl ImagesBuilder { |
David Brown | 5bc62c6 | 2019-03-05 12:11:48 -0700 | [diff] [blame] | 84 | /// Construct a new image builder for the given device. Returns |
| 85 | /// Some(builder) if is possible to test this configuration, or None if |
| 86 | /// not possible (for example, if there aren't enough image slots). |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 87 | pub fn new(device: DeviceName, align: usize, erased_val: u8) -> Option<Self> { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 88 | let (flash, areadesc) = Self::make_device(device, align, erased_val); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 89 | |
David Brown | 06ef06e | 2019-03-05 12:28:10 -0700 | [diff] [blame] | 90 | let num_images = Caps::get_num_images(); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 91 | |
David Brown | 06ef06e | 2019-03-05 12:28:10 -0700 | [diff] [blame] | 92 | let mut slots = Vec::with_capacity(num_images); |
| 93 | for image in 0..num_images { |
| 94 | // This mapping must match that defined in |
| 95 | // `boot/zephyr/include/sysflash/sysflash.h`. |
| 96 | let id0 = match image { |
| 97 | 0 => FlashId::Image0, |
| 98 | 1 => FlashId::Image2, |
| 99 | _ => panic!("More than 2 images not supported"), |
| 100 | }; |
| 101 | let (primary_base, primary_len, primary_dev_id) = match areadesc.find(id0) { |
| 102 | Some(info) => info, |
| 103 | None => return None, |
| 104 | }; |
| 105 | let id1 = match image { |
| 106 | 0 => FlashId::Image1, |
| 107 | 1 => FlashId::Image3, |
| 108 | _ => panic!("More than 2 images not supported"), |
| 109 | }; |
| 110 | let (secondary_base, secondary_len, secondary_dev_id) = match areadesc.find(id1) { |
| 111 | Some(info) => info, |
| 112 | None => return None, |
| 113 | }; |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 114 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 115 | let offset_from_end = c::boot_magic_sz() + c::boot_max_align() * 4; |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 116 | |
David Brown | 06ef06e | 2019-03-05 12:28:10 -0700 | [diff] [blame] | 117 | // Construct a primary image. |
| 118 | let primary = SlotInfo { |
| 119 | base_off: primary_base as usize, |
| 120 | trailer_off: primary_base + primary_len - offset_from_end, |
| 121 | len: primary_len as usize, |
| 122 | dev_id: primary_dev_id, |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 123 | index: 0, |
David Brown | 06ef06e | 2019-03-05 12:28:10 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | // And an upgrade image. |
| 127 | let secondary = SlotInfo { |
| 128 | base_off: secondary_base as usize, |
| 129 | trailer_off: secondary_base + secondary_len - offset_from_end, |
| 130 | len: secondary_len as usize, |
| 131 | dev_id: secondary_dev_id, |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 132 | index: 1, |
David Brown | 06ef06e | 2019-03-05 12:28:10 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | slots.push([primary, secondary]); |
| 136 | } |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 137 | |
David Brown | 5bc62c6 | 2019-03-05 12:11:48 -0700 | [diff] [blame] | 138 | Some(ImagesBuilder { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 139 | flash: flash, |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 140 | areadesc: areadesc, |
David Brown | 06ef06e | 2019-03-05 12:28:10 -0700 | [diff] [blame] | 141 | slots: slots, |
David Brown | 5bc62c6 | 2019-03-05 12:11:48 -0700 | [diff] [blame] | 142 | }) |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | pub fn each_device<F>(f: F) |
| 146 | where F: Fn(Self) |
| 147 | { |
| 148 | for &dev in ALL_DEVICES { |
David Brown | 95de450 | 2019-11-15 12:01:34 -0700 | [diff] [blame] | 149 | for &align in test_alignments() { |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 150 | for &erased_val in &[0, 0xff] { |
David Brown | 5bc62c6 | 2019-03-05 12:11:48 -0700 | [diff] [blame] | 151 | match Self::new(dev, align, erased_val) { |
| 152 | Some(run) => f(run), |
| 153 | None => warn!("Skipping {:?}, insufficient partitions", dev), |
| 154 | } |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /// Construct an `Images` that doesn't expect an upgrade to happen. |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 161 | pub fn make_no_upgrade_image(self, deps: &DepTest) -> Images { |
| 162 | let num_images = self.num_images(); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 163 | let mut flash = self.flash; |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 164 | let images = self.slots.into_iter().enumerate().map(|(image_num, slots)| { |
| 165 | let dep: Box<dyn Depender> = if num_images > 1 { |
| 166 | Box::new(PairDep::new(num_images, image_num, deps)) |
| 167 | } else { |
| 168 | Box::new(BoringDep(image_num)) |
| 169 | }; |
| 170 | let primaries = install_image(&mut flash, &slots[0], 42784, &*dep, false); |
David Brown | 873be31 | 2019-09-03 12:22:32 -0600 | [diff] [blame] | 171 | let upgrades = match deps.depends[image_num] { |
| 172 | DepType::NoUpgrade => install_no_image(), |
| 173 | _ => install_image(&mut flash, &slots[1], 46928, &*dep, false) |
| 174 | }; |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 175 | OneImage { |
| 176 | slots: slots, |
| 177 | primaries: primaries, |
| 178 | upgrades: upgrades, |
| 179 | }}).collect(); |
David Brown | 297029a | 2019-08-13 14:29:51 -0600 | [diff] [blame] | 180 | install_ptable(&mut flash, &self.areadesc); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 181 | Images { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 182 | flash: flash, |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 183 | areadesc: self.areadesc, |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 184 | images: images, |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 185 | total_count: None, |
| 186 | } |
| 187 | } |
| 188 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 189 | pub fn make_image(self, deps: &DepTest, permanent: bool) -> Images { |
| 190 | let mut images = self.make_no_upgrade_image(deps); |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 191 | for image in &images.images { |
| 192 | mark_upgrade(&mut images.flash, &image.slots[1]); |
| 193 | } |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 194 | |
| 195 | // upgrades without fails, counts number of flash operations |
Fabio Utzig | ed4a536 | 2019-07-30 12:43:23 -0300 | [diff] [blame] | 196 | let total_count = match images.run_basic_upgrade(permanent) { |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 197 | Ok(v) => v, |
Fabio Utzig | 7c1d155 | 2019-08-28 10:59:22 -0300 | [diff] [blame] | 198 | Err(_) => |
David Brown | 0e6bc7f | 2019-09-03 12:29:56 -0600 | [diff] [blame] | 199 | if deps.upgrades.iter().any(|u| *u == UpgradeInfo::Held) { |
| 200 | 0 |
| 201 | } else { |
| 202 | panic!("Unable to perform basic upgrade"); |
| 203 | } |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | images.total_count = Some(total_count); |
| 207 | images |
| 208 | } |
| 209 | |
| 210 | pub fn make_bad_secondary_slot_image(self) -> Images { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 211 | let mut bad_flash = self.flash; |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 212 | let images = self.slots.into_iter().enumerate().map(|(image_num, slots)| { |
| 213 | let dep = BoringDep(image_num); |
| 214 | let primaries = install_image(&mut bad_flash, &slots[0], 32784, &dep, false); |
| 215 | let upgrades = install_image(&mut bad_flash, &slots[1], 41928, &dep, true); |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 216 | OneImage { |
| 217 | slots: slots, |
| 218 | primaries: primaries, |
| 219 | upgrades: upgrades, |
| 220 | }}).collect(); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 221 | Images { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 222 | flash: bad_flash, |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 223 | areadesc: self.areadesc, |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 224 | images: images, |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 225 | total_count: None, |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /// Build the Flash and area descriptor for a given device. |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 230 | pub fn make_device(device: DeviceName, align: usize, erased_val: u8) -> (SimMultiFlash, AreaDesc) { |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 231 | match device { |
| 232 | DeviceName::Stm32f4 => { |
| 233 | // STM style flash. Large sectors, with a large scratch area. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 234 | let dev = SimFlash::new(vec![16 * 1024, 16 * 1024, 16 * 1024, 16 * 1024, |
| 235 | 64 * 1024, |
| 236 | 128 * 1024, 128 * 1024, 128 * 1024], |
| 237 | align as usize, erased_val); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 238 | let dev_id = 0; |
| 239 | let mut areadesc = AreaDesc::new(); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 240 | areadesc.add_flash_sectors(dev_id, &dev); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 241 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 242 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 243 | areadesc.add_image(0x060000, 0x020000, FlashId::ImageScratch, dev_id); |
| 244 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 245 | let mut flash = SimMultiFlash::new(); |
| 246 | flash.insert(dev_id, dev); |
| 247 | (flash, areadesc) |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 248 | } |
| 249 | DeviceName::K64f => { |
| 250 | // NXP style flash. Small sectors, one small sector for scratch. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 251 | let dev = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 252 | |
| 253 | let dev_id = 0; |
| 254 | let mut areadesc = AreaDesc::new(); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 255 | areadesc.add_flash_sectors(dev_id, &dev); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 256 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 257 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 258 | areadesc.add_image(0x060000, 0x001000, FlashId::ImageScratch, dev_id); |
| 259 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 260 | let mut flash = SimMultiFlash::new(); |
| 261 | flash.insert(dev_id, dev); |
| 262 | (flash, areadesc) |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 263 | } |
| 264 | DeviceName::K64fBig => { |
| 265 | // Simulating an STM style flash on top of an NXP style flash. Underlying flash device |
| 266 | // uses small sectors, but we tell the bootloader they are large. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 267 | let dev = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 268 | |
| 269 | let dev_id = 0; |
| 270 | let mut areadesc = AreaDesc::new(); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 271 | areadesc.add_flash_sectors(dev_id, &dev); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 272 | areadesc.add_simple_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 273 | areadesc.add_simple_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 274 | areadesc.add_simple_image(0x060000, 0x020000, FlashId::ImageScratch, dev_id); |
| 275 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 276 | let mut flash = SimMultiFlash::new(); |
| 277 | flash.insert(dev_id, dev); |
| 278 | (flash, areadesc) |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 279 | } |
| 280 | DeviceName::Nrf52840 => { |
| 281 | // Simulating the flash on the nrf52840 with partitions set up so that the scratch size |
| 282 | // does not divide into the image size. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 283 | let dev = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 284 | |
| 285 | let dev_id = 0; |
| 286 | let mut areadesc = AreaDesc::new(); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 287 | areadesc.add_flash_sectors(dev_id, &dev); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 288 | areadesc.add_image(0x008000, 0x034000, FlashId::Image0, dev_id); |
| 289 | areadesc.add_image(0x03c000, 0x034000, FlashId::Image1, dev_id); |
| 290 | areadesc.add_image(0x070000, 0x00d000, FlashId::ImageScratch, dev_id); |
| 291 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 292 | let mut flash = SimMultiFlash::new(); |
| 293 | flash.insert(dev_id, dev); |
| 294 | (flash, areadesc) |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 295 | } |
| 296 | DeviceName::Nrf52840SpiFlash => { |
| 297 | // Simulate nrf52840 with external SPI flash. The external SPI flash |
| 298 | // has a larger sector size so for now store scratch on that flash. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 299 | let dev0 = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
| 300 | let dev1 = SimFlash::new(vec![8192; 64], align as usize, erased_val); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 301 | |
| 302 | let mut areadesc = AreaDesc::new(); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 303 | areadesc.add_flash_sectors(0, &dev0); |
| 304 | areadesc.add_flash_sectors(1, &dev1); |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 305 | |
| 306 | areadesc.add_image(0x008000, 0x068000, FlashId::Image0, 0); |
| 307 | areadesc.add_image(0x000000, 0x068000, FlashId::Image1, 1); |
| 308 | areadesc.add_image(0x068000, 0x018000, FlashId::ImageScratch, 1); |
| 309 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 310 | let mut flash = SimMultiFlash::new(); |
| 311 | flash.insert(0, dev0); |
| 312 | flash.insert(1, dev1); |
| 313 | (flash, areadesc) |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 314 | } |
David Brown | 2bff647 | 2019-03-05 13:58:35 -0700 | [diff] [blame] | 315 | DeviceName::K64fMulti => { |
| 316 | // NXP style flash, but larger, to support multiple images. |
| 317 | let dev = SimFlash::new(vec![4096; 256], align as usize, erased_val); |
| 318 | |
| 319 | let dev_id = 0; |
| 320 | let mut areadesc = AreaDesc::new(); |
| 321 | areadesc.add_flash_sectors(dev_id, &dev); |
| 322 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 323 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 324 | areadesc.add_image(0x060000, 0x001000, FlashId::ImageScratch, dev_id); |
| 325 | areadesc.add_image(0x080000, 0x020000, FlashId::Image2, dev_id); |
| 326 | areadesc.add_image(0x0a0000, 0x020000, FlashId::Image3, dev_id); |
| 327 | |
| 328 | let mut flash = SimMultiFlash::new(); |
| 329 | flash.insert(dev_id, dev); |
| 330 | (flash, areadesc) |
| 331 | } |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 334 | |
| 335 | pub fn num_images(&self) -> usize { |
| 336 | self.slots.len() |
| 337 | } |
David Brown | e513324 | 2019-02-28 11:05:19 -0700 | [diff] [blame] | 338 | } |
| 339 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 340 | impl Images { |
| 341 | /// A simple upgrade without forced failures. |
| 342 | /// |
| 343 | /// Returns the number of flash operations which can later be used to |
| 344 | /// inject failures at chosen steps. |
Fabio Utzig | ed4a536 | 2019-07-30 12:43:23 -0300 | [diff] [blame] | 345 | pub fn run_basic_upgrade(&self, permanent: bool) -> Result<i32, ()> { |
| 346 | let (flash, total_count) = self.try_upgrade(None, permanent); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 347 | info!("Total flash operation count={}", total_count); |
| 348 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 349 | if !self.verify_images(&flash, 0, 1) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 350 | warn!("Image mismatch after first boot"); |
| 351 | Err(()) |
| 352 | } else { |
| 353 | Ok(total_count) |
| 354 | } |
| 355 | } |
| 356 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 357 | /// Test a simple upgrade, with dependencies given, and verify that the |
| 358 | /// image does as is described in the test. |
| 359 | pub fn run_check_deps(&self, deps: &DepTest) -> bool { |
| 360 | let (flash, _) = self.try_upgrade(None, true); |
| 361 | |
| 362 | self.verify_dep_images(&flash, deps) |
| 363 | } |
| 364 | |
Fabio Utzig | f5480c7 | 2019-11-28 10:41:57 -0300 | [diff] [blame^] | 365 | fn is_swap_upgrade(&self) -> bool { |
| 366 | Caps::SwapUsingScratch.present() || Caps::SwapUsingMove.present() |
| 367 | } |
| 368 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 369 | pub fn run_basic_revert(&self) -> bool { |
David Brown | 3910ab1 | 2019-01-11 12:02:26 -0700 | [diff] [blame] | 370 | if Caps::OverwriteUpgrade.present() { |
| 371 | return false; |
| 372 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 373 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 374 | let mut fails = 0; |
| 375 | |
| 376 | // FIXME: this test would also pass if no swap is ever performed??? |
Fabio Utzig | f5480c7 | 2019-11-28 10:41:57 -0300 | [diff] [blame^] | 377 | if self.is_swap_upgrade() { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 378 | for count in 2 .. 5 { |
| 379 | info!("Try revert: {}", count); |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 380 | let flash = self.try_revert(count); |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 381 | if !self.verify_images(&flash, 0, 0) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 382 | error!("Revert failure on count {}", count); |
| 383 | fails += 1; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | fails > 0 |
| 389 | } |
| 390 | |
| 391 | pub fn run_perm_with_fails(&self) -> bool { |
| 392 | let mut fails = 0; |
| 393 | let total_flash_ops = self.total_count.unwrap(); |
| 394 | |
| 395 | // Let's try an image halfway through. |
| 396 | for i in 1 .. total_flash_ops { |
| 397 | info!("Try interruption at {}", i); |
Fabio Utzig | ed4a536 | 2019-07-30 12:43:23 -0300 | [diff] [blame] | 398 | let (flash, count) = self.try_upgrade(Some(i), true); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 399 | info!("Second boot, count={}", count); |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 400 | if !self.verify_images(&flash, 0, 1) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 401 | warn!("FAIL at step {} of {}", i, total_flash_ops); |
| 402 | fails += 1; |
| 403 | } |
| 404 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 405 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 406 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 407 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 408 | fails += 1; |
| 409 | } |
| 410 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 411 | if !self.verify_trailers(&flash, 1, BOOT_MAGIC_UNSET, |
| 412 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 413 | warn!("Mismatched trailer for the secondary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 414 | fails += 1; |
| 415 | } |
| 416 | |
Fabio Utzig | f5480c7 | 2019-11-28 10:41:57 -0300 | [diff] [blame^] | 417 | if self.is_swap_upgrade() { |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 418 | if !self.verify_images(&flash, 1, 0) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 419 | warn!("Secondary slot FAIL at step {} of {}", |
| 420 | i, total_flash_ops); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 421 | fails += 1; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if fails > 0 { |
| 427 | error!("{} out of {} failed {:.2}%", fails, total_flash_ops, |
| 428 | fails as f32 * 100.0 / total_flash_ops as f32); |
| 429 | } |
| 430 | |
| 431 | fails > 0 |
| 432 | } |
| 433 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 434 | pub fn run_perm_with_random_fails(&self, total_fails: usize) -> bool { |
| 435 | let mut fails = 0; |
| 436 | let total_flash_ops = self.total_count.unwrap(); |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 437 | let (flash, total_counts) = self.try_random_fails(total_flash_ops, total_fails); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 438 | info!("Random interruptions at reset points={:?}", total_counts); |
| 439 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 440 | let primary_slot_ok = self.verify_images(&flash, 0, 1); |
Fabio Utzig | f5480c7 | 2019-11-28 10:41:57 -0300 | [diff] [blame^] | 441 | let secondary_slot_ok = if self.is_swap_upgrade() { |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 442 | // TODO: This result is ignored. |
| 443 | self.verify_images(&flash, 1, 0) |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 444 | } else { |
| 445 | true |
| 446 | }; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 447 | if !primary_slot_ok || !secondary_slot_ok { |
| 448 | error!("Image mismatch after random interrupts: primary slot={} \ |
| 449 | secondary slot={}", |
| 450 | if primary_slot_ok { "ok" } else { "fail" }, |
| 451 | if secondary_slot_ok { "ok" } else { "fail" }); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 452 | fails += 1; |
| 453 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 454 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 455 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 456 | error!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 457 | fails += 1; |
| 458 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 459 | if !self.verify_trailers(&flash, 1, BOOT_MAGIC_UNSET, |
| 460 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 461 | error!("Mismatched trailer for the secondary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 462 | fails += 1; |
| 463 | } |
| 464 | |
| 465 | if fails > 0 { |
| 466 | error!("Error testing perm upgrade with {} fails", total_fails); |
| 467 | } |
| 468 | |
| 469 | fails > 0 |
| 470 | } |
| 471 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 472 | pub fn run_revert_with_fails(&self) -> bool { |
David Brown | 3910ab1 | 2019-01-11 12:02:26 -0700 | [diff] [blame] | 473 | if Caps::OverwriteUpgrade.present() { |
| 474 | return false; |
| 475 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 476 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 477 | let mut fails = 0; |
| 478 | |
Fabio Utzig | f5480c7 | 2019-11-28 10:41:57 -0300 | [diff] [blame^] | 479 | if self.is_swap_upgrade() { |
Fabio Utzig | ed4a536 | 2019-07-30 12:43:23 -0300 | [diff] [blame] | 480 | for i in 1 .. self.total_count.unwrap() { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 481 | info!("Try interruption at {}", i); |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 482 | if self.try_revert_with_fail_at(i) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 483 | error!("Revert failed at interruption {}", i); |
| 484 | fails += 1; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | fails > 0 |
| 490 | } |
| 491 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 492 | pub fn run_norevert(&self) -> bool { |
David Brown | 3910ab1 | 2019-01-11 12:02:26 -0700 | [diff] [blame] | 493 | if Caps::OverwriteUpgrade.present() { |
| 494 | return false; |
| 495 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 496 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 497 | let mut flash = self.flash.clone(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 498 | let mut fails = 0; |
| 499 | |
| 500 | info!("Try norevert"); |
| 501 | |
| 502 | // First do a normal upgrade... |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 503 | let (result, _) = c::boot_go(&mut flash, &self.areadesc, None, false); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 504 | if result != 0 { |
| 505 | warn!("Failed first boot"); |
| 506 | fails += 1; |
| 507 | } |
| 508 | |
| 509 | //FIXME: copy_done is written by boot_go, is it ok if no copy |
| 510 | // was ever done? |
| 511 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 512 | if !self.verify_images(&flash, 0, 1) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 513 | warn!("Primary slot image verification FAIL"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 514 | fails += 1; |
| 515 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 516 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 517 | BOOT_FLAG_UNSET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 518 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 519 | fails += 1; |
| 520 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 521 | if !self.verify_trailers(&flash, 1, BOOT_MAGIC_UNSET, |
| 522 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 523 | warn!("Mismatched trailer for the secondary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 524 | fails += 1; |
| 525 | } |
| 526 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 527 | // Marks image in the primary slot as permanent, |
| 528 | // no revert should happen... |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 529 | self.mark_permanent_upgrades(&mut flash, 0); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 530 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 531 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 532 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 533 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 534 | fails += 1; |
| 535 | } |
| 536 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 537 | let (result, _) = c::boot_go(&mut flash, &self.areadesc, None, false); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 538 | if result != 0 { |
| 539 | warn!("Failed second boot"); |
| 540 | fails += 1; |
| 541 | } |
| 542 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 543 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 544 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 545 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 546 | fails += 1; |
| 547 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 548 | if !self.verify_images(&flash, 0, 1) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 549 | warn!("Failed image verification"); |
| 550 | fails += 1; |
| 551 | } |
| 552 | |
| 553 | if fails > 0 { |
| 554 | error!("Error running upgrade without revert"); |
| 555 | } |
| 556 | |
| 557 | fails > 0 |
| 558 | } |
| 559 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 560 | // Tests a new image written to the primary slot that already has magic and |
| 561 | // image_ok set while there is no image on the secondary slot, so no revert |
| 562 | // should ever happen... |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 563 | pub fn run_norevert_newimage(&self) -> bool { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 564 | let mut flash = self.flash.clone(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 565 | let mut fails = 0; |
| 566 | |
| 567 | info!("Try non-revert on imgtool generated image"); |
| 568 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 569 | self.mark_upgrades(&mut flash, 0); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 570 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 571 | // This simulates writing an image created by imgtool to |
| 572 | // the primary slot |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 573 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 574 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 575 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 576 | fails += 1; |
| 577 | } |
| 578 | |
| 579 | // Run the bootloader... |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 580 | let (result, _) = c::boot_go(&mut flash, &self.areadesc, None, false); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 581 | if result != 0 { |
| 582 | warn!("Failed first boot"); |
| 583 | fails += 1; |
| 584 | } |
| 585 | |
| 586 | // State should not have changed |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 587 | if !self.verify_images(&flash, 0, 0) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 588 | warn!("Failed image verification"); |
| 589 | fails += 1; |
| 590 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 591 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 592 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 593 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 594 | fails += 1; |
| 595 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 596 | if !self.verify_trailers(&flash, 1, BOOT_MAGIC_UNSET, |
| 597 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 598 | warn!("Mismatched trailer for the secondary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 599 | fails += 1; |
| 600 | } |
| 601 | |
| 602 | if fails > 0 { |
| 603 | error!("Expected a non revert with new image"); |
| 604 | } |
| 605 | |
| 606 | fails > 0 |
| 607 | } |
| 608 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 609 | // Tests a new image written to the primary slot that already has magic and |
| 610 | // image_ok set while there is no image on the secondary slot, so no revert |
| 611 | // should ever happen... |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 612 | pub fn run_signfail_upgrade(&self) -> bool { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 613 | let mut flash = self.flash.clone(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 614 | let mut fails = 0; |
| 615 | |
| 616 | info!("Try upgrade image with bad signature"); |
| 617 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 618 | self.mark_upgrades(&mut flash, 0); |
| 619 | self.mark_permanent_upgrades(&mut flash, 0); |
| 620 | self.mark_upgrades(&mut flash, 1); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 621 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 622 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 623 | BOOT_FLAG_SET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 624 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 625 | fails += 1; |
| 626 | } |
| 627 | |
| 628 | // Run the bootloader... |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 629 | let (result, _) = c::boot_go(&mut flash, &self.areadesc, None, false); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 630 | if result != 0 { |
| 631 | warn!("Failed first boot"); |
| 632 | fails += 1; |
| 633 | } |
| 634 | |
| 635 | // State should not have changed |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 636 | if !self.verify_images(&flash, 0, 0) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 637 | warn!("Failed image verification"); |
| 638 | fails += 1; |
| 639 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 640 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 641 | BOOT_FLAG_SET, BOOT_FLAG_UNSET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 642 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 643 | fails += 1; |
| 644 | } |
| 645 | |
| 646 | if fails > 0 { |
| 647 | error!("Expected an upgrade failure when image has bad signature"); |
| 648 | } |
| 649 | |
| 650 | fails > 0 |
| 651 | } |
| 652 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 653 | fn trailer_sz(&self, align: usize) -> usize { |
| 654 | c::boot_trailer_sz(align as u8) as usize |
| 655 | } |
| 656 | |
| 657 | // FIXME: could get status sz from bootloader |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 658 | fn status_sz(&self, align: usize) -> usize { |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 659 | let bias = if Caps::EncRsa.present() || Caps::EncKw.present() || |
| 660 | Caps::EncEc256.present() { |
David Brown | 9930a3e | 2019-01-11 12:28:26 -0700 | [diff] [blame] | 661 | 32 |
| 662 | } else { |
| 663 | 0 |
| 664 | }; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 665 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 666 | self.trailer_sz(align) - (16 + 32 + bias) |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /// This test runs a simple upgrade with no fails in the images, but |
| 670 | /// allowing for fails in the status area. This should run to the end |
| 671 | /// and warn that write fails were detected... |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 672 | pub fn run_with_status_fails_complete(&self) -> bool { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 673 | if !Caps::ValidatePrimarySlot.present() { |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 677 | let mut flash = self.flash.clone(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 678 | let mut fails = 0; |
| 679 | |
| 680 | info!("Try swap with status fails"); |
| 681 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 682 | self.mark_permanent_upgrades(&mut flash, 1); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 683 | self.mark_bad_status_with_rate(&mut flash, 0, 1.0); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 684 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 685 | let (result, asserts) = c::boot_go(&mut flash, &self.areadesc, None, true); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 686 | if result != 0 { |
| 687 | warn!("Failed!"); |
| 688 | fails += 1; |
| 689 | } |
| 690 | |
| 691 | // Failed writes to the marked "bad" region don't assert anymore. |
| 692 | // Any detected assert() is happening in another part of the code. |
| 693 | if asserts != 0 { |
| 694 | warn!("At least one assert() was called"); |
| 695 | fails += 1; |
| 696 | } |
| 697 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 698 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 699 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 700 | warn!("Mismatched trailer for the primary slot"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 701 | fails += 1; |
| 702 | } |
| 703 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 704 | if !self.verify_images(&flash, 0, 1) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 705 | warn!("Failed image verification"); |
| 706 | fails += 1; |
| 707 | } |
| 708 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 709 | info!("validate primary slot enabled; \ |
| 710 | re-run of boot_go should just work"); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 711 | let (result, _) = c::boot_go(&mut flash, &self.areadesc, None, false); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 712 | if result != 0 { |
| 713 | warn!("Failed!"); |
| 714 | fails += 1; |
| 715 | } |
| 716 | |
| 717 | if fails > 0 { |
| 718 | error!("Error running upgrade with status write fails"); |
| 719 | } |
| 720 | |
| 721 | fails > 0 |
| 722 | } |
| 723 | |
| 724 | /// This test runs a simple upgrade with no fails in the images, but |
| 725 | /// allowing for fails in the status area. This should run to the end |
| 726 | /// and warn that write fails were detected... |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 727 | pub fn run_with_status_fails_with_reset(&self) -> bool { |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 728 | if Caps::OverwriteUpgrade.present() { |
| 729 | false |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 730 | } else if Caps::ValidatePrimarySlot.present() { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 731 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 732 | let mut flash = self.flash.clone(); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 733 | let mut fails = 0; |
| 734 | let mut count = self.total_count.unwrap() / 2; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 735 | |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 736 | //info!("count={}\n", count); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 737 | |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 738 | info!("Try interrupted swap with status fails"); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 739 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 740 | self.mark_permanent_upgrades(&mut flash, 1); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 741 | self.mark_bad_status_with_rate(&mut flash, 0, 0.5); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 742 | |
| 743 | // Should not fail, writing to bad regions does not assert |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 744 | let (_, asserts) = c::boot_go(&mut flash, &self.areadesc, Some(&mut count), true); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 745 | if asserts != 0 { |
| 746 | warn!("At least one assert() was called"); |
| 747 | fails += 1; |
| 748 | } |
| 749 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 750 | self.reset_bad_status(&mut flash, 0); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 751 | |
| 752 | info!("Resuming an interrupted swap operation"); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 753 | let (_, asserts) = c::boot_go(&mut flash, &self.areadesc, None, true); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 754 | |
| 755 | // This might throw no asserts, for large sector devices, where |
| 756 | // a single failure writing is indistinguishable from no failure, |
| 757 | // or throw a single assert for small sector devices that fail |
| 758 | // multiple times... |
| 759 | if asserts > 1 { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 760 | warn!("Expected single assert validating the primary slot, \ |
| 761 | more detected {}", asserts); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 762 | fails += 1; |
| 763 | } |
| 764 | |
| 765 | if fails > 0 { |
| 766 | error!("Error running upgrade with status write fails"); |
| 767 | } |
| 768 | |
| 769 | fails > 0 |
| 770 | } else { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 771 | let mut flash = self.flash.clone(); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 772 | let mut fails = 0; |
| 773 | |
| 774 | info!("Try interrupted swap with status fails"); |
| 775 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 776 | self.mark_permanent_upgrades(&mut flash, 1); |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 777 | self.mark_bad_status_with_rate(&mut flash, 0, 1.0); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 778 | |
| 779 | // This is expected to fail while writing to bad regions... |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 780 | let (_, asserts) = c::boot_go(&mut flash, &self.areadesc, None, true); |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 781 | if asserts == 0 { |
| 782 | warn!("No assert() detected"); |
| 783 | fails += 1; |
| 784 | } |
| 785 | |
| 786 | fails > 0 |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 787 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /// Adds a new flash area that fails statistically |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 791 | fn mark_bad_status_with_rate(&self, flash: &mut SimMultiFlash, slot: usize, |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 792 | rate: f32) { |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 793 | if Caps::OverwriteUpgrade.present() { |
| 794 | return; |
| 795 | } |
| 796 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 797 | // Set this for each image. |
| 798 | for image in &self.images { |
| 799 | let dev_id = &image.slots[slot].dev_id; |
| 800 | let dev = flash.get_mut(&dev_id).unwrap(); |
| 801 | let align = dev.align(); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 802 | let off = &image.slots[slot].base_off; |
| 803 | let len = &image.slots[slot].len; |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 804 | let status_off = off + len - self.trailer_sz(align); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 805 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 806 | // Mark the status area as a bad area |
| 807 | let _ = dev.add_bad_region(status_off, self.status_sz(align), rate); |
| 808 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 809 | } |
| 810 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 811 | fn reset_bad_status(&self, flash: &mut SimMultiFlash, slot: usize) { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 812 | if !Caps::ValidatePrimarySlot.present() { |
David Brown | 85904a8 | 2019-01-11 13:45:12 -0700 | [diff] [blame] | 813 | return; |
| 814 | } |
| 815 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 816 | for image in &self.images { |
| 817 | let dev_id = &image.slots[slot].dev_id; |
| 818 | let dev = flash.get_mut(&dev_id).unwrap(); |
| 819 | dev.reset_bad_regions(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 820 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 821 | // Disabling write verification the only assert triggered by |
| 822 | // boot_go should be checking for integrity of status bytes. |
| 823 | dev.set_verify_writes(false); |
| 824 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 825 | } |
| 826 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 827 | /// Test a boot, optionally stopping after 'n' flash options. Returns a count |
| 828 | /// of the number of flash operations done total. |
Fabio Utzig | ed4a536 | 2019-07-30 12:43:23 -0300 | [diff] [blame] | 829 | fn try_upgrade(&self, stop: Option<i32>, permanent: bool) -> (SimMultiFlash, i32) { |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 830 | // Clone the flash to have a new copy. |
| 831 | let mut flash = self.flash.clone(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 832 | |
Fabio Utzig | ed4a536 | 2019-07-30 12:43:23 -0300 | [diff] [blame] | 833 | if permanent { |
| 834 | self.mark_permanent_upgrades(&mut flash, 1); |
| 835 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 836 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 837 | let mut counter = stop.unwrap_or(0); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 838 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 839 | let (first_interrupted, count) = match c::boot_go(&mut flash, &self.areadesc, Some(&mut counter), false) { |
| 840 | (-0x13579, _) => (true, stop.unwrap()), |
| 841 | (0, _) => (false, -counter), |
| 842 | (x, _) => panic!("Unknown return: {}", x), |
| 843 | }; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 844 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 845 | counter = 0; |
| 846 | if first_interrupted { |
| 847 | // fl.dump(); |
| 848 | match c::boot_go(&mut flash, &self.areadesc, Some(&mut counter), false) { |
| 849 | (-0x13579, _) => panic!("Shouldn't stop again"), |
| 850 | (0, _) => (), |
| 851 | (x, _) => panic!("Unknown return: {}", x), |
| 852 | } |
| 853 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 854 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 855 | (flash, count - counter) |
| 856 | } |
| 857 | |
| 858 | fn try_revert(&self, count: usize) -> SimMultiFlash { |
| 859 | let mut flash = self.flash.clone(); |
| 860 | |
| 861 | // fl.write_file("image0.bin").unwrap(); |
| 862 | for i in 0 .. count { |
| 863 | info!("Running boot pass {}", i + 1); |
| 864 | assert_eq!(c::boot_go(&mut flash, &self.areadesc, None, false), (0, 0)); |
| 865 | } |
| 866 | flash |
| 867 | } |
| 868 | |
| 869 | fn try_revert_with_fail_at(&self, stop: i32) -> bool { |
| 870 | let mut flash = self.flash.clone(); |
| 871 | let mut fails = 0; |
| 872 | |
| 873 | let mut counter = stop; |
| 874 | let (x, _) = c::boot_go(&mut flash, &self.areadesc, Some(&mut counter), false); |
| 875 | if x != -0x13579 { |
Fabio Utzig | fc07eab | 2019-05-17 10:23:38 -0700 | [diff] [blame] | 876 | warn!("Should have stopped test at interruption point"); |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 877 | fails += 1; |
| 878 | } |
| 879 | |
Fabio Utzig | 8af7f79 | 2019-07-30 12:40:01 -0300 | [diff] [blame] | 880 | // In a multi-image setup, copy done might be set if any number of |
| 881 | // images was already successfully swapped. |
| 882 | if !self.verify_trailers_loose(&flash, 0, None, None, BOOT_FLAG_UNSET) { |
| 883 | warn!("copy_done should be unset"); |
| 884 | fails += 1; |
| 885 | } |
| 886 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 887 | let (x, _) = c::boot_go(&mut flash, &self.areadesc, None, false); |
| 888 | if x != 0 { |
Fabio Utzig | fc07eab | 2019-05-17 10:23:38 -0700 | [diff] [blame] | 889 | warn!("Should have finished test upgrade"); |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 890 | fails += 1; |
| 891 | } |
| 892 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 893 | if !self.verify_images(&flash, 0, 1) { |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 894 | warn!("Image in the primary slot before revert is invalid at stop={}", |
| 895 | stop); |
| 896 | fails += 1; |
| 897 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 898 | if !self.verify_images(&flash, 1, 0) { |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 899 | warn!("Image in the secondary slot before revert is invalid at stop={}", |
| 900 | stop); |
| 901 | fails += 1; |
| 902 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 903 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 904 | BOOT_FLAG_UNSET, BOOT_FLAG_SET) { |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 905 | warn!("Mismatched trailer for the primary slot before revert"); |
| 906 | fails += 1; |
| 907 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 908 | if !self.verify_trailers(&flash, 1, BOOT_MAGIC_UNSET, |
| 909 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 910 | warn!("Mismatched trailer for the secondary slot before revert"); |
| 911 | fails += 1; |
| 912 | } |
| 913 | |
| 914 | // Do Revert |
Fabio Utzig | fc07eab | 2019-05-17 10:23:38 -0700 | [diff] [blame] | 915 | let mut counter = stop; |
| 916 | let (x, _) = c::boot_go(&mut flash, &self.areadesc, Some(&mut counter), false); |
| 917 | if x != -0x13579 { |
| 918 | warn!("Should have stopped revert at interruption point"); |
| 919 | fails += 1; |
| 920 | } |
| 921 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 922 | let (x, _) = c::boot_go(&mut flash, &self.areadesc, None, false); |
| 923 | if x != 0 { |
Fabio Utzig | fc07eab | 2019-05-17 10:23:38 -0700 | [diff] [blame] | 924 | warn!("Should have finished revert upgrade"); |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 925 | fails += 1; |
| 926 | } |
| 927 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 928 | if !self.verify_images(&flash, 0, 0) { |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 929 | warn!("Image in the primary slot after revert is invalid at stop={}", |
| 930 | stop); |
| 931 | fails += 1; |
| 932 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 933 | if !self.verify_images(&flash, 1, 1) { |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 934 | warn!("Image in the secondary slot after revert is invalid at stop={}", |
| 935 | stop); |
| 936 | fails += 1; |
| 937 | } |
Fabio Utzig | fc07eab | 2019-05-17 10:23:38 -0700 | [diff] [blame] | 938 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 939 | if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD, |
| 940 | BOOT_FLAG_SET, BOOT_FLAG_SET) { |
Fabio Utzig | fc07eab | 2019-05-17 10:23:38 -0700 | [diff] [blame] | 941 | warn!("Mismatched trailer for the primary slot after revert"); |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 942 | fails += 1; |
| 943 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 944 | if !self.verify_trailers(&flash, 1, BOOT_MAGIC_UNSET, |
| 945 | BOOT_FLAG_UNSET, BOOT_FLAG_UNSET) { |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 946 | warn!("Mismatched trailer for the secondary slot after revert"); |
| 947 | fails += 1; |
| 948 | } |
| 949 | |
Fabio Utzig | fc07eab | 2019-05-17 10:23:38 -0700 | [diff] [blame] | 950 | let (x, _) = c::boot_go(&mut flash, &self.areadesc, None, false); |
| 951 | if x != 0 { |
| 952 | warn!("Should have finished 3rd boot"); |
| 953 | fails += 1; |
| 954 | } |
| 955 | |
| 956 | if !self.verify_images(&flash, 0, 0) { |
| 957 | warn!("Image in the primary slot is invalid on 1st boot after revert"); |
| 958 | fails += 1; |
| 959 | } |
| 960 | if !self.verify_images(&flash, 1, 1) { |
| 961 | warn!("Image in the secondary slot is invalid on 1st boot after revert"); |
| 962 | fails += 1; |
| 963 | } |
| 964 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 965 | fails > 0 |
| 966 | } |
| 967 | |
Fabio Utzig | fc07eab | 2019-05-17 10:23:38 -0700 | [diff] [blame] | 968 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 969 | fn try_random_fails(&self, total_ops: i32, count: usize) -> (SimMultiFlash, Vec<i32>) { |
| 970 | let mut flash = self.flash.clone(); |
| 971 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 972 | self.mark_permanent_upgrades(&mut flash, 1); |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 973 | |
| 974 | let mut rng = rand::thread_rng(); |
| 975 | let mut resets = vec![0i32; count]; |
| 976 | let mut remaining_ops = total_ops; |
| 977 | for i in 0 .. count { |
| 978 | let ops = Range::new(1, remaining_ops / 2); |
| 979 | let reset_counter = ops.ind_sample(&mut rng); |
| 980 | let mut counter = reset_counter; |
| 981 | match c::boot_go(&mut flash, &self.areadesc, Some(&mut counter), false) { |
| 982 | (0, _) | (-0x13579, _) => (), |
| 983 | (x, _) => panic!("Unknown return: {}", x), |
| 984 | } |
| 985 | remaining_ops -= reset_counter; |
| 986 | resets[i] = reset_counter; |
| 987 | } |
| 988 | |
| 989 | match c::boot_go(&mut flash, &self.areadesc, None, false) { |
| 990 | (-0x13579, _) => panic!("Should not be have been interrupted!"), |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 991 | (0, _) => (), |
| 992 | (x, _) => panic!("Unknown return: {}", x), |
| 993 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 994 | |
David Brown | db50582 | 2019-03-01 10:04:20 -0700 | [diff] [blame] | 995 | (flash, resets) |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 996 | } |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 997 | |
| 998 | /// Verify the image in the given flash device, the specified slot |
| 999 | /// against the expected image. |
| 1000 | fn verify_images(&self, flash: &SimMultiFlash, slot: usize, against: usize) -> bool { |
David Brown | f9aec95 | 2019-08-06 10:23:58 -0600 | [diff] [blame] | 1001 | self.images.iter().all(|image| { |
| 1002 | verify_image(flash, &image.slots[slot], |
| 1003 | match against { |
| 1004 | 0 => &image.primaries, |
| 1005 | 1 => &image.upgrades, |
| 1006 | _ => panic!("Invalid 'against'") |
| 1007 | }) |
| 1008 | }) |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 1011 | /// Verify the images, according to the dependency test. |
| 1012 | fn verify_dep_images(&self, flash: &SimMultiFlash, deps: &DepTest) -> bool { |
| 1013 | for (image_num, (image, upgrade)) in self.images.iter().zip(deps.upgrades.iter()).enumerate() { |
| 1014 | info!("Upgrade: slot:{}, {:?}", image_num, upgrade); |
| 1015 | if !verify_image(flash, &image.slots[0], |
| 1016 | match upgrade { |
| 1017 | UpgradeInfo::Upgraded => &image.upgrades, |
| 1018 | UpgradeInfo::Held => &image.primaries, |
| 1019 | }) { |
| 1020 | error!("Failed to upgrade properly: image: {}, upgrade: {:?}", image_num, upgrade); |
| 1021 | return true; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | false |
| 1026 | } |
| 1027 | |
Fabio Utzig | 8af7f79 | 2019-07-30 12:40:01 -0300 | [diff] [blame] | 1028 | /// Verify that at least one of the trailers of the images have the |
| 1029 | /// specified values. |
| 1030 | fn verify_trailers_loose(&self, flash: &SimMultiFlash, slot: usize, |
| 1031 | magic: Option<u8>, image_ok: Option<u8>, |
| 1032 | copy_done: Option<u8>) -> bool { |
David Brown | f9aec95 | 2019-08-06 10:23:58 -0600 | [diff] [blame] | 1033 | self.images.iter().any(|image| { |
| 1034 | verify_trailer(flash, &image.slots[slot], |
| 1035 | magic, image_ok, copy_done) |
| 1036 | }) |
Fabio Utzig | 8af7f79 | 2019-07-30 12:40:01 -0300 | [diff] [blame] | 1037 | } |
| 1038 | |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 1039 | /// Verify that the trailers of the images have the specified |
| 1040 | /// values. |
| 1041 | fn verify_trailers(&self, flash: &SimMultiFlash, slot: usize, |
| 1042 | magic: Option<u8>, image_ok: Option<u8>, |
| 1043 | copy_done: Option<u8>) -> bool { |
David Brown | f9aec95 | 2019-08-06 10:23:58 -0600 | [diff] [blame] | 1044 | self.images.iter().all(|image| { |
| 1045 | verify_trailer(flash, &image.slots[slot], |
| 1046 | magic, image_ok, copy_done) |
| 1047 | }) |
David Brown | 84b49f7 | 2019-03-01 10:58:22 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /// Mark each of the images for permanent upgrade. |
| 1051 | fn mark_permanent_upgrades(&self, flash: &mut SimMultiFlash, slot: usize) { |
| 1052 | for image in &self.images { |
| 1053 | mark_permanent_upgrade(flash, &image.slots[slot]); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | /// Mark each of the images for permanent upgrade. |
| 1058 | fn mark_upgrades(&self, flash: &mut SimMultiFlash, slot: usize) { |
| 1059 | for image in &self.images { |
| 1060 | mark_upgrade(flash, &image.slots[slot]); |
| 1061 | } |
| 1062 | } |
David Brown | 297029a | 2019-08-13 14:29:51 -0600 | [diff] [blame] | 1063 | |
| 1064 | /// Dump out the flash image(s) to one or more files for debugging |
| 1065 | /// purposes. The names will be written as either "{prefix}.mcubin" or |
| 1066 | /// "{prefix}-001.mcubin" depending on how many images there are. |
| 1067 | pub fn debug_dump(&self, prefix: &str) { |
| 1068 | for (id, fdev) in &self.flash { |
| 1069 | let name = if self.flash.len() == 1 { |
| 1070 | format!("{}.mcubin", prefix) |
| 1071 | } else { |
| 1072 | format!("{}-{:>0}.mcubin", prefix, id) |
| 1073 | }; |
| 1074 | fdev.write_file(&name).unwrap(); |
| 1075 | } |
| 1076 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /// Show the flash layout. |
| 1080 | #[allow(dead_code)] |
| 1081 | fn show_flash(flash: &dyn Flash) { |
| 1082 | println!("---- Flash configuration ----"); |
| 1083 | for sector in flash.sector_iter() { |
| 1084 | println!(" {:3}: 0x{:08x}, 0x{:08x}", |
| 1085 | sector.num, sector.base, sector.size); |
| 1086 | } |
| 1087 | println!(""); |
| 1088 | } |
| 1089 | |
| 1090 | /// Install a "program" into the given image. This fakes the image header, or at least all of the |
| 1091 | /// fields used by the given code. Returns a copy of the image that was written. |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 1092 | fn install_image(flash: &mut SimMultiFlash, slot: &SlotInfo, len: usize, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 1093 | deps: &dyn Depender, bad_sig: bool) -> ImageData { |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 1094 | let offset = slot.base_off; |
| 1095 | let slot_len = slot.len; |
| 1096 | let dev_id = slot.dev_id; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1097 | |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 1098 | let mut tlv: Box<dyn ManifestGen> = Box::new(make_tlv()); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1099 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 1100 | // Add the dependencies early to the tlv. |
| 1101 | for dep in deps.my_deps(offset, slot.index) { |
| 1102 | tlv.add_dependency(deps.other_id(), &dep); |
| 1103 | } |
| 1104 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1105 | const HDR_SIZE: usize = 32; |
| 1106 | |
| 1107 | // Generate a boot header. Note that the size doesn't include the header. |
| 1108 | let header = ImageHeader { |
David Brown | ac46e26 | 2019-01-11 15:46:18 -0700 | [diff] [blame] | 1109 | magic: tlv.get_magic(), |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1110 | load_addr: 0, |
| 1111 | hdr_size: HDR_SIZE as u16, |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 1112 | protect_tlv_size: tlv.protect_size(), |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1113 | img_size: len as u32, |
| 1114 | flags: tlv.get_flags(), |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 1115 | ver: deps.my_version(offset, slot.index), |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1116 | _pad2: 0, |
| 1117 | }; |
| 1118 | |
| 1119 | let mut b_header = [0; HDR_SIZE]; |
| 1120 | b_header[..32].clone_from_slice(header.as_raw()); |
| 1121 | assert_eq!(b_header.len(), HDR_SIZE); |
| 1122 | |
| 1123 | tlv.add_bytes(&b_header); |
| 1124 | |
| 1125 | // The core of the image itself is just pseudorandom data. |
| 1126 | let mut b_img = vec![0; len]; |
| 1127 | splat(&mut b_img, offset); |
| 1128 | |
David Brown | cb47dd7 | 2019-08-05 14:21:49 -0600 | [diff] [blame] | 1129 | // Add some information at the start of the payload to make it easier |
| 1130 | // to see what it is. This will fail if the image itself is too small. |
| 1131 | { |
| 1132 | let mut wr = Cursor::new(&mut b_img); |
| 1133 | writeln!(&mut wr, "offset: {:#x}, dev_id: {:#x}, slot_info: {:?}", |
| 1134 | offset, dev_id, slot).unwrap(); |
| 1135 | writeln!(&mut wr, "version: {:?}", deps.my_version(offset, slot.index)).unwrap(); |
| 1136 | } |
| 1137 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1138 | // TLV signatures work over plain image |
| 1139 | tlv.add_bytes(&b_img); |
| 1140 | |
| 1141 | // Generate encrypted images |
| 1142 | let flag = TlvFlags::ENCRYPTED as u32; |
| 1143 | let is_encrypted = (tlv.get_flags() & flag) == flag; |
| 1144 | let mut b_encimg = vec![]; |
| 1145 | if is_encrypted { |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 1146 | tlv.generate_enc_key(); |
| 1147 | let enc_key = tlv.get_enc_key(); |
| 1148 | let key = GenericArray::from_slice(enc_key.as_slice()); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1149 | let nonce = GenericArray::from_slice(&[0; 16]); |
| 1150 | let mut cipher = Aes128Ctr::new(&key, &nonce); |
| 1151 | b_encimg = b_img.clone(); |
| 1152 | cipher.apply_keystream(&mut b_encimg); |
| 1153 | } |
| 1154 | |
| 1155 | // Build the TLV itself. |
David Brown | e90b13f | 2019-12-06 15:04:00 -0700 | [diff] [blame] | 1156 | if bad_sig { |
| 1157 | tlv.corrupt_sig(); |
| 1158 | } |
| 1159 | let mut b_tlv = tlv.make_tlv(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1160 | |
Fabio Utzig | 2f6c164 | 2019-09-11 19:36:30 -0300 | [diff] [blame] | 1161 | let dev = flash.get_mut(&dev_id).unwrap(); |
| 1162 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1163 | let mut buf = vec![]; |
| 1164 | buf.append(&mut b_header.to_vec()); |
| 1165 | buf.append(&mut b_img); |
| 1166 | buf.append(&mut b_tlv.clone()); |
| 1167 | |
David Brown | 95de450 | 2019-11-15 12:01:34 -0700 | [diff] [blame] | 1168 | // Pad the buffer to a multiple of the flash alignment. |
| 1169 | let align = dev.align(); |
| 1170 | while buf.len() % align != 0 { |
| 1171 | buf.push(dev.erased_val()); |
| 1172 | } |
| 1173 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1174 | let mut encbuf = vec![]; |
| 1175 | if is_encrypted { |
| 1176 | encbuf.append(&mut b_header.to_vec()); |
| 1177 | encbuf.append(&mut b_encimg); |
| 1178 | encbuf.append(&mut b_tlv); |
David Brown | 95de450 | 2019-11-15 12:01:34 -0700 | [diff] [blame] | 1179 | |
| 1180 | while encbuf.len() % align != 0 { |
| 1181 | encbuf.push(dev.erased_val()); |
| 1182 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 1185 | // Since images are always non-encrypted in the primary slot, we first write |
| 1186 | // an encrypted image, re-read to use for verification, erase + flash |
| 1187 | // un-encrypted. In the secondary slot the image is written un-encrypted, |
| 1188 | // and if encryption is requested, it follows an erase + flash encrypted. |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1189 | |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 1190 | if slot.index == 0 { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1191 | let enc_copy: Option<Vec<u8>>; |
| 1192 | |
| 1193 | if is_encrypted { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1194 | dev.write(offset, &encbuf).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1195 | |
| 1196 | let mut enc = vec![0u8; encbuf.len()]; |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1197 | dev.read(offset, &mut enc).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1198 | |
| 1199 | enc_copy = Some(enc); |
| 1200 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1201 | dev.erase(offset, slot_len).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1202 | } else { |
| 1203 | enc_copy = None; |
| 1204 | } |
| 1205 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1206 | dev.write(offset, &buf).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1207 | |
| 1208 | let mut copy = vec![0u8; buf.len()]; |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1209 | dev.read(offset, &mut copy).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1210 | |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame] | 1211 | ImageData { |
| 1212 | plain: copy, |
| 1213 | cipher: enc_copy, |
| 1214 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1215 | } else { |
| 1216 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1217 | dev.write(offset, &buf).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1218 | |
| 1219 | let mut copy = vec![0u8; buf.len()]; |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1220 | dev.read(offset, &mut copy).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1221 | |
| 1222 | let enc_copy: Option<Vec<u8>>; |
| 1223 | |
| 1224 | if is_encrypted { |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1225 | dev.erase(offset, slot_len).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1226 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1227 | dev.write(offset, &encbuf).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1228 | |
| 1229 | let mut enc = vec![0u8; encbuf.len()]; |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1230 | dev.read(offset, &mut enc).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1231 | |
| 1232 | enc_copy = Some(enc); |
| 1233 | } else { |
| 1234 | enc_copy = None; |
| 1235 | } |
| 1236 | |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame] | 1237 | ImageData { |
| 1238 | plain: copy, |
| 1239 | cipher: enc_copy, |
| 1240 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1241 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
David Brown | 873be31 | 2019-09-03 12:22:32 -0600 | [diff] [blame] | 1244 | /// Install no image. This is used when no upgrade happens. |
| 1245 | fn install_no_image() -> ImageData { |
| 1246 | ImageData { |
| 1247 | plain: vec![], |
| 1248 | cipher: None, |
| 1249 | } |
| 1250 | } |
| 1251 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1252 | fn make_tlv() -> TlvGen { |
David Brown | b888211 | 2019-01-11 14:04:11 -0700 | [diff] [blame] | 1253 | if Caps::EcdsaP224.present() { |
| 1254 | panic!("Ecdsa P224 not supported in Simulator"); |
| 1255 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1256 | |
David Brown | b888211 | 2019-01-11 14:04:11 -0700 | [diff] [blame] | 1257 | if Caps::EncKw.present() { |
| 1258 | if Caps::RSA2048.present() { |
| 1259 | TlvGen::new_rsa_kw() |
| 1260 | } else if Caps::EcdsaP256.present() { |
| 1261 | TlvGen::new_ecdsa_kw() |
| 1262 | } else { |
| 1263 | TlvGen::new_enc_kw() |
| 1264 | } |
| 1265 | } else if Caps::EncRsa.present() { |
| 1266 | if Caps::RSA2048.present() { |
| 1267 | TlvGen::new_sig_enc_rsa() |
| 1268 | } else { |
| 1269 | TlvGen::new_enc_rsa() |
| 1270 | } |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 1271 | } else if Caps::EncEc256.present() { |
| 1272 | //FIXME: should fail with RSA signature? |
| 1273 | TlvGen::new_ecdsa_ecies_p256() |
David Brown | b888211 | 2019-01-11 14:04:11 -0700 | [diff] [blame] | 1274 | } else { |
| 1275 | // The non-encrypted configuration. |
| 1276 | if Caps::RSA2048.present() { |
| 1277 | TlvGen::new_rsa_pss() |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 1278 | } else if Caps::RSA3072.present() { |
| 1279 | TlvGen::new_rsa3072_pss() |
David Brown | b888211 | 2019-01-11 14:04:11 -0700 | [diff] [blame] | 1280 | } else if Caps::EcdsaP256.present() { |
| 1281 | TlvGen::new_ecdsa() |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 1282 | } else if Caps::Ed25519.present() { |
| 1283 | TlvGen::new_ed25519() |
David Brown | b888211 | 2019-01-11 14:04:11 -0700 | [diff] [blame] | 1284 | } else { |
| 1285 | TlvGen::new_hash_only() |
| 1286 | } |
| 1287 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame] | 1290 | impl ImageData { |
| 1291 | /// Find the image contents for the given slot. This assumes that slot 0 |
| 1292 | /// is unencrypted, and slot 1 is encrypted. |
| 1293 | fn find(&self, slot: usize) -> &Vec<u8> { |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 1294 | let encrypted = Caps::EncRsa.present() || Caps::EncKw.present() || |
| 1295 | Caps::EncEc256.present(); |
David Brown | ca23469 | 2019-02-28 11:22:19 -0700 | [diff] [blame] | 1296 | match (encrypted, slot) { |
| 1297 | (false, _) => &self.plain, |
| 1298 | (true, 0) => &self.plain, |
| 1299 | (true, 1) => self.cipher.as_ref().expect("Invalid image"), |
| 1300 | _ => panic!("Invalid slot requested"), |
| 1301 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1305 | /// Verify that given image is present in the flash at the given offset. |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 1306 | fn verify_image(flash: &SimMultiFlash, slot: &SlotInfo, images: &ImageData) -> bool { |
| 1307 | let image = images.find(slot.index); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1308 | let buf = image.as_slice(); |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 1309 | let dev_id = slot.dev_id; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1310 | |
| 1311 | let mut copy = vec![0u8; buf.len()]; |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 1312 | let offset = slot.base_off; |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1313 | let dev = flash.get(&dev_id).unwrap(); |
| 1314 | dev.read(offset, &mut copy).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1315 | |
| 1316 | if buf != ©[..] { |
| 1317 | for i in 0 .. buf.len() { |
| 1318 | if buf[i] != copy[i] { |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 1319 | info!("First failure for slot{} at {:#x} ({:#x} within) {:#x}!={:#x}", |
| 1320 | slot.index, offset + i, i, buf[i], copy[i]); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1321 | break; |
| 1322 | } |
| 1323 | } |
| 1324 | false |
| 1325 | } else { |
| 1326 | true |
| 1327 | } |
| 1328 | } |
| 1329 | |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 1330 | fn verify_trailer(flash: &SimMultiFlash, slot: &SlotInfo, |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1331 | magic: Option<u8>, image_ok: Option<u8>, |
| 1332 | copy_done: Option<u8>) -> bool { |
David Brown | 61a540d | 2019-01-11 14:29:14 -0700 | [diff] [blame] | 1333 | if Caps::OverwriteUpgrade.present() { |
| 1334 | return true; |
| 1335 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1336 | |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 1337 | let offset = slot.trailer_off + c::boot_max_align(); |
| 1338 | let dev_id = slot.dev_id; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1339 | let mut copy = vec![0u8; c::boot_magic_sz() + c::boot_max_align() * 3]; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1340 | let mut failed = false; |
| 1341 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1342 | let dev = flash.get(&dev_id).unwrap(); |
| 1343 | let erased_val = dev.erased_val(); |
| 1344 | dev.read(offset, &mut copy).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1345 | |
| 1346 | failed |= match magic { |
| 1347 | Some(v) => { |
David Brown | 347dc57 | 2019-11-15 11:37:25 -0700 | [diff] [blame] | 1348 | if v == 1 && ©[24..] != MAGIC { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1349 | warn!("\"magic\" mismatch at {:#x}", offset); |
| 1350 | true |
| 1351 | } else if v == 3 { |
| 1352 | let expected = [erased_val; 16]; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1353 | if ©[24..] != expected { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1354 | warn!("\"magic\" mismatch at {:#x}", offset); |
| 1355 | true |
| 1356 | } else { |
| 1357 | false |
| 1358 | } |
| 1359 | } else { |
| 1360 | false |
| 1361 | } |
| 1362 | }, |
| 1363 | None => false, |
| 1364 | }; |
| 1365 | |
| 1366 | failed |= match image_ok { |
| 1367 | Some(v) => { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1368 | if (v == 1 && copy[16] != v) || (v == 3 && copy[16] != erased_val) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1369 | warn!("\"image_ok\" mismatch at {:#x} v={} val={:#x}", offset, v, copy[8]); |
| 1370 | true |
| 1371 | } else { |
| 1372 | false |
| 1373 | } |
| 1374 | }, |
| 1375 | None => false, |
| 1376 | }; |
| 1377 | |
| 1378 | failed |= match copy_done { |
| 1379 | Some(v) => { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1380 | if (v == 1 && copy[8] != v) || (v == 3 && copy[8] != erased_val) { |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1381 | warn!("\"copy_done\" mismatch at {:#x} v={} val={:#x}", offset, v, copy[0]); |
| 1382 | true |
| 1383 | } else { |
| 1384 | false |
| 1385 | } |
| 1386 | }, |
| 1387 | None => false, |
| 1388 | }; |
| 1389 | |
| 1390 | !failed |
| 1391 | } |
| 1392 | |
David Brown | 297029a | 2019-08-13 14:29:51 -0600 | [diff] [blame] | 1393 | /// Install a partition table. This is a simplified partition table that |
| 1394 | /// we write at the beginning of flash so make it easier for external tools |
| 1395 | /// to analyze these images. |
| 1396 | fn install_ptable(flash: &mut SimMultiFlash, areadesc: &AreaDesc) { |
| 1397 | let ids: HashSet<u8> = areadesc.iter_areas().map(|area| area.device_id).collect(); |
| 1398 | for &id in &ids { |
| 1399 | // If there are any partitions in this device that start at 0, and |
| 1400 | // aren't marked as the BootLoader partition, avoid adding the |
| 1401 | // partition table. This makes it harder to view the image, but |
| 1402 | // avoids messing up images already written. |
| 1403 | if areadesc.iter_areas().any(|area| { |
| 1404 | area.device_id == id && |
| 1405 | area.off == 0 && |
| 1406 | area.flash_id != FlashId::BootLoader |
| 1407 | }) { |
| 1408 | if log_enabled!(Info) { |
| 1409 | let special: Vec<FlashId> = areadesc.iter_areas() |
| 1410 | .filter(|area| area.device_id == id && area.off == 0) |
| 1411 | .map(|area| area.flash_id) |
| 1412 | .collect(); |
| 1413 | info!("Skipping partition table: {:?}", special); |
| 1414 | } |
| 1415 | break; |
| 1416 | } |
| 1417 | |
| 1418 | let mut buf: Vec<u8> = vec![]; |
| 1419 | write!(&mut buf, "mcuboot\0").unwrap(); |
| 1420 | |
| 1421 | // Iterate through all of the partitions in that device, and encode |
| 1422 | // into the table. |
| 1423 | let count = areadesc.iter_areas().filter(|area| area.device_id == id).count(); |
| 1424 | buf.write_u32::<LittleEndian>(count as u32).unwrap(); |
| 1425 | |
| 1426 | for area in areadesc.iter_areas().filter(|area| area.device_id == id) { |
| 1427 | buf.write_u32::<LittleEndian>(area.flash_id as u32).unwrap(); |
| 1428 | buf.write_u32::<LittleEndian>(area.off).unwrap(); |
| 1429 | buf.write_u32::<LittleEndian>(area.size).unwrap(); |
| 1430 | buf.write_u32::<LittleEndian>(0).unwrap(); |
| 1431 | } |
| 1432 | |
| 1433 | let dev = flash.get_mut(&id).unwrap(); |
| 1434 | |
| 1435 | // Pad to alignment. |
| 1436 | while buf.len() % dev.align() != 0 { |
| 1437 | buf.push(0); |
| 1438 | } |
| 1439 | |
| 1440 | dev.write(0, &buf).unwrap(); |
| 1441 | } |
| 1442 | } |
| 1443 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1444 | /// The image header |
| 1445 | #[repr(C)] |
| 1446 | pub struct ImageHeader { |
| 1447 | magic: u32, |
| 1448 | load_addr: u32, |
| 1449 | hdr_size: u16, |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 1450 | protect_tlv_size: u16, |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1451 | img_size: u32, |
| 1452 | flags: u32, |
| 1453 | ver: ImageVersion, |
| 1454 | _pad2: u32, |
| 1455 | } |
| 1456 | |
| 1457 | impl AsRaw for ImageHeader {} |
| 1458 | |
| 1459 | #[repr(C)] |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 1460 | #[derive(Clone, Debug)] |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1461 | pub struct ImageVersion { |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 1462 | pub major: u8, |
| 1463 | pub minor: u8, |
| 1464 | pub revision: u16, |
| 1465 | pub build_num: u32, |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 1468 | #[derive(Clone, Debug)] |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1469 | pub struct SlotInfo { |
| 1470 | pub base_off: usize, |
| 1471 | pub trailer_off: usize, |
| 1472 | pub len: usize, |
David Brown | 3b09021 | 2019-07-30 15:59:28 -0600 | [diff] [blame] | 1473 | // Which slot within this device. |
| 1474 | pub index: usize, |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1475 | pub dev_id: u8, |
| 1476 | } |
| 1477 | |
David Brown | 347dc57 | 2019-11-15 11:37:25 -0700 | [diff] [blame] | 1478 | const MAGIC: &[u8] = &[0x77, 0xc2, 0x95, 0xf3, |
| 1479 | 0x60, 0xd2, 0xef, 0x7f, |
| 1480 | 0x35, 0x52, 0x50, 0x0f, |
| 1481 | 0x2c, 0xb6, 0x79, 0x80]; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1482 | |
| 1483 | // Replicates defines found in bootutil.h |
| 1484 | const BOOT_MAGIC_GOOD: Option<u8> = Some(1); |
| 1485 | const BOOT_MAGIC_UNSET: Option<u8> = Some(3); |
| 1486 | |
| 1487 | const BOOT_FLAG_SET: Option<u8> = Some(1); |
| 1488 | const BOOT_FLAG_UNSET: Option<u8> = Some(3); |
| 1489 | |
| 1490 | /// Write out the magic so that the loader tries doing an upgrade. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1491 | pub fn mark_upgrade(flash: &mut SimMultiFlash, slot: &SlotInfo) { |
| 1492 | let dev = flash.get_mut(&slot.dev_id).unwrap(); |
David Brown | 95de450 | 2019-11-15 12:01:34 -0700 | [diff] [blame] | 1493 | let align = dev.align(); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1494 | let offset = slot.trailer_off + c::boot_max_align() * 4; |
David Brown | 95de450 | 2019-11-15 12:01:34 -0700 | [diff] [blame] | 1495 | if offset % align != 0 || MAGIC.len() % align != 0 { |
| 1496 | // The write size is larger than the magic value. Fill a buffer |
| 1497 | // with the erased value, put the MAGIC in it, and write it in its |
| 1498 | // entirety. |
| 1499 | let mut buf = vec![dev.erased_val(); align]; |
| 1500 | buf[(offset % align)..].copy_from_slice(MAGIC); |
| 1501 | dev.write(offset - (offset % align), &buf).unwrap(); |
| 1502 | } else { |
| 1503 | dev.write(offset, MAGIC).unwrap(); |
| 1504 | } |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | /// Writes the image_ok flag which, guess what, tells the bootloader |
| 1508 | /// the this image is ok (not a test, and no revert is to be performed). |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1509 | fn mark_permanent_upgrade(flash: &mut SimMultiFlash, slot: &SlotInfo) { |
David Brown | eecae52 | 2019-11-15 12:00:20 -0700 | [diff] [blame] | 1510 | // Overwrite mode always is permanent, and only the magic is used in |
| 1511 | // the trailer. To avoid problems with large write sizes, don't try to |
| 1512 | // set anything in this case. |
| 1513 | if Caps::OverwriteUpgrade.present() { |
| 1514 | return; |
| 1515 | } |
| 1516 | |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1517 | let dev = flash.get_mut(&slot.dev_id).unwrap(); |
| 1518 | let mut ok = [dev.erased_val(); 8]; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1519 | ok[0] = 1u8; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 1520 | let off = slot.trailer_off + c::boot_max_align() * 3; |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 1521 | let align = dev.align(); |
| 1522 | dev.write(off, &ok[..align]).unwrap(); |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | // Drop some pseudo-random gibberish onto the data. |
| 1526 | fn splat(data: &mut [u8], seed: usize) { |
| 1527 | let seed_block = [0x135782ea, 0x92184728, data.len() as u32, seed as u32]; |
| 1528 | let mut rng: XorShiftRng = SeedableRng::from_seed(seed_block); |
| 1529 | rng.fill_bytes(data); |
| 1530 | } |
| 1531 | |
| 1532 | /// Return a read-only view into the raw bytes of this object |
| 1533 | trait AsRaw : Sized { |
| 1534 | fn as_raw<'a>(&'a self) -> &'a [u8] { |
| 1535 | unsafe { slice::from_raw_parts(self as *const _ as *const u8, |
| 1536 | mem::size_of::<Self>()) } |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | pub fn show_sizes() { |
| 1541 | // This isn't panic safe. |
| 1542 | for min in &[1, 2, 4, 8] { |
| 1543 | let msize = c::boot_trailer_sz(*min); |
| 1544 | println!("{:2}: {} (0x{:x})", min, msize, msize); |
| 1545 | } |
| 1546 | } |
David Brown | 95de450 | 2019-11-15 12:01:34 -0700 | [diff] [blame] | 1547 | |
| 1548 | #[cfg(not(feature = "large-write"))] |
| 1549 | fn test_alignments() -> &'static [usize] { |
David Brown | 95de450 | 2019-11-15 12:01:34 -0700 | [diff] [blame] | 1550 | &[1, 2, 4, 8] |
| 1551 | } |
| 1552 | |
| 1553 | #[cfg(feature = "large-write")] |
| 1554 | fn test_alignments() -> &'static [usize] { |
David Brown | 95de450 | 2019-11-15 12:01:34 -0700 | [diff] [blame] | 1555 | &[1, 2, 4, 8, 128, 512] |
| 1556 | } |