fsp_srv.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cinttypes>
  5. #include "common/logging/log.h"
  6. #include "core/core.h"
  7. #include "core/file_sys/directory.h"
  8. #include "core/file_sys/filesystem.h"
  9. #include "core/file_sys/storage.h"
  10. #include "core/hle/ipc_helpers.h"
  11. #include "core/hle/kernel/client_port.h"
  12. #include "core/hle/kernel/client_session.h"
  13. #include "core/hle/service/filesystem/filesystem.h"
  14. #include "core/hle/service/filesystem/fsp_srv.h"
  15. namespace Service {
  16. namespace FileSystem {
  17. class IStorage final : public ServiceFramework<IStorage> {
  18. public:
  19. IStorage(std::unique_ptr<FileSys::StorageBackend>&& backend)
  20. : ServiceFramework("IStorage"), backend(std::move(backend)) {
  21. static const FunctionInfo functions[] = {
  22. {0, &IStorage::Read, "Read"}, {1, nullptr, "Write"}, {2, nullptr, "Flush"},
  23. {3, nullptr, "SetSize"}, {4, nullptr, "GetSize"},
  24. };
  25. RegisterHandlers(functions);
  26. }
  27. private:
  28. std::unique_ptr<FileSys::StorageBackend> backend;
  29. void Read(Kernel::HLERequestContext& ctx) {
  30. IPC::RequestParser rp{ctx};
  31. const s64 offset = rp.Pop<s64>();
  32. const s64 length = rp.Pop<s64>();
  33. LOG_DEBUG(Service_FS, "called, offset=0x%llx, length=0x%llx", offset, length);
  34. // Error checking
  35. if (length < 0) {
  36. IPC::ResponseBuilder rb{ctx, 2};
  37. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidLength));
  38. return;
  39. }
  40. if (offset < 0) {
  41. IPC::ResponseBuilder rb{ctx, 2};
  42. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidOffset));
  43. return;
  44. }
  45. // Read the data from the Storage backend
  46. std::vector<u8> output(length);
  47. ResultVal<size_t> res = backend->Read(offset, length, output.data());
  48. if (res.Failed()) {
  49. IPC::ResponseBuilder rb{ctx, 2};
  50. rb.Push(res.Code());
  51. return;
  52. }
  53. // Write the data to memory
  54. ctx.WriteBuffer(output);
  55. IPC::ResponseBuilder rb{ctx, 2};
  56. rb.Push(RESULT_SUCCESS);
  57. }
  58. };
  59. class IFile final : public ServiceFramework<IFile> {
  60. public:
  61. explicit IFile(std::unique_ptr<FileSys::StorageBackend>&& backend)
  62. : ServiceFramework("IFile"), backend(std::move(backend)) {
  63. static const FunctionInfo functions[] = {
  64. {0, &IFile::Read, "Read"}, {1, &IFile::Write, "Write"}, {2, nullptr, "Flush"},
  65. {3, nullptr, "SetSize"}, {4, nullptr, "GetSize"},
  66. };
  67. RegisterHandlers(functions);
  68. }
  69. private:
  70. std::unique_ptr<FileSys::StorageBackend> backend;
  71. void Read(Kernel::HLERequestContext& ctx) {
  72. IPC::RequestParser rp{ctx};
  73. const u64 unk = rp.Pop<u64>();
  74. const s64 offset = rp.Pop<s64>();
  75. const s64 length = rp.Pop<s64>();
  76. LOG_DEBUG(Service_FS, "called, offset=0x%llx, length=0x%llx", offset, length);
  77. // Error checking
  78. if (length < 0) {
  79. IPC::ResponseBuilder rb{ctx, 2};
  80. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidLength));
  81. return;
  82. }
  83. if (offset < 0) {
  84. IPC::ResponseBuilder rb{ctx, 2};
  85. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidOffset));
  86. return;
  87. }
  88. // Read the data from the Storage backend
  89. std::vector<u8> output(length);
  90. ResultVal<size_t> res = backend->Read(offset, length, output.data());
  91. if (res.Failed()) {
  92. IPC::ResponseBuilder rb{ctx, 2};
  93. rb.Push(res.Code());
  94. return;
  95. }
  96. // Write the data to memory
  97. ctx.WriteBuffer(output);
  98. IPC::ResponseBuilder rb{ctx, 4};
  99. rb.Push(RESULT_SUCCESS);
  100. rb.Push(static_cast<u64>(*res));
  101. }
  102. void Write(Kernel::HLERequestContext& ctx) {
  103. IPC::RequestParser rp{ctx};
  104. const u64 unk = rp.Pop<u64>();
  105. const s64 offset = rp.Pop<s64>();
  106. const s64 length = rp.Pop<s64>();
  107. LOG_DEBUG(Service_FS, "called, offset=0x%llx, length=0x%llx", offset, length);
  108. // Error checking
  109. if (length < 0) {
  110. IPC::ResponseBuilder rb{ctx, 2};
  111. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidLength));
  112. return;
  113. }
  114. if (offset < 0) {
  115. IPC::ResponseBuilder rb{ctx, 2};
  116. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidOffset));
  117. return;
  118. }
  119. // Write the data to the Storage backend
  120. std::vector<u8> data = ctx.ReadBuffer();
  121. ResultVal<size_t> res = backend->Write(offset, length, true, data.data());
  122. if (res.Failed()) {
  123. IPC::ResponseBuilder rb{ctx, 2};
  124. rb.Push(res.Code());
  125. return;
  126. }
  127. IPC::ResponseBuilder rb{ctx, 2};
  128. rb.Push(RESULT_SUCCESS);
  129. }
  130. };
  131. class IDirectory final : public ServiceFramework<IDirectory> {
  132. public:
  133. explicit IDirectory(std::unique_ptr<FileSys::DirectoryBackend>&& backend)
  134. : ServiceFramework("IDirectory"), backend(std::move(backend)) {
  135. static const FunctionInfo functions[] = {
  136. {0, &IDirectory::Read, "Read"},
  137. {1, &IDirectory::GetEntryCount, "GetEntryCount"},
  138. };
  139. RegisterHandlers(functions);
  140. }
  141. private:
  142. std::unique_ptr<FileSys::DirectoryBackend> backend;
  143. void Read(Kernel::HLERequestContext& ctx) {
  144. IPC::RequestParser rp{ctx};
  145. const u64 unk = rp.Pop<u64>();
  146. LOG_DEBUG(Service_FS, "called, unk=0x%llx", unk);
  147. // Calculate how many entries we can fit in the output buffer
  148. u64 count_entries = ctx.GetWriteBufferSize() / sizeof(FileSys::Entry);
  149. // Read the data from the Directory backend
  150. std::vector<FileSys::Entry> entries(count_entries);
  151. u64 read_entries = backend->Read(count_entries, entries.data());
  152. // Convert the data into a byte array
  153. std::vector<u8> output(entries.size() * sizeof(FileSys::Entry));
  154. std::memcpy(output.data(), entries.data(), output.size());
  155. // Write the data to memory
  156. ctx.WriteBuffer(output);
  157. IPC::ResponseBuilder rb{ctx, 4};
  158. rb.Push(RESULT_SUCCESS);
  159. rb.Push(read_entries);
  160. }
  161. void GetEntryCount(Kernel::HLERequestContext& ctx) {
  162. LOG_DEBUG(Service_FS, "called");
  163. u64 count = backend->GetEntryCount();
  164. IPC::ResponseBuilder rb{ctx, 4};
  165. rb.Push(RESULT_SUCCESS);
  166. rb.Push(count);
  167. }
  168. };
  169. class IFileSystem final : public ServiceFramework<IFileSystem> {
  170. public:
  171. explicit IFileSystem(std::unique_ptr<FileSys::FileSystemBackend>&& backend)
  172. : ServiceFramework("IFileSystem"), backend(std::move(backend)) {
  173. static const FunctionInfo functions[] = {
  174. {0, &IFileSystem::CreateFile, "CreateFile"},
  175. {7, &IFileSystem::GetEntryType, "GetEntryType"},
  176. {8, &IFileSystem::OpenFile, "OpenFile"},
  177. {9, &IFileSystem::OpenDirectory, "OpenDirectory"},
  178. {10, &IFileSystem::Commit, "Commit"},
  179. };
  180. RegisterHandlers(functions);
  181. }
  182. void CreateFile(Kernel::HLERequestContext& ctx) {
  183. IPC::RequestParser rp{ctx};
  184. auto file_buffer = ctx.ReadBuffer();
  185. auto end = std::find(file_buffer.begin(), file_buffer.end(), '\0');
  186. std::string name(file_buffer.begin(), end);
  187. u64 mode = rp.Pop<u64>();
  188. u32 size = rp.Pop<u32>();
  189. LOG_DEBUG(Service_FS, "called file %s mode 0x%" PRIX64 " size 0x%08X", name.c_str(), mode,
  190. size);
  191. IPC::ResponseBuilder rb{ctx, 2};
  192. rb.Push(backend->CreateFile(name, size));
  193. }
  194. void OpenFile(Kernel::HLERequestContext& ctx) {
  195. IPC::RequestParser rp{ctx};
  196. auto file_buffer = ctx.ReadBuffer();
  197. auto end = std::find(file_buffer.begin(), file_buffer.end(), '\0');
  198. std::string name(file_buffer.begin(), end);
  199. auto mode = static_cast<FileSys::Mode>(rp.Pop<u32>());
  200. LOG_DEBUG(Service_FS, "called file %s mode %u", name.c_str(), static_cast<u32>(mode));
  201. auto result = backend->OpenFile(name, mode);
  202. if (result.Failed()) {
  203. IPC::ResponseBuilder rb{ctx, 2};
  204. rb.Push(result.Code());
  205. return;
  206. }
  207. auto file = std::move(result.Unwrap());
  208. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  209. rb.Push(RESULT_SUCCESS);
  210. rb.PushIpcInterface<IFile>(std::move(file));
  211. }
  212. void OpenDirectory(Kernel::HLERequestContext& ctx) {
  213. IPC::RequestParser rp{ctx};
  214. auto file_buffer = ctx.ReadBuffer();
  215. auto end = std::find(file_buffer.begin(), file_buffer.end(), '\0');
  216. std::string name(file_buffer.begin(), end);
  217. // TODO(Subv): Implement this filter.
  218. u32 filter_flags = rp.Pop<u32>();
  219. LOG_DEBUG(Service_FS, "called directory %s filter %u", name.c_str(), filter_flags);
  220. auto result = backend->OpenDirectory(name);
  221. if (result.Failed()) {
  222. IPC::ResponseBuilder rb{ctx, 2};
  223. rb.Push(result.Code());
  224. return;
  225. }
  226. auto directory = std::move(result.Unwrap());
  227. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  228. rb.Push(RESULT_SUCCESS);
  229. rb.PushIpcInterface<IDirectory>(std::move(directory));
  230. }
  231. void GetEntryType(Kernel::HLERequestContext& ctx) {
  232. IPC::RequestParser rp{ctx};
  233. auto file_buffer = ctx.ReadBuffer();
  234. auto end = std::find(file_buffer.begin(), file_buffer.end(), '\0');
  235. std::string name(file_buffer.begin(), end);
  236. LOG_DEBUG(Service_FS, "called file %s", name.c_str());
  237. auto result = backend->GetEntryType(name);
  238. if (result.Failed()) {
  239. IPC::ResponseBuilder rb{ctx, 2};
  240. rb.Push(result.Code());
  241. return;
  242. }
  243. IPC::ResponseBuilder rb{ctx, 3};
  244. rb.Push(RESULT_SUCCESS);
  245. rb.Push<u32>(static_cast<u32>(*result));
  246. }
  247. void Commit(Kernel::HLERequestContext& ctx) {
  248. LOG_WARNING(Service_FS, "(STUBBED) called");
  249. IPC::ResponseBuilder rb{ctx, 2};
  250. rb.Push(RESULT_SUCCESS);
  251. }
  252. private:
  253. std::unique_ptr<FileSys::FileSystemBackend> backend;
  254. };
  255. FSP_SRV::FSP_SRV() : ServiceFramework("fsp-srv") {
  256. static const FunctionInfo functions[] = {
  257. {1, &FSP_SRV::Initalize, "Initalize"},
  258. {18, &FSP_SRV::MountSdCard, "MountSdCard"},
  259. {22, &FSP_SRV::CreateSaveData, "CreateSaveData"},
  260. {51, &FSP_SRV::MountSaveData, "MountSaveData"},
  261. {200, &FSP_SRV::OpenDataStorageByCurrentProcess, "OpenDataStorageByCurrentProcess"},
  262. {202, nullptr, "OpenDataStorageByDataId"},
  263. {203, &FSP_SRV::OpenRomStorage, "OpenRomStorage"},
  264. {1005, &FSP_SRV::GetGlobalAccessLogMode, "GetGlobalAccessLogMode"},
  265. };
  266. RegisterHandlers(functions);
  267. }
  268. void FSP_SRV::TryLoadRomFS() {
  269. if (romfs) {
  270. return;
  271. }
  272. FileSys::Path unused;
  273. auto res = OpenFileSystem(Type::RomFS, unused);
  274. if (res.Succeeded()) {
  275. romfs = std::move(res.Unwrap());
  276. }
  277. }
  278. void FSP_SRV::Initalize(Kernel::HLERequestContext& ctx) {
  279. LOG_WARNING(Service_FS, "(STUBBED) called");
  280. IPC::ResponseBuilder rb{ctx, 2};
  281. rb.Push(RESULT_SUCCESS);
  282. }
  283. void FSP_SRV::MountSdCard(Kernel::HLERequestContext& ctx) {
  284. LOG_DEBUG(Service_FS, "called");
  285. FileSys::Path unused;
  286. auto filesystem = OpenFileSystem(Type::SDMC, unused).Unwrap();
  287. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  288. rb.Push(RESULT_SUCCESS);
  289. rb.PushIpcInterface<IFileSystem>(std::move(filesystem));
  290. }
  291. void FSP_SRV::CreateSaveData(Kernel::HLERequestContext& ctx) {
  292. IPC::RequestParser rp{ctx};
  293. auto save_struct = rp.PopRaw<std::array<u8, 0x40>>();
  294. auto save_create_struct = rp.PopRaw<std::array<u8, 0x40>>();
  295. u128 uid = rp.PopRaw<u128>();
  296. LOG_WARNING(Service_FS, "(STUBBED) called uid = %016" PRIX64 "%016" PRIX64, uid[1], uid[0]);
  297. IPC::ResponseBuilder rb{ctx, 2};
  298. rb.Push(RESULT_SUCCESS);
  299. }
  300. void FSP_SRV::MountSaveData(Kernel::HLERequestContext& ctx) {
  301. LOG_WARNING(Service_FS, "(STUBBED) called");
  302. FileSys::Path unused;
  303. auto filesystem = OpenFileSystem(Type::SaveData, unused).Unwrap();
  304. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  305. rb.Push(RESULT_SUCCESS);
  306. rb.PushIpcInterface<IFileSystem>(std::move(filesystem));
  307. }
  308. void FSP_SRV::GetGlobalAccessLogMode(Kernel::HLERequestContext& ctx) {
  309. LOG_WARNING(Service_FS, "(STUBBED) called");
  310. IPC::ResponseBuilder rb{ctx, 3};
  311. rb.Push(RESULT_SUCCESS);
  312. rb.Push<u32>(5);
  313. }
  314. void FSP_SRV::OpenDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx) {
  315. LOG_DEBUG(Service_FS, "called");
  316. TryLoadRomFS();
  317. if (!romfs) {
  318. // TODO (bunnei): Find the right error code to use here
  319. LOG_CRITICAL(Service_FS, "no file system interface available!");
  320. IPC::ResponseBuilder rb{ctx, 2};
  321. rb.Push(ResultCode(-1));
  322. return;
  323. }
  324. // Attempt to open a StorageBackend interface to the RomFS
  325. auto storage = romfs->OpenFile({}, {});
  326. if (storage.Failed()) {
  327. LOG_CRITICAL(Service_FS, "no storage interface available!");
  328. IPC::ResponseBuilder rb{ctx, 2};
  329. rb.Push(storage.Code());
  330. return;
  331. }
  332. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  333. rb.Push(RESULT_SUCCESS);
  334. rb.PushIpcInterface<IStorage>(std::move(storage.Unwrap()));
  335. }
  336. void FSP_SRV::OpenRomStorage(Kernel::HLERequestContext& ctx) {
  337. LOG_WARNING(Service_FS, "(STUBBED) called, using OpenDataStorageByCurrentProcess");
  338. OpenDataStorageByCurrentProcess(ctx);
  339. }
  340. } // namespace FileSystem
  341. } // namespace Service