def main(): inname = sys.argv[1] outname = sys.argv[2] # Read data in f = open(inname, 'rb') data = f.read() f.close() count = len(data) # Pad to a 512 byte boundary data += as_bytes("\0") * (alignpos(count, 512) - count) count = len(data) # Check if a pci header is present pcidata = ord(data[24:25]) + (ord(data[25:26]) << 8) if pcidata != 0: blocks = struct.pack('<H', int(count/512)) data = data[:pcidata + 16] + blocks + data[pcidata + 18:] # Fill in size field; clear checksum field blocks = struct.pack('<B', int(count/512)) data = data[:2] + blocks + data[3:6] + as_bytes("\0") + data[7:] # Checksum rom data = data[:6] + checksum(data) + data[7:] # Write new rom f = open(outname, 'wb') f.write(data) f.close()
def main(): inname = sys.argv[1] outname = sys.argv[2] # Read data in f = open(inname, 'rb') data = f.read() f.close() count = len(data) # Pad to a 512 byte boundary data += as_bytes("\0") * (alignpos(count, 512) - count) count = len(data) # Check if a pci header is present pcidata = ord(data[24:25]) + (ord(data[25:26]) << 8) if pcidata != 0: blocks = struct.pack('<H', int(count / 512)) data = data[:pcidata + 16] + blocks + data[pcidata + 18:] # Fill in size field; clear checksum field blocks = struct.pack('<B', int(count / 512)) data = data[:2] + blocks + data[3:6] + as_bytes("\0") + data[7:] # Checksum rom data = data[:6] + checksum(data) + data[7:] # Write new rom f = open(outname, 'wb') f.write(data) f.close()
def calibrateserialwrite(outfile, byteadjust): # Build 4000 bytes of dummy data. data = "0123456789" * 4 + "012345678" + "\n" data = data * 80 while 1: st = time.time() outfile.write(as_bytes(data)) outfile.flush() et = time.time() sys.stdout.write( "Wrote %d - %.1fus per char (theory states %.1fus)\n" % ( len(data), (et-st) / len(data) * 1000000, byteadjust * 1000000)) sys.stdout.flush() time.sleep(3)
def calibrateserialwrite(outfile, byteadjust): # Build 4000 bytes of dummy data. data = "0123456789" * 4 + "012345678" + "\n" data = data * 80 while 1: st = time.time() outfile.write(as_bytes(data)) outfile.flush() et = time.time() sys.stdout.write( "Wrote %d - %.1fus per char (theory states %.1fus)\n" % (len(data), (et - st) / len(data) * 1000000, byteadjust * 1000000)) sys.stdout.flush() time.sleep(3)
def readserial(infile, logfile, byteadjust): lasttime = 0 while 1: # Read data try: res = select.select([infile, sys.stdin], [], []) except KeyboardInterrupt: sys.stdout.write("\n") return -1 if sys.stdin in res[0]: # Got keyboard input - force reset on next serial input sys.stdin.read(1) lasttime = 0 if len(res[0]) == 1: continue d = infile.read(4096) if not d: return 0 datatime = time.time() datatime -= len(d) * byteadjust # Reset start time if no data for some time if datatime - lasttime > RESTARTINTERVAL: starttime = datatime charcount = 0 isnewline = 1 msg = "\n\n======= %s (adjust=%.1fus)\n" % ( time.asctime(time.localtime(datatime)), byteadjust * 1000000) sys.stdout.write(msg) logfile.write(as_bytes(msg)) lasttime = datatime # Translate unprintable chars; add timestamps out = as_bytes("") for c in d: if isnewline: delta = datatime - starttime - (charcount * byteadjust) out += "%06.3f: " % delta isnewline = 0 oc = ord(c) charcount += 1 datatime += byteadjust if oc == 0x0d: continue if oc == 0x00: out += "<00>\n" isnewline = 1 continue if oc == 0x0a: out += "\n" isnewline = 1 continue if oc < 0x20 or oc >= 0x7f and oc != 0x09: out += "<%02x>" % oc continue out += c if (sys.version_info > (3, 0)): sys.stdout.buffer.write(out) else: sys.stdout.write(out) sys.stdout.flush() logfile.write(out) logfile.flush()
def main(): # Get args objinfo, finalsize, rawfile, outfile = sys.argv[1:] # Read in symbols objinfofile = open(objinfo, 'r') symbols = layoutrom.parseObjDump(objinfofile, 'in')[1] # Read in raw file f = open(rawfile, 'rb') rawdata = f.read() f.close() datasize = len(rawdata) finalsize = int(finalsize) * 1024 if finalsize == 0: finalsize = 64 * 1024 if datasize > 64 * 1024: finalsize = 128 * 1024 if datasize > 128 * 1024: finalsize = 256 * 1024 if datasize > finalsize: print("Error! ROM doesn't fit (%d > %d)" % (datasize, finalsize)) print(" You have to either increate the size (CONFIG_ROM_SIZE)") print(" or turn off some features (such as hardware support not") print(" needed) to make it fit. Trying a more recent gcc version") print(" might work too.") sys.exit(1) # Sanity checks start = symbols['code32flat_start'].offset end = symbols['code32flat_end'].offset expend = layoutrom.BUILD_BIOS_ADDR + layoutrom.BUILD_BIOS_SIZE if end != expend: print("Error! Code does not end at 0x%x (got 0x%x)" % (expend, end)) sys.exit(1) if datasize > finalsize: print("Error! Code is too big (0x%x vs 0x%x)" % (datasize, finalsize)) sys.exit(1) expdatasize = end - start if datasize != expdatasize: print("Error! Unknown extra data (0x%x vs 0x%x)" % (datasize, expdatasize)) sys.exit(1) # Fix up CSM Compatibility16 table if 'csm_compat_table' in symbols and 'entry_csm' in symbols: # Field offsets within EFI_COMPATIBILITY16_TABLE ENTRY_FIELD_OFS = 14 # Compatibility16CallOffset (UINT16) SIZE_FIELD_OFS = 5 # TableLength (UINT8) CSUM_FIELD_OFS = 4 # TableChecksum (UINT8) tableofs = symbols['csm_compat_table'].offset - symbols[ 'code32flat_start'].offset entry_addr = symbols['entry_csm'].offset - layoutrom.BUILD_BIOS_ADDR entry_addr = struct.pack('<H', entry_addr) rawdata = subst(rawdata, tableofs + ENTRY_FIELD_OFS, entry_addr) tsfield = tableofs + SIZE_FIELD_OFS tablesize = ord(rawdata[tsfield:tsfield + 1]) rawdata = checksum(rawdata, tableofs, tablesize, CSUM_FIELD_OFS) # Print statistics runtimesize = end - symbols['code32init_end'].offset print("Total size: %d Fixed: %d Free: %d (used %.1f%% of %dKiB rom)" % (datasize, runtimesize, finalsize - datasize, (datasize / float(finalsize)) * 100.0, int(finalsize / 1024))) # Write final file f = open(outfile, 'wb') f.write((as_bytes("\0") * (finalsize - datasize)) + rawdata) f.close()
def main(): # Get args objinfo, finalsize, rawfile, outfile = sys.argv[1:] # Read in symbols objinfofile = open(objinfo, 'r') symbols = layoutrom.parseObjDump(objinfofile, 'in')[1] # Read in raw file f = open(rawfile, 'rb') rawdata = f.read() f.close() datasize = len(rawdata) finalsize = int(finalsize) * 1024 if finalsize == 0: finalsize = 64*1024 if datasize > 64*1024: finalsize = 128*1024 if datasize > 128*1024: finalsize = 256*1024 if datasize > finalsize: print("Error! ROM doesn't fit (%d > %d)" % (datasize, finalsize)) print(" You have to either increase the size (CONFIG_ROM_SIZE)") print(" or turn off some features (such as hardware support not") print(" needed) to make it fit. Trying a more recent gcc version") print(" might work too.") sys.exit(1) # Sanity checks start = symbols['code32flat_start'].offset end = symbols['code32flat_end'].offset expend = layoutrom.BUILD_BIOS_ADDR + layoutrom.BUILD_BIOS_SIZE if end != expend: print("Error! Code does not end at 0x%x (got 0x%x)" % ( expend, end)) sys.exit(1) if datasize > finalsize: print("Error! Code is too big (0x%x vs 0x%x)" % ( datasize, finalsize)) sys.exit(1) expdatasize = end - start if datasize != expdatasize: print("Error! Unknown extra data (0x%x vs 0x%x)" % ( datasize, expdatasize)) sys.exit(1) # Fix up CSM Compatibility16 table if 'csm_compat_table' in symbols and 'entry_csm' in symbols: # Field offsets within EFI_COMPATIBILITY16_TABLE ENTRY_FIELD_OFS = 14 # Compatibility16CallOffset (UINT16) SIZE_FIELD_OFS = 5 # TableLength (UINT8) CSUM_FIELD_OFS = 4 # TableChecksum (UINT8) tableofs = symbols['csm_compat_table'].offset - symbols['code32flat_start'].offset entry_addr = symbols['entry_csm'].offset - layoutrom.BUILD_BIOS_ADDR entry_addr = struct.pack('<H', entry_addr) rawdata = subst(rawdata, tableofs+ENTRY_FIELD_OFS, entry_addr) tsfield = tableofs+SIZE_FIELD_OFS tablesize = ord(rawdata[tsfield:tsfield+1]) rawdata = checksum(rawdata, tableofs, tablesize, CSUM_FIELD_OFS) # Print statistics runtimesize = end - symbols['code32init_end'].offset print("Total size: %d Fixed: %d Free: %d (used %.1f%% of %dKiB rom)" % ( datasize, runtimesize, finalsize - datasize , (datasize / float(finalsize)) * 100.0 , int(finalsize / 1024))) # Write final file f = open(outfile, 'wb') f.write((as_bytes("\0") * (finalsize - datasize)) + rawdata) f.close()
def readserial(infile, logfile, byteadjust): lasttime = 0 while 1: # Read data try: res = select.select([infile, sys.stdin], [], []) except KeyboardInterrupt: sys.stdout.write("\n") break if sys.stdin in res[0]: # Got keyboard input - force reset on next serial input sys.stdin.read(1) lasttime = 0 if len(res[0]) == 1: continue d = infile.read(4096) if not d: break datatime = time.time() datatime -= len(d) * byteadjust # Reset start time if no data for some time if datatime - lasttime > RESTARTINTERVAL: starttime = datatime charcount = 0 isnewline = 1 msg = "\n\n======= %s (adjust=%.1fus)\n" % (time.asctime( time.localtime(datatime)), byteadjust * 1000000) sys.stdout.write(msg) logfile.write(as_bytes(msg)) lasttime = datatime # Translate unprintable chars; add timestamps out = as_bytes("") for c in d: if isnewline: delta = datatime - starttime - (charcount * byteadjust) out += "%06.3f: " % delta isnewline = 0 oc = ord(c) charcount += 1 datatime += byteadjust if oc == 0x0d: continue if oc == 0x00: out += "<00>\n" isnewline = 1 continue if oc == 0x0a: out += "\n" isnewline = 1 continue if oc < 0x20 or oc >= 0x7f and oc != 0x09: out += "<%02x>" % oc continue out += c if (sys.version_info > (3, 0)): sys.stdout.buffer.write(out) else: sys.stdout.write(out) sys.stdout.flush() logfile.write(out) logfile.flush()