diff options
Diffstat (limited to 'esp8266/scripts')
-rw-r--r-- | esp8266/scripts/_boot.py | 9 | ||||
-rw-r--r-- | esp8266/scripts/flashbdev.py | 68 | ||||
-rw-r--r-- | esp8266/scripts/inisetup.py | 2 | ||||
-rw-r--r-- | esp8266/scripts/webrepl.py | 75 | ||||
-rw-r--r-- | esp8266/scripts/webrepl_setup.py | 83 | ||||
-rw-r--r-- | esp8266/scripts/websocket_helper.py | 19 |
6 files changed, 11 insertions, 245 deletions
diff --git a/esp8266/scripts/_boot.py b/esp8266/scripts/_boot.py deleted file mode 100644 index c950de6758..0000000000 --- a/esp8266/scripts/_boot.py +++ /dev/null @@ -1,9 +0,0 @@ -import uos -from flashbdev import bdev - -try: - if bdev: - vfs = uos.VfsFat(bdev, "") -except OSError: - import inisetup - vfs = inisetup.setup() diff --git a/esp8266/scripts/flashbdev.py b/esp8266/scripts/flashbdev.py deleted file mode 100644 index 07ed966020..0000000000 --- a/esp8266/scripts/flashbdev.py +++ /dev/null @@ -1,68 +0,0 @@ -import esp - -class FlashBdev: - - SEC_SIZE = 4096 - START_SEC = 0x89000 // SEC_SIZE - NUM_BLK = 0x73 - - 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 - -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) - -size = esp.flash_size() -if size < 1024*1024: - bdev = None -else: - # 16K at the flash end is reserved for SDK params storage - bdev = FlashBdev((size - 16384) // FlashBdev.SEC_SIZE - FlashBdev.START_SEC) diff --git a/esp8266/scripts/inisetup.py b/esp8266/scripts/inisetup.py index 461690b162..7d8f6fa0a8 100644 --- a/esp8266/scripts/inisetup.py +++ b/esp8266/scripts/inisetup.py @@ -40,7 +40,9 @@ def setup(): with open("/boot.py", "w") as f: f.write("""\ # This file is executed on every boot (including wake-boot from deepsleep) +import gc #import webrepl #webrepl.start() +gc.collect() """) return vfs diff --git a/esp8266/scripts/webrepl.py b/esp8266/scripts/webrepl.py deleted file mode 100644 index da3e70c595..0000000000 --- a/esp8266/scripts/webrepl.py +++ /dev/null @@ -1,75 +0,0 @@ -# This module should be imported from REPL, not run from command line. -import socket -import uos -import network -import websocket -import websocket_helper -import _webrepl - -listen_s = None -client_s = None - -def setup_conn(port, accept_handler): - global listen_s - listen_s = socket.socket() - listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - - ai = socket.getaddrinfo("0.0.0.0", port) - addr = ai[0][4] - - listen_s.bind(addr) - listen_s.listen(1) - if accept_handler: - listen_s.setsockopt(socket.SOL_SOCKET, 20, accept_handler) - for i in (network.AP_IF, network.STA_IF): - iface = network.WLAN(i) - if iface.active(): - print("WebREPL daemon started on ws://%s:%d" % (iface.ifconfig()[0], port)) - return listen_s - - -def accept_conn(listen_sock): - global client_s - cl, remote_addr = listen_sock.accept() - print("\nWebREPL connection from:", remote_addr) - client_s = cl - websocket_helper.server_handshake(cl) - ws = websocket.websocket(cl, True) - ws = _webrepl._webrepl(ws) - cl.setblocking(False) - # notify REPL on socket incoming data - cl.setsockopt(socket.SOL_SOCKET, 20, uos.dupterm_notify) - uos.dupterm(ws) - - -def stop(): - global listen_s, client_s - uos.dupterm(None) - if client_s: - client_s.close() - if listen_s: - listen_s.close() - - -def start(port=8266, password=None): - stop() - if password is None: - try: - import port_config - _webrepl.password(port_config.WEBREPL_PASS) - setup_conn(port, accept_conn) - print("Started webrepl in normal mode") - except: - import webrepl_setup - setup_conn(port, webrepl_setup.handle_conn) - print("Started webrepl in setup mode") - else: - _webrepl.password(password) - setup_conn(port, accept_conn) - print("Started webrepl in normal mode") - - -def start_foreground(port=8266): - stop() - s = setup_conn(port, None) - accept_conn(s) diff --git a/esp8266/scripts/webrepl_setup.py b/esp8266/scripts/webrepl_setup.py deleted file mode 100644 index d0bf8465d5..0000000000 --- a/esp8266/scripts/webrepl_setup.py +++ /dev/null @@ -1,83 +0,0 @@ -import sys -import socket -import time - -from websocket import * -import websocket_helper - - -def setup_server(): - s = socket.socket() - s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - - ai = socket.getaddrinfo("0.0.0.0", 8266) - addr = ai[0][4] - - s.bind(addr) - s.listen(1) - return s - -def getpass(stream, prompt): - stream.write(prompt) - passwd = b"" - while 1: - c = stream.read(1) - if c in (b"\r", b"\n"): - stream.write("\r\n") - return passwd - passwd += c - stream.write("*") - -def handle_conn(listen_sock): - cl, remote_addr = listen_sock.accept() - - print(""" - -First-time WebREPL connection has been received. WebREPL initial setup -will now start over this connection. During setup, UART REPL will be -non-responsive. After setup finishes, the board will be rebooted. In -case of error during setup, current session will continue. - -If you receive this message unexpectedly, it may mean that your WebREPL -connection is being hacked (power off board if unsure). -""") - - websocket_helper.server_handshake(cl) - ws = websocket(cl) - - ws.write("""\ -Welcome to MicroPython WebREPL!\r -\r -This is the first time you connect to WebREPL, so please set a password\r -to use for the following WebREPL sessions. Once you enter the password\r -twice, your board will reboot with WebREPL running in active mode. On\r -some boards, you may need to press reset button or reconnect power.\r -\r -""") - - while 1: - passwd1 = getpass(ws, "New password: ") - if len(passwd1) < 4: - ws.write("Password too short\r\n") - continue - elif len(passwd1) > 9: - ws.write("Password too long\r\n") - continue - passwd2 = getpass(ws, "Confirm password: ") - if passwd1 == passwd2: - break - ws.write("Passwords do not match\r\n") - - with open("port_config.py", "w") as f: - f.write("WEBREPL_PASS = %r\n" % passwd1.decode("ascii")) - - ws.write("Password successfully set, restarting...\r\n") - cl.close() - time.sleep(2) - import machine - machine.reset() - - -def test(): - s = setup_server() - handle_conn(s) diff --git a/esp8266/scripts/websocket_helper.py b/esp8266/scripts/websocket_helper.py index 22ac28592d..9c06db5023 100644 --- a/esp8266/scripts/websocket_helper.py +++ b/esp8266/scripts/websocket_helper.py @@ -36,21 +36,20 @@ def server_handshake(sock): if DEBUG: print("Sec-WebSocket-Key:", webkey, len(webkey)) - respkey = webkey + b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11" - respkey = hashlib.sha1(respkey).digest() + d = hashlib.sha1(webkey) + d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11") + respkey = d.digest() respkey = binascii.b2a_base64(respkey)[:-1] + if DEBUG: + print("respkey:", respkey) - resp = b"""\ + sock.send(b"""\ HTTP/1.1 101 Switching Protocols\r Upgrade: websocket\r Connection: Upgrade\r -Sec-WebSocket-Accept: %s\r -\r -""" % respkey - - if DEBUG: - print(resp) - sock.send(resp) +Sec-WebSocket-Accept: """) + sock.send(respkey) + sock.send("\r\n\r\n") # Very simplified client handshake, works for MicroPython's |