def change_file(self): oldselected = self.fb_selected - self.fb_topitem self.fb_selected = self.fb_cursor try: filename = self.fullpath(self.direntries[self.fb_cursor][0]) except: filename = False self.fb_selected = -1 self.show_dir_line(oldselected) self.show_dir_line(self.fb_cursor - self.fb_topitem) if filename: if filename.endswith(".mac") or filename.endswith( ".MAC") or filename.endswith(".dsk") or filename.endswith( ".DSK"): self.diskfile[self.drive[0]] = open(filename, "rb") self.imgtype[self.drive[0]] = 0 if filename.endswith(".dsk") or filename.endswith(".DSK"): self.imgtype[self.drive[0]] = 1 #self.update_track() self.ctrl(16 << self.drive[0]) # set insert_disk self.enable[0] = 0 self.osd_enable(0) if filename.endswith(".bit"): self.spi_request.irq(handler=None) self.timer.deinit() self.enable[0] = 0 self.osd_enable(0) self.spi.deinit() import ecp5 ecp5.prog_stream(open(filename, "rb"), blocksize=1024) if filename.endswith("_sd.bit"): os.umount("/sd") for i in bytearray([2, 4, 12, 13, 14, 15]): p = Pin(i, Pin.IN) a = p.value() del p, a result = ecp5.prog_close() del tap gc.collect() #os.mount(SDCard(slot=3),"/sd") # BUG, won't work self.init_spi() # because of ecp5.prog() spi.deinit() self.spi_request.irq(trigger=Pin.IRQ_FALLING, handler=self.irq_handler_ref) self.irq_handler(0) # handle stuck IRQ if filename.endswith(".nes") \ or filename.endswith(".snes") \ or filename.endswith(".smc") \ or filename.endswith(".sfc"): import ld_nes s = ld_nes.ld_nes(self.spi, self.cs) s.ctrl(1) s.ctrl(0) s.load_stream(open(filename, "rb")) del s gc.collect() self.enable[0] = 0 self.osd_enable(0) if filename.startswith("/sd/ti99_4a/") and filename.endswith( ".bin"): import ld_ti99_4a s = ld_ti99_4a.ld_ti99_4a(self.spi, self.cs) s.load_rom_auto(open(filename, "rb"), filename) del s gc.collect() self.enable[0] = 0 self.osd_enable(0) if (filename.startswith("/sd/msx") and filename.endswith(".rom")) \ or filename.endswith(".mx1"): import ld_msx s = ld_msx.ld_msx(self.spi, self.cs) s.load_msx_rom(open(filename, "rb")) del s gc.collect() self.enable[0] = 0 self.osd_enable(0) if filename.endswith(".z80"): self.enable[0] = 0 self.osd_enable(0) import ld_zxspectrum s = ld_zxspectrum.ld_zxspectrum(self.spi, self.cs) s.loadz80(filename) del s gc.collect() if filename.endswith(".ora") or filename.endswith(".orao"): self.enable[0] = 0 self.osd_enable(0) import ld_orao s = ld_orao.ld_orao(self.spi, self.cs) s.loadorao(filename) del s gc.collect() if filename.endswith(".vsf"): self.enable[0] = 0 self.osd_enable(0) import ld_vic20 s = ld_vic20.ld_vic20(self.spi, self.cs) s.loadvsf(filename) del s gc.collect() if filename.endswith(".prg"): self.enable[0] = 0 self.osd_enable(0) import ld_vic20 s = ld_vic20.ld_vic20(self.spi, self.cs) s.loadprg(filename) del s gc.collect() if filename.endswith(".cas"): self.enable[0] = 0 self.osd_enable(0) import ld_trs80 s = ld_trs80.ld_trs80(self.spi, self.cs) s.loadcas(filename) del s gc.collect() if filename.endswith(".cmd"): self.enable[0] = 0 self.osd_enable(0) import ld_trs80 s = ld_trs80.ld_trs80(self.spi, self.cs) s.loadcmd(filename) del s gc.collect()
def exec_ftp_command(self, cl): global datasocket global client_busy global my_ip_addr try: collect() data = cl.readline().decode("utf-8").rstrip("\r\n") if len(data) <= 0: # No data, close # This part is NOT CLEAN; there is still a chance that a # closing data connection will be signalled as closing # command connection log_msg(1, "*** No data, assume QUIT") close_client(cl) return if client_busy: # check if another client is busy cl.sendall("400 Device busy.\r\n") # tell so the remote client return # and quit client_busy = True # now it's my turn # check for log-in state may done here, like # if self.logged_in == False and not command in\ # ("USER", "PASS", "QUIT"): # cl.sendall("530 Not logged in.\r\n") # return command = data.split()[0].upper() payload = data[len(command):].lstrip() # partition is missing path = self.get_absolute_path(self.cwd, payload) log_msg(1, "Command={}, Payload={}".format(command, payload)) if command == "USER": # self.logged_in = True cl.sendall("230 Logged in.\r\n") # If you want to see a password,return # "331 Need password.\r\n" instead # If you want to reject an user, return # "530 Not logged in.\r\n" elif command == "PASS": # you may check here for a valid password and return # "530 Not logged in.\r\n" in case it's wrong # self.logged_in = True cl.sendall("230 Logged in.\r\n") elif command == "SYST": cl.sendall("215 UNIX Type: L8\r\n") elif command in ("TYPE", "NOOP", "ABOR"): # just accept & ignore cl.sendall('200 OK\r\n') elif command == "QUIT": cl.sendall('221 Bye.\r\n') close_client(cl) elif command == "PWD" or command == "XPWD": cl.sendall('257 "{}"\r\n'.format(self.cwd)) elif command == "CWD" or command == "XCWD": try: if (uos.stat(path)[0] & 0o170000) == 0o040000: self.cwd = path cl.sendall('250 OK\r\n') else: cl.sendall('550 Fail\r\n') except: cl.sendall('550 Fail\r\n') elif command == "PASV": cl.sendall('227 Entering Passive Mode ({},{},{}).\r\n'.format( self.pasv_data_addr.replace('.', ','), _DATA_PORT >> 8, _DATA_PORT % 256)) self.active = False elif command == "PORT": items = payload.split(",") if len(items) >= 6: self.act_data_addr = '.'.join(items[:4]) if self.act_data_addr == "127.0.1.1": # replace by command session addr self.act_data_addr = self.remote_addr self.DATA_PORT = int(items[4]) * 256 + int(items[5]) cl.sendall('200 OK\r\n') self.active = True else: cl.sendall('504 Fail\r\n') elif command == "LIST" or command == "NLST": if payload.startswith("-"): option = payload.split()[0].lower() path = self.get_absolute_path( self.cwd, payload[len(option):].lstrip()) else: option = "" try: data_client = self.open_dataclient() cl.sendall("150 Directory listing:\r\n") self.send_list_data(path, data_client, command == "LIST" or 'l' in option) cl.sendall("226 Done.\r\n") data_client.close() except: cl.sendall('550 Fail\r\n') if data_client is not None: data_client.close() elif command == "RETR": try: data_client = self.open_dataclient() cl.sendall("150 Opened data connection.\r\n") self.send_file_data(path, data_client) # if the next statement is reached, # the data_client was closed. data_client = None cl.sendall("226 Done.\r\n") except: cl.sendall('550 Fail\r\n') if data_client is not None: data_client.close() elif command == "STOR" or command == "APPE": result = False try: data_client = self.open_dataclient() cl.sendall("150 Opened data connection.\r\n") if path == "/fpga": import ecp5 ecp5.prog_stream(data_client,_CHUNK_SIZE) result = ecp5.prog_close() data_client.close() elif path.startswith("/flash@"): import ecp5 dummy, addr = path.split("@") addr = int(addr) result = ecp5.flash_stream(data_client,addr) ecp5.flash_close() del addr, dummy data_client.close() elif path.startswith("/sd@"): import sdraw dummy, addr = path.split("@") addr = int(addr) sd_raw = sdraw.sdraw() result = sd_raw.sd_write_stream(data_client,addr) del sd_raw, addr, dummy data_client.close() else: self.save_file_data(path, data_client, "w" if command == "STOR" else "a") result = True # if the next statement is reached, # the data_client was closed. data_client = None except: if data_client is not None: data_client.close() if result: cl.sendall("226 Done.\r\n") else: cl.sendall('550 Fail\r\n') del result elif command == "SIZE": try: cl.sendall('213 {}\r\n'.format(uos.stat(path)[6])) except: cl.sendall('550 Fail\r\n') elif command == "STAT": if payload == "": cl.sendall("211-Connected to ({})\r\n" " Data address ({})\r\n" " TYPE: Binary STRU: File MODE: Stream\r\n" " Session timeout {}\r\n" "211 Client count is {}\r\n".format( self.remote_addr, self.pasv_data_addr, _COMMAND_TIMEOUT, len(client_list))) else: cl.sendall("213-Directory listing:\r\n") self.send_list_data(path, cl, True) cl.sendall("213 Done.\r\n") elif command == "DELE": try: uos.remove(path) cl.sendall('250 OK\r\n') except: cl.sendall('550 Fail\r\n') elif command == "RNFR": try: # just test if the name exists, exception if not uos.stat(path) self.fromname = path cl.sendall("350 Rename from\r\n") except: cl.sendall('550 Fail\r\n') elif command == "RNTO": try: uos.rename(self.fromname, path) cl.sendall('250 OK\r\n') except: cl.sendall('550 Fail\r\n') self.fromname = None elif command == "CDUP" or command == "XCUP": self.cwd = self.get_absolute_path(self.cwd, "..") cl.sendall('250 OK\r\n') elif command == "RMD" or command == "XRMD": try: uos.rmdir(path) cl.sendall('250 OK\r\n') except: cl.sendall('550 Fail\r\n') elif command == "MKD" or command == "XMKD": try: uos.mkdir(path) cl.sendall('250 OK\r\n') except: cl.sendall('550 Fail\r\n') elif command == "SITE": if path == "/mount": if self.mount(): cl.sendall('250 OK\r\n') else: cl.sendall('550 Fail\r\n') elif path == "/umount": if self.umount(): cl.sendall('250 OK\r\n') else: cl.sendall('550 Fail\r\n') elif path == "/passthru": import ecp5 ecp5.passthru() cl.sendall('250 OK passthru\r\n') elif path.endswith(".bit") or path.endswith(".bit.gz"): try: import ecp5 if ecp5.prog(path, close=False): if path.startswith("/sd/"): try: self.umount() cl.sendall('111 umount /sd OK\r\n') except: cl.sendall('411 umount /sd Fail\r\n') if ecp5.prog_close(): cl.sendall('250 OK\r\n') else: cl.sendall('550 Fail\r\n') else: cl.sendall('550 Fail\r\n') except: cl.sendall('550 Fail\r\n') else: if path.startswith("/"): exe=path[1:] else: exe=path try: exec(exe) cl.sendall('250 OK '+exe+'\r\n') except: cl.sendall('550 Fail '+exe+'\r\n') del exe else: cl.sendall("502 Unsupported command.\r\n") # log_msg(2, # "Unsupported command {} with payload {}".format(command, # payload)) # handle unexpected errors except Exception as err: log_msg(1, "Exception in exec_ftp_command: {}".format(err)) # tidy up before leaving client_busy = False
def change_file(): global fb_selected, fb_topitem, fb_cursor, diskfile oldselected = fb_selected - fb_topitem fb_selected = fb_cursor try: filename = fullpath(direntries[fb_cursor][0]) except: filename = False fb_selected = -1 show_dir_line(oldselected) show_dir_line(fb_cursor - fb_topitem) if filename: if filename.endswith(".bit"): spi_request.irq(handler=None) timer.deinit() enable[0] = 0 osd_enable(0) spi.deinit() import ecp5 ecp5.prog_stream(open(filename, "rb"), blocksize=1024) if filename.endswith("_sd.bit"): os.umount("/sd") for i in bytearray([2, 4, 12, 13, 14, 15]): p = Pin(i, Pin.IN) a = p.value() del p, a result = ecp5.prog_close() gc.collect() #os.mount(SDCard(slot=3),"/sd") # BUG, won't work init_spi() # because of ecp5.prog() spi.deinit() spi_request.irq(trigger=Pin.IRQ_FALLING, handler=irq_handler_ref) irq_handler(0) # handle stuck IRQ if filename.endswith(".z80"): enable[0] = 0 osd_enable(0) import ld_zxspectrum s = ld_zxspectrum.ld_zxspectrum(spi, cs) s.loadz80(filename) del s gc.collect() if filename.endswith(".nes"): import ld_zxspectrum s = ld_zxspectrum.ld_zxspectrum(spi, cs) s.ctrl(1) s.ctrl(0) s.load_stream(open(filename, "rb"), addr=0, maxlen=0x101000) del s gc.collect() enable[0] = 0 osd_enable(0) if filename.endswith(".ora") or filename.endswith(".orao"): enable[0] = 0 osd_enable(0) import ld_orao s = ld_orao.ld_orao(spi, cs) s.loadorao(filename) del s gc.collect() if filename.endswith(".vsf"): enable[0] = 0 osd_enable(0) import ld_vic20 s = ld_vic20.ld_vic20(spi, cs) s.loadvsf(filename) del s gc.collect() if (filename.find("vic20") >= 0 or filename.find("VIC20") >= 0) and ( filename.endswith(".prg") or filename.endswith(".PRG")): enable[0] = 0 osd_enable(0) import ld_vic20 s = ld_vic20.ld_vic20(spi, cs) s.loadprg(filename) del s gc.collect() if (filename.find("c64") >= 0 or filename.find("C64") >= 0) and ( filename.endswith(".prg") or filename.endswith(".PRG")): enable[0] = 0 osd_enable(0) import ld_c64 s = ld_c64.ld_c64(spi, cs) s.loadprg(filename) del s gc.collect() if filename.endswith(".nib"): diskfile = open(filename, "rb") osd_enable(0) enable[0] = 0