Skip to content

Instantly share code, notes, and snippets.

@prozacchiwawa
Created December 29, 2023 19:31
Show Gist options
  • Save prozacchiwawa/4ce92ea67b4164fa754e785e9e881a05 to your computer and use it in GitHub Desktop.
Save prozacchiwawa/4ce92ea67b4164fa754e785e9e881a05 to your computer and use it in GitHub Desktop.
RS/6000 nvram checksum algorithm
def rol(r,n):
return (r << n) | (r >> (32 - n))
def crcgen(r3,r4):
r6 = rol(r3,8) & 0xff00
r0 = rol(r4,0) & 0xff
r3 = rol(r3,24) & 0xffffff
r4 = r3 ^ r0
r0 = rol(r4,8) & 0xffffff00
r0 = rol(r0,20) & 0xfffff
r7 = rol(r4,5) & 0x1fe0
r8 = rol(r4,12) & 0xfffff000
r0 = r0 | r8
r3 = rol(r4,1) & 0x1e0
r0 = r0 & 0xf00f
r5 = rol(r4,8) & 0xf000
r4 = r4 | r6
r0 = r0 ^ r4
r0 = r7 ^ r0
r0 = r5 ^ r0
r3 = r3 ^ r0
return r3
def checksum(location,data):
checksum = 0xffff
for i,b in enumerate(data):
offset = location + i
if offset >= 4 and offset < 8:
continue
checksum = crcgen(checksum, b)
return checksum
if __name__ == "__main__":
import sys
file_data = open(sys.argv[1], 'rb').read()
print(hex(checksum(0x1fd1, file_data[0x1fd1:0x2000])))
print(hex(checksum(0, file_data[:0x1a00])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment