Merge pull request #50 from utzig/fix-var-shadowing

Fix shadowing of variable in print
diff --git a/boot/bootutil/include/bootutil/bootutil_log.h b/boot/bootutil/include/bootutil/bootutil_log.h
index 6dca9b3..3cd03a3 100644
--- a/boot/bootutil/include/bootutil/bootutil_log.h
+++ b/boot/bootutil/include/bootutil/bootutil_log.h
@@ -62,9 +62,54 @@
 #include <logging/sys_log.h>
 
 /*
+ * When built on the simulator, just use printf().
+ */
+#elif defined(__BOOTSIM__)	/* !defined(__ZEPHYR__) */
+
+#include <stdio.h>
+
+#define BOOT_LOG_LEVEL_OFF	0
+#define BOOT_LOG_LEVEL_ERROR	1
+#define BOOT_LOG_LEVEL_WARNING	2
+#define BOOT_LOG_LEVEL_INFO	3
+#define BOOT_LOG_LEVEL_DEBUG	4
+
+#ifndef BOOT_LOG_LEVEL
+#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR
+#endif
+
+#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_ERROR
+#define BOOT_LOG_ERR(_fmt, ...)						\
+    do { printf("[ERR] " _fmt "\n", ##__VA_ARGS__); } while (0)
+#else
+#define BOOT_LOG_ERR(...) IGNORE(__VA_ARGS__)
+#endif
+
+#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_WARNING
+#define BOOT_LOG_WRN(_fmt, ...)                                               \
+    do { printf("[WRN] " _fmt "\n", ##__VA_ARGS__); } while (0)
+#else
+#define BOOT_LOG_WRN(...) IGNORE(__VA_ARGS__)
+#endif
+
+#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_INFO
+#define BOOT_LOG_INF(_fmt, ...)                                               \
+    do { printf("[INF] " _fmt "\n", ##__VA_ARGS__); } while (0)
+#else
+#define BOOT_LOG_INF(...) IGNORE(__VA_ARGS__)
+#endif
+
+#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_DEBUG
+#define BOOT_LOG_DBG(_fmt, ...)                                               \
+    do { printf("[DBG] " _fmt "\n", ##__VA_ARGS__); } while (0)
+#else
+#define BOOT_LOG_DBG(...) IGNORE(__VA_ARGS__)
+#endif
+
+/*
  * In other environments, logging calls are no-ops.
  */
-#else  /* !defined(__ZEPHYR__) */
+#else  /* !defined(__BOOTSIM__) */
 
 #define BOOT_LOG_LEVEL_OFF	0
 #define BOOT_LOG_LEVEL_ERROR	1
diff --git a/imgtool/sign.go b/imgtool/sign.go
index b53279e..1f808da 100644
--- a/imgtool/sign.go
+++ b/imgtool/sign.go
@@ -126,16 +126,52 @@
 		return errors.New("Image is too large for specified padding")
 	}
 
-	_, err = f.WriteAt(bootMagic, padTo-trailerSize)
+	// Unwritten data in files is written as zero, but we need it
+	// to be unwritten in flash, so write as all FFs.
+	err = ffPadFile(f, padTo-trailerSize)
 	if err != nil {
 		return err
 	}
 
-	err = f.Truncate(padTo)
+	_, err = f.Write(bootMagic)
 	if err != nil {
 		return err
 	}
 
+	err = ffPadFile(f, padTo)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// Pad the file to the given size, writing all 0xFF to the file.
+func ffPadFile(f *os.File, pos int64) error {
+	buf := make([]byte, 4096)
+	for i := range buf {
+		buf[i] = 0xff
+	}
+
+	base, err := f.Seek(0, 2)
+	if err != nil {
+		return err
+	}
+
+	for base < pos {
+		count := len(buf)
+		if int64(count) > pos-base {
+			count = int(pos - base)
+		}
+
+		_, err = f.Write(buf[:count])
+		if err != nil {
+			return err
+		}
+
+		base += int64(count)
+	}
+
 	return nil
 }
 
diff --git a/sim/build.rs b/sim/build.rs
index 74e271c..2fd997e 100644
--- a/sim/build.rs
+++ b/sim/build.rs
@@ -15,6 +15,8 @@
     conf.include("../boot/bootutil/include");
     conf.include("../boot/zephyr/include");
     conf.debug(true);
+    conf.flag("-Wall");
+    conf.define("__BOOTSIM__", None);
     conf.compile("libbootutil.a");
     walk_dir("../boot").unwrap();
     walk_dir("csupport").unwrap();
diff --git a/sim/csupport/run.c b/sim/csupport/run.c
index c9743fe..68cbbf4 100644
--- a/sim/csupport/run.c
+++ b/sim/csupport/run.c
@@ -10,6 +10,9 @@
 
 #include "../../boot/bootutil/src/bootutil_priv.h"
 
+#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR
+#include <bootutil/bootutil_log.h>
+
 extern int sim_flash_erase(void *flash, uint32_t offset, uint32_t size);
 extern int sim_flash_read(void *flash, uint32_t offset, uint8_t *dest, uint32_t size);
 extern int sim_flash_write(void *flash, uint32_t offset, const uint8_t *src, uint32_t size);
@@ -107,7 +110,6 @@
 int flash_area_open(uint8_t id, const struct flash_area **area)
 {
 	int i;
-	struct area *slot;
 
 	for (i = 0; i < flash_areas->num_slots; i++) {
 		if (flash_areas->slots[i].id == id)
@@ -133,6 +135,8 @@
 int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
 		    uint32_t len)
 {
+	BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x",
+		     __func__, area->fa_id, off, len);
 	return hal_flash_read(area->fa_id,
 			      area->fa_off + off,
 			      dst, len);
@@ -141,6 +145,8 @@
 int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
 		     uint32_t len)
 {
+	BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
+                     area->fa_id, off, len);
 	return hal_flash_write(area->fa_id,
 			       area->fa_off + off,
 			       src, len);
@@ -148,6 +154,8 @@
 
 int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
 {
+	BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
+                     area->fa_id, off, len);
 	return hal_flash_erase(area->fa_id,
 			       area->fa_off + off,
 			       len);