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