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