From 40f5ecd3a82e810203b39131cb61474f101de58e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 12 Apr 2016 00:37:04 +0300 Subject: esp8266: Add Python modules for initial configuration. Main entry point is _boot.py which checks whether FAT FS in flash mountable, and if so, mounts it. Otherwise, it checks if flash is empty, and if so, performs initial module setup: makes FAT FS, configures default AP name, etc. As a last option, if flash is not empty, and could not be mounted, it means filesystem corruption, and warning message with instructions is printed in an infinite loop. --- esp8266/scripts/flashbdev.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 esp8266/scripts/flashbdev.py (limited to 'esp8266/scripts/flashbdev.py') diff --git a/esp8266/scripts/flashbdev.py b/esp8266/scripts/flashbdev.py new file mode 100644 index 0000000000..1d120b920d --- /dev/null +++ b/esp8266/scripts/flashbdev.py @@ -0,0 +1,29 @@ +import esp + +class FlashBdev: + + SEC_SIZE = 4096 + START_SEC = 0xa0000 // SEC_SIZE + NUM_BLK = 64 + + def __init__(self, blocks=NUM_BLK): + self.blocks = blocks + + def readblocks(self, n, buf): + print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) + esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf) + + def writeblocks(self, n, buf): + print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf))) + assert len(buf) <= self.SEC_SIZE, len(buf) + esp.flash_erase(n + self.START_SEC) + esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf) + + def ioctl(self, op, arg): + print("ioctl(%d, %r)" % (op, arg)) + if op == 4: # BP_IOCTL_SEC_COUNT + return self.blocks + if op == 5: # BP_IOCTL_SEC_SIZE + return self.SEC_SIZE + +bdev = FlashBdev() -- cgit v1.2.3 From 3a5a35aaeccfc03a45771b0a6f613d8ff25cd25f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 18 Apr 2016 01:23:04 +0300 Subject: esp8266/scripts/flashbdev: Use all available space in 1MB FlashROM for FS. --- esp8266/scripts/flashbdev.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'esp8266/scripts/flashbdev.py') diff --git a/esp8266/scripts/flashbdev.py b/esp8266/scripts/flashbdev.py index 1d120b920d..09223d8e7e 100644 --- a/esp8266/scripts/flashbdev.py +++ b/esp8266/scripts/flashbdev.py @@ -3,8 +3,8 @@ import esp class FlashBdev: SEC_SIZE = 4096 - START_SEC = 0xa0000 // SEC_SIZE - NUM_BLK = 64 + START_SEC = 0x89000 // SEC_SIZE + NUM_BLK = 0x73 def __init__(self, blocks=NUM_BLK): self.blocks = blocks -- cgit v1.2.3 From 8dcce9260614aca9f89a346a31ca47e2b1288398 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 18 Apr 2016 17:14:00 +0300 Subject: esp8266/scripts: Don't try to create filesystem on 512KB devices or less. There's no space for it. --- esp8266/scripts/_boot.py | 3 ++- esp8266/scripts/flashbdev.py | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'esp8266/scripts/flashbdev.py') diff --git a/esp8266/scripts/_boot.py b/esp8266/scripts/_boot.py index 2b261fb475..7e38f03236 100644 --- a/esp8266/scripts/_boot.py +++ b/esp8266/scripts/_boot.py @@ -3,7 +3,8 @@ import builtins from flashbdev import bdev try: - vfs = uos.VfsFat(bdev, "") + if bdev: + vfs = uos.VfsFat(bdev, "") except OSError: import inisetup inisetup.check_bootsec() diff --git a/esp8266/scripts/flashbdev.py b/esp8266/scripts/flashbdev.py index 09223d8e7e..f1e85d923b 100644 --- a/esp8266/scripts/flashbdev.py +++ b/esp8266/scripts/flashbdev.py @@ -26,4 +26,7 @@ class FlashBdev: if op == 5: # BP_IOCTL_SEC_SIZE return self.SEC_SIZE -bdev = FlashBdev() +if esp.flash_size() < 1024*1024: + bdev = None +else: + bdev = FlashBdev() -- cgit v1.2.3 From 2494399a42bff3b8286a4ccc7ae29e03645f230d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 20 Apr 2016 00:35:46 +0300 Subject: esp8266/scripts/flashbdev: Disable debug output/checks. --- esp8266/scripts/flashbdev.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'esp8266/scripts/flashbdev.py') diff --git a/esp8266/scripts/flashbdev.py b/esp8266/scripts/flashbdev.py index f1e85d923b..a46d60be58 100644 --- a/esp8266/scripts/flashbdev.py +++ b/esp8266/scripts/flashbdev.py @@ -10,17 +10,17 @@ class FlashBdev: self.blocks = blocks def readblocks(self, n, buf): - print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) + #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf) def writeblocks(self, n, buf): - print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf))) - assert len(buf) <= self.SEC_SIZE, len(buf) + #print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf))) + #assert len(buf) <= self.SEC_SIZE, len(buf) esp.flash_erase(n + self.START_SEC) esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf) def ioctl(self, op, arg): - print("ioctl(%d, %r)" % (op, arg)) + #print("ioctl(%d, %r)" % (op, arg)) if op == 4: # BP_IOCTL_SEC_COUNT return self.blocks if op == 5: # BP_IOCTL_SEC_SIZE -- cgit v1.2.3 From cc1ef76f887f8f56363e3e431ba17320cf99a533 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 20 Apr 2016 13:48:33 +0300 Subject: 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. --- esp8266/scripts/flashbdev.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'esp8266/scripts/flashbdev.py') diff --git a/esp8266/scripts/flashbdev.py b/esp8266/scripts/flashbdev.py index a46d60be58..1427d9c427 100644 --- a/esp8266/scripts/flashbdev.py +++ b/esp8266/scripts/flashbdev.py @@ -26,6 +26,40 @@ class FlashBdev: if op == 5: # BP_IOCTL_SEC_SIZE return self.SEC_SIZE +def set_bl_flash_size(real_size): + if real_size == 256*1024: + code = 1 + elif real_size == 512*1024: + code = 0 + elif real_size == 1024*1024: + code = 2 + elif real_size == 2048*1024: + code = 3 + elif real_size == 4096*1024: + code = 4 + else: + code = 2 + buf = bytearray(4096) + esp.flash_read(0, buf) + buf[3] = (buf[3] & 0xf) | (code << 4) + esp.flash_erase(0) + esp.flash_write(0, buf) + +# If bootloader size ID doesn't correspond to real Flash size, +# fix bootloader value and reboot. +size = esp.flash_id() >> 16 +# Check that it looks like realistic power of 2 for flash sizes +# commonly used with esp8266 +if 22 >= size >= 18: + size = 1 << size + if size != esp.flash_size(): + import machine + import time + print("Bootloader Flash size appear to have been set incorrectly, trying to fix") + set_bl_flash_size(size) + machine.reset() + while 1: time.sleep(1) + if esp.flash_size() < 1024*1024: bdev = None else: -- cgit v1.2.3 From 237c519ac43060d766a611f30d9aa39dc277cd16 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 26 Apr 2016 01:36:32 +0300 Subject: esp8266/scripts/flashbdev: Use all available Flash for filesystem. All Flash sans firmware at the beginning and 16K SDK param block at the end is used for filesystem (and that's calculated depending on the Flash size). --- esp8266/scripts/flashbdev.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'esp8266/scripts/flashbdev.py') diff --git a/esp8266/scripts/flashbdev.py b/esp8266/scripts/flashbdev.py index 1427d9c427..07ed966020 100644 --- a/esp8266/scripts/flashbdev.py +++ b/esp8266/scripts/flashbdev.py @@ -60,7 +60,9 @@ if 22 >= size >= 18: machine.reset() while 1: time.sleep(1) -if esp.flash_size() < 1024*1024: +size = esp.flash_size() +if size < 1024*1024: bdev = None else: - bdev = FlashBdev() + # 16K at the flash end is reserved for SDK params storage + bdev = FlashBdev((size - 16384) // FlashBdev.SEC_SIZE - FlashBdev.START_SEC) -- cgit v1.2.3