From dec71bb39586a339408737672c642f60f6cb93a7 Mon Sep 17 00:00:00 2001
From: Alexandre Piel <alexandre.piel@zooplus.com>
Date: Thu, 26 Jul 2018 16:59:08 +0200
Subject: [PATCH] fix http ota

---
 extras/http_client_ota/http_client_ota.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/extras/http_client_ota/http_client_ota.c b/extras/http_client_ota/http_client_ota.c
index f7a2be9..ebcc985 100644
--- a/extras/http_client_ota/http_client_ota.c
+++ b/extras/http_client_ota/http_client_ota.c
@@ -68,17 +68,21 @@ static unsigned int ota_firmaware_dowload_callback(char *buf, uint16_t size)
         return -1;
     }
 
-    // Ready for flash device, the erase NANDFLASH Block
-    if (flash_offset % SECTOR_SIZE == 0) {
-        unsigned int sector;
-
-        sector = flash_offset / SECTOR_SIZE;
-        sdk_spi_flash_erase_sector(sector);
+    int offset = 0;
+    if(size && ((uint32_t)buf % 4)) {
+        // sdk_spi_flash_write requires a word aligned
+        // Assuming size is always a multiple of 4 bytes.
+        uint32_t first_word;
+        memcpy(&first_word, buf, 4);
+        sdk_spi_flash_write(flash_offset, &first_word, 4);
+        memmove(LWIP_MEM_ALIGN(buf),&buf[1],size-4);
+        buf = (uint32_t *)LWIP_MEM_ALIGN(buf);
+        offset += 4;
+        size -= 4;
     }
-
-    // Write into Flash
     sdk_spi_flash_write(flash_offset, (uint32_t *) buf, size);
     flash_offset += size;
+
     return 1;
 }