sim: Naive move of binding into mcuboot-sys

Create a crate `mcuboot-sys` to hold the building and binding of the
mcuboot code.  There aren't any substantive code changes here, just
moving the code into a separate crate.

Signed-off-by: David Brown <david.brown@linaro.org>
diff --git a/sim/Cargo.lock b/sim/Cargo.lock
index 330fad9..caca524 100644
--- a/sim/Cargo.lock
+++ b/sim/Cargo.lock
@@ -8,6 +8,7 @@
  "gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)",
  "libc 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "mcuboot-sys 0.1.0",
  "pem 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
  "ring 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -172,6 +173,16 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "mcuboot-sys"
+version = "0.1.0"
+dependencies = [
+ "gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "simflash 0.1.0",
+]
+
+[[package]]
 name = "memchr"
 version = "0.1.11"
 source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/sim/Cargo.toml b/sim/Cargo.toml
index e1bb57a..24b186d 100644
--- a/sim/Cargo.toml
+++ b/sim/Cargo.toml
@@ -2,7 +2,6 @@
 name = "bootsim"
 version = "0.1.0"
 authors = ["David Brown <davidb@davidb.org>"]
-build = "build.rs"
 
 [build-dependencies]
 gcc = "0.3.38"
@@ -15,6 +14,7 @@
 log = "0.3"
 env_logger = "0.3"
 simflash = { path = "simflash" }
+mcuboot-sys = { path = "mcuboot-sys", features = ["sig-rsa"] }
 bitflags = "0.9"
 ring = { version = "0.11", features = ["rsa_signing"] }
 untrusted = "0.5"
diff --git a/sim/build.rs b/sim/build.rs
deleted file mode 100644
index ba89a04..0000000
--- a/sim/build.rs
+++ /dev/null
@@ -1,66 +0,0 @@
-// Build the library.
-
-extern crate gcc;
-
-use std::fs;
-use std::io;
-use std::path::Path;
-
-fn main() {
-    let mut conf = gcc::Config::new();
-
-    conf.file("../boot/bootutil/src/loader.c");
-    conf.file("../boot/bootutil/src/caps.c");
-    conf.file("../boot/bootutil/src/bootutil_misc.c");
-    conf.file("../boot/bootutil/src/image_validate.c");
-    conf.file("../boot/bootutil/src/image_rsa.c");
-    conf.file("../boot/zephyr/keys.c");
-    conf.file("csupport/run.c");
-    conf.include("../boot/bootutil/include");
-    conf.include("../boot/zephyr/include");
-    conf.debug(true);
-    conf.flag("-Wall");
-    conf.define("__BOOTSIM__", None);
-    // conf.define("MCUBOOT_OVERWRITE_ONLY", None);
-    conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
-
-    // Bring in some pieces of mbedtls we need.
-    conf.define("MCUBOOT_USE_MBED_TLS", None);
-    conf.define("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
-    conf.include("mbedtls/include");
-    conf.file("mbedtls/library/sha256.c");
-
-    // Code for RSA.
-    conf.file("mbedtls/library/rsa.c");
-    conf.file("mbedtls/library/bignum.c");
-    conf.file("mbedtls/library/asn1parse.c");
-
-    // Test that signature/hashes are present.
-    conf.define("MCUBOOT_VALIDATE_SLOT0", None);
-    conf.define("MCUBOOT_SIGN_RSA", None);
-
-    conf.compile("libbootutil.a");
-    walk_dir("../boot").unwrap();
-    walk_dir("csupport").unwrap();
-    walk_dir("mbedtls/include").unwrap();
-    walk_dir("mbedtls/library").unwrap();
-}
-
-// Output the names of all files within a directory so that Cargo knows when to rebuild.
-fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
-    for ent in fs::read_dir(path.as_ref())? {
-        let ent = ent?;
-        let p = ent.path();
-        if p.is_dir() {
-            walk_dir(p)?;
-        } else {
-            // Note that non-utf8 names will fail.
-            let name = p.to_str().unwrap();
-            if name.ends_with(".c") || name.ends_with(".h") {
-                println!("cargo:rerun-if-changed={}", name);
-            }
-        }
-    }
-
-    Ok(())
-}
diff --git a/sim/mcuboot-sys/.gitignore b/sim/mcuboot-sys/.gitignore
new file mode 100644
index 0000000..03314f7
--- /dev/null
+++ b/sim/mcuboot-sys/.gitignore
@@ -0,0 +1 @@
+Cargo.lock
diff --git a/sim/mcuboot-sys/Cargo.toml b/sim/mcuboot-sys/Cargo.toml
new file mode 100644
index 0000000..dd236ba
--- /dev/null
+++ b/sim/mcuboot-sys/Cargo.toml
@@ -0,0 +1,34 @@
+[package]
+name = "mcuboot-sys"
+version = "0.1.0"
+authors = ["David Brown <david.brown@linaro.org>"]
+description = "A simple wrapper around the mcuboot code."
+build = "build.rs"
+publish = false
+
+[features]
+# By default, build with simplistic signature verification.
+default = []
+
+# Verify RSA signatures.  Note that at this time, the C code will not
+# compile with both sig-rsa and sig-ecdsa enabled.
+sig-rsa = []
+
+# Verify ECDSA signatures.
+sig-ecdsa = []
+
+# Overwrite only upgrade
+overwrite-only = []
+
+[build-dependencies]
+gcc = "0.3.51"
+
+[dependencies]
+libc = "0.2.0"
+log = "0.3"
+simflash = { path = "../simflash" }
+
+# Optimize some, even when building for debugging, otherwise the tests
+# are too slow.
+[profile.test]
+opt-level = 1
diff --git a/sim/mcuboot-sys/build.rs b/sim/mcuboot-sys/build.rs
new file mode 100644
index 0000000..d053229
--- /dev/null
+++ b/sim/mcuboot-sys/build.rs
@@ -0,0 +1,89 @@
+// Build mcuboot as a library, based on the requested features.
+
+extern crate gcc;
+
+use std::env;
+use std::fs;
+use std::io;
+use std::path::Path;
+
+fn main() {
+    // Feature flags.
+    let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok();
+    let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
+    let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
+
+    // TODO: Force clang if we are requestion fuzzing.
+
+    let mut conf = gcc::Config::new();
+    conf.define("__BOOTSIM__", None);
+    conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
+    conf.define("MCUBOOT_VALIDATE_SLOT0", None);
+
+    if sig_rsa {
+        if sig_ecdsa {
+            panic!("mcuboot does not support RSA and ECDSA at the same time");
+        }
+
+        conf.define("MCUBOOT_SIGN_RSA", None);
+        conf.define("MCUBOOT_USE_MBED_TLS", None);
+
+        conf.file("../../boot/bootutil/src/image_validate.c");
+        conf.file("../../boot/bootutil/src/image_rsa.c");
+        conf.file("../../boot/zephyr/keys.c");
+
+        conf.define("MCUBOOT_USE_MBED_TLS", None);
+        conf.define("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
+        conf.include("../mbedtls/include");
+        conf.file("../mbedtls/library/sha256.c");
+
+        conf.file("../mbedtls/library/rsa.c");
+        conf.file("../mbedtls/library/bignum.c");
+        conf.file("../mbedtls/library/asn1parse.c");
+    }
+    if sig_ecdsa {
+        conf.define("MCUBOOT_SIGN_ECDSA", None);
+        conf.define("MCUBOOT_USE_TINYCRYPT", None);
+        // TODO: Compile files + tinycrypt.
+        panic!("ECDSA not yet implemented in sim");
+    }
+
+    if overwrite_only {
+        conf.define("MCUBOOT_OVERWRITE_ONLY", None);
+    }
+
+    conf.file("../../boot/bootutil/src/loader.c");
+    conf.file("../../boot/bootutil/src/caps.c");
+    conf.file("../../boot/bootutil/src/bootutil_misc.c");
+    conf.file("../csupport/run.c");
+    conf.include("../../boot/bootutil/include");
+    conf.include("../../boot/zephyr/include");
+    conf.debug(true);
+    conf.flag("-Wall");
+
+    conf.compile("libbootutil.a");
+
+    walk_dir("../../boot").unwrap();
+    walk_dir("../csupport").unwrap();
+    walk_dir("../mbedtls/include").unwrap();
+    walk_dir("../mbedtls/library").unwrap();
+}
+
+// Output the names of all files within a directory so that Cargo knows when to rebuild.
+fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
+    for ent in fs::read_dir(path.as_ref())? {
+        let ent = ent?;
+        let p = ent.path();
+        if p.is_dir() {
+            walk_dir(p)?;
+        } else {
+            // Note that non-utf8 names will fail.
+            let name = p.to_str().unwrap();
+            if name.ends_with(".c") || name.ends_with(".h") {
+                println!("cargo:rerun-if-changed={}", name);
+            }
+        }
+    }
+
+    Ok(())
+}
diff --git a/sim/src/api.rs b/sim/mcuboot-sys/src/api.rs
similarity index 99%
rename from sim/src/api.rs
rename to sim/mcuboot-sys/src/api.rs
index 0347b65..4e744e2 100644
--- a/sim/src/api.rs
+++ b/sim/mcuboot-sys/src/api.rs
@@ -3,7 +3,7 @@
 use simflash::{Result, Flash};
 use libc;
 use log::LogLevel;
-use mem;
+use std::mem;
 use std::slice;
 
 // The current active flash device.  The 'static is a lie, and we manage the lifetime ourselves.
diff --git a/sim/src/area.rs b/sim/mcuboot-sys/src/area.rs
similarity index 100%
rename from sim/src/area.rs
rename to sim/mcuboot-sys/src/area.rs
diff --git a/sim/src/c.rs b/sim/mcuboot-sys/src/c.rs
similarity index 100%
rename from sim/src/c.rs
rename to sim/mcuboot-sys/src/c.rs
diff --git a/sim/mcuboot-sys/src/lib.rs b/sim/mcuboot-sys/src/lib.rs
new file mode 100644
index 0000000..2da90ef
--- /dev/null
+++ b/sim/mcuboot-sys/src/lib.rs
@@ -0,0 +1,7 @@
+extern crate libc;
+#[macro_use] extern crate log;
+extern crate simflash;
+
+pub mod area;
+pub mod c;
+pub mod api;
diff --git a/sim/src/main.rs b/sim/src/main.rs
index acd15a9..61eb2c0 100644
--- a/sim/src/main.rs
+++ b/sim/src/main.rs
@@ -9,6 +9,7 @@
 extern crate rustc_serialize;
 extern crate simflash;
 extern crate untrusted;
+extern crate mcuboot_sys;
 
 use docopt::Docopt;
 use rand::{Rng, SeedableRng, XorShiftRng};
@@ -19,14 +20,12 @@
 use std::process;
 use std::slice;
 
-mod area;
-mod c;
-pub mod api;
 mod caps;
 mod tlv;
 
 use simflash::{Flash, SimFlash};
-use area::{AreaDesc, FlashId};
+use mcuboot_sys::area::{AreaDesc, FlashId};
+use mcuboot_sys::c;
 use caps::Caps;
 use tlv::TlvGen;