David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 1 | // 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 Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 7 | use docopt::Docopt; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 8 | use log::{warn, error}; |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 9 | use std::{ |
| 10 | fmt, |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 11 | process, |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 12 | }; |
| 13 | use serde_derive::Deserialize; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 14 | |
| 15 | mod caps; |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 16 | mod depends; |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 17 | mod image; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 18 | mod tlv; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 19 | mod utils; |
David Brown | ca7b5d3 | 2017-11-03 08:37:38 -0600 | [diff] [blame] | 20 | pub mod testlog; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 21 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 22 | pub use crate::{ |
| 23 | depends::{ |
| 24 | DepTest, |
| 25 | DepType, |
| 26 | UpgradeInfo, |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 27 | NO_DEPS, |
| 28 | REV_DEPS, |
| 29 | }, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 30 | image::{ |
| 31 | ImagesBuilder, |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 32 | Images, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 33 | show_sizes, |
| 34 | }, |
David Brown | 5c9e0f1 | 2019-01-09 16:34:33 -0700 | [diff] [blame] | 35 | }; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 36 | |
David Brown | 1997f53 | 2021-03-10 05:04:05 -0700 | [diff] [blame] | 37 | const USAGE: &str = " |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 38 | Mcuboot simulator |
| 39 | |
| 40 | Usage: |
| 41 | bootsim sizes |
| 42 | bootsim run --device TYPE [--align SIZE] |
| 43 | bootsim runall |
| 44 | bootsim (--help | --version) |
| 45 | |
| 46 | Options: |
| 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)] |
| 55 | struct Args { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 56 | 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 Utzig | c659ec5 | 2020-07-13 21:18:48 -0300 | [diff] [blame] | 64 | pub enum DeviceName { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 65 | Stm32f4, |
| 66 | K64f, K64fBig, K64fMulti, |
| 67 | Nrf52840, Nrf52840SpiFlash, Nrf52840UnequalSlots, |
| 68 | PSoC6 |
Fabio Utzig | c659ec5 | 2020-07-13 21:18:48 -0300 | [diff] [blame] | 69 | } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 70 | |
David Brown | 1997f53 | 2021-03-10 05:04:05 -0700 | [diff] [blame] | 71 | pub static ALL_DEVICES: &[DeviceName] = &[ |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 72 | DeviceName::Stm32f4, |
| 73 | DeviceName::K64f, |
| 74 | DeviceName::K64fBig, |
David Brown | 2bff647 | 2019-03-05 13:58:35 -0700 | [diff] [blame] | 75 | DeviceName::K64fMulti, |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 76 | DeviceName::Nrf52840, |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 77 | DeviceName::Nrf52840SpiFlash, |
Fabio Utzig | c659ec5 | 2020-07-13 21:18:48 -0300 | [diff] [blame] | 78 | DeviceName::Nrf52840UnequalSlots, |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 79 | DeviceName::PSoC6 |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 80 | ]; |
| 81 | |
| 82 | impl fmt::Display for DeviceName { |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 83 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 84 | let name = match *self { |
| 85 | DeviceName::Stm32f4 => "stm32f4", |
| 86 | DeviceName::K64f => "k64f", |
| 87 | DeviceName::K64fBig => "k64fbig", |
David Brown | 2bff647 | 2019-03-05 13:58:35 -0700 | [diff] [blame] | 88 | DeviceName::K64fMulti => "k64fmulti", |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 89 | DeviceName::Nrf52840 => "nrf52840", |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 90 | DeviceName::Nrf52840SpiFlash => "Nrf52840SpiFlash", |
Fabio Utzig | c659ec5 | 2020-07-13 21:18:48 -0300 | [diff] [blame] | 91 | DeviceName::Nrf52840UnequalSlots => "Nrf52840UnequalSlots", |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 92 | DeviceName::PSoC6 => "PSoC6" |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 93 | }; |
| 94 | f.write_str(name) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[derive(Debug)] |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 99 | struct AlignArg(usize); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 100 | |
| 101 | struct AlignArgVisitor; |
| 102 | |
| 103 | impl<'de> serde::de::Visitor<'de> for AlignArgVisitor { |
| 104 | type Value = AlignArg; |
| 105 | |
David Brown | 10b5de1 | 2019-01-02 16:10:01 -0700 | [diff] [blame] | 106 | fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 107 | formatter.write_str("1, 2, 4 or 8") |
| 108 | } |
| 109 | |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 110 | fn visit_u32<E>(self, n: u32) -> Result<Self::Value, E> |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 111 | where E: serde::de::Error |
| 112 | { |
| 113 | Ok(match n { |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 114 | 1 | 2 | 4 | 8 => AlignArg(n as usize), |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 115 | n => { |
| 116 | let err = format!("Could not deserialize '{}' as alignment", n); |
| 117 | return Err(E::custom(err)); |
| 118 | } |
| 119 | }) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | impl<'de> serde::de::Deserialize<'de> for AlignArg { |
| 124 | fn deserialize<D>(d: D) -> Result<AlignArg, D::Error> |
| 125 | where D: serde::de::Deserializer<'de> |
| 126 | { |
| 127 | d.deserialize_u8(AlignArgVisitor) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | pub fn main() { |
| 132 | let args: Args = Docopt::new(USAGE) |
| 133 | .and_then(|d| d.deserialize()) |
| 134 | .unwrap_or_else(|e| e.exit()); |
| 135 | // println!("args: {:#?}", args); |
| 136 | |
| 137 | if args.cmd_sizes { |
| 138 | show_sizes(); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | let mut status = RunStatus::new(); |
| 143 | if args.cmd_run { |
| 144 | |
| 145 | let align = args.flag_align.map(|x| x.0).unwrap_or(1); |
| 146 | |
| 147 | |
| 148 | let device = match args.flag_device { |
| 149 | None => panic!("Missing mandatory device argument"), |
| 150 | Some(dev) => dev, |
| 151 | }; |
| 152 | |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 153 | status.run_single(device, align, 0xff); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if args.cmd_runall { |
| 157 | for &dev in ALL_DEVICES { |
| 158 | for &align in &[1, 2, 4, 8] { |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 159 | for &erased_val in &[0, 0xff] { |
| 160 | status.run_single(dev, align, erased_val); |
| 161 | } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if status.failures > 0 { |
| 167 | error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures); |
| 168 | process::exit(1); |
| 169 | } else { |
| 170 | error!("{} Tests ran successfully", status.passes); |
| 171 | process::exit(0); |
| 172 | } |
| 173 | } |
| 174 | |
David Brown | fc8e3c5 | 2021-03-10 05:11:26 -0700 | [diff] [blame] | 175 | #[derive(Default)] |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 176 | pub struct RunStatus { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 177 | failures: usize, |
| 178 | passes: usize, |
| 179 | } |
| 180 | |
| 181 | impl RunStatus { |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 182 | pub fn new() -> RunStatus { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 183 | RunStatus { |
| 184 | failures: 0, |
| 185 | passes: 0, |
| 186 | } |
| 187 | } |
| 188 | |
David Brown | 5a31775 | 2019-11-15 09:53:39 -0700 | [diff] [blame] | 189 | pub fn run_single(&mut self, device: DeviceName, align: usize, erased_val: u8) { |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 190 | warn!("Running on device {} with alignment {}", device, align); |
| 191 | |
David Brown | 5bc62c6 | 2019-03-05 12:11:48 -0700 | [diff] [blame] | 192 | let run = match ImagesBuilder::new(device, align, erased_val) { |
Fabio Utzig | 114a647 | 2019-11-28 10:24:09 -0300 | [diff] [blame] | 193 | Ok(builder) => builder, |
| 194 | Err(msg) => { |
| 195 | warn!("Skipping {}: {}", device, msg); |
David Brown | 5bc62c6 | 2019-03-05 12:11:48 -0700 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | }; |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 199 | |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 200 | let mut failed = false; |
| 201 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 202 | // Creates a badly signed image in the secondary slot to check that |
| 203 | // it is not upgraded to |
David Brown | 01617af | 2019-02-28 10:45:12 -0700 | [diff] [blame] | 204 | let bad_secondary_slot_image = run.clone().make_bad_secondary_slot_image(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 205 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 206 | failed |= bad_secondary_slot_image.run_signfail_upgrade(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 207 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 208 | let images = run.clone().make_no_upgrade_image(&NO_DEPS); |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 209 | failed |= images.run_norevert_newimage(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 210 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 211 | let images = run.make_image(&NO_DEPS, true); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 212 | |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 213 | failed |= images.run_basic_revert(); |
David Brown | c49811e | 2017-11-06 14:20:45 -0700 | [diff] [blame] | 214 | failed |= images.run_revert_with_fails(); |
| 215 | failed |= images.run_perm_with_fails(); |
| 216 | failed |= images.run_perm_with_random_fails(5); |
David Brown | 5f7ec2b | 2017-11-06 13:54:02 -0700 | [diff] [blame] | 217 | failed |= images.run_norevert(); |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 218 | |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 219 | failed |= images.run_with_status_fails_complete(); |
Fabio Utzig | eedcc45 | 2017-11-24 10:48:52 -0200 | [diff] [blame] | 220 | failed |= images.run_with_status_fails_with_reset(); |
Fabio Utzig | b841f0a | 2017-11-24 08:11:05 -0200 | [diff] [blame] | 221 | |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 222 | //show_flash(&flash); |
| 223 | |
| 224 | if failed { |
| 225 | self.failures += 1; |
| 226 | } else { |
| 227 | self.passes += 1; |
| 228 | } |
| 229 | } |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 230 | |
| 231 | pub fn failures(&self) -> usize { |
| 232 | self.failures |
| 233 | } |
David Brown | 2639e07 | 2017-10-11 11:18:44 -0600 | [diff] [blame] | 234 | } |