David Brown | c8d6201 | 2021-10-27 15:03:48 -0600 | [diff] [blame] | 1 | // Copyright (c) 2017-2021 Linaro LTD |
David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 2 | // Copyright (c) 2018-2019 JUUL Labs |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame^] | 3 | // Copyright (c) 2023 Arm Limited |
David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 4 | // |
| 5 | // SPDX-License-Identifier: Apache-2.0 |
| 6 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 7 | //! HAL api for MyNewt applications |
| 8 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 9 | use crate::area::CAreaDesc; |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 10 | use log::{Level, log_enabled, warn}; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 11 | use simflash::{Result, Flash, FlashPtr}; |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 12 | use std::{ |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 13 | cell::RefCell, |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 14 | collections::HashMap, |
| 15 | mem, |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 16 | ptr, |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 17 | slice, |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 18 | }; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 19 | |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 20 | /// A FlashMap maintain a table of [device_id -> Flash trait] |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 21 | pub type FlashMap = HashMap<u8, FlashPtr>; |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 22 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 23 | pub struct FlashParamsStruct { |
Gustavo Henrique Nihei | 4aa286d | 2021-11-24 14:54:56 -0300 | [diff] [blame] | 24 | align: u32, |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 25 | erased_val: u8, |
| 26 | } |
| 27 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 28 | pub type FlashParams = HashMap<u8, FlashParamsStruct>; |
| 29 | |
David Brown | d216b20 | 2021-06-04 10:14:33 -0600 | [diff] [blame] | 30 | /// The `boot_rsp` structure used by boot_go. |
| 31 | #[repr(C)] |
| 32 | #[derive(Debug)] |
| 33 | pub struct BootRsp { |
| 34 | pub br_hdr: *const ImageHeader, |
| 35 | pub flash_dev_id: u8, |
| 36 | pub image_off: u32, |
| 37 | } |
| 38 | |
| 39 | // TODO: Don't duplicate this image header declaration. |
| 40 | #[repr(C)] |
| 41 | #[derive(Debug)] |
| 42 | pub struct ImageHeader { |
| 43 | magic: u32, |
| 44 | load_addr: u32, |
| 45 | hdr_size: u16, |
| 46 | protect_tlv_size: u16, |
| 47 | img_size: u32, |
| 48 | flags: u32, |
| 49 | ver: ImageVersion, |
| 50 | _pad2: u32, |
| 51 | } |
| 52 | |
| 53 | #[repr(C)] |
| 54 | #[derive(Debug)] |
| 55 | pub struct ImageVersion { |
| 56 | pub major: u8, |
| 57 | pub minor: u8, |
| 58 | pub revision: u16, |
| 59 | pub build_num: u32, |
| 60 | } |
| 61 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 62 | pub struct CAreaDescPtr { |
| 63 | pub ptr: *const CAreaDesc, |
| 64 | } |
| 65 | |
| 66 | pub struct FlashContext { |
| 67 | flash_map: FlashMap, |
| 68 | flash_params: FlashParams, |
| 69 | flash_areas: CAreaDescPtr, |
| 70 | } |
| 71 | |
| 72 | impl FlashContext { |
| 73 | pub fn new() -> FlashContext { |
| 74 | FlashContext { |
| 75 | flash_map: HashMap::new(), |
| 76 | flash_params: HashMap::new(), |
| 77 | flash_areas: CAreaDescPtr{ptr: ptr::null()}, |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
David Brown | fc8e3c5 | 2021-03-10 05:11:26 -0700 | [diff] [blame] | 82 | impl Default for FlashContext { |
| 83 | fn default() -> FlashContext { |
| 84 | FlashContext { |
| 85 | flash_map: HashMap::new(), |
| 86 | flash_params: HashMap::new(), |
| 87 | flash_areas: CAreaDescPtr{ptr: ptr::null()}, |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 92 | #[repr(C)] |
| 93 | #[derive(Debug, Default)] |
| 94 | pub struct CSimContext { |
| 95 | pub flash_counter: libc::c_int, |
| 96 | pub jumped: libc::c_int, |
| 97 | pub c_asserts: u8, |
| 98 | pub c_catch_asserts: u8, |
| 99 | // NOTE: Always leave boot_jmpbuf declaration at the end; this should |
| 100 | // store a "jmp_buf" which is arch specific and not defined by libc crate. |
| 101 | // The size below is enough to store data on a x86_64 machine. |
| 102 | pub boot_jmpbuf: [u64; 16], |
| 103 | } |
| 104 | |
| 105 | pub struct CSimContextPtr { |
| 106 | pub ptr: *const CSimContext, |
| 107 | } |
| 108 | |
| 109 | impl CSimContextPtr { |
| 110 | pub fn new() -> CSimContextPtr { |
| 111 | CSimContextPtr { |
| 112 | ptr: ptr::null(), |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
David Brown | fc8e3c5 | 2021-03-10 05:11:26 -0700 | [diff] [blame] | 117 | impl Default for CSimContextPtr { |
| 118 | fn default() -> CSimContextPtr { |
| 119 | CSimContextPtr { |
| 120 | ptr: ptr::null(), |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
David Brown | 8a4e23b | 2021-06-11 10:29:01 -0600 | [diff] [blame] | 125 | /// This struct describes the RAM layout of the current device. It will be stashed, per test |
| 126 | /// thread, and queried by the C code. |
| 127 | #[repr(C)] |
| 128 | #[derive(Debug, Default)] |
| 129 | pub struct BootsimRamInfo { |
| 130 | pub start: u32, |
| 131 | pub size: u32, |
| 132 | pub base: usize, |
| 133 | } |
| 134 | |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame^] | 135 | /// This struct stores the non-volatile security counter per image. It will be stored per test thread, |
| 136 | /// and the C code will set / get the values here. |
| 137 | #[repr(C)] |
| 138 | #[derive(Debug, Default)] |
| 139 | pub struct NvCounterStorage { |
| 140 | pub storage: Vec<u32>, |
| 141 | } |
| 142 | |
| 143 | impl NvCounterStorage { |
| 144 | pub fn new() -> Self { |
| 145 | let count = if cfg!(feature = "multiimage") { |
| 146 | 2 |
| 147 | } else { |
| 148 | 1 |
| 149 | }; |
| 150 | Self { |
| 151 | storage: vec![0; count] |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 156 | thread_local! { |
| 157 | pub static THREAD_CTX: RefCell<FlashContext> = RefCell::new(FlashContext::new()); |
| 158 | pub static SIM_CTX: RefCell<CSimContextPtr> = RefCell::new(CSimContextPtr::new()); |
David Brown | 8a4e23b | 2021-06-11 10:29:01 -0600 | [diff] [blame] | 159 | pub static RAM_CTX: RefCell<BootsimRamInfo> = RefCell::new(BootsimRamInfo::default()); |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame^] | 160 | pub static NV_COUNTER_CTX: RefCell<NvCounterStorage> = RefCell::new(NvCounterStorage::new()); |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 161 | } |
| 162 | |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 163 | /// Set the flash device to be used by the simulation. The pointer is unsafely stashed away. |
| 164 | /// |
| 165 | /// # Safety |
| 166 | /// |
| 167 | /// This uses mem::transmute to stash a Rust pointer into a C value to |
| 168 | /// retrieve later. It should be safe to use this. |
| 169 | pub fn set_flash(dev_id: u8, dev: &mut dyn Flash) { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 170 | THREAD_CTX.with(|ctx| { |
| 171 | ctx.borrow_mut().flash_params.insert(dev_id, FlashParamsStruct { |
Gustavo Henrique Nihei | 4aa286d | 2021-11-24 14:54:56 -0300 | [diff] [blame] | 172 | align: dev.align() as u32, |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 173 | erased_val: dev.erased_val(), |
| 174 | }); |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 175 | unsafe { |
| 176 | let dev: &'static mut dyn Flash = mem::transmute(dev); |
| 177 | ctx.borrow_mut().flash_map.insert( |
| 178 | dev_id, FlashPtr{ptr: dev as *mut dyn Flash}); |
| 179 | } |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 180 | }); |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 181 | } |
| 182 | |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 183 | pub fn clear_flash(dev_id: u8) { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 184 | THREAD_CTX.with(|ctx| { |
| 185 | ctx.borrow_mut().flash_map.remove(&dev_id); |
| 186 | }); |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 187 | } |
| 188 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 189 | // This isn't meant to call directly, but by a wrapper. |
| 190 | |
| 191 | #[no_mangle] |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 192 | pub extern fn sim_get_flash_areas() -> *const CAreaDesc { |
| 193 | THREAD_CTX.with(|ctx| { |
| 194 | ctx.borrow().flash_areas.ptr |
| 195 | }) |
| 196 | } |
| 197 | |
| 198 | #[no_mangle] |
| 199 | pub extern fn sim_set_flash_areas(areas: *const CAreaDesc) { |
| 200 | THREAD_CTX.with(|ctx| { |
| 201 | ctx.borrow_mut().flash_areas.ptr = areas; |
| 202 | }); |
| 203 | } |
| 204 | |
| 205 | #[no_mangle] |
| 206 | pub extern fn sim_reset_flash_areas() { |
| 207 | THREAD_CTX.with(|ctx| { |
| 208 | ctx.borrow_mut().flash_areas.ptr = ptr::null(); |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | #[no_mangle] |
| 213 | pub extern fn sim_get_context() -> *const CSimContext { |
| 214 | SIM_CTX.with(|ctx| { |
| 215 | ctx.borrow().ptr |
| 216 | }) |
| 217 | } |
| 218 | |
| 219 | #[no_mangle] |
| 220 | pub extern fn sim_set_context(ptr: *const CSimContext) { |
| 221 | SIM_CTX.with(|ctx| { |
| 222 | ctx.borrow_mut().ptr = ptr; |
| 223 | }); |
| 224 | } |
| 225 | |
| 226 | #[no_mangle] |
| 227 | pub extern fn sim_reset_context() { |
| 228 | SIM_CTX.with(|ctx| { |
| 229 | ctx.borrow_mut().ptr = ptr::null(); |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | #[no_mangle] |
David Brown | 8a4e23b | 2021-06-11 10:29:01 -0600 | [diff] [blame] | 234 | pub extern "C" fn bootsim_get_ram_info() -> *const BootsimRamInfo { |
| 235 | RAM_CTX.with(|ctx| { |
| 236 | if ctx.borrow().base == 0 { |
| 237 | // Option is messier to get a pointer out of, so just check if the base has been set to |
| 238 | // anything. |
| 239 | panic!("ram info not set, but being used"); |
| 240 | } |
| 241 | ctx.as_ptr() |
| 242 | }) |
| 243 | } |
| 244 | |
| 245 | /// Store a copy of this RAM info. |
| 246 | pub fn set_ram_info(info: BootsimRamInfo) { |
| 247 | RAM_CTX.with(|ctx| { |
| 248 | ctx.replace(info); |
| 249 | }); |
| 250 | } |
| 251 | |
| 252 | /// Clear out the ram info. |
| 253 | pub fn clear_ram_info() { |
| 254 | RAM_CTX.with(|ctx| { |
| 255 | ctx.borrow_mut().base = 0; |
| 256 | }); |
| 257 | } |
| 258 | |
| 259 | #[no_mangle] |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 260 | pub extern fn sim_flash_erase(dev_id: u8, offset: u32, size: u32) -> libc::c_int { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 261 | let mut rc: libc::c_int = -19; |
| 262 | THREAD_CTX.with(|ctx| { |
| 263 | if let Some(flash) = ctx.borrow().flash_map.get(&dev_id) { |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 264 | let dev = unsafe { &mut *(flash.ptr) }; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 265 | rc = map_err(dev.erase(offset as usize, size as usize)); |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 266 | } |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 267 | }); |
| 268 | rc |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | #[no_mangle] |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 272 | pub extern fn sim_flash_read(dev_id: u8, offset: u32, dest: *mut u8, size: u32) -> libc::c_int { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 273 | let mut rc: libc::c_int = -19; |
| 274 | THREAD_CTX.with(|ctx| { |
| 275 | if let Some(flash) = ctx.borrow().flash_map.get(&dev_id) { |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 276 | let mut buf: &mut[u8] = unsafe { slice::from_raw_parts_mut(dest, size as usize) }; |
| 277 | let dev = unsafe { &mut *(flash.ptr) }; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 278 | rc = map_err(dev.read(offset as usize, &mut buf)); |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 279 | } |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 280 | }); |
| 281 | rc |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | #[no_mangle] |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 285 | pub extern fn sim_flash_write(dev_id: u8, offset: u32, src: *const u8, size: u32) -> libc::c_int { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 286 | let mut rc: libc::c_int = -19; |
| 287 | THREAD_CTX.with(|ctx| { |
| 288 | if let Some(flash) = ctx.borrow().flash_map.get(&dev_id) { |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 289 | let buf: &[u8] = unsafe { slice::from_raw_parts(src, size as usize) }; |
| 290 | let dev = unsafe { &mut *(flash.ptr) }; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 291 | rc = map_err(dev.write(offset as usize, &buf)); |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 292 | } |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 293 | }); |
| 294 | rc |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 297 | #[no_mangle] |
Gustavo Henrique Nihei | 4aa286d | 2021-11-24 14:54:56 -0300 | [diff] [blame] | 298 | pub extern fn sim_flash_align(id: u8) -> u32 { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 299 | THREAD_CTX.with(|ctx| { |
| 300 | ctx.borrow().flash_params.get(&id).unwrap().align |
| 301 | }) |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | #[no_mangle] |
| 305 | pub extern fn sim_flash_erased_val(id: u8) -> u8 { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 306 | THREAD_CTX.with(|ctx| { |
| 307 | ctx.borrow().flash_params.get(&id).unwrap().erased_val |
| 308 | }) |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 309 | } |
| 310 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 311 | fn map_err(err: Result<()>) -> libc::c_int { |
| 312 | match err { |
| 313 | Ok(()) => 0, |
Fabio Utzig | 19b2c1a | 2017-04-20 07:32:44 -0300 | [diff] [blame] | 314 | Err(e) => { |
| 315 | warn!("{}", e); |
| 316 | -1 |
| 317 | }, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
David Brown | 2d1d7cf | 2017-05-10 08:55:09 -0600 | [diff] [blame] | 320 | |
| 321 | /// Called by C code to determine if we should log at this level. Levels are defined in |
| 322 | /// bootutil/bootutil_log.h. This makes the logging from the C code controlled by bootsim::api, so |
| 323 | /// for example, it can be enabled with something like: |
| 324 | /// RUST_LOG=bootsim::api=info cargo run --release runall |
| 325 | /// or |
| 326 | /// RUST_LOG=bootsim=info cargo run --release runall |
| 327 | #[no_mangle] |
| 328 | pub extern fn sim_log_enabled(level: libc::c_int) -> libc::c_int { |
| 329 | let res = match level { |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 330 | 1 => log_enabled!(Level::Error), |
| 331 | 2 => log_enabled!(Level::Warn), |
| 332 | 3 => log_enabled!(Level::Info), |
Fabio Utzig | e92df93 | 2019-12-10 14:29:17 -0300 | [diff] [blame] | 333 | 4 => log_enabled!(Level::Debug), |
| 334 | 5 => log_enabled!(Level::Trace), // log level == SIM |
David Brown | 2d1d7cf | 2017-05-10 08:55:09 -0600 | [diff] [blame] | 335 | _ => false, |
| 336 | }; |
| 337 | if res { |
| 338 | 1 |
| 339 | } else { |
| 340 | 0 |
| 341 | } |
| 342 | } |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame^] | 343 | |
| 344 | #[no_mangle] |
| 345 | pub extern "C" fn sim_set_nv_counter_for_image(image_index: u32, security_counter_value: u32) -> libc::c_int { |
| 346 | let mut rc = 0; |
| 347 | NV_COUNTER_CTX.with(|ctx| { |
| 348 | let mut counter_storage = ctx.borrow_mut(); |
| 349 | if image_index as usize >= counter_storage.storage.len() { |
| 350 | rc = -1; |
| 351 | return; |
| 352 | } |
| 353 | if counter_storage.storage[image_index as usize] > security_counter_value { |
| 354 | rc = -2; |
| 355 | warn!("Failed to set security counter value ({}) for image index {}", security_counter_value, image_index); |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | counter_storage.storage[image_index as usize] = security_counter_value; |
| 360 | }); |
| 361 | |
| 362 | return rc; |
| 363 | } |
| 364 | |
| 365 | #[no_mangle] |
| 366 | pub extern "C" fn sim_get_nv_counter_for_image(image_index: u32, security_counter_value: *mut u32) -> libc::c_int { |
| 367 | let mut rc = 0; |
| 368 | NV_COUNTER_CTX.with(|ctx| { |
| 369 | let counter_storage = ctx.borrow(); |
| 370 | if image_index as usize >= counter_storage.storage.len() { |
| 371 | rc = -1; |
| 372 | return; |
| 373 | } |
| 374 | unsafe { *security_counter_value = counter_storage.storage[image_index as usize] }; |
| 375 | |
| 376 | }); |
| 377 | return rc; |
| 378 | } |