fsmitm_romfsbuild.cpp 13 KB

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