David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 1 | use docopt::Docopt; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 2 | use log::{warn, error}; |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 3 | use std::{ |
| 4 | fmt, |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 5 | process, |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 6 | }; |
| 7 | use serde_derive::Deserialize; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 8 | |
| 9 | mod caps; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 10 | mod image; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 11 | mod tlv; |
David Brown | ca7b5d3 | 2017-11-03 08:37:38 -0600 | [diff] [blame] | 12 | pub mod testlog; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 13 | |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 14 | use simflash::{SimFlash, SimFlashMap}; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 15 | use mcuboot_sys::{c, AreaDesc, FlashId}; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 16 | |
| 17 | use crate::image::{ |
| 18 | Images, |
| 19 | install_image, |
| 20 | mark_upgrade, |
| 21 | SlotInfo, |
| 22 | show_sizes, |
| 23 | }; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 24 | |
| 25 | const USAGE: &'static str = " |
| 26 | Mcuboot simulator |
| 27 | |
| 28 | Usage: |
| 29 | bootsim sizes |
| 30 | bootsim run --device TYPE [--align SIZE] |
| 31 | bootsim runall |
| 32 | bootsim (--help | --version) |
| 33 | |
| 34 | Options: |
| 35 | -h, --help Show this message |
| 36 | --version Version |
| 37 | --device TYPE MCU to simulate |
| 38 | Valid values: stm32f4, k64f |
| 39 | --align SIZE Flash write alignment |
| 40 | "; |
| 41 | |
| 42 | #[derive(Debug, Deserialize)] |
| 43 | struct Args { |
| 44 | flag_help: bool, |
| 45 | flag_version: bool, |
| 46 | flag_device: Option<DeviceName>, |
| 47 | flag_align: Option<AlignArg>, |
| 48 | cmd_sizes: bool, |
| 49 | cmd_run: bool, |
| 50 | cmd_runall: bool, |
| 51 | } |
| 52 | |
| 53 | #[derive(Copy, Clone, Debug, Deserialize)] |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 54 | pub enum DeviceName { Stm32f4, K64f, K64fBig, Nrf52840, Nrf52840SpiFlash, } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 55 | |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 56 | pub static ALL_DEVICES: &'static [DeviceName] = &[ |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 57 | DeviceName::Stm32f4, |
| 58 | DeviceName::K64f, |
| 59 | DeviceName::K64fBig, |
| 60 | DeviceName::Nrf52840, |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 61 | DeviceName::Nrf52840SpiFlash, |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 62 | ]; |
| 63 | |
| 64 | impl fmt::Display for DeviceName { |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 65 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 66 | let name = match *self { |
| 67 | DeviceName::Stm32f4 => "stm32f4", |
| 68 | DeviceName::K64f => "k64f", |
| 69 | DeviceName::K64fBig => "k64fbig", |
| 70 | DeviceName::Nrf52840 => "nrf52840", |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 71 | DeviceName::Nrf52840SpiFlash => "Nrf52840SpiFlash", |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 72 | }; |
| 73 | f.write_str(name) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #[derive(Debug)] |
| 78 | struct AlignArg(u8); |
| 79 | |
| 80 | struct AlignArgVisitor; |
| 81 | |
| 82 | impl<'de> serde::de::Visitor<'de> for AlignArgVisitor { |
| 83 | type Value = AlignArg; |
| 84 | |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 85 | fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 86 | formatter.write_str("1, 2, 4 or 8") |
| 87 | } |
| 88 | |
| 89 | fn visit_u8<E>(self, n: u8) -> Result<Self::Value, E> |
| 90 | where E: serde::de::Error |
| 91 | { |
| 92 | Ok(match n { |
| 93 | 1 | 2 | 4 | 8 => AlignArg(n), |
| 94 | n => { |
| 95 | let err = format!("Could not deserialize '{}' as alignment", n); |
| 96 | return Err(E::custom(err)); |
| 97 | } |
| 98 | }) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl<'de> serde::de::Deserialize<'de> for AlignArg { |
| 103 | fn deserialize<D>(d: D) -> Result<AlignArg, D::Error> |
| 104 | where D: serde::de::Deserializer<'de> |
| 105 | { |
| 106 | d.deserialize_u8(AlignArgVisitor) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn main() { |
| 111 | let args: Args = Docopt::new(USAGE) |
| 112 | .and_then(|d| d.deserialize()) |
| 113 | .unwrap_or_else(|e| e.exit()); |
| 114 | // println!("args: {:#?}", args); |
| 115 | |
| 116 | if args.cmd_sizes { |
| 117 | show_sizes(); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | let mut status = RunStatus::new(); |
| 122 | if args.cmd_run { |
| 123 | |
| 124 | let align = args.flag_align.map(|x| x.0).unwrap_or(1); |
| 125 | |
| 126 | |
| 127 | let device = match args.flag_device { |
| 128 | None => panic!("Missing mandatory device argument"), |
| 129 | Some(dev) => dev, |
| 130 | }; |
| 131 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 132 | status.run_single(device, align, 0xff); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if args.cmd_runall { |
| 136 | for &dev in ALL_DEVICES { |
| 137 | for &align in &[1, 2, 4, 8] { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 138 | for &erased_val in &[0, 0xff] { |
| 139 | status.run_single(dev, align, erased_val); |
| 140 | } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if status.failures > 0 { |
| 146 | error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures); |
| 147 | process::exit(1); |
| 148 | } else { |
| 149 | error!("{} Tests ran successfully", status.passes); |
| 150 | process::exit(0); |
| 151 | } |
| 152 | } |
| 153 | |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 154 | /// A test run, intended to be run from "cargo test", so panics on failure. |
| 155 | pub struct Run { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 156 | flashmap: SimFlashMap, |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 157 | areadesc: AreaDesc, |
| 158 | slots: [SlotInfo; 2], |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | impl Run { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 162 | pub fn new(device: DeviceName, align: u8, erased_val: u8) -> Run { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 163 | let (flashmap, areadesc) = make_device(device, align, erased_val); |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 164 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 165 | let (primary_slot_base, primary_slot_len, primary_slot_dev_id) = |
| 166 | areadesc.find(FlashId::Image0); |
| 167 | let (secondary_slot_base, secondary_slot_len, secondary_slot_dev_id) = |
| 168 | areadesc.find(FlashId::Image1); |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 169 | |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 170 | // NOTE: not accounting "swap_size" because it is not used by sim... |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 171 | let offset_from_end = c::boot_magic_sz() + c::boot_max_align() * 2; |
| 172 | |
| 173 | // Construct a primary image. |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 174 | let primary_slot = SlotInfo { |
| 175 | base_off: primary_slot_base as usize, |
| 176 | trailer_off: primary_slot_base + primary_slot_len - offset_from_end, |
| 177 | len: primary_slot_len as usize, |
| 178 | dev_id: primary_slot_dev_id, |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | // And an upgrade image. |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 182 | let secondary_slot = SlotInfo { |
| 183 | base_off: secondary_slot_base as usize, |
| 184 | trailer_off: secondary_slot_base + secondary_slot_len - offset_from_end, |
| 185 | len: secondary_slot_len as usize, |
| 186 | dev_id: secondary_slot_dev_id, |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | Run { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 190 | flashmap: flashmap, |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 191 | areadesc: areadesc, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 192 | slots: [primary_slot, secondary_slot], |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | pub fn each_device<F>(f: F) |
| 197 | where F: Fn(&mut Run) |
| 198 | { |
| 199 | for &dev in ALL_DEVICES { |
| 200 | for &align in &[1, 2, 4, 8] { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 201 | for &erased_val in &[0, 0xff] { |
| 202 | let mut run = Run::new(dev, align, erased_val); |
| 203 | f(&mut run); |
| 204 | } |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 208 | |
| 209 | /// Construct an `Images` that doesn't expect an upgrade to happen. |
| 210 | pub fn make_no_upgrade_image(&self) -> Images { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 211 | let mut flashmap = self.flashmap.clone(); |
| 212 | let primaries = install_image(&mut flashmap, &self.slots, 0, 32784, false); |
| 213 | let upgrades = install_image(&mut flashmap, &self.slots, 1, 41928, false); |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 214 | Images { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 215 | flashmap: flashmap, |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 216 | areadesc: self.areadesc.clone(), |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 217 | slots: [self.slots[0].clone(), self.slots[1].clone()], |
| 218 | primaries: primaries, |
| 219 | upgrades: upgrades, |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 220 | total_count: None, |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | /// Construct an `Images` for normal testing. |
| 225 | pub fn make_image(&self) -> Images { |
| 226 | let mut images = self.make_no_upgrade_image(); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 227 | mark_upgrade(&mut images.flashmap, &images.slots[1]); |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 228 | |
| 229 | // upgrades without fails, counts number of flash operations |
| 230 | let total_count = match images.run_basic_upgrade() { |
| 231 | Ok(v) => v, |
| 232 | Err(_) => { |
| 233 | panic!("Unable to perform basic upgrade"); |
| 234 | }, |
| 235 | }; |
| 236 | |
| 237 | images.total_count = Some(total_count); |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 238 | images |
| 239 | } |
| 240 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 241 | pub fn make_bad_secondary_slot_image(&self) -> Images { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 242 | let mut bad_flashmap = self.flashmap.clone(); |
| 243 | let primaries = install_image(&mut bad_flashmap, &self.slots, 0, 32784, false); |
| 244 | let upgrades = install_image(&mut bad_flashmap, &self.slots, 1, 41928, true); |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 245 | Images { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 246 | flashmap: bad_flashmap, |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 247 | areadesc: self.areadesc.clone(), |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 248 | slots: [self.slots[0].clone(), self.slots[1].clone()], |
| 249 | primaries: primaries, |
| 250 | upgrades: upgrades, |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 251 | total_count: None, |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 254 | |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 255 | } |
| 256 | |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 257 | pub struct RunStatus { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 258 | failures: usize, |
| 259 | passes: usize, |
| 260 | } |
| 261 | |
| 262 | impl RunStatus { |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 263 | pub fn new() -> RunStatus { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 264 | RunStatus { |
| 265 | failures: 0, |
| 266 | passes: 0, |
| 267 | } |
| 268 | } |
| 269 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 270 | pub fn run_single(&mut self, device: DeviceName, align: u8, erased_val: u8) { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 271 | warn!("Running on device {} with alignment {}", device, align); |
| 272 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 273 | let run = Run::new(device, align, erased_val); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 274 | |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 275 | let mut failed = false; |
| 276 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 277 | // Creates a badly signed image in the secondary slot to check that |
| 278 | // it is not upgraded to |
| 279 | let bad_secondary_slot_image = run.make_bad_secondary_slot_image(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 280 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 281 | failed |= bad_secondary_slot_image.run_signfail_upgrade(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 282 | |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 283 | let images = run.make_no_upgrade_image(); |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 284 | failed |= images.run_norevert_newimage(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 285 | |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 286 | let images = run.make_image(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 287 | |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 288 | failed |= images.run_basic_revert(); |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 289 | failed |= images.run_revert_with_fails(); |
| 290 | failed |= images.run_perm_with_fails(); |
| 291 | failed |= images.run_perm_with_random_fails(5); |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 292 | failed |= images.run_norevert(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 293 | |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 294 | failed |= images.run_with_status_fails_complete(); |
Fabio Utzig | eedcc45 | 2017-11-24 10:48:52 -0200 | [diff] [blame] | 295 | failed |= images.run_with_status_fails_with_reset(); |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 296 | |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 297 | //show_flash(&flash); |
| 298 | |
| 299 | if failed { |
| 300 | self.failures += 1; |
| 301 | } else { |
| 302 | self.passes += 1; |
| 303 | } |
| 304 | } |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 305 | |
| 306 | pub fn failures(&self) -> usize { |
| 307 | self.failures |
| 308 | } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 309 | } |
| 310 | |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 311 | /// Build the Flash and area descriptor for a given device. |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 312 | pub fn make_device(device: DeviceName, align: u8, erased_val: u8) -> (SimFlashMap, AreaDesc) { |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 313 | match device { |
| 314 | DeviceName::Stm32f4 => { |
| 315 | // STM style flash. Large sectors, with a large scratch area. |
| 316 | let flash = SimFlash::new(vec![16 * 1024, 16 * 1024, 16 * 1024, 16 * 1024, |
| 317 | 64 * 1024, |
| 318 | 128 * 1024, 128 * 1024, 128 * 1024], |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 319 | align as usize, erased_val); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 320 | let dev_id = 0; |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 321 | let mut areadesc = AreaDesc::new(); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 322 | areadesc.add_flash_sectors(dev_id, &flash); |
| 323 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 324 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 325 | areadesc.add_image(0x060000, 0x020000, FlashId::ImageScratch, dev_id); |
| 326 | |
| 327 | let mut flashmap = SimFlashMap::new(); |
| 328 | flashmap.insert(dev_id, flash); |
| 329 | (flashmap, areadesc) |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 330 | } |
| 331 | DeviceName::K64f => { |
| 332 | // NXP style flash. Small sectors, one small sector for scratch. |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 333 | let flash = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 334 | |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 335 | let dev_id = 0; |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 336 | let mut areadesc = AreaDesc::new(); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 337 | areadesc.add_flash_sectors(dev_id, &flash); |
| 338 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 339 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 340 | areadesc.add_image(0x060000, 0x001000, FlashId::ImageScratch, dev_id); |
| 341 | |
| 342 | let mut flashmap = SimFlashMap::new(); |
| 343 | flashmap.insert(dev_id, flash); |
| 344 | (flashmap, areadesc) |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 345 | } |
| 346 | DeviceName::K64fBig => { |
| 347 | // Simulating an STM style flash on top of an NXP style flash. Underlying flash device |
| 348 | // uses small sectors, but we tell the bootloader they are large. |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 349 | let flash = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 350 | |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 351 | let dev_id = 0; |
| 352 | let mut areadesc = AreaDesc::new(); |
| 353 | areadesc.add_flash_sectors(dev_id, &flash); |
| 354 | areadesc.add_simple_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 355 | areadesc.add_simple_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 356 | areadesc.add_simple_image(0x060000, 0x020000, FlashId::ImageScratch, dev_id); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 357 | |
| 358 | let mut flashmap = SimFlashMap::new(); |
| 359 | flashmap.insert(dev_id, flash); |
| 360 | (flashmap, areadesc) |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 361 | } |
| 362 | DeviceName::Nrf52840 => { |
| 363 | // Simulating the flash on the nrf52840 with partitions set up so that the scratch size |
| 364 | // does not divide into the image size. |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 365 | let flash = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 366 | |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 367 | let dev_id = 0; |
| 368 | let mut areadesc = AreaDesc::new(); |
| 369 | areadesc.add_flash_sectors(dev_id, &flash); |
| 370 | areadesc.add_image(0x008000, 0x034000, FlashId::Image0, dev_id); |
| 371 | areadesc.add_image(0x03c000, 0x034000, FlashId::Image1, dev_id); |
| 372 | areadesc.add_image(0x070000, 0x00d000, FlashId::ImageScratch, dev_id); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 373 | |
| 374 | let mut flashmap = SimFlashMap::new(); |
| 375 | flashmap.insert(dev_id, flash); |
| 376 | (flashmap, areadesc) |
| 377 | } |
| 378 | DeviceName::Nrf52840SpiFlash => { |
| 379 | // Simulate nrf52840 with external SPI flash. The external SPI flash |
| 380 | // has a larger sector size so for now store scratch on that flash. |
| 381 | let flash0 = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
Fabio Utzig | 6465077 | 2018-11-19 16:51:13 -0200 | [diff] [blame] | 382 | let flash1 = SimFlash::new(vec![8192; 64], align as usize, erased_val); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 383 | |
| 384 | let mut areadesc = AreaDesc::new(); |
| 385 | areadesc.add_flash_sectors(0, &flash0); |
| 386 | areadesc.add_flash_sectors(1, &flash1); |
| 387 | |
| 388 | areadesc.add_image(0x008000, 0x068000, FlashId::Image0, 0); |
| 389 | areadesc.add_image(0x000000, 0x068000, FlashId::Image1, 1); |
| 390 | areadesc.add_image(0x068000, 0x018000, FlashId::ImageScratch, 1); |
| 391 | |
| 392 | let mut flashmap = SimFlashMap::new(); |
| 393 | flashmap.insert(0, flash0); |
| 394 | flashmap.insert(1, flash1); |
| 395 | (flashmap, areadesc) |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | } |