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