blob: 4273b2639c0f8559222881f232cf68f91217036e [file] [log] [blame]
Paul Sokolovsky40f5ecd2016-04-12 00:37:04 +03001import esp
2
3class FlashBdev:
4
5 SEC_SIZE = 4096
Paul Sokolovsky5f7ce2a2017-05-12 16:08:54 +03006 RESERVED_SECS = 1
Damien Georgebae77982017-01-04 23:47:09 +11007 START_SEC = esp.flash_user_start() // SEC_SIZE + RESERVED_SECS
8 NUM_BLK = 0x6b - RESERVED_SECS
Paul Sokolovsky40f5ecd2016-04-12 00:37:04 +03009
10 def __init__(self, blocks=NUM_BLK):
11 self.blocks = blocks
12
Damien George6eee5412019-11-07 18:43:37 +110013 def readblocks(self, n, buf, off=0):
14 #print("readblocks(%s, %x(%d), %d)" % (n, id(buf), len(buf), off))
15 esp.flash_read((n + self.START_SEC) * self.SEC_SIZE + off, buf)
Paul Sokolovsky40f5ecd2016-04-12 00:37:04 +030016
Damien George6eee5412019-11-07 18:43:37 +110017 def writeblocks(self, n, buf, off=None):
18 #print("writeblocks(%s, %x(%d), %d)" % (n, id(buf), len(buf), off))
Paul Sokolovsky24943992016-04-20 00:35:46 +030019 #assert len(buf) <= self.SEC_SIZE, len(buf)
Damien George6eee5412019-11-07 18:43:37 +110020 if off is None:
21 esp.flash_erase(n + self.START_SEC)
22 off = 0
23 esp.flash_write((n + self.START_SEC) * self.SEC_SIZE + off, buf)
Paul Sokolovsky40f5ecd2016-04-12 00:37:04 +030024
25 def ioctl(self, op, arg):
Paul Sokolovsky24943992016-04-20 00:35:46 +030026 #print("ioctl(%d, %r)" % (op, arg))
Damien Georgecfe1c5a2019-10-29 12:25:30 +110027 if op == 4: # MP_BLOCKDEV_IOCTL_BLOCK_COUNT
Paul Sokolovsky40f5ecd2016-04-12 00:37:04 +030028 return self.blocks
Damien Georgecfe1c5a2019-10-29 12:25:30 +110029 if op == 5: # MP_BLOCKDEV_IOCTL_BLOCK_SIZE
Paul Sokolovsky40f5ecd2016-04-12 00:37:04 +030030 return self.SEC_SIZE
Damien George6eee5412019-11-07 18:43:37 +110031 if op == 6: # MP_BLOCKDEV_IOCTL_BLOCK_ERASE
32 esp.flash_erase(arg + self.START_SEC)
33 return 0
Paul Sokolovsky40f5ecd2016-04-12 00:37:04 +030034
Paul Sokolovsky237c5192016-04-26 01:36:32 +030035size = esp.flash_size()
36if size < 1024*1024:
Paul Sokolovsky8dcce922016-04-18 17:14:00 +030037 bdev = None
38else:
Paul Sokolovskyca59f5f2016-08-06 15:21:49 +030039 # 20K at the flash end is reserved for SDK params storage
40 bdev = FlashBdev((size - 20480) // FlashBdev.SEC_SIZE - FlashBdev.START_SEC)