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