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