Skip to content

Instantly share code, notes, and snippets.

@GeoffWilliams
Created September 21, 2019 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeoffWilliams/708e31ee847d21b43e23440430d0c331 to your computer and use it in GitHub Desktop.
Save GeoffWilliams/708e31ee847d21b43e23440430d0c331 to your computer and use it in GitHub Desktop.
Compare disk images block-by-block, print out index of sectors with changes and save the different blocks to file
import os
afile = "sdcard.img"
bfile = "sdcard.img.manualworks"
afilediff = "sdcard.img.diff"
bfilediff = "sdcard.img.manualworks.diff"
afile_size = os.path.getsize(afile)
bfile_size = os.path.getsize(bfile)
if afile_size != bfile_size:
raise "different sizes!"
incident = 0
with open(afile, 'rb') as af:
with open(bfile, 'rb') as bf:
pos = 0
while(pos < afile_size):
pos = af.tell()
a_sect = af.read(512)
b_sect = bf.read(512)
if (a_sect != b_sect):
print(f"different data at byte {pos} (sector {pos/512})")
print("a_file")
print(str(a_sect))
print("b_file")
print(str(b_sect))
print()
with open(f"afilediff-{incident}", 'wb') as afd:
afd.write(a_sect)
with open(f"bfilediff-{incident}", 'wb') as bfd:
bfd.write(b_sect)
incident += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment