2021-12-18 11:58:21 +01:00
#!/usr/bin/env python3
from pathlib import Path
2021-12-18 12:17:59 +01:00
import logging
2021-12-18 11:58:21 +01:00
2022-01-18 19:47:47 +01:00
__version__ = " 0.0.10 "
2021-12-18 11:58:21 +01:00
__author__ = " rigodron, algoflash, GGLinnk "
__license__ = " MIT "
__status__ = " developpement "
2022-01-13 17:30:45 +01:00
def align_offset ( offset : int , align : int ) :
2022-01-11 14:19:31 +01:00
if offset % align != 0 :
offset + = align - ( offset % align )
2022-01-13 17:30:45 +01:00
return offset
2022-01-11 14:19:31 +01:00
2022-01-14 15:37:41 +01:00
class Fst :
TYPE_FILE = 0
TYPE_DIR = 1
2022-01-09 18:56:03 +01:00
class Node :
__id = None
__name = None
2022-01-14 15:37:41 +01:00
__name_offset = None
def __init__ ( self , name : str ) :
2022-01-09 18:56:03 +01:00
self . __name = name
def id ( self ) : return self . __id
def name ( self ) : return self . __name
2022-01-14 15:37:41 +01:00
def name_offset ( self ) : return self . __name_offset
def set_id ( self , id : int ) : self . __id = id
def set_name_offset ( self , name_offset : int ) : self . __name_offset = name_offset
2022-01-09 18:56:03 +01:00
class File ( Node ) :
2022-01-14 15:37:41 +01:00
__type = Fst . TYPE_FILE
2022-01-09 18:56:03 +01:00
__size = None
__offset = None
def __init__ ( self , name : str , size : int ) :
2022-01-14 15:37:41 +01:00
super ( ) . __init__ ( name )
2022-01-09 18:56:03 +01:00
self . __size = size
def __str__ ( self ) :
2022-01-14 15:37:41 +01:00
return f " { self . id ( ) } ; { self . name ( ) } ; { self . size ( ) } ; { self . offset ( ) } ; { self . name_offset ( ) } "
def type ( self ) : return self . __type
2022-01-09 18:56:03 +01:00
def size ( self ) : return self . __size
def offset ( self ) : return self . __offset
def set_offset ( self , offset : int ) : self . __offset = offset
def format ( self ) :
2022-01-14 15:37:41 +01:00
return self . type ( ) . to_bytes ( 1 , " big " ) + self . name_offset ( ) . to_bytes ( 3 , " big " ) + self . offset ( ) . to_bytes ( 4 , " big " ) + self . size ( ) . to_bytes ( 4 , " big " )
2022-01-09 18:56:03 +01:00
class Folder ( Node ) :
2022-01-14 15:37:41 +01:00
__type = Fst . TYPE_DIR
2022-01-09 18:56:03 +01:00
__parent = None
__next_dir = None
__childs = None
def __init__ ( self , name : str , parent : Node ) :
2022-01-14 15:37:41 +01:00
super ( ) . __init__ ( name )
2022-01-09 18:56:03 +01:00
self . __parent = parent
self . __childs = [ ]
def __str__ ( self ) :
2022-01-14 15:37:41 +01:00
return f " { self . id ( ) } ; { self . name ( ) } ; { self . next_dir ( ) } ; { self . name_offset ( ) } "
def type ( self ) : return self . __type
2022-01-09 18:56:03 +01:00
def parent ( self ) : return self . __parent
def next_dir ( self ) : return self . __next_dir
def childs ( self ) : return self . __childs
def set_next_dir ( self , next_dir ) : self . __next_dir = next_dir
# Search child by name an return existing if found or new if not existing
def add_child ( self , node : Node ) :
for child in self . __childs :
if node . name ( ) == child . name ( ) :
return child
self . __childs . append ( node )
return node
def format ( self ) :
2022-01-14 15:37:41 +01:00
return self . type ( ) . to_bytes ( 1 , " big " ) + self . name_offset ( ) . to_bytes ( 3 , " big " ) + self . parent ( ) . id ( ) . to_bytes ( 4 , " big " ) + self . next_dir ( ) . to_bytes ( 4 , " big " )
2022-01-09 18:56:03 +01:00
2022-01-14 15:37:41 +01:00
class FstTree ( Fst ) :
2022-01-09 18:56:03 +01:00
__root_path_length = None
__root_node = None
2022-01-14 15:37:41 +01:00
__current_id = 0
2022-01-09 18:56:03 +01:00
__current_file_offset = None
__align = None
__fst_block = None
__name_block = None
__nameblock_length = None # Used to find min file_offset when fst is at the end of the iso beginning
2022-01-10 22:17:20 +01:00
def __init__ ( self , root_path : Path , fst_offset : int , align : int = 4 ) :
2022-01-09 18:56:03 +01:00
self . __root_path_length = len ( root_path . parts )
self . __root_node = Folder ( root_path . name , None )
self . __align = align
self . __name_block = b " "
self . __fst_block = b " "
self . __nameblock_length = 0
2022-01-10 22:17:20 +01:00
self . __current_file_offset = fst_offset
2022-01-09 18:56:03 +01:00
def __str__ ( self ) :
return self . __to_str ( self . __root_node )
def __to_str ( self , node : Node , depth = 0 ) :
result = ( depth * " " ) + str ( node ) + " \n "
2022-01-14 15:37:41 +01:00
if node . type ( ) == FstTree . TYPE_DIR :
2022-01-09 18:56:03 +01:00
for child in node . childs ( ) :
result + = self . __to_str ( child , depth + 1 )
return result
2022-01-10 22:17:20 +01:00
# Needed to know where we can begin to write files
2022-01-09 18:56:03 +01:00
def __get_fst_length ( self ) :
self . __generate_nameblock_length ( )
2022-01-13 17:30:45 +01:00
return align_offset ( self . __count_childs ( self . __root_node ) * 12 + 12 + self . __nameblock_length , self . __align )
2022-01-09 18:56:03 +01:00
def __generate_nameblock_length ( self , node : Node = None ) :
if node == None :
node = self . __root_node
else :
self . __nameblock_length + = len ( node . name ( ) ) + 1
2022-01-14 15:37:41 +01:00
if node . type ( ) == FstTree . TYPE_DIR :
2022-01-09 18:56:03 +01:00
for child in node . childs ( ) :
self . __generate_nameblock_length ( child )
2022-01-10 22:17:20 +01:00
def __prepare ( self , node : Node = None ) :
2022-01-14 15:37:41 +01:00
name_offset = 0
2022-01-09 18:56:03 +01:00
if node == None :
node = self . __root_node
else :
2022-01-14 15:37:41 +01:00
name_offset = len ( self . __name_block )
2022-01-09 18:56:03 +01:00
self . __name_block + = node . name ( ) . encode ( " utf-8 " ) + b " \x00 "
2022-01-14 15:37:41 +01:00
node . set_name_offset ( name_offset )
node . set_id ( self . __current_id )
self . __current_id + = 1
2022-01-09 18:56:03 +01:00
2022-01-14 15:37:41 +01:00
if node . type ( ) == FstTree . TYPE_DIR :
node . set_next_dir ( self . __current_id + self . __count_childs ( node ) )
2022-01-09 18:56:03 +01:00
if node == self . __root_node :
self . __fst_block = b " \x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 " + node . next_dir ( ) . to_bytes ( 4 , " big " )
else :
self . __fst_block + = node . format ( )
for child in node . childs ( ) :
2022-01-10 22:17:20 +01:00
self . __prepare ( child )
2022-01-09 18:56:03 +01:00
else :
node . set_offset ( self . __current_file_offset )
self . __fst_block + = node . format ( )
2022-01-13 17:30:45 +01:00
self . __current_file_offset = align_offset ( self . __current_file_offset + node . size ( ) , self . __align )
2022-01-10 22:17:20 +01:00
def __count_childs ( self , node : Folder ) :
2022-01-09 18:56:03 +01:00
count = 0
for child in node . childs ( ) :
2022-01-14 15:37:41 +01:00
if child . type ( ) == FstTree . TYPE_DIR :
2022-01-10 22:17:20 +01:00
count + = self . __count_childs ( child )
2022-01-09 18:56:03 +01:00
return count + len ( node . childs ( ) )
2022-01-14 15:37:41 +01:00
def add_node_by_path ( self , node_path : Path ) :
parent = self . __root_node
node = None
for i in range ( self . __root_path_length , len ( node_path . parts ) - 1 ) :
node = Folder ( node_path . parts [ i ] , parent )
parent = parent . add_child ( node )
if node_path . is_file ( ) :
node = File ( node_path . name , node_path . stat ( ) . st_size )
else :
node = Folder ( node_path . name , parent )
parent . add_child ( node )
def get_fst ( self ) :
self . __current_file_offset + = self . __get_fst_length ( )
self . __prepare ( )
return self . __fst_block + self . __name_block
class BootBin :
LEN = 0x440
DOLOFFSET_OFFSET = 0x420
FSTOFFSET_OFFSET = 0x424
FSTLEN_OFFSET = 0x428
MAXFSTLEN_OFFSET = 0x42c
__data = None
def __init__ ( self , data : bytes ) :
self . __data = bytearray ( data )
def data ( self ) : return self . __data
def dvd_magic ( self ) :
return self . __data [ 0x1c : 0x20 ]
def fstbin_offset ( self ) :
return int . from_bytes ( self . __data [ BootBin . FSTOFFSET_OFFSET : BootBin . FSTOFFSET_OFFSET + 4 ] , " big " , signed = False )
def fstbin_len ( self ) :
return int . from_bytes ( self . __data [ BootBin . FSTLEN_OFFSET : BootBin . FSTLEN_OFFSET + 4 ] , " big " , signed = False )
def dol_offset ( self ) :
return int . from_bytes ( self . __data [ BootBin . DOLOFFSET_OFFSET : BootBin . DOLOFFSET_OFFSET + 4 ] , " big " , signed = False )
def game_code ( self ) :
return self . __data [ : 4 ] . decode ( ' utf-8 ' )
def disc_number ( self ) :
return int . from_bytes ( self . __data [ 6 : 7 ] , ' big ' , signed = False )
def set_dol_offset ( self , offset : int ) :
self . __data [ BootBin . DOLOFFSET_OFFSET : BootBin . DOLOFFSET_OFFSET + 4 ] = offset . to_bytes ( 4 , " big " )
def set_fst_offset ( self , offset : int ) :
self . __data [ BootBin . FSTOFFSET_OFFSET : BootBin . FSTOFFSET_OFFSET + 4 ] = offset . to_bytes ( 4 , " big " )
def set_fst_len ( self , size : int ) :
self . __data [ BootBin . FSTLEN_OFFSET : BootBin . FSTLEN_OFFSET + 4 ] = size . to_bytes ( 4 , " big " )
def set_max_fst_len ( self , size : int ) :
self . __data [ BootBin . MAXFSTLEN_OFFSET : BootBin . MAXFSTLEN_OFFSET + 4 ] = size . to_bytes ( 4 , " big " )
2022-01-09 18:56:03 +01:00
2021-12-18 11:58:21 +01:00
class Dol :
2022-01-14 15:37:41 +01:00
HEADER_LEN = 0x100
HEADER_SECTIONLENTABLE_OFFSET = 0x90
2022-01-10 22:17:20 +01:00
# Get total length using the sum of the 18 sections length and dol header length
def get_dol_len ( self , dolheader_data : bytes ) :
2022-01-14 15:37:41 +01:00
dol_len = Dol . HEADER_LEN
2021-12-18 11:58:21 +01:00
for i in range ( 18 ) :
2022-01-14 15:37:41 +01:00
dol_len + = int . from_bytes ( dolheader_data [ Dol . HEADER_SECTIONLENTABLE_OFFSET + i * 4 : Dol . HEADER_SECTIONLENTABLE_OFFSET + ( i + 1 ) * 4 ] , " big " , signed = False )
2021-12-18 11:58:21 +01:00
return dol_len
# https://sudonull.com/post/68549-Gamecube-file-system-device
2022-01-14 15:37:41 +01:00
class Gcm :
BI2BIN_LEN = 0x2000
APPLOADER_HEADER_LEN = 0x20
APPLOADER_OFFSET = 0x2440
APPLOADERSIZE_OFFSET = 0x2454
DVD_MAGIC = b " \xC2 \x33 \x9F \x3D "
2021-12-19 17:22:28 +01:00
def unpack ( self , iso_path : Path , folder_path : Path ) :
2021-12-18 11:58:21 +01:00
with iso_path . open ( " rb " ) as iso_file :
2022-01-14 15:37:41 +01:00
bootbin = BootBin ( iso_file . read ( BootBin . LEN ) )
if bootbin . dvd_magic ( ) != Gcm . DVD_MAGIC :
2021-12-18 11:58:21 +01:00
raise Exception ( " Invalid DVD format - this tool is for ISO/GCM files " )
2022-01-18 19:47:47 +01:00
2022-01-14 15:37:41 +01:00
bi2bin_data = iso_file . read ( Gcm . BI2BIN_LEN )
2021-12-18 11:58:21 +01:00
2022-01-14 15:37:41 +01:00
iso_file . seek ( Gcm . APPLOADERSIZE_OFFSET )
2021-12-18 11:58:21 +01:00
size = int . from_bytes ( iso_file . read ( 4 ) , " big " , signed = False )
trailerSize = int . from_bytes ( iso_file . read ( 4 ) , " big " , signed = False )
2022-01-14 15:37:41 +01:00
apploader_size = Gcm . APPLOADER_HEADER_LEN + size + trailerSize
2021-12-18 11:58:21 +01:00
2022-01-14 15:37:41 +01:00
iso_file . seek ( Gcm . APPLOADER_OFFSET )
2021-12-18 11:58:21 +01:00
apploaderimg_data = iso_file . read ( apploader_size )
2022-01-14 15:37:41 +01:00
fstbin_offset = bootbin . fstbin_offset ( )
fstbin_len = bootbin . fstbin_len ( )
2021-12-21 15:17:38 +01:00
iso_file . seek ( fstbin_offset )
fstbin_data = iso_file . read ( fstbin_len )
2021-12-18 11:58:21 +01:00
2022-01-14 15:37:41 +01:00
dol_offset = bootbin . dol_offset ( )
2021-12-18 11:58:21 +01:00
iso_file . seek ( dol_offset )
dol = Dol ( )
2022-01-14 15:37:41 +01:00
dolheader_data = iso_file . read ( Dol . HEADER_LEN )
2022-01-10 22:17:20 +01:00
dol_len = dol . get_dol_len ( dolheader_data )
2022-01-14 15:37:41 +01:00
bootdol_data = dolheader_data + iso_file . read ( dol_len - Dol . HEADER_LEN )
2022-01-18 19:47:47 +01:00
if folder_path == Path ( " . " ) :
folder_path = Path ( f " { bootbin . game_code ( ) } - { bootbin . disc_number ( ) : 02 } " )
if folder_path . is_dir ( ) :
raise Exception ( f " Error - \" { folder_path } \" already exist. Remove this folder or use another name for the unpack folder. " )
2021-12-21 15:17:38 +01:00
2022-01-18 19:47:47 +01:00
logging . info ( f " unpacking \" { iso_path } \" in \" { folder_path } \" " )
sys_path = folder_path / " sys "
sys_path . mkdir ( parents = True )
2021-12-18 11:58:21 +01:00
2022-01-16 18:00:02 +01:00
logging . debug ( f " { iso_path } (0x0:0x { BootBin . LEN : x } ) -> { sys_path / ' boot.bin ' } " )
( sys_path / " boot.bin " ) . write_bytes ( bootbin . data ( ) )
logging . debug ( f " { iso_path } (0x440:0x { Gcm . APPLOADER_OFFSET : x } ) -> { sys_path / ' bi2.bin ' } " )
( sys_path / " bi2.bin " ) . write_bytes ( bi2bin_data )
logging . debug ( f " { iso_path } (0x { Gcm . APPLOADER_OFFSET : x } :0x { Gcm . APPLOADER_OFFSET + apploader_size : x } -> { sys_path / ' apploader.img ' } " )
( sys_path / " apploader.img " ) . write_bytes ( apploaderimg_data )
logging . debug ( f " { iso_path } (0x { fstbin_offset : x } :0x { fstbin_offset + fstbin_len : x } ) -> { sys_path / ' fst.bin ' } " )
( sys_path / " fst.bin " ) . write_bytes ( fstbin_data )
logging . debug ( f " { iso_path } (0x { dol_offset : x } :0x { dol_offset + dol_len : x } ) -> { sys_path / ' boot.dol ' } " )
( sys_path / " boot.dol " ) . write_bytes ( bootdol_data )
2022-01-18 19:47:47 +01:00
root_path = folder_path / " root "
root_path . mkdir ( )
2021-12-18 11:58:21 +01:00
# And now we parse FST data to unpack all files in the GCM iso file
2022-01-14 15:37:41 +01:00
dir_id_path = { 0 : root_path }
2021-12-18 11:58:21 +01:00
currentdir_path = root_path
2022-01-14 15:37:41 +01:00
# root: id=0 so nextdir is the end
2021-12-18 11:58:21 +01:00
nextdir = int . from_bytes ( fstbin_data [ 8 : 12 ] , " big " , signed = False )
# offset of filenames block
base_names = nextdir * 12
2022-01-14 15:37:41 +01:00
# go to parent when id reach next dir
2021-12-18 11:58:21 +01:00
nextdir_arr = [ nextdir ]
2022-01-14 15:37:41 +01:00
for id in range ( 1 , base_names / / 12 ) :
i = id * 12
2021-12-18 11:58:21 +01:00
file_type = int . from_bytes ( fstbin_data [ i : i + 1 ] , " big " , signed = False )
name = fstbin_data [ base_names + int . from_bytes ( fstbin_data [ i + 1 : i + 4 ] , " big " , signed = False ) : ] . split ( b " \x00 " ) [ 0 ] . decode ( " utf-8 " )
2022-01-14 15:37:41 +01:00
while id == nextdir_arr [ - 1 ] :
2021-12-18 11:58:21 +01:00
currentdir_path = currentdir_path . parent
nextdir_arr . pop ( )
2022-01-14 15:37:41 +01:00
if file_type == FstTree . TYPE_DIR :
2021-12-18 11:58:21 +01:00
nextdir = int . from_bytes ( fstbin_data [ i + 8 : i + 12 ] , " big " , signed = False )
parentdir = int . from_bytes ( fstbin_data [ i + 4 : i + 8 ] , " big " , signed = False )
nextdir_arr . append ( nextdir )
2022-01-14 15:37:41 +01:00
currentdir_path = dir_id_path [ parentdir ] / name
dir_id_path [ id ] = currentdir_path
2021-12-18 11:58:21 +01:00
currentdir_path . mkdir ( exist_ok = True )
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 )
2022-01-16 18:00:02 +01:00
iso_file . seek ( fileoffset )
( currentdir_path / name ) . write_bytes ( iso_file . read ( filesize ) )
2021-12-19 17:22:28 +01:00
2022-01-16 18:00:02 +01:00
logging . debug ( f " { iso_path } (0x { fileoffset : x } :0x { fileoffset + filesize : x } ) -> { currentdir_path / name } " )
2021-12-18 11:58:21 +01:00
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 " )
2022-01-18 19:47:47 +01:00
if iso_path . is_file ( ) :
raise Exception ( f " Error - { iso_path } already exist. Remove this file or use another GCM file name. " )
2022-01-16 18:00:02 +01:00
with iso_path . open ( " wb " ) as iso_file :
2022-01-18 19:47:47 +01:00
sys_path = folder_path / " sys "
logging . debug ( f " { sys_path / ' boot.bin ' } -> { iso_path } (0x0:0x { BootBin . LEN : x } ) " )
logging . debug ( f " { sys_path / ' bi2.bin ' } -> { iso_path } (0x { BootBin . LEN : x } :0x { Gcm . APPLOADER_OFFSET : x } ) " )
logging . debug ( f " { sys_path / ' apploader.img ' } -> { iso_path } (0x { Gcm . APPLOADER_OFFSET : x } :0x { Gcm . APPLOADER_OFFSET + ( sys_path / ' apploader.img ' ) . stat ( ) . st_size : x } " )
2021-12-21 15:17:38 +01:00
2022-01-18 19:47:47 +01:00
bootbin = BootBin ( ( sys_path / " boot.bin " ) . read_bytes ( ) )
2022-01-14 15:37:41 +01:00
iso_file . write ( bootbin . data ( ) )
2022-01-18 19:47:47 +01:00
iso_file . write ( ( sys_path / " bi2.bin " ) . read_bytes ( ) )
iso_file . write ( ( sys_path / " apploader.img " ) . read_bytes ( ) )
2021-12-18 11:58:21 +01:00
2022-01-14 15:37:41 +01:00
fstbin_offset = bootbin . fstbin_offset ( )
fstbin_len = bootbin . fstbin_len ( )
2022-01-18 19:47:47 +01:00
if ( sys_path / " fst.bin " ) . stat ( ) . st_size != fstbin_len :
2022-01-16 18:00:02 +01:00
raise Exception ( f " Invalid fst.bin size in boot.bin offset 0x { BootBin . FSTLEN_OFFSET : x } :0x { BootBin . FSTLEN_OFFSET + 4 : x } ! " )
2022-01-18 19:47:47 +01:00
logging . debug ( f " { sys_path / ' fst.bin ' } -> { iso_path } (0x { fstbin_offset : x } :0x { fstbin_offset + fstbin_len : x } ) " )
2021-12-18 11:58:21 +01:00
iso_file . seek ( fstbin_offset )
2022-01-18 19:47:47 +01:00
fstbin_data = ( sys_path / " fst.bin " ) . read_bytes ( )
2021-12-18 11:58:21 +01:00
iso_file . write ( fstbin_data )
2022-01-14 15:37:41 +01:00
dol_offset = bootbin . dol_offset ( )
2022-01-18 19:47:47 +01:00
logging . debug ( f " { sys_path / ' boot.dol ' } -> { iso_path } (0x { dol_offset : x } :0x { dol_offset + ( sys_path / ' boot.dol ' ) . stat ( ) . st_size : x } ) " )
2021-12-18 11:58:21 +01:00
iso_file . seek ( dol_offset )
2022-01-18 19:47:47 +01:00
iso_file . write ( ( sys_path / " boot.dol " ) . read_bytes ( ) )
2021-12-18 11:58:21 +01:00
# Now parse fst.bin for writing files in the iso
2022-01-14 15:37:41 +01:00
dir_id_path = { 0 : folder_path / " root " }
2021-12-18 11:58:21 +01:00
currentdir_path = folder_path / " root "
2022-01-14 15:37:41 +01:00
# root: id=0 so nextdir is the end
2021-12-18 11:58:21 +01:00
nextdir = int . from_bytes ( fstbin_data [ 8 : 12 ] , " big " , signed = False )
# offset of filenames block
base_names = nextdir * 12
2022-01-14 15:37:41 +01:00
# go to parent when id reach next dir
2021-12-18 11:58:21 +01:00
nextdir_arr = [ nextdir ]
2022-01-18 19:47:47 +01:00
# Check if there is new / removed files or dirs in the root folder
if nextdir - 1 != len ( list ( currentdir_path . glob ( " **/* " ) ) ) :
raise Exception ( f " Error - Invalid file count inside { currentdir_path } . Use --rebuild-fst to update the FST before packing. " )
2022-01-14 15:37:41 +01:00
for id in range ( 1 , base_names / / 12 ) :
i = id * 12
2021-12-18 11:58:21 +01:00
file_type = int . from_bytes ( fstbin_data [ i : i + 1 ] , " big " , signed = False )
name = fstbin_data [ base_names + int . from_bytes ( fstbin_data [ i + 1 : i + 4 ] , " big " , signed = False ) : ] . split ( b " \x00 " ) [ 0 ] . decode ( " utf-8 " )
2022-01-14 15:37:41 +01:00
while id == nextdir_arr [ - 1 ] :
2021-12-18 11:58:21 +01:00
currentdir_path = currentdir_path . parent
nextdir_arr . pop ( )
2022-01-14 15:37:41 +01:00
if file_type == FstTree . TYPE_DIR :
2021-12-18 11:58:21 +01:00
nextdir = int . from_bytes ( fstbin_data [ i + 8 : i + 12 ] , " big " , signed = False )
parentdir = int . from_bytes ( fstbin_data [ i + 4 : i + 8 ] , " big " , signed = False )
nextdir_arr . append ( nextdir )
2022-01-14 15:37:41 +01:00
currentdir_path = dir_id_path [ parentdir ] / name
dir_id_path [ id ] = currentdir_path
2021-12-18 11:58:21 +01:00
currentdir_path . mkdir ( exist_ok = True )
else :
2022-01-16 18:00:02 +01:00
file_offset = int . from_bytes ( fstbin_data [ i + 4 : i + 8 ] , " big " , signed = False )
file_len = int . from_bytes ( fstbin_data [ i + 8 : i + 12 ] , " big " , signed = False )
if ( currentdir_path / name ) . stat ( ) . st_size != file_len :
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 } ) " )
iso_file . seek ( file_offset )
iso_file . write ( ( currentdir_path / name ) . read_bytes ( ) )
2022-01-09 18:56:03 +01:00
def rebuild_fst ( self , folder_path : Path , align : int ) :
root_path = folder_path / " root "
sys_path = folder_path / " sys "
2022-01-10 22:17:20 +01:00
2022-01-16 18:00:02 +01:00
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 ( ( sys_path / " boot.bin " ) . read_bytes ( ) )
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 in original fst specials chars are after and not before chars
path_list = sorted ( [ path for path in root_path . glob ( ' **/* ' ) ] , key = lambda s : Path ( str ( s ) . upper ( ) ) )
for path in path_list :
fst_tree . add_node_by_path ( path )
logging . debug ( fst_tree )
fst_path = sys_path / " fst.bin "
2022-01-11 14:19:31 +01:00
2022-01-16 18:00:02 +01:00
logging . info ( " Writing fst in sys/fst.bin " )
fst_path . write_bytes ( fst_tree . get_fst ( ) )
2022-01-11 14:19:31 +01:00
2022-01-16 18:00:02 +01:00
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 )
2022-01-14 15:37:41 +01:00
2022-01-16 18:00:02 +01:00
( sys_path / " boot.bin " ) . write_bytes ( bootbin . data ( ) )
2021-12-18 11:58:21 +01:00
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__ )
2021-12-21 15:17:38 +01:00
parser . add_argument ( ' -v ' , ' --verbose ' , action = ' store_true ' , help = ' verbose mode ' )
2022-01-18 19:47:47 +01:00
parser . add_argument ( ' -a ' , ' --align ' , type = int , help = ' -a=10: alignment of files in the GCM ISO (default value is 4) ' , default = 4 )
2021-12-18 11:58:21 +01:00
parser . add_argument ( ' input_path ' , metavar = ' INPUT ' , help = ' ' )
parser . add_argument ( ' output_path ' , metavar = ' OUTPUT ' , help = ' ' , nargs = ' ? ' , default = " " )
group = parser . add_mutually_exclusive_group ( required = True )
group . add_argument ( ' -p ' , ' --pack ' , action = ' store_true ' , help = " -p source_folder (dest_file.iso) : Pack source_folder in new file source_folder.iso or dest_file.iso if specified " )
group . add_argument ( ' -u ' , ' --unpack ' , action = ' store_true ' , help = ' -u source_iso.iso (dest_folder) : Unpack the GCM/ISO in new folder source_iso or dest_folder if specified ' )
2022-01-09 18:56:03 +01:00
group . add_argument ( ' -r ' , ' --rebuild-fst ' , action = ' store_true ' , help = ' -r game_folder : Rebuild the game_folder/sys/fst.bin using files in game_folder/root ' )
2021-12-18 11:58:21 +01:00
return parser
if __name__ == ' __main__ ' :
2021-12-21 15:17:38 +01:00
logging . basicConfig ( format = ' %(levelname)s : %(message)s ' , level = logging . INFO )
2021-12-18 11:58:21 +01:00
args = get_argparser ( ) . parse_args ( )
p_input = Path ( args . input_path )
p_output = Path ( args . output_path )
2022-01-14 15:37:41 +01:00
gcm = Gcm ( )
2021-12-21 15:17:38 +01:00
if args . verbose :
logging . getLogger ( ) . setLevel ( logging . DEBUG )
2021-12-18 11:58:21 +01:00
if args . pack :
2022-01-11 14:19:31 +01:00
logging . info ( " ### Pack in new GCM iso " )
2021-12-18 11:58:21 +01:00
if ( p_output == Path ( " . " ) ) :
p_output = Path ( p_input . with_suffix ( " .iso " ) )
2022-01-16 18:00:02 +01:00
logging . info ( f " packing folder \" { p_input } \" in \" { p_output } \" " )
2021-12-18 11:58:21 +01:00
gcm . pack ( p_input , p_output )
elif args . unpack :
2022-01-11 14:19:31 +01:00
logging . info ( " ### Unpack GCM iso in new folder " )
2021-12-18 11:58:21 +01:00
gcm . unpack ( p_input , p_output )
2022-01-09 18:56:03 +01:00
elif args . rebuild_fst :
2022-01-11 14:19:31 +01:00
logging . info ( " ### Rebuilding FST and patching boot.bin " )
2022-01-10 22:42:22 +01:00
if args . align < 1 :
raise Exception ( " Align must be > 0 " )
2022-01-09 18:56:03 +01:00
logging . info ( f " Using alignment : { args . align } " )
gcm . rebuild_fst ( p_input , args . align )