#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Is a zipped PSP image's executable encrypted, and which firmware does it ask for? For each zip holding one .iso, read the ISO9660 directory by seeking inside the compressed stream (nothing is extracted to disk), then print the first 4 bytes of PSP_GAME/SYSDIR/EBOOT.BIN (``~PSP`` is Sony's encrypted container, ``\\x7fELF`` a plain module), PARAM.SFO's PSP_SYSTEM_VER, and the names in SYSDIR. Nothing else of the game is read, kept or printed. Slow on large images (seeking decompresses), so use it on a handful. python -I scripts/peek_umd.py "$REDUMP_DIR/Sony - PlayStation Portable/Some Game (USA).zip" """ from __future__ import annotations import struct import sys import zipfile SECTOR = 2048 def directory(read, lba: int, size: int) -> dict[str, tuple[int, int]]: data = read(lba, (size + SECTOR - 1) // SECTOR) out: dict[str, tuple[int, int]] = {} offset = 0 while offset < len(data): length = data[offset] if length == 0: # records never cross a sector: skip to the next one offset = (offset // SECTOR + 1) * SECTOR continue record = data[offset : offset + length] extent, extent_size = struct.unpack_from(" str: with zipfile.ZipFile(path) as archive: member = next(i for i in archive.infolist() if i.filename.lower().endswith(".iso")) with archive.open(member) as stream: def read(lba: int, count: int = 1) -> bytes: stream.seek(lba * SECTOR) return stream.read(count * SECTOR) root = read(16)[156 : 156 + 34] # primary volume descriptor, root record top = directory(read, struct.unpack_from(" int: for path in sys.argv[1:]: try: print(peek(path)) except (OSError, KeyError, StopIteration, zipfile.BadZipFile, struct.error) as error: print(f"ERROR {error!r}\t{path}") return 0 if __name__ == "__main__": sys.exit(main())