Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
start_sector = offset // sector_size
head_sectors = sectors_per_block - (start_sector % sectors_per_block)
if num_sectors < head_sectors:
head_sectors = num_sectors
if num_sectors < 2 * head_sectors:
return (num_sectors + 1) // 2 * sector_size
else:
return (num_sectors - head_sectors) * sector_size
def override_vddsdio(self, new_voltage):
raise NotImplementedInROMError("Overriding VDDSDIO setting only applies to ESP32")
class ESP8266StubLoader(ESP8266ROM):
""" Access class for ESP8266 stub loader, runs on top of ROM.
"""
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
IS_STUB = True
def __init__(self, rom_loader):
self._port = rom_loader._port
self._trace_enabled = rom_loader._trace_enabled
self.flush_input() # resets _slip_reader
def get_erase_size(self, offset, size):
return size # stub doesn't have same size bug as ROM loader
ESP8266ROM.STUB_CLASS = ESP8266StubLoader
print(" Byte 0x%x: new 0x%02x old 0x%02x" % (b, ord(new[k][b]), ord(old[k][b])))
if __name__ == "__main__":
same = True
sys.path.append("..")
import esptool
old_8266_stub = esptool.ESP8266ROM.STUB_CODE
old_32_stub = esptool.ESP32ROM.STUB_CODE
# hackiness: importing this module edits the esptool module
import esptool_test_stub
if esptool.ESP8266ROM.STUB_CODE != old_8266_stub:
print("ESP8266 stub code in esptool.py is different to just-built stub")
verbose_diff(esptool.ESP8266ROM.STUB_CODE, old_8266_stub)
same = False
if esptool.ESP32ROM.STUB_CODE != old_32_stub:
print("ESP32 stub code in esptool.py is different to just-built stub.")
verbose_diff(esptool.ESP32ROM.STUB_CODE, old_32_stub)
same = False
if same:
print("Stub code is the same")
sys.exit(0 if same else 1)
from esptool import ESPLoader, ESP8266ROM, ESP32ROM, DETECTED_FLASH_SIZES
from serial.tools import list_ports as _list_ports
from ampy.core.mpy_boards import MpyBoard, ESP8266Board, ESP32Board
def main(baud: int) -> Generator[MpyBoard, None, None]:
for port in list_ports():
try:
yield detect_board(port, baud)
except StopIteration:
pass
CHIP_CLASSES = {
ESP8266ROM.DATE_REG_VALUE: (ESP8266ROM, ESP8266Board),
ESP32ROM.DATE_REG_VALUE: (ESP32ROM, ESP32Board),
}
def list_ports() -> List[str]:
ports = [
it.device
for it in _list_ports.comports()
if "BLUETOOTH" not in it.device.upper()
]
ports.sort(reverse=True)
return ports
def detect_board(port: str, baud: int) -> MpyBoard:
board_cls: Type[MpyBoard]
class ESP8266StubLoader(ESP8266ROM):
""" Access class for ESP8266 stub loader, runs on top of ROM.
"""
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
IS_STUB = True
def __init__(self, rom_loader):
self._port = rom_loader._port
self._trace_enabled = rom_loader._trace_enabled
self.flush_input() # resets _slip_reader
def get_erase_size(self, offset, size):
return size # stub doesn't have same size bug as ROM loader
ESP8266ROM.STUB_CLASS = ESP8266StubLoader
class ESP32ROM(ESPLoader):
"""Access class for ESP32 ROM bootloader
"""
CHIP_NAME = "ESP32"
IS_STUB = False
DATE_REG_VALUE = 0x15122500
IROM_MAP_START = 0x400d0000
IROM_MAP_END = 0x40400000
DROM_MAP_START = 0x3F400000
DROM_MAP_END = 0x3F800000
for k in [ "data", "text" ]:
if len(new[k]) != len(old[k]):
print("New %s %d bytes, old stub code %d bytes" % (k, len(new[k]), len(old)[k]))
if new[k] != old[k]:
print("%s is different" % k)
if len(new[k]) == len(old[k]):
for b in range(len(new[k])):
if new[k][b] != old[k][b]:
print(" Byte 0x%x: new 0x%02x old 0x%02x" % (b, ord(new[k][b]), ord(old[k][b])))
if __name__ == "__main__":
same = True
sys.path.append("..")
import esptool
old_8266_stub = esptool.ESP8266ROM.STUB_CODE
old_32_stub = esptool.ESP32ROM.STUB_CODE
# hackiness: importing this module edits the esptool module
import esptool_test_stub
if esptool.ESP8266ROM.STUB_CODE != old_8266_stub:
print("ESP8266 stub code in esptool.py is different to just-built stub")
verbose_diff(esptool.ESP8266ROM.STUB_CODE, old_8266_stub)
same = False
if esptool.ESP32ROM.STUB_CODE != old_32_stub:
print("ESP32 stub code in esptool.py is different to just-built stub.")
verbose_diff(esptool.ESP32ROM.STUB_CODE, old_32_stub)
same = False
if same:
print("Stub code is the same")
def is_irom_addr(self, addr):
""" Returns True if an address starts in the irom region.
Valid for ESP8266 only.
"""
return ESP8266ROM.IROM_MAP_START <= addr < ESP8266ROM.IROM_MAP_END
def flash_spi_attach(self, hspi_arg):
if self.IS_STUB:
super(ESP8266ROM, self).flash_spi_attach(hspi_arg)
else:
# ESP8266 ROM has no flash_spi_attach command in serial protocol,
# but flash_begin will do it
self.flash_begin(0, 0)
value = {
'2m': '256KB',
'4m': '512KB',
'8m': '1MB',
'16m': '2MB',
'32m': '4MB',
'16m-c1': '2MB-c1',
'32m-c1': '4MB-c1',
}[values[0]]
print("WARNING: Flash size arguments in megabits like '%s' are deprecated." % (values[0]))
print("Please use the equivalent size '%s'." % (value))
print("Megabit arguments may be removed in a future release.")
except KeyError:
value = values[0]
known_sizes = dict(ESP8266ROM.FLASH_SIZES)
known_sizes.update(ESP32ROM.FLASH_SIZES)
if self._auto_detect:
known_sizes['detect'] = 'detect'
if value not in known_sizes:
raise argparse.ArgumentError(self, '%s is not a known flash size. Known sizes: %s' % (value, ", ".join(known_sizes.keys())))
setattr(namespace, self.dest, value)
end = 0
for address, argfile in sorted(pairs):
argfile.seek(0,2) # seek to end
size = argfile.tell()
argfile.seek(0)
sector_start = address & ~(ESPLoader.FLASH_SECTOR_SIZE - 1)
sector_end = ((address + size + ESPLoader.FLASH_SECTOR_SIZE - 1) & ~(ESPLoader.FLASH_SECTOR_SIZE - 1)) - 1
if sector_start < end:
message = 'Detected overlap at address: 0x%x for file: %s' % (address, argfile.name)
raise argparse.ArgumentError(self, message)
end = sector_end
setattr(namespace, self.dest, pairs)
# Binary stub code (see flasher_stub dir for source & details)
ESP8266ROM.STUB_CODE = eval(zlib.decompress(base64.b64decode(b"""
eNrNPHt/1Da2X2XshJCE0Fq2x5bTsMxMwvAo3PLYpHQ3bWPLNpTbd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/ZKteHlAMTl47nX531W5hYPsTF5yQNoxT9mSOKvxHBy8\
9KIb0vLwzwQAuKqNWexQLw3UUePNH2G2cmNRruOo23svQLO+B3mED9LXoG1ArVaJx7swdtrBrRFgskeb9B7RETOr5i3zsq+FgZ0Kkhr4X8cFCEtBEV+Z393F0Hn9J+rS/dxgBwcsSYca8AzNWoSpq+DfjDHDBhQN\
+sPqW8KZqSmjoDMXWJc5aeEGBJXSDcEpjQLrMm3B3mzGfxE/oMHmvg8Vu3RTr+8S/OgcNDD53g6D48c6OPKCVnbjaY4rM25lUfKNXZG46nYZmVvG94NlFLKMZJh/E/mIgn96fQz2Cb7hRwBBAgmv+DVbiugGiNKb\
value = {
'2m': '256KB',
'4m': '512KB',
'8m': '1MB',
'16m': '2MB',
'32m': '4MB',
'16m-c1': '2MB-c1',
'32m-c1': '4MB-c1',
}[values[0]]
print("WARNING: Flash size arguments in megabits like '%s' are deprecated." % (values[0]))
print("Please use the equivalent size '%s'." % (value))
print("Megabit arguments may be removed in a future release.")
except KeyError:
value = values[0]
known_sizes = dict(ESP8266ROM.FLASH_SIZES)
known_sizes.update(ESP32ROM.FLASH_SIZES)
if self._auto_detect:
known_sizes['detect'] = 'detect'
if value not in known_sizes:
raise argparse.ArgumentError(self, '%s is not a known flash size. Known sizes: %s' % (value, ", ".join(known_sizes.keys())))
setattr(namespace, self.dest, value)