Skip to content

Instantly share code, notes, and snippets.

@angelsl
Last active October 15, 2017 16:56
Show Gist options
  • Save angelsl/af5e458d658ee6e83ef55ffa291a0964 to your computer and use it in GitHub Desktop.
Save angelsl/af5e458d658ee6e83ef55ffa291a0964 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# Parses the ROM => NOR map of r4isdhc family carts
import struct, sys
canonical = (
'''N.B. mapping does not apply to 0x00 header read and 0x20 secure area read
Header [0x0, 0x200) => NOR [0x1F0000, 0x1F0200)
Header [0x200, 0x1000) => zeroes (presumably past 0x1000 too)
Secure area [0x4000, 0x8000) => NOR [0x1F4000, 0x1F8000)'''
)
NOR_SIZE = 0x200000 # 2M
def main():
if len(sys.argv) < 2:
print("usage: parse_nor_map.py <NOR file>")
return
with open(sys.argv[1], 'rb') as f:
f.seek(0x40)
mapping = f.read(0x100)
rom_off = struct.unpack(">32I", mapping[:0x80])
nor_off = struct.unpack(">32I", mapping[0x80:])
del mapping
for i in range(32):
romlb = rom_off[i]
romub = -1
j = i+1
while romub < romlb and j < 32:
romub = rom_off[j]
j += 1
if i == 31 or romub < romlb:
romlb = rom_off[i]
norlb = nor_off[i] % NOR_SIZE
print("ROM [0x{:X}, ... => NOR [0x{:X}, ...".format(romlb, norlb))
break
norlb = nor_off[i] % NOR_SIZE
while romlb < romub:
mapsize = min(romub - romlb, NOR_SIZE - norlb)
print("ROM [0x{:X}, 0x{:X}) => NOR [0x{:X}, 0x{:X})".format(romlb, romlb+mapsize, norlb, norlb+mapsize))
romlb += mapsize
norlb = (norlb + mapsize) % NOR_SIZE
print()
print(canonical)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment