samples: Create a zephyr sample

Most of the meat of this is in the Makefile, which is able to build the
bootloader, and two small applications, along with instructions on how
to load these into flash and test that upgrades work.

JIRA: MCUB-62
Signed-off-by: David Brown <david.brown@linaro.org>
diff --git a/samples/zephyr/Makefile b/samples/zephyr/Makefile
new file mode 100644
index 0000000..3c0b58e
--- /dev/null
+++ b/samples/zephyr/Makefile
@@ -0,0 +1,140 @@
+###########################################################################
+# Sample multi-part application Makefile
+#
+# Copyright (c) 2017 Linaro Limited
+#
+# 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.
+###########################################################################
+
+# This is an example Makefile to demonstrate how to use mcuboot to
+# deploy and upgrade images.  The image building should work on any
+# supported target, however flashing will likely require changes to
+# the flash addresses, depending on the partition layout of the device
+# in question.
+#
+# running
+# 
+#     make BOARD=frdm_k64f all
+#
+# should generate three "*.bin" files in this directory:
+#
+#   mcuboot.bin: The bootloader itself
+#   signed-hello1.bin: A signed sample.
+#   signed-hello2.bin: An upgrade image, signed and marked for
+#   upgrade.
+#
+# "make flash_boot" should flash the bootloader into the flash,
+# erasing the rest of the device.  If you examine the device at this
+# time, you should see a message about the bootloader not being able
+# to find a bootable image.
+#
+# "make flash_hello1" will then flash the first application into
+# "slot0".  This should boot into this app, print a small message, and
+# give the zephyr console.
+#
+# "make flash_hello2" will flash hello2 into the second slot.  The
+# reset should upgrade and run the new image.  Resetting again should
+# then revert back to the first app, since we did not mark this image
+# as good.
+
+BOARD ?=
+
+.PHONY: check boot hello1 clean_boot clean_hello1 \
+	hello2 clean_hello2 flash_boot flash_hello1 flash_hello2
+
+# For signing, use the default RSA demo key, to match the default in
+# the mcuboot Makefile.
+SIGNING_KEY = ../../root-rsa-2048.pem
+
+# The header size should match that in hello1/prj.conf
+# CONFIG_TEXT_SECTION_OFFSET.  This value needs to be a power of two
+# that is at least as large as the size of the vector table.  The
+# value given here of 0x200 should be sufficient for any supported
+# devices, but it can be made smaller, as long as this value matches
+# that used to build the app.
+BOOT_HEADER_LEN = 0x200
+
+# For upgrades, the signing tool needs to know the device alignment.
+# This requirement will be going away soon.
+FLASH_ALIGNMENT = 8
+
+IMGTOOL = ../../scripts/imgtool.py
+PYOCD_FLASHTOOL = pyocd-flashtool
+
+help:
+	@echo "make <target> BOARD=<board>"
+	@echo "<target>: all, boot, hello1"
+	@echo "<board>: frdm_k64f, 96b_carbon, etc."
+
+all: boot hello1 hello2
+
+clean: clean_boot clean_hello1 clean_hello2
+	@rm -f signed-hello1.bin
+	@rm -f signed-hello2.bin
+	@rm -f mcuboot.bin
+
+boot: check
+	@rm -f mcuboot.bin
+	$(MAKE) -C ../.. BOARD=$(BOARD) -j$(nproc)
+	cp ../../outdir/$(BOARD)/zephyr.bin mcuboot.bin
+
+clean_boot: check
+	rm -rf ../../outdir/$(BOARD)
+
+# Build and sign "hello1".
+hello1: check
+	$(MAKE) -C hello1 BOARD=$(BOARD) -j$(nproc)
+	$(IMGTOOL) sign \
+		--key $(SIGNING_KEY) \
+		--header-size $(BOOT_HEADER_LEN) \
+		--align $(FLASH_ALIGNMENT) \
+		--version 1.2 \
+		--included-header \
+		hello1/outdir/$(BOARD)/zephyr.bin \
+		signed-hello1.bin
+
+clean_hello1: check
+	rm -rf hello1/outdir/$(BOARD)
+
+# Build and sign "hello2".
+# This is the same signing command as above, except that it adds the
+# "--pad" argument.  This will also add the trailer that indicates
+# this image is intended to be an upgrade.  It should be flashed into
+# slot1 instead of slot0.
+hello2: check
+	$(MAKE) -C hello2 BOARD=$(BOARD) -j$(nproc)
+	$(IMGTOOL) sign \
+		--key $(SIGNING_KEY) \
+		--header-size $(BOOT_HEADER_LEN) \
+		--align $(FLASH_ALIGNMENT) \
+		--version 1.2 \
+		--included-header \
+		--pad 0x60000 \
+		hello2/outdir/$(BOARD)/zephyr.bin \
+		signed-hello2.bin
+
+clean_hello2: check
+	rm -rf hello2/outdir/$(BOARD)
+
+flash_boot:
+	$(PYOCD_FLASHTOOL) -ce -a 0 mcuboot.bin
+
+flash_hello1:
+	$(PYOCD_FLASHTOOL) -a 0x20000 signed-hello1.bin
+
+flash_hello2:
+	$(PYOCD_FLASHTOOL) -a 0x80000 signed-hello2.bin
+
+check:
+	@if [ -z "$$ZEPHYR_BASE" ]; then echo "Zephyr environment not set up"; false; fi
+	@if [ -z "$(BOARD)" ]; then echo "You must specity BOARD=<board>"; false; fi
diff --git a/samples/zephyr/README.md b/samples/zephyr/README.md
new file mode 100644
index 0000000..1aaa8c1
--- /dev/null
+++ b/samples/zephyr/README.md
@@ -0,0 +1,13 @@
+# Zephyr sample application.
+
+In order to successfully deploy an application using mcuboot, it is
+necessary to build at least one other binary: the application itself.
+It is beyond the scope of this documentation to describe what an
+application is able to do, however a working example is certainly
+useful.
+
+Please see the comments in the Makefile in this directory for more
+details on how to build and test this application.
+
+The hello applications were taken directly from the Zephyr source
+tree.
diff --git a/samples/zephyr/build-boot.sh b/samples/zephyr/build-boot.sh
new file mode 100755
index 0000000..20e61a0
--- /dev/null
+++ b/samples/zephyr/build-boot.sh
@@ -0,0 +1,22 @@
+#! /bin/bash
+
+# Build the bootloader.
+
+# In order to build successfully, ZEPHYR_SDK_INSTALL_DIR and
+# ZEPHYR_GCC_VARIANT need to be set, as well as zephyr/zephyr-env.sh
+# must be sourced.
+
+die() {
+    echo error: "$@"
+    exit 1
+}
+
+if [ -z "$ZEPHYR_BASE" ]; then
+    die "Please setup for a Zephyr build before running this script."
+fi
+
+if [ -z "$BOARD" ]; then
+    die "Please set BOARD to a valid board before running this script."
+fi
+
+make -C ../.. BOARD=${BOARD} -j$(nproc) || die "Build mcuboot"
diff --git a/samples/zephyr/build-hello.sh b/samples/zephyr/build-hello.sh
new file mode 100755
index 0000000..d8a3af4
--- /dev/null
+++ b/samples/zephyr/build-hello.sh
@@ -0,0 +1,22 @@
+#! /bin/bash
+
+# Build the Sample hello program
+
+# In order to build successfully, ZEPHYR_SDK_INSTALL_DIR and
+# ZEPHYR_GCC_VARIANT need to be set, as well as zephyr/zephyr-env.sh
+# must be sourced.
+
+die() {
+    echo error: "$@"
+    exit 1
+}
+
+if [ -z "$ZEPHYR_BASE" ]; then
+    die "Please setup for a Zephyr build before running this script."
+fi
+
+if [ -z "$BOARD" ]; then
+    die "Please set BOARD to a valid board before running this script."
+fi
+
+make -C hello1 BOARD=${BOARD} -j$(nproc) || die "Build hello1"
diff --git a/samples/zephyr/hello1/Makefile b/samples/zephyr/hello1/Makefile
new file mode 100644
index 0000000..e70a750
--- /dev/null
+++ b/samples/zephyr/hello1/Makefile
@@ -0,0 +1,4 @@
+BOARD ?= qemu_x86
+CONF_FILE = prj.conf
+
+include ${ZEPHYR_BASE}/Makefile.test
diff --git a/samples/zephyr/hello1/README.rst b/samples/zephyr/hello1/README.rst
new file mode 100644
index 0000000..52a25d7
--- /dev/null
+++ b/samples/zephyr/hello1/README.rst
@@ -0,0 +1,38 @@
+.. _hello_world:
+
+Hello World
+###########
+
+Overview
+********
+A simple Hello World example that can be used with any supported board and
+prints 'Hello World' to the console. This application can be built into modes:
+
+* single thread
+* multi threading
+
+Building and Running
+********************
+
+This project outputs 'Hello World' to the console.  It can be built and executed
+on QEMU as follows:
+
+.. code-block:: console
+
+   $ cd samples/hello_world
+   $ make run
+
+
+To build the single thread version, use the supplied configuration file for
+single thread: :file:`prj_single.conf`:
+
+.. code-block:: console
+
+   $ make CONF_FILE=prj_single.conf run
+
+Sample Output
+=============
+
+.. code-block:: console
+
+    Hello World! x86
diff --git a/samples/zephyr/hello1/frdm_k64f.overlay b/samples/zephyr/hello1/frdm_k64f.overlay
new file mode 100644
index 0000000..8ca56d7
--- /dev/null
+++ b/samples/zephyr/hello1/frdm_k64f.overlay
@@ -0,0 +1,9 @@
+/* DTS overlay file.
+ * vim: ft=dts
+ */
+
+/ {
+	chosen {
+		zephyr,code-partition = &slot0_partition;
+	};
+};
diff --git a/samples/zephyr/hello1/prj.conf b/samples/zephyr/hello1/prj.conf
new file mode 100644
index 0000000..b5d941b
--- /dev/null
+++ b/samples/zephyr/hello1/prj.conf
@@ -0,0 +1,7 @@
+# Enable the console
+CONFIG_CONSOLE_SHELL=y
+CONFIG_KERNEL_SHELL=y
+
+# Use the bootloader.
+CONFIG_BOOT_HEADER=y
+CONFIG_TEXT_SECTION_OFFSET=0x200
diff --git a/samples/zephyr/hello1/sample.yaml b/samples/zephyr/hello1/sample.yaml
new file mode 100644
index 0000000..47396bf
--- /dev/null
+++ b/samples/zephyr/hello1/sample.yaml
@@ -0,0 +1,15 @@
+sample:
+  name: hello world
+  description: Hello World sample, the simplest Zephyr application
+  platforms: all
+tests:
+    - test:
+        build_only: true
+        tags: samples tests
+        min_ram: 16
+    - singlethread:
+        build_only: true
+        extra_args: CONF_FILE=prj_single.conf
+        filter: not CONFIG_BLUETOOTH and not CONFIG_GPIO_SCH
+        tags: samples tests
+        min_ram: 16
diff --git a/samples/zephyr/hello1/src/Makefile b/samples/zephyr/hello1/src/Makefile
new file mode 100644
index 0000000..00066e1
--- /dev/null
+++ b/samples/zephyr/hello1/src/Makefile
@@ -0,0 +1 @@
+obj-y = main.o
diff --git a/samples/zephyr/hello1/src/main.c b/samples/zephyr/hello1/src/main.c
new file mode 100644
index 0000000..3acc91d
--- /dev/null
+++ b/samples/zephyr/hello1/src/main.c
@@ -0,0 +1,13 @@
+/*
+ * Copyright (c) 2012-2014 Wind River Systems, Inc.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <zephyr.h>
+#include <misc/printk.h>
+
+void main(void)
+{
+	printk("Hello World number 1! %s\n", CONFIG_ARCH);
+}
diff --git a/samples/zephyr/hello2/Makefile b/samples/zephyr/hello2/Makefile
new file mode 100644
index 0000000..e70a750
--- /dev/null
+++ b/samples/zephyr/hello2/Makefile
@@ -0,0 +1,4 @@
+BOARD ?= qemu_x86
+CONF_FILE = prj.conf
+
+include ${ZEPHYR_BASE}/Makefile.test
diff --git a/samples/zephyr/hello2/README.rst b/samples/zephyr/hello2/README.rst
new file mode 100644
index 0000000..52a25d7
--- /dev/null
+++ b/samples/zephyr/hello2/README.rst
@@ -0,0 +1,38 @@
+.. _hello_world:
+
+Hello World
+###########
+
+Overview
+********
+A simple Hello World example that can be used with any supported board and
+prints 'Hello World' to the console. This application can be built into modes:
+
+* single thread
+* multi threading
+
+Building and Running
+********************
+
+This project outputs 'Hello World' to the console.  It can be built and executed
+on QEMU as follows:
+
+.. code-block:: console
+
+   $ cd samples/hello_world
+   $ make run
+
+
+To build the single thread version, use the supplied configuration file for
+single thread: :file:`prj_single.conf`:
+
+.. code-block:: console
+
+   $ make CONF_FILE=prj_single.conf run
+
+Sample Output
+=============
+
+.. code-block:: console
+
+    Hello World! x86
diff --git a/samples/zephyr/hello2/frdm_k64f.overlay b/samples/zephyr/hello2/frdm_k64f.overlay
new file mode 100644
index 0000000..8ca56d7
--- /dev/null
+++ b/samples/zephyr/hello2/frdm_k64f.overlay
@@ -0,0 +1,9 @@
+/* DTS overlay file.
+ * vim: ft=dts
+ */
+
+/ {
+	chosen {
+		zephyr,code-partition = &slot0_partition;
+	};
+};
diff --git a/samples/zephyr/hello2/prj.conf b/samples/zephyr/hello2/prj.conf
new file mode 100644
index 0000000..b5d941b
--- /dev/null
+++ b/samples/zephyr/hello2/prj.conf
@@ -0,0 +1,7 @@
+# Enable the console
+CONFIG_CONSOLE_SHELL=y
+CONFIG_KERNEL_SHELL=y
+
+# Use the bootloader.
+CONFIG_BOOT_HEADER=y
+CONFIG_TEXT_SECTION_OFFSET=0x200
diff --git a/samples/zephyr/hello2/sample.yaml b/samples/zephyr/hello2/sample.yaml
new file mode 100644
index 0000000..47396bf
--- /dev/null
+++ b/samples/zephyr/hello2/sample.yaml
@@ -0,0 +1,15 @@
+sample:
+  name: hello world
+  description: Hello World sample, the simplest Zephyr application
+  platforms: all
+tests:
+    - test:
+        build_only: true
+        tags: samples tests
+        min_ram: 16
+    - singlethread:
+        build_only: true
+        extra_args: CONF_FILE=prj_single.conf
+        filter: not CONFIG_BLUETOOTH and not CONFIG_GPIO_SCH
+        tags: samples tests
+        min_ram: 16
diff --git a/samples/zephyr/hello2/src/Makefile b/samples/zephyr/hello2/src/Makefile
new file mode 100644
index 0000000..00066e1
--- /dev/null
+++ b/samples/zephyr/hello2/src/Makefile
@@ -0,0 +1 @@
+obj-y = main.o
diff --git a/samples/zephyr/hello2/src/main.c b/samples/zephyr/hello2/src/main.c
new file mode 100644
index 0000000..784f206
--- /dev/null
+++ b/samples/zephyr/hello2/src/main.c
@@ -0,0 +1,13 @@
+/*
+ * Copyright (c) 2012-2014 Wind River Systems, Inc.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <zephyr.h>
+#include <misc/printk.h>
+
+void main(void)
+{
+	printk("Upgraded hello! %s\n", CONFIG_ARCH);
+}