Don't drop an ISO's trailing partial sector

Not every disc image is a whole number of 2048-byte sectors - tools that build
pre-patched ISOs write images that stop partway through their last one, with a
file legitimately ending there. Two things then conspired to lose that tail.

FileBlockDevice::GetNumBlocks() rounds down, so the partial sector isn't
counted, and the file size clamp in ISOFileSystem measured what the image holds
in whole blocks. A file running to the last byte of such an image got clamped
short - by up to a sector - before anything read it.

FileBlockDevice::ReadBlock() then returned false for a short read of that
sector, and ISOFileSystem::ReadFile substitutes an all-zero sector when a read
fails, so even the bytes that were there came back as zeroes.

Measure the clamp in bytes via GetUncompressedSize() instead of blocks, and
treat a short read at the end of the image as a success with the rest of the
sector zeroed. GetUncompressedSize() defaults to the block-based value and is
only overridden by FileBlockDevice, so nothing else changes behaviour.

Also report why a module was rejected. "Failed to load module" named the file
and nothing else, and the truncation check logged only the byte count, which
points at the executable when the real cause is that the loader was handed
fewer bytes than the file has. ElfReader now keeps the reason for a failed
LoadInto, __KernelLoadELFFromPtr puts it in the error string that reaches the
user, and both messages say which header table overran and by how much.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Henrik Rydgård
2026-09-08 12:25:21 -06:00
co-authored by Claude Opus 5
parent 1fc823681f
commit 51ec045947
5 changed files with 48 additions and 8 deletions
+8 -3
View File
@@ -307,10 +307,15 @@ void ISOFileSystem::ReadDirectory(TreeEntry *root) const {
// drop the entry - truncated ISOs are common and used to work with just the warning
// above, and dropping EBOOT.BIN would turn that into an unbootable game. For a sane
// file this is a no-op, since the extent always fits in its sectors.
// Measured in bytes, not whole sectors: an image whose length isn't a multiple of the
// sector size still contains its final partial sector, and a file is allowed to end
// there. Counting blocks discards that tail, which clamped real files short - a dump
// with EBOOT.BIN running to the last byte of the image lost the end of it, and the ELF
// section headers that live there went with it.
if (isFile) {
const u64 numBlocks = blockDevice->GetNumBlocks();
const u64 firstSector = dir.firstDataSector;
const s64 availableBytes = firstSector >= numBlocks ? 0 : (s64)((numBlocks - firstSector) * (u64)sectorSize);
const u64 imageBytes = blockDevice->GetUncompressedSize();
const u64 firstByte = (u64)dir.firstDataSector * (u64)sectorSize;
const s64 availableBytes = firstByte >= imageBytes ? 0 : (s64)(imageBytes - firstByte);
if (entry->size > availableBytes) {
entry->size = availableBytes;
}