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