| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/FileSystemGCWii.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstddef> |
| 8 | #include <cstring> |
| 9 | #include <map> |
| 10 | #include <memory> |
| 11 | #include <optional> |
| 12 | #include <string> |
| 13 | #include <string_view> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "Common/CommonTypes.h" |
| 17 | #include "Common/Logging/Log.h" |
| 18 | #include "Common/StringUtil.h" |
| 19 | #include "DiscIO/DiscUtils.h" |
| 20 | #include "DiscIO/Filesystem.h" |
| 21 | #include "DiscIO/VolumeDisc.h" |
| 22 | |
| 23 | namespace DiscIO |
| 24 | { |
| 25 | constexpr u32 FST_ENTRY_SIZE = 4 * 3; // An FST entry consists of three 32-bit integers |
| 26 | |
| 27 | // Set everything manually. |
| 28 | FileInfoGCWii::FileInfoGCWii(const u8* fst, u8 offset_shift, u32 index, u32 total_file_infos) |
| 29 | : m_fst(fst), m_offset_shift(offset_shift), m_index(index), m_total_file_infos(total_file_infos) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | // For the root object only. |
| 34 | // m_fst and m_index must be correctly set before GetSize() is called! |
| 35 | FileInfoGCWii::FileInfoGCWii(const u8* fst, u8 offset_shift) |
| 36 | : m_fst(fst), m_offset_shift(offset_shift), m_index(0), m_total_file_infos(GetSize()) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | // Copy data that is common to the whole file system. |
| 41 | FileInfoGCWii::FileInfoGCWii(const FileInfoGCWii& file_info, u32 index) |
| 42 | : FileInfoGCWii(file_info.m_fst, file_info.m_offset_shift, index, file_info.m_total_file_infos) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | FileInfoGCWii::~FileInfoGCWii() = default; |
| 47 | |
| 48 | uintptr_t FileInfoGCWii::GetAddress() const |
| 49 | { |
| 50 | return reinterpret_cast<uintptr_t>(m_fst + FST_ENTRY_SIZE * m_index); |
| 51 | } |
| 52 | |
| 53 | u32 FileInfoGCWii::GetNextIndex() const |
| 54 | { |
| 55 | return IsDirectory() ? GetSize() : m_index + 1; |
| 56 | } |
| 57 | |
| 58 | FileInfo& FileInfoGCWii::operator++() |
| 59 | { |
| 60 | m_index = GetNextIndex(); |
| 61 | return *this; |
| 62 | } |
| 63 | |
| 64 | std::unique_ptr<FileInfo> FileInfoGCWii::clone() const |
| 65 | { |
| 66 | return std::make_unique<FileInfoGCWii>(*this); |
| 67 | } |
| 68 | |
| 69 | FileInfo::const_iterator FileInfoGCWii::begin() const |
| 70 | { |
| 71 | return const_iterator(std::make_unique<FileInfoGCWii>(*this, m_index + 1)); |
| 72 | } |
| 73 | |
| 74 | FileInfo::const_iterator FileInfoGCWii::end() const |
| 75 | { |
| 76 | return const_iterator(std::make_unique<FileInfoGCWii>(*this, GetNextIndex())); |
| 77 | } |
| 78 | |
| 79 | u32 FileInfoGCWii::Get(EntryProperty entry_property) const |
| 80 | { |
| 81 | return Common::swap32(m_fst + FST_ENTRY_SIZE * m_index + |
| 82 | sizeof(u32) * static_cast<int>(entry_property)); |
| 83 | } |
| 84 | |
| 85 | u32 FileInfoGCWii::GetSize() const |
| 86 | { |
| 87 | return Get(EntryProperty::FILE_SIZE); |
| 88 | } |
| 89 | |
| 90 | u64 FileInfoGCWii::GetOffset() const |
| 91 | { |
| 92 | return static_cast<u64>(Get(EntryProperty::FILE_OFFSET)) << m_offset_shift; |
| 93 | } |
| 94 | |
| 95 | bool FileInfoGCWii::IsRoot() const |
| 96 | { |
| 97 | return m_index == 0; |
| 98 | } |
| 99 | |
| 100 | bool FileInfoGCWii::IsDirectory() const |
| 101 | { |
| 102 | return (Get(EntryProperty::NAME_OFFSET) & 0xFF000000) != 0; |
| 103 | } |
| 104 | |
| 105 | u32 FileInfoGCWii::GetTotalChildren() const |
| 106 | { |
| 107 | return Get(EntryProperty::FILE_SIZE) - (m_index + 1); |
| 108 | } |
| 109 | |
| 110 | u64 FileInfoGCWii::GetNameOffset() const |
| 111 | { |
| 112 | return static_cast<u64>(FST_ENTRY_SIZE) * m_total_file_infos + |
| 113 | (Get(EntryProperty::NAME_OFFSET) & 0xFFFFFF); |
| 114 | } |
| 115 | |
| 116 | std::string FileInfoGCWii::GetName() const |
| 117 | { |
| 118 | // TODO: Should we really always use SHIFT-JIS? |
| 119 | // Some names in Pikmin (NTSC-U) don't make sense without it, but is it correct? |
| 120 | return SHIFTJISToUTF8(reinterpret_cast<const char*>(m_fst + GetNameOffset())); |
| 121 | } |
| 122 | |
| 123 | bool FileInfoGCWii::NameCaseInsensitiveEquals(std::string_view other) const |
| 124 | { |
| 125 | // For speed, this function avoids allocating new strings, except when we are comparing |
| 126 | // non-ASCII characters with non-ASCII characters, which is a rare case. |
| 127 | |
| 128 | const char* this_ptr = reinterpret_cast<const char*>(m_fst + GetNameOffset()); |
| 129 | const char* other_ptr = other.data(); |
| 130 | |
| 131 | for (size_t i = 0; i < other.size(); ++i, ++this_ptr, ++other_ptr) |
| 132 | { |
| 133 | if (*this_ptr == '\0') |
| 134 | { |
| 135 | // A null byte in this is always a terminator and a null byte in other is never a terminator, |
| 136 | // so if we reach this case, this is shorter than other |
| 137 | return false; |
| 138 | } |
| 139 | else if (static_cast<unsigned char>(*this_ptr) >= 0x80 && |
| 140 | static_cast<unsigned char>(*other_ptr) >= 0x80) |
| 141 | { |
| 142 | // other is in UTF-8 and this is in Shift-JIS, so we convert so that we can compare correctly |
| 143 | const std::string this_utf8 = SHIFTJISToUTF8(this_ptr); |
| 144 | return std::equal(this_utf8.cbegin(), this_utf8.cend(), other.cbegin() + i, other.cend(), |
| 145 | [](char a, char b) { return Common::ToLower(a) == Common::ToLower(b); }); |
| 146 | } |
| 147 | else if (Common::ToLower(*this_ptr) != Common::ToLower(*other_ptr)) |
| 148 | { |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return *this_ptr == '\0'; // If we're not at a null byte, this is longer than other |
| 154 | } |
| 155 | |
| 156 | std::string FileInfoGCWii::GetPath() const |
| 157 | { |
| 158 | // The root entry doesn't have a name |
| 159 | if (m_index == 0) |
| 160 | return ""; |
| 161 | |
| 162 | if (IsDirectory()) |
| 163 | { |
| 164 | u32 parent_directory_index = Get(EntryProperty::FILE_OFFSET); |
| 165 | return FileInfoGCWii(*this, parent_directory_index).GetPath() + GetName() + "/"; |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | // The parent directory can be found by searching backwards |
| 170 | // for a directory that contains this file. The search cannot fail, |
| 171 | // because the root directory at index 0 contains all files. |
| 172 | FileInfoGCWii potential_parent(*this, m_index - 1); |
| 173 | while (!(potential_parent.IsDirectory() && |
| 174 | potential_parent.Get(EntryProperty::FILE_SIZE) > m_index)) |
| 175 | { |
| 176 | potential_parent = FileInfoGCWii(*this, potential_parent.m_index - 1); |
| 177 | } |
| 178 | return potential_parent.GetPath() + GetName(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | bool FileInfoGCWii::IsValid(u64 fst_size, const FileInfoGCWii& parent_directory) const |
| 183 | { |
| 184 | if (GetNameOffset() >= fst_size) |
| 185 | { |
| 186 | ERROR_LOG_FMT(DISCIO, "Impossibly large name offset in file system"); |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if (IsDirectory()) |
| 191 | { |
| 192 | if (Get(EntryProperty::FILE_OFFSET) != parent_directory.m_index) |
| 193 | { |
| 194 | ERROR_LOG_FMT(DISCIO, "Incorrect parent offset in file system"); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | const u32 size = Get(EntryProperty::FILE_SIZE); |
| 199 | |
| 200 | if (size <= m_index) |
| 201 | { |
| 202 | ERROR_LOG_FMT(DISCIO, "Impossibly small directory size in file system"); |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | if (size > parent_directory.Get(EntryProperty::FILE_SIZE)) |
| 207 | { |
| 208 | ERROR_LOG_FMT(DISCIO, "Impossibly large directory size in file system"); |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | for (const FileInfo& child : *this) |
| 213 | { |
| 214 | if (!static_cast<const FileInfoGCWii&>(child).IsValid(fst_size, *this)) |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | FileSystemGCWii::FileSystemGCWii(const VolumeDisc* volume, const Partition& partition) |
| 223 | : m_valid(false), m_root(nullptr, 0, 0, 0) |
| 224 | { |
| 225 | u8 offset_shift; |
| 226 | // Check if this is a GameCube or Wii disc |
| 227 | if (volume->ReadSwapped<u32>(0x18, partition) == WII_DISC_MAGIC) |
| 228 | offset_shift = 2; // Wii file system |
| 229 | else if (volume->ReadSwapped<u32>(0x1c, partition) == GAMECUBE_DISC_MAGIC) |
| 230 | offset_shift = 0; // GameCube file system |
| 231 | else |
| 232 | return; // Invalid partition (maybe someone removed its data but not its partition table entry) |
| 233 | |
| 234 | const std::optional<u64> fst_offset = GetFSTOffset(*volume, partition); |
| 235 | const std::optional<u64> fst_size = GetFSTSize(*volume, partition); |
| 236 | if (!fst_offset || !fst_size) |
| 237 | return; |
| 238 | if (*fst_size < FST_ENTRY_SIZE) |
| 239 | { |
| 240 | ERROR_LOG_FMT(DISCIO, "File system is too small"); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | // 128 MiB is more than the total amount of RAM in a Wii. |
| 245 | // No file system should use anywhere near that much. |
| 246 | static constexpr u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 128 * 1024 * 1024; |
| 247 | if (*fst_size > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT) |
| 248 | { |
| 249 | // Without this check, Dolphin can crash by trying to allocate too much |
| 250 | // memory when loading a disc image with an incorrect FST size. |
| 251 | |
| 252 | ERROR_LOG_FMT(DISCIO, "File system is abnormally large! Aborting loading"); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | // Read the whole FST |
| 257 | m_file_system_table.resize(*fst_size); |
| 258 | if (!volume->Read(*fst_offset, *fst_size, m_file_system_table.data(), partition)) |
| 259 | { |
| 260 | ERROR_LOG_FMT(DISCIO, "Couldn't read file system table"); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | // Create the root object |
| 265 | m_root = FileInfoGCWii(m_file_system_table.data(), offset_shift); |
| 266 | if (!m_root.IsDirectory()) |
| 267 | { |
| 268 | ERROR_LOG_FMT(DISCIO, "File system root is not a directory"); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | if (FST_ENTRY_SIZE * m_root.GetSize() > *fst_size) |
| 273 | { |
| 274 | ERROR_LOG_FMT(DISCIO, "File system has too many entries for its size"); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | // If the FST's final byte isn't 0, FileInfoGCWii::GetName() can read past the end |
| 279 | if (m_file_system_table[*fst_size - 1] != 0) |
| 280 | { |
| 281 | ERROR_LOG_FMT(DISCIO, "File system does not end with a null byte"); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | m_valid = m_root.IsValid(*fst_size, m_root); |
| 286 | } |
| 287 | |
| 288 | FileSystemGCWii::~FileSystemGCWii() = default; |
| 289 | |
| 290 | const FileInfo& FileSystemGCWii::GetRoot() const |
| 291 | { |
| 292 | return m_root; |
| 293 | } |
| 294 | |
| 295 | std::unique_ptr<FileInfo> FileSystemGCWii::FindFileInfo(std::string_view path) const |
| 296 | { |
| 297 | if (!IsValid()) |
| 298 | return nullptr; |
| 299 | |
| 300 | return FindFileInfo(path, m_root); |
| 301 | } |
| 302 | |
| 303 | std::unique_ptr<FileInfo> FileSystemGCWii::FindFileInfo(std::string_view path, |
| 304 | const FileInfo& file_info) const |
| 305 | { |
| 306 | // Given a path like "directory1/directory2/fileA.bin", this function will |
| 307 | // find directory1 and then call itself to search for "directory2/fileA.bin". |
| 308 | |
| 309 | const size_t name_start = path.find_first_not_of('/'); |
| 310 | if (name_start == std::string::npos) |
| 311 | return file_info.clone(); // We're done |
| 312 | |
| 313 | const size_t name_end = path.find('/', name_start); |
| 314 | const std::string_view name = path.substr(name_start, name_end - name_start); |
| 315 | const std::string_view rest_of_path = |
| 316 | (name_end != std::string::npos) ? path.substr(name_end + 1) : ""; |
| 317 | |
| 318 | for (const FileInfo& child : file_info) |
| 319 | { |
| 320 | // We need case insensitive comparison since some games have OPENING.BNR instead of opening.bnr |
| 321 | if (child.NameCaseInsensitiveEquals(name)) |
| 322 | { |
| 323 | // A match is found. The rest of the path is passed on to finish the search. |
| 324 | std::unique_ptr<FileInfo> result = FindFileInfo(rest_of_path, child); |
| 325 | |
| 326 | // If the search wasn't successful, the loop continues, just in case there's a second |
| 327 | // file info that matches searching_for (which probably won't happen in practice) |
| 328 | if (result) |
| 329 | return result; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return nullptr; |
| 334 | } |
| 335 | |
| 336 | std::unique_ptr<FileInfo> FileSystemGCWii::FindFileInfo(u64 disc_offset) const |
| 337 | { |
| 338 | if (!IsValid()) |
| 339 | return nullptr; |
| 340 | |
| 341 | // Build a cache (unless there already is one) |
| 342 | if (m_offset_file_info_cache.empty()) |
| 343 | { |
| 344 | u32 fst_entries = m_root.GetSize(); |
| 345 | for (u32 i = 0; i < fst_entries; i++) |
| 346 | { |
| 347 | FileInfoGCWii file_info(m_root, i); |
| 348 | if (!file_info.IsDirectory()) |
| 349 | { |
| 350 | const u32 size = file_info.GetSize(); |
| 351 | if (size != 0) |
| 352 | m_offset_file_info_cache.emplace(file_info.GetOffset() + size, i); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Get the first file that ends after disc_offset |
| 358 | const auto it = m_offset_file_info_cache.upper_bound(disc_offset); |
| 359 | if (it == m_offset_file_info_cache.end()) |
| 360 | return nullptr; |
| 361 | std::unique_ptr<FileInfo> result(std::make_unique<FileInfoGCWii>(m_root, it->second)); |
| 362 | |
| 363 | // If the file's start isn't after disc_offset, success |
| 364 | if (result->GetOffset() <= disc_offset) |
| 365 | return result; |
| 366 | |
| 367 | return nullptr; |
| 368 | } |
| 369 | |
| 370 | } // namespace DiscIO |