David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 1 | /// Interface wrappers to C API entering to the bootloader |
| 2 | |
| 3 | use area::AreaDesc; |
David Brown | 2cbc470 | 2017-07-06 14:18:58 -0600 | [diff] [blame] | 4 | use simflash::Flash; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 5 | use libc; |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 6 | use api; |
David Brown | 353610d | 2017-11-06 11:31:31 -0700 | [diff] [blame] | 7 | use std::sync::Mutex; |
| 8 | |
| 9 | lazy_static! { |
| 10 | /// Mutex to lock the simulation. The C code for the bootloader uses |
| 11 | /// global variables, and is therefore non-reentrant. |
| 12 | static ref BOOT_LOCK: Mutex<()> = Mutex::new(()); |
| 13 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 14 | |
| 15 | /// Invoke the bootloader on this flash device. |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 16 | pub fn boot_go(flash: &mut Flash, areadesc: &AreaDesc, counter: Option<&mut i32>, |
| 17 | align: u8, catch_asserts: bool) -> (i32, u8) { |
David Brown | 353610d | 2017-11-06 11:31:31 -0700 | [diff] [blame] | 18 | let _lock = BOOT_LOCK.lock().unwrap(); |
| 19 | |
David Brown | ee61c83 | 2017-11-06 11:13:25 -0700 | [diff] [blame] | 20 | unsafe { |
| 21 | api::set_flash(flash); |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 22 | raw::c_catch_asserts = if catch_asserts { 1 } else { 0 }; |
| 23 | raw::c_asserts = 0u8; |
David Brown | 541860c | 2017-11-06 11:25:42 -0700 | [diff] [blame] | 24 | raw::sim_flash_align = align; |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 25 | raw::sim_flash_erased_val = flash.erased_val(); |
David Brown | ee61c83 | 2017-11-06 11:13:25 -0700 | [diff] [blame] | 26 | raw::flash_counter = match counter { |
| 27 | None => 0, |
| 28 | Some(ref c) => **c as libc::c_int |
| 29 | }; |
| 30 | } |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 31 | let result = unsafe { raw::invoke_boot_go(&areadesc.get_c() as *const _) as i32 }; |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 32 | let asserts = unsafe { raw::c_asserts }; |
David Brown | ee61c83 | 2017-11-06 11:13:25 -0700 | [diff] [blame] | 33 | unsafe { |
| 34 | counter.map(|c| *c = raw::flash_counter as i32); |
| 35 | api::clear_flash(); |
| 36 | }; |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 37 | (result, asserts) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 38 | } |
| 39 | |
David Brown | 541860c | 2017-11-06 11:25:42 -0700 | [diff] [blame] | 40 | pub fn boot_trailer_sz(align: u8) -> u32 { |
| 41 | unsafe { raw::boot_slots_trailer_sz(align) } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 44 | pub fn boot_magic_sz() -> usize { |
| 45 | unsafe { raw::BOOT_MAGIC_SZ as usize } |
| 46 | } |
| 47 | |
| 48 | pub fn boot_max_align() -> usize { |
| 49 | unsafe { raw::BOOT_MAX_ALIGN as usize } |
| 50 | } |
| 51 | |
Fabio Utzig | 92be3fb | 2017-12-05 08:52:53 -0200 | [diff] [blame] | 52 | pub fn ecdsa256_sign(privkey: &[u8], hash: &[u8]) -> Result<[u8; 64], &'static str> { |
| 53 | unsafe { |
| 54 | let mut signature: [u8; 64] = [0; 64]; |
| 55 | if raw::ecdsa256_sign_(privkey.as_ptr(), hash.as_ptr(), |
| 56 | hash.len() as u32, signature.as_mut_ptr()) == 1 { |
| 57 | return Ok(signature); |
| 58 | } |
| 59 | return Err("Failed signature generation"); |
| 60 | } |
| 61 | } |
| 62 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 63 | mod raw { |
| 64 | use area::CAreaDesc; |
| 65 | use libc; |
| 66 | |
| 67 | extern "C" { |
| 68 | // This generates a warning about `CAreaDesc` not being foreign safe. There doesn't appear to |
| 69 | // be any way to get rid of this warning. See https://github.com/rust-lang/rust/issues/34798 |
| 70 | // for information and tracking. |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 71 | pub fn invoke_boot_go(areadesc: *const CAreaDesc) -> libc::c_int; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 72 | pub static mut flash_counter: libc::c_int; |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 73 | pub static mut c_asserts: u8; |
| 74 | pub static mut c_catch_asserts: u8; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 75 | |
| 76 | pub static mut sim_flash_align: u8; |
Fabio Utzig | ea0290b | 2018-08-09 14:23:01 -0300 | [diff] [blame] | 77 | pub static mut sim_flash_erased_val: u8; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 78 | pub fn boot_slots_trailer_sz(min_write_sz: u8) -> u32; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 79 | |
| 80 | pub static BOOT_MAGIC_SZ: u32; |
| 81 | pub static BOOT_MAX_ALIGN: u32; |
Fabio Utzig | 92be3fb | 2017-12-05 08:52:53 -0200 | [diff] [blame] | 82 | |
| 83 | pub fn ecdsa256_sign_(privkey: *const u8, hash: *const u8, |
| 84 | hash_len: libc::c_uint, |
| 85 | signature: *mut u8) -> libc::c_int; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 86 | } |
| 87 | } |