-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn64crc.py
126 lines (94 loc) · 3.25 KB
/
n64crc.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# ============================================
# MIPS-CodeWrite
# Author: Nayla Hanegan ([email protected])
# Date: 6/20/2024
# License: GPLv2
# ============================================
# Copyright (C) 2005 Parasyte
# Copyright (C) 2021 Daniel K. O.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# This code is ported from the C code at http://n64dev.org/n64crc.html
# Based on uCON64's N64 checksum algorithm by Andreas Sterbenz
from binascii import crc32
import sys
def read32(data : bytearray, offset):
return int.from_bytes(data[offset : offset+4],
byteorder='big')
def write32(val):
return val.to_bytes(4, byteorder='big')
def mask(x):
return x & 0xffffffff
def rotl(val, s):
return mask(val << s) | (val >> (32 - s))
def get_cic(data : bytearray):
c = crc32(data[64:4096])
if c == 0x6170a4a1:
return 6101
if c == 0x90bb6cb5:
return 6102
if c == 0x0b050ee0:
return 6103
if c == 0x98bc2c86:
return 6105
if c == 0xacc8580a:
return 6106
if c == 0x009e9ea3:
return 7102
return 6105
def get_crc_seed(cic):
if cic in [6101, 6102, 7102]:
return 0xf8ca4ddc
if cic == 6103:
return 0xa3886759
if cic == 6105:
return 0xdf26f436
if cic == 6106:
return 0x1fea617a
return 1
def crc(data : bytearray):
cic = get_cic(data)
seed = get_crc_seed(cic)
t1 = t2 = t3 = t4 = t5 = t6 = seed
# CRC1/CRC2 are calculated from offset 0x1000 to 0x101000
# Impl. note: XOR operations don't need to be masked to 32 bits.
for offset in range(0x1000, 0x101000, 4):
d = read32(data, offset)
if mask(t6 + d) < t6:
t4 = mask(t4 + 1)
t6 = mask(t6 + d)
t3 ^= d
r = rotl(d, (d & 0x1f))
t5 = mask(t5 + r)
t2 ^= r if t2 > d else t6 ^ d
if cic == 6105:
t1 = mask(t1 + (d ^ read32(data, 0x750 + (offset & 0xff))))
else:
t1 = mask(t1 + (d ^ t5))
if cic == 6103:
return mask((t6 ^ t4) + t3), mask((t5 ^ t2) + t1)
if cic == 6106:
return mask((t6 * t4) + t3), mask((t5 * t2) + t1)
return mask(t6 ^ t4 ^ t3), mask(t5 ^ t2 ^ t1)
if __name__ == '__main__':
with open(sys.argv[1], "rb") as f:
rom = f.read(0x101000)
cic = get_cic(rom)
h1 = int.from_bytes(rom[16:20], byteorder='big')
h2 = int.from_bytes(rom[20:24], byteorder='big')
c1, c2 = crc(rom)
print("CIC:", cic)
print("header: {:08x} - {:08x}".format(h1, h2))
print(" calc: {:08x} - {:08x}".format(c1, c2))