fsp_srv.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 <cstring>
  6. #include <iterator>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "common/assert.h"
  11. #include "common/common_types.h"
  12. #include "common/logging/log.h"
  13. #include "common/string_util.h"
  14. #include "core/file_sys/directory.h"
  15. #include "core/file_sys/errors.h"
  16. #include "core/file_sys/mode.h"
  17. #include "core/file_sys/nca_metadata.h"
  18. #include "core/file_sys/patch_manager.h"
  19. #include "core/file_sys/savedata_factory.h"
  20. #include "core/file_sys/vfs.h"
  21. #include "core/hle/ipc_helpers.h"
  22. #include "core/hle/kernel/process.h"
  23. #include "core/hle/service/filesystem/filesystem.h"
  24. #include "core/hle/service/filesystem/fsp_srv.h"
  25. namespace Service::FileSystem {
  26. enum class FileSystemType : u8 {
  27. Invalid0 = 0,
  28. Invalid1 = 1,
  29. Logo = 2,
  30. ContentControl = 3,
  31. ContentManual = 4,
  32. ContentMeta = 5,
  33. ContentData = 6,
  34. ApplicationPackage = 7,
  35. };
  36. class IStorage final : public ServiceFramework<IStorage> {
  37. public:
  38. explicit IStorage(FileSys::VirtualFile backend_)
  39. : ServiceFramework("IStorage"), backend(std::move(backend_)) {
  40. static const FunctionInfo functions[] = {
  41. {0, &IStorage::Read, "Read"}, {1, nullptr, "Write"}, {2, nullptr, "Flush"},
  42. {3, nullptr, "SetSize"}, {4, nullptr, "GetSize"}, {5, nullptr, "OperateRange"},
  43. };
  44. RegisterHandlers(functions);
  45. }
  46. private:
  47. FileSys::VirtualFile backend;
  48. void Read(Kernel::HLERequestContext& ctx) {
  49. IPC::RequestParser rp{ctx};
  50. const s64 offset = rp.Pop<s64>();
  51. const s64 length = rp.Pop<s64>();
  52. LOG_DEBUG(Service_FS, "called, offset=0x{:X}, length={}", offset, length);
  53. // Error checking
  54. if (length < 0) {
  55. IPC::ResponseBuilder rb{ctx, 2};
  56. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidLength));
  57. return;
  58. }
  59. if (offset < 0) {
  60. IPC::ResponseBuilder rb{ctx, 2};
  61. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidOffset));
  62. return;
  63. }
  64. // Read the data from the Storage backend
  65. std::vector<u8> output = backend->ReadBytes(length, offset);
  66. // Write the data to memory
  67. ctx.WriteBuffer(output);
  68. IPC::ResponseBuilder rb{ctx, 2};
  69. rb.Push(RESULT_SUCCESS);
  70. }
  71. };
  72. class IFile final : public ServiceFramework<IFile> {
  73. public:
  74. explicit IFile(FileSys::VirtualFile backend_)
  75. : ServiceFramework("IFile"), backend(std::move(backend_)) {
  76. static const FunctionInfo functions[] = {
  77. {0, &IFile::Read, "Read"}, {1, &IFile::Write, "Write"},
  78. {2, &IFile::Flush, "Flush"}, {3, &IFile::SetSize, "SetSize"},
  79. {4, &IFile::GetSize, "GetSize"}, {5, nullptr, "OperateRange"},
  80. };
  81. RegisterHandlers(functions);
  82. }
  83. private:
  84. FileSys::VirtualFile backend;
  85. void Read(Kernel::HLERequestContext& ctx) {
  86. IPC::RequestParser rp{ctx};
  87. const u64 unk = rp.Pop<u64>();
  88. const s64 offset = rp.Pop<s64>();
  89. const s64 length = rp.Pop<s64>();
  90. LOG_DEBUG(Service_FS, "called, offset=0x{:X}, length={}", offset, length);
  91. // Error checking
  92. if (length < 0) {
  93. IPC::ResponseBuilder rb{ctx, 2};
  94. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidLength));
  95. return;
  96. }
  97. if (offset < 0) {
  98. IPC::ResponseBuilder rb{ctx, 2};
  99. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidOffset));
  100. return;
  101. }
  102. // Read the data from the Storage backend
  103. std::vector<u8> output = backend->ReadBytes(length, offset);
  104. // Write the data to memory
  105. ctx.WriteBuffer(output);
  106. IPC::ResponseBuilder rb{ctx, 4};
  107. rb.Push(RESULT_SUCCESS);
  108. rb.Push(static_cast<u64>(output.size()));
  109. }
  110. void Write(Kernel::HLERequestContext& ctx) {
  111. IPC::RequestParser rp{ctx};
  112. const u64 unk = rp.Pop<u64>();
  113. const s64 offset = rp.Pop<s64>();
  114. const s64 length = rp.Pop<s64>();
  115. LOG_DEBUG(Service_FS, "called, offset=0x{:X}, length={}", offset, length);
  116. // Error checking
  117. if (length < 0) {
  118. IPC::ResponseBuilder rb{ctx, 2};
  119. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidLength));
  120. return;
  121. }
  122. if (offset < 0) {
  123. IPC::ResponseBuilder rb{ctx, 2};
  124. rb.Push(ResultCode(ErrorModule::FS, ErrorDescription::InvalidOffset));
  125. return;
  126. }
  127. const std::vector<u8> data = ctx.ReadBuffer();
  128. ASSERT_MSG(
  129. static_cast<s64>(data.size()) <= length,
  130. "Attempting to write more data than requested (requested={:016X}, actual={:016X}).",
  131. length, data.size());
  132. // Write the data to the Storage backend
  133. const auto write_size =
  134. static_cast<std::size_t>(std::distance(data.begin(), data.begin() + length));
  135. const std::size_t written = backend->Write(data.data(), write_size, offset);
  136. ASSERT_MSG(static_cast<s64>(written) == length,
  137. "Could not write all bytes to file (requested={:016X}, actual={:016X}).", length,
  138. written);
  139. IPC::ResponseBuilder rb{ctx, 2};
  140. rb.Push(RESULT_SUCCESS);
  141. }
  142. void Flush(Kernel::HLERequestContext& ctx) {
  143. LOG_DEBUG(Service_FS, "called");
  144. // Exists for SDK compatibiltity -- No need to flush file.
  145. IPC::ResponseBuilder rb{ctx, 2};
  146. rb.Push(RESULT_SUCCESS);
  147. }
  148. void SetSize(Kernel::HLERequestContext& ctx) {
  149. IPC::RequestParser rp{ctx};
  150. const u64 size = rp.Pop<u64>();
  151. backend->Resize(size);
  152. LOG_DEBUG(Service_FS, "called, size={}", size);
  153. IPC::ResponseBuilder rb{ctx, 2};
  154. rb.Push(RESULT_SUCCESS);
  155. }
  156. void GetSize(Kernel::HLERequestContext& ctx) {
  157. const u64 size = backend->GetSize();
  158. LOG_DEBUG(Service_FS, "called, size={}", size);
  159. IPC::ResponseBuilder rb{ctx, 4};
  160. rb.Push(RESULT_SUCCESS);
  161. rb.Push<u64>(size);
  162. }
  163. };
  164. template <typename T>
  165. static void BuildEntryIndex(std::vector<FileSys::Entry>& entries, const std::vector<T>& new_data,
  166. FileSys::EntryType type) {
  167. entries.reserve(entries.size() + new_data.size());
  168. for (const auto& new_entry : new_data) {
  169. entries.emplace_back(new_entry->GetName(), type, new_entry->GetSize());
  170. }
  171. }
  172. class IDirectory final : public ServiceFramework<IDirectory> {
  173. public:
  174. explicit IDirectory(FileSys::VirtualDir backend_)
  175. : ServiceFramework("IDirectory"), backend(std::move(backend_)) {
  176. static const FunctionInfo functions[] = {
  177. {0, &IDirectory::Read, "Read"},
  178. {1, &IDirectory::GetEntryCount, "GetEntryCount"},
  179. };
  180. RegisterHandlers(functions);
  181. // TODO(DarkLordZach): Verify that this is the correct behavior.
  182. // Build entry index now to save time later.
  183. BuildEntryIndex(entries, backend->GetFiles(), FileSys::File);
  184. BuildEntryIndex(entries, backend->GetSubdirectories(), FileSys::Directory);
  185. }
  186. private:
  187. FileSys::VirtualDir backend;
  188. std::vector<FileSys::Entry> entries;
  189. u64 next_entry_index = 0;
  190. void Read(Kernel::HLERequestContext& ctx) {
  191. IPC::RequestParser rp{ctx};
  192. const u64 unk = rp.Pop<u64>();
  193. LOG_DEBUG(Service_FS, "called, unk=0x{:X}", unk);
  194. // Calculate how many entries we can fit in the output buffer
  195. const u64 count_entries = ctx.GetWriteBufferSize() / sizeof(FileSys::Entry);
  196. // Cap at total number of entries.
  197. const u64 actual_entries = std::min(count_entries, entries.size() - next_entry_index);
  198. // Determine data start and end
  199. const auto* begin = reinterpret_cast<u8*>(entries.data() + next_entry_index);
  200. const auto* end = reinterpret_cast<u8*>(entries.data() + next_entry_index + actual_entries);
  201. const auto range_size = static_cast<std::size_t>(std::distance(begin, end));
  202. next_entry_index += actual_entries;
  203. // Write the data to memory
  204. ctx.WriteBuffer(begin, range_size);
  205. IPC::ResponseBuilder rb{ctx, 4};
  206. rb.Push(RESULT_SUCCESS);
  207. rb.Push(actual_entries);
  208. }
  209. void GetEntryCount(Kernel::HLERequestContext& ctx) {
  210. LOG_DEBUG(Service_FS, "called");
  211. u64 count = entries.size() - next_entry_index;
  212. IPC::ResponseBuilder rb{ctx, 4};
  213. rb.Push(RESULT_SUCCESS);
  214. rb.Push(count);
  215. }
  216. };
  217. class IFileSystem final : public ServiceFramework<IFileSystem> {
  218. public:
  219. explicit IFileSystem(FileSys::VirtualDir backend)
  220. : ServiceFramework("IFileSystem"), backend(std::move(backend)) {
  221. static const FunctionInfo functions[] = {
  222. {0, &IFileSystem::CreateFile, "CreateFile"},
  223. {1, &IFileSystem::DeleteFile, "DeleteFile"},
  224. {2, &IFileSystem::CreateDirectory, "CreateDirectory"},
  225. {3, nullptr, "DeleteDirectory"},
  226. {4, nullptr, "DeleteDirectoryRecursively"},
  227. {5, &IFileSystem::RenameFile, "RenameFile"},
  228. {6, nullptr, "RenameDirectory"},
  229. {7, &IFileSystem::GetEntryType, "GetEntryType"},
  230. {8, &IFileSystem::OpenFile, "OpenFile"},
  231. {9, &IFileSystem::OpenDirectory, "OpenDirectory"},
  232. {10, &IFileSystem::Commit, "Commit"},
  233. {11, nullptr, "GetFreeSpaceSize"},
  234. {12, nullptr, "GetTotalSpaceSize"},
  235. {13, nullptr, "CleanDirectoryRecursively"},
  236. {14, nullptr, "GetFileTimeStampRaw"},
  237. {15, nullptr, "QueryEntry"},
  238. };
  239. RegisterHandlers(functions);
  240. }
  241. void CreateFile(Kernel::HLERequestContext& ctx) {
  242. IPC::RequestParser rp{ctx};
  243. auto file_buffer = ctx.ReadBuffer();
  244. std::string name = Common::StringFromBuffer(file_buffer);
  245. u64 mode = rp.Pop<u64>();
  246. u32 size = rp.Pop<u32>();
  247. LOG_DEBUG(Service_FS, "called file {} mode 0x{:X} size 0x{:08X}", name, mode, size);
  248. IPC::ResponseBuilder rb{ctx, 2};
  249. rb.Push(backend.CreateFile(name, size));
  250. }
  251. void DeleteFile(Kernel::HLERequestContext& ctx) {
  252. IPC::RequestParser rp{ctx};
  253. auto file_buffer = ctx.ReadBuffer();
  254. std::string name = Common::StringFromBuffer(file_buffer);
  255. LOG_DEBUG(Service_FS, "called file {}", name);
  256. IPC::ResponseBuilder rb{ctx, 2};
  257. rb.Push(backend.DeleteFile(name));
  258. }
  259. void CreateDirectory(Kernel::HLERequestContext& ctx) {
  260. IPC::RequestParser rp{ctx};
  261. auto file_buffer = ctx.ReadBuffer();
  262. std::string name = Common::StringFromBuffer(file_buffer);
  263. LOG_DEBUG(Service_FS, "called directory {}", name);
  264. IPC::ResponseBuilder rb{ctx, 2};
  265. rb.Push(backend.CreateDirectory(name));
  266. }
  267. void RenameFile(Kernel::HLERequestContext& ctx) {
  268. IPC::RequestParser rp{ctx};
  269. std::vector<u8> buffer;
  270. buffer.resize(ctx.BufferDescriptorX()[0].Size());
  271. Memory::ReadBlock(ctx.BufferDescriptorX()[0].Address(), buffer.data(), buffer.size());
  272. std::string src_name = Common::StringFromBuffer(buffer);
  273. buffer.resize(ctx.BufferDescriptorX()[1].Size());
  274. Memory::ReadBlock(ctx.BufferDescriptorX()[1].Address(), buffer.data(), buffer.size());
  275. std::string dst_name = Common::StringFromBuffer(buffer);
  276. LOG_DEBUG(Service_FS, "called file '{}' to file '{}'", src_name, dst_name);
  277. IPC::ResponseBuilder rb{ctx, 2};
  278. rb.Push(backend.RenameFile(src_name, dst_name));
  279. }
  280. void OpenFile(Kernel::HLERequestContext& ctx) {
  281. IPC::RequestParser rp{ctx};
  282. auto file_buffer = ctx.ReadBuffer();
  283. std::string name = Common::StringFromBuffer(file_buffer);
  284. auto mode = static_cast<FileSys::Mode>(rp.Pop<u32>());
  285. LOG_DEBUG(Service_FS, "called file {} mode {}", name, static_cast<u32>(mode));
  286. auto result = backend.OpenFile(name, mode);
  287. if (result.Failed()) {
  288. IPC::ResponseBuilder rb{ctx, 2};
  289. rb.Push(result.Code());
  290. return;
  291. }
  292. IFile file(result.Unwrap());
  293. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  294. rb.Push(RESULT_SUCCESS);
  295. rb.PushIpcInterface<IFile>(std::move(file));
  296. }
  297. void OpenDirectory(Kernel::HLERequestContext& ctx) {
  298. IPC::RequestParser rp{ctx};
  299. auto file_buffer = ctx.ReadBuffer();
  300. std::string name = Common::StringFromBuffer(file_buffer);
  301. // TODO(Subv): Implement this filter.
  302. u32 filter_flags = rp.Pop<u32>();
  303. LOG_DEBUG(Service_FS, "called directory {} filter {}", name, filter_flags);
  304. auto result = backend.OpenDirectory(name);
  305. if (result.Failed()) {
  306. IPC::ResponseBuilder rb{ctx, 2};
  307. rb.Push(result.Code());
  308. return;
  309. }
  310. IDirectory directory(result.Unwrap());
  311. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  312. rb.Push(RESULT_SUCCESS);
  313. rb.PushIpcInterface<IDirectory>(std::move(directory));
  314. }
  315. void GetEntryType(Kernel::HLERequestContext& ctx) {
  316. IPC::RequestParser rp{ctx};
  317. auto file_buffer = ctx.ReadBuffer();
  318. std::string name = Common::StringFromBuffer(file_buffer);
  319. LOG_DEBUG(Service_FS, "called file {}", name);
  320. auto result = backend.GetEntryType(name);
  321. if (result.Failed()) {
  322. IPC::ResponseBuilder rb{ctx, 2};
  323. rb.Push(result.Code());
  324. return;
  325. }
  326. IPC::ResponseBuilder rb{ctx, 3};
  327. rb.Push(RESULT_SUCCESS);
  328. rb.Push<u32>(static_cast<u32>(*result));
  329. }
  330. void Commit(Kernel::HLERequestContext& ctx) {
  331. LOG_WARNING(Service_FS, "(STUBBED) called");
  332. IPC::ResponseBuilder rb{ctx, 2};
  333. rb.Push(RESULT_SUCCESS);
  334. }
  335. private:
  336. VfsDirectoryServiceWrapper backend;
  337. };
  338. FSP_SRV::FSP_SRV() : ServiceFramework("fsp-srv") {
  339. static const FunctionInfo functions[] = {
  340. {0, nullptr, "MountContent"},
  341. {1, &FSP_SRV::Initialize, "Initialize"},
  342. {2, nullptr, "OpenDataFileSystemByCurrentProcess"},
  343. {7, &FSP_SRV::OpenFileSystemWithPatch, "OpenFileSystemWithPatch"},
  344. {8, nullptr, "OpenFileSystemWithId"},
  345. {9, nullptr, "OpenDataFileSystemByApplicationId"},
  346. {11, nullptr, "OpenBisFileSystem"},
  347. {12, nullptr, "OpenBisStorage"},
  348. {13, nullptr, "InvalidateBisCache"},
  349. {17, nullptr, "OpenHostFileSystem"},
  350. {18, &FSP_SRV::MountSdCard, "MountSdCard"},
  351. {19, nullptr, "FormatSdCardFileSystem"},
  352. {21, nullptr, "DeleteSaveDataFileSystem"},
  353. {22, &FSP_SRV::CreateSaveData, "CreateSaveData"},
  354. {23, nullptr, "CreateSaveDataFileSystemBySystemSaveDataId"},
  355. {24, nullptr, "RegisterSaveDataFileSystemAtomicDeletion"},
  356. {25, nullptr, "DeleteSaveDataFileSystemBySaveDataSpaceId"},
  357. {26, nullptr, "FormatSdCardDryRun"},
  358. {27, nullptr, "IsExFatSupported"},
  359. {28, nullptr, "DeleteSaveDataFileSystemBySaveDataAttribute"},
  360. {30, nullptr, "OpenGameCardStorage"},
  361. {31, nullptr, "OpenGameCardFileSystem"},
  362. {32, nullptr, "ExtendSaveDataFileSystem"},
  363. {33, nullptr, "DeleteCacheStorage"},
  364. {34, nullptr, "GetCacheStorageSize"},
  365. {51, &FSP_SRV::MountSaveData, "MountSaveData"},
  366. {52, nullptr, "OpenSaveDataFileSystemBySystemSaveDataId"},
  367. {53, &FSP_SRV::OpenReadOnlySaveDataFileSystem, "OpenReadOnlySaveDataFileSystem"},
  368. {57, nullptr, "ReadSaveDataFileSystemExtraDataBySaveDataSpaceId"},
  369. {58, nullptr, "ReadSaveDataFileSystemExtraData"},
  370. {59, nullptr, "WriteSaveDataFileSystemExtraData"},
  371. {60, nullptr, "OpenSaveDataInfoReader"},
  372. {61, nullptr, "OpenSaveDataInfoReaderBySaveDataSpaceId"},
  373. {62, nullptr, "OpenCacheStorageList"},
  374. {64, nullptr, "OpenSaveDataInternalStorageFileSystem"},
  375. {65, nullptr, "UpdateSaveDataMacForDebug"},
  376. {66, nullptr, "WriteSaveDataFileSystemExtraData2"},
  377. {80, nullptr, "OpenSaveDataMetaFile"},
  378. {81, nullptr, "OpenSaveDataTransferManager"},
  379. {82, nullptr, "OpenSaveDataTransferManagerVersion2"},
  380. {83, nullptr, "OpenSaveDataTransferProhibiterForCloudBackUp"},
  381. {100, nullptr, "OpenImageDirectoryFileSystem"},
  382. {110, nullptr, "OpenContentStorageFileSystem"},
  383. {200, &FSP_SRV::OpenDataStorageByCurrentProcess, "OpenDataStorageByCurrentProcess"},
  384. {201, nullptr, "OpenDataStorageByProgramId"},
  385. {202, &FSP_SRV::OpenDataStorageByDataId, "OpenDataStorageByDataId"},
  386. {203, &FSP_SRV::OpenRomStorage, "OpenRomStorage"},
  387. {400, nullptr, "OpenDeviceOperator"},
  388. {500, nullptr, "OpenSdCardDetectionEventNotifier"},
  389. {501, nullptr, "OpenGameCardDetectionEventNotifier"},
  390. {510, nullptr, "OpenSystemDataUpdateEventNotifier"},
  391. {511, nullptr, "NotifySystemDataUpdateEvent"},
  392. {600, nullptr, "SetCurrentPosixTime"},
  393. {601, nullptr, "QuerySaveDataTotalSize"},
  394. {602, nullptr, "VerifySaveDataFileSystem"},
  395. {603, nullptr, "CorruptSaveDataFileSystem"},
  396. {604, nullptr, "CreatePaddingFile"},
  397. {605, nullptr, "DeleteAllPaddingFiles"},
  398. {606, nullptr, "GetRightsId"},
  399. {607, nullptr, "RegisterExternalKey"},
  400. {608, nullptr, "UnregisterAllExternalKey"},
  401. {609, nullptr, "GetRightsIdByPath"},
  402. {610, nullptr, "GetRightsIdAndKeyGenerationByPath"},
  403. {611, nullptr, "SetCurrentPosixTimeWithTimeDifference"},
  404. {612, nullptr, "GetFreeSpaceSizeForSaveData"},
  405. {613, nullptr, "VerifySaveDataFileSystemBySaveDataSpaceId"},
  406. {614, nullptr, "CorruptSaveDataFileSystemBySaveDataSpaceId"},
  407. {615, nullptr, "QuerySaveDataInternalStorageTotalSize"},
  408. {616, nullptr, "GetSaveDataCommitId"},
  409. {620, nullptr, "SetSdCardEncryptionSeed"},
  410. {630, nullptr, "SetSdCardAccessibility"},
  411. {631, nullptr, "IsSdCardAccessible"},
  412. {640, nullptr, "IsSignedSystemPartitionOnSdCardValid"},
  413. {700, nullptr, "OpenAccessFailureResolver"},
  414. {701, nullptr, "GetAccessFailureDetectionEvent"},
  415. {702, nullptr, "IsAccessFailureDetected"},
  416. {710, nullptr, "ResolveAccessFailure"},
  417. {720, nullptr, "AbandonAccessFailure"},
  418. {800, nullptr, "GetAndClearFileSystemProxyErrorInfo"},
  419. {1000, nullptr, "SetBisRootForHost"},
  420. {1001, nullptr, "SetSaveDataSize"},
  421. {1002, nullptr, "SetSaveDataRootPath"},
  422. {1003, nullptr, "DisableAutoSaveDataCreation"},
  423. {1004, nullptr, "SetGlobalAccessLogMode"},
  424. {1005, &FSP_SRV::GetGlobalAccessLogMode, "GetGlobalAccessLogMode"},
  425. {1006, nullptr, "OutputAccessLogToSdCard"},
  426. {1007, nullptr, "RegisterUpdatePartition"},
  427. {1008, nullptr, "OpenRegisteredUpdatePartition"},
  428. {1009, nullptr, "GetAndClearMemoryReportInfo"},
  429. {1100, nullptr, "OverrideSaveDataTransferTokenSignVerificationKey"},
  430. };
  431. RegisterHandlers(functions);
  432. }
  433. FSP_SRV::~FSP_SRV() = default;
  434. void FSP_SRV::Initialize(Kernel::HLERequestContext& ctx) {
  435. LOG_WARNING(Service_FS, "(STUBBED) called");
  436. IPC::ResponseBuilder rb{ctx, 2};
  437. rb.Push(RESULT_SUCCESS);
  438. }
  439. void FSP_SRV::OpenFileSystemWithPatch(Kernel::HLERequestContext& ctx) {
  440. IPC::RequestParser rp{ctx};
  441. const auto type = rp.PopRaw<FileSystemType>();
  442. const auto title_id = rp.PopRaw<u64>();
  443. IPC::ResponseBuilder rb{ctx, 2, 0, 0};
  444. rb.Push(ResultCode(-1));
  445. }
  446. void FSP_SRV::MountSdCard(Kernel::HLERequestContext& ctx) {
  447. LOG_DEBUG(Service_FS, "called");
  448. IFileSystem filesystem(OpenSDMC().Unwrap());
  449. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  450. rb.Push(RESULT_SUCCESS);
  451. rb.PushIpcInterface<IFileSystem>(std::move(filesystem));
  452. }
  453. void FSP_SRV::CreateSaveData(Kernel::HLERequestContext& ctx) {
  454. IPC::RequestParser rp{ctx};
  455. auto save_struct = rp.PopRaw<FileSys::SaveDataDescriptor>();
  456. auto save_create_struct = rp.PopRaw<std::array<u8, 0x40>>();
  457. u128 uid = rp.PopRaw<u128>();
  458. LOG_WARNING(Service_FS, "(STUBBED) called save_struct = {}, uid = {:016X}{:016X}",
  459. save_struct.DebugInfo(), uid[1], uid[0]);
  460. IPC::ResponseBuilder rb{ctx, 2};
  461. rb.Push(RESULT_SUCCESS);
  462. }
  463. void FSP_SRV::MountSaveData(Kernel::HLERequestContext& ctx) {
  464. IPC::RequestParser rp{ctx};
  465. auto space_id = rp.PopRaw<FileSys::SaveDataSpaceId>();
  466. auto unk = rp.Pop<u32>();
  467. LOG_INFO(Service_FS, "called with unknown={:08X}", unk);
  468. auto save_struct = rp.PopRaw<FileSys::SaveDataDescriptor>();
  469. auto dir = OpenSaveData(space_id, save_struct);
  470. if (dir.Failed()) {
  471. IPC::ResponseBuilder rb{ctx, 2, 0, 0};
  472. rb.Push(ResultCode(ErrorModule::FS, FileSys::ErrCodes::TitleNotFound));
  473. return;
  474. }
  475. IFileSystem filesystem(std::move(dir.Unwrap()));
  476. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  477. rb.Push(RESULT_SUCCESS);
  478. rb.PushIpcInterface<IFileSystem>(std::move(filesystem));
  479. }
  480. void FSP_SRV::OpenReadOnlySaveDataFileSystem(Kernel::HLERequestContext& ctx) {
  481. LOG_WARNING(Service_FS, "(STUBBED) called, delegating to 51 OpenSaveDataFilesystem");
  482. MountSaveData(ctx);
  483. }
  484. void FSP_SRV::GetGlobalAccessLogMode(Kernel::HLERequestContext& ctx) {
  485. LOG_WARNING(Service_FS, "(STUBBED) called");
  486. IPC::ResponseBuilder rb{ctx, 3};
  487. rb.Push(RESULT_SUCCESS);
  488. rb.Push<u32>(5);
  489. }
  490. void FSP_SRV::OpenDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx) {
  491. LOG_DEBUG(Service_FS, "called");
  492. auto romfs = OpenRomFSCurrentProcess();
  493. if (romfs.Failed()) {
  494. // TODO (bunnei): Find the right error code to use here
  495. LOG_CRITICAL(Service_FS, "no file system interface available!");
  496. IPC::ResponseBuilder rb{ctx, 2};
  497. rb.Push(ResultCode(-1));
  498. return;
  499. }
  500. IStorage storage(std::move(romfs.Unwrap()));
  501. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  502. rb.Push(RESULT_SUCCESS);
  503. rb.PushIpcInterface<IStorage>(std::move(storage));
  504. }
  505. void FSP_SRV::OpenDataStorageByDataId(Kernel::HLERequestContext& ctx) {
  506. IPC::RequestParser rp{ctx};
  507. const auto storage_id = rp.PopRaw<FileSys::StorageId>();
  508. const auto unknown = rp.PopRaw<u32>();
  509. const auto title_id = rp.PopRaw<u64>();
  510. LOG_DEBUG(Service_FS, "called with storage_id={:02X}, unknown={:08X}, title_id={:016X}",
  511. static_cast<u8>(storage_id), unknown, title_id);
  512. auto data = OpenRomFS(title_id, storage_id, FileSys::ContentRecordType::Data);
  513. if (data.Failed()) {
  514. // TODO(DarkLordZach): Find the right error code to use here
  515. LOG_ERROR(Service_FS,
  516. "could not open data storage with title_id={:016X}, storage_id={:02X}", title_id,
  517. static_cast<u8>(storage_id));
  518. IPC::ResponseBuilder rb{ctx, 2};
  519. rb.Push(ResultCode(-1));
  520. return;
  521. }
  522. FileSys::PatchManager pm{title_id};
  523. IStorage storage(pm.PatchRomFS(std::move(data.Unwrap()), 0, FileSys::ContentRecordType::Data));
  524. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  525. rb.Push(RESULT_SUCCESS);
  526. rb.PushIpcInterface<IStorage>(std::move(storage));
  527. }
  528. void FSP_SRV::OpenRomStorage(Kernel::HLERequestContext& ctx) {
  529. IPC::RequestParser rp{ctx};
  530. auto storage_id = rp.PopRaw<FileSys::StorageId>();
  531. auto title_id = rp.PopRaw<u64>();
  532. LOG_DEBUG(Service_FS, "called with storage_id={:02X}, title_id={:016X}",
  533. static_cast<u8>(storage_id), title_id);
  534. IPC::ResponseBuilder rb{ctx, 2};
  535. rb.Push(ResultCode(ErrorModule::FS, FileSys::ErrCodes::TitleNotFound));
  536. }
  537. } // namespace Service::FileSystem