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 | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 10 | mod depends; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 11 | mod image; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 12 | mod tlv; |
David Brown | ca7b5d3 | 2017-11-03 08:37:38 -0600 | [diff] [blame] | 13 | pub mod testlog; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 14 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 15 | pub use crate::{ |
| 16 | depends::{ |
| 17 | DepTest, |
| 18 | DepType, |
| 19 | UpgradeInfo, |
| 20 | NO_DEPS,}, |
| 21 | image::{ |
| 22 | ImagesBuilder, |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 23 | Images, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 24 | show_sizes, |
| 25 | }, |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 26 | }; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 27 | |
| 28 | const USAGE: &'static str = " |
| 29 | Mcuboot simulator |
| 30 | |
| 31 | Usage: |
| 32 | bootsim sizes |
| 33 | bootsim run --device TYPE [--align SIZE] |
| 34 | bootsim runall |
| 35 | bootsim (--help | --version) |
| 36 | |
| 37 | Options: |
| 38 | -h, --help Show this message |
| 39 | --version Version |
| 40 | --device TYPE MCU to simulate |
| 41 | Valid values: stm32f4, k64f |
| 42 | --align SIZE Flash write alignment |
| 43 | "; |
| 44 | |
| 45 | #[derive(Debug, Deserialize)] |
| 46 | struct Args { |
| 47 | flag_help: bool, |
| 48 | flag_version: bool, |
| 49 | flag_device: Option<DeviceName>, |
| 50 | flag_align: Option<AlignArg>, |
| 51 | cmd_sizes: bool, |
| 52 | cmd_run: bool, |
| 53 | cmd_runall: bool, |
| 54 | } |
| 55 | |
| 56 | #[derive(Copy, Clone, Debug, Deserialize)] |
David Brown | 2bff647 | 2019-03-05 13:58:35 -0700 | [diff] [blame] | 57 | pub enum DeviceName { Stm32f4, K64f, K64fBig, K64fMulti, Nrf52840, Nrf52840SpiFlash, } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 58 | |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 59 | pub static ALL_DEVICES: &'static [DeviceName] = &[ |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 60 | DeviceName::Stm32f4, |
| 61 | DeviceName::K64f, |
| 62 | DeviceName::K64fBig, |
David Brown | 2bff647 | 2019-03-05 13:58:35 -0700 | [diff] [blame] | 63 | DeviceName::K64fMulti, |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 64 | DeviceName::Nrf52840, |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 65 | DeviceName::Nrf52840SpiFlash, |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 66 | ]; |
| 67 | |
| 68 | impl fmt::Display for DeviceName { |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 69 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 70 | let name = match *self { |
| 71 | DeviceName::Stm32f4 => "stm32f4", |
| 72 | DeviceName::K64f => "k64f", |
| 73 | DeviceName::K64fBig => "k64fbig", |
David Brown | 2bff647 | 2019-03-05 13:58:35 -0700 | [diff] [blame] | 74 | DeviceName::K64fMulti => "k64fmulti", |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 75 | DeviceName::Nrf52840 => "nrf52840", |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 76 | DeviceName::Nrf52840SpiFlash => "Nrf52840SpiFlash", |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 77 | }; |
| 78 | f.write_str(name) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #[derive(Debug)] |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 83 | struct AlignArg(usize); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 84 | |
| 85 | struct AlignArgVisitor; |
| 86 | |
| 87 | impl<'de> serde::de::Visitor<'de> for AlignArgVisitor { |
| 88 | type Value = AlignArg; |
| 89 | |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 90 | fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 91 | formatter.write_str("1, 2, 4 or 8") |
| 92 | } |
| 93 | |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 94 | fn visit_u32<E>(self, n: u32) -> Result<Self::Value, E> |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 95 | where E: serde::de::Error |
| 96 | { |
| 97 | Ok(match n { |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 98 | 1 | 2 | 4 | 8 => AlignArg(n as usize), |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 99 | n => { |
| 100 | let err = format!("Could not deserialize '{}' as alignment", n); |
| 101 | return Err(E::custom(err)); |
| 102 | } |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl<'de> serde::de::Deserialize<'de> for AlignArg { |
| 108 | fn deserialize<D>(d: D) -> Result<AlignArg, D::Error> |
| 109 | where D: serde::de::Deserializer<'de> |
| 110 | { |
| 111 | d.deserialize_u8(AlignArgVisitor) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | pub fn main() { |
| 116 | let args: Args = Docopt::new(USAGE) |
| 117 | .and_then(|d| d.deserialize()) |
| 118 | .unwrap_or_else(|e| e.exit()); |
| 119 | // println!("args: {:#?}", args); |
| 120 | |
| 121 | if args.cmd_sizes { |
| 122 | show_sizes(); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | let mut status = RunStatus::new(); |
| 127 | if args.cmd_run { |
| 128 | |
| 129 | let align = args.flag_align.map(|x| x.0).unwrap_or(1); |
| 130 | |
| 131 | |
| 132 | let device = match args.flag_device { |
| 133 | None => panic!("Missing mandatory device argument"), |
| 134 | Some(dev) => dev, |
| 135 | }; |
| 136 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 137 | status.run_single(device, align, 0xff); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if args.cmd_runall { |
| 141 | for &dev in ALL_DEVICES { |
| 142 | for &align in &[1, 2, 4, 8] { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 143 | for &erased_val in &[0, 0xff] { |
| 144 | status.run_single(dev, align, erased_val); |
| 145 | } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if status.failures > 0 { |
| 151 | error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures); |
| 152 | process::exit(1); |
| 153 | } else { |
| 154 | error!("{} Tests ran successfully", status.passes); |
| 155 | process::exit(0); |
| 156 | } |
| 157 | } |
| 158 | |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 159 | pub struct RunStatus { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 160 | failures: usize, |
| 161 | passes: usize, |
| 162 | } |
| 163 | |
| 164 | impl RunStatus { |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 165 | pub fn new() -> RunStatus { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 166 | RunStatus { |
| 167 | failures: 0, |
| 168 | passes: 0, |
| 169 | } |
| 170 | } |
| 171 | |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 172 | pub fn run_single(&mut self, device: DeviceName, align: usize, erased_val: u8) { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 173 | warn!("Running on device {} with alignment {}", device, align); |
| 174 | |
David Brown | 5bc62c6 | 2019-03-05 12:11:48 -0700 | [diff] [blame] | 175 | let run = match ImagesBuilder::new(device, align, erased_val) { |
Fabio Utzig | 114a647 | 2019-11-28 10:24:09 -0300 | [diff] [blame^] | 176 | Ok(builder) => builder, |
| 177 | Err(msg) => { |
| 178 | warn!("Skipping {}: {}", device, msg); |
David Brown | 5bc62c6 | 2019-03-05 12:11:48 -0700 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | }; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 182 | |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 183 | let mut failed = false; |
| 184 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 185 | // Creates a badly signed image in the secondary slot to check that |
| 186 | // it is not upgraded to |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame] | 187 | let bad_secondary_slot_image = run.clone().make_bad_secondary_slot_image(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 188 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 189 | failed |= bad_secondary_slot_image.run_signfail_upgrade(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 190 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 191 | let images = run.clone().make_no_upgrade_image(&NO_DEPS); |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 192 | failed |= images.run_norevert_newimage(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 193 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 194 | let images = run.make_image(&NO_DEPS, true); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 195 | |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 196 | failed |= images.run_basic_revert(); |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 197 | failed |= images.run_revert_with_fails(); |
| 198 | failed |= images.run_perm_with_fails(); |
| 199 | failed |= images.run_perm_with_random_fails(5); |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 200 | failed |= images.run_norevert(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 201 | |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 202 | failed |= images.run_with_status_fails_complete(); |
Fabio Utzig | eedcc45 | 2017-11-24 10:48:52 -0200 | [diff] [blame] | 203 | failed |= images.run_with_status_fails_with_reset(); |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 204 | |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 205 | //show_flash(&flash); |
| 206 | |
| 207 | if failed { |
| 208 | self.failures += 1; |
| 209 | } else { |
| 210 | self.passes += 1; |
| 211 | } |
| 212 | } |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 213 | |
| 214 | pub fn failures(&self) -> usize { |
| 215 | self.failures |
| 216 | } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 217 | } |
| 218 | |