Skip to content

Commit

Permalink
Split up entry types into actual type and flags
Browse files Browse the repository at this point in the history
  • Loading branch information
cwerling committed Jul 11, 2023
1 parent 1a5edc8 commit ec478c0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
4 changes: 4 additions & 0 deletions psptool/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def _parse_entries(self):
for key, word in zip(self.ENTRY_FIELDS, chunker(entry_bytes, 4)):
entry_fields[key] = struct.unpack('<I', word)[0]

entry_fields['type_flags'] = entry_fields['type'] >> 8
entry_fields['type'] = entry_fields['type'] & 0xFFFF

entry_fields['offset'] &= self.rom.addr_mask
destination = None

Expand All @@ -135,6 +138,7 @@ def _parse_entries(self):
try:
entry = Entry.from_fields(self, self.parent_buffer,
entry_fields['type'],
entry_fields['type_flags'],
entry_fields['size'],
entry_fields['offset'],
self.rom,
Expand Down
27 changes: 13 additions & 14 deletions psptool/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ class Entry(NestedBuffer):
0x70: '!BL2_SECONDARY_DIRECTORY',
0x15f: '!FW_PSP_SMUSCS_2', # seems to be a secondary FW_PSP_SMUSCS (see above)
0x112: '!SMU_OFF_CHIP_FW_3', # seems to tbe a tertiary SMU image (see above)
0x10062: '!UEFI-IMAGE',
0x30062: '!UEFI-IMAGE',
0xdead: '!KEY_NOT_IN_DIR'

}
Expand All @@ -135,19 +133,15 @@ class TypeError(Exception):
pass

@classmethod
def from_fields(cls, parent_directory, parent_buffer, type_, size, offset, blob, psptool, destination: int = None):
def from_fields(cls, parent_directory, parent_buffer, type_, type_flags, size, offset, blob, psptool, destination: int = None):
# Try to parse these ID's as a key entry
# todo: consolidate these constants with Directory._ENTRY_TYPES_PUBKEY
PUBKEY_ENTRY_TYPES = [0x0, 0x9, 0xa, 0x5, 0xd, 0x43, 0x4e, 0xdead]

# Types known to have no PSP HDR
# TODO: Find a better way to identify those entries
NO_HDR_ENTRY_TYPES = [0x4, 0xb, 0x21, 0x40, 0x70, 0x30062, 0x6, 0x61, 0x60,
0x68, 0x100060, 0x100068, 0x5f, 0x15f, 0x1a, 0x22, 0x63,
0x67, 0x66, 0x100066, 0x200066, 0x300066, 0x10062,
0x400066, 0x500066, 0x800068, 0x61, 0x200060, 0x300060,
0x300068, 0x400068, 0x500068, 0x400060, 0x500060, 0x200068,
0x7, 0x38, 0x46, 0x54, 0x600060, 0x700060, 0x600068, 0x700068]
NO_HDR_ENTRY_TYPES = [0x4, 0xb, 0x21, 0x40, 0x70, 0x6, 0x61, 0x60, 0x68, 0x5f, 0x15f, 0x1a, 0x22, 0x63, 0x67,
0x66, 0x62, 0x61, 0x7, 0x38, 0x46, 0x54]

NO_SIZE_ENTRY_TYPES = [0xb]

