diff --git a/esp8266/Makefile b/esp8266/Makefile
index 1586c576cad302800e81faa3030315fa6ce88a18..c57206c4dca92bb17d7b88953877fe0ee38bae69 100644
--- a/esp8266/Makefile
+++ b/esp8266/Makefile
@@ -65,6 +65,7 @@ SRC_C = \
 	main.c \
 	help.c \
 	esp_mphal.c \
+	esp_init_data.c \
 	gccollect.c \
 	lexerstr32.c \
 	uart.c \
diff --git a/esp8266/esp8266.ld b/esp8266/esp8266.ld
index c726790d3828dafd8b4d2fcbc73a380df09aecbd..5251d168156d82e8d11fe58ee8feefd4b82c0543 100644
--- a/esp8266/esp8266.ld
+++ b/esp8266/esp8266.ld
@@ -20,7 +20,7 @@ PHDRS
     irom0_0_phdr PT_LOAD;
 }
 
-ENTRY(call_user_start)
+ENTRY(firmware_start)
 EXTERN(_DebugExceptionVector)
 EXTERN(_DoubleExceptionVector)
 EXTERN(_KernelExceptionVector)
diff --git a/esp8266/esp_init_data.c b/esp8266/esp_init_data.c
new file mode 100644
index 0000000000000000000000000000000000000000..b229f751dade0434c68c757b95b5eed7d28011d2
--- /dev/null
+++ b/esp8266/esp_init_data.c
@@ -0,0 +1,80 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Paul Sokolovsky
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include "ets_sys.h"
+#include "etshal.h"
+#include "esp_mphal.h"
+#include "user_interface.h"
+#include "extmod/misc.h"
+
+uint32_t SPIRead(uint32_t offset, void *buf, uint32_t len);
+uint32_t SPIWrite(uint32_t offset, const void *buf, uint32_t len);
+uint32_t SPIEraseSector(int sector);
+NORETURN void call_user_start(void);
+void ets_printf(const char *fmt, ...);
+extern char flashchip;
+
+static const uint8_t default_init_data[] __attribute__((aligned(4))) = {
+0x05, 0x00, 0x04, 0x02, 0x05, 0x05, 0x05, 0x02, 0x05, 0x00, 0x04, 0x05, 0x05, 0x04, 0x05, 0x05,
+0x04, 0xfe, 0xfd, 0xff, 0xf0, 0xf0, 0xf0, 0xe0, 0xe0, 0xe0, 0xe1, 0x0a, 0xff, 0xff, 0xf8, 0x00,
+0xf8, 0xf8, 0x52, 0x4e, 0x4a, 0x44, 0x40, 0x38, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05,
+0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0xe1, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x93, 0x43, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+void firmware_start(void) {
+    // For SDK 1.5.2, either address has shifted and not mirrored in
+    // eagle.rom.addr.v6.ld, or extra initial member was added.
+    SpiFlashChip *flash = (SpiFlashChip*)(&flashchip + 4);
+
+    char buf[128];
+    SPIRead(flash->chip_size - 4 * 0x1000, buf, sizeof(buf));
+    /*for (int i = 0; i < sizeof(buf); i++) {
+        static char hexf[] = "%x ";
+        ets_printf(hexf, buf[i]);
+    }*/
+
+    bool inited = false;
+    for (int i = 0; i < sizeof(buf); i++) {
+        if (buf[i] != 0xff) {
+            inited = true;
+            break;
+        }
+    }
+
+    if (!inited) {
+        static char msg[] = "Writing init data\n";
+        ets_printf(msg);
+        SPIRead((uint32_t)&default_init_data - 0x40200000, buf, sizeof(buf));
+        SPIWrite(flash->chip_size - 4 * 0x1000, buf, sizeof(buf));
+    }
+
+    asm("j call_user_start");
+}