Fix assert abstraction in sim

Assertions that are expected to fail under sim test, are now marked as such
using the macro ASSERT which allows to programmatically switch between normal
assert() behavior and captured assertion.

Assertion changes were moved to more appropriate owners and code duplication
was removed.

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/bootutil/src/bootutil_misc.c b/boot/bootutil/src/bootutil_misc.c
index 7229a5d..0ec8605 100644
--- a/boot/bootutil/src/bootutil_misc.c
+++ b/boot/bootutil/src/bootutil_misc.c
@@ -34,12 +34,6 @@
 #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
 #include "bootutil/bootutil_log.h"
 
-#ifdef __BOOTSIM__
-#undef assert
-void sim_assert(int, const char *test, const char *, unsigned int, const char *);
-#define assert(x) sim_assert((x), #x, __FILE__, __LINE__, __func__)
-#endif
-
 int boot_current_slot;
 
 const uint32_t boot_img_magic[] = {
diff --git a/boot/bootutil/src/bootutil_priv.h b/boot/bootutil/src/bootutil_priv.h
index 08e8c83..1fd0403 100644
--- a/boot/bootutil/src/bootutil_priv.h
+++ b/boot/bootutil/src/bootutil_priv.h
@@ -28,6 +28,12 @@
 extern "C" {
 #endif
 
+#ifdef __BOOTSIM__
+#include "bootsim.h"
+#else
+#define ASSERT assert
+#endif
+
 struct flash_area;
 
 #define BOOT_EFLASH     1
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index fbbc805..75494f6 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -41,12 +41,6 @@
 #include "mcuboot_config/mcuboot_config.h"
 #endif
 
-#ifdef __BOOTSIM__
-#undef assert
-void sim_assert(int, const char *test, const char *, unsigned int, const char *);
-#define assert(x) sim_assert((x), #x, __FILE__, __LINE__, __func__)
-#endif
-
 static struct boot_loader_state boot_data;
 
 #ifdef MCUBOOT_VALIDATE_SLOT0
@@ -58,7 +52,7 @@
         }                                    \
     } while (0)
 #else
-#define BOOT_STATUS_ASSERT(x) assert(x)
+#define BOOT_STATUS_ASSERT(x) ASSERT(x)
 #endif
 
 struct boot_status_table {
@@ -1429,7 +1423,7 @@
 
 #ifdef MCUBOOT_VALIDATE_SLOT0
     rc = boot_validate_slot(0);
-    assert(rc == 0);
+    ASSERT(rc == 0);
     if (rc != 0) {
         rc = BOOT_EBADIMAGE;
         goto out;
diff --git a/sim/mcuboot-sys/build.rs b/sim/mcuboot-sys/build.rs
index 5f5eeac..ebff3c5 100644
--- a/sim/mcuboot-sys/build.rs
+++ b/sim/mcuboot-sys/build.rs
@@ -81,6 +81,7 @@
     conf.file("csupport/run.c");
     conf.include("../../boot/bootutil/include");
     conf.include("../../boot/zephyr/include");
+    conf.include("csupport");
     conf.debug(true);
     conf.flag("-Wall");
     conf.flag("-Werror");
diff --git a/sim/mcuboot-sys/csupport/bootsim.h b/sim/mcuboot-sys/csupport/bootsim.h
new file mode 100644
index 0000000..f3fc5e4
--- /dev/null
+++ b/sim/mcuboot-sys/csupport/bootsim.h
@@ -0,0 +1,7 @@
+#ifndef H_BOOTSIM_
+#define H_BOOTSIM_
+
+void sim_assert(int, const char *test, const char *, unsigned int, const char *);
+#define ASSERT(x) sim_assert((x), #x, __FILE__, __LINE__, __func__)
+
+#endif
diff --git a/sim/mcuboot-sys/csupport/run.c b/sim/mcuboot-sys/csupport/run.c
index 91d779a..9b5e36e 100644
--- a/sim/mcuboot-sys/csupport/run.c
+++ b/sim/mcuboot-sys/csupport/run.c
@@ -10,6 +10,7 @@
 #include "flash_map/flash_map.h"
 
 #include "../../../boot/bootutil/src/bootutil_priv.h"
+#include "bootsim.h"
 
 #ifdef MCUBOOT_SIGN_EC256
 #include "../../../ext/tinycrypt/lib/include/tinycrypt/ecc_dsa.h"