Expand All @@ -165,6 +159,7 @@ def from_fields(cls, parent_directory, parent_buffer, type_, size, offset, blob,
parent_directory,
parent_buffer,
type_,
type_flags,
size,
offset,
blob,
Expand All @@ -177,12 +172,13 @@ def from_fields(cls, parent_directory, parent_buffer, type_, size, offset, blob,
elif type_ in PUBKEY_ENTRY_TYPES:
# Option 2: it's a PubkeyEntry
try:
new_entry = PubkeyEntry(parent_directory, parent_buffer, type_, size, offset, blob, psptool)
new_entry = PubkeyEntry(parent_directory, parent_buffer, type_, type_flags, size, offset, blob, psptool)
except Exception as e:
new_entry = Entry(
parent_directory,
parent_buffer,
type_,
type_flags,
size,
offset,
blob,
Expand All @@ -194,12 +190,13 @@ def from_fields(cls, parent_directory, parent_buffer, type_, size, offset, blob,
elif type_ in Entry.KEY_STORE_TYPES:
# Option 2: it's a KeyStoreEntry
try:
new_entry = KeyStoreEntry(parent_directory, parent_buffer, type_, size, offset, blob, psptool)
new_entry = KeyStoreEntry(parent_directory, parent_buffer, type_, type_flags, size, offset, blob, psptool)
except:
new_entry = Entry(
parent_directory,
parent_buffer,
type_,
type_flags,
size,
offset,
blob,
Expand All @@ -213,14 +210,15 @@ def from_fields(cls, parent_directory, parent_buffer, type_, size, offset, blob,
# If the size in the directory is zero, set the size to hdr len
size = HeaderEntry.HEADER_LEN
try:
new_entry = HeaderEntry(parent_directory, parent_buffer, type_, size, offset, blob, psptool)
new_entry = HeaderEntry(parent_directory, parent_buffer, type_, type_flags, size, offset, blob, psptool)
if size == 0:
psptool.ph.print_warning(f"Entry with zero size. Type: {type_}. Dir: 0x{offset:x}")
except:
new_entry = Entry(
parent_directory,
parent_buffer,
type_,
type_flags,
size,
offset,
blob,
Expand Down Expand Up @@ -282,7 +280,7 @@ def from_blob(cls, binary, id_, type_, compressed, signed, psptool, private_key:
# Set zlib_size
blob[0x54:0x58] = zlib_size.to_bytes(4, 'little')

entry = HeaderEntry(None, blob, id_, total_size, 0x0, blob, psptool)
entry = HeaderEntry(None, blob, id_, None, total_size, 0x0, blob, psptool)

if signed:
entry.signature[:] = private_key.sign(entry.get_signed_bytes())
Expand All @@ -291,14 +289,15 @@ def from_blob(cls, binary, id_, type_, compressed, signed, psptool, private_key:
else:
raise Entry.TypeError()

def __init__(self, parent_directory, parent_buffer, type_, buffer_size, buffer_offset: int, blob, psptool,
def __init__(self, parent_directory, parent_buffer, type_, type_flags, buffer_size, buffer_offset: int, blob, psptool,
destination: int = None):
super().__init__(parent_buffer, buffer_size, buffer_offset=buffer_offset)

# TODO: Fix to reference of FET
self.blob = blob
self.psptool = psptool
self.type = type_
self.type_flags = type_flags
self.destination = destination
# todo: deduplicate Entry objects pointing to the same address (in `from_fields`?)
self.references = [parent_directory] if parent_directory is not None else []
Expand Down
3 changes: 2 additions & 1 deletion psptool/psptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def ls_entries(self, entries=None, verbose=False):
entries = sorted(self.blob.unique_entries())

basic_fields = ['', ' ', 'Entry', 'Address', 'Size', 'Type', 'Magic/ID', 'Version', 'Info']
verbose_fields = ['MD5', 'size_signed', 'size_full', 'size_packed', 'load_addr']
verbose_fields = ['type_flags', 'MD5', 'size_signed', 'size_full', 'size_packed', 'load_addr']

t = PrettyTable(basic_fields + verbose_fields)
t.align = 'r'
Expand Down Expand Up @@ -144,6 +144,7 @@ def ls_entries(self, entries=None, verbose=False):
entry.get_readable_magic(),
entry.get_readable_version(),
', '.join(info),
hex(entry.type_flags),
entry.md5()[:4].upper()
]

Expand Down

0 comments on commit ec478c0

Please sign in to comment.