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