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