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