Skip to content

Instantly share code, notes, and snippets.

@angelsl
Created October 15, 2017 16:56
Show Gist options
  • Save angelsl/da7f1d4ed4d11ec0a9ce32156c747102 to your computer and use it in GitHub Desktop.
Save angelsl/da7f1d4ed4d11ec0a9ce32156c747102 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 runstreak(c):
h = f2h[c]
if h == "5c3eb80066420002bc3dcc7ca4ab6efad7ed4ae5":
print("ROM 0x{:X} => zeroes".format(c*BLOCKSIZE))
return 1
offs = f1d.get(h, None)
if offs is None:
print("ROM 0x{:X} => not in NOR".format(c*BLOCKSIZE));
return 1
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:
run = 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