David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2017-2019 Linaro LTD |
| 2 | // Copyright (c) 2018-2019 JUUL Labs |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 6 | //! HAL api for MyNewt applications |
| 7 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 8 | use crate::area::CAreaDesc; |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 9 | use log::{Level, log_enabled, warn}; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 10 | use simflash::{Result, Flash, FlashPtr}; |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 11 | use std::{ |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 12 | cell::RefCell, |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 13 | collections::HashMap, |
| 14 | mem, |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 15 | ptr, |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 16 | slice, |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 17 | }; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 18 | |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 19 | /// A FlashMap maintain a table of [device_id -> Flash trait] |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 20 | pub type FlashMap = HashMap<u8, FlashPtr>; |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 21 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 22 | pub struct FlashParamsStruct { |
Fabio Utzig | 1edb788 | 2020-10-04 11:51:53 -0300 | [diff] [blame] | 23 | align: u16, |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 24 | erased_val: u8, |
| 25 | } |
| 26 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 27 | pub type FlashParams = HashMap<u8, FlashParamsStruct>; |
| 28 | |
| 29 | pub struct CAreaDescPtr { |
| 30 | pub ptr: *const CAreaDesc, |
| 31 | } |
| 32 | |
| 33 | pub struct FlashContext { |
| 34 | flash_map: FlashMap, |
| 35 | flash_params: FlashParams, |
| 36 | flash_areas: CAreaDescPtr, |
| 37 | } |
| 38 | |
| 39 | impl FlashContext { |
| 40 | pub fn new() -> FlashContext { |
| 41 | FlashContext { |
| 42 | flash_map: HashMap::new(), |
| 43 | flash_params: HashMap::new(), |
| 44 | flash_areas: CAreaDescPtr{ptr: ptr::null()}, |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
David Brown | fc8e3c5 | 2021-03-10 05:11:26 -0700 | [diff] [blame] | 49 | impl Default for FlashContext { |
| 50 | fn default() -> FlashContext { |
| 51 | FlashContext { |
| 52 | flash_map: HashMap::new(), |
| 53 | flash_params: HashMap::new(), |
| 54 | flash_areas: CAreaDescPtr{ptr: ptr::null()}, |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 59 | #[repr(C)] |
| 60 | #[derive(Debug, Default)] |
| 61 | pub struct CSimContext { |
| 62 | pub flash_counter: libc::c_int, |
| 63 | pub jumped: libc::c_int, |
| 64 | pub c_asserts: u8, |
| 65 | pub c_catch_asserts: u8, |
| 66 | // NOTE: Always leave boot_jmpbuf declaration at the end; this should |
| 67 | // store a "jmp_buf" which is arch specific and not defined by libc crate. |
| 68 | // The size below is enough to store data on a x86_64 machine. |
| 69 | pub boot_jmpbuf: [u64; 16], |
| 70 | } |
| 71 | |
| 72 | pub struct CSimContextPtr { |
| 73 | pub ptr: *const CSimContext, |
| 74 | } |
| 75 | |
| 76 | impl CSimContextPtr { |
| 77 | pub fn new() -> CSimContextPtr { |
| 78 | CSimContextPtr { |
| 79 | ptr: ptr::null(), |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
David Brown | fc8e3c5 | 2021-03-10 05:11:26 -0700 | [diff] [blame] | 84 | impl Default for CSimContextPtr { |
| 85 | fn default() -> CSimContextPtr { |
| 86 | CSimContextPtr { |
| 87 | ptr: ptr::null(), |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 92 | thread_local! { |
| 93 | pub static THREAD_CTX: RefCell<FlashContext> = RefCell::new(FlashContext::new()); |
| 94 | pub static SIM_CTX: RefCell<CSimContextPtr> = RefCell::new(CSimContextPtr::new()); |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 95 | } |
| 96 | |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 97 | /// Set the flash device to be used by the simulation. The pointer is unsafely stashed away. |
| 98 | /// |
| 99 | /// # Safety |
| 100 | /// |
| 101 | /// This uses mem::transmute to stash a Rust pointer into a C value to |
| 102 | /// retrieve later. It should be safe to use this. |
| 103 | pub fn set_flash(dev_id: u8, dev: &mut dyn Flash) { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 104 | THREAD_CTX.with(|ctx| { |
| 105 | ctx.borrow_mut().flash_params.insert(dev_id, FlashParamsStruct { |
Fabio Utzig | 1edb788 | 2020-10-04 11:51:53 -0300 | [diff] [blame] | 106 | align: dev.align() as u16, |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 107 | erased_val: dev.erased_val(), |
| 108 | }); |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 109 | unsafe { |
| 110 | let dev: &'static mut dyn Flash = mem::transmute(dev); |
| 111 | ctx.borrow_mut().flash_map.insert( |
| 112 | dev_id, FlashPtr{ptr: dev as *mut dyn Flash}); |
| 113 | } |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 114 | }); |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 115 | } |
| 116 | |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 117 | pub fn clear_flash(dev_id: u8) { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 118 | THREAD_CTX.with(|ctx| { |
| 119 | ctx.borrow_mut().flash_map.remove(&dev_id); |
| 120 | }); |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 121 | } |
| 122 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 123 | // This isn't meant to call directly, but by a wrapper. |
| 124 | |
| 125 | #[no_mangle] |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 126 | pub extern fn sim_get_flash_areas() -> *const CAreaDesc { |
| 127 | THREAD_CTX.with(|ctx| { |
| 128 | ctx.borrow().flash_areas.ptr |
| 129 | }) |
| 130 | } |
| 131 | |
| 132 | #[no_mangle] |
| 133 | pub extern fn sim_set_flash_areas(areas: *const CAreaDesc) { |
| 134 | THREAD_CTX.with(|ctx| { |
| 135 | ctx.borrow_mut().flash_areas.ptr = areas; |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | #[no_mangle] |
| 140 | pub extern fn sim_reset_flash_areas() { |
| 141 | THREAD_CTX.with(|ctx| { |
| 142 | ctx.borrow_mut().flash_areas.ptr = ptr::null(); |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | #[no_mangle] |
| 147 | pub extern fn sim_get_context() -> *const CSimContext { |
| 148 | SIM_CTX.with(|ctx| { |
| 149 | ctx.borrow().ptr |
| 150 | }) |
| 151 | } |
| 152 | |
| 153 | #[no_mangle] |
| 154 | pub extern fn sim_set_context(ptr: *const CSimContext) { |
| 155 | SIM_CTX.with(|ctx| { |
| 156 | ctx.borrow_mut().ptr = ptr; |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | #[no_mangle] |
| 161 | pub extern fn sim_reset_context() { |
| 162 | SIM_CTX.with(|ctx| { |
| 163 | ctx.borrow_mut().ptr = ptr::null(); |
| 164 | }); |
| 165 | } |
| 166 | |
| 167 | #[no_mangle] |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 168 | 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] | 169 | let mut rc: libc::c_int = -19; |
| 170 | THREAD_CTX.with(|ctx| { |
| 171 | if let Some(flash) = ctx.borrow().flash_map.get(&dev_id) { |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 172 | let dev = unsafe { &mut *(flash.ptr) }; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 173 | rc = map_err(dev.erase(offset as usize, size as usize)); |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 174 | } |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 175 | }); |
| 176 | rc |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | #[no_mangle] |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 180 | 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] | 181 | let mut rc: libc::c_int = -19; |
| 182 | THREAD_CTX.with(|ctx| { |
| 183 | if let Some(flash) = ctx.borrow().flash_map.get(&dev_id) { |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 184 | let mut buf: &mut[u8] = unsafe { slice::from_raw_parts_mut(dest, size as usize) }; |
| 185 | let dev = unsafe { &mut *(flash.ptr) }; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 186 | rc = map_err(dev.read(offset as usize, &mut buf)); |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 187 | } |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 188 | }); |
| 189 | rc |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | #[no_mangle] |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 193 | 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] | 194 | let mut rc: libc::c_int = -19; |
| 195 | THREAD_CTX.with(|ctx| { |
| 196 | if let Some(flash) = ctx.borrow().flash_map.get(&dev_id) { |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 197 | let buf: &[u8] = unsafe { slice::from_raw_parts(src, size as usize) }; |
| 198 | let dev = unsafe { &mut *(flash.ptr) }; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 199 | rc = map_err(dev.write(offset as usize, &buf)); |
Fabio Utzig | 1c9aea5 | 2018-11-15 10:36:07 -0200 | [diff] [blame] | 200 | } |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 201 | }); |
| 202 | rc |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 205 | #[no_mangle] |
Fabio Utzig | 1edb788 | 2020-10-04 11:51:53 -0300 | [diff] [blame] | 206 | pub extern fn sim_flash_align(id: u8) -> u16 { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 207 | THREAD_CTX.with(|ctx| { |
| 208 | ctx.borrow().flash_params.get(&id).unwrap().align |
| 209 | }) |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | #[no_mangle] |
| 213 | pub extern fn sim_flash_erased_val(id: u8) -> u8 { |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 214 | THREAD_CTX.with(|ctx| { |
| 215 | ctx.borrow().flash_params.get(&id).unwrap().erased_val |
| 216 | }) |
Fabio Utzig | 73ffc44 | 2018-10-24 21:49:09 -0300 | [diff] [blame] | 217 | } |
| 218 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 219 | fn map_err(err: Result<()>) -> libc::c_int { |
| 220 | match err { |
| 221 | Ok(()) => 0, |
Fabio Utzig | 19b2c1a | 2017-04-20 07:32:44 -0300 | [diff] [blame] | 222 | Err(e) => { |
| 223 | warn!("{}", e); |
| 224 | -1 |
| 225 | }, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
David Brown | 2d1d7cf | 2017-05-10 08:55:09 -0600 | [diff] [blame] | 228 | |
| 229 | /// Called by C code to determine if we should log at this level. Levels are defined in |
| 230 | /// bootutil/bootutil_log.h. This makes the logging from the C code controlled by bootsim::api, so |
| 231 | /// for example, it can be enabled with something like: |
| 232 | /// RUST_LOG=bootsim::api=info cargo run --release runall |
| 233 | /// or |
| 234 | /// RUST_LOG=bootsim=info cargo run --release runall |
| 235 | #[no_mangle] |
| 236 | pub extern fn sim_log_enabled(level: libc::c_int) -> libc::c_int { |
| 237 | let res = match level { |
David Brown | 2821564 | 2019-01-02 11:42:39 -0700 | [diff] [blame] | 238 | 1 => log_enabled!(Level::Error), |
| 239 | 2 => log_enabled!(Level::Warn), |
| 240 | 3 => log_enabled!(Level::Info), |
Fabio Utzig | e92df93 | 2019-12-10 14:29:17 -0300 | [diff] [blame] | 241 | 4 => log_enabled!(Level::Debug), |
| 242 | 5 => log_enabled!(Level::Trace), // log level == SIM |
David Brown | 2d1d7cf | 2017-05-10 08:55:09 -0600 | [diff] [blame] | 243 | _ => false, |
| 244 | }; |
| 245 | if res { |
| 246 | 1 |
| 247 | } else { |
| 248 | 0 |
| 249 | } |
| 250 | } |