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