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