Merge pull request #24 from andrewboie/master

Inital x86 support
diff --git a/Makefile b/Makefile
index 1c726bb..25d06de 100644
--- a/Makefile
+++ b/Makefile
@@ -29,6 +29,9 @@
 # mcuboot/Zephyr.  These can be found in ``boot/zephyr/targets``
 BOARD ?= qemu_x86
 
+# Additional board-specific Zephyr configuration
+CONF_FILE += $(wildcard boot/zephyr/targets/$(BOARD).conf)
+
 # The source to the Zephyr-specific code lives here.
 SOURCE_DIR = boot/zephyr
 
diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c
index 50da986..4e79f0a 100644
--- a/boot/zephyr/main.c
+++ b/boot/zephyr/main.c
@@ -32,17 +32,49 @@
 
 struct device *boot_flash_device;
 
-struct vector_table {
+void os_heap_init(void);
+
+#if defined(CONFIG_ARM)
+struct arm_vector_table {
 	uint32_t msp;
 	uint32_t reset;
 };
 
-void os_heap_init(void);
+static void do_boot(struct boot_rsp *rsp)
+{
+	struct arm_vector_table *vt;
+
+	/* The beginning of the image is the ARM vector table, containing
+	 * the initial stack pointer address and the reset vector
+	 * consecutively. Manually set the stack pointer and jump into the
+	 * reset vector
+	 */
+	vt = (struct vector_table *)(rsp->br_image_addr +
+				     rsp->br_hdr->ih_hdr_size);
+	irq_lock();
+	_MspSet(vt->msp);
+	((void (*)(void))vt->reset)();
+}
+#else
+/* Default: Assume entry point is at the very beginning of the image. Simply
+ * lock interrupts and jump there. This is the right thing to do for X86 and
+ * possibly other platforms.
+ */
+static void do_boot(struct boot_rsp *rsp)
+{
+	void *start;
+
+	start = (void *)(rsp->br_image_addr + rsp->br_hdr->ih_hdr_size);
+
+	/* Lock interrupts and dive into the entry point */
+	irq_lock();
+	((void (*)(void))start)();
+}
+#endif
 
 void main(void)
 {
 	struct boot_rsp rsp;
-	struct vector_table *vt;
 	int rc;
 
 	BOOT_LOG_INF("Starting bootloader");
@@ -64,13 +96,8 @@
 	}
 
 	BOOT_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);
-
 	BOOT_LOG_INF("Jumping to the first image slot");
-	((void (*)(void))vt->reset)();
+	do_boot(&rsp);
 
 	BOOT_LOG_ERR("Never should get here");
 	while (1)
diff --git a/boot/zephyr/prj-p256.conf b/boot/zephyr/prj-p256.conf
index 95b2b7e..8a3bd84 100644
--- a/boot/zephyr/prj-p256.conf
+++ b/boot/zephyr/prj-p256.conf
@@ -16,3 +16,5 @@
 
 ### Disable Bluetooth by default
 # CONFIG_BLUETOOTH is not set
+
+CONFIG_MULTITHREADING=n
diff --git a/boot/zephyr/prj.conf b/boot/zephyr/prj.conf
index f3cffde..f6d9342 100644
--- a/boot/zephyr/prj.conf
+++ b/boot/zephyr/prj.conf
@@ -14,3 +14,5 @@
 
 ### Disable Bluetooth by default
 # CONFIG_BLUETOOTH is not set
+
+CONFIG_MULTITHREADING=n
diff --git a/boot/zephyr/targets/arduino_101.conf b/boot/zephyr/targets/arduino_101.conf
new file mode 100644
index 0000000..b58b0d9
--- /dev/null
+++ b/boot/zephyr/targets/arduino_101.conf
@@ -0,0 +1,4 @@
+CONFIG_PHYS_LOAD_ADDR=0x40010000
+CONFIG_TEXT_SECTION_OFFSET=0x30
+CONFIG_ROM_SIZE=64
+CONFIG_SOC_FLASH_QMSI=y
diff --git a/boot/zephyr/targets/arduino_101.h b/boot/zephyr/targets/arduino_101.h
new file mode 100644
index 0000000..cecd9d4
--- /dev/null
+++ b/boot/zephyr/targets/arduino_101.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2017 Intel Corporation
+ *
+ * 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.
+ */
+
+/*
+ * NOTE: This flash layout is a simple one for demonstration purposes.
+ * It assumes we are building MCUBoot as a 3rd-stage loader, leaving the
+ * stock bootloaders on the Arduino 101 untouched.
+ *
+ * In this configuration MCUBoot will live at 0x40010000
+ */
+
+#define FLASH_DRIVER_NAME		CONFIG_SOC_FLASH_QMSI_DEV_NAME
+#define FLASH_ALIGN			4
+#define FLASH_AREA_IMAGE_0_OFFSET	0x40020000
+#define FLASH_AREA_IMAGE_0_SIZE		0x10000
+#define FLASH_AREA_IMAGE_1_OFFSET	0x40030000
+#define FLASH_AREA_IMAGE_1_SIZE		0x10000
+#define FLASH_AREA_IMAGE_SCRATCH_OFFSET	0x40040000
+#define FLASH_AREA_IMAGE_SCRATCH_SIZE	0x10000
diff --git a/scripts/zep2newt.py b/scripts/zep2newt.py
index 3062362..8d73ab2 100755
--- a/scripts/zep2newt.py
+++ b/scripts/zep2newt.py
@@ -172,8 +172,23 @@
         table will be at the beginning, rather than the zero padding.
         Verify that the padding is present.
         """
-        if self.image[:self.vtable_offs] != bytearray(self.vtable_offs):
-            raise Exception("Image does not have space for header")
+
+        size = self.vtable_offs
+
+        # First test: check if padded with all zeroes (ARM)
+        expected = bytearray(size)
+        if self.image[:size] == expected:
+            return
+
+        # x86 has a repeating pattern of 0x66 0x90
+        for i in range(0, size, 2):
+            expected[i] = 0x66
+            expected[i + 1] = 0x90
+
+        if self.image[:size] == expected:
+            return
+
+        raise Exception("Image does not have space for header")
 
     def make_header(self, sig):
         image_size = len(self.image) - self.vtable_offs