From 7885e63deb7310d59701639d02e6c400fa91ccb7 Mon Sep 17 00:00:00 2001 From: skidau Date: Fri, 4 May 2012 00:09:01 +1000 Subject: [PATCH] Added wbfs support. Thanks to lordlyhobo for the patch. Fixes issue 859. --- Source/Core/Core/Src/CoreParameter.cpp | 1 + Source/Core/DiscIO/CMakeLists.txt | 1 + Source/Core/DiscIO/DiscIO.vcxproj | 2 + Source/Core/DiscIO/DiscIO.vcxproj.filters | 6 + Source/Core/DiscIO/Src/Blob.cpp | 4 + Source/Core/DiscIO/Src/WbfsBlob.cpp | 209 +++++++++++++++++++++ Source/Core/DiscIO/Src/WbfsBlob.h | 82 ++++++++ Source/Core/DolphinWX/Src/GameListCtrl.cpp | 1 + 8 files changed, 306 insertions(+) create mode 100644 Source/Core/DiscIO/Src/WbfsBlob.cpp create mode 100644 Source/Core/DiscIO/Src/WbfsBlob.h diff --git a/Source/Core/Core/Src/CoreParameter.cpp b/Source/Core/Core/Src/CoreParameter.cpp index d85f8e1ea2..ba72d47ffb 100644 --- a/Source/Core/Core/Src/CoreParameter.cpp +++ b/Source/Core/Core/Src/CoreParameter.cpp @@ -126,6 +126,7 @@ bool SCoreStartupParameter::AutoSetup(EBootBS2 _BootBS2) SplitPath(m_strFilename, NULL, NULL, &Extension); if (!strcasecmp(Extension.c_str(), ".gcm") || !strcasecmp(Extension.c_str(), ".iso") || + !strcasecmp(Extension.c_str(), ".wbfs") || !strcasecmp(Extension.c_str(), ".ciso") || !strcasecmp(Extension.c_str(), ".gcz") || bootDrive) diff --git a/Source/Core/DiscIO/CMakeLists.txt b/Source/Core/DiscIO/CMakeLists.txt index 7f6ff7e4cc..01e15a374f 100644 --- a/Source/Core/DiscIO/CMakeLists.txt +++ b/Source/Core/DiscIO/CMakeLists.txt @@ -3,6 +3,7 @@ set(SRCS Src/BannerLoader.cpp Src/BannerLoaderWii.cpp Src/Blob.cpp Src/CISOBlob.cpp + Src/WbfsBlob.cpp Src/CompressedBlob.cpp Src/DiscScrubber.cpp Src/DriveBlob.cpp diff --git a/Source/Core/DiscIO/DiscIO.vcxproj b/Source/Core/DiscIO/DiscIO.vcxproj index 417b28fa0a..6dd24ddd44 100644 --- a/Source/Core/DiscIO/DiscIO.vcxproj +++ b/Source/Core/DiscIO/DiscIO.vcxproj @@ -175,6 +175,7 @@ + @@ -199,6 +200,7 @@ + diff --git a/Source/Core/DiscIO/DiscIO.vcxproj.filters b/Source/Core/DiscIO/DiscIO.vcxproj.filters index a16eb0625d..105d3ffc47 100644 --- a/Source/Core/DiscIO/DiscIO.vcxproj.filters +++ b/Source/Core/DiscIO/DiscIO.vcxproj.filters @@ -8,6 +8,9 @@ Volume\Blob + + Volume\Blob + Volume\Blob @@ -71,6 +74,9 @@ Volume\Blob + + Volume\Blob + Volume\Blob diff --git a/Source/Core/DiscIO/Src/Blob.cpp b/Source/Core/DiscIO/Src/Blob.cpp index a728780f23..115145e290 100644 --- a/Source/Core/DiscIO/Src/Blob.cpp +++ b/Source/Core/DiscIO/Src/Blob.cpp @@ -23,6 +23,7 @@ #include "FileBlob.h" #include "CISOBlob.h" #include "DriveBlob.h" +#include "WbfsBlob.h" namespace DiscIO { @@ -128,6 +129,9 @@ IBlobReader* CreateBlobReader(const char* filename) if (!File::Exists(filename)) return 0; + if (IsWbfsBlob(filename)) + return WbfsFileReader::Create(filename); + if (IsCompressedBlob(filename)) return CompressedBlobReader::Create(filename); diff --git a/Source/Core/DiscIO/Src/WbfsBlob.cpp b/Source/Core/DiscIO/Src/WbfsBlob.cpp new file mode 100644 index 0000000000..1b7696cc58 --- /dev/null +++ b/Source/Core/DiscIO/Src/WbfsBlob.cpp @@ -0,0 +1,209 @@ +// Copyright (C) 2003 Dolphin Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official SVN repository and contact information can be found at +// http://code.google.com/p/dolphin-emu/ + +#include "WbfsBlob.h" +#include "FileUtil.h" + +namespace DiscIO +{ +const u64 wii_sector_size = 0x8000; +const u64 wii_sector_count = 143432 * 2; +const u64 wii_sector_log2 = 15; +const u64 wii_disc_header_size = 256; + +static inline u64 align(u64 value, u64 bounds) +{ + return (value + (bounds - 1)) & (~(bounds - 1)); +} + +WbfsFileReader::WbfsFileReader(const char* filename) + : m_total_files(0), m_size(0), m_wlba_table(0), m_good(true) +{ + if(!filename || (strlen(filename) < 4) || !OpenFiles(filename) || !ReadHeader()) + { + m_good = false; + return; + } + + // Grab disc info (assume slot 0, checked in ReadHeader()) + m_wlba_table = new u16[m_blocks_per_disc]; + m_files[0]->file.Seek(hd_sector_size + wii_disc_header_size /*+ i * m_disc_info_size*/, SEEK_SET); + m_files[0]->file.ReadBytes(m_wlba_table, m_blocks_per_disc * sizeof(u16)); +} + +WbfsFileReader::~WbfsFileReader() +{ + for(u32 i = 0; i != m_files.size(); ++ i) + { + delete m_files[i]; + } + + delete[] m_wlba_table; +} + +bool WbfsFileReader::OpenFiles(const char* filename) +{ + m_total_files = 0; + + while(true) + { + file_entry* new_entry = new file_entry; + + // Replace last character with index (e.g. wbfs = wbf1) + std::string path = filename; + if(0 != m_total_files) + { + path[path.length() - 1] = '0' + m_total_files; + } + + if(!new_entry->file.Open(path, "rb")) + { + delete new_entry; + return 0 != m_total_files; + } + + new_entry->base_address = m_size; + new_entry->size = new_entry->file.GetSize(); + m_size += new_entry->size; + + m_total_files ++; + m_files.push_back(new_entry); + } +} + +bool WbfsFileReader::ReadHeader() +{ + m_files[0]->file.Seek(4, SEEK_SET); + + // Read hd size info + m_files[0]->file.ReadBytes(&hd_sector_count, 4); + hd_sector_count = Common::swap32(hd_sector_count); + + m_files[0]->file.ReadBytes(&hd_sector_shift, 1); + hd_sector_size = 1 << hd_sector_shift; + + if(m_size != hd_sector_count * hd_sector_size) + { + //printf("File size doesn't match expected size\n"); + return false; + } + + // Read wbfs cluster info + m_files[0]->file.ReadBytes(&wbfs_sector_shift, 1); + wbfs_sector_size = 1 << wbfs_sector_shift; + wbfs_sector_count = m_size / wbfs_sector_size; + + if(wbfs_sector_size < wii_sector_size) + { + //Setting this too low would case a very large memory allocation + return false; + } + + m_blocks_per_disc = (wii_sector_count * wii_sector_size) / wbfs_sector_size; + m_disc_info_size = align(wii_disc_header_size + m_blocks_per_disc * 2, hd_sector_size); + + // Read disc table + m_files[0]->file.Seek(2, SEEK_CUR); + m_files[0]->file.ReadBytes(disc_table, 500); + + if(0 == disc_table[0]) + { + //printf("Game must be in 'slot 0'\n"); + return false; + } + + return true; +} + +bool WbfsFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) +{ + while(nbytes) + { + u64 read_size; + File::IOFile& data_file = SeekToCluster(offset, &read_size); + read_size = (read_size > nbytes) ? nbytes : read_size; + + data_file.ReadBytes(out_ptr, read_size); + + out_ptr += read_size; + nbytes -= read_size; + offset += read_size; + } + + return true; +} + +File::IOFile& WbfsFileReader::SeekToCluster(u64 offset, u64* available) +{ + u64 base_cluster = offset >> wbfs_sector_shift; + if(base_cluster < m_blocks_per_disc) + { + u64 cluster_address = wbfs_sector_size * Common::swap16(m_wlba_table[base_cluster]); + u64 cluster_offset = offset & (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 = 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"); + m_files[0]->file.Seek(0, SEEK_SET); + return m_files[0]->file; +} + +WbfsFileReader* WbfsFileReader::Create(const char* filename) +{ + WbfsFileReader* reader = new WbfsFileReader(filename); + + if(reader->IsGood()) + { + return reader; + } + else + { + delete reader; + return NULL; + } +} + +bool IsWbfsBlob(const char* filename) +{ + File::IOFile f(filename, "rb"); + + u8 magic[4] = {0, 0, 0, 0}; + f.ReadBytes(&magic, 4); + + return (magic[0] == 'W') && + (magic[1] == 'B') && + (magic[2] == 'F') && + (magic[3] == 'S'); +} + +} // namespace diff --git a/Source/Core/DiscIO/Src/WbfsBlob.h b/Source/Core/DiscIO/Src/WbfsBlob.h new file mode 100644 index 0000000000..78b3ae8b78 --- /dev/null +++ b/Source/Core/DiscIO/Src/WbfsBlob.h @@ -0,0 +1,82 @@ +// Copyright (C) 2003 Dolphin Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official SVN repository and contact information can be found at +// http://code.google.com/p/dolphin-emu/ + +#ifndef _WBFS_BLOB_H +#define _WBFS_BLOB_H + +#include "Blob.h" +#include "FileUtil.h" + +namespace DiscIO +{ + +struct wbfs_head_t; + +class WbfsFileReader : public IBlobReader +{ + WbfsFileReader(const char* filename); + ~WbfsFileReader(); + + bool OpenFiles(const char* filename); + bool ReadHeader(); + + File::IOFile& SeekToCluster(u64 offset, u64* available); + bool IsGood() {return m_good;} + + + struct file_entry + { + File::IOFile file; + u64 base_address; + u64 size; + }; + + std::vector m_files; + + u32 m_total_files; + u64 m_size; + + u64 hd_sector_size; + u8 hd_sector_shift; + u32 hd_sector_count; + + u64 wbfs_sector_size; + u64 wbfs_sector_shift; + u64 wbfs_sector_count; + u64 m_disc_info_size; + + u8 disc_table[500]; + + u16* m_wlba_table; + u64 m_blocks_per_disc; + + bool m_good; + +public: + static WbfsFileReader* Create(const char* filename); + + u64 GetDataSize() const { return m_size; } + u64 GetRawSize() const { return m_size; } + bool Read(u64 offset, u64 nbytes, u8* out_ptr); +}; + +bool IsWbfsBlob(const char* filename); + + +} // namespace + +#endif // _FILE_BLOB_H diff --git a/Source/Core/DolphinWX/Src/GameListCtrl.cpp b/Source/Core/DolphinWX/Src/GameListCtrl.cpp index 1f8a5d9350..4a7d7d5dce 100644 --- a/Source/Core/DolphinWX/Src/GameListCtrl.cpp +++ b/Source/Core/DolphinWX/Src/GameListCtrl.cpp @@ -553,6 +553,7 @@ void CGameListCtrl::ScanForISOs() Extensions.push_back("*.iso"); Extensions.push_back("*.ciso"); Extensions.push_back("*.gcz"); + Extensions.push_back("*.wbfs"); } if (SConfig::GetInstance().m_ListWad) Extensions.push_back("*.wad");