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