fsmitm_romfsbuild.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. std::vector<std::shared_ptr<RomFSBuildDirectoryContext>> child_dirs;
  95. const auto entries = romfs_dir->GetEntries();
  96. for (const auto& kv : entries) {
  97. if (kv.second == VfsEntryType::Directory) {
  98. const auto child = std::make_shared<RomFSBuildDirectoryContext>();
  99. // Set child's path.
  100. child->cur_path_ofs = parent->path_len + 1;
  101. child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
  102. child->path = parent->path + "/" + kv.first;
  103. if (ext_dir != nullptr && ext_dir->GetFile(kv.first + ".stub") != nullptr) {
  104. continue;
  105. }
  106. // Sanity check on path_len
  107. ASSERT(child->path_len < FS_MAX_PATH);
  108. if (AddDirectory(parent, child)) {
  109. child_dirs.push_back(child);
  110. }
  111. } else {
  112. const auto child = std::make_shared<RomFSBuildFileContext>();
  113. // Set child's path.
  114. child->cur_path_ofs = parent->path_len + 1;
  115. child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
  116. child->path = parent->path + "/" + kv.first;
  117. if (ext_dir != nullptr && ext_dir->GetFile(kv.first + ".stub") != nullptr) {
  118. continue;
  119. }
  120. // Sanity check on path_len
  121. ASSERT(child->path_len < FS_MAX_PATH);
  122. child->source = romfs_dir->GetFile(kv.first);
  123. if (ext_dir != nullptr) {
  124. if (const auto ips = ext_dir->GetFile(kv.first + ".ips")) {
  125. if (auto patched = PatchIPS(child->source, ips)) {
  126. child->source = std::move(patched);
  127. }
  128. }
  129. }
  130. child->size = child->source->GetSize();
  131. AddFile(parent, child);
  132. }
  133. }
  134. for (auto& child : child_dirs) {
  135. auto subdir_name = std::string_view(child->path).substr(child->cur_path_ofs);
  136. auto child_romfs_dir = romfs_dir->GetSubdirectory(subdir_name);
  137. auto child_ext_dir = ext_dir != nullptr ? ext_dir->GetSubdirectory(subdir_name) : nullptr;
  138. this->VisitDirectory(child_romfs_dir, child_ext_dir, child);
  139. }
  140. }
  141. bool RomFSBuildContext::AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
  142. std::shared_ptr<RomFSBuildDirectoryContext> dir_ctx) {
  143. // Check whether it's already in the known directories.
  144. const auto [it, is_new] = directories.emplace(dir_ctx->path, nullptr);
  145. if (!is_new) {
  146. return false;
  147. }
  148. // Add a new directory.
  149. num_dirs++;
  150. dir_table_size +=
  151. sizeof(RomFSDirectoryEntry) + Common::AlignUp(dir_ctx->path_len - dir_ctx->cur_path_ofs, 4);
  152. dir_ctx->parent = parent_dir_ctx;
  153. it->second = dir_ctx;
  154. return true;
  155. }
  156. bool RomFSBuildContext::AddFile(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
  157. std::shared_ptr<RomFSBuildFileContext> file_ctx) {
  158. // Check whether it's already in the known files.
  159. const auto [it, is_new] = files.emplace(file_ctx->path, nullptr);
  160. if (!is_new) {
  161. return false;
  162. }
  163. // Add a new file.
  164. num_files++;
  165. file_table_size +=
  166. sizeof(RomFSFileEntry) + Common::AlignUp(file_ctx->path_len - file_ctx->cur_path_ofs, 4);
  167. file_ctx->parent = parent_dir_ctx;
  168. it->second = file_ctx;
  169. return true;
  170. }
  171. RomFSBuildContext::RomFSBuildContext(VirtualDir base_, VirtualDir ext_)
  172. : base(std::move(base_)), ext(std::move(ext_)) {
  173. root = std::make_shared<RomFSBuildDirectoryContext>();
  174. root->path = "\0";
  175. directories.emplace(root->path, root);
  176. num_dirs = 1;
  177. dir_table_size = 0x18;
  178. VisitDirectory(base, ext, root);
  179. }
  180. RomFSBuildContext::~RomFSBuildContext() = default;
  181. std::multimap<u64, VirtualFile> RomFSBuildContext::Build() {
  182. const u64 dir_hash_table_entry_count = romfs_get_hash_table_count(num_dirs);
  183. const u64 file_hash_table_entry_count = romfs_get_hash_table_count(num_files);
  184. dir_hash_table_size = 4 * dir_hash_table_entry_count;
  185. file_hash_table_size = 4 * file_hash_table_entry_count;
  186. // Assign metadata pointers
  187. RomFSHeader header{};
  188. std::vector<u32> dir_hash_table(dir_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
  189. std::vector<u32> file_hash_table(file_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
  190. std::vector<u8> dir_table(dir_table_size);
  191. std::vector<u8> file_table(file_table_size);
  192. std::shared_ptr<RomFSBuildFileContext> cur_file;
  193. // Determine file offsets.
  194. u32 entry_offset = 0;
  195. std::shared_ptr<RomFSBuildFileContext> prev_file = nullptr;
  196. for (const auto& it : files) {
  197. cur_file = it.second;
  198. file_partition_size = Common::AlignUp(file_partition_size, 16);
  199. cur_file->offset = file_partition_size;
  200. file_partition_size += cur_file->size;
  201. cur_file->entry_offset = entry_offset;
  202. entry_offset +=
  203. static_cast<u32>(sizeof(RomFSFileEntry) +
  204. Common::AlignUp(cur_file->path_len - cur_file->cur_path_ofs, 4));
  205. prev_file = cur_file;
  206. }
  207. // Assign deferred parent/sibling ownership.
  208. for (auto it = files.rbegin(); it != files.rend(); ++it) {
  209. cur_file = it->second;
  210. cur_file->sibling = cur_file->parent->file;
  211. cur_file->parent->file = cur_file;
  212. }
  213. std::shared_ptr<RomFSBuildDirectoryContext> cur_dir;
  214. // Determine directory offsets.
  215. entry_offset = 0;
  216. for (const auto& it : directories) {
  217. cur_dir = it.second;
  218. cur_dir->entry_offset = entry_offset;
  219. entry_offset +=
  220. static_cast<u32>(sizeof(RomFSDirectoryEntry) +
  221. Common::AlignUp(cur_dir->path_len - cur_dir->cur_path_ofs, 4));
  222. }
  223. // Assign deferred parent/sibling ownership.
  224. for (auto it = directories.rbegin(); it->second != root; ++it) {
  225. cur_dir = it->second;
  226. cur_dir->sibling = cur_dir->parent->child;
  227. cur_dir->parent->child = cur_dir;
  228. }
  229. std::multimap<u64, VirtualFile> out;
  230. // Populate file tables.
  231. for (const auto& it : files) {
  232. cur_file = it.second;
  233. RomFSFileEntry cur_entry{};
  234. cur_entry.parent = cur_file->parent->entry_offset;
  235. cur_entry.sibling =
  236. cur_file->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset;
  237. cur_entry.offset = cur_file->offset;
  238. cur_entry.size = cur_file->size;
  239. const auto name_size = cur_file->path_len - cur_file->cur_path_ofs;
  240. const auto hash = romfs_calc_path_hash(cur_file->parent->entry_offset, cur_file->path,
  241. cur_file->cur_path_ofs, name_size);
  242. cur_entry.hash = file_hash_table[hash % file_hash_table_entry_count];
  243. file_hash_table[hash % file_hash_table_entry_count] = cur_file->entry_offset;
  244. cur_entry.name_size = name_size;
  245. out.emplace(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->source);
  246. std::memcpy(file_table.data() + cur_file->entry_offset, &cur_entry, sizeof(RomFSFileEntry));
  247. std::memset(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry), 0,
  248. Common::AlignUp(cur_entry.name_size, 4));
  249. std::memcpy(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry),
  250. cur_file->path.data() + cur_file->cur_path_ofs, name_size);
  251. }
  252. // Populate dir tables.
  253. for (const auto& it : directories) {
  254. cur_dir = it.second;
  255. RomFSDirectoryEntry cur_entry{};
  256. cur_entry.parent = cur_dir == root ? 0 : cur_dir->parent->entry_offset;
  257. cur_entry.sibling =
  258. cur_dir->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset;
  259. cur_entry.child =
  260. cur_dir->child == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset;
  261. cur_entry.file = cur_dir->file == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset;
  262. const auto name_size = cur_dir->path_len - cur_dir->cur_path_ofs;
  263. const auto hash = romfs_calc_path_hash(cur_dir == root ? 0 : cur_dir->parent->entry_offset,
  264. cur_dir->path, cur_dir->cur_path_ofs, name_size);
  265. cur_entry.hash = dir_hash_table[hash % dir_hash_table_entry_count];
  266. dir_hash_table[hash % dir_hash_table_entry_count] = cur_dir->entry_offset;
  267. cur_entry.name_size = name_size;
  268. std::memcpy(dir_table.data() + cur_dir->entry_offset, &cur_entry,
  269. sizeof(RomFSDirectoryEntry));
  270. std::memset(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry), 0,
  271. Common::AlignUp(cur_entry.name_size, 4));
  272. std::memcpy(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry),
  273. cur_dir->path.data() + cur_dir->cur_path_ofs, name_size);
  274. }
  275. // Set header fields.
  276. header.header_size = sizeof(RomFSHeader);
  277. header.file_hash_table_size = file_hash_table_size;
  278. header.file_table_size = file_table_size;
  279. header.dir_hash_table_size = dir_hash_table_size;
  280. header.dir_table_size = dir_table_size;
  281. header.file_partition_ofs = ROMFS_FILEPARTITION_OFS;
  282. header.dir_hash_table_ofs = Common::AlignUp(header.file_partition_ofs + file_partition_size, 4);
  283. header.dir_table_ofs = header.dir_hash_table_ofs + header.dir_hash_table_size;
  284. header.file_hash_table_ofs = header.dir_table_ofs + header.dir_table_size;
  285. header.file_table_ofs = header.file_hash_table_ofs + header.file_hash_table_size;
  286. std::vector<u8> header_data(sizeof(RomFSHeader));
  287. std::memcpy(header_data.data(), &header, header_data.size());
  288. out.emplace(0, std::make_shared<VectorVfsFile>(std::move(header_data)));
  289. std::vector<u8> metadata(file_hash_table_size + file_table_size + dir_hash_table_size +
  290. dir_table_size);
  291. std::size_t index = 0;
  292. std::memcpy(metadata.data(), dir_hash_table.data(), dir_hash_table.size() * sizeof(u32));
  293. index += dir_hash_table.size() * sizeof(u32);
  294. std::memcpy(metadata.data() + index, dir_table.data(), dir_table.size());
  295. index += dir_table.size();
  296. std::memcpy(metadata.data() + index, file_hash_table.data(),
  297. file_hash_table.size() * sizeof(u32));
  298. index += file_hash_table.size() * sizeof(u32);
  299. std::memcpy(metadata.data() + index, file_table.data(), file_table.size());
  300. out.emplace(header.dir_hash_table_ofs, std::make_shared<VectorVfsFile>(std::move(metadata)));
  301. return out;
  302. }
  303. } // namespace FileSys