---
 cores/esp8266/Esp.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp
index f4c0b220..c5c32f74 100644
--- a/cores/esp8266/Esp.cpp
+++ b/cores/esp8266/Esp.cpp
@@ -501,7 +501,21 @@ bool EspClass::flashEraseSector(uint32_t sector) {
 
 bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
     ets_isr_mask(FLASH_INT_MASK);
-    int rc = spi_flash_write(offset, (uint32_t*) data, size);
+    int rc;
+    uint32_t* ptr = data;
+#ifdef FLASH_QUIRK_WRITE_0_TO_1
+    static uint32_t read_buf[SPI_FLASH_SEC_SIZE / 4];
+    rc = spi_flash_read(offset, read_buf, size);
+    if (rc != 0) {
+        ets_isr_unmask(FLASH_INT_MASK);
+        return false;
+    }
+    for (size_t i = 0; i < size / 4; ++i) {
+        read_buf[i] &= data[i];
+    }
+    ptr = read_buf;
+#endif // FLASH_QUIRK_WRITE_0_TO_1
+    rc = spi_flash_write(offset, ptr, size);
     ets_isr_unmask(FLASH_INT_MASK);
     return rc == 0;
 }
-- 
