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 | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 7 | |
| 8 | /// Invoke the bootloader on this flash device. |
| 9 | pub fn boot_go(flash: &mut Flash, areadesc: &AreaDesc) -> i32 { |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 10 | unsafe { api::set_flash(flash) }; |
| 11 | let result = unsafe { raw::invoke_boot_go(&areadesc.get_c() as *const _) as i32 }; |
| 12 | unsafe { api::clear_flash(); }; |
| 13 | result |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | /// Setter/getter for the flash counter. This isn't thread safe. |
| 17 | pub fn get_flash_counter() -> i32 { |
| 18 | unsafe { raw::flash_counter as i32 } |
| 19 | } |
| 20 | |
| 21 | /// Set the flash counter. Zero indicates the flash should not be interrupted. The counter will |
| 22 | /// then go negative for each flash operation. |
| 23 | pub fn set_flash_counter(counter: i32) { |
| 24 | unsafe { raw::flash_counter = counter as libc::c_int }; |
| 25 | } |
| 26 | |
| 27 | pub fn boot_trailer_sz() -> u32 { |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 28 | unsafe { raw::boot_slots_trailer_sz(raw::sim_flash_align) } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | pub fn get_sim_flash_align() -> u8 { |
| 32 | unsafe { raw::sim_flash_align } |
| 33 | } |
| 34 | |
| 35 | pub fn set_sim_flash_align(align: u8) { |
| 36 | unsafe { raw::sim_flash_align = align }; |
| 37 | } |
| 38 | |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 39 | pub fn boot_magic_sz() -> usize { |
| 40 | unsafe { raw::BOOT_MAGIC_SZ as usize } |
| 41 | } |
| 42 | |
| 43 | pub fn boot_max_align() -> usize { |
| 44 | unsafe { raw::BOOT_MAX_ALIGN as usize } |
| 45 | } |
| 46 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 47 | mod raw { |
| 48 | use area::CAreaDesc; |
| 49 | use libc; |
| 50 | |
| 51 | extern "C" { |
| 52 | // This generates a warning about `CAreaDesc` not being foreign safe. There doesn't appear to |
| 53 | // be any way to get rid of this warning. See https://github.com/rust-lang/rust/issues/34798 |
| 54 | // for information and tracking. |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 55 | pub fn invoke_boot_go(areadesc: *const CAreaDesc) -> libc::c_int; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 56 | pub static mut flash_counter: libc::c_int; |
| 57 | |
| 58 | pub static mut sim_flash_align: u8; |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 59 | pub fn boot_slots_trailer_sz(min_write_sz: u8) -> u32; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 60 | |
| 61 | pub static BOOT_MAGIC_SZ: u32; |
| 62 | pub static BOOT_MAX_ALIGN: u32; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 63 | } |
| 64 | } |