| 1 | // Copyright 2012 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/WbfsBlob.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstdio> |
| 8 | #include <cstring> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "Common/Align.h" |
| 15 | #include "Common/Assert.h" |
| 16 | #include "Common/BitUtils.h" |
| 17 | #include "Common/CommonTypes.h" |
| 18 | #include "Common/Logging/Log.h" |
| 19 | #include "Common/Swap.h" |
| 20 | |
| 21 | namespace DiscIO |
| 22 | { |
| 23 | static constexpr u64 WII_SECTOR_SIZE = 0x8000; |
| 24 | static constexpr u64 WII_SECTOR_COUNT = 143432 * 2; |
| 25 | static constexpr u64 WII_DISC_HEADER_SIZE = 256; |
| 26 | |
| 27 | WbfsFileReader::WbfsFileReader(File::DirectIOFile file, const std::string& path) |
| 28 | : m_size(0), m_good(false) |
| 29 | { |
| 30 | if (!AddFileToList(std::move(file))) |
| 31 | return; |
| 32 | if (!path.empty()) |
| 33 | OpenAdditionalFiles(path); |
| 34 | if (!ReadHeader()) |
| 35 | return; |
| 36 | m_good = true; |
| 37 | |
| 38 | // Grab disc info (assume slot 0, checked in ReadHeader()) |
| 39 | m_wlba_table.resize(m_blocks_per_disc); |
| 40 | m_files[0].file.Seek(m_hd_sector_size + WII_DISC_HEADER_SIZE /*+ i * m_disc_info_size*/, |
| 41 | File::SeekOrigin::Begin); |
| 42 | m_files[0].file.Read(Common::AsWritableU8Span(m_wlba_table)); |
| 43 | for (size_t i = 0; i < m_blocks_per_disc; i++) |
| 44 | m_wlba_table[i] = Common::swap16(m_wlba_table[i]); |
| 45 | } |
| 46 | |
| 47 | WbfsFileReader::~WbfsFileReader() = default; |
| 48 | |
| 49 | std::unique_ptr<BlobReader> WbfsFileReader::CopyReader() const |
| 50 | { |
| 51 | auto retval = std::unique_ptr<WbfsFileReader>(new WbfsFileReader(m_files[0].file)); |
| 52 | for (size_t ix = 1; ix < m_files.size(); ix++) |
| 53 | retval->AddFileToList(m_files[ix].file); |
| 54 | return retval; |
| 55 | } |
| 56 | |
| 57 | u64 WbfsFileReader::GetDataSize() const |
| 58 | { |
| 59 | return WII_SECTOR_COUNT * WII_SECTOR_SIZE; |
| 60 | } |
| 61 | |
| 62 | void WbfsFileReader::OpenAdditionalFiles(const std::string& path) |
| 63 | { |
| 64 | if (path.length() < 4) |
| 65 | return; |
| 66 | |
| 67 | ASSERT(!m_files.empty()); // The code below gives .wbf0 for index 0, but it should be .wbfs |
| 68 | |
| 69 | while (true) |
| 70 | { |
| 71 | // Replace last character with index (e.g. wbfs = wbf1) |
| 72 | if (m_files.size() >= 10) |
| 73 | return; |
| 74 | std::string current_path = path; |
| 75 | current_path.back() = static_cast<char>('0' + m_files.size()); |
| 76 | if (!AddFileToList(File::DirectIOFile(current_path, File::AccessMode::Read))) |
| 77 | return; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool WbfsFileReader::AddFileToList(File::DirectIOFile file) |
| 82 | { |
| 83 | if (!file.IsOpen()) |
| 84 | return false; |
| 85 | |
| 86 | const u64 file_size = file.GetSize(); |
| 87 | m_files.emplace_back(std::move(file), m_size, file_size); |
| 88 | m_size += file_size; |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool WbfsFileReader::ReadHeader() |
| 94 | { |
| 95 | // Read hd size info |
| 96 | m_files[0].file.Seek(0, File::SeekOrigin::Begin); |
| 97 | m_files[0].file.Read(Common::AsWritableU8Span(m_header)); |
| 98 | if (m_header.magic != WBFS_MAGIC) |
| 99 | return false; |
| 100 | |
| 101 | m_header.hd_sector_count = Common::swap32(m_header.hd_sector_count); |
| 102 | m_hd_sector_size = 1ull << m_header.hd_sector_shift; |
| 103 | |
| 104 | if (m_size != (m_header.hd_sector_count * m_hd_sector_size)) |
| 105 | return false; |
| 106 | |
| 107 | // Read wbfs cluster info |
| 108 | m_wbfs_sector_size = 1ull << m_header.wbfs_sector_shift; |
| 109 | m_wbfs_sector_count = m_size / m_wbfs_sector_size; |
| 110 | |
| 111 | if (m_wbfs_sector_size < WII_SECTOR_SIZE) |
| 112 | return false; |
| 113 | |
| 114 | m_blocks_per_disc = |
| 115 | (WII_SECTOR_COUNT * WII_SECTOR_SIZE + m_wbfs_sector_size - 1) / m_wbfs_sector_size; |
| 116 | m_disc_info_size = |
| 117 | Common::AlignUp(WII_DISC_HEADER_SIZE + m_blocks_per_disc * sizeof(u16), m_hd_sector_size); |
| 118 | |
| 119 | return m_header.disc_table[0] != 0; |
| 120 | } |
| 121 | |
| 122 | bool WbfsFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) |
| 123 | { |
| 124 | if (offset + nbytes > GetDataSize()) |
| 125 | return false; |
| 126 | |
| 127 | while (nbytes) |
| 128 | { |
| 129 | u64 read_size; |
| 130 | auto& data_file = SeekToCluster(offset, &read_size); |
| 131 | if (read_size == 0) |
| 132 | return false; |
| 133 | read_size = std::min(read_size, nbytes); |
| 134 | |
| 135 | if (!data_file.Read(out_ptr, read_size)) |
| 136 | { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | out_ptr += read_size; |
| 141 | nbytes -= read_size; |
| 142 | offset += read_size; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | File::DirectIOFile& WbfsFileReader::SeekToCluster(u64 offset, u64* available) |
| 149 | { |
| 150 | u64 base_cluster = (offset >> m_header.wbfs_sector_shift); |
| 151 | if (base_cluster < m_blocks_per_disc) |
| 152 | { |
| 153 | u64 cluster_address = m_wbfs_sector_size * m_wlba_table[base_cluster]; |
| 154 | u64 cluster_offset = offset & (m_wbfs_sector_size - 1); |
| 155 | u64 final_address = cluster_address + cluster_offset; |
| 156 | |
| 157 | for (FileEntry& file_entry : m_files) |
| 158 | { |
| 159 | if (final_address < (file_entry.base_address + file_entry.size)) |
| 160 | { |
| 161 | file_entry.file.Seek(final_address - file_entry.base_address, File::SeekOrigin::Begin); |
| 162 | if (available) |
| 163 | { |
| 164 | u64 till_end_of_file = file_entry.size - (final_address - file_entry.base_address); |
| 165 | u64 till_end_of_sector = m_wbfs_sector_size - cluster_offset; |
| 166 | *available = std::min(till_end_of_file, till_end_of_sector); |
| 167 | } |
| 168 | |
| 169 | return file_entry.file; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | ERROR_LOG_FMT(DISCIO, "Read beyond end of disc"); |
| 175 | if (available) |
| 176 | *available = 0; |
| 177 | m_files[0].file.Seek(0, File::SeekOrigin::Begin); |
| 178 | return m_files[0].file; |
| 179 | } |
| 180 | |
| 181 | std::unique_ptr<WbfsFileReader> WbfsFileReader::Create(File::DirectIOFile file, |
| 182 | const std::string& path) |
| 183 | { |
| 184 | auto reader = std::unique_ptr<WbfsFileReader>(new WbfsFileReader(std::move(file), path)); |
| 185 | |
| 186 | if (!reader->IsGood()) |
| 187 | reader.reset(); |
| 188 | |
| 189 | return reader; |
| 190 | } |
| 191 | |
| 192 | } // namespace DiscIO |