blob: f5643ab3bc0f322dfb3750513813326e01965909 [file] [log] [blame]
David Brownde7729e2017-01-09 10:41:35 -07001/// Interface wrappers to C API entering to the bootloader
2
3use area::AreaDesc;
David Brown2cbc4702017-07-06 14:18:58 -06004use simflash::Flash;
David Brownde7729e2017-01-09 10:41:35 -07005use libc;
David Brownbdb6db72017-07-06 10:14:37 -06006use api;
David Brownde7729e2017-01-09 10:41:35 -07007
8/// Invoke the bootloader on this flash device.
David Brown541860c2017-11-06 11:25:42 -07009pub fn boot_go(flash: &mut Flash, areadesc: &AreaDesc, counter: Option<&mut i32>, align: u8) -> i32 {
David Brownee61c832017-11-06 11:13:25 -070010 unsafe {
11 api::set_flash(flash);
David Brown541860c2017-11-06 11:25:42 -070012 raw::sim_flash_align = align;
David Brownee61c832017-11-06 11:13:25 -070013 raw::flash_counter = match counter {
14 None => 0,
15 Some(ref c) => **c as libc::c_int
16 };
17 }
David Brownbdb6db72017-07-06 10:14:37 -060018 let result = unsafe { raw::invoke_boot_go(&areadesc.get_c() as *const _) as i32 };
David Brownee61c832017-11-06 11:13:25 -070019 unsafe {
20 counter.map(|c| *c = raw::flash_counter as i32);
21 api::clear_flash();
22 };
David Brownbdb6db72017-07-06 10:14:37 -060023 result
David Brownde7729e2017-01-09 10:41:35 -070024}
25
David Brown541860c2017-11-06 11:25:42 -070026pub fn boot_trailer_sz(align: u8) -> u32 {
27 unsafe { raw::boot_slots_trailer_sz(align) }
David Brownde7729e2017-01-09 10:41:35 -070028}
29
Fabio Utziga0bc9b52017-06-28 09:19:55 -030030pub fn boot_magic_sz() -> usize {
31 unsafe { raw::BOOT_MAGIC_SZ as usize }
32}
33
34pub fn boot_max_align() -> usize {
35 unsafe { raw::BOOT_MAX_ALIGN as usize }
36}
37
David Brownde7729e2017-01-09 10:41:35 -070038mod 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 Brownbdb6db72017-07-06 10:14:37 -060046 pub fn invoke_boot_go(areadesc: *const CAreaDesc) -> libc::c_int;
David Brownde7729e2017-01-09 10:41:35 -070047 pub static mut flash_counter: libc::c_int;
48
49 pub static mut sim_flash_align: u8;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030050 pub fn boot_slots_trailer_sz(min_write_sz: u8) -> u32;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030051
52 pub static BOOT_MAGIC_SZ: u32;
53 pub static BOOT_MAX_ALIGN: u32;
David Brownde7729e2017-01-09 10:41:35 -070054 }
55}