Project import
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..8d8fd09
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,104 @@
+#
+#    Copyright (c) 2010-2015 Nest Labs, Inc.
+#    All rights reserved.
+#
+#    This document is the property of Nest. It is considered
+#    confidential and proprietary information.
+#
+#    This document may not be reproduced or transmitted in any form,
+#    in whole or in part, without the express written permission of
+#    Nest.
+#
+#    Description:
+#      This file is the makefile for the STM32 flash utility.
+#
+
+BuildConfigSpecialized	:= No
+BuildProductSpecialized	:= No
+
+include pre.mak
+
+PackageRoot		:= .
+
+PackageName		:= stm32flash
+
+PackageExtension    := tar.gz
+PackageSeparator	:=
+
+PackagePatchArgs    := -p1
+
+PackageArchive		:= $(PackageName).$(PackageExtension)
+PackageSourceDir	:= $(PackageRoot)/$(PackageName)
+
+PackageBuildMakefile	= $(call GenerateBuildPaths,Makefile)
+
+LicenseSourceFile	:= $(PackageSourceDir)/gpl-2.0.txt
+
+CleanPaths		+= $(PackageLicenseFile)
+
+ExecutableName		:= $(PackageName)
+
+all: $(PackageDefaultGoal)
+
+# Generate the package license contents.
+
+$(LicenseSourceFile): source
+
+$(PackageLicenseFile): $(LicenseSourceFile)
+	$(copy-result)
+
+# Extract the source from the archive and apply patches, if any.
+
+$(PackageSourceDir): $(PackageArchive) $(PackagePatchPaths)
+	echo $(PackageArchive) $(PackagePatchPaths)
+	$(expand-and-patch-package)
+
+# Prepare the sources.
+
+.PHONY: source
+source: | $(PackageSourceDir)
+
+# Patch the sources, if necessary.
+
+.PHONY: patch
+patch: source
+
+# Generate the package build makefile.
+
+$(PackageBuildMakefile): | $(PackageSourceDir) $(BuildDirectory)
+	$(call create-links,$(CURDIR)/$(PackageSourceDir),$(BuildDirectory))
+
+# Configure the source for building.
+
+.PHONY: configure
+configure: source $(PackageBuildMakefile)
+
+# Build the source.
+#
+# We have to unset MAKEFLAGS since they confuse the package build otherwise.
+
+.PHONY: build
+build: configure
+	$(Verbose)unset MAKEFLAGS && \
+	$(MAKE) $(JOBSFLAG) -C $(BuildDirectory) \
+	INSTALL="$(INSTALL) $(INSTALLFLAGS)" \
+	CC="$(CC)" CXX="$(CXX)" AR=$(AR) NM=$(NM) RANLIB=$(RANLIB) STRIP=$(STRIP) \
+	all
+
+# Stage the build to a temporary installation area.
+#
+# We have to unset MAKEFLAGS since they confuse the package build otherwise.
+
+.PHONY: stage
+stage: build | $(ResultDirectory)
+	$(Verbose)unset MAKEFLAGS && \
+	$(MAKE) $(JOBSFLAG) -C $(BuildDirectory) \
+	INSTALL="$(INSTALL) $(INSTALLFLAGS)" \
+	DESTDIR=$(ResultDirectory) \
+	install
+
+clean:
+	$(Verbose)$(RM) $(RMFLAGS) -r $(BuildDirectory)
+	$(Verbose)$(RM) $(RMFLAGS) -r $(ResultDirectory)
+
+include post.mak
diff --git a/stm32flash.patches/stm32flash-50.description b/stm32flash.patches/stm32flash-50.description
new file mode 100644
index 0000000..4c15407
--- /dev/null
+++ b/stm32flash.patches/stm32flash-50.description
@@ -0,0 +1,7 @@
+sw/tps/stm32flash-0.4/stm32flash/dev_table.c
+  Minor tweaks to Diamond MCU entries in devices[].
+sw/tps/stm32flash-0.4/stm32flash/stm32.c
+  Detect MCUs that do not support mass erase and erase the whole flash before writing image.
+  Only Diamond MCUs are flagged as incapable of mass erase for now.
+sw/tps/stm32flash-0.4/stm32flash/stm32.h
+  Prototype change for stm32_erase_memory().
diff --git a/stm32flash.patches/stm32flash-50.patch b/stm32flash.patches/stm32flash-50.patch
new file mode 100644
index 0000000..e0f95ba
--- /dev/null
+++ b/stm32flash.patches/stm32flash-50.patch
@@ -0,0 +1,86 @@
+diff -aruN stm32flash.orig/dev_table.c stm32flash/dev_table.c
+--- stm32flash.orig/dev_table.c	2014-10-03 14:35:08.000000000 -0700
++++ stm32flash/dev_table.c	2015-02-02 17:25:20.752854394 -0800
+@@ -57,9 +57,10 @@
+ 	/* L0 */
+ 	{0x417, "L05xxx/06xxx"      , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 32,  128, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000},
+ 	/* L1 */
+-	{0x416, "L1xxx6(8/B)"       , 0x20000800, 0x20004000, 0x08000000, 0x08020000, 16,  256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000},
+-	{0x429, "L1xxx6(8/B)A"      , 0x20001000, 0x20008000, 0x08000000, 0x08020000, 16,  256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000},
+-	{0x427, "L1xxxC"            , 0x20001000, 0x20008000, 0x08000000, 0x08020000, 16,  256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF02000},
++	{0x416, "Medium-density ULP", 0x20000800, 0x20004000, 0x08000000, 0x08020000, 16,  256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000},
++	{0x429, "Medium-density ULP Shrink", 0x20000800, 0x20004000, 0x08000000, 0x08020000, 16,  256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000},
++	{0x427, "Medium+-density ULP", 0x20001000, 0x20008000, 0x08000000, 0x08040000, 16,  256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000},
++
+ 	{0x436, "L1xxxD"            , 0x20001000, 0x2000C000, 0x08000000, 0x08060000, 16,  256, 0x1ff80000, 0x1ff8000F, 0x1FF00000, 0x1FF02000},
+ 	{0x437, "L1xxxE"            , 0x20001000, 0x20014000, 0x08000000, 0x08060000, 16,  256, 0x1ff80000, 0x1ff8000F, 0x1FF00000, 0x1FF02000},
+ 	/* These are not (yet) in AN2606: */
+diff -aruN stm32flash.orig/stm32.c stm32flash/stm32.c
+--- stm32flash.orig/stm32.c	2014-10-02 00:20:26.000000000 -0700
++++ stm32flash/stm32.c	2015-02-02 17:25:20.752854394 -0800
+@@ -96,6 +96,7 @@
+ static const uint32_t stm_reset_code_length = sizeof(stm_reset_code);
+ 
+ extern const stm32_dev_t devices[];
++extern char	reset_flag;
+ 
+ static void stm32_warn_stretching(const char *f)
+ {
+@@ -698,7 +699,16 @@
+ 	return STM32_ERR_OK;
+ }
+ 
+-stm32_err_t stm32_erase_memory(const stm32_t *stm, uint8_t spage, uint8_t pages)
++int supports_mass_erase(uint16_t chip_id)
++{
++	int retval = 1;
++	if(chip_id == 0x416 || chip_id == 0x417 || chip_id == 0x427 || chip_id == 0x429) {
++		retval = 0;
++	}
++	return retval;
++}
++
++stm32_err_t stm32_erase_memory(const stm32_t *stm, uint16_t spage, uint16_t pages)
+ {
+ 	struct port_interface *port = stm->port;
+ 	stm32_err_t s_err;
+@@ -725,8 +735,11 @@
+ 		/* Currently known as not supporting mass erase is the Ultra Low Power STM32L15xx range */
+ 		/* So if someone has not overridden the default, but uses one of these chips, take it out of */
+ 		/* mass erase mode, so it will be done page by page. This maximum might not be correct either! */
+-		if (stm->pid == 0x416 && pages == 0xFF)
++
++		if (!supports_mass_erase(stm->pid) && pages == 0xFF) {
+ 			pages = 0xF8; /* works for the STM32L152RB with 128Kb flash */
++			reset_flag	= 1; /* To mimic earlier behavior*/
++		}
+ 
+ 		if (pages == 0xFF) {
+ 			uint8_t buf[3];
+@@ -756,6 +769,12 @@
+ 		uint8_t *buf;
+ 		int i = 0;
+ 
++		if (pages == 0xF8) {
++			/* Erase the whole flash */
++			pages = (stm->dev->fl_end - stm->dev->fl_start) / stm->dev->fl_ps;
++			fprintf(stderr, "Erase 0x%x pages\n", pages);
++		}
++
+ 		buf = malloc(2 + 2 * pages + 1);
+ 		if (!buf)
+ 			return STM32_ERR_UNKNOWN;
+diff -aruN stm32flash.orig/stm32.h stm32flash/stm32.h
+--- stm32flash.orig/stm32.h	2014-10-02 00:20:26.000000000 -0700
++++ stm32flash/stm32.h	2015-02-02 17:25:20.752854394 -0800
+@@ -68,8 +68,8 @@
+ 			       const uint8_t data[], unsigned int len);
+ stm32_err_t stm32_wunprot_memory(const stm32_t *stm);
+ stm32_err_t stm32_wprot_memory(const stm32_t *stm);
+-stm32_err_t stm32_erase_memory(const stm32_t *stm, uint8_t spage,
+-			       uint8_t pages);
++stm32_err_t stm32_erase_memory(const stm32_t *stm, uint16_t spage,
++			       uint16_t pages);
+ stm32_err_t stm32_go(const stm32_t *stm, uint32_t address);
+ stm32_err_t stm32_reset_device(const stm32_t *stm);
+ stm32_err_t stm32_readprot_memory(const stm32_t *stm);
diff --git a/stm32flash.tar.gz b/stm32flash.tar.gz
new file mode 100644
index 0000000..b7ff44f
--- /dev/null
+++ b/stm32flash.tar.gz
Binary files differ
diff --git a/stm32flash.url b/stm32flash.url
new file mode 100644
index 0000000..deed025
--- /dev/null
+++ b/stm32flash.url
@@ -0,0 +1 @@
+http://releases.stm32flash.googlecode.com/git/stm32flash-0.4.tar.gz
diff --git a/stm32flash.version b/stm32flash.version
new file mode 100644
index 0000000..bd73f47
--- /dev/null
+++ b/stm32flash.version
@@ -0,0 +1 @@
+0.4