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