diff --git a/boot_surgeon.py b/boot_surgeon.py index f3d6264..c085e39 100644 --- a/boot_surgeon.py +++ b/boot_surgeon.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import pager import cbf import sys @@ -14,7 +15,7 @@ def do_surgeon_boot(path): """ pager_client = pager.client(conn_iface(mount_connection())) pager_client.upload(path) - print 'Booting surgeon.' + print ('Booting surgeon.') if len(sys.argv) != 2: diff --git a/cbf.py b/cbf.py index d0e1577..f1b2719 100644 --- a/cbf.py +++ b/cbf.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ############################################################################## # OpenLFConnect # @@ -31,6 +31,8 @@ #@ # cbf.py Version 0.1.2 +# Ported to Python 3 by A.Mccarthy +# import os import array @@ -45,8 +47,9 @@ ############################## PACKET_SIZE = 16384 -MAGIC_NUMBER = '\xf0\xde\xbc\x9a' -COMPRESSION_SIG = '\x1F\x8B\x08\x00' #gunzip +MAGIC_NUMBER = b'\xf0\xde\xbc\x9a' +# MAGIC_NUMBER = struct.pack('IIII', 240, 222, 188, 154) +COMPRESSION_SIG = b'\x1F\x8B\x08\x00' #gunzip CBF_VERSION = 1 BLOCK_SIZE = 0x20000 KERNEL_LOAD_08 = 0x8000 @@ -59,7 +62,6 @@ def error(e): assert False, e - def check(path, ret_bool=False): try: if not os.path.exists(path): @@ -71,11 +73,12 @@ def check(path, ret_bool=False): magic = f.read(4) f.close() - if not str(file_size/PACKET_SIZE).isdigit(): + # if not str(file_size/PACKET_SIZE).isdigit(): + if (file_size % PACKET_SIZE) != 0: if ret_bool: return False else: - error('File is the wrong size, should be multiple of %s.' % PACKET_SIZE) + error('File is the wrong size, should be multiple of %s.' % str(file_size/PACKET_SIZE)) if magic != MAGIC_NUMBER: if ret_bool: @@ -84,7 +87,7 @@ def check(path, ret_bool=False): error('File failed CBF Magic Number check.') else: return True - except Exception, e: + except Exception as e: error(e) @@ -100,12 +103,12 @@ def extract(path): kernel_path = os.path.join(os.path.dirname(path), kernel_name) - print 'Unwrapping Kernel from CBF' + print('Unwrapping Kernel from CBF') fimg = open(kernel_path, 'wb') fimg.write(image) fimg.close() - print 'Saved as: %s' % kernel_name + print('Saved as: %s' % kernel_name) def create(mem, opath, ipath): @@ -123,7 +126,7 @@ def create(mem, opath, ipath): error('No output file selected') if not os.path.exists(ipath): - error('Path does not exist.') + error('CBF: Path does not exist.') elif os.path.isdir(ipath): error('Path is not a file.') elif 'zImage' not in image_name and 'Image' not in image_name: @@ -138,7 +141,7 @@ def create(mem, opath, ipath): p = packer(mem, opath, ipath) p.pack() summary(os.path.join(image_path, opath)) - except Exception, e: + except Exception as e: error(e) @@ -147,16 +150,16 @@ def summary(path): p = parse(path) p.create_summary() - print 'CBF File Summary:' + print('CBF File Summary:') - for k,v in p.summary.iteritems(): + for k,v in p.summary.items(): if len(k) < 7: tab = '\t\t' else: tab = '\t' - print '%s:%s0x%08x' % (k,tab,v) + print('%s:%s0x%08x' % (k,tab,v)) - print 'Compressed: \t %s' % p.is_compressed + print('Compressed: \t %s' % p.is_compressed) @@ -210,7 +213,7 @@ def get_image(self): f.close() return image - except Exception, e: + except Exception as e: self.error(e) @@ -220,15 +223,14 @@ class packer(object): def __init__(self, mem, opath, ipath): self._in_path = ipath self._out_path = opath - self._summary = '' - self._summary_crc = '' - self._buffer = '' - self._buffer_crc = '' + self._summary = b'' + self._summary_crc = b'' + self._buffer = b'' + self._buffer_crc = b'' self._size = 0 - - if mem.lower() == 'superhigh': - self._kernel_jump = KERNEL_JUMP_28 - self._kernel_load = KERNEL_LOAD_28 + if mem.lower() == 'superhigh': + self._kernel_jump = KERNEL_JUMP_28 + self._kernel_load = KERNEL_LOAD_28 elif mem.lower() == 'high': self._kernel_jump = KERNEL_JUMP_10 self._kernel_load = KERNEL_LOAD_10 @@ -266,19 +268,21 @@ def pack(self): rem = buf_len % BLOCK_SIZE pad_len = 0 + padding = b'' if rem != 0: pad_len = BLOCK_SIZE - rem - padding = pad_len * '\xFF' + padding = pad_len * b'\xFF' + buf += padding - print 'Writing CBF file.' + print('Writing CBF file.') f = open(self._out_path, 'wb') f.write(buf) f.close() - except Exception, e: + except Exception as e: error(e) @@ -303,9 +307,7 @@ def set_summary(self): self._summary = MAGIC_NUMBER self._summary += struct.pack('IIII', CBF_VERSION, self._kernel_load, self._kernel_jump, self._size) self._summary_crc = self.crc(self._summary) - self._summary += struct.pack('I', self._summary_crc) - -if __name__ == '__main__': - print 'No examples yet.' - + self._summary += struct.pack('I', self._summary_crc) +if __name__ == '__main__': + print('No examples yet.') diff --git a/interface.py b/interface.py index 1d4b152..f0dee7f 100644 --- a/interface.py +++ b/interface.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ############################################################################## # OpenLFConnect # @@ -31,6 +31,8 @@ #@ # services/interface.py Version 0.5 +# Ported to Python 3 by A.McCarthy + class config(object): def __init__(self, connection): self._connection = connection @@ -47,7 +49,7 @@ def get_root_dir(self): def get_device_id(self): try: return self._connection.get_device_id_i() - except Exception, e: + except Exception as e: self._connection.rerror(e) device_id = property(get_device_id) @@ -57,7 +59,7 @@ def get_device_id(self): def get_host_id(self): try: return self._connection.get_host_id_i() - except Exception, e: + except Exception as e: self._connection.rerror(e) host_id = property(get_host_id) @@ -66,8 +68,8 @@ def get_host_id(self): def is_connected(self): try: return self._connection.is_connected_i() - except Exception, e: + except Exception as e: self._connection.rerror(e) if __name__ == '__main__': - print 'No examples yet.' \ No newline at end of file + print('No examples yet.') diff --git a/make_cbf.py b/make_cbf.py index 89d0250..b4f5262 100644 --- a/make_cbf.py +++ b/make_cbf.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import cbf import sys diff --git a/mount.py b/mount.py index e554bf8..16afeaa 100644 --- a/mount.py +++ b/mount.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ############################################################################## # OpenLFConnect # @@ -31,6 +31,8 @@ #@ # mount.py Version 0.5.2 +# Ported to Python 3 by A.McCarthy + import os import re import sys @@ -114,13 +116,13 @@ def sg_scan(self): if not err: ret = p.stdout.read() - if self._vendor_name in ret.lower(): + if self._vendor_name in str(ret.lower(), 'utf-8'): return ret else: return '' else: return '' - except Exception, e: + except Exception as e: self.error(e) @@ -131,7 +133,7 @@ def find_device_id(self): while time_out: if sys.platform == 'win32': - lines = self.sg_scan().split('\n') + lines = str(self.sg_scan(), 'utf-8').split('\n') if lines: for line in lines: if self._vendor_name in line.lower(): @@ -168,7 +170,7 @@ def find_device_id(self): time_out -= 1 sleep(1) self.error('Device not found.') - except Exception, e: + except Exception as e: self.error(e) @@ -211,7 +213,7 @@ def find_mount_point(self, win_label='didj'): sleep(1) timeout -= 1 self.error('Mount not found.') - except Exception, e: + except Exception as e: self.error(e) @@ -228,7 +230,7 @@ def get_root_dir_i(self): def get_device_id_i(self): try: return self._device_id or self.find_device_id() - except Exception, e: + except Exception as e: self.error(e) @@ -236,7 +238,7 @@ def get_device_id_i(self): def get_host_id_i(self): try: return self._mount_point or self.find_mount_point() - except Exception, e: + except Exception as e: self.error(e) @@ -244,8 +246,8 @@ def get_host_id_i(self): def is_connected_i(self): try: return os.path.exists(self._mount_point) - except Exception, e: + except Exception as e: self.error(e) if __name__ == '__main__': - print 'No examples yet.' \ No newline at end of file + print('No examples yet.') diff --git a/pager.py b/pager.py index 5231593..5f1a2f4 100644 --- a/pager.py +++ b/pager.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ############################################################################## # OpenLFConnect # @@ -31,6 +31,8 @@ #@ # pager.py Version 0.7 +# Ported to Python 3 by A.McCarthy + import os import sys import struct @@ -73,20 +75,27 @@ def upload(self, path): self.error('Surgeon not found.') cbf.check(path) - buf = '' + buf = b'' + packets = 0 + with open(path, 'rb') as f: buf = f.read() f.close() buf_len = len(buf) + packet_leftovers = buf_len % cbf.PACKET_SIZE + if packet_leftovers > 0: padding_size = cbf.PACKET_SIZE - packet_leftovers - buf += struct.pack('%ss' % padding_size, '\xFF'*padding_size) + buf += struct.pack('%ss' % padding_size, b'\xFF'*padding_size) buf_len = len(buf) - packets = buf_len/cbf.PACKET_SIZE + + # Use of int function - should be ok as already checked in cbf to ensure buffer/packet size = whole number + packets = int(buf_len/cbf.PACKET_SIZE) + print(packets) byte1 = '00' total = 0 @@ -96,9 +105,10 @@ def upload(self, path): cmdl = '%s %s -b -s %s -n 2A 00 00 00 00 %s 00 00 20 00' % (self._sg_raw, self._mount_config.device_id, cbf.PACKET_SIZE, byte1) cmd = shlex_split(cmdl) byte1 = '01' + #print(cmd) p = Popen(cmd, stdin=PIPE, stderr=PIPE) p.stdin.write(buf[last_total:last_total+cbf.PACKET_SIZE]) - err = p.stderr.read() + err = str(p.stderr.read(), 'utf-8') if not 'Good' in err: self.error('SCSI error.') @@ -110,15 +120,9 @@ def upload(self, path): if len(err) != 0: self.error('SCSI error.') - except Exception, e: + except Exception as e: self.rerror(e) if __name__ == '__main__': - print 'No examples yet.' - - - - - - + print('No examples yet.') diff --git a/remote_flash.sh b/remote_flash.sh index b3c7615..5d6cd5c 100755 --- a/remote_flash.sh +++ b/remote_flash.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # We use a public/private keypair to authenticate. # Surgeon uses the 169.254.8.X subnet to differentiate itself from @@ -35,8 +35,8 @@ boot_surgeon () { surgeon_path=$1 memloc=$2 echo "Booting the Surgeon environment..." - python2 make_cbf.py $memloc $surgeon_path surgeon_tmp.cbf - sudo python2 boot_surgeon.py surgeon_tmp.cbf + python3 make_cbf.py $memloc $surgeon_path surgeon_tmp.cbf + sudo python3 boot_surgeon.py surgeon_tmp.cbf echo -n "Done! Waiting for Surgeon to come up..." rm surgeon_tmp.cbf sleep 15 @@ -95,7 +95,7 @@ flash_nand () { if [[ $prefix == lf1000_* ]]; then memloc="high" kernel="zImage_tmp.cbf" - python2 make_cbf.py $memloc ${prefix}zImage $kernel + python3 make_cbf.py $memloc ${prefix}zImage $kernel else memloc="superhigh" kernel=${prefix}uImage