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