fsmitm_romfsbuild.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (c) 2018 Atmosphère-NX
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. * Adapted by DarkLordZach for use/interaction with yuzu
  18. *
  19. * Modifications Copyright 2018 yuzu emulator team
  20. * Licensed under GPLv2 or any later version
  21. * Refer to the license.txt file included.
  22. */
  23. #include <cstring>
  24. #include <string_view>
  25. #include "common/alignment.h"
  26. #include "common/assert.h"
  27. #include "core/file_sys/fsmitm_romfsbuild.h"
  28. #include "core/file_sys/ips_layer.h"
  29. #include "core/file_sys/vfs.h"
  30. #include "core/file_sys/vfs_vector.h"
  31. namespace FileSys {
  32. constexpr u64 FS_MAX_PATH = 0x301;
  33. constexpr u32 ROMFS_ENTRY_EMPTY = 0xFFFFFFFF;
  34. constexpr u32 ROMFS_FILEPARTITION_OFS = 0x200;
  35. // Types for building a RomFS.
  36. struct RomFSHeader {
  37. u64 header_size;
  38. u64 dir_hash_table_ofs;
  39. u64 dir_hash_table_size;
  40. u64 dir_table_ofs;
  41. u64 dir_table_size;
  42. u64 file_hash_table_ofs;
  43. u64 file_hash_table_size;
  44. u64 file_table_ofs;
  45. u64 file_table_size;
  46. u64 file_partition_ofs;
  47. };
  48. static_assert(sizeof(RomFSHeader) == 0x50, "RomFSHeader has incorrect size.");
  49. struct RomFSDirectoryEntry {
  50. u32 parent;
  51. u32 sibling;
  52. u32 child;
  53. u32 file;
  54. u32 hash;
  55. u32 name_size;
  56. };
  57. static_assert(sizeof(RomFSDirectoryEntry) == 0x18, "RomFSDirectoryEntry has incorrect size.");
  58. struct RomFSFileEntry {
  59. u32 parent;
  60. u32 sibling;
  61. u64 offset;
  62. u64 size;
  63. u32 hash;
  64. u32 name_size;
  65. };
  66. static_assert(sizeof(RomFSFileEntry) == 0x20, "RomFSFileEntry has incorrect size.");
  67. struct RomFSBuildFileContext;
  68. struct RomFSBuildDirectoryContext {
  69. std::string path;
  70. u32 cur_path_ofs = 0;
  71. u32 path_len = 0;
  72. u32 entry_offset = 0;
  73. std::shared_ptr<RomFSBuildDirectoryContext> parent;
  74. std::shared_ptr<RomFSBuildDirectoryContext> child;
  75. std::shared_ptr<RomFSBuildDirectoryContext> sibling;
  76. std::shared_ptr<RomFSBuildFileContext> file;
  77. };
  78. struct RomFSBuildFileContext {
  79. std::string path;
  80. u32 cur_path_ofs = 0;
  81. u32 path_len = 0;
  82. u32 entry_offset = 0;
  83. u64 offset = 0;
  84. u64 size = 0;
  85. std::shared_ptr<RomFSBuildDirectoryContext> parent;
  86. std::shared_ptr<RomFSBuildFileContext> sibling;
  87. VirtualFile source;
  88. };
  89. static u32 romfs_calc_path_hash(u32 parent, std::string_view path, u32 start,
  90. std::size_t path_len) {
  91. u32 hash = parent ^ 123456789;
  92. for (u32 i = 0; i < path_len; i++) {
  93. hash = (hash >> 5) | (hash << 27);
  94. hash ^= path[start + i];
  95. }
  96. return hash;
  97. }
  98. static u64 romfs_get_hash_table_count(u64 num_entries) {
  99. if (num_entries < 3) {
  100. return 3;
  101. }
  102. if (num_entries < 19) {
  103. return num_entries | 1;
  104. }
  105. u64 count = num_entries;
  106. while (count % 2 == 0 || count % 3 == 0 || count % 5 == 0 || count % 7 == 0 ||
  107. count % 11 == 0 || count % 13 == 0 || count % 17 == 0) {
  108. count++;
  109. }
  110. return count;
  111. }
  112. void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, VirtualDir ext,
  113. std::shared_ptr<RomFSBuildDirectoryContext> parent) {
  114. std::vector<std::shared_ptr<RomFSBuildDirectoryContext>> child_dirs;
  115. VirtualDir dir;
  116. if (parent->path_len == 0)
  117. dir = root_romfs;
  118. else
  119. dir = root_romfs->GetDirectoryRelative(parent->path);
  120. const auto entries = dir->GetEntries();
  121. for (const auto& kv : entries) {
  122. if (kv.second == VfsEntryType::Directory) {
  123. const auto child = std::make_shared<RomFSBuildDirectoryContext>();
  124. // Set child's path.
  125. child->cur_path_ofs = parent->path_len + 1;
  126. child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
  127. child->path = parent->path + "/" + kv.first;
  128. if (ext != nullptr && ext->GetFileRelative(child->path + ".stub") != nullptr)
  129. continue;
  130. // Sanity check on path_len
  131. ASSERT(child->path_len < FS_MAX_PATH);
  132. if (AddDirectory(parent, child)) {
  133. child_dirs.push_back(child);
  134. }
  135. } else {
  136. const auto child = std::make_shared<RomFSBuildFileContext>();
  137. // Set child's path.
  138. child->cur_path_ofs = parent->path_len + 1;
  139. child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
  140. child->path = parent->path + "/" + kv.first;
  141. if (ext != nullptr && ext->GetFileRelative(child->path + ".stub") != nullptr)
  142. continue;
  143. // Sanity check on path_len
  144. ASSERT(child->path_len < FS_MAX_PATH);
  145. child->source = root_romfs->GetFileRelative(child->path);
  146. if (ext != nullptr) {
  147. const auto ips = ext->GetFileRelative(child->path + ".ips");
  148. if (ips != nullptr) {
  149. auto patched = PatchIPS(child->source, ips);
  150. if (patched != nullptr)
  151. child->source = std::move(patched);
  152. }
  153. }
  154. child->size = child->source->GetSize();
  155. AddFile(parent, child);
  156. }
  157. }
  158. for (auto& child : child_dirs) {
  159. this->VisitDirectory(root_romfs, ext, child);
  160. }
  161. }
  162. bool RomFSBuildContext::AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
  163. std::shared_ptr<RomFSBuildDirectoryContext> dir_ctx) {
  164. // Check whether it's already in the known directories.
  165. const auto existing = directories.find(dir_ctx->path);
  166. if (existing != directories.end())
  167. return false;
  168. // Add a new directory.
  169. num_dirs++;
  170. dir_table_size +=
  171. sizeof(RomFSDirectoryEntry) + Common::AlignUp(dir_ctx->path_len - dir_ctx->cur_path_ofs, 4);
  172. dir_ctx->parent = parent_dir_ctx;
  173. directories.emplace(dir_ctx->path, dir_ctx);
  174. return true;
  175. }
  176. bool RomFSBuildContext::AddFile(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
  177. std::shared_ptr<RomFSBuildFileContext> file_ctx) {
  178. // Check whether it's already in the known files.
  179. const auto existing = files.find(file_ctx->path);
  180. if (existing != files.end()) {
  181. return false;
  182. }
  183. // Add a new file.
  184. num_files++;
  185. file_table_size +=
  186. sizeof(RomFSFileEntry) + Common::AlignUp(file_ctx->path_len - file_ctx->cur_path_ofs, 4);
  187. file_ctx->parent = parent_dir_ctx;
  188. files.emplace(file_ctx->path, file_ctx);
  189. return true;
  190. }
  191. RomFSBuildContext::RomFSBuildContext(VirtualDir base_, VirtualDir ext_)
  192. : base(std::move(base_)), ext(std::move(ext_)) {
  193. root = std::make_shared<RomFSBuildDirectoryContext>();
  194. root->path = "\0";
  195. directories.emplace(root->path, root);
  196. num_dirs = 1;
  197. dir_table_size = 0x18;
  198. VisitDirectory(base, ext, root);
  199. }
  200. RomFSBuildContext::~RomFSBuildContext() = default;
  201. std::multimap<u64, VirtualFile> RomFSBuildContext::Build() {
  202. const u64 dir_hash_table_entry_count = romfs_get_hash_table_count(num_dirs);
  203. const u64 file_hash_table_entry_count = romfs_get_hash_table_count(num_files);
  204. dir_hash_table_size = 4 * dir_hash_table_entry_count;
  205. file_hash_table_size = 4 * file_hash_table_entry_count;
  206. // Assign metadata pointers
  207. RomFSHeader header{};
  208. std::vector<u32> dir_hash_table(dir_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
  209. std::vector<u32> file_hash_table(file_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
  210. std::vector<u8> dir_table(dir_table_size);
  211. std::vector<u8> file_table(file_table_size);
  212. std::shared_ptr<RomFSBuildFileContext> cur_file;
  213. // Determine file offsets.
  214. u32 entry_offset = 0;
  215. std::shared_ptr<RomFSBuildFileContext> prev_file = nullptr;
  216. for (const auto& it : files) {
  217. cur_file = it.second;
  218. file_partition_size = Common::AlignUp(file_partition_size, 16);
  219. cur_file->offset = file_partition_size;
  220. file_partition_size += cur_file->size;
  221. cur_file->entry_offset = entry_offset;
  222. entry_offset += sizeof(RomFSFileEntry) +
  223. Common::AlignUp(cur_file->path_len - cur_file->cur_path_ofs, 4);
  224. prev_file = cur_file;
  225. }
  226. // Assign deferred parent/sibling ownership.
  227. for (auto it = files.rbegin(); it != files.rend(); ++it) {
  228. cur_file = it->second;
  229. cur_file->sibling = cur_file->parent->file;
  230. cur_file->parent->file = cur_file;
  231. }
  232. std::shared_ptr<RomFSBuildDirectoryContext> cur_dir;
  233. // Determine directory offsets.
  234. entry_offset = 0;
  235. for (const auto& it : directories) {
  236. cur_dir = it.second;
  237. cur_dir->entry_offset = entry_offset;
  238. entry_offset += sizeof(RomFSDirectoryEntry) +
  239. Common::AlignUp(cur_dir->path_len - cur_dir->cur_path_ofs, 4);
  240. }
  241. // Assign deferred parent/sibling ownership.
  242. for (auto it = directories.rbegin(); it->second != root; ++it) {
  243. cur_dir = it->second;
  244. cur_dir->sibling = cur_dir->parent->child;
  245. cur_dir->parent->child = cur_dir;
  246. }
  247. std::multimap<u64, VirtualFile> out;
  248. // Populate file tables.
  249. for (const auto& it : files) {
  250. cur_file = it.second;
  251. RomFSFileEntry cur_entry{};
  252. cur_entry.parent = cur_file->parent->entry_offset;
  253. cur_entry.sibling =
  254. cur_file->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset;
  255. cur_entry.offset = cur_file->offset;
  256. cur_entry.size = cur_file->size;
  257. const auto name_size = cur_file->path_len - cur_file->cur_path_ofs;
  258. const auto hash = romfs_calc_path_hash(cur_file->parent->entry_offset, cur_file->path,
  259. cur_file->cur_path_ofs, name_size);
  260. cur_entry.hash = file_hash_table[hash % file_hash_table_entry_count];
  261. file_hash_table[hash % file_hash_table_entry_count] = cur_file->entry_offset;
  262. cur_entry.name_size = name_size;
  263. out.emplace(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->source);
  264. std::memcpy(file_table.data() + cur_file->entry_offset, &cur_entry, sizeof(RomFSFileEntry));
  265. std::memset(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry), 0,
  266. Common::AlignUp(cur_entry.name_size, 4));
  267. std::memcpy(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry),
  268. cur_file->path.data() + cur_file->cur_path_ofs, name_size);
  269. }
  270. // Populate dir tables.
  271. for (const auto& it : directories) {
  272. cur_dir = it.second;
  273. RomFSDirectoryEntry cur_entry{};
  274. cur_entry.parent = cur_dir == root ? 0 : cur_dir->parent->entry_offset;
  275. cur_entry.sibling =
  276. cur_dir->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset;
  277. cur_entry.child =
  278. cur_dir->child == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset;
  279. cur_entry.file = cur_dir->file == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset;
  280. const auto name_size = cur_dir->path_len - cur_dir->cur_path_ofs;
  281. const auto hash = romfs_calc_path_hash(cur_dir == root ? 0 : cur_dir->parent->entry_offset,
  282. cur_dir->path, cur_dir->cur_path_ofs, name_size);
  283. cur_entry.hash = dir_hash_table[hash % dir_hash_table_entry_count];
  284. dir_hash_table[hash % dir_hash_table_entry_count] = cur_dir->entry_offset;
  285. cur_entry.name_size = name_size;
  286. std::memcpy(dir_table.data() + cur_dir->entry_offset, &cur_entry,
  287. sizeof(RomFSDirectoryEntry));
  288. std::memset(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry), 0,
  289. Common::AlignUp(cur_entry.name_size, 4));
  290. std::memcpy(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry),
  291. cur_dir->path.data() + cur_dir->cur_path_ofs, name_size);
  292. }
  293. // Set header fields.
  294. header.header_size = sizeof(RomFSHeader);
  295. header.file_hash_table_size = file_hash_table_size;
  296. header.file_table_size = file_table_size;
  297. header.dir_hash_table_size = dir_hash_table_size;
  298. header.dir_table_size = dir_table_size;
  299. header.file_partition_ofs = ROMFS_FILEPARTITION_OFS;
  300. header.dir_hash_table_ofs = Common::AlignUp(header.file_partition_ofs + file_partition_size, 4);
  301. header.dir_table_ofs = header.dir_hash_table_ofs + header.dir_hash_table_size;
  302. header.file_hash_table_ofs = header.dir_table_ofs + header.dir_table_size;
  303. header.file_table_ofs = header.file_hash_table_ofs + header.file_hash_table_size;
  304. std::vector<u8> header_data(sizeof(RomFSHeader));
  305. std::memcpy(header_data.data(), &header, header_data.size());
  306. out.emplace(0, std::make_shared<VectorVfsFile>(std::move(header_data)));
  307. std::vector<u8> metadata(file_hash_table_size + file_table_size + dir_hash_table_size +
  308. dir_table_size);
  309. std::size_t index = 0;
  310. std::memcpy(metadata.data(), dir_hash_table.data(), dir_hash_table.size() * sizeof(u32));
  311. index += dir_hash_table.size() * sizeof(u32);
  312. std::memcpy(metadata.data() + index, dir_table.data(), dir_table.size());
  313. index += dir_table.size();
  314. std::memcpy(metadata.data() + index, file_hash_table.data(),
  315. file_hash_table.size() * sizeof(u32));
  316. index += file_hash_table.size() * sizeof(u32);
  317. std::memcpy(metadata.data() + index, file_table.data(), file_table.size());
  318. out.emplace(header.dir_hash_table_ofs, std::make_shared<VectorVfsFile>(std::move(metadata)));
  319. return out;
  320. }
  321. } // namespace FileSys