2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2012-05-03 14:09:01 +00:00
|
|
|
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "DiscIO/WbfsBlob.h"
|
|
|
|
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2015-12-07 04:15:51 +00:00
|
|
|
#include <memory>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <string>
|
2016-12-21 13:01:00 +00:00
|
|
|
#include <utility>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
#include "Common/Align.h"
|
2016-12-21 13:01:00 +00:00
|
|
|
#include "Common/Assert.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-09-15 10:29:41 +00:00
|
|
|
#include "Common/IOFile.h"
|
2021-03-20 10:50:00 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "Common/Swap.h"
|
2012-05-03 14:09:01 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2014-09-01 19:48:02 +00:00
|
|
|
static const u64 WII_SECTOR_SIZE = 0x8000;
|
|
|
|
static const u64 WII_SECTOR_COUNT = 143432 * 2;
|
|
|
|
static const u64 WII_DISC_HEADER_SIZE = 256;
|
2012-05-03 14:09:01 +00:00
|
|
|
|
2016-12-21 13:01:00 +00:00
|
|
|
WbfsFileReader::WbfsFileReader(File::IOFile file, const std::string& path)
|
2016-12-21 13:05:33 +00:00
|
|
|
: m_size(0), m_good(false)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2016-12-21 13:01:00 +00:00
|
|
|
if (!AddFileToList(std::move(file)))
|
2012-05-03 14:09:01 +00:00
|
|
|
return;
|
2016-12-21 13:01:00 +00:00
|
|
|
OpenAdditionalFiles(path);
|
|
|
|
if (!ReadHeader())
|
|
|
|
return;
|
|
|
|
m_good = true;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2012-05-03 14:09:01 +00:00
|
|
|
// Grab disc info (assume slot 0, checked in ReadHeader())
|
2015-12-08 00:53:42 +00:00
|
|
|
m_wlba_table.resize(m_blocks_per_disc);
|
2016-12-31 21:51:16 +00:00
|
|
|
m_files[0].file.Seek(m_hd_sector_size + WII_DISC_HEADER_SIZE /*+ i * m_disc_info_size*/,
|
|
|
|
SEEK_SET);
|
|
|
|
m_files[0].file.ReadBytes(m_wlba_table.data(), m_blocks_per_disc * sizeof(u16));
|
2015-09-17 18:29:41 +00:00
|
|
|
for (size_t i = 0; i < m_blocks_per_disc; i++)
|
|
|
|
m_wlba_table[i] = Common::swap16(m_wlba_table[i]);
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WbfsFileReader::~WbfsFileReader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-17 08:58:54 +00:00
|
|
|
u64 WbfsFileReader::GetDataSize() const
|
|
|
|
{
|
|
|
|
return WII_SECTOR_COUNT * WII_SECTOR_SIZE;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:01:00 +00:00
|
|
|
void WbfsFileReader::OpenAdditionalFiles(const std::string& path)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2016-12-21 13:01:00 +00:00
|
|
|
if (path.length() < 4)
|
|
|
|
return;
|
|
|
|
|
2019-02-12 22:47:17 +00:00
|
|
|
ASSERT(!m_files.empty()); // The code below gives .wbf0 for index 0, but it should be .wbfs
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
while (true)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
|
|
|
// Replace last character with index (e.g. wbfs = wbf1)
|
2016-12-21 13:05:33 +00:00
|
|
|
if (m_files.size() >= 10)
|
|
|
|
return;
|
2016-12-21 13:01:00 +00:00
|
|
|
std::string current_path = path;
|
2016-12-21 13:05:33 +00:00
|
|
|
current_path.back() = static_cast<char>('0' + m_files.size());
|
2016-12-21 13:01:00 +00:00
|
|
|
if (!AddFileToList(File::IOFile(current_path, "rb")))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-12-21 13:01:00 +00:00
|
|
|
bool WbfsFileReader::AddFileToList(File::IOFile file)
|
|
|
|
{
|
|
|
|
if (!file.IsOpen())
|
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-12-21 13:01:00 +00:00
|
|
|
const u64 file_size = file.GetSize();
|
2016-12-31 21:51:16 +00:00
|
|
|
m_files.emplace_back(std::move(file), m_size, file_size);
|
2016-12-21 13:01:00 +00:00
|
|
|
m_size += file_size;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-12-21 13:01:00 +00:00
|
|
|
return true;
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WbfsFileReader::ReadHeader()
|
|
|
|
{
|
|
|
|
// Read hd size info
|
2016-12-31 21:51:16 +00:00
|
|
|
m_files[0].file.Seek(0, SEEK_SET);
|
|
|
|
m_files[0].file.ReadBytes(&m_header, sizeof(WbfsHeader));
|
2016-12-21 10:30:12 +00:00
|
|
|
if (m_header.magic != WBFS_MAGIC)
|
|
|
|
return false;
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2016-12-21 10:30:12 +00:00
|
|
|
m_header.hd_sector_count = Common::swap32(m_header.hd_sector_count);
|
2015-09-17 22:00:14 +00:00
|
|
|
m_hd_sector_size = 1ull << m_header.hd_sector_shift;
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2015-09-17 22:00:14 +00:00
|
|
|
if (m_size != (m_header.hd_sector_count * m_hd_sector_size))
|
2012-05-03 14:09:01 +00:00
|
|
|
return false;
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2012-05-03 14:09:01 +00:00
|
|
|
// Read wbfs cluster info
|
2015-09-17 22:00:14 +00:00
|
|
|
m_wbfs_sector_size = 1ull << m_header.wbfs_sector_shift;
|
2014-09-01 19:48:02 +00:00
|
|
|
m_wbfs_sector_count = m_size / m_wbfs_sector_size;
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2014-09-01 19:48:02 +00:00
|
|
|
if (m_wbfs_sector_size < WII_SECTOR_SIZE)
|
2012-05-03 14:09:01 +00:00
|
|
|
return false;
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2015-09-19 08:07:09 +00:00
|
|
|
m_blocks_per_disc =
|
|
|
|
(WII_SECTOR_COUNT * WII_SECTOR_SIZE + m_wbfs_sector_size - 1) / m_wbfs_sector_size;
|
2015-09-19 00:23:38 +00:00
|
|
|
m_disc_info_size =
|
2016-11-27 10:56:22 +00:00
|
|
|
Common::AlignUp(WII_DISC_HEADER_SIZE + m_blocks_per_disc * sizeof(u16), m_hd_sector_size);
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2015-09-17 22:00:14 +00:00
|
|
|
return m_header.disc_table[0] != 0;
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WbfsFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
|
|
|
|
{
|
2020-01-13 21:08:44 +00:00
|
|
|
if (offset + nbytes > GetDataSize())
|
|
|
|
return false;
|
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
while (nbytes)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2015-07-06 13:41:37 +00:00
|
|
|
u64 read_size;
|
2012-05-03 14:09:01 +00:00
|
|
|
File::IOFile& data_file = SeekToCluster(offset, &read_size);
|
2015-07-06 13:41:37 +00:00
|
|
|
if (read_size == 0)
|
|
|
|
return false;
|
2015-09-19 00:23:38 +00:00
|
|
|
read_size = std::min(read_size, nbytes);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-12-14 12:16:21 +00:00
|
|
|
if (!data_file.ReadBytes(out_ptr, read_size))
|
|
|
|
{
|
|
|
|
data_file.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2012-05-03 14:09:01 +00:00
|
|
|
out_ptr += read_size;
|
|
|
|
nbytes -= read_size;
|
|
|
|
offset += read_size;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2012-05-03 14:09:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
File::IOFile& WbfsFileReader::SeekToCluster(u64 offset, u64* available)
|
|
|
|
{
|
2015-09-17 22:00:14 +00:00
|
|
|
u64 base_cluster = (offset >> m_header.wbfs_sector_shift);
|
2014-03-10 11:30:55 +00:00
|
|
|
if (base_cluster < m_blocks_per_disc)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2015-09-17 18:29:41 +00:00
|
|
|
u64 cluster_address = m_wbfs_sector_size * m_wlba_table[base_cluster];
|
2014-09-01 19:48:02 +00:00
|
|
|
u64 cluster_offset = offset & (m_wbfs_sector_size - 1);
|
2012-05-03 14:09:01 +00:00
|
|
|
u64 final_address = cluster_address + cluster_offset;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-01-22 16:27:37 +00:00
|
|
|
for (FileEntry& file_entry : m_files)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2016-12-31 21:51:16 +00:00
|
|
|
if (final_address < (file_entry.base_address + file_entry.size))
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2016-12-31 21:51:16 +00:00
|
|
|
file_entry.file.Seek(final_address - file_entry.base_address, SEEK_SET);
|
2014-03-10 11:30:55 +00:00
|
|
|
if (available)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2016-12-31 21:51:16 +00:00
|
|
|
u64 till_end_of_file = file_entry.size - (final_address - file_entry.base_address);
|
2014-09-01 19:48:02 +00:00
|
|
|
u64 till_end_of_sector = m_wbfs_sector_size - cluster_offset;
|
2012-05-03 14:09:01 +00:00
|
|
|
*available = std::min(till_end_of_file, till_end_of_sector);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-12-31 21:51:16 +00:00
|
|
|
return file_entry.file;
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-03-20 10:50:00 +00:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Read beyond end of disc");
|
2015-07-06 13:41:37 +00:00
|
|
|
if (available)
|
|
|
|
*available = 0;
|
2016-12-31 21:51:16 +00:00
|
|
|
m_files[0].file.Seek(0, SEEK_SET);
|
|
|
|
return m_files[0].file;
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:01:00 +00:00
|
|
|
std::unique_ptr<WbfsFileReader> WbfsFileReader::Create(File::IOFile file, const std::string& path)
|
2013-03-19 13:59:41 +00:00
|
|
|
{
|
2016-12-21 13:01:00 +00:00
|
|
|
auto reader = std::unique_ptr<WbfsFileReader>(new WbfsFileReader(std::move(file), path));
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2015-12-07 04:15:51 +00:00
|
|
|
if (!reader->IsGood())
|
|
|
|
reader.reset();
|
|
|
|
|
|
|
|
return reader;
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace DiscIO
|