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 | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 154 | /// A Run describes a single run of the simulator. It captures the |
| 155 | /// configuration of a particular device configuration, including the flash |
| 156 | /// devices and the information about the slots. This can be thought of as |
| 157 | /// a builder for `Images`. |
| 158 | #[derive(Clone)] |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 159 | pub struct Run { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 160 | flashmap: SimFlashMap, |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 161 | areadesc: AreaDesc, |
| 162 | slots: [SlotInfo; 2], |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | impl Run { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 166 | pub fn new(device: DeviceName, align: u8, erased_val: u8) -> Run { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 167 | let (flashmap, areadesc) = make_device(device, align, erased_val); |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 168 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 169 | let (primary_slot_base, primary_slot_len, primary_slot_dev_id) = |
| 170 | areadesc.find(FlashId::Image0); |
| 171 | let (secondary_slot_base, secondary_slot_len, secondary_slot_dev_id) = |
| 172 | areadesc.find(FlashId::Image1); |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 173 | |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 174 | // NOTE: not accounting "swap_size" because it is not used by sim... |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 175 | let offset_from_end = c::boot_magic_sz() + c::boot_max_align() * 2; |
| 176 | |
| 177 | // Construct a primary image. |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 178 | let primary_slot = SlotInfo { |
| 179 | base_off: primary_slot_base as usize, |
| 180 | trailer_off: primary_slot_base + primary_slot_len - offset_from_end, |
| 181 | len: primary_slot_len as usize, |
| 182 | dev_id: primary_slot_dev_id, |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | // And an upgrade image. |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 186 | let secondary_slot = SlotInfo { |
| 187 | base_off: secondary_slot_base as usize, |
| 188 | trailer_off: secondary_slot_base + secondary_slot_len - offset_from_end, |
| 189 | len: secondary_slot_len as usize, |
| 190 | dev_id: secondary_slot_dev_id, |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | Run { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 194 | flashmap: flashmap, |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 195 | areadesc: areadesc, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 196 | slots: [primary_slot, secondary_slot], |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | pub fn each_device<F>(f: F) |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 201 | where F: Fn(Run) |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 202 | { |
| 203 | for &dev in ALL_DEVICES { |
| 204 | for &align in &[1, 2, 4, 8] { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 205 | for &erased_val in &[0, 0xff] { |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 206 | let run = Run::new(dev, align, erased_val); |
| 207 | f(run); |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 208 | } |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 212 | |
| 213 | /// Construct an `Images` that doesn't expect an upgrade to happen. |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 214 | pub fn make_no_upgrade_image(self) -> Images { |
| 215 | let mut flashmap = self.flashmap; |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 216 | let primaries = install_image(&mut flashmap, &self.slots, 0, 32784, false); |
| 217 | let upgrades = install_image(&mut flashmap, &self.slots, 1, 41928, false); |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 218 | Images { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 219 | flashmap: flashmap, |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 220 | areadesc: self.areadesc, |
| 221 | slots: self.slots, |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 222 | primaries: primaries, |
| 223 | upgrades: upgrades, |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 224 | total_count: None, |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | /// Construct an `Images` for normal testing. |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 229 | pub fn make_image(self) -> Images { |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 230 | let mut images = self.make_no_upgrade_image(); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 231 | mark_upgrade(&mut images.flashmap, &images.slots[1]); |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 232 | |
| 233 | // upgrades without fails, counts number of flash operations |
| 234 | let total_count = match images.run_basic_upgrade() { |
| 235 | Ok(v) => v, |
| 236 | Err(_) => { |
| 237 | panic!("Unable to perform basic upgrade"); |
| 238 | }, |
| 239 | }; |
| 240 | |
| 241 | images.total_count = Some(total_count); |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 242 | images |
| 243 | } |
| 244 | |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 245 | pub fn make_bad_secondary_slot_image(self) -> Images { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 246 | let mut bad_flashmap = self.flashmap.clone(); |
| 247 | let primaries = install_image(&mut bad_flashmap, &self.slots, 0, 32784, false); |
| 248 | let upgrades = install_image(&mut bad_flashmap, &self.slots, 1, 41928, true); |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 249 | Images { |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 250 | flashmap: bad_flashmap, |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 251 | areadesc: self.areadesc, |
| 252 | slots: self.slots, |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 253 | primaries: primaries, |
| 254 | upgrades: upgrades, |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 255 | total_count: None, |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 258 | |
David Brown | db9a395 | 2017-11-06 13:16:15 -0700 | [diff] [blame] | 259 | } |
| 260 | |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 261 | pub struct RunStatus { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 262 | failures: usize, |
| 263 | passes: usize, |
| 264 | } |
| 265 | |
| 266 | impl RunStatus { |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 267 | pub fn new() -> RunStatus { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 268 | RunStatus { |
| 269 | failures: 0, |
| 270 | passes: 0, |
| 271 | } |
| 272 | } |
| 273 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 274 | 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] | 275 | warn!("Running on device {} with alignment {}", device, align); |
| 276 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 277 | let run = Run::new(device, align, erased_val); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 278 | |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 279 | let mut failed = false; |
| 280 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 281 | // Creates a badly signed image in the secondary slot to check that |
| 282 | // it is not upgraded to |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 283 | let bad_secondary_slot_image = run.clone().make_bad_secondary_slot_image(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 284 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 285 | failed |= bad_secondary_slot_image.run_signfail_upgrade(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 286 | |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame^] | 287 | let images = run.clone().make_no_upgrade_image(); |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 288 | failed |= images.run_norevert_newimage(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 289 | |
David Brown | f48b950 | 2017-11-06 14:00:26 -0700 | [diff] [blame] | 290 | let images = run.make_image(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 291 | |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 292 | failed |= images.run_basic_revert(); |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 293 | failed |= images.run_revert_with_fails(); |
| 294 | failed |= images.run_perm_with_fails(); |
| 295 | failed |= images.run_perm_with_random_fails(5); |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 296 | failed |= images.run_norevert(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 297 | |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 298 | failed |= images.run_with_status_fails_complete(); |
Fabio Utzig | eedcc45 | 2017-11-24 10:48:52 -0200 | [diff] [blame] | 299 | failed |= images.run_with_status_fails_with_reset(); |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 300 | |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 301 | //show_flash(&flash); |
| 302 | |
| 303 | if failed { |
| 304 | self.failures += 1; |
| 305 | } else { |
| 306 | self.passes += 1; |
| 307 | } |
| 308 | } |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 309 | |
| 310 | pub fn failures(&self) -> usize { |
| 311 | self.failures |
| 312 | } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 313 | } |
| 314 | |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 315 | /// Build the Flash and area descriptor for a given device. |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 316 | 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] | 317 | match device { |
| 318 | DeviceName::Stm32f4 => { |
| 319 | // STM style flash. Large sectors, with a large scratch area. |
| 320 | let flash = SimFlash::new(vec![16 * 1024, 16 * 1024, 16 * 1024, 16 * 1024, |
| 321 | 64 * 1024, |
| 322 | 128 * 1024, 128 * 1024, 128 * 1024], |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 323 | align as usize, erased_val); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 324 | let dev_id = 0; |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 325 | let mut areadesc = AreaDesc::new(); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 326 | areadesc.add_flash_sectors(dev_id, &flash); |
| 327 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 328 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 329 | areadesc.add_image(0x060000, 0x020000, FlashId::ImageScratch, dev_id); |
| 330 | |
| 331 | let mut flashmap = SimFlashMap::new(); |
| 332 | flashmap.insert(dev_id, flash); |
| 333 | (flashmap, areadesc) |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 334 | } |
| 335 | DeviceName::K64f => { |
| 336 | // NXP style flash. Small sectors, one small sector for scratch. |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 337 | let flash = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 338 | |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 339 | let dev_id = 0; |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 340 | let mut areadesc = AreaDesc::new(); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 341 | areadesc.add_flash_sectors(dev_id, &flash); |
| 342 | areadesc.add_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 343 | areadesc.add_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 344 | areadesc.add_image(0x060000, 0x001000, FlashId::ImageScratch, dev_id); |
| 345 | |
| 346 | let mut flashmap = SimFlashMap::new(); |
| 347 | flashmap.insert(dev_id, flash); |
| 348 | (flashmap, areadesc) |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 349 | } |
| 350 | DeviceName::K64fBig => { |
| 351 | // Simulating an STM style flash on top of an NXP style flash. Underlying flash device |
| 352 | // uses small sectors, but we tell the bootloader they are large. |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 353 | let flash = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 354 | |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 355 | let dev_id = 0; |
| 356 | let mut areadesc = AreaDesc::new(); |
| 357 | areadesc.add_flash_sectors(dev_id, &flash); |
| 358 | areadesc.add_simple_image(0x020000, 0x020000, FlashId::Image0, dev_id); |
| 359 | areadesc.add_simple_image(0x040000, 0x020000, FlashId::Image1, dev_id); |
| 360 | areadesc.add_simple_image(0x060000, 0x020000, FlashId::ImageScratch, dev_id); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 361 | |
| 362 | let mut flashmap = SimFlashMap::new(); |
| 363 | flashmap.insert(dev_id, flash); |
| 364 | (flashmap, areadesc) |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 365 | } |
| 366 | DeviceName::Nrf52840 => { |
| 367 | // Simulating the flash on the nrf52840 with partitions set up so that the scratch size |
| 368 | // does not divide into the image size. |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 369 | let flash = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 370 | |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 371 | let dev_id = 0; |
| 372 | let mut areadesc = AreaDesc::new(); |
| 373 | areadesc.add_flash_sectors(dev_id, &flash); |
| 374 | areadesc.add_image(0x008000, 0x034000, FlashId::Image0, dev_id); |
| 375 | areadesc.add_image(0x03c000, 0x034000, FlashId::Image1, dev_id); |
| 376 | areadesc.add_image(0x070000, 0x00d000, FlashId::ImageScratch, dev_id); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 377 | |
| 378 | let mut flashmap = SimFlashMap::new(); |
| 379 | flashmap.insert(dev_id, flash); |
| 380 | (flashmap, areadesc) |
| 381 | } |
| 382 | DeviceName::Nrf52840SpiFlash => { |
| 383 | // Simulate nrf52840 with external SPI flash. The external SPI flash |
| 384 | // has a larger sector size so for now store scratch on that flash. |
| 385 | let flash0 = SimFlash::new(vec![4096; 128], align as usize, erased_val); |
Fabio Utzig | 6465077 | 2018-11-19 16:51:13 -0200 | [diff] [blame] | 386 | let flash1 = SimFlash::new(vec![8192; 64], align as usize, erased_val); |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 387 | |
| 388 | let mut areadesc = AreaDesc::new(); |
| 389 | areadesc.add_flash_sectors(0, &flash0); |
| 390 | areadesc.add_flash_sectors(1, &flash1); |
| 391 | |
| 392 | areadesc.add_image(0x008000, 0x068000, FlashId::Image0, 0); |
| 393 | areadesc.add_image(0x000000, 0x068000, FlashId::Image1, 1); |
| 394 | areadesc.add_image(0x068000, 0x018000, FlashId::ImageScratch, 1); |
| 395 | |
| 396 | let mut flashmap = SimFlashMap::new(); |
| 397 | flashmap.insert(0, flash0); |
| 398 | flashmap.insert(1, flash1); |
| 399 | (flashmap, areadesc) |
David Brown | decbd04 | 2017-10-19 10:43:17 -0600 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | } |