blob: 4203c0e162de293d4a339890f0c683d55603f0f8 [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;
David Brownca7b5d32017-11-03 08:37:38 -060019pub mod testlog;
David Brown2639e072017-10-11 11:18:44 -060020
David Brownc3898d62019-08-05 14:20:02 -060021pub use crate::{
22 depends::{
23 DepTest,
24 DepType,
25 UpgradeInfo,
David Brown2ee5f7f2020-01-13 14:04:01 -070026 NO_DEPS,
27 REV_DEPS,
28 },
David Brownc3898d62019-08-05 14:20:02 -060029 image::{
30 ImagesBuilder,
David Brownfe5ab1c2019-08-13 15:35:23 -060031 Images,
David Brownc3898d62019-08-05 14:20:02 -060032 show_sizes,
33 },
David Brown5c9e0f12019-01-09 16:34:33 -070034};
David Brown2639e072017-10-11 11:18:44 -060035
David Brown1997f532021-03-10 05:04:05 -070036const USAGE: &str = "
David Brown2639e072017-10-11 11:18:44 -060037Mcuboot simulator
38
39Usage:
40 bootsim sizes
41 bootsim run --device TYPE [--align SIZE]
42 bootsim runall
43 bootsim (--help | --version)
44
45Options:
46 -h, --help Show this message
47 --version Version
48 --device TYPE MCU to simulate
49 Valid values: stm32f4, k64f
50 --align SIZE Flash write alignment
51";
52
53#[derive(Debug, Deserialize)]
54struct Args {
55 flag_help: bool,
56 flag_version: bool,
57 flag_device: Option<DeviceName>,
58 flag_align: Option<AlignArg>,
59 cmd_sizes: bool,
60 cmd_run: bool,
61 cmd_runall: bool,
62}
63
64#[derive(Copy, Clone, Debug, Deserialize)]
Fabio Utzigc659ec52020-07-13 21:18:48 -030065pub enum DeviceName {
66 Stm32f4, K64f, K64fBig, K64fMulti, Nrf52840, Nrf52840SpiFlash,
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +020067 Nrf52840UnequalSlots, PSoC6Multi,
Fabio Utzigc659ec52020-07-13 21:18:48 -030068}
David Brown2639e072017-10-11 11:18:44 -060069
David Brown1997f532021-03-10 05:04:05 -070070pub static ALL_DEVICES: &[DeviceName] = &[
David Brown2639e072017-10-11 11:18:44 -060071 DeviceName::Stm32f4,
72 DeviceName::K64f,
73 DeviceName::K64fBig,
David Brown2bff6472019-03-05 13:58:35 -070074 DeviceName::K64fMulti,
David Brown2639e072017-10-11 11:18:44 -060075 DeviceName::Nrf52840,
Fabio Utzigafb2bc92018-11-19 16:11:52 -020076 DeviceName::Nrf52840SpiFlash,
Fabio Utzigc659ec52020-07-13 21:18:48 -030077 DeviceName::Nrf52840UnequalSlots,
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +020078 DeviceName::PSoC6Multi,
David Brown2639e072017-10-11 11:18:44 -060079];
80
81impl fmt::Display for DeviceName {
David Brown10b5de12019-01-02 16:10:01 -070082 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
David Brown2639e072017-10-11 11:18:44 -060083 let name = match *self {
84 DeviceName::Stm32f4 => "stm32f4",
85 DeviceName::K64f => "k64f",
86 DeviceName::K64fBig => "k64fbig",
David Brown2bff6472019-03-05 13:58:35 -070087 DeviceName::K64fMulti => "k64fmulti",
David Brown2639e072017-10-11 11:18:44 -060088 DeviceName::Nrf52840 => "nrf52840",
Fabio Utzigafb2bc92018-11-19 16:11:52 -020089 DeviceName::Nrf52840SpiFlash => "Nrf52840SpiFlash",
Fabio Utzigc659ec52020-07-13 21:18:48 -030090 DeviceName::Nrf52840UnequalSlots => "Nrf52840UnequalSlots",
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +020091 DeviceName::PSoC6Multi => "psoc6multi",
David Brown2639e072017-10-11 11:18:44 -060092 };
93 f.write_str(name)
94 }
95}
96
97#[derive(Debug)]
David Brown5a317752019-11-15 09:53:39 -070098struct AlignArg(usize);
David Brown2639e072017-10-11 11:18:44 -060099
100struct AlignArgVisitor;
101
102impl<'de> serde::de::Visitor<'de> for AlignArgVisitor {
103 type Value = AlignArg;
104
David Brown10b5de12019-01-02 16:10:01 -0700105 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
David Brown2639e072017-10-11 11:18:44 -0600106 formatter.write_str("1, 2, 4 or 8")
107 }
108
David Brown5a317752019-11-15 09:53:39 -0700109 fn visit_u32<E>(self, n: u32) -> Result<Self::Value, E>
David Brown2639e072017-10-11 11:18:44 -0600110 where E: serde::de::Error
111 {
112 Ok(match n {
David Brown5a317752019-11-15 09:53:39 -0700113 1 | 2 | 4 | 8 => AlignArg(n as usize),
David Brown2639e072017-10-11 11:18:44 -0600114 n => {
115 let err = format!("Could not deserialize '{}' as alignment", n);
116 return Err(E::custom(err));
117 }
118 })
119 }
120}
121
122impl<'de> serde::de::Deserialize<'de> for AlignArg {
123 fn deserialize<D>(d: D) -> Result<AlignArg, D::Error>
124 where D: serde::de::Deserializer<'de>
125 {
126 d.deserialize_u8(AlignArgVisitor)
127 }
128}
129
130pub fn main() {
131 let args: Args = Docopt::new(USAGE)
132 .and_then(|d| d.deserialize())
133 .unwrap_or_else(|e| e.exit());
134 // println!("args: {:#?}", args);
135
136 if args.cmd_sizes {
137 show_sizes();
138 return;
139 }
140
141 let mut status = RunStatus::new();
142 if args.cmd_run {
143
144 let align = args.flag_align.map(|x| x.0).unwrap_or(1);
145
146
147 let device = match args.flag_device {
148 None => panic!("Missing mandatory device argument"),
149 Some(dev) => dev,
150 };
151
Fabio Utzigea0290b2018-08-09 14:23:01 -0300152 status.run_single(device, align, 0xff);
David Brown2639e072017-10-11 11:18:44 -0600153 }
154
155 if args.cmd_runall {
156 for &dev in ALL_DEVICES {
157 for &align in &[1, 2, 4, 8] {
Fabio Utzigea0290b2018-08-09 14:23:01 -0300158 for &erased_val in &[0, 0xff] {
159 status.run_single(dev, align, erased_val);
160 }
David Brown2639e072017-10-11 11:18:44 -0600161 }
162 }
163 }
164
165 if status.failures > 0 {
166 error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures);
167 process::exit(1);
168 } else {
169 error!("{} Tests ran successfully", status.passes);
170 process::exit(0);
171 }
172}
173
David Brownfc8e3c52021-03-10 05:11:26 -0700174#[derive(Default)]
David Browndd2b1182017-11-02 15:39:21 -0600175pub struct RunStatus {
David Brown2639e072017-10-11 11:18:44 -0600176 failures: usize,
177 passes: usize,
178}
179
180impl RunStatus {
David Browndd2b1182017-11-02 15:39:21 -0600181 pub fn new() -> RunStatus {
David Brown2639e072017-10-11 11:18:44 -0600182 RunStatus {
183 failures: 0,
184 passes: 0,
185 }
186 }
187
David Brown5a317752019-11-15 09:53:39 -0700188 pub fn run_single(&mut self, device: DeviceName, align: usize, erased_val: u8) {
David Brown2639e072017-10-11 11:18:44 -0600189 warn!("Running on device {} with alignment {}", device, align);
190
David Brown5bc62c62019-03-05 12:11:48 -0700191 let run = match ImagesBuilder::new(device, align, erased_val) {
Fabio Utzig114a6472019-11-28 10:24:09 -0300192 Ok(builder) => builder,
193 Err(msg) => {
194 warn!("Skipping {}: {}", device, msg);
David Brown5bc62c62019-03-05 12:11:48 -0700195 return;
196 }
197 };
David Brown2639e072017-10-11 11:18:44 -0600198
David Brown2639e072017-10-11 11:18:44 -0600199 let mut failed = false;
200
David Vincze2d736ad2019-02-18 11:50:22 +0100201 // Creates a badly signed image in the secondary slot to check that
202 // it is not upgraded to
David Brown01617af2019-02-28 10:45:12 -0700203 let bad_secondary_slot_image = run.clone().make_bad_secondary_slot_image();
David Brown2639e072017-10-11 11:18:44 -0600204
David Vincze2d736ad2019-02-18 11:50:22 +0100205 failed |= bad_secondary_slot_image.run_signfail_upgrade();
David Brown2639e072017-10-11 11:18:44 -0600206
David Brownc3898d62019-08-05 14:20:02 -0600207 let images = run.clone().make_no_upgrade_image(&NO_DEPS);
David Brown5f7ec2b2017-11-06 13:54:02 -0700208 failed |= images.run_norevert_newimage();
David Brown2639e072017-10-11 11:18:44 -0600209
David Brownc3898d62019-08-05 14:20:02 -0600210 let images = run.make_image(&NO_DEPS, true);
David Brown2639e072017-10-11 11:18:44 -0600211
David Brown5f7ec2b2017-11-06 13:54:02 -0700212 failed |= images.run_basic_revert();
David Brownc49811e2017-11-06 14:20:45 -0700213 failed |= images.run_revert_with_fails();
214 failed |= images.run_perm_with_fails();
215 failed |= images.run_perm_with_random_fails(5);
David Brown5f7ec2b2017-11-06 13:54:02 -0700216 failed |= images.run_norevert();
David Brown2639e072017-10-11 11:18:44 -0600217
Fabio Utzigb841f0a2017-11-24 08:11:05 -0200218 failed |= images.run_with_status_fails_complete();
Fabio Utzigeedcc452017-11-24 10:48:52 -0200219 failed |= images.run_with_status_fails_with_reset();
Fabio Utzigb841f0a2017-11-24 08:11:05 -0200220
David Brown2639e072017-10-11 11:18:44 -0600221 //show_flash(&flash);
222
223 if failed {
224 self.failures += 1;
225 } else {
226 self.passes += 1;
227 }
228 }
David Browndd2b1182017-11-02 15:39:21 -0600229
230 pub fn failures(&self) -> usize {
231 self.failures
232 }
David Brown2639e072017-10-11 11:18:44 -0600233}