sim: Add simulator code
'sim' is a small simulator for the bootloader's update code. It tests
untimely powerdowns to ensure that the bootloader will recover from a
power loss or reset at any time during the boot.
Note that, as of this commit, there are some failures in the test that
need to be investigated.
Also note that this build script does not output proper dependencies for
source files outside of the simulator directory, and won't rebuild the C
files if they or headers are modified.
diff --git a/sim/src/c.rs b/sim/src/c.rs
new file mode 100644
index 0000000..7ddb21b
--- /dev/null
+++ b/sim/src/c.rs
@@ -0,0 +1,50 @@
+/// Interface wrappers to C API entering to the bootloader
+
+use area::AreaDesc;
+use flash::Flash;
+use libc;
+
+/// Invoke the bootloader on this flash device.
+pub fn boot_go(flash: &mut Flash, areadesc: &AreaDesc) -> i32 {
+ unsafe { raw::invoke_boot_go(flash as *mut _ as *mut libc::c_void,
+ &areadesc.get_c() as *const _) as i32 }
+}
+
+/// Setter/getter for the flash counter. This isn't thread safe.
+pub fn get_flash_counter() -> i32 {
+ unsafe { raw::flash_counter as i32 }
+}
+
+/// Set the flash counter. Zero indicates the flash should not be interrupted. The counter will
+/// then go negative for each flash operation.
+pub fn set_flash_counter(counter: i32) {
+ unsafe { raw::flash_counter = counter as libc::c_int };
+}
+
+pub fn boot_trailer_sz() -> u32 {
+ unsafe { raw::boot_trailer_sz(raw::sim_flash_align) }
+}
+
+pub fn get_sim_flash_align() -> u8 {
+ unsafe { raw::sim_flash_align }
+}
+
+pub fn set_sim_flash_align(align: u8) {
+ unsafe { raw::sim_flash_align = align };
+}
+
+mod raw {
+ use area::CAreaDesc;
+ use libc;
+
+ extern "C" {
+ // This generates a warning about `CAreaDesc` not being foreign safe. There doesn't appear to
+ // be any way to get rid of this warning. See https://github.com/rust-lang/rust/issues/34798
+ // for information and tracking.
+ pub fn invoke_boot_go(flash: *mut libc::c_void, areadesc: *const CAreaDesc) -> libc::c_int;
+ pub static mut flash_counter: libc::c_int;
+
+ pub static mut sim_flash_align: u8;
+ pub fn boot_trailer_sz(min_write_sz: u8) -> u32;
+ }
+}