def test_xmodem1k_recv_bad_checksum(): """ Using external program for send, verify checksum fail in XMODEM.recv(). """ # Given, _, send_filename = tempfile.mkstemp() try: with open(send_filename, 'wb') as stream: fill_binary_data(stream) proc = subprocess.Popen( (send_prog, '--xmodem', '--verbose', send_filename), stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=0) getc = functools.partial(_proc_getc_fail_16bit_checksum, proc=proc) putc = functools.partial(_proc_putc, proc=proc) xmodem = XMODEM1k(getc, putc) recv_stream = BytesIO() # Exercise, status = xmodem.recv(recv_stream, timeout=5, crc_mode=1) # Verify, assert status == recv_stream.tell() verify_binary_data(recv_stream, padding=b'\x1a') proc.wait() assert proc.returncode == 0 finally: os.unlink(send_filename)
def DownloadZ12File(self, RFileName, SegBeg, WordCount, CombinedFileHeader): filelen = WordCount * 2 headerlen = len(CombinedFileHeader) totallen = headerlen + filelen filebuf = open(RFileName, 'wb') filebuf.write(bytearray(CombinedFileHeader)) modem = XMODEM1k(self.getc, self.putc) QueryString = str.encode("$PASHQ,BLK,%X,%X\n\r" % (SegBeg, filelen)) self.serial.timeout = 0 print("Requesting download of %s (%s)" % (RFileName, self.Human_Bytes(filelen))) self.serial.write(QueryString) time.sleep(1) download_bytes = 0 t1_start = time.perf_counter() download_bytes = modem.recv(filebuf, crc_mode=1) t1_stop = time.perf_counter() elapsed = t1_stop - t1_start bytes_per_sec = downloadbytes / elapsed print("Received %s in %f.0 seconds: (%f.0 bytes/sec)" % (self.Human_Bytes(download_bytes), elapsed, bytes_per_sec)) filebuf.close self.serial.timeout = self.TIMEOUT return download_bytes
def test_xmodem1k_send(): """ Using external program for receive, verify XMODEM1k.send(). """ # Given, _, recv_filename = tempfile.mkstemp() try: proc = subprocess.Popen( (recv_prog, '--xmodem', '--verbose', recv_filename), stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=0) getc = functools.partial(_proc_getc, proc=proc) putc = functools.partial(_proc_putc, proc=proc) xmodem = XMODEM1k(getc, putc, pad=b'\xbb') stream = fill_binary_data(BytesIO()) # Exercise, status = xmodem.send(stream, timeout=5, callback=_send_callback) # Verify, assert status is True verify_binary_data(stream, padding=b'\xbb') verify_binary_data(open(recv_filename, 'rb'), padding=b'\xbb') proc.wait() assert proc.returncode == 0 finally: os.unlink(recv_filename)
def deviceUploadFile(fn): ser.timeout = 1 statinfo_bin = os.stat(fn) #bar = pyprind.ProgBar(statinfo_bin.st_size//1024) print("Start upgrade %s " % fn); sys.stdout.flush(); def ser_write(data, timeout=1): #bar.update() #sys.stdout.flush(); return ser.write(data) def ser_read(size, timeout=1): return ser.read(size) stream = open(fn, 'rb+') print("Please wait for upgrade ..."); sys.stdout.flush(); time.sleep(0.2) modem = XMODEM1k(ser_read, ser_write) ser.flushInput() if modem.send(stream): time.sleep(1) print("Upgrade image success! => %s" % fn); sys.stdout.flush(); #print("Please wait for about 10 seconds before uncompress & restart...") reply = ser.read_until(b'run user code...') reply = reply.decode('ascii').strip() return reply return None
def deviceUploadFile(fn): ser.timeout = 1 statinfo_bin = os.stat(fn) bar = pyprind.ProgBar(statinfo_bin.st_size, bar_char='═') def ser_write(data, timeout=1): bar.update(1024) return ser.write(data) def ser_read(size, timeout=1): return ser.read(size) stream = open(fn, 'rb+') time.sleep(0.2) modem = XMODEM1k(ser_read, ser_write) ser.flushInput() if modem.send(stream): time.sleep(1) reply = ser.read_until(b'run user code...') reply = reply.decode('ascii').strip() return reply return None
def download(self): try: with open(self._image, 'rb') as f: def send_callback(total_packets, success_count, error_count): # self._log.warning('total_packets:{}, success_count:{}, error_count:{}'.format( # total_packets, success_count, error_count)) self._progbar.update() self.set_timeout(1) time.sleep(0.2) modem = XMODEM1k(self.read, self.write) self._log.warning('downloading ... ({} packets)'.format( self._total_packets)) result = modem.send(f, callback=send_callback) time.sleep(1) self._log.warning('') if result: self._log.warning('done') else: self._log.error('fail!') except Exception as e: self._log.error('error: {}'.format(e))
def burn_by_xmodem(params, in_flasher=False): # pylint: disable=unused-argument """ burn by xmodem """ console = None remove_rawfile = False with open(params['fwfile'], 'rb') as f_in: data = f_in.read(16) if data[:4] == b'cr6c' or data[:4] == b'r6cr': with open("{}_raw".format(params['fwfile']), 'wb') as f_out: f_out.write(f_in.read()) params['fwfile'] = "{}_raw".format(params['fwfile']) remove_rawfile = True if not _generate_padded_firmware(params['fwfile']): print("Generate padded firmware Failed!") return False console = _bootrom_download_flasher(params, console, in_flasher) if console is None: print("Goto flasher failed, try again.") return False console.write(b'\n\n') wait_for_realtek_cli(console) command = "xmod {}\n".format(params['ddr_base']) console.write(command.encode()) time.sleep(1) clear_serial_buffer(console) print("Now transmitting {}".format(params['fwfile'])) fwsize = os.stat("{}_padding".format(params['fwfile'])).st_size def getc(size, timeout=1): return console.read(size) def putc(data, timeout=1): return console.write(data) if 'pyprind' in sys.modules: def putc_user(data, timeout=1): bar_user.update() return console.write(data) bar_user = pyprind.ProgBar(fwsize / 1024 - 1, width=60) modem = XMODEM1k(getc, putc_user) else: modem = XMODEM1k(getc, putc) with open("{}_padding".format(params['fwfile']), 'rb') as f_in: modem.send(f_in) data = str(console.read(console.in_waiting), encoding="utf-8") if "Rx len=" not in data: print("Transmit Error!") console.close() os.remove("{}_padding".format(params['fwfile'])) return False print("Transmit Done! Please wait for programming to flash.") command = 'NANDW {} {} {}\n'.format(hex(int(params['offset'], 0)), params['ddr_base'], hex(fwsize)) console.write(command.encode()) console.write(b'y\n') time.sleep(1) wait_for_realtek_cli(console) sum_firmware = calc_sum_of_firmware(params['fwfile']) _update_boot_info(console, params['fwtype'], sum_firmware, fwsize) console.close() if remove_rawfile and os.path.exists(params['fwfile']): os.remove(params['fwfile']) if os.path.exists("{}_padding".format(params['fwfile'])): os.remove("{}_padding".format(params['fwfile'])) print("Programming {} Done!".format(params['fwfile'])) return True
def GetZ12Files(self): # Assumes all files are the same 4-character sitename and # each represents a session number starting with 0 MaxFileNumbers = 100 # For some versions of Z-12 it's 10 FatInfoFile = "fatinfo.dat" MemInfoSize = 10 FileInfoSize = 40 FatInfoSize = MemInfoSize + FileInfoSize * MaxFileNumbers modem = XMODEM1k(self.Serial.getc, self.Serial.putc) self.Serial.timeout = 0 # Download FAT to buffer Query = b"BLK,%X,%X" % (0, FatInfoSize * 2) self.Commands.QueryCommand(Query, verbose=True) time.sleep(1) print("Requesting FAT Download...") mybuf = open('data', 'wb') print("Bytes received: ", modem.recv(mybuf)) mybuf.close # Now read and process FAT mybuf = open('data', "rb") buf = mybuf.read(20) MemHeader = struct.unpack(">10H", buf) FilesFound = MemHeader[9] # decrement by one for current file print("Files Found (less current): ", FilesFound) # Perl: N = unsigned 32 bit long big endian Python: l # Perl: A = Ascii string, space padded Python: s (string) # Perl: C = unsigned 8 bit char Python: B # Perl: n = unsigned 16 bit short big endian Python: H # FAT struct from remote33.pl: # FileHdrStruct = "N N A4 C C n N C A3 A1 A33 n N n n C C C C C C n n n" # FAT struct translated to Python: FileHdrStruct = ">l l 4s B B H l B 3s 1s 33s H l H H B B B B B B H H H" # Now loop through the files downloading each, creating header, # and saving combination to disk for i in range(0, FilesFound): FileHeader = buffer.read(FileInfoSize * 2) # FileHeader is 80 bytes FileHdr = struct.unpack(FileHdrStruct, FileHeader) (SegBeg, WordCount, SegName, RangerMode, Tmp, FileStartWn, FileStartTime, RcvrType, Tmp2, SessName, Project, SegWn, SegTime, d13, d14, d15, d16, d17, d18, d19, d20, d21, d22, d23) = FileHdr SegName = SegName.decode('ascii') Tmp2 = Tmp2.decode('ascii') SessName = SessName.decode('ascii') Project = Project.decode('ascii') if (WordCount > 0): # don't download zero sized file # Generate filename RFileName = self.ComposeRFileName(SegName, i, SegWn, SegTime) print("RFileName: ", RFileName) # Build image header ImageHeader = self.BuildImageHeader() # Build file FAT FatInfo = self.BuildFat(FileHeader, i, MaxFileNumbers, FileInfoSize, FileHdrStruct) CombinedFileHeader = ImageHeader + FatInfo self.DownloadZ12File(RFileName, SegBeg, WordCount, CombinedFileHeader) buffer.close return ()
def main(argv): argc = len(argv) if argc == 1: download = WMDownload() elif argc == 2: if argv[1] == '--help': help() return else: if platform.system() == 'Windows': user_port = argv[1] else: user_port = "/dev/" + argv[1] download = WMDownload(port=user_port) elif argc == 3: if platform.system() == 'Windows': user_port = argv[1] else: user_port = "/dev/" + argv[1] download = WMDownload(port=user_port, image=argv[2]) else: help() return print('') print("serial open success!com: %s, baudrate: %s;" % download.info()) print('please restart device!') download.set_timeout(0.1) while True: c = download.getc(1) if c == b'C': download.putc(bytes.fromhex('210a00ef2a3100000080841e00')) break else: download.putc(struct.pack('<B', 27)) download.close() time.sleep(0.2) download.set_port_baudrate(2000000) download.open() time.sleep(0.2) # check baudrate while True: c = download.getc(1) if c == b'C': print('serial into high speed mode') break else: download.close() download.set_port_baudrate(115200) download.open() time.sleep(0.2) download.putc(bytes.fromhex('210a00ef2a3100000080841e00')) download.close() download.set_port_baudrate(2000000) download.open() time.sleep(0.2) print("start download %s " % download.image_path()) try: stream = open(download.image_path(), 'rb+') except IOError: print("can't open %s file." % download.image_path()) download.close() raise else: download.set_timeout(1) time.sleep(0.2) modem = XMODEM1k(download.getc, download.putc_bar) print("please wait for download....") result = modem.send(stream) time.sleep(1) print('') if result: print("download %s image success!" % download.image_path()) else: print("download %s image fail!" % download.image_path()) download.close()
def flash(serial_port, force, low_speed): # init stat_image_file = stat(IMG_FILE) p_bar = ProgBar(stat_image_file.st_size) def clear_buffer(): serial_port.reset_output_buffer() serial_port.reset_input_buffer() def read_with_bar(data, timeout=0): return serial_port.read(data) def write_with_bar(data, timeout=0): write_with_bar.current += len(data) p_bar.title = "%d / %d" % (write_with_bar.current, stat_image_file.st_size) p_bar.update(len(data)) ret = serial_port.write(data) sleep(0.001) return ret write_with_bar.current = 0 # go to flash mode = goto_flash_mode(serial_port) if mode == False: return False if mode == 'C': image_file = IMG_FILE mac = '' get_mac_cmd = bytes.fromhex('210600ea2d38000000') serial_port.timeout = 3 sleep(0.1) serial_port.write(get_mac_cmd) mac = (serial_port.read_until()) pos = mac.find(b'MAC:') if pos >= 0: mac = mac[pos + 4:len(mac) - 1].decode("ascii") print('MAC Address: %s' % mac) if isfile(FLASH_SIGNAL) and not force: with open(FLASH_SIGNAL, 'rt') as f: if f.read().strip() == mac: print( "this device already flash this program! skip flash. (--force to overwrite)" ) clear_buffer() control_reset(serial_port) return True else: print( 'bootloader not response MAC address, seems level 1 running.') mode = 'P' if mode == 'P': image_file = FLS_FILE # up speed def switch_baudrate(br): clear_buffer() serial_port.baudrate = br if not low_speed: print('switching to 2M baudrate...') sleep(0.2) speed_magic = bytes.fromhex('210a00ef2a3100000080841e00') serial_port.write(speed_magic) sleep(0.01) switch_baudrate(2000000) serial_port.timeout = 0.3 sleep(0.01) wront_cnt = 0 while True: c_in = serial_port.read(1) # print('got: "%s"' % c_in) if c_in == b'C' or c_in == b'P': break if c_in == b'\x00': continue elif wront_cnt >= 10: print('retry...') wront_cnt = 0 switch_baudrate(115200) serial_port.write(speed_magic) switch_baudrate(2000000) sleep(0.01) else: wront_cnt += 1 print('high speed mode!') serial_port.timeout = None print('sending file:', image_file) stream = open(image_file, 'rb') clear_buffer() modem = XMODEM(getc=read_with_bar, putc=write_with_bar) print("please wait for download....") result = modem.send(stream) print('') if result: print("download image success!") if mode == 'C': with open(FLASH_SIGNAL, 'wt') as f: f.write(mac) else: control_reset(serial_port) else: print("download image fail!") return False stream.close() clear_buffer() return True