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