This closes #5.

Merge remote-tracking branch 'rsalveti/zephyr-updates'

* rsalveti/zephyr-updates:
  zephyr: use SYS_LOG instead of printk
  image_rsa: include missing string.h
  boot/zephyr/prj.conf: disable bluetooth support by default
  boot/zephyr/prj.conf: remove SOC_FLASH_STM32F4, make it generic
  zephyr: targets: add support for 96b_nitrogen
  zephyr: restructure the build process to use board config files
diff --git a/boot/bootutil/src/image_rsa.c b/boot/bootutil/src/image_rsa.c
index 7b1f4d4..4890f46 100644
--- a/boot/bootutil/src/image_rsa.c
+++ b/boot/bootutil/src/image_rsa.c
@@ -17,6 +17,8 @@
  * under the License.
  */
 
+#include <string.h>
+
 #include "syscfg/syscfg.h"
 
 #if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
diff --git a/boot/zephyr/Makefile b/boot/zephyr/Makefile
index 6b8618a..b3f79f3 100644
--- a/boot/zephyr/Makefile
+++ b/boot/zephyr/Makefile
@@ -1,5 +1,7 @@
+subdir-ccflags-y += -DMCUBOOT_TARGET_CONFIG='"$(BOARD).h"'
 subdir-ccflags-y += -I$(PROJECT)/boot/bootutil/include
 subdir-ccflags-y += -I$(PROJECT)/boot/zephyr/include
+subdir-ccflags-y += -I$(PROJECT)/boot/zephyr/targets
 
 obj-y += main.o
 obj-y += flash_map.o hal_flash.o os.o
diff --git a/boot/zephyr/flash_map.c b/boot/zephyr/flash_map.c
index aadf13a..2736e94 100644
--- a/boot/zephyr/flash_map.c
+++ b/boot/zephyr/flash_map.c
@@ -18,13 +18,18 @@
  */
 
 #include <zephyr.h>
-#include <misc/printk.h>
 #include <flash.h>
 
+#include MCUBOOT_TARGET_CONFIG
+
 #include <flash_map/flash_map.h>
 #include <hal/hal_flash.h>
 #include <sysflash/sysflash.h>
 
+#define SYS_LOG_DOMAIN "BOOTLOADER"
+#define SYS_LOG_LEVEL SYS_LOG_LEVEL_INFO
+#include <logging/sys_log.h>
+
 extern struct device *boot_flash_device;
 
 /*
@@ -34,35 +39,30 @@
 static const struct flash_area part_map[] = {
 	{
 		.fa_id = FLASH_AREA_IMAGE_0,
-		.fa_off = 0x20000,
-		.fa_size = 0x20000,
+		.fa_off = FLASH_AREA_IMAGE_0_OFFSET,
+		.fa_size = FLASH_AREA_IMAGE_0_SIZE,
 	},
 	{
 		.fa_id = FLASH_AREA_IMAGE_1,
-		.fa_off = 0x40000,
-		.fa_size = 0x20000,
+		.fa_off = FLASH_AREA_IMAGE_1_OFFSET,
+		.fa_size = FLASH_AREA_IMAGE_1_SIZE,
 	},
 	{
 		.fa_id = FLASH_AREA_IMAGE_SCRATCH,
-		.fa_off = 0x60000,
-		.fa_size = 0x20000,
+		.fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET,
+		.fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE,
 	},
 };
 
 /*
- * The K64F has a simple 1MB of uniform 4KB sectors.  Initially, we'll
- * use the same partition layout as the Carbon board to make
- * development easier.
- */
-
-/*
  * `open` a flash area.  The `area` in this case is not the individual
  * sectors, but describes the particular flash area in question.
  */
 int flash_area_open(uint8_t id, const struct flash_area **area)
 {
 	int i;
-	printk("%s: area %d\n", __func__, id);
+
+	SYS_LOG_DBG("%s: area %d", __func__, id);
 
 	for (i = 0; i < ARRAY_SIZE(part_map); i++) {
 		if (id == part_map[i].fa_id)
@@ -85,20 +85,23 @@
 int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
 		    uint32_t len)
 {
-	// printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len);
+	SYS_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
+			area->fa_id, off, len);
 	return flash_read(boot_flash_device, area->fa_off + off, dst, len);
 }
 
 int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
 		     uint32_t len)
 {
-	printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len);
+	SYS_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
+			area->fa_id, off, len);
 	return flash_write(boot_flash_device, area->fa_off + off, src, len);
 }
 
 int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
 {
-	printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len);
+	SYS_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
+			area->fa_id, off, len);
 	return flash_erase(boot_flash_device, area->fa_off + off, len);
 }
 
@@ -126,7 +129,7 @@
 {
 	uint32_t off;
 
-	printk("%s: lookup area %d\n", __func__, idx);
+	SYS_LOG_DBG("%s: lookup area %d", __func__, idx);
 	/*
 	 * This simple layout has uniform slots, so just fill in the
 	 * right one.
@@ -137,13 +140,13 @@
 	if (*cnt < 1)
 		return -1;
 
-	off = (idx - FLASH_AREA_IMAGE_0 + 1) * 0x20000;
+	off = (idx - FLASH_AREA_IMAGE_0 + 1) * FLASH_AREA_IMAGE_0_OFFSET;
 
 	ret->fa_id = idx;
 	ret->fa_device_id = 0;
 	ret->pad16 = 0;
 	ret->fa_off = off;
-	ret->fa_size = 0x20000;
+	ret->fa_size = FLASH_AREA_IMAGE_0_SIZE;
 
 	return 0;
 }
diff --git a/boot/zephyr/hal_flash.c b/boot/zephyr/hal_flash.c
index a3700bc..cebf161 100644
--- a/boot/zephyr/hal_flash.c
+++ b/boot/zephyr/hal_flash.c
@@ -18,18 +18,11 @@
  */
 
 #include <zephyr.h>
-#include <misc/printk.h>
+
+#include MCUBOOT_TARGET_CONFIG
 
 #include "hal/hal_flash.h"
 
-#if defined(CONFIG_BOARD_FRDM_K64F)
-#define FLASH_ALIGN 8
-#elif defined(CONFIG_BOARD_96B_CARBON)
-#define FLASH_ALIGN 1
-#else
-#error "Board is currently not supported by bootloader"
-#endif
-
 /* All of the currently supported devices allow single byte writes. */
 uint8_t hal_flash_align(uint8_t flash_id)
 {
diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c
index 800fe51..960dcf2 100644
--- a/boot/zephyr/main.c
+++ b/boot/zephyr/main.c
@@ -15,21 +15,22 @@
  */
 
 #include <zephyr.h>
-#include <misc/printk.h>
 #include <flash.h>
 #include <asm_inline.h>
 
-#include "bootutil/image.h"
-#include "bootutil/bootutil.h"
+#define SYS_LOG_DOMAIN "BOOTLOADER"
+#define SYS_LOG_LEVEL SYS_LOG_LEVEL_INFO
+#include <logging/sys_log.h>
 
-#if defined(CONFIG_BOARD_FRDM_K64F)
-#define BOOT_FLASH "KSDK_FLASH"
-#elif defined(CONFIG_BOARD_96B_CARBON)
-#define BOOT_FLASH "STM32F4_FLASH"
+#if defined(MCUBOOT_TARGET_CONFIG)
+#include MCUBOOT_TARGET_CONFIG
 #else
 #error "Board is currently not supported by bootloader"
 #endif
 
+#include "bootutil/image.h"
+#include "bootutil/bootutil.h"
+
 struct device *boot_flash_device;
 
 struct vector_table {
@@ -45,34 +46,39 @@
 	struct vector_table *vt;
 	int rc;
 
+	SYS_LOG_INF("Starting bootloader");
+
 	os_heap_init();
 
-	boot_flash_device = device_get_binding(BOOT_FLASH);
+	boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
 	if (!boot_flash_device) {
-		printk("Flash device not found\n");
+		SYS_LOG_ERR("Flash device not found");
 		while (1)
 			;
 	}
 
 	rc = boot_go(&rsp);
 	if (rc != 0) {
-		printk("Unable to find bootable image\n");
+		SYS_LOG_ERR("Unable to find bootable image");
 		while (1)
 			;
 	}
 
-	printk("Bootloader chain: 0x%x\n", rsp.br_image_addr);
+	SYS_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr);
 	vt = (struct vector_table *)(rsp.br_image_addr +
 				     rsp.br_hdr->ih_hdr_size);
 	irq_lock();
 	_MspSet(vt->msp);
 
+	SYS_LOG_INF("Setting vector table to %p", vt);
+
 	/* Not all targets set the VTOR, so just set it. */
 	_scs_relocate_vector_table((void *) vt);
 
+	SYS_LOG_INF("Jumping to the first image slot");
 	((void (*)(void))vt->reset)();
 
-	printk("Never should get here\n");
+	SYS_LOG_ERR("Never should get here");
 	while (1)
 		;
 }
diff --git a/boot/zephyr/os.c b/boot/zephyr/os.c
index 80be2fa..e73edaa 100644
--- a/boot/zephyr/os.c
+++ b/boot/zephyr/os.c
@@ -18,7 +18,6 @@
  */
 
 #include <zephyr.h>
-#include <misc/printk.h>
 #include <string.h>
 
 #include "os/os_heap.h"
diff --git a/boot/zephyr/prj.conf b/boot/zephyr/prj.conf
index 5c36b1d..f3cffde 100644
--- a/boot/zephyr/prj.conf
+++ b/boot/zephyr/prj.conf
@@ -1,5 +1,5 @@
 CONFIG_CONSOLE_HANDLER=y
-CONFIG_PRINTK=y
+CONFIG_SYS_LOG=y
 CONFIG_DEBUG=y
 
 CONFIG_MAIN_STACK_SIZE=10240
@@ -11,4 +11,6 @@
 CONFIG_HEAP_MEM_POOL_SIZE=16384
 
 CONFIG_FLASH=y
-CONFIG_SOC_FLASH_STM32F4=y
+
+### Disable Bluetooth by default
+# CONFIG_BLUETOOTH is not set
diff --git a/boot/zephyr/target.sh.example b/boot/zephyr/target.sh.example
deleted file mode 100644
index 1652853..0000000
--- a/boot/zephyr/target.sh.example
+++ /dev/null
@@ -1,17 +0,0 @@
-# Copy this file to target.sh and modify to suit your needs
-
-if true; then
-	BOARD=96b_carbon
-	SOC=STM32F401RE
-	BASE_BOOT=0x08000000
-	BASE_SLOT0=0x08020000
-	BASE_SLOT1=0x08040000
-else
-	BOARD=frdm_k64f
-	SOC=MK64FN1M0VLL12
-	BASE_BOOT=0x00000000
-	BASE_SLOT0=0x00020000
-	BASE_SLOT1=0x00040000
-fi
-
-gdbexe=/mnt/linaro/toolchains/aarch32/bin/arm-linux-gnueabihf-gdb
diff --git a/boot/zephyr/targets/96b_carbon.h b/boot/zephyr/targets/96b_carbon.h
new file mode 100644
index 0000000..6d8b248
--- /dev/null
+++ b/boot/zephyr/targets/96b_carbon.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017 Linaro
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file
+ * @brief Bootloader device specific configuration.
+ */
+
+#define FLASH_DRIVER_NAME		"STM32F4_FLASH"
+#define FLASH_ALIGN			1
+#define FLASH_AREA_IMAGE_0_OFFSET	0x20000
+#define FLASH_AREA_IMAGE_0_SIZE		0x20000
+#define FLASH_AREA_IMAGE_1_OFFSET	0x40000
+#define FLASH_AREA_IMAGE_1_SIZE		0x20000
+#define FLASH_AREA_IMAGE_SCRATCH_OFFSET	0x60000
+#define FLASH_AREA_IMAGE_SCRATCH_SIZE	0x20000
diff --git a/boot/zephyr/targets/96b_nitrogen.h b/boot/zephyr/targets/96b_nitrogen.h
new file mode 100644
index 0000000..291844f
--- /dev/null
+++ b/boot/zephyr/targets/96b_nitrogen.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017 Linaro
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file
+ * @brief Bootloader device specific configuration.
+ */
+
+#define FLASH_DRIVER_NAME		"NRF5_FLASH"
+#define FLASH_ALIGN			1
+#define FLASH_AREA_IMAGE_0_OFFSET	0x08000
+#define FLASH_AREA_IMAGE_0_SIZE		0x3A000
+#define FLASH_AREA_IMAGE_1_OFFSET	0x42000
+#define FLASH_AREA_IMAGE_1_SIZE		0x3A000
+#define FLASH_AREA_IMAGE_SCRATCH_OFFSET	0x7c000
+#define FLASH_AREA_IMAGE_SCRATCH_SIZE	0x01000
diff --git a/boot/zephyr/targets/frdm_k64f.h b/boot/zephyr/targets/frdm_k64f.h
new file mode 100644
index 0000000..01926d5
--- /dev/null
+++ b/boot/zephyr/targets/frdm_k64f.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017 Linaro
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file
+ * @brief Bootloader device specific configuration.
+ */
+
+#define FLASH_DRIVER_NAME		"KSDK_FLASH"
+#define FLASH_ALIGN			8
+#define FLASH_AREA_IMAGE_0_OFFSET	0x20000
+#define FLASH_AREA_IMAGE_0_SIZE		0x20000
+#define FLASH_AREA_IMAGE_1_OFFSET	0x40000
+#define FLASH_AREA_IMAGE_1_SIZE		0x20000
+#define FLASH_AREA_IMAGE_SCRATCH_OFFSET	0x60000
+#define FLASH_AREA_IMAGE_SCRATCH_SIZE	0x20000
diff --git a/build_boot.sh b/build_boot.sh
index b998812..72ee96f 100755
--- a/build_boot.sh
+++ b/build_boot.sh
@@ -1,6 +1,22 @@
-#! /bin/bash
+#!/bin/sh
 
-source $(dirname 0)/target.sh
-source ../zephyr/zephyr-env.sh
+# Assume first argument is the board name (as defined in Zephyr)
+BOARD=$1
 
-make BOARD=$BOARD "$@"
+if [ -z "$BOARD" ]; then
+	echo "Please specify the board name (as in Zephyr) as first argument."
+	exit 1;
+fi
+
+if [ ! -f "$(dirname $0)/boot/zephyr/targets/${BOARD}.h" ]; then
+	echo "Board $BOARD not yet supported, please use a supported target."
+	exit 1;
+fi
+
+# Check if there is a valid Zephyr environment available
+if [ -z "$ZEPHYR_BASE" ]; then
+	echo "ZEPHYR_BASE not provided by the environment."
+	exit 1;
+fi
+
+make BOARD=$BOARD