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