blob: c8e8963e2b095f17464c58cacece98958edbe104 [file] [log] [blame]
David Brown2639e072017-10-11 11:18:44 -06001use docopt::Docopt;
David Brown5c9e0f12019-01-09 16:34:33 -07002use log::{warn, error};
David Brown10b5de12019-01-02 16:10:01 -07003use std::{
4 fmt,
David Brown10b5de12019-01-02 16:10:01 -07005 process,
David Brown10b5de12019-01-02 16:10:01 -07006};
7use serde_derive::Deserialize;
David Brown2639e072017-10-11 11:18:44 -06008
9mod caps;
David Brownc3898d62019-08-05 14:20:02 -060010mod depends;
David Brown5c9e0f12019-01-09 16:34:33 -070011mod image;
David Brown2639e072017-10-11 11:18:44 -060012mod tlv;
David Brownca7b5d32017-11-03 08:37:38 -060013pub mod testlog;
David Brown2639e072017-10-11 11:18:44 -060014
David Brownc3898d62019-08-05 14:20:02 -060015pub use crate::{
16 depends::{
17 DepTest,
18 DepType,
19 UpgradeInfo,
20 NO_DEPS,},
21 image::{
22 ImagesBuilder,
23 show_sizes,
24 },
David Brown5c9e0f12019-01-09 16:34:33 -070025};
David Brown2639e072017-10-11 11:18:44 -060026
27const USAGE: &'static str = "
28Mcuboot simulator
29
30Usage:
31 bootsim sizes
32 bootsim run --device TYPE [--align SIZE]
33 bootsim runall
34 bootsim (--help | --version)
35
36Options:
37 -h, --help Show this message
38 --version Version
39 --device TYPE MCU to simulate
40 Valid values: stm32f4, k64f
41 --align SIZE Flash write alignment
42";
43
44#[derive(Debug, Deserialize)]
45struct Args {
46 flag_help: bool,
47 flag_version: bool,
48 flag_device: Option<DeviceName>,
49 flag_align: Option<AlignArg>,
50 cmd_sizes: bool,
51 cmd_run: bool,
52 cmd_runall: bool,
53}
54
55#[derive(Copy, Clone, Debug, Deserialize)]
David Brown2bff6472019-03-05 13:58:35 -070056pub enum DeviceName { Stm32f4, K64f, K64fBig, K64fMulti, Nrf52840, Nrf52840SpiFlash, }
David Brown2639e072017-10-11 11:18:44 -060057
David Browndd2b1182017-11-02 15:39:21 -060058pub static ALL_DEVICES: &'static [DeviceName] = &[
David Brown2639e072017-10-11 11:18:44 -060059 DeviceName::Stm32f4,
60 DeviceName::K64f,
61 DeviceName::K64fBig,
David Brown2bff6472019-03-05 13:58:35 -070062 DeviceName::K64fMulti,
David Brown2639e072017-10-11 11:18:44 -060063 DeviceName::Nrf52840,
Fabio Utzigafb2bc92018-11-19 16:11:52 -020064 DeviceName::Nrf52840SpiFlash,
David Brown2639e072017-10-11 11:18:44 -060065];
66
67impl fmt::Display for DeviceName {
David Brown10b5de12019-01-02 16:10:01 -070068 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
David Brown2639e072017-10-11 11:18:44 -060069 let name = match *self {
70 DeviceName::Stm32f4 => "stm32f4",
71 DeviceName::K64f => "k64f",
72 DeviceName::K64fBig => "k64fbig",
David Brown2bff6472019-03-05 13:58:35 -070073 DeviceName::K64fMulti => "k64fmulti",
David Brown2639e072017-10-11 11:18:44 -060074 DeviceName::Nrf52840 => "nrf52840",
Fabio Utzigafb2bc92018-11-19 16:11:52 -020075 DeviceName::Nrf52840SpiFlash => "Nrf52840SpiFlash",
David Brown2639e072017-10-11 11:18:44 -060076 };
77 f.write_str(name)
78 }
79}
80
81#[derive(Debug)]
82struct AlignArg(u8);
83
84struct AlignArgVisitor;
85
86impl<'de> serde::de::Visitor<'de> for AlignArgVisitor {
87 type Value = AlignArg;
88
David Brown10b5de12019-01-02 16:10:01 -070089 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
David Brown2639e072017-10-11 11:18:44 -060090 formatter.write_str("1, 2, 4 or 8")
91 }
92
93 fn visit_u8<E>(self, n: u8) -> Result<Self::Value, E>
94 where E: serde::de::Error
95 {
96 Ok(match n {
97 1 | 2 | 4 | 8 => AlignArg(n),
98 n => {
99 let err = format!("Could not deserialize '{}' as alignment", n);
100 return Err(E::custom(err));
101 }
102 })
103 }
104}
105
106impl<'de> serde::de::Deserialize<'de> for AlignArg {
107 fn deserialize<D>(d: D) -> Result<AlignArg, D::Error>
108 where D: serde::de::Deserializer<'de>
109 {
110 d.deserialize_u8(AlignArgVisitor)
111 }
112}
113
114pub fn main() {
115 let args: Args = Docopt::new(USAGE)
116 .and_then(|d| d.deserialize())
117 .unwrap_or_else(|e| e.exit());
118 // println!("args: {:#?}", args);
119
120 if args.cmd_sizes {
121 show_sizes();
122 return;
123 }
124
125 let mut status = RunStatus::new();
126 if args.cmd_run {
127
128 let align = args.flag_align.map(|x| x.0).unwrap_or(1);
129
130
131 let device = match args.flag_device {
132 None => panic!("Missing mandatory device argument"),
133 Some(dev) => dev,
134 };
135
Fabio Utzigea0290b2018-08-09 14:23:01 -0300136 status.run_single(device, align, 0xff);
David Brown2639e072017-10-11 11:18:44 -0600137 }
138
139 if args.cmd_runall {
140 for &dev in ALL_DEVICES {
141 for &align in &[1, 2, 4, 8] {
Fabio Utzigea0290b2018-08-09 14:23:01 -0300142 for &erased_val in &[0, 0xff] {
143 status.run_single(dev, align, erased_val);
144 }
David Brown2639e072017-10-11 11:18:44 -0600145 }
146 }
147 }
148
149 if status.failures > 0 {
150 error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures);
151 process::exit(1);
152 } else {
153 error!("{} Tests ran successfully", status.passes);
154 process::exit(0);
155 }
156}
157
David Browndd2b1182017-11-02 15:39:21 -0600158pub struct RunStatus {
David Brown2639e072017-10-11 11:18:44 -0600159 failures: usize,
160 passes: usize,
161}
162
163impl RunStatus {
David Browndd2b1182017-11-02 15:39:21 -0600164 pub fn new() -> RunStatus {
David Brown2639e072017-10-11 11:18:44 -0600165 RunStatus {
166 failures: 0,
167 passes: 0,
168 }
169 }
170
Fabio Utzigea0290b2018-08-09 14:23:01 -0300171 pub fn run_single(&mut self, device: DeviceName, align: u8, erased_val: u8) {
David Brown2639e072017-10-11 11:18:44 -0600172 warn!("Running on device {} with alignment {}", device, align);
173
David Brown5bc62c62019-03-05 12:11:48 -0700174 let run = match ImagesBuilder::new(device, align, erased_val) {
175 Some(builder) => builder,
176 None => {
177 warn!("Skipping device with insuffienct partitions");
178 return;
179 }
180 };
David Brown2639e072017-10-11 11:18:44 -0600181
David Brown2639e072017-10-11 11:18:44 -0600182 let mut failed = false;
183
David Vincze2d736ad2019-02-18 11:50:22 +0100184 // Creates a badly signed image in the secondary slot to check that
185 // it is not upgraded to
David Brown01617af2019-02-28 10:45:12 -0700186 let bad_secondary_slot_image = run.clone().make_bad_secondary_slot_image();
David Brown2639e072017-10-11 11:18:44 -0600187
David Vincze2d736ad2019-02-18 11:50:22 +0100188 failed |= bad_secondary_slot_image.run_signfail_upgrade();
David Brown2639e072017-10-11 11:18:44 -0600189
David Brownc3898d62019-08-05 14:20:02 -0600190 let images = run.clone().make_no_upgrade_image(&NO_DEPS);
David Brown5f7ec2b2017-11-06 13:54:02 -0700191 failed |= images.run_norevert_newimage();
David Brown2639e072017-10-11 11:18:44 -0600192
David Brownc3898d62019-08-05 14:20:02 -0600193 let images = run.make_image(&NO_DEPS, true);
David Brown2639e072017-10-11 11:18:44 -0600194
David Brown5f7ec2b2017-11-06 13:54:02 -0700195 failed |= images.run_basic_revert();
David Brownc49811e2017-11-06 14:20:45 -0700196 failed |= images.run_revert_with_fails();
197 failed |= images.run_perm_with_fails();
198 failed |= images.run_perm_with_random_fails(5);
David Brown5f7ec2b2017-11-06 13:54:02 -0700199 failed |= images.run_norevert();
David Brown2639e072017-10-11 11:18:44 -0600200
Fabio Utzigb841f0a2017-11-24 08:11:05 -0200201 failed |= images.run_with_status_fails_complete();
Fabio Utzigeedcc452017-11-24 10:48:52 -0200202 failed |= images.run_with_status_fails_with_reset();
Fabio Utzigb841f0a2017-11-24 08:11:05 -0200203
David Brown2639e072017-10-11 11:18:44 -0600204 //show_flash(&flash);
205
206 if failed {
207 self.failures += 1;
208 } else {
209 self.passes += 1;
210 }
211 }
David Browndd2b1182017-11-02 15:39:21 -0600212
213 pub fn failures(&self) -> usize {
214 self.failures
215 }
David Brown2639e072017-10-11 11:18:44 -0600216}
217