Skip to content

Commit cc1ef76

Browse files
committed
esp8266/scripts/flashbdev: Correct bootloader flash size to match real size.
Flash size as seen by vendor SDK doesn't depend on real size, but rather on a particular value in firmware header, as put there by flash tool. That means it's user responsibility to know what flash size a particular device has, and specify correct parameters during flashing. That's not end user friendly however, so we try to make it "flash and play" by detecting real size vs from-header size mismatch, and correct the header accordingly.
1 parent 5844068 commit cc1ef76

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

esp8266/scripts/flashbdev.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,40 @@ def ioctl(self, op, arg):
2626
if op == 5: # BP_IOCTL_SEC_SIZE
2727
return self.SEC_SIZE
2828

29+
def set_bl_flash_size(real_size):
30+
if real_size == 256*1024:
31+
code = 1
32+
elif real_size == 512*1024:
33+
code = 0
34+
elif real_size == 1024*1024:
35+
code = 2
36+
elif real_size == 2048*1024:
37+
code = 3
38+
elif real_size == 4096*1024:
39+
code = 4
40+
else:
41+
code = 2
42+
buf = bytearray(4096)
43+
esp.flash_read(0, buf)
44+
buf[3] = (buf[3] & 0xf) | (code << 4)
45+
esp.flash_erase(0)
46+
esp.flash_write(0, buf)
47+
48+
# If bootloader size ID doesn't correspond to real Flash size,
49+
# fix bootloader value and reboot.
50+
size = esp.flash_id() >> 16
51+
# Check that it looks like realistic power of 2 for flash sizes
52+
# commonly used with esp8266
53+
if 22 >= size >= 18:
54+
size = 1 << size
55+
if size != esp.flash_size():
56+
import machine
57+
import time
58+
print("Bootloader Flash size appear to have been set incorrectly, trying to fix")
59+
set_bl_flash_size(size)
60+
machine.reset()
61+
while 1: time.sleep(1)
62+
2963
if esp.flash_size() < 1024*1024:
3064
bdev = None
3165
else:

0 commit comments

Comments
 (0)