blob: df1919e07863a9fef445852d8f45c67d94f1751e [file] [log] [blame]
David Browne2acfae2020-01-21 16:45:01 -07001// Copyright (c) 2017-2019 Linaro LTD
2// Copyright (c) 2017-2019 JUUL Labs
3// Copyright (c) 2019 Arm Limited
4//
5// SPDX-License-Identifier: Apache-2.0
6
David Brown2639e072017-10-11 11:18:44 -06007use docopt::Docopt;
David Brown5c9e0f12019-01-09 16:34:33 -07008use log::{warn, error};
David Brown10b5de12019-01-02 16:10:01 -07009use std::{
10 fmt,
David Brown10b5de12019-01-02 16:10:01 -070011 process,
David Brown10b5de12019-01-02 16:10:01 -070012};
13use serde_derive::Deserialize;
David Brown2639e072017-10-11 11:18:44 -060014
15mod caps;
David Brownc3898d62019-08-05 14:20:02 -060016mod depends;
David Brown5c9e0f12019-01-09 16:34:33 -070017mod image;
David Brown2639e072017-10-11 11:18:44 -060018mod tlv;
Gustavo Henrique Nihei7bfd14b2021-11-24 23:27:22 -030019mod utils;
David Brownca7b5d32017-11-03 08:37:38 -060020pub mod testlog;
David Brown2639e072017-10-11 11:18:44 -060021
David Brownc3898d62019-08-05 14:20:02 -060022pub use crate::{
23 depends::{
24 DepTest,
25 DepType,
26 UpgradeInfo,
David Brown2ee5f7f2020-01-13 14:04:01 -070027 NO_DEPS,
28 REV_DEPS,
29 },
David Brownc3898d62019-08-05 14:20:02 -060030 image::{
31 ImagesBuilder,
David Brownfe5ab1c2019-08-13 15:35:23 -060032 Images,
David Brownc3898d62019-08-05 14:20:02 -060033 show_sizes,
34 },
David Brown5c9e0f12019-01-09 16:34:33 -070035};
David Brown2639e072017-10-11 11:18:44 -060036
David Brown1997f532021-03-10 05:04:05 -070037const USAGE: &str = "
David Brown2639e072017-10-11 11:18:44 -060038Mcuboot simulator
39
40Usage:
41 bootsim sizes
42 bootsim run --device TYPE [--align SIZE]
43 bootsim runall
44 bootsim (--help | --version)
45
46Options:
47 -h, --help Show this message
48 --version Version
49 --device TYPE MCU to simulate
50 Valid values: stm32f4, k64f
51 --align SIZE Flash write alignment
52";
53
54#[derive(Debug, Deserialize)]
55struct Args {
David Brown2639e072017-10-11 11:18:44 -060056 flag_device: Option<DeviceName>,
57 flag_align: Option<AlignArg>,
58 cmd_sizes: bool,
59 cmd_run: bool,
60 cmd_runall: bool,
61}
62
63#[derive(Copy, Clone, Debug, Deserialize)]
Fabio Utzigc659ec52020-07-13 21:18:48 -030064pub enum DeviceName {
65 Stm32f4, K64f, K64fBig, K64fMulti, Nrf52840, Nrf52840SpiFlash,
66 Nrf52840UnequalSlots,
67}
David Brown2639e072017-10-11 11:18:44 -060068
David Brown1997f532021-03-10 05:04:05 -070069pub static ALL_DEVICES: &[DeviceName] = &[
David Brown2639e072017-10-11 11:18:44 -060070 DeviceName::Stm32f4,
71 DeviceName::K64f,
72 DeviceName::K64fBig,
David Brown2bff6472019-03-05 13:58:35 -070073 DeviceName::K64fMulti,
David Brown2639e072017-10-11 11:18:44 -060074 DeviceName::Nrf52840,
Fabio Utzigafb2bc92018-11-19 16:11:52 -020075 DeviceName::Nrf52840SpiFlash,
Fabio Utzigc659ec52020-07-13 21:18:48 -030076 DeviceName::Nrf52840UnequalSlots,
David Brown2639e072017-10-11 11:18:44 -060077];
78
79impl fmt::Display for DeviceName {
David Brown10b5de12019-01-02 16:10:01 -070080 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
David Brown2639e072017-10-11 11:18:44 -060081 let name = match *self {
82 DeviceName::Stm32f4 => "stm32f4",
83 DeviceName::K64f => "k64f",
84 DeviceName::K64fBig => "k64fbig",
David Brown2bff6472019-03-05 13:58:35 -070085 DeviceName::K64fMulti => "k64fmulti",
David Brown2639e072017-10-11 11:18:44 -060086 DeviceName::Nrf52840 => "nrf52840",
Fabio Utzigafb2bc92018-11-19 16:11:52 -020087 DeviceName::Nrf52840SpiFlash => "Nrf52840SpiFlash",
Fabio Utzigc659ec52020-07-13 21:18:48 -030088 DeviceName::Nrf52840UnequalSlots => "Nrf52840UnequalSlots",
David Brown2639e072017-10-11 11:18:44 -060089 };
90 f.write_str(name)
91 }
92}
93
94#[derive(Debug)]
David Brown5a317752019-11-15 09:53:39 -070095struct AlignArg(usize);
David Brown2639e072017-10-11 11:18:44 -060096
97struct AlignArgVisitor;
98
99impl<'de> serde::de::Visitor<'de> for AlignArgVisitor {
100 type Value = AlignArg;
101
David Brown10b5de12019-01-02 16:10:01 -0700102 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
David Brown2639e072017-10-11 11:18:44 -0600103 formatter.write_str("1, 2, 4 or 8")
104 }
105
David Brown5a317752019-11-15 09:53:39 -0700106 fn visit_u32<E>(self, n: u32) -> Result<Self::Value, E>
David Brown2639e072017-10-11 11:18:44 -0600107 where E: serde::de::Error
108 {
109 Ok(match n {
David Brown5a317752019-11-15 09:53:39 -0700110 1 | 2 | 4 | 8 => AlignArg(n as usize),
David Brown2639e072017-10-11 11:18:44 -0600111 n => {
112 let err = format!("Could not deserialize '{}' as alignment", n);
113 return Err(E::custom(err));
114 }
115 })
116 }
117}
118
119impl<'de> serde::de::Deserialize<'de> for AlignArg {
120 fn deserialize<D>(d: D) -> Result<AlignArg, D::Error>
121 where D: serde::de::Deserializer<'de>
122 {
123 d.deserialize_u8(AlignArgVisitor)
124 }
125}
126
127pub fn main() {
128 let args: Args = Docopt::new(USAGE)
129 .and_then(|d| d.deserialize())
130 .unwrap_or_else(|e| e.exit());
131 // println!("args: {:#?}", args);
132
133 if args.cmd_sizes {
134 show_sizes();
135 return;
136 }
137
138 let mut status = RunStatus::new();
139 if args.cmd_run {
140
141 let align = args.flag_align.map(|x| x.0).unwrap_or(1);
142
143
144 let device = match args.flag_device {
145 None => panic!("Missing mandatory device argument"),
146 Some(dev) => dev,
147 };
148
Fabio Utzigea0290b2018-08-09 14:23:01 -0300149 status.run_single(device, align, 0xff);
David Brown2639e072017-10-11 11:18:44 -0600150 }
151
152 if args.cmd_runall {
153 for &dev in ALL_DEVICES {
154 for &align in &[1, 2, 4, 8] {
Fabio Utzigea0290b2018-08-09 14:23:01 -0300155 for &erased_val in &[0, 0xff] {
156 status.run_single(dev, align, erased_val);
157 }
David Brown2639e072017-10-11 11:18:44 -0600158 }
159 }
160 }
161
162 if status.failures > 0 {
163 error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures);
164 process::exit(1);
165 } else {
166 error!("{} Tests ran successfully", status.passes);
167 process::exit(0);
168 }
169}
170
David Brownfc8e3c52021-03-10 05:11:26 -0700171#[derive(Default)]
David Browndd2b1182017-11-02 15:39:21 -0600172pub struct RunStatus {
David Brown2639e072017-10-11 11:18:44 -0600173 failures: usize,
174 passes: usize,
175}
176
177impl RunStatus {
David Browndd2b1182017-11-02 15:39:21 -0600178 pub fn new() -> RunStatus {
David Brown2639e072017-10-11 11:18:44 -0600179 RunStatus {
180 failures: 0,
181 passes: 0,
182 }
183 }
184
David Brown5a317752019-11-15 09:53:39 -0700185 pub fn run_single(&mut self, device: DeviceName, align: usize, erased_val: u8) {
David Brown2639e072017-10-11 11:18:44 -0600186 warn!("Running on device {} with alignment {}", device, align);
187
David Brown5bc62c62019-03-05 12:11:48 -0700188 let run = match ImagesBuilder::new(device, align, erased_val) {
Fabio Utzig114a6472019-11-28 10:24:09 -0300189 Ok(builder) => builder,
190 Err(msg) => {
191 warn!("Skipping {}: {}", device, msg);
David Brown5bc62c62019-03-05 12:11:48 -0700192 return;
193 }
194 };
David Brown2639e072017-10-11 11:18:44 -0600195
David Brown2639e072017-10-11 11:18:44 -0600196 let mut failed = false;
197
David Vincze2d736ad2019-02-18 11:50:22 +0100198 // Creates a badly signed image in the secondary slot to check that
199 // it is not upgraded to
David Brown01617af2019-02-28 10:45:12 -0700200 let bad_secondary_slot_image = run.clone().make_bad_secondary_slot_image();
David Brown2639e072017-10-11 11:18:44 -0600201
David Vincze2d736ad2019-02-18 11:50:22 +0100202 failed |= bad_secondary_slot_image.run_signfail_upgrade();
David Brown2639e072017-10-11 11:18:44 -0600203
David Brownc3898d62019-08-05 14:20:02 -0600204 let images = run.clone().make_no_upgrade_image(&NO_DEPS);
David Brown5f7ec2b2017-11-06 13:54:02 -0700205 failed |= images.run_norevert_newimage();
David Brown2639e072017-10-11 11:18:44 -0600206
David Brownc3898d62019-08-05 14:20:02 -0600207 let images = run.make_image(&NO_DEPS, true);
David Brown2639e072017-10-11 11:18:44 -0600208
David Brown5f7ec2b2017-11-06 13:54:02 -0700209 failed |= images.run_basic_revert();
David Brownc49811e2017-11-06 14:20:45 -0700210 failed |= images.run_revert_with_fails();
211 failed |= images.run_perm_with_fails();
212 failed |= images.run_perm_with_random_fails(5);
David Brown5f7ec2b2017-11-06 13:54:02 -0700213 failed |= images.run_norevert();
David Brown2639e072017-10-11 11:18:44 -0600214
Fabio Utzigb841f0a2017-11-24 08:11:05 -0200215 failed |= images.run_with_status_fails_complete();
Fabio Utzigeedcc452017-11-24 10:48:52 -0200216 failed |= images.run_with_status_fails_with_reset();
Fabio Utzigb841f0a2017-11-24 08:11:05 -0200217
David Brown2639e072017-10-11 11:18:44 -0600218 //show_flash(&flash);
219
220 if failed {
221 self.failures += 1;
222 } else {
223 self.passes += 1;
224 }
225 }
David Browndd2b1182017-11-02 15:39:21 -0600226
227 pub fn failures(&self) -> usize {
228 self.failures
229 }
David Brown2639e072017-10-11 11:18:44 -0600230}