fsmitm_romfsbuild.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <cstring>
  4. #include <string_view>
  5. #include "common/alignment.h"
  6. #include "common/assert.h"
  7. #include "core/file_sys/fsmitm_romfsbuild.h"
  8. #include "core/file_sys/ips_layer.h"
  9. #include "core/file_sys/vfs.h"
  10. #include "core/file_sys/vfs_vector.h"
  11. namespace FileSys {
  12. constexpr u64 FS_MAX_PATH = 0x301;
  13. constexpr u32 ROMFS_ENTRY_EMPTY = 0xFFFFFFFF;
  14. constexpr u32 ROMFS_FILEPARTITION_OFS = 0x200;
  15. // Types for building a RomFS.
  16. struct RomFSHeader {
  17. u64 header_size;
  18. u64 dir_hash_table_ofs;
  19. u64 dir_hash_table_size;
  20. u64 dir_table_ofs;
  21. u64 dir_table_size;
  22. u64 file_hash_table_ofs;
  23. u64 file_hash_table_size;
  24. u64 file_table_ofs;
  25. u64 file_table_size;
  26. u64 file_partition_ofs;
  27. };
  28. static_assert(sizeof(RomFSHeader) == 0x50, "RomFSHeader has incorrect size.");
  29. struct RomFSDirectoryEntry {
  30. u32 parent;
  31. u32 sibling;
  32. u32 child;
  33. u32 file;
  34. u32 hash;
  35. u32 name_size;
  36. };
  37. static_assert(sizeof(RomFSDirectoryEntry) == 0x18, "RomFSDirectoryEntry has incorrect size.");
  38. struct RomFSFileEntry {
  39. u32 parent;
  40. u32 sibling;
  41. u64 offset;
  42. u64 size;
  43. u32 hash;
  44. u32 name_size;
  45. };
  46. static_assert(sizeof(RomFSFileEntry) == 0x20, "RomFSFileEntry has incorrect size.");
  47. struct RomFSBuildFileContext;
  48. struct RomFSBuildDirectoryContext {
  49. std::string path;
  50. u32 cur_path_ofs = 0;
  51. u32 path_len = 0;
  52. u32 entry_offset = 0;
  53. std::shared_ptr<RomFSBuildDirectoryContext> parent;
  54. std::shared_ptr<RomFSBuildDirectoryContext> child;
  55. std::shared_ptr<RomFSBuildDirectoryContext> sibling;
  56. std::shared_ptr<RomFSBuildFileContext> file;
  57. };
  58. struct RomFSBuildFileContext {
  59. std::string path;
  60. u32 cur_path_ofs = 0;
  61. u32 path_len = 0;
  62. u32 entry_offset = 0;
  63. u64 offset = 0;
  64. u64 size = 0;
  65. std::shared_ptr<RomFSBuildDirectoryContext> parent;
  66. std::shared_ptr<RomFSBuildFileContext> sibling;
  67. VirtualFile source;
  68. };
  69. static u32 romfs_calc_path_hash(u32 parent, std::string_view path, u32 start,
  70. std::size_t path_len) {
  71. u32 hash = parent ^ 123456789;
  72. for (u32 i = 0; i < path_len; i++) {
  73. hash = (hash >> 5) | (hash << 27);
  74. hash ^= path[start + i];
  75. }
  76. return hash;
  77. }
  78. static u64 romfs_get_hash_table_count(u64 num_entries) {
  79. if (num_entries < 3) {
  80. return 3;
  81. }
  82. if (num_entries < 19) {
  83. return num_entries | 1;
  84. }
  85. u64 count = num_entries;
  86. while (count % 2 == 0 || count % 3 == 0 || count % 5 == 0 || count % 7 == 0 ||
  87. count % 11 == 0 || count % 13 == 0 || count % 17 == 0) {
  88. count++;
  89. }
  90. return count;
  91. }
  92. void RomFSBuildContext::VisitDirectory(VirtualDir romfs_dir, VirtualDir ext_dir,
  93. std::shared_ptr<RomFSBuildDirectoryContext> parent) {
  94. for (auto& child_romfs_file : romfs_dir->GetFiles()) {
  95. const auto name = child_romfs_file->GetName();
  96. const auto child = std::make_shared<RomFSBuildFileContext>();
  97. // Set child's path.
  98. child->cur_path_ofs = parent->path_len + 1;
  99. child->path_len = child->cur_path_ofs + static_cast<u32>(name.size());
  100. child->path = parent->path + "/" + name;
  101. if (ext_dir != nullptr && ext_dir->GetFile(name + ".stub") != nullptr) {
  102. continue;
  103. }
  104. // Sanity check on path_len
  105. ASSERT(child->path_len < FS_MAX_PATH);
  106. child->source = std::move(child_romfs_file);
  107. if (ext_dir != nullptr) {
  108. if (const auto ips = ext_dir->GetFile(name + ".ips")) {
  109. if (auto patched = PatchIPS(child->source, ips)) {
  110. child->source = std::move(patched);
  111. }
  112. }
  113. }
  114. child->size = child->source->GetSize();
  115. AddFile(parent, child);
  116. }
  117. for (auto& child_romfs_dir : romfs_dir->GetSubdirectories()) {
  118. const auto name = child_romfs_dir->GetName();
  119. const auto child = std::make_shared<RomFSBuildDirectoryContext>();
  120. // Set child's path.
  121. child->cur_path_ofs = parent->path_len + 1;
  122. child->path_len = child->cur_path_ofs + static_cast<u32>(name.size());
  123. child->path = parent->path + "/" + name;
  124. if (ext_dir != nullptr && ext_dir->GetFile(name + ".stub") != nullptr) {
  125. continue;
  126. }
  127. // Sanity check on path_len
  128. ASSERT(child->path_len < FS_MAX_PATH);
  129. if (!AddDirectory(parent, child)) {
  130. continue;
  131. }
  132. auto child_ext_dir = ext_dir != nullptr ? ext_dir->GetSubdirectory(name) : nullptr;
  133. this->VisitDirectory(child_romfs_dir, child_ext_dir, child);
  134. }
  135. }
  136. bool RomFSBuildContext::AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
  137. std::shared_ptr<RomFSBuildDirectoryContext> dir_ctx) {
  138. // Check whether it's already in the known directories.
  139. const auto [it, is_new] = directories.emplace(dir_ctx->path, nullptr);
  140. if (!is_new) {
  141. return false;
  142. }
  143. // Add a new directory.
  144. num_dirs++;
  145. dir_table_size +=
  146. sizeof(RomFSDirectoryEntry) + Common::AlignUp(dir_ctx->path_len - dir_ctx->cur_path_ofs, 4);
  147. dir_ctx->parent = parent_dir_ctx;
  148. it->second = dir_ctx;
  149. return true;
  150. }
  151. bool RomFSBuildContext::AddFile(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
  152. std::shared_ptr<RomFSBuildFileContext> file_ctx) {
  153. // Check whether it's already in the known files.
  154. const auto [it, is_new] = files.emplace(file_ctx->path, nullptr);
  155. if (!is_new) {
  156. return false;
  157. }
  158. // Add a new file.
  159. num_files++;
  160. file_table_size +=
  161. sizeof(RomFSFileEntry) + Common::AlignUp(file_ctx->path_len - file_ctx->cur_path_ofs, 4);
  162. file_ctx->parent = parent_dir_ctx;
  163. it->second = file_ctx;
  164. return true;
  165. }
  166. RomFSBuildContext::RomFSBuildContext(VirtualDir base_, VirtualDir ext_)
  167. : base(std::move(base_)), ext(std::move(ext_)) {
  168. root = std::make_shared<RomFSBuildDirectoryContext>();
  169. root->path = "\0";
  170. directories.emplace(root->path, root);
  171. num_dirs = 1;
  172. dir_table_size = 0x18;
  173. VisitDirectory(base, ext, root);
  174. }
  175. RomFSBuildContext::~RomFSBuildContext() = default;
  176. std::multimap<u64, VirtualFile> RomFSBuildContext::Build() {
  177. const u64 dir_hash_table_entry_count = romfs_get_hash_table_count(num_dirs);
  178. const u64 file_hash_table_entry_count = romfs_get_hash_table_count(num_files);
  179. dir_hash_table_size = 4 * dir_hash_table_entry_count;
  180. file_hash_table_size = 4 * file_hash_table_entry_count;
  181. // Assign metadata pointers
  182. RomFSHeader header{};
  183. std::vector<u32> dir_hash_table(dir_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
  184. std::vector<u32> file_hash_table(file_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
  185. std::vector<u8> dir_table(dir_table_size);
  186. std::vector<u8> file_table(file_table_size);
  187. std::shared_ptr<RomFSBuildFileContext> cur_file;
  188. // Determine file offsets.
  189. u32 entry_offset = 0;
  190. std::shared_ptr<RomFSBuildFileContext> prev_file = nullptr;
  191. for (const auto& it : files) {
  192. cur_file = it.second;
  193. file_partition_size = Common::AlignUp(file_partition_size, 16);
  194. cur_file->offset = file_partition_size;
  195. file_partition_size += cur_file->size;
  196. cur_file->entry_offset = entry_offset;
  197. entry_offset +=
  198. static_cast<u32>(sizeof(RomFSFileEntry) +
  199. Common::AlignUp(cur_file->path_len - cur_file->cur_path_ofs, 4));
  200. prev_file = cur_file;
  201. }
  202. // Assign deferred parent/sibling ownership.
  203. for (auto it = files.rbegin(); it != files.rend(); ++it) {
  204. cur_file = it->second;
  205. cur_file->sibling = cur_file->parent->file;
  206. cur_file->parent->file = cur_file;
  207. }
  208. std::shared_ptr<RomFSBuildDirectoryContext> cur_dir;
  209. // Determine directory offsets.
  210. entry_offset = 0;
  211. for (const auto& it : directories) {
  212. cur_dir = it.second;
  213. cur_dir->entry_offset = entry_offset;
  214. entry_offset +=
  215. static_cast<u32>(sizeof(RomFSDirectoryEntry) +
  216. Common::AlignUp(cur_dir->path_len - cur_dir->cur_path_ofs, 4));
  217. }
  218. // Assign deferred parent/sibling ownership.
  219. for (auto it = directories.rbegin(); it->second != root; ++it) {
  220. cur_dir = it->second;
  221. cur_dir->sibling = cur_dir->parent->child;
  222. cur_dir->parent->child = cur_dir;
  223. }
  224. std::multimap<u64, VirtualFile> out;
  225. // Populate file tables.
  226. for (const auto& it : files) {
  227. cur_file = it.second;
  228. RomFSFileEntry cur_entry{};
  229. cur_entry.parent = cur_file->parent->entry_offset;
  230. cur_entry.sibling =
  231. cur_file->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset;
  232. cur_entry.offset = cur_file->offset;
  233. cur_entry.size = cur_file->size;
  234. const auto name_size = cur_file->path_len - cur_file->cur_path_ofs;
  235. const auto hash = romfs_calc_path_hash(cur_file->parent->entry_offset, cur_file->path,
  236. cur_file->cur_path_ofs, name_size);
  237. cur_entry.hash = file_hash_table[hash % file_hash_table_entry_count];
  238. file_hash_table[hash % file_hash_table_entry_count] = cur_file->entry_offset;
  239. cur_entry.name_size = name_size;
  240. out.emplace(cur_file->offset + ROMFS_FILEPARTITION_OFS, std::move(cur_file->source));
  241. std::memcpy(file_table.data() + cur_file->entry_offset, &cur_entry, sizeof(RomFSFileEntry));
  242. std::memset(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry), 0,
  243. Common::AlignUp(cur_entry.name_size, 4));
  244. std::memcpy(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry),
  245. cur_file->path.data() + cur_file->cur_path_ofs, name_size);
  246. }
  247. // Populate dir tables.
  248. for (const auto& it : directories) {
  249. cur_dir = it.second;
  250. RomFSDirectoryEntry cur_entry{};
  251. cur_entry.parent = cur_dir == root ? 0 : cur_dir->parent->entry_offset;
  252. cur_entry.sibling =
  253. cur_dir->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset;
  254. cur_entry.child =
  255. cur_dir->child == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset;
  256. cur_entry.file = cur_dir->file == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset;
  257. const auto name_size = cur_dir->path_len - cur_dir->cur_path_ofs;
  258. const auto hash = romfs_calc_path_hash(cur_dir == root ? 0 : cur_dir->parent->entry_offset,
  259. cur_dir->path, cur_dir->cur_path_ofs, name_size);
  260. cur_entry.hash = dir_hash_table[hash % dir_hash_table_entry_count];
  261. dir_hash_table[hash % dir_hash_table_entry_count] = cur_dir->entry_offset;
  262. cur_entry.name_size = name_size;
  263. std::memcpy(dir_table.data() + cur_dir->entry_offset, &cur_entry,
  264. sizeof(RomFSDirectoryEntry));
  265. std::memset(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry), 0,
  266. Common::AlignUp(cur_entry.name_size, 4));
  267. std::memcpy(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry),
  268. cur_dir->path.data() + cur_dir->cur_path_ofs, name_size);
  269. }
  270. // Set header fields.
  271. header.header_size = sizeof(RomFSHeader);
  272. header.file_hash_table_size = file_hash_table_size;
  273. header.file_table_size = file_table_size;
  274. header.dir_hash_table_size = dir_hash_table_size;
  275. header.dir_table_size = dir_table_size;
  276. header.file_partition_ofs = ROMFS_FILEPARTITION_OFS;
  277. header.dir_hash_table_ofs = Common::AlignUp(header.file_partition_ofs + file_partition_size, 4);
  278. header.dir_table_ofs = header.dir_hash_table_ofs + header.dir_hash_table_size;
  279. header.file_hash_table_ofs = header.dir_table_ofs + header.dir_table_size;
  280. header.file_table_ofs = header.file_hash_table_ofs + header.file_hash_table_size;
  281. std::vector<u8> header_data(sizeof(RomFSHeader));
  282. std::memcpy(header_data.data(), &header, header_data.size());
  283. out.emplace(0, std::make_shared<VectorVfsFile>(std::move(header_data)));
  284. std::vector<u8> metadata(file_hash_table_size + file_table_size + dir_hash_table_size +
  285. dir_table_size);
  286. std::size_t index = 0;
  287. std::memcpy(metadata.data(), dir_hash_table.data(), dir_hash_table.size() * sizeof(u32));
  288. index += dir_hash_table.size() * sizeof(u32);
  289. std::memcpy(metadata.data() + index, dir_table.data(), dir_table.size());
  290. index += dir_table.size();
  291. std::memcpy(metadata.data() + index, file_hash_table.data(),
  292. file_hash_table.size() * sizeof(u32));
  293. index += file_hash_table.size() * sizeof(u32);
  294. std::memcpy(metadata.data() + index, file_table.data(), file_table.size());
  295. out.emplace(header.dir_hash_table_ofs, std::make_shared<VectorVfsFile>(std::move(metadata)));
  296. return out;
  297. }
  298. } // namespace FileSys