blob: ccd853e5c6943f8857743d48bd3281219bf54494 [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 Brownee61c832017-11-06 11:13:25 -07009pub 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 Brownbdb6db72017-07-06 10:14:37 -060017 let result = unsafe { raw::invoke_boot_go(&areadesc.get_c() as *const _) as i32 };
David Brownee61c832017-11-06 11:13:25 -070018 unsafe {
19 counter.map(|c| *c = raw::flash_counter as i32);
20 api::clear_flash();
21 };
David Brownbdb6db72017-07-06 10:14:37 -060022 result
David Brownde7729e2017-01-09 10:41:35 -070023}
24
David Brownde7729e2017-01-09 10:41:35 -070025pub fn boot_trailer_sz() -> u32 {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030026 unsafe { raw::boot_slots_trailer_sz(raw::sim_flash_align) }
David Brownde7729e2017-01-09 10:41:35 -070027}
28
29pub fn get_sim_flash_align() -> u8 {
30 unsafe { raw::sim_flash_align }
31}
32
33pub fn set_sim_flash_align(align: u8) {
34 unsafe { raw::sim_flash_align = align };
35}
36
Fabio Utziga0bc9b52017-06-28 09:19:55 -030037pub fn boot_magic_sz() -> usize {
38 unsafe { raw::BOOT_MAGIC_SZ as usize }
39}
40
41pub fn boot_max_align() -> usize {
42 unsafe { raw::BOOT_MAX_ALIGN as usize }
43}
44
David Brownde7729e2017-01-09 10:41:35 -070045mod 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 Brownbdb6db72017-07-06 10:14:37 -060053 pub fn invoke_boot_go(areadesc: *const CAreaDesc) -> libc::c_int;
David Brownde7729e2017-01-09 10:41:35 -070054 pub static mut flash_counter: libc::c_int;
55
56 pub static mut sim_flash_align: u8;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030057 pub fn boot_slots_trailer_sz(min_write_sz: u8) -> u32;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030058
59 pub static BOOT_MAGIC_SZ: u32;
60 pub static BOOT_MAX_ALIGN: u32;
David Brownde7729e2017-01-09 10:41:35 -070061 }
62}