Update gcmtool.py

added debug logging
This commit is contained in:
tmpz23 2021-12-21 15:17:38 +01:00 committed by GitHub
parent 87a0c8c0df
commit 8a76b7c1a5
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
__version__ = "0.0.2"
__version__ = "0.0.3"
__author__ = "rigodron, algoflash, GGLinnk"
__license__ = "MIT"
__status__ = "developpement"
@ -15,7 +15,7 @@ FST_TYPE_DIR = 1
######################################################################
# Todo : add logging ; add extension check ; add --verbose ; add --disable-ignore
# Todo : add extension check ; add --disable-ignore
# -> test it on random iso and check that it's the same than dolphin extract
# -> test it !!!!
# Add FST rebuild ;
@ -52,22 +52,23 @@ class GCM:
iso_file.seek(0x2440)
apploaderimg_data = iso_file.read(apploader_size)
#print( 0x2440 + apploader_size )
#print("fst.bin :", int.from_bytes(bootbin_data[0x424:0x428],"big", signed=False), int.from_bytes(bootbin_data[0x428:0x42c],"big", signed=False))
iso_file.seek( int.from_bytes(bootbin_data[0x424:0x428],"big", signed=False) )
fstbin_data = iso_file.read( int.from_bytes(bootbin_data[0x428:0x42c],"big", signed=False) )
fstbin_offset = int.from_bytes(bootbin_data[0x424:0x428],"big", signed=False)
fstbin_len = int.from_bytes(bootbin_data[0x428:0x42c],"big", signed=False)
iso_file.seek( fstbin_offset )
fstbin_data = iso_file.read( fstbin_len )
dol_offset = int.from_bytes(bootbin_data[0x420:0x424],"big", signed=False)
iso_file.seek( dol_offset )
dol = Dol()
dolheader_data = iso_file.read(0x100)
bootdol_data = dolheader_data + iso_file.read( dol.getDolLen( dolheader_data ) - 0x100 )
#print("dol :", dol_offset, dol.getDolLen( dolheader_data ))
dol_len = dol.getDolLen( dolheader_data )
bootdol_data = dolheader_data + iso_file.read( dol_len - 0x100 )
if folder_path != Path("."):
base_path = folder_path
else:
base_path = Path(f"{bootbin_data[:4].decode('utf-8')}-{int.from_bytes(bootbin_data[6:7], 'little', signed=False):02}")
logging.info(f"unpacking {iso_path} in {base_path}")
sys_path = base_path / "sys"
sys_path.mkdir(parents=True, exist_ok=True)
@ -75,11 +76,16 @@ class GCM:
(sys_path / "bi2.bin" ).open("wb") as bi2bin_file, \
(sys_path / "fst.bin").open("wb") as fstbin_file, \
(sys_path / "apploader.img").open("wb") as apploaderimg_file,\
(sys_path / "boot.dol").open("wb") as bootdol_file :
(sys_path / "boot.dol").open("wb") as bootdol_file:
logging.debug(f"{iso_path}(0x0:0x440) -> {sys_path / 'boot.bin'}")
bootbin_file.write(bootbin_data)
logging.debug(f"{iso_path}(0x440:0x2440) -> {sys_path / 'bi2.bin'}")
bi2bin_file.write(bi2bin_data)
fstbin_file.write(fstbin_data)
logging.debug(f"{iso_path}(0x2440:0x{0x2440 + apploader_size:x} -> {sys_path / 'apploader.img'}")
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.mkdir(exist_ok=True)
@ -119,9 +125,8 @@ class GCM:
with (currentdir_path / name).open("wb") as new_file:
iso_file.seek(fileoffset)
new_file.write( iso_file.read(filesize) )
#print("file :", fileoffset, filesize)
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):
if iso_path == None:
iso_path = folder_path.parent / Path(folder_path.name).with_suffix(".iso")
@ -131,6 +136,11 @@ class GCM:
(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:0x440)")
logging.debug(f"{folder_path / 'sys' / 'bi2.bin'} -> {iso_path}(0x440:0x2440)")
logging.debug(f"{folder_path / 'sys' / 'apploader.img'} -> {iso_path}(0x2440:0x{0x2440 + (folder_path / 'sys' / 'apploader.img').stat().st_size:x}")
bootbin_data = bootbin_file.read()
iso_file.write( bootbin_data )
iso_file.write(bi2bin_file.read())
@ -140,11 +150,13 @@ class GCM:
fstbin_len = int.from_bytes(bootbin_data[0x428:0x42c],"big", signed=False)
if (folder_path / "sys" / "fst.bin").stat().st_size != fstbin_len:
raise Exception("Invalid fst.bin size in boot.bin offset 0x428:0x42c!")
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 )
fstbin_data = fstbin_file.read()
iso_file.write( fstbin_data )
dol_offset = int.from_bytes(bootbin_data[0x420:0x424],"big", signed=False)
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.write( bootdol_file.read() )
@ -179,9 +191,9 @@ class GCM:
else:
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)
#print(fileoffset, filesize)
with (currentdir_path / name).open("rb") as new_file:
logging.debug(f"{currentdir_path / name} -> {iso_path}(0x{fileoffset:x}:0x{fileoffset + filesize:x})")
iso_file.seek(fileoffset)
iso_file.write( new_file.read(filesize) )
@ -190,6 +202,7 @@ def get_argparser():
import argparse
parser = argparse.ArgumentParser(description='ISO/GCM packer & unpacker - [GameCube] v' + __version__)
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
parser.add_argument('-v', '--verbose', action='store_true', help='verbose mode')
parser.add_argument('input_path', metavar='INPUT', help='')
parser.add_argument('output_path', metavar='OUTPUT', help='', nargs='?', default="")
@ -200,17 +213,21 @@ def get_argparser():
if __name__ == '__main__':
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
args = get_argparser().parse_args()
p_input = Path(args.input_path)
p_output = Path(args.output_path)
gcm = GCM()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
if args.pack:
logging.info("### Pack")
if(p_output == Path(".")):
p_output = Path(p_input.with_suffix(".iso"))
logging.info(f"packing folder {p_input} in {p_output}")
gcm.pack( p_input, p_output )
elif args.unpack:
logging.info("### Unpack")