2020-03-28 02:30:19 +00:00
|
|
|
from os import path
|
|
|
|
import sys
|
|
|
|
import struct
|
2020-03-30 23:38:51 +00:00
|
|
|
import hashlib
|
2020-03-28 02:30:19 +00:00
|
|
|
|
2020-03-31 20:41:02 +00:00
|
|
|
|
2020-04-23 18:53:08 +00:00
|
|
|
def get_str_hash(byte_array):
|
|
|
|
return str(hashlib.md5(byte_array).hexdigest())
|
|
|
|
|
|
|
|
|
|
|
|
# If the baserom exists and is correct, we don't need to change anything
|
|
|
|
if path.exists("baserom.z64"):
|
|
|
|
with open("baserom.z64", mode="rb") as file:
|
|
|
|
fileContent = bytearray(file.read())
|
|
|
|
if get_str_hash(fileContent) == "f0b7f35375f9cc8ca1b2d59d78e35405":
|
|
|
|
print("Found valid baserom - exiting early")
|
|
|
|
sys.exit(0)
|
|
|
|
|
2020-03-31 20:41:02 +00:00
|
|
|
# Determine if we have a ROM file
|
|
|
|
romFileName = ""
|
2020-04-23 18:53:08 +00:00
|
|
|
if path.exists("baserom_original.z64"):
|
2020-03-31 20:41:02 +00:00
|
|
|
romFileName = "baserom_original.z64"
|
2020-04-23 18:53:08 +00:00
|
|
|
elif path.exists("baserom_original.n64"):
|
2020-03-31 20:41:02 +00:00
|
|
|
romFileName = "baserom_original.n64"
|
|
|
|
|
|
|
|
# Read in the original ROM
|
2020-04-23 18:53:08 +00:00
|
|
|
if romFileName != "":
|
2020-03-31 20:41:02 +00:00
|
|
|
print("File '" + romFileName + "' found.")
|
2020-04-23 18:53:08 +00:00
|
|
|
with open(romFileName, mode="rb") as file:
|
2020-03-28 02:30:19 +00:00
|
|
|
fileContent = bytearray(file.read())
|
2020-03-30 23:38:51 +00:00
|
|
|
|
|
|
|
# Check if ROM needs to be byte swapped
|
2020-04-23 18:53:08 +00:00
|
|
|
if fileContent[0] == 0x40:
|
|
|
|
# Byte Swap ROM
|
2020-03-30 23:38:51 +00:00
|
|
|
# TODO: This is pretty slow at the moment. Look into optimizing it later...
|
|
|
|
print("ROM needs to be byte swapped...")
|
|
|
|
i = 0
|
2020-04-23 18:53:08 +00:00
|
|
|
while i < len(fileContent):
|
2020-03-30 23:38:51 +00:00
|
|
|
tmp = struct.unpack_from("BBBB", fileContent, i)
|
|
|
|
struct.pack_into("BBBB", fileContent, i + 0, tmp[3], tmp[2], tmp[1], tmp[0])
|
|
|
|
i += 4
|
|
|
|
|
|
|
|
perc = float(i) / float(len(fileContent))
|
|
|
|
|
2020-04-23 18:53:08 +00:00
|
|
|
if i % (1024 * 1024 * 4) == 0:
|
2020-03-30 23:38:51 +00:00
|
|
|
print(str(perc * 100) + "%")
|
2020-03-28 02:30:19 +00:00
|
|
|
|
2020-03-30 23:38:51 +00:00
|
|
|
print("Byte swapping done.")
|
2020-03-28 02:30:19 +00:00
|
|
|
else:
|
2020-03-31 20:41:02 +00:00
|
|
|
print("Error: Could not find baserom_original.z64/baserom_original.n64.")
|
2020-03-28 02:30:19 +00:00
|
|
|
sys.exit(1)
|
2020-03-31 20:41:02 +00:00
|
|
|
|
2020-03-28 02:30:19 +00:00
|
|
|
# Strip the overdump
|
|
|
|
print("Stripping overdump...")
|
|
|
|
strippedContent = list(fileContent[0:0x3600000])
|
|
|
|
|
|
|
|
# Patch the header
|
|
|
|
print("Patching header...")
|
|
|
|
strippedContent[0x3E] = 0x50
|
|
|
|
|
2020-03-30 23:38:51 +00:00
|
|
|
# Check to see if the ROM is a "vanilla" Debug ROM
|
2020-04-23 18:57:40 +00:00
|
|
|
str_hash = get_str_hash(bytearray(strippedContent))
|
2020-04-23 18:53:08 +00:00
|
|
|
if str_hash != "f0b7f35375f9cc8ca1b2d59d78e35405":
|
|
|
|
print("Error: Expected a hash of f0b7f35375f9cc8ca1b2d59d78e35405 but got " + str_hash + ". " +
|
|
|
|
"The baserom has probably been tampered, find a new one")
|
2020-03-30 23:38:51 +00:00
|
|
|
sys.exit(1)
|
2020-03-28 02:30:19 +00:00
|
|
|
|
|
|
|
# Write out our new ROM
|
|
|
|
print("Writing new ROM 'baserom.z64'.")
|
|
|
|
with open("baserom.z64", mode="wb") as file:
|
|
|
|
file.write(bytes(strippedContent))
|
|
|
|
|
|
|
|
print("Done!")
|