2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "DiscIO/Blob.h"
|
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
#include <algorithm>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <cstddef>
|
2015-12-08 00:53:42 +00:00
|
|
|
#include <limits>
|
2015-12-07 04:15:51 +00:00
|
|
|
#include <memory>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <string>
|
2016-12-21 11:50:15 +00:00
|
|
|
#include <utility>
|
2014-02-21 00:47:53 +00:00
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2020-09-15 10:29:41 +00:00
|
|
|
#include "Common/IOFile.h"
|
2020-06-21 18:41:50 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "DiscIO/CISOBlob.h"
|
|
|
|
#include "DiscIO/CompressedBlob.h"
|
2017-06-07 18:32:09 +00:00
|
|
|
#include "DiscIO/DirectoryBlob.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/FileBlob.h"
|
2022-07-31 08:14:03 +00:00
|
|
|
#include "DiscIO/NFSBlob.h"
|
2023-02-12 21:08:21 +00:00
|
|
|
#include "DiscIO/SplitFileBlob.h"
|
2016-12-17 13:48:49 +00:00
|
|
|
#include "DiscIO/TGCBlob.h"
|
2018-09-28 15:04:16 +00:00
|
|
|
#include "DiscIO/WIABlob.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/WbfsBlob.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
namespace DiscIO
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2020-06-21 18:41:50 +00:00
|
|
|
std::string GetName(BlobType blob_type, bool translate)
|
|
|
|
{
|
|
|
|
const auto translate_str = [translate](const std::string& str) {
|
|
|
|
return translate ? Common::GetStringT(str.c_str()) : str;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (blob_type)
|
|
|
|
{
|
|
|
|
case BlobType::PLAIN:
|
|
|
|
return "ISO";
|
|
|
|
case BlobType::DIRECTORY:
|
|
|
|
return translate_str("Directory");
|
|
|
|
case BlobType::GCZ:
|
|
|
|
return "GCZ";
|
|
|
|
case BlobType::CISO:
|
|
|
|
return "CISO";
|
|
|
|
case BlobType::WBFS:
|
|
|
|
return "WBFS";
|
|
|
|
case BlobType::TGC:
|
|
|
|
return "TGC";
|
|
|
|
case BlobType::WIA:
|
|
|
|
return "WIA";
|
|
|
|
case BlobType::RVZ:
|
|
|
|
return "RVZ";
|
2021-10-26 00:01:16 +00:00
|
|
|
case BlobType::MOD_DESCRIPTOR:
|
|
|
|
return translate_str("Mod");
|
2022-07-31 08:14:03 +00:00
|
|
|
case BlobType::NFS:
|
|
|
|
return "NFS";
|
2023-02-12 21:08:21 +00:00
|
|
|
case BlobType::SPLIT_PLAIN:
|
|
|
|
return translate_str("Multi-part ISO");
|
2020-06-21 18:41:50 +00:00
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
void SectorReader::SetSectorSize(int blocksize)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2016-04-26 11:24:08 +00:00
|
|
|
m_block_size = std::max(blocksize, 0);
|
2015-12-08 00:53:42 +00:00
|
|
|
for (auto& cache_entry : m_cache)
|
2016-04-26 11:24:08 +00:00
|
|
|
{
|
|
|
|
cache_entry.Reset();
|
|
|
|
cache_entry.data.resize(m_chunk_blocks * m_block_size);
|
|
|
|
}
|
|
|
|
}
|
2015-12-08 00:53:42 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
void SectorReader::SetChunkSize(int block_cnt)
|
|
|
|
{
|
|
|
|
m_chunk_blocks = std::max(block_cnt, 1);
|
|
|
|
// Clear cache and resize the data arrays
|
|
|
|
SetSectorSize(m_block_size);
|
2008-09-17 23:25:35 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
SectorReader::~SectorReader()
|
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
const SectorReader::Cache* SectorReader::FindCacheLine(u64 block_num)
|
2008-09-17 23:25:35 +00:00
|
|
|
{
|
2024-09-22 01:09:34 +00:00
|
|
|
auto itr =
|
|
|
|
std::ranges::find_if(m_cache, [&](const Cache& entry) { return entry.Contains(block_num); });
|
2016-04-26 11:24:08 +00:00
|
|
|
if (itr == m_cache.end())
|
|
|
|
return nullptr;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
itr->MarkUsed();
|
|
|
|
return &*itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SectorReader::Cache* SectorReader::GetEmptyCacheLine()
|
|
|
|
{
|
|
|
|
Cache* oldest = &m_cache[0];
|
|
|
|
// Find the Least Recently Used cache line to replace.
|
2016-07-06 10:37:59 +00:00
|
|
|
std::for_each(m_cache.begin() + 1, m_cache.end(), [&](Cache& line) {
|
|
|
|
if (line.IsLessRecentlyUsedThan(*oldest))
|
|
|
|
{
|
|
|
|
oldest->ShiftLRU();
|
|
|
|
oldest = &line;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
line.ShiftLRU();
|
|
|
|
});
|
2016-04-26 11:24:08 +00:00
|
|
|
oldest->Reset();
|
|
|
|
return oldest;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SectorReader::Cache* SectorReader::GetCacheLine(u64 block_num)
|
|
|
|
{
|
|
|
|
if (auto entry = FindCacheLine(block_num))
|
|
|
|
return entry;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Cache miss. Fault in the missing entry.
|
|
|
|
Cache* cache = GetEmptyCacheLine();
|
|
|
|
// We only read aligned chunks, this avoids duplicate overlapping entries.
|
|
|
|
u64 chunk_idx = block_num / m_chunk_blocks;
|
|
|
|
u32 blocks_read = ReadChunk(cache->data.data(), chunk_idx);
|
|
|
|
if (!blocks_read)
|
|
|
|
return nullptr;
|
|
|
|
cache->Fill(chunk_idx * m_chunk_blocks, blocks_read);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Secondary check for out-of-bounds read.
|
|
|
|
// If we got less than m_chunk_blocks, we may still have missed.
|
|
|
|
// We do this after the cache fill since the cache line itself is
|
|
|
|
// fine, the problem is being asked to read past the end of the disk.
|
|
|
|
return cache->Contains(block_num) ? cache : nullptr;
|
2008-09-17 23:25:35 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
bool SectorReader::Read(u64 offset, u64 size, u8* out_ptr)
|
|
|
|
{
|
2020-01-13 21:08:44 +00:00
|
|
|
if (offset + size > GetDataSize())
|
|
|
|
return false;
|
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
u64 remain = size;
|
2016-04-26 11:24:08 +00:00
|
|
|
u64 block = 0;
|
|
|
|
u32 position_in_block = static_cast<u32>(offset % m_block_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
while (remain > 0)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2016-04-26 11:24:08 +00:00
|
|
|
block = offset / m_block_size;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
const Cache* cache = GetCacheLine(block);
|
|
|
|
if (!cache)
|
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Cache entries are aligned chunks, we may not want to read from the start
|
|
|
|
u32 read_offset = static_cast<u32>(block - cache->block_idx) * m_block_size + position_in_block;
|
|
|
|
u32 can_read = m_block_size * cache->num_blocks - read_offset;
|
|
|
|
u32 was_read = static_cast<u32>(std::min<u64>(can_read, remain));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2024-09-28 22:37:09 +00:00
|
|
|
std::copy_n(cache->data.begin() + read_offset, was_read, out_ptr);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
offset += was_read;
|
|
|
|
out_ptr += was_read;
|
|
|
|
remain -= was_read;
|
|
|
|
position_in_block = 0;
|
|
|
|
}
|
2008-09-17 23:25:35 +00:00
|
|
|
return true;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Crap default implementation if not overridden.
|
|
|
|
bool SectorReader::ReadMultipleAlignedBlocks(u64 block_num, u64 cnt_blocks, u8* out_ptr)
|
2009-02-22 16:48:54 +00:00
|
|
|
{
|
2016-04-26 11:24:08 +00:00
|
|
|
for (u64 i = 0; i < cnt_blocks; ++i)
|
2009-02-22 16:48:54 +00:00
|
|
|
{
|
2016-04-26 11:24:08 +00:00
|
|
|
if (!GetBlock(block_num + i, out_ptr))
|
|
|
|
return false;
|
|
|
|
out_ptr += m_block_size;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-08 00:53:42 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
u32 SectorReader::ReadChunk(u8* buffer, u64 chunk_num)
|
|
|
|
{
|
|
|
|
u64 block_num = chunk_num * m_chunk_blocks;
|
|
|
|
u32 cnt_blocks = m_chunk_blocks;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// If we are reading the end of a disk, there may not be enough blocks to
|
|
|
|
// read a whole chunk. We need to clamp down in that case.
|
2016-05-29 15:55:34 +00:00
|
|
|
u64 end_block = (GetDataSize() + m_block_size - 1) / m_block_size;
|
2016-04-26 11:24:08 +00:00
|
|
|
if (end_block)
|
|
|
|
cnt_blocks = static_cast<u32>(std::min<u64>(m_chunk_blocks, end_block - block_num));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
if (ReadMultipleAlignedBlocks(block_num, cnt_blocks, buffer))
|
|
|
|
{
|
|
|
|
if (cnt_blocks < m_chunk_blocks)
|
|
|
|
{
|
|
|
|
std::fill(buffer + cnt_blocks * m_block_size, buffer + m_chunk_blocks * m_block_size, 0u);
|
|
|
|
}
|
|
|
|
return cnt_blocks;
|
2009-02-22 16:48:54 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// end_block may be zero on real disks if we fail to get the media size.
|
|
|
|
// We have to fallback to probing the disk instead.
|
|
|
|
if (!end_block)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < cnt_blocks; ++i)
|
|
|
|
{
|
|
|
|
if (!GetBlock(block_num + i, buffer))
|
|
|
|
{
|
2024-08-23 02:24:49 +00:00
|
|
|
std::fill_n(buffer, (cnt_blocks - i) * m_block_size, 0u);
|
2016-04-26 11:24:08 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
buffer += m_block_size;
|
|
|
|
}
|
|
|
|
return cnt_blocks;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-02-22 16:48:54 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::unique_ptr<BlobReader> CreateBlobReader(const std::string& filename)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2016-12-21 10:30:12 +00:00
|
|
|
File::IOFile file(filename, "rb");
|
|
|
|
u32 magic;
|
|
|
|
if (!file.ReadArray(&magic, 1))
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-06-07 18:32:09 +00:00
|
|
|
// Conveniently, every supported file format (except for plain disc images and
|
|
|
|
// extracted discs) starts with a 4-byte magic number that identifies the format,
|
|
|
|
// so we just need a simple switch statement to create the right blob type. If the
|
|
|
|
// magic number doesn't match any known magic number and the directory structure
|
|
|
|
// doesn't match the directory blob format, we assume it's a plain disc image. If
|
|
|
|
// that assumption is wrong, the volume code that runs later will notice the error
|
|
|
|
// because the blob won't provide the right data when reading the GC/Wii disc header.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-12-21 10:30:12 +00:00
|
|
|
switch (magic)
|
|
|
|
{
|
|
|
|
case CISO_MAGIC:
|
2016-12-21 11:50:15 +00:00
|
|
|
return CISOFileReader::Create(std::move(file));
|
2016-12-21 10:30:12 +00:00
|
|
|
case GCZ_MAGIC:
|
2016-12-21 11:50:15 +00:00
|
|
|
return CompressedBlobReader::Create(std::move(file), filename);
|
2016-12-21 10:30:12 +00:00
|
|
|
case TGC_MAGIC:
|
2016-12-21 11:50:15 +00:00
|
|
|
return TGCFileReader::Create(std::move(file));
|
2016-12-21 10:30:12 +00:00
|
|
|
case WBFS_MAGIC:
|
2016-12-21 13:01:00 +00:00
|
|
|
return WbfsFileReader::Create(std::move(file), filename);
|
2018-09-28 15:04:16 +00:00
|
|
|
case WIA_MAGIC:
|
|
|
|
return WIAFileReader::Create(std::move(file), filename);
|
2020-05-17 13:34:50 +00:00
|
|
|
case RVZ_MAGIC:
|
|
|
|
return RVZFileReader::Create(std::move(file), filename);
|
2022-07-31 08:14:03 +00:00
|
|
|
case NFS_MAGIC:
|
|
|
|
return NFSFileReader::Create(std::move(file), filename);
|
2016-12-21 10:30:12 +00:00
|
|
|
default:
|
2017-06-09 10:56:36 +00:00
|
|
|
if (auto directory_blob = DirectoryBlobReader::Create(filename))
|
|
|
|
return std::move(directory_blob);
|
2023-02-12 21:08:21 +00:00
|
|
|
if (auto split_blob = SplitPlainFileReader::Create(filename))
|
|
|
|
return std::move(split_blob);
|
2017-06-07 18:32:09 +00:00
|
|
|
|
2016-12-21 11:50:15 +00:00
|
|
|
return PlainFileReader::Create(std::move(file));
|
2016-12-21 10:30:12 +00:00
|
|
|
}
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace DiscIO
|