diff --git a/tools/efs_parser.py b/tools/efs_parser.py index a16830a..cbcb0aa 100644 --- a/tools/efs_parser.py +++ b/tools/efs_parser.py @@ -3,10 +3,15 @@ from struct import unpack import zlib +EFS_MAGIC = b"\xAA\x55\xAA\x55" +PSP_COOKIE = b"$PSP" +PSPL2_COOKIE = b"$PL2" +PSP2_COOKIE = b"2PSP" +BHD_COOKIE = b"$BHD" +BHDL2_COOKIE = b"$BL2" +BHD2_COOKIE = b"2BHD" def extract_efs(rom): - efs_magic = b"\xAA\x55\xAA\x55" - efs_offsets = [ 0x020000, 0x820000, @@ -17,22 +22,21 @@ def extract_efs(rom): ] for offset in efs_offsets: - if rom[offset:offset+4] == efs_magic: + if rom[offset:offset+4] == EFS_MAGIC: return rom[offset:offset+0x4B] def parse_efs(efs): print("Parsing EFS") - efs_magic = b"\xAA\x55\xAA\x55" - if efs[0x0:0x4] != efs_magic: + if efs[0x0:0x4] != EFS_MAGIC: print(f"Invalid EFS magic {efs[0:4]}") exit() dir_offsets = [] - for offset in range(0x14, 0x30, 4): + for offset in range(0x10, 0x30, 4): ptr = int.from_bytes(efs[offset:offset+4], "little") - if ptr != 0x00000000 and ptr != 0xFFFFFFFE: + if ptr != 0x00000000 and ptr != 0xFFFFFFFE and ptr != 0xFFFFFFFF: dir_offsets.append(ptr) print(f"# EFS entries: {len(dir_offsets)}") @@ -45,13 +49,13 @@ def parse_efs(efs): def parse_dir(rom, offset): magic = rom[offset:offset+4] - if magic == b"$PSP": + if magic == PSP_COOKIE: parse_psp_dir(rom, offset) pass - elif magic == b"2PSP" or magic == b"2BHD": + elif magic == PSP2_COOKIE or magic == BHD2_COOKIE: parse_psp_combodir(rom, offset) pass - elif magic == b"$BHD": + elif magic == BHD_COOKIE: parse_bios_dir(rom, offset) pass else: @@ -66,7 +70,7 @@ def parse_psp_dir(rom, offset): psp_hdr = rom[offset:offset+0x10] psp_magic = psp_hdr[0:4] - if psp_magic != b"$PSP" and psp_magic != b"$PL2": + if psp_magic != PSP_COOKIE and psp_magic != PSPL2_COOKIE: print(f"Invalid magic {psp_magic} when parsing PSP directory") exit() @@ -149,7 +153,7 @@ def parse_psp_combodir(rom, offset): psp_hdr = rom[offset:offset+0x20] psp_magic = psp_hdr[0:4] - if psp_magic != b"2PSP" and psp_magic != b"2BHD": + if psp_magic != PSP2_COOKIE and psp_magic != BHD2_COOKIE: print(f"Invalid magic {psp_magic} when parsing PSP directory") exit() @@ -214,7 +218,7 @@ def parse_bios_dir(rom, offset): bios_hdr = rom[offset:offset+0x10] bios_magic = bios_hdr[0x0:0x4] - if bios_magic != b"$BHD" and bios_magic != b"$BL2": + if bios_magic != BHD_COOKIE and bios_magic != BHDL2_COOKIE: print(f"Invalid magic {bios_magic} when parsing BIOS directory") exit()