fsmitm_romfsbuild.cpp 13 KB

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