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