ctr_ncch: fix reading format v1

This commit is contained in:
xprism1 2025-01-19 22:48:49 +08:00
parent 2a34e6e4ad
commit 0305210f45

View File

@ -3,8 +3,6 @@ from .keys import *
from .ctr_exefs import ExeFSFileHdr from .ctr_exefs import ExeFSFileHdr
from .ctr_romfs import RomFSReader from .ctr_romfs import RomFSReader
media_unit = 0x200
class NCCHHdr(Structure): class NCCHHdr(Structure):
_fields_ = [ _fields_ = [
('sig', c_uint8 * 0x100), ('sig', c_uint8 * 0x100),
@ -58,12 +56,12 @@ def get_ncch_counter(hdr, component):
if component == 'exheader.bin': if component == 'exheader.bin':
x = 0x200 x = 0x200
elif component == 'exefs.bin': elif component == 'exefs.bin':
x = hdr.exefs_offset * media_unit x = hdr.exefs_offset
elif component == 'romfs.bin': elif component == 'romfs.bin':
x = hdr.romfs_offset * media_unit x = hdr.romfs_offset
counter[:8] = bytearray(hdr.titleID) counter[:8] = bytearray(hdr.titleID)
for i in range(4): for i in range(4):
counter[12 + i] = int8tobytes(x >> (3 - i) * 8 & 255) counter[12 + i:12 + i + 1] = int8tobytes(x >> (3 - i) * 8 & 255)
return bytes(counter) return bytes(counter)
@ -89,6 +87,11 @@ class NCCHReader:
with open(file, 'rb') as f: with open(file, 'rb') as f:
self.hdr = NCCHHdr(f.read(0x200)) self.hdr = NCCHHdr(f.read(0x200))
if self.hdr.format_ver == 0 or self.hdr.format_ver == 2:
media_unit = 0x200
elif self.hdr.format_ver == 1:
media_unit = 1
# Parse flags # Parse flags
self.keyX_2 = { 0x00: CTR.KeyX0x2C, self.keyX_2 = { 0x00: CTR.KeyX0x2C,
0x01: CTR.KeyX0x25, 0x01: CTR.KeyX0x25,
@ -368,6 +371,7 @@ class NCCHReader:
return ( return (
f'TitleID: {hex(readle(self.hdr.titleID))[2:].zfill(16)}\n' f'TitleID: {hex(readle(self.hdr.titleID))[2:].zfill(16)}\n'
f'Maker code: {self.hdr.maker_code.decode("ascii")}\n' f'Maker code: {self.hdr.maker_code.decode("ascii")}\n'
f'Format version: {self.hdr.format_ver}\n'
f'Product code: {self.hdr.product_code.decode("ascii")}\n' f'Product code: {self.hdr.product_code.decode("ascii")}\n'
f'Flags:\n' f'Flags:\n'
f' > Crypto method: {crypto}\n' f' > Crypto method: {crypto}\n'
@ -539,6 +543,11 @@ class NCCHBuilder:
f.seek(0x400) f.seek(0x400)
f.write(sig) f.write(sig)
if hdr.format_ver == 0 or hdr.format_ver == 2:
media_unit = 0x200
elif hdr.format_ver == 1:
media_unit = 1
curr = 0x200 curr = 0x200
files = {} files = {}
size_check = [] size_check = []