diff --git a/a3driverutil.py b/a3driverutil.py index 11d18b5..b2d1d76 100644 --- a/a3driverutil.py +++ b/a3driverutil.py @@ -135,12 +135,12 @@ def readUnpack(file,bytes, **options): # this function reads a word from a string at the specified # offset and returns an integer def readWord(data,startpos): - return ord(data[startpos+1])*256+ord(data[startpos]) + return data[startpos+1]*256 + data[startpos] # this function reads a byte from a string at the specified # offset and returns an integer def readByte(data,startpos): - return ord(data[startpos]) + return data[startpos] # # this function reads in a o65 binary file of a driver and @@ -229,7 +229,7 @@ def convert_o65(file): reloctable.append(offset_address) offset = readUnpack(o65file,1,type = '1') else: - print 'Error, only 16 bit word offsets allowed, ie no lda #
address' + print('Error, only 16 bit word offsets allowed, ie no lda #address') o65file.close() exit() @@ -244,7 +244,7 @@ def convert_o65(file): return driver #return the converted driver binary else: - print 'not o65 input file' + print('not o65 input file') o65file.close() exit() @@ -258,8 +258,9 @@ def convert_o65(file): # def parsedriverfile(filecontents): filetype = filecontents[0:8] #check for 'SOS DRVR' header + filetype = filetype.decode('ascii') if filetype != 'SOS DRVR': - print "INVALID SOS.DRIVER file" + print("INVALID SOS.DRIVER file") exit() drivers_list = [] @@ -292,7 +293,7 @@ def parseDIB(filedata,offset,dib): driver_details={} driver_details['dib_num'] = dib driver_details['name_len'] = readByte(filedata,offset+6) - driver_details['name'] = filedata[offset+7:offset+7+driver_details['name_len']] + driver_details['name'] = filedata[offset+7:offset+7+driver_details['name_len']].decode('ascii') driver_details['status'] = readByte(filedata,offset+22) driver_details['slot'] = readByte(filedata,offset+23) driver_details['unit'] = readByte(filedata,offset+24) @@ -336,7 +337,7 @@ if args.command == 'bin': bin_file = args.binfile outfile = open(bin_file,'wb') outfile.write(driver) - print 'File converted and written as raw binary file to:',bin_file + print('File converted and written as raw binary file to:',bin_file) outfile.close() # Convert file and output as SOS.DRIVER format @@ -362,7 +363,7 @@ elif args.command == 'sos': outfile = open(sos_file,'wb') outfile.write(header + driver + pack('>H',0xFFFF)) #add the end marker - print 'File converted and written as SOS.DRIVER binary file to:',sos_file + print('File converted and written as SOS.DRIVER binary file to:',sos_file) outfile.close() #Convert and add to an existing SOS.DRIVER file @@ -397,11 +398,11 @@ elif args.command == 'add': sosdriver.write(newsosdriverfile) sosdriver.close() - print 'Driver: ' + driver_name + ' added to ' + sos_file + print('Driver: ' + driver_name + ' added to ' + sos_file) else: #found, report error - print 'Driver: ' + driver_name + ' elready exists in ' + sos_file + ', not added' + print('Driver: ' + driver_name + ' elready exists in ' + sos_file + ', not added') #List drivers in a SOS.DRIVER file @@ -423,7 +424,7 @@ elif args.command == 'list': driver_details.append(parseDIB(filedata,offset+nextdib,dib)) nextdib = readWord(filedata,offset+nextdib+2) #next dib of this driver - print 'DriverName Status Slot Unit Manid Release' + print('DriverName Status Slot Unit Manid Release') for i in range(0,len(driver_details)): #decode status byte if driver_details[i]['status'] & 0x80 == 0x80: @@ -437,11 +438,13 @@ elif args.command == 'list': slot = driver_details[i]['slot'] if driver_details[i]['dib_num'] == 0: #don't indent the first DIB - print '{:16} {:10} {:3} {:02X} {:04X} {:04X}'.format(driver_details[i]['name'], status, slot, driver_details[i]['unit'],driver_details[i]['manid'],driver_details[i]['release']) + #print('{:16} {:10} {:3} {:02X} {:04X} {:04X}'.format(driver_details[i]['name'], status, slot, driver_details[i]['unit'],driver_details[i]['manid'],driver_details[i]['release'])) + print('{} {} {} {:02X} {:04X} {:04X}'.format(driver_details[i]['name'].decode('ascii').ljust(16,' '), status.ljust(10,' '), str(slot).ljust(3,' '), driver_details[i]['unit'], driver_details[i]['manid'],driver_details[i]['release'])) else: #otherwise indent the rest, ie sub devices - print ' {:16}{:10} {:3} {:02X} {:04X} {:04X}'.format(driver_details[i]['name'], status, slot, driver_details[i]['unit'],driver_details[i]['manid'],driver_details[i]['release']) + #print(' {:16}{:10} {:3} {:02X} {:04X} {:04X}'.format(driver_details[i]['name'], status, slot, driver_details[i]['unit'],driver_details[i]['manid'],driver_details[i]['release'])) + print(' {}{} {} {:02X} {:04X} {:04X}'.format(driver_details[i]['name'].decode('ascii').ljust(16,' '), status.ljust(10,' '), str(slot).ljust(3,' '), driver_details[i]['unit'], driver_details[i]['manid'],driver_details[i]['release'])) - print '\n Total size: ',len(filedata) + print('\n Total size: ',len(filedata)) #Convert and update an existing driver in a SOS.DRIVER file @@ -450,7 +453,7 @@ elif args.command == 'update': driver_name = getDriverName(driver) #extract the driver name from the driver - print 'Driver in o65 file: ',driver_name + print('Driver in o65 file: ',driver_name) sos_file = args.sosfile #read in the existing SOS.DRIVER file sosdriver = open(sos_file,'rb') @@ -470,7 +473,7 @@ elif args.command == 'update': if i != -1: #found it - print 'Driver found in SOS.DRIVER, updating..' + print('Driver found in SOS.DRIVER, updating..') #print drivers_list newsosdriverfile = sosdriverfile[0:drivers_list[i]['comment_start']] #part up to target driver @@ -485,11 +488,11 @@ elif args.command == 'update': sosdriver.write(newsosdriverfile) sosdriver.close() - print 'Driver: ' + driver_name + ' updated!' + print('Driver: ' + driver_name + ' updated!') else: #not found - print 'Driver: ' + driver_name + ' not found in SOS.DRIVER file' + print('Driver: ' + driver_name + ' not found in SOS.DRIVER file') #Delete an existing driver in a SOS.DRIVER file @@ -514,7 +517,7 @@ elif args.command == 'delete': if i != -1: #found it - print 'Driver found in SOS.DRIVER, deleting..' + print('Driver found in SOS.DRIVER, deleting..') newsosdriverfile = sosdriverfile[0:drivers_list[i]['comment_start']] #part up to target driver @@ -527,11 +530,11 @@ elif args.command == 'delete': sosdriver.write(newsosdriverfile) sosdriver.close() - print 'Driver: ' + driver_name + ' deleted!' + print('Driver: ' + driver_name + ' deleted!') else: #not found - print 'Driver: ' + driver_name + ' not found in SOS.DRIVER file' + print('Driver: ' + driver_name + ' not found in SOS.DRIVER file') @@ -552,11 +555,12 @@ elif args.command == 'extract': offset = drivers_list[i]['code_start'] driver_details.append(parseDIB(sosdriverfile,offset,0)) #we always use dib0 + print(driver_details) i = find(driver_details,'name',driver_name.upper()) #find index of the driver to extract, convert name to uppercase if i != -1: #found it - print 'Driver found in SOS.DRIVER, extracting..' + print('Driver found in SOS.DRIVER, extracting..') if i < len(drivers_list)-1: #check if its not the last one in sos.driver extracted_driver = sosdriverfile[drivers_list[i]['comment_start']:drivers_list[i+1]['comment_start']] @@ -569,11 +573,11 @@ elif args.command == 'extract': driverfile.write(extracted_driver) driverfile.close() - print 'Driver: ' + driver_name + ' extracted and written to file: ' + filename + print('Driver: ' + driver_name + ' extracted and written to file: ' + filename) else: #not found - print 'Driver: ' + driver_name + ' not found in SOS.DRIVER file' + print('Driver: ' + driver_name + ' not found in SOS.DRIVER file') #Extract a drivers code from a SOS.DRIVER file and relocate to 0x2000 to aid disassembly # relocating to something other than 0x0000 helps to remove zero page ambiguities when @@ -598,7 +602,7 @@ elif args.command == 'extractcode': if i != -1: #found it - print 'Driver found in SOS.DRIVER, extracting code..' + print('Driver found in SOS.DRIVER, extracting code..') #grab the code extracted_driver_code = sosdriverfile[drivers_list[i]['code_start']+2:drivers_list[i]['reloc_start']] #skip the code length(+2) @@ -621,7 +625,11 @@ elif args.command == 'extractcode': for i in range(0,len(extracted_driver_code)): byte = readByte(extracted_driver_code,i) if i == offset_table[j]+1: #looking at high byte - extracted_driver_code = extracted_driver_code[:i] + chr(byte + 0x20) + extracted_driver_code[i+1:] #add to the existing address high byte + #print('something: {}'.format(extracted_driver_code[:i])) + #print('extracted_driver_code: {}'.format(extracted_driver_code)) + #extracted_driver_code[i+1] = extracted_driver_code[i+1] + 0x20 + #extracted_driver_code = extracted_driver_code + (byte + 0x20) + extracted_driver_code[i+1] #add to the existing address high byte + extracted_driver_code = extracted_driver_code[:i] + byte.to_bytes(1,"little") + ( b'\x20') + extracted_driver_code[i+1:] #add to the existing address high byte if j < (len(offset_table)-1): j += 1 @@ -630,8 +638,8 @@ elif args.command == 'extractcode': driverfile.write(extracted_driver_code) driverfile.close() - print 'Driver: ' + driver_name + ' extracted, relocated and written to file: ' + filename + print('Driver: ' + driver_name + ' extracted, relocated and written to file: ' + filename) else: #not found - print 'Driver: ' + driver_name + ' not found in SOS.DRIVER file' + print('Driver: ' + driver_name + ' not found in SOS.DRIVER file')