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