Update gcmtool.py

This commit is contained in:
tmpz23 2022-01-16 18:00:02 +01:00 committed by GitHub
parent 1c9a484bb4
commit 012fbb5195
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,7 @@ from pathlib import Path
import logging import logging
__version__ = "0.0.8" __version__ = "0.0.9"
__author__ = "rigodron, algoflash, GGLinnk" __author__ = "rigodron, algoflash, GGLinnk"
__license__ = "MIT" __license__ = "MIT"
__status__ = "developpement" __status__ = "developpement"
@ -242,25 +242,21 @@ class Gcm:
else: else:
base_path = Path(f"{bootbin.game_code()}-{bootbin.disc_number():02}") base_path = Path(f"{bootbin.game_code()}-{bootbin.disc_number():02}")
logging.info(f"unpacking {iso_path} in {base_path}") logging.info(f"unpacking \"{iso_path}\" in \"{base_path}\"")
sys_path = base_path / "sys" sys_path = base_path / "sys"
sys_path.mkdir(parents=True, exist_ok=True) sys_path.mkdir(parents=True, exist_ok=True)
with (sys_path / "boot.bin").open("wb") as bootbin_file, \ logging.debug(f"{iso_path}(0x0:0x{BootBin.LEN:x}) -> {sys_path / 'boot.bin'}")
(sys_path / "bi2.bin" ).open("wb") as bi2bin_file, \ (sys_path / "boot.bin").write_bytes(bootbin.data())
(sys_path / "fst.bin").open("wb") as fstbin_file, \ logging.debug(f"{iso_path}(0x440:0x{Gcm.APPLOADER_OFFSET:x}) -> {sys_path / 'bi2.bin'}")
(sys_path / "apploader.img").open("wb") as apploaderimg_file,\ (sys_path / "bi2.bin" ).write_bytes(bi2bin_data)
(sys_path / "boot.dol").open("wb") as bootdol_file: logging.debug(f"{iso_path}(0x{Gcm.APPLOADER_OFFSET:x}:0x{Gcm.APPLOADER_OFFSET + apploader_size:x} -> {sys_path / 'apploader.img'}")
logging.debug(f"{iso_path}(0x0:0x{BootBin.LEN:x}) -> {sys_path / 'boot.bin'}") (sys_path / "apploader.img").write_bytes(apploaderimg_data)
bootbin_file.write(bootbin.data()) logging.debug(f"{iso_path}(0x{fstbin_offset:x}:0x{fstbin_offset + fstbin_len:x}) -> {sys_path / 'fst.bin'}")
logging.debug(f"{iso_path}(0x440:0x{Gcm.APPLOADER_OFFSET:x}) -> {sys_path / 'bi2.bin'}") (sys_path / "fst.bin").write_bytes(fstbin_data)
bi2bin_file.write(bi2bin_data) logging.debug(f"{iso_path}(0x{dol_offset:x}:0x{dol_offset + dol_len:x}) -> {sys_path / 'boot.dol'}")
logging.debug(f"{iso_path}(0x{Gcm.APPLOADER_OFFSET:x}:0x{Gcm.APPLOADER_OFFSET + apploader_size:x} -> {sys_path / 'apploader.img'}") (sys_path / "boot.dol").write_bytes(bootdol_data)
apploaderimg_file.write(apploaderimg_data)
logging.debug(f"{iso_path}(0x{fstbin_offset:x}:0x{fstbin_offset + fstbin_len:x}) -> {sys_path / 'fst.bin'}")
fstbin_file.write(fstbin_data)
logging.debug(f"{iso_path}(0x{dol_offset:x}:0x{dol_offset + dol_len:x}) -> {sys_path / 'boot.dol'}")
bootdol_file.write(bootdol_data)
root_path = base_path / "root" root_path = base_path / "root"
root_path.mkdir(exist_ok=True) root_path.mkdir(exist_ok=True)
@ -296,43 +292,36 @@ class Gcm:
fileoffset = int.from_bytes(fstbin_data[i+4:i+8], "big", signed=False) fileoffset = int.from_bytes(fstbin_data[i+4:i+8], "big", signed=False)
filesize = int.from_bytes(fstbin_data[i+8:i+12], "big", signed=False) filesize = int.from_bytes(fstbin_data[i+8:i+12], "big", signed=False)
with (currentdir_path / name).open("wb") as new_file: iso_file.seek(fileoffset)
iso_file.seek(fileoffset) (currentdir_path / name).write_bytes( iso_file.read(filesize) )
new_file.write( iso_file.read(filesize) )
logging.debug(f"{iso_path}(0x{fileoffset:x}:0x{fileoffset + filesize:x}) -> {currentdir_path / name}") logging.debug(f"{iso_path}(0x{fileoffset:x}:0x{fileoffset + filesize:x}) -> {currentdir_path / name}")
def pack(self, folder_path:Path, iso_path:Path = None): def pack(self, folder_path:Path, iso_path:Path = None):
if iso_path == None: if iso_path == None:
iso_path = folder_path.parent / Path(folder_path.name).with_suffix(".iso") iso_path = folder_path.parent / Path(folder_path.name).with_suffix(".iso")
with iso_path.open("wb") as iso_file, \ with iso_path.open("wb") as iso_file:
(folder_path / "sys" / "boot.bin").open("rb") as bootbin_file, \
(folder_path / "sys" / "bi2.bin" ).open("rb") as bi2bin_file, \
(folder_path / "sys" / "fst.bin").open("rb") as fstbin_file, \
(folder_path / "sys" / "apploader.img").open("rb") as apploaderimg_file,\
(folder_path / "sys" / "boot.dol").open("rb") as bootdol_file :
logging.debug(f"{folder_path / 'sys' / 'boot.bin'} -> {iso_path}(0x0:0x{BootBin.LEN:x})") logging.debug(f"{folder_path / 'sys' / 'boot.bin'} -> {iso_path}(0x0:0x{BootBin.LEN:x})")
logging.debug(f"{folder_path / 'sys' / 'bi2.bin'} -> {iso_path}(0x{BootBin.LEN:x}:0x{Gcm.APPLOADER_OFFSET:x})") logging.debug(f"{folder_path / 'sys' / 'bi2.bin'} -> {iso_path}(0x{BootBin.LEN:x}:0x{Gcm.APPLOADER_OFFSET:x})")
logging.debug(f"{folder_path / 'sys' / 'apploader.img'} -> {iso_path}(0x{Gcm.APPLOADER_OFFSET:x}:0x{Gcm.APPLOADER_OFFSET + (folder_path / 'sys' / 'apploader.img').stat().st_size:x}") logging.debug(f"{folder_path / 'sys' / 'apploader.img'} -> {iso_path}(0x{Gcm.APPLOADER_OFFSET:x}:0x{Gcm.APPLOADER_OFFSET + (folder_path / 'sys' / 'apploader.img').stat().st_size:x}")
bootbin = BootBin(bootbin_file.read()) bootbin = BootBin((folder_path / "sys" / "boot.bin").read_bytes())
iso_file.write(bootbin.data()) iso_file.write(bootbin.data())
iso_file.write(bi2bin_file.read()) iso_file.write((folder_path / "sys" / "bi2.bin").read_bytes())
iso_file.write(apploaderimg_file.read()) iso_file.write((folder_path / "sys" / "apploader.img").read_bytes())
fstbin_offset = bootbin.fstbin_offset() fstbin_offset = bootbin.fstbin_offset()
fstbin_len = bootbin.fstbin_len() fstbin_len = bootbin.fstbin_len()
if (folder_path / "sys" / "fst.bin").stat().st_size != fstbin_len: if (folder_path / "sys" / "fst.bin").stat().st_size != fstbin_len:
raise Exception("Invalid fst.bin size in boot.bin offset 0x{BootBin.FSTLEN_OFFSET:x}:0x{BootBin.FSTLEN_OFFSET+4:x}!") raise Exception(f"Invalid fst.bin size in boot.bin offset 0x{BootBin.FSTLEN_OFFSET:x}:0x{BootBin.FSTLEN_OFFSET+4:x}!")
logging.debug(f"{folder_path / 'sys' / 'fst.bin'} -> {iso_path}(0x{fstbin_offset:x}:0x{fstbin_offset + fstbin_len:x})") logging.debug(f"{folder_path / 'sys' / 'fst.bin'} -> {iso_path}(0x{fstbin_offset:x}:0x{fstbin_offset + fstbin_len:x})")
iso_file.seek( fstbin_offset ) iso_file.seek( fstbin_offset )
fstbin_data = fstbin_file.read() fstbin_data = (folder_path / "sys" / "fst.bin").read_bytes()
iso_file.write( fstbin_data ) iso_file.write( fstbin_data )
dol_offset = bootbin.dol_offset() dol_offset = bootbin.dol_offset()
logging.debug(f"{folder_path / 'sys' / 'boot.dol'} -> {iso_path}(0x{dol_offset:x}:0x{dol_offset + (folder_path / 'sys' / 'boot.dol').stat().st_size:x})") logging.debug(f"{folder_path / 'sys' / 'boot.dol'} -> {iso_path}(0x{dol_offset:x}:0x{dol_offset + (folder_path / 'sys' / 'boot.dol').stat().st_size:x})")
iso_file.seek( dol_offset ) iso_file.seek( dol_offset )
iso_file.write( bootdol_file.read() ) iso_file.write( (folder_path / "sys" / "boot.dol").read_bytes() )
# Now parse fst.bin for writing files in the iso # Now parse fst.bin for writing files in the iso
dir_id_path = {0: folder_path / "root"} dir_id_path = {0: folder_path / "root"}
@ -363,49 +352,47 @@ class Gcm:
dir_id_path[id] = currentdir_path dir_id_path[id] = currentdir_path
currentdir_path.mkdir(exist_ok=True) currentdir_path.mkdir(exist_ok=True)
else: else:
fileoffset = int.from_bytes(fstbin_data[i+4:i+8], "big", signed=False) file_offset = int.from_bytes(fstbin_data[i+4:i+8], "big", signed=False)
filesize = int.from_bytes(fstbin_data[i+8:i+12], "big", signed=False) file_len = int.from_bytes(fstbin_data[i+8:i+12], "big", signed=False)
with (currentdir_path / name).open("rb") as new_file: if (currentdir_path / name).stat().st_size != file_len:
if (currentdir_path / name).stat().st_size != filesize: raise Exception(f"Invalid file size : {currentdir_path / name} - use --rebuild-fst before packing files in the iso.")
raise Exception(f"Invalid file size : {currentdir_path / name} - use --rebuild-fst before packing files in the iso.") logging.debug(f"{currentdir_path / name} -> {iso_path}(0x{file_offset:x}:0x{file_offset + file_len:x})")
logging.debug(f"{currentdir_path / name} -> {iso_path}(0x{fileoffset:x}:0x{fileoffset + filesize:x})") iso_file.seek(file_offset)
iso_file.seek(fileoffset) iso_file.write( (currentdir_path / name).read_bytes() )
iso_file.write( new_file.read() )
def rebuild_fst(self, folder_path:Path, align:int): def rebuild_fst(self, folder_path:Path, align:int):
root_path = folder_path / "root" root_path = folder_path / "root"
sys_path = folder_path / "sys" sys_path = folder_path / "sys"
with (sys_path / "boot.bin").open("rb+") as bootbin_file:
dol_offset = align_offset(Gcm.APPLOADER_OFFSET + (sys_path / "apploader.img").stat().st_size, align)
logging.info(f"Patching sys/boot.bin offset 0x{BootBin.DOLOFFSET_OFFSET:x} with new dol offset (0x{dol_offset:x})")
bootbin = BootBin(bootbin_file.read())
bootbin.set_dol_offset(dol_offset)
fst_offset = align_offset(dol_offset + (sys_path / "boot.dol").stat().st_size, align)
logging.info(f"Patching sys/boot.bin offset 0x{BootBin.FSTOFFSET_OFFSET:x} with new fst offset (0x{fst_offset:x})")
bootbin.set_fst_offset(fst_offset)
fst_tree = FstTree(root_path, fst_offset, align=align)
# Sorting paths approach original fst sort but specials chars are after and not before chars dol_offset = align_offset(Gcm.APPLOADER_OFFSET + (sys_path / "apploader.img").stat().st_size, align)
path_list = sorted([path for path in root_path.glob('**/*')], key=lambda s:Path(str(s).upper())) logging.info(f"Patching sys/boot.bin offset 0x{BootBin.DOLOFFSET_OFFSET:x} with new dol offset (0x{dol_offset:x})")
for path in path_list: bootbin = BootBin((sys_path / "boot.bin").read_bytes())
fst_tree.add_node_by_path(path) bootbin.set_dol_offset(dol_offset)
logging.debug(fst_tree)
fst_offset = align_offset(dol_offset + (sys_path / "boot.dol").stat().st_size, align)
logging.info(f"Patching sys/boot.bin offset 0x{BootBin.FSTOFFSET_OFFSET:x} with new fst offset (0x{fst_offset:x})")
bootbin.set_fst_offset(fst_offset)
fst_tree = FstTree(root_path, fst_offset, align=align)
fst_path = sys_path / "fst.bin" # Sorting paths approach original fst sort, but in original fst specials chars are after and not before chars
with fst_path.open("wb") as fstbin_file: path_list = sorted([path for path in root_path.glob('**/*')], key=lambda s:Path(str(s).upper()))
logging.info("Writing fst in sys/fst.bin") for path in path_list:
fstbin_file.write( fst_tree.get_fst() ) fst_tree.add_node_by_path(path)
logging.debug(fst_tree)
fst_size = fst_path.stat().st_size fst_path = sys_path / "fst.bin"
logging.info(f"Patching sys/boot.bin offset 0x{BootBin.FSTLEN_OFFSET:x} with new fst size (0x{fst_size:x})")
bootbin.set_fst_len(fst_size)
logging.info(f"Patching sys/boot.bin offset 0x{BootBin.MAXFSTLEN_OFFSET:x} with new max fst size (0x{fst_size:x})")
bootbin.set_max_fst_len(fst_size)
bootbin_file.seek(0) logging.info("Writing fst in sys/fst.bin")
bootbin_file.write(bootbin.data()) fst_path.write_bytes( fst_tree.get_fst() )
fst_size = fst_path.stat().st_size
logging.info(f"Patching sys/boot.bin offset 0x{BootBin.FSTLEN_OFFSET:x} with new fst size (0x{fst_size:x})")
bootbin.set_fst_len(fst_size)
logging.info(f"Patching sys/boot.bin offset 0x{BootBin.MAXFSTLEN_OFFSET:x} with new max fst size (0x{fst_size:x})")
bootbin.set_max_fst_len(fst_size)
(sys_path / "boot.bin").write_bytes(bootbin.data())
def get_argparser(): def get_argparser():
@ -439,7 +426,7 @@ if __name__ == '__main__':
logging.info("### Pack in new GCM iso") logging.info("### Pack in new GCM iso")
if(p_output == Path(".")): if(p_output == Path(".")):
p_output = Path(p_input.with_suffix(".iso")) p_output = Path(p_input.with_suffix(".iso"))
logging.info(f"packing folder {p_input} in {p_output}") logging.info(f"packing folder \"{p_input}\" in \"{p_output}\"")
gcm.pack( p_input, p_output ) gcm.pack( p_input, p_output )
elif args.unpack: elif args.unpack:
logging.info("### Unpack GCM iso in new folder") logging.info("### Unpack GCM iso in new folder")