Skip to content

Instantly share code, notes, and snippets.

@angelsl
Created December 9, 2017 09:09
Show Gist options
  • Save angelsl/b1204efada9f78769ad776451d905d9e to your computer and use it in GitHub Desktop.
Save angelsl/b1204efada9f78769ad776451d905d9e to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import sys
import hashlib
BLOCKSIZE=0x200
f1d = {}
f2h = []
with open(sys.argv[1], 'rb') as f:
f1 = bytearray(f.read())
for i in range(0, len(f1), BLOCKSIZE):
s = f1d.setdefault(hashlib.sha1(f1[i:i+BLOCKSIZE]).hexdigest(), [])
s.append(i)
del f1
with open(sys.argv[2], 'rb') as f:
f2 = bytearray(f.read())
for i in range(0, len(f2), BLOCKSIZE):
f2h.append(hashlib.sha1(f2[i:i+BLOCKSIZE]).hexdigest())
del f2
def getoffs(c):
h = f2h[c]
if h == "5c3eb80066420002bc3dcc7ca4ab6efad7ed4ae5":
return "zeroes"
if h == "9d0ac37bb3ec8c95990fd37a962a17a95ce97aa0":
return "0xFF"
if h == "3da96bc604d8819346a5ca15bb73d38f891cd08a":
return "NOR 0x10000 repeating"
offs = f1d.get(h, None)
if offs is None:
return "not in NOR"
return offs
def dospecialrun(code, c):
run = 1
while True:
offs = getoffs(c+run)
if offs == code:
run += 1
else:
break
return run
def runstreak(c):
offs = getoffs(c)
if offs == 0xDEAD0000:
return 1
run = 1
if type(offs) != list:
run = dospecialrun(offs, c)
if run > 1:
print("ROM [0x{:X}, 0x{:X}) => {}".format(c*BLOCKSIZE, (c + run)*BLOCKSIZE, offs))
else:
print("ROM 0x{:X} => {}".format(c*BLOCKSIZE, offs))
return run
l = len(offs)
if l > 1:
print("ROM 0x{:X} => NOR {}".format(c*BLOCKSIZE, ", ".join(map(lambda x: "0x{:X}".format(x), offs))));
return 1
if l == 1:
startnor, lastnor = offs[0], offs[0]
while True:
oln = lastnor
if c+run >= len(f2h):
break
offs = f1d.get(f2h[c+run], None)
if offs is None:
break
l = len(offs)
if l == 0:
break
if l > 1:
if lastnor+BLOCKSIZE in offs:
lastnor += BLOCKSIZE
run += 1
continue
else:
break
if l == 1:
lastnor = offs[0]
if lastnor - oln == BLOCKSIZE:
run += 1
continue
else:
break
if run == 1:
print("ROM 0x{:X} => NOR 0x{:X}".format(c*BLOCKSIZE, startnor));
else:
print("ROM [0x{:X}, 0x{:X}) => NOR [0x{:X}, 0x{:X})".format(c*BLOCKSIZE, (c + run)*BLOCKSIZE, startnor, oln+BLOCKSIZE))
return run
l = len(f2h)
c = 0
while c < l:
c += runstreak(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment