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