From 6104a25f1f3f91cc57c861c5c799bc1fc94961f1 Mon Sep 17 00:00:00 2001
From: Fredrik Fornstad <fredrik.fornstad@gmail.com>
Date: Fri, 14 Sep 2018 19:56:18 +0200
Subject: [PATCH] Fix QTA erase after factory partition update

---
 include/libwebsockets/lws-esp32.h | 2 +-
 lib/plat/esp32/esp32-helpers.c    | 6 ++----
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/include/libwebsockets/lws-esp32.h b/include/libwebsockets/lws-esp32.h
index e11d735e..2d1c0f42 100644
--- a/include/libwebsockets/lws-esp32.h
+++ b/include/libwebsockets/lws-esp32.h
@@ -109,7 +109,7 @@ static LWS_INLINE void uv_close(uv_handle_t *h, void *v)
 #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY 0xb00bcafe
 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
-
+#define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY_ERASE_OTA 0xfac0eeee
 
 /* user code provides these */
 
diff --git a/lib/plat/esp32/esp32-helpers.c b/lib/plat/esp32/esp32-helpers.c
index 9bd4cd4e..11eda871 100644
--- a/lib/plat/esp32/esp32-helpers.c
+++ b/lib/plat/esp32/esp32-helpers.c
@@ -1032,14 +1032,12 @@ lws_esp_ota_get_boot_partition(void)
 	                	}
 			}
 
-			/* destroy our OTA image header */
-			spi_flash_erase_range(ota->address, 4096);
-
 			/*
-			 * with no viable OTA image, we will come back up in
+			 * We send a message to the bootloader to erase the OTA header, we will come back up in
 			 * factory where the user can reload the OTA image
 			 */
 			lwsl_notice("  FACTORY copy successful, rebooting\n");
+			lws_esp32_restart_guided(LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY_ERASE_OTA);
 retry:
 			esp_restart();
 		}
-- 
GitLab