-
Notifications
You must be signed in to change notification settings - Fork 12
/
animatedbannerpatch.py
60 lines (53 loc) · 1.8 KB
/
animatedbannerpatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
from argparse import ArgumentParser
from struct import pack
# GBATEK swiCRC16 pseudocode
# https://problemkaputt.de/gbatek-bios-misc-functions.htm
def crc16(data):
crc = 0xFFFF
for byte in bytearray(data):
crc ^= byte
for i in range(8):
carry = (crc & 0x0001) > 0
crc = crc >> 1
if carry:
crc = crc ^ 0xA001
return pack("<H", crc)
def patch(path, banner):
# we're going to make this look nice
filesize = os.path.getsize(path)
while filesize % 16 != 0:
filesize += 1
with open(path, "rb+") as outfile:
# new animated banner location
outfile.seek(0x68, 0)
outfile.write(pack("<I", filesize))
outfile.seek(0x208, 0)
outfile.write(b'\xC0')
outfile.seek(0x209, 0)
outfile.write(b'\x23')
outfile.seek(filesize, 0)
infile = open(banner, "rb")
outfile.write(infile.read())
infile.close()
# update header CRC
outfile.seek(0, 0)
crc = crc16(outfile.read(0x15E))
outfile.seek(0x15E, 0)
outfile.write(crc)
outfile.close()
return 0
if __name__ == "__main__":
description = "Animated banner injector tool\n\
This must have a prepared animated banner binary!"
parser = ArgumentParser(description=description)
parser.add_argument("input", metavar="input.nds", type=str, help="DS ROM path")
parser.add_argument("banner", metavar="banner.bin", type=str, help="Animated banner path")
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
args = parser.parse_args()
if args.verbose:
print(description)
path = args.input
banner = args.banner
if patch(path, banner) == 0 and args.verbose:
print("\nSuccess.")