zephyr: Bring in the Zephyr build

Add support for building mcuboot as a Zephyr application.  This is
copied from the iotboot repo with some minor reorganization to work with
the mcuboot directory layout.
diff --git a/zephyr/Makefile b/zephyr/Makefile
new file mode 100644
index 0000000..4de50f9
--- /dev/null
+++ b/zephyr/Makefile
@@ -0,0 +1,4 @@
+BOARD ?= qemu_x86
+CONF_FILE = prj.conf
+
+include ${ZEPHYR_BASE}/Makefile.inc
diff --git a/zephyr/build_boot.sh b/zephyr/build_boot.sh
new file mode 100755
index 0000000..2fc8326
--- /dev/null
+++ b/zephyr/build_boot.sh
@@ -0,0 +1,6 @@
+#! /bin/bash
+
+source $(dirname 0)/target.sh
+source ../../zephyr/zephyr-env.sh
+
+make BOARD=$BOARD "$@"
diff --git a/zephyr/include/flash_map/flash_map.h b/zephyr/include/flash_map/flash_map.h
new file mode 100644
index 0000000..c0c9489
--- /dev/null
+++ b/zephyr/include/flash_map/flash_map.h
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef H_UTIL_FLASH_MAP_
+#define H_UTIL_FLASH_MAP_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ *
+ * Provides abstraction of flash regions for type of use.
+ * I.e. dude where's my image?
+ *
+ * System will contain a map which contains flash areas. Every
+ * region will contain flash identifier, offset within flash and length.
+ *
+ * 1. This system map could be in a file within filesystem (Initializer
+ * must know/figure out where the filesystem is at).
+ * 2. Map could be at fixed location for project (compiled to code)
+ * 3. Map could be at specific place in flash (put in place at mfg time).
+ *
+ * Note that the map you use must be valid for BSP it's for,
+ * match the linker scripts when platform executes from flash,
+ * and match the target offset specified in download script.
+ */
+#include <inttypes.h>
+
+struct flash_area {
+    uint8_t fa_id;
+    uint8_t fa_device_id;
+    uint16_t pad16;
+    uint32_t fa_off;
+    uint32_t fa_size;
+};
+
+extern const struct flash_area *flash_map;
+extern int flash_map_entries;
+
+/*
+ * Initializes flash map. Memory will be referenced by flash_map code
+ * from this on.
+ */
+void flash_map_init(void);
+
+/*
+ * Start using flash area.
+ */
+int flash_area_open(uint8_t id, const struct flash_area **);
+
+void flash_area_close(const struct flash_area *);
+
+/*
+ * Read/write/erase. Offset is relative from beginning of flash area.
+ */
+int flash_area_read(const struct flash_area *, uint32_t off, void *dst,
+  uint32_t len);
+int flash_area_write(const struct flash_area *, uint32_t off, const void *src,
+  uint32_t len);
+int flash_area_erase(const struct flash_area *, uint32_t off, uint32_t len);
+
+/*
+ * Alignment restriction for flash writes.
+ */
+uint8_t flash_area_align(const struct flash_area *);
+
+/*
+ * Given flash map index, return info about sectors within the area.
+ */
+int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret);
+
+int flash_area_id_from_image_slot(int slot);
+int flash_area_id_to_image_slot(int area_id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_UTIL_FLASH_MAP_ */
diff --git a/zephyr/include/hal/hal_bsp.h b/zephyr/include/hal/hal_bsp.h
new file mode 100644
index 0000000..8b52f27
--- /dev/null
+++ b/zephyr/include/hal/hal_bsp.h
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef __HAL_BSP_H_
+#define __HAL_BSP_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <inttypes.h>
+
+/*
+ * Initializes BSP; registers flash_map with the system.
+ */
+void hal_bsp_init(void);
+
+/*
+ * Return pointer to flash device structure, given BSP specific
+ * flash id.
+ */
+struct hal_flash;
+const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id);
+
+/*
+ * Grows heap by given amount. XXX giving space back not implemented.
+ */
+void *_sbrk(int incr);
+
+/*
+ * Report which memory areas should be included inside a coredump.
+ */
+struct hal_bsp_mem_dump {
+    void *hbmd_start;
+    uint32_t hbmd_size;
+};
+
+const struct hal_bsp_mem_dump *hal_bsp_core_dump(int *area_cnt);
+
+/*
+ * Get unique HW identifier/serial number for platform.
+ * Returns the number of bytes filled in.
+ */
+#define HAL_BSP_MAX_ID_LEN  32
+int hal_bsp_hw_id(uint8_t *id, int max_len);
+
+#define HAL_BSP_POWER_ON (1)
+#define HAL_BSP_POWER_WFI (2)
+#define HAL_BSP_POWER_SLEEP (3)
+#define HAL_BSP_POWER_DEEP_SLEEP (4)
+#define HAL_BSP_POWER_OFF (5)
+#define HAL_BSP_POWER_PERUSER (128)
+
+int hal_bsp_power_state(int state);
+
+/* Returns priority of given interrupt number */
+uint32_t hal_bsp_get_nvic_priority(int irq_num, uint32_t pri);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/zephyr/include/hal/hal_flash.h b/zephyr/include/hal/hal_flash.h
new file mode 100644
index 0000000..2895479
--- /dev/null
+++ b/zephyr/include/hal/hal_flash.h
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef H_HAL_FLASH_
+#define H_HAL_FLASH_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <inttypes.h>
+
+int hal_flash_read(uint8_t flash_id, uint32_t address, void *dst,
+  uint32_t num_bytes);
+int hal_flash_write(uint8_t flash_id, uint32_t address, const void *src,
+  uint32_t num_bytes);
+int hal_flash_erase_sector(uint8_t flash_id, uint32_t sector_address);
+int hal_flash_erase(uint8_t flash_id, uint32_t address, uint32_t num_bytes);
+uint8_t hal_flash_align(uint8_t flash_id);
+int hal_flash_init(void);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_HAL_FLASH_ */
diff --git a/zephyr/include/os/os.h b/zephyr/include/os/os.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/zephyr/include/os/os.h
diff --git a/zephyr/include/os/os_heap.h b/zephyr/include/os/os_heap.h
new file mode 100644
index 0000000..4413568
--- /dev/null
+++ b/zephyr/include/os/os_heap.h
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef H_OS_HEAP_
+#define H_OS_HEAP_
+
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void *os_malloc(size_t size);
+void os_free(void *mem);
+void *os_realloc(void *ptr, size_t size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
diff --git a/zephyr/include/os/os_malloc.h b/zephyr/include/os/os_malloc.h
new file mode 100644
index 0000000..32b72c2
--- /dev/null
+++ b/zephyr/include/os/os_malloc.h
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef H_OS_MALLOC_
+#define H_OS_MALLOC_
+
+#include "os/os_heap.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#undef  malloc
+#define malloc  os_malloc
+
+#undef  free
+#define free    os_free
+
+#undef  realloc
+#define realloc  os_realloc
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/zephyr/include/syscfg/syscfg.h b/zephyr/include/syscfg/syscfg.h
new file mode 100644
index 0000000..ad58eed
--- /dev/null
+++ b/zephyr/include/syscfg/syscfg.h
@@ -0,0 +1,6 @@
+#ifndef __SYSCFG_H__
+#define __SYSCFG_H__
+
+#define MYNEWT_VAL(x) (x)
+
+#endif /* __SYSCFG_H__ */
diff --git a/zephyr/include/sysflash/sysflash.h b/zephyr/include/sysflash/sysflash.h
new file mode 100644
index 0000000..e08f9eb
--- /dev/null
+++ b/zephyr/include/sysflash/sysflash.h
@@ -0,0 +1,10 @@
+/* Manual version of auto-generated version. */
+
+#ifndef __SYSFLASH_H__
+#define __SYSFLASH_H__
+
+#define FLASH_AREA_IMAGE_0 1
+#define FLASH_AREA_IMAGE_1 2
+#define FLASH_AREA_IMAGE_SCRATCH 3
+
+#endif /* __SYSFLASH_H__ */
diff --git a/zephyr/prj.conf b/zephyr/prj.conf
new file mode 100644
index 0000000..5c36b1d
--- /dev/null
+++ b/zephyr/prj.conf
@@ -0,0 +1,14 @@
+CONFIG_CONSOLE_HANDLER=y
+CONFIG_PRINTK=y
+CONFIG_DEBUG=y
+
+CONFIG_MAIN_STACK_SIZE=10240
+CONFIG_MBEDTLS=y
+CONFIG_MBEDTLS_BUILTIN=y
+CONFIG_MBEDTLS_CFG_FILE="config-boot.h"
+
+### mbedTLS wants a heap
+CONFIG_HEAP_MEM_POOL_SIZE=16384
+
+CONFIG_FLASH=y
+CONFIG_SOC_FLASH_STM32F4=y
diff --git a/zephyr/src/Makefile b/zephyr/src/Makefile
new file mode 100644
index 0000000..69283e3
--- /dev/null
+++ b/zephyr/src/Makefile
@@ -0,0 +1,8 @@
+subdir-ccflags-y += -I$(PROJECT)/../boot/bootutil/include
+subdir-ccflags-y += -I$(PROJECT)/include
+
+obj-y += main.o
+obj-y += flash_map.o hal_flash.o os.o
+obj-y += keys.o
+
+obj-y += ../../boot/bootutil/src/
diff --git a/zephyr/src/flash_map.c b/zephyr/src/flash_map.c
new file mode 100644
index 0000000..aadf13a
--- /dev/null
+++ b/zephyr/src/flash_map.c
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#include <zephyr.h>
+#include <misc/printk.h>
+#include <flash.h>
+
+#include <flash_map/flash_map.h>
+#include <hal/hal_flash.h>
+#include <sysflash/sysflash.h>
+
+extern struct device *boot_flash_device;
+
+/*
+ * The flash area describes essentially the partition table of the
+ * flash.  In this case, it starts with FLASH_AREA_IMAGE_0.
+ */
+static const struct flash_area part_map[] = {
+	{
+		.fa_id = FLASH_AREA_IMAGE_0,
+		.fa_off = 0x20000,
+		.fa_size = 0x20000,
+	},
+	{
+		.fa_id = FLASH_AREA_IMAGE_1,
+		.fa_off = 0x40000,
+		.fa_size = 0x20000,
+	},
+	{
+		.fa_id = FLASH_AREA_IMAGE_SCRATCH,
+		.fa_off = 0x60000,
+		.fa_size = 0x20000,
+	},
+};
+
+/*
+ * 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);
+
+	for (i = 0; i < ARRAY_SIZE(part_map); i++) {
+		if (id == part_map[i].fa_id)
+			break;
+	}
+	if (i == ARRAY_SIZE(part_map))
+		return -1;
+
+	*area = &part_map[i];
+	return 0;
+}
+
+/*
+ * Nothing to do on close.
+ */
+void flash_area_close(const struct flash_area *area)
+{
+}
+
+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);
+	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);
+	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);
+	return flash_erase(boot_flash_device, area->fa_off + off, len);
+}
+
+uint8_t flash_area_align(const struct flash_area *area)
+{
+	return hal_flash_align(area->fa_id);
+}
+
+/*
+ * This depends on the mappings defined in sysflash.h, and assumes
+ * that slot 0, slot 1, and the scratch area area contiguous.
+ */
+int flash_area_id_from_image_slot(int slot)
+{
+	return slot + FLASH_AREA_IMAGE_0;
+}
+
+/*
+ * Lookup the sector map for a given flash area.  This should fill in
+ * `ret` with all of the sectors in the area.  `*cnt` will be set to
+ * the storage at `ret` and should be set to the final number of
+ * sectors in this area.
+ */
+int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
+{
+	uint32_t off;
+
+	printk("%s: lookup area %d\n", __func__, idx);
+	/*
+	 * This simple layout has uniform slots, so just fill in the
+	 * right one.
+	 */
+	if (idx < FLASH_AREA_IMAGE_0 || idx > FLASH_AREA_IMAGE_SCRATCH)
+		return -1;
+
+	if (*cnt < 1)
+		return -1;
+
+	off = (idx - FLASH_AREA_IMAGE_0 + 1) * 0x20000;
+
+	ret->fa_id = idx;
+	ret->fa_device_id = 0;
+	ret->pad16 = 0;
+	ret->fa_off = off;
+	ret->fa_size = 0x20000;
+
+	return 0;
+}
diff --git a/zephyr/src/hal_flash.c b/zephyr/src/hal_flash.c
new file mode 100644
index 0000000..a3700bc
--- /dev/null
+++ b/zephyr/src/hal_flash.c
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#include <zephyr.h>
+#include <misc/printk.h>
+
+#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)
+{
+	return FLASH_ALIGN;
+}
diff --git a/zephyr/src/keys.c b/zephyr/src/keys.c
new file mode 100644
index 0000000..051eb8a
--- /dev/null
+++ b/zephyr/src/keys.c
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#include <bootutil/sign_key.h>
+
+const unsigned char root_pub_der[] = {
+  0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xd1, 0x06, 0x08,
+  0x1a, 0x18, 0x44, 0x2c, 0x18, 0xe8, 0xfb, 0xfd, 0xf7, 0x0d, 0xa3, 0x4f,
+  0x1f, 0xbb, 0xee, 0x5e, 0xf9, 0xaa, 0xd2, 0x4b, 0x18, 0xd3, 0x5a, 0xe9,
+  0x6d, 0x18, 0x80, 0x19, 0xf9, 0xf0, 0x9c, 0x34, 0x1b, 0xcb, 0xf3, 0xbc,
+  0x74, 0xdb, 0x42, 0xe7, 0x8c, 0x7f, 0x10, 0x53, 0x7e, 0x43, 0x5e, 0x0d,
+  0x57, 0x2c, 0x44, 0xd1, 0x67, 0x08, 0x0f, 0x0d, 0xbb, 0x5c, 0xee, 0xec,
+  0xb3, 0x99, 0xdf, 0xe0, 0x4d, 0x84, 0x0b, 0xaa, 0x77, 0x41, 0x60, 0xed,
+  0x15, 0x28, 0x49, 0xa7, 0x01, 0xb4, 0x3c, 0x10, 0xe6, 0x69, 0x8c, 0x2f,
+  0x5f, 0xac, 0x41, 0x4d, 0x9e, 0x5c, 0x14, 0xdf, 0xf2, 0xf8, 0xcf, 0x3d,
+  0x1e, 0x6f, 0xe7, 0x5b, 0xba, 0xb4, 0xa9, 0xc8, 0x88, 0x7e, 0x47, 0x3c,
+  0x94, 0xc3, 0x77, 0x67, 0x54, 0x4b, 0xaa, 0x8d, 0x38, 0x35, 0xca, 0x62,
+  0x61, 0x7e, 0xb7, 0xe1, 0x15, 0xdb, 0x77, 0x73, 0xd4, 0xbe, 0x7b, 0x72,
+  0x21, 0x89, 0x69, 0x24, 0xfb, 0xf8, 0x65, 0x6e, 0x64, 0x3e, 0xc8, 0x0e,
+  0xd7, 0x85, 0xd5, 0x5c, 0x4a, 0xe4, 0x53, 0x0d, 0x2f, 0xff, 0xb7, 0xfd,
+  0xf3, 0x13, 0x39, 0x83, 0x3f, 0xa3, 0xae, 0xd2, 0x0f, 0xa7, 0x6a, 0x9d,
+  0xf9, 0xfe, 0xb8, 0xce, 0xfa, 0x2a, 0xbe, 0xaf, 0xb8, 0xe0, 0xfa, 0x82,
+  0x37, 0x54, 0xf4, 0x3e, 0xe1, 0x2b, 0xd0, 0xd3, 0x08, 0x58, 0x18, 0xf6,
+  0x5e, 0x4c, 0xc8, 0x88, 0x81, 0x31, 0xad, 0x5f, 0xb0, 0x82, 0x17, 0xf2,
+  0x8a, 0x69, 0x27, 0x23, 0xf3, 0xab, 0x87, 0x3e, 0x93, 0x1a, 0x1d, 0xfe,
+  0xe8, 0xf8, 0x1a, 0x24, 0x66, 0x59, 0xf8, 0x1c, 0xab, 0xdc, 0xce, 0x68,
+  0x1b, 0x66, 0x64, 0x35, 0xec, 0xfa, 0x0d, 0x11, 0x9d, 0xaf, 0x5c, 0x3a,
+  0xa7, 0xd1, 0x67, 0xc6, 0x47, 0xef, 0xb1, 0x4b, 0x2c, 0x62, 0xe1, 0xd1,
+  0xc9, 0x02, 0x03, 0x01, 0x00, 0x01
+};
+const unsigned int root_pub_der_len = 270;
+
+const struct bootutil_key bootutil_keys[] = {
+	{
+		.key = root_pub_der,
+		.len = &root_pub_der_len,
+	},
+};
+const int bootutil_key_cnt = 1;
diff --git a/zephyr/src/main.c b/zephyr/src/main.c
new file mode 100644
index 0000000..800fe51
--- /dev/null
+++ b/zephyr/src/main.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2012-2014 Wind River Systems, Inc.
+ *
+ * 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.
+ */
+
+#include <zephyr.h>
+#include <misc/printk.h>
+#include <flash.h>
+#include <asm_inline.h>
+
+#include "bootutil/image.h"
+#include "bootutil/bootutil.h"
+
+#if defined(CONFIG_BOARD_FRDM_K64F)
+#define BOOT_FLASH "KSDK_FLASH"
+#elif defined(CONFIG_BOARD_96B_CARBON)
+#define BOOT_FLASH "STM32F4_FLASH"
+#else
+#error "Board is currently not supported by bootloader"
+#endif
+
+struct device *boot_flash_device;
+
+struct vector_table {
+	uint32_t msp;
+	uint32_t reset;
+};
+
+void os_heap_init(void);
+
+void main(void)
+{
+	struct boot_rsp rsp;
+	struct vector_table *vt;
+	int rc;
+
+	os_heap_init();
+
+	boot_flash_device = device_get_binding(BOOT_FLASH);
+	if (!boot_flash_device) {
+		printk("Flash device not found\n");
+		while (1)
+			;
+	}
+
+	rc = boot_go(&rsp);
+	if (rc != 0) {
+		printk("Unable to find bootable image\n");
+		while (1)
+			;
+	}
+
+	printk("Bootloader chain: 0x%x\n", rsp.br_image_addr);
+	vt = (struct vector_table *)(rsp.br_image_addr +
+				     rsp.br_hdr->ih_hdr_size);
+	irq_lock();
+	_MspSet(vt->msp);
+
+	/* Not all targets set the VTOR, so just set it. */
+	_scs_relocate_vector_table((void *) vt);
+
+	((void (*)(void))vt->reset)();
+
+	printk("Never should get here\n");
+	while (1)
+		;
+}
diff --git a/zephyr/src/os.c b/zephyr/src/os.c
new file mode 100644
index 0000000..80be2fa
--- /dev/null
+++ b/zephyr/src/os.c
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#include <zephyr.h>
+#include <misc/printk.h>
+#include <string.h>
+
+#include "os/os_heap.h"
+
+#define MBEDTLS_CONFIG_FILE CONFIG_MBEDTLS_CFG_FILE
+#include <mbedtls/platform.h>
+
+/* D(void *os_malloc(size_t size)) */
+void *os_calloc(size_t nelem, size_t size)
+{
+	/* Note that this doesn't check for overflow.  Assume the
+	 * calls only come from within the app. */
+	size_t total = nelem * size;
+	void *buf = k_malloc(total);
+	if (buf)
+		memset(buf, 0, total);
+	return buf;
+}
+
+void os_free(void *ptr)
+{
+	k_free(ptr);
+}
+
+/*
+ * Initialize mbedtls to be able to use the local heap.
+ */
+void os_heap_init(void)
+{
+	mbedtls_platform_set_calloc_free(os_calloc, os_free);
+}
diff --git a/zephyr/target.sh.example b/zephyr/target.sh.example
new file mode 100644
index 0000000..1652853
--- /dev/null
+++ b/zephyr/target.sh.example
@@ -0,0 +1,17 @@
+# 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