build: restructure nanohub tree

1. rearrange tree so that soft links are no longer necessary
2. decouple OS variants build from app build
3. add support for output directory

Bug: 29635686
Change-Id: I273b823c29582a85c632e43c23b10fa44123d8af
diff --git a/firmware/Makefile b/firmware/Makefile
deleted file mode 100644
index 81b8feb..0000000
--- a/firmware/Makefile
+++ /dev/null
@@ -1,129 +0,0 @@
-#
-# Copyright (C) 2016 The Android Open Source Project
-#
-# 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.
-#
-
-#find build target
-PLATFORM ?= stm32f4xx
-CPU ?= cortexm4f
-VARIANT ?= lunchbox
-DEBUG ?= -DDEBUG
-
-#bad words
-BADWORDS += strcpy strcat atoi
-BADWORDS += "rsaPrivOp=RSA private ops must never be compiled into firmware."
-
-#find makefiles
-MAKE_PLAT = misc/platform/$(PLATFORM)/Makefile
-MAKE_CPU = misc/cpu/$(CPU)/Makefile
-
-#link paths
-ifdef VARIANT_PATH
-LINK_PATH_MISC = $(VARIANT_PATH)/misc
-LINK_PATH_SRC = $(VARIANT_PATH)/src
-LINK_PATH_INC = $(VARIANT_PATH)/inc
-else
-LINK_PATH_MISC = misc/variant/$(VARIANT)
-LINK_PATH_SRC = src/variant/$(VARIANT)
-LINK_PATH_INC = inc/variant/$(VARIANT)
-endif
-
-#variant makefile
-MAKE_VAR = $(LINK_PATH_MISC)/Makefile
-
-#top make target
-SRCS_os :=
-SRCS_bl :=
-DELIVERABLES :=
-real: all
-
-#include makefiles for plat and cpu
-include $(MAKE_PLAT)
-include $(MAKE_CPU)
-include $(MAKE_VAR)
-
-FLAGS += -Wall -Werror -Iinc -Ilinks -Iexternal/freebsd/inc -I../lib/include -fshort-double
-#help avoid commmon embedded C mistakes
-FLAGS += -Wmissing-declarations -Wlogical-op -Waddress -Wempty-body -Wpointer-arith -Wenum-compare -Wdouble-promotion -Wfloat-equal -Wshadow -fno-strict-aliasing
-
-OSFLAGS += -g -ggdb3 -D_OS_BUILD_ -O2
-APPFLAGS += -Os
-
-#debug mode
-FLAGS += $(DEBUG)
-
-#bootloader pieces
-SRCS_bl += ../lib/nanohub/sha2.c ../lib/nanohub/rsa.c ../lib/nanohub/aes.c src/seos.c
-
-#frameworks
-SRCS_os += src/printf.c src/timer.c src/seos.c src/heap.c src/slab.c src/spi.c src/trylock.c
-SRCS_os += src/hostIntf.c src/hostIntfI2c.c src/hostIntfSpi.c src/nanohubCommand.c src/sensors.c src/syscall.c
-SRCS_os += src/eventQ.c src/osApi.c src/appSec.c src/simpleQ.c src/floatRt.c
-
-#some help for bootloader
-SRCS_bl += src/printf.c
-
-ifndef PLATFORM_HAS_HARDWARE_CRC
-SRCS_os += ../lib/nanohub/softcrc.c
-endif
-
-#app code
-include $(wildcard app/*/Makefile)
-
-
-#extra deps
-DEPS += $(wildcard app/*.h)
-DEPS += $(wildcard inc/*.h)
-DEPS += Makefile $(MAKE_PLAT) $(MAKE_CPU) $(MAKE_VAR)
-
-all: symlinks $(DELIVERABLES)
-
-symlinks: links/p_$(PLATFORM) links/c_$(CPU) links/v_$(VARIANT)
-
-links/p_$(PLATFORM):
-	rm -rf links/plat links/p_*
-	mkdir -p links/plat
-	ln -s ../../misc/platform/$(PLATFORM) links/plat/misc
-	ln -s ../../src/platform/$(PLATFORM) links/plat/src
-	ln -s ../../inc/platform/$(PLATFORM) links/plat/inc
-	touch links/p_$(PLATFORM)
-
-links/c_$(CPU):
-	rm -rf links/cpu links/c_*
-	mkdir -p links/cpu
-	ln -s ../../misc/cpu/$(CPU) links/cpu/misc
-	ln -s ../../src/cpu/$(CPU) links/cpu/src
-	ln -s ../../inc/cpu/$(CPU) links/cpu/inc
-	touch links/c_$(CPU)
-
-links/v_$(VARIANT):
-	rm -rf links/variant links/v_*
-	mkdir -p links/variant
-	ln -s ../../$(LINK_PATH_MISC) links/variant/misc
-	ln -s ../../$(LINK_PATH_SRC) links/variant/src
-	ln -s ../../$(LINK_PATH_INC) links/variant/inc
-	touch links/v_$(VARIANT)
-
-%.unchecked.elf: symlinks $(SRCS_$*) $(DEPS)
-	$(GCC) -o $@ $(SRCS_$*) $(OSFLAGS) $(OSFLAGS_$*) $(FLAGS) -lgcc -nostdlib
-
-%.checked.elf : %.unchecked.elf  symcheck.sh
-	./symcheck.sh $< $@ $(BADWORDS)
-
-full.bin: $(BL_FILE) $(OS_FILE)
-	cat $(BL_FILE) $(OS_FILE) > $@
-
-clean:
-	rm -rf $(DELIVERABLES) os.elf bootloader.elf links $(CLEANFILES)
-
diff --git a/firmware/README b/firmware/README
new file mode 100644
index 0000000..2bdb624
--- /dev/null
+++ b/firmware/README
@@ -0,0 +1,17 @@
+Define and export CROSS_COMPILE env var for your toolchain
+
+1. to build any OS variant, run
+
+make -C <variant_path>
+
+where <variant_path> is relative path to OS variant dir;
+or simply "cd  <variant_path>" and run "make".
+for local variants, variant_path is variant/<variant_name>
+
+2. to build nanoapp, run
+
+make -C <nanapp_path>
+
+where <nanoapp_path> is relative path to nanoapp;
+or simply "cd  <nanoapp_path>" and run "make".
+for local nanoapps, nanoapp_path is app/<app_name>
diff --git a/firmware/app/Android.mk b/firmware/app/Android.mk
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/firmware/app/Android.mk
diff --git a/firmware/app/app.mk b/firmware/app/app.mk
new file mode 100644
index 0000000..2ace2d2
--- /dev/null
+++ b/firmware/app/app.mk
@@ -0,0 +1,267 @@
+#
+# Copyright (C) 2016 The Android Open Source Project
+#
+# 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.
+#
+################################################################################
+#
+# NanoApp C/C++ Makefile Utils
+#
+################################################################################
+
+# Configuration ################################################################
+
+# Toolchain Prefix
+ifndef CROSS_COMPILE
+  $(error Please set the environment variable CROSS_COMPILE to the complete \
+          path to the toolchain directory plus the binary prefix, e.g. export \
+          CROSS_COMPILE=~/bin/gcc-arm-none-eabi-4_8-2014q3/bin/arm-none-eabi-)
+endif
+
+PREFIX = $(CROSS_COMPILE)
+TOOLCHAIN_DIR = $(shell dirname `which $(CROSS_COMPILE)gcc`)/..
+
+NANOAPP_POSTPROCESS := $(NANOHUB_DIR)/../util/nanoapp_postprocess/nanoapp_postprocess
+NANOAPP_SIGN := $(NANOHUB_DIR)/../util/nanoapp_sign/nanoapp_sign
+
+VARIANT ?= lunchbox
+PLATFORM ?= stm32
+CHIP ?= stm32f411
+CPU ?= cortexm4
+
+VARIANT_PATH ?= device/google/contexthub/firmware/variant/$(VARIANT)
+VARIANT_PATH := $(NANOHUB_DIR)/../../../../$(VARIANT_PATH)
+
+# all output goes here
+OUT ?= out/$(VARIANT)/app/$(BIN)
+
+################################################################################
+#
+# Nanoapp Libc/Libm Utils
+#
+################################################################################
+
+include $(NANOHUB_DIR)/lib/lib.mk
+
+# Tools ########################################################################
+
+AS := $(PREFIX)gcc
+CC := $(PREFIX)gcc
+CXX := $(PREFIX)g++
+OBJCOPY := $(PREFIX)objcopy
+OBJDUMP := $(PREFIX)objdump
+
+# Assembly Flags ###############################################################
+
+AS_FLAGS +=
+
+# C++ Flags ####################################################################
+
+CXX_CFLAGS += -std=c++11
+CXX_CFLAGS += -fno-exceptions
+CXX_CFLAGS += -fno-rtti
+
+# C Flags ######################################################################
+
+C_CFLAGS +=
+
+# Common Flags #################################################################
+
+# Defines
+CFLAGS += -DAPP_ID=$(APP_ID)
+CFLAGS += -DAPP_VERSION=$(APP_VERSION)
+CFLAGS += -D__NANOHUB__
+
+# Optimization/debug
+CFLAGS += -Os
+CFLAGS += -g
+
+# Include paths
+CFLAGS += -I$(NANOHUB_DIR)/os/inc
+CFLAGS += -I$(NANOHUB_DIR)/os/platform/$(PLATFORM)/inc
+CFLAGS += -I$(NANOHUB_DIR)/os/cpu/$(CPU)/inc
+CFLAGS += -I$(VARIANT_PATH)/inc
+CFLAGS += -I$(NANOHUB_DIR)/../lib/include
+
+# Warnings/error configuration.
+CFLAGS += -Wall
+CFLAGS += -Werror
+CFLAGS += -Wmissing-declarations
+CFLAGS += -Wlogical-op
+CFLAGS += -Waddress
+CFLAGS += -Wempty-body
+CFLAGS += -Wpointer-arith
+CFLAGS += -Wenum-compare
+CFLAGS += -Wdouble-promotion
+CFLAGS += -Wshadow
+CFLAGS += -Wno-attributes
+
+# Produce position independent code.
+CFLAGS += -fpic
+CFLAGS += -mno-pic-data-is-text-relative
+CFLAGS += -msingle-pic-base
+CFLAGS += -mpic-register=r9
+
+# Code generation options for Cortex-M4F
+CFLAGS += -mthumb
+CFLAGS += -mcpu=cortex-m4
+CFLAGS += -march=armv7e-m
+CFLAGS += -mfloat-abi=softfp
+CFLAGS += -mfpu=fpv4-sp-d16
+CFLAGS += -mno-thumb-interwork
+CFLAGS += -ffast-math
+CFLAGS += -fsingle-precision-constant
+
+# Platform defines
+CFLAGS += -DARM
+CFLAGS += -DUSE_NANOHUB_FLOAT_RUNTIME
+CFLAGS += -DARM_MATH_CM4
+CFLAGS += -D__FPU_PRESENT
+
+# Miscellaneous
+CFLAGS += -fno-strict-aliasing
+CFLAGS += -fshort-double
+CFLAGS += -fvisibility=hidden
+CFLAGS += -fno-unwind-tables
+CFLAGS += -fstack-reuse=all
+CFLAGS += -ffunction-sections
+CFLAGS += -fdata-sections
+
+# Linker Configuration #########################################################
+
+LD := $(PREFIX)ld
+
+LDFLAGS := -T $(NANOHUB_DIR)/os/platform/$(PLATFORM)/lkr/app.lkr
+LDFLAGS += -nostartfiles
+LDFLAGS += --gc-sections
+LDFLAGS += -Map $(OUT)/$(BIN).map
+LDFLAGS += --cref
+ifeq ($(BIN_MODE),static)
+LDFLAGS += -static
+LDFLAGS += --emit-relocs
+LDFLAGS += -L$(wildcard $(TOOLCHAIN_DIR)/lib/gcc/arm-none-eabi/*/armv7e-m/softfp)
+STATIC_LIBS += -lgcc
+else
+LDFLAGS += -shared
+LDFLAGS += --no-undefined
+LDFLAGS += --no-allow-shlib-undefined
+LDFLAGS += -L$(wildcard $(TOOLCHAIN_DIR)/lib/gcc/arm-none-eabi/*/armv7e-m/softfp)
+STATIC_LIBS += -lgcc
+endif
+
+# Build Rules ##################################################################
+
+AS_SRCS := $(filter %.S, $(SRCS))
+C_SRCS := $(filter %.c, $(SRCS))
+CXX_SRCS := $(filter %.cc, $(SRCS))
+
+OBJS := $(patsubst %.S, $(OUT)/%.o, $(AS_SRCS))
+OBJS += $(patsubst %.c, $(OUT)/%.o, $(C_SRCS))
+OBJS += $(patsubst %.cc, $(OUT)/%.o, $(CXX_SRCS))
+
+UNSIGNED_BIN := $(BIN).unsigned.napp
+
+NANOHUB_KEY_PATH := $(NANOHUB_DIR)/os/platform/$(PLATFORM)/misc
+
+
+.PHONY: all
+all: $(OUT)/$(BIN).S $(OUT)/$(BIN).napp
+
+$(OUT)/$(BIN).napp : $(OUT)/$(UNSIGNED_BIN) $(NANOAPP_SIGN)
+	@mkdir -p $(dir $@)
+	$(NANOAPP_SIGN) -e $(NANOHUB_KEY_PATH)/debug.privkey \
+		-m $(NANOHUB_KEY_PATH)/debug.pubkey -s $< $@
+
+ifeq ($(BIN_MODE),static)
+$(OUT)/$(UNSIGNED_BIN) : $(OUT)/$(BIN).elf $(NANOAPP_POSTPROCESS)
+	@mkdir -p $(dir $@)
+	$(NANOAPP_POSTPROCESS) -s -a $(APP_ID) -v $< $@
+else
+$(OUT)/$(UNSIGNED_BIN) : $(OUT)/$(BIN).bin $(NANOAPP_POSTPROCESS)
+	@mkdir -p $(dir $@)
+	$(NANOAPP_POSTPROCESS) -a $(APP_ID) -v $< $@
+
+$(OUT)/$(BIN).bin : $(OUT)/$(BIN).elf
+	@mkdir -p $(dir $@)
+	$(OBJCOPY) -j.relocs -j.flash -j.data -j.dynsym -O binary $< $@
+endif
+
+$(OUT)/$(BIN).S : $(OUT)/$(BIN).elf
+	@mkdir -p $(dir $@)
+	$(OBJDUMP) $< -DS > $@
+
+$(OUT)/$(BIN).elf : $(OBJS)
+	@mkdir -p $(dir $@)
+	$(LD) $(LDFLAGS) $(OBJS) $(STATIC_LIBS) -o $@
+
+$(OUT)/%.o : %.S
+	@mkdir -p $(dir $@)
+	$(AS) $(AS_FLAGS) $(CFLAGS) -c $< -o $@
+
+$(OUT)/%.o : %.c
+	@mkdir -p $(dir $@)
+	$(CC) $(C_CFLAGS) $(CFLAGS) -c $< -o $@
+
+$(OUT)/%.o : %.cc
+	@mkdir -p $(dir $@)
+	$(CXX) $(CXX_CFLAGS) $(CFLAGS) -c $< -o $@
+
+# Automatic dependency resolution ##############################################
+
+DEPS_AS = $(OUT)/deps_as
+DEPS_C = $(OUT)/deps_c
+DEPS_CXX = $(OUT)/deps_cxx
+
+$(DEPS_AS) : $(AS_SRCS)
+	@mkdir -p $(dir $@)
+	$(AS) $(AS_CFLAGS) $(CFLAGS) -MM $^ > $@
+
+$(DEPS_C) : $(C_SRCS)
+	@mkdir -p $(dir $@)
+	$(CC) $(C_CFLAGS) $(CFLAGS) -MM $^ > $@
+
+$(DEPS_CXX) : $(CXX_SRCS)
+	@mkdir -p $(dir $@)
+	$(CXX) $(CXX_CFLAGS) $(CFLAGS) -MM $^ > $@
+
+NOAUTODEPTARGETS = clean
+
+ifeq ($(words $(findstring $(MAKECMDGOALS), $(NOAUTODEPTARGETS))), 0)
+
+ifneq ($(AS_SRCS), )
+-include $(DEPS_AS)
+endif
+
+ifneq ($(C_SRCS), )
+-include $(DEPS_C)
+endif
+
+ifneq ($(CXX_SRCS), )
+-include $(DEPS_CXX)
+endif
+
+endif
+
+$(NANOAPP_POSTPROCESS): $(wildcard $(dir $(NANOAPP_POSTPROCESS))/*.c* $(dir $(NANOAPP_POSTPROCESS))/*.h)
+	echo DEPS [$@]: $^
+	make -C $(dir $@)
+
+$(NANOAPP_SIGN): $(wildcard $(dir $(NANOAPP_SIGN))/*.c* $(dir $(NANOAPP_SIGN))/*.h)
+	echo DEPS [$@]: $^
+	make -C $(dir $@)
+
+# Clean targets ################################################################
+
+.PHONY: clean
+clean :
+	rm -rf $(OUT)
diff --git a/firmware/app/common.mk b/firmware/app/common.mk
deleted file mode 100644
index 81a9624..0000000
--- a/firmware/app/common.mk
+++ /dev/null
@@ -1,38 +0,0 @@
-#
-# Copyright (C) 2016 The Android Open Source Project
-#
-# 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.
-#
-
-SELF_DIR := $(SELF_MKFILE:Makefile=)
-SELF_FILES := $(wildcard $(SELF_DIR)*.c)
-APP_NM := $(SELF_DIR)app
-CLEANFILES := $(CLEANFILES) $(APP_NM).elf  $(APP_NM).bin
-DELIVERABLES := $(DELIVERABLES) $(APP_NM).napp
-APP_ELF := $(APP_NM).elf
-APP_BIN := $(APP_NM).bin
-APP_APP := $(APP_NM).napp
-APPFLAGS += $(EXTRA_FLAGS) -Wall -Werror
-
-define APPRULE
-$(APP_APP): $(APP_BIN)
-	nanoapp_postprocess -v -a $(APP_ID) $(APP_BIN) $(APP_APP)
-
-$(APP_BIN): $(APP_ELF)
-	$(OBJCOPY) -j.relocs -j.flash -j.data -j.dynsym -O binary $(APP_ELF) $(APP_BIN)
-
-$(APP_ELF): $(SELF_FILES) symlinks
-	$(GCC) -o $(APP_ELF) $(FLAGS) $(APPFLAGS) -fvisibility=hidden $(SELF_FILES)
-endef
-
-$(eval $(APPRULE))
diff --git a/firmware/app/external.app/Makefile b/firmware/app/external.app/Makefile
deleted file mode 100644
index 7b3ae79..0000000
--- a/firmware/app/external.app/Makefile
+++ /dev/null
@@ -1,26 +0,0 @@
-#
-# Copyright (C) 2016 The Android Open Source Project
-#
-# 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.
-#
-
-#makefile for all extrernal appspop app
-
-EXTRA_FLAGS ?= 
-
-ifneq ($(EXTERNAL_APP_BUILD),)
- SELF_MKFILE := $(EXTERNAL_APP_MAKEFILE)
- APP_ID := $(EXTERNAL_APP_ID)
- include app/common.mk
-endif
-
diff --git a/firmware/app/test0.app/Makefile b/firmware/app/test0.app/Makefile
index 0303b64..b5931d1 100644
--- a/firmware/app/test0.app/Makefile
+++ b/firmware/app/test0.app/Makefile
@@ -14,10 +14,29 @@
 # limitations under the License.
 #
 
-#makefile for test0 app
+################################################################################
+#
+# test NanoApp Makefile
+#
+################################################################################
 
-SELF_MKFILE := $(abspath $(lastword $(MAKEFILE_LIST)))
-# Googl + T + 0x8000
+
+SRCS := test_app0.c
+BIN := test0
 APP_ID := 476f6f676c548000
+APP_VERSION := 0
 
-include app/common.mk
+# Nanohub relative path
+NANOHUB_DIR := ../..
+
+# Device configuration #########################################################
+
+# select device variant for this app
+VARIANT_PATH = device/google/contexthub/firmware/variant/nucleo
+VARIANT = nucleo
+
+# setup variant parameters
+# PLATFORM, CPU, CHIP must match variant Makefile (located under VARIANT_PATH);
+# if not defined there, there is not need to specify them here either
+
+include $(NANOHUB_DIR)/app/app.mk
diff --git a/firmware/app/test1.app/Makefile b/firmware/app/test1.app/Makefile
index 8cade3f..b21c4ba 100644
--- a/firmware/app/test1.app/Makefile
+++ b/firmware/app/test1.app/Makefile
@@ -14,10 +14,31 @@
 # limitations under the License.
 #
 
-#makefile for test1 app
+################################################################################
+#
+# test1 NanoApp Makefile
+#
+################################################################################
 
-SELF_MKFILE := $(abspath $(lastword $(MAKEFILE_LIST)))
-# Googl + T + 0x8001
+# Device configuration #########################################################
+
+SRCS := test_app1.c
+BIN := test1
+
 APP_ID := 476f6f676c548001
+APP_VERSION := 0
 
-include app/common.mk
+# Nanohub relative path
+NANOHUB_DIR := ../..
+
+# Device configuration #########################################################
+
+# select device variant for this app
+VARIANT_PATH = device/google/contexthub/firmware/variant/nucleo
+VARIANT = nucleo
+
+# setup variant parameters
+# PLATFORM, CPU, CHIP must match variant Makefile (located under VARIANT_PATH);
+# if not defined there, there is not need to specify them here either
+
+include $(NANOHUB_DIR)/app/app.mk
diff --git a/firmware/external/arm/arm_common_tables.c b/firmware/external/arm/arm_common_tables.c
index 7f1ac84..148d985 100644
--- a/firmware/external/arm/arm_common_tables.c
+++ b/firmware/external/arm/arm_common_tables.c
@@ -40,7 +40,7 @@
 
 
 #include <arm_math.h>
-#include <plat/inc/cmsis/arm_common_tables.h>
+#include <plat/cmsis/arm_common_tables.h>
 
 /**    
  * @ingroup groupTransforms    
diff --git a/firmware/external/freebsd/lib/msun/src/e_atan2f.c b/firmware/external/freebsd/lib/msun/src/e_atan2f.c
index fc77bff..9088323 100644
--- a/firmware/external/freebsd/lib/msun/src/e_atan2f.c
+++ b/firmware/external/freebsd/lib/msun/src/e_atan2f.c
@@ -17,7 +17,7 @@
 __FBSDID("$FreeBSD$");
 
 #include "math.h"
-#include "math_private.h"
+#include <math_private.h>
 
 static volatile float
 tiny  = 1.0e-30;
diff --git a/firmware/external/freebsd/lib/msun/src/e_expf.c b/firmware/external/freebsd/lib/msun/src/e_expf.c
index b1fe2c5..19931da 100644
--- a/firmware/external/freebsd/lib/msun/src/e_expf.c
+++ b/firmware/external/freebsd/lib/msun/src/e_expf.c
@@ -19,7 +19,7 @@
 #include <float.h>
 
 #include "math.h"
-#include "math_private.h"
+#include <math_private.h>
 
 static const float
 one	= 1.0,
diff --git a/firmware/external/freebsd/lib/msun/src/s_atanf.c b/firmware/external/freebsd/lib/msun/src/s_atanf.c
index b3a371f..965fea5 100644
--- a/firmware/external/freebsd/lib/msun/src/s_atanf.c
+++ b/firmware/external/freebsd/lib/msun/src/s_atanf.c
@@ -17,7 +17,7 @@
 __FBSDID("$FreeBSD$");
 
 #include "math.h"
-#include "math_private.h"
+#include <math_private.h>
 
 static const float atanhi[] = {
   4.6364760399e-01, /* atan(0.5)hi 0x3eed6338 */
diff --git a/firmware/firmware.mk b/firmware/firmware.mk
new file mode 100644
index 0000000..fbb13a2
--- /dev/null
+++ b/firmware/firmware.mk
@@ -0,0 +1,108 @@
+#
+# Copyright (C) 2016 The Android Open Source Project
+#
+# 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.
+#
+
+#find build target
+PLATFORM ?= stm32
+CHIP ?= stm32f411
+CPU ?= cortexm4
+VARIANT ?= lunchbox
+DEBUG ?= -DDEBUG
+OUT := out/nanohub/$(VARIANT)
+
+#bad words
+BADWORDS += strcpy strcat atoi
+BADWORDS += "rsaPrivOp=RSA private ops must never be compiled into firmware."
+
+#find makefiles
+MAKE_PLAT = os/platform/$(PLATFORM)/$(PLATFORM).mk
+MAKE_CPU = os/cpu/$(CPU)/$(CPU).mk
+
+ifndef VARIANT_PATH
+VARIANT_PATH := variant/$(VARIANT)
+endif
+
+MAKE_VAR = $(VARIANT_PATH)/$(VARIANT).mk
+
+#top make target
+SRCS_os :=
+SRCS_bl :=
+DELIVERABLES :=
+
+.PHONY: real all
+real: all
+
+#include makefiles for plat and cpu
+include $(MAKE_PLAT)
+include $(MAKE_CPU)
+include $(MAKE_VAR)
+
+FLAGS += -Ios/inc
+FLAGS += -Ios/platform/$(PLATFORM)/inc
+FLAGS += -Ios/platform/$(PLATFORM)/inc/plat/cmsis
+FLAGS += -Ios/cpu/$(CPU)/inc
+FLAGS += -I$(VARIANT_PATH)/inc
+FLAGS += -Iexternal/freebsd/inc
+FLAGS += -I../lib/include
+
+FLAGS += -Wall -Werror -fshort-double
+#help avoid commmon embedded C mistakes
+FLAGS += -Wmissing-declarations -Wlogical-op -Waddress -Wempty-body -Wpointer-arith -Wenum-compare -Wdouble-promotion -Wfloat-equal -Wshadow -fno-strict-aliasing
+
+OSFLAGS += -g -ggdb3 -D_OS_BUILD_ -O2
+
+#debug mode
+FLAGS += $(DEBUG)
+
+#bootloader pieces
+SRCS_bl += ../lib/nanohub/sha2.c ../lib/nanohub/rsa.c ../lib/nanohub/aes.c os/core/seos.c
+
+#frameworks
+SRCS_os += os/core/printf.c os/core/timer.c os/core/seos.c os/core/heap.c os/core/slab.c os/core/spi.c os/core/trylock.c
+SRCS_os += os/core/hostIntf.c os/core/hostIntfI2c.c os/core/hostIntfSpi.c os/core/nanohubCommand.c os/core/sensors.c os/core/syscall.c
+SRCS_os += os/core/eventQ.c os/core/osApi.c os/core/appSec.c os/core/simpleQ.c os/core/floatRt.c
+
+#some help for bootloader
+SRCS_bl += os/core/printf.c
+
+ifndef PLATFORM_HAS_HARDWARE_CRC
+SRCS_os += ../lib/nanohub/softcrc.c
+endif
+
+#extra deps
+DEPS += $(wildcard inc/*.h)
+DEPS += firmware.mk $(MAKE_PLAT) $(MAKE_CPU) $(MAKE_VAR)
+DELIVERABLES += $(OUT)/full.bin
+
+all: $(DELIVERABLES)
+
+$(OUT)/bl.unchecked.elf: $(SRCS_bl) $(DEPS)
+	mkdir -p $(dir $@)
+	$(GCC) -o $@ $(SRCS_bl) $(OSFLAGS) $(OSFLAGS_bl) $(FLAGS)
+
+$(OUT)/os.unchecked.elf: $(SRCS_os) $(DEPS)
+	mkdir -p $(dir $@)
+	$(GCC) -o $@ $(SRCS_os) $(OSFLAGS) $(OSFLAGS_os) $(FLAGS)
+
+$(OUT)/%.checked.elf : $(OUT)/%.unchecked.elf  symcheck.sh
+	mkdir -p $(dir $@)
+	./symcheck.sh $< $@ $(BADWORDS)
+
+$(OUT)/full.bin: $(BL_FILE) $(OS_FILE)
+	mkdir -p $(dir $@)
+	cat $(BL_FILE) $(OS_FILE) > $@
+
+clean:
+	rm -rf $(OUT)
diff --git a/firmware/lib/lib.mk b/firmware/lib/lib.mk
new file mode 100644
index 0000000..d8c6b70
--- /dev/null
+++ b/firmware/lib/lib.mk
@@ -0,0 +1,43 @@
+################################################################################
+#
+# Nanoapp Libc/Libm build helper script
+#
+################################################################################
+
+# Libm #########################################################################
+
+LIBM_PATH := $(NANOHUB_DIR)/lib/libm
+
+CFLAGS += -D_IEEE_LIBM
+
+SRCS += $(LIBM_PATH)/ef_atan2.c
+SRCS += $(LIBM_PATH)/ef_asin.c
+SRCS += $(LIBM_PATH)/ef_fmod.c
+SRCS += $(LIBM_PATH)/ef_rem_pio2.c
+SRCS += $(LIBM_PATH)/ef_sqrt.c
+SRCS += $(LIBM_PATH)/kf_cos.c
+SRCS += $(LIBM_PATH)/kf_rem_pio2.c
+SRCS += $(LIBM_PATH)/kf_sin.c
+SRCS += $(LIBM_PATH)/sf_atan.c
+SRCS += $(LIBM_PATH)/sf_cos.c
+SRCS += $(LIBM_PATH)/sf_floor.c
+SRCS += $(LIBM_PATH)/sf_fpclassify.c
+SRCS += $(LIBM_PATH)/sf_round.c
+SRCS += $(LIBM_PATH)/sf_scalbn.c
+SRCS += $(LIBM_PATH)/sf_sin.c
+SRCS += $(LIBM_PATH)/wf_atan2.c
+SRCS += $(LIBM_PATH)/wf_asin.c
+SRCS += $(LIBM_PATH)/wf_fmod.c
+
+# Libc #########################################################################
+
+LIBC_PATH := $(NANOHUB_DIR)/lib/libc
+
+SRCS += $(LIBC_PATH)/bcopy.c
+SRCS += $(LIBC_PATH)/memcmp.c
+#SRCS += $(LIBC_PATH)/memcpy-armv7m.S
+SRCS += $(LIBC_PATH)/memcpy.c
+SRCS += $(LIBC_PATH)/memmove.c
+SRCS += $(LIBC_PATH)/memset.c
+SRCS += $(LIBC_PATH)/strcasecmp.c
+SRCS += $(LIBC_PATH)/strlen.c
diff --git a/lib/libc/acle-compat.h b/firmware/lib/libc/acle-compat.h
similarity index 100%
rename from lib/libc/acle-compat.h
rename to firmware/lib/libc/acle-compat.h
diff --git a/lib/libc/arm_asm.h b/firmware/lib/libc/arm_asm.h
similarity index 100%
rename from lib/libc/arm_asm.h
rename to firmware/lib/libc/arm_asm.h
diff --git a/lib/libc/bcopy.c b/firmware/lib/libc/bcopy.c
similarity index 100%
rename from lib/libc/bcopy.c
rename to firmware/lib/libc/bcopy.c
diff --git a/lib/libc/memcmp.c b/firmware/lib/libc/memcmp.c
similarity index 100%
rename from lib/libc/memcmp.c
rename to firmware/lib/libc/memcmp.c
diff --git a/lib/libc/memcpy-armv7m.S b/firmware/lib/libc/memcpy-armv7m.S
similarity index 100%
rename from lib/libc/memcpy-armv7m.S
rename to firmware/lib/libc/memcpy-armv7m.S
diff --git a/lib/libc/memcpy.c b/firmware/lib/libc/memcpy.c
similarity index 100%
rename from lib/libc/memcpy.c
rename to firmware/lib/libc/memcpy.c
diff --git a/lib/libc/memmove.c b/firmware/lib/libc/memmove.c
similarity index 100%
rename from lib/libc/memmove.c
rename to firmware/lib/libc/memmove.c
diff --git a/lib/libc/memset.c b/firmware/lib/libc/memset.c
similarity index 100%
rename from lib/libc/memset.c
rename to firmware/lib/libc/memset.c
diff --git a/lib/libc/strcasecmp.c b/firmware/lib/libc/strcasecmp.c
similarity index 100%
rename from lib/libc/strcasecmp.c
rename to firmware/lib/libc/strcasecmp.c
diff --git a/lib/libc/strlen.c b/firmware/lib/libc/strlen.c
similarity index 98%
rename from lib/libc/strlen.c
rename to firmware/lib/libc/strlen.c
index 7e59e75..bf56ed6 100644
--- a/lib/libc/strlen.c
+++ b/firmware/lib/libc/strlen.c
@@ -84,6 +84,7 @@
 size_t __attribute__((naked))
 strlen (const char* str)
 {
+  (void)str; /* disable unused argument warning */
   asm ("len .req r0\n\t"
        "data .req r3\n\t"
        "addr .req r1\n\t"
diff --git a/lib/libc/strncpy.c b/firmware/lib/libc/strncpy.c
similarity index 100%
rename from lib/libc/strncpy.c
rename to firmware/lib/libc/strncpy.c
diff --git a/lib/libm/ef_asin.c b/firmware/lib/libm/ef_asin.c
similarity index 100%
rename from lib/libm/ef_asin.c
rename to firmware/lib/libm/ef_asin.c
diff --git a/lib/libm/ef_atan2.c b/firmware/lib/libm/ef_atan2.c
similarity index 100%
rename from lib/libm/ef_atan2.c
rename to firmware/lib/libm/ef_atan2.c
diff --git a/lib/libm/ef_fmod.c b/firmware/lib/libm/ef_fmod.c
similarity index 100%
rename from lib/libm/ef_fmod.c
rename to firmware/lib/libm/ef_fmod.c
diff --git a/lib/libm/ef_rem_pio2.c b/firmware/lib/libm/ef_rem_pio2.c
similarity index 100%
rename from lib/libm/ef_rem_pio2.c
rename to firmware/lib/libm/ef_rem_pio2.c
diff --git a/lib/libm/ef_sqrt.c b/firmware/lib/libm/ef_sqrt.c
similarity index 100%
rename from lib/libm/ef_sqrt.c
rename to firmware/lib/libm/ef_sqrt.c
diff --git a/lib/libm/fdlibm.h b/firmware/lib/libm/fdlibm.h
similarity index 100%
rename from lib/libm/fdlibm.h
rename to firmware/lib/libm/fdlibm.h
diff --git a/lib/libm/kf_cos.c b/firmware/lib/libm/kf_cos.c
similarity index 100%
rename from lib/libm/kf_cos.c
rename to firmware/lib/libm/kf_cos.c
diff --git a/lib/libm/kf_rem_pio2.c b/firmware/lib/libm/kf_rem_pio2.c
similarity index 100%
rename from lib/libm/kf_rem_pio2.c
rename to firmware/lib/libm/kf_rem_pio2.c
diff --git a/lib/libm/kf_sin.c b/firmware/lib/libm/kf_sin.c
similarity index 100%
rename from lib/libm/kf_sin.c
rename to firmware/lib/libm/kf_sin.c
diff --git a/lib/libm/sf_atan.c b/firmware/lib/libm/sf_atan.c
similarity index 100%
rename from lib/libm/sf_atan.c
rename to firmware/lib/libm/sf_atan.c
diff --git a/lib/libm/sf_cos.c b/firmware/lib/libm/sf_cos.c
similarity index 100%
rename from lib/libm/sf_cos.c
rename to firmware/lib/libm/sf_cos.c
diff --git a/lib/libm/sf_floor.c b/firmware/lib/libm/sf_floor.c
similarity index 100%
rename from lib/libm/sf_floor.c
rename to firmware/lib/libm/sf_floor.c
diff --git a/lib/libm/sf_fpclassify.c b/firmware/lib/libm/sf_fpclassify.c
similarity index 100%
rename from lib/libm/sf_fpclassify.c
rename to firmware/lib/libm/sf_fpclassify.c
diff --git a/lib/libm/sf_round.c b/firmware/lib/libm/sf_round.c
similarity index 100%
rename from lib/libm/sf_round.c
rename to firmware/lib/libm/sf_round.c
diff --git a/lib/libm/sf_scalbn.c b/firmware/lib/libm/sf_scalbn.c
similarity index 100%
rename from lib/libm/sf_scalbn.c
rename to firmware/lib/libm/sf_scalbn.c
diff --git a/lib/libm/sf_sin.c b/firmware/lib/libm/sf_sin.c
similarity index 100%
rename from lib/libm/sf_sin.c
rename to firmware/lib/libm/sf_sin.c
diff --git a/lib/libm/wf_asin.c b/firmware/lib/libm/wf_asin.c
similarity index 100%
rename from lib/libm/wf_asin.c
rename to firmware/lib/libm/wf_asin.c
diff --git a/lib/libm/wf_atan2.c b/firmware/lib/libm/wf_atan2.c
similarity index 100%
rename from lib/libm/wf_atan2.c
rename to firmware/lib/libm/wf_atan2.c
diff --git a/lib/libm/wf_fmod.c b/firmware/lib/libm/wf_fmod.c
similarity index 100%
rename from lib/libm/wf_fmod.c
rename to firmware/lib/libm/wf_fmod.c
diff --git a/firmware/misc/platform/stm32f4xx/Makefile b/firmware/misc/platform/stm32f4xx/Makefile
deleted file mode 100644
index c21fd8f..0000000
--- a/firmware/misc/platform/stm32f4xx/Makefile
+++ /dev/null
@@ -1,80 +0,0 @@
-#
-# Copyright (C) 2016 The Android Open Source Project
-#
-# 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.
-#
-
-M3DEBUG ?= m3debug
-
-ifneq ($(CPU),cortexm4f)
-        $(error "stm32f4xx cplatform only supports Cortex-M4F CPUs")
-endif
-
-DELIVERABLES = os.checked.bin bl.unchecked.bin full.bin showsizes
-BL_FILE = bl.unchecked.bin
-OS_FILE = os.checked.bin
-
-FLAGS += -I. -fno-unwind-tables -fstack-reuse=all -ffunction-sections -fdata-sections
-FLAGS += -Wl,--gc-sections
-FLAGS += -Iinc/platform/$(PLATFORM)/cmsis
-
-APPFLAGS += -msingle-pic-base -mpic-register=r9 -nostartfiles -fpic -shared
-
-#platform bootloader
-SRCS_bl += src/platform/$(PLATFORM)/bl.c
-
-#platform runtime
-SRCS_os +=  src/platform/$(PLATFORM)/crt_$(PLATFORM).c
-
-#platform drivers
-SRCS_os += src/platform/$(PLATFORM)/platform.c \
-	src/platform/$(PLATFORM)/usart.c \
-	src/platform/$(PLATFORM)/gpio.c \
-	src/platform/$(PLATFORM)/pwr.c \
-	src/platform/$(PLATFORM)/wdt.c \
-	src/platform/$(PLATFORM)/i2c.c \
-	src/platform/$(PLATFORM)/exti.c \
-	src/platform/$(PLATFORM)/syscfg.c \
-	src/platform/$(PLATFORM)/spi.c \
-	src/platform/$(PLATFORM)/rtc.c \
-	src/platform/$(PLATFORM)/mpu.c \
-	src/platform/$(PLATFORM)/dma.c \
-	src/platform/$(PLATFORM)/crc.c \
-	src/platform/$(PLATFORM)/hostIntf.c \
-	src/platform/$(PLATFORM)/apInt.c \
-	src/platform/$(PLATFORM)/eeData.c
-
-
-#platform drivers for bootloader
-SRCS_bl += src/platform/$(PLATFORM)/pwr.c src/platform/$(PLATFORM)/gpio.c
-
-#extra deps
-DEPS += $(wildcard inc/platform/$(PLATFORM)/*.h)
-DEPS += $(wildcard inc/platform/$(PLATFORM)/cmsis/*.h)
-
-#platform flags
-PLATFORM_HAS_HARDWARE_CRC = true
-FLAGS += -DPLATFORM_HW_VER=0
-
-#platform-specific rules
-OBJCOPY_PARAMS = -I elf32-littlearm -O binary
-
-bl.%.bin : bl.%.elf
-	$(OBJCOPY) -j .bl -j .blrodata -j .eedata $(OBJCOPY_PARAMS) $< $@
-
-os.%.bin : os.%.elf
-	$(OBJCOPY) -j .data -j .text $(OBJCOPY_PARAMS) $< $@
-
-showsizes: os.unchecked.elf
-	misc/platform/$(PLATFORM)/showsizes.sh $<
-
diff --git a/firmware/misc/variant/common/stm32f401.bl.lkr b/firmware/misc/variant/common/stm32f401.bl.lkr
deleted file mode 100644
index bb187ea..0000000
--- a/firmware/misc/variant/common/stm32f401.bl.lkr
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * 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 misc/variant/common/stm32f401.map.lkr
-INCLUDE misc/variant/common/stm32f4xx.bl.lkr
-INCLUDE misc/variant/common/stm32f4xx.common.lkr
diff --git a/firmware/misc/variant/common/stm32f401.os.lkr b/firmware/misc/variant/common/stm32f401.os.lkr
deleted file mode 100644
index b842ce9..0000000
--- a/firmware/misc/variant/common/stm32f401.os.lkr
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * 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 misc/variant/common/stm32f401.map.lkr
-INCLUDE misc/variant/common/stm32f4xx.os.lkr
-INCLUDE misc/variant/common/stm32f4xx.common.lkr
diff --git a/firmware/misc/variant/common/stm32f411.bl.lkr b/firmware/misc/variant/common/stm32f411.bl.lkr
deleted file mode 100644
index 0bb4e40..0000000
--- a/firmware/misc/variant/common/stm32f411.bl.lkr
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * 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 misc/variant/common/stm32f411.map.lkr
-INCLUDE misc/variant/common/stm32f4xx.bl.lkr
-INCLUDE misc/variant/common/stm32f4xx.common.lkr
diff --git a/firmware/misc/variant/lunchbox/Makefile b/firmware/misc/variant/lunchbox/Makefile
deleted file mode 100644
index db18758..0000000
--- a/firmware/misc/variant/lunchbox/Makefile
+++ /dev/null
@@ -1,72 +0,0 @@
-#
-# Copyright (C) 2016 The Android Open Source Project
-#
-# 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.
-#
-
-#variant makefile for lunchbox
-
-ifneq ($(PLATFORM),stm32f4xx)
-        $(error "LUNCHBOX variant cannot be build on a platform that is not stm32f411")
-endif
-
-FLAGS += -DPLATFORM_HW_TYPE=0x4C75   #'Lu' -> lunchbox
-FLAGS += -DDEBUG_UART_PIN=16         #GPIOB0 is debug uart at 2MBps
-
-
-#board configuration shims
-SRCS_os += links/variant/src/i2c.c
-SRCS_os += links/variant/src/spi.c
-
-#keys
-
-#drivers
-# ROHM ALS/prox
-SRCS_os += src/drivers/rohm_rpr0521/rohm_rpr0521.c
-
-# Fusion algorithms
-SRCS_os += src/algos/fusion.c src/algos/mat.c src/algos/quat.c src/algos/vec.c
-
-# BMI160 accel and gyro, BMM150 mag drivers
-FLAGS += -DUSE_BMM150 -DMAG_SLAVE_PRESENT
-SRCS_os += src/drivers/bosch_bmi160/bosch_bmi160.c \
-	src/drivers/bosch_bmi160/bosch_bmm150_slave.c \
-	src/algos/mag_cal.c \
-	src/algos/time_sync.c
-
-# Orientation sensor driver
-SRCS_os += src/drivers/orientation/orientation.c
-
-# Window orientation sensor driver
-SRCS_os += src/drivers/window_orientation/window_orientation.c
-
-# Bosch BMP280 Barometer/Temperature
-SRCS_os += src/drivers/bosch_bmp280/bosch_bmp280.c
-
-# Hall effect sensor driver
-SRCS_os += src/drivers/hall/hall.c
-
-# Camera Vsync driver
-SRCS_os += src/drivers/vsync/vsync.c
-
-# Tilt detection
-SRCS_os += src/drivers/tilt_detection/tilt_detection.c
-
-DEPS += $(wildcard links/variant/inc/*.h)
-
-#linker script
-LKR_os = misc/variant/common/stm32f411.os.lkr
-LKR_bl = misc/variant/common/stm32f411.bl.lkr
-OSFLAGS_os += -Wl,-T $(LKR_os)
-OSFLAGS_bl += -Wl,-T $(LKR_bl)
-DEPS += $(LKR_os) $(LKR_bl)
diff --git a/firmware/misc/variant/nucleo/Makefile b/firmware/misc/variant/nucleo/Makefile
deleted file mode 100644
index 12ba238..0000000
--- a/firmware/misc/variant/nucleo/Makefile
+++ /dev/null
@@ -1,74 +0,0 @@
-#
-# Copyright (C) 2016 The Android Open Source Project
-#
-# 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.
-#
-
-#variant makefile for nucleo
-
-
-ifneq ($(PLATFORM),stm32f4xx)
-        $(error "NUCLEO variant cannot be build on a platform that is not stm32f411")
-endif
-
-FLAGS += -DPLATFORM_HW_TYPE=0x4E75   #'Nu' ->nucleo
-FLAGS += -DDEBUG_SWD
-
-#board configuration shims
-SRCS_os += src/variant/$(VARIANT)/i2c.c
-SRCS_os += src/variant/$(VARIANT)/spi.c
-
-#keys
-
-#drivers
-# AMS ALS/prox
-SRCS_os += src/drivers/ams_tmd2772/ams_tmd2772.c
-
-# ROHM ALS/prox
-SRCS_os += src/drivers/rohm_rpr0521/rohm_rpr0521.c
-
-# Fusion algorithm
-SRCS_os += src/algos/fusion.c src/algos/mat.c src/algos/quat.c src/algos/vec.c
-
-# BMI160 accel and gyro, BMM150 mag drivers
-FLAGS += -DUSE_BMM150
-SRCS_os += src/drivers/bosch_bmi160/bosch_bmi160.c \
-	src/drivers/bosch_bmi160/bosch_bmm150_slave.c \
-	src/algos/mag_cal.c \
-	src/algos/time_sync.c
-
-# Orientation sensor driver
-SRCS_os += src/drivers/orientation/orientation.c
-
-# Window orientation sensor driver
-SRCS_os += src/drivers/window_orientation/window_orientation.c
-
-# Bosch BMP280 Barometer/Temperature
-SRCS_os += src/drivers/bosch_bmp280/bosch_bmp280.c
-
-# Hall effect sensor driver
-SRCS_os += src/drivers/hall/hall.c
-
-# Camera Vsync driver
-SRCS_os += src/drivers/vsync/vsync.c
-
-# Tilt detection
-SRCS_os += src/drivers/tilt_detection/tilt_detection.c
-
-#linker script
-LKR_os = misc/variant/common/stm32f411.os.lkr
-LKR_bl = misc/variant/common/stm32f411.bl.lkr
-OSFLAGS_os += -Wl,-T $(LKR_os)
-OSFLAGS_bl += -Wl,-T $(LKR_bl)
-DEPS += $(LKR_os) $(LKR_bl)
-DEPS += $(LKR)
diff --git a/firmware/src/algos/accel_cal.c b/firmware/os/algos/accel_cal.c
similarity index 100%
rename from firmware/src/algos/accel_cal.c
rename to firmware/os/algos/accel_cal.c
diff --git a/firmware/src/algos/fusion.c b/firmware/os/algos/fusion.c
similarity index 100%
rename from firmware/src/algos/fusion.c
rename to firmware/os/algos/fusion.c
diff --git a/firmware/src/algos/gyro_cal.c b/firmware/os/algos/gyro_cal.c
similarity index 100%
rename from firmware/src/algos/gyro_cal.c
rename to firmware/os/algos/gyro_cal.c
diff --git a/firmware/src/algos/gyro_stillness_detect.c b/firmware/os/algos/gyro_stillness_detect.c
similarity index 100%
rename from firmware/src/algos/gyro_stillness_detect.c
rename to firmware/os/algos/gyro_stillness_detect.c
diff --git a/firmware/src/algos/mag_cal.c b/firmware/os/algos/mag_cal.c
similarity index 100%
rename from firmware/src/algos/mag_cal.c
rename to firmware/os/algos/mag_cal.c
diff --git a/firmware/src/algos/mat.c b/firmware/os/algos/mat.c
similarity index 100%
rename from firmware/src/algos/mat.c
rename to firmware/os/algos/mat.c
diff --git a/firmware/src/algos/quat.c b/firmware/os/algos/quat.c
similarity index 100%
rename from firmware/src/algos/quat.c
rename to firmware/os/algos/quat.c
diff --git a/firmware/src/algos/time_sync.c b/firmware/os/algos/time_sync.c
similarity index 100%
rename from firmware/src/algos/time_sync.c
rename to firmware/os/algos/time_sync.c
diff --git a/firmware/src/algos/vec.c b/firmware/os/algos/vec.c
similarity index 100%
rename from firmware/src/algos/vec.c
rename to firmware/os/algos/vec.c
diff --git a/firmware/src/appSec.c b/firmware/os/core/appSec.c
similarity index 99%
rename from firmware/src/appSec.c
rename to firmware/os/core/appSec.c
index de7421a..6d4aec8 100644
--- a/firmware/src/appSec.c
+++ b/firmware/os/core/appSec.c
@@ -16,7 +16,7 @@
 
 #include <stdint.h>
 
-#include <plat/inc/bl.h>
+#include <plat/bl.h>
 
 #include <nanohub/sha2.h>
 #include <nanohub/rsa.h>
diff --git a/firmware/src/eventQ.c b/firmware/os/core/eventQ.c
similarity index 98%
rename from firmware/src/eventQ.c
rename to firmware/os/core/eventQ.c
index 6c77b09..60a8c4f 100644
--- a/firmware/src/eventQ.c
+++ b/firmware/os/core/eventQ.c
@@ -23,8 +23,8 @@
 #include <slab.h>
 #include <cpu.h>
 #include <util.h>
-#include <plat/inc/plat.h>
-#include <plat/inc/taggedPtr.h>
+#include <plat/plat.h>
+#include <plat/taggedPtr.h>
 
 
 struct EvtRecord {
diff --git a/firmware/src/floatRt.c b/firmware/os/core/floatRt.c
similarity index 100%
rename from firmware/src/floatRt.c
rename to firmware/os/core/floatRt.c
diff --git a/firmware/src/heap.c b/firmware/os/core/heap.c
similarity index 100%
rename from firmware/src/heap.c
rename to firmware/os/core/heap.c
diff --git a/firmware/src/hostIntf.c b/firmware/os/core/hostIntf.c
similarity index 99%
rename from firmware/src/hostIntf.c
rename to firmware/os/core/hostIntf.c
index f08f1a9..35b6d98 100644
--- a/firmware/src/hostIntf.c
+++ b/firmware/os/core/hostIntf.c
@@ -20,10 +20,10 @@
 #include <string.h>
 #include <alloca.h>
 
-#include <variant/inc/variant.h>
+#include <variant/variant.h>
 #include <eventnums.h>
 
-#include <plat/inc/pwr.h>
+#include <plat/pwr.h>
 
 #include <nanohub/crc.h>
 
diff --git a/firmware/src/hostIntfI2c.c b/firmware/os/core/hostIntfI2c.c
similarity index 100%
rename from firmware/src/hostIntfI2c.c
rename to firmware/os/core/hostIntfI2c.c
diff --git a/firmware/src/hostIntfSpi.c b/firmware/os/core/hostIntfSpi.c
similarity index 100%
rename from firmware/src/hostIntfSpi.c
rename to firmware/os/core/hostIntfSpi.c
diff --git a/firmware/src/nanohubCommand.c b/firmware/os/core/nanohubCommand.c
similarity index 99%
rename from firmware/src/nanohubCommand.c
rename to firmware/os/core/nanohubCommand.c
index e70477d..79387d6 100644
--- a/firmware/src/nanohubCommand.c
+++ b/firmware/os/core/nanohubCommand.c
@@ -19,12 +19,12 @@
 #include <stdint.h>
 #include <sys/endian.h>
 
-#include <variant/inc/variant.h>
+#include <variant/variant.h>
 #include <eventnums.h>
 
-#include <plat/inc/taggedPtr.h>
-#include <plat/inc/bl.h>
-#include <plat/inc/plat.h>
+#include <plat/taggedPtr.h>
+#include <plat/bl.h>
+#include <plat/plat.h>
 
 #include <nanohub/crc.h>
 #include <nanohub/rsa.h>
diff --git a/firmware/src/osApi.c b/firmware/os/core/osApi.c
similarity index 99%
rename from firmware/src/osApi.c
rename to firmware/os/core/osApi.c
index 1e1ec7b..77ffe7a 100644
--- a/firmware/src/osApi.c
+++ b/firmware/os/core/osApi.c
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#include <plat/inc/taggedPtr.h>
-#include <plat/inc/rtc.h>
+#include <plat/taggedPtr.h>
+#include <plat/rtc.h>
 #include <syscall.h>
 #include <sensors.h>
 #include <errno.h>
diff --git a/firmware/src/printf.c b/firmware/os/core/printf.c
similarity index 99%
rename from firmware/src/printf.c
rename to firmware/os/core/printf.c
index 6f3d94a..9c10dc3 100644
--- a/firmware/src/printf.c
+++ b/firmware/os/core/printf.c
@@ -16,7 +16,7 @@
 
 #include <stdio.h>
 #include <printf.h>
-#include <cpu/inc/cpuMath.h>
+#include <cpu/cpuMath.h>
 
 
 static uint32_t StrPrvPrintfEx_number(printf_write_c putc_, void* userData, uint64_t number, bool base10, bool zeroExtend, bool isSigned, uint32_t padToLength, bool caps, bool* bail)
diff --git a/firmware/src/sensors.c b/firmware/os/core/sensors.c
similarity index 99%
rename from firmware/src/sensors.c
rename to firmware/os/core/sensors.c
index b5f6cc6..4658351 100644
--- a/firmware/src/sensors.c
+++ b/firmware/os/core/sensors.c
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include <plat/inc/taggedPtr.h>
-#include <plat/inc/rtc.h>
-#include <cpu/inc/barrier.h>
+#include <plat/taggedPtr.h>
+#include <plat/rtc.h>
+#include <cpu/barrier.h>
 #include <atomicBitset.h>
 #include <inttypes.h>
 #include <sensors.h>
diff --git a/firmware/src/seos.c b/firmware/os/core/seos.c
similarity index 99%
rename from firmware/src/seos.c
rename to firmware/os/core/seos.c
index 6d36337..e1570f8 100644
--- a/firmware/src/seos.c
+++ b/firmware/os/core/seos.c
@@ -14,11 +14,12 @@
  * limitations under the License.
  */
 
-#include <plat/inc/eeData.h>
-#include <plat/inc/plat.h>
-#include <plat/inc/bl.h>
-#include <plat/inc/wdt.h>
+#include <plat/eeData.h>
+#include <plat/plat.h>
+#include <plat/bl.h>
+#include <plat/wdt.h>
 #include <platform.h>
+#include <crc.h>
 #include <hostIntf.h>
 #include <inttypes.h>
 #include <syscall.h>
@@ -41,7 +42,6 @@
 #include <atomic.h>
 
 #include <nanohub/nanohub.h>
-#include <nanohub/crc.h>
 
 #define NO_NODE (TaskIndex)(-1)
 #define for_each_task(listHead, task) for (task = osTaskByIdx((listHead)->next); task; task = osTaskByIdx(task->list.next))
diff --git a/firmware/src/simpleQ.c b/firmware/os/core/simpleQ.c
similarity index 100%
rename from firmware/src/simpleQ.c
rename to firmware/os/core/simpleQ.c
diff --git a/firmware/src/slab.c b/firmware/os/core/slab.c
similarity index 100%
rename from firmware/src/slab.c
rename to firmware/os/core/slab.c
diff --git a/firmware/src/spi.c b/firmware/os/core/spi.c
similarity index 100%
rename from firmware/src/spi.c
rename to firmware/os/core/spi.c
diff --git a/firmware/src/syscall.c b/firmware/os/core/syscall.c
similarity index 100%
rename from firmware/src/syscall.c
rename to firmware/os/core/syscall.c
diff --git a/firmware/src/timer.c b/firmware/os/core/timer.c
similarity index 98%
rename from firmware/src/timer.c
rename to firmware/os/core/timer.c
index e64c557..df9c212 100644
--- a/firmware/src/timer.c
+++ b/firmware/os/core/timer.c
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#include <cpu/inc/atomicBitset.h>
-#include <plat/inc/rtc.h>
+#include <cpu/atomicBitset.h>
+#include <plat/rtc.h>
 #include <atomicBitset.h>
 #include <platform.h>
 #include <atomic.h>
diff --git a/firmware/src/trylock.c b/firmware/os/core/trylock.c
similarity index 100%
rename from firmware/src/trylock.c
rename to firmware/os/core/trylock.c
diff --git a/firmware/src/cpu/cortexm4f/appSupport.c b/firmware/os/cpu/cortexm4/appSupport.c
similarity index 100%
rename from firmware/src/cpu/cortexm4f/appSupport.c
rename to firmware/os/cpu/cortexm4/appSupport.c
diff --git a/firmware/src/cpu/cortexm4f/atomic.c b/firmware/os/cpu/cortexm4/atomic.c
similarity index 100%
rename from firmware/src/cpu/cortexm4f/atomic.c
rename to firmware/os/cpu/cortexm4/atomic.c
diff --git a/firmware/src/cpu/cortexm4f/atomicBitset.c b/firmware/os/cpu/cortexm4/atomicBitset.c
similarity index 100%
rename from firmware/src/cpu/cortexm4f/atomicBitset.c
rename to firmware/os/cpu/cortexm4/atomicBitset.c
diff --git a/firmware/misc/cpu/cortexm4f/Makefile b/firmware/os/cpu/cortexm4/cortexm4.mk
similarity index 69%
rename from firmware/misc/cpu/cortexm4f/Makefile
rename to firmware/os/cpu/cortexm4/cortexm4.mk
index e81465c..8e3b78b 100644
--- a/firmware/misc/cpu/cortexm4f/Makefile
+++ b/firmware/os/cpu/cortexm4/cortexm4.mk
@@ -18,33 +18,52 @@
 OBJCOPY = $(CROSS_COMPILE)objcopy
 
 FLAGS += -mthumb -mcpu=cortex-m4 -march=armv7e-m -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -mno-thumb-interwork -ffast-math -fsingle-precision-constant -DARM -DUSE_NANOHUB_FLOAT_RUNTIME
-APPFLAGS += -Wl,-T misc/cpu/$(CPU)/app.lkr -mno-pic-data-is-text-relative
+
+LIB_PATH := lib
 
 #defines
 FLAGS += -DCPU_NUM_PERSISTENT_RAM_BITS=32
 
 #cpu runtime
-SRCS_os += src/cpu/$(CPU)/atomicBitset.c src/cpu/$(CPU)/cpu.c src/cpu/$(CPU)/pendsv.c src/cpu/$(CPU)/atomic.c src/cpu/$(CPU)/appSupport.c src/cpu/$(CPU)/cpuMath.c
+SRCS_os += \
+    os/cpu/$(CPU)/atomicBitset.c \
+    os/cpu/$(CPU)/cpu.c \
+    os/cpu/$(CPU)/pendsv.c \
+    os/cpu/$(CPU)/atomic.c \
+    os/cpu/$(CPU)/appSupport.c \
+    os/cpu/$(CPU)/cpuMath.c \
 
 #cpu runtime for bootloader
-SRCS_bl += src/cpu/$(CPU)/cpu.c
+SRCS_bl += os/cpu/$(CPU)/cpu.c
 
 #c runtime
-SRCS_os += ../lib/libc/memcpy-armv7m.S ../lib/libc/memset.c ../lib/libc/memcmp.c ../lib/libc/memmove.c
+SRCS_os += \
+    $(LIB_PATH)/libc/memcpy-armv7m.S \
+    $(LIB_PATH)/libc/memset.c \
+    $(LIB_PATH)/libc/memcmp.c \
+    $(LIB_PATH)/libc/memmove.c \
 
 #c runtime for bootloader
-SRCS_bl += ../lib/libc/memcpy-armv7m.S ../lib/libc/memset.c ../lib/libc/memcmp.c ../lib/libc/memmove.c
+SRCS_bl += \
+    $(LIB_PATH)/libc/memcpy-armv7m.S \
+    $(LIB_PATH)/libc/memset.c \
+    $(LIB_PATH)/libc/memcmp.c \
+    $(LIB_PATH)/libc/memmove.c \
 
 #floating point runtime (ARM)
 SRCS_os += external/arm/arm_sin_cos_f32.c
 FLAGS += -DARM_MATH_CM4 -D__FPU_PRESENT
 
 #floating point runtime (FreeBSD)
-SRCS_os += external/freebsd/lib/msun/src/e_atan2f.c external/freebsd/lib/msun/src/e_expf.c external/freebsd/lib/msun/src/s_atanf.c
-FLAGS += -DFLT_EVAL_METHOD
+SRCS_os += \
+    external/freebsd/lib/msun/src/e_atan2f.c \
+    external/freebsd/lib/msun/src/e_expf.c \
+    external/freebsd/lib/msun/src/s_atanf.c \
+
+FLAGS += -DFLT_EVAL_METHOD -Iexternal/freebsd/lib/msun/src
 
 #extra deps
-DEPS += $(wildcard inc/cpu/$(CPU)/*.h)
+DEPS += $(wildcard os/cpu/$(CPU)/inc/cpu/*.h)
 
 #bad words for C-M4F
 BADWORDS += "__floatundisf=When casting a uint64_t to float, use floatFromUint64"
@@ -60,6 +79,4 @@
 #all softfloat double funcs are forbidden
 BADWORDS += __muldf3 __divdf3 __subdf3 __adddf3 __truncdfsf2
 
-
-
-
+$(info Included CORTEX-M4 CPU)
diff --git a/firmware/src/cpu/cortexm4f/cpu.c b/firmware/os/cpu/cortexm4/cpu.c
similarity index 98%
rename from firmware/src/cpu/cortexm4f/cpu.c
rename to firmware/os/cpu/cortexm4/cpu.c
index 3aea26a..2c887e9 100644
--- a/firmware/src/cpu/cortexm4f/cpu.c
+++ b/firmware/os/cpu/cortexm4/cpu.c
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#include <plat/inc/cmsis.h>
-#include <plat/inc/plat.h>
-#include <plat/inc/pwr.h>
-#include <plat/inc/wdt.h>
+#include <plat/cmsis.h>
+#include <plat/plat.h>
+#include <plat/pwr.h>
+#include <plat/wdt.h>
 #include <syscall.h>
 #include <string.h>
 #include <seos.h>
diff --git a/firmware/src/cpu/cortexm4f/cpuMath.c b/firmware/os/cpu/cortexm4/cpuMath.c
similarity index 98%
rename from firmware/src/cpu/cortexm4f/cpuMath.c
rename to firmware/os/cpu/cortexm4/cpuMath.c
index f7e3970..af2df53 100644
--- a/firmware/src/cpu/cortexm4f/cpuMath.c
+++ b/firmware/os/cpu/cortexm4/cpuMath.c
@@ -1,4 +1,4 @@
-#include <cpu/inc/cpuMath.h>
+#include <cpu/cpuMath.h>
 
 uint32_t cpuMathUint44Div1000ToUint32_slow_path(uint64_t val)
 {
diff --git a/firmware/inc/cpu/cortexm4f/atomic.h b/firmware/os/cpu/cortexm4/inc/cpu/atomic.h
similarity index 85%
rename from firmware/inc/cpu/cortexm4f/atomic.h
rename to firmware/os/cpu/cortexm4/inc/cpu/atomic.h
index b942340..3eb66c2 100644
--- a/firmware/inc/cpu/cortexm4f/atomic.h
+++ b/firmware/os/cpu/cortexm4/inc/cpu/atomic.h
@@ -17,9 +17,6 @@
 #ifndef _CM4F_ATOMIC_H_
 #define _CM4F_ATOMIC_H_
 
-// real definition available in CPU-independent header file
-extern bool atomicCmpXchg32bits(volatile uint32_t *word, uint32_t prevVal, uint32_t newVal);
-
 static inline bool atomicCmpXchgPtr(volatile uintptr_t *word, uintptr_t prevVal, uintptr_t newVal) {
     // 32-bit CPU architecture so fall back appropriately
     return atomicCmpXchg32bits((volatile uint32_t *) word, (uint32_t) prevVal, (uint32_t) newVal);
diff --git a/firmware/inc/cpu/cortexm4f/atomicBitset.h b/firmware/os/cpu/cortexm4/inc/cpu/atomicBitset.h
similarity index 100%
rename from firmware/inc/cpu/cortexm4f/atomicBitset.h
rename to firmware/os/cpu/cortexm4/inc/cpu/atomicBitset.h
diff --git a/firmware/inc/cpu/cortexm4f/barrier.h b/firmware/os/cpu/cortexm4/inc/cpu/barrier.h
similarity index 100%
rename from firmware/inc/cpu/cortexm4f/barrier.h
rename to firmware/os/cpu/cortexm4/inc/cpu/barrier.h
diff --git a/firmware/inc/cpu/cortexm4f/cpuMath.h b/firmware/os/cpu/cortexm4/inc/cpu/cpuMath.h
similarity index 100%
rename from firmware/inc/cpu/cortexm4f/cpuMath.h
rename to firmware/os/cpu/cortexm4/inc/cpu/cpuMath.h
diff --git a/firmware/inc/cpu/cortexm4f/nanohub_math.h b/firmware/os/cpu/cortexm4/inc/cpu/nanohub_math.h
similarity index 97%
rename from firmware/inc/cpu/cortexm4f/nanohub_math.h
rename to firmware/os/cpu/cortexm4/inc/cpu/nanohub_math.h
index 320154f..d353c45 100644
--- a/firmware/inc/cpu/cortexm4f/nanohub_math.h
+++ b/firmware/os/cpu/cortexm4/inc/cpu/nanohub_math.h
@@ -17,6 +17,7 @@
 #ifndef _CPU_NANOHUB_MATH_H_
 #define _CPU_NANOHUB_MATH_H_
 
+#include <arm_math.h>
 
 #define asinf  arm_asinf
 #define sinf   arm_sin_f32
diff --git a/firmware/inc/cpu/cortexm4f/pendsv.h b/firmware/os/cpu/cortexm4/inc/cpu/pendsv.h
similarity index 100%
rename from firmware/inc/cpu/cortexm4f/pendsv.h
rename to firmware/os/cpu/cortexm4/inc/cpu/pendsv.h
diff --git a/firmware/inc/cpu/cortexm4f/syscallDo.h b/firmware/os/cpu/cortexm4/inc/cpu/syscallDo.h
similarity index 100%
rename from firmware/inc/cpu/cortexm4f/syscallDo.h
rename to firmware/os/cpu/cortexm4/inc/cpu/syscallDo.h
diff --git a/firmware/src/cpu/cortexm4f/pendsv.c b/firmware/os/cpu/cortexm4/pendsv.c
similarity index 97%
rename from firmware/src/cpu/cortexm4f/pendsv.c
rename to firmware/os/cpu/cortexm4/pendsv.c
index 1112de4..ef37c10 100644
--- a/firmware/src/cpu/cortexm4f/pendsv.c
+++ b/firmware/os/cpu/cortexm4/pendsv.c
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#include <plat/inc/cmsis.h>
-#include <cpu/inc/pendsv.h>
+#include <plat/cmsis.h>
+#include <cpu/pendsv.h>
 #include <stdio.h>
 
 
diff --git a/firmware/src/cpu/x86/atomic.c b/firmware/os/cpu/x86/atomic.c
similarity index 100%
rename from firmware/src/cpu/x86/atomic.c
rename to firmware/os/cpu/x86/atomic.c
diff --git a/firmware/src/cpu/x86/atomicBitset.c b/firmware/os/cpu/x86/atomicBitset.c
similarity index 100%
rename from firmware/src/cpu/x86/atomicBitset.c
rename to firmware/os/cpu/x86/atomicBitset.c
diff --git a/firmware/src/cpu/x86/cpu.c b/firmware/os/cpu/x86/cpu.c
similarity index 100%
rename from firmware/src/cpu/x86/cpu.c
rename to firmware/os/cpu/x86/cpu.c
diff --git a/firmware/inc/cpu/x86/atomicBitset.h b/firmware/os/cpu/x86/inc/cpu/atomicBitset.h
similarity index 100%
rename from firmware/inc/cpu/x86/atomicBitset.h
rename to firmware/os/cpu/x86/inc/cpu/atomicBitset.h
diff --git a/firmware/inc/cpu/x86/barrier.h b/firmware/os/cpu/x86/inc/cpu/barrier.h
similarity index 100%
rename from firmware/inc/cpu/x86/barrier.h
rename to firmware/os/cpu/x86/inc/cpu/barrier.h
diff --git a/firmware/misc/cpu/x86/Makefile b/firmware/os/cpu/x86/x86.mk
similarity index 80%
rename from firmware/misc/cpu/x86/Makefile
rename to firmware/os/cpu/x86/x86.mk
index dcc9295..352e91c 100644
--- a/firmware/misc/cpu/x86/Makefile
+++ b/firmware/os/cpu/x86/x86.mk
@@ -20,9 +20,12 @@
 FLAGS += -march=core2 -msse2 -DSYSCALL_VARARGS_PARAMS_PASSED_AS_PTRS
 
 #cpu runtime
-SRCS += src/cpu/$(CPU)/atomicBitset.c src/cpu/$(CPU)/cpu.c src/cpu/$(CPU)/atomic.c
+SRCS += \
+    os/cpu/$(CPU)/atomicBitset.c \
+    os/cpu/$(CPU)/cpu.c \
+    os/cpu/$(CPU)/atomic.c \
 
 #extra deps
-DEPS += $(wildcard inc/cpu/$(CPU)/*.h)
+DEPS += $(wildcard os/cpu/$(CPU)/inc/cpu/*.h)
 
-
+$(info Included x86 CPU)
diff --git a/firmware/src/drivers/ams_tmd2772/ams_tmd2772.c b/firmware/os/drivers/ams_tmd2772/ams_tmd2772.c
similarity index 100%
rename from firmware/src/drivers/ams_tmd2772/ams_tmd2772.c
rename to firmware/os/drivers/ams_tmd2772/ams_tmd2772.c
diff --git a/firmware/src/drivers/ams_tmd4903/ams_tmd4903.c b/firmware/os/drivers/ams_tmd4903/ams_tmd4903.c
similarity index 99%
rename from firmware/src/drivers/ams_tmd4903/ams_tmd4903.c
rename to firmware/os/drivers/ams_tmd4903/ams_tmd4903.c
index 104393c..a74e040 100644
--- a/firmware/src/drivers/ams_tmd4903/ams_tmd4903.c
+++ b/firmware/os/drivers/ams_tmd4903/ams_tmd4903.c
@@ -30,10 +30,10 @@
 #include <sensors.h>
 #include <seos.h>
 
-#include <plat/inc/exti.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/syscfg.h>
-#include <variant/inc/variant.h>
+#include <plat/exti.h>
+#include <plat/gpio.h>
+#include <plat/syscfg.h>
+#include <variant/variant.h>
 
 #define AMS_TMD4903_APP_ID      APP_ID_MAKE(APP_ID_VENDOR_GOOGLE, 12)
 #define AMS_TMD4903_APP_VERSION 10
diff --git a/firmware/src/drivers/bosch_bmi160/akm_ak09915_slave.c b/firmware/os/drivers/bosch_bmi160/akm_ak09915_slave.c
similarity index 100%
rename from firmware/src/drivers/bosch_bmi160/akm_ak09915_slave.c
rename to firmware/os/drivers/bosch_bmi160/akm_ak09915_slave.c
diff --git a/firmware/src/drivers/bosch_bmi160/akm_ak09915_slave.h b/firmware/os/drivers/bosch_bmi160/akm_ak09915_slave.h
similarity index 100%
rename from firmware/src/drivers/bosch_bmi160/akm_ak09915_slave.h
rename to firmware/os/drivers/bosch_bmi160/akm_ak09915_slave.h
diff --git a/firmware/src/drivers/bosch_bmi160/bosch_bmi160.c b/firmware/os/drivers/bosch_bmi160/bosch_bmi160.c
similarity index 99%
rename from firmware/src/drivers/bosch_bmi160/bosch_bmi160.c
rename to firmware/os/drivers/bosch_bmi160/bosch_bmi160.c
index ced8f0f..8283c5b 100644
--- a/firmware/src/drivers/bosch_bmi160/bosch_bmi160.c
+++ b/firmware/os/drivers/bosch_bmi160/bosch_bmi160.c
@@ -16,25 +16,25 @@
 
 #include <algos/time_sync.h>
 #include <atomic.h>
-#include <cpu/inc/cpuMath.h>
+#include <cpu/cpuMath.h>
 #include <gpio.h>
 #include <heap.h>
 #include <hostIntf.h>
 #include <isr.h>
 #include <nanohub_math.h>
 #include <nanohubPacket.h>
-#include <plat/inc/exti.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/syscfg.h>
-#include <plat/inc/rtc.h>
 #include <printf.h>
+#include <plat/exti.h>
+#include <plat/gpio.h>
+#include <plat/syscfg.h>
+#include <plat/rtc.h>
 #include <sensors.h>
 #include <seos.h>
 #include <slab.h>
 #include <spi.h>
 #include <timer.h>
-#include <variant/inc/sensType.h>
-#include <variant/inc/variant.h>
+#include <variant/sensType.h>
+#include <variant/variant.h>
 
 #ifdef MAG_SLAVE_PRESENT
 #include <algos/mag_cal.h>
diff --git a/firmware/src/drivers/bosch_bmi160/bosch_bmm150_slave.c b/firmware/os/drivers/bosch_bmi160/bosch_bmm150_slave.c
similarity index 100%
rename from firmware/src/drivers/bosch_bmi160/bosch_bmm150_slave.c
rename to firmware/os/drivers/bosch_bmi160/bosch_bmm150_slave.c
diff --git a/firmware/src/drivers/bosch_bmi160/bosch_bmm150_slave.h b/firmware/os/drivers/bosch_bmi160/bosch_bmm150_slave.h
similarity index 100%
rename from firmware/src/drivers/bosch_bmi160/bosch_bmm150_slave.h
rename to firmware/os/drivers/bosch_bmi160/bosch_bmm150_slave.h
diff --git a/firmware/src/drivers/bosch_bmp280/bosch_bmp280.c b/firmware/os/drivers/bosch_bmp280/bosch_bmp280.c
similarity index 100%
rename from firmware/src/drivers/bosch_bmp280/bosch_bmp280.c
rename to firmware/os/drivers/bosch_bmp280/bosch_bmp280.c
diff --git a/firmware/src/drivers/hall/hall.c b/firmware/os/drivers/hall/hall.c
similarity index 97%
rename from firmware/src/drivers/hall/hall.c
rename to firmware/os/drivers/hall/hall.c
index 01e8c8a..6469177 100644
--- a/firmware/src/drivers/hall/hall.c
+++ b/firmware/os/drivers/hall/hall.c
@@ -27,10 +27,10 @@
 #include <sensors.h>
 #include <seos.h>
 #include <timer.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/exti.h>
-#include <plat/inc/syscfg.h>
-#include <variant/inc/variant.h>
+#include <plat/gpio.h>
+#include <plat/exti.h>
+#include <plat/syscfg.h>
+#include <variant/variant.h>
 
 #define APP_VERSION 2
 
diff --git a/firmware/src/drivers/hall_twopole/hall_twopole.c b/firmware/os/drivers/hall_twopole/hall_twopole.c
similarity index 98%
rename from firmware/src/drivers/hall_twopole/hall_twopole.c
rename to firmware/os/drivers/hall_twopole/hall_twopole.c
index 11c77e5..847071f 100644
--- a/firmware/src/drivers/hall_twopole/hall_twopole.c
+++ b/firmware/os/drivers/hall_twopole/hall_twopole.c
@@ -27,10 +27,10 @@
 #include <sensors.h>
 #include <seos.h>
 #include <timer.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/exti.h>
-#include <plat/inc/syscfg.h>
-#include <variant/inc/variant.h>
+#include <plat/gpio.h>
+#include <plat/exti.h>
+#include <plat/syscfg.h>
+#include <variant/variant.h>
 
 #define APP_ID      APP_ID_MAKE(APP_ID_VENDOR_GOOGLE, 11)
 #define APP_VERSION 2
diff --git a/firmware/src/drivers/orientation/orientation.c b/firmware/os/drivers/orientation/orientation.c
similarity index 99%
rename from firmware/src/drivers/orientation/orientation.c
rename to firmware/os/drivers/orientation/orientation.c
index 7bb06ef..03fe0c8 100644
--- a/firmware/src/drivers/orientation/orientation.c
+++ b/firmware/os/drivers/orientation/orientation.c
@@ -18,8 +18,8 @@
 #include <string.h>
 #include <timer.h>
 #include <heap.h>
-#include <plat/inc/rtc.h>
-#include <plat/inc/syscfg.h>
+#include <plat/rtc.h>
+#include <plat/syscfg.h>
 #include <hostIntf.h>
 #include <nanohubPacket.h>
 #include <floatRt.h>
diff --git a/firmware/src/drivers/rohm_rpr0521/rohm_rpr0521.c b/firmware/os/drivers/rohm_rpr0521/rohm_rpr0521.c
similarity index 99%
rename from firmware/src/drivers/rohm_rpr0521/rohm_rpr0521.c
rename to firmware/os/drivers/rohm_rpr0521/rohm_rpr0521.c
index 7997820..c0ba625 100644
--- a/firmware/src/drivers/rohm_rpr0521/rohm_rpr0521.c
+++ b/firmware/os/drivers/rohm_rpr0521/rohm_rpr0521.c
@@ -30,10 +30,10 @@
 #include <sensors.h>
 #include <seos.h>
 
-#include <plat/inc/exti.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/syscfg.h>
-#include <variant/inc/variant.h>
+#include <plat/exti.h>
+#include <plat/gpio.h>
+#include <plat/syscfg.h>
+#include <variant/variant.h>
 
 #ifndef PROX_INT_PIN
 #error "PROX_INT_PIN is not defined; please define in variant.h"
diff --git a/firmware/src/drivers/tilt_detection/tilt_detection.c b/firmware/os/drivers/tilt_detection/tilt_detection.c
similarity index 99%
rename from firmware/src/drivers/tilt_detection/tilt_detection.c
rename to firmware/os/drivers/tilt_detection/tilt_detection.c
index c21d5b0..7ed4de0 100644
--- a/firmware/src/drivers/tilt_detection/tilt_detection.c
+++ b/firmware/os/drivers/tilt_detection/tilt_detection.c
@@ -18,8 +18,8 @@
 #include <string.h>
 #include <timer.h>
 #include <heap.h>
-#include <plat/inc/rtc.h>
-#include <plat/inc/syscfg.h>
+#include <plat/rtc.h>
+#include <plat/syscfg.h>
 #include <hostIntf.h>
 #include <nanohubPacket.h>
 
diff --git a/firmware/src/drivers/vsync/vsync.c b/firmware/os/drivers/vsync/vsync.c
similarity index 97%
rename from firmware/src/drivers/vsync/vsync.c
rename to firmware/os/drivers/vsync/vsync.c
index c6d55aa..b541ee1 100644
--- a/firmware/src/drivers/vsync/vsync.c
+++ b/firmware/os/drivers/vsync/vsync.c
@@ -28,10 +28,10 @@
 #include <seos.h>
 #include <slab.h>
 #include <timer.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/exti.h>
-#include <plat/inc/syscfg.h>
-#include <variant/inc/variant.h>
+#include <plat/gpio.h>
+#include <plat/exti.h>
+#include <plat/syscfg.h>
+#include <variant/variant.h>
 
 #define VSYNC_APP_ID      APP_ID_MAKE(APP_ID_VENDOR_GOOGLE, 7)
 #define VSYNC_APP_VERSION 2
diff --git a/firmware/src/drivers/window_orientation/window_orientation.c b/firmware/os/drivers/window_orientation/window_orientation.c
similarity index 99%
rename from firmware/src/drivers/window_orientation/window_orientation.c
rename to firmware/os/drivers/window_orientation/window_orientation.c
index e5cab76..2c6c94a 100644
--- a/firmware/src/drivers/window_orientation/window_orientation.c
+++ b/firmware/os/drivers/window_orientation/window_orientation.c
@@ -18,8 +18,8 @@
 #include <string.h>
 #include <timer.h>
 #include <heap.h>
-#include <plat/inc/rtc.h>
-#include <plat/inc/syscfg.h>
+#include <plat/rtc.h>
+#include <plat/syscfg.h>
 #include <hostIntf.h>
 #include <nanohubPacket.h>
 #include <floatRt.h>
diff --git a/firmware/inc/algos/accel_cal.h b/firmware/os/inc/algos/accel_cal.h
similarity index 100%
rename from firmware/inc/algos/accel_cal.h
rename to firmware/os/inc/algos/accel_cal.h
diff --git a/firmware/inc/algos/fusion.h b/firmware/os/inc/algos/fusion.h
similarity index 100%
rename from firmware/inc/algos/fusion.h
rename to firmware/os/inc/algos/fusion.h
diff --git a/firmware/inc/algos/gyro_cal.h b/firmware/os/inc/algos/gyro_cal.h
similarity index 100%
rename from firmware/inc/algos/gyro_cal.h
rename to firmware/os/inc/algos/gyro_cal.h
diff --git a/firmware/inc/algos/gyro_stillness_detect.h b/firmware/os/inc/algos/gyro_stillness_detect.h
similarity index 100%
rename from firmware/inc/algos/gyro_stillness_detect.h
rename to firmware/os/inc/algos/gyro_stillness_detect.h
diff --git a/firmware/inc/algos/mag_cal.h b/firmware/os/inc/algos/mag_cal.h
similarity index 100%
rename from firmware/inc/algos/mag_cal.h
rename to firmware/os/inc/algos/mag_cal.h
diff --git a/firmware/inc/algos/mat.h b/firmware/os/inc/algos/mat.h
similarity index 100%
rename from firmware/inc/algos/mat.h
rename to firmware/os/inc/algos/mat.h
diff --git a/firmware/inc/algos/quat.h b/firmware/os/inc/algos/quat.h
similarity index 100%
rename from firmware/inc/algos/quat.h
rename to firmware/os/inc/algos/quat.h
diff --git a/firmware/inc/algos/time_sync.h b/firmware/os/inc/algos/time_sync.h
similarity index 100%
rename from firmware/inc/algos/time_sync.h
rename to firmware/os/inc/algos/time_sync.h
diff --git a/firmware/inc/algos/vec.h b/firmware/os/inc/algos/vec.h
similarity index 100%
rename from firmware/inc/algos/vec.h
rename to firmware/os/inc/algos/vec.h
diff --git a/firmware/inc/apInt.h b/firmware/os/inc/apInt.h
similarity index 100%
rename from firmware/inc/apInt.h
rename to firmware/os/inc/apInt.h
diff --git a/firmware/inc/appSec.h b/firmware/os/inc/appSec.h
similarity index 100%
rename from firmware/inc/appSec.h
rename to firmware/os/inc/appSec.h
diff --git a/firmware/inc/atomic.h b/firmware/os/inc/atomic.h
similarity index 94%
rename from firmware/inc/atomic.h
rename to firmware/os/inc/atomic.h
index 5a44624..129b861 100644
--- a/firmware/inc/atomic.h
+++ b/firmware/os/inc/atomic.h
@@ -23,8 +23,6 @@
 
 #include <stdint.h>
 #include <stdbool.h>
-#include <cpu/inc/atomic.h>
-#include <cpu/inc/barrier.h>
 
 /* almost all platforms support byte and 32-bit operations of this sort. please do not add other sizes here */
 uint32_t atomicXchgByte(volatile uint8_t *byte, uint32_t newVal);
@@ -36,6 +34,10 @@
 uint32_t atomicAddByte(volatile uint8_t *byte, uint32_t addend);
 uint32_t atomicAdd32bits(volatile uint32_t *word, uint32_t addend);
 
+// pull in inline cpu-specific implementations, if any
+#include <cpu/atomic.h>
+#include <cpu/barrier.h>
+
 //writes with barriers
 static inline uint32_t atomicReadByte(volatile uint8_t *byte)
 {
diff --git a/firmware/inc/atomicBitset.h b/firmware/os/inc/atomicBitset.h
similarity index 98%
rename from firmware/inc/atomicBitset.h
rename to firmware/os/inc/atomicBitset.h
index 34374bf..c752176 100644
--- a/firmware/inc/atomicBitset.h
+++ b/firmware/os/inc/atomicBitset.h
@@ -19,7 +19,7 @@
 
 #include <stdint.h>
 #include <stdbool.h>
-#include <cpu/inc/atomicBitset.h>
+#include <cpu/atomicBitset.h>
 
 struct AtomicBitset;
 
diff --git a/firmware/inc/console.h b/firmware/os/inc/console.h
similarity index 100%
rename from firmware/inc/console.h
rename to firmware/os/inc/console.h
diff --git a/firmware/inc/cpu.h b/firmware/os/inc/cpu.h
similarity index 98%
rename from firmware/inc/cpu.h
rename to firmware/os/inc/cpu.h
index 2cf4445..22416e3 100644
--- a/firmware/inc/cpu.h
+++ b/firmware/os/inc/cpu.h
@@ -24,7 +24,7 @@
 
 #include <seos.h>
 #include <stdint.h>
-#include <plat/inc/app.h>
+#include <plat/app.h>
 
 
 void cpuInit(void);
diff --git a/firmware/os/inc/crc.h b/firmware/os/inc/crc.h
new file mode 100644
index 0000000..2c88d46
--- /dev/null
+++ b/firmware/os/inc/crc.h
@@ -0,0 +1,2 @@
+#include <nanohub/crc.h>
+#include <plat/crc.h>
diff --git a/firmware/inc/eeData.h b/firmware/os/inc/eeData.h
similarity index 100%
rename from firmware/inc/eeData.h
rename to firmware/os/inc/eeData.h
diff --git a/firmware/inc/eventQ.h b/firmware/os/inc/eventQ.h
similarity index 97%
rename from firmware/inc/eventQ.h
rename to firmware/os/inc/eventQ.h
index 2d4d417..b15e349 100644
--- a/firmware/inc/eventQ.h
+++ b/firmware/os/inc/eventQ.h
@@ -20,7 +20,7 @@
 
 #include <stdbool.h>
 #include <stdint.h>
-#include <plat/inc/taggedPtr.h>
+#include <plat/taggedPtr.h>
 
 
 #define EVENT_TYPE_BIT_DISCARDABLE_COMPAT    0x80000000 /* some external apps are using this one */
diff --git a/firmware/inc/eventnums.h b/firmware/os/inc/eventnums.h
similarity index 100%
rename from firmware/inc/eventnums.h
rename to firmware/os/inc/eventnums.h
diff --git a/firmware/inc/floatRt.h b/firmware/os/inc/floatRt.h
similarity index 100%
rename from firmware/inc/floatRt.h
rename to firmware/os/inc/floatRt.h
diff --git a/firmware/inc/gpio.h b/firmware/os/inc/gpio.h
similarity index 100%
rename from firmware/inc/gpio.h
rename to firmware/os/inc/gpio.h
diff --git a/firmware/inc/heap.h b/firmware/os/inc/heap.h
similarity index 100%
rename from firmware/inc/heap.h
rename to firmware/os/inc/heap.h
diff --git a/firmware/inc/hostIntf.h b/firmware/os/inc/hostIntf.h
similarity index 100%
rename from firmware/inc/hostIntf.h
rename to firmware/os/inc/hostIntf.h
diff --git a/firmware/inc/hostIntf_priv.h b/firmware/os/inc/hostIntf_priv.h
similarity index 100%
rename from firmware/inc/hostIntf_priv.h
rename to firmware/os/inc/hostIntf_priv.h
diff --git a/firmware/inc/i2c.h b/firmware/os/inc/i2c.h
similarity index 100%
rename from firmware/inc/i2c.h
rename to firmware/os/inc/i2c.h
diff --git a/firmware/inc/isr.h b/firmware/os/inc/isr.h
similarity index 100%
rename from firmware/inc/isr.h
rename to firmware/os/inc/isr.h
diff --git a/firmware/inc/list.h b/firmware/os/inc/list.h
similarity index 100%
rename from firmware/inc/list.h
rename to firmware/os/inc/list.h
diff --git a/firmware/inc/mpu.h b/firmware/os/inc/mpu.h
similarity index 100%
rename from firmware/inc/mpu.h
rename to firmware/os/inc/mpu.h
diff --git a/firmware/inc/nanohubCommand.h b/firmware/os/inc/nanohubCommand.h
similarity index 100%
rename from firmware/inc/nanohubCommand.h
rename to firmware/os/inc/nanohubCommand.h
diff --git a/firmware/inc/nanohubPacket.h b/firmware/os/inc/nanohubPacket.h
similarity index 100%
rename from firmware/inc/nanohubPacket.h
rename to firmware/os/inc/nanohubPacket.h
diff --git a/firmware/inc/nanohub_math.h b/firmware/os/inc/nanohub_math.h
similarity index 94%
rename from firmware/inc/nanohub_math.h
rename to firmware/os/inc/nanohub_math.h
index 6257c76..27de6ac 100644
--- a/firmware/inc/nanohub_math.h
+++ b/firmware/os/inc/nanohub_math.h
@@ -14,5 +14,5 @@
  * limitations under the License.
  */
 
-#include <cpu/inc/nanohub_math.h>
+#include <cpu/nanohub_math.h>
 #include <math.h>
diff --git a/firmware/inc/osApi.h b/firmware/os/inc/osApi.h
similarity index 100%
rename from firmware/inc/osApi.h
rename to firmware/os/inc/osApi.h
diff --git a/firmware/inc/platform.h b/firmware/os/inc/platform.h
similarity index 100%
rename from firmware/inc/platform.h
rename to firmware/os/inc/platform.h
diff --git a/firmware/inc/printf.h b/firmware/os/inc/printf.h
similarity index 100%
rename from firmware/inc/printf.h
rename to firmware/os/inc/printf.h
diff --git a/firmware/inc/reset.h b/firmware/os/inc/reset.h
similarity index 100%
rename from firmware/inc/reset.h
rename to firmware/os/inc/reset.h
diff --git a/firmware/inc/sensType.h b/firmware/os/inc/sensType.h
similarity index 100%
rename from firmware/inc/sensType.h
rename to firmware/os/inc/sensType.h
diff --git a/firmware/inc/sensors.h b/firmware/os/inc/sensors.h
similarity index 99%
rename from firmware/inc/sensors.h
rename to firmware/os/inc/sensors.h
index 8ff9bf8..dc1d5ad 100644
--- a/firmware/inc/sensors.h
+++ b/firmware/os/inc/sensors.h
@@ -20,8 +20,8 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-#include <plat/inc/taggedPtr.h>
-#include <variant/inc/variant.h>
+#include <plat/taggedPtr.h>
+#include <variant/variant.h>
 #include <eventnums.h>
 #include <sensType.h>
 #include <stdbool.h>
diff --git a/firmware/inc/seos.h b/firmware/os/inc/seos.h
similarity index 98%
rename from firmware/inc/seos.h
rename to firmware/os/inc/seos.h
index f417e57..9cc8afb 100644
--- a/firmware/inc/seos.h
+++ b/firmware/os/inc/seos.h
@@ -21,15 +21,15 @@
 extern "C" {
 #endif
 
-#include <plat/inc/taggedPtr.h>
+#include <plat/taggedPtr.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <eventQ.h>
-#include <plat/inc/app.h>
+#include <plat/app.h>
 #include <eventnums.h>
-#include <variant/inc/variant.h>
+#include <variant/variant.h>
 #include "toolchain.h"
 
 #include <nanohub/nanohub.h>
diff --git a/firmware/inc/simpleQ.h b/firmware/os/inc/simpleQ.h
similarity index 100%
rename from firmware/inc/simpleQ.h
rename to firmware/os/inc/simpleQ.h
diff --git a/firmware/inc/slab.h b/firmware/os/inc/slab.h
similarity index 100%
rename from firmware/inc/slab.h
rename to firmware/os/inc/slab.h
diff --git a/firmware/inc/spi.h b/firmware/os/inc/spi.h
similarity index 100%
rename from firmware/inc/spi.h
rename to firmware/os/inc/spi.h
diff --git a/firmware/inc/spi_priv.h b/firmware/os/inc/spi_priv.h
similarity index 100%
rename from firmware/inc/spi_priv.h
rename to firmware/os/inc/spi_priv.h
diff --git a/firmware/inc/syscall.h b/firmware/os/inc/syscall.h
similarity index 100%
rename from firmware/inc/syscall.h
rename to firmware/os/inc/syscall.h
diff --git a/firmware/inc/syscallDo.h b/firmware/os/inc/syscallDo.h
similarity index 99%
rename from firmware/inc/syscallDo.h
rename to firmware/os/inc/syscallDo.h
index 7dba269..f41e5e2 100644
--- a/firmware/inc/syscallDo.h
+++ b/firmware/os/inc/syscallDo.h
@@ -26,7 +26,7 @@
     #error "Syscalls should not be called from OS code"
 #endif
 
-#include <cpu/inc/syscallDo.h>
+#include <cpu/syscallDo.h>
 #include <sensors.h>
 #include <syscall.h>
 #include <stdarg.h>
diff --git a/firmware/inc/timer.h b/firmware/os/inc/timer.h
similarity index 100%
rename from firmware/inc/timer.h
rename to firmware/os/inc/timer.h
diff --git a/firmware/inc/toolchain.h b/firmware/os/inc/toolchain.h
similarity index 100%
rename from firmware/inc/toolchain.h
rename to firmware/os/inc/toolchain.h
diff --git a/firmware/inc/trylock.h b/firmware/os/inc/trylock.h
similarity index 97%
rename from firmware/inc/trylock.h
rename to firmware/os/inc/trylock.h
index 63a726d..e6eec91 100644
--- a/firmware/inc/trylock.h
+++ b/firmware/os/inc/trylock.h
@@ -21,7 +21,7 @@
 #include <stdbool.h>
 
 #ifdef PLATFORM_HAS_OWN_TRYLOCK
-#include <plat/inc/trylock.h>
+#include <plat/trylock.h>
 #else
 
 struct TryLock {
diff --git a/firmware/inc/usart.h b/firmware/os/inc/usart.h
similarity index 100%
rename from firmware/inc/usart.h
rename to firmware/os/inc/usart.h
diff --git a/firmware/inc/util.h b/firmware/os/inc/util.h
similarity index 98%
rename from firmware/inc/util.h
rename to firmware/os/inc/util.h
index c29345d..ed3e656 100644
--- a/firmware/inc/util.h
+++ b/firmware/os/inc/util.h
@@ -16,7 +16,7 @@
 
 #ifndef __UTIL_H
 #define __UTIL_H
-#include <plat/inc/plat.h>
+#include <plat/plat.h>
 #include "toolchain.h"
 #include <limits.h>
 #include <stdbool.h>
diff --git a/firmware/src/platform/linux/hostIntf.c b/firmware/os/platform/native/hostIntf.c
similarity index 100%
rename from firmware/src/platform/linux/hostIntf.c
rename to firmware/os/platform/native/hostIntf.c
diff --git a/firmware/src/platform/linux/i2c.c b/firmware/os/platform/native/i2c.c
similarity index 100%
rename from firmware/src/platform/linux/i2c.c
rename to firmware/os/platform/native/i2c.c
diff --git a/firmware/inc/platform/linux/app.h b/firmware/os/platform/native/inc/plat/app.h
similarity index 100%
rename from firmware/inc/platform/linux/app.h
rename to firmware/os/platform/native/inc/plat/app.h
diff --git a/firmware/os/platform/native/inc/plat/crc.h b/firmware/os/platform/native/inc/plat/crc.h
new file mode 100644
index 0000000..2c846cb
--- /dev/null
+++ b/firmware/os/platform/native/inc/plat/crc.h
@@ -0,0 +1,3 @@
+#include <nanohub/crc.h>
+
+#define crc32 soft_crc32
diff --git a/firmware/inc/platform/linux/plat.h b/firmware/os/platform/native/inc/plat/plat.h
similarity index 100%
rename from firmware/inc/platform/linux/plat.h
rename to firmware/os/platform/native/inc/plat/plat.h
diff --git a/firmware/inc/platform/linux/pwr.h b/firmware/os/platform/native/inc/plat/pwr.h
similarity index 100%
rename from firmware/inc/platform/linux/pwr.h
rename to firmware/os/platform/native/inc/plat/pwr.h
diff --git a/firmware/inc/platform/linux/rtc.h b/firmware/os/platform/native/inc/plat/rtc.h
similarity index 100%
rename from firmware/inc/platform/linux/rtc.h
rename to firmware/os/platform/native/inc/plat/rtc.h
diff --git a/firmware/inc/platform/linux/usart.h b/firmware/os/platform/native/inc/plat/usart.h
similarity index 100%
rename from firmware/inc/platform/linux/usart.h
rename to firmware/os/platform/native/inc/plat/usart.h
diff --git a/firmware/inc/platform/linux/wdt.h b/firmware/os/platform/native/inc/plat/wdt.h
similarity index 100%
rename from firmware/inc/platform/linux/wdt.h
rename to firmware/os/platform/native/inc/plat/wdt.h
diff --git a/firmware/inc/nanohub_math.h b/firmware/os/platform/native/lkr/app.lkr
similarity index 91%
copy from firmware/inc/nanohub_math.h
copy to firmware/os/platform/native/lkr/app.lkr
index 6257c76..37766bd 100644
--- a/firmware/inc/nanohub_math.h
+++ b/firmware/os/platform/native/lkr/app.lkr
@@ -13,6 +13,3 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-#include <cpu/inc/nanohub_math.h>
-#include <math.h>
diff --git a/firmware/misc/platform/linux/linux.extra.lkr b/firmware/os/platform/native/lkr/native.extra.lkr
similarity index 100%
rename from firmware/misc/platform/linux/linux.extra.lkr
rename to firmware/os/platform/native/lkr/native.extra.lkr
diff --git a/firmware/misc/platform/linux/Makefile b/firmware/os/platform/native/native.mk
similarity index 74%
rename from firmware/misc/platform/linux/Makefile
rename to firmware/os/platform/native/native.mk
index ad14dbf..f113061 100644
--- a/firmware/misc/platform/linux/Makefile
+++ b/firmware/os/platform/native/native.mk
@@ -15,21 +15,21 @@
 #
 
 DELIVERABLES = $(APP).bin
-LKR = misc/platform/$(PLATFORM)/linux.extra.lkr
+LKR = os/platform/$(PLATFORM)/lkr/native.extra.lkr
 
 FLAGS += -I. -fno-unwind-tables -fstack-reuse=all -ffunction-sections -fdata-sections -m32
 FLAGS += -Wl,-T $(LKR) -Wl,--gc-sections
 
 
 #platform drivers
-SRCS += src/platform/$(PLATFORM)/platform.c \
-	src/platform/$(PLATFORM)/i2c.c \
-	src/platform/$(PLATFORM)/spi.c \
-	src/platform/$(PLATFORM)/rtc.c \
-	src/platform/$(PLATFORM)/hostIntf.c
+SRCS += os/platform/$(PLATFORM)/platform.c \
+	os/platform/$(PLATFORM)/i2c.c \
+	os/platform/$(PLATFORM)/spi.c \
+	os/platform/$(PLATFORM)/rtc.c \
+	os/platform/$(PLATFORM)/hostIntf.c
 
 #extra deps
-DEPS += $(wildcard inc/platform/$(PLATFORM)/*.h)
+DEPS += $(wildcard os/platform/$(PLATFORM)/inc/plat/*.h)
 DEPS += $(LKR)
 
 #platform flags
@@ -40,5 +40,6 @@
 #platform-specific rules
 
 %.bin : %.elf
-	mv $< $@
+	cp $< $@
 
+$(info Included NATIVE platfrom)
diff --git a/firmware/src/platform/linux/platform.c b/firmware/os/platform/native/platform.c
similarity index 97%
rename from firmware/src/platform/linux/platform.c
rename to firmware/os/platform/native/platform.c
index 735f295..ceac5a6 100644
--- a/firmware/src/platform/linux/platform.c
+++ b/firmware/os/platform/native/platform.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <plat/inc/rtc.h>
+#include <plat/rtc.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
diff --git a/firmware/src/platform/linux/rtc.c b/firmware/os/platform/native/rtc.c
similarity index 94%
rename from firmware/src/platform/linux/rtc.c
rename to firmware/os/platform/native/rtc.c
index e262f46..075f5e2 100644
--- a/firmware/src/platform/linux/rtc.c
+++ b/firmware/os/platform/native/rtc.c
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#include <cpu/inc/barrier.h>
-#include <plat/inc/rtc.h>
+#include <cpu/barrier.h>
+#include <plat/rtc.h>
 #include <timer.h>
 #include <platform.h>
 
diff --git a/firmware/src/platform/linux/spi.c b/firmware/os/platform/native/spi.c
similarity index 100%
rename from firmware/src/platform/linux/spi.c
rename to firmware/os/platform/native/spi.c
diff --git a/firmware/src/platform/stm32f4xx/apInt.c b/firmware/os/platform/stm32/apInt.c
similarity index 94%
rename from firmware/src/platform/stm32f4xx/apInt.c
rename to firmware/os/platform/stm32/apInt.c
index b8a1c01..d8521d1 100644
--- a/firmware/src/platform/stm32f4xx/apInt.c
+++ b/firmware/os/platform/stm32/apInt.c
@@ -16,11 +16,11 @@
 
 #include <apInt.h>
 #include <gpio.h>
-#include <variant/inc/variant.h>
-#include <plat/inc/gpio.h>
+#include <variant/variant.h>
+#include <plat/gpio.h>
 #include <seos.h>
 #include <platform.h>
-#include <plat/inc/plat.h>
+#include <plat/plat.h>
 
 static struct Gpio *apIntWkup;
 #ifdef AP_INT_NONWAKEUP
diff --git a/firmware/src/platform/stm32f4xx/bl.c b/firmware/os/platform/stm32/bl.c
similarity index 99%
rename from firmware/src/platform/stm32f4xx/bl.c
rename to firmware/os/platform/stm32/bl.c
index 2786c36..9d634e9 100644
--- a/firmware/src/platform/stm32f4xx/bl.c
+++ b/firmware/os/platform/stm32/bl.c
@@ -15,12 +15,12 @@
  */
 
 
-#include <variant/inc/variant.h>
+#include <variant/variant.h>
 
-#include <plat/inc/cmsis.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/pwr.h>
-#include <plat/inc/bl.h>
+#include <plat/cmsis.h>
+#include <plat/gpio.h>
+#include <plat/pwr.h>
+#include <plat/bl.h>
 
 #include <nanohub/sha2.h>
 #include <nanohub/aes.h>
diff --git a/firmware/src/platform/stm32f4xx/crc.c b/firmware/os/platform/stm32/crc.c
similarity index 98%
rename from firmware/src/platform/stm32f4xx/crc.c
rename to firmware/os/platform/stm32/crc.c
index 771703d..ece90e7 100644
--- a/firmware/src/platform/stm32f4xx/crc.c
+++ b/firmware/os/platform/stm32/crc.c
@@ -19,7 +19,7 @@
 #include <nanohub/crc.h>
 #include <seos.h>
 
-#include <plat/inc/pwr.h>
+#include <plat/pwr.h>
 
 struct StmCrcRegs {
     volatile uint32_t DR;
diff --git a/firmware/src/platform/stm32f4xx/crt_stm32f4xx.c b/firmware/os/platform/stm32/crt_stm32.c
similarity index 100%
rename from firmware/src/platform/stm32f4xx/crt_stm32f4xx.c
rename to firmware/os/platform/stm32/crt_stm32.c
diff --git a/firmware/src/platform/stm32f4xx/dma.c b/firmware/os/platform/stm32/dma.c
similarity index 98%
rename from firmware/src/platform/stm32f4xx/dma.c
rename to firmware/os/platform/stm32/dma.c
index 226bb98..6813b25 100644
--- a/firmware/src/platform/stm32f4xx/dma.c
+++ b/firmware/os/platform/stm32/dma.c
@@ -20,9 +20,9 @@
 #include <seos.h>
 #include <util.h>
 
-#include <plat/inc/cmsis.h>
-#include <plat/inc/dma.h>
-#include <plat/inc/pwr.h>
+#include <plat/cmsis.h>
+#include <plat/dma.h>
+#include <plat/pwr.h>
 
 #define DMA_VERBOSE_DEBUG 0
 
diff --git a/firmware/src/platform/stm32f4xx/eeData.c b/firmware/os/platform/stm32/eeData.c
similarity index 98%
rename from firmware/src/platform/stm32f4xx/eeData.c
rename to firmware/os/platform/stm32/eeData.c
index 906b9f3..8ce4774 100644
--- a/firmware/src/platform/stm32f4xx/eeData.c
+++ b/firmware/os/platform/stm32/eeData.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <plat/inc/bl.h>
+#include <plat/bl.h>
 #include <stdbool.h>
 #include <string.h>
 #include <stdint.h>
diff --git a/firmware/src/platform/stm32f4xx/exti.c b/firmware/os/platform/stm32/exti.c
similarity index 97%
rename from firmware/src/platform/stm32f4xx/exti.c
rename to firmware/os/platform/stm32/exti.c
index 30e91db..ababe3c 100644
--- a/firmware/src/platform/stm32f4xx/exti.c
+++ b/firmware/os/platform/stm32/exti.c
@@ -17,9 +17,9 @@
 #include <errno.h>
 #include <isr.h>
 
-#include <plat/inc/cmsis.h>
-#include <plat/inc/exti.h>
-#include <plat/inc/pwr.h>
+#include <plat/cmsis.h>
+#include <plat/exti.h>
+#include <plat/pwr.h>
 
 struct StmExti
 {
diff --git a/firmware/misc/platform/stm32f4xx/flash_script/Makefile b/firmware/os/platform/stm32/flash_script/Makefile
similarity index 100%
rename from firmware/misc/platform/stm32f4xx/flash_script/Makefile
rename to firmware/os/platform/stm32/flash_script/Makefile
diff --git a/firmware/misc/platform/stm32f4xx/flash_script/tool.c b/firmware/os/platform/stm32/flash_script/tool.c
similarity index 100%
rename from firmware/misc/platform/stm32f4xx/flash_script/tool.c
rename to firmware/os/platform/stm32/flash_script/tool.c
diff --git a/firmware/src/platform/stm32f4xx/gpio.c b/firmware/os/platform/stm32/gpio.c
similarity index 99%
rename from firmware/src/platform/stm32f4xx/gpio.c
rename to firmware/os/platform/stm32/gpio.c
index ff17c79..648ca09 100644
--- a/firmware/src/platform/stm32f4xx/gpio.c
+++ b/firmware/os/platform/stm32/gpio.c
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#include <plat/inc/gpio.h>
-#include <plat/inc/pwr.h>
+#include <plat/gpio.h>
+#include <plat/pwr.h>
 #include <gpio.h>
 #include <cpu.h>
 
diff --git a/firmware/src/platform/stm32f4xx/hostIntf.c b/firmware/os/platform/stm32/hostIntf.c
similarity index 94%
rename from firmware/src/platform/stm32f4xx/hostIntf.c
rename to firmware/os/platform/stm32/hostIntf.c
index c561d9a..8993f56 100644
--- a/firmware/src/platform/stm32f4xx/hostIntf.c
+++ b/firmware/os/platform/stm32/hostIntf.c
@@ -16,10 +16,10 @@
 
 #include <hostIntf.h>
 #include <hostIntf_priv.h>
-#include <variant/inc/variant.h>
-#include <plat/inc/bl.h>
-#include <plat/inc/cmsis.h>
-#include <plat/inc/spi.h>
+#include <variant/variant.h>
+#include <plat/bl.h>
+#include <plat/cmsis.h>
+#include <plat/spi.h>
 
 
 const struct HostIntfComm *platHostIntfInit()
diff --git a/firmware/src/platform/stm32f4xx/i2c.c b/firmware/os/platform/stm32/i2c.c
similarity index 98%
rename from firmware/src/platform/stm32f4xx/i2c.c
rename to firmware/os/platform/stm32/i2c.c
index dd911c1..32b3985 100644
--- a/firmware/src/platform/stm32f4xx/i2c.c
+++ b/firmware/os/platform/stm32/i2c.c
@@ -27,14 +27,14 @@
 #include <atomic.h>
 #include <platform.h>
 
-#include <plat/inc/cmsis.h>
-#include <plat/inc/dma.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/i2c.h>
-#include <plat/inc/pwr.h>
-#include <plat/inc/plat.h>
+#include <plat/cmsis.h>
+#include <plat/dma.h>
+#include <plat/gpio.h>
+#include <plat/i2c.h>
+#include <plat/pwr.h>
+#include <plat/plat.h>
 
-#include <cpu/inc/barrier.h>
+#include <cpu/barrier.h>
 
 #define I2C_VERBOSE_DEBUG       0
 #define I2C_MAX_QUEUE_DEPTH     5
diff --git a/firmware/inc/platform/stm32f4xx/app.h b/firmware/os/platform/stm32/inc/plat/app.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/app.h
rename to firmware/os/platform/stm32/inc/plat/app.h
diff --git a/firmware/inc/platform/stm32f4xx/bl.h b/firmware/os/platform/stm32/inc/plat/bl.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/bl.h
rename to firmware/os/platform/stm32/inc/plat/bl.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis.h b/firmware/os/platform/stm32/inc/plat/cmsis.h
similarity index 99%
rename from firmware/inc/platform/stm32f4xx/cmsis.h
rename to firmware/os/platform/stm32/inc/plat/cmsis.h
index efac7cd..07f0798 100644
--- a/firmware/inc/platform/stm32f4xx/cmsis.h
+++ b/firmware/os/platform/stm32/inc/plat/cmsis.h
@@ -126,7 +126,7 @@
     NUM_INTERRUPTS

 } IRQn_Type;

 

-#include "core_cm4.h"

+#include <core_cm4.h>

 

 #ifdef __cplusplus

 }

diff --git a/firmware/inc/platform/stm32f4xx/cmsis/arm_common_tables.h b/firmware/os/platform/stm32/inc/plat/cmsis/arm_common_tables.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/arm_common_tables.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/arm_common_tables.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/arm_const_structs.h b/firmware/os/platform/stm32/inc/plat/cmsis/arm_const_structs.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/arm_const_structs.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/arm_const_structs.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/arm_math.h b/firmware/os/platform/stm32/inc/plat/cmsis/arm_math.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/arm_math.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/arm_math.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_cm0.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_cm0.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_cm0.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_cm0.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_cm0plus.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_cm0plus.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_cm0plus.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_cm0plus.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_cm3.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_cm3.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_cm3.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_cm3.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_cm4.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_cm4.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_cm4.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_cm4.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_cm7.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_cm7.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_cm7.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_cm7.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_cmFunc.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_cmFunc.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_cmFunc.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_cmFunc.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_cmInstr.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_cmInstr.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_cmInstr.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_cmInstr.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_cmSimd.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_cmSimd.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_cmSimd.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_cmSimd.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_sc000.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_sc000.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_sc000.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_sc000.h
diff --git a/firmware/inc/platform/stm32f4xx/cmsis/core_sc300.h b/firmware/os/platform/stm32/inc/plat/cmsis/core_sc300.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/cmsis/core_sc300.h
rename to firmware/os/platform/stm32/inc/plat/cmsis/core_sc300.h
diff --git a/firmware/os/platform/stm32/inc/plat/crc.h b/firmware/os/platform/stm32/inc/plat/crc.h
new file mode 100644
index 0000000..40a8c17
--- /dev/null
+++ b/firmware/os/platform/stm32/inc/plat/crc.h
@@ -0,0 +1 @@
+/* empty */
diff --git a/firmware/inc/platform/stm32f4xx/dma.h b/firmware/os/platform/stm32/inc/plat/dma.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/dma.h
rename to firmware/os/platform/stm32/inc/plat/dma.h
diff --git a/firmware/inc/platform/stm32f4xx/eeData.h b/firmware/os/platform/stm32/inc/plat/eeData.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/eeData.h
rename to firmware/os/platform/stm32/inc/plat/eeData.h
diff --git a/firmware/inc/platform/stm32f4xx/exti.h b/firmware/os/platform/stm32/inc/plat/exti.h
similarity index 97%
rename from firmware/inc/platform/stm32f4xx/exti.h
rename to firmware/os/platform/stm32/inc/plat/exti.h
index 84fcd39..5d32a21 100644
--- a/firmware/inc/platform/stm32f4xx/exti.h
+++ b/firmware/os/platform/stm32/inc/plat/exti.h
@@ -19,8 +19,8 @@
 
 #include <isr.h>
 #include <stdbool.h>
-#include <plat/inc/cmsis.h>
-#include <plat/inc/gpio.h>
+#include <plat/cmsis.h>
+#include <plat/gpio.h>
 #include <gpio.h>
 
 #ifdef __cplusplus
diff --git a/firmware/inc/platform/stm32f4xx/gpio.h b/firmware/os/platform/stm32/inc/plat/gpio.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/gpio.h
rename to firmware/os/platform/stm32/inc/plat/gpio.h
diff --git a/firmware/inc/platform/stm32f4xx/i2c.h b/firmware/os/platform/stm32/inc/plat/i2c.h
similarity index 96%
rename from firmware/inc/platform/stm32f4xx/i2c.h
rename to firmware/os/platform/stm32/inc/plat/i2c.h
index f1f021f..bf46f45 100644
--- a/firmware/inc/platform/stm32f4xx/i2c.h
+++ b/firmware/os/platform/stm32/inc/plat/i2c.h
@@ -19,9 +19,9 @@
 
 #include <gpio.h>
 #include <platform.h>
-#include <plat/inc/cmsis.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/plat.h>
+#include <plat/cmsis.h>
+#include <plat/gpio.h>
+#include <plat/plat.h>
 
 struct StmI2cDmaCfg {
     uint8_t channel;
diff --git a/firmware/inc/platform/stm32f4xx/plat.h b/firmware/os/platform/stm32/inc/plat/plat.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/plat.h
rename to firmware/os/platform/stm32/inc/plat/plat.h
diff --git a/firmware/inc/platform/stm32f4xx/pwr.h b/firmware/os/platform/stm32/inc/plat/pwr.h
similarity index 99%
rename from firmware/inc/platform/stm32f4xx/pwr.h
rename to firmware/os/platform/stm32/inc/plat/pwr.h
index fde926f..e142c51 100644
--- a/firmware/inc/platform/stm32f4xx/pwr.h
+++ b/firmware/os/platform/stm32/inc/plat/pwr.h
@@ -23,7 +23,7 @@
 
 #include <stdint.h>
 #include <stdbool.h>
-#include <plat/inc/rtc.h>
+#include <plat/rtc.h>
 
 /* busses */
 #define PERIPH_BUS_AHB1               0
diff --git a/firmware/inc/platform/stm32f4xx/rtc.h b/firmware/os/platform/stm32/inc/plat/rtc.h
similarity index 97%
rename from firmware/inc/platform/stm32f4xx/rtc.h
rename to firmware/os/platform/stm32/inc/plat/rtc.h
index e807a1d..5615356 100644
--- a/firmware/inc/platform/stm32f4xx/rtc.h
+++ b/firmware/os/platform/stm32/inc/plat/rtc.h
@@ -22,7 +22,7 @@
 #endif
 
 #include <stdint.h>
-#include <inc/seos.h>
+#include <seos.h>
 
 enum RtcClock
 {
diff --git a/firmware/inc/platform/stm32f4xx/spi.h b/firmware/os/platform/stm32/inc/plat/spi.h
similarity index 96%
rename from firmware/inc/platform/stm32f4xx/spi.h
rename to firmware/os/platform/stm32/inc/plat/spi.h
index 79bc3c4..22c8070 100644
--- a/firmware/inc/platform/stm32f4xx/spi.h
+++ b/firmware/os/platform/stm32/inc/plat/spi.h
@@ -19,9 +19,9 @@
 
 #include <gpio.h>
 #include <platform.h>
-#include <plat/inc/cmsis.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/plat.h>
+#include <plat/cmsis.h>
+#include <plat/gpio.h>
+#include <plat/plat.h>
 
 struct StmSpiDmaCfg {
     uint8_t channel;
diff --git a/firmware/inc/platform/stm32f4xx/syscfg.h b/firmware/os/platform/stm32/inc/plat/syscfg.h
similarity index 96%
rename from firmware/inc/platform/stm32f4xx/syscfg.h
rename to firmware/os/platform/stm32/inc/plat/syscfg.h
index de8bab1..464895e 100644
--- a/firmware/inc/platform/stm32f4xx/syscfg.h
+++ b/firmware/os/platform/stm32/inc/plat/syscfg.h
@@ -17,7 +17,7 @@
 #ifndef _STM32F4XX_SYSCFG_H_
 #define _STM32F4XX_SYSCFG_H_
 
-#include <plat/inc/gpio.h>
+#include <plat/gpio.h>
 #include <gpio.h>
 
 #ifdef __cplusplus
diff --git a/firmware/inc/platform/stm32f4xx/taggedPtr.h b/firmware/os/platform/stm32/inc/plat/taggedPtr.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/taggedPtr.h
rename to firmware/os/platform/stm32/inc/plat/taggedPtr.h
diff --git a/firmware/inc/platform/stm32f4xx/usart.h b/firmware/os/platform/stm32/inc/plat/usart.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/usart.h
rename to firmware/os/platform/stm32/inc/plat/usart.h
diff --git a/firmware/inc/platform/stm32f4xx/wdt.h b/firmware/os/platform/stm32/inc/plat/wdt.h
similarity index 100%
rename from firmware/inc/platform/stm32f4xx/wdt.h
rename to firmware/os/platform/stm32/inc/plat/wdt.h
diff --git a/firmware/misc/cpu/cortexm4f/app.lkr b/firmware/os/platform/stm32/lkr/app.lkr
similarity index 91%
rename from firmware/misc/cpu/cortexm4f/app.lkr
rename to firmware/os/platform/stm32/lkr/app.lkr
index 6766e29..c2196a9 100644
--- a/firmware/misc/cpu/cortexm4f/app.lkr
+++ b/firmware/os/platform/stm32/lkr/app.lkr
@@ -87,6 +87,18 @@
         __data_end = ABSOLUTE(.);
 
         . = ALIGN(4);
+        __init_array_start = ABSOLUTE(.);
+        KEEP(*(SORT(.init_array.*)))
+        KEEP(*(.init_array))
+        __init_array_end = ABSOLUTE(.);
+
+        . = ALIGN(4);
+        __fini_array_start = ABSOLUTE(.);
+        KEEP(*(SORT(.fini_array.*)))
+        KEEP(*(.fini_array))
+        __fini_array_end = ABSOLUTE(.);
+
+        . = ALIGN(4);
         __got_start = ABSOLUTE(.);
         *(.got) *(.got.*) ;
         __got_end = ABSOLUTE(.);
diff --git a/firmware/misc/variant/common/stm32f411.os.lkr b/firmware/os/platform/stm32/lkr/stm32f401.bl.lkr
similarity index 80%
rename from firmware/misc/variant/common/stm32f411.os.lkr
rename to firmware/os/platform/stm32/lkr/stm32f401.bl.lkr
index 2553459..c903e15 100644
--- a/firmware/misc/variant/common/stm32f411.os.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f401.bl.lkr
@@ -15,6 +15,6 @@
  */
 
 
-INCLUDE misc/variant/common/stm32f411.map.lkr
-INCLUDE misc/variant/common/stm32f4xx.os.lkr
-INCLUDE misc/variant/common/stm32f4xx.common.lkr
+INCLUDE os/platform/stm32/lkr/stm32f401.map.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.bl.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.common.lkr
diff --git a/firmware/misc/variant/common/stm32f401.map.lkr b/firmware/os/platform/stm32/lkr/stm32f401.map.lkr
similarity index 99%
rename from firmware/misc/variant/common/stm32f401.map.lkr
rename to firmware/os/platform/stm32/lkr/stm32f401.map.lkr
index f03ef56..b726f14 100644
--- a/firmware/misc/variant/common/stm32f401.map.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f401.map.lkr
@@ -14,8 +14,6 @@
  * limitations under the License.
  */
 
-
-
 MEMORY
 {
 	bl	: ORIGIN = 0x08000000,	LENGTH = 16K	/* one block */
@@ -24,4 +22,3 @@
 	shared	: ORIGIN = 0x08020000,	LENGTH = 384K	/* 3x 128K blocks */
 	ram	: ORIGIN = 0x20000000,	LENGTH = 96K
 }
-
diff --git a/firmware/misc/variant/common/stm32f411.os.lkr b/firmware/os/platform/stm32/lkr/stm32f401.os.lkr
similarity index 80%
copy from firmware/misc/variant/common/stm32f411.os.lkr
copy to firmware/os/platform/stm32/lkr/stm32f401.os.lkr
index 2553459..aef5c61 100644
--- a/firmware/misc/variant/common/stm32f411.os.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f401.os.lkr
@@ -15,6 +15,6 @@
  */
 
 
-INCLUDE misc/variant/common/stm32f411.map.lkr
-INCLUDE misc/variant/common/stm32f4xx.os.lkr
-INCLUDE misc/variant/common/stm32f4xx.common.lkr
+INCLUDE os/platform/stm32/lkr/stm32f401.map.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.os.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.common.lkr
diff --git a/firmware/misc/variant/common/stm32f411.os.lkr b/firmware/os/platform/stm32/lkr/stm32f411.bl.lkr
similarity index 80%
copy from firmware/misc/variant/common/stm32f411.os.lkr
copy to firmware/os/platform/stm32/lkr/stm32f411.bl.lkr
index 2553459..09a2136 100644
--- a/firmware/misc/variant/common/stm32f411.os.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f411.bl.lkr
@@ -15,6 +15,6 @@
  */
 
 
-INCLUDE misc/variant/common/stm32f411.map.lkr
-INCLUDE misc/variant/common/stm32f4xx.os.lkr
-INCLUDE misc/variant/common/stm32f4xx.common.lkr
+INCLUDE os/platform/stm32/lkr/stm32f411.map.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.bl.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.common.lkr
diff --git a/firmware/misc/variant/common/stm32f411.map.lkr b/firmware/os/platform/stm32/lkr/stm32f411.map.lkr
similarity index 100%
rename from firmware/misc/variant/common/stm32f411.map.lkr
rename to firmware/os/platform/stm32/lkr/stm32f411.map.lkr
diff --git a/firmware/misc/variant/common/stm32f411.os.lkr b/firmware/os/platform/stm32/lkr/stm32f411.os.lkr
similarity index 80%
copy from firmware/misc/variant/common/stm32f411.os.lkr
copy to firmware/os/platform/stm32/lkr/stm32f411.os.lkr
index 2553459..db0a6c8 100644
--- a/firmware/misc/variant/common/stm32f411.os.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f411.os.lkr
@@ -15,6 +15,6 @@
  */
 
 
-INCLUDE misc/variant/common/stm32f411.map.lkr
-INCLUDE misc/variant/common/stm32f4xx.os.lkr
-INCLUDE misc/variant/common/stm32f4xx.common.lkr
+INCLUDE os/platform/stm32/lkr/stm32f411.map.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.os.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.common.lkr
diff --git a/firmware/misc/variant/common/stm32f411.os.lkr b/firmware/os/platform/stm32/lkr/stm32f412.bl.lkr
similarity index 80%
copy from firmware/misc/variant/common/stm32f411.os.lkr
copy to firmware/os/platform/stm32/lkr/stm32f412.bl.lkr
index 2553459..1ae513d 100644
--- a/firmware/misc/variant/common/stm32f411.os.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f412.bl.lkr
@@ -15,6 +15,6 @@
  */
 
 
-INCLUDE misc/variant/common/stm32f411.map.lkr
-INCLUDE misc/variant/common/stm32f4xx.os.lkr
-INCLUDE misc/variant/common/stm32f4xx.common.lkr
+INCLUDE os/platform/stm32/lkr/stm32f412.map.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.bl.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.common.lkr
diff --git a/firmware/misc/variant/common/stm32f401.map.lkr b/firmware/os/platform/stm32/lkr/stm32f412.map.lkr
similarity index 79%
copy from firmware/misc/variant/common/stm32f401.map.lkr
copy to firmware/os/platform/stm32/lkr/stm32f412.map.lkr
index f03ef56..5e5f82a 100644
--- a/firmware/misc/variant/common/stm32f401.map.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f412.map.lkr
@@ -14,14 +14,11 @@
  * limitations under the License.
  */
 
-
-
 MEMORY
 {
 	bl	: ORIGIN = 0x08000000,	LENGTH = 16K	/* one block */
 	eedata	: ORIGIN = 0x08004000,	LENGTH = 32K	/* two 16K blocks */
-	code	: ORIGIN = 0x0800C000,	LENGTH = 80K	/* 16K block + 64K block */
-	shared	: ORIGIN = 0x08020000,	LENGTH = 384K	/* 3x 128K blocks */
-	ram	: ORIGIN = 0x20000000,	LENGTH = 96K
+	code	: ORIGIN = 0x0800C000,	LENGTH = 208K	/* 16K block + 64K block + 128K block */
+	shared	: ORIGIN = 0x08040000,	LENGTH = 768K	/* 6x 128K blocks */
+	ram	: ORIGIN = 0x20000000,	LENGTH = 256K
 }
-
diff --git a/firmware/misc/variant/common/stm32f411.os.lkr b/firmware/os/platform/stm32/lkr/stm32f412.os.lkr
similarity index 80%
copy from firmware/misc/variant/common/stm32f411.os.lkr
copy to firmware/os/platform/stm32/lkr/stm32f412.os.lkr
index 2553459..0662cba 100644
--- a/firmware/misc/variant/common/stm32f411.os.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f412.os.lkr
@@ -15,6 +15,6 @@
  */
 
 
-INCLUDE misc/variant/common/stm32f411.map.lkr
-INCLUDE misc/variant/common/stm32f4xx.os.lkr
-INCLUDE misc/variant/common/stm32f4xx.common.lkr
+INCLUDE os/platform/stm32/lkr/stm32f412.map.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.os.lkr
+INCLUDE os/platform/stm32/lkr/stm32f4xx.common.lkr
diff --git a/firmware/misc/variant/common/stm32f4xx.bl.lkr b/firmware/os/platform/stm32/lkr/stm32f4xx.bl.lkr
similarity index 99%
rename from firmware/misc/variant/common/stm32f4xx.bl.lkr
rename to firmware/os/platform/stm32/lkr/stm32f4xx.bl.lkr
index d83b529..b2940c5 100644
--- a/firmware/misc/variant/common/stm32f4xx.bl.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f4xx.bl.lkr
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-
 SECTIONS
 {
 	/* bootloader */
@@ -74,6 +73,4 @@
 	} > ram
 }
 
-
 ENTRY(__BL_VECTORS)
-
diff --git a/firmware/misc/variant/common/stm32f4xx.common.lkr b/firmware/os/platform/stm32/lkr/stm32f4xx.common.lkr
similarity index 98%
rename from firmware/misc/variant/common/stm32f4xx.common.lkr
rename to firmware/os/platform/stm32/lkr/stm32f4xx.common.lkr
index 30b2d7e..8df1d05 100644
--- a/firmware/misc/variant/common/stm32f4xx.common.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f4xx.common.lkr
@@ -1,4 +1,3 @@
-
 __ram_start = ORIGIN(ram);
 __ram_end = ORIGIN(ram) + LENGTH(ram);
 __eedata_start = ORIGIN(eedata);
@@ -8,5 +7,3 @@
 __shared_start = ORIGIN(shared);
 __shared_end = ORIGIN(shared) + LENGTH(shared);
 __data_data = LOADADDR(.data);
-
-
diff --git a/firmware/misc/variant/common/stm32f4xx.os.lkr b/firmware/os/platform/stm32/lkr/stm32f4xx.os.lkr
similarity index 99%
rename from firmware/misc/variant/common/stm32f4xx.os.lkr
rename to firmware/os/platform/stm32/lkr/stm32f4xx.os.lkr
index 153faed..ac7a019 100644
--- a/firmware/misc/variant/common/stm32f4xx.os.lkr
+++ b/firmware/os/platform/stm32/lkr/stm32f4xx.os.lkr
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-
 SECTIONS
 {
 	/* things that live in the main app image */
@@ -68,5 +67,3 @@
 __app_start = __data_data + __data_end - __data_start;
 __heap_end = ORIGIN(ram) + LENGTH(ram);
 ENTRY(__VECTORS)
-
-
diff --git a/firmware/misc/debug.privkey b/firmware/os/platform/stm32/misc/debug.privkey
similarity index 100%
rename from firmware/misc/debug.privkey
rename to firmware/os/platform/stm32/misc/debug.privkey
diff --git a/firmware/misc/debug.pubkey b/firmware/os/platform/stm32/misc/debug.pubkey
similarity index 100%
rename from firmware/misc/debug.pubkey
rename to firmware/os/platform/stm32/misc/debug.pubkey
Binary files differ
diff --git a/firmware/misc/platform/stm32f4xx/m3debug.script b/firmware/os/platform/stm32/misc/m3debug.script
similarity index 100%
rename from firmware/misc/platform/stm32f4xx/m3debug.script
rename to firmware/os/platform/stm32/misc/m3debug.script
diff --git a/firmware/misc/platform/stm32f4xx/showsizes.sh b/firmware/os/platform/stm32/misc/showsizes.sh
similarity index 100%
rename from firmware/misc/platform/stm32f4xx/showsizes.sh
rename to firmware/os/platform/stm32/misc/showsizes.sh
diff --git a/firmware/src/platform/stm32f4xx/mpu.c b/firmware/os/platform/stm32/mpu.c
similarity index 98%
rename from firmware/src/platform/stm32f4xx/mpu.c
rename to firmware/os/platform/stm32/mpu.c
index 175657a..9a3c94f 100644
--- a/firmware/src/platform/stm32f4xx/mpu.c
+++ b/firmware/os/platform/stm32/mpu.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <plat/inc/bl.h>
+#include <plat/bl.h>
 #include <platform.h>
 #include <stdbool.h>
 #include <stdint.h>
diff --git a/firmware/src/platform/stm32f4xx/platform.c b/firmware/os/platform/stm32/platform.c
similarity index 98%
rename from firmware/src/platform/stm32f4xx/platform.c
rename to firmware/os/platform/stm32/platform.c
index 5cbe5a7..4e22f10 100644
--- a/firmware/src/platform/stm32f4xx/platform.c
+++ b/firmware/os/platform/stm32/platform.c
@@ -14,17 +14,17 @@
  * limitations under the License.
  */
 
-#include <cpu/inc/cpuMath.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/usart.h>
-#include <plat/inc/cmsis.h>
-#include <plat/inc/pwr.h>
-#include <plat/inc/rtc.h>
-#include <plat/inc/plat.h>
-#include <plat/inc/exti.h>
-#include <plat/inc/syscfg.h>
-#include <plat/inc/wdt.h>
-#include <plat/inc/dma.h>
+#include <cpu/cpuMath.h>
+#include <plat/gpio.h>
+#include <plat/usart.h>
+#include <plat/cmsis.h>
+#include <plat/pwr.h>
+#include <plat/rtc.h>
+#include <plat/plat.h>
+#include <plat/exti.h>
+#include <plat/syscfg.h>
+#include <plat/wdt.h>
+#include <plat/dma.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
@@ -43,7 +43,7 @@
 #include <hostIntf.h>
 #include <nanohubPacket.h>
 #include <sensType.h>
-#include <variant/inc/variant.h>
+#include <variant/variant.h>
 
 
 struct StmDbg {
diff --git a/firmware/src/platform/stm32f4xx/pwr.c b/firmware/os/platform/stm32/pwr.c
similarity index 98%
rename from firmware/src/platform/stm32f4xx/pwr.c
rename to firmware/os/platform/stm32/pwr.c
index e0e07cb..39510a5 100644
--- a/firmware/src/platform/stm32f4xx/pwr.c
+++ b/firmware/os/platform/stm32/pwr.c
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#include <cpu/inc/barrier.h>
-#include <plat/inc/cmsis.h>
-#include <plat/inc/pwr.h>
-#include <plat/inc/rtc.h>
+#include <cpu/barrier.h>
+#include <plat/cmsis.h>
+#include <plat/pwr.h>
+#include <plat/rtc.h>
 #include <reset.h>
 #include <stddef.h>
 
diff --git a/firmware/src/platform/stm32f4xx/rtc.c b/firmware/os/platform/stm32/rtc.c
similarity index 97%
rename from firmware/src/platform/stm32f4xx/rtc.c
rename to firmware/os/platform/stm32/rtc.c
index 33430a4..28acde1 100644
--- a/firmware/src/platform/stm32f4xx/rtc.c
+++ b/firmware/os/platform/stm32/rtc.c
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#include <cpu/inc/barrier.h>
-#include <cpu/inc/cpuMath.h>
-#include <plat/inc/rtc.h>
-#include <plat/inc/pwr.h>
-#include <inc/timer.h>
-#include <inc/platform.h>
-#include <plat/inc/exti.h>
-#include <plat/inc/cmsis.h>
-#include <variant/inc/variant.h>
+#include <cpu/barrier.h>
+#include <cpu/cpuMath.h>
+#include <plat/rtc.h>
+#include <plat/pwr.h>
+#include <timer.h>
+#include <platform.h>
+#include <plat/exti.h>
+#include <plat/cmsis.h>
+#include <variant/variant.h>
 
 #ifndef NS_PER_S
 #define NS_PER_S                    1000000000ULL
diff --git a/firmware/src/platform/stm32f4xx/spi.c b/firmware/os/platform/stm32/spi.c
similarity index 98%
rename from firmware/src/platform/stm32f4xx/spi.c
rename to firmware/os/platform/stm32/spi.c
index dc305a5..5f1915b 100644
--- a/firmware/src/platform/stm32f4xx/spi.c
+++ b/firmware/os/platform/stm32/spi.c
@@ -25,14 +25,14 @@
 #include <atomic.h>
 #include <platform.h>
 
-#include <plat/inc/cmsis.h>
-#include <plat/inc/dma.h>
-#include <plat/inc/gpio.h>
-#include <plat/inc/pwr.h>
-#include <plat/inc/exti.h>
-#include <plat/inc/syscfg.h>
-#include <plat/inc/spi.h>
-#include <plat/inc/plat.h>
+#include <plat/cmsis.h>
+#include <plat/dma.h>
+#include <plat/gpio.h>
+#include <plat/pwr.h>
+#include <plat/exti.h>
+#include <plat/syscfg.h>
+#include <plat/spi.h>
+#include <plat/plat.h>
 
 #define SPI_CR1_CPHA                (1 << 0)
 #define SPI_CR1_CPOL                (1 << 1)
diff --git a/firmware/os/platform/stm32/stm32.mk b/firmware/os/platform/stm32/stm32.mk
new file mode 100644
index 0000000..103ca3d
--- /dev/null
+++ b/firmware/os/platform/stm32/stm32.mk
@@ -0,0 +1,86 @@
+#
+# Copyright (C) 2016 The Android Open Source Project
+#
+# 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.
+#
+
+M3DEBUG ?= m3debug
+
+ifneq ($(CPU),cortexm4)
+        $(error "stm32 cplatform only supports Cortex-M4 CPUs")
+endif
+
+BL_FILE = $(OUT)/bl.unchecked.bin
+OS_FILE = $(OUT)/os.checked.bin
+
+DELIVERABLES += showsizes
+FLAGS += -I. -fno-unwind-tables -fstack-reuse=all -ffunction-sections -fdata-sections
+FLAGS += -Wl,--gc-sections -nostartfiles
+FLAGS_os += -nostdlib
+
+#platform bootloader
+SRCS_bl += os/platform/$(PLATFORM)/bl.c
+
+#platform runtime
+SRCS_os +=  os/platform/$(PLATFORM)/crt_$(PLATFORM).c
+
+#platform drivers
+SRCS_os += os/platform/$(PLATFORM)/platform.c \
+	os/platform/$(PLATFORM)/usart.c \
+	os/platform/$(PLATFORM)/gpio.c \
+	os/platform/$(PLATFORM)/pwr.c \
+	os/platform/$(PLATFORM)/wdt.c \
+	os/platform/$(PLATFORM)/i2c.c \
+	os/platform/$(PLATFORM)/exti.c \
+	os/platform/$(PLATFORM)/syscfg.c \
+	os/platform/$(PLATFORM)/spi.c \
+	os/platform/$(PLATFORM)/rtc.c \
+	os/platform/$(PLATFORM)/mpu.c \
+	os/platform/$(PLATFORM)/dma.c \
+	os/platform/$(PLATFORM)/crc.c \
+	os/platform/$(PLATFORM)/hostIntf.c \
+	os/platform/$(PLATFORM)/apInt.c \
+	os/platform/$(PLATFORM)/eeData.c
+
+
+#platform drivers for bootloader
+SRCS_bl += os/platform/$(PLATFORM)/pwr.c os/platform/$(PLATFORM)/gpio.c
+
+#extra deps
+DEPS += $(wildcard os/platform/$(PLATFORM)/inc/plat/*.h)
+DEPS += $(wildcard os/platform/$(PLATFORM)/inc/plat/cmsis/*.h)
+
+#linker script
+LKR_os = os/platform/$(PLATFORM)/lkr/$(CHIP).os.lkr
+LKR_bl = os/platform/$(PLATFORM)/lkr/$(CHIP).bl.lkr
+OSFLAGS_os += -Wl,-T $(LKR_os)
+OSFLAGS_bl += -Wl,-T $(LKR_bl)
+DEPS += $(LKR_os) $(LKR_bl)
+
+#platform flags
+PLATFORM_HAS_HARDWARE_CRC = true
+FLAGS += -DPLATFORM_HW_VER=0
+
+#platform-specific rules
+OBJCOPY_PARAMS = -I elf32-littlearm -O binary
+
+$(OUT)/bl.%.bin : $(OUT)/bl.%.elf
+	$(OBJCOPY) -j .bl -j .blrodata -j .eedata $(OBJCOPY_PARAMS) $< $@
+
+$(OUT)/os.%.bin : $(OUT)/os.%.elf
+	$(OBJCOPY) -j .data -j .text $(OBJCOPY_PARAMS) $< $@
+
+showsizes: $(OUT)/os.unchecked.elf
+	os/platform/$(PLATFORM)/misc/showsizes.sh $<
+
+$(info Included STM32 platfrom)
diff --git a/firmware/src/platform/stm32f4xx/syscfg.c b/firmware/os/platform/stm32/syscfg.c
similarity index 96%
rename from firmware/src/platform/stm32f4xx/syscfg.c
rename to firmware/os/platform/stm32/syscfg.c
index 27aa132..845bf0d 100644
--- a/firmware/src/platform/stm32f4xx/syscfg.c
+++ b/firmware/os/platform/stm32/syscfg.c
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#include <plat/inc/pwr.h>
-#include <plat/inc/syscfg.h>
+#include <plat/pwr.h>
+#include <plat/syscfg.h>
 
 #define SYSCFG_REG_SHIFT 2
 
diff --git a/firmware/src/platform/stm32f4xx/usart.c b/firmware/os/platform/stm32/usart.c
similarity index 97%
rename from firmware/src/platform/stm32f4xx/usart.c
rename to firmware/os/platform/stm32/usart.c
index ae20f9c..302cb85 100644
--- a/firmware/src/platform/stm32f4xx/usart.c
+++ b/firmware/os/platform/stm32/usart.c
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include <plat/inc/gpio.h>
-#include <plat/inc/usart.h>
-#include <plat/inc/pwr.h>
+#include <plat/gpio.h>
+#include <plat/usart.h>
+#include <plat/pwr.h>
 #include <usart.h>
 #include <gpio.h>
 
diff --git a/firmware/src/platform/stm32f4xx/wdt.c b/firmware/os/platform/stm32/wdt.c
similarity index 95%
rename from firmware/src/platform/stm32f4xx/wdt.c
rename to firmware/os/platform/stm32/wdt.c
index d29fcf1..b3c785f 100644
--- a/firmware/src/platform/stm32f4xx/wdt.c
+++ b/firmware/os/platform/stm32/wdt.c
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include <plat/inc/pwr.h>
-#include <plat/inc/wdt.h>
-#include <plat/inc/cmsis.h>
+#include <plat/pwr.h>
+#include <plat/wdt.h>
+#include <plat/cmsis.h>
 
 struct StmWwdg {
     volatile uint16_t CR;
diff --git a/firmware/src/variant/nucleo/i2c.c b/firmware/src/variant/nucleo/i2c.c
deleted file mode 100644
index 87278a4..0000000
--- a/firmware/src/variant/nucleo/i2c.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * 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 <plat/inc/i2c.h>
-#include <util.h>
-
-static const struct StmI2cBoardCfg mStmI2cBoardCfgs[] = {
-    [0] = {
-        .gpioScl = I2C1_GPIO_SCL_PB8,
-        .gpioSda = I2C1_GPIO_SDA_PB9,
-
-        .gpioPull = GPIO_PULL_NONE,
-
-        .dmaRx = I2C1_DMA_RX_CFG_A,
-        .dmaTx = I2C1_DMA_TX_CFG_A,
-
-        .sleepDev = Stm32sleepDevI2c1,
-    },
-};
-
-const struct StmI2cBoardCfg *boardStmI2cCfg(uint8_t busId)
-{
-    if (busId >= ARRAY_SIZE(mStmI2cBoardCfgs))
-        return NULL;
-
-    return &mStmI2cBoardCfgs[busId];
-}
diff --git a/firmware/variant/Android.mk b/firmware/variant/Android.mk
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/firmware/variant/Android.mk
diff --git a/firmware/variant/README b/firmware/variant/README
new file mode 100644
index 0000000..3cece9d
--- /dev/null
+++ b/firmware/variant/README
@@ -0,0 +1,2 @@
+for ease of source browsing this directory may contain soft links to variant directories located elsewhere;
+build scripts fully support such layout, however this is not necessary in order to build such variants.
diff --git a/firmware/variant/linux/Makefile b/firmware/variant/linux/Makefile
new file mode 100644
index 0000000..4dec406
--- /dev/null
+++ b/firmware/variant/linux/Makefile
@@ -0,0 +1,5 @@
+SRC_PATH=../..
+VARIANT_PATH=src/variant/linux
+VARIANT=linux
+
+include $(SRC_PATH)/src/variant/variant.mk
diff --git a/firmware/inc/variant/linux/variant.h b/firmware/variant/linux/inc/variant/variant.h
similarity index 89%
rename from firmware/inc/variant/linux/variant.h
rename to firmware/variant/linux/inc/variant/variant.h
index 092ce1c..2e6c280 100644
--- a/firmware/inc/variant/linux/variant.h
+++ b/firmware/variant/linux/inc/variant/variant.h
@@ -21,6 +21,11 @@
 extern "C" {
 #endif
 
+#define PLATFORM_HOST_INTF_I2C_BUS 0
+#define PLATFORM_HW_TYPE  0x8086
+
+#define PLAT_HAS_NO_U_TYPES_H
+
 //we have no LSE in linux
 #define HAVE_LSE    false
 
diff --git a/firmware/misc/variant/linux/Makefile b/firmware/variant/linux/linux.mk
similarity index 100%
rename from firmware/misc/variant/linux/Makefile
rename to firmware/variant/linux/linux.mk
diff --git a/firmware/variant/lunchbox/Makefile b/firmware/variant/lunchbox/Makefile
new file mode 100644
index 0000000..3079098
--- /dev/null
+++ b/firmware/variant/lunchbox/Makefile
@@ -0,0 +1,5 @@
+SRC_PATH=../..
+VARIANT_PATH=src/variant/lunchbox
+VARIANT=lunchbox
+
+include $(SRC_PATH)/src/variant/variant.mk
diff --git a/firmware/inc/variant/lunchbox/sensType.h b/firmware/variant/lunchbox/inc/variant/sensType.h
similarity index 100%
rename from firmware/inc/variant/lunchbox/sensType.h
rename to firmware/variant/lunchbox/inc/variant/sensType.h
diff --git a/firmware/inc/variant/lunchbox/variant.h b/firmware/variant/lunchbox/inc/variant/variant.h
similarity index 95%
rename from firmware/inc/variant/lunchbox/variant.h
rename to firmware/variant/lunchbox/inc/variant/variant.h
index dafe9f2..ae499f2 100644
--- a/firmware/inc/variant/lunchbox/variant.h
+++ b/firmware/variant/lunchbox/inc/variant/variant.h
@@ -21,6 +21,9 @@
 extern "C" {
 #endif
 
+#define PLATFORM_HW_TYPE  0x4C75   // 'Lu' -> lunchbox
+//#define DEBUG_UART_PIN    16       // GPIOB0 is debug uart at 2MBps
+
 #define VARIANT_VER                 0x00000000
 
 //we have LSE in lunchbox
diff --git a/firmware/variant/lunchbox/lunchbox.mk b/firmware/variant/lunchbox/lunchbox.mk
new file mode 100644
index 0000000..59c8010
--- /dev/null
+++ b/firmware/variant/lunchbox/lunchbox.mk
@@ -0,0 +1,65 @@
+#
+# Copyright (C) 2016 The Android Open Source Project
+#
+# 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.
+#
+
+#variant makefile for lunchbox
+
+ifneq ($(PLATFORM),stm32)
+        $(error "LUNCHBOX variant cannot be build on a platform that is not stm32")
+endif
+
+FLAGS += -DPLATFORM_HW_TYPE=0x4C75   #'Lu' -> lunchbox
+FLAGS += -DDEBUG_UART_PIN=16         #GPIOB0 is debug uart at 2MBps
+
+FLAGS += -I$(VARIANT_PATH)/inc
+
+#board configuration shims
+SRCS_os += $(VARIANT_PATH)/src/os/i2c.c
+SRCS_os += $(VARIANT_PATH)/src/os/spi.c
+#keys
+
+#drivers
+# ROHM ALS/prox
+SRCS_os += os/drivers/rohm_rpr0521/rohm_rpr0521.c
+
+# Fusion algorithms
+SRCS_os += os/algos/fusion.c os/algos/mat.c os/algos/quat.c os/algos/vec.c
+
+# BMI160 accel and gyro, BMM150 mag drivers
+FLAGS += -DUSE_BMM150 -DMAG_SLAVE_PRESENT
+SRCS_os += os/drivers/bosch_bmi160/bosch_bmi160.c \
+	os/drivers/bosch_bmi160/bosch_bmm150_slave.c \
+	os/algos/mag_cal.c \
+	os/algos/time_sync.c
+
+# Orientation sensor driver
+SRCS_os += os/drivers/orientation/orientation.c
+
+# Window orientation sensor driver
+SRCS_os += os/drivers/window_orientation/window_orientation.c
+
+# Bosch BMP280 Barometer/Temperature
+SRCS_os += os/drivers/bosch_bmp280/bosch_bmp280.c
+
+# Hall effect sensor driver
+SRCS_os += os/drivers/hall/hall.c
+
+# Camera Vsync driver
+SRCS_os += os/drivers/vsync/vsync.c
+
+# Tilt detection
+SRCS_os += os/drivers/tilt_detection/tilt_detection.c
+
+$(info included LUNCHBOX variant)
diff --git a/firmware/src/variant/lunchbox/i2c.c b/firmware/variant/lunchbox/src/os/i2c.c
similarity index 97%
rename from firmware/src/variant/lunchbox/i2c.c
rename to firmware/variant/lunchbox/src/os/i2c.c
index 87278a4..ec4c899 100644
--- a/firmware/src/variant/lunchbox/i2c.c
+++ b/firmware/variant/lunchbox/src/os/i2c.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <plat/inc/i2c.h>
+#include <plat/i2c.h>
 #include <util.h>
 
 static const struct StmI2cBoardCfg mStmI2cBoardCfgs[] = {
diff --git a/firmware/src/variant/lunchbox/spi.c b/firmware/variant/lunchbox/src/os/spi.c
similarity index 97%
rename from firmware/src/variant/lunchbox/spi.c
rename to firmware/variant/lunchbox/src/os/spi.c
index bbe986d..40457bd 100644
--- a/firmware/src/variant/lunchbox/spi.c
+++ b/firmware/variant/lunchbox/src/os/spi.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <plat/inc/spi.h>
+#include <plat/spi.h>
 #include <util.h>
 
 static const struct StmSpiBoardCfg mStmSpiBoardCfgs[] = {
diff --git a/firmware/variant/nucleo/Makefile b/firmware/variant/nucleo/Makefile
new file mode 100644
index 0000000..872c73e
--- /dev/null
+++ b/firmware/variant/nucleo/Makefile
@@ -0,0 +1,5 @@
+SRC_PATH=../..
+VARIANT_PATH=src/variant/nucleo
+VARIANT=nucleo
+
+include $(SRC_PATH)/src/variant/variant.mk
diff --git a/firmware/inc/variant/nucleo/sensType.h b/firmware/variant/nucleo/inc/variant/sensType.h
similarity index 100%
rename from firmware/inc/variant/nucleo/sensType.h
rename to firmware/variant/nucleo/inc/variant/sensType.h
diff --git a/firmware/inc/variant/nucleo/variant.h b/firmware/variant/nucleo/inc/variant/variant.h
similarity index 100%
rename from firmware/inc/variant/nucleo/variant.h
rename to firmware/variant/nucleo/inc/variant/variant.h
diff --git a/firmware/variant/nucleo/nucleo.mk b/firmware/variant/nucleo/nucleo.mk
new file mode 100644
index 0000000..af070ed
--- /dev/null
+++ b/firmware/variant/nucleo/nucleo.mk
@@ -0,0 +1,70 @@
+#
+# Copyright (C) 2016 The Android Open Source Project
+#
+# 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.
+#
+
+#variant makefile for nucleo
+
+
+ifneq ($(PLATFORM),stm32)
+        $(error "NUCLEO variant cannot be build on a platform that is not stm32")
+endif
+
+FLAGS += -DPLATFORM_HW_TYPE=0x4E75   #'Nu' ->nucleo
+FLAGS += -DDEBUG_SWD
+
+FLAGS += -I$(VARIANT_PATH)/inc
+
+#board configuration shims
+SRCS_os += $(VARIANT_PATH)/src/os/i2c.c
+SRCS_os += $(VARIANT_PATH)/src/os/spi.c
+
+#keys
+
+#drivers
+# AMS ALS/prox
+SRCS_os += os/drivers/ams_tmd2772/ams_tmd2772.c
+
+# ROHM ALS/prox
+SRCS_os += os/drivers/rohm_rpr0521/rohm_rpr0521.c
+
+# Fusion algorithm
+SRCS_os += os/algos/fusion.c os/algos/mat.c os/algos/quat.c os/algos/vec.c
+
+# BMI160 accel and gyro, BMM150 mag drivers
+FLAGS += -DUSE_BMM150 -DMAG_SLAVE_PRESENT
+SRCS_os += os/drivers/bosch_bmi160/bosch_bmi160.c \
+	os/drivers/bosch_bmi160/bosch_bmm150_slave.c \
+	os/algos/mag_cal.c \
+	os/algos/time_sync.c
+
+# Orientation sensor driver
+SRCS_os += os/drivers/orientation/orientation.c
+
+# Window orientation sensor driver
+SRCS_os += os/drivers/window_orientation/window_orientation.c
+
+# Bosch BMP280 Barometer/Temperature
+SRCS_os += os/drivers/bosch_bmp280/bosch_bmp280.c
+
+# Hall effect sensor driver
+SRCS_os += os/drivers/hall/hall.c
+
+# Camera Vsync driver
+SRCS_os += os/drivers/vsync/vsync.c
+
+# Tilt detection
+SRCS_os += os/drivers/tilt_detection/tilt_detection.c
+
+$(info included NUCLEO variant)
diff --git a/firmware/src/variant/lunchbox/i2c.c b/firmware/variant/nucleo/src/os/i2c.c
similarity index 97%
copy from firmware/src/variant/lunchbox/i2c.c
copy to firmware/variant/nucleo/src/os/i2c.c
index 87278a4..ec4c899 100644
--- a/firmware/src/variant/lunchbox/i2c.c
+++ b/firmware/variant/nucleo/src/os/i2c.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <plat/inc/i2c.h>
+#include <plat/i2c.h>
 #include <util.h>
 
 static const struct StmI2cBoardCfg mStmI2cBoardCfgs[] = {
diff --git a/firmware/src/variant/nucleo/spi.c b/firmware/variant/nucleo/src/os/spi.c
similarity index 97%
rename from firmware/src/variant/nucleo/spi.c
rename to firmware/variant/nucleo/src/os/spi.c
index 6a69452..d75824c 100644
--- a/firmware/src/variant/nucleo/spi.c
+++ b/firmware/variant/nucleo/src/os/spi.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <plat/inc/spi.h>
+#include <plat/spi.h>
 #include <util.h>
 
 static const struct StmSpiBoardCfg mStmSpiBoardCfgs[] = {
diff --git a/firmware/variant/variant.mk b/firmware/variant/variant.mk
new file mode 100644
index 0000000..340f91c
--- /dev/null
+++ b/firmware/variant/variant.mk
@@ -0,0 +1,22 @@
+PLATFORM?=stm32
+CPU?=cortexm4
+CHIP?=stm32f411
+OUT?=out
+
+# SRC_PATH points to "firmware"; TOP_PATH is the abs path to top of andrlid tree
+TOP_RELPATH := ../../../..
+TOP_ABSPATH := $(realpath $(SRC_PATH)/$(TOP_RELPATH))
+VARIANT_ABSPATH := $(TOP_ABSPATH)/$(VARIANT_PATH)
+VARIANT_PATH := $(TOP_RELPATH)/$(VARIANT_PATH)
+
+all:
+	make -C $(SRC_PATH) -f firmware.mk VARIANT=$(VARIANT) VARIANT_PATH=$(VARIANT_PATH) OUT=$(VARIANT_PATH)/$(OUT) PLATFORM=$(PLATFORM) CPU=$(CPU) CHIP=$(CHIP) $(EXTRA_ARGS)
+ifdef IMAGE_OUT
+	cd $(VARIANT_ABSPATH) && cp $(OUT)/full.bin $(IMAGE_OUT)
+endif
+
+clean:
+	rm -rf $(OUT)
+ifdef IMAGE_OUT
+	rm $(IMAGE_OUT)
+endif
diff --git a/lib/Android.mk b/lib/Android.mk
new file mode 100644
index 0000000..a39d13f
--- /dev/null
+++ b/lib/Android.mk
@@ -0,0 +1,53 @@
+#
+# Copyright (C) 2016 The Android Open Source Project
+#
+# 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+src_files := \
+    nanohub/aes.c \
+    nanohub/rsa.c \
+    nanohub/sha2.c \
+    nanohub/softcrc.c \
+
+src_includes := \
+    $(LOCAL_PATH)/include \
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libnanohub_common
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := \
+    $(src_files) \
+    nanohub/nanoapp.c \
+
+LOCAL_CFLAGS := \
+    -DHOST_BUILD \
+    -DRSA_SUPPORT_PRIV_OP_BIGRAM \
+
+LOCAL_C_INCLUDES := \
+    $(src_includes)
+
+LOCAL_EXPORT_C_INCLUDE_DIRS := \
+    $(src_includes)
+
+LOCAL_MULTILIB := both
+
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+include $(call first-makefiles-under, $(LOCAL_PATH))
+
+src_files :=
+src_includes :=
diff --git a/lib/include/nanohub/crc.h b/lib/include/nanohub/crc.h
index 96b7e4c..35d716e 100644
--- a/lib/include/nanohub/crc.h
+++ b/lib/include/nanohub/crc.h
@@ -38,4 +38,8 @@
  */
 uint32_t crc32(const void *buf, size_t size, uint32_t crc);
 
+/* concrete public implementation of crc32() w/o HW acceleration */
+uint32_t soft_crc32(const void *buf, size_t size, uint32_t crc);
+
+
 #endif /* _NANOHUB_CRC_H_ */
diff --git a/lib/nanohub/softcrc.c b/lib/nanohub/softcrc.c
index fb2b711..286e36b 100644
--- a/lib/nanohub/softcrc.c
+++ b/lib/nanohub/softcrc.c
@@ -29,7 +29,7 @@
 
 static uint32_t crcOneWord(uint32_t crc, uint32_t data, int cnt)
 {
-        uint32_t i;
+        int i;
 
         crc = crc ^ data;
         for (i = 0; i < cnt; i++)
@@ -38,11 +38,12 @@
         return crc;
 }
 
-uint32_t crc32(const void *buf, size_t size, uint32_t crc)
+uint32_t soft_crc32(const void *buf, size_t size, uint32_t crc)
 {
         const uint32_t *data32 = (const uint32_t *)buf;
         const uint8_t *data8;
-        uint32_t word, i;
+        uint32_t word;
+        size_t i;
 
         // word by word crc32
         for (i = 0; i < size / 4; i++)
diff --git a/sensorhal/Android.mk b/sensorhal/Android.mk
index c34f0e9..a2fb0a9 100644
--- a/sensorhal/Android.mk
+++ b/sensorhal/Android.mk
@@ -56,7 +56,7 @@
 LOCAL_CFLAGS += $(COMMON_CFLAGS)
 
 LOCAL_C_INCLUDES += \
-	device/google/contexthub/firmware/inc \
+	device/google/contexthub/firmware/os/inc \
 	device/google/contexthub/util/common
 
 LOCAL_SRC_FILES := \
@@ -83,7 +83,7 @@
 LOCAL_CFLAGS += $(COMMON_CFLAGS)
 
 LOCAL_C_INCLUDES += \
-	device/google/contexthub/firmware/inc \
+	device/google/contexthub/firmware/os/inc \
 	device/google/contexthub/util/common
 
 LOCAL_SRC_FILES := \
@@ -121,7 +121,7 @@
 endif
 
 LOCAL_C_INCLUDES += \
-	device/google/contexthub/firmware/inc \
+	device/google/contexthub/firmware/os/inc \
 	device/google/contexthub/util/common
 
 LOCAL_SRC_FILES := \
diff --git a/util/nanoapp_cmd/Android.mk b/util/nanoapp_cmd/Android.mk
index b9eb493..8dcd8ac 100644
--- a/util/nanoapp_cmd/Android.mk
+++ b/util/nanoapp_cmd/Android.mk
@@ -19,7 +19,7 @@
 
 LOCAL_SRC_FILES:= nanoapp_cmd.c
 
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../firmware/inc
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../firmware/os/inc
 LOCAL_CFLAGS := -Wall -Werror -Wextra
 
 LOCAL_MODULE:= nanoapp_cmd
diff --git a/util/nanoapp_encr/Android.mk b/util/nanoapp_encr/Android.mk
index 4d64acc..84444e1 100644
--- a/util/nanoapp_encr/Android.mk
+++ b/util/nanoapp_encr/Android.mk
@@ -20,9 +20,6 @@
 
 
 LOCAL_SRC_FILES := \
-    ../../lib/nanohub/aes.c \
-    ../../lib/nanohub/sha2.c \
-    ../../lib/nanohub/nanoapp.c \
     nanoapp_encr.c \
 
 
@@ -34,9 +31,7 @@
         -DBOOTLOADER= \
         -DBOOTLOADER_RO= \
 
-
-LOCAL_C_INCLUDES := \
-        device/google/contexthub/lib/include \
+LOCAL_STATIC_LIBRARIES := libnanohub_common
 
 LOCAL_MODULE := nanoapp_encr
 
diff --git a/util/nanoapp_postprocess/Android.mk b/util/nanoapp_postprocess/Android.mk
index b20deec..dce1711 100644
--- a/util/nanoapp_postprocess/Android.mk
+++ b/util/nanoapp_postprocess/Android.mk
@@ -24,15 +24,20 @@
 
 LOCAL_SRC_FILES := \
     postprocess.c \
-    ../../lib/nanohub/nanoapp.c \
 
 LOCAL_CFLAGS := -Wall -Werror -Wextra
 
-LOCAL_C_INCLUDES += \
-    device/google/contexthub/lib/include \
+LOCAL_STATIC_LIBRARIES := libnanohub_common
 
 LOCAL_MODULE := nanoapp_postprocess
 
+# libelf needed for ELF parsing support, libz required by libelf
+LOCAL_STATIC_LIBRARIES += libelf libz
+
+# Statically linking libc++ so this binary can be copied out of the tree and
+# still work (needed by dependencies)
+LOCAL_CXX_STL := libc++_static
+
 LOCAL_MODULE_TAGS := optional
 
 include $(BUILD_HOST_EXECUTABLE)
diff --git a/util/nanoapp_sign/Android.mk b/util/nanoapp_sign/Android.mk
index 81e204a..7be5197 100644
--- a/util/nanoapp_sign/Android.mk
+++ b/util/nanoapp_sign/Android.mk
@@ -19,12 +19,8 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := \
-    ../../lib/nanohub/rsa.c \
-    ../../lib/nanohub/sha2.c \
-    ../../lib/nanohub/nanoapp.c \
     nanoapp_sign.c \
 
-
 LOCAL_CFLAGS := \
     -Wall \
     -Werror \
@@ -35,13 +31,12 @@
     -DBOOTLOADER_RO= \
 
 
-LOCAL_C_INCLUDES := \
-    device/google/contexthub/lib/include \
-
 ifneq ($(filter userdebug eng,$(TARGET_BUILD_VARIANT)),)
 LOCAL_CFLAGS += -DDEBUG_KEYS
 endif
 
+LOCAL_STATIC_LIBRARIES := libnanohub_common
+
 LOCAL_MODULE := nanoapp_sign
 
 LOCAL_MODULE_TAGS := optional