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