archive.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include "common/common_types.h"
  5. #include "common/file_util.h"
  6. #include "common/math_util.h"
  7. #include "core/file_sys/archive.h"
  8. #include "core/file_sys/archive_sdmc.h"
  9. #include "core/file_sys/directory.h"
  10. #include "core/hle/kernel/archive.h"
  11. #include "core/hle/result.h"
  12. #include "core/hle/service/service.h"
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Kernel namespace
  15. namespace Kernel {
  16. // Command to access archive file
  17. enum class FileCommand : u32 {
  18. Dummy1 = 0x000100C6,
  19. Control = 0x040100C4,
  20. OpenSubFile = 0x08010100,
  21. Read = 0x080200C2,
  22. Write = 0x08030102,
  23. GetSize = 0x08040000,
  24. SetSize = 0x08050080,
  25. GetAttributes = 0x08060000,
  26. SetAttributes = 0x08070040,
  27. Close = 0x08080000,
  28. Flush = 0x08090000,
  29. };
  30. // Command to access directory
  31. enum class DirectoryCommand : u32 {
  32. Dummy1 = 0x000100C6,
  33. Control = 0x040100C4,
  34. Read = 0x08010042,
  35. Close = 0x08020000,
  36. };
  37. class Archive : public Object {
  38. public:
  39. std::string GetTypeName() const override { return "Archive"; }
  40. std::string GetName() const override { return name; }
  41. static Kernel::HandleType GetStaticHandleType() { return HandleType::Archive; }
  42. Kernel::HandleType GetHandleType() const override { return HandleType::Archive; }
  43. std::string name; ///< Name of archive (optional)
  44. FileSys::Archive* backend; ///< Archive backend interface
  45. ResultVal<bool> SyncRequest() override {
  46. u32* cmd_buff = Service::GetCommandBuffer();
  47. FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
  48. switch (cmd) {
  49. // Read from archive...
  50. case FileCommand::Read:
  51. {
  52. u64 offset = cmd_buff[1] | ((u64)cmd_buff[2] << 32);
  53. u32 length = cmd_buff[3];
  54. u32 address = cmd_buff[5];
  55. // Number of bytes read
  56. cmd_buff[2] = backend->Read(offset, length, Memory::GetPointer(address));
  57. break;
  58. }
  59. // Write to archive...
  60. case FileCommand::Write:
  61. {
  62. u64 offset = cmd_buff[1] | ((u64)cmd_buff[2] << 32);
  63. u32 length = cmd_buff[3];
  64. u32 flush = cmd_buff[4];
  65. u32 address = cmd_buff[6];
  66. // Number of bytes written
  67. cmd_buff[2] = backend->Write(offset, length, flush, Memory::GetPointer(address));
  68. break;
  69. }
  70. case FileCommand::GetSize:
  71. {
  72. u64 filesize = (u64) backend->GetSize();
  73. cmd_buff[2] = (u32) filesize; // Lower word
  74. cmd_buff[3] = (u32) (filesize >> 32); // Upper word
  75. break;
  76. }
  77. case FileCommand::SetSize:
  78. {
  79. backend->SetSize(cmd_buff[1] | ((u64)cmd_buff[2] << 32));
  80. break;
  81. }
  82. case FileCommand::Close:
  83. {
  84. DEBUG_LOG(KERNEL, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
  85. CloseArchive(backend->GetIdCode());
  86. break;
  87. }
  88. // Unknown command...
  89. default:
  90. {
  91. ERROR_LOG(KERNEL, "Unknown command=0x%08X!", cmd);
  92. return UnimplementedFunction(ErrorModule::FS);
  93. }
  94. }
  95. cmd_buff[1] = 0; // No error
  96. return MakeResult<bool>(false);
  97. }
  98. ResultVal<bool> WaitSynchronization() override {
  99. // TODO(bunnei): ImplementMe
  100. ERROR_LOG(OSHLE, "(UNIMPLEMENTED)");
  101. return UnimplementedFunction(ErrorModule::FS);
  102. }
  103. };
  104. class File : public Object {
  105. public:
  106. std::string GetTypeName() const override { return "File"; }
  107. std::string GetName() const override { return path.DebugStr(); }
  108. static Kernel::HandleType GetStaticHandleType() { return HandleType::File; }
  109. Kernel::HandleType GetHandleType() const override { return HandleType::File; }
  110. FileSys::Path path; ///< Path of the file
  111. std::unique_ptr<FileSys::File> backend; ///< File backend interface
  112. ResultVal<bool> SyncRequest() override {
  113. u32* cmd_buff = Service::GetCommandBuffer();
  114. FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
  115. switch (cmd) {
  116. // Read from file...
  117. case FileCommand::Read:
  118. {
  119. u64 offset = cmd_buff[1] | ((u64) cmd_buff[2]) << 32;
  120. u32 length = cmd_buff[3];
  121. u32 address = cmd_buff[5];
  122. DEBUG_LOG(KERNEL, "Read %s %s: offset=0x%llx length=%d address=0x%x",
  123. GetTypeName().c_str(), GetName().c_str(), offset, length, address);
  124. cmd_buff[2] = backend->Read(offset, length, Memory::GetPointer(address));
  125. break;
  126. }
  127. // Write to file...
  128. case FileCommand::Write:
  129. {
  130. u64 offset = cmd_buff[1] | ((u64) cmd_buff[2]) << 32;
  131. u32 length = cmd_buff[3];
  132. u32 flush = cmd_buff[4];
  133. u32 address = cmd_buff[6];
  134. DEBUG_LOG(KERNEL, "Write %s %s: offset=0x%llx length=%d address=0x%x, flush=0x%x",
  135. GetTypeName().c_str(), GetName().c_str(), offset, length, address, flush);
  136. cmd_buff[2] = backend->Write(offset, length, flush, Memory::GetPointer(address));
  137. break;
  138. }
  139. case FileCommand::GetSize:
  140. {
  141. DEBUG_LOG(KERNEL, "GetSize %s %s", GetTypeName().c_str(), GetName().c_str());
  142. u64 size = backend->GetSize();
  143. cmd_buff[2] = (u32)size;
  144. cmd_buff[3] = size >> 32;
  145. break;
  146. }
  147. case FileCommand::SetSize:
  148. {
  149. u64 size = cmd_buff[1] | ((u64)cmd_buff[2] << 32);
  150. DEBUG_LOG(KERNEL, "SetSize %s %s size=%llu",
  151. GetTypeName().c_str(), GetName().c_str(), size);
  152. backend->SetSize(size);
  153. break;
  154. }
  155. case FileCommand::Close:
  156. {
  157. DEBUG_LOG(KERNEL, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
  158. Kernel::g_object_pool.Destroy<File>(GetHandle());
  159. break;
  160. }
  161. // Unknown command...
  162. default:
  163. ERROR_LOG(KERNEL, "Unknown command=0x%08X!", cmd);
  164. ResultCode error = UnimplementedFunction(ErrorModule::FS);
  165. cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
  166. return error;
  167. }
  168. cmd_buff[1] = 0; // No error
  169. return MakeResult<bool>(false);
  170. }
  171. ResultVal<bool> WaitSynchronization() override {
  172. // TODO(bunnei): ImplementMe
  173. ERROR_LOG(OSHLE, "(UNIMPLEMENTED)");
  174. return UnimplementedFunction(ErrorModule::FS);
  175. }
  176. };
  177. class Directory : public Object {
  178. public:
  179. std::string GetTypeName() const override { return "Directory"; }
  180. std::string GetName() const override { return path.DebugStr(); }
  181. static Kernel::HandleType GetStaticHandleType() { return HandleType::Directory; }
  182. Kernel::HandleType GetHandleType() const override { return HandleType::Directory; }
  183. FileSys::Path path; ///< Path of the directory
  184. std::unique_ptr<FileSys::Directory> backend; ///< File backend interface
  185. ResultVal<bool> SyncRequest() override {
  186. u32* cmd_buff = Service::GetCommandBuffer();
  187. DirectoryCommand cmd = static_cast<DirectoryCommand>(cmd_buff[0]);
  188. switch (cmd) {
  189. // Read from directory...
  190. case DirectoryCommand::Read:
  191. {
  192. u32 count = cmd_buff[1];
  193. u32 address = cmd_buff[3];
  194. auto entries = reinterpret_cast<FileSys::Entry*>(Memory::GetPointer(address));
  195. DEBUG_LOG(KERNEL, "Read %s %s: count=%d",
  196. GetTypeName().c_str(), GetName().c_str(), count);
  197. // Number of entries actually read
  198. cmd_buff[2] = backend->Read(count, entries);
  199. break;
  200. }
  201. case DirectoryCommand::Close:
  202. {
  203. DEBUG_LOG(KERNEL, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
  204. Kernel::g_object_pool.Destroy<Directory>(GetHandle());
  205. break;
  206. }
  207. // Unknown command...
  208. default:
  209. ERROR_LOG(KERNEL, "Unknown command=0x%08X!", cmd);
  210. ResultCode error = UnimplementedFunction(ErrorModule::FS);
  211. cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
  212. return error;
  213. }
  214. cmd_buff[1] = 0; // No error
  215. return MakeResult<bool>(false);
  216. }
  217. ResultVal<bool> WaitSynchronization() override {
  218. // TODO(bunnei): ImplementMe
  219. ERROR_LOG(OSHLE, "(UNIMPLEMENTED)");
  220. return UnimplementedFunction(ErrorModule::FS);
  221. }
  222. };
  223. ////////////////////////////////////////////////////////////////////////////////////////////////////
  224. std::map<FileSys::Archive::IdCode, Handle> g_archive_map; ///< Map of file archives by IdCode
  225. ResultVal<Handle> OpenArchive(FileSys::Archive::IdCode id_code) {
  226. auto itr = g_archive_map.find(id_code);
  227. if (itr == g_archive_map.end()) {
  228. return ResultCode(ErrorDescription::NotFound, ErrorModule::FS,
  229. ErrorSummary::NotFound, ErrorLevel::Permanent);
  230. }
  231. return MakeResult<Handle>(itr->second);
  232. }
  233. ResultCode CloseArchive(FileSys::Archive::IdCode id_code) {
  234. auto itr = g_archive_map.find(id_code);
  235. if (itr == g_archive_map.end()) {
  236. ERROR_LOG(KERNEL, "Cannot close archive %d, does not exist!", (int)id_code);
  237. return InvalidHandle(ErrorModule::FS);
  238. }
  239. INFO_LOG(KERNEL, "Closed archive %d", (int) id_code);
  240. return RESULT_SUCCESS;
  241. }
  242. /**
  243. * Mounts an archive
  244. * @param archive Pointer to the archive to mount
  245. */
  246. ResultCode MountArchive(Archive* archive) {
  247. FileSys::Archive::IdCode id_code = archive->backend->GetIdCode();
  248. ResultVal<Handle> archive_handle = OpenArchive(id_code);
  249. if (archive_handle.Succeeded()) {
  250. ERROR_LOG(KERNEL, "Cannot mount two archives with the same ID code! (%d)", (int) id_code);
  251. return archive_handle.Code();
  252. }
  253. g_archive_map[id_code] = archive->GetHandle();
  254. INFO_LOG(KERNEL, "Mounted archive %s", archive->GetName().c_str());
  255. return RESULT_SUCCESS;
  256. }
  257. ResultCode CreateArchive(FileSys::Archive* backend, const std::string& name) {
  258. Archive* archive = new Archive;
  259. Handle handle = Kernel::g_object_pool.Create(archive);
  260. archive->name = name;
  261. archive->backend = backend;
  262. ResultCode result = MountArchive(archive);
  263. if (result.IsError()) {
  264. return result;
  265. }
  266. return RESULT_SUCCESS;
  267. }
  268. ResultVal<Handle> OpenFileFromArchive(Handle archive_handle, const FileSys::Path& path, const FileSys::Mode mode) {
  269. // TODO(bunnei): Binary type files get a raw file pointer to the archive. Currently, we create
  270. // the archive file handles at app loading, and then keep them persistent throughout execution.
  271. // Archives file handles are just reused and not actually freed until emulation shut down.
  272. // Verify if real hardware works this way, or if new handles are created each time
  273. if (path.GetType() == FileSys::Binary)
  274. // TODO(bunnei): FixMe - this is a hack to compensate for an incorrect FileSys backend
  275. // design. While the functionally of this is OK, our implementation decision to separate
  276. // normal files from archive file pointers is very likely wrong.
  277. // See https://github.com/citra-emu/citra/issues/205
  278. return MakeResult<Handle>(archive_handle);
  279. File* file = new File;
  280. Handle handle = Kernel::g_object_pool.Create(file);
  281. Archive* archive = Kernel::g_object_pool.Get<Archive>(archive_handle);
  282. if (archive == nullptr) {
  283. return InvalidHandle(ErrorModule::FS);
  284. }
  285. file->path = path;
  286. file->backend = archive->backend->OpenFile(path, mode);
  287. if (!file->backend) {
  288. return ResultCode(ErrorDescription::NotFound, ErrorModule::FS,
  289. ErrorSummary::NotFound, ErrorLevel::Permanent);
  290. }
  291. return MakeResult<Handle>(handle);
  292. }
  293. /**
  294. * Delete a File from an Archive
  295. * @param archive_handle Handle to an open Archive object
  296. * @param path Path to the File inside of the Archive
  297. * @return Whether deletion succeeded
  298. */
  299. Result DeleteFileFromArchive(Handle archive_handle, const FileSys::Path& path) {
  300. Archive* archive = Kernel::g_object_pool.GetFast<Archive>(archive_handle);
  301. if (archive == nullptr)
  302. return -1;
  303. if (archive->backend->DeleteFile(path))
  304. return 0;
  305. return -1;
  306. }
  307. /**
  308. * Rename a File between two Archives
  309. * @param src_archive_handle Handle to the source Archive object
  310. * @param src_path Path to the File inside of the source Archive
  311. * @param dest_archive_handle Handle to the destination Archive object
  312. * @param dest_path Path to the File inside of the destination Archive
  313. * @return Whether rename succeeded
  314. */
  315. Result RenameFileBetweenArchives(Handle src_archive_handle, const FileSys::Path& src_path,
  316. Handle dest_archive_handle, const FileSys::Path& dest_path) {
  317. Archive* src_archive = Kernel::g_object_pool.GetFast<Archive>(src_archive_handle);
  318. Archive* dest_archive = Kernel::g_object_pool.GetFast<Archive>(dest_archive_handle);
  319. if (src_archive == nullptr || dest_archive == nullptr)
  320. return -1;
  321. if (src_archive == dest_archive) {
  322. if (src_archive->backend->RenameFile(src_path, dest_path))
  323. return 0;
  324. } else {
  325. // TODO: Implement renaming across archives
  326. return -1;
  327. }
  328. return -1;
  329. }
  330. /**
  331. * Delete a Directory from an Archive
  332. * @param archive_handle Handle to an open Archive object
  333. * @param path Path to the Directory inside of the Archive
  334. * @return Whether deletion succeeded
  335. */
  336. Result DeleteDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path) {
  337. Archive* archive = Kernel::g_object_pool.GetFast<Archive>(archive_handle);
  338. if (archive == nullptr)
  339. return -1;
  340. if (archive->backend->DeleteDirectory(path))
  341. return 0;
  342. return -1;
  343. }
  344. /**
  345. * Create a Directory from an Archive
  346. * @param archive_handle Handle to an open Archive object
  347. * @param path Path to the Directory inside of the Archive
  348. * @return Whether creation succeeded
  349. */
  350. Result CreateDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path) {
  351. Archive* archive = Kernel::g_object_pool.GetFast<Archive>(archive_handle);
  352. if (archive == nullptr)
  353. return -1;
  354. if (archive->backend->CreateDirectory(path))
  355. return 0;
  356. return -1;
  357. }
  358. /**
  359. * Rename a Directory between two Archives
  360. * @param src_archive_handle Handle to the source Archive object
  361. * @param src_path Path to the Directory inside of the source Archive
  362. * @param dest_archive_handle Handle to the destination Archive object
  363. * @param dest_path Path to the Directory inside of the destination Archive
  364. * @return Whether rename succeeded
  365. */
  366. Result RenameDirectoryBetweenArchives(Handle src_archive_handle, const FileSys::Path& src_path,
  367. Handle dest_archive_handle, const FileSys::Path& dest_path) {
  368. Archive* src_archive = Kernel::g_object_pool.GetFast<Archive>(src_archive_handle);
  369. Archive* dest_archive = Kernel::g_object_pool.GetFast<Archive>(dest_archive_handle);
  370. if (src_archive == nullptr || dest_archive == nullptr)
  371. return -1;
  372. if (src_archive == dest_archive) {
  373. if (src_archive->backend->RenameDirectory(src_path, dest_path))
  374. return 0;
  375. } else {
  376. // TODO: Implement renaming across archives
  377. return -1;
  378. }
  379. return -1;
  380. }
  381. /**
  382. * Open a Directory from an Archive
  383. * @param archive_handle Handle to an open Archive object
  384. * @param path Path to the Directory inside of the Archive
  385. * @return Opened Directory object
  386. */
  387. ResultVal<Handle> OpenDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path) {
  388. Directory* directory = new Directory;
  389. Handle handle = Kernel::g_object_pool.Create(directory);
  390. Archive* archive = Kernel::g_object_pool.Get<Archive>(archive_handle);
  391. if (archive == nullptr) {
  392. return InvalidHandle(ErrorModule::FS);
  393. }
  394. directory->path = path;
  395. directory->backend = archive->backend->OpenDirectory(path);
  396. return MakeResult<Handle>(handle);
  397. }
  398. /// Initialize archives
  399. void ArchiveInit() {
  400. g_archive_map.clear();
  401. // TODO(Link Mauve): Add the other archive types (see here for the known types:
  402. // http://3dbrew.org/wiki/FS:OpenArchive#Archive_idcodes). Currently the only half-finished
  403. // archive type is SDMC, so it is the only one getting exposed.
  404. std::string sdmc_directory = FileUtil::GetUserPath(D_SDMC_IDX);
  405. auto archive = new FileSys::Archive_SDMC(sdmc_directory);
  406. if (archive->Initialize())
  407. CreateArchive(archive, "SDMC");
  408. else
  409. ERROR_LOG(KERNEL, "Can't instantiate SDMC archive with path %s", sdmc_directory.c_str());
  410. }
  411. /// Shutdown archives
  412. void ArchiveShutdown() {
  413. g_archive_map.clear();
  414. }
  415. } // namespace Kernel