David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 1 | //! HAL api for MyNewt applications |
| 2 | |
David Brown | 2cbc470 | 2017-07-06 14:18:58 -0600 | [diff] [blame] | 3 | use simflash::{Result, Flash}; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 4 | use libc; |
David Brown | 2d1d7cf | 2017-05-10 08:55:09 -0600 | [diff] [blame] | 5 | use log::LogLevel; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 6 | use std::mem; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 7 | use std::slice; |
| 8 | |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 9 | // The current active flash device. The 'static is a lie, and we manage the lifetime ourselves. |
| 10 | static mut FLASH: Option<*mut Flash> = None; |
| 11 | |
| 12 | // Set the flash device to be used by the simulation. The pointer is unsafely stashed away. |
| 13 | pub unsafe fn set_flash(dev: &mut Flash) { |
David Brown | 7ddec0b | 2017-07-06 10:47:35 -0600 | [diff] [blame] | 14 | let dev: &'static mut Flash = mem::transmute(dev); |
| 15 | FLASH = Some(dev as *mut Flash); |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | pub unsafe fn clear_flash() { |
| 19 | FLASH = None; |
| 20 | } |
| 21 | |
| 22 | // Retrieve the flash, returning an error from the enclosing function. We can't panic here because |
| 23 | // we've called through C and unwinding is prohibited (it seems to just exit the program). |
| 24 | macro_rules! get_flash { |
| 25 | () => { |
| 26 | match FLASH { |
| 27 | Some(x) => &mut *x, |
| 28 | None => return -19, |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 33 | // This isn't meant to call directly, but by a wrapper. |
| 34 | |
| 35 | #[no_mangle] |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame^] | 36 | pub extern fn sim_flash_erase(_id: u8, offset: u32, size: u32) -> libc::c_int { |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 37 | let dev = unsafe { get_flash!() }; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 38 | map_err(dev.erase(offset as usize, size as usize)) |
| 39 | } |
| 40 | |
| 41 | #[no_mangle] |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame^] | 42 | pub extern fn sim_flash_read(_id: u8, offset: u32, dest: *mut u8, size: u32) -> libc::c_int { |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 43 | let dev = unsafe { get_flash!() }; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 44 | let mut buf: &mut[u8] = unsafe { slice::from_raw_parts_mut(dest, size as usize) }; |
| 45 | map_err(dev.read(offset as usize, &mut buf)) |
| 46 | } |
| 47 | |
| 48 | #[no_mangle] |
Fabio Utzig | 99dfc78 | 2018-10-15 15:10:55 -0700 | [diff] [blame^] | 49 | pub extern fn sim_flash_write(_id: u8, offset: u32, src: *const u8, size: u32) -> libc::c_int { |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 50 | let dev = unsafe { get_flash!() }; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 51 | let buf: &[u8] = unsafe { slice::from_raw_parts(src, size as usize) }; |
| 52 | map_err(dev.write(offset as usize, &buf)) |
| 53 | } |
| 54 | |
| 55 | fn map_err(err: Result<()>) -> libc::c_int { |
| 56 | match err { |
| 57 | Ok(()) => 0, |
Fabio Utzig | 19b2c1a | 2017-04-20 07:32:44 -0300 | [diff] [blame] | 58 | Err(e) => { |
| 59 | warn!("{}", e); |
| 60 | -1 |
| 61 | }, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
David Brown | 2d1d7cf | 2017-05-10 08:55:09 -0600 | [diff] [blame] | 64 | |
| 65 | /// Called by C code to determine if we should log at this level. Levels are defined in |
| 66 | /// bootutil/bootutil_log.h. This makes the logging from the C code controlled by bootsim::api, so |
| 67 | /// for example, it can be enabled with something like: |
| 68 | /// RUST_LOG=bootsim::api=info cargo run --release runall |
| 69 | /// or |
| 70 | /// RUST_LOG=bootsim=info cargo run --release runall |
| 71 | #[no_mangle] |
| 72 | pub extern fn sim_log_enabled(level: libc::c_int) -> libc::c_int { |
| 73 | let res = match level { |
| 74 | 1 => log_enabled!(LogLevel::Error), |
| 75 | 2 => log_enabled!(LogLevel::Warn), |
| 76 | 3 => log_enabled!(LogLevel::Info), |
| 77 | 4 => log_enabled!(LogLevel::Trace), |
| 78 | _ => false, |
| 79 | }; |
| 80 | if res { |
| 81 | 1 |
| 82 | } else { |
| 83 | 0 |
| 84 | } |
| 85 | } |