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
|
|
|
|
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>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
#include "Common/Align.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/CommonFuncs.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/WbfsBlob.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
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
WbfsFileReader::WbfsFileReader(const std::string& filename)
|
2016-06-24 08:43:46 +00:00
|
|
|
: m_total_files(0), m_size(0), m_good(true)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (filename.length() < 4 || !OpenFiles(filename) || !ReadHeader())
|
|
|
|
{
|
|
|
|
m_good = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab disc info (assume slot 0, checked in ReadHeader())
|
|
|
|
m_wlba_table.resize(m_blocks_per_disc);
|
|
|
|
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));
|
|
|
|
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
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return WII_SECTOR_COUNT * WII_SECTOR_SIZE;
|
2015-09-17 08:58:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
bool WbfsFileReader::OpenFiles(const std::string& filename)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_total_files = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
auto new_entry = std::make_unique<file_entry>();
|
|
|
|
|
|
|
|
// Replace last character with index (e.g. wbfs = wbf1)
|
|
|
|
std::string path = filename;
|
|
|
|
if (m_total_files != 0)
|
|
|
|
{
|
|
|
|
path[path.length() - 1] = '0' + m_total_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!new_entry->file.Open(path, "rb"))
|
|
|
|
{
|
|
|
|
return m_total_files != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_entry->base_address = m_size;
|
|
|
|
new_entry->size = new_entry->file.GetSize();
|
|
|
|
m_size += new_entry->size;
|
|
|
|
|
|
|
|
m_total_files++;
|
|
|
|
m_files.emplace_back(std::move(new_entry));
|
|
|
|
}
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WbfsFileReader::ReadHeader()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Read hd size info
|
|
|
|
m_files[0]->file.ReadBytes(&m_header, sizeof(WbfsHeader));
|
|
|
|
m_header.hd_sector_count = Common::swap32(m_header.hd_sector_count);
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_hd_sector_size = 1ull << m_header.hd_sector_shift;
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_size != (m_header.hd_sector_count * m_hd_sector_size))
|
|
|
|
return false;
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Read wbfs cluster info
|
|
|
|
m_wbfs_sector_size = 1ull << m_header.wbfs_sector_shift;
|
|
|
|
m_wbfs_sector_count = m_size / m_wbfs_sector_size;
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_wbfs_sector_size < WII_SECTOR_SIZE)
|
|
|
|
return false;
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_blocks_per_disc =
|
|
|
|
(WII_SECTOR_COUNT * WII_SECTOR_SIZE + m_wbfs_sector_size - 1) / m_wbfs_sector_size;
|
|
|
|
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
|
|
|
|
2016-06-24 08:43:46 +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)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
while (nbytes)
|
|
|
|
{
|
|
|
|
u64 read_size;
|
|
|
|
File::IOFile& data_file = SeekToCluster(offset, &read_size);
|
|
|
|
if (read_size == 0)
|
|
|
|
return false;
|
|
|
|
read_size = std::min(read_size, nbytes);
|
|
|
|
|
|
|
|
if (!data_file.ReadBytes(out_ptr, read_size))
|
|
|
|
{
|
|
|
|
data_file.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_ptr += read_size;
|
|
|
|
nbytes -= read_size;
|
|
|
|
offset += read_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
File::IOFile& WbfsFileReader::SeekToCluster(u64 offset, u64* available)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u64 base_cluster = (offset >> m_header.wbfs_sector_shift);
|
|
|
|
if (base_cluster < m_blocks_per_disc)
|
|
|
|
{
|
|
|
|
u64 cluster_address = m_wbfs_sector_size * m_wlba_table[base_cluster];
|
|
|
|
u64 cluster_offset = offset & (m_wbfs_sector_size - 1);
|
|
|
|
u64 final_address = cluster_address + cluster_offset;
|
|
|
|
|
|
|
|
for (u32 i = 0; i != m_total_files; i++)
|
|
|
|
{
|
|
|
|
if (final_address < (m_files[i]->base_address + m_files[i]->size))
|
|
|
|
{
|
|
|
|
m_files[i]->file.Seek(final_address - m_files[i]->base_address, SEEK_SET);
|
|
|
|
if (available)
|
|
|
|
{
|
|
|
|
u64 till_end_of_file = m_files[i]->size - (final_address - m_files[i]->base_address);
|
|
|
|
u64 till_end_of_sector = m_wbfs_sector_size - cluster_offset;
|
|
|
|
*available = std::min(till_end_of_file, till_end_of_sector);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_files[i]->file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PanicAlert("Read beyond end of disc");
|
|
|
|
if (available)
|
|
|
|
*available = 0;
|
|
|
|
m_files[0]->file.Seek(0, SEEK_SET);
|
|
|
|
return m_files[0]->file;
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
2015-12-07 04:15:51 +00:00
|
|
|
std::unique_ptr<WbfsFileReader> WbfsFileReader::Create(const std::string& filename)
|
2013-03-19 13:59:41 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
auto reader = std::unique_ptr<WbfsFileReader>(new WbfsFileReader(filename));
|
2013-03-19 13:59:41 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!reader->IsGood())
|
|
|
|
reader.reset();
|
2015-12-07 04:15:51 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return reader;
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
bool IsWbfsBlob(const std::string& filename)
|
2012-05-03 14:09:01 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
File::IOFile f(filename, "rb");
|
2012-05-03 14:09:01 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
u8 magic[4] = {0, 0, 0, 0};
|
|
|
|
f.ReadBytes(&magic, 4);
|
2012-05-03 14:09:01 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return (magic[0] == 'W') && (magic[1] == 'B') && (magic[2] == 'F') && (magic[3] == 'S');
|
2012-05-03 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